* config/i386/i386.c (ix86_expand_prologue): If the function uses a
[official-gcc.git] / gcc / rtlanal.c
blob82eccf433b307914d7bc5626014ed8d6b5eb9f50
1 /* Analyze RTL for C-Compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 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 "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "rtl.h"
29 #include "hard-reg-set.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "target.h"
33 #include "output.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "basic-block.h"
37 #include "real.h"
38 #include "regs.h"
39 #include "function.h"
41 /* Forward declarations */
42 static int global_reg_mentioned_p_1 (rtx *, void *);
43 static void set_of_1 (rtx, rtx, void *);
44 static void insn_dependent_p_1 (rtx, rtx, void *);
45 static int rtx_referenced_p_1 (rtx *, void *);
46 static int computed_jump_p_1 (rtx);
47 static void parms_set (rtx, rtx, void *);
48 static bool hoist_test_store (rtx, rtx, regset);
49 static void hoist_update_store (rtx, rtx *, rtx, rtx);
51 static unsigned HOST_WIDE_INT cached_nonzero_bits (rtx, enum machine_mode,
52 rtx, enum machine_mode,
53 unsigned HOST_WIDE_INT);
54 static unsigned HOST_WIDE_INT nonzero_bits1 (rtx, enum machine_mode, rtx,
55 enum machine_mode,
56 unsigned HOST_WIDE_INT);
57 static unsigned int cached_num_sign_bit_copies (rtx, enum machine_mode, rtx,
58 enum machine_mode,
59 unsigned int);
60 static unsigned int num_sign_bit_copies1 (rtx, enum machine_mode, rtx,
61 enum machine_mode, unsigned int);
63 /* Bit flags that specify the machine subtype we are compiling for.
64 Bits are tested using macros TARGET_... defined in the tm.h file
65 and set by `-m...' switches. Must be defined in rtlanal.c. */
67 int target_flags;
69 /* Return 1 if the value of X is unstable
70 (would be different at a different point in the program).
71 The frame pointer, arg pointer, etc. are considered stable
72 (within one function) and so is anything marked `unchanging'. */
74 int
75 rtx_unstable_p (rtx x)
77 RTX_CODE code = GET_CODE (x);
78 int i;
79 const char *fmt;
81 switch (code)
83 case MEM:
84 return ! RTX_UNCHANGING_P (x) || rtx_unstable_p (XEXP (x, 0));
86 case CONST:
87 case CONST_INT:
88 case CONST_DOUBLE:
89 case CONST_VECTOR:
90 case SYMBOL_REF:
91 case LABEL_REF:
92 return 0;
94 case REG:
95 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
96 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
97 /* The arg pointer varies if it is not a fixed register. */
98 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
99 || RTX_UNCHANGING_P (x))
100 return 0;
101 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
102 /* ??? When call-clobbered, the value is stable modulo the restore
103 that must happen after a call. This currently screws up local-alloc
104 into believing that the restore is not needed. */
105 if (x == pic_offset_table_rtx)
106 return 0;
107 #endif
108 return 1;
110 case ASM_OPERANDS:
111 if (MEM_VOLATILE_P (x))
112 return 1;
114 /* Fall through. */
116 default:
117 break;
120 fmt = GET_RTX_FORMAT (code);
121 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
122 if (fmt[i] == 'e')
124 if (rtx_unstable_p (XEXP (x, i)))
125 return 1;
127 else if (fmt[i] == 'E')
129 int j;
130 for (j = 0; j < XVECLEN (x, i); j++)
131 if (rtx_unstable_p (XVECEXP (x, i, j)))
132 return 1;
135 return 0;
138 /* Return 1 if X has a value that can vary even between two
139 executions of the program. 0 means X can be compared reliably
140 against certain constants or near-constants.
141 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
142 zero, we are slightly more conservative.
143 The frame pointer and the arg pointer are considered constant. */
146 rtx_varies_p (rtx x, int for_alias)
148 RTX_CODE code;
149 int i;
150 const char *fmt;
152 if (!x)
153 return 0;
155 code = GET_CODE (x);
156 switch (code)
158 case MEM:
159 return ! RTX_UNCHANGING_P (x) || rtx_varies_p (XEXP (x, 0), for_alias);
161 case CONST:
162 case CONST_INT:
163 case CONST_DOUBLE:
164 case CONST_VECTOR:
165 case SYMBOL_REF:
166 case LABEL_REF:
167 return 0;
169 case REG:
170 /* Note that we have to test for the actual rtx used for the frame
171 and arg pointers and not just the register number in case we have
172 eliminated the frame and/or arg pointer and are using it
173 for pseudos. */
174 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
175 /* The arg pointer varies if it is not a fixed register. */
176 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
177 return 0;
178 if (x == pic_offset_table_rtx
179 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
180 /* ??? When call-clobbered, the value is stable modulo the restore
181 that must happen after a call. This currently screws up
182 local-alloc into believing that the restore is not needed, so we
183 must return 0 only if we are called from alias analysis. */
184 && for_alias
185 #endif
187 return 0;
188 return 1;
190 case LO_SUM:
191 /* The operand 0 of a LO_SUM is considered constant
192 (in fact it is related specifically to operand 1)
193 during alias analysis. */
194 return (! for_alias && rtx_varies_p (XEXP (x, 0), for_alias))
195 || rtx_varies_p (XEXP (x, 1), for_alias);
197 case ASM_OPERANDS:
198 if (MEM_VOLATILE_P (x))
199 return 1;
201 /* Fall through. */
203 default:
204 break;
207 fmt = GET_RTX_FORMAT (code);
208 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
209 if (fmt[i] == 'e')
211 if (rtx_varies_p (XEXP (x, i), for_alias))
212 return 1;
214 else if (fmt[i] == 'E')
216 int j;
217 for (j = 0; j < XVECLEN (x, i); j++)
218 if (rtx_varies_p (XVECEXP (x, i, j), for_alias))
219 return 1;
222 return 0;
225 /* Return 0 if the use of X as an address in a MEM can cause a trap. */
228 rtx_addr_can_trap_p (rtx x)
230 enum rtx_code code = GET_CODE (x);
232 switch (code)
234 case SYMBOL_REF:
235 return SYMBOL_REF_WEAK (x);
237 case LABEL_REF:
238 return 0;
240 case REG:
241 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
242 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
243 || x == stack_pointer_rtx
244 /* The arg pointer varies if it is not a fixed register. */
245 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
246 return 0;
247 /* All of the virtual frame registers are stack references. */
248 if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
249 && REGNO (x) <= LAST_VIRTUAL_REGISTER)
250 return 0;
251 return 1;
253 case CONST:
254 return rtx_addr_can_trap_p (XEXP (x, 0));
256 case PLUS:
257 /* An address is assumed not to trap if it is an address that can't
258 trap plus a constant integer or it is the pic register plus a
259 constant. */
260 return ! ((! rtx_addr_can_trap_p (XEXP (x, 0))
261 && GET_CODE (XEXP (x, 1)) == CONST_INT)
262 || (XEXP (x, 0) == pic_offset_table_rtx
263 && CONSTANT_P (XEXP (x, 1))));
265 case LO_SUM:
266 case PRE_MODIFY:
267 return rtx_addr_can_trap_p (XEXP (x, 1));
269 case PRE_DEC:
270 case PRE_INC:
271 case POST_DEC:
272 case POST_INC:
273 case POST_MODIFY:
274 return rtx_addr_can_trap_p (XEXP (x, 0));
276 default:
277 break;
280 /* If it isn't one of the case above, it can cause a trap. */
281 return 1;
284 /* Return true if X is an address that is known to not be zero. */
286 bool
287 nonzero_address_p (rtx x)
289 enum rtx_code code = GET_CODE (x);
291 switch (code)
293 case SYMBOL_REF:
294 return !SYMBOL_REF_WEAK (x);
296 case LABEL_REF:
297 return true;
299 case REG:
300 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
301 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
302 || x == stack_pointer_rtx
303 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
304 return true;
305 /* All of the virtual frame registers are stack references. */
306 if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
307 && REGNO (x) <= LAST_VIRTUAL_REGISTER)
308 return true;
309 return false;
311 case CONST:
312 return nonzero_address_p (XEXP (x, 0));
314 case PLUS:
315 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
317 /* Pointers aren't allowed to wrap. If we've got a register
318 that is known to be a pointer, and a positive offset, then
319 the composite can't be zero. */
320 if (INTVAL (XEXP (x, 1)) > 0
321 && REG_P (XEXP (x, 0))
322 && REG_POINTER (XEXP (x, 0)))
323 return true;
325 return nonzero_address_p (XEXP (x, 0));
327 /* Handle PIC references. */
328 else if (XEXP (x, 0) == pic_offset_table_rtx
329 && CONSTANT_P (XEXP (x, 1)))
330 return true;
331 return false;
333 case PRE_MODIFY:
334 /* Similar to the above; allow positive offsets. Further, since
335 auto-inc is only allowed in memories, the register must be a
336 pointer. */
337 if (GET_CODE (XEXP (x, 1)) == CONST_INT
338 && INTVAL (XEXP (x, 1)) > 0)
339 return true;
340 return nonzero_address_p (XEXP (x, 0));
342 case PRE_INC:
343 /* Similarly. Further, the offset is always positive. */
344 return true;
346 case PRE_DEC:
347 case POST_DEC:
348 case POST_INC:
349 case POST_MODIFY:
350 return nonzero_address_p (XEXP (x, 0));
352 case LO_SUM:
353 return nonzero_address_p (XEXP (x, 1));
355 default:
356 break;
359 /* If it isn't one of the case above, might be zero. */
360 return false;
363 /* Return 1 if X refers to a memory location whose address
364 cannot be compared reliably with constant addresses,
365 or if X refers to a BLKmode memory object.
366 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
367 zero, we are slightly more conservative. */
370 rtx_addr_varies_p (rtx x, int for_alias)
372 enum rtx_code code;
373 int i;
374 const char *fmt;
376 if (x == 0)
377 return 0;
379 code = GET_CODE (x);
380 if (code == MEM)
381 return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0), for_alias);
383 fmt = GET_RTX_FORMAT (code);
384 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
385 if (fmt[i] == 'e')
387 if (rtx_addr_varies_p (XEXP (x, i), for_alias))
388 return 1;
390 else if (fmt[i] == 'E')
392 int j;
393 for (j = 0; j < XVECLEN (x, i); j++)
394 if (rtx_addr_varies_p (XVECEXP (x, i, j), for_alias))
395 return 1;
397 return 0;
400 /* Return the value of the integer term in X, if one is apparent;
401 otherwise return 0.
402 Only obvious integer terms are detected.
403 This is used in cse.c with the `related_value' field. */
405 HOST_WIDE_INT
406 get_integer_term (rtx x)
408 if (GET_CODE (x) == CONST)
409 x = XEXP (x, 0);
411 if (GET_CODE (x) == MINUS
412 && GET_CODE (XEXP (x, 1)) == CONST_INT)
413 return - INTVAL (XEXP (x, 1));
414 if (GET_CODE (x) == PLUS
415 && GET_CODE (XEXP (x, 1)) == CONST_INT)
416 return INTVAL (XEXP (x, 1));
417 return 0;
420 /* If X is a constant, return the value sans apparent integer term;
421 otherwise return 0.
422 Only obvious integer terms are detected. */
425 get_related_value (rtx x)
427 if (GET_CODE (x) != CONST)
428 return 0;
429 x = XEXP (x, 0);
430 if (GET_CODE (x) == PLUS
431 && GET_CODE (XEXP (x, 1)) == CONST_INT)
432 return XEXP (x, 0);
433 else if (GET_CODE (x) == MINUS
434 && GET_CODE (XEXP (x, 1)) == CONST_INT)
435 return XEXP (x, 0);
436 return 0;
439 /* Given a tablejump insn INSN, return the RTL expression for the offset
440 into the jump table. If the offset cannot be determined, then return
441 NULL_RTX.
443 If EARLIEST is nonzero, it is a pointer to a place where the earliest
444 insn used in locating the offset was found. */
447 get_jump_table_offset (rtx insn, rtx *earliest)
449 rtx label = NULL;
450 rtx table = NULL;
451 rtx set;
452 rtx old_insn;
453 rtx x;
454 rtx old_x;
455 rtx y;
456 rtx old_y;
457 int i;
459 if (!tablejump_p (insn, &label, &table) || !(set = single_set (insn)))
460 return NULL_RTX;
462 x = SET_SRC (set);
464 /* Some targets (eg, ARM) emit a tablejump that also
465 contains the out-of-range target. */
466 if (GET_CODE (x) == IF_THEN_ELSE
467 && GET_CODE (XEXP (x, 2)) == LABEL_REF)
468 x = XEXP (x, 1);
470 /* Search backwards and locate the expression stored in X. */
471 for (old_x = NULL_RTX; REG_P (x) && x != old_x;
472 old_x = x, x = find_last_value (x, &insn, NULL_RTX, 0))
475 /* If X is an expression using a relative address then strip
476 off the addition / subtraction of PC, PIC_OFFSET_TABLE_REGNUM,
477 or the jump table label. */
478 if (GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC
479 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS))
481 for (i = 0; i < 2; i++)
483 old_insn = insn;
484 y = XEXP (x, i);
486 if (y == pc_rtx || y == pic_offset_table_rtx)
487 break;
489 for (old_y = NULL_RTX; REG_P (y) && y != old_y;
490 old_y = y, y = find_last_value (y, &old_insn, NULL_RTX, 0))
493 if ((GET_CODE (y) == LABEL_REF && XEXP (y, 0) == label))
494 break;
497 if (i >= 2)
498 return NULL_RTX;
500 x = XEXP (x, 1 - i);
502 for (old_x = NULL_RTX; REG_P (x) && x != old_x;
503 old_x = x, x = find_last_value (x, &insn, NULL_RTX, 0))
507 /* Strip off any sign or zero extension. */
508 if (GET_CODE (x) == SIGN_EXTEND || GET_CODE (x) == ZERO_EXTEND)
510 x = XEXP (x, 0);
512 for (old_x = NULL_RTX; REG_P (x) && x != old_x;
513 old_x = x, x = find_last_value (x, &insn, NULL_RTX, 0))
517 /* If X isn't a MEM then this isn't a tablejump we understand. */
518 if (!MEM_P (x))
519 return NULL_RTX;
521 /* Strip off the MEM. */
522 x = XEXP (x, 0);
524 for (old_x = NULL_RTX; REG_P (x) && x != old_x;
525 old_x = x, x = find_last_value (x, &insn, NULL_RTX, 0))
528 /* If X isn't a PLUS than this isn't a tablejump we understand. */
529 if (GET_CODE (x) != PLUS)
530 return NULL_RTX;
532 /* At this point we should have an expression representing the jump table
533 plus an offset. Examine each operand in order to determine which one
534 represents the jump table. Knowing that tells us that the other operand
535 must represent the offset. */
536 for (i = 0; i < 2; i++)
538 old_insn = insn;
539 y = XEXP (x, i);
541 for (old_y = NULL_RTX; REG_P (y) && y != old_y;
542 old_y = y, y = find_last_value (y, &old_insn, NULL_RTX, 0))
545 if ((GET_CODE (y) == CONST || GET_CODE (y) == LABEL_REF)
546 && reg_mentioned_p (label, y))
547 break;
550 if (i >= 2)
551 return NULL_RTX;
553 x = XEXP (x, 1 - i);
555 /* Strip off the addition / subtraction of PIC_OFFSET_TABLE_REGNUM. */
556 if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS)
557 for (i = 0; i < 2; i++)
558 if (XEXP (x, i) == pic_offset_table_rtx)
560 x = XEXP (x, 1 - i);
561 break;
564 if (earliest)
565 *earliest = insn;
567 /* Return the RTL expression representing the offset. */
568 return x;
571 /* A subroutine of global_reg_mentioned_p, returns 1 if *LOC mentions
572 a global register. */
574 static int
575 global_reg_mentioned_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
577 int regno;
578 rtx x = *loc;
580 if (! x)
581 return 0;
583 switch (GET_CODE (x))
585 case SUBREG:
586 if (REG_P (SUBREG_REG (x)))
588 if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
589 && global_regs[subreg_regno (x)])
590 return 1;
591 return 0;
593 break;
595 case REG:
596 regno = REGNO (x);
597 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
598 return 1;
599 return 0;
601 case SCRATCH:
602 case PC:
603 case CC0:
604 case CONST_INT:
605 case CONST_DOUBLE:
606 case CONST:
607 case LABEL_REF:
608 return 0;
610 case CALL:
611 /* A non-constant call might use a global register. */
612 return 1;
614 default:
615 break;
618 return 0;
621 /* Returns nonzero if X mentions a global register. */
624 global_reg_mentioned_p (rtx x)
626 if (INSN_P (x))
628 if (CALL_P (x))
630 if (! CONST_OR_PURE_CALL_P (x))
631 return 1;
632 x = CALL_INSN_FUNCTION_USAGE (x);
633 if (x == 0)
634 return 0;
636 else
637 x = PATTERN (x);
640 return for_each_rtx (&x, global_reg_mentioned_p_1, NULL);
643 /* Return the number of places FIND appears within X. If COUNT_DEST is
644 zero, we do not count occurrences inside the destination of a SET. */
647 count_occurrences (rtx x, rtx find, int count_dest)
649 int i, j;
650 enum rtx_code code;
651 const char *format_ptr;
652 int count;
654 if (x == find)
655 return 1;
657 code = GET_CODE (x);
659 switch (code)
661 case REG:
662 case CONST_INT:
663 case CONST_DOUBLE:
664 case CONST_VECTOR:
665 case SYMBOL_REF:
666 case CODE_LABEL:
667 case PC:
668 case CC0:
669 return 0;
671 case MEM:
672 if (MEM_P (find) && rtx_equal_p (x, find))
673 return 1;
674 break;
676 case SET:
677 if (SET_DEST (x) == find && ! count_dest)
678 return count_occurrences (SET_SRC (x), find, count_dest);
679 break;
681 default:
682 break;
685 format_ptr = GET_RTX_FORMAT (code);
686 count = 0;
688 for (i = 0; i < GET_RTX_LENGTH (code); i++)
690 switch (*format_ptr++)
692 case 'e':
693 count += count_occurrences (XEXP (x, i), find, count_dest);
694 break;
696 case 'E':
697 for (j = 0; j < XVECLEN (x, i); j++)
698 count += count_occurrences (XVECEXP (x, i, j), find, count_dest);
699 break;
702 return count;
705 /* Nonzero if register REG appears somewhere within IN.
706 Also works if REG is not a register; in this case it checks
707 for a subexpression of IN that is Lisp "equal" to REG. */
710 reg_mentioned_p (rtx reg, rtx in)
712 const char *fmt;
713 int i;
714 enum rtx_code code;
716 if (in == 0)
717 return 0;
719 if (reg == in)
720 return 1;
722 if (GET_CODE (in) == LABEL_REF)
723 return reg == XEXP (in, 0);
725 code = GET_CODE (in);
727 switch (code)
729 /* Compare registers by number. */
730 case REG:
731 return REG_P (reg) && REGNO (in) == REGNO (reg);
733 /* These codes have no constituent expressions
734 and are unique. */
735 case SCRATCH:
736 case CC0:
737 case PC:
738 return 0;
740 case CONST_INT:
741 case CONST_VECTOR:
742 case CONST_DOUBLE:
743 /* These are kept unique for a given value. */
744 return 0;
746 default:
747 break;
750 if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
751 return 1;
753 fmt = GET_RTX_FORMAT (code);
755 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
757 if (fmt[i] == 'E')
759 int j;
760 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
761 if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
762 return 1;
764 else if (fmt[i] == 'e'
765 && reg_mentioned_p (reg, XEXP (in, i)))
766 return 1;
768 return 0;
771 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
772 no CODE_LABEL insn. */
775 no_labels_between_p (rtx beg, rtx end)
777 rtx p;
778 if (beg == end)
779 return 0;
780 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
781 if (LABEL_P (p))
782 return 0;
783 return 1;
786 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
787 no JUMP_INSN insn. */
790 no_jumps_between_p (rtx beg, rtx end)
792 rtx p;
793 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
794 if (JUMP_P (p))
795 return 0;
796 return 1;
799 /* Nonzero if register REG is used in an insn between
800 FROM_INSN and TO_INSN (exclusive of those two). */
803 reg_used_between_p (rtx reg, rtx from_insn, rtx to_insn)
805 rtx insn;
807 if (from_insn == to_insn)
808 return 0;
810 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
811 if (INSN_P (insn)
812 && (reg_overlap_mentioned_p (reg, PATTERN (insn))
813 || (CALL_P (insn)
814 && (find_reg_fusage (insn, USE, reg)
815 || find_reg_fusage (insn, CLOBBER, reg)))))
816 return 1;
817 return 0;
820 /* Nonzero if the old value of X, a register, is referenced in BODY. If X
821 is entirely replaced by a new value and the only use is as a SET_DEST,
822 we do not consider it a reference. */
825 reg_referenced_p (rtx x, rtx body)
827 int i;
829 switch (GET_CODE (body))
831 case SET:
832 if (reg_overlap_mentioned_p (x, SET_SRC (body)))
833 return 1;
835 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
836 of a REG that occupies all of the REG, the insn references X if
837 it is mentioned in the destination. */
838 if (GET_CODE (SET_DEST (body)) != CC0
839 && GET_CODE (SET_DEST (body)) != PC
840 && !REG_P (SET_DEST (body))
841 && ! (GET_CODE (SET_DEST (body)) == SUBREG
842 && REG_P (SUBREG_REG (SET_DEST (body)))
843 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
844 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
845 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
846 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
847 && reg_overlap_mentioned_p (x, SET_DEST (body)))
848 return 1;
849 return 0;
851 case ASM_OPERANDS:
852 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
853 if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
854 return 1;
855 return 0;
857 case CALL:
858 case USE:
859 case IF_THEN_ELSE:
860 return reg_overlap_mentioned_p (x, body);
862 case TRAP_IF:
863 return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
865 case PREFETCH:
866 return reg_overlap_mentioned_p (x, XEXP (body, 0));
868 case UNSPEC:
869 case UNSPEC_VOLATILE:
870 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
871 if (reg_overlap_mentioned_p (x, XVECEXP (body, 0, i)))
872 return 1;
873 return 0;
875 case PARALLEL:
876 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
877 if (reg_referenced_p (x, XVECEXP (body, 0, i)))
878 return 1;
879 return 0;
881 case CLOBBER:
882 if (MEM_P (XEXP (body, 0)))
883 if (reg_overlap_mentioned_p (x, XEXP (XEXP (body, 0), 0)))
884 return 1;
885 return 0;
887 case COND_EXEC:
888 if (reg_overlap_mentioned_p (x, COND_EXEC_TEST (body)))
889 return 1;
890 return reg_referenced_p (x, COND_EXEC_CODE (body));
892 default:
893 return 0;
897 /* Nonzero if register REG is referenced in an insn between
898 FROM_INSN and TO_INSN (exclusive of those two). Sets of REG do
899 not count. */
902 reg_referenced_between_p (rtx reg, rtx from_insn, rtx to_insn)
904 rtx insn;
906 if (from_insn == to_insn)
907 return 0;
909 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
910 if (INSN_P (insn)
911 && (reg_referenced_p (reg, PATTERN (insn))
912 || (CALL_P (insn)
913 && find_reg_fusage (insn, USE, reg))))
914 return 1;
915 return 0;
918 /* Nonzero if register REG is set or clobbered in an insn between
919 FROM_INSN and TO_INSN (exclusive of those two). */
922 reg_set_between_p (rtx reg, rtx from_insn, rtx to_insn)
924 rtx insn;
926 if (from_insn == to_insn)
927 return 0;
929 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
930 if (INSN_P (insn) && reg_set_p (reg, insn))
931 return 1;
932 return 0;
935 /* Internals of reg_set_between_p. */
937 reg_set_p (rtx reg, rtx insn)
939 /* We can be passed an insn or part of one. If we are passed an insn,
940 check if a side-effect of the insn clobbers REG. */
941 if (INSN_P (insn)
942 && (FIND_REG_INC_NOTE (insn, reg)
943 || (CALL_P (insn)
944 && ((REG_P (reg)
945 && REGNO (reg) < FIRST_PSEUDO_REGISTER
946 && TEST_HARD_REG_BIT (regs_invalidated_by_call,
947 REGNO (reg)))
948 || MEM_P (reg)
949 || find_reg_fusage (insn, CLOBBER, reg)))))
950 return 1;
952 return set_of (reg, insn) != NULL_RTX;
955 /* Similar to reg_set_between_p, but check all registers in X. Return 0
956 only if none of them are modified between START and END. Do not
957 consider non-registers one way or the other. */
960 regs_set_between_p (rtx x, rtx start, rtx end)
962 enum rtx_code code = GET_CODE (x);
963 const char *fmt;
964 int i, j;
966 switch (code)
968 case CONST_INT:
969 case CONST_DOUBLE:
970 case CONST_VECTOR:
971 case CONST:
972 case SYMBOL_REF:
973 case LABEL_REF:
974 case PC:
975 case CC0:
976 return 0;
978 case REG:
979 return reg_set_between_p (x, start, end);
981 default:
982 break;
985 fmt = GET_RTX_FORMAT (code);
986 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
988 if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
989 return 1;
991 else if (fmt[i] == 'E')
992 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
993 if (regs_set_between_p (XVECEXP (x, i, j), start, end))
994 return 1;
997 return 0;
1000 /* Similar to reg_set_between_p, but check all registers in X. Return 0
1001 only if none of them are modified between START and END. Return 1 if
1002 X contains a MEM; this routine does usememory aliasing. */
1005 modified_between_p (rtx x, rtx start, rtx end)
1007 enum rtx_code code = GET_CODE (x);
1008 const char *fmt;
1009 int i, j;
1010 rtx insn;
1012 if (start == end)
1013 return 0;
1015 switch (code)
1017 case CONST_INT:
1018 case CONST_DOUBLE:
1019 case CONST_VECTOR:
1020 case CONST:
1021 case SYMBOL_REF:
1022 case LABEL_REF:
1023 return 0;
1025 case PC:
1026 case CC0:
1027 return 1;
1029 case MEM:
1030 if (RTX_UNCHANGING_P (x))
1031 return 0;
1032 if (modified_between_p (XEXP (x, 0), start, end))
1033 return 1;
1034 for (insn = NEXT_INSN (start); insn != end; insn = NEXT_INSN (insn))
1035 if (memory_modified_in_insn_p (x, insn))
1036 return 1;
1037 return 0;
1038 break;
1040 case REG:
1041 return reg_set_between_p (x, start, end);
1043 default:
1044 break;
1047 fmt = GET_RTX_FORMAT (code);
1048 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1050 if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
1051 return 1;
1053 else if (fmt[i] == 'E')
1054 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1055 if (modified_between_p (XVECEXP (x, i, j), start, end))
1056 return 1;
1059 return 0;
1062 /* Similar to reg_set_p, but check all registers in X. Return 0 only if none
1063 of them are modified in INSN. Return 1 if X contains a MEM; this routine
1064 does use memory aliasing. */
1067 modified_in_p (rtx x, rtx insn)
1069 enum rtx_code code = GET_CODE (x);
1070 const char *fmt;
1071 int i, j;
1073 switch (code)
1075 case CONST_INT:
1076 case CONST_DOUBLE:
1077 case CONST_VECTOR:
1078 case CONST:
1079 case SYMBOL_REF:
1080 case LABEL_REF:
1081 return 0;
1083 case PC:
1084 case CC0:
1085 return 1;
1087 case MEM:
1088 if (RTX_UNCHANGING_P (x))
1089 return 0;
1090 if (modified_in_p (XEXP (x, 0), insn))
1091 return 1;
1092 if (memory_modified_in_insn_p (x, insn))
1093 return 1;
1094 return 0;
1095 break;
1097 case REG:
1098 return reg_set_p (x, insn);
1100 default:
1101 break;
1104 fmt = GET_RTX_FORMAT (code);
1105 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1107 if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
1108 return 1;
1110 else if (fmt[i] == 'E')
1111 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1112 if (modified_in_p (XVECEXP (x, i, j), insn))
1113 return 1;
1116 return 0;
1119 /* Return true if anything in insn X is (anti,output,true) dependent on
1120 anything in insn Y. */
1123 insn_dependent_p (rtx x, rtx y)
1125 rtx tmp;
1127 if (! INSN_P (x) || ! INSN_P (y))
1128 abort ();
1130 tmp = PATTERN (y);
1131 note_stores (PATTERN (x), insn_dependent_p_1, &tmp);
1132 if (tmp == NULL_RTX)
1133 return 1;
1135 tmp = PATTERN (x);
1136 note_stores (PATTERN (y), insn_dependent_p_1, &tmp);
1137 if (tmp == NULL_RTX)
1138 return 1;
1140 return 0;
1143 /* A helper routine for insn_dependent_p called through note_stores. */
1145 static void
1146 insn_dependent_p_1 (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
1148 rtx * pinsn = (rtx *) data;
1150 if (*pinsn && reg_mentioned_p (x, *pinsn))
1151 *pinsn = NULL_RTX;
1154 /* Helper function for set_of. */
1155 struct set_of_data
1157 rtx found;
1158 rtx pat;
1161 static void
1162 set_of_1 (rtx x, rtx pat, void *data1)
1164 struct set_of_data *data = (struct set_of_data *) (data1);
1165 if (rtx_equal_p (x, data->pat)
1166 || (!MEM_P (x) && reg_overlap_mentioned_p (data->pat, x)))
1167 data->found = pat;
1170 /* Give an INSN, return a SET or CLOBBER expression that does modify PAT
1171 (either directly or via STRICT_LOW_PART and similar modifiers). */
1173 set_of (rtx pat, rtx insn)
1175 struct set_of_data data;
1176 data.found = NULL_RTX;
1177 data.pat = pat;
1178 note_stores (INSN_P (insn) ? PATTERN (insn) : insn, set_of_1, &data);
1179 return data.found;
1182 /* Given an INSN, return a SET expression if this insn has only a single SET.
1183 It may also have CLOBBERs, USEs, or SET whose output
1184 will not be used, which we ignore. */
1187 single_set_2 (rtx insn, rtx pat)
1189 rtx set = NULL;
1190 int set_verified = 1;
1191 int i;
1193 if (GET_CODE (pat) == PARALLEL)
1195 for (i = 0; i < XVECLEN (pat, 0); i++)
1197 rtx sub = XVECEXP (pat, 0, i);
1198 switch (GET_CODE (sub))
1200 case USE:
1201 case CLOBBER:
1202 break;
1204 case SET:
1205 /* We can consider insns having multiple sets, where all
1206 but one are dead as single set insns. In common case
1207 only single set is present in the pattern so we want
1208 to avoid checking for REG_UNUSED notes unless necessary.
1210 When we reach set first time, we just expect this is
1211 the single set we are looking for and only when more
1212 sets are found in the insn, we check them. */
1213 if (!set_verified)
1215 if (find_reg_note (insn, REG_UNUSED, SET_DEST (set))
1216 && !side_effects_p (set))
1217 set = NULL;
1218 else
1219 set_verified = 1;
1221 if (!set)
1222 set = sub, set_verified = 0;
1223 else if (!find_reg_note (insn, REG_UNUSED, SET_DEST (sub))
1224 || side_effects_p (sub))
1225 return NULL_RTX;
1226 break;
1228 default:
1229 return NULL_RTX;
1233 return set;
1236 /* Given an INSN, return nonzero if it has more than one SET, else return
1237 zero. */
1240 multiple_sets (rtx insn)
1242 int found;
1243 int i;
1245 /* INSN must be an insn. */
1246 if (! INSN_P (insn))
1247 return 0;
1249 /* Only a PARALLEL can have multiple SETs. */
1250 if (GET_CODE (PATTERN (insn)) == PARALLEL)
1252 for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1253 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1255 /* If we have already found a SET, then return now. */
1256 if (found)
1257 return 1;
1258 else
1259 found = 1;
1263 /* Either zero or one SET. */
1264 return 0;
1267 /* Return nonzero if the destination of SET equals the source
1268 and there are no side effects. */
1271 set_noop_p (rtx set)
1273 rtx src = SET_SRC (set);
1274 rtx dst = SET_DEST (set);
1276 if (dst == pc_rtx && src == pc_rtx)
1277 return 1;
1279 if (MEM_P (dst) && MEM_P (src))
1280 return rtx_equal_p (dst, src) && !side_effects_p (dst);
1282 if (GET_CODE (dst) == SIGN_EXTRACT
1283 || GET_CODE (dst) == ZERO_EXTRACT)
1284 return rtx_equal_p (XEXP (dst, 0), src)
1285 && ! BYTES_BIG_ENDIAN && XEXP (dst, 2) == const0_rtx
1286 && !side_effects_p (src);
1288 if (GET_CODE (dst) == STRICT_LOW_PART)
1289 dst = XEXP (dst, 0);
1291 if (GET_CODE (src) == SUBREG && GET_CODE (dst) == SUBREG)
1293 if (SUBREG_BYTE (src) != SUBREG_BYTE (dst))
1294 return 0;
1295 src = SUBREG_REG (src);
1296 dst = SUBREG_REG (dst);
1299 return (REG_P (src) && REG_P (dst)
1300 && REGNO (src) == REGNO (dst));
1303 /* Return nonzero if an insn consists only of SETs, each of which only sets a
1304 value to itself. */
1307 noop_move_p (rtx insn)
1309 rtx pat = PATTERN (insn);
1311 if (INSN_CODE (insn) == NOOP_MOVE_INSN_CODE)
1312 return 1;
1314 /* Insns carrying these notes are useful later on. */
1315 if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
1316 return 0;
1318 /* For now treat an insn with a REG_RETVAL note as a
1319 a special insn which should not be considered a no-op. */
1320 if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
1321 return 0;
1323 if (GET_CODE (pat) == SET && set_noop_p (pat))
1324 return 1;
1326 if (GET_CODE (pat) == PARALLEL)
1328 int i;
1329 /* If nothing but SETs of registers to themselves,
1330 this insn can also be deleted. */
1331 for (i = 0; i < XVECLEN (pat, 0); i++)
1333 rtx tem = XVECEXP (pat, 0, i);
1335 if (GET_CODE (tem) == USE
1336 || GET_CODE (tem) == CLOBBER)
1337 continue;
1339 if (GET_CODE (tem) != SET || ! set_noop_p (tem))
1340 return 0;
1343 return 1;
1345 return 0;
1349 /* Return the last thing that X was assigned from before *PINSN. If VALID_TO
1350 is not NULL_RTX then verify that the object is not modified up to VALID_TO.
1351 If the object was modified, if we hit a partial assignment to X, or hit a
1352 CODE_LABEL first, return X. If we found an assignment, update *PINSN to
1353 point to it. ALLOW_HWREG is set to 1 if hardware registers are allowed to
1354 be the src. */
1357 find_last_value (rtx x, rtx *pinsn, rtx valid_to, int allow_hwreg)
1359 rtx p;
1361 for (p = PREV_INSN (*pinsn); p && !LABEL_P (p);
1362 p = PREV_INSN (p))
1363 if (INSN_P (p))
1365 rtx set = single_set (p);
1366 rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
1368 if (set && rtx_equal_p (x, SET_DEST (set)))
1370 rtx src = SET_SRC (set);
1372 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
1373 src = XEXP (note, 0);
1375 if ((valid_to == NULL_RTX
1376 || ! modified_between_p (src, PREV_INSN (p), valid_to))
1377 /* Reject hard registers because we don't usually want
1378 to use them; we'd rather use a pseudo. */
1379 && (! (REG_P (src)
1380 && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
1382 *pinsn = p;
1383 return src;
1387 /* If set in non-simple way, we don't have a value. */
1388 if (reg_set_p (x, p))
1389 break;
1392 return x;
1395 /* Return nonzero if register in range [REGNO, ENDREGNO)
1396 appears either explicitly or implicitly in X
1397 other than being stored into.
1399 References contained within the substructure at LOC do not count.
1400 LOC may be zero, meaning don't ignore anything. */
1403 refers_to_regno_p (unsigned int regno, unsigned int endregno, rtx x,
1404 rtx *loc)
1406 int i;
1407 unsigned int x_regno;
1408 RTX_CODE code;
1409 const char *fmt;
1411 repeat:
1412 /* The contents of a REG_NONNEG note is always zero, so we must come here
1413 upon repeat in case the last REG_NOTE is a REG_NONNEG note. */
1414 if (x == 0)
1415 return 0;
1417 code = GET_CODE (x);
1419 switch (code)
1421 case REG:
1422 x_regno = REGNO (x);
1424 /* If we modifying the stack, frame, or argument pointer, it will
1425 clobber a virtual register. In fact, we could be more precise,
1426 but it isn't worth it. */
1427 if ((x_regno == STACK_POINTER_REGNUM
1428 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1429 || x_regno == ARG_POINTER_REGNUM
1430 #endif
1431 || x_regno == FRAME_POINTER_REGNUM)
1432 && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
1433 return 1;
1435 return (endregno > x_regno
1436 && regno < x_regno + (x_regno < FIRST_PSEUDO_REGISTER
1437 ? hard_regno_nregs[x_regno][GET_MODE (x)]
1438 : 1));
1440 case SUBREG:
1441 /* If this is a SUBREG of a hard reg, we can see exactly which
1442 registers are being modified. Otherwise, handle normally. */
1443 if (REG_P (SUBREG_REG (x))
1444 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
1446 unsigned int inner_regno = subreg_regno (x);
1447 unsigned int inner_endregno
1448 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
1449 ? hard_regno_nregs[inner_regno][GET_MODE (x)] : 1);
1451 return endregno > inner_regno && regno < inner_endregno;
1453 break;
1455 case CLOBBER:
1456 case SET:
1457 if (&SET_DEST (x) != loc
1458 /* Note setting a SUBREG counts as referring to the REG it is in for
1459 a pseudo but not for hard registers since we can
1460 treat each word individually. */
1461 && ((GET_CODE (SET_DEST (x)) == SUBREG
1462 && loc != &SUBREG_REG (SET_DEST (x))
1463 && REG_P (SUBREG_REG (SET_DEST (x)))
1464 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
1465 && refers_to_regno_p (regno, endregno,
1466 SUBREG_REG (SET_DEST (x)), loc))
1467 || (!REG_P (SET_DEST (x))
1468 && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
1469 return 1;
1471 if (code == CLOBBER || loc == &SET_SRC (x))
1472 return 0;
1473 x = SET_SRC (x);
1474 goto repeat;
1476 default:
1477 break;
1480 /* X does not match, so try its subexpressions. */
1482 fmt = GET_RTX_FORMAT (code);
1483 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1485 if (fmt[i] == 'e' && loc != &XEXP (x, i))
1487 if (i == 0)
1489 x = XEXP (x, 0);
1490 goto repeat;
1492 else
1493 if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
1494 return 1;
1496 else if (fmt[i] == 'E')
1498 int j;
1499 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1500 if (loc != &XVECEXP (x, i, j)
1501 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
1502 return 1;
1505 return 0;
1508 /* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
1509 we check if any register number in X conflicts with the relevant register
1510 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
1511 contains a MEM (we don't bother checking for memory addresses that can't
1512 conflict because we expect this to be a rare case. */
1515 reg_overlap_mentioned_p (rtx x, rtx in)
1517 unsigned int regno, endregno;
1519 /* If either argument is a constant, then modifying X can not
1520 affect IN. Here we look at IN, we can profitably combine
1521 CONSTANT_P (x) with the switch statement below. */
1522 if (CONSTANT_P (in))
1523 return 0;
1525 recurse:
1526 switch (GET_CODE (x))
1528 case STRICT_LOW_PART:
1529 case ZERO_EXTRACT:
1530 case SIGN_EXTRACT:
1531 /* Overly conservative. */
1532 x = XEXP (x, 0);
1533 goto recurse;
1535 case SUBREG:
1536 regno = REGNO (SUBREG_REG (x));
1537 if (regno < FIRST_PSEUDO_REGISTER)
1538 regno = subreg_regno (x);
1539 goto do_reg;
1541 case REG:
1542 regno = REGNO (x);
1543 do_reg:
1544 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
1545 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
1546 return refers_to_regno_p (regno, endregno, in, (rtx*) 0);
1548 case MEM:
1550 const char *fmt;
1551 int i;
1553 if (MEM_P (in))
1554 return 1;
1556 fmt = GET_RTX_FORMAT (GET_CODE (in));
1557 for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
1558 if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
1559 return 1;
1561 return 0;
1564 case SCRATCH:
1565 case PC:
1566 case CC0:
1567 return reg_mentioned_p (x, in);
1569 case PARALLEL:
1571 int i;
1573 /* If any register in here refers to it we return true. */
1574 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1575 if (XEXP (XVECEXP (x, 0, i), 0) != 0
1576 && reg_overlap_mentioned_p (XEXP (XVECEXP (x, 0, i), 0), in))
1577 return 1;
1578 return 0;
1581 default:
1582 #ifdef ENABLE_CHECKING
1583 if (!CONSTANT_P (x))
1584 abort ();
1585 #endif
1587 return 0;
1591 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1592 (X would be the pattern of an insn).
1593 FUN receives two arguments:
1594 the REG, MEM, CC0 or PC being stored in or clobbered,
1595 the SET or CLOBBER rtx that does the store.
1597 If the item being stored in or clobbered is a SUBREG of a hard register,
1598 the SUBREG will be passed. */
1600 void
1601 note_stores (rtx x, void (*fun) (rtx, rtx, void *), void *data)
1603 int i;
1605 if (GET_CODE (x) == COND_EXEC)
1606 x = COND_EXEC_CODE (x);
1608 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1610 rtx dest = SET_DEST (x);
1612 while ((GET_CODE (dest) == SUBREG
1613 && (!REG_P (SUBREG_REG (dest))
1614 || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1615 || GET_CODE (dest) == ZERO_EXTRACT
1616 || GET_CODE (dest) == SIGN_EXTRACT
1617 || GET_CODE (dest) == STRICT_LOW_PART)
1618 dest = XEXP (dest, 0);
1620 /* If we have a PARALLEL, SET_DEST is a list of EXPR_LIST expressions,
1621 each of whose first operand is a register. */
1622 if (GET_CODE (dest) == PARALLEL)
1624 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1625 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1626 (*fun) (XEXP (XVECEXP (dest, 0, i), 0), x, data);
1628 else
1629 (*fun) (dest, x, data);
1632 else if (GET_CODE (x) == PARALLEL)
1633 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1634 note_stores (XVECEXP (x, 0, i), fun, data);
1637 /* Like notes_stores, but call FUN for each expression that is being
1638 referenced in PBODY, a pointer to the PATTERN of an insn. We only call
1639 FUN for each expression, not any interior subexpressions. FUN receives a
1640 pointer to the expression and the DATA passed to this function.
1642 Note that this is not quite the same test as that done in reg_referenced_p
1643 since that considers something as being referenced if it is being
1644 partially set, while we do not. */
1646 void
1647 note_uses (rtx *pbody, void (*fun) (rtx *, void *), void *data)
1649 rtx body = *pbody;
1650 int i;
1652 switch (GET_CODE (body))
1654 case COND_EXEC:
1655 (*fun) (&COND_EXEC_TEST (body), data);
1656 note_uses (&COND_EXEC_CODE (body), fun, data);
1657 return;
1659 case PARALLEL:
1660 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1661 note_uses (&XVECEXP (body, 0, i), fun, data);
1662 return;
1664 case USE:
1665 (*fun) (&XEXP (body, 0), data);
1666 return;
1668 case ASM_OPERANDS:
1669 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
1670 (*fun) (&ASM_OPERANDS_INPUT (body, i), data);
1671 return;
1673 case TRAP_IF:
1674 (*fun) (&TRAP_CONDITION (body), data);
1675 return;
1677 case PREFETCH:
1678 (*fun) (&XEXP (body, 0), data);
1679 return;
1681 case UNSPEC:
1682 case UNSPEC_VOLATILE:
1683 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1684 (*fun) (&XVECEXP (body, 0, i), data);
1685 return;
1687 case CLOBBER:
1688 if (MEM_P (XEXP (body, 0)))
1689 (*fun) (&XEXP (XEXP (body, 0), 0), data);
1690 return;
1692 case SET:
1694 rtx dest = SET_DEST (body);
1696 /* For sets we replace everything in source plus registers in memory
1697 expression in store and operands of a ZERO_EXTRACT. */
1698 (*fun) (&SET_SRC (body), data);
1700 if (GET_CODE (dest) == ZERO_EXTRACT)
1702 (*fun) (&XEXP (dest, 1), data);
1703 (*fun) (&XEXP (dest, 2), data);
1706 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART)
1707 dest = XEXP (dest, 0);
1709 if (MEM_P (dest))
1710 (*fun) (&XEXP (dest, 0), data);
1712 return;
1714 default:
1715 /* All the other possibilities never store. */
1716 (*fun) (pbody, data);
1717 return;
1721 /* Return nonzero if X's old contents don't survive after INSN.
1722 This will be true if X is (cc0) or if X is a register and
1723 X dies in INSN or because INSN entirely sets X.
1725 "Entirely set" means set directly and not through a SUBREG,
1726 ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1727 Likewise, REG_INC does not count.
1729 REG may be a hard or pseudo reg. Renumbering is not taken into account,
1730 but for this use that makes no difference, since regs don't overlap
1731 during their lifetimes. Therefore, this function may be used
1732 at any time after deaths have been computed (in flow.c).
1734 If REG is a hard reg that occupies multiple machine registers, this
1735 function will only return 1 if each of those registers will be replaced
1736 by INSN. */
1739 dead_or_set_p (rtx insn, rtx x)
1741 unsigned int regno, last_regno;
1742 unsigned int i;
1744 /* Can't use cc0_rtx below since this file is used by genattrtab.c. */
1745 if (GET_CODE (x) == CC0)
1746 return 1;
1748 if (!REG_P (x))
1749 abort ();
1751 regno = REGNO (x);
1752 last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1753 : regno + hard_regno_nregs[regno][GET_MODE (x)] - 1);
1755 for (i = regno; i <= last_regno; i++)
1756 if (! dead_or_set_regno_p (insn, i))
1757 return 0;
1759 return 1;
1762 /* Utility function for dead_or_set_p to check an individual register. Also
1763 called from flow.c. */
1766 dead_or_set_regno_p (rtx insn, unsigned int test_regno)
1768 unsigned int regno, endregno;
1769 rtx pattern;
1771 /* See if there is a death note for something that includes TEST_REGNO. */
1772 if (find_regno_note (insn, REG_DEAD, test_regno))
1773 return 1;
1775 if (CALL_P (insn)
1776 && find_regno_fusage (insn, CLOBBER, test_regno))
1777 return 1;
1779 pattern = PATTERN (insn);
1781 if (GET_CODE (pattern) == COND_EXEC)
1782 pattern = COND_EXEC_CODE (pattern);
1784 if (GET_CODE (pattern) == SET)
1786 rtx dest = SET_DEST (pattern);
1788 /* A value is totally replaced if it is the destination or the
1789 destination is a SUBREG of REGNO that does not change the number of
1790 words in it. */
1791 if (GET_CODE (dest) == SUBREG
1792 && (((GET_MODE_SIZE (GET_MODE (dest))
1793 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1794 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1795 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1796 dest = SUBREG_REG (dest);
1798 if (!REG_P (dest))
1799 return 0;
1801 regno = REGNO (dest);
1802 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1803 : regno + hard_regno_nregs[regno][GET_MODE (dest)]);
1805 return (test_regno >= regno && test_regno < endregno);
1807 else if (GET_CODE (pattern) == PARALLEL)
1809 int i;
1811 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
1813 rtx body = XVECEXP (pattern, 0, i);
1815 if (GET_CODE (body) == COND_EXEC)
1816 body = COND_EXEC_CODE (body);
1818 if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1820 rtx dest = SET_DEST (body);
1822 if (GET_CODE (dest) == SUBREG
1823 && (((GET_MODE_SIZE (GET_MODE (dest))
1824 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1825 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1826 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1827 dest = SUBREG_REG (dest);
1829 if (!REG_P (dest))
1830 continue;
1832 regno = REGNO (dest);
1833 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1834 : regno + hard_regno_nregs[regno][GET_MODE (dest)]);
1836 if (test_regno >= regno && test_regno < endregno)
1837 return 1;
1842 return 0;
1845 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1846 If DATUM is nonzero, look for one whose datum is DATUM. */
1849 find_reg_note (rtx insn, enum reg_note kind, rtx datum)
1851 rtx link;
1853 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1854 if (! INSN_P (insn))
1855 return 0;
1856 if (datum == 0)
1858 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1859 if (REG_NOTE_KIND (link) == kind)
1860 return link;
1861 return 0;
1864 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1865 if (REG_NOTE_KIND (link) == kind && datum == XEXP (link, 0))
1866 return link;
1867 return 0;
1870 /* Return the reg-note of kind KIND in insn INSN which applies to register
1871 number REGNO, if any. Return 0 if there is no such reg-note. Note that
1872 the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1873 it might be the case that the note overlaps REGNO. */
1876 find_regno_note (rtx insn, enum reg_note kind, unsigned int regno)
1878 rtx link;
1880 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1881 if (! INSN_P (insn))
1882 return 0;
1884 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1885 if (REG_NOTE_KIND (link) == kind
1886 /* Verify that it is a register, so that scratch and MEM won't cause a
1887 problem here. */
1888 && REG_P (XEXP (link, 0))
1889 && REGNO (XEXP (link, 0)) <= regno
1890 && ((REGNO (XEXP (link, 0))
1891 + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1892 : hard_regno_nregs[REGNO (XEXP (link, 0))]
1893 [GET_MODE (XEXP (link, 0))]))
1894 > regno))
1895 return link;
1896 return 0;
1899 /* Return a REG_EQUIV or REG_EQUAL note if insn has only a single set and
1900 has such a note. */
1903 find_reg_equal_equiv_note (rtx insn)
1905 rtx link;
1907 if (!INSN_P (insn))
1908 return 0;
1909 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1910 if (REG_NOTE_KIND (link) == REG_EQUAL
1911 || REG_NOTE_KIND (link) == REG_EQUIV)
1913 if (single_set (insn) == 0)
1914 return 0;
1915 return link;
1917 return NULL;
1920 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1921 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1924 find_reg_fusage (rtx insn, enum rtx_code code, rtx datum)
1926 /* If it's not a CALL_INSN, it can't possibly have a
1927 CALL_INSN_FUNCTION_USAGE field, so don't bother checking. */
1928 if (!CALL_P (insn))
1929 return 0;
1931 if (! datum)
1932 abort ();
1934 if (!REG_P (datum))
1936 rtx link;
1938 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1939 link;
1940 link = XEXP (link, 1))
1941 if (GET_CODE (XEXP (link, 0)) == code
1942 && rtx_equal_p (datum, XEXP (XEXP (link, 0), 0)))
1943 return 1;
1945 else
1947 unsigned int regno = REGNO (datum);
1949 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1950 to pseudo registers, so don't bother checking. */
1952 if (regno < FIRST_PSEUDO_REGISTER)
1954 unsigned int end_regno
1955 = regno + hard_regno_nregs[regno][GET_MODE (datum)];
1956 unsigned int i;
1958 for (i = regno; i < end_regno; i++)
1959 if (find_regno_fusage (insn, code, i))
1960 return 1;
1964 return 0;
1967 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1968 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1971 find_regno_fusage (rtx insn, enum rtx_code code, unsigned int regno)
1973 rtx link;
1975 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1976 to pseudo registers, so don't bother checking. */
1978 if (regno >= FIRST_PSEUDO_REGISTER
1979 || !CALL_P (insn) )
1980 return 0;
1982 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1984 unsigned int regnote;
1985 rtx op, reg;
1987 if (GET_CODE (op = XEXP (link, 0)) == code
1988 && REG_P (reg = XEXP (op, 0))
1989 && (regnote = REGNO (reg)) <= regno
1990 && regnote + hard_regno_nregs[regnote][GET_MODE (reg)] > regno)
1991 return 1;
1994 return 0;
1997 /* Return true if INSN is a call to a pure function. */
2000 pure_call_p (rtx insn)
2002 rtx link;
2004 if (!CALL_P (insn) || ! CONST_OR_PURE_CALL_P (insn))
2005 return 0;
2007 /* Look for the note that differentiates const and pure functions. */
2008 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2010 rtx u, m;
2012 if (GET_CODE (u = XEXP (link, 0)) == USE
2013 && MEM_P (m = XEXP (u, 0)) && GET_MODE (m) == BLKmode
2014 && GET_CODE (XEXP (m, 0)) == SCRATCH)
2015 return 1;
2018 return 0;
2021 /* Remove register note NOTE from the REG_NOTES of INSN. */
2023 void
2024 remove_note (rtx insn, rtx note)
2026 rtx link;
2028 if (note == NULL_RTX)
2029 return;
2031 if (REG_NOTES (insn) == note)
2033 REG_NOTES (insn) = XEXP (note, 1);
2034 return;
2037 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2038 if (XEXP (link, 1) == note)
2040 XEXP (link, 1) = XEXP (note, 1);
2041 return;
2044 abort ();
2047 /* Search LISTP (an EXPR_LIST) for an entry whose first operand is NODE and
2048 return 1 if it is found. A simple equality test is used to determine if
2049 NODE matches. */
2052 in_expr_list_p (rtx listp, rtx node)
2054 rtx x;
2056 for (x = listp; x; x = XEXP (x, 1))
2057 if (node == XEXP (x, 0))
2058 return 1;
2060 return 0;
2063 /* Search LISTP (an EXPR_LIST) for an entry whose first operand is NODE and
2064 remove that entry from the list if it is found.
2066 A simple equality test is used to determine if NODE matches. */
2068 void
2069 remove_node_from_expr_list (rtx node, rtx *listp)
2071 rtx temp = *listp;
2072 rtx prev = NULL_RTX;
2074 while (temp)
2076 if (node == XEXP (temp, 0))
2078 /* Splice the node out of the list. */
2079 if (prev)
2080 XEXP (prev, 1) = XEXP (temp, 1);
2081 else
2082 *listp = XEXP (temp, 1);
2084 return;
2087 prev = temp;
2088 temp = XEXP (temp, 1);
2092 /* Nonzero if X contains any volatile instructions. These are instructions
2093 which may cause unpredictable machine state instructions, and thus no
2094 instructions should be moved or combined across them. This includes
2095 only volatile asms and UNSPEC_VOLATILE instructions. */
2098 volatile_insn_p (rtx x)
2100 RTX_CODE code;
2102 code = GET_CODE (x);
2103 switch (code)
2105 case LABEL_REF:
2106 case SYMBOL_REF:
2107 case CONST_INT:
2108 case CONST:
2109 case CONST_DOUBLE:
2110 case CONST_VECTOR:
2111 case CC0:
2112 case PC:
2113 case REG:
2114 case SCRATCH:
2115 case CLOBBER:
2116 case ADDR_VEC:
2117 case ADDR_DIFF_VEC:
2118 case CALL:
2119 case MEM:
2120 return 0;
2122 case UNSPEC_VOLATILE:
2123 /* case TRAP_IF: This isn't clear yet. */
2124 return 1;
2126 case ASM_INPUT:
2127 case ASM_OPERANDS:
2128 if (MEM_VOLATILE_P (x))
2129 return 1;
2131 default:
2132 break;
2135 /* Recursively scan the operands of this expression. */
2138 const char *fmt = GET_RTX_FORMAT (code);
2139 int i;
2141 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2143 if (fmt[i] == 'e')
2145 if (volatile_insn_p (XEXP (x, i)))
2146 return 1;
2148 else if (fmt[i] == 'E')
2150 int j;
2151 for (j = 0; j < XVECLEN (x, i); j++)
2152 if (volatile_insn_p (XVECEXP (x, i, j)))
2153 return 1;
2157 return 0;
2160 /* Nonzero if X contains any volatile memory references
2161 UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions. */
2164 volatile_refs_p (rtx x)
2166 RTX_CODE code;
2168 code = GET_CODE (x);
2169 switch (code)
2171 case LABEL_REF:
2172 case SYMBOL_REF:
2173 case CONST_INT:
2174 case CONST:
2175 case CONST_DOUBLE:
2176 case CONST_VECTOR:
2177 case CC0:
2178 case PC:
2179 case REG:
2180 case SCRATCH:
2181 case CLOBBER:
2182 case ADDR_VEC:
2183 case ADDR_DIFF_VEC:
2184 return 0;
2186 case UNSPEC_VOLATILE:
2187 return 1;
2189 case MEM:
2190 case ASM_INPUT:
2191 case ASM_OPERANDS:
2192 if (MEM_VOLATILE_P (x))
2193 return 1;
2195 default:
2196 break;
2199 /* Recursively scan the operands of this expression. */
2202 const char *fmt = GET_RTX_FORMAT (code);
2203 int i;
2205 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2207 if (fmt[i] == 'e')
2209 if (volatile_refs_p (XEXP (x, i)))
2210 return 1;
2212 else if (fmt[i] == 'E')
2214 int j;
2215 for (j = 0; j < XVECLEN (x, i); j++)
2216 if (volatile_refs_p (XVECEXP (x, i, j)))
2217 return 1;
2221 return 0;
2224 /* Similar to above, except that it also rejects register pre- and post-
2225 incrementing. */
2228 side_effects_p (rtx x)
2230 RTX_CODE code;
2232 code = GET_CODE (x);
2233 switch (code)
2235 case LABEL_REF:
2236 case SYMBOL_REF:
2237 case CONST_INT:
2238 case CONST:
2239 case CONST_DOUBLE:
2240 case CONST_VECTOR:
2241 case CC0:
2242 case PC:
2243 case REG:
2244 case SCRATCH:
2245 case ADDR_VEC:
2246 case ADDR_DIFF_VEC:
2247 return 0;
2249 case CLOBBER:
2250 /* Reject CLOBBER with a non-VOID mode. These are made by combine.c
2251 when some combination can't be done. If we see one, don't think
2252 that we can simplify the expression. */
2253 return (GET_MODE (x) != VOIDmode);
2255 case PRE_INC:
2256 case PRE_DEC:
2257 case POST_INC:
2258 case POST_DEC:
2259 case PRE_MODIFY:
2260 case POST_MODIFY:
2261 case CALL:
2262 case UNSPEC_VOLATILE:
2263 /* case TRAP_IF: This isn't clear yet. */
2264 return 1;
2266 case MEM:
2267 case ASM_INPUT:
2268 case ASM_OPERANDS:
2269 if (MEM_VOLATILE_P (x))
2270 return 1;
2272 default:
2273 break;
2276 /* Recursively scan the operands of this expression. */
2279 const char *fmt = GET_RTX_FORMAT (code);
2280 int i;
2282 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2284 if (fmt[i] == 'e')
2286 if (side_effects_p (XEXP (x, i)))
2287 return 1;
2289 else if (fmt[i] == 'E')
2291 int j;
2292 for (j = 0; j < XVECLEN (x, i); j++)
2293 if (side_effects_p (XVECEXP (x, i, j)))
2294 return 1;
2298 return 0;
2301 /* Return nonzero if evaluating rtx X might cause a trap. */
2304 may_trap_p (rtx x)
2306 int i;
2307 enum rtx_code code;
2308 const char *fmt;
2310 if (x == 0)
2311 return 0;
2312 code = GET_CODE (x);
2313 switch (code)
2315 /* Handle these cases quickly. */
2316 case CONST_INT:
2317 case CONST_DOUBLE:
2318 case CONST_VECTOR:
2319 case SYMBOL_REF:
2320 case LABEL_REF:
2321 case CONST:
2322 case PC:
2323 case CC0:
2324 case REG:
2325 case SCRATCH:
2326 return 0;
2328 case ASM_INPUT:
2329 case UNSPEC_VOLATILE:
2330 case TRAP_IF:
2331 return 1;
2333 case ASM_OPERANDS:
2334 return MEM_VOLATILE_P (x);
2336 /* Memory ref can trap unless it's a static var or a stack slot. */
2337 case MEM:
2338 if (MEM_NOTRAP_P (x))
2339 return 0;
2340 return rtx_addr_can_trap_p (XEXP (x, 0));
2342 /* Division by a non-constant might trap. */
2343 case DIV:
2344 case MOD:
2345 case UDIV:
2346 case UMOD:
2347 if (HONOR_SNANS (GET_MODE (x)))
2348 return 1;
2349 if (! CONSTANT_P (XEXP (x, 1))
2350 || (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
2351 && flag_trapping_math))
2352 return 1;
2353 if (XEXP (x, 1) == const0_rtx)
2354 return 1;
2355 break;
2357 case EXPR_LIST:
2358 /* An EXPR_LIST is used to represent a function call. This
2359 certainly may trap. */
2360 return 1;
2362 case GE:
2363 case GT:
2364 case LE:
2365 case LT:
2366 case LTGT:
2367 case COMPARE:
2368 /* Some floating point comparisons may trap. */
2369 if (!flag_trapping_math)
2370 break;
2371 /* ??? There is no machine independent way to check for tests that trap
2372 when COMPARE is used, though many targets do make this distinction.
2373 For instance, sparc uses CCFPE for compares which generate exceptions
2374 and CCFP for compares which do not generate exceptions. */
2375 if (HONOR_NANS (GET_MODE (x)))
2376 return 1;
2377 /* But often the compare has some CC mode, so check operand
2378 modes as well. */
2379 if (HONOR_NANS (GET_MODE (XEXP (x, 0)))
2380 || HONOR_NANS (GET_MODE (XEXP (x, 1))))
2381 return 1;
2382 break;
2384 case EQ:
2385 case NE:
2386 if (HONOR_SNANS (GET_MODE (x)))
2387 return 1;
2388 /* Often comparison is CC mode, so check operand modes. */
2389 if (HONOR_SNANS (GET_MODE (XEXP (x, 0)))
2390 || HONOR_SNANS (GET_MODE (XEXP (x, 1))))
2391 return 1;
2392 break;
2394 case FIX:
2395 /* Conversion of floating point might trap. */
2396 if (flag_trapping_math && HONOR_NANS (GET_MODE (XEXP (x, 0))))
2397 return 1;
2398 break;
2400 case NEG:
2401 case ABS:
2402 /* These operations don't trap even with floating point. */
2403 break;
2405 default:
2406 /* Any floating arithmetic may trap. */
2407 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
2408 && flag_trapping_math)
2409 return 1;
2412 fmt = GET_RTX_FORMAT (code);
2413 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2415 if (fmt[i] == 'e')
2417 if (may_trap_p (XEXP (x, i)))
2418 return 1;
2420 else if (fmt[i] == 'E')
2422 int j;
2423 for (j = 0; j < XVECLEN (x, i); j++)
2424 if (may_trap_p (XVECEXP (x, i, j)))
2425 return 1;
2428 return 0;
2431 /* Return nonzero if X contains a comparison that is not either EQ or NE,
2432 i.e., an inequality. */
2435 inequality_comparisons_p (rtx x)
2437 const char *fmt;
2438 int len, i;
2439 enum rtx_code code = GET_CODE (x);
2441 switch (code)
2443 case REG:
2444 case SCRATCH:
2445 case PC:
2446 case CC0:
2447 case CONST_INT:
2448 case CONST_DOUBLE:
2449 case CONST_VECTOR:
2450 case CONST:
2451 case LABEL_REF:
2452 case SYMBOL_REF:
2453 return 0;
2455 case LT:
2456 case LTU:
2457 case GT:
2458 case GTU:
2459 case LE:
2460 case LEU:
2461 case GE:
2462 case GEU:
2463 return 1;
2465 default:
2466 break;
2469 len = GET_RTX_LENGTH (code);
2470 fmt = GET_RTX_FORMAT (code);
2472 for (i = 0; i < len; i++)
2474 if (fmt[i] == 'e')
2476 if (inequality_comparisons_p (XEXP (x, i)))
2477 return 1;
2479 else if (fmt[i] == 'E')
2481 int j;
2482 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2483 if (inequality_comparisons_p (XVECEXP (x, i, j)))
2484 return 1;
2488 return 0;
2491 /* Replace any occurrence of FROM in X with TO. The function does
2492 not enter into CONST_DOUBLE for the replace.
2494 Note that copying is not done so X must not be shared unless all copies
2495 are to be modified. */
2498 replace_rtx (rtx x, rtx from, rtx to)
2500 int i, j;
2501 const char *fmt;
2503 /* The following prevents loops occurrence when we change MEM in
2504 CONST_DOUBLE onto the same CONST_DOUBLE. */
2505 if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
2506 return x;
2508 if (x == from)
2509 return to;
2511 /* Allow this function to make replacements in EXPR_LISTs. */
2512 if (x == 0)
2513 return 0;
2515 if (GET_CODE (x) == SUBREG)
2517 rtx new = replace_rtx (SUBREG_REG (x), from, to);
2519 if (GET_CODE (new) == CONST_INT)
2521 x = simplify_subreg (GET_MODE (x), new,
2522 GET_MODE (SUBREG_REG (x)),
2523 SUBREG_BYTE (x));
2524 if (! x)
2525 abort ();
2527 else
2528 SUBREG_REG (x) = new;
2530 return x;
2532 else if (GET_CODE (x) == ZERO_EXTEND)
2534 rtx new = replace_rtx (XEXP (x, 0), from, to);
2536 if (GET_CODE (new) == CONST_INT)
2538 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
2539 new, GET_MODE (XEXP (x, 0)));
2540 if (! x)
2541 abort ();
2543 else
2544 XEXP (x, 0) = new;
2546 return x;
2549 fmt = GET_RTX_FORMAT (GET_CODE (x));
2550 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2552 if (fmt[i] == 'e')
2553 XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
2554 else if (fmt[i] == 'E')
2555 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2556 XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
2559 return x;
2562 /* Throughout the rtx X, replace many registers according to REG_MAP.
2563 Return the replacement for X (which may be X with altered contents).
2564 REG_MAP[R] is the replacement for register R, or 0 for don't replace.
2565 NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
2567 We only support REG_MAP entries of REG or SUBREG. Also, hard registers
2568 should not be mapped to pseudos or vice versa since validate_change
2569 is not called.
2571 If REPLACE_DEST is 1, replacements are also done in destinations;
2572 otherwise, only sources are replaced. */
2575 replace_regs (rtx x, rtx *reg_map, unsigned int nregs, int replace_dest)
2577 enum rtx_code code;
2578 int i;
2579 const char *fmt;
2581 if (x == 0)
2582 return x;
2584 code = GET_CODE (x);
2585 switch (code)
2587 case SCRATCH:
2588 case PC:
2589 case CC0:
2590 case CONST_INT:
2591 case CONST_DOUBLE:
2592 case CONST_VECTOR:
2593 case CONST:
2594 case SYMBOL_REF:
2595 case LABEL_REF:
2596 return x;
2598 case REG:
2599 /* Verify that the register has an entry before trying to access it. */
2600 if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
2602 /* SUBREGs can't be shared. Always return a copy to ensure that if
2603 this replacement occurs more than once then each instance will
2604 get distinct rtx. */
2605 if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
2606 return copy_rtx (reg_map[REGNO (x)]);
2607 return reg_map[REGNO (x)];
2609 return x;
2611 case SUBREG:
2612 /* Prevent making nested SUBREGs. */
2613 if (REG_P (SUBREG_REG (x)) && REGNO (SUBREG_REG (x)) < nregs
2614 && reg_map[REGNO (SUBREG_REG (x))] != 0
2615 && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
2617 rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
2618 return simplify_gen_subreg (GET_MODE (x), map_val,
2619 GET_MODE (SUBREG_REG (x)),
2620 SUBREG_BYTE (x));
2622 break;
2624 case SET:
2625 if (replace_dest)
2626 SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
2628 else if (MEM_P (SET_DEST (x))
2629 || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2630 /* Even if we are not to replace destinations, replace register if it
2631 is CONTAINED in destination (destination is memory or
2632 STRICT_LOW_PART). */
2633 XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
2634 reg_map, nregs, 0);
2635 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2636 /* Similarly, for ZERO_EXTRACT we replace all operands. */
2637 break;
2639 SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2640 return x;
2642 default:
2643 break;
2646 fmt = GET_RTX_FORMAT (code);
2647 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2649 if (fmt[i] == 'e')
2650 XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
2651 else if (fmt[i] == 'E')
2653 int j;
2654 for (j = 0; j < XVECLEN (x, i); j++)
2655 XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2656 nregs, replace_dest);
2659 return x;
2662 /* Replace occurrences of the old label in *X with the new one.
2663 DATA is a REPLACE_LABEL_DATA containing the old and new labels. */
2666 replace_label (rtx *x, void *data)
2668 rtx l = *x;
2669 rtx old_label = ((replace_label_data *) data)->r1;
2670 rtx new_label = ((replace_label_data *) data)->r2;
2671 bool update_label_nuses = ((replace_label_data *) data)->update_label_nuses;
2673 if (l == NULL_RTX)
2674 return 0;
2676 if (GET_CODE (l) == SYMBOL_REF
2677 && CONSTANT_POOL_ADDRESS_P (l))
2679 rtx c = get_pool_constant (l);
2680 if (rtx_referenced_p (old_label, c))
2682 rtx new_c, new_l;
2683 replace_label_data *d = (replace_label_data *) data;
2685 /* Create a copy of constant C; replace the label inside
2686 but do not update LABEL_NUSES because uses in constant pool
2687 are not counted. */
2688 new_c = copy_rtx (c);
2689 d->update_label_nuses = false;
2690 for_each_rtx (&new_c, replace_label, data);
2691 d->update_label_nuses = update_label_nuses;
2693 /* Add the new constant NEW_C to constant pool and replace
2694 the old reference to constant by new reference. */
2695 new_l = XEXP (force_const_mem (get_pool_mode (l), new_c), 0);
2696 *x = replace_rtx (l, l, new_l);
2698 return 0;
2701 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
2702 field. This is not handled by for_each_rtx because it doesn't
2703 handle unprinted ('0') fields. */
2704 if (JUMP_P (l) && JUMP_LABEL (l) == old_label)
2705 JUMP_LABEL (l) = new_label;
2707 if ((GET_CODE (l) == LABEL_REF
2708 || GET_CODE (l) == INSN_LIST)
2709 && XEXP (l, 0) == old_label)
2711 XEXP (l, 0) = new_label;
2712 if (update_label_nuses)
2714 ++LABEL_NUSES (new_label);
2715 --LABEL_NUSES (old_label);
2717 return 0;
2720 return 0;
2723 /* When *BODY is equal to X or X is directly referenced by *BODY
2724 return nonzero, thus FOR_EACH_RTX stops traversing and returns nonzero
2725 too, otherwise FOR_EACH_RTX continues traversing *BODY. */
2727 static int
2728 rtx_referenced_p_1 (rtx *body, void *x)
2730 rtx y = (rtx) x;
2732 if (*body == NULL_RTX)
2733 return y == NULL_RTX;
2735 /* Return true if a label_ref *BODY refers to label Y. */
2736 if (GET_CODE (*body) == LABEL_REF && LABEL_P (y))
2737 return XEXP (*body, 0) == y;
2739 /* If *BODY is a reference to pool constant traverse the constant. */
2740 if (GET_CODE (*body) == SYMBOL_REF
2741 && CONSTANT_POOL_ADDRESS_P (*body))
2742 return rtx_referenced_p (y, get_pool_constant (*body));
2744 /* By default, compare the RTL expressions. */
2745 return rtx_equal_p (*body, y);
2748 /* Return true if X is referenced in BODY. */
2751 rtx_referenced_p (rtx x, rtx body)
2753 return for_each_rtx (&body, rtx_referenced_p_1, x);
2756 /* If INSN is a tablejump return true and store the label (before jump table) to
2757 *LABELP and the jump table to *TABLEP. LABELP and TABLEP may be NULL. */
2759 bool
2760 tablejump_p (rtx insn, rtx *labelp, rtx *tablep)
2762 rtx label, table;
2764 if (JUMP_P (insn)
2765 && (label = JUMP_LABEL (insn)) != NULL_RTX
2766 && (table = next_active_insn (label)) != NULL_RTX
2767 && JUMP_P (table)
2768 && (GET_CODE (PATTERN (table)) == ADDR_VEC
2769 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
2771 if (labelp)
2772 *labelp = label;
2773 if (tablep)
2774 *tablep = table;
2775 return true;
2777 return false;
2780 /* A subroutine of computed_jump_p, return 1 if X contains a REG or MEM or
2781 constant that is not in the constant pool and not in the condition
2782 of an IF_THEN_ELSE. */
2784 static int
2785 computed_jump_p_1 (rtx x)
2787 enum rtx_code code = GET_CODE (x);
2788 int i, j;
2789 const char *fmt;
2791 switch (code)
2793 case LABEL_REF:
2794 case PC:
2795 return 0;
2797 case CONST:
2798 case CONST_INT:
2799 case CONST_DOUBLE:
2800 case CONST_VECTOR:
2801 case SYMBOL_REF:
2802 case REG:
2803 return 1;
2805 case MEM:
2806 return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2807 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2809 case IF_THEN_ELSE:
2810 return (computed_jump_p_1 (XEXP (x, 1))
2811 || computed_jump_p_1 (XEXP (x, 2)));
2813 default:
2814 break;
2817 fmt = GET_RTX_FORMAT (code);
2818 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2820 if (fmt[i] == 'e'
2821 && computed_jump_p_1 (XEXP (x, i)))
2822 return 1;
2824 else if (fmt[i] == 'E')
2825 for (j = 0; j < XVECLEN (x, i); j++)
2826 if (computed_jump_p_1 (XVECEXP (x, i, j)))
2827 return 1;
2830 return 0;
2833 /* Return nonzero if INSN is an indirect jump (aka computed jump).
2835 Tablejumps and casesi insns are not considered indirect jumps;
2836 we can recognize them by a (use (label_ref)). */
2839 computed_jump_p (rtx insn)
2841 int i;
2842 if (JUMP_P (insn))
2844 rtx pat = PATTERN (insn);
2846 if (find_reg_note (insn, REG_LABEL, NULL_RTX))
2847 return 0;
2848 else if (GET_CODE (pat) == PARALLEL)
2850 int len = XVECLEN (pat, 0);
2851 int has_use_labelref = 0;
2853 for (i = len - 1; i >= 0; i--)
2854 if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2855 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2856 == LABEL_REF))
2857 has_use_labelref = 1;
2859 if (! has_use_labelref)
2860 for (i = len - 1; i >= 0; i--)
2861 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2862 && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2863 && computed_jump_p_1 (SET_SRC (XVECEXP (pat, 0, i))))
2864 return 1;
2866 else if (GET_CODE (pat) == SET
2867 && SET_DEST (pat) == pc_rtx
2868 && computed_jump_p_1 (SET_SRC (pat)))
2869 return 1;
2871 return 0;
2874 /* Traverse X via depth-first search, calling F for each
2875 sub-expression (including X itself). F is also passed the DATA.
2876 If F returns -1, do not traverse sub-expressions, but continue
2877 traversing the rest of the tree. If F ever returns any other
2878 nonzero value, stop the traversal, and return the value returned
2879 by F. Otherwise, return 0. This function does not traverse inside
2880 tree structure that contains RTX_EXPRs, or into sub-expressions
2881 whose format code is `0' since it is not known whether or not those
2882 codes are actually RTL.
2884 This routine is very general, and could (should?) be used to
2885 implement many of the other routines in this file. */
2888 for_each_rtx (rtx *x, rtx_function f, void *data)
2890 int result;
2891 int length;
2892 const char *format;
2893 int i;
2895 /* Call F on X. */
2896 result = (*f) (x, data);
2897 if (result == -1)
2898 /* Do not traverse sub-expressions. */
2899 return 0;
2900 else if (result != 0)
2901 /* Stop the traversal. */
2902 return result;
2904 if (*x == NULL_RTX)
2905 /* There are no sub-expressions. */
2906 return 0;
2908 length = GET_RTX_LENGTH (GET_CODE (*x));
2909 format = GET_RTX_FORMAT (GET_CODE (*x));
2911 for (i = 0; i < length; ++i)
2913 switch (format[i])
2915 case 'e':
2916 result = for_each_rtx (&XEXP (*x, i), f, data);
2917 if (result != 0)
2918 return result;
2919 break;
2921 case 'V':
2922 case 'E':
2923 if (XVEC (*x, i) != 0)
2925 int j;
2926 for (j = 0; j < XVECLEN (*x, i); ++j)
2928 result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2929 if (result != 0)
2930 return result;
2933 break;
2935 default:
2936 /* Nothing to do. */
2937 break;
2942 return 0;
2945 /* Searches X for any reference to REGNO, returning the rtx of the
2946 reference found if any. Otherwise, returns NULL_RTX. */
2949 regno_use_in (unsigned int regno, rtx x)
2951 const char *fmt;
2952 int i, j;
2953 rtx tem;
2955 if (REG_P (x) && REGNO (x) == regno)
2956 return x;
2958 fmt = GET_RTX_FORMAT (GET_CODE (x));
2959 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2961 if (fmt[i] == 'e')
2963 if ((tem = regno_use_in (regno, XEXP (x, i))))
2964 return tem;
2966 else if (fmt[i] == 'E')
2967 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2968 if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2969 return tem;
2972 return NULL_RTX;
2975 /* Return a value indicating whether OP, an operand of a commutative
2976 operation, is preferred as the first or second operand. The higher
2977 the value, the stronger the preference for being the first operand.
2978 We use negative values to indicate a preference for the first operand
2979 and positive values for the second operand. */
2982 commutative_operand_precedence (rtx op)
2984 enum rtx_code code = GET_CODE (op);
2986 /* Constants always come the second operand. Prefer "nice" constants. */
2987 if (code == CONST_INT)
2988 return -7;
2989 if (code == CONST_DOUBLE)
2990 return -6;
2991 op = avoid_constant_pool_reference (op);
2993 switch (GET_RTX_CLASS (code))
2995 case RTX_CONST_OBJ:
2996 if (code == CONST_INT)
2997 return -5;
2998 if (code == CONST_DOUBLE)
2999 return -4;
3000 return -3;
3002 case RTX_EXTRA:
3003 /* SUBREGs of objects should come second. */
3004 if (code == SUBREG && OBJECT_P (SUBREG_REG (op)))
3005 return -2;
3007 if (!CONSTANT_P (op))
3008 return 0;
3009 else
3010 /* As for RTX_CONST_OBJ. */
3011 return -3;
3013 case RTX_OBJ:
3014 /* Complex expressions should be the first, so decrease priority
3015 of objects. */
3016 return -1;
3018 case RTX_COMM_ARITH:
3019 /* Prefer operands that are themselves commutative to be first.
3020 This helps to make things linear. In particular,
3021 (and (and (reg) (reg)) (not (reg))) is canonical. */
3022 return 4;
3024 case RTX_BIN_ARITH:
3025 /* If only one operand is a binary expression, it will be the first
3026 operand. In particular, (plus (minus (reg) (reg)) (neg (reg)))
3027 is canonical, although it will usually be further simplified. */
3028 return 2;
3030 case RTX_UNARY:
3031 /* Then prefer NEG and NOT. */
3032 if (code == NEG || code == NOT)
3033 return 1;
3035 default:
3036 return 0;
3040 /* Return 1 iff it is necessary to swap operands of commutative operation
3041 in order to canonicalize expression. */
3044 swap_commutative_operands_p (rtx x, rtx y)
3046 return (commutative_operand_precedence (x)
3047 < commutative_operand_precedence (y));
3050 /* Return 1 if X is an autoincrement side effect and the register is
3051 not the stack pointer. */
3053 auto_inc_p (rtx x)
3055 switch (GET_CODE (x))
3057 case PRE_INC:
3058 case POST_INC:
3059 case PRE_DEC:
3060 case POST_DEC:
3061 case PRE_MODIFY:
3062 case POST_MODIFY:
3063 /* There are no REG_INC notes for SP. */
3064 if (XEXP (x, 0) != stack_pointer_rtx)
3065 return 1;
3066 default:
3067 break;
3069 return 0;
3072 /* Return 1 if the sequence of instructions beginning with FROM and up
3073 to and including TO is safe to move. If NEW_TO is non-NULL, and
3074 the sequence is not already safe to move, but can be easily
3075 extended to a sequence which is safe, then NEW_TO will point to the
3076 end of the extended sequence.
3078 For now, this function only checks that the region contains whole
3079 exception regions, but it could be extended to check additional
3080 conditions as well. */
3083 insns_safe_to_move_p (rtx from, rtx to, rtx *new_to)
3085 int eh_region_count = 0;
3086 int past_to_p = 0;
3087 rtx r = from;
3089 /* By default, assume the end of the region will be what was
3090 suggested. */
3091 if (new_to)
3092 *new_to = to;
3094 while (r)
3096 if (NOTE_P (r))
3098 switch (NOTE_LINE_NUMBER (r))
3100 case NOTE_INSN_EH_REGION_BEG:
3101 ++eh_region_count;
3102 break;
3104 case NOTE_INSN_EH_REGION_END:
3105 if (eh_region_count == 0)
3106 /* This sequence of instructions contains the end of
3107 an exception region, but not he beginning. Moving
3108 it will cause chaos. */
3109 return 0;
3111 --eh_region_count;
3112 break;
3114 default:
3115 break;
3118 else if (past_to_p)
3119 /* If we've passed TO, and we see a non-note instruction, we
3120 can't extend the sequence to a movable sequence. */
3121 return 0;
3123 if (r == to)
3125 if (!new_to)
3126 /* It's OK to move the sequence if there were matched sets of
3127 exception region notes. */
3128 return eh_region_count == 0;
3130 past_to_p = 1;
3133 /* It's OK to move the sequence if there were matched sets of
3134 exception region notes. */
3135 if (past_to_p && eh_region_count == 0)
3137 *new_to = r;
3138 return 1;
3141 /* Go to the next instruction. */
3142 r = NEXT_INSN (r);
3145 return 0;
3148 /* Return nonzero if IN contains a piece of rtl that has the address LOC. */
3150 loc_mentioned_in_p (rtx *loc, rtx in)
3152 enum rtx_code code = GET_CODE (in);
3153 const char *fmt = GET_RTX_FORMAT (code);
3154 int i, j;
3156 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3158 if (loc == &in->u.fld[i].rt_rtx)
3159 return 1;
3160 if (fmt[i] == 'e')
3162 if (loc_mentioned_in_p (loc, XEXP (in, i)))
3163 return 1;
3165 else if (fmt[i] == 'E')
3166 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
3167 if (loc_mentioned_in_p (loc, XVECEXP (in, i, j)))
3168 return 1;
3170 return 0;
3173 /* Helper function for subreg_lsb. Given a subreg's OUTER_MODE, INNER_MODE,
3174 and SUBREG_BYTE, return the bit offset where the subreg begins
3175 (counting from the least significant bit of the operand). */
3177 unsigned int
3178 subreg_lsb_1 (enum machine_mode outer_mode,
3179 enum machine_mode inner_mode,
3180 unsigned int subreg_byte)
3182 unsigned int bitpos;
3183 unsigned int byte;
3184 unsigned int word;
3186 /* A paradoxical subreg begins at bit position 0. */
3187 if (GET_MODE_BITSIZE (outer_mode) > GET_MODE_BITSIZE (inner_mode))
3188 return 0;
3190 if (WORDS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
3191 /* If the subreg crosses a word boundary ensure that
3192 it also begins and ends on a word boundary. */
3193 if ((subreg_byte % UNITS_PER_WORD
3194 + GET_MODE_SIZE (outer_mode)) > UNITS_PER_WORD
3195 && (subreg_byte % UNITS_PER_WORD
3196 || GET_MODE_SIZE (outer_mode) % UNITS_PER_WORD))
3197 abort ();
3199 if (WORDS_BIG_ENDIAN)
3200 word = (GET_MODE_SIZE (inner_mode)
3201 - (subreg_byte + GET_MODE_SIZE (outer_mode))) / UNITS_PER_WORD;
3202 else
3203 word = subreg_byte / UNITS_PER_WORD;
3204 bitpos = word * BITS_PER_WORD;
3206 if (BYTES_BIG_ENDIAN)
3207 byte = (GET_MODE_SIZE (inner_mode)
3208 - (subreg_byte + GET_MODE_SIZE (outer_mode))) % UNITS_PER_WORD;
3209 else
3210 byte = subreg_byte % UNITS_PER_WORD;
3211 bitpos += byte * BITS_PER_UNIT;
3213 return bitpos;
3216 /* Given a subreg X, return the bit offset where the subreg begins
3217 (counting from the least significant bit of the reg). */
3219 unsigned int
3220 subreg_lsb (rtx x)
3222 return subreg_lsb_1 (GET_MODE (x), GET_MODE (SUBREG_REG (x)),
3223 SUBREG_BYTE (x));
3226 /* This function returns the regno offset of a subreg expression.
3227 xregno - A regno of an inner hard subreg_reg (or what will become one).
3228 xmode - The mode of xregno.
3229 offset - The byte offset.
3230 ymode - The mode of a top level SUBREG (or what may become one).
3231 RETURN - The regno offset which would be used. */
3232 unsigned int
3233 subreg_regno_offset (unsigned int xregno, enum machine_mode xmode,
3234 unsigned int offset, enum machine_mode ymode)
3236 int nregs_xmode, nregs_ymode;
3237 int mode_multiple, nregs_multiple;
3238 int y_offset;
3240 if (xregno >= FIRST_PSEUDO_REGISTER)
3241 abort ();
3243 nregs_xmode = hard_regno_nregs[xregno][xmode];
3244 nregs_ymode = hard_regno_nregs[xregno][ymode];
3246 /* If this is a big endian paradoxical subreg, which uses more actual
3247 hard registers than the original register, we must return a negative
3248 offset so that we find the proper highpart of the register. */
3249 if (offset == 0
3250 && nregs_ymode > nregs_xmode
3251 && (GET_MODE_SIZE (ymode) > UNITS_PER_WORD
3252 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
3253 return nregs_xmode - nregs_ymode;
3255 if (offset == 0 || nregs_xmode == nregs_ymode)
3256 return 0;
3258 /* size of ymode must not be greater than the size of xmode. */
3259 mode_multiple = GET_MODE_SIZE (xmode) / GET_MODE_SIZE (ymode);
3260 if (mode_multiple == 0)
3261 abort ();
3263 y_offset = offset / GET_MODE_SIZE (ymode);
3264 nregs_multiple = nregs_xmode / nregs_ymode;
3265 return (y_offset / (mode_multiple / nregs_multiple)) * nregs_ymode;
3268 /* This function returns true when the offset is representable via
3269 subreg_offset in the given regno.
3270 xregno - A regno of an inner hard subreg_reg (or what will become one).
3271 xmode - The mode of xregno.
3272 offset - The byte offset.
3273 ymode - The mode of a top level SUBREG (or what may become one).
3274 RETURN - The regno offset which would be used. */
3275 bool
3276 subreg_offset_representable_p (unsigned int xregno, enum machine_mode xmode,
3277 unsigned int offset, enum machine_mode ymode)
3279 int nregs_xmode, nregs_ymode;
3280 int mode_multiple, nregs_multiple;
3281 int y_offset;
3283 if (xregno >= FIRST_PSEUDO_REGISTER)
3284 abort ();
3286 nregs_xmode = hard_regno_nregs[xregno][xmode];
3287 nregs_ymode = hard_regno_nregs[xregno][ymode];
3289 /* Paradoxical subregs are always valid. */
3290 if (offset == 0
3291 && nregs_ymode > nregs_xmode
3292 && (GET_MODE_SIZE (ymode) > UNITS_PER_WORD
3293 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
3294 return true;
3296 /* Lowpart subregs are always valid. */
3297 if (offset == subreg_lowpart_offset (ymode, xmode))
3298 return true;
3300 #ifdef ENABLE_CHECKING
3301 /* This should always pass, otherwise we don't know how to verify the
3302 constraint. These conditions may be relaxed but subreg_offset would
3303 need to be redesigned. */
3304 if (GET_MODE_SIZE (xmode) % GET_MODE_SIZE (ymode)
3305 || GET_MODE_SIZE (ymode) % nregs_ymode
3306 || nregs_xmode % nregs_ymode)
3307 abort ();
3308 #endif
3310 /* The XMODE value can be seen as a vector of NREGS_XMODE
3311 values. The subreg must represent a lowpart of given field.
3312 Compute what field it is. */
3313 offset -= subreg_lowpart_offset (ymode,
3314 mode_for_size (GET_MODE_BITSIZE (xmode)
3315 / nregs_xmode,
3316 MODE_INT, 0));
3318 /* size of ymode must not be greater than the size of xmode. */
3319 mode_multiple = GET_MODE_SIZE (xmode) / GET_MODE_SIZE (ymode);
3320 if (mode_multiple == 0)
3321 abort ();
3323 y_offset = offset / GET_MODE_SIZE (ymode);
3324 nregs_multiple = nregs_xmode / nregs_ymode;
3325 #ifdef ENABLE_CHECKING
3326 if (offset % GET_MODE_SIZE (ymode)
3327 || mode_multiple % nregs_multiple)
3328 abort ();
3329 #endif
3330 return (!(y_offset % (mode_multiple / nregs_multiple)));
3333 /* Return the final regno that a subreg expression refers to. */
3334 unsigned int
3335 subreg_regno (rtx x)
3337 unsigned int ret;
3338 rtx subreg = SUBREG_REG (x);
3339 int regno = REGNO (subreg);
3341 ret = regno + subreg_regno_offset (regno,
3342 GET_MODE (subreg),
3343 SUBREG_BYTE (x),
3344 GET_MODE (x));
3345 return ret;
3348 struct parms_set_data
3350 int nregs;
3351 HARD_REG_SET regs;
3354 /* Helper function for noticing stores to parameter registers. */
3355 static void
3356 parms_set (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
3358 struct parms_set_data *d = data;
3359 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
3360 && TEST_HARD_REG_BIT (d->regs, REGNO (x)))
3362 CLEAR_HARD_REG_BIT (d->regs, REGNO (x));
3363 d->nregs--;
3367 /* Look backward for first parameter to be loaded.
3368 Do not skip BOUNDARY. */
3370 find_first_parameter_load (rtx call_insn, rtx boundary)
3372 struct parms_set_data parm;
3373 rtx p, before;
3375 /* Since different machines initialize their parameter registers
3376 in different orders, assume nothing. Collect the set of all
3377 parameter registers. */
3378 CLEAR_HARD_REG_SET (parm.regs);
3379 parm.nregs = 0;
3380 for (p = CALL_INSN_FUNCTION_USAGE (call_insn); p; p = XEXP (p, 1))
3381 if (GET_CODE (XEXP (p, 0)) == USE
3382 && REG_P (XEXP (XEXP (p, 0), 0)))
3384 if (REGNO (XEXP (XEXP (p, 0), 0)) >= FIRST_PSEUDO_REGISTER)
3385 abort ();
3387 /* We only care about registers which can hold function
3388 arguments. */
3389 if (!FUNCTION_ARG_REGNO_P (REGNO (XEXP (XEXP (p, 0), 0))))
3390 continue;
3392 SET_HARD_REG_BIT (parm.regs, REGNO (XEXP (XEXP (p, 0), 0)));
3393 parm.nregs++;
3395 before = call_insn;
3397 /* Search backward for the first set of a register in this set. */
3398 while (parm.nregs && before != boundary)
3400 before = PREV_INSN (before);
3402 /* It is possible that some loads got CSEed from one call to
3403 another. Stop in that case. */
3404 if (CALL_P (before))
3405 break;
3407 /* Our caller needs either ensure that we will find all sets
3408 (in case code has not been optimized yet), or take care
3409 for possible labels in a way by setting boundary to preceding
3410 CODE_LABEL. */
3411 if (LABEL_P (before))
3413 if (before != boundary)
3414 abort ();
3415 break;
3418 if (INSN_P (before))
3419 note_stores (PATTERN (before), parms_set, &parm);
3421 return before;
3424 /* Return true if we should avoid inserting code between INSN and preceding
3425 call instruction. */
3427 bool
3428 keep_with_call_p (rtx insn)
3430 rtx set;
3432 if (INSN_P (insn) && (set = single_set (insn)) != NULL)
3434 if (REG_P (SET_DEST (set))
3435 && REGNO (SET_DEST (set)) < FIRST_PSEUDO_REGISTER
3436 && fixed_regs[REGNO (SET_DEST (set))]
3437 && general_operand (SET_SRC (set), VOIDmode))
3438 return true;
3439 if (REG_P (SET_SRC (set))
3440 && FUNCTION_VALUE_REGNO_P (REGNO (SET_SRC (set)))
3441 && REG_P (SET_DEST (set))
3442 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3443 return true;
3444 /* There may be a stack pop just after the call and before the store
3445 of the return register. Search for the actual store when deciding
3446 if we can break or not. */
3447 if (SET_DEST (set) == stack_pointer_rtx)
3449 rtx i2 = next_nonnote_insn (insn);
3450 if (i2 && keep_with_call_p (i2))
3451 return true;
3454 return false;
3457 /* Return true when store to register X can be hoisted to the place
3458 with LIVE registers (can be NULL). Value VAL contains destination
3459 whose value will be used. */
3461 static bool
3462 hoist_test_store (rtx x, rtx val, regset live)
3464 if (GET_CODE (x) == SCRATCH)
3465 return true;
3467 if (rtx_equal_p (x, val))
3468 return true;
3470 /* Allow subreg of X in case it is not writing just part of multireg pseudo.
3471 Then we would need to update all users to care hoisting the store too.
3472 Caller may represent that by specifying whole subreg as val. */
3474 if (GET_CODE (x) == SUBREG && rtx_equal_p (SUBREG_REG (x), val))
3476 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) > UNITS_PER_WORD
3477 && GET_MODE_BITSIZE (GET_MODE (x)) <
3478 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
3479 return false;
3480 return true;
3482 if (GET_CODE (x) == SUBREG)
3483 x = SUBREG_REG (x);
3485 /* Anything except register store is not hoistable. This includes the
3486 partial stores to registers. */
3488 if (!REG_P (x))
3489 return false;
3491 /* Pseudo registers can be always replaced by another pseudo to avoid
3492 the side effect, for hard register we must ensure that they are dead.
3493 Eventually we may want to add code to try turn pseudos to hards, but it
3494 is unlikely useful. */
3496 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
3498 int regno = REGNO (x);
3499 int n = hard_regno_nregs[regno][GET_MODE (x)];
3501 if (!live)
3502 return false;
3503 if (REGNO_REG_SET_P (live, regno))
3504 return false;
3505 while (--n > 0)
3506 if (REGNO_REG_SET_P (live, regno + n))
3507 return false;
3509 return true;
3513 /* Return true if INSN can be hoisted to place with LIVE hard registers
3514 (LIVE can be NULL when unknown). VAL is expected to be stored by the insn
3515 and used by the hoisting pass. */
3517 bool
3518 can_hoist_insn_p (rtx insn, rtx val, regset live)
3520 rtx pat = PATTERN (insn);
3521 int i;
3523 /* It probably does not worth the complexity to handle multiple
3524 set stores. */
3525 if (!single_set (insn))
3526 return false;
3527 /* We can move CALL_INSN, but we need to check that all caller clobbered
3528 regs are dead. */
3529 if (CALL_P (insn))
3530 return false;
3531 /* In future we will handle hoisting of libcall sequences, but
3532 give up for now. */
3533 if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
3534 return false;
3535 switch (GET_CODE (pat))
3537 case SET:
3538 if (!hoist_test_store (SET_DEST (pat), val, live))
3539 return false;
3540 break;
3541 case USE:
3542 /* USES do have sick semantics, so do not move them. */
3543 return false;
3544 break;
3545 case CLOBBER:
3546 if (!hoist_test_store (XEXP (pat, 0), val, live))
3547 return false;
3548 break;
3549 case PARALLEL:
3550 for (i = 0; i < XVECLEN (pat, 0); i++)
3552 rtx x = XVECEXP (pat, 0, i);
3553 switch (GET_CODE (x))
3555 case SET:
3556 if (!hoist_test_store (SET_DEST (x), val, live))
3557 return false;
3558 break;
3559 case USE:
3560 /* We need to fix callers to really ensure availability
3561 of all values insn uses, but for now it is safe to prohibit
3562 hoisting of any insn having such a hidden uses. */
3563 return false;
3564 break;
3565 case CLOBBER:
3566 if (!hoist_test_store (SET_DEST (x), val, live))
3567 return false;
3568 break;
3569 default:
3570 break;
3573 break;
3574 default:
3575 abort ();
3577 return true;
3580 /* Update store after hoisting - replace all stores to pseudo registers
3581 by new ones to avoid clobbering of values except for store to VAL that will
3582 be updated to NEW. */
3584 static void
3585 hoist_update_store (rtx insn, rtx *xp, rtx val, rtx new)
3587 rtx x = *xp;
3589 if (GET_CODE (x) == SCRATCH)
3590 return;
3592 if (GET_CODE (x) == SUBREG && SUBREG_REG (x) == val)
3593 validate_change (insn, xp,
3594 simplify_gen_subreg (GET_MODE (x), new, GET_MODE (new),
3595 SUBREG_BYTE (x)), 1);
3596 if (rtx_equal_p (x, val))
3598 validate_change (insn, xp, new, 1);
3599 return;
3601 if (GET_CODE (x) == SUBREG)
3603 xp = &SUBREG_REG (x);
3604 x = *xp;
3607 if (!REG_P (x))
3608 abort ();
3610 /* We've verified that hard registers are dead, so we may keep the side
3611 effect. Otherwise replace it by new pseudo. */
3612 if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
3613 validate_change (insn, xp, gen_reg_rtx (GET_MODE (x)), 1);
3614 REG_NOTES (insn)
3615 = alloc_EXPR_LIST (REG_UNUSED, *xp, REG_NOTES (insn));
3618 /* Create a copy of INSN after AFTER replacing store of VAL to NEW
3619 and each other side effect to pseudo register by new pseudo register. */
3622 hoist_insn_after (rtx insn, rtx after, rtx val, rtx new)
3624 rtx pat;
3625 int i;
3626 rtx note;
3628 insn = emit_copy_of_insn_after (insn, after);
3629 pat = PATTERN (insn);
3631 /* Remove REG_UNUSED notes as we will re-emit them. */
3632 while ((note = find_reg_note (insn, REG_UNUSED, NULL_RTX)))
3633 remove_note (insn, note);
3635 /* To get this working callers must ensure to move everything referenced
3636 by REG_EQUAL/REG_EQUIV notes too. Lets remove them, it is probably
3637 easier. */
3638 while ((note = find_reg_note (insn, REG_EQUAL, NULL_RTX)))
3639 remove_note (insn, note);
3640 while ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)))
3641 remove_note (insn, note);
3643 /* Remove REG_DEAD notes as they might not be valid anymore in case
3644 we create redundancy. */
3645 while ((note = find_reg_note (insn, REG_DEAD, NULL_RTX)))
3646 remove_note (insn, note);
3647 switch (GET_CODE (pat))
3649 case SET:
3650 hoist_update_store (insn, &SET_DEST (pat), val, new);
3651 break;
3652 case USE:
3653 break;
3654 case CLOBBER:
3655 hoist_update_store (insn, &XEXP (pat, 0), val, new);
3656 break;
3657 case PARALLEL:
3658 for (i = 0; i < XVECLEN (pat, 0); i++)
3660 rtx x = XVECEXP (pat, 0, i);
3661 switch (GET_CODE (x))
3663 case SET:
3664 hoist_update_store (insn, &SET_DEST (x), val, new);
3665 break;
3666 case USE:
3667 break;
3668 case CLOBBER:
3669 hoist_update_store (insn, &SET_DEST (x), val, new);
3670 break;
3671 default:
3672 break;
3675 break;
3676 default:
3677 abort ();
3679 if (!apply_change_group ())
3680 abort ();
3682 return insn;
3686 hoist_insn_to_edge (rtx insn, edge e, rtx val, rtx new)
3688 rtx new_insn;
3690 /* We cannot insert instructions on an abnormal critical edge.
3691 It will be easier to find the culprit if we die now. */
3692 if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
3693 abort ();
3695 /* Do not use emit_insn_on_edge as we want to preserve notes and similar
3696 stuff. We also emit CALL_INSNS and firends. */
3697 if (e->insns.r == NULL_RTX)
3699 start_sequence ();
3700 emit_note (NOTE_INSN_DELETED);
3702 else
3703 push_to_sequence (e->insns.r);
3705 new_insn = hoist_insn_after (insn, get_last_insn (), val, new);
3707 e->insns.r = get_insns ();
3708 end_sequence ();
3709 return new_insn;
3712 /* Return true if LABEL is a target of JUMP_INSN. This applies only
3713 to non-complex jumps. That is, direct unconditional, conditional,
3714 and tablejumps, but not computed jumps or returns. It also does
3715 not apply to the fallthru case of a conditional jump. */
3717 bool
3718 label_is_jump_target_p (rtx label, rtx jump_insn)
3720 rtx tmp = JUMP_LABEL (jump_insn);
3722 if (label == tmp)
3723 return true;
3725 if (tablejump_p (jump_insn, NULL, &tmp))
3727 rtvec vec = XVEC (PATTERN (tmp),
3728 GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC);
3729 int i, veclen = GET_NUM_ELEM (vec);
3731 for (i = 0; i < veclen; ++i)
3732 if (XEXP (RTVEC_ELT (vec, i), 0) == label)
3733 return true;
3736 return false;
3740 /* Return an estimate of the cost of computing rtx X.
3741 One use is in cse, to decide which expression to keep in the hash table.
3742 Another is in rtl generation, to pick the cheapest way to multiply.
3743 Other uses like the latter are expected in the future. */
3746 rtx_cost (rtx x, enum rtx_code outer_code ATTRIBUTE_UNUSED)
3748 int i, j;
3749 enum rtx_code code;
3750 const char *fmt;
3751 int total;
3753 if (x == 0)
3754 return 0;
3756 /* Compute the default costs of certain things.
3757 Note that targetm.rtx_costs can override the defaults. */
3759 code = GET_CODE (x);
3760 switch (code)
3762 case MULT:
3763 total = COSTS_N_INSNS (5);
3764 break;
3765 case DIV:
3766 case UDIV:
3767 case MOD:
3768 case UMOD:
3769 total = COSTS_N_INSNS (7);
3770 break;
3771 case USE:
3772 /* Used in loop.c and combine.c as a marker. */
3773 total = 0;
3774 break;
3775 default:
3776 total = COSTS_N_INSNS (1);
3779 switch (code)
3781 case REG:
3782 return 0;
3784 case SUBREG:
3785 /* If we can't tie these modes, make this expensive. The larger
3786 the mode, the more expensive it is. */
3787 if (! MODES_TIEABLE_P (GET_MODE (x), GET_MODE (SUBREG_REG (x))))
3788 return COSTS_N_INSNS (2
3789 + GET_MODE_SIZE (GET_MODE (x)) / UNITS_PER_WORD);
3790 break;
3792 default:
3793 if (targetm.rtx_costs (x, code, outer_code, &total))
3794 return total;
3795 break;
3798 /* Sum the costs of the sub-rtx's, plus cost of this operation,
3799 which is already in total. */
3801 fmt = GET_RTX_FORMAT (code);
3802 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3803 if (fmt[i] == 'e')
3804 total += rtx_cost (XEXP (x, i), code);
3805 else if (fmt[i] == 'E')
3806 for (j = 0; j < XVECLEN (x, i); j++)
3807 total += rtx_cost (XVECEXP (x, i, j), code);
3809 return total;
3812 /* Return cost of address expression X.
3813 Expect that X is properly formed address reference. */
3816 address_cost (rtx x, enum machine_mode mode)
3818 /* We may be asked for cost of various unusual addresses, such as operands
3819 of push instruction. It is not worthwhile to complicate writing
3820 of the target hook by such cases. */
3822 if (!memory_address_p (mode, x))
3823 return 1000;
3825 return targetm.address_cost (x);
3828 /* If the target doesn't override, compute the cost as with arithmetic. */
3831 default_address_cost (rtx x)
3833 return rtx_cost (x, MEM);
3837 unsigned HOST_WIDE_INT
3838 nonzero_bits (rtx x, enum machine_mode mode)
3840 return cached_nonzero_bits (x, mode, NULL_RTX, VOIDmode, 0);
3843 unsigned int
3844 num_sign_bit_copies (rtx x, enum machine_mode mode)
3846 return cached_num_sign_bit_copies (x, mode, NULL_RTX, VOIDmode, 0);
3849 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
3850 It avoids exponential behavior in nonzero_bits1 when X has
3851 identical subexpressions on the first or the second level. */
3853 static unsigned HOST_WIDE_INT
3854 cached_nonzero_bits (rtx x, enum machine_mode mode, rtx known_x,
3855 enum machine_mode known_mode,
3856 unsigned HOST_WIDE_INT known_ret)
3858 if (x == known_x && mode == known_mode)
3859 return known_ret;
3861 /* Try to find identical subexpressions. If found call
3862 nonzero_bits1 on X with the subexpressions as KNOWN_X and the
3863 precomputed value for the subexpression as KNOWN_RET. */
3865 if (ARITHMETIC_P (x))
3867 rtx x0 = XEXP (x, 0);
3868 rtx x1 = XEXP (x, 1);
3870 /* Check the first level. */
3871 if (x0 == x1)
3872 return nonzero_bits1 (x, mode, x0, mode,
3873 cached_nonzero_bits (x0, mode, known_x,
3874 known_mode, known_ret));
3876 /* Check the second level. */
3877 if (ARITHMETIC_P (x0)
3878 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
3879 return nonzero_bits1 (x, mode, x1, mode,
3880 cached_nonzero_bits (x1, mode, known_x,
3881 known_mode, known_ret));
3883 if (ARITHMETIC_P (x1)
3884 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
3885 return nonzero_bits1 (x, mode, x0, mode,
3886 cached_nonzero_bits (x0, mode, known_x,
3887 known_mode, known_ret));
3890 return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
3893 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
3894 We don't let nonzero_bits recur into num_sign_bit_copies, because that
3895 is less useful. We can't allow both, because that results in exponential
3896 run time recursion. There is a nullstone testcase that triggered
3897 this. This macro avoids accidental uses of num_sign_bit_copies. */
3898 #define cached_num_sign_bit_copies sorry_i_am_preventing_exponential_behavior
3900 /* Given an expression, X, compute which bits in X can be nonzero.
3901 We don't care about bits outside of those defined in MODE.
3903 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
3904 an arithmetic operation, we can do better. */
3906 static unsigned HOST_WIDE_INT
3907 nonzero_bits1 (rtx x, enum machine_mode mode, rtx known_x,
3908 enum machine_mode known_mode,
3909 unsigned HOST_WIDE_INT known_ret)
3911 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
3912 unsigned HOST_WIDE_INT inner_nz;
3913 enum rtx_code code;
3914 unsigned int mode_width = GET_MODE_BITSIZE (mode);
3916 /* For floating-point values, assume all bits are needed. */
3917 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
3918 return nonzero;
3920 /* If X is wider than MODE, use its mode instead. */
3921 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
3923 mode = GET_MODE (x);
3924 nonzero = GET_MODE_MASK (mode);
3925 mode_width = GET_MODE_BITSIZE (mode);
3928 if (mode_width > HOST_BITS_PER_WIDE_INT)
3929 /* Our only callers in this case look for single bit values. So
3930 just return the mode mask. Those tests will then be false. */
3931 return nonzero;
3933 #ifndef WORD_REGISTER_OPERATIONS
3934 /* If MODE is wider than X, but both are a single word for both the host
3935 and target machines, we can compute this from which bits of the
3936 object might be nonzero in its own mode, taking into account the fact
3937 that on many CISC machines, accessing an object in a wider mode
3938 causes the high-order bits to become undefined. So they are
3939 not known to be zero. */
3941 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
3942 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
3943 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
3944 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
3946 nonzero &= cached_nonzero_bits (x, GET_MODE (x),
3947 known_x, known_mode, known_ret);
3948 nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
3949 return nonzero;
3951 #endif
3953 code = GET_CODE (x);
3954 switch (code)
3956 case REG:
3957 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
3958 /* If pointers extend unsigned and this is a pointer in Pmode, say that
3959 all the bits above ptr_mode are known to be zero. */
3960 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
3961 && REG_POINTER (x))
3962 nonzero &= GET_MODE_MASK (ptr_mode);
3963 #endif
3965 /* Include declared information about alignment of pointers. */
3966 /* ??? We don't properly preserve REG_POINTER changes across
3967 pointer-to-integer casts, so we can't trust it except for
3968 things that we know must be pointers. See execute/960116-1.c. */
3969 if ((x == stack_pointer_rtx
3970 || x == frame_pointer_rtx
3971 || x == arg_pointer_rtx)
3972 && REGNO_POINTER_ALIGN (REGNO (x)))
3974 unsigned HOST_WIDE_INT alignment
3975 = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
3977 #ifdef PUSH_ROUNDING
3978 /* If PUSH_ROUNDING is defined, it is possible for the
3979 stack to be momentarily aligned only to that amount,
3980 so we pick the least alignment. */
3981 if (x == stack_pointer_rtx && PUSH_ARGS)
3982 alignment = MIN ((unsigned HOST_WIDE_INT) PUSH_ROUNDING (1),
3983 alignment);
3984 #endif
3986 nonzero &= ~(alignment - 1);
3990 unsigned HOST_WIDE_INT nonzero_for_hook = nonzero;
3991 rtx new = rtl_hooks.reg_nonzero_bits (x, mode, known_x,
3992 known_mode, known_ret,
3993 &nonzero_for_hook);
3995 if (new)
3996 nonzero_for_hook &= cached_nonzero_bits (new, mode, known_x,
3997 known_mode, known_ret);
3999 return nonzero_for_hook;
4002 case CONST_INT:
4003 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
4004 /* If X is negative in MODE, sign-extend the value. */
4005 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
4006 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
4007 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
4008 #endif
4010 return INTVAL (x);
4012 case MEM:
4013 #ifdef LOAD_EXTEND_OP
4014 /* In many, if not most, RISC machines, reading a byte from memory
4015 zeros the rest of the register. Noticing that fact saves a lot
4016 of extra zero-extends. */
4017 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
4018 nonzero &= GET_MODE_MASK (GET_MODE (x));
4019 #endif
4020 break;
4022 case EQ: case NE:
4023 case UNEQ: case LTGT:
4024 case GT: case GTU: case UNGT:
4025 case LT: case LTU: case UNLT:
4026 case GE: case GEU: case UNGE:
4027 case LE: case LEU: case UNLE:
4028 case UNORDERED: case ORDERED:
4030 /* If this produces an integer result, we know which bits are set.
4031 Code here used to clear bits outside the mode of X, but that is
4032 now done above. */
4034 if (GET_MODE_CLASS (mode) == MODE_INT
4035 && mode_width <= HOST_BITS_PER_WIDE_INT)
4036 nonzero = STORE_FLAG_VALUE;
4037 break;
4039 case NEG:
4040 #if 0
4041 /* Disabled to avoid exponential mutual recursion between nonzero_bits
4042 and num_sign_bit_copies. */
4043 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
4044 == GET_MODE_BITSIZE (GET_MODE (x)))
4045 nonzero = 1;
4046 #endif
4048 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
4049 nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
4050 break;
4052 case ABS:
4053 #if 0
4054 /* Disabled to avoid exponential mutual recursion between nonzero_bits
4055 and num_sign_bit_copies. */
4056 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
4057 == GET_MODE_BITSIZE (GET_MODE (x)))
4058 nonzero = 1;
4059 #endif
4060 break;
4062 case TRUNCATE:
4063 nonzero &= (cached_nonzero_bits (XEXP (x, 0), mode,
4064 known_x, known_mode, known_ret)
4065 & GET_MODE_MASK (mode));
4066 break;
4068 case ZERO_EXTEND:
4069 nonzero &= cached_nonzero_bits (XEXP (x, 0), mode,
4070 known_x, known_mode, known_ret);
4071 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
4072 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
4073 break;
4075 case SIGN_EXTEND:
4076 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
4077 Otherwise, show all the bits in the outer mode but not the inner
4078 may be nonzero. */
4079 inner_nz = cached_nonzero_bits (XEXP (x, 0), mode,
4080 known_x, known_mode, known_ret);
4081 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
4083 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
4084 if (inner_nz
4085 & (((HOST_WIDE_INT) 1
4086 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
4087 inner_nz |= (GET_MODE_MASK (mode)
4088 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
4091 nonzero &= inner_nz;
4092 break;
4094 case AND:
4095 nonzero &= cached_nonzero_bits (XEXP (x, 0), mode,
4096 known_x, known_mode, known_ret)
4097 & cached_nonzero_bits (XEXP (x, 1), mode,
4098 known_x, known_mode, known_ret);
4099 break;
4101 case XOR: case IOR:
4102 case UMIN: case UMAX: case SMIN: case SMAX:
4104 unsigned HOST_WIDE_INT nonzero0 =
4105 cached_nonzero_bits (XEXP (x, 0), mode,
4106 known_x, known_mode, known_ret);
4108 /* Don't call nonzero_bits for the second time if it cannot change
4109 anything. */
4110 if ((nonzero & nonzero0) != nonzero)
4111 nonzero &= nonzero0
4112 | cached_nonzero_bits (XEXP (x, 1), mode,
4113 known_x, known_mode, known_ret);
4115 break;
4117 case PLUS: case MINUS:
4118 case MULT:
4119 case DIV: case UDIV:
4120 case MOD: case UMOD:
4121 /* We can apply the rules of arithmetic to compute the number of
4122 high- and low-order zero bits of these operations. We start by
4123 computing the width (position of the highest-order nonzero bit)
4124 and the number of low-order zero bits for each value. */
4126 unsigned HOST_WIDE_INT nz0 =
4127 cached_nonzero_bits (XEXP (x, 0), mode,
4128 known_x, known_mode, known_ret);
4129 unsigned HOST_WIDE_INT nz1 =
4130 cached_nonzero_bits (XEXP (x, 1), mode,
4131 known_x, known_mode, known_ret);
4132 int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
4133 int width0 = floor_log2 (nz0) + 1;
4134 int width1 = floor_log2 (nz1) + 1;
4135 int low0 = floor_log2 (nz0 & -nz0);
4136 int low1 = floor_log2 (nz1 & -nz1);
4137 HOST_WIDE_INT op0_maybe_minusp
4138 = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
4139 HOST_WIDE_INT op1_maybe_minusp
4140 = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
4141 unsigned int result_width = mode_width;
4142 int result_low = 0;
4144 switch (code)
4146 case PLUS:
4147 result_width = MAX (width0, width1) + 1;
4148 result_low = MIN (low0, low1);
4149 break;
4150 case MINUS:
4151 result_low = MIN (low0, low1);
4152 break;
4153 case MULT:
4154 result_width = width0 + width1;
4155 result_low = low0 + low1;
4156 break;
4157 case DIV:
4158 if (width1 == 0)
4159 break;
4160 if (! op0_maybe_minusp && ! op1_maybe_minusp)
4161 result_width = width0;
4162 break;
4163 case UDIV:
4164 if (width1 == 0)
4165 break;
4166 result_width = width0;
4167 break;
4168 case MOD:
4169 if (width1 == 0)
4170 break;
4171 if (! op0_maybe_minusp && ! op1_maybe_minusp)
4172 result_width = MIN (width0, width1);
4173 result_low = MIN (low0, low1);
4174 break;
4175 case UMOD:
4176 if (width1 == 0)
4177 break;
4178 result_width = MIN (width0, width1);
4179 result_low = MIN (low0, low1);
4180 break;
4181 default:
4182 abort ();
4185 if (result_width < mode_width)
4186 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
4188 if (result_low > 0)
4189 nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
4191 #ifdef POINTERS_EXTEND_UNSIGNED
4192 /* If pointers extend unsigned and this is an addition or subtraction
4193 to a pointer in Pmode, all the bits above ptr_mode are known to be
4194 zero. */
4195 if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
4196 && (code == PLUS || code == MINUS)
4197 && REG_P (XEXP (x, 0)) && REG_POINTER (XEXP (x, 0)))
4198 nonzero &= GET_MODE_MASK (ptr_mode);
4199 #endif
4201 break;
4203 case ZERO_EXTRACT:
4204 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4205 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
4206 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
4207 break;
4209 case SUBREG:
4210 /* If this is a SUBREG formed for a promoted variable that has
4211 been zero-extended, we know that at least the high-order bits
4212 are zero, though others might be too. */
4214 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
4215 nonzero = GET_MODE_MASK (GET_MODE (x))
4216 & cached_nonzero_bits (SUBREG_REG (x), GET_MODE (x),
4217 known_x, known_mode, known_ret);
4219 /* If the inner mode is a single word for both the host and target
4220 machines, we can compute this from which bits of the inner
4221 object might be nonzero. */
4222 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
4223 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
4224 <= HOST_BITS_PER_WIDE_INT))
4226 nonzero &= cached_nonzero_bits (SUBREG_REG (x), mode,
4227 known_x, known_mode, known_ret);
4229 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
4230 /* If this is a typical RISC machine, we only have to worry
4231 about the way loads are extended. */
4232 if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
4233 ? (((nonzero
4234 & (((unsigned HOST_WIDE_INT) 1
4235 << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
4236 != 0))
4237 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
4238 || !MEM_P (SUBREG_REG (x)))
4239 #endif
4241 /* On many CISC machines, accessing an object in a wider mode
4242 causes the high-order bits to become undefined. So they are
4243 not known to be zero. */
4244 if (GET_MODE_SIZE (GET_MODE (x))
4245 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4246 nonzero |= (GET_MODE_MASK (GET_MODE (x))
4247 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
4250 break;
4252 case ASHIFTRT:
4253 case LSHIFTRT:
4254 case ASHIFT:
4255 case ROTATE:
4256 /* The nonzero bits are in two classes: any bits within MODE
4257 that aren't in GET_MODE (x) are always significant. The rest of the
4258 nonzero bits are those that are significant in the operand of
4259 the shift when shifted the appropriate number of bits. This
4260 shows that high-order bits are cleared by the right shift and
4261 low-order bits by left shifts. */
4262 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4263 && INTVAL (XEXP (x, 1)) >= 0
4264 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
4266 enum machine_mode inner_mode = GET_MODE (x);
4267 unsigned int width = GET_MODE_BITSIZE (inner_mode);
4268 int count = INTVAL (XEXP (x, 1));
4269 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
4270 unsigned HOST_WIDE_INT op_nonzero =
4271 cached_nonzero_bits (XEXP (x, 0), mode,
4272 known_x, known_mode, known_ret);
4273 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
4274 unsigned HOST_WIDE_INT outer = 0;
4276 if (mode_width > width)
4277 outer = (op_nonzero & nonzero & ~mode_mask);
4279 if (code == LSHIFTRT)
4280 inner >>= count;
4281 else if (code == ASHIFTRT)
4283 inner >>= count;
4285 /* If the sign bit may have been nonzero before the shift, we
4286 need to mark all the places it could have been copied to
4287 by the shift as possibly nonzero. */
4288 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
4289 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
4291 else if (code == ASHIFT)
4292 inner <<= count;
4293 else
4294 inner = ((inner << (count % width)
4295 | (inner >> (width - (count % width)))) & mode_mask);
4297 nonzero &= (outer | inner);
4299 break;
4301 case FFS:
4302 case POPCOUNT:
4303 /* This is at most the number of bits in the mode. */
4304 nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
4305 break;
4307 case CLZ:
4308 /* If CLZ has a known value at zero, then the nonzero bits are
4309 that value, plus the number of bits in the mode minus one. */
4310 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
4311 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
4312 else
4313 nonzero = -1;
4314 break;
4316 case CTZ:
4317 /* If CTZ has a known value at zero, then the nonzero bits are
4318 that value, plus the number of bits in the mode minus one. */
4319 if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
4320 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
4321 else
4322 nonzero = -1;
4323 break;
4325 case PARITY:
4326 nonzero = 1;
4327 break;
4329 case IF_THEN_ELSE:
4331 unsigned HOST_WIDE_INT nonzero_true =
4332 cached_nonzero_bits (XEXP (x, 1), mode,
4333 known_x, known_mode, known_ret);
4335 /* Don't call nonzero_bits for the second time if it cannot change
4336 anything. */
4337 if ((nonzero & nonzero_true) != nonzero)
4338 nonzero &= nonzero_true
4339 | cached_nonzero_bits (XEXP (x, 2), mode,
4340 known_x, known_mode, known_ret);
4342 break;
4344 default:
4345 break;
4348 return nonzero;
4351 /* See the macro definition above. */
4352 #undef cached_num_sign_bit_copies
4355 /* The function cached_num_sign_bit_copies is a wrapper around
4356 num_sign_bit_copies1. It avoids exponential behavior in
4357 num_sign_bit_copies1 when X has identical subexpressions on the
4358 first or the second level. */
4360 static unsigned int
4361 cached_num_sign_bit_copies (rtx x, enum machine_mode mode, rtx known_x,
4362 enum machine_mode known_mode,
4363 unsigned int known_ret)
4365 if (x == known_x && mode == known_mode)
4366 return known_ret;
4368 /* Try to find identical subexpressions. If found call
4369 num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
4370 the precomputed value for the subexpression as KNOWN_RET. */
4372 if (ARITHMETIC_P (x))
4374 rtx x0 = XEXP (x, 0);
4375 rtx x1 = XEXP (x, 1);
4377 /* Check the first level. */
4378 if (x0 == x1)
4379 return
4380 num_sign_bit_copies1 (x, mode, x0, mode,
4381 cached_num_sign_bit_copies (x0, mode, known_x,
4382 known_mode,
4383 known_ret));
4385 /* Check the second level. */
4386 if (ARITHMETIC_P (x0)
4387 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
4388 return
4389 num_sign_bit_copies1 (x, mode, x1, mode,
4390 cached_num_sign_bit_copies (x1, mode, known_x,
4391 known_mode,
4392 known_ret));
4394 if (ARITHMETIC_P (x1)
4395 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
4396 return
4397 num_sign_bit_copies1 (x, mode, x0, mode,
4398 cached_num_sign_bit_copies (x0, mode, known_x,
4399 known_mode,
4400 known_ret));
4403 return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
4406 /* Return the number of bits at the high-order end of X that are known to
4407 be equal to the sign bit. X will be used in mode MODE; if MODE is
4408 VOIDmode, X will be used in its own mode. The returned value will always
4409 be between 1 and the number of bits in MODE. */
4411 static unsigned int
4412 num_sign_bit_copies1 (rtx x, enum machine_mode mode, rtx known_x,
4413 enum machine_mode known_mode,
4414 unsigned int known_ret)
4416 enum rtx_code code = GET_CODE (x);
4417 unsigned int bitwidth = GET_MODE_BITSIZE (mode);
4418 int num0, num1, result;
4419 unsigned HOST_WIDE_INT nonzero;
4421 /* If we weren't given a mode, use the mode of X. If the mode is still
4422 VOIDmode, we don't know anything. Likewise if one of the modes is
4423 floating-point. */
4425 if (mode == VOIDmode)
4426 mode = GET_MODE (x);
4428 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
4429 return 1;
4431 /* For a smaller object, just ignore the high bits. */
4432 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
4434 num0 = cached_num_sign_bit_copies (x, GET_MODE (x),
4435 known_x, known_mode, known_ret);
4436 return MAX (1,
4437 num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
4440 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
4442 #ifndef WORD_REGISTER_OPERATIONS
4443 /* If this machine does not do all register operations on the entire
4444 register and MODE is wider than the mode of X, we can say nothing
4445 at all about the high-order bits. */
4446 return 1;
4447 #else
4448 /* Likewise on machines that do, if the mode of the object is smaller
4449 than a word and loads of that size don't sign extend, we can say
4450 nothing about the high order bits. */
4451 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
4452 #ifdef LOAD_EXTEND_OP
4453 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
4454 #endif
4456 return 1;
4457 #endif
4460 switch (code)
4462 case REG:
4464 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
4465 /* If pointers extend signed and this is a pointer in Pmode, say that
4466 all the bits above ptr_mode are known to be sign bit copies. */
4467 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
4468 && REG_POINTER (x))
4469 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
4470 #endif
4473 unsigned int copies_for_hook = 1, copies = 1;
4474 rtx new = rtl_hooks.reg_num_sign_bit_copies (x, mode, known_x,
4475 known_mode, known_ret,
4476 &copies_for_hook);
4478 if (new)
4479 copies = cached_num_sign_bit_copies (new, mode, known_x,
4480 known_mode, known_ret);
4482 if (copies > 1 || copies_for_hook > 1)
4483 return MAX (copies, copies_for_hook);
4485 /* Else, use nonzero_bits to guess num_sign_bit_copies (see below). */
4487 break;
4489 case MEM:
4490 #ifdef LOAD_EXTEND_OP
4491 /* Some RISC machines sign-extend all loads of smaller than a word. */
4492 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
4493 return MAX (1, ((int) bitwidth
4494 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
4495 #endif
4496 break;
4498 case CONST_INT:
4499 /* If the constant is negative, take its 1's complement and remask.
4500 Then see how many zero bits we have. */
4501 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
4502 if (bitwidth <= HOST_BITS_PER_WIDE_INT
4503 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4504 nonzero = (~nonzero) & GET_MODE_MASK (mode);
4506 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
4508 case SUBREG:
4509 /* If this is a SUBREG for a promoted object that is sign-extended
4510 and we are looking at it in a wider mode, we know that at least the
4511 high-order bits are known to be sign bit copies. */
4513 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
4515 num0 = cached_num_sign_bit_copies (SUBREG_REG (x), mode,
4516 known_x, known_mode, known_ret);
4517 return MAX ((int) bitwidth
4518 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
4519 num0);
4522 /* For a smaller object, just ignore the high bits. */
4523 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
4525 num0 = cached_num_sign_bit_copies (SUBREG_REG (x), VOIDmode,
4526 known_x, known_mode, known_ret);
4527 return MAX (1, (num0
4528 - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
4529 - bitwidth)));
4532 #ifdef WORD_REGISTER_OPERATIONS
4533 #ifdef LOAD_EXTEND_OP
4534 /* For paradoxical SUBREGs on machines where all register operations
4535 affect the entire register, just look inside. Note that we are
4536 passing MODE to the recursive call, so the number of sign bit copies
4537 will remain relative to that mode, not the inner mode. */
4539 /* This works only if loads sign extend. Otherwise, if we get a
4540 reload for the inner part, it may be loaded from the stack, and
4541 then we lose all sign bit copies that existed before the store
4542 to the stack. */
4544 if ((GET_MODE_SIZE (GET_MODE (x))
4545 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4546 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
4547 && MEM_P (SUBREG_REG (x)))
4548 return cached_num_sign_bit_copies (SUBREG_REG (x), mode,
4549 known_x, known_mode, known_ret);
4550 #endif
4551 #endif
4552 break;
4554 case SIGN_EXTRACT:
4555 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4556 return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
4557 break;
4559 case SIGN_EXTEND:
4560 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4561 + cached_num_sign_bit_copies (XEXP (x, 0), VOIDmode,
4562 known_x, known_mode, known_ret));
4564 case TRUNCATE:
4565 /* For a smaller object, just ignore the high bits. */
4566 num0 = cached_num_sign_bit_copies (XEXP (x, 0), VOIDmode,
4567 known_x, known_mode, known_ret);
4568 return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4569 - bitwidth)));
4571 case NOT:
4572 return cached_num_sign_bit_copies (XEXP (x, 0), mode,
4573 known_x, known_mode, known_ret);
4575 case ROTATE: case ROTATERT:
4576 /* If we are rotating left by a number of bits less than the number
4577 of sign bit copies, we can just subtract that amount from the
4578 number. */
4579 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4580 && INTVAL (XEXP (x, 1)) >= 0
4581 && INTVAL (XEXP (x, 1)) < (int) bitwidth)
4583 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4584 known_x, known_mode, known_ret);
4585 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
4586 : (int) bitwidth - INTVAL (XEXP (x, 1))));
4588 break;
4590 case NEG:
4591 /* In general, this subtracts one sign bit copy. But if the value
4592 is known to be positive, the number of sign bit copies is the
4593 same as that of the input. Finally, if the input has just one bit
4594 that might be nonzero, all the bits are copies of the sign bit. */
4595 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4596 known_x, known_mode, known_ret);
4597 if (bitwidth > HOST_BITS_PER_WIDE_INT)
4598 return num0 > 1 ? num0 - 1 : 1;
4600 nonzero = nonzero_bits (XEXP (x, 0), mode);
4601 if (nonzero == 1)
4602 return bitwidth;
4604 if (num0 > 1
4605 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
4606 num0--;
4608 return num0;
4610 case IOR: case AND: case XOR:
4611 case SMIN: case SMAX: case UMIN: case UMAX:
4612 /* Logical operations will preserve the number of sign-bit copies.
4613 MIN and MAX operations always return one of the operands. */
4614 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4615 known_x, known_mode, known_ret);
4616 num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4617 known_x, known_mode, known_ret);
4618 return MIN (num0, num1);
4620 case PLUS: case MINUS:
4621 /* For addition and subtraction, we can have a 1-bit carry. However,
4622 if we are subtracting 1 from a positive number, there will not
4623 be such a carry. Furthermore, if the positive number is known to
4624 be 0 or 1, we know the result is either -1 or 0. */
4626 if (code == PLUS && XEXP (x, 1) == constm1_rtx
4627 && bitwidth <= HOST_BITS_PER_WIDE_INT)
4629 nonzero = nonzero_bits (XEXP (x, 0), mode);
4630 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
4631 return (nonzero == 1 || nonzero == 0 ? bitwidth
4632 : bitwidth - floor_log2 (nonzero) - 1);
4635 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4636 known_x, known_mode, known_ret);
4637 num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4638 known_x, known_mode, known_ret);
4639 result = MAX (1, MIN (num0, num1) - 1);
4641 #ifdef POINTERS_EXTEND_UNSIGNED
4642 /* If pointers extend signed and this is an addition or subtraction
4643 to a pointer in Pmode, all the bits above ptr_mode are known to be
4644 sign bit copies. */
4645 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
4646 && (code == PLUS || code == MINUS)
4647 && REG_P (XEXP (x, 0)) && REG_POINTER (XEXP (x, 0)))
4648 result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
4649 - GET_MODE_BITSIZE (ptr_mode) + 1),
4650 result);
4651 #endif
4652 return result;
4654 case MULT:
4655 /* The number of bits of the product is the sum of the number of
4656 bits of both terms. However, unless one of the terms if known
4657 to be positive, we must allow for an additional bit since negating
4658 a negative number can remove one sign bit copy. */
4660 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4661 known_x, known_mode, known_ret);
4662 num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4663 known_x, known_mode, known_ret);
4665 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
4666 if (result > 0
4667 && (bitwidth > HOST_BITS_PER_WIDE_INT
4668 || (((nonzero_bits (XEXP (x, 0), mode)
4669 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4670 && ((nonzero_bits (XEXP (x, 1), mode)
4671 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
4672 result--;
4674 return MAX (1, result);
4676 case UDIV:
4677 /* The result must be <= the first operand. If the first operand
4678 has the high bit set, we know nothing about the number of sign
4679 bit copies. */
4680 if (bitwidth > HOST_BITS_PER_WIDE_INT)
4681 return 1;
4682 else if ((nonzero_bits (XEXP (x, 0), mode)
4683 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4684 return 1;
4685 else
4686 return cached_num_sign_bit_copies (XEXP (x, 0), mode,
4687 known_x, known_mode, known_ret);
4689 case UMOD:
4690 /* The result must be <= the second operand. */
4691 return cached_num_sign_bit_copies (XEXP (x, 1), mode,
4692 known_x, known_mode, known_ret);
4694 case DIV:
4695 /* Similar to unsigned division, except that we have to worry about
4696 the case where the divisor is negative, in which case we have
4697 to add 1. */
4698 result = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4699 known_x, known_mode, known_ret);
4700 if (result > 1
4701 && (bitwidth > HOST_BITS_PER_WIDE_INT
4702 || (nonzero_bits (XEXP (x, 1), mode)
4703 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
4704 result--;
4706 return result;
4708 case MOD:
4709 result = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4710 known_x, known_mode, known_ret);
4711 if (result > 1
4712 && (bitwidth > HOST_BITS_PER_WIDE_INT
4713 || (nonzero_bits (XEXP (x, 1), mode)
4714 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
4715 result--;
4717 return result;
4719 case ASHIFTRT:
4720 /* Shifts by a constant add to the number of bits equal to the
4721 sign bit. */
4722 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4723 known_x, known_mode, known_ret);
4724 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4725 && INTVAL (XEXP (x, 1)) > 0)
4726 num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
4728 return num0;
4730 case ASHIFT:
4731 /* Left shifts destroy copies. */
4732 if (GET_CODE (XEXP (x, 1)) != CONST_INT
4733 || INTVAL (XEXP (x, 1)) < 0
4734 || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
4735 return 1;
4737 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4738 known_x, known_mode, known_ret);
4739 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
4741 case IF_THEN_ELSE:
4742 num0 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4743 known_x, known_mode, known_ret);
4744 num1 = cached_num_sign_bit_copies (XEXP (x, 2), mode,
4745 known_x, known_mode, known_ret);
4746 return MIN (num0, num1);
4748 case EQ: case NE: case GE: case GT: case LE: case LT:
4749 case UNEQ: case LTGT: case UNGE: case UNGT: case UNLE: case UNLT:
4750 case GEU: case GTU: case LEU: case LTU:
4751 case UNORDERED: case ORDERED:
4752 /* If the constant is negative, take its 1's complement and remask.
4753 Then see how many zero bits we have. */
4754 nonzero = STORE_FLAG_VALUE;
4755 if (bitwidth <= HOST_BITS_PER_WIDE_INT
4756 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4757 nonzero = (~nonzero) & GET_MODE_MASK (mode);
4759 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
4761 default:
4762 break;
4765 /* If we haven't been able to figure it out by one of the above rules,
4766 see if some of the high-order bits are known to be zero. If so,
4767 count those bits and return one less than that amount. If we can't
4768 safely compute the mask for this mode, always return BITWIDTH. */
4770 bitwidth = GET_MODE_BITSIZE (mode);
4771 if (bitwidth > HOST_BITS_PER_WIDE_INT)
4772 return 1;
4774 nonzero = nonzero_bits (x, mode);
4775 return nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
4776 ? 1 : bitwidth - floor_log2 (nonzero) - 1;
4779 /* Calculate the rtx_cost of a single instruction. A return value of
4780 zero indicates an instruction pattern without a known cost. */
4783 insn_rtx_cost (rtx pat)
4785 int i, cost;
4786 rtx set;
4788 /* Extract the single set rtx from the instruction pattern.
4789 We can't use single_set since we only have the pattern. */
4790 if (GET_CODE (pat) == SET)
4791 set = pat;
4792 else if (GET_CODE (pat) == PARALLEL)
4794 set = NULL_RTX;
4795 for (i = 0; i < XVECLEN (pat, 0); i++)
4797 rtx x = XVECEXP (pat, 0, i);
4798 if (GET_CODE (x) == SET)
4800 if (set)
4801 return 0;
4802 set = x;
4805 if (!set)
4806 return 0;
4808 else
4809 return 0;
4811 cost = rtx_cost (SET_SRC (set), SET);
4812 return cost > 0 ? cost : COSTS_N_INSNS (1);