[official-gcc.git] / gcc / rtlanal.c
blob031d103b34e3eff73b13222a7eabb78a1d52a2fe
1 /* Analyze RTL for C-Compiler
2 Copyright (C) 1987, 88, 92-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
26 static int rtx_addr_can_trap_p PROTO((rtx));
27 static void reg_set_p_1 PROTO((rtx, rtx));
28 static void reg_set_last_1 PROTO((rtx, rtx));
31 /* Forward declarations */
32 static int jmp_uses_reg_or_mem PROTO((rtx));
34 /* Bit flags that specify the machine subtype we are compiling for.
35 Bits are tested using macros TARGET_... defined in the tm.h file
36 and set by `-m...' switches. Must be defined in rtlanal.c. */
38 int target_flags;
40 /* Return 1 if the value of X is unstable
41 (would be different at a different point in the program).
42 The frame pointer, arg pointer, etc. are considered stable
43 (within one function) and so is anything marked `unchanging'. */
45 int
46 rtx_unstable_p (x)
47 rtx x;
49 register RTX_CODE code = GET_CODE (x);
50 register int i;
51 register char *fmt;
53 if (code == MEM)
54 return ! RTX_UNCHANGING_P (x);
56 if (code == QUEUED)
57 return 1;
59 if (code == CONST || code == CONST_INT)
60 return 0;
62 if (code == REG)
63 return ! (REGNO (x) == FRAME_POINTER_REGNUM
64 || REGNO (x) == HARD_FRAME_POINTER_REGNUM
65 || REGNO (x) == ARG_POINTER_REGNUM
66 || RTX_UNCHANGING_P (x));
68 fmt = GET_RTX_FORMAT (code);
69 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
70 if (fmt[i] == 'e')
71 if (rtx_unstable_p (XEXP (x, i)))
72 return 1;
73 return 0;
76 /* Return 1 if X has a value that can vary even between two
77 executions of the program. 0 means X can be compared reliably
78 against certain constants or near-constants.
79 The frame pointer and the arg pointer are considered constant. */
81 int
82 rtx_varies_p (x)
83 rtx x;
85 register RTX_CODE code = GET_CODE (x);
86 register int i;
87 register char *fmt;
89 switch (code)
91 case MEM:
92 case QUEUED:
93 return 1;
95 case CONST:
96 case CONST_INT:
97 case CONST_DOUBLE:
98 case SYMBOL_REF:
99 case LABEL_REF:
100 return 0;
102 case REG:
103 /* Note that we have to test for the actual rtx used for the frame
104 and arg pointers and not just the register number in case we have
105 eliminated the frame and/or arg pointer and are using it
106 for pseudos. */
107 return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
108 || x == arg_pointer_rtx || x == pic_offset_table_rtx);
110 case LO_SUM:
111 /* The operand 0 of a LO_SUM is considered constant
112 (in fact is it related specifically to operand 1). */
113 return rtx_varies_p (XEXP (x, 1));
115 default:
116 break;
119 fmt = GET_RTX_FORMAT (code);
120 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
121 if (fmt[i] == 'e')
122 if (rtx_varies_p (XEXP (x, i)))
123 return 1;
124 return 0;
127 /* Return 0 if the use of X as an address in a MEM can cause a trap. */
129 static int
130 rtx_addr_can_trap_p (x)
131 register rtx x;
133 register enum rtx_code code = GET_CODE (x);
135 switch (code)
137 case SYMBOL_REF:
138 case LABEL_REF:
139 /* SYMBOL_REF is problematic due to the possible presence of
140 a #pragma weak, but to say that loads from symbols can trap is
141 *very* costly. It's not at all clear what's best here. For
142 now, we ignore the impact of #pragma weak. */
143 return 0;
145 case REG:
146 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
147 return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
148 || x == stack_pointer_rtx || x == arg_pointer_rtx);
150 case CONST:
151 return rtx_addr_can_trap_p (XEXP (x, 0));
153 case PLUS:
154 /* An address is assumed not to trap if it is an address that can't
155 trap plus a constant integer. */
156 return (rtx_addr_can_trap_p (XEXP (x, 0))
157 || GET_CODE (XEXP (x, 1)) != CONST_INT);
159 case LO_SUM:
160 return rtx_addr_can_trap_p (XEXP (x, 1));
162 default:
163 break;
166 /* If it isn't one of the case above, it can cause a trap. */
167 return 1;
170 /* Return 1 if X refers to a memory location whose address
171 cannot be compared reliably with constant addresses,
172 or if X refers to a BLKmode memory object. */
175 rtx_addr_varies_p (x)
176 rtx x;
178 register enum rtx_code code;
179 register int i;
180 register char *fmt;
182 if (x == 0)
183 return 0;
185 code = GET_CODE (x);
186 if (code == MEM)
187 return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0));
189 fmt = GET_RTX_FORMAT (code);
190 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
191 if (fmt[i] == 'e')
193 if (rtx_addr_varies_p (XEXP (x, i)))
194 return 1;
196 else if (fmt[i] == 'E')
198 int j;
199 for (j = 0; j < XVECLEN (x, i); j++)
200 if (rtx_addr_varies_p (XVECEXP (x, i, j)))
201 return 1;
203 return 0;
206 /* Return the value of the integer term in X, if one is apparent;
207 otherwise return 0.
208 Only obvious integer terms are detected.
209 This is used in cse.c with the `related_value' field.*/
211 HOST_WIDE_INT
212 get_integer_term (x)
213 rtx x;
215 if (GET_CODE (x) == CONST)
216 x = XEXP (x, 0);
218 if (GET_CODE (x) == MINUS
219 && GET_CODE (XEXP (x, 1)) == CONST_INT)
220 return - INTVAL (XEXP (x, 1));
221 if (GET_CODE (x) == PLUS
222 && GET_CODE (XEXP (x, 1)) == CONST_INT)
223 return INTVAL (XEXP (x, 1));
224 return 0;
227 /* If X is a constant, return the value sans apparent integer term;
228 otherwise return 0.
229 Only obvious integer terms are detected. */
232 get_related_value (x)
233 rtx x;
235 if (GET_CODE (x) != CONST)
236 return 0;
237 x = XEXP (x, 0);
238 if (GET_CODE (x) == PLUS
239 && GET_CODE (XEXP (x, 1)) == CONST_INT)
240 return XEXP (x, 0);
241 else if (GET_CODE (x) == MINUS
242 && GET_CODE (XEXP (x, 1)) == CONST_INT)
243 return XEXP (x, 0);
244 return 0;
247 /* Nonzero if register REG appears somewhere within IN.
248 Also works if REG is not a register; in this case it checks
249 for a subexpression of IN that is Lisp "equal" to REG. */
252 reg_mentioned_p (reg, in)
253 register rtx reg, in;
255 register char *fmt;
256 register int i;
257 register enum rtx_code code;
259 if (in == 0)
260 return 0;
262 if (reg == in)
263 return 1;
265 if (GET_CODE (in) == LABEL_REF)
266 return reg == XEXP (in, 0);
268 code = GET_CODE (in);
270 switch (code)
272 /* Compare registers by number. */
273 case REG:
274 return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
276 /* These codes have no constituent expressions
277 and are unique. */
278 case SCRATCH:
279 case CC0:
280 case PC:
281 return 0;
283 case CONST_INT:
284 return GET_CODE (reg) == CONST_INT && INTVAL (in) == INTVAL (reg);
286 case CONST_DOUBLE:
287 /* These are kept unique for a given value. */
288 return 0;
290 default:
291 break;
294 if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
295 return 1;
297 fmt = GET_RTX_FORMAT (code);
299 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
301 if (fmt[i] == 'E')
303 register int j;
304 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
305 if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
306 return 1;
308 else if (fmt[i] == 'e'
309 && reg_mentioned_p (reg, XEXP (in, i)))
310 return 1;
312 return 0;
315 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
316 no CODE_LABEL insn. */
319 no_labels_between_p (beg, end)
320 rtx beg, end;
322 register rtx p;
323 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
324 if (GET_CODE (p) == CODE_LABEL)
325 return 0;
326 return 1;
329 /* Nonzero if register REG is used in an insn between
330 FROM_INSN and TO_INSN (exclusive of those two). */
333 reg_used_between_p (reg, from_insn, to_insn)
334 rtx reg, from_insn, to_insn;
336 register rtx insn;
338 if (from_insn == to_insn)
339 return 0;
341 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
342 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
343 && (reg_overlap_mentioned_p (reg, PATTERN (insn))
344 || (GET_CODE (insn) == CALL_INSN
345 && (find_reg_fusage (insn, USE, reg)
346 || find_reg_fusage (insn, CLOBBER, reg)))))
347 return 1;
348 return 0;
351 /* Nonzero if the old value of X, a register, is referenced in BODY. If X
352 is entirely replaced by a new value and the only use is as a SET_DEST,
353 we do not consider it a reference. */
356 reg_referenced_p (x, body)
357 rtx x;
358 rtx body;
360 int i;
362 switch (GET_CODE (body))
364 case SET:
365 if (reg_overlap_mentioned_p (x, SET_SRC (body)))
366 return 1;
368 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
369 of a REG that occupies all of the REG, the insn references X if
370 it is mentioned in the destination. */
371 if (GET_CODE (SET_DEST (body)) != CC0
372 && GET_CODE (SET_DEST (body)) != PC
373 && GET_CODE (SET_DEST (body)) != REG
374 && ! (GET_CODE (SET_DEST (body)) == SUBREG
375 && GET_CODE (SUBREG_REG (SET_DEST (body))) == REG
376 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
377 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
378 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
379 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
380 && reg_overlap_mentioned_p (x, SET_DEST (body)))
381 return 1;
382 return 0;
384 case ASM_OPERANDS:
385 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
386 if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
387 return 1;
388 return 0;
390 case CALL:
391 case USE:
392 return reg_overlap_mentioned_p (x, body);
394 case TRAP_IF:
395 return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
397 case UNSPEC:
398 case UNSPEC_VOLATILE:
399 case PARALLEL:
400 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
401 if (reg_referenced_p (x, XVECEXP (body, 0, i)))
402 return 1;
403 return 0;
405 default:
406 return 0;
410 /* Nonzero if register REG is referenced in an insn between
411 FROM_INSN and TO_INSN (exclusive of those two). Sets of REG do
412 not count. */
415 reg_referenced_between_p (reg, from_insn, to_insn)
416 rtx reg, from_insn, to_insn;
418 register rtx insn;
420 if (from_insn == to_insn)
421 return 0;
423 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
424 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
425 && (reg_referenced_p (reg, PATTERN (insn))
426 || (GET_CODE (insn) == CALL_INSN
427 && find_reg_fusage (insn, USE, reg))))
428 return 1;
429 return 0;
432 /* Nonzero if register REG is set or clobbered in an insn between
433 FROM_INSN and TO_INSN (exclusive of those two). */
436 reg_set_between_p (reg, from_insn, to_insn)
437 rtx reg, from_insn, to_insn;
439 register rtx insn;
441 if (from_insn == to_insn)
442 return 0;
444 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
445 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
446 && reg_set_p (reg, insn))
447 return 1;
448 return 0;
451 /* Internals of reg_set_between_p. */
453 static rtx reg_set_reg;
454 static int reg_set_flag;
456 static void
457 reg_set_p_1 (x, pat)
458 rtx x;
459 rtx pat ATTRIBUTE_UNUSED;
461 /* We don't want to return 1 if X is a MEM that contains a register
462 within REG_SET_REG. */
464 if ((GET_CODE (x) != MEM)
465 && reg_overlap_mentioned_p (reg_set_reg, x))
466 reg_set_flag = 1;
470 reg_set_p (reg, insn)
471 rtx reg, insn;
473 rtx body = insn;
475 /* We can be passed an insn or part of one. If we are passed an insn,
476 check if a side-effect of the insn clobbers REG. */
477 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
479 if (FIND_REG_INC_NOTE (insn, reg)
480 || (GET_CODE (insn) == CALL_INSN
481 /* We'd like to test call_used_regs here, but rtlanal.c can't
482 reference that variable due to its use in genattrtab. So
483 we'll just be more conservative.
485 ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
486 information holds all clobbered registers. */
487 && ((GET_CODE (reg) == REG
488 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
489 || GET_CODE (reg) == MEM
490 || find_reg_fusage (insn, CLOBBER, reg))))
491 return 1;
493 body = PATTERN (insn);
496 reg_set_reg = reg;
497 reg_set_flag = 0;
498 note_stores (body, reg_set_p_1);
499 return reg_set_flag;
502 /* Similar to reg_set_between_p, but check all registers in X. Return 0
503 only if none of them are modified between START and END. Return 1 if
504 X contains a MEM; this routine does not perform any memory aliasing. */
507 modified_between_p (x, start, end)
508 rtx x;
509 rtx start, end;
511 enum rtx_code code = GET_CODE (x);
512 char *fmt;
513 int i, j;
515 switch (code)
517 case CONST_INT:
518 case CONST_DOUBLE:
519 case CONST:
520 case SYMBOL_REF:
521 case LABEL_REF:
522 return 0;
524 case PC:
525 case CC0:
526 return 1;
528 case MEM:
529 /* If the memory is not constant, assume it is modified. If it is
530 constant, we still have to check the address. */
531 if (! RTX_UNCHANGING_P (x))
532 return 1;
533 break;
535 case REG:
536 return reg_set_between_p (x, start, end);
538 default:
539 break;
542 fmt = GET_RTX_FORMAT (code);
543 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
545 if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
546 return 1;
548 if (fmt[i] == 'E')
549 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
550 if (modified_between_p (XVECEXP (x, i, j), start, end))
551 return 1;
554 return 0;
557 /* Similar to reg_set_p, but check all registers in X. Return 0 only if none
558 of them are modified in INSN. Return 1 if X contains a MEM; this routine
559 does not perform any memory aliasing. */
562 modified_in_p (x, insn)
563 rtx x;
564 rtx insn;
566 enum rtx_code code = GET_CODE (x);
567 char *fmt;
568 int i, j;
570 switch (code)
572 case CONST_INT:
573 case CONST_DOUBLE:
574 case CONST:
575 case SYMBOL_REF:
576 case LABEL_REF:
577 return 0;
579 case PC:
580 case CC0:
581 return 1;
583 case MEM:
584 /* If the memory is not constant, assume it is modified. If it is
585 constant, we still have to check the address. */
586 if (! RTX_UNCHANGING_P (x))
587 return 1;
588 break;
590 case REG:
591 return reg_set_p (x, insn);
593 default:
594 break;
597 fmt = GET_RTX_FORMAT (code);
598 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
600 if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
601 return 1;
603 if (fmt[i] == 'E')
604 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
605 if (modified_in_p (XVECEXP (x, i, j), insn))
606 return 1;
609 return 0;
612 /* Given an INSN, return a SET expression if this insn has only a single SET.
613 It may also have CLOBBERs, USEs, or SET whose output
614 will not be used, which we ignore. */
617 single_set (insn)
618 rtx insn;
620 rtx set;
621 int i;
623 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
624 return 0;
626 if (GET_CODE (PATTERN (insn)) == SET)
627 return PATTERN (insn);
629 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
631 for (i = 0, set = 0; i < XVECLEN (PATTERN (insn), 0); i++)
632 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
633 && (! find_reg_note (insn, REG_UNUSED,
634 SET_DEST (XVECEXP (PATTERN (insn), 0, i)))
635 || side_effects_p (XVECEXP (PATTERN (insn), 0, i))))
637 if (set)
638 return 0;
639 else
640 set = XVECEXP (PATTERN (insn), 0, i);
642 return set;
645 return 0;
648 /* Return the last thing that X was assigned from before *PINSN. Verify that
649 the object is not modified up to VALID_TO. If it was, if we hit
650 a partial assignment to X, or hit a CODE_LABEL first, return X. If we
651 found an assignment, update *PINSN to point to it. */
654 find_last_value (x, pinsn, valid_to)
655 rtx x;
656 rtx *pinsn;
657 rtx valid_to;
659 rtx p;
661 for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
662 p = PREV_INSN (p))
663 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
665 rtx set = single_set (p);
666 rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
668 if (set && rtx_equal_p (x, SET_DEST (set)))
670 rtx src = SET_SRC (set);
672 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
673 src = XEXP (note, 0);
675 if (! modified_between_p (src, PREV_INSN (p), valid_to)
676 /* Reject hard registers because we don't usually want
677 to use them; we'd rather use a pseudo. */
678 && ! (GET_CODE (src) == REG
679 && REGNO (src) < FIRST_PSEUDO_REGISTER))
681 *pinsn = p;
682 return src;
686 /* If set in non-simple way, we don't have a value. */
687 if (reg_set_p (x, p))
688 break;
691 return x;
694 /* Return nonzero if register in range [REGNO, ENDREGNO)
695 appears either explicitly or implicitly in X
696 other than being stored into.
698 References contained within the substructure at LOC do not count.
699 LOC may be zero, meaning don't ignore anything. */
702 refers_to_regno_p (regno, endregno, x, loc)
703 int regno, endregno;
704 rtx x;
705 rtx *loc;
707 register int i;
708 register RTX_CODE code;
709 register char *fmt;
711 repeat:
712 /* The contents of a REG_NONNEG note is always zero, so we must come here
713 upon repeat in case the last REG_NOTE is a REG_NONNEG note. */
714 if (x == 0)
715 return 0;
717 code = GET_CODE (x);
719 switch (code)
721 case REG:
722 i = REGNO (x);
724 /* If we modifying the stack, frame, or argument pointer, it will
725 clobber a virtual register. In fact, we could be more precise,
726 but it isn't worth it. */
727 if ((i == STACK_POINTER_REGNUM
728 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
729 || i == ARG_POINTER_REGNUM
730 #endif
731 || i == FRAME_POINTER_REGNUM)
732 && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
733 return 1;
735 return (endregno > i
736 && regno < i + (i < FIRST_PSEUDO_REGISTER
737 ? HARD_REGNO_NREGS (i, GET_MODE (x))
738 : 1));
740 case SUBREG:
741 /* If this is a SUBREG of a hard reg, we can see exactly which
742 registers are being modified. Otherwise, handle normally. */
743 if (GET_CODE (SUBREG_REG (x)) == REG
744 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
746 int inner_regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
747 int inner_endregno
748 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
749 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
751 return endregno > inner_regno && regno < inner_endregno;
753 break;
755 case CLOBBER:
756 case SET:
757 if (&SET_DEST (x) != loc
758 /* Note setting a SUBREG counts as referring to the REG it is in for
759 a pseudo but not for hard registers since we can
760 treat each word individually. */
761 && ((GET_CODE (SET_DEST (x)) == SUBREG
762 && loc != &SUBREG_REG (SET_DEST (x))
763 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
764 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
765 && refers_to_regno_p (regno, endregno,
766 SUBREG_REG (SET_DEST (x)), loc))
767 || (GET_CODE (SET_DEST (x)) != REG
768 && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
769 return 1;
771 if (code == CLOBBER || loc == &SET_SRC (x))
772 return 0;
773 x = SET_SRC (x);
774 goto repeat;
776 default:
777 break;
780 /* X does not match, so try its subexpressions. */
782 fmt = GET_RTX_FORMAT (code);
783 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
785 if (fmt[i] == 'e' && loc != &XEXP (x, i))
787 if (i == 0)
789 x = XEXP (x, 0);
790 goto repeat;
792 else
793 if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
794 return 1;
796 else if (fmt[i] == 'E')
798 register int j;
799 for (j = XVECLEN (x, i) - 1; j >=0; j--)
800 if (loc != &XVECEXP (x, i, j)
801 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
802 return 1;
805 return 0;
808 /* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
809 we check if any register number in X conflicts with the relevant register
810 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
811 contains a MEM (we don't bother checking for memory addresses that can't
812 conflict because we expect this to be a rare case. */
815 reg_overlap_mentioned_p (x, in)
816 rtx x, in;
818 int regno, endregno;
820 /* Overly conservative. */
821 if (GET_CODE (x) == STRICT_LOW_PART)
822 x = XEXP (x, 0);
824 /* If either argument is a constant, then modifying X can not affect IN. */
825 if (CONSTANT_P (x) || CONSTANT_P (in))
826 return 0;
827 else if (GET_CODE (x) == SUBREG)
829 regno = REGNO (SUBREG_REG (x));
830 if (regno < FIRST_PSEUDO_REGISTER)
831 regno += SUBREG_WORD (x);
833 else if (GET_CODE (x) == REG)
834 regno = REGNO (x);
835 else if (GET_CODE (x) == MEM)
837 char *fmt;
838 int i;
840 if (GET_CODE (in) == MEM)
841 return 1;
843 fmt = GET_RTX_FORMAT (GET_CODE (in));
845 for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
846 if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
847 return 1;
849 return 0;
851 else if (GET_CODE (x) == SCRATCH || GET_CODE (x) == PC
852 || GET_CODE (x) == CC0)
853 return reg_mentioned_p (x, in);
854 else if (GET_CODE (x) == PARALLEL
855 && GET_MODE (x) == BLKmode)
857 register int i;
859 /* If any register in here refers to it
860 we return true. */
861 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
862 if (reg_overlap_mentioned_p (SET_DEST (XVECEXP (x, 0, i)), in))
863 return 1;
864 return 0;
866 else
867 abort ();
869 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
870 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
872 return refers_to_regno_p (regno, endregno, in, NULL_PTR);
875 /* Used for communications between the next few functions. */
877 static int reg_set_last_unknown;
878 static rtx reg_set_last_value;
879 static int reg_set_last_first_regno, reg_set_last_last_regno;
881 /* Called via note_stores from reg_set_last. */
883 static void
884 reg_set_last_1 (x, pat)
885 rtx x;
886 rtx pat;
888 int first, last;
890 /* If X is not a register, or is not one in the range we care
891 about, ignore. */
892 if (GET_CODE (x) != REG)
893 return;
895 first = REGNO (x);
896 last = first + (first < FIRST_PSEUDO_REGISTER
897 ? HARD_REGNO_NREGS (first, GET_MODE (x)) : 1);
899 if (first >= reg_set_last_last_regno
900 || last <= reg_set_last_first_regno)
901 return;
903 /* If this is a CLOBBER or is some complex LHS, or doesn't modify
904 exactly the registers we care about, show we don't know the value. */
905 if (GET_CODE (pat) == CLOBBER || SET_DEST (pat) != x
906 || first != reg_set_last_first_regno
907 || last != reg_set_last_last_regno)
908 reg_set_last_unknown = 1;
909 else
910 reg_set_last_value = SET_SRC (pat);
913 /* Return the last value to which REG was set prior to INSN. If we can't
914 find it easily, return 0.
916 We only return a REG, SUBREG, or constant because it is too hard to
917 check if a MEM remains unchanged. */
920 reg_set_last (x, insn)
921 rtx x;
922 rtx insn;
924 rtx orig_insn = insn;
926 reg_set_last_first_regno = REGNO (x);
928 reg_set_last_last_regno
929 = reg_set_last_first_regno
930 + (reg_set_last_first_regno < FIRST_PSEUDO_REGISTER
931 ? HARD_REGNO_NREGS (reg_set_last_first_regno, GET_MODE (x)) : 1);
933 reg_set_last_unknown = 0;
934 reg_set_last_value = 0;
936 /* Scan backwards until reg_set_last_1 changed one of the above flags.
937 Stop when we reach a label or X is a hard reg and we reach a
938 CALL_INSN (if reg_set_last_last_regno is a hard reg).
940 If we find a set of X, ensure that its SET_SRC remains unchanged. */
942 /* We compare with <= here, because reg_set_last_last_regno
943 is actually the number of the first reg *not* in X. */
944 for (;
945 insn && GET_CODE (insn) != CODE_LABEL
946 && ! (GET_CODE (insn) == CALL_INSN
947 && reg_set_last_last_regno <= FIRST_PSEUDO_REGISTER);
948 insn = PREV_INSN (insn))
949 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
951 note_stores (PATTERN (insn), reg_set_last_1);
952 if (reg_set_last_unknown)
953 return 0;
954 else if (reg_set_last_value)
956 if (CONSTANT_P (reg_set_last_value)
957 || ((GET_CODE (reg_set_last_value) == REG
958 || GET_CODE (reg_set_last_value) == SUBREG)
959 && ! reg_set_between_p (reg_set_last_value,
960 insn, orig_insn)))
961 return reg_set_last_value;
962 else
963 return 0;
967 return 0;
970 /* This is 1 until after the rtl generation pass. */
971 int rtx_equal_function_value_matters;
973 /* Return 1 if X and Y are identical-looking rtx's.
974 This is the Lisp function EQUAL for rtx arguments. */
977 rtx_equal_p (x, y)
978 rtx x, y;
980 register int i;
981 register int j;
982 register enum rtx_code code;
983 register char *fmt;
985 if (x == y)
986 return 1;
987 if (x == 0 || y == 0)
988 return 0;
990 code = GET_CODE (x);
991 /* Rtx's of different codes cannot be equal. */
992 if (code != GET_CODE (y))
993 return 0;
995 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
996 (REG:SI x) and (REG:HI x) are NOT equivalent. */
998 if (GET_MODE (x) != GET_MODE (y))
999 return 0;
1001 /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively. */
1003 if (code == REG)
1004 /* Until rtl generation is complete, don't consider a reference to the
1005 return register of the current function the same as the return from a
1006 called function. This eases the job of function integration. Once the
1007 distinction is no longer needed, they can be considered equivalent. */
1008 return (REGNO (x) == REGNO (y)
1009 && (! rtx_equal_function_value_matters
1010 || REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y)));
1011 else if (code == LABEL_REF)
1012 return XEXP (x, 0) == XEXP (y, 0);
1013 else if (code == SYMBOL_REF)
1014 return XSTR (x, 0) == XSTR (y, 0);
1015 else if (code == SCRATCH || code == CONST_DOUBLE)
1016 return 0;
1018 /* Compare the elements. If any pair of corresponding elements
1019 fail to match, return 0 for the whole things. */
1021 fmt = GET_RTX_FORMAT (code);
1022 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1024 switch (fmt[i])
1026 case 'w':
1027 if (XWINT (x, i) != XWINT (y, i))
1028 return 0;
1029 break;
1031 case 'n':
1032 case 'i':
1033 if (XINT (x, i) != XINT (y, i))
1034 return 0;
1035 break;
1037 case 'V':
1038 case 'E':
1039 /* Two vectors must have the same length. */
1040 if (XVECLEN (x, i) != XVECLEN (y, i))
1041 return 0;
1043 /* And the corresponding elements must match. */
1044 for (j = 0; j < XVECLEN (x, i); j++)
1045 if (rtx_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
1046 return 0;
1047 break;
1049 case 'e':
1050 if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
1051 return 0;
1052 break;
1054 case 'S':
1055 case 's':
1056 if (strcmp (XSTR (x, i), XSTR (y, i)))
1057 return 0;
1058 break;
1060 case 'u':
1061 /* These are just backpointers, so they don't matter. */
1062 break;
1064 case '0':
1065 break;
1067 /* It is believed that rtx's at this level will never
1068 contain anything but integers and other rtx's,
1069 except for within LABEL_REFs and SYMBOL_REFs. */
1070 default:
1071 abort ();
1074 return 1;
1077 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1078 (X would be the pattern of an insn).
1079 FUN receives two arguments:
1080 the REG, MEM, CC0 or PC being stored in or clobbered,
1081 the SET or CLOBBER rtx that does the store.
1083 If the item being stored in or clobbered is a SUBREG of a hard register,
1084 the SUBREG will be passed. */
1086 void
1087 note_stores (x, fun)
1088 register rtx x;
1089 void (*fun) ();
1091 if ((GET_CODE (x) == SET || GET_CODE (x) == CLOBBER))
1093 register rtx dest = SET_DEST (x);
1094 while ((GET_CODE (dest) == SUBREG
1095 && (GET_CODE (SUBREG_REG (dest)) != REG
1096 || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1097 || GET_CODE (dest) == ZERO_EXTRACT
1098 || GET_CODE (dest) == SIGN_EXTRACT
1099 || GET_CODE (dest) == STRICT_LOW_PART)
1100 dest = XEXP (dest, 0);
1102 if (GET_CODE (dest) == PARALLEL
1103 && GET_MODE (dest) == BLKmode)
1105 register int i;
1106 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1107 (*fun) (SET_DEST (XVECEXP (dest, 0, i)), x);
1109 else
1110 (*fun) (dest, x);
1112 else if (GET_CODE (x) == PARALLEL)
1114 register int i;
1115 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1117 register rtx y = XVECEXP (x, 0, i);
1118 if (GET_CODE (y) == SET || GET_CODE (y) == CLOBBER)
1120 register rtx dest = SET_DEST (y);
1121 while ((GET_CODE (dest) == SUBREG
1122 && (GET_CODE (SUBREG_REG (dest)) != REG
1123 || (REGNO (SUBREG_REG (dest))
1124 >= FIRST_PSEUDO_REGISTER)))
1125 || GET_CODE (dest) == ZERO_EXTRACT
1126 || GET_CODE (dest) == SIGN_EXTRACT
1127 || GET_CODE (dest) == STRICT_LOW_PART)
1128 dest = XEXP (dest, 0);
1129 if (GET_CODE (dest) == PARALLEL
1130 && GET_MODE (dest) == BLKmode)
1132 register int i;
1133 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1134 (*fun) (SET_DEST (XVECEXP (dest, 0, i)), y);
1136 else
1137 (*fun) (dest, y);
1143 /* Return nonzero if X's old contents don't survive after INSN.
1144 This will be true if X is (cc0) or if X is a register and
1145 X dies in INSN or because INSN entirely sets X.
1147 "Entirely set" means set directly and not through a SUBREG,
1148 ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1149 Likewise, REG_INC does not count.
1151 REG may be a hard or pseudo reg. Renumbering is not taken into account,
1152 but for this use that makes no difference, since regs don't overlap
1153 during their lifetimes. Therefore, this function may be used
1154 at any time after deaths have been computed (in flow.c).
1156 If REG is a hard reg that occupies multiple machine registers, this
1157 function will only return 1 if each of those registers will be replaced
1158 by INSN. */
1161 dead_or_set_p (insn, x)
1162 rtx insn;
1163 rtx x;
1165 register int regno, last_regno;
1166 register int i;
1168 /* Can't use cc0_rtx below since this file is used by genattrtab.c. */
1169 if (GET_CODE (x) == CC0)
1170 return 1;
1172 if (GET_CODE (x) != REG)
1173 abort ();
1175 regno = REGNO (x);
1176 last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1177 : regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1179 for (i = regno; i <= last_regno; i++)
1180 if (! dead_or_set_regno_p (insn, i))
1181 return 0;
1183 return 1;
1186 /* Utility function for dead_or_set_p to check an individual register. Also
1187 called from flow.c. */
1190 dead_or_set_regno_p (insn, test_regno)
1191 rtx insn;
1192 int test_regno;
1194 int regno, endregno;
1195 rtx link;
1197 /* See if there is a death note for something that includes
1198 TEST_REGNO. */
1199 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1201 if (REG_NOTE_KIND (link) != REG_DEAD
1202 || GET_CODE (XEXP (link, 0)) != REG)
1203 continue;
1205 regno = REGNO (XEXP (link, 0));
1206 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1207 : regno + HARD_REGNO_NREGS (regno,
1208 GET_MODE (XEXP (link, 0))));
1210 if (test_regno >= regno && test_regno < endregno)
1211 return 1;
1214 if (GET_CODE (insn) == CALL_INSN
1215 && find_regno_fusage (insn, CLOBBER, test_regno))
1216 return 1;
1218 if (GET_CODE (PATTERN (insn)) == SET)
1220 rtx dest = SET_DEST (PATTERN (insn));
1222 /* A value is totally replaced if it is the destination or the
1223 destination is a SUBREG of REGNO that does not change the number of
1224 words in it. */
1225 if (GET_CODE (dest) == SUBREG
1226 && (((GET_MODE_SIZE (GET_MODE (dest))
1227 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1228 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1229 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1230 dest = SUBREG_REG (dest);
1232 if (GET_CODE (dest) != REG)
1233 return 0;
1235 regno = REGNO (dest);
1236 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1237 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1239 return (test_regno >= regno && test_regno < endregno);
1241 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1243 register int i;
1245 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1247 rtx body = XVECEXP (PATTERN (insn), 0, i);
1249 if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1251 rtx dest = SET_DEST (body);
1253 if (GET_CODE (dest) == SUBREG
1254 && (((GET_MODE_SIZE (GET_MODE (dest))
1255 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1256 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1257 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1258 dest = SUBREG_REG (dest);
1260 if (GET_CODE (dest) != REG)
1261 continue;
1263 regno = REGNO (dest);
1264 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1265 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1267 if (test_regno >= regno && test_regno < endregno)
1268 return 1;
1273 return 0;
1276 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1277 If DATUM is nonzero, look for one whose datum is DATUM. */
1280 find_reg_note (insn, kind, datum)
1281 rtx insn;
1282 enum reg_note kind;
1283 rtx datum;
1285 register rtx link;
1287 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1288 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
1289 return 0;
1291 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1292 if (REG_NOTE_KIND (link) == kind
1293 && (datum == 0 || datum == XEXP (link, 0)))
1294 return link;
1295 return 0;
1298 /* Return the reg-note of kind KIND in insn INSN which applies to register
1299 number REGNO, if any. Return 0 if there is no such reg-note. Note that
1300 the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1301 it might be the case that the note overlaps REGNO. */
1304 find_regno_note (insn, kind, regno)
1305 rtx insn;
1306 enum reg_note kind;
1307 int regno;
1309 register rtx link;
1311 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1312 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
1313 return 0;
1315 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1316 if (REG_NOTE_KIND (link) == kind
1317 /* Verify that it is a register, so that scratch and MEM won't cause a
1318 problem here. */
1319 && GET_CODE (XEXP (link, 0)) == REG
1320 && REGNO (XEXP (link, 0)) <= regno
1321 && ((REGNO (XEXP (link, 0))
1322 + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1323 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1324 GET_MODE (XEXP (link, 0)))))
1325 > regno))
1326 return link;
1327 return 0;
1330 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1331 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1334 find_reg_fusage (insn, code, datum)
1335 rtx insn;
1336 enum rtx_code code;
1337 rtx datum;
1339 /* If it's not a CALL_INSN, it can't possibly have a
1340 CALL_INSN_FUNCTION_USAGE field, so don't bother checking. */
1341 if (GET_CODE (insn) != CALL_INSN)
1342 return 0;
1344 if (! datum)
1345 abort();
1347 if (GET_CODE (datum) != REG)
1349 register rtx link;
1351 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1352 link;
1353 link = XEXP (link, 1))
1354 if (GET_CODE (XEXP (link, 0)) == code
1355 && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1356 return 1;
1358 else
1360 register int regno = REGNO (datum);
1362 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1363 to pseudo registers, so don't bother checking. */
1365 if (regno < FIRST_PSEUDO_REGISTER)
1367 int end_regno = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1368 int i;
1370 for (i = regno; i < end_regno; i++)
1371 if (find_regno_fusage (insn, code, i))
1372 return 1;
1376 return 0;
1379 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1380 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1383 find_regno_fusage (insn, code, regno)
1384 rtx insn;
1385 enum rtx_code code;
1386 int regno;
1388 register rtx link;
1390 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1391 to pseudo registers, so don't bother checking. */
1393 if (regno >= FIRST_PSEUDO_REGISTER
1394 || GET_CODE (insn) != CALL_INSN )
1395 return 0;
1397 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1399 register int regnote;
1400 register rtx op;
1402 if (GET_CODE (op = XEXP (link, 0)) == code
1403 && GET_CODE (SET_DEST (op)) == REG
1404 && (regnote = REGNO (SET_DEST (op))) <= regno
1405 && regnote
1406 + HARD_REGNO_NREGS (regnote, GET_MODE (SET_DEST (op)))
1407 > regno)
1408 return 1;
1411 return 0;
1414 /* Remove register note NOTE from the REG_NOTES of INSN. */
1416 void
1417 remove_note (insn, note)
1418 register rtx note;
1419 register rtx insn;
1421 register rtx link;
1423 if (REG_NOTES (insn) == note)
1425 REG_NOTES (insn) = XEXP (note, 1);
1426 return;
1429 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1430 if (XEXP (link, 1) == note)
1432 XEXP (link, 1) = XEXP (note, 1);
1433 return;
1436 abort ();
1439 /* Nonzero if X contains any volatile instructions. These are instructions
1440 which may cause unpredictable machine state instructions, and thus no
1441 instructions should be moved or combined across them. This includes
1442 only volatile asms and UNSPEC_VOLATILE instructions. */
1445 volatile_insn_p (x)
1446 rtx x;
1448 register RTX_CODE code;
1450 code = GET_CODE (x);
1451 switch (code)
1453 case LABEL_REF:
1454 case SYMBOL_REF:
1455 case CONST_INT:
1456 case CONST:
1457 case CONST_DOUBLE:
1458 case CC0:
1459 case PC:
1460 case REG:
1461 case SCRATCH:
1462 case CLOBBER:
1463 case ASM_INPUT:
1464 case ADDR_VEC:
1465 case ADDR_DIFF_VEC:
1466 case CALL:
1467 case MEM:
1468 return 0;
1470 case UNSPEC_VOLATILE:
1471 /* case TRAP_IF: This isn't clear yet. */
1472 return 1;
1474 case ASM_OPERANDS:
1475 if (MEM_VOLATILE_P (x))
1476 return 1;
1478 default:
1479 break;
1482 /* Recursively scan the operands of this expression. */
1485 register char *fmt = GET_RTX_FORMAT (code);
1486 register int i;
1488 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1490 if (fmt[i] == 'e')
1492 if (volatile_insn_p (XEXP (x, i)))
1493 return 1;
1495 if (fmt[i] == 'E')
1497 register int j;
1498 for (j = 0; j < XVECLEN (x, i); j++)
1499 if (volatile_insn_p (XVECEXP (x, i, j)))
1500 return 1;
1504 return 0;
1507 /* Nonzero if X contains any volatile memory references
1508 UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions. */
1511 volatile_refs_p (x)
1512 rtx x;
1514 register RTX_CODE code;
1516 code = GET_CODE (x);
1517 switch (code)
1519 case LABEL_REF:
1520 case SYMBOL_REF:
1521 case CONST_INT:
1522 case CONST:
1523 case CONST_DOUBLE:
1524 case CC0:
1525 case PC:
1526 case REG:
1527 case SCRATCH:
1528 case CLOBBER:
1529 case ASM_INPUT:
1530 case ADDR_VEC:
1531 case ADDR_DIFF_VEC:
1532 return 0;
1534 case CALL:
1535 case UNSPEC_VOLATILE:
1536 /* case TRAP_IF: This isn't clear yet. */
1537 return 1;
1539 case MEM:
1540 case ASM_OPERANDS:
1541 if (MEM_VOLATILE_P (x))
1542 return 1;
1544 default:
1545 break;
1548 /* Recursively scan the operands of this expression. */
1551 register char *fmt = GET_RTX_FORMAT (code);
1552 register int i;
1554 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1556 if (fmt[i] == 'e')
1558 if (volatile_refs_p (XEXP (x, i)))
1559 return 1;
1561 if (fmt[i] == 'E')
1563 register int j;
1564 for (j = 0; j < XVECLEN (x, i); j++)
1565 if (volatile_refs_p (XVECEXP (x, i, j)))
1566 return 1;
1570 return 0;
1573 /* Similar to above, except that it also rejects register pre- and post-
1574 incrementing. */
1577 side_effects_p (x)
1578 rtx x;
1580 register RTX_CODE code;
1582 code = GET_CODE (x);
1583 switch (code)
1585 case LABEL_REF:
1586 case SYMBOL_REF:
1587 case CONST_INT:
1588 case CONST:
1589 case CONST_DOUBLE:
1590 case CC0:
1591 case PC:
1592 case REG:
1593 case SCRATCH:
1594 case ASM_INPUT:
1595 case ADDR_VEC:
1596 case ADDR_DIFF_VEC:
1597 return 0;
1599 case CLOBBER:
1600 /* Reject CLOBBER with a non-VOID mode. These are made by combine.c
1601 when some combination can't be done. If we see one, don't think
1602 that we can simplify the expression. */
1603 return (GET_MODE (x) != VOIDmode);
1605 case PRE_INC:
1606 case PRE_DEC:
1607 case POST_INC:
1608 case POST_DEC:
1609 case CALL:
1610 case UNSPEC_VOLATILE:
1611 /* case TRAP_IF: This isn't clear yet. */
1612 return 1;
1614 case MEM:
1615 case ASM_OPERANDS:
1616 if (MEM_VOLATILE_P (x))
1617 return 1;
1619 default:
1620 break;
1623 /* Recursively scan the operands of this expression. */
1626 register char *fmt = GET_RTX_FORMAT (code);
1627 register int i;
1629 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1631 if (fmt[i] == 'e')
1633 if (side_effects_p (XEXP (x, i)))
1634 return 1;
1636 if (fmt[i] == 'E')
1638 register int j;
1639 for (j = 0; j < XVECLEN (x, i); j++)
1640 if (side_effects_p (XVECEXP (x, i, j)))
1641 return 1;
1645 return 0;
1648 /* Return nonzero if evaluating rtx X might cause a trap. */
1651 may_trap_p (x)
1652 rtx x;
1654 int i;
1655 enum rtx_code code;
1656 char *fmt;
1658 if (x == 0)
1659 return 0;
1660 code = GET_CODE (x);
1661 switch (code)
1663 /* Handle these cases quickly. */
1664 case CONST_INT:
1665 case CONST_DOUBLE:
1666 case SYMBOL_REF:
1667 case LABEL_REF:
1668 case CONST:
1669 case PC:
1670 case CC0:
1671 case REG:
1672 case SCRATCH:
1673 return 0;
1675 /* Conditional trap can trap! */
1676 case UNSPEC_VOLATILE:
1677 case TRAP_IF:
1678 return 1;
1680 /* Memory ref can trap unless it's a static var or a stack slot. */
1681 case MEM:
1682 return rtx_addr_can_trap_p (XEXP (x, 0));
1684 /* Division by a non-constant might trap. */
1685 case DIV:
1686 case MOD:
1687 case UDIV:
1688 case UMOD:
1689 if (! CONSTANT_P (XEXP (x, 1))
1690 || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1691 return 1;
1692 /* This was const0_rtx, but by not using that,
1693 we can link this file into other programs. */
1694 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
1695 return 1;
1696 break;
1698 case EXPR_LIST:
1699 /* An EXPR_LIST is used to represent a function call. This
1700 certainly may trap. */
1701 return 1;
1703 default:
1704 /* Any floating arithmetic may trap. */
1705 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1706 return 1;
1709 fmt = GET_RTX_FORMAT (code);
1710 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1712 if (fmt[i] == 'e')
1714 if (may_trap_p (XEXP (x, i)))
1715 return 1;
1717 else if (fmt[i] == 'E')
1719 register int j;
1720 for (j = 0; j < XVECLEN (x, i); j++)
1721 if (may_trap_p (XVECEXP (x, i, j)))
1722 return 1;
1725 return 0;
1728 /* Return nonzero if X contains a comparison that is not either EQ or NE,
1729 i.e., an inequality. */
1732 inequality_comparisons_p (x)
1733 rtx x;
1735 register char *fmt;
1736 register int len, i;
1737 register enum rtx_code code = GET_CODE (x);
1739 switch (code)
1741 case REG:
1742 case SCRATCH:
1743 case PC:
1744 case CC0:
1745 case CONST_INT:
1746 case CONST_DOUBLE:
1747 case CONST:
1748 case LABEL_REF:
1749 case SYMBOL_REF:
1750 return 0;
1752 case LT:
1753 case LTU:
1754 case GT:
1755 case GTU:
1756 case LE:
1757 case LEU:
1758 case GE:
1759 case GEU:
1760 return 1;
1762 default:
1763 break;
1766 len = GET_RTX_LENGTH (code);
1767 fmt = GET_RTX_FORMAT (code);
1769 for (i = 0; i < len; i++)
1771 if (fmt[i] == 'e')
1773 if (inequality_comparisons_p (XEXP (x, i)))
1774 return 1;
1776 else if (fmt[i] == 'E')
1778 register int j;
1779 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1780 if (inequality_comparisons_p (XVECEXP (x, i, j)))
1781 return 1;
1785 return 0;
1788 /* Replace any occurrence of FROM in X with TO. The function does
1789 not enter into CONST_DOUBLE for the replace.
1791 Note that copying is not done so X must not be shared unless all copies
1792 are to be modified. */
1795 replace_rtx (x, from, to)
1796 rtx x, from, to;
1798 register int i, j;
1799 register char *fmt;
1801 /* The following prevents loops occurrence when we change MEM in
1802 CONST_DOUBLE onto the same CONST_DOUBLE. */
1803 if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
1804 return x;
1806 if (x == from)
1807 return to;
1809 /* Allow this function to make replacements in EXPR_LISTs. */
1810 if (x == 0)
1811 return 0;
1813 fmt = GET_RTX_FORMAT (GET_CODE (x));
1814 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1816 if (fmt[i] == 'e')
1817 XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
1818 else if (fmt[i] == 'E')
1819 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1820 XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
1823 return x;
1826 /* Throughout the rtx X, replace many registers according to REG_MAP.
1827 Return the replacement for X (which may be X with altered contents).
1828 REG_MAP[R] is the replacement for register R, or 0 for don't replace.
1829 NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
1831 We only support REG_MAP entries of REG or SUBREG. Also, hard registers
1832 should not be mapped to pseudos or vice versa since validate_change
1833 is not called.
1835 If REPLACE_DEST is 1, replacements are also done in destinations;
1836 otherwise, only sources are replaced. */
1839 replace_regs (x, reg_map, nregs, replace_dest)
1840 rtx x;
1841 rtx *reg_map;
1842 int nregs;
1843 int replace_dest;
1845 register enum rtx_code code;
1846 register int i;
1847 register char *fmt;
1849 if (x == 0)
1850 return x;
1852 code = GET_CODE (x);
1853 switch (code)
1855 case SCRATCH:
1856 case PC:
1857 case CC0:
1858 case CONST_INT:
1859 case CONST_DOUBLE:
1860 case CONST:
1861 case SYMBOL_REF:
1862 case LABEL_REF:
1863 return x;
1865 case REG:
1866 /* Verify that the register has an entry before trying to access it. */
1867 if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
1869 /* SUBREGs can't be shared. Always return a copy to ensure that if
1870 this replacement occurs more than once then each instance will
1871 get distinct rtx. */
1872 if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
1873 return copy_rtx (reg_map[REGNO (x)]);
1874 return reg_map[REGNO (x)];
1876 return x;
1878 case SUBREG:
1879 /* Prevent making nested SUBREGs. */
1880 if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
1881 && reg_map[REGNO (SUBREG_REG (x))] != 0
1882 && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
1884 rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
1885 rtx map_inner = SUBREG_REG (map_val);
1887 if (GET_MODE (x) == GET_MODE (map_inner))
1888 return map_inner;
1889 else
1891 /* We cannot call gen_rtx here since we may be linked with
1892 genattrtab.c. */
1893 /* Let's try clobbering the incoming SUBREG and see
1894 if this is really safe. */
1895 SUBREG_REG (x) = map_inner;
1896 SUBREG_WORD (x) += SUBREG_WORD (map_val);
1897 return x;
1898 #if 0
1899 rtx new = rtx_alloc (SUBREG);
1900 PUT_MODE (new, GET_MODE (x));
1901 SUBREG_REG (new) = map_inner;
1902 SUBREG_WORD (new) = SUBREG_WORD (x) + SUBREG_WORD (map_val);
1903 #endif
1906 break;
1908 case SET:
1909 if (replace_dest)
1910 SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
1912 else if (GET_CODE (SET_DEST (x)) == MEM
1913 || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
1914 /* Even if we are not to replace destinations, replace register if it
1915 is CONTAINED in destination (destination is memory or
1916 STRICT_LOW_PART). */
1917 XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
1918 reg_map, nregs, 0);
1919 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
1920 /* Similarly, for ZERO_EXTRACT we replace all operands. */
1921 break;
1923 SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
1924 return x;
1926 default:
1927 break;
1930 fmt = GET_RTX_FORMAT (code);
1931 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1933 if (fmt[i] == 'e')
1934 XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
1935 if (fmt[i] == 'E')
1937 register int j;
1938 for (j = 0; j < XVECLEN (x, i); j++)
1939 XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
1940 nregs, replace_dest);
1943 return x;
1946 /* Return 1 if X, the SRC_SRC of SET of (pc) contain a REG or MEM that is
1947 not in the constant pool and not in the condition of an IF_THEN_ELSE. */
1949 static int
1950 jmp_uses_reg_or_mem (x)
1951 rtx x;
1953 enum rtx_code code = GET_CODE (x);
1954 int i, j;
1955 char *fmt;
1957 switch (code)
1959 case CONST:
1960 case LABEL_REF:
1961 case PC:
1962 return 0;
1964 case REG:
1965 return 1;
1967 case MEM:
1968 return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1969 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
1971 case IF_THEN_ELSE:
1972 return (jmp_uses_reg_or_mem (XEXP (x, 1))
1973 || jmp_uses_reg_or_mem (XEXP (x, 2)));
1975 case PLUS: case MINUS: case MULT:
1976 return (jmp_uses_reg_or_mem (XEXP (x, 0))
1977 || jmp_uses_reg_or_mem (XEXP (x, 1)));
1979 default:
1980 break;
1983 fmt = GET_RTX_FORMAT (code);
1984 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1986 if (fmt[i] == 'e'
1987 && jmp_uses_reg_or_mem (XEXP (x, i)))
1988 return 1;
1990 if (fmt[i] == 'E')
1991 for (j = 0; j < XVECLEN (x, i); j++)
1992 if (jmp_uses_reg_or_mem (XVECEXP (x, i, j)))
1993 return 1;
1996 return 0;
1999 /* Return nonzero if INSN is an indirect jump (aka computed jump).
2001 Tablejumps and casesi insns are not considered indirect jumps;
2002 we can recognize them by a (use (lael_ref)). */
2005 computed_jump_p (insn)
2006 rtx insn;
2008 int i;
2009 if (GET_CODE (insn) == JUMP_INSN)
2011 rtx pat = PATTERN (insn);
2013 if (GET_CODE (pat) == PARALLEL)
2015 int len = XVECLEN (pat, 0);
2016 int has_use_labelref = 0;
2018 for (i = len - 1; i >= 0; i--)
2019 if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2020 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2021 == LABEL_REF))
2022 has_use_labelref = 1;
2024 if (! has_use_labelref)
2025 for (i = len - 1; i >= 0; i--)
2026 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2027 && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2028 && jmp_uses_reg_or_mem (SET_SRC (XVECEXP (pat, 0, i))))
2029 return 1;
2031 else if (GET_CODE (pat) == SET
2032 && SET_DEST (pat) == pc_rtx
2033 && jmp_uses_reg_or_mem (SET_SRC (pat)))
2034 return 1;
2036 return 0;
2039 /* Traverse X via depth-first search, calling F for each
2040 sub-expression (including X itself). F is also passed the DATA.
2041 If F returns -1, do not traverse sub-expressions, but continue
2042 traversing the rest of the tree. If F ever returns any other
2043 non-zero value, stop the traversal, and return the value returned
2044 by F. Otherwise, return 0. This function does not traverse inside
2045 tree structure that contains RTX_EXPRs, or into sub-expressions
2046 whose format code is `0' since it is not known whether or not those
2047 codes are actually RTL.
2049 This routine is very general, and could (should?) be used to
2050 implement many of the other routines in this file. */
2053 for_each_rtx (x, f, data)
2054 rtx* x;
2055 rtx_function f;
2056 void* data;
2058 int result;
2059 int length;
2060 char* format;
2061 int i;
2063 /* Call F on X. */
2064 result = (*f)(x, data);
2065 if (result == -1)
2066 /* Do not traverse sub-expressions. */
2067 return 0;
2068 else if (result != 0)
2069 /* Stop the traversal. */
2070 return result;
2072 if (*x == NULL_RTX)
2073 /* There are no sub-expressions. */
2074 return 0;
2076 length = GET_RTX_LENGTH (GET_CODE (*x));
2077 format = GET_RTX_FORMAT (GET_CODE (*x));
2079 for (i = 0; i < length; ++i)
2081 switch (format[i])
2083 case 'e':
2084 result = for_each_rtx (&XEXP (*x, i), f, data);
2085 if (result != 0)
2086 return result;
2087 break;
2089 case 'V':
2090 case 'E':
2091 if (XVEC (*x, i) != 0)
2093 int j;
2094 for (j = 0; j < XVECLEN (*x, i); ++j)
2096 result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2097 if (result != 0)
2098 return result;
2101 break;
2103 default:
2104 /* Nothing to do. */
2105 break;
2110 return 0;