Extra arg for rtx_varies_p
[official-gcc.git] / gcc / rtlanal.c
blob67c5319dfbf6791802f4c8a897bd2f0672383246
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 "toplev.h"
26 #include "rtl.h"
28 static void reg_set_p_1 PARAMS ((rtx, rtx, void *));
29 static void insn_dependent_p_1 PARAMS ((rtx, rtx, void *));
30 static void reg_set_last_1 PARAMS ((rtx, rtx, void *));
33 /* Forward declarations */
34 static int jmp_uses_reg_or_mem PARAMS ((rtx));
36 /* Bit flags that specify the machine subtype we are compiling for.
37 Bits are tested using macros TARGET_... defined in the tm.h file
38 and set by `-m...' switches. Must be defined in rtlanal.c. */
40 int target_flags;
42 /* Return 1 if the value of X is unstable
43 (would be different at a different point in the program).
44 The frame pointer, arg pointer, etc. are considered stable
45 (within one function) and so is anything marked `unchanging'. */
47 int
48 rtx_unstable_p (x)
49 rtx x;
51 register RTX_CODE code = GET_CODE (x);
52 register int i;
53 register const char *fmt;
55 switch (code)
57 case MEM:
58 return ! RTX_UNCHANGING_P (x) || rtx_unstable_p (XEXP (x, 0));
60 case QUEUED:
61 return 1;
63 case CONST:
64 case CONST_INT:
65 case CONST_DOUBLE:
66 case SYMBOL_REF:
67 case LABEL_REF:
68 return 0;
70 case REG:
71 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
72 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
73 || x == arg_pointer_rtx || RTX_UNCHANGING_P (x))
74 return 0;
75 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
76 /* ??? When call-clobbered, the value is stable modulo the restore
77 that must happen after a call. This currently screws up local-alloc
78 into believing that the restore is not needed. */
79 if (x == pic_offset_table_rtx)
80 return 0;
81 #endif
82 return 1;
84 case ASM_OPERANDS:
85 if (MEM_VOLATILE_P (x))
86 return 1;
88 /* FALLTHROUGH */
90 default:
91 break;
94 fmt = GET_RTX_FORMAT (code);
95 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
96 if (fmt[i] == 'e')
98 if (rtx_unstable_p (XEXP (x, i)))
99 return 1;
101 else if (fmt[i] == 'E')
103 int j;
104 for (j = 0; j < XVECLEN (x, i); j++)
105 if (rtx_unstable_p (XVECEXP (x, i, j)))
106 return 1;
109 return 0;
112 /* Return 1 if X has a value that can vary even between two
113 executions of the program. 0 means X can be compared reliably
114 against certain constants or near-constants.
115 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
116 zero, we are slightly more conservative.
117 The frame pointer and the arg pointer are considered constant. */
120 rtx_varies_p (x, for_alias)
121 rtx x;
122 int for_alias;
124 register RTX_CODE code = GET_CODE (x);
125 register int i;
126 register const char *fmt;
128 switch (code)
130 case MEM:
131 return ! RTX_UNCHANGING_P (x) || rtx_varies_p (XEXP (x, 0), for_alias);
133 case QUEUED:
134 return 1;
136 case CONST:
137 case CONST_INT:
138 case CONST_DOUBLE:
139 case SYMBOL_REF:
140 case LABEL_REF:
141 return 0;
143 case REG:
144 /* Note that we have to test for the actual rtx used for the frame
145 and arg pointers and not just the register number in case we have
146 eliminated the frame and/or arg pointer and are using it
147 for pseudos. */
148 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
149 || x == arg_pointer_rtx)
150 return 0;
151 if (x == pic_offset_table_rtx
152 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
153 /* ??? When call-clobbered, the value is stable modulo the restore
154 that must happen after a call. This currently screws up
155 local-alloc into believing that the restore is not needed, so we
156 must return 0 only if we are called from alias analysis. */
157 && for_alias
158 #endif
160 return 0;
161 return 1;
163 case LO_SUM:
164 /* The operand 0 of a LO_SUM is considered constant
165 (in fact is it related specifically to operand 1). */
166 return rtx_varies_p (XEXP (x, 1), for_alias);
168 case ASM_OPERANDS:
169 if (MEM_VOLATILE_P (x))
170 return 1;
172 /* FALLTHROUGH */
174 default:
175 break;
178 fmt = GET_RTX_FORMAT (code);
179 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
180 if (fmt[i] == 'e')
182 if (rtx_varies_p (XEXP (x, i), for_alias))
183 return 1;
185 else if (fmt[i] == 'E')
187 int j;
188 for (j = 0; j < XVECLEN (x, i); j++)
189 if (rtx_varies_p (XVECEXP (x, i, j), for_alias))
190 return 1;
193 return 0;
196 /* Return 0 if the use of X as an address in a MEM can cause a trap. */
199 rtx_addr_can_trap_p (x)
200 register rtx x;
202 register enum rtx_code code = GET_CODE (x);
204 switch (code)
206 case SYMBOL_REF:
207 case LABEL_REF:
208 /* SYMBOL_REF is problematic due to the possible presence of
209 a #pragma weak, but to say that loads from symbols can trap is
210 *very* costly. It's not at all clear what's best here. For
211 now, we ignore the impact of #pragma weak. */
212 return 0;
214 case REG:
215 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
216 return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
217 || x == stack_pointer_rtx || x == arg_pointer_rtx);
219 case CONST:
220 return rtx_addr_can_trap_p (XEXP (x, 0));
222 case PLUS:
223 /* An address is assumed not to trap if it is an address that can't
224 trap plus a constant integer or it is the pic register plus a
225 constant. */
226 return ! ((! rtx_addr_can_trap_p (XEXP (x, 0))
227 && GET_CODE (XEXP (x, 1)) == CONST_INT)
228 || (XEXP (x, 0) == pic_offset_table_rtx
229 && CONSTANT_P (XEXP (x, 1))));
231 case LO_SUM:
232 return rtx_addr_can_trap_p (XEXP (x, 1));
234 default:
235 break;
238 /* If it isn't one of the case above, it can cause a trap. */
239 return 1;
242 /* Return 1 if X refers to a memory location whose address
243 cannot be compared reliably with constant addresses,
244 or if X refers to a BLKmode memory object.
245 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
246 zero, we are slightly more conservative. */
249 rtx_addr_varies_p (x, for_alias)
250 rtx x;
251 int for_alias;
253 register enum rtx_code code;
254 register int i;
255 register const char *fmt;
257 if (x == 0)
258 return 0;
260 code = GET_CODE (x);
261 if (code == MEM)
262 return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0), for_alias);
264 fmt = GET_RTX_FORMAT (code);
265 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
266 if (fmt[i] == 'e')
268 if (rtx_addr_varies_p (XEXP (x, i), for_alias))
269 return 1;
271 else if (fmt[i] == 'E')
273 int j;
274 for (j = 0; j < XVECLEN (x, i); j++)
275 if (rtx_addr_varies_p (XVECEXP (x, i, j), for_alias))
276 return 1;
278 return 0;
281 /* Return the value of the integer term in X, if one is apparent;
282 otherwise return 0.
283 Only obvious integer terms are detected.
284 This is used in cse.c with the `related_value' field.*/
286 HOST_WIDE_INT
287 get_integer_term (x)
288 rtx x;
290 if (GET_CODE (x) == CONST)
291 x = XEXP (x, 0);
293 if (GET_CODE (x) == MINUS
294 && GET_CODE (XEXP (x, 1)) == CONST_INT)
295 return - INTVAL (XEXP (x, 1));
296 if (GET_CODE (x) == PLUS
297 && GET_CODE (XEXP (x, 1)) == CONST_INT)
298 return INTVAL (XEXP (x, 1));
299 return 0;
302 /* If X is a constant, return the value sans apparent integer term;
303 otherwise return 0.
304 Only obvious integer terms are detected. */
307 get_related_value (x)
308 rtx x;
310 if (GET_CODE (x) != CONST)
311 return 0;
312 x = XEXP (x, 0);
313 if (GET_CODE (x) == PLUS
314 && GET_CODE (XEXP (x, 1)) == CONST_INT)
315 return XEXP (x, 0);
316 else if (GET_CODE (x) == MINUS
317 && GET_CODE (XEXP (x, 1)) == CONST_INT)
318 return XEXP (x, 0);
319 return 0;
322 /* Return the number of places FIND appears within X. If COUNT_DEST is
323 zero, we do not count occurrences inside the destination of a SET. */
326 count_occurrences (x, find, count_dest)
327 rtx x, find;
328 int count_dest;
330 int i, j;
331 enum rtx_code code;
332 const char *format_ptr;
333 int count;
335 if (x == find)
336 return 1;
338 code = GET_CODE (x);
340 switch (code)
342 case REG:
343 case CONST_INT:
344 case CONST_DOUBLE:
345 case SYMBOL_REF:
346 case CODE_LABEL:
347 case PC:
348 case CC0:
349 return 0;
351 case MEM:
352 if (GET_CODE (find) == MEM && rtx_equal_p (x, find))
353 return 1;
354 break;
356 case SET:
357 if (SET_DEST (x) == find && ! count_dest)
358 return count_occurrences (SET_SRC (x), find, count_dest);
359 break;
361 default:
362 break;
365 format_ptr = GET_RTX_FORMAT (code);
366 count = 0;
368 for (i = 0; i < GET_RTX_LENGTH (code); i++)
370 switch (*format_ptr++)
372 case 'e':
373 count += count_occurrences (XEXP (x, i), find, count_dest);
374 break;
376 case 'E':
377 for (j = 0; j < XVECLEN (x, i); j++)
378 count += count_occurrences (XVECEXP (x, i, j), find, count_dest);
379 break;
382 return count;
385 /* Nonzero if register REG appears somewhere within IN.
386 Also works if REG is not a register; in this case it checks
387 for a subexpression of IN that is Lisp "equal" to REG. */
390 reg_mentioned_p (reg, in)
391 register rtx reg, in;
393 register const char *fmt;
394 register int i;
395 register enum rtx_code code;
397 if (in == 0)
398 return 0;
400 if (reg == in)
401 return 1;
403 if (GET_CODE (in) == LABEL_REF)
404 return reg == XEXP (in, 0);
406 code = GET_CODE (in);
408 switch (code)
410 /* Compare registers by number. */
411 case REG:
412 return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
414 /* These codes have no constituent expressions
415 and are unique. */
416 case SCRATCH:
417 case CC0:
418 case PC:
419 return 0;
421 case CONST_INT:
422 return GET_CODE (reg) == CONST_INT && INTVAL (in) == INTVAL (reg);
424 case CONST_DOUBLE:
425 /* These are kept unique for a given value. */
426 return 0;
428 default:
429 break;
432 if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
433 return 1;
435 fmt = GET_RTX_FORMAT (code);
437 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
439 if (fmt[i] == 'E')
441 register int j;
442 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
443 if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
444 return 1;
446 else if (fmt[i] == 'e'
447 && reg_mentioned_p (reg, XEXP (in, i)))
448 return 1;
450 return 0;
453 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
454 no CODE_LABEL insn. */
457 no_labels_between_p (beg, end)
458 rtx beg, end;
460 register rtx p;
461 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
462 if (GET_CODE (p) == CODE_LABEL)
463 return 0;
464 return 1;
467 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
468 no JUMP_INSN insn. */
471 no_jumps_between_p (beg, end)
472 rtx beg, end;
474 register rtx p;
475 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
476 if (GET_CODE (p) == JUMP_INSN)
477 return 0;
478 return 1;
481 /* Nonzero if register REG is used in an insn between
482 FROM_INSN and TO_INSN (exclusive of those two). */
485 reg_used_between_p (reg, from_insn, to_insn)
486 rtx reg, from_insn, to_insn;
488 register rtx insn;
490 if (from_insn == to_insn)
491 return 0;
493 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
494 if (INSN_P (insn)
495 && (reg_overlap_mentioned_p (reg, PATTERN (insn))
496 || (GET_CODE (insn) == CALL_INSN
497 && (find_reg_fusage (insn, USE, reg)
498 || find_reg_fusage (insn, CLOBBER, reg)))))
499 return 1;
500 return 0;
503 /* Nonzero if the old value of X, a register, is referenced in BODY. If X
504 is entirely replaced by a new value and the only use is as a SET_DEST,
505 we do not consider it a reference. */
508 reg_referenced_p (x, body)
509 rtx x;
510 rtx body;
512 int i;
514 switch (GET_CODE (body))
516 case SET:
517 if (reg_overlap_mentioned_p (x, SET_SRC (body)))
518 return 1;
520 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
521 of a REG that occupies all of the REG, the insn references X if
522 it is mentioned in the destination. */
523 if (GET_CODE (SET_DEST (body)) != CC0
524 && GET_CODE (SET_DEST (body)) != PC
525 && GET_CODE (SET_DEST (body)) != REG
526 && ! (GET_CODE (SET_DEST (body)) == SUBREG
527 && GET_CODE (SUBREG_REG (SET_DEST (body))) == REG
528 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
529 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
530 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
531 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
532 && reg_overlap_mentioned_p (x, SET_DEST (body)))
533 return 1;
534 return 0;
536 case ASM_OPERANDS:
537 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
538 if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
539 return 1;
540 return 0;
542 case CALL:
543 case USE:
544 case IF_THEN_ELSE:
545 return reg_overlap_mentioned_p (x, body);
547 case TRAP_IF:
548 return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
550 case UNSPEC:
551 case UNSPEC_VOLATILE:
552 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
553 if (reg_overlap_mentioned_p (x, XVECEXP (body, 0, i)))
554 return 1;
555 return 0;
557 case PARALLEL:
558 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
559 if (reg_referenced_p (x, XVECEXP (body, 0, i)))
560 return 1;
561 return 0;
563 case CLOBBER:
564 if (GET_CODE (XEXP (body, 0)) == MEM)
565 if (reg_overlap_mentioned_p (x, XEXP (XEXP (body, 0), 0)))
566 return 1;
567 return 0;
569 case COND_EXEC:
570 if (reg_overlap_mentioned_p (x, COND_EXEC_TEST (body)))
571 return 1;
572 return reg_referenced_p (x, COND_EXEC_CODE (body));
574 default:
575 return 0;
579 /* Nonzero if register REG is referenced in an insn between
580 FROM_INSN and TO_INSN (exclusive of those two). Sets of REG do
581 not count. */
584 reg_referenced_between_p (reg, from_insn, to_insn)
585 rtx reg, from_insn, to_insn;
587 register rtx insn;
589 if (from_insn == to_insn)
590 return 0;
592 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
593 if (INSN_P (insn)
594 && (reg_referenced_p (reg, PATTERN (insn))
595 || (GET_CODE (insn) == CALL_INSN
596 && find_reg_fusage (insn, USE, reg))))
597 return 1;
598 return 0;
601 /* Nonzero if register REG is set or clobbered in an insn between
602 FROM_INSN and TO_INSN (exclusive of those two). */
605 reg_set_between_p (reg, from_insn, to_insn)
606 rtx reg, from_insn, to_insn;
608 register rtx insn;
610 if (from_insn == to_insn)
611 return 0;
613 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
614 if (INSN_P (insn) && reg_set_p (reg, insn))
615 return 1;
616 return 0;
619 /* Internals of reg_set_between_p. */
621 static rtx reg_set_reg;
622 static int reg_set_flag;
624 static void
625 reg_set_p_1 (x, pat, data)
626 rtx x;
627 rtx pat ATTRIBUTE_UNUSED;
628 void *data ATTRIBUTE_UNUSED;
630 /* We don't want to return 1 if X is a MEM that contains a register
631 within REG_SET_REG. */
633 if ((GET_CODE (x) != MEM)
634 && reg_overlap_mentioned_p (reg_set_reg, x))
635 reg_set_flag = 1;
639 reg_set_p (reg, insn)
640 rtx reg, insn;
642 rtx body = insn;
644 /* We can be passed an insn or part of one. If we are passed an insn,
645 check if a side-effect of the insn clobbers REG. */
646 if (INSN_P (insn))
648 if (FIND_REG_INC_NOTE (insn, reg)
649 || (GET_CODE (insn) == CALL_INSN
650 /* We'd like to test call_used_regs here, but rtlanal.c can't
651 reference that variable due to its use in genattrtab. So
652 we'll just be more conservative.
654 ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
655 information holds all clobbered registers. */
656 && ((GET_CODE (reg) == REG
657 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
658 || GET_CODE (reg) == MEM
659 || find_reg_fusage (insn, CLOBBER, reg))))
660 return 1;
662 body = PATTERN (insn);
665 reg_set_reg = reg;
666 reg_set_flag = 0;
667 note_stores (body, reg_set_p_1, NULL);
668 return reg_set_flag;
671 /* Similar to reg_set_between_p, but check all registers in X. Return 0
672 only if none of them are modified between START and END. Do not
673 consider non-registers one way or the other. */
676 regs_set_between_p (x, start, end)
677 rtx x;
678 rtx start, end;
680 enum rtx_code code = GET_CODE (x);
681 const char *fmt;
682 int i, j;
684 switch (code)
686 case CONST_INT:
687 case CONST_DOUBLE:
688 case CONST:
689 case SYMBOL_REF:
690 case LABEL_REF:
691 case PC:
692 case CC0:
693 return 0;
695 case REG:
696 return reg_set_between_p (x, start, end);
698 default:
699 break;
702 fmt = GET_RTX_FORMAT (code);
703 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
705 if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
706 return 1;
708 else if (fmt[i] == 'E')
709 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
710 if (regs_set_between_p (XVECEXP (x, i, j), start, end))
711 return 1;
714 return 0;
717 /* Similar to reg_set_between_p, but check all registers in X. Return 0
718 only if none of them are modified between START and END. Return 1 if
719 X contains a MEM; this routine does not perform any memory aliasing. */
722 modified_between_p (x, start, end)
723 rtx x;
724 rtx start, end;
726 enum rtx_code code = GET_CODE (x);
727 const char *fmt;
728 int i, j;
730 switch (code)
732 case CONST_INT:
733 case CONST_DOUBLE:
734 case CONST:
735 case SYMBOL_REF:
736 case LABEL_REF:
737 return 0;
739 case PC:
740 case CC0:
741 return 1;
743 case MEM:
744 /* If the memory is not constant, assume it is modified. If it is
745 constant, we still have to check the address. */
746 if (! RTX_UNCHANGING_P (x))
747 return 1;
748 break;
750 case REG:
751 return reg_set_between_p (x, start, end);
753 default:
754 break;
757 fmt = GET_RTX_FORMAT (code);
758 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
760 if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
761 return 1;
763 else if (fmt[i] == 'E')
764 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
765 if (modified_between_p (XVECEXP (x, i, j), start, end))
766 return 1;
769 return 0;
772 /* Similar to reg_set_p, but check all registers in X. Return 0 only if none
773 of them are modified in INSN. Return 1 if X contains a MEM; this routine
774 does not perform any memory aliasing. */
777 modified_in_p (x, insn)
778 rtx x;
779 rtx insn;
781 enum rtx_code code = GET_CODE (x);
782 const char *fmt;
783 int i, j;
785 switch (code)
787 case CONST_INT:
788 case CONST_DOUBLE:
789 case CONST:
790 case SYMBOL_REF:
791 case LABEL_REF:
792 return 0;
794 case PC:
795 case CC0:
796 return 1;
798 case MEM:
799 /* If the memory is not constant, assume it is modified. If it is
800 constant, we still have to check the address. */
801 if (! RTX_UNCHANGING_P (x))
802 return 1;
803 break;
805 case REG:
806 return reg_set_p (x, insn);
808 default:
809 break;
812 fmt = GET_RTX_FORMAT (code);
813 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
815 if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
816 return 1;
818 else if (fmt[i] == 'E')
819 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
820 if (modified_in_p (XVECEXP (x, i, j), insn))
821 return 1;
824 return 0;
827 /* Return true if anything in insn X is (anti,output,true) dependent on
828 anything in insn Y. */
831 insn_dependent_p (x, y)
832 rtx x, y;
834 rtx tmp;
836 if (! INSN_P (x) || ! INSN_P (y))
837 abort ();
839 tmp = PATTERN (y);
840 note_stores (PATTERN (x), insn_dependent_p_1, &tmp);
841 if (tmp == NULL_RTX)
842 return 1;
844 tmp = PATTERN (x);
845 note_stores (PATTERN (y), insn_dependent_p_1, &tmp);
846 if (tmp == NULL_RTX)
847 return 1;
849 return 0;
852 /* A helper routine for insn_dependent_p called through note_stores. */
854 static void
855 insn_dependent_p_1 (x, pat, data)
856 rtx x;
857 rtx pat ATTRIBUTE_UNUSED;
858 void *data;
860 rtx * pinsn = (rtx *) data;
862 if (*pinsn && reg_mentioned_p (x, *pinsn))
863 *pinsn = NULL_RTX;
866 /* Given an INSN, return a SET expression if this insn has only a single SET.
867 It may also have CLOBBERs, USEs, or SET whose output
868 will not be used, which we ignore. */
871 single_set_2 (insn, pat)
872 rtx insn, pat;
874 rtx set = NULL;
875 int set_verified = 1;
876 int i;
878 if (GET_CODE (pat) == PARALLEL)
880 for (i = 0; i < XVECLEN (pat, 0); i++)
882 rtx sub = XVECEXP (pat, 0, i);
883 switch (GET_CODE (sub))
885 case USE:
886 case CLOBBER:
887 break;
889 case SET:
890 /* We can consider insns having multiple sets, where all
891 but one are dead as single set insns. In common case
892 only single set is present in the pattern so we want
893 to avoid checking for REG_UNUSED notes unless neccesary.
895 When we reach set first time, we just expect this is
896 the single set we are looking for and only when more
897 sets are found in the insn, we check them. */
898 if (!set_verified)
900 if (find_reg_note (insn, REG_UNUSED, SET_DEST (set))
901 && !side_effects_p (set))
902 set = NULL;
903 else
904 set_verified = 1;
906 if (!set)
907 set = sub, set_verified = 0;
908 else if (!find_reg_note (insn, REG_UNUSED, SET_DEST (sub))
909 || side_effects_p (sub))
910 return NULL_RTX;
911 break;
913 default:
914 return NULL_RTX;
918 return set;
921 /* Given an INSN, return nonzero if it has more than one SET, else return
922 zero. */
925 multiple_sets (insn)
926 rtx insn;
928 int found;
929 int i;
931 /* INSN must be an insn. */
932 if (! INSN_P (insn))
933 return 0;
935 /* Only a PARALLEL can have multiple SETs. */
936 if (GET_CODE (PATTERN (insn)) == PARALLEL)
938 for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
939 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
941 /* If we have already found a SET, then return now. */
942 if (found)
943 return 1;
944 else
945 found = 1;
949 /* Either zero or one SET. */
950 return 0;
953 /* Return the last thing that X was assigned from before *PINSN. If VALID_TO
954 is not NULL_RTX then verify that the object is not modified up to VALID_TO.
955 If the object was modified, if we hit a partial assignment to X, or hit a
956 CODE_LABEL first, return X. If we found an assignment, update *PINSN to
957 point to it. ALLOW_HWREG is set to 1 if hardware registers are allowed to
958 be the src. */
961 find_last_value (x, pinsn, valid_to, allow_hwreg)
962 rtx x;
963 rtx *pinsn;
964 rtx valid_to;
965 int allow_hwreg;
967 rtx p;
969 for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
970 p = PREV_INSN (p))
971 if (INSN_P (p))
973 rtx set = single_set (p);
974 rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
976 if (set && rtx_equal_p (x, SET_DEST (set)))
978 rtx src = SET_SRC (set);
980 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
981 src = XEXP (note, 0);
983 if ((valid_to == NULL_RTX
984 || ! modified_between_p (src, PREV_INSN (p), valid_to))
985 /* Reject hard registers because we don't usually want
986 to use them; we'd rather use a pseudo. */
987 && (! (GET_CODE (src) == REG
988 && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
990 *pinsn = p;
991 return src;
995 /* If set in non-simple way, we don't have a value. */
996 if (reg_set_p (x, p))
997 break;
1000 return x;
1003 /* Return nonzero if register in range [REGNO, ENDREGNO)
1004 appears either explicitly or implicitly in X
1005 other than being stored into.
1007 References contained within the substructure at LOC do not count.
1008 LOC may be zero, meaning don't ignore anything. */
1011 refers_to_regno_p (regno, endregno, x, loc)
1012 unsigned int regno, endregno;
1013 rtx x;
1014 rtx *loc;
1016 int i;
1017 unsigned int x_regno;
1018 RTX_CODE code;
1019 const char *fmt;
1021 repeat:
1022 /* The contents of a REG_NONNEG note is always zero, so we must come here
1023 upon repeat in case the last REG_NOTE is a REG_NONNEG note. */
1024 if (x == 0)
1025 return 0;
1027 code = GET_CODE (x);
1029 switch (code)
1031 case REG:
1032 x_regno = REGNO (x);
1034 /* If we modifying the stack, frame, or argument pointer, it will
1035 clobber a virtual register. In fact, we could be more precise,
1036 but it isn't worth it. */
1037 if ((x_regno == STACK_POINTER_REGNUM
1038 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1039 || x_regno == ARG_POINTER_REGNUM
1040 #endif
1041 || x_regno == FRAME_POINTER_REGNUM)
1042 && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
1043 return 1;
1045 return (endregno > x_regno
1046 && regno < x_regno + (x_regno < FIRST_PSEUDO_REGISTER
1047 ? HARD_REGNO_NREGS (x_regno, GET_MODE (x))
1048 : 1));
1050 case SUBREG:
1051 /* If this is a SUBREG of a hard reg, we can see exactly which
1052 registers are being modified. Otherwise, handle normally. */
1053 if (GET_CODE (SUBREG_REG (x)) == REG
1054 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
1056 unsigned int inner_regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
1057 unsigned int inner_endregno
1058 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
1059 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
1061 return endregno > inner_regno && regno < inner_endregno;
1063 break;
1065 case CLOBBER:
1066 case SET:
1067 if (&SET_DEST (x) != loc
1068 /* Note setting a SUBREG counts as referring to the REG it is in for
1069 a pseudo but not for hard registers since we can
1070 treat each word individually. */
1071 && ((GET_CODE (SET_DEST (x)) == SUBREG
1072 && loc != &SUBREG_REG (SET_DEST (x))
1073 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
1074 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
1075 && refers_to_regno_p (regno, endregno,
1076 SUBREG_REG (SET_DEST (x)), loc))
1077 || (GET_CODE (SET_DEST (x)) != REG
1078 && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
1079 return 1;
1081 if (code == CLOBBER || loc == &SET_SRC (x))
1082 return 0;
1083 x = SET_SRC (x);
1084 goto repeat;
1086 default:
1087 break;
1090 /* X does not match, so try its subexpressions. */
1092 fmt = GET_RTX_FORMAT (code);
1093 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1095 if (fmt[i] == 'e' && loc != &XEXP (x, i))
1097 if (i == 0)
1099 x = XEXP (x, 0);
1100 goto repeat;
1102 else
1103 if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
1104 return 1;
1106 else if (fmt[i] == 'E')
1108 register int j;
1109 for (j = XVECLEN (x, i) - 1; j >=0; j--)
1110 if (loc != &XVECEXP (x, i, j)
1111 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
1112 return 1;
1115 return 0;
1118 /* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
1119 we check if any register number in X conflicts with the relevant register
1120 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
1121 contains a MEM (we don't bother checking for memory addresses that can't
1122 conflict because we expect this to be a rare case. */
1125 reg_overlap_mentioned_p (x, in)
1126 rtx x, in;
1128 unsigned int regno, endregno;
1130 /* Overly conservative. */
1131 if (GET_CODE (x) == STRICT_LOW_PART)
1132 x = XEXP (x, 0);
1134 /* If either argument is a constant, then modifying X can not affect IN. */
1135 if (CONSTANT_P (x) || CONSTANT_P (in))
1136 return 0;
1138 switch (GET_CODE (x))
1140 case SUBREG:
1141 regno = REGNO (SUBREG_REG (x));
1142 if (regno < FIRST_PSEUDO_REGISTER)
1143 regno += SUBREG_WORD (x);
1144 goto do_reg;
1146 case REG:
1147 regno = REGNO (x);
1148 do_reg:
1149 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
1150 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
1151 return refers_to_regno_p (regno, endregno, in, NULL_PTR);
1153 case MEM:
1155 const char *fmt;
1156 int i;
1158 if (GET_CODE (in) == MEM)
1159 return 1;
1161 fmt = GET_RTX_FORMAT (GET_CODE (in));
1162 for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
1163 if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
1164 return 1;
1166 return 0;
1169 case SCRATCH:
1170 case PC:
1171 case CC0:
1172 return reg_mentioned_p (x, in);
1174 case PARALLEL:
1176 int i, n;
1178 /* Check for a NULL entry, used to indicate that the parameter goes
1179 both on the stack and in registers. */
1180 if (XEXP (XVECEXP (x, 0, 0), 0))
1181 i = 0;
1182 else
1183 i = 1;
1185 /* If any register in here refers to it we return true. */
1186 for (n = XVECLEN (x, 0); i < n; ++i)
1187 if (reg_overlap_mentioned_p (XEXP (XVECEXP (x, 0, i), 0), in))
1188 return 1;
1189 return 0;
1192 default:
1193 break;
1196 abort ();
1199 /* Used for communications between the next few functions. */
1201 static int reg_set_last_unknown;
1202 static rtx reg_set_last_value;
1203 static unsigned int reg_set_last_first_regno, reg_set_last_last_regno;
1205 /* Called via note_stores from reg_set_last. */
1207 static void
1208 reg_set_last_1 (x, pat, data)
1209 rtx x;
1210 rtx pat;
1211 void *data ATTRIBUTE_UNUSED;
1213 unsigned int first, last;
1215 /* If X is not a register, or is not one in the range we care
1216 about, ignore. */
1217 if (GET_CODE (x) != REG)
1218 return;
1220 first = REGNO (x);
1221 last = first + (first < FIRST_PSEUDO_REGISTER
1222 ? HARD_REGNO_NREGS (first, GET_MODE (x)) : 1);
1224 if (first >= reg_set_last_last_regno
1225 || last <= reg_set_last_first_regno)
1226 return;
1228 /* If this is a CLOBBER or is some complex LHS, or doesn't modify
1229 exactly the registers we care about, show we don't know the value. */
1230 if (GET_CODE (pat) == CLOBBER || SET_DEST (pat) != x
1231 || first != reg_set_last_first_regno
1232 || last != reg_set_last_last_regno)
1233 reg_set_last_unknown = 1;
1234 else
1235 reg_set_last_value = SET_SRC (pat);
1238 /* Return the last value to which REG was set prior to INSN. If we can't
1239 find it easily, return 0.
1241 We only return a REG, SUBREG, or constant because it is too hard to
1242 check if a MEM remains unchanged. */
1245 reg_set_last (x, insn)
1246 rtx x;
1247 rtx insn;
1249 rtx orig_insn = insn;
1251 reg_set_last_first_regno = REGNO (x);
1253 reg_set_last_last_regno
1254 = reg_set_last_first_regno
1255 + (reg_set_last_first_regno < FIRST_PSEUDO_REGISTER
1256 ? HARD_REGNO_NREGS (reg_set_last_first_regno, GET_MODE (x)) : 1);
1258 reg_set_last_unknown = 0;
1259 reg_set_last_value = 0;
1261 /* Scan backwards until reg_set_last_1 changed one of the above flags.
1262 Stop when we reach a label or X is a hard reg and we reach a
1263 CALL_INSN (if reg_set_last_last_regno is a hard reg).
1265 If we find a set of X, ensure that its SET_SRC remains unchanged. */
1267 /* We compare with <= here, because reg_set_last_last_regno
1268 is actually the number of the first reg *not* in X. */
1269 for (;
1270 insn && GET_CODE (insn) != CODE_LABEL
1271 && ! (GET_CODE (insn) == CALL_INSN
1272 && reg_set_last_last_regno <= FIRST_PSEUDO_REGISTER);
1273 insn = PREV_INSN (insn))
1274 if (INSN_P (insn))
1276 note_stores (PATTERN (insn), reg_set_last_1, NULL);
1277 if (reg_set_last_unknown)
1278 return 0;
1279 else if (reg_set_last_value)
1281 if (CONSTANT_P (reg_set_last_value)
1282 || ((GET_CODE (reg_set_last_value) == REG
1283 || GET_CODE (reg_set_last_value) == SUBREG)
1284 && ! reg_set_between_p (reg_set_last_value,
1285 insn, orig_insn)))
1286 return reg_set_last_value;
1287 else
1288 return 0;
1292 return 0;
1295 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1296 (X would be the pattern of an insn).
1297 FUN receives two arguments:
1298 the REG, MEM, CC0 or PC being stored in or clobbered,
1299 the SET or CLOBBER rtx that does the store.
1301 If the item being stored in or clobbered is a SUBREG of a hard register,
1302 the SUBREG will be passed. */
1304 void
1305 note_stores (x, fun, data)
1306 register rtx x;
1307 void (*fun) PARAMS ((rtx, rtx, void *));
1308 void *data;
1310 if (GET_CODE (x) == COND_EXEC)
1311 x = COND_EXEC_CODE (x);
1312 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1314 register rtx dest = SET_DEST (x);
1315 while ((GET_CODE (dest) == SUBREG
1316 && (GET_CODE (SUBREG_REG (dest)) != REG
1317 || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1318 || GET_CODE (dest) == ZERO_EXTRACT
1319 || GET_CODE (dest) == SIGN_EXTRACT
1320 || GET_CODE (dest) == STRICT_LOW_PART)
1321 dest = XEXP (dest, 0);
1323 if (GET_CODE (dest) == PARALLEL
1324 && GET_MODE (dest) == BLKmode)
1326 register int i;
1327 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1328 (*fun) (SET_DEST (XVECEXP (dest, 0, i)), x, data);
1330 else
1331 (*fun) (dest, x, data);
1333 else if (GET_CODE (x) == PARALLEL)
1335 register int i;
1336 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1338 register rtx y = XVECEXP (x, 0, i);
1339 if (GET_CODE (y) == COND_EXEC)
1340 y = COND_EXEC_CODE (y);
1341 if (GET_CODE (y) == SET || GET_CODE (y) == CLOBBER)
1343 register rtx dest = SET_DEST (y);
1344 while ((GET_CODE (dest) == SUBREG
1345 && (GET_CODE (SUBREG_REG (dest)) != REG
1346 || (REGNO (SUBREG_REG (dest))
1347 >= FIRST_PSEUDO_REGISTER)))
1348 || GET_CODE (dest) == ZERO_EXTRACT
1349 || GET_CODE (dest) == SIGN_EXTRACT
1350 || GET_CODE (dest) == STRICT_LOW_PART)
1351 dest = XEXP (dest, 0);
1352 if (GET_CODE (dest) == PARALLEL
1353 && GET_MODE (dest) == BLKmode)
1355 register int i;
1357 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1358 (*fun) (SET_DEST (XVECEXP (dest, 0, i)), y, data);
1360 else
1361 (*fun) (dest, y, data);
1367 /* Return nonzero if X's old contents don't survive after INSN.
1368 This will be true if X is (cc0) or if X is a register and
1369 X dies in INSN or because INSN entirely sets X.
1371 "Entirely set" means set directly and not through a SUBREG,
1372 ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1373 Likewise, REG_INC does not count.
1375 REG may be a hard or pseudo reg. Renumbering is not taken into account,
1376 but for this use that makes no difference, since regs don't overlap
1377 during their lifetimes. Therefore, this function may be used
1378 at any time after deaths have been computed (in flow.c).
1380 If REG is a hard reg that occupies multiple machine registers, this
1381 function will only return 1 if each of those registers will be replaced
1382 by INSN. */
1385 dead_or_set_p (insn, x)
1386 rtx insn;
1387 rtx x;
1389 unsigned int regno, last_regno;
1390 unsigned int i;
1392 /* Can't use cc0_rtx below since this file is used by genattrtab.c. */
1393 if (GET_CODE (x) == CC0)
1394 return 1;
1396 if (GET_CODE (x) != REG)
1397 abort ();
1399 regno = REGNO (x);
1400 last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1401 : regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1403 for (i = regno; i <= last_regno; i++)
1404 if (! dead_or_set_regno_p (insn, i))
1405 return 0;
1407 return 1;
1410 /* Utility function for dead_or_set_p to check an individual register. Also
1411 called from flow.c. */
1414 dead_or_set_regno_p (insn, test_regno)
1415 rtx insn;
1416 unsigned int test_regno;
1418 unsigned int regno, endregno;
1419 rtx pattern;
1421 /* See if there is a death note for something that includes TEST_REGNO. */
1422 if (find_regno_note (insn, REG_DEAD, test_regno))
1423 return 1;
1425 if (GET_CODE (insn) == CALL_INSN
1426 && find_regno_fusage (insn, CLOBBER, test_regno))
1427 return 1;
1429 pattern = PATTERN (insn);
1431 if (GET_CODE (pattern) == COND_EXEC)
1432 pattern = COND_EXEC_CODE (pattern);
1434 if (GET_CODE (pattern) == SET)
1436 rtx dest = SET_DEST (PATTERN (insn));
1438 /* A value is totally replaced if it is the destination or the
1439 destination is a SUBREG of REGNO that does not change the number of
1440 words in it. */
1441 if (GET_CODE (dest) == SUBREG
1442 && (((GET_MODE_SIZE (GET_MODE (dest))
1443 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1444 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1445 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1446 dest = SUBREG_REG (dest);
1448 if (GET_CODE (dest) != REG)
1449 return 0;
1451 regno = REGNO (dest);
1452 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1453 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1455 return (test_regno >= regno && test_regno < endregno);
1457 else if (GET_CODE (pattern) == PARALLEL)
1459 register int i;
1461 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
1463 rtx body = XVECEXP (pattern, 0, i);
1465 if (GET_CODE (body) == COND_EXEC)
1466 body = COND_EXEC_CODE (body);
1468 if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1470 rtx dest = SET_DEST (body);
1472 if (GET_CODE (dest) == SUBREG
1473 && (((GET_MODE_SIZE (GET_MODE (dest))
1474 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1475 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1476 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1477 dest = SUBREG_REG (dest);
1479 if (GET_CODE (dest) != REG)
1480 continue;
1482 regno = REGNO (dest);
1483 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1484 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1486 if (test_regno >= regno && test_regno < endregno)
1487 return 1;
1492 return 0;
1495 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1496 If DATUM is nonzero, look for one whose datum is DATUM. */
1499 find_reg_note (insn, kind, datum)
1500 rtx insn;
1501 enum reg_note kind;
1502 rtx datum;
1504 register rtx link;
1506 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1507 if (! INSN_P (insn))
1508 return 0;
1510 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1511 if (REG_NOTE_KIND (link) == kind
1512 && (datum == 0 || datum == XEXP (link, 0)))
1513 return link;
1514 return 0;
1517 /* Return the reg-note of kind KIND in insn INSN which applies to register
1518 number REGNO, if any. Return 0 if there is no such reg-note. Note that
1519 the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1520 it might be the case that the note overlaps REGNO. */
1523 find_regno_note (insn, kind, regno)
1524 rtx insn;
1525 enum reg_note kind;
1526 unsigned int regno;
1528 register rtx link;
1530 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1531 if (! INSN_P (insn))
1532 return 0;
1534 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1535 if (REG_NOTE_KIND (link) == kind
1536 /* Verify that it is a register, so that scratch and MEM won't cause a
1537 problem here. */
1538 && GET_CODE (XEXP (link, 0)) == REG
1539 && REGNO (XEXP (link, 0)) <= regno
1540 && ((REGNO (XEXP (link, 0))
1541 + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1542 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1543 GET_MODE (XEXP (link, 0)))))
1544 > regno))
1545 return link;
1546 return 0;
1549 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1550 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1553 find_reg_fusage (insn, code, datum)
1554 rtx insn;
1555 enum rtx_code code;
1556 rtx datum;
1558 /* If it's not a CALL_INSN, it can't possibly have a
1559 CALL_INSN_FUNCTION_USAGE field, so don't bother checking. */
1560 if (GET_CODE (insn) != CALL_INSN)
1561 return 0;
1563 if (! datum)
1564 abort();
1566 if (GET_CODE (datum) != REG)
1568 register rtx link;
1570 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1571 link;
1572 link = XEXP (link, 1))
1573 if (GET_CODE (XEXP (link, 0)) == code
1574 && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1575 return 1;
1577 else
1579 unsigned int regno = REGNO (datum);
1581 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1582 to pseudo registers, so don't bother checking. */
1584 if (regno < FIRST_PSEUDO_REGISTER)
1586 unsigned int end_regno
1587 = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1588 unsigned int i;
1590 for (i = regno; i < end_regno; i++)
1591 if (find_regno_fusage (insn, code, i))
1592 return 1;
1596 return 0;
1599 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1600 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1603 find_regno_fusage (insn, code, regno)
1604 rtx insn;
1605 enum rtx_code code;
1606 unsigned int regno;
1608 register rtx link;
1610 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1611 to pseudo registers, so don't bother checking. */
1613 if (regno >= FIRST_PSEUDO_REGISTER
1614 || GET_CODE (insn) != CALL_INSN )
1615 return 0;
1617 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1619 unsigned int regnote;
1620 rtx op, reg;
1622 if (GET_CODE (op = XEXP (link, 0)) == code
1623 && GET_CODE (reg = XEXP (op, 0)) == REG
1624 && (regnote = REGNO (reg)) <= regno
1625 && regnote + HARD_REGNO_NREGS (regnote, GET_MODE (reg)) > regno)
1626 return 1;
1629 return 0;
1632 /* Remove register note NOTE from the REG_NOTES of INSN. */
1634 void
1635 remove_note (insn, note)
1636 register rtx insn;
1637 register rtx note;
1639 register rtx link;
1641 if (note == NULL_RTX)
1642 return;
1644 if (REG_NOTES (insn) == note)
1646 REG_NOTES (insn) = XEXP (note, 1);
1647 return;
1650 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1651 if (XEXP (link, 1) == note)
1653 XEXP (link, 1) = XEXP (note, 1);
1654 return;
1657 abort ();
1660 /* Search LISTP (an EXPR_LIST) for NODE and remove NODE from the list
1661 if it is found.
1663 A simple equality test is used to determine if NODE is on the
1664 EXPR_LIST. */
1666 void
1667 remove_node_from_expr_list (node, listp)
1668 rtx node;
1669 rtx *listp;
1671 rtx temp = *listp;
1672 rtx prev = NULL_RTX;
1674 while (temp)
1676 if (node == XEXP (temp, 0))
1678 /* Splice the node out of the list. */
1679 if (prev)
1680 XEXP (prev, 1) = XEXP (temp, 1);
1681 else
1682 *listp = XEXP (temp, 1);
1684 return;
1686 temp = XEXP (temp, 1);
1690 /* Nonzero if X contains any volatile instructions. These are instructions
1691 which may cause unpredictable machine state instructions, and thus no
1692 instructions should be moved or combined across them. This includes
1693 only volatile asms and UNSPEC_VOLATILE instructions. */
1696 volatile_insn_p (x)
1697 rtx x;
1699 register RTX_CODE code;
1701 code = GET_CODE (x);
1702 switch (code)
1704 case LABEL_REF:
1705 case SYMBOL_REF:
1706 case CONST_INT:
1707 case CONST:
1708 case CONST_DOUBLE:
1709 case CC0:
1710 case PC:
1711 case REG:
1712 case SCRATCH:
1713 case CLOBBER:
1714 case ASM_INPUT:
1715 case ADDR_VEC:
1716 case ADDR_DIFF_VEC:
1717 case CALL:
1718 case MEM:
1719 return 0;
1721 case UNSPEC_VOLATILE:
1722 /* case TRAP_IF: This isn't clear yet. */
1723 return 1;
1725 case ASM_OPERANDS:
1726 if (MEM_VOLATILE_P (x))
1727 return 1;
1729 default:
1730 break;
1733 /* Recursively scan the operands of this expression. */
1736 register const char *fmt = GET_RTX_FORMAT (code);
1737 register int i;
1739 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1741 if (fmt[i] == 'e')
1743 if (volatile_insn_p (XEXP (x, i)))
1744 return 1;
1746 else if (fmt[i] == 'E')
1748 register int j;
1749 for (j = 0; j < XVECLEN (x, i); j++)
1750 if (volatile_insn_p (XVECEXP (x, i, j)))
1751 return 1;
1755 return 0;
1758 /* Nonzero if X contains any volatile memory references
1759 UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions. */
1762 volatile_refs_p (x)
1763 rtx x;
1765 register RTX_CODE code;
1767 code = GET_CODE (x);
1768 switch (code)
1770 case LABEL_REF:
1771 case SYMBOL_REF:
1772 case CONST_INT:
1773 case CONST:
1774 case CONST_DOUBLE:
1775 case CC0:
1776 case PC:
1777 case REG:
1778 case SCRATCH:
1779 case CLOBBER:
1780 case ASM_INPUT:
1781 case ADDR_VEC:
1782 case ADDR_DIFF_VEC:
1783 return 0;
1785 case CALL:
1786 case UNSPEC_VOLATILE:
1787 /* case TRAP_IF: This isn't clear yet. */
1788 return 1;
1790 case MEM:
1791 case ASM_OPERANDS:
1792 if (MEM_VOLATILE_P (x))
1793 return 1;
1795 default:
1796 break;
1799 /* Recursively scan the operands of this expression. */
1802 register const char *fmt = GET_RTX_FORMAT (code);
1803 register int i;
1805 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1807 if (fmt[i] == 'e')
1809 if (volatile_refs_p (XEXP (x, i)))
1810 return 1;
1812 else if (fmt[i] == 'E')
1814 register int j;
1815 for (j = 0; j < XVECLEN (x, i); j++)
1816 if (volatile_refs_p (XVECEXP (x, i, j)))
1817 return 1;
1821 return 0;
1824 /* Similar to above, except that it also rejects register pre- and post-
1825 incrementing. */
1828 side_effects_p (x)
1829 rtx x;
1831 register RTX_CODE code;
1833 code = GET_CODE (x);
1834 switch (code)
1836 case LABEL_REF:
1837 case SYMBOL_REF:
1838 case CONST_INT:
1839 case CONST:
1840 case CONST_DOUBLE:
1841 case CC0:
1842 case PC:
1843 case REG:
1844 case SCRATCH:
1845 case ASM_INPUT:
1846 case ADDR_VEC:
1847 case ADDR_DIFF_VEC:
1848 return 0;
1850 case CLOBBER:
1851 /* Reject CLOBBER with a non-VOID mode. These are made by combine.c
1852 when some combination can't be done. If we see one, don't think
1853 that we can simplify the expression. */
1854 return (GET_MODE (x) != VOIDmode);
1856 case PRE_INC:
1857 case PRE_DEC:
1858 case POST_INC:
1859 case POST_DEC:
1860 case PRE_MODIFY:
1861 case POST_MODIFY:
1862 case CALL:
1863 case UNSPEC_VOLATILE:
1864 /* case TRAP_IF: This isn't clear yet. */
1865 return 1;
1867 case MEM:
1868 case ASM_OPERANDS:
1869 if (MEM_VOLATILE_P (x))
1870 return 1;
1872 default:
1873 break;
1876 /* Recursively scan the operands of this expression. */
1879 register const char *fmt = GET_RTX_FORMAT (code);
1880 register int i;
1882 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1884 if (fmt[i] == 'e')
1886 if (side_effects_p (XEXP (x, i)))
1887 return 1;
1889 else if (fmt[i] == 'E')
1891 register int j;
1892 for (j = 0; j < XVECLEN (x, i); j++)
1893 if (side_effects_p (XVECEXP (x, i, j)))
1894 return 1;
1898 return 0;
1901 /* Return nonzero if evaluating rtx X might cause a trap. */
1904 may_trap_p (x)
1905 rtx x;
1907 int i;
1908 enum rtx_code code;
1909 const char *fmt;
1911 if (x == 0)
1912 return 0;
1913 code = GET_CODE (x);
1914 switch (code)
1916 /* Handle these cases quickly. */
1917 case CONST_INT:
1918 case CONST_DOUBLE:
1919 case SYMBOL_REF:
1920 case LABEL_REF:
1921 case CONST:
1922 case PC:
1923 case CC0:
1924 case REG:
1925 case SCRATCH:
1926 return 0;
1928 case ASM_INPUT:
1929 case UNSPEC_VOLATILE:
1930 case TRAP_IF:
1931 return 1;
1933 case ASM_OPERANDS:
1934 return MEM_VOLATILE_P (x);
1936 /* Memory ref can trap unless it's a static var or a stack slot. */
1937 case MEM:
1938 return rtx_addr_can_trap_p (XEXP (x, 0));
1940 /* Division by a non-constant might trap. */
1941 case DIV:
1942 case MOD:
1943 case UDIV:
1944 case UMOD:
1945 if (! CONSTANT_P (XEXP (x, 1))
1946 || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1947 return 1;
1948 /* This was const0_rtx, but by not using that,
1949 we can link this file into other programs. */
1950 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
1951 return 1;
1952 break;
1954 case EXPR_LIST:
1955 /* An EXPR_LIST is used to represent a function call. This
1956 certainly may trap. */
1957 return 1;
1959 case GE:
1960 case GT:
1961 case LE:
1962 case LT:
1963 case COMPARE:
1964 /* Some floating point comparisons may trap. */
1965 /* ??? There is no machine independent way to check for tests that trap
1966 when COMPARE is used, though many targets do make this distinction.
1967 For instance, sparc uses CCFPE for compares which generate exceptions
1968 and CCFP for compares which do not generate exceptions. */
1969 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1970 return 1;
1971 /* But often the compare has some CC mode, so check operand
1972 modes as well. */
1973 if (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) == MODE_FLOAT
1974 || GET_MODE_CLASS (GET_MODE (XEXP (x, 1))) == MODE_FLOAT)
1975 return 1;
1976 break;
1978 default:
1979 /* Any floating arithmetic may trap. */
1980 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1981 return 1;
1984 fmt = GET_RTX_FORMAT (code);
1985 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1987 if (fmt[i] == 'e')
1989 if (may_trap_p (XEXP (x, i)))
1990 return 1;
1992 else if (fmt[i] == 'E')
1994 register int j;
1995 for (j = 0; j < XVECLEN (x, i); j++)
1996 if (may_trap_p (XVECEXP (x, i, j)))
1997 return 1;
2000 return 0;
2003 /* Return nonzero if X contains a comparison that is not either EQ or NE,
2004 i.e., an inequality. */
2007 inequality_comparisons_p (x)
2008 rtx x;
2010 register const char *fmt;
2011 register int len, i;
2012 register enum rtx_code code = GET_CODE (x);
2014 switch (code)
2016 case REG:
2017 case SCRATCH:
2018 case PC:
2019 case CC0:
2020 case CONST_INT:
2021 case CONST_DOUBLE:
2022 case CONST:
2023 case LABEL_REF:
2024 case SYMBOL_REF:
2025 return 0;
2027 case LT:
2028 case LTU:
2029 case GT:
2030 case GTU:
2031 case LE:
2032 case LEU:
2033 case GE:
2034 case GEU:
2035 return 1;
2037 default:
2038 break;
2041 len = GET_RTX_LENGTH (code);
2042 fmt = GET_RTX_FORMAT (code);
2044 for (i = 0; i < len; i++)
2046 if (fmt[i] == 'e')
2048 if (inequality_comparisons_p (XEXP (x, i)))
2049 return 1;
2051 else if (fmt[i] == 'E')
2053 register int j;
2054 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2055 if (inequality_comparisons_p (XVECEXP (x, i, j)))
2056 return 1;
2060 return 0;
2063 /* Replace any occurrence of FROM in X with TO. The function does
2064 not enter into CONST_DOUBLE for the replace.
2066 Note that copying is not done so X must not be shared unless all copies
2067 are to be modified. */
2070 replace_rtx (x, from, to)
2071 rtx x, from, to;
2073 register int i, j;
2074 register const char *fmt;
2076 /* The following prevents loops occurrence when we change MEM in
2077 CONST_DOUBLE onto the same CONST_DOUBLE. */
2078 if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
2079 return x;
2081 if (x == from)
2082 return to;
2084 /* Allow this function to make replacements in EXPR_LISTs. */
2085 if (x == 0)
2086 return 0;
2088 fmt = GET_RTX_FORMAT (GET_CODE (x));
2089 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2091 if (fmt[i] == 'e')
2092 XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
2093 else if (fmt[i] == 'E')
2094 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2095 XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
2098 return x;
2101 /* Throughout the rtx X, replace many registers according to REG_MAP.
2102 Return the replacement for X (which may be X with altered contents).
2103 REG_MAP[R] is the replacement for register R, or 0 for don't replace.
2104 NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
2106 We only support REG_MAP entries of REG or SUBREG. Also, hard registers
2107 should not be mapped to pseudos or vice versa since validate_change
2108 is not called.
2110 If REPLACE_DEST is 1, replacements are also done in destinations;
2111 otherwise, only sources are replaced. */
2114 replace_regs (x, reg_map, nregs, replace_dest)
2115 rtx x;
2116 rtx *reg_map;
2117 unsigned int nregs;
2118 int replace_dest;
2120 register enum rtx_code code;
2121 register int i;
2122 register const char *fmt;
2124 if (x == 0)
2125 return x;
2127 code = GET_CODE (x);
2128 switch (code)
2130 case SCRATCH:
2131 case PC:
2132 case CC0:
2133 case CONST_INT:
2134 case CONST_DOUBLE:
2135 case CONST:
2136 case SYMBOL_REF:
2137 case LABEL_REF:
2138 return x;
2140 case REG:
2141 /* Verify that the register has an entry before trying to access it. */
2142 if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
2144 /* SUBREGs can't be shared. Always return a copy to ensure that if
2145 this replacement occurs more than once then each instance will
2146 get distinct rtx. */
2147 if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
2148 return copy_rtx (reg_map[REGNO (x)]);
2149 return reg_map[REGNO (x)];
2151 return x;
2153 case SUBREG:
2154 /* Prevent making nested SUBREGs. */
2155 if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
2156 && reg_map[REGNO (SUBREG_REG (x))] != 0
2157 && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
2159 rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
2160 rtx map_inner = SUBREG_REG (map_val);
2162 if (GET_MODE (x) == GET_MODE (map_inner))
2163 return map_inner;
2164 else
2166 /* We cannot call gen_rtx here since we may be linked with
2167 genattrtab.c. */
2168 /* Let's try clobbering the incoming SUBREG and see
2169 if this is really safe. */
2170 SUBREG_REG (x) = map_inner;
2171 SUBREG_WORD (x) += SUBREG_WORD (map_val);
2172 return x;
2173 #if 0
2174 rtx new = rtx_alloc (SUBREG);
2175 PUT_MODE (new, GET_MODE (x));
2176 SUBREG_REG (new) = map_inner;
2177 SUBREG_WORD (new) = SUBREG_WORD (x) + SUBREG_WORD (map_val);
2178 #endif
2181 break;
2183 case SET:
2184 if (replace_dest)
2185 SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
2187 else if (GET_CODE (SET_DEST (x)) == MEM
2188 || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2189 /* Even if we are not to replace destinations, replace register if it
2190 is CONTAINED in destination (destination is memory or
2191 STRICT_LOW_PART). */
2192 XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
2193 reg_map, nregs, 0);
2194 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2195 /* Similarly, for ZERO_EXTRACT we replace all operands. */
2196 break;
2198 SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2199 return x;
2201 default:
2202 break;
2205 fmt = GET_RTX_FORMAT (code);
2206 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2208 if (fmt[i] == 'e')
2209 XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
2210 else if (fmt[i] == 'E')
2212 register int j;
2213 for (j = 0; j < XVECLEN (x, i); j++)
2214 XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2215 nregs, replace_dest);
2218 return x;
2221 /* Return 1 if X, the SRC_SRC of SET of (pc) contain a REG or MEM that is
2222 not in the constant pool and not in the condition of an IF_THEN_ELSE. */
2224 static int
2225 jmp_uses_reg_or_mem (x)
2226 rtx x;
2228 enum rtx_code code = GET_CODE (x);
2229 int i, j;
2230 const char *fmt;
2232 switch (code)
2234 case CONST:
2235 case LABEL_REF:
2236 case PC:
2237 return 0;
2239 case REG:
2240 return 1;
2242 case MEM:
2243 return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2244 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2246 case IF_THEN_ELSE:
2247 return (jmp_uses_reg_or_mem (XEXP (x, 1))
2248 || jmp_uses_reg_or_mem (XEXP (x, 2)));
2250 case PLUS: case MINUS: case MULT:
2251 return (jmp_uses_reg_or_mem (XEXP (x, 0))
2252 || jmp_uses_reg_or_mem (XEXP (x, 1)));
2254 default:
2255 break;
2258 fmt = GET_RTX_FORMAT (code);
2259 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2261 if (fmt[i] == 'e'
2262 && jmp_uses_reg_or_mem (XEXP (x, i)))
2263 return 1;
2265 else if (fmt[i] == 'E')
2266 for (j = 0; j < XVECLEN (x, i); j++)
2267 if (jmp_uses_reg_or_mem (XVECEXP (x, i, j)))
2268 return 1;
2271 return 0;
2274 /* Return nonzero if INSN is an indirect jump (aka computed jump).
2276 Tablejumps and casesi insns are not considered indirect jumps;
2277 we can recognize them by a (use (label_ref)). */
2280 computed_jump_p (insn)
2281 rtx insn;
2283 int i;
2284 if (GET_CODE (insn) == JUMP_INSN)
2286 rtx pat = PATTERN (insn);
2288 if (GET_CODE (pat) == PARALLEL)
2290 int len = XVECLEN (pat, 0);
2291 int has_use_labelref = 0;
2293 for (i = len - 1; i >= 0; i--)
2294 if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2295 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2296 == LABEL_REF))
2297 has_use_labelref = 1;
2299 if (! has_use_labelref)
2300 for (i = len - 1; i >= 0; i--)
2301 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2302 && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2303 && jmp_uses_reg_or_mem (SET_SRC (XVECEXP (pat, 0, i))))
2304 return 1;
2306 else if (GET_CODE (pat) == SET
2307 && SET_DEST (pat) == pc_rtx
2308 && jmp_uses_reg_or_mem (SET_SRC (pat)))
2309 return 1;
2311 return 0;
2314 /* Traverse X via depth-first search, calling F for each
2315 sub-expression (including X itself). F is also passed the DATA.
2316 If F returns -1, do not traverse sub-expressions, but continue
2317 traversing the rest of the tree. If F ever returns any other
2318 non-zero value, stop the traversal, and return the value returned
2319 by F. Otherwise, return 0. This function does not traverse inside
2320 tree structure that contains RTX_EXPRs, or into sub-expressions
2321 whose format code is `0' since it is not known whether or not those
2322 codes are actually RTL.
2324 This routine is very general, and could (should?) be used to
2325 implement many of the other routines in this file. */
2328 for_each_rtx (x, f, data)
2329 rtx *x;
2330 rtx_function f;
2331 void *data;
2333 int result;
2334 int length;
2335 const char* format;
2336 int i;
2338 /* Call F on X. */
2339 result = (*f)(x, data);
2340 if (result == -1)
2341 /* Do not traverse sub-expressions. */
2342 return 0;
2343 else if (result != 0)
2344 /* Stop the traversal. */
2345 return result;
2347 if (*x == NULL_RTX)
2348 /* There are no sub-expressions. */
2349 return 0;
2351 length = GET_RTX_LENGTH (GET_CODE (*x));
2352 format = GET_RTX_FORMAT (GET_CODE (*x));
2354 for (i = 0; i < length; ++i)
2356 switch (format[i])
2358 case 'e':
2359 result = for_each_rtx (&XEXP (*x, i), f, data);
2360 if (result != 0)
2361 return result;
2362 break;
2364 case 'V':
2365 case 'E':
2366 if (XVEC (*x, i) != 0)
2368 int j;
2369 for (j = 0; j < XVECLEN (*x, i); ++j)
2371 result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2372 if (result != 0)
2373 return result;
2376 break;
2378 default:
2379 /* Nothing to do. */
2380 break;
2385 return 0;
2388 /* Searches X for any reference to REGNO, returning the rtx of the
2389 reference found if any. Otherwise, returns NULL_RTX. */
2392 regno_use_in (regno, x)
2393 unsigned int regno;
2394 rtx x;
2396 register const char *fmt;
2397 int i, j;
2398 rtx tem;
2400 if (GET_CODE (x) == REG && REGNO (x) == regno)
2401 return x;
2403 fmt = GET_RTX_FORMAT (GET_CODE (x));
2404 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2406 if (fmt[i] == 'e')
2408 if ((tem = regno_use_in (regno, XEXP (x, i))))
2409 return tem;
2411 else if (fmt[i] == 'E')
2412 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2413 if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2414 return tem;
2417 return NULL_RTX;
2421 /* Return 1 if X is an autoincrement side effect and the register is
2422 not the stack pointer. */
2424 auto_inc_p (x)
2425 rtx x;
2427 switch (GET_CODE (x))
2429 case PRE_INC:
2430 case POST_INC:
2431 case PRE_DEC:
2432 case POST_DEC:
2433 case PRE_MODIFY:
2434 case POST_MODIFY:
2435 /* There are no REG_INC notes for SP. */
2436 if (XEXP (x, 0) != stack_pointer_rtx)
2437 return 1;
2438 default:
2439 break;
2441 return 0;
2444 /* Return 1 if the sequence of instructions beginning with FROM and up
2445 to and including TO is safe to move. If NEW_TO is non-NULL, and
2446 the sequence is not already safe to move, but can be easily
2447 extended to a sequence which is safe, then NEW_TO will point to the
2448 end of the extended sequence.
2450 For now, this function only checks that the region contains whole
2451 exception regiongs, but it could be extended to check additional
2452 conditions as well. */
2455 insns_safe_to_move_p (from, to, new_to)
2456 rtx from;
2457 rtx to;
2458 rtx *new_to;
2460 int eh_region_count = 0;
2461 int past_to_p = 0;
2462 rtx r = from;
2464 /* By default, assume the end of the region will be what was
2465 suggested. */
2466 if (new_to)
2467 *new_to = to;
2469 while (r)
2471 if (GET_CODE (r) == NOTE)
2473 switch (NOTE_LINE_NUMBER (r))
2475 case NOTE_INSN_EH_REGION_BEG:
2476 ++eh_region_count;
2477 break;
2479 case NOTE_INSN_EH_REGION_END:
2480 if (eh_region_count == 0)
2481 /* This sequence of instructions contains the end of
2482 an exception region, but not he beginning. Moving
2483 it will cause chaos. */
2484 return 0;
2486 --eh_region_count;
2487 break;
2489 default:
2490 break;
2493 else if (past_to_p)
2494 /* If we've passed TO, and we see a non-note instruction, we
2495 can't extend the sequence to a movable sequence. */
2496 return 0;
2498 if (r == to)
2500 if (!new_to)
2501 /* It's OK to move the sequence if there were matched sets of
2502 exception region notes. */
2503 return eh_region_count == 0;
2505 past_to_p = 1;
2508 /* It's OK to move the sequence if there were matched sets of
2509 exception region notes. */
2510 if (past_to_p && eh_region_count == 0)
2512 *new_to = r;
2513 return 1;
2516 /* Go to the next instruction. */
2517 r = NEXT_INSN (r);
2520 return 0;
2523 /* Return non-zero if IN contains a piece of rtl that has the address LOC */
2525 loc_mentioned_in_p (loc, in)
2526 rtx *loc, in;
2528 enum rtx_code code = GET_CODE (in);
2529 const char *fmt = GET_RTX_FORMAT (code);
2530 int i, j;
2532 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2534 if (loc == &in->fld[i].rtx)
2535 return 1;
2536 if (fmt[i] == 'e')
2538 if (loc_mentioned_in_p (loc, XEXP (in, i)))
2539 return 1;
2541 else if (fmt[i] == 'E')
2542 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
2543 if (loc_mentioned_in_p (loc, XVECEXP (in, i, j)))
2544 return 1;
2546 return 0;