* trans-stmt.c (gfc_trans_simple_do): New function.
[official-gcc.git] / gcc / rtlanal.c
blob7e79a8abeec2df08bde5e99b1c77395244e94f17
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 !MEM_READONLY_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 return 0;
100 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
101 /* ??? When call-clobbered, the value is stable modulo the restore
102 that must happen after a call. This currently screws up local-alloc
103 into believing that the restore is not needed. */
104 if (x == pic_offset_table_rtx)
105 return 0;
106 #endif
107 return 1;
109 case ASM_OPERANDS:
110 if (MEM_VOLATILE_P (x))
111 return 1;
113 /* Fall through. */
115 default:
116 break;
119 fmt = GET_RTX_FORMAT (code);
120 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
121 if (fmt[i] == 'e')
123 if (rtx_unstable_p (XEXP (x, i)))
124 return 1;
126 else if (fmt[i] == 'E')
128 int j;
129 for (j = 0; j < XVECLEN (x, i); j++)
130 if (rtx_unstable_p (XVECEXP (x, i, j)))
131 return 1;
134 return 0;
137 /* Return 1 if X has a value that can vary even between two
138 executions of the program. 0 means X can be compared reliably
139 against certain constants or near-constants.
140 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
141 zero, we are slightly more conservative.
142 The frame pointer and the arg pointer are considered constant. */
145 rtx_varies_p (rtx x, int for_alias)
147 RTX_CODE code;
148 int i;
149 const char *fmt;
151 if (!x)
152 return 0;
154 code = GET_CODE (x);
155 switch (code)
157 case MEM:
158 return !MEM_READONLY_P (x) || rtx_varies_p (XEXP (x, 0), for_alias);
160 case CONST:
161 case CONST_INT:
162 case CONST_DOUBLE:
163 case CONST_VECTOR:
164 case SYMBOL_REF:
165 case LABEL_REF:
166 return 0;
168 case REG:
169 /* Note that we have to test for the actual rtx used for the frame
170 and arg pointers and not just the register number in case we have
171 eliminated the frame and/or arg pointer and are using it
172 for pseudos. */
173 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
174 /* The arg pointer varies if it is not a fixed register. */
175 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
176 return 0;
177 if (x == pic_offset_table_rtx
178 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
179 /* ??? When call-clobbered, the value is stable modulo the restore
180 that must happen after a call. This currently screws up
181 local-alloc into believing that the restore is not needed, so we
182 must return 0 only if we are called from alias analysis. */
183 && for_alias
184 #endif
186 return 0;
187 return 1;
189 case LO_SUM:
190 /* The operand 0 of a LO_SUM is considered constant
191 (in fact it is related specifically to operand 1)
192 during alias analysis. */
193 return (! for_alias && rtx_varies_p (XEXP (x, 0), for_alias))
194 || rtx_varies_p (XEXP (x, 1), for_alias);
196 case ASM_OPERANDS:
197 if (MEM_VOLATILE_P (x))
198 return 1;
200 /* Fall through. */
202 default:
203 break;
206 fmt = GET_RTX_FORMAT (code);
207 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
208 if (fmt[i] == 'e')
210 if (rtx_varies_p (XEXP (x, i), for_alias))
211 return 1;
213 else if (fmt[i] == 'E')
215 int j;
216 for (j = 0; j < XVECLEN (x, i); j++)
217 if (rtx_varies_p (XVECEXP (x, i, j), for_alias))
218 return 1;
221 return 0;
224 /* Return 0 if the use of X as an address in a MEM can cause a trap. */
227 rtx_addr_can_trap_p (rtx x)
229 enum rtx_code code = GET_CODE (x);
231 switch (code)
233 case SYMBOL_REF:
234 return SYMBOL_REF_WEAK (x);
236 case LABEL_REF:
237 return 0;
239 case REG:
240 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
241 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
242 || x == stack_pointer_rtx
243 /* The arg pointer varies if it is not a fixed register. */
244 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
245 return 0;
246 /* All of the virtual frame registers are stack references. */
247 if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
248 && REGNO (x) <= LAST_VIRTUAL_REGISTER)
249 return 0;
250 return 1;
252 case CONST:
253 return rtx_addr_can_trap_p (XEXP (x, 0));
255 case PLUS:
256 /* An address is assumed not to trap if it is an address that can't
257 trap plus a constant integer or it is the pic register plus a
258 constant. */
259 return ! ((! rtx_addr_can_trap_p (XEXP (x, 0))
260 && GET_CODE (XEXP (x, 1)) == CONST_INT)
261 || (XEXP (x, 0) == pic_offset_table_rtx
262 && CONSTANT_P (XEXP (x, 1))));
264 case LO_SUM:
265 case PRE_MODIFY:
266 return rtx_addr_can_trap_p (XEXP (x, 1));
268 case PRE_DEC:
269 case PRE_INC:
270 case POST_DEC:
271 case POST_INC:
272 case POST_MODIFY:
273 return rtx_addr_can_trap_p (XEXP (x, 0));
275 default:
276 break;
279 /* If it isn't one of the case above, it can cause a trap. */
280 return 1;
283 /* Return true if X is an address that is known to not be zero. */
285 bool
286 nonzero_address_p (rtx x)
288 enum rtx_code code = GET_CODE (x);
290 switch (code)
292 case SYMBOL_REF:
293 return !SYMBOL_REF_WEAK (x);
295 case LABEL_REF:
296 return true;
298 case REG:
299 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
300 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
301 || x == stack_pointer_rtx
302 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
303 return true;
304 /* All of the virtual frame registers are stack references. */
305 if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
306 && REGNO (x) <= LAST_VIRTUAL_REGISTER)
307 return true;
308 return false;
310 case CONST:
311 return nonzero_address_p (XEXP (x, 0));
313 case PLUS:
314 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
316 /* Pointers aren't allowed to wrap. If we've got a register
317 that is known to be a pointer, and a positive offset, then
318 the composite can't be zero. */
319 if (INTVAL (XEXP (x, 1)) > 0
320 && REG_P (XEXP (x, 0))
321 && REG_POINTER (XEXP (x, 0)))
322 return true;
324 return nonzero_address_p (XEXP (x, 0));
326 /* Handle PIC references. */
327 else if (XEXP (x, 0) == pic_offset_table_rtx
328 && CONSTANT_P (XEXP (x, 1)))
329 return true;
330 return false;
332 case PRE_MODIFY:
333 /* Similar to the above; allow positive offsets. Further, since
334 auto-inc is only allowed in memories, the register must be a
335 pointer. */
336 if (GET_CODE (XEXP (x, 1)) == CONST_INT
337 && INTVAL (XEXP (x, 1)) > 0)
338 return true;
339 return nonzero_address_p (XEXP (x, 0));
341 case PRE_INC:
342 /* Similarly. Further, the offset is always positive. */
343 return true;
345 case PRE_DEC:
346 case POST_DEC:
347 case POST_INC:
348 case POST_MODIFY:
349 return nonzero_address_p (XEXP (x, 0));
351 case LO_SUM:
352 return nonzero_address_p (XEXP (x, 1));
354 default:
355 break;
358 /* If it isn't one of the case above, might be zero. */
359 return false;
362 /* Return 1 if X refers to a memory location whose address
363 cannot be compared reliably with constant addresses,
364 or if X refers to a BLKmode memory object.
365 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
366 zero, we are slightly more conservative. */
369 rtx_addr_varies_p (rtx x, int for_alias)
371 enum rtx_code code;
372 int i;
373 const char *fmt;
375 if (x == 0)
376 return 0;
378 code = GET_CODE (x);
379 if (code == MEM)
380 return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0), for_alias);
382 fmt = GET_RTX_FORMAT (code);
383 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
384 if (fmt[i] == 'e')
386 if (rtx_addr_varies_p (XEXP (x, i), for_alias))
387 return 1;
389 else if (fmt[i] == 'E')
391 int j;
392 for (j = 0; j < XVECLEN (x, i); j++)
393 if (rtx_addr_varies_p (XVECEXP (x, i, j), for_alias))
394 return 1;
396 return 0;
399 /* Return the value of the integer term in X, if one is apparent;
400 otherwise return 0.
401 Only obvious integer terms are detected.
402 This is used in cse.c with the `related_value' field. */
404 HOST_WIDE_INT
405 get_integer_term (rtx x)
407 if (GET_CODE (x) == CONST)
408 x = XEXP (x, 0);
410 if (GET_CODE (x) == MINUS
411 && GET_CODE (XEXP (x, 1)) == CONST_INT)
412 return - INTVAL (XEXP (x, 1));
413 if (GET_CODE (x) == PLUS
414 && GET_CODE (XEXP (x, 1)) == CONST_INT)
415 return INTVAL (XEXP (x, 1));
416 return 0;
419 /* If X is a constant, return the value sans apparent integer term;
420 otherwise return 0.
421 Only obvious integer terms are detected. */
424 get_related_value (rtx x)
426 if (GET_CODE (x) != CONST)
427 return 0;
428 x = XEXP (x, 0);
429 if (GET_CODE (x) == PLUS
430 && GET_CODE (XEXP (x, 1)) == CONST_INT)
431 return XEXP (x, 0);
432 else if (GET_CODE (x) == MINUS
433 && GET_CODE (XEXP (x, 1)) == CONST_INT)
434 return XEXP (x, 0);
435 return 0;
438 /* A subroutine of global_reg_mentioned_p, returns 1 if *LOC mentions
439 a global register. */
441 static int
442 global_reg_mentioned_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
444 int regno;
445 rtx x = *loc;
447 if (! x)
448 return 0;
450 switch (GET_CODE (x))
452 case SUBREG:
453 if (REG_P (SUBREG_REG (x)))
455 if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
456 && global_regs[subreg_regno (x)])
457 return 1;
458 return 0;
460 break;
462 case REG:
463 regno = REGNO (x);
464 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
465 return 1;
466 return 0;
468 case SCRATCH:
469 case PC:
470 case CC0:
471 case CONST_INT:
472 case CONST_DOUBLE:
473 case CONST:
474 case LABEL_REF:
475 return 0;
477 case CALL:
478 /* A non-constant call might use a global register. */
479 return 1;
481 default:
482 break;
485 return 0;
488 /* Returns nonzero if X mentions a global register. */
491 global_reg_mentioned_p (rtx x)
493 if (INSN_P (x))
495 if (CALL_P (x))
497 if (! CONST_OR_PURE_CALL_P (x))
498 return 1;
499 x = CALL_INSN_FUNCTION_USAGE (x);
500 if (x == 0)
501 return 0;
503 else
504 x = PATTERN (x);
507 return for_each_rtx (&x, global_reg_mentioned_p_1, NULL);
510 /* Return the number of places FIND appears within X. If COUNT_DEST is
511 zero, we do not count occurrences inside the destination of a SET. */
514 count_occurrences (rtx x, rtx find, int count_dest)
516 int i, j;
517 enum rtx_code code;
518 const char *format_ptr;
519 int count;
521 if (x == find)
522 return 1;
524 code = GET_CODE (x);
526 switch (code)
528 case REG:
529 case CONST_INT:
530 case CONST_DOUBLE:
531 case CONST_VECTOR:
532 case SYMBOL_REF:
533 case CODE_LABEL:
534 case PC:
535 case CC0:
536 return 0;
538 case MEM:
539 if (MEM_P (find) && rtx_equal_p (x, find))
540 return 1;
541 break;
543 case SET:
544 if (SET_DEST (x) == find && ! count_dest)
545 return count_occurrences (SET_SRC (x), find, count_dest);
546 break;
548 default:
549 break;
552 format_ptr = GET_RTX_FORMAT (code);
553 count = 0;
555 for (i = 0; i < GET_RTX_LENGTH (code); i++)
557 switch (*format_ptr++)
559 case 'e':
560 count += count_occurrences (XEXP (x, i), find, count_dest);
561 break;
563 case 'E':
564 for (j = 0; j < XVECLEN (x, i); j++)
565 count += count_occurrences (XVECEXP (x, i, j), find, count_dest);
566 break;
569 return count;
572 /* Nonzero if register REG appears somewhere within IN.
573 Also works if REG is not a register; in this case it checks
574 for a subexpression of IN that is Lisp "equal" to REG. */
577 reg_mentioned_p (rtx reg, rtx in)
579 const char *fmt;
580 int i;
581 enum rtx_code code;
583 if (in == 0)
584 return 0;
586 if (reg == in)
587 return 1;
589 if (GET_CODE (in) == LABEL_REF)
590 return reg == XEXP (in, 0);
592 code = GET_CODE (in);
594 switch (code)
596 /* Compare registers by number. */
597 case REG:
598 return REG_P (reg) && REGNO (in) == REGNO (reg);
600 /* These codes have no constituent expressions
601 and are unique. */
602 case SCRATCH:
603 case CC0:
604 case PC:
605 return 0;
607 case CONST_INT:
608 case CONST_VECTOR:
609 case CONST_DOUBLE:
610 /* These are kept unique for a given value. */
611 return 0;
613 default:
614 break;
617 if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
618 return 1;
620 fmt = GET_RTX_FORMAT (code);
622 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
624 if (fmt[i] == 'E')
626 int j;
627 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
628 if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
629 return 1;
631 else if (fmt[i] == 'e'
632 && reg_mentioned_p (reg, XEXP (in, i)))
633 return 1;
635 return 0;
638 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
639 no CODE_LABEL insn. */
642 no_labels_between_p (rtx beg, rtx end)
644 rtx p;
645 if (beg == end)
646 return 0;
647 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
648 if (LABEL_P (p))
649 return 0;
650 return 1;
653 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
654 no JUMP_INSN insn. */
657 no_jumps_between_p (rtx beg, rtx end)
659 rtx p;
660 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
661 if (JUMP_P (p))
662 return 0;
663 return 1;
666 /* Nonzero if register REG is used in an insn between
667 FROM_INSN and TO_INSN (exclusive of those two). */
670 reg_used_between_p (rtx reg, rtx from_insn, rtx to_insn)
672 rtx insn;
674 if (from_insn == to_insn)
675 return 0;
677 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
678 if (INSN_P (insn)
679 && (reg_overlap_mentioned_p (reg, PATTERN (insn))
680 || (CALL_P (insn)
681 && (find_reg_fusage (insn, USE, reg)
682 || find_reg_fusage (insn, CLOBBER, reg)))))
683 return 1;
684 return 0;
687 /* Nonzero if the old value of X, a register, is referenced in BODY. If X
688 is entirely replaced by a new value and the only use is as a SET_DEST,
689 we do not consider it a reference. */
692 reg_referenced_p (rtx x, rtx body)
694 int i;
696 switch (GET_CODE (body))
698 case SET:
699 if (reg_overlap_mentioned_p (x, SET_SRC (body)))
700 return 1;
702 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
703 of a REG that occupies all of the REG, the insn references X if
704 it is mentioned in the destination. */
705 if (GET_CODE (SET_DEST (body)) != CC0
706 && GET_CODE (SET_DEST (body)) != PC
707 && !REG_P (SET_DEST (body))
708 && ! (GET_CODE (SET_DEST (body)) == SUBREG
709 && REG_P (SUBREG_REG (SET_DEST (body)))
710 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
711 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
712 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
713 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
714 && reg_overlap_mentioned_p (x, SET_DEST (body)))
715 return 1;
716 return 0;
718 case ASM_OPERANDS:
719 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
720 if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
721 return 1;
722 return 0;
724 case CALL:
725 case USE:
726 case IF_THEN_ELSE:
727 return reg_overlap_mentioned_p (x, body);
729 case TRAP_IF:
730 return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
732 case PREFETCH:
733 return reg_overlap_mentioned_p (x, XEXP (body, 0));
735 case UNSPEC:
736 case UNSPEC_VOLATILE:
737 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
738 if (reg_overlap_mentioned_p (x, XVECEXP (body, 0, i)))
739 return 1;
740 return 0;
742 case PARALLEL:
743 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
744 if (reg_referenced_p (x, XVECEXP (body, 0, i)))
745 return 1;
746 return 0;
748 case CLOBBER:
749 if (MEM_P (XEXP (body, 0)))
750 if (reg_overlap_mentioned_p (x, XEXP (XEXP (body, 0), 0)))
751 return 1;
752 return 0;
754 case COND_EXEC:
755 if (reg_overlap_mentioned_p (x, COND_EXEC_TEST (body)))
756 return 1;
757 return reg_referenced_p (x, COND_EXEC_CODE (body));
759 default:
760 return 0;
764 /* Nonzero if register REG is referenced in an insn between
765 FROM_INSN and TO_INSN (exclusive of those two). Sets of REG do
766 not count. */
769 reg_referenced_between_p (rtx reg, rtx from_insn, rtx to_insn)
771 rtx insn;
773 if (from_insn == to_insn)
774 return 0;
776 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
777 if (INSN_P (insn)
778 && (reg_referenced_p (reg, PATTERN (insn))
779 || (CALL_P (insn)
780 && find_reg_fusage (insn, USE, reg))))
781 return 1;
782 return 0;
785 /* Nonzero if register REG is set or clobbered in an insn between
786 FROM_INSN and TO_INSN (exclusive of those two). */
789 reg_set_between_p (rtx reg, rtx from_insn, rtx to_insn)
791 rtx insn;
793 if (from_insn == to_insn)
794 return 0;
796 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
797 if (INSN_P (insn) && reg_set_p (reg, insn))
798 return 1;
799 return 0;
802 /* Internals of reg_set_between_p. */
804 reg_set_p (rtx reg, rtx insn)
806 /* We can be passed an insn or part of one. If we are passed an insn,
807 check if a side-effect of the insn clobbers REG. */
808 if (INSN_P (insn)
809 && (FIND_REG_INC_NOTE (insn, reg)
810 || (CALL_P (insn)
811 && ((REG_P (reg)
812 && REGNO (reg) < FIRST_PSEUDO_REGISTER
813 && TEST_HARD_REG_BIT (regs_invalidated_by_call,
814 REGNO (reg)))
815 || MEM_P (reg)
816 || find_reg_fusage (insn, CLOBBER, reg)))))
817 return 1;
819 return set_of (reg, insn) != NULL_RTX;
822 /* Similar to reg_set_between_p, but check all registers in X. Return 0
823 only if none of them are modified between START and END. Do not
824 consider non-registers one way or the other. */
827 regs_set_between_p (rtx x, rtx start, rtx end)
829 enum rtx_code code = GET_CODE (x);
830 const char *fmt;
831 int i, j;
833 switch (code)
835 case CONST_INT:
836 case CONST_DOUBLE:
837 case CONST_VECTOR:
838 case CONST:
839 case SYMBOL_REF:
840 case LABEL_REF:
841 case PC:
842 case CC0:
843 return 0;
845 case REG:
846 return reg_set_between_p (x, start, end);
848 default:
849 break;
852 fmt = GET_RTX_FORMAT (code);
853 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
855 if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
856 return 1;
858 else if (fmt[i] == 'E')
859 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
860 if (regs_set_between_p (XVECEXP (x, i, j), start, end))
861 return 1;
864 return 0;
867 /* Similar to reg_set_between_p, but check all registers in X. Return 0
868 only if none of them are modified between START and END. Return 1 if
869 X contains a MEM; this routine does usememory aliasing. */
872 modified_between_p (rtx x, rtx start, rtx end)
874 enum rtx_code code = GET_CODE (x);
875 const char *fmt;
876 int i, j;
877 rtx insn;
879 if (start == end)
880 return 0;
882 switch (code)
884 case CONST_INT:
885 case CONST_DOUBLE:
886 case CONST_VECTOR:
887 case CONST:
888 case SYMBOL_REF:
889 case LABEL_REF:
890 return 0;
892 case PC:
893 case CC0:
894 return 1;
896 case MEM:
897 if (MEM_READONLY_P (x))
898 return 0;
899 if (modified_between_p (XEXP (x, 0), start, end))
900 return 1;
901 for (insn = NEXT_INSN (start); insn != end; insn = NEXT_INSN (insn))
902 if (memory_modified_in_insn_p (x, insn))
903 return 1;
904 return 0;
905 break;
907 case REG:
908 return reg_set_between_p (x, start, end);
910 default:
911 break;
914 fmt = GET_RTX_FORMAT (code);
915 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
917 if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
918 return 1;
920 else if (fmt[i] == 'E')
921 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
922 if (modified_between_p (XVECEXP (x, i, j), start, end))
923 return 1;
926 return 0;
929 /* Similar to reg_set_p, but check all registers in X. Return 0 only if none
930 of them are modified in INSN. Return 1 if X contains a MEM; this routine
931 does use memory aliasing. */
934 modified_in_p (rtx x, rtx insn)
936 enum rtx_code code = GET_CODE (x);
937 const char *fmt;
938 int i, j;
940 switch (code)
942 case CONST_INT:
943 case CONST_DOUBLE:
944 case CONST_VECTOR:
945 case CONST:
946 case SYMBOL_REF:
947 case LABEL_REF:
948 return 0;
950 case PC:
951 case CC0:
952 return 1;
954 case MEM:
955 if (MEM_READONLY_P (x))
956 return 0;
957 if (modified_in_p (XEXP (x, 0), insn))
958 return 1;
959 if (memory_modified_in_insn_p (x, insn))
960 return 1;
961 return 0;
962 break;
964 case REG:
965 return reg_set_p (x, insn);
967 default:
968 break;
971 fmt = GET_RTX_FORMAT (code);
972 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
974 if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
975 return 1;
977 else if (fmt[i] == 'E')
978 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
979 if (modified_in_p (XVECEXP (x, i, j), insn))
980 return 1;
983 return 0;
986 /* Return true if anything in insn X is (anti,output,true) dependent on
987 anything in insn Y. */
990 insn_dependent_p (rtx x, rtx y)
992 rtx tmp;
994 gcc_assert (INSN_P (x));
995 gcc_assert (INSN_P (y));
997 tmp = PATTERN (y);
998 note_stores (PATTERN (x), insn_dependent_p_1, &tmp);
999 if (tmp == NULL_RTX)
1000 return 1;
1002 tmp = PATTERN (x);
1003 note_stores (PATTERN (y), insn_dependent_p_1, &tmp);
1004 if (tmp == NULL_RTX)
1005 return 1;
1007 return 0;
1010 /* A helper routine for insn_dependent_p called through note_stores. */
1012 static void
1013 insn_dependent_p_1 (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
1015 rtx * pinsn = (rtx *) data;
1017 if (*pinsn && reg_mentioned_p (x, *pinsn))
1018 *pinsn = NULL_RTX;
1021 /* Helper function for set_of. */
1022 struct set_of_data
1024 rtx found;
1025 rtx pat;
1028 static void
1029 set_of_1 (rtx x, rtx pat, void *data1)
1031 struct set_of_data *data = (struct set_of_data *) (data1);
1032 if (rtx_equal_p (x, data->pat)
1033 || (!MEM_P (x) && reg_overlap_mentioned_p (data->pat, x)))
1034 data->found = pat;
1037 /* Give an INSN, return a SET or CLOBBER expression that does modify PAT
1038 (either directly or via STRICT_LOW_PART and similar modifiers). */
1040 set_of (rtx pat, rtx insn)
1042 struct set_of_data data;
1043 data.found = NULL_RTX;
1044 data.pat = pat;
1045 note_stores (INSN_P (insn) ? PATTERN (insn) : insn, set_of_1, &data);
1046 return data.found;
1049 /* Given an INSN, return a SET expression if this insn has only a single SET.
1050 It may also have CLOBBERs, USEs, or SET whose output
1051 will not be used, which we ignore. */
1054 single_set_2 (rtx insn, rtx pat)
1056 rtx set = NULL;
1057 int set_verified = 1;
1058 int i;
1060 if (GET_CODE (pat) == PARALLEL)
1062 for (i = 0; i < XVECLEN (pat, 0); i++)
1064 rtx sub = XVECEXP (pat, 0, i);
1065 switch (GET_CODE (sub))
1067 case USE:
1068 case CLOBBER:
1069 break;
1071 case SET:
1072 /* We can consider insns having multiple sets, where all
1073 but one are dead as single set insns. In common case
1074 only single set is present in the pattern so we want
1075 to avoid checking for REG_UNUSED notes unless necessary.
1077 When we reach set first time, we just expect this is
1078 the single set we are looking for and only when more
1079 sets are found in the insn, we check them. */
1080 if (!set_verified)
1082 if (find_reg_note (insn, REG_UNUSED, SET_DEST (set))
1083 && !side_effects_p (set))
1084 set = NULL;
1085 else
1086 set_verified = 1;
1088 if (!set)
1089 set = sub, set_verified = 0;
1090 else if (!find_reg_note (insn, REG_UNUSED, SET_DEST (sub))
1091 || side_effects_p (sub))
1092 return NULL_RTX;
1093 break;
1095 default:
1096 return NULL_RTX;
1100 return set;
1103 /* Given an INSN, return nonzero if it has more than one SET, else return
1104 zero. */
1107 multiple_sets (rtx insn)
1109 int found;
1110 int i;
1112 /* INSN must be an insn. */
1113 if (! INSN_P (insn))
1114 return 0;
1116 /* Only a PARALLEL can have multiple SETs. */
1117 if (GET_CODE (PATTERN (insn)) == PARALLEL)
1119 for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1120 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1122 /* If we have already found a SET, then return now. */
1123 if (found)
1124 return 1;
1125 else
1126 found = 1;
1130 /* Either zero or one SET. */
1131 return 0;
1134 /* Return nonzero if the destination of SET equals the source
1135 and there are no side effects. */
1138 set_noop_p (rtx set)
1140 rtx src = SET_SRC (set);
1141 rtx dst = SET_DEST (set);
1143 if (dst == pc_rtx && src == pc_rtx)
1144 return 1;
1146 if (MEM_P (dst) && MEM_P (src))
1147 return rtx_equal_p (dst, src) && !side_effects_p (dst);
1149 if (GET_CODE (dst) == SIGN_EXTRACT
1150 || GET_CODE (dst) == ZERO_EXTRACT)
1151 return rtx_equal_p (XEXP (dst, 0), src)
1152 && ! BYTES_BIG_ENDIAN && XEXP (dst, 2) == const0_rtx
1153 && !side_effects_p (src);
1155 if (GET_CODE (dst) == STRICT_LOW_PART)
1156 dst = XEXP (dst, 0);
1158 if (GET_CODE (src) == SUBREG && GET_CODE (dst) == SUBREG)
1160 if (SUBREG_BYTE (src) != SUBREG_BYTE (dst))
1161 return 0;
1162 src = SUBREG_REG (src);
1163 dst = SUBREG_REG (dst);
1166 return (REG_P (src) && REG_P (dst)
1167 && REGNO (src) == REGNO (dst));
1170 /* Return nonzero if an insn consists only of SETs, each of which only sets a
1171 value to itself. */
1174 noop_move_p (rtx insn)
1176 rtx pat = PATTERN (insn);
1178 if (INSN_CODE (insn) == NOOP_MOVE_INSN_CODE)
1179 return 1;
1181 /* Insns carrying these notes are useful later on. */
1182 if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
1183 return 0;
1185 /* For now treat an insn with a REG_RETVAL note as a
1186 a special insn which should not be considered a no-op. */
1187 if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
1188 return 0;
1190 if (GET_CODE (pat) == SET && set_noop_p (pat))
1191 return 1;
1193 if (GET_CODE (pat) == PARALLEL)
1195 int i;
1196 /* If nothing but SETs of registers to themselves,
1197 this insn can also be deleted. */
1198 for (i = 0; i < XVECLEN (pat, 0); i++)
1200 rtx tem = XVECEXP (pat, 0, i);
1202 if (GET_CODE (tem) == USE
1203 || GET_CODE (tem) == CLOBBER)
1204 continue;
1206 if (GET_CODE (tem) != SET || ! set_noop_p (tem))
1207 return 0;
1210 return 1;
1212 return 0;
1216 /* Return the last thing that X was assigned from before *PINSN. If VALID_TO
1217 is not NULL_RTX then verify that the object is not modified up to VALID_TO.
1218 If the object was modified, if we hit a partial assignment to X, or hit a
1219 CODE_LABEL first, return X. If we found an assignment, update *PINSN to
1220 point to it. ALLOW_HWREG is set to 1 if hardware registers are allowed to
1221 be the src. */
1224 find_last_value (rtx x, rtx *pinsn, rtx valid_to, int allow_hwreg)
1226 rtx p;
1228 for (p = PREV_INSN (*pinsn); p && !LABEL_P (p);
1229 p = PREV_INSN (p))
1230 if (INSN_P (p))
1232 rtx set = single_set (p);
1233 rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
1235 if (set && rtx_equal_p (x, SET_DEST (set)))
1237 rtx src = SET_SRC (set);
1239 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
1240 src = XEXP (note, 0);
1242 if ((valid_to == NULL_RTX
1243 || ! modified_between_p (src, PREV_INSN (p), valid_to))
1244 /* Reject hard registers because we don't usually want
1245 to use them; we'd rather use a pseudo. */
1246 && (! (REG_P (src)
1247 && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
1249 *pinsn = p;
1250 return src;
1254 /* If set in non-simple way, we don't have a value. */
1255 if (reg_set_p (x, p))
1256 break;
1259 return x;
1262 /* Return nonzero if register in range [REGNO, ENDREGNO)
1263 appears either explicitly or implicitly in X
1264 other than being stored into.
1266 References contained within the substructure at LOC do not count.
1267 LOC may be zero, meaning don't ignore anything. */
1270 refers_to_regno_p (unsigned int regno, unsigned int endregno, rtx x,
1271 rtx *loc)
1273 int i;
1274 unsigned int x_regno;
1275 RTX_CODE code;
1276 const char *fmt;
1278 repeat:
1279 /* The contents of a REG_NONNEG note is always zero, so we must come here
1280 upon repeat in case the last REG_NOTE is a REG_NONNEG note. */
1281 if (x == 0)
1282 return 0;
1284 code = GET_CODE (x);
1286 switch (code)
1288 case REG:
1289 x_regno = REGNO (x);
1291 /* If we modifying the stack, frame, or argument pointer, it will
1292 clobber a virtual register. In fact, we could be more precise,
1293 but it isn't worth it. */
1294 if ((x_regno == STACK_POINTER_REGNUM
1295 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1296 || x_regno == ARG_POINTER_REGNUM
1297 #endif
1298 || x_regno == FRAME_POINTER_REGNUM)
1299 && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
1300 return 1;
1302 return (endregno > x_regno
1303 && regno < x_regno + (x_regno < FIRST_PSEUDO_REGISTER
1304 ? hard_regno_nregs[x_regno][GET_MODE (x)]
1305 : 1));
1307 case SUBREG:
1308 /* If this is a SUBREG of a hard reg, we can see exactly which
1309 registers are being modified. Otherwise, handle normally. */
1310 if (REG_P (SUBREG_REG (x))
1311 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
1313 unsigned int inner_regno = subreg_regno (x);
1314 unsigned int inner_endregno
1315 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
1316 ? hard_regno_nregs[inner_regno][GET_MODE (x)] : 1);
1318 return endregno > inner_regno && regno < inner_endregno;
1320 break;
1322 case CLOBBER:
1323 case SET:
1324 if (&SET_DEST (x) != loc
1325 /* Note setting a SUBREG counts as referring to the REG it is in for
1326 a pseudo but not for hard registers since we can
1327 treat each word individually. */
1328 && ((GET_CODE (SET_DEST (x)) == SUBREG
1329 && loc != &SUBREG_REG (SET_DEST (x))
1330 && REG_P (SUBREG_REG (SET_DEST (x)))
1331 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
1332 && refers_to_regno_p (regno, endregno,
1333 SUBREG_REG (SET_DEST (x)), loc))
1334 || (!REG_P (SET_DEST (x))
1335 && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
1336 return 1;
1338 if (code == CLOBBER || loc == &SET_SRC (x))
1339 return 0;
1340 x = SET_SRC (x);
1341 goto repeat;
1343 default:
1344 break;
1347 /* X does not match, so try its subexpressions. */
1349 fmt = GET_RTX_FORMAT (code);
1350 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1352 if (fmt[i] == 'e' && loc != &XEXP (x, i))
1354 if (i == 0)
1356 x = XEXP (x, 0);
1357 goto repeat;
1359 else
1360 if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
1361 return 1;
1363 else if (fmt[i] == 'E')
1365 int j;
1366 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1367 if (loc != &XVECEXP (x, i, j)
1368 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
1369 return 1;
1372 return 0;
1375 /* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
1376 we check if any register number in X conflicts with the relevant register
1377 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
1378 contains a MEM (we don't bother checking for memory addresses that can't
1379 conflict because we expect this to be a rare case. */
1382 reg_overlap_mentioned_p (rtx x, rtx in)
1384 unsigned int regno, endregno;
1386 /* If either argument is a constant, then modifying X can not
1387 affect IN. Here we look at IN, we can profitably combine
1388 CONSTANT_P (x) with the switch statement below. */
1389 if (CONSTANT_P (in))
1390 return 0;
1392 recurse:
1393 switch (GET_CODE (x))
1395 case STRICT_LOW_PART:
1396 case ZERO_EXTRACT:
1397 case SIGN_EXTRACT:
1398 /* Overly conservative. */
1399 x = XEXP (x, 0);
1400 goto recurse;
1402 case SUBREG:
1403 regno = REGNO (SUBREG_REG (x));
1404 if (regno < FIRST_PSEUDO_REGISTER)
1405 regno = subreg_regno (x);
1406 goto do_reg;
1408 case REG:
1409 regno = REGNO (x);
1410 do_reg:
1411 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
1412 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
1413 return refers_to_regno_p (regno, endregno, in, (rtx*) 0);
1415 case MEM:
1417 const char *fmt;
1418 int i;
1420 if (MEM_P (in))
1421 return 1;
1423 fmt = GET_RTX_FORMAT (GET_CODE (in));
1424 for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
1425 if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
1426 return 1;
1428 return 0;
1431 case SCRATCH:
1432 case PC:
1433 case CC0:
1434 return reg_mentioned_p (x, in);
1436 case PARALLEL:
1438 int i;
1440 /* If any register in here refers to it we return true. */
1441 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1442 if (XEXP (XVECEXP (x, 0, i), 0) != 0
1443 && reg_overlap_mentioned_p (XEXP (XVECEXP (x, 0, i), 0), in))
1444 return 1;
1445 return 0;
1448 default:
1449 gcc_assert (CONSTANT_P (x));
1450 return 0;
1454 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1455 (X would be the pattern of an insn).
1456 FUN receives two arguments:
1457 the REG, MEM, CC0 or PC being stored in or clobbered,
1458 the SET or CLOBBER rtx that does the store.
1460 If the item being stored in or clobbered is a SUBREG of a hard register,
1461 the SUBREG will be passed. */
1463 void
1464 note_stores (rtx x, void (*fun) (rtx, rtx, void *), void *data)
1466 int i;
1468 if (GET_CODE (x) == COND_EXEC)
1469 x = COND_EXEC_CODE (x);
1471 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1473 rtx dest = SET_DEST (x);
1475 while ((GET_CODE (dest) == SUBREG
1476 && (!REG_P (SUBREG_REG (dest))
1477 || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1478 || GET_CODE (dest) == ZERO_EXTRACT
1479 || GET_CODE (dest) == SIGN_EXTRACT
1480 || GET_CODE (dest) == STRICT_LOW_PART)
1481 dest = XEXP (dest, 0);
1483 /* If we have a PARALLEL, SET_DEST is a list of EXPR_LIST expressions,
1484 each of whose first operand is a register. */
1485 if (GET_CODE (dest) == PARALLEL)
1487 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1488 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1489 (*fun) (XEXP (XVECEXP (dest, 0, i), 0), x, data);
1491 else
1492 (*fun) (dest, x, data);
1495 else if (GET_CODE (x) == PARALLEL)
1496 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1497 note_stores (XVECEXP (x, 0, i), fun, data);
1500 /* Like notes_stores, but call FUN for each expression that is being
1501 referenced in PBODY, a pointer to the PATTERN of an insn. We only call
1502 FUN for each expression, not any interior subexpressions. FUN receives a
1503 pointer to the expression and the DATA passed to this function.
1505 Note that this is not quite the same test as that done in reg_referenced_p
1506 since that considers something as being referenced if it is being
1507 partially set, while we do not. */
1509 void
1510 note_uses (rtx *pbody, void (*fun) (rtx *, void *), void *data)
1512 rtx body = *pbody;
1513 int i;
1515 switch (GET_CODE (body))
1517 case COND_EXEC:
1518 (*fun) (&COND_EXEC_TEST (body), data);
1519 note_uses (&COND_EXEC_CODE (body), fun, data);
1520 return;
1522 case PARALLEL:
1523 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1524 note_uses (&XVECEXP (body, 0, i), fun, data);
1525 return;
1527 case USE:
1528 (*fun) (&XEXP (body, 0), data);
1529 return;
1531 case ASM_OPERANDS:
1532 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
1533 (*fun) (&ASM_OPERANDS_INPUT (body, i), data);
1534 return;
1536 case TRAP_IF:
1537 (*fun) (&TRAP_CONDITION (body), data);
1538 return;
1540 case PREFETCH:
1541 (*fun) (&XEXP (body, 0), data);
1542 return;
1544 case UNSPEC:
1545 case UNSPEC_VOLATILE:
1546 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1547 (*fun) (&XVECEXP (body, 0, i), data);
1548 return;
1550 case CLOBBER:
1551 if (MEM_P (XEXP (body, 0)))
1552 (*fun) (&XEXP (XEXP (body, 0), 0), data);
1553 return;
1555 case SET:
1557 rtx dest = SET_DEST (body);
1559 /* For sets we replace everything in source plus registers in memory
1560 expression in store and operands of a ZERO_EXTRACT. */
1561 (*fun) (&SET_SRC (body), data);
1563 if (GET_CODE (dest) == ZERO_EXTRACT)
1565 (*fun) (&XEXP (dest, 1), data);
1566 (*fun) (&XEXP (dest, 2), data);
1569 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART)
1570 dest = XEXP (dest, 0);
1572 if (MEM_P (dest))
1573 (*fun) (&XEXP (dest, 0), data);
1575 return;
1577 default:
1578 /* All the other possibilities never store. */
1579 (*fun) (pbody, data);
1580 return;
1584 /* Return nonzero if X's old contents don't survive after INSN.
1585 This will be true if X is (cc0) or if X is a register and
1586 X dies in INSN or because INSN entirely sets X.
1588 "Entirely set" means set directly and not through a SUBREG,
1589 ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1590 Likewise, REG_INC does not count.
1592 REG may be a hard or pseudo reg. Renumbering is not taken into account,
1593 but for this use that makes no difference, since regs don't overlap
1594 during their lifetimes. Therefore, this function may be used
1595 at any time after deaths have been computed (in flow.c).
1597 If REG is a hard reg that occupies multiple machine registers, this
1598 function will only return 1 if each of those registers will be replaced
1599 by INSN. */
1602 dead_or_set_p (rtx insn, rtx x)
1604 unsigned int regno, last_regno;
1605 unsigned int i;
1607 /* Can't use cc0_rtx below since this file is used by genattrtab.c. */
1608 if (GET_CODE (x) == CC0)
1609 return 1;
1611 gcc_assert (REG_P (x));
1613 regno = REGNO (x);
1614 last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1615 : regno + hard_regno_nregs[regno][GET_MODE (x)] - 1);
1617 for (i = regno; i <= last_regno; i++)
1618 if (! dead_or_set_regno_p (insn, i))
1619 return 0;
1621 return 1;
1624 /* Utility function for dead_or_set_p to check an individual register. Also
1625 called from flow.c. */
1628 dead_or_set_regno_p (rtx insn, unsigned int test_regno)
1630 unsigned int regno, endregno;
1631 rtx pattern;
1633 /* See if there is a death note for something that includes TEST_REGNO. */
1634 if (find_regno_note (insn, REG_DEAD, test_regno))
1635 return 1;
1637 if (CALL_P (insn)
1638 && find_regno_fusage (insn, CLOBBER, test_regno))
1639 return 1;
1641 pattern = PATTERN (insn);
1643 if (GET_CODE (pattern) == COND_EXEC)
1644 pattern = COND_EXEC_CODE (pattern);
1646 if (GET_CODE (pattern) == SET)
1648 rtx dest = SET_DEST (pattern);
1650 /* A value is totally replaced if it is the destination or the
1651 destination is a SUBREG of REGNO that does not change the number of
1652 words in it. */
1653 if (GET_CODE (dest) == SUBREG
1654 && (((GET_MODE_SIZE (GET_MODE (dest))
1655 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1656 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1657 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1658 dest = SUBREG_REG (dest);
1660 if (!REG_P (dest))
1661 return 0;
1663 regno = REGNO (dest);
1664 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1665 : regno + hard_regno_nregs[regno][GET_MODE (dest)]);
1667 return (test_regno >= regno && test_regno < endregno);
1669 else if (GET_CODE (pattern) == PARALLEL)
1671 int i;
1673 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
1675 rtx body = XVECEXP (pattern, 0, i);
1677 if (GET_CODE (body) == COND_EXEC)
1678 body = COND_EXEC_CODE (body);
1680 if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1682 rtx dest = SET_DEST (body);
1684 if (GET_CODE (dest) == SUBREG
1685 && (((GET_MODE_SIZE (GET_MODE (dest))
1686 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1687 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1688 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1689 dest = SUBREG_REG (dest);
1691 if (!REG_P (dest))
1692 continue;
1694 regno = REGNO (dest);
1695 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1696 : regno + hard_regno_nregs[regno][GET_MODE (dest)]);
1698 if (test_regno >= regno && test_regno < endregno)
1699 return 1;
1704 return 0;
1707 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1708 If DATUM is nonzero, look for one whose datum is DATUM. */
1711 find_reg_note (rtx insn, enum reg_note kind, rtx datum)
1713 rtx link;
1715 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1716 if (! INSN_P (insn))
1717 return 0;
1718 if (datum == 0)
1720 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1721 if (REG_NOTE_KIND (link) == kind)
1722 return link;
1723 return 0;
1726 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1727 if (REG_NOTE_KIND (link) == kind && datum == XEXP (link, 0))
1728 return link;
1729 return 0;
1732 /* Return the reg-note of kind KIND in insn INSN which applies to register
1733 number REGNO, if any. Return 0 if there is no such reg-note. Note that
1734 the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1735 it might be the case that the note overlaps REGNO. */
1738 find_regno_note (rtx insn, enum reg_note kind, unsigned int regno)
1740 rtx link;
1742 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1743 if (! INSN_P (insn))
1744 return 0;
1746 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1747 if (REG_NOTE_KIND (link) == kind
1748 /* Verify that it is a register, so that scratch and MEM won't cause a
1749 problem here. */
1750 && REG_P (XEXP (link, 0))
1751 && REGNO (XEXP (link, 0)) <= regno
1752 && ((REGNO (XEXP (link, 0))
1753 + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1754 : hard_regno_nregs[REGNO (XEXP (link, 0))]
1755 [GET_MODE (XEXP (link, 0))]))
1756 > regno))
1757 return link;
1758 return 0;
1761 /* Return a REG_EQUIV or REG_EQUAL note if insn has only a single set and
1762 has such a note. */
1765 find_reg_equal_equiv_note (rtx insn)
1767 rtx link;
1769 if (!INSN_P (insn))
1770 return 0;
1771 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1772 if (REG_NOTE_KIND (link) == REG_EQUAL
1773 || REG_NOTE_KIND (link) == REG_EQUIV)
1775 if (single_set (insn) == 0)
1776 return 0;
1777 return link;
1779 return NULL;
1782 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1783 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1786 find_reg_fusage (rtx insn, enum rtx_code code, rtx datum)
1788 /* If it's not a CALL_INSN, it can't possibly have a
1789 CALL_INSN_FUNCTION_USAGE field, so don't bother checking. */
1790 if (!CALL_P (insn))
1791 return 0;
1793 gcc_assert (datum);
1795 if (!REG_P (datum))
1797 rtx link;
1799 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1800 link;
1801 link = XEXP (link, 1))
1802 if (GET_CODE (XEXP (link, 0)) == code
1803 && rtx_equal_p (datum, XEXP (XEXP (link, 0), 0)))
1804 return 1;
1806 else
1808 unsigned int regno = REGNO (datum);
1810 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1811 to pseudo registers, so don't bother checking. */
1813 if (regno < FIRST_PSEUDO_REGISTER)
1815 unsigned int end_regno
1816 = regno + hard_regno_nregs[regno][GET_MODE (datum)];
1817 unsigned int i;
1819 for (i = regno; i < end_regno; i++)
1820 if (find_regno_fusage (insn, code, i))
1821 return 1;
1825 return 0;
1828 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1829 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1832 find_regno_fusage (rtx insn, enum rtx_code code, unsigned int regno)
1834 rtx link;
1836 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1837 to pseudo registers, so don't bother checking. */
1839 if (regno >= FIRST_PSEUDO_REGISTER
1840 || !CALL_P (insn) )
1841 return 0;
1843 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1845 unsigned int regnote;
1846 rtx op, reg;
1848 if (GET_CODE (op = XEXP (link, 0)) == code
1849 && REG_P (reg = XEXP (op, 0))
1850 && (regnote = REGNO (reg)) <= regno
1851 && regnote + hard_regno_nregs[regnote][GET_MODE (reg)] > regno)
1852 return 1;
1855 return 0;
1858 /* Return true if INSN is a call to a pure function. */
1861 pure_call_p (rtx insn)
1863 rtx link;
1865 if (!CALL_P (insn) || ! CONST_OR_PURE_CALL_P (insn))
1866 return 0;
1868 /* Look for the note that differentiates const and pure functions. */
1869 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1871 rtx u, m;
1873 if (GET_CODE (u = XEXP (link, 0)) == USE
1874 && MEM_P (m = XEXP (u, 0)) && GET_MODE (m) == BLKmode
1875 && GET_CODE (XEXP (m, 0)) == SCRATCH)
1876 return 1;
1879 return 0;
1882 /* Remove register note NOTE from the REG_NOTES of INSN. */
1884 void
1885 remove_note (rtx insn, rtx note)
1887 rtx link;
1889 if (note == NULL_RTX)
1890 return;
1892 if (REG_NOTES (insn) == note)
1894 REG_NOTES (insn) = XEXP (note, 1);
1895 return;
1898 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1899 if (XEXP (link, 1) == note)
1901 XEXP (link, 1) = XEXP (note, 1);
1902 return;
1905 gcc_unreachable ();
1908 /* Search LISTP (an EXPR_LIST) for an entry whose first operand is NODE and
1909 return 1 if it is found. A simple equality test is used to determine if
1910 NODE matches. */
1913 in_expr_list_p (rtx listp, rtx node)
1915 rtx x;
1917 for (x = listp; x; x = XEXP (x, 1))
1918 if (node == XEXP (x, 0))
1919 return 1;
1921 return 0;
1924 /* Search LISTP (an EXPR_LIST) for an entry whose first operand is NODE and
1925 remove that entry from the list if it is found.
1927 A simple equality test is used to determine if NODE matches. */
1929 void
1930 remove_node_from_expr_list (rtx node, rtx *listp)
1932 rtx temp = *listp;
1933 rtx prev = NULL_RTX;
1935 while (temp)
1937 if (node == XEXP (temp, 0))
1939 /* Splice the node out of the list. */
1940 if (prev)
1941 XEXP (prev, 1) = XEXP (temp, 1);
1942 else
1943 *listp = XEXP (temp, 1);
1945 return;
1948 prev = temp;
1949 temp = XEXP (temp, 1);
1953 /* Nonzero if X contains any volatile instructions. These are instructions
1954 which may cause unpredictable machine state instructions, and thus no
1955 instructions should be moved or combined across them. This includes
1956 only volatile asms and UNSPEC_VOLATILE instructions. */
1959 volatile_insn_p (rtx x)
1961 RTX_CODE code;
1963 code = GET_CODE (x);
1964 switch (code)
1966 case LABEL_REF:
1967 case SYMBOL_REF:
1968 case CONST_INT:
1969 case CONST:
1970 case CONST_DOUBLE:
1971 case CONST_VECTOR:
1972 case CC0:
1973 case PC:
1974 case REG:
1975 case SCRATCH:
1976 case CLOBBER:
1977 case ADDR_VEC:
1978 case ADDR_DIFF_VEC:
1979 case CALL:
1980 case MEM:
1981 return 0;
1983 case UNSPEC_VOLATILE:
1984 /* case TRAP_IF: This isn't clear yet. */
1985 return 1;
1987 case ASM_INPUT:
1988 case ASM_OPERANDS:
1989 if (MEM_VOLATILE_P (x))
1990 return 1;
1992 default:
1993 break;
1996 /* Recursively scan the operands of this expression. */
1999 const char *fmt = GET_RTX_FORMAT (code);
2000 int i;
2002 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2004 if (fmt[i] == 'e')
2006 if (volatile_insn_p (XEXP (x, i)))
2007 return 1;
2009 else if (fmt[i] == 'E')
2011 int j;
2012 for (j = 0; j < XVECLEN (x, i); j++)
2013 if (volatile_insn_p (XVECEXP (x, i, j)))
2014 return 1;
2018 return 0;
2021 /* Nonzero if X contains any volatile memory references
2022 UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions. */
2025 volatile_refs_p (rtx x)
2027 RTX_CODE code;
2029 code = GET_CODE (x);
2030 switch (code)
2032 case LABEL_REF:
2033 case SYMBOL_REF:
2034 case CONST_INT:
2035 case CONST:
2036 case CONST_DOUBLE:
2037 case CONST_VECTOR:
2038 case CC0:
2039 case PC:
2040 case REG:
2041 case SCRATCH:
2042 case CLOBBER:
2043 case ADDR_VEC:
2044 case ADDR_DIFF_VEC:
2045 return 0;
2047 case UNSPEC_VOLATILE:
2048 return 1;
2050 case MEM:
2051 case ASM_INPUT:
2052 case ASM_OPERANDS:
2053 if (MEM_VOLATILE_P (x))
2054 return 1;
2056 default:
2057 break;
2060 /* Recursively scan the operands of this expression. */
2063 const char *fmt = GET_RTX_FORMAT (code);
2064 int i;
2066 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2068 if (fmt[i] == 'e')
2070 if (volatile_refs_p (XEXP (x, i)))
2071 return 1;
2073 else if (fmt[i] == 'E')
2075 int j;
2076 for (j = 0; j < XVECLEN (x, i); j++)
2077 if (volatile_refs_p (XVECEXP (x, i, j)))
2078 return 1;
2082 return 0;
2085 /* Similar to above, except that it also rejects register pre- and post-
2086 incrementing. */
2089 side_effects_p (rtx x)
2091 RTX_CODE code;
2093 code = GET_CODE (x);
2094 switch (code)
2096 case LABEL_REF:
2097 case SYMBOL_REF:
2098 case CONST_INT:
2099 case CONST:
2100 case CONST_DOUBLE:
2101 case CONST_VECTOR:
2102 case CC0:
2103 case PC:
2104 case REG:
2105 case SCRATCH:
2106 case ADDR_VEC:
2107 case ADDR_DIFF_VEC:
2108 return 0;
2110 case CLOBBER:
2111 /* Reject CLOBBER with a non-VOID mode. These are made by combine.c
2112 when some combination can't be done. If we see one, don't think
2113 that we can simplify the expression. */
2114 return (GET_MODE (x) != VOIDmode);
2116 case PRE_INC:
2117 case PRE_DEC:
2118 case POST_INC:
2119 case POST_DEC:
2120 case PRE_MODIFY:
2121 case POST_MODIFY:
2122 case CALL:
2123 case UNSPEC_VOLATILE:
2124 /* case TRAP_IF: This isn't clear yet. */
2125 return 1;
2127 case MEM:
2128 case ASM_INPUT:
2129 case ASM_OPERANDS:
2130 if (MEM_VOLATILE_P (x))
2131 return 1;
2133 default:
2134 break;
2137 /* Recursively scan the operands of this expression. */
2140 const char *fmt = GET_RTX_FORMAT (code);
2141 int i;
2143 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2145 if (fmt[i] == 'e')
2147 if (side_effects_p (XEXP (x, i)))
2148 return 1;
2150 else if (fmt[i] == 'E')
2152 int j;
2153 for (j = 0; j < XVECLEN (x, i); j++)
2154 if (side_effects_p (XVECEXP (x, i, j)))
2155 return 1;
2159 return 0;
2162 /* Return nonzero if evaluating rtx X might cause a trap. */
2165 may_trap_p (rtx x)
2167 int i;
2168 enum rtx_code code;
2169 const char *fmt;
2171 if (x == 0)
2172 return 0;
2173 code = GET_CODE (x);
2174 switch (code)
2176 /* Handle these cases quickly. */
2177 case CONST_INT:
2178 case CONST_DOUBLE:
2179 case CONST_VECTOR:
2180 case SYMBOL_REF:
2181 case LABEL_REF:
2182 case CONST:
2183 case PC:
2184 case CC0:
2185 case REG:
2186 case SCRATCH:
2187 return 0;
2189 case ASM_INPUT:
2190 case UNSPEC_VOLATILE:
2191 case TRAP_IF:
2192 return 1;
2194 case ASM_OPERANDS:
2195 return MEM_VOLATILE_P (x);
2197 /* Memory ref can trap unless it's a static var or a stack slot. */
2198 case MEM:
2199 if (MEM_NOTRAP_P (x))
2200 return 0;
2201 return rtx_addr_can_trap_p (XEXP (x, 0));
2203 /* Division by a non-constant might trap. */
2204 case DIV:
2205 case MOD:
2206 case UDIV:
2207 case UMOD:
2208 if (HONOR_SNANS (GET_MODE (x)))
2209 return 1;
2210 if (! CONSTANT_P (XEXP (x, 1))
2211 || (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
2212 && flag_trapping_math))
2213 return 1;
2214 if (XEXP (x, 1) == const0_rtx)
2215 return 1;
2216 break;
2218 case EXPR_LIST:
2219 /* An EXPR_LIST is used to represent a function call. This
2220 certainly may trap. */
2221 return 1;
2223 case GE:
2224 case GT:
2225 case LE:
2226 case LT:
2227 case LTGT:
2228 case COMPARE:
2229 /* Some floating point comparisons may trap. */
2230 if (!flag_trapping_math)
2231 break;
2232 /* ??? There is no machine independent way to check for tests that trap
2233 when COMPARE is used, though many targets do make this distinction.
2234 For instance, sparc uses CCFPE for compares which generate exceptions
2235 and CCFP for compares which do not generate exceptions. */
2236 if (HONOR_NANS (GET_MODE (x)))
2237 return 1;
2238 /* But often the compare has some CC mode, so check operand
2239 modes as well. */
2240 if (HONOR_NANS (GET_MODE (XEXP (x, 0)))
2241 || HONOR_NANS (GET_MODE (XEXP (x, 1))))
2242 return 1;
2243 break;
2245 case EQ:
2246 case NE:
2247 if (HONOR_SNANS (GET_MODE (x)))
2248 return 1;
2249 /* Often comparison is CC mode, so check operand modes. */
2250 if (HONOR_SNANS (GET_MODE (XEXP (x, 0)))
2251 || HONOR_SNANS (GET_MODE (XEXP (x, 1))))
2252 return 1;
2253 break;
2255 case FIX:
2256 /* Conversion of floating point might trap. */
2257 if (flag_trapping_math && HONOR_NANS (GET_MODE (XEXP (x, 0))))
2258 return 1;
2259 break;
2261 case NEG:
2262 case ABS:
2263 /* These operations don't trap even with floating point. */
2264 break;
2266 default:
2267 /* Any floating arithmetic may trap. */
2268 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
2269 && flag_trapping_math)
2270 return 1;
2273 fmt = GET_RTX_FORMAT (code);
2274 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2276 if (fmt[i] == 'e')
2278 if (may_trap_p (XEXP (x, i)))
2279 return 1;
2281 else if (fmt[i] == 'E')
2283 int j;
2284 for (j = 0; j < XVECLEN (x, i); j++)
2285 if (may_trap_p (XVECEXP (x, i, j)))
2286 return 1;
2289 return 0;
2292 /* Return nonzero if X contains a comparison that is not either EQ or NE,
2293 i.e., an inequality. */
2296 inequality_comparisons_p (rtx x)
2298 const char *fmt;
2299 int len, i;
2300 enum rtx_code code = GET_CODE (x);
2302 switch (code)
2304 case REG:
2305 case SCRATCH:
2306 case PC:
2307 case CC0:
2308 case CONST_INT:
2309 case CONST_DOUBLE:
2310 case CONST_VECTOR:
2311 case CONST:
2312 case LABEL_REF:
2313 case SYMBOL_REF:
2314 return 0;
2316 case LT:
2317 case LTU:
2318 case GT:
2319 case GTU:
2320 case LE:
2321 case LEU:
2322 case GE:
2323 case GEU:
2324 return 1;
2326 default:
2327 break;
2330 len = GET_RTX_LENGTH (code);
2331 fmt = GET_RTX_FORMAT (code);
2333 for (i = 0; i < len; i++)
2335 if (fmt[i] == 'e')
2337 if (inequality_comparisons_p (XEXP (x, i)))
2338 return 1;
2340 else if (fmt[i] == 'E')
2342 int j;
2343 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2344 if (inequality_comparisons_p (XVECEXP (x, i, j)))
2345 return 1;
2349 return 0;
2352 /* Replace any occurrence of FROM in X with TO. The function does
2353 not enter into CONST_DOUBLE for the replace.
2355 Note that copying is not done so X must not be shared unless all copies
2356 are to be modified. */
2359 replace_rtx (rtx x, rtx from, rtx to)
2361 int i, j;
2362 const char *fmt;
2364 /* The following prevents loops occurrence when we change MEM in
2365 CONST_DOUBLE onto the same CONST_DOUBLE. */
2366 if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
2367 return x;
2369 if (x == from)
2370 return to;
2372 /* Allow this function to make replacements in EXPR_LISTs. */
2373 if (x == 0)
2374 return 0;
2376 if (GET_CODE (x) == SUBREG)
2378 rtx new = replace_rtx (SUBREG_REG (x), from, to);
2380 if (GET_CODE (new) == CONST_INT)
2382 x = simplify_subreg (GET_MODE (x), new,
2383 GET_MODE (SUBREG_REG (x)),
2384 SUBREG_BYTE (x));
2385 gcc_assert (x);
2387 else
2388 SUBREG_REG (x) = new;
2390 return x;
2392 else if (GET_CODE (x) == ZERO_EXTEND)
2394 rtx new = replace_rtx (XEXP (x, 0), from, to);
2396 if (GET_CODE (new) == CONST_INT)
2398 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
2399 new, GET_MODE (XEXP (x, 0)));
2400 gcc_assert (x);
2402 else
2403 XEXP (x, 0) = new;
2405 return x;
2408 fmt = GET_RTX_FORMAT (GET_CODE (x));
2409 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2411 if (fmt[i] == 'e')
2412 XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
2413 else if (fmt[i] == 'E')
2414 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2415 XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
2418 return x;
2421 /* Throughout the rtx X, replace many registers according to REG_MAP.
2422 Return the replacement for X (which may be X with altered contents).
2423 REG_MAP[R] is the replacement for register R, or 0 for don't replace.
2424 NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
2426 We only support REG_MAP entries of REG or SUBREG. Also, hard registers
2427 should not be mapped to pseudos or vice versa since validate_change
2428 is not called.
2430 If REPLACE_DEST is 1, replacements are also done in destinations;
2431 otherwise, only sources are replaced. */
2434 replace_regs (rtx x, rtx *reg_map, unsigned int nregs, int replace_dest)
2436 enum rtx_code code;
2437 int i;
2438 const char *fmt;
2440 if (x == 0)
2441 return x;
2443 code = GET_CODE (x);
2444 switch (code)
2446 case SCRATCH:
2447 case PC:
2448 case CC0:
2449 case CONST_INT:
2450 case CONST_DOUBLE:
2451 case CONST_VECTOR:
2452 case CONST:
2453 case SYMBOL_REF:
2454 case LABEL_REF:
2455 return x;
2457 case REG:
2458 /* Verify that the register has an entry before trying to access it. */
2459 if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
2461 /* SUBREGs can't be shared. Always return a copy to ensure that if
2462 this replacement occurs more than once then each instance will
2463 get distinct rtx. */
2464 if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
2465 return copy_rtx (reg_map[REGNO (x)]);
2466 return reg_map[REGNO (x)];
2468 return x;
2470 case SUBREG:
2471 /* Prevent making nested SUBREGs. */
2472 if (REG_P (SUBREG_REG (x)) && REGNO (SUBREG_REG (x)) < nregs
2473 && reg_map[REGNO (SUBREG_REG (x))] != 0
2474 && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
2476 rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
2477 return simplify_gen_subreg (GET_MODE (x), map_val,
2478 GET_MODE (SUBREG_REG (x)),
2479 SUBREG_BYTE (x));
2481 break;
2483 case SET:
2484 if (replace_dest)
2485 SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
2487 else if (MEM_P (SET_DEST (x))
2488 || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2489 /* Even if we are not to replace destinations, replace register if it
2490 is CONTAINED in destination (destination is memory or
2491 STRICT_LOW_PART). */
2492 XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
2493 reg_map, nregs, 0);
2494 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2495 /* Similarly, for ZERO_EXTRACT we replace all operands. */
2496 break;
2498 SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2499 return x;
2501 default:
2502 break;
2505 fmt = GET_RTX_FORMAT (code);
2506 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2508 if (fmt[i] == 'e')
2509 XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
2510 else if (fmt[i] == 'E')
2512 int j;
2513 for (j = 0; j < XVECLEN (x, i); j++)
2514 XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2515 nregs, replace_dest);
2518 return x;
2521 /* Replace occurrences of the old label in *X with the new one.
2522 DATA is a REPLACE_LABEL_DATA containing the old and new labels. */
2525 replace_label (rtx *x, void *data)
2527 rtx l = *x;
2528 rtx old_label = ((replace_label_data *) data)->r1;
2529 rtx new_label = ((replace_label_data *) data)->r2;
2530 bool update_label_nuses = ((replace_label_data *) data)->update_label_nuses;
2532 if (l == NULL_RTX)
2533 return 0;
2535 if (GET_CODE (l) == SYMBOL_REF
2536 && CONSTANT_POOL_ADDRESS_P (l))
2538 rtx c = get_pool_constant (l);
2539 if (rtx_referenced_p (old_label, c))
2541 rtx new_c, new_l;
2542 replace_label_data *d = (replace_label_data *) data;
2544 /* Create a copy of constant C; replace the label inside
2545 but do not update LABEL_NUSES because uses in constant pool
2546 are not counted. */
2547 new_c = copy_rtx (c);
2548 d->update_label_nuses = false;
2549 for_each_rtx (&new_c, replace_label, data);
2550 d->update_label_nuses = update_label_nuses;
2552 /* Add the new constant NEW_C to constant pool and replace
2553 the old reference to constant by new reference. */
2554 new_l = XEXP (force_const_mem (get_pool_mode (l), new_c), 0);
2555 *x = replace_rtx (l, l, new_l);
2557 return 0;
2560 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
2561 field. This is not handled by for_each_rtx because it doesn't
2562 handle unprinted ('0') fields. */
2563 if (JUMP_P (l) && JUMP_LABEL (l) == old_label)
2564 JUMP_LABEL (l) = new_label;
2566 if ((GET_CODE (l) == LABEL_REF
2567 || GET_CODE (l) == INSN_LIST)
2568 && XEXP (l, 0) == old_label)
2570 XEXP (l, 0) = new_label;
2571 if (update_label_nuses)
2573 ++LABEL_NUSES (new_label);
2574 --LABEL_NUSES (old_label);
2576 return 0;
2579 return 0;
2582 /* When *BODY is equal to X or X is directly referenced by *BODY
2583 return nonzero, thus FOR_EACH_RTX stops traversing and returns nonzero
2584 too, otherwise FOR_EACH_RTX continues traversing *BODY. */
2586 static int
2587 rtx_referenced_p_1 (rtx *body, void *x)
2589 rtx y = (rtx) x;
2591 if (*body == NULL_RTX)
2592 return y == NULL_RTX;
2594 /* Return true if a label_ref *BODY refers to label Y. */
2595 if (GET_CODE (*body) == LABEL_REF && LABEL_P (y))
2596 return XEXP (*body, 0) == y;
2598 /* If *BODY is a reference to pool constant traverse the constant. */
2599 if (GET_CODE (*body) == SYMBOL_REF
2600 && CONSTANT_POOL_ADDRESS_P (*body))
2601 return rtx_referenced_p (y, get_pool_constant (*body));
2603 /* By default, compare the RTL expressions. */
2604 return rtx_equal_p (*body, y);
2607 /* Return true if X is referenced in BODY. */
2610 rtx_referenced_p (rtx x, rtx body)
2612 return for_each_rtx (&body, rtx_referenced_p_1, x);
2615 /* If INSN is a tablejump return true and store the label (before jump table) to
2616 *LABELP and the jump table to *TABLEP. LABELP and TABLEP may be NULL. */
2618 bool
2619 tablejump_p (rtx insn, rtx *labelp, rtx *tablep)
2621 rtx label, table;
2623 if (JUMP_P (insn)
2624 && (label = JUMP_LABEL (insn)) != NULL_RTX
2625 && (table = next_active_insn (label)) != NULL_RTX
2626 && JUMP_P (table)
2627 && (GET_CODE (PATTERN (table)) == ADDR_VEC
2628 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
2630 if (labelp)
2631 *labelp = label;
2632 if (tablep)
2633 *tablep = table;
2634 return true;
2636 return false;
2639 /* A subroutine of computed_jump_p, return 1 if X contains a REG or MEM or
2640 constant that is not in the constant pool and not in the condition
2641 of an IF_THEN_ELSE. */
2643 static int
2644 computed_jump_p_1 (rtx x)
2646 enum rtx_code code = GET_CODE (x);
2647 int i, j;
2648 const char *fmt;
2650 switch (code)
2652 case LABEL_REF:
2653 case PC:
2654 return 0;
2656 case CONST:
2657 case CONST_INT:
2658 case CONST_DOUBLE:
2659 case CONST_VECTOR:
2660 case SYMBOL_REF:
2661 case REG:
2662 return 1;
2664 case MEM:
2665 return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2666 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2668 case IF_THEN_ELSE:
2669 return (computed_jump_p_1 (XEXP (x, 1))
2670 || computed_jump_p_1 (XEXP (x, 2)));
2672 default:
2673 break;
2676 fmt = GET_RTX_FORMAT (code);
2677 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2679 if (fmt[i] == 'e'
2680 && computed_jump_p_1 (XEXP (x, i)))
2681 return 1;
2683 else if (fmt[i] == 'E')
2684 for (j = 0; j < XVECLEN (x, i); j++)
2685 if (computed_jump_p_1 (XVECEXP (x, i, j)))
2686 return 1;
2689 return 0;
2692 /* Return nonzero if INSN is an indirect jump (aka computed jump).
2694 Tablejumps and casesi insns are not considered indirect jumps;
2695 we can recognize them by a (use (label_ref)). */
2698 computed_jump_p (rtx insn)
2700 int i;
2701 if (JUMP_P (insn))
2703 rtx pat = PATTERN (insn);
2705 if (find_reg_note (insn, REG_LABEL, NULL_RTX))
2706 return 0;
2707 else if (GET_CODE (pat) == PARALLEL)
2709 int len = XVECLEN (pat, 0);
2710 int has_use_labelref = 0;
2712 for (i = len - 1; i >= 0; i--)
2713 if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2714 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2715 == LABEL_REF))
2716 has_use_labelref = 1;
2718 if (! has_use_labelref)
2719 for (i = len - 1; i >= 0; i--)
2720 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2721 && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2722 && computed_jump_p_1 (SET_SRC (XVECEXP (pat, 0, i))))
2723 return 1;
2725 else if (GET_CODE (pat) == SET
2726 && SET_DEST (pat) == pc_rtx
2727 && computed_jump_p_1 (SET_SRC (pat)))
2728 return 1;
2730 return 0;
2733 /* Traverse X via depth-first search, calling F for each
2734 sub-expression (including X itself). F is also passed the DATA.
2735 If F returns -1, do not traverse sub-expressions, but continue
2736 traversing the rest of the tree. If F ever returns any other
2737 nonzero value, stop the traversal, and return the value returned
2738 by F. Otherwise, return 0. This function does not traverse inside
2739 tree structure that contains RTX_EXPRs, or into sub-expressions
2740 whose format code is `0' since it is not known whether or not those
2741 codes are actually RTL.
2743 This routine is very general, and could (should?) be used to
2744 implement many of the other routines in this file. */
2747 for_each_rtx (rtx *x, rtx_function f, void *data)
2749 int result;
2750 int length;
2751 const char *format;
2752 int i;
2754 /* Call F on X. */
2755 result = (*f) (x, data);
2756 if (result == -1)
2757 /* Do not traverse sub-expressions. */
2758 return 0;
2759 else if (result != 0)
2760 /* Stop the traversal. */
2761 return result;
2763 if (*x == NULL_RTX)
2764 /* There are no sub-expressions. */
2765 return 0;
2767 length = GET_RTX_LENGTH (GET_CODE (*x));
2768 format = GET_RTX_FORMAT (GET_CODE (*x));
2770 for (i = 0; i < length; ++i)
2772 switch (format[i])
2774 case 'e':
2775 result = for_each_rtx (&XEXP (*x, i), f, data);
2776 if (result != 0)
2777 return result;
2778 break;
2780 case 'V':
2781 case 'E':
2782 if (XVEC (*x, i) != 0)
2784 int j;
2785 for (j = 0; j < XVECLEN (*x, i); ++j)
2787 result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2788 if (result != 0)
2789 return result;
2792 break;
2794 default:
2795 /* Nothing to do. */
2796 break;
2801 return 0;
2804 /* Searches X for any reference to REGNO, returning the rtx of the
2805 reference found if any. Otherwise, returns NULL_RTX. */
2808 regno_use_in (unsigned int regno, rtx x)
2810 const char *fmt;
2811 int i, j;
2812 rtx tem;
2814 if (REG_P (x) && REGNO (x) == regno)
2815 return x;
2817 fmt = GET_RTX_FORMAT (GET_CODE (x));
2818 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2820 if (fmt[i] == 'e')
2822 if ((tem = regno_use_in (regno, XEXP (x, i))))
2823 return tem;
2825 else if (fmt[i] == 'E')
2826 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2827 if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2828 return tem;
2831 return NULL_RTX;
2834 /* Return a value indicating whether OP, an operand of a commutative
2835 operation, is preferred as the first or second operand. The higher
2836 the value, the stronger the preference for being the first operand.
2837 We use negative values to indicate a preference for the first operand
2838 and positive values for the second operand. */
2841 commutative_operand_precedence (rtx op)
2843 enum rtx_code code = GET_CODE (op);
2845 /* Constants always come the second operand. Prefer "nice" constants. */
2846 if (code == CONST_INT)
2847 return -7;
2848 if (code == CONST_DOUBLE)
2849 return -6;
2850 op = avoid_constant_pool_reference (op);
2851 code = GET_CODE (op);
2853 switch (GET_RTX_CLASS (code))
2855 case RTX_CONST_OBJ:
2856 if (code == CONST_INT)
2857 return -5;
2858 if (code == CONST_DOUBLE)
2859 return -4;
2860 return -3;
2862 case RTX_EXTRA:
2863 /* SUBREGs of objects should come second. */
2864 if (code == SUBREG && OBJECT_P (SUBREG_REG (op)))
2865 return -2;
2867 if (!CONSTANT_P (op))
2868 return 0;
2869 else
2870 /* As for RTX_CONST_OBJ. */
2871 return -3;
2873 case RTX_OBJ:
2874 /* Complex expressions should be the first, so decrease priority
2875 of objects. */
2876 return -1;
2878 case RTX_COMM_ARITH:
2879 /* Prefer operands that are themselves commutative to be first.
2880 This helps to make things linear. In particular,
2881 (and (and (reg) (reg)) (not (reg))) is canonical. */
2882 return 4;
2884 case RTX_BIN_ARITH:
2885 /* If only one operand is a binary expression, it will be the first
2886 operand. In particular, (plus (minus (reg) (reg)) (neg (reg)))
2887 is canonical, although it will usually be further simplified. */
2888 return 2;
2890 case RTX_UNARY:
2891 /* Then prefer NEG and NOT. */
2892 if (code == NEG || code == NOT)
2893 return 1;
2895 default:
2896 return 0;
2900 /* Return 1 iff it is necessary to swap operands of commutative operation
2901 in order to canonicalize expression. */
2904 swap_commutative_operands_p (rtx x, rtx y)
2906 return (commutative_operand_precedence (x)
2907 < commutative_operand_precedence (y));
2910 /* Return 1 if X is an autoincrement side effect and the register is
2911 not the stack pointer. */
2913 auto_inc_p (rtx x)
2915 switch (GET_CODE (x))
2917 case PRE_INC:
2918 case POST_INC:
2919 case PRE_DEC:
2920 case POST_DEC:
2921 case PRE_MODIFY:
2922 case POST_MODIFY:
2923 /* There are no REG_INC notes for SP. */
2924 if (XEXP (x, 0) != stack_pointer_rtx)
2925 return 1;
2926 default:
2927 break;
2929 return 0;
2932 /* Return 1 if the sequence of instructions beginning with FROM and up
2933 to and including TO is safe to move. If NEW_TO is non-NULL, and
2934 the sequence is not already safe to move, but can be easily
2935 extended to a sequence which is safe, then NEW_TO will point to the
2936 end of the extended sequence.
2938 For now, this function only checks that the region contains whole
2939 exception regions, but it could be extended to check additional
2940 conditions as well. */
2943 insns_safe_to_move_p (rtx from, rtx to, rtx *new_to)
2945 int eh_region_count = 0;
2946 int past_to_p = 0;
2947 rtx r = from;
2949 /* By default, assume the end of the region will be what was
2950 suggested. */
2951 if (new_to)
2952 *new_to = to;
2954 while (r)
2956 if (NOTE_P (r))
2958 switch (NOTE_LINE_NUMBER (r))
2960 case NOTE_INSN_EH_REGION_BEG:
2961 ++eh_region_count;
2962 break;
2964 case NOTE_INSN_EH_REGION_END:
2965 if (eh_region_count == 0)
2966 /* This sequence of instructions contains the end of
2967 an exception region, but not he beginning. Moving
2968 it will cause chaos. */
2969 return 0;
2971 --eh_region_count;
2972 break;
2974 default:
2975 break;
2978 else if (past_to_p)
2979 /* If we've passed TO, and we see a non-note instruction, we
2980 can't extend the sequence to a movable sequence. */
2981 return 0;
2983 if (r == to)
2985 if (!new_to)
2986 /* It's OK to move the sequence if there were matched sets of
2987 exception region notes. */
2988 return eh_region_count == 0;
2990 past_to_p = 1;
2993 /* It's OK to move the sequence if there were matched sets of
2994 exception region notes. */
2995 if (past_to_p && eh_region_count == 0)
2997 *new_to = r;
2998 return 1;
3001 /* Go to the next instruction. */
3002 r = NEXT_INSN (r);
3005 return 0;
3008 /* Return nonzero if IN contains a piece of rtl that has the address LOC. */
3010 loc_mentioned_in_p (rtx *loc, rtx in)
3012 enum rtx_code code = GET_CODE (in);
3013 const char *fmt = GET_RTX_FORMAT (code);
3014 int i, j;
3016 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3018 if (loc == &in->u.fld[i].rt_rtx)
3019 return 1;
3020 if (fmt[i] == 'e')
3022 if (loc_mentioned_in_p (loc, XEXP (in, i)))
3023 return 1;
3025 else if (fmt[i] == 'E')
3026 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
3027 if (loc_mentioned_in_p (loc, XVECEXP (in, i, j)))
3028 return 1;
3030 return 0;
3033 /* Helper function for subreg_lsb. Given a subreg's OUTER_MODE, INNER_MODE,
3034 and SUBREG_BYTE, return the bit offset where the subreg begins
3035 (counting from the least significant bit of the operand). */
3037 unsigned int
3038 subreg_lsb_1 (enum machine_mode outer_mode,
3039 enum machine_mode inner_mode,
3040 unsigned int subreg_byte)
3042 unsigned int bitpos;
3043 unsigned int byte;
3044 unsigned int word;
3046 /* A paradoxical subreg begins at bit position 0. */
3047 if (GET_MODE_BITSIZE (outer_mode) > GET_MODE_BITSIZE (inner_mode))
3048 return 0;
3050 if (WORDS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
3051 /* If the subreg crosses a word boundary ensure that
3052 it also begins and ends on a word boundary. */
3053 gcc_assert (!((subreg_byte % UNITS_PER_WORD
3054 + GET_MODE_SIZE (outer_mode)) > UNITS_PER_WORD
3055 && (subreg_byte % UNITS_PER_WORD
3056 || GET_MODE_SIZE (outer_mode) % UNITS_PER_WORD)));
3058 if (WORDS_BIG_ENDIAN)
3059 word = (GET_MODE_SIZE (inner_mode)
3060 - (subreg_byte + GET_MODE_SIZE (outer_mode))) / UNITS_PER_WORD;
3061 else
3062 word = subreg_byte / UNITS_PER_WORD;
3063 bitpos = word * BITS_PER_WORD;
3065 if (BYTES_BIG_ENDIAN)
3066 byte = (GET_MODE_SIZE (inner_mode)
3067 - (subreg_byte + GET_MODE_SIZE (outer_mode))) % UNITS_PER_WORD;
3068 else
3069 byte = subreg_byte % UNITS_PER_WORD;
3070 bitpos += byte * BITS_PER_UNIT;
3072 return bitpos;
3075 /* Given a subreg X, return the bit offset where the subreg begins
3076 (counting from the least significant bit of the reg). */
3078 unsigned int
3079 subreg_lsb (rtx x)
3081 return subreg_lsb_1 (GET_MODE (x), GET_MODE (SUBREG_REG (x)),
3082 SUBREG_BYTE (x));
3085 /* This function returns the regno offset of a subreg expression.
3086 xregno - A regno of an inner hard subreg_reg (or what will become one).
3087 xmode - The mode of xregno.
3088 offset - The byte offset.
3089 ymode - The mode of a top level SUBREG (or what may become one).
3090 RETURN - The regno offset which would be used. */
3091 unsigned int
3092 subreg_regno_offset (unsigned int xregno, enum machine_mode xmode,
3093 unsigned int offset, enum machine_mode ymode)
3095 int nregs_xmode, nregs_ymode;
3096 int mode_multiple, nregs_multiple;
3097 int y_offset;
3099 gcc_assert (xregno < FIRST_PSEUDO_REGISTER);
3101 nregs_xmode = hard_regno_nregs[xregno][xmode];
3102 nregs_ymode = hard_regno_nregs[xregno][ymode];
3104 /* If this is a big endian paradoxical subreg, which uses more actual
3105 hard registers than the original register, we must return a negative
3106 offset so that we find the proper highpart of the register. */
3107 if (offset == 0
3108 && nregs_ymode > nregs_xmode
3109 && (GET_MODE_SIZE (ymode) > UNITS_PER_WORD
3110 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
3111 return nregs_xmode - nregs_ymode;
3113 if (offset == 0 || nregs_xmode == nregs_ymode)
3114 return 0;
3116 /* size of ymode must not be greater than the size of xmode. */
3117 mode_multiple = GET_MODE_SIZE (xmode) / GET_MODE_SIZE (ymode);
3118 gcc_assert (mode_multiple != 0);
3120 y_offset = offset / GET_MODE_SIZE (ymode);
3121 nregs_multiple = nregs_xmode / nregs_ymode;
3122 return (y_offset / (mode_multiple / nregs_multiple)) * nregs_ymode;
3125 /* This function returns true when the offset is representable via
3126 subreg_offset in the given regno.
3127 xregno - A regno of an inner hard subreg_reg (or what will become one).
3128 xmode - The mode of xregno.
3129 offset - The byte offset.
3130 ymode - The mode of a top level SUBREG (or what may become one).
3131 RETURN - The regno offset which would be used. */
3132 bool
3133 subreg_offset_representable_p (unsigned int xregno, enum machine_mode xmode,
3134 unsigned int offset, enum machine_mode ymode)
3136 int nregs_xmode, nregs_ymode;
3137 int mode_multiple, nregs_multiple;
3138 int y_offset;
3140 gcc_assert (xregno < FIRST_PSEUDO_REGISTER);
3142 nregs_xmode = hard_regno_nregs[xregno][xmode];
3143 nregs_ymode = hard_regno_nregs[xregno][ymode];
3145 /* Paradoxical subregs are always valid. */
3146 if (offset == 0
3147 && nregs_ymode > nregs_xmode
3148 && (GET_MODE_SIZE (ymode) > UNITS_PER_WORD
3149 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
3150 return true;
3152 /* Lowpart subregs are always valid. */
3153 if (offset == subreg_lowpart_offset (ymode, xmode))
3154 return true;
3156 /* This should always pass, otherwise we don't know how to verify the
3157 constraint. These conditions may be relaxed but subreg_offset would
3158 need to be redesigned. */
3159 gcc_assert ((GET_MODE_SIZE (xmode) % GET_MODE_SIZE (ymode)) == 0);
3160 gcc_assert ((GET_MODE_SIZE (ymode) % nregs_ymode) == 0);
3161 gcc_assert ((nregs_xmode % nregs_ymode) == 0);
3163 /* The XMODE value can be seen as a vector of NREGS_XMODE
3164 values. The subreg must represent a lowpart of given field.
3165 Compute what field it is. */
3166 offset -= subreg_lowpart_offset (ymode,
3167 mode_for_size (GET_MODE_BITSIZE (xmode)
3168 / nregs_xmode,
3169 MODE_INT, 0));
3171 /* size of ymode must not be greater than the size of xmode. */
3172 mode_multiple = GET_MODE_SIZE (xmode) / GET_MODE_SIZE (ymode);
3173 gcc_assert (mode_multiple != 0);
3175 y_offset = offset / GET_MODE_SIZE (ymode);
3176 nregs_multiple = nregs_xmode / nregs_ymode;
3178 gcc_assert ((offset % GET_MODE_SIZE (ymode)) == 0);
3179 gcc_assert ((mode_multiple % nregs_multiple) == 0);
3181 return (!(y_offset % (mode_multiple / nregs_multiple)));
3184 /* Return the final regno that a subreg expression refers to. */
3185 unsigned int
3186 subreg_regno (rtx x)
3188 unsigned int ret;
3189 rtx subreg = SUBREG_REG (x);
3190 int regno = REGNO (subreg);
3192 ret = regno + subreg_regno_offset (regno,
3193 GET_MODE (subreg),
3194 SUBREG_BYTE (x),
3195 GET_MODE (x));
3196 return ret;
3199 struct parms_set_data
3201 int nregs;
3202 HARD_REG_SET regs;
3205 /* Helper function for noticing stores to parameter registers. */
3206 static void
3207 parms_set (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
3209 struct parms_set_data *d = data;
3210 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
3211 && TEST_HARD_REG_BIT (d->regs, REGNO (x)))
3213 CLEAR_HARD_REG_BIT (d->regs, REGNO (x));
3214 d->nregs--;
3218 /* Look backward for first parameter to be loaded.
3219 Do not skip BOUNDARY. */
3221 find_first_parameter_load (rtx call_insn, rtx boundary)
3223 struct parms_set_data parm;
3224 rtx p, before;
3226 /* Since different machines initialize their parameter registers
3227 in different orders, assume nothing. Collect the set of all
3228 parameter registers. */
3229 CLEAR_HARD_REG_SET (parm.regs);
3230 parm.nregs = 0;
3231 for (p = CALL_INSN_FUNCTION_USAGE (call_insn); p; p = XEXP (p, 1))
3232 if (GET_CODE (XEXP (p, 0)) == USE
3233 && REG_P (XEXP (XEXP (p, 0), 0)))
3235 gcc_assert (REGNO (XEXP (XEXP (p, 0), 0)) < FIRST_PSEUDO_REGISTER);
3237 /* We only care about registers which can hold function
3238 arguments. */
3239 if (!FUNCTION_ARG_REGNO_P (REGNO (XEXP (XEXP (p, 0), 0))))
3240 continue;
3242 SET_HARD_REG_BIT (parm.regs, REGNO (XEXP (XEXP (p, 0), 0)));
3243 parm.nregs++;
3245 before = call_insn;
3247 /* Search backward for the first set of a register in this set. */
3248 while (parm.nregs && before != boundary)
3250 before = PREV_INSN (before);
3252 /* It is possible that some loads got CSEed from one call to
3253 another. Stop in that case. */
3254 if (CALL_P (before))
3255 break;
3257 /* Our caller needs either ensure that we will find all sets
3258 (in case code has not been optimized yet), or take care
3259 for possible labels in a way by setting boundary to preceding
3260 CODE_LABEL. */
3261 if (LABEL_P (before))
3263 gcc_assert (before == boundary);
3264 break;
3267 if (INSN_P (before))
3268 note_stores (PATTERN (before), parms_set, &parm);
3270 return before;
3273 /* Return true if we should avoid inserting code between INSN and preceding
3274 call instruction. */
3276 bool
3277 keep_with_call_p (rtx insn)
3279 rtx set;
3281 if (INSN_P (insn) && (set = single_set (insn)) != NULL)
3283 if (REG_P (SET_DEST (set))
3284 && REGNO (SET_DEST (set)) < FIRST_PSEUDO_REGISTER
3285 && fixed_regs[REGNO (SET_DEST (set))]
3286 && general_operand (SET_SRC (set), VOIDmode))
3287 return true;
3288 if (REG_P (SET_SRC (set))
3289 && FUNCTION_VALUE_REGNO_P (REGNO (SET_SRC (set)))
3290 && REG_P (SET_DEST (set))
3291 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3292 return true;
3293 /* There may be a stack pop just after the call and before the store
3294 of the return register. Search for the actual store when deciding
3295 if we can break or not. */
3296 if (SET_DEST (set) == stack_pointer_rtx)
3298 rtx i2 = next_nonnote_insn (insn);
3299 if (i2 && keep_with_call_p (i2))
3300 return true;
3303 return false;
3306 /* Return true when store to register X can be hoisted to the place
3307 with LIVE registers (can be NULL). Value VAL contains destination
3308 whose value will be used. */
3310 static bool
3311 hoist_test_store (rtx x, rtx val, regset live)
3313 if (GET_CODE (x) == SCRATCH)
3314 return true;
3316 if (rtx_equal_p (x, val))
3317 return true;
3319 /* Allow subreg of X in case it is not writing just part of multireg pseudo.
3320 Then we would need to update all users to care hoisting the store too.
3321 Caller may represent that by specifying whole subreg as val. */
3323 if (GET_CODE (x) == SUBREG && rtx_equal_p (SUBREG_REG (x), val))
3325 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) > UNITS_PER_WORD
3326 && GET_MODE_BITSIZE (GET_MODE (x)) <
3327 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
3328 return false;
3329 return true;
3331 if (GET_CODE (x) == SUBREG)
3332 x = SUBREG_REG (x);
3334 /* Anything except register store is not hoistable. This includes the
3335 partial stores to registers. */
3337 if (!REG_P (x))
3338 return false;
3340 /* Pseudo registers can be always replaced by another pseudo to avoid
3341 the side effect, for hard register we must ensure that they are dead.
3342 Eventually we may want to add code to try turn pseudos to hards, but it
3343 is unlikely useful. */
3345 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
3347 int regno = REGNO (x);
3348 int n = hard_regno_nregs[regno][GET_MODE (x)];
3350 if (!live)
3351 return false;
3352 if (REGNO_REG_SET_P (live, regno))
3353 return false;
3354 while (--n > 0)
3355 if (REGNO_REG_SET_P (live, regno + n))
3356 return false;
3358 return true;
3362 /* Return true if INSN can be hoisted to place with LIVE hard registers
3363 (LIVE can be NULL when unknown). VAL is expected to be stored by the insn
3364 and used by the hoisting pass. */
3366 bool
3367 can_hoist_insn_p (rtx insn, rtx val, regset live)
3369 rtx pat = PATTERN (insn);
3370 int i;
3372 /* It probably does not worth the complexity to handle multiple
3373 set stores. */
3374 if (!single_set (insn))
3375 return false;
3376 /* We can move CALL_INSN, but we need to check that all caller clobbered
3377 regs are dead. */
3378 if (CALL_P (insn))
3379 return false;
3380 /* In future we will handle hoisting of libcall sequences, but
3381 give up for now. */
3382 if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
3383 return false;
3384 switch (GET_CODE (pat))
3386 case SET:
3387 if (!hoist_test_store (SET_DEST (pat), val, live))
3388 return false;
3389 break;
3390 case USE:
3391 /* USES do have sick semantics, so do not move them. */
3392 return false;
3393 break;
3394 case CLOBBER:
3395 if (!hoist_test_store (XEXP (pat, 0), val, live))
3396 return false;
3397 break;
3398 case PARALLEL:
3399 for (i = 0; i < XVECLEN (pat, 0); i++)
3401 rtx x = XVECEXP (pat, 0, i);
3402 switch (GET_CODE (x))
3404 case SET:
3405 if (!hoist_test_store (SET_DEST (x), val, live))
3406 return false;
3407 break;
3408 case USE:
3409 /* We need to fix callers to really ensure availability
3410 of all values insn uses, but for now it is safe to prohibit
3411 hoisting of any insn having such a hidden uses. */
3412 return false;
3413 break;
3414 case CLOBBER:
3415 if (!hoist_test_store (SET_DEST (x), val, live))
3416 return false;
3417 break;
3418 default:
3419 break;
3422 break;
3423 default:
3424 gcc_unreachable ();
3426 return true;
3429 /* Update store after hoisting - replace all stores to pseudo registers
3430 by new ones to avoid clobbering of values except for store to VAL that will
3431 be updated to NEW. */
3433 static void
3434 hoist_update_store (rtx insn, rtx *xp, rtx val, rtx new)
3436 rtx x = *xp;
3438 if (GET_CODE (x) == SCRATCH)
3439 return;
3441 if (GET_CODE (x) == SUBREG && SUBREG_REG (x) == val)
3442 validate_change (insn, xp,
3443 simplify_gen_subreg (GET_MODE (x), new, GET_MODE (new),
3444 SUBREG_BYTE (x)), 1);
3445 if (rtx_equal_p (x, val))
3447 validate_change (insn, xp, new, 1);
3448 return;
3450 if (GET_CODE (x) == SUBREG)
3452 xp = &SUBREG_REG (x);
3453 x = *xp;
3456 gcc_assert (REG_P (x));
3458 /* We've verified that hard registers are dead, so we may keep the side
3459 effect. Otherwise replace it by new pseudo. */
3460 if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
3461 validate_change (insn, xp, gen_reg_rtx (GET_MODE (x)), 1);
3462 REG_NOTES (insn)
3463 = alloc_EXPR_LIST (REG_UNUSED, *xp, REG_NOTES (insn));
3466 /* Create a copy of INSN after AFTER replacing store of VAL to NEW
3467 and each other side effect to pseudo register by new pseudo register. */
3470 hoist_insn_after (rtx insn, rtx after, rtx val, rtx new)
3472 rtx pat;
3473 int i;
3474 rtx note;
3475 int applied;
3477 insn = emit_copy_of_insn_after (insn, after);
3478 pat = PATTERN (insn);
3480 /* Remove REG_UNUSED notes as we will re-emit them. */
3481 while ((note = find_reg_note (insn, REG_UNUSED, NULL_RTX)))
3482 remove_note (insn, note);
3484 /* To get this working callers must ensure to move everything referenced
3485 by REG_EQUAL/REG_EQUIV notes too. Lets remove them, it is probably
3486 easier. */
3487 while ((note = find_reg_note (insn, REG_EQUAL, NULL_RTX)))
3488 remove_note (insn, note);
3489 while ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)))
3490 remove_note (insn, note);
3492 /* Remove REG_DEAD notes as they might not be valid anymore in case
3493 we create redundancy. */
3494 while ((note = find_reg_note (insn, REG_DEAD, NULL_RTX)))
3495 remove_note (insn, note);
3496 switch (GET_CODE (pat))
3498 case SET:
3499 hoist_update_store (insn, &SET_DEST (pat), val, new);
3500 break;
3501 case USE:
3502 break;
3503 case CLOBBER:
3504 hoist_update_store (insn, &XEXP (pat, 0), val, new);
3505 break;
3506 case PARALLEL:
3507 for (i = 0; i < XVECLEN (pat, 0); i++)
3509 rtx x = XVECEXP (pat, 0, i);
3510 switch (GET_CODE (x))
3512 case SET:
3513 hoist_update_store (insn, &SET_DEST (x), val, new);
3514 break;
3515 case USE:
3516 break;
3517 case CLOBBER:
3518 hoist_update_store (insn, &SET_DEST (x), val, new);
3519 break;
3520 default:
3521 break;
3524 break;
3525 default:
3526 gcc_unreachable ();
3528 applied = apply_change_group ();
3529 gcc_assert (applied);
3531 return insn;
3535 hoist_insn_to_edge (rtx insn, edge e, rtx val, rtx new)
3537 rtx new_insn;
3539 /* We cannot insert instructions on an abnormal critical edge.
3540 It will be easier to find the culprit if we die now. */
3541 gcc_assert (!(e->flags & EDGE_ABNORMAL) || !EDGE_CRITICAL_P (e));
3543 /* Do not use emit_insn_on_edge as we want to preserve notes and similar
3544 stuff. We also emit CALL_INSNS and firends. */
3545 if (e->insns.r == NULL_RTX)
3547 start_sequence ();
3548 emit_note (NOTE_INSN_DELETED);
3550 else
3551 push_to_sequence (e->insns.r);
3553 new_insn = hoist_insn_after (insn, get_last_insn (), val, new);
3555 e->insns.r = get_insns ();
3556 end_sequence ();
3557 return new_insn;
3560 /* Return true if LABEL is a target of JUMP_INSN. This applies only
3561 to non-complex jumps. That is, direct unconditional, conditional,
3562 and tablejumps, but not computed jumps or returns. It also does
3563 not apply to the fallthru case of a conditional jump. */
3565 bool
3566 label_is_jump_target_p (rtx label, rtx jump_insn)
3568 rtx tmp = JUMP_LABEL (jump_insn);
3570 if (label == tmp)
3571 return true;
3573 if (tablejump_p (jump_insn, NULL, &tmp))
3575 rtvec vec = XVEC (PATTERN (tmp),
3576 GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC);
3577 int i, veclen = GET_NUM_ELEM (vec);
3579 for (i = 0; i < veclen; ++i)
3580 if (XEXP (RTVEC_ELT (vec, i), 0) == label)
3581 return true;
3584 return false;
3588 /* Return an estimate of the cost of computing rtx X.
3589 One use is in cse, to decide which expression to keep in the hash table.
3590 Another is in rtl generation, to pick the cheapest way to multiply.
3591 Other uses like the latter are expected in the future. */
3594 rtx_cost (rtx x, enum rtx_code outer_code ATTRIBUTE_UNUSED)
3596 int i, j;
3597 enum rtx_code code;
3598 const char *fmt;
3599 int total;
3601 if (x == 0)
3602 return 0;
3604 /* Compute the default costs of certain things.
3605 Note that targetm.rtx_costs can override the defaults. */
3607 code = GET_CODE (x);
3608 switch (code)
3610 case MULT:
3611 total = COSTS_N_INSNS (5);
3612 break;
3613 case DIV:
3614 case UDIV:
3615 case MOD:
3616 case UMOD:
3617 total = COSTS_N_INSNS (7);
3618 break;
3619 case USE:
3620 /* Used in loop.c and combine.c as a marker. */
3621 total = 0;
3622 break;
3623 default:
3624 total = COSTS_N_INSNS (1);
3627 switch (code)
3629 case REG:
3630 return 0;
3632 case SUBREG:
3633 /* If we can't tie these modes, make this expensive. The larger
3634 the mode, the more expensive it is. */
3635 if (! MODES_TIEABLE_P (GET_MODE (x), GET_MODE (SUBREG_REG (x))))
3636 return COSTS_N_INSNS (2
3637 + GET_MODE_SIZE (GET_MODE (x)) / UNITS_PER_WORD);
3638 break;
3640 default:
3641 if (targetm.rtx_costs (x, code, outer_code, &total))
3642 return total;
3643 break;
3646 /* Sum the costs of the sub-rtx's, plus cost of this operation,
3647 which is already in total. */
3649 fmt = GET_RTX_FORMAT (code);
3650 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3651 if (fmt[i] == 'e')
3652 total += rtx_cost (XEXP (x, i), code);
3653 else if (fmt[i] == 'E')
3654 for (j = 0; j < XVECLEN (x, i); j++)
3655 total += rtx_cost (XVECEXP (x, i, j), code);
3657 return total;
3660 /* Return cost of address expression X.
3661 Expect that X is properly formed address reference. */
3664 address_cost (rtx x, enum machine_mode mode)
3666 /* We may be asked for cost of various unusual addresses, such as operands
3667 of push instruction. It is not worthwhile to complicate writing
3668 of the target hook by such cases. */
3670 if (!memory_address_p (mode, x))
3671 return 1000;
3673 return targetm.address_cost (x);
3676 /* If the target doesn't override, compute the cost as with arithmetic. */
3679 default_address_cost (rtx x)
3681 return rtx_cost (x, MEM);
3685 unsigned HOST_WIDE_INT
3686 nonzero_bits (rtx x, enum machine_mode mode)
3688 return cached_nonzero_bits (x, mode, NULL_RTX, VOIDmode, 0);
3691 unsigned int
3692 num_sign_bit_copies (rtx x, enum machine_mode mode)
3694 return cached_num_sign_bit_copies (x, mode, NULL_RTX, VOIDmode, 0);
3697 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
3698 It avoids exponential behavior in nonzero_bits1 when X has
3699 identical subexpressions on the first or the second level. */
3701 static unsigned HOST_WIDE_INT
3702 cached_nonzero_bits (rtx x, enum machine_mode mode, rtx known_x,
3703 enum machine_mode known_mode,
3704 unsigned HOST_WIDE_INT known_ret)
3706 if (x == known_x && mode == known_mode)
3707 return known_ret;
3709 /* Try to find identical subexpressions. If found call
3710 nonzero_bits1 on X with the subexpressions as KNOWN_X and the
3711 precomputed value for the subexpression as KNOWN_RET. */
3713 if (ARITHMETIC_P (x))
3715 rtx x0 = XEXP (x, 0);
3716 rtx x1 = XEXP (x, 1);
3718 /* Check the first level. */
3719 if (x0 == x1)
3720 return nonzero_bits1 (x, mode, x0, mode,
3721 cached_nonzero_bits (x0, mode, known_x,
3722 known_mode, known_ret));
3724 /* Check the second level. */
3725 if (ARITHMETIC_P (x0)
3726 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
3727 return nonzero_bits1 (x, mode, x1, mode,
3728 cached_nonzero_bits (x1, mode, known_x,
3729 known_mode, known_ret));
3731 if (ARITHMETIC_P (x1)
3732 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
3733 return nonzero_bits1 (x, mode, x0, mode,
3734 cached_nonzero_bits (x0, mode, known_x,
3735 known_mode, known_ret));
3738 return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
3741 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
3742 We don't let nonzero_bits recur into num_sign_bit_copies, because that
3743 is less useful. We can't allow both, because that results in exponential
3744 run time recursion. There is a nullstone testcase that triggered
3745 this. This macro avoids accidental uses of num_sign_bit_copies. */
3746 #define cached_num_sign_bit_copies sorry_i_am_preventing_exponential_behavior
3748 /* Given an expression, X, compute which bits in X can be nonzero.
3749 We don't care about bits outside of those defined in MODE.
3751 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
3752 an arithmetic operation, we can do better. */
3754 static unsigned HOST_WIDE_INT
3755 nonzero_bits1 (rtx x, enum machine_mode mode, rtx known_x,
3756 enum machine_mode known_mode,
3757 unsigned HOST_WIDE_INT known_ret)
3759 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
3760 unsigned HOST_WIDE_INT inner_nz;
3761 enum rtx_code code;
3762 unsigned int mode_width = GET_MODE_BITSIZE (mode);
3764 /* For floating-point values, assume all bits are needed. */
3765 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
3766 return nonzero;
3768 /* If X is wider than MODE, use its mode instead. */
3769 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
3771 mode = GET_MODE (x);
3772 nonzero = GET_MODE_MASK (mode);
3773 mode_width = GET_MODE_BITSIZE (mode);
3776 if (mode_width > HOST_BITS_PER_WIDE_INT)
3777 /* Our only callers in this case look for single bit values. So
3778 just return the mode mask. Those tests will then be false. */
3779 return nonzero;
3781 #ifndef WORD_REGISTER_OPERATIONS
3782 /* If MODE is wider than X, but both are a single word for both the host
3783 and target machines, we can compute this from which bits of the
3784 object might be nonzero in its own mode, taking into account the fact
3785 that on many CISC machines, accessing an object in a wider mode
3786 causes the high-order bits to become undefined. So they are
3787 not known to be zero. */
3789 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
3790 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
3791 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
3792 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
3794 nonzero &= cached_nonzero_bits (x, GET_MODE (x),
3795 known_x, known_mode, known_ret);
3796 nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
3797 return nonzero;
3799 #endif
3801 code = GET_CODE (x);
3802 switch (code)
3804 case REG:
3805 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
3806 /* If pointers extend unsigned and this is a pointer in Pmode, say that
3807 all the bits above ptr_mode are known to be zero. */
3808 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
3809 && REG_POINTER (x))
3810 nonzero &= GET_MODE_MASK (ptr_mode);
3811 #endif
3813 /* Include declared information about alignment of pointers. */
3814 /* ??? We don't properly preserve REG_POINTER changes across
3815 pointer-to-integer casts, so we can't trust it except for
3816 things that we know must be pointers. See execute/960116-1.c. */
3817 if ((x == stack_pointer_rtx
3818 || x == frame_pointer_rtx
3819 || x == arg_pointer_rtx)
3820 && REGNO_POINTER_ALIGN (REGNO (x)))
3822 unsigned HOST_WIDE_INT alignment
3823 = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
3825 #ifdef PUSH_ROUNDING
3826 /* If PUSH_ROUNDING is defined, it is possible for the
3827 stack to be momentarily aligned only to that amount,
3828 so we pick the least alignment. */
3829 if (x == stack_pointer_rtx && PUSH_ARGS)
3830 alignment = MIN ((unsigned HOST_WIDE_INT) PUSH_ROUNDING (1),
3831 alignment);
3832 #endif
3834 nonzero &= ~(alignment - 1);
3838 unsigned HOST_WIDE_INT nonzero_for_hook = nonzero;
3839 rtx new = rtl_hooks.reg_nonzero_bits (x, mode, known_x,
3840 known_mode, known_ret,
3841 &nonzero_for_hook);
3843 if (new)
3844 nonzero_for_hook &= cached_nonzero_bits (new, mode, known_x,
3845 known_mode, known_ret);
3847 return nonzero_for_hook;
3850 case CONST_INT:
3851 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
3852 /* If X is negative in MODE, sign-extend the value. */
3853 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
3854 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
3855 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
3856 #endif
3858 return INTVAL (x);
3860 case MEM:
3861 #ifdef LOAD_EXTEND_OP
3862 /* In many, if not most, RISC machines, reading a byte from memory
3863 zeros the rest of the register. Noticing that fact saves a lot
3864 of extra zero-extends. */
3865 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
3866 nonzero &= GET_MODE_MASK (GET_MODE (x));
3867 #endif
3868 break;
3870 case EQ: case NE:
3871 case UNEQ: case LTGT:
3872 case GT: case GTU: case UNGT:
3873 case LT: case LTU: case UNLT:
3874 case GE: case GEU: case UNGE:
3875 case LE: case LEU: case UNLE:
3876 case UNORDERED: case ORDERED:
3878 /* If this produces an integer result, we know which bits are set.
3879 Code here used to clear bits outside the mode of X, but that is
3880 now done above. */
3882 if (GET_MODE_CLASS (mode) == MODE_INT
3883 && mode_width <= HOST_BITS_PER_WIDE_INT)
3884 nonzero = STORE_FLAG_VALUE;
3885 break;
3887 case NEG:
3888 #if 0
3889 /* Disabled to avoid exponential mutual recursion between nonzero_bits
3890 and num_sign_bit_copies. */
3891 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
3892 == GET_MODE_BITSIZE (GET_MODE (x)))
3893 nonzero = 1;
3894 #endif
3896 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
3897 nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
3898 break;
3900 case ABS:
3901 #if 0
3902 /* Disabled to avoid exponential mutual recursion between nonzero_bits
3903 and num_sign_bit_copies. */
3904 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
3905 == GET_MODE_BITSIZE (GET_MODE (x)))
3906 nonzero = 1;
3907 #endif
3908 break;
3910 case TRUNCATE:
3911 nonzero &= (cached_nonzero_bits (XEXP (x, 0), mode,
3912 known_x, known_mode, known_ret)
3913 & GET_MODE_MASK (mode));
3914 break;
3916 case ZERO_EXTEND:
3917 nonzero &= cached_nonzero_bits (XEXP (x, 0), mode,
3918 known_x, known_mode, known_ret);
3919 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
3920 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
3921 break;
3923 case SIGN_EXTEND:
3924 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
3925 Otherwise, show all the bits in the outer mode but not the inner
3926 may be nonzero. */
3927 inner_nz = cached_nonzero_bits (XEXP (x, 0), mode,
3928 known_x, known_mode, known_ret);
3929 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
3931 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
3932 if (inner_nz
3933 & (((HOST_WIDE_INT) 1
3934 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
3935 inner_nz |= (GET_MODE_MASK (mode)
3936 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
3939 nonzero &= inner_nz;
3940 break;
3942 case AND:
3943 nonzero &= cached_nonzero_bits (XEXP (x, 0), mode,
3944 known_x, known_mode, known_ret)
3945 & cached_nonzero_bits (XEXP (x, 1), mode,
3946 known_x, known_mode, known_ret);
3947 break;
3949 case XOR: case IOR:
3950 case UMIN: case UMAX: case SMIN: case SMAX:
3952 unsigned HOST_WIDE_INT nonzero0 =
3953 cached_nonzero_bits (XEXP (x, 0), mode,
3954 known_x, known_mode, known_ret);
3956 /* Don't call nonzero_bits for the second time if it cannot change
3957 anything. */
3958 if ((nonzero & nonzero0) != nonzero)
3959 nonzero &= nonzero0
3960 | cached_nonzero_bits (XEXP (x, 1), mode,
3961 known_x, known_mode, known_ret);
3963 break;
3965 case PLUS: case MINUS:
3966 case MULT:
3967 case DIV: case UDIV:
3968 case MOD: case UMOD:
3969 /* We can apply the rules of arithmetic to compute the number of
3970 high- and low-order zero bits of these operations. We start by
3971 computing the width (position of the highest-order nonzero bit)
3972 and the number of low-order zero bits for each value. */
3974 unsigned HOST_WIDE_INT nz0 =
3975 cached_nonzero_bits (XEXP (x, 0), mode,
3976 known_x, known_mode, known_ret);
3977 unsigned HOST_WIDE_INT nz1 =
3978 cached_nonzero_bits (XEXP (x, 1), mode,
3979 known_x, known_mode, known_ret);
3980 int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
3981 int width0 = floor_log2 (nz0) + 1;
3982 int width1 = floor_log2 (nz1) + 1;
3983 int low0 = floor_log2 (nz0 & -nz0);
3984 int low1 = floor_log2 (nz1 & -nz1);
3985 HOST_WIDE_INT op0_maybe_minusp
3986 = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
3987 HOST_WIDE_INT op1_maybe_minusp
3988 = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
3989 unsigned int result_width = mode_width;
3990 int result_low = 0;
3992 switch (code)
3994 case PLUS:
3995 result_width = MAX (width0, width1) + 1;
3996 result_low = MIN (low0, low1);
3997 break;
3998 case MINUS:
3999 result_low = MIN (low0, low1);
4000 break;
4001 case MULT:
4002 result_width = width0 + width1;
4003 result_low = low0 + low1;
4004 break;
4005 case DIV:
4006 if (width1 == 0)
4007 break;
4008 if (! op0_maybe_minusp && ! op1_maybe_minusp)
4009 result_width = width0;
4010 break;
4011 case UDIV:
4012 if (width1 == 0)
4013 break;
4014 result_width = width0;
4015 break;
4016 case MOD:
4017 if (width1 == 0)
4018 break;
4019 if (! op0_maybe_minusp && ! op1_maybe_minusp)
4020 result_width = MIN (width0, width1);
4021 result_low = MIN (low0, low1);
4022 break;
4023 case UMOD:
4024 if (width1 == 0)
4025 break;
4026 result_width = MIN (width0, width1);
4027 result_low = MIN (low0, low1);
4028 break;
4029 default:
4030 gcc_unreachable ();
4033 if (result_width < mode_width)
4034 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
4036 if (result_low > 0)
4037 nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
4039 #ifdef POINTERS_EXTEND_UNSIGNED
4040 /* If pointers extend unsigned and this is an addition or subtraction
4041 to a pointer in Pmode, all the bits above ptr_mode are known to be
4042 zero. */
4043 if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
4044 && (code == PLUS || code == MINUS)
4045 && REG_P (XEXP (x, 0)) && REG_POINTER (XEXP (x, 0)))
4046 nonzero &= GET_MODE_MASK (ptr_mode);
4047 #endif
4049 break;
4051 case ZERO_EXTRACT:
4052 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4053 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
4054 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
4055 break;
4057 case SUBREG:
4058 /* If this is a SUBREG formed for a promoted variable that has
4059 been zero-extended, we know that at least the high-order bits
4060 are zero, though others might be too. */
4062 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
4063 nonzero = GET_MODE_MASK (GET_MODE (x))
4064 & cached_nonzero_bits (SUBREG_REG (x), GET_MODE (x),
4065 known_x, known_mode, known_ret);
4067 /* If the inner mode is a single word for both the host and target
4068 machines, we can compute this from which bits of the inner
4069 object might be nonzero. */
4070 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
4071 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
4072 <= HOST_BITS_PER_WIDE_INT))
4074 nonzero &= cached_nonzero_bits (SUBREG_REG (x), mode,
4075 known_x, known_mode, known_ret);
4077 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
4078 /* If this is a typical RISC machine, we only have to worry
4079 about the way loads are extended. */
4080 if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
4081 ? (((nonzero
4082 & (((unsigned HOST_WIDE_INT) 1
4083 << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
4084 != 0))
4085 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
4086 || !MEM_P (SUBREG_REG (x)))
4087 #endif
4089 /* On many CISC machines, accessing an object in a wider mode
4090 causes the high-order bits to become undefined. So they are
4091 not known to be zero. */
4092 if (GET_MODE_SIZE (GET_MODE (x))
4093 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4094 nonzero |= (GET_MODE_MASK (GET_MODE (x))
4095 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
4098 break;
4100 case ASHIFTRT:
4101 case LSHIFTRT:
4102 case ASHIFT:
4103 case ROTATE:
4104 /* The nonzero bits are in two classes: any bits within MODE
4105 that aren't in GET_MODE (x) are always significant. The rest of the
4106 nonzero bits are those that are significant in the operand of
4107 the shift when shifted the appropriate number of bits. This
4108 shows that high-order bits are cleared by the right shift and
4109 low-order bits by left shifts. */
4110 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4111 && INTVAL (XEXP (x, 1)) >= 0
4112 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
4114 enum machine_mode inner_mode = GET_MODE (x);
4115 unsigned int width = GET_MODE_BITSIZE (inner_mode);
4116 int count = INTVAL (XEXP (x, 1));
4117 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
4118 unsigned HOST_WIDE_INT op_nonzero =
4119 cached_nonzero_bits (XEXP (x, 0), mode,
4120 known_x, known_mode, known_ret);
4121 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
4122 unsigned HOST_WIDE_INT outer = 0;
4124 if (mode_width > width)
4125 outer = (op_nonzero & nonzero & ~mode_mask);
4127 if (code == LSHIFTRT)
4128 inner >>= count;
4129 else if (code == ASHIFTRT)
4131 inner >>= count;
4133 /* If the sign bit may have been nonzero before the shift, we
4134 need to mark all the places it could have been copied to
4135 by the shift as possibly nonzero. */
4136 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
4137 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
4139 else if (code == ASHIFT)
4140 inner <<= count;
4141 else
4142 inner = ((inner << (count % width)
4143 | (inner >> (width - (count % width)))) & mode_mask);
4145 nonzero &= (outer | inner);
4147 break;
4149 case FFS:
4150 case POPCOUNT:
4151 /* This is at most the number of bits in the mode. */
4152 nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
4153 break;
4155 case CLZ:
4156 /* If CLZ has a known value at zero, then the nonzero bits are
4157 that value, plus the number of bits in the mode minus one. */
4158 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
4159 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
4160 else
4161 nonzero = -1;
4162 break;
4164 case CTZ:
4165 /* If CTZ has a known value at zero, then the nonzero bits are
4166 that value, plus the number of bits in the mode minus one. */
4167 if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
4168 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
4169 else
4170 nonzero = -1;
4171 break;
4173 case PARITY:
4174 nonzero = 1;
4175 break;
4177 case IF_THEN_ELSE:
4179 unsigned HOST_WIDE_INT nonzero_true =
4180 cached_nonzero_bits (XEXP (x, 1), mode,
4181 known_x, known_mode, known_ret);
4183 /* Don't call nonzero_bits for the second time if it cannot change
4184 anything. */
4185 if ((nonzero & nonzero_true) != nonzero)
4186 nonzero &= nonzero_true
4187 | cached_nonzero_bits (XEXP (x, 2), mode,
4188 known_x, known_mode, known_ret);
4190 break;
4192 default:
4193 break;
4196 return nonzero;
4199 /* See the macro definition above. */
4200 #undef cached_num_sign_bit_copies
4203 /* The function cached_num_sign_bit_copies is a wrapper around
4204 num_sign_bit_copies1. It avoids exponential behavior in
4205 num_sign_bit_copies1 when X has identical subexpressions on the
4206 first or the second level. */
4208 static unsigned int
4209 cached_num_sign_bit_copies (rtx x, enum machine_mode mode, rtx known_x,
4210 enum machine_mode known_mode,
4211 unsigned int known_ret)
4213 if (x == known_x && mode == known_mode)
4214 return known_ret;
4216 /* Try to find identical subexpressions. If found call
4217 num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
4218 the precomputed value for the subexpression as KNOWN_RET. */
4220 if (ARITHMETIC_P (x))
4222 rtx x0 = XEXP (x, 0);
4223 rtx x1 = XEXP (x, 1);
4225 /* Check the first level. */
4226 if (x0 == x1)
4227 return
4228 num_sign_bit_copies1 (x, mode, x0, mode,
4229 cached_num_sign_bit_copies (x0, mode, known_x,
4230 known_mode,
4231 known_ret));
4233 /* Check the second level. */
4234 if (ARITHMETIC_P (x0)
4235 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
4236 return
4237 num_sign_bit_copies1 (x, mode, x1, mode,
4238 cached_num_sign_bit_copies (x1, mode, known_x,
4239 known_mode,
4240 known_ret));
4242 if (ARITHMETIC_P (x1)
4243 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
4244 return
4245 num_sign_bit_copies1 (x, mode, x0, mode,
4246 cached_num_sign_bit_copies (x0, mode, known_x,
4247 known_mode,
4248 known_ret));
4251 return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
4254 /* Return the number of bits at the high-order end of X that are known to
4255 be equal to the sign bit. X will be used in mode MODE; if MODE is
4256 VOIDmode, X will be used in its own mode. The returned value will always
4257 be between 1 and the number of bits in MODE. */
4259 static unsigned int
4260 num_sign_bit_copies1 (rtx x, enum machine_mode mode, rtx known_x,
4261 enum machine_mode known_mode,
4262 unsigned int known_ret)
4264 enum rtx_code code = GET_CODE (x);
4265 unsigned int bitwidth = GET_MODE_BITSIZE (mode);
4266 int num0, num1, result;
4267 unsigned HOST_WIDE_INT nonzero;
4269 /* If we weren't given a mode, use the mode of X. If the mode is still
4270 VOIDmode, we don't know anything. Likewise if one of the modes is
4271 floating-point. */
4273 if (mode == VOIDmode)
4274 mode = GET_MODE (x);
4276 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
4277 return 1;
4279 /* For a smaller object, just ignore the high bits. */
4280 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
4282 num0 = cached_num_sign_bit_copies (x, GET_MODE (x),
4283 known_x, known_mode, known_ret);
4284 return MAX (1,
4285 num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
4288 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
4290 #ifndef WORD_REGISTER_OPERATIONS
4291 /* If this machine does not do all register operations on the entire
4292 register and MODE is wider than the mode of X, we can say nothing
4293 at all about the high-order bits. */
4294 return 1;
4295 #else
4296 /* Likewise on machines that do, if the mode of the object is smaller
4297 than a word and loads of that size don't sign extend, we can say
4298 nothing about the high order bits. */
4299 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
4300 #ifdef LOAD_EXTEND_OP
4301 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
4302 #endif
4304 return 1;
4305 #endif
4308 switch (code)
4310 case REG:
4312 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
4313 /* If pointers extend signed and this is a pointer in Pmode, say that
4314 all the bits above ptr_mode are known to be sign bit copies. */
4315 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
4316 && REG_POINTER (x))
4317 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
4318 #endif
4321 unsigned int copies_for_hook = 1, copies = 1;
4322 rtx new = rtl_hooks.reg_num_sign_bit_copies (x, mode, known_x,
4323 known_mode, known_ret,
4324 &copies_for_hook);
4326 if (new)
4327 copies = cached_num_sign_bit_copies (new, mode, known_x,
4328 known_mode, known_ret);
4330 if (copies > 1 || copies_for_hook > 1)
4331 return MAX (copies, copies_for_hook);
4333 /* Else, use nonzero_bits to guess num_sign_bit_copies (see below). */
4335 break;
4337 case MEM:
4338 #ifdef LOAD_EXTEND_OP
4339 /* Some RISC machines sign-extend all loads of smaller than a word. */
4340 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
4341 return MAX (1, ((int) bitwidth
4342 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
4343 #endif
4344 break;
4346 case CONST_INT:
4347 /* If the constant is negative, take its 1's complement and remask.
4348 Then see how many zero bits we have. */
4349 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
4350 if (bitwidth <= HOST_BITS_PER_WIDE_INT
4351 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4352 nonzero = (~nonzero) & GET_MODE_MASK (mode);
4354 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
4356 case SUBREG:
4357 /* If this is a SUBREG for a promoted object that is sign-extended
4358 and we are looking at it in a wider mode, we know that at least the
4359 high-order bits are known to be sign bit copies. */
4361 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
4363 num0 = cached_num_sign_bit_copies (SUBREG_REG (x), mode,
4364 known_x, known_mode, known_ret);
4365 return MAX ((int) bitwidth
4366 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
4367 num0);
4370 /* For a smaller object, just ignore the high bits. */
4371 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
4373 num0 = cached_num_sign_bit_copies (SUBREG_REG (x), VOIDmode,
4374 known_x, known_mode, known_ret);
4375 return MAX (1, (num0
4376 - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
4377 - bitwidth)));
4380 #ifdef WORD_REGISTER_OPERATIONS
4381 #ifdef LOAD_EXTEND_OP
4382 /* For paradoxical SUBREGs on machines where all register operations
4383 affect the entire register, just look inside. Note that we are
4384 passing MODE to the recursive call, so the number of sign bit copies
4385 will remain relative to that mode, not the inner mode. */
4387 /* This works only if loads sign extend. Otherwise, if we get a
4388 reload for the inner part, it may be loaded from the stack, and
4389 then we lose all sign bit copies that existed before the store
4390 to the stack. */
4392 if ((GET_MODE_SIZE (GET_MODE (x))
4393 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4394 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
4395 && MEM_P (SUBREG_REG (x)))
4396 return cached_num_sign_bit_copies (SUBREG_REG (x), mode,
4397 known_x, known_mode, known_ret);
4398 #endif
4399 #endif
4400 break;
4402 case SIGN_EXTRACT:
4403 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4404 return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
4405 break;
4407 case SIGN_EXTEND:
4408 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4409 + cached_num_sign_bit_copies (XEXP (x, 0), VOIDmode,
4410 known_x, known_mode, known_ret));
4412 case TRUNCATE:
4413 /* For a smaller object, just ignore the high bits. */
4414 num0 = cached_num_sign_bit_copies (XEXP (x, 0), VOIDmode,
4415 known_x, known_mode, known_ret);
4416 return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4417 - bitwidth)));
4419 case NOT:
4420 return cached_num_sign_bit_copies (XEXP (x, 0), mode,
4421 known_x, known_mode, known_ret);
4423 case ROTATE: case ROTATERT:
4424 /* If we are rotating left by a number of bits less than the number
4425 of sign bit copies, we can just subtract that amount from the
4426 number. */
4427 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4428 && INTVAL (XEXP (x, 1)) >= 0
4429 && INTVAL (XEXP (x, 1)) < (int) bitwidth)
4431 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4432 known_x, known_mode, known_ret);
4433 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
4434 : (int) bitwidth - INTVAL (XEXP (x, 1))));
4436 break;
4438 case NEG:
4439 /* In general, this subtracts one sign bit copy. But if the value
4440 is known to be positive, the number of sign bit copies is the
4441 same as that of the input. Finally, if the input has just one bit
4442 that might be nonzero, all the bits are copies of the sign bit. */
4443 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4444 known_x, known_mode, known_ret);
4445 if (bitwidth > HOST_BITS_PER_WIDE_INT)
4446 return num0 > 1 ? num0 - 1 : 1;
4448 nonzero = nonzero_bits (XEXP (x, 0), mode);
4449 if (nonzero == 1)
4450 return bitwidth;
4452 if (num0 > 1
4453 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
4454 num0--;
4456 return num0;
4458 case IOR: case AND: case XOR:
4459 case SMIN: case SMAX: case UMIN: case UMAX:
4460 /* Logical operations will preserve the number of sign-bit copies.
4461 MIN and MAX operations always return one of the operands. */
4462 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4463 known_x, known_mode, known_ret);
4464 num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4465 known_x, known_mode, known_ret);
4466 return MIN (num0, num1);
4468 case PLUS: case MINUS:
4469 /* For addition and subtraction, we can have a 1-bit carry. However,
4470 if we are subtracting 1 from a positive number, there will not
4471 be such a carry. Furthermore, if the positive number is known to
4472 be 0 or 1, we know the result is either -1 or 0. */
4474 if (code == PLUS && XEXP (x, 1) == constm1_rtx
4475 && bitwidth <= HOST_BITS_PER_WIDE_INT)
4477 nonzero = nonzero_bits (XEXP (x, 0), mode);
4478 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
4479 return (nonzero == 1 || nonzero == 0 ? bitwidth
4480 : bitwidth - floor_log2 (nonzero) - 1);
4483 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4484 known_x, known_mode, known_ret);
4485 num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4486 known_x, known_mode, known_ret);
4487 result = MAX (1, MIN (num0, num1) - 1);
4489 #ifdef POINTERS_EXTEND_UNSIGNED
4490 /* If pointers extend signed and this is an addition or subtraction
4491 to a pointer in Pmode, all the bits above ptr_mode are known to be
4492 sign bit copies. */
4493 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
4494 && (code == PLUS || code == MINUS)
4495 && REG_P (XEXP (x, 0)) && REG_POINTER (XEXP (x, 0)))
4496 result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
4497 - GET_MODE_BITSIZE (ptr_mode) + 1),
4498 result);
4499 #endif
4500 return result;
4502 case MULT:
4503 /* The number of bits of the product is the sum of the number of
4504 bits of both terms. However, unless one of the terms if known
4505 to be positive, we must allow for an additional bit since negating
4506 a negative number can remove one sign bit copy. */
4508 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4509 known_x, known_mode, known_ret);
4510 num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4511 known_x, known_mode, known_ret);
4513 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
4514 if (result > 0
4515 && (bitwidth > HOST_BITS_PER_WIDE_INT
4516 || (((nonzero_bits (XEXP (x, 0), mode)
4517 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4518 && ((nonzero_bits (XEXP (x, 1), mode)
4519 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
4520 result--;
4522 return MAX (1, result);
4524 case UDIV:
4525 /* The result must be <= the first operand. If the first operand
4526 has the high bit set, we know nothing about the number of sign
4527 bit copies. */
4528 if (bitwidth > HOST_BITS_PER_WIDE_INT)
4529 return 1;
4530 else if ((nonzero_bits (XEXP (x, 0), mode)
4531 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4532 return 1;
4533 else
4534 return cached_num_sign_bit_copies (XEXP (x, 0), mode,
4535 known_x, known_mode, known_ret);
4537 case UMOD:
4538 /* The result must be <= the second operand. */
4539 return cached_num_sign_bit_copies (XEXP (x, 1), mode,
4540 known_x, known_mode, known_ret);
4542 case DIV:
4543 /* Similar to unsigned division, except that we have to worry about
4544 the case where the divisor is negative, in which case we have
4545 to add 1. */
4546 result = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4547 known_x, known_mode, known_ret);
4548 if (result > 1
4549 && (bitwidth > HOST_BITS_PER_WIDE_INT
4550 || (nonzero_bits (XEXP (x, 1), mode)
4551 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
4552 result--;
4554 return result;
4556 case MOD:
4557 result = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4558 known_x, known_mode, known_ret);
4559 if (result > 1
4560 && (bitwidth > HOST_BITS_PER_WIDE_INT
4561 || (nonzero_bits (XEXP (x, 1), mode)
4562 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
4563 result--;
4565 return result;
4567 case ASHIFTRT:
4568 /* Shifts by a constant add to the number of bits equal to the
4569 sign bit. */
4570 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4571 known_x, known_mode, known_ret);
4572 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4573 && INTVAL (XEXP (x, 1)) > 0)
4574 num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
4576 return num0;
4578 case ASHIFT:
4579 /* Left shifts destroy copies. */
4580 if (GET_CODE (XEXP (x, 1)) != CONST_INT
4581 || INTVAL (XEXP (x, 1)) < 0
4582 || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
4583 return 1;
4585 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4586 known_x, known_mode, known_ret);
4587 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
4589 case IF_THEN_ELSE:
4590 num0 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4591 known_x, known_mode, known_ret);
4592 num1 = cached_num_sign_bit_copies (XEXP (x, 2), mode,
4593 known_x, known_mode, known_ret);
4594 return MIN (num0, num1);
4596 case EQ: case NE: case GE: case GT: case LE: case LT:
4597 case UNEQ: case LTGT: case UNGE: case UNGT: case UNLE: case UNLT:
4598 case GEU: case GTU: case LEU: case LTU:
4599 case UNORDERED: case ORDERED:
4600 /* If the constant is negative, take its 1's complement and remask.
4601 Then see how many zero bits we have. */
4602 nonzero = STORE_FLAG_VALUE;
4603 if (bitwidth <= HOST_BITS_PER_WIDE_INT
4604 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4605 nonzero = (~nonzero) & GET_MODE_MASK (mode);
4607 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
4609 default:
4610 break;
4613 /* If we haven't been able to figure it out by one of the above rules,
4614 see if some of the high-order bits are known to be zero. If so,
4615 count those bits and return one less than that amount. If we can't
4616 safely compute the mask for this mode, always return BITWIDTH. */
4618 bitwidth = GET_MODE_BITSIZE (mode);
4619 if (bitwidth > HOST_BITS_PER_WIDE_INT)
4620 return 1;
4622 nonzero = nonzero_bits (x, mode);
4623 return nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
4624 ? 1 : bitwidth - floor_log2 (nonzero) - 1;
4627 /* Calculate the rtx_cost of a single instruction. A return value of
4628 zero indicates an instruction pattern without a known cost. */
4631 insn_rtx_cost (rtx pat)
4633 int i, cost;
4634 rtx set;
4636 /* Extract the single set rtx from the instruction pattern.
4637 We can't use single_set since we only have the pattern. */
4638 if (GET_CODE (pat) == SET)
4639 set = pat;
4640 else if (GET_CODE (pat) == PARALLEL)
4642 set = NULL_RTX;
4643 for (i = 0; i < XVECLEN (pat, 0); i++)
4645 rtx x = XVECEXP (pat, 0, i);
4646 if (GET_CODE (x) == SET)
4648 if (set)
4649 return 0;
4650 set = x;
4653 if (!set)
4654 return 0;
4656 else
4657 return 0;
4659 cost = rtx_cost (SET_SRC (set), SET);
4660 return cost > 0 ? cost : COSTS_N_INSNS (1);