(in m32rx patch): Replace "." with "@." when preceeded by a capital letter
[official-gcc.git] / gcc / rtlanal.c
blobeb79fe95476b6bc59be636b4f0c0c4908e847f78
1 /* Analyze RTL for C-Compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "toplev.h"
26 #include "rtl.h"
27 #include "hard-reg-set.h"
28 #include "tm_p.h"
30 /* Forward declarations */
31 static void set_of_1 PARAMS ((rtx, rtx, void *));
32 static void insn_dependent_p_1 PARAMS ((rtx, rtx, void *));
33 static int computed_jump_p_1 PARAMS ((rtx));
34 static void parms_set PARAMS ((rtx, rtx, void *));
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 RTX_CODE code = GET_CODE (x);
52 int i;
53 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 ADDRESSOF:
64 case CONST:
65 case CONST_INT:
66 case CONST_DOUBLE:
67 case SYMBOL_REF:
68 case LABEL_REF:
69 return 0;
71 case REG:
72 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
73 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
74 /* The arg pointer varies if it is not a fixed register. */
75 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
76 || RTX_UNCHANGING_P (x))
77 return 0;
78 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
79 /* ??? When call-clobbered, the value is stable modulo the restore
80 that must happen after a call. This currently screws up local-alloc
81 into believing that the restore is not needed. */
82 if (x == pic_offset_table_rtx)
83 return 0;
84 #endif
85 return 1;
87 case ASM_OPERANDS:
88 if (MEM_VOLATILE_P (x))
89 return 1;
91 /* FALLTHROUGH */
93 default:
94 break;
97 fmt = GET_RTX_FORMAT (code);
98 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
99 if (fmt[i] == 'e')
101 if (rtx_unstable_p (XEXP (x, i)))
102 return 1;
104 else if (fmt[i] == 'E')
106 int j;
107 for (j = 0; j < XVECLEN (x, i); j++)
108 if (rtx_unstable_p (XVECEXP (x, i, j)))
109 return 1;
112 return 0;
115 /* Return 1 if X has a value that can vary even between two
116 executions of the program. 0 means X can be compared reliably
117 against certain constants or near-constants.
118 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
119 zero, we are slightly more conservative.
120 The frame pointer and the arg pointer are considered constant. */
123 rtx_varies_p (x, for_alias)
124 rtx x;
125 int for_alias;
127 RTX_CODE code = GET_CODE (x);
128 int i;
129 const char *fmt;
131 switch (code)
133 case MEM:
134 return ! RTX_UNCHANGING_P (x) || rtx_varies_p (XEXP (x, 0), for_alias);
136 case QUEUED:
137 return 1;
139 case CONST:
140 case CONST_INT:
141 case CONST_DOUBLE:
142 case SYMBOL_REF:
143 case LABEL_REF:
144 return 0;
146 case REG:
147 /* Note that we have to test for the actual rtx used for the frame
148 and arg pointers and not just the register number in case we have
149 eliminated the frame and/or arg pointer and are using it
150 for pseudos. */
151 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
152 /* The arg pointer varies if it is not a fixed register. */
153 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
154 return 0;
155 if (x == pic_offset_table_rtx
156 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
157 /* ??? When call-clobbered, the value is stable modulo the restore
158 that must happen after a call. This currently screws up
159 local-alloc into believing that the restore is not needed, so we
160 must return 0 only if we are called from alias analysis. */
161 && for_alias
162 #endif
164 return 0;
165 return 1;
167 case LO_SUM:
168 /* The operand 0 of a LO_SUM is considered constant
169 (in fact it is related specifically to operand 1)
170 during alias analysis. */
171 return (! for_alias && rtx_varies_p (XEXP (x, 0), for_alias))
172 || rtx_varies_p (XEXP (x, 1), for_alias);
174 case ASM_OPERANDS:
175 if (MEM_VOLATILE_P (x))
176 return 1;
178 /* FALLTHROUGH */
180 default:
181 break;
184 fmt = GET_RTX_FORMAT (code);
185 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
186 if (fmt[i] == 'e')
188 if (rtx_varies_p (XEXP (x, i), for_alias))
189 return 1;
191 else if (fmt[i] == 'E')
193 int j;
194 for (j = 0; j < XVECLEN (x, i); j++)
195 if (rtx_varies_p (XVECEXP (x, i, j), for_alias))
196 return 1;
199 return 0;
202 /* Return 0 if the use of X as an address in a MEM can cause a trap. */
205 rtx_addr_can_trap_p (x)
206 rtx x;
208 enum rtx_code code = GET_CODE (x);
210 switch (code)
212 case SYMBOL_REF:
213 return SYMBOL_REF_WEAK (x);
215 case LABEL_REF:
216 return 0;
218 case REG:
219 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
220 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
221 || x == stack_pointer_rtx
222 /* The arg pointer varies if it is not a fixed register. */
223 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
224 return 0;
225 /* All of the virtual frame registers are stack references. */
226 if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
227 && REGNO (x) <= LAST_VIRTUAL_REGISTER)
228 return 0;
229 return 1;
231 case CONST:
232 return rtx_addr_can_trap_p (XEXP (x, 0));
234 case PLUS:
235 /* An address is assumed not to trap if it is an address that can't
236 trap plus a constant integer or it is the pic register plus a
237 constant. */
238 return ! ((! rtx_addr_can_trap_p (XEXP (x, 0))
239 && GET_CODE (XEXP (x, 1)) == CONST_INT)
240 || (XEXP (x, 0) == pic_offset_table_rtx
241 && CONSTANT_P (XEXP (x, 1))));
243 case LO_SUM:
244 case PRE_MODIFY:
245 return rtx_addr_can_trap_p (XEXP (x, 1));
247 case PRE_DEC:
248 case PRE_INC:
249 case POST_DEC:
250 case POST_INC:
251 case POST_MODIFY:
252 return rtx_addr_can_trap_p (XEXP (x, 0));
254 default:
255 break;
258 /* If it isn't one of the case above, it can cause a trap. */
259 return 1;
262 /* Return 1 if X refers to a memory location whose address
263 cannot be compared reliably with constant addresses,
264 or if X refers to a BLKmode memory object.
265 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
266 zero, we are slightly more conservative. */
269 rtx_addr_varies_p (x, for_alias)
270 rtx x;
271 int for_alias;
273 enum rtx_code code;
274 int i;
275 const char *fmt;
277 if (x == 0)
278 return 0;
280 code = GET_CODE (x);
281 if (code == MEM)
282 return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0), for_alias);
284 fmt = GET_RTX_FORMAT (code);
285 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
286 if (fmt[i] == 'e')
288 if (rtx_addr_varies_p (XEXP (x, i), for_alias))
289 return 1;
291 else if (fmt[i] == 'E')
293 int j;
294 for (j = 0; j < XVECLEN (x, i); j++)
295 if (rtx_addr_varies_p (XVECEXP (x, i, j), for_alias))
296 return 1;
298 return 0;
301 /* Return the value of the integer term in X, if one is apparent;
302 otherwise return 0.
303 Only obvious integer terms are detected.
304 This is used in cse.c with the `related_value' field.*/
306 HOST_WIDE_INT
307 get_integer_term (x)
308 rtx x;
310 if (GET_CODE (x) == CONST)
311 x = XEXP (x, 0);
313 if (GET_CODE (x) == MINUS
314 && GET_CODE (XEXP (x, 1)) == CONST_INT)
315 return - INTVAL (XEXP (x, 1));
316 if (GET_CODE (x) == PLUS
317 && GET_CODE (XEXP (x, 1)) == CONST_INT)
318 return INTVAL (XEXP (x, 1));
319 return 0;
322 /* If X is a constant, return the value sans apparent integer term;
323 otherwise return 0.
324 Only obvious integer terms are detected. */
327 get_related_value (x)
328 rtx x;
330 if (GET_CODE (x) != CONST)
331 return 0;
332 x = XEXP (x, 0);
333 if (GET_CODE (x) == PLUS
334 && GET_CODE (XEXP (x, 1)) == CONST_INT)
335 return XEXP (x, 0);
336 else if (GET_CODE (x) == MINUS
337 && GET_CODE (XEXP (x, 1)) == CONST_INT)
338 return XEXP (x, 0);
339 return 0;
342 /* Return the number of places FIND appears within X. If COUNT_DEST is
343 zero, we do not count occurrences inside the destination of a SET. */
346 count_occurrences (x, find, count_dest)
347 rtx x, find;
348 int count_dest;
350 int i, j;
351 enum rtx_code code;
352 const char *format_ptr;
353 int count;
355 if (x == find)
356 return 1;
358 code = GET_CODE (x);
360 switch (code)
362 case REG:
363 case CONST_INT:
364 case CONST_DOUBLE:
365 case SYMBOL_REF:
366 case CODE_LABEL:
367 case PC:
368 case CC0:
369 return 0;
371 case MEM:
372 if (GET_CODE (find) == MEM && rtx_equal_p (x, find))
373 return 1;
374 break;
376 case SET:
377 if (SET_DEST (x) == find && ! count_dest)
378 return count_occurrences (SET_SRC (x), find, count_dest);
379 break;
381 default:
382 break;
385 format_ptr = GET_RTX_FORMAT (code);
386 count = 0;
388 for (i = 0; i < GET_RTX_LENGTH (code); i++)
390 switch (*format_ptr++)
392 case 'e':
393 count += count_occurrences (XEXP (x, i), find, count_dest);
394 break;
396 case 'E':
397 for (j = 0; j < XVECLEN (x, i); j++)
398 count += count_occurrences (XVECEXP (x, i, j), find, count_dest);
399 break;
402 return count;
405 /* Nonzero if register REG appears somewhere within IN.
406 Also works if REG is not a register; in this case it checks
407 for a subexpression of IN that is Lisp "equal" to REG. */
410 reg_mentioned_p (reg, in)
411 rtx reg, in;
413 const char *fmt;
414 int i;
415 enum rtx_code code;
417 if (in == 0)
418 return 0;
420 if (reg == in)
421 return 1;
423 if (GET_CODE (in) == LABEL_REF)
424 return reg == XEXP (in, 0);
426 code = GET_CODE (in);
428 switch (code)
430 /* Compare registers by number. */
431 case REG:
432 return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
434 /* These codes have no constituent expressions
435 and are unique. */
436 case SCRATCH:
437 case CC0:
438 case PC:
439 return 0;
441 case CONST_INT:
442 return GET_CODE (reg) == CONST_INT && INTVAL (in) == INTVAL (reg);
444 case CONST_DOUBLE:
445 /* These are kept unique for a given value. */
446 return 0;
448 default:
449 break;
452 if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
453 return 1;
455 fmt = GET_RTX_FORMAT (code);
457 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
459 if (fmt[i] == 'E')
461 int j;
462 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
463 if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
464 return 1;
466 else if (fmt[i] == 'e'
467 && reg_mentioned_p (reg, XEXP (in, i)))
468 return 1;
470 return 0;
473 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
474 no CODE_LABEL insn. */
477 no_labels_between_p (beg, end)
478 rtx beg, end;
480 rtx p;
481 if (beg == end)
482 return 0;
483 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
484 if (GET_CODE (p) == CODE_LABEL)
485 return 0;
486 return 1;
489 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
490 no JUMP_INSN insn. */
493 no_jumps_between_p (beg, end)
494 rtx beg, end;
496 rtx p;
497 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
498 if (GET_CODE (p) == JUMP_INSN)
499 return 0;
500 return 1;
503 /* Nonzero if register REG is used in an insn between
504 FROM_INSN and TO_INSN (exclusive of those two). */
507 reg_used_between_p (reg, from_insn, to_insn)
508 rtx reg, from_insn, to_insn;
510 rtx insn;
512 if (from_insn == to_insn)
513 return 0;
515 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
516 if (INSN_P (insn)
517 && (reg_overlap_mentioned_p (reg, PATTERN (insn))
518 || (GET_CODE (insn) == CALL_INSN
519 && (find_reg_fusage (insn, USE, reg)
520 || find_reg_fusage (insn, CLOBBER, reg)))))
521 return 1;
522 return 0;
525 /* Nonzero if the old value of X, a register, is referenced in BODY. If X
526 is entirely replaced by a new value and the only use is as a SET_DEST,
527 we do not consider it a reference. */
530 reg_referenced_p (x, body)
531 rtx x;
532 rtx body;
534 int i;
536 switch (GET_CODE (body))
538 case SET:
539 if (reg_overlap_mentioned_p (x, SET_SRC (body)))
540 return 1;
542 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
543 of a REG that occupies all of the REG, the insn references X if
544 it is mentioned in the destination. */
545 if (GET_CODE (SET_DEST (body)) != CC0
546 && GET_CODE (SET_DEST (body)) != PC
547 && GET_CODE (SET_DEST (body)) != REG
548 && ! (GET_CODE (SET_DEST (body)) == SUBREG
549 && GET_CODE (SUBREG_REG (SET_DEST (body))) == REG
550 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
551 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
552 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
553 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
554 && reg_overlap_mentioned_p (x, SET_DEST (body)))
555 return 1;
556 return 0;
558 case ASM_OPERANDS:
559 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
560 if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
561 return 1;
562 return 0;
564 case CALL:
565 case USE:
566 case IF_THEN_ELSE:
567 return reg_overlap_mentioned_p (x, body);
569 case TRAP_IF:
570 return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
572 case UNSPEC:
573 case UNSPEC_VOLATILE:
574 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
575 if (reg_overlap_mentioned_p (x, XVECEXP (body, 0, i)))
576 return 1;
577 return 0;
579 case PARALLEL:
580 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
581 if (reg_referenced_p (x, XVECEXP (body, 0, i)))
582 return 1;
583 return 0;
585 case CLOBBER:
586 if (GET_CODE (XEXP (body, 0)) == MEM)
587 if (reg_overlap_mentioned_p (x, XEXP (XEXP (body, 0), 0)))
588 return 1;
589 return 0;
591 case COND_EXEC:
592 if (reg_overlap_mentioned_p (x, COND_EXEC_TEST (body)))
593 return 1;
594 return reg_referenced_p (x, COND_EXEC_CODE (body));
596 default:
597 return 0;
601 /* Nonzero if register REG is referenced in an insn between
602 FROM_INSN and TO_INSN (exclusive of those two). Sets of REG do
603 not count. */
606 reg_referenced_between_p (reg, from_insn, to_insn)
607 rtx reg, from_insn, to_insn;
609 rtx insn;
611 if (from_insn == to_insn)
612 return 0;
614 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
615 if (INSN_P (insn)
616 && (reg_referenced_p (reg, PATTERN (insn))
617 || (GET_CODE (insn) == CALL_INSN
618 && find_reg_fusage (insn, USE, reg))))
619 return 1;
620 return 0;
623 /* Nonzero if register REG is set or clobbered in an insn between
624 FROM_INSN and TO_INSN (exclusive of those two). */
627 reg_set_between_p (reg, from_insn, to_insn)
628 rtx reg, from_insn, to_insn;
630 rtx insn;
632 if (from_insn == to_insn)
633 return 0;
635 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
636 if (INSN_P (insn) && reg_set_p (reg, insn))
637 return 1;
638 return 0;
641 /* Internals of reg_set_between_p. */
643 reg_set_p (reg, insn)
644 rtx reg, insn;
646 rtx body = insn;
648 /* We can be passed an insn or part of one. If we are passed an insn,
649 check if a side-effect of the insn clobbers REG. */
650 if (INSN_P (insn))
652 if (FIND_REG_INC_NOTE (insn, reg)
653 || (GET_CODE (insn) == CALL_INSN
654 /* We'd like to test call_used_regs here, but rtlanal.c can't
655 reference that variable due to its use in genattrtab. So
656 we'll just be more conservative.
658 ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
659 information holds all clobbered registers. */
660 && ((GET_CODE (reg) == REG
661 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
662 || GET_CODE (reg) == MEM
663 || find_reg_fusage (insn, CLOBBER, reg))))
664 return 1;
666 body = PATTERN (insn);
669 return set_of (reg, insn) != NULL_RTX;
672 /* Similar to reg_set_between_p, but check all registers in X. Return 0
673 only if none of them are modified between START and END. Do not
674 consider non-registers one way or the other. */
677 regs_set_between_p (x, start, end)
678 rtx x;
679 rtx start, end;
681 enum rtx_code code = GET_CODE (x);
682 const char *fmt;
683 int i, j;
685 switch (code)
687 case CONST_INT:
688 case CONST_DOUBLE:
689 case CONST:
690 case SYMBOL_REF:
691 case LABEL_REF:
692 case PC:
693 case CC0:
694 return 0;
696 case REG:
697 return reg_set_between_p (x, start, end);
699 default:
700 break;
703 fmt = GET_RTX_FORMAT (code);
704 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
706 if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
707 return 1;
709 else if (fmt[i] == 'E')
710 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
711 if (regs_set_between_p (XVECEXP (x, i, j), start, end))
712 return 1;
715 return 0;
718 /* Similar to reg_set_between_p, but check all registers in X. Return 0
719 only if none of them are modified between START and END. Return 1 if
720 X contains a MEM; this routine does not perform any memory aliasing. */
723 modified_between_p (x, start, end)
724 rtx x;
725 rtx start, end;
727 enum rtx_code code = GET_CODE (x);
728 const char *fmt;
729 int i, j;
731 switch (code)
733 case CONST_INT:
734 case CONST_DOUBLE:
735 case CONST:
736 case SYMBOL_REF:
737 case LABEL_REF:
738 return 0;
740 case PC:
741 case CC0:
742 return 1;
744 case MEM:
745 /* If the memory is not constant, assume it is modified. If it is
746 constant, we still have to check the address. */
747 if (! RTX_UNCHANGING_P (x))
748 return 1;
749 break;
751 case REG:
752 return reg_set_between_p (x, start, end);
754 default:
755 break;
758 fmt = GET_RTX_FORMAT (code);
759 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
761 if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
762 return 1;
764 else if (fmt[i] == 'E')
765 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
766 if (modified_between_p (XVECEXP (x, i, j), start, end))
767 return 1;
770 return 0;
773 /* Similar to reg_set_p, but check all registers in X. Return 0 only if none
774 of them are modified in INSN. Return 1 if X contains a MEM; this routine
775 does not perform any memory aliasing. */
778 modified_in_p (x, insn)
779 rtx x;
780 rtx insn;
782 enum rtx_code code = GET_CODE (x);
783 const char *fmt;
784 int i, j;
786 switch (code)
788 case CONST_INT:
789 case CONST_DOUBLE:
790 case CONST:
791 case SYMBOL_REF:
792 case LABEL_REF:
793 return 0;
795 case PC:
796 case CC0:
797 return 1;
799 case MEM:
800 /* If the memory is not constant, assume it is modified. If it is
801 constant, we still have to check the address. */
802 if (! RTX_UNCHANGING_P (x))
803 return 1;
804 break;
806 case REG:
807 return reg_set_p (x, insn);
809 default:
810 break;
813 fmt = GET_RTX_FORMAT (code);
814 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
816 if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
817 return 1;
819 else if (fmt[i] == 'E')
820 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
821 if (modified_in_p (XVECEXP (x, i, j), insn))
822 return 1;
825 return 0;
828 /* Return true if anything in insn X is (anti,output,true) dependent on
829 anything in insn Y. */
832 insn_dependent_p (x, y)
833 rtx x, y;
835 rtx tmp;
837 if (! INSN_P (x) || ! INSN_P (y))
838 abort ();
840 tmp = PATTERN (y);
841 note_stores (PATTERN (x), insn_dependent_p_1, &tmp);
842 if (tmp == NULL_RTX)
843 return 1;
845 tmp = PATTERN (x);
846 note_stores (PATTERN (y), insn_dependent_p_1, &tmp);
847 if (tmp == NULL_RTX)
848 return 1;
850 return 0;
853 /* A helper routine for insn_dependent_p called through note_stores. */
855 static void
856 insn_dependent_p_1 (x, pat, data)
857 rtx x;
858 rtx pat ATTRIBUTE_UNUSED;
859 void *data;
861 rtx * pinsn = (rtx *) data;
863 if (*pinsn && reg_mentioned_p (x, *pinsn))
864 *pinsn = NULL_RTX;
867 /* Helper function for set_of. */
868 struct set_of_data
870 rtx found;
871 rtx pat;
874 static void
875 set_of_1 (x, pat, data1)
876 rtx x;
877 rtx pat;
878 void *data1;
880 struct set_of_data *data = (struct set_of_data *) (data1);
881 if (rtx_equal_p (x, data->pat)
882 || (GET_CODE (x) != MEM && reg_overlap_mentioned_p (data->pat, x)))
883 data->found = pat;
886 /* Give an INSN, return a SET or CLOBBER expression that does modify PAT
887 (either directly or via STRICT_LOW_PART and similar modifiers). */
889 set_of (pat, insn)
890 rtx pat, insn;
892 struct set_of_data data;
893 data.found = NULL_RTX;
894 data.pat = pat;
895 note_stores (INSN_P (insn) ? PATTERN (insn) : insn, set_of_1, &data);
896 return data.found;
899 /* Given an INSN, return a SET expression if this insn has only a single SET.
900 It may also have CLOBBERs, USEs, or SET whose output
901 will not be used, which we ignore. */
904 single_set_2 (insn, pat)
905 rtx insn, pat;
907 rtx set = NULL;
908 int set_verified = 1;
909 int i;
911 if (GET_CODE (pat) == PARALLEL)
913 for (i = 0; i < XVECLEN (pat, 0); i++)
915 rtx sub = XVECEXP (pat, 0, i);
916 switch (GET_CODE (sub))
918 case USE:
919 case CLOBBER:
920 break;
922 case SET:
923 /* We can consider insns having multiple sets, where all
924 but one are dead as single set insns. In common case
925 only single set is present in the pattern so we want
926 to avoid checking for REG_UNUSED notes unless necessary.
928 When we reach set first time, we just expect this is
929 the single set we are looking for and only when more
930 sets are found in the insn, we check them. */
931 if (!set_verified)
933 if (find_reg_note (insn, REG_UNUSED, SET_DEST (set))
934 && !side_effects_p (set))
935 set = NULL;
936 else
937 set_verified = 1;
939 if (!set)
940 set = sub, set_verified = 0;
941 else if (!find_reg_note (insn, REG_UNUSED, SET_DEST (sub))
942 || side_effects_p (sub))
943 return NULL_RTX;
944 break;
946 default:
947 return NULL_RTX;
951 return set;
954 /* Given an INSN, return nonzero if it has more than one SET, else return
955 zero. */
958 multiple_sets (insn)
959 rtx insn;
961 int found;
962 int i;
964 /* INSN must be an insn. */
965 if (! INSN_P (insn))
966 return 0;
968 /* Only a PARALLEL can have multiple SETs. */
969 if (GET_CODE (PATTERN (insn)) == PARALLEL)
971 for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
972 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
974 /* If we have already found a SET, then return now. */
975 if (found)
976 return 1;
977 else
978 found = 1;
982 /* Either zero or one SET. */
983 return 0;
986 /* Return nonzero if the destination of SET equals the source
987 and there are no side effects. */
990 set_noop_p (set)
991 rtx set;
993 rtx src = SET_SRC (set);
994 rtx dst = SET_DEST (set);
996 if (side_effects_p (src) || side_effects_p (dst))
997 return 0;
999 if (GET_CODE (dst) == MEM && GET_CODE (src) == MEM)
1000 return rtx_equal_p (dst, src);
1002 if (dst == pc_rtx && src == pc_rtx)
1003 return 1;
1005 if (GET_CODE (dst) == SIGN_EXTRACT
1006 || GET_CODE (dst) == ZERO_EXTRACT)
1007 return rtx_equal_p (XEXP (dst, 0), src)
1008 && ! BYTES_BIG_ENDIAN && XEXP (dst, 2) == const0_rtx;
1010 if (GET_CODE (dst) == STRICT_LOW_PART)
1011 dst = XEXP (dst, 0);
1013 if (GET_CODE (src) == SUBREG && GET_CODE (dst) == SUBREG)
1015 if (SUBREG_BYTE (src) != SUBREG_BYTE (dst))
1016 return 0;
1017 src = SUBREG_REG (src);
1018 dst = SUBREG_REG (dst);
1021 return (GET_CODE (src) == REG && GET_CODE (dst) == REG
1022 && REGNO (src) == REGNO (dst));
1025 /* Return nonzero if an insn consists only of SETs, each of which only sets a
1026 value to itself. */
1029 noop_move_p (insn)
1030 rtx insn;
1032 rtx pat = PATTERN (insn);
1034 if (INSN_CODE (insn) == NOOP_MOVE_INSN_CODE)
1035 return 1;
1037 /* Insns carrying these notes are useful later on. */
1038 if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
1039 return 0;
1041 /* For now treat an insn with a REG_RETVAL note as a
1042 a special insn which should not be considered a no-op. */
1043 if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
1044 return 0;
1046 if (GET_CODE (pat) == SET && set_noop_p (pat))
1047 return 1;
1049 if (GET_CODE (pat) == PARALLEL)
1051 int i;
1052 /* If nothing but SETs of registers to themselves,
1053 this insn can also be deleted. */
1054 for (i = 0; i < XVECLEN (pat, 0); i++)
1056 rtx tem = XVECEXP (pat, 0, i);
1058 if (GET_CODE (tem) == USE
1059 || GET_CODE (tem) == CLOBBER)
1060 continue;
1062 if (GET_CODE (tem) != SET || ! set_noop_p (tem))
1063 return 0;
1066 return 1;
1068 return 0;
1072 /* Return the last thing that X was assigned from before *PINSN. If VALID_TO
1073 is not NULL_RTX then verify that the object is not modified up to VALID_TO.
1074 If the object was modified, if we hit a partial assignment to X, or hit a
1075 CODE_LABEL first, return X. If we found an assignment, update *PINSN to
1076 point to it. ALLOW_HWREG is set to 1 if hardware registers are allowed to
1077 be the src. */
1080 find_last_value (x, pinsn, valid_to, allow_hwreg)
1081 rtx x;
1082 rtx *pinsn;
1083 rtx valid_to;
1084 int allow_hwreg;
1086 rtx p;
1088 for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
1089 p = PREV_INSN (p))
1090 if (INSN_P (p))
1092 rtx set = single_set (p);
1093 rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
1095 if (set && rtx_equal_p (x, SET_DEST (set)))
1097 rtx src = SET_SRC (set);
1099 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
1100 src = XEXP (note, 0);
1102 if ((valid_to == NULL_RTX
1103 || ! modified_between_p (src, PREV_INSN (p), valid_to))
1104 /* Reject hard registers because we don't usually want
1105 to use them; we'd rather use a pseudo. */
1106 && (! (GET_CODE (src) == REG
1107 && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
1109 *pinsn = p;
1110 return src;
1114 /* If set in non-simple way, we don't have a value. */
1115 if (reg_set_p (x, p))
1116 break;
1119 return x;
1122 /* Return nonzero if register in range [REGNO, ENDREGNO)
1123 appears either explicitly or implicitly in X
1124 other than being stored into.
1126 References contained within the substructure at LOC do not count.
1127 LOC may be zero, meaning don't ignore anything. */
1130 refers_to_regno_p (regno, endregno, x, loc)
1131 unsigned int regno, endregno;
1132 rtx x;
1133 rtx *loc;
1135 int i;
1136 unsigned int x_regno;
1137 RTX_CODE code;
1138 const char *fmt;
1140 repeat:
1141 /* The contents of a REG_NONNEG note is always zero, so we must come here
1142 upon repeat in case the last REG_NOTE is a REG_NONNEG note. */
1143 if (x == 0)
1144 return 0;
1146 code = GET_CODE (x);
1148 switch (code)
1150 case REG:
1151 x_regno = REGNO (x);
1153 /* If we modifying the stack, frame, or argument pointer, it will
1154 clobber a virtual register. In fact, we could be more precise,
1155 but it isn't worth it. */
1156 if ((x_regno == STACK_POINTER_REGNUM
1157 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1158 || x_regno == ARG_POINTER_REGNUM
1159 #endif
1160 || x_regno == FRAME_POINTER_REGNUM)
1161 && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
1162 return 1;
1164 return (endregno > x_regno
1165 && regno < x_regno + (x_regno < FIRST_PSEUDO_REGISTER
1166 ? HARD_REGNO_NREGS (x_regno, GET_MODE (x))
1167 : 1));
1169 case SUBREG:
1170 /* If this is a SUBREG of a hard reg, we can see exactly which
1171 registers are being modified. Otherwise, handle normally. */
1172 if (GET_CODE (SUBREG_REG (x)) == REG
1173 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
1175 unsigned int inner_regno = subreg_regno (x);
1176 unsigned int inner_endregno
1177 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
1178 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
1180 return endregno > inner_regno && regno < inner_endregno;
1182 break;
1184 case CLOBBER:
1185 case SET:
1186 if (&SET_DEST (x) != loc
1187 /* Note setting a SUBREG counts as referring to the REG it is in for
1188 a pseudo but not for hard registers since we can
1189 treat each word individually. */
1190 && ((GET_CODE (SET_DEST (x)) == SUBREG
1191 && loc != &SUBREG_REG (SET_DEST (x))
1192 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
1193 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
1194 && refers_to_regno_p (regno, endregno,
1195 SUBREG_REG (SET_DEST (x)), loc))
1196 || (GET_CODE (SET_DEST (x)) != REG
1197 && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
1198 return 1;
1200 if (code == CLOBBER || loc == &SET_SRC (x))
1201 return 0;
1202 x = SET_SRC (x);
1203 goto repeat;
1205 default:
1206 break;
1209 /* X does not match, so try its subexpressions. */
1211 fmt = GET_RTX_FORMAT (code);
1212 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1214 if (fmt[i] == 'e' && loc != &XEXP (x, i))
1216 if (i == 0)
1218 x = XEXP (x, 0);
1219 goto repeat;
1221 else
1222 if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
1223 return 1;
1225 else if (fmt[i] == 'E')
1227 int j;
1228 for (j = XVECLEN (x, i) - 1; j >=0; j--)
1229 if (loc != &XVECEXP (x, i, j)
1230 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
1231 return 1;
1234 return 0;
1237 /* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
1238 we check if any register number in X conflicts with the relevant register
1239 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
1240 contains a MEM (we don't bother checking for memory addresses that can't
1241 conflict because we expect this to be a rare case. */
1244 reg_overlap_mentioned_p (x, in)
1245 rtx x, in;
1247 unsigned int regno, endregno;
1249 /* Overly conservative. */
1250 if (GET_CODE (x) == STRICT_LOW_PART)
1251 x = XEXP (x, 0);
1253 /* If either argument is a constant, then modifying X can not affect IN. */
1254 if (CONSTANT_P (x) || CONSTANT_P (in))
1255 return 0;
1257 switch (GET_CODE (x))
1259 case SUBREG:
1260 regno = REGNO (SUBREG_REG (x));
1261 if (regno < FIRST_PSEUDO_REGISTER)
1262 regno = subreg_regno (x);
1263 goto do_reg;
1265 case REG:
1266 regno = REGNO (x);
1267 do_reg:
1268 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
1269 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
1270 return refers_to_regno_p (regno, endregno, in, (rtx*)0);
1272 case MEM:
1274 const char *fmt;
1275 int i;
1277 if (GET_CODE (in) == MEM)
1278 return 1;
1280 fmt = GET_RTX_FORMAT (GET_CODE (in));
1281 for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
1282 if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
1283 return 1;
1285 return 0;
1288 case SCRATCH:
1289 case PC:
1290 case CC0:
1291 return reg_mentioned_p (x, in);
1293 case PARALLEL:
1295 int i;
1297 /* If any register in here refers to it we return true. */
1298 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1299 if (XEXP (XVECEXP (x, 0, i), 0) != 0
1300 && reg_overlap_mentioned_p (XEXP (XVECEXP (x, 0, i), 0), in))
1301 return 1;
1302 return 0;
1305 default:
1306 break;
1309 abort ();
1312 /* Return the last value to which REG was set prior to INSN. If we can't
1313 find it easily, return 0.
1315 We only return a REG, SUBREG, or constant because it is too hard to
1316 check if a MEM remains unchanged. */
1319 reg_set_last (x, insn)
1320 rtx x;
1321 rtx insn;
1323 rtx orig_insn = insn;
1325 /* Scan backwards until reg_set_last_1 changed one of the above flags.
1326 Stop when we reach a label or X is a hard reg and we reach a
1327 CALL_INSN (if reg_set_last_last_regno is a hard reg).
1329 If we find a set of X, ensure that its SET_SRC remains unchanged. */
1331 /* We compare with <= here, because reg_set_last_last_regno
1332 is actually the number of the first reg *not* in X. */
1333 for (;
1334 insn && GET_CODE (insn) != CODE_LABEL
1335 && ! (GET_CODE (insn) == CALL_INSN
1336 && REGNO (x) <= FIRST_PSEUDO_REGISTER);
1337 insn = PREV_INSN (insn))
1338 if (INSN_P (insn))
1340 rtx set = set_of (x, insn);
1341 /* OK, this function modify our register. See if we understand it. */
1342 if (set)
1344 rtx last_value;
1345 if (GET_CODE (set) != SET || SET_DEST (set) != x)
1346 return 0;
1347 last_value = SET_SRC (x);
1348 if (CONSTANT_P (last_value)
1349 || ((GET_CODE (last_value) == REG
1350 || GET_CODE (last_value) == SUBREG)
1351 && ! reg_set_between_p (last_value,
1352 insn, orig_insn)))
1353 return last_value;
1354 else
1355 return 0;
1359 return 0;
1362 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1363 (X would be the pattern of an insn).
1364 FUN receives two arguments:
1365 the REG, MEM, CC0 or PC being stored in or clobbered,
1366 the SET or CLOBBER rtx that does the store.
1368 If the item being stored in or clobbered is a SUBREG of a hard register,
1369 the SUBREG will be passed. */
1371 void
1372 note_stores (x, fun, data)
1373 rtx x;
1374 void (*fun) PARAMS ((rtx, rtx, void *));
1375 void *data;
1377 int i;
1379 if (GET_CODE (x) == COND_EXEC)
1380 x = COND_EXEC_CODE (x);
1382 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1384 rtx dest = SET_DEST (x);
1386 while ((GET_CODE (dest) == SUBREG
1387 && (GET_CODE (SUBREG_REG (dest)) != REG
1388 || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1389 || GET_CODE (dest) == ZERO_EXTRACT
1390 || GET_CODE (dest) == SIGN_EXTRACT
1391 || GET_CODE (dest) == STRICT_LOW_PART)
1392 dest = XEXP (dest, 0);
1394 /* If we have a PARALLEL, SET_DEST is a list of EXPR_LIST expressions,
1395 each of whose first operand is a register. We can't know what
1396 precisely is being set in these cases, so make up a CLOBBER to pass
1397 to the function. */
1398 if (GET_CODE (dest) == PARALLEL)
1400 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1401 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1402 (*fun) (XEXP (XVECEXP (dest, 0, i), 0),
1403 gen_rtx_CLOBBER (VOIDmode,
1404 XEXP (XVECEXP (dest, 0, i), 0)),
1405 data);
1407 else
1408 (*fun) (dest, x, data);
1411 else if (GET_CODE (x) == PARALLEL)
1412 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1413 note_stores (XVECEXP (x, 0, i), fun, data);
1416 /* Like notes_stores, but call FUN for each expression that is being
1417 referenced in PBODY, a pointer to the PATTERN of an insn. We only call
1418 FUN for each expression, not any interior subexpressions. FUN receives a
1419 pointer to the expression and the DATA passed to this function.
1421 Note that this is not quite the same test as that done in reg_referenced_p
1422 since that considers something as being referenced if it is being
1423 partially set, while we do not. */
1425 void
1426 note_uses (pbody, fun, data)
1427 rtx *pbody;
1428 void (*fun) PARAMS ((rtx *, void *));
1429 void *data;
1431 rtx body = *pbody;
1432 int i;
1434 switch (GET_CODE (body))
1436 case COND_EXEC:
1437 (*fun) (&COND_EXEC_TEST (body), data);
1438 note_uses (&COND_EXEC_CODE (body), fun, data);
1439 return;
1441 case PARALLEL:
1442 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1443 note_uses (&XVECEXP (body, 0, i), fun, data);
1444 return;
1446 case USE:
1447 (*fun) (&XEXP (body, 0), data);
1448 return;
1450 case ASM_OPERANDS:
1451 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
1452 (*fun) (&ASM_OPERANDS_INPUT (body, i), data);
1453 return;
1455 case TRAP_IF:
1456 (*fun) (&TRAP_CONDITION (body), data);
1457 return;
1459 case UNSPEC:
1460 case UNSPEC_VOLATILE:
1461 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1462 (*fun) (&XVECEXP (body, 0, i), data);
1463 return;
1465 case CLOBBER:
1466 if (GET_CODE (XEXP (body, 0)) == MEM)
1467 (*fun) (&XEXP (XEXP (body, 0), 0), data);
1468 return;
1470 case SET:
1472 rtx dest = SET_DEST (body);
1474 /* For sets we replace everything in source plus registers in memory
1475 expression in store and operands of a ZERO_EXTRACT. */
1476 (*fun) (&SET_SRC (body), data);
1478 if (GET_CODE (dest) == ZERO_EXTRACT)
1480 (*fun) (&XEXP (dest, 1), data);
1481 (*fun) (&XEXP (dest, 2), data);
1484 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART)
1485 dest = XEXP (dest, 0);
1487 if (GET_CODE (dest) == MEM)
1488 (*fun) (&XEXP (dest, 0), data);
1490 return;
1492 default:
1493 /* All the other possibilities never store. */
1494 (*fun) (pbody, data);
1495 return;
1499 /* Return nonzero if X's old contents don't survive after INSN.
1500 This will be true if X is (cc0) or if X is a register and
1501 X dies in INSN or because INSN entirely sets X.
1503 "Entirely set" means set directly and not through a SUBREG,
1504 ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1505 Likewise, REG_INC does not count.
1507 REG may be a hard or pseudo reg. Renumbering is not taken into account,
1508 but for this use that makes no difference, since regs don't overlap
1509 during their lifetimes. Therefore, this function may be used
1510 at any time after deaths have been computed (in flow.c).
1512 If REG is a hard reg that occupies multiple machine registers, this
1513 function will only return 1 if each of those registers will be replaced
1514 by INSN. */
1517 dead_or_set_p (insn, x)
1518 rtx insn;
1519 rtx x;
1521 unsigned int regno, last_regno;
1522 unsigned int i;
1524 /* Can't use cc0_rtx below since this file is used by genattrtab.c. */
1525 if (GET_CODE (x) == CC0)
1526 return 1;
1528 if (GET_CODE (x) != REG)
1529 abort ();
1531 regno = REGNO (x);
1532 last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1533 : regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1535 for (i = regno; i <= last_regno; i++)
1536 if (! dead_or_set_regno_p (insn, i))
1537 return 0;
1539 return 1;
1542 /* Utility function for dead_or_set_p to check an individual register. Also
1543 called from flow.c. */
1546 dead_or_set_regno_p (insn, test_regno)
1547 rtx insn;
1548 unsigned int test_regno;
1550 unsigned int regno, endregno;
1551 rtx pattern;
1553 /* See if there is a death note for something that includes TEST_REGNO. */
1554 if (find_regno_note (insn, REG_DEAD, test_regno))
1555 return 1;
1557 if (GET_CODE (insn) == CALL_INSN
1558 && find_regno_fusage (insn, CLOBBER, test_regno))
1559 return 1;
1561 pattern = PATTERN (insn);
1563 if (GET_CODE (pattern) == COND_EXEC)
1564 pattern = COND_EXEC_CODE (pattern);
1566 if (GET_CODE (pattern) == SET)
1568 rtx dest = SET_DEST (PATTERN (insn));
1570 /* A value is totally replaced if it is the destination or the
1571 destination is a SUBREG of REGNO that does not change the number of
1572 words in it. */
1573 if (GET_CODE (dest) == SUBREG
1574 && (((GET_MODE_SIZE (GET_MODE (dest))
1575 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1576 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1577 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1578 dest = SUBREG_REG (dest);
1580 if (GET_CODE (dest) != REG)
1581 return 0;
1583 regno = REGNO (dest);
1584 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1585 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1587 return (test_regno >= regno && test_regno < endregno);
1589 else if (GET_CODE (pattern) == PARALLEL)
1591 int i;
1593 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
1595 rtx body = XVECEXP (pattern, 0, i);
1597 if (GET_CODE (body) == COND_EXEC)
1598 body = COND_EXEC_CODE (body);
1600 if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1602 rtx dest = SET_DEST (body);
1604 if (GET_CODE (dest) == SUBREG
1605 && (((GET_MODE_SIZE (GET_MODE (dest))
1606 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1607 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1608 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1609 dest = SUBREG_REG (dest);
1611 if (GET_CODE (dest) != REG)
1612 continue;
1614 regno = REGNO (dest);
1615 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1616 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1618 if (test_regno >= regno && test_regno < endregno)
1619 return 1;
1624 return 0;
1627 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1628 If DATUM is nonzero, look for one whose datum is DATUM. */
1631 find_reg_note (insn, kind, datum)
1632 rtx insn;
1633 enum reg_note kind;
1634 rtx datum;
1636 rtx link;
1638 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1639 if (! INSN_P (insn))
1640 return 0;
1642 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1643 if (REG_NOTE_KIND (link) == kind
1644 && (datum == 0 || datum == XEXP (link, 0)))
1645 return link;
1646 return 0;
1649 /* Return the reg-note of kind KIND in insn INSN which applies to register
1650 number REGNO, if any. Return 0 if there is no such reg-note. Note that
1651 the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1652 it might be the case that the note overlaps REGNO. */
1655 find_regno_note (insn, kind, regno)
1656 rtx insn;
1657 enum reg_note kind;
1658 unsigned int regno;
1660 rtx link;
1662 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1663 if (! INSN_P (insn))
1664 return 0;
1666 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1667 if (REG_NOTE_KIND (link) == kind
1668 /* Verify that it is a register, so that scratch and MEM won't cause a
1669 problem here. */
1670 && GET_CODE (XEXP (link, 0)) == REG
1671 && REGNO (XEXP (link, 0)) <= regno
1672 && ((REGNO (XEXP (link, 0))
1673 + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1674 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1675 GET_MODE (XEXP (link, 0)))))
1676 > regno))
1677 return link;
1678 return 0;
1681 /* Return a REG_EQUIV or REG_EQUAL note if insn has only a single set and
1682 has such a note. */
1685 find_reg_equal_equiv_note (insn)
1686 rtx insn;
1688 rtx note;
1690 if (single_set (insn) == 0)
1691 return 0;
1692 else if ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
1693 return note;
1694 else
1695 return find_reg_note (insn, REG_EQUAL, NULL_RTX);
1698 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1699 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1702 find_reg_fusage (insn, code, datum)
1703 rtx insn;
1704 enum rtx_code code;
1705 rtx datum;
1707 /* If it's not a CALL_INSN, it can't possibly have a
1708 CALL_INSN_FUNCTION_USAGE field, so don't bother checking. */
1709 if (GET_CODE (insn) != CALL_INSN)
1710 return 0;
1712 if (! datum)
1713 abort();
1715 if (GET_CODE (datum) != REG)
1717 rtx link;
1719 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1720 link;
1721 link = XEXP (link, 1))
1722 if (GET_CODE (XEXP (link, 0)) == code
1723 && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1724 return 1;
1726 else
1728 unsigned int regno = REGNO (datum);
1730 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1731 to pseudo registers, so don't bother checking. */
1733 if (regno < FIRST_PSEUDO_REGISTER)
1735 unsigned int end_regno
1736 = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1737 unsigned int i;
1739 for (i = regno; i < end_regno; i++)
1740 if (find_regno_fusage (insn, code, i))
1741 return 1;
1745 return 0;
1748 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1749 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1752 find_regno_fusage (insn, code, regno)
1753 rtx insn;
1754 enum rtx_code code;
1755 unsigned int regno;
1757 rtx link;
1759 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1760 to pseudo registers, so don't bother checking. */
1762 if (regno >= FIRST_PSEUDO_REGISTER
1763 || GET_CODE (insn) != CALL_INSN )
1764 return 0;
1766 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1768 unsigned int regnote;
1769 rtx op, reg;
1771 if (GET_CODE (op = XEXP (link, 0)) == code
1772 && GET_CODE (reg = XEXP (op, 0)) == REG
1773 && (regnote = REGNO (reg)) <= regno
1774 && regnote + HARD_REGNO_NREGS (regnote, GET_MODE (reg)) > regno)
1775 return 1;
1778 return 0;
1781 /* Remove register note NOTE from the REG_NOTES of INSN. */
1783 void
1784 remove_note (insn, note)
1785 rtx insn;
1786 rtx note;
1788 rtx link;
1790 if (note == NULL_RTX)
1791 return;
1793 if (REG_NOTES (insn) == note)
1795 REG_NOTES (insn) = XEXP (note, 1);
1796 return;
1799 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1800 if (XEXP (link, 1) == note)
1802 XEXP (link, 1) = XEXP (note, 1);
1803 return;
1806 abort ();
1809 /* Search LISTP (an EXPR_LIST) for an entry whose first operand is NODE and
1810 remove that entry from the list if it is found.
1812 A simple equality test is used to determine if NODE matches. */
1814 void
1815 remove_node_from_expr_list (node, listp)
1816 rtx node;
1817 rtx *listp;
1819 rtx temp = *listp;
1820 rtx prev = NULL_RTX;
1822 while (temp)
1824 if (node == XEXP (temp, 0))
1826 /* Splice the node out of the list. */
1827 if (prev)
1828 XEXP (prev, 1) = XEXP (temp, 1);
1829 else
1830 *listp = XEXP (temp, 1);
1832 return;
1835 prev = temp;
1836 temp = XEXP (temp, 1);
1840 /* Nonzero if X contains any volatile instructions. These are instructions
1841 which may cause unpredictable machine state instructions, and thus no
1842 instructions should be moved or combined across them. This includes
1843 only volatile asms and UNSPEC_VOLATILE instructions. */
1846 volatile_insn_p (x)
1847 rtx x;
1849 RTX_CODE code;
1851 code = GET_CODE (x);
1852 switch (code)
1854 case LABEL_REF:
1855 case SYMBOL_REF:
1856 case CONST_INT:
1857 case CONST:
1858 case CONST_DOUBLE:
1859 case CC0:
1860 case PC:
1861 case REG:
1862 case SCRATCH:
1863 case CLOBBER:
1864 case ASM_INPUT:
1865 case ADDR_VEC:
1866 case ADDR_DIFF_VEC:
1867 case CALL:
1868 case MEM:
1869 return 0;
1871 case UNSPEC_VOLATILE:
1872 /* case TRAP_IF: This isn't clear yet. */
1873 return 1;
1875 case ASM_OPERANDS:
1876 if (MEM_VOLATILE_P (x))
1877 return 1;
1879 default:
1880 break;
1883 /* Recursively scan the operands of this expression. */
1886 const char *fmt = GET_RTX_FORMAT (code);
1887 int i;
1889 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1891 if (fmt[i] == 'e')
1893 if (volatile_insn_p (XEXP (x, i)))
1894 return 1;
1896 else if (fmt[i] == 'E')
1898 int j;
1899 for (j = 0; j < XVECLEN (x, i); j++)
1900 if (volatile_insn_p (XVECEXP (x, i, j)))
1901 return 1;
1905 return 0;
1908 /* Nonzero if X contains any volatile memory references
1909 UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions. */
1912 volatile_refs_p (x)
1913 rtx x;
1915 RTX_CODE code;
1917 code = GET_CODE (x);
1918 switch (code)
1920 case LABEL_REF:
1921 case SYMBOL_REF:
1922 case CONST_INT:
1923 case CONST:
1924 case CONST_DOUBLE:
1925 case CC0:
1926 case PC:
1927 case REG:
1928 case SCRATCH:
1929 case CLOBBER:
1930 case ASM_INPUT:
1931 case ADDR_VEC:
1932 case ADDR_DIFF_VEC:
1933 return 0;
1935 case CALL:
1936 case UNSPEC_VOLATILE:
1937 /* case TRAP_IF: This isn't clear yet. */
1938 return 1;
1940 case MEM:
1941 case ASM_OPERANDS:
1942 if (MEM_VOLATILE_P (x))
1943 return 1;
1945 default:
1946 break;
1949 /* Recursively scan the operands of this expression. */
1952 const char *fmt = GET_RTX_FORMAT (code);
1953 int i;
1955 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1957 if (fmt[i] == 'e')
1959 if (volatile_refs_p (XEXP (x, i)))
1960 return 1;
1962 else if (fmt[i] == 'E')
1964 int j;
1965 for (j = 0; j < XVECLEN (x, i); j++)
1966 if (volatile_refs_p (XVECEXP (x, i, j)))
1967 return 1;
1971 return 0;
1974 /* Similar to above, except that it also rejects register pre- and post-
1975 incrementing. */
1978 side_effects_p (x)
1979 rtx x;
1981 RTX_CODE code;
1983 code = GET_CODE (x);
1984 switch (code)
1986 case LABEL_REF:
1987 case SYMBOL_REF:
1988 case CONST_INT:
1989 case CONST:
1990 case CONST_DOUBLE:
1991 case CC0:
1992 case PC:
1993 case REG:
1994 case SCRATCH:
1995 case ASM_INPUT:
1996 case ADDR_VEC:
1997 case ADDR_DIFF_VEC:
1998 return 0;
2000 case CLOBBER:
2001 /* Reject CLOBBER with a non-VOID mode. These are made by combine.c
2002 when some combination can't be done. If we see one, don't think
2003 that we can simplify the expression. */
2004 return (GET_MODE (x) != VOIDmode);
2006 case PRE_INC:
2007 case PRE_DEC:
2008 case POST_INC:
2009 case POST_DEC:
2010 case PRE_MODIFY:
2011 case POST_MODIFY:
2012 case CALL:
2013 case UNSPEC_VOLATILE:
2014 /* case TRAP_IF: This isn't clear yet. */
2015 return 1;
2017 case MEM:
2018 case ASM_OPERANDS:
2019 if (MEM_VOLATILE_P (x))
2020 return 1;
2022 default:
2023 break;
2026 /* Recursively scan the operands of this expression. */
2029 const char *fmt = GET_RTX_FORMAT (code);
2030 int i;
2032 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2034 if (fmt[i] == 'e')
2036 if (side_effects_p (XEXP (x, i)))
2037 return 1;
2039 else if (fmt[i] == 'E')
2041 int j;
2042 for (j = 0; j < XVECLEN (x, i); j++)
2043 if (side_effects_p (XVECEXP (x, i, j)))
2044 return 1;
2048 return 0;
2051 /* Return nonzero if evaluating rtx X might cause a trap. */
2054 may_trap_p (x)
2055 rtx x;
2057 int i;
2058 enum rtx_code code;
2059 const char *fmt;
2061 if (x == 0)
2062 return 0;
2063 code = GET_CODE (x);
2064 switch (code)
2066 /* Handle these cases quickly. */
2067 case CONST_INT:
2068 case CONST_DOUBLE:
2069 case SYMBOL_REF:
2070 case LABEL_REF:
2071 case CONST:
2072 case PC:
2073 case CC0:
2074 case REG:
2075 case SCRATCH:
2076 return 0;
2078 case ASM_INPUT:
2079 case UNSPEC_VOLATILE:
2080 case TRAP_IF:
2081 return 1;
2083 case ASM_OPERANDS:
2084 return MEM_VOLATILE_P (x);
2086 /* Memory ref can trap unless it's a static var or a stack slot. */
2087 case MEM:
2088 return rtx_addr_can_trap_p (XEXP (x, 0));
2090 /* Division by a non-constant might trap. */
2091 case DIV:
2092 case MOD:
2093 case UDIV:
2094 case UMOD:
2095 if (! CONSTANT_P (XEXP (x, 1))
2096 || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2097 return 1;
2098 /* This was const0_rtx, but by not using that,
2099 we can link this file into other programs. */
2100 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
2101 return 1;
2102 break;
2104 case EXPR_LIST:
2105 /* An EXPR_LIST is used to represent a function call. This
2106 certainly may trap. */
2107 return 1;
2109 case GE:
2110 case GT:
2111 case LE:
2112 case LT:
2113 case COMPARE:
2114 /* Some floating point comparisons may trap. */
2115 /* ??? There is no machine independent way to check for tests that trap
2116 when COMPARE is used, though many targets do make this distinction.
2117 For instance, sparc uses CCFPE for compares which generate exceptions
2118 and CCFP for compares which do not generate exceptions. */
2119 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2120 return 1;
2121 /* But often the compare has some CC mode, so check operand
2122 modes as well. */
2123 if (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) == MODE_FLOAT
2124 || GET_MODE_CLASS (GET_MODE (XEXP (x, 1))) == MODE_FLOAT)
2125 return 1;
2126 break;
2128 case NEG:
2129 case ABS:
2130 /* These operations don't trap even with floating point. */
2131 break;
2133 default:
2134 /* Any floating arithmetic may trap. */
2135 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2136 return 1;
2139 fmt = GET_RTX_FORMAT (code);
2140 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2142 if (fmt[i] == 'e')
2144 if (may_trap_p (XEXP (x, i)))
2145 return 1;
2147 else if (fmt[i] == 'E')
2149 int j;
2150 for (j = 0; j < XVECLEN (x, i); j++)
2151 if (may_trap_p (XVECEXP (x, i, j)))
2152 return 1;
2155 return 0;
2158 /* Return nonzero if X contains a comparison that is not either EQ or NE,
2159 i.e., an inequality. */
2162 inequality_comparisons_p (x)
2163 rtx x;
2165 const char *fmt;
2166 int len, i;
2167 enum rtx_code code = GET_CODE (x);
2169 switch (code)
2171 case REG:
2172 case SCRATCH:
2173 case PC:
2174 case CC0:
2175 case CONST_INT:
2176 case CONST_DOUBLE:
2177 case CONST:
2178 case LABEL_REF:
2179 case SYMBOL_REF:
2180 return 0;
2182 case LT:
2183 case LTU:
2184 case GT:
2185 case GTU:
2186 case LE:
2187 case LEU:
2188 case GE:
2189 case GEU:
2190 return 1;
2192 default:
2193 break;
2196 len = GET_RTX_LENGTH (code);
2197 fmt = GET_RTX_FORMAT (code);
2199 for (i = 0; i < len; i++)
2201 if (fmt[i] == 'e')
2203 if (inequality_comparisons_p (XEXP (x, i)))
2204 return 1;
2206 else if (fmt[i] == 'E')
2208 int j;
2209 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2210 if (inequality_comparisons_p (XVECEXP (x, i, j)))
2211 return 1;
2215 return 0;
2218 /* Replace any occurrence of FROM in X with TO. The function does
2219 not enter into CONST_DOUBLE for the replace.
2221 Note that copying is not done so X must not be shared unless all copies
2222 are to be modified. */
2225 replace_rtx (x, from, to)
2226 rtx x, from, to;
2228 int i, j;
2229 const char *fmt;
2231 /* The following prevents loops occurrence when we change MEM in
2232 CONST_DOUBLE onto the same CONST_DOUBLE. */
2233 if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
2234 return x;
2236 if (x == from)
2237 return to;
2239 /* Allow this function to make replacements in EXPR_LISTs. */
2240 if (x == 0)
2241 return 0;
2243 fmt = GET_RTX_FORMAT (GET_CODE (x));
2244 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2246 if (fmt[i] == 'e')
2247 XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
2248 else if (fmt[i] == 'E')
2249 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2250 XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
2253 return x;
2256 /* Throughout the rtx X, replace many registers according to REG_MAP.
2257 Return the replacement for X (which may be X with altered contents).
2258 REG_MAP[R] is the replacement for register R, or 0 for don't replace.
2259 NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
2261 We only support REG_MAP entries of REG or SUBREG. Also, hard registers
2262 should not be mapped to pseudos or vice versa since validate_change
2263 is not called.
2265 If REPLACE_DEST is 1, replacements are also done in destinations;
2266 otherwise, only sources are replaced. */
2269 replace_regs (x, reg_map, nregs, replace_dest)
2270 rtx x;
2271 rtx *reg_map;
2272 unsigned int nregs;
2273 int replace_dest;
2275 enum rtx_code code;
2276 int i;
2277 const char *fmt;
2279 if (x == 0)
2280 return x;
2282 code = GET_CODE (x);
2283 switch (code)
2285 case SCRATCH:
2286 case PC:
2287 case CC0:
2288 case CONST_INT:
2289 case CONST_DOUBLE:
2290 case CONST:
2291 case SYMBOL_REF:
2292 case LABEL_REF:
2293 return x;
2295 case REG:
2296 /* Verify that the register has an entry before trying to access it. */
2297 if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
2299 /* SUBREGs can't be shared. Always return a copy to ensure that if
2300 this replacement occurs more than once then each instance will
2301 get distinct rtx. */
2302 if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
2303 return copy_rtx (reg_map[REGNO (x)]);
2304 return reg_map[REGNO (x)];
2306 return x;
2308 case SUBREG:
2309 /* Prevent making nested SUBREGs. */
2310 if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
2311 && reg_map[REGNO (SUBREG_REG (x))] != 0
2312 && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
2314 rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
2315 return simplify_gen_subreg (GET_MODE (x), map_val,
2316 GET_MODE (SUBREG_REG (x)),
2317 SUBREG_BYTE (x));
2319 break;
2321 case SET:
2322 if (replace_dest)
2323 SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
2325 else if (GET_CODE (SET_DEST (x)) == MEM
2326 || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2327 /* Even if we are not to replace destinations, replace register if it
2328 is CONTAINED in destination (destination is memory or
2329 STRICT_LOW_PART). */
2330 XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
2331 reg_map, nregs, 0);
2332 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2333 /* Similarly, for ZERO_EXTRACT we replace all operands. */
2334 break;
2336 SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2337 return x;
2339 default:
2340 break;
2343 fmt = GET_RTX_FORMAT (code);
2344 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2346 if (fmt[i] == 'e')
2347 XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
2348 else if (fmt[i] == 'E')
2350 int j;
2351 for (j = 0; j < XVECLEN (x, i); j++)
2352 XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2353 nregs, replace_dest);
2356 return x;
2359 /* A subroutine of computed_jump_p, return 1 if X contains a REG or MEM or
2360 constant that is not in the constant pool and not in the condition
2361 of an IF_THEN_ELSE. */
2363 static int
2364 computed_jump_p_1 (x)
2365 rtx x;
2367 enum rtx_code code = GET_CODE (x);
2368 int i, j;
2369 const char *fmt;
2371 switch (code)
2373 case LABEL_REF:
2374 case PC:
2375 return 0;
2377 case CONST:
2378 case CONST_INT:
2379 case CONST_DOUBLE:
2380 case SYMBOL_REF:
2381 case REG:
2382 return 1;
2384 case MEM:
2385 return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2386 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2388 case IF_THEN_ELSE:
2389 return (computed_jump_p_1 (XEXP (x, 1))
2390 || computed_jump_p_1 (XEXP (x, 2)));
2392 default:
2393 break;
2396 fmt = GET_RTX_FORMAT (code);
2397 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2399 if (fmt[i] == 'e'
2400 && computed_jump_p_1 (XEXP (x, i)))
2401 return 1;
2403 else if (fmt[i] == 'E')
2404 for (j = 0; j < XVECLEN (x, i); j++)
2405 if (computed_jump_p_1 (XVECEXP (x, i, j)))
2406 return 1;
2409 return 0;
2412 /* Return nonzero if INSN is an indirect jump (aka computed jump).
2414 Tablejumps and casesi insns are not considered indirect jumps;
2415 we can recognize them by a (use (label_ref)). */
2418 computed_jump_p (insn)
2419 rtx insn;
2421 int i;
2422 if (GET_CODE (insn) == JUMP_INSN)
2424 rtx pat = PATTERN (insn);
2426 if (find_reg_note (insn, REG_LABEL, NULL_RTX))
2427 return 0;
2428 else if (GET_CODE (pat) == PARALLEL)
2430 int len = XVECLEN (pat, 0);
2431 int has_use_labelref = 0;
2433 for (i = len - 1; i >= 0; i--)
2434 if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2435 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2436 == LABEL_REF))
2437 has_use_labelref = 1;
2439 if (! has_use_labelref)
2440 for (i = len - 1; i >= 0; i--)
2441 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2442 && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2443 && computed_jump_p_1 (SET_SRC (XVECEXP (pat, 0, i))))
2444 return 1;
2446 else if (GET_CODE (pat) == SET
2447 && SET_DEST (pat) == pc_rtx
2448 && computed_jump_p_1 (SET_SRC (pat)))
2449 return 1;
2451 return 0;
2454 /* Traverse X via depth-first search, calling F for each
2455 sub-expression (including X itself). F is also passed the DATA.
2456 If F returns -1, do not traverse sub-expressions, but continue
2457 traversing the rest of the tree. If F ever returns any other
2458 non-zero value, stop the traversal, and return the value returned
2459 by F. Otherwise, return 0. This function does not traverse inside
2460 tree structure that contains RTX_EXPRs, or into sub-expressions
2461 whose format code is `0' since it is not known whether or not those
2462 codes are actually RTL.
2464 This routine is very general, and could (should?) be used to
2465 implement many of the other routines in this file. */
2468 for_each_rtx (x, f, data)
2469 rtx *x;
2470 rtx_function f;
2471 void *data;
2473 int result;
2474 int length;
2475 const char *format;
2476 int i;
2478 /* Call F on X. */
2479 result = (*f) (x, data);
2480 if (result == -1)
2481 /* Do not traverse sub-expressions. */
2482 return 0;
2483 else if (result != 0)
2484 /* Stop the traversal. */
2485 return result;
2487 if (*x == NULL_RTX)
2488 /* There are no sub-expressions. */
2489 return 0;
2491 length = GET_RTX_LENGTH (GET_CODE (*x));
2492 format = GET_RTX_FORMAT (GET_CODE (*x));
2494 for (i = 0; i < length; ++i)
2496 switch (format[i])
2498 case 'e':
2499 result = for_each_rtx (&XEXP (*x, i), f, data);
2500 if (result != 0)
2501 return result;
2502 break;
2504 case 'V':
2505 case 'E':
2506 if (XVEC (*x, i) != 0)
2508 int j;
2509 for (j = 0; j < XVECLEN (*x, i); ++j)
2511 result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2512 if (result != 0)
2513 return result;
2516 break;
2518 default:
2519 /* Nothing to do. */
2520 break;
2525 return 0;
2528 /* Searches X for any reference to REGNO, returning the rtx of the
2529 reference found if any. Otherwise, returns NULL_RTX. */
2532 regno_use_in (regno, x)
2533 unsigned int regno;
2534 rtx x;
2536 const char *fmt;
2537 int i, j;
2538 rtx tem;
2540 if (GET_CODE (x) == REG && REGNO (x) == regno)
2541 return x;
2543 fmt = GET_RTX_FORMAT (GET_CODE (x));
2544 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2546 if (fmt[i] == 'e')
2548 if ((tem = regno_use_in (regno, XEXP (x, i))))
2549 return tem;
2551 else if (fmt[i] == 'E')
2552 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2553 if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2554 return tem;
2557 return NULL_RTX;
2560 /* Return a value indicating whether OP, an operand of a commutative
2561 operation, is preferred as the first or second operand. The higher
2562 the value, the stronger the preference for being the first operand.
2563 We use negative values to indicate a preference for the first operand
2564 and positive values for the second operand. */
2567 commutative_operand_precedence (op)
2568 rtx op;
2570 /* Constants always come the second operand. Prefer "nice" constants. */
2571 if (GET_CODE (op) == CONST_INT)
2572 return -5;
2573 if (GET_CODE (op) == CONST_DOUBLE)
2574 return -4;
2575 if (CONSTANT_P (op))
2576 return -3;
2578 /* SUBREGs of objects should come second. */
2579 if (GET_CODE (op) == SUBREG
2580 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op))) == 'o')
2581 return -2;
2583 /* If only one operand is a `neg', `not',
2584 `mult', `plus', or `minus' expression, it will be the first
2585 operand. */
2586 if (GET_CODE (op) == NEG || GET_CODE (op) == NOT
2587 || GET_CODE (op) == MULT || GET_CODE (op) == PLUS
2588 || GET_CODE (op) == MINUS)
2589 return 2;
2591 /* Complex expressions should be the first, so decrease priority
2592 of objects. */
2593 if (GET_RTX_CLASS (GET_CODE (op)) == 'o')
2594 return -1;
2595 return 0;
2598 /* Return 1 iff it is necessary to swap operands of commutative operation
2599 in order to canonicalize expression. */
2602 swap_commutative_operands_p (x, y)
2603 rtx x, y;
2605 return (commutative_operand_precedence (x)
2606 < commutative_operand_precedence (y));
2609 /* Return 1 if X is an autoincrement side effect and the register is
2610 not the stack pointer. */
2612 auto_inc_p (x)
2613 rtx x;
2615 switch (GET_CODE (x))
2617 case PRE_INC:
2618 case POST_INC:
2619 case PRE_DEC:
2620 case POST_DEC:
2621 case PRE_MODIFY:
2622 case POST_MODIFY:
2623 /* There are no REG_INC notes for SP. */
2624 if (XEXP (x, 0) != stack_pointer_rtx)
2625 return 1;
2626 default:
2627 break;
2629 return 0;
2632 /* Return 1 if the sequence of instructions beginning with FROM and up
2633 to and including TO is safe to move. If NEW_TO is non-NULL, and
2634 the sequence is not already safe to move, but can be easily
2635 extended to a sequence which is safe, then NEW_TO will point to the
2636 end of the extended sequence.
2638 For now, this function only checks that the region contains whole
2639 exception regions, but it could be extended to check additional
2640 conditions as well. */
2643 insns_safe_to_move_p (from, to, new_to)
2644 rtx from;
2645 rtx to;
2646 rtx *new_to;
2648 int eh_region_count = 0;
2649 int past_to_p = 0;
2650 rtx r = from;
2652 /* By default, assume the end of the region will be what was
2653 suggested. */
2654 if (new_to)
2655 *new_to = to;
2657 while (r)
2659 if (GET_CODE (r) == NOTE)
2661 switch (NOTE_LINE_NUMBER (r))
2663 case NOTE_INSN_EH_REGION_BEG:
2664 ++eh_region_count;
2665 break;
2667 case NOTE_INSN_EH_REGION_END:
2668 if (eh_region_count == 0)
2669 /* This sequence of instructions contains the end of
2670 an exception region, but not he beginning. Moving
2671 it will cause chaos. */
2672 return 0;
2674 --eh_region_count;
2675 break;
2677 default:
2678 break;
2681 else if (past_to_p)
2682 /* If we've passed TO, and we see a non-note instruction, we
2683 can't extend the sequence to a movable sequence. */
2684 return 0;
2686 if (r == to)
2688 if (!new_to)
2689 /* It's OK to move the sequence if there were matched sets of
2690 exception region notes. */
2691 return eh_region_count == 0;
2693 past_to_p = 1;
2696 /* It's OK to move the sequence if there were matched sets of
2697 exception region notes. */
2698 if (past_to_p && eh_region_count == 0)
2700 *new_to = r;
2701 return 1;
2704 /* Go to the next instruction. */
2705 r = NEXT_INSN (r);
2708 return 0;
2711 /* Return non-zero if IN contains a piece of rtl that has the address LOC */
2713 loc_mentioned_in_p (loc, in)
2714 rtx *loc, in;
2716 enum rtx_code code = GET_CODE (in);
2717 const char *fmt = GET_RTX_FORMAT (code);
2718 int i, j;
2720 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2722 if (loc == &in->fld[i].rtx)
2723 return 1;
2724 if (fmt[i] == 'e')
2726 if (loc_mentioned_in_p (loc, XEXP (in, i)))
2727 return 1;
2729 else if (fmt[i] == 'E')
2730 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
2731 if (loc_mentioned_in_p (loc, XVECEXP (in, i, j)))
2732 return 1;
2734 return 0;
2737 /* This function returns the regno offset of a subreg expression.
2738 xregno - A regno of an inner hard subreg_reg (or what will become one).
2739 xmode - The mode of xregno.
2740 offset - The byte offset.
2741 ymode - The mode of a top level SUBREG (or what may become one).
2742 RETURN - The regno offset which would be used.
2743 This function can be overridden by defining SUBREG_REGNO_OFFSET,
2744 taking the same parameters. */
2745 unsigned int
2746 subreg_regno_offset (xregno, xmode, offset, ymode)
2747 unsigned int xregno;
2748 enum machine_mode xmode;
2749 unsigned int offset;
2750 enum machine_mode ymode;
2752 unsigned ret;
2753 int nregs_xmode, nregs_ymode;
2754 int mode_multiple, nregs_multiple;
2755 int y_offset;
2757 /* Check for an override, and use it instead. */
2758 #ifdef SUBREG_REGNO_OFFSET
2759 ret = SUBREG_REGNO_OFFSET (xregno, xmode, offset, ymode);
2760 #else
2761 if (xregno >= FIRST_PSEUDO_REGISTER)
2762 abort ();
2764 nregs_xmode = HARD_REGNO_NREGS (xregno, xmode);
2765 nregs_ymode = HARD_REGNO_NREGS (xregno, ymode);
2766 if (offset == 0 || nregs_xmode == nregs_ymode)
2767 return 0;
2769 /* size of ymode must not be greater than the size of xmode. */
2770 mode_multiple = GET_MODE_SIZE (xmode) / GET_MODE_SIZE (ymode);
2771 if (mode_multiple == 0)
2772 abort ();
2774 y_offset = offset / GET_MODE_SIZE (ymode);
2775 nregs_multiple = nregs_xmode / nregs_ymode;
2776 ret = (y_offset / (mode_multiple / nregs_multiple)) * nregs_ymode;
2777 #endif
2779 return ret;
2782 /* Return the final regno that a subreg expression refers to. */
2783 unsigned int
2784 subreg_regno (x)
2785 rtx x;
2787 unsigned int ret;
2788 rtx subreg = SUBREG_REG (x);
2789 int regno = REGNO (subreg);
2791 ret = regno + subreg_regno_offset (regno,
2792 GET_MODE (subreg),
2793 SUBREG_BYTE (x),
2794 GET_MODE (x));
2795 return ret;
2798 struct parms_set_data
2800 int nregs;
2801 HARD_REG_SET regs;
2804 /* Helper function for noticing stores to parameter registers. */
2805 static void
2806 parms_set (x, pat, data)
2807 rtx x, pat ATTRIBUTE_UNUSED;
2808 void *data;
2810 struct parms_set_data *d = data;
2811 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
2812 && TEST_HARD_REG_BIT (d->regs, REGNO (x)))
2814 CLEAR_HARD_REG_BIT (d->regs, REGNO (x));
2815 d->nregs--;
2819 /* Look backward for first parameter to be loaded.
2820 Do not skip BOUNDARY. */
2822 find_first_parameter_load (call_insn, boundary)
2823 rtx call_insn, boundary;
2825 struct parms_set_data parm;
2826 rtx p, before;
2828 /* Since different machines initialize their parameter registers
2829 in different orders, assume nothing. Collect the set of all
2830 parameter registers. */
2831 CLEAR_HARD_REG_SET (parm.regs);
2832 parm.nregs = 0;
2833 for (p = CALL_INSN_FUNCTION_USAGE (call_insn); p; p = XEXP (p, 1))
2834 if (GET_CODE (XEXP (p, 0)) == USE
2835 && GET_CODE (XEXP (XEXP (p, 0), 0)) == REG)
2837 if (REGNO (XEXP (XEXP (p, 0), 0)) >= FIRST_PSEUDO_REGISTER)
2838 abort ();
2840 /* We only care about registers which can hold function
2841 arguments. */
2842 if (!FUNCTION_ARG_REGNO_P (REGNO (XEXP (XEXP (p, 0), 0))))
2843 continue;
2845 SET_HARD_REG_BIT (parm.regs, REGNO (XEXP (XEXP (p, 0), 0)));
2846 parm.nregs++;
2848 before = call_insn;
2850 /* Search backward for the first set of a register in this set. */
2851 while (parm.nregs && before != boundary)
2853 before = PREV_INSN (before);
2855 /* It is possible that some loads got CSEed from one call to
2856 another. Stop in that case. */
2857 if (GET_CODE (before) == CALL_INSN)
2858 break;
2860 /* Our caller needs either ensure that we will find all sets
2861 (in case code has not been optimized yet), or take care
2862 for possible labels in a way by setting boundary to preceding
2863 CODE_LABEL. */
2864 if (GET_CODE (before) == CODE_LABEL)
2866 if (before != boundary)
2867 abort ();
2868 break;
2871 if (INSN_P (before))
2872 note_stores (PATTERN (before), parms_set, &parm);
2874 return before;