1 /* Common subexpression elimination for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 /* stdio.h must precede rtl.h for FFS. */
31 #include "hard-reg-set.h"
34 #include "insn-config.h"
45 /* Simplification and canonicalization of RTL. */
47 /* Nonzero if X has the form (PLUS frame-pointer integer). We check for
48 virtual regs here because the simplify_*_operation routines are called
49 by integrate.c, which is called before virtual register instantiation.
51 ?!? FIXED_BASE_PLUS_P and NONZERO_BASE_PLUS_P need to move into
52 a header file so that their definitions can be shared with the
53 simplification routines in simplify-rtx.c. Until then, do not
54 change these macros without also changing the copy in simplify-rtx.c. */
56 #define FIXED_BASE_PLUS_P(X) \
57 ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \
58 || ((X) == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])\
59 || (X) == virtual_stack_vars_rtx \
60 || (X) == virtual_incoming_args_rtx \
61 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
62 && (XEXP (X, 0) == frame_pointer_rtx \
63 || XEXP (X, 0) == hard_frame_pointer_rtx \
64 || ((X) == arg_pointer_rtx \
65 && fixed_regs[ARG_POINTER_REGNUM]) \
66 || XEXP (X, 0) == virtual_stack_vars_rtx \
67 || XEXP (X, 0) == virtual_incoming_args_rtx)) \
68 || GET_CODE (X) == ADDRESSOF)
70 /* Similar, but also allows reference to the stack pointer.
72 This used to include FIXED_BASE_PLUS_P, however, we can't assume that
73 arg_pointer_rtx by itself is nonzero, because on at least one machine,
74 the i960, the arg pointer is zero when it is unused. */
76 #define NONZERO_BASE_PLUS_P(X) \
77 ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \
78 || (X) == virtual_stack_vars_rtx \
79 || (X) == virtual_incoming_args_rtx \
80 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
81 && (XEXP (X, 0) == frame_pointer_rtx \
82 || XEXP (X, 0) == hard_frame_pointer_rtx \
83 || ((X) == arg_pointer_rtx \
84 && fixed_regs[ARG_POINTER_REGNUM]) \
85 || XEXP (X, 0) == virtual_stack_vars_rtx \
86 || XEXP (X, 0) == virtual_incoming_args_rtx)) \
87 || (X) == stack_pointer_rtx \
88 || (X) == virtual_stack_dynamic_rtx \
89 || (X) == virtual_outgoing_args_rtx \
90 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
91 && (XEXP (X, 0) == stack_pointer_rtx \
92 || XEXP (X, 0) == virtual_stack_dynamic_rtx \
93 || XEXP (X, 0) == virtual_outgoing_args_rtx)) \
94 || GET_CODE (X) == ADDRESSOF)
97 static rtx simplify_plus_minus
PARAMS ((enum rtx_code
, enum machine_mode
,
99 static void check_fold_consts
PARAMS ((PTR
));
101 /* Make a binary operation by properly ordering the operands and
102 seeing if the expression folds. */
105 simplify_gen_binary (code
, mode
, op0
, op1
)
107 enum machine_mode mode
;
112 /* Put complex operands first and constants second if commutative. */
113 if (GET_RTX_CLASS (code
) == 'c'
114 && ((CONSTANT_P (op0
) && GET_CODE (op1
) != CONST_INT
)
115 || (GET_RTX_CLASS (GET_CODE (op0
)) == 'o'
116 && GET_RTX_CLASS (GET_CODE (op1
)) != 'o')
117 || (GET_CODE (op0
) == SUBREG
118 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0
))) == 'o'
119 && GET_RTX_CLASS (GET_CODE (op1
)) != 'o')))
120 tem
= op0
, op0
= op1
, op1
= tem
;
122 /* If this simplifies, do it. */
123 tem
= simplify_binary_operation (code
, mode
, op0
, op1
);
128 /* Handle addition and subtraction of CONST_INT specially. Otherwise,
129 just form the operation. */
131 if (code
== PLUS
&& GET_CODE (op1
) == CONST_INT
132 && GET_MODE (op0
) != VOIDmode
)
133 return plus_constant (op0
, INTVAL (op1
));
134 else if (code
== MINUS
&& GET_CODE (op1
) == CONST_INT
135 && GET_MODE (op0
) != VOIDmode
)
136 return plus_constant (op0
, - INTVAL (op1
));
138 return gen_rtx_fmt_ee (code
, mode
, op0
, op1
);
141 /* Try to simplify a unary operation CODE whose output mode is to be
142 MODE with input operand OP whose mode was originally OP_MODE.
143 Return zero if no simplification can be made. */
146 simplify_unary_operation (code
, mode
, op
, op_mode
)
148 enum machine_mode mode
;
150 enum machine_mode op_mode
;
152 register int width
= GET_MODE_BITSIZE (mode
);
154 /* The order of these tests is critical so that, for example, we don't
155 check the wrong mode (input vs. output) for a conversion operation,
156 such as FIX. At some point, this should be simplified. */
158 #if !defined(REAL_IS_NOT_DOUBLE) || defined(REAL_ARITHMETIC)
160 if (code
== FLOAT
&& GET_MODE (op
) == VOIDmode
161 && (GET_CODE (op
) == CONST_DOUBLE
|| GET_CODE (op
) == CONST_INT
))
163 HOST_WIDE_INT hv
, lv
;
166 if (GET_CODE (op
) == CONST_INT
)
167 lv
= INTVAL (op
), hv
= INTVAL (op
) < 0 ? -1 : 0;
169 lv
= CONST_DOUBLE_LOW (op
), hv
= CONST_DOUBLE_HIGH (op
);
171 #ifdef REAL_ARITHMETIC
172 REAL_VALUE_FROM_INT (d
, lv
, hv
, mode
);
177 d
*= ((double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2))
178 * (double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2)));
179 d
+= (double) (unsigned HOST_WIDE_INT
) (~ lv
);
185 d
*= ((double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2))
186 * (double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2)));
187 d
+= (double) (unsigned HOST_WIDE_INT
) lv
;
189 #endif /* REAL_ARITHMETIC */
190 d
= real_value_truncate (mode
, d
);
191 return CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
193 else if (code
== UNSIGNED_FLOAT
&& GET_MODE (op
) == VOIDmode
194 && (GET_CODE (op
) == CONST_DOUBLE
|| GET_CODE (op
) == CONST_INT
))
196 HOST_WIDE_INT hv
, lv
;
199 if (GET_CODE (op
) == CONST_INT
)
200 lv
= INTVAL (op
), hv
= INTVAL (op
) < 0 ? -1 : 0;
202 lv
= CONST_DOUBLE_LOW (op
), hv
= CONST_DOUBLE_HIGH (op
);
204 if (op_mode
== VOIDmode
)
206 /* We don't know how to interpret negative-looking numbers in
207 this case, so don't try to fold those. */
211 else if (GET_MODE_BITSIZE (op_mode
) >= HOST_BITS_PER_WIDE_INT
* 2)
214 hv
= 0, lv
&= GET_MODE_MASK (op_mode
);
216 #ifdef REAL_ARITHMETIC
217 REAL_VALUE_FROM_UNSIGNED_INT (d
, lv
, hv
, mode
);
220 d
= (double) (unsigned HOST_WIDE_INT
) hv
;
221 d
*= ((double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2))
222 * (double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2)));
223 d
+= (double) (unsigned HOST_WIDE_INT
) lv
;
224 #endif /* REAL_ARITHMETIC */
225 d
= real_value_truncate (mode
, d
);
226 return CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
230 if (GET_CODE (op
) == CONST_INT
231 && width
<= HOST_BITS_PER_WIDE_INT
&& width
> 0)
233 register HOST_WIDE_INT arg0
= INTVAL (op
);
234 register HOST_WIDE_INT val
;
247 val
= (arg0
>= 0 ? arg0
: - arg0
);
251 /* Don't use ffs here. Instead, get low order bit and then its
252 number. If arg0 is zero, this will return 0, as desired. */
253 arg0
&= GET_MODE_MASK (mode
);
254 val
= exact_log2 (arg0
& (- arg0
)) + 1;
262 if (op_mode
== VOIDmode
)
264 if (GET_MODE_BITSIZE (op_mode
) == HOST_BITS_PER_WIDE_INT
)
266 /* If we were really extending the mode,
267 we would have to distinguish between zero-extension
268 and sign-extension. */
269 if (width
!= GET_MODE_BITSIZE (op_mode
))
273 else if (GET_MODE_BITSIZE (op_mode
) < HOST_BITS_PER_WIDE_INT
)
274 val
= arg0
& ~((HOST_WIDE_INT
) (-1) << GET_MODE_BITSIZE (op_mode
));
280 if (op_mode
== VOIDmode
)
282 if (GET_MODE_BITSIZE (op_mode
) == HOST_BITS_PER_WIDE_INT
)
284 /* If we were really extending the mode,
285 we would have to distinguish between zero-extension
286 and sign-extension. */
287 if (width
!= GET_MODE_BITSIZE (op_mode
))
291 else if (GET_MODE_BITSIZE (op_mode
) < HOST_BITS_PER_WIDE_INT
)
294 = arg0
& ~((HOST_WIDE_INT
) (-1) << GET_MODE_BITSIZE (op_mode
));
296 & ((HOST_WIDE_INT
) 1 << (GET_MODE_BITSIZE (op_mode
) - 1)))
297 val
-= (HOST_WIDE_INT
) 1 << GET_MODE_BITSIZE (op_mode
);
310 val
= trunc_int_for_mode (val
, mode
);
312 return GEN_INT (val
);
315 /* We can do some operations on integer CONST_DOUBLEs. Also allow
316 for a DImode operation on a CONST_INT. */
317 else if (GET_MODE (op
) == VOIDmode
&& width
<= HOST_BITS_PER_INT
* 2
318 && (GET_CODE (op
) == CONST_DOUBLE
|| GET_CODE (op
) == CONST_INT
))
320 HOST_WIDE_INT l1
, h1
, lv
, hv
;
322 if (GET_CODE (op
) == CONST_DOUBLE
)
323 l1
= CONST_DOUBLE_LOW (op
), h1
= CONST_DOUBLE_HIGH (op
);
325 l1
= INTVAL (op
), h1
= l1
< 0 ? -1 : 0;
335 neg_double (l1
, h1
, &lv
, &hv
);
340 neg_double (l1
, h1
, &lv
, &hv
);
348 lv
= HOST_BITS_PER_WIDE_INT
+ exact_log2 (h1
& (-h1
)) + 1;
350 lv
= exact_log2 (l1
& (-l1
)) + 1;
354 /* This is just a change-of-mode, so do nothing. */
359 if (op_mode
== VOIDmode
360 || GET_MODE_BITSIZE (op_mode
) > HOST_BITS_PER_WIDE_INT
)
364 lv
= l1
& GET_MODE_MASK (op_mode
);
368 if (op_mode
== VOIDmode
369 || GET_MODE_BITSIZE (op_mode
) > HOST_BITS_PER_WIDE_INT
)
373 lv
= l1
& GET_MODE_MASK (op_mode
);
374 if (GET_MODE_BITSIZE (op_mode
) < HOST_BITS_PER_WIDE_INT
375 && (lv
& ((HOST_WIDE_INT
) 1
376 << (GET_MODE_BITSIZE (op_mode
) - 1))) != 0)
377 lv
-= (HOST_WIDE_INT
) 1 << GET_MODE_BITSIZE (op_mode
);
379 hv
= (lv
< 0) ? ~ (HOST_WIDE_INT
) 0 : 0;
390 return immed_double_const (lv
, hv
, mode
);
393 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
394 else if (GET_CODE (op
) == CONST_DOUBLE
395 && GET_MODE_CLASS (mode
) == MODE_FLOAT
)
401 if (setjmp (handler
))
402 /* There used to be a warning here, but that is inadvisable.
403 People may want to cause traps, and the natural way
404 to do it should not get a warning. */
407 set_float_handler (handler
);
409 REAL_VALUE_FROM_CONST_DOUBLE (d
, op
);
414 d
= REAL_VALUE_NEGATE (d
);
418 if (REAL_VALUE_NEGATIVE (d
))
419 d
= REAL_VALUE_NEGATE (d
);
423 d
= real_value_truncate (mode
, d
);
427 /* All this does is change the mode. */
431 d
= REAL_VALUE_RNDZINT (d
);
435 d
= REAL_VALUE_UNSIGNED_RNDZINT (d
);
445 x
= CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
446 set_float_handler (NULL_PTR
);
450 else if (GET_CODE (op
) == CONST_DOUBLE
451 && GET_MODE_CLASS (GET_MODE (op
)) == MODE_FLOAT
452 && GET_MODE_CLASS (mode
) == MODE_INT
453 && width
<= HOST_BITS_PER_WIDE_INT
&& width
> 0)
459 if (setjmp (handler
))
462 set_float_handler (handler
);
464 REAL_VALUE_FROM_CONST_DOUBLE (d
, op
);
469 val
= REAL_VALUE_FIX (d
);
473 val
= REAL_VALUE_UNSIGNED_FIX (d
);
480 set_float_handler (NULL_PTR
);
482 val
= trunc_int_for_mode (val
, mode
);
484 return GEN_INT (val
);
487 /* This was formerly used only for non-IEEE float.
488 eggert@twinsun.com says it is safe for IEEE also. */
491 /* There are some simplifications we can do even if the operands
497 /* (not (not X)) == X, similarly for NEG. */
498 if (GET_CODE (op
) == code
)
503 /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
504 becomes just the MINUS if its mode is MODE. This allows
505 folding switch statements on machines using casesi (such as
507 if (GET_CODE (op
) == TRUNCATE
508 && GET_MODE (XEXP (op
, 0)) == mode
509 && GET_CODE (XEXP (op
, 0)) == MINUS
510 && GET_CODE (XEXP (XEXP (op
, 0), 0)) == LABEL_REF
511 && GET_CODE (XEXP (XEXP (op
, 0), 1)) == LABEL_REF
)
514 #ifdef POINTERS_EXTEND_UNSIGNED
515 if (! POINTERS_EXTEND_UNSIGNED
516 && mode
== Pmode
&& GET_MODE (op
) == ptr_mode
518 return convert_memory_address (Pmode
, op
);
522 #ifdef POINTERS_EXTEND_UNSIGNED
524 if (POINTERS_EXTEND_UNSIGNED
525 && mode
== Pmode
&& GET_MODE (op
) == ptr_mode
527 return convert_memory_address (Pmode
, op
);
539 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
540 and OP1. Return 0 if no simplification is possible.
542 Don't use this for relational operations such as EQ or LT.
543 Use simplify_relational_operation instead. */
546 simplify_binary_operation (code
, mode
, op0
, op1
)
548 enum machine_mode mode
;
551 register HOST_WIDE_INT arg0
, arg1
, arg0s
, arg1s
;
553 int width
= GET_MODE_BITSIZE (mode
);
556 /* Relational operations don't work here. We must know the mode
557 of the operands in order to do the comparison correctly.
558 Assuming a full word can give incorrect results.
559 Consider comparing 128 with -128 in QImode. */
561 if (GET_RTX_CLASS (code
) == '<')
564 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
565 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
566 && GET_CODE (op0
) == CONST_DOUBLE
&& GET_CODE (op1
) == CONST_DOUBLE
567 && mode
== GET_MODE (op0
) && mode
== GET_MODE (op1
))
569 REAL_VALUE_TYPE f0
, f1
, value
;
572 if (setjmp (handler
))
575 set_float_handler (handler
);
577 REAL_VALUE_FROM_CONST_DOUBLE (f0
, op0
);
578 REAL_VALUE_FROM_CONST_DOUBLE (f1
, op1
);
579 f0
= real_value_truncate (mode
, f0
);
580 f1
= real_value_truncate (mode
, f1
);
582 #ifdef REAL_ARITHMETIC
583 #ifndef REAL_INFINITY
584 if (code
== DIV
&& REAL_VALUES_EQUAL (f1
, dconst0
))
587 REAL_ARITHMETIC (value
, rtx_to_tree_code (code
), f0
, f1
);
601 #ifndef REAL_INFINITY
608 value
= MIN (f0
, f1
);
611 value
= MAX (f0
, f1
);
618 value
= real_value_truncate (mode
, value
);
619 set_float_handler (NULL_PTR
);
620 return CONST_DOUBLE_FROM_REAL_VALUE (value
, mode
);
622 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
624 /* We can fold some multi-word operations. */
625 if (GET_MODE_CLASS (mode
) == MODE_INT
626 && width
== HOST_BITS_PER_WIDE_INT
* 2
627 && (GET_CODE (op0
) == CONST_DOUBLE
|| GET_CODE (op0
) == CONST_INT
)
628 && (GET_CODE (op1
) == CONST_DOUBLE
|| GET_CODE (op1
) == CONST_INT
))
630 HOST_WIDE_INT l1
, l2
, h1
, h2
, lv
, hv
;
632 if (GET_CODE (op0
) == CONST_DOUBLE
)
633 l1
= CONST_DOUBLE_LOW (op0
), h1
= CONST_DOUBLE_HIGH (op0
);
635 l1
= INTVAL (op0
), h1
= l1
< 0 ? -1 : 0;
637 if (GET_CODE (op1
) == CONST_DOUBLE
)
638 l2
= CONST_DOUBLE_LOW (op1
), h2
= CONST_DOUBLE_HIGH (op1
);
640 l2
= INTVAL (op1
), h2
= l2
< 0 ? -1 : 0;
645 /* A - B == A + (-B). */
646 neg_double (l2
, h2
, &lv
, &hv
);
649 /* .. fall through ... */
652 add_double (l1
, h1
, l2
, h2
, &lv
, &hv
);
656 mul_double (l1
, h1
, l2
, h2
, &lv
, &hv
);
659 case DIV
: case MOD
: case UDIV
: case UMOD
:
660 /* We'd need to include tree.h to do this and it doesn't seem worth
665 lv
= l1
& l2
, hv
= h1
& h2
;
669 lv
= l1
| l2
, hv
= h1
| h2
;
673 lv
= l1
^ l2
, hv
= h1
^ h2
;
679 && ((unsigned HOST_WIDE_INT
) l1
680 < (unsigned HOST_WIDE_INT
) l2
)))
689 && ((unsigned HOST_WIDE_INT
) l1
690 > (unsigned HOST_WIDE_INT
) l2
)))
697 if ((unsigned HOST_WIDE_INT
) h1
< (unsigned HOST_WIDE_INT
) h2
699 && ((unsigned HOST_WIDE_INT
) l1
700 < (unsigned HOST_WIDE_INT
) l2
)))
707 if ((unsigned HOST_WIDE_INT
) h1
> (unsigned HOST_WIDE_INT
) h2
709 && ((unsigned HOST_WIDE_INT
) l1
710 > (unsigned HOST_WIDE_INT
) l2
)))
716 case LSHIFTRT
: case ASHIFTRT
:
718 case ROTATE
: case ROTATERT
:
719 #ifdef SHIFT_COUNT_TRUNCATED
720 if (SHIFT_COUNT_TRUNCATED
)
721 l2
&= (GET_MODE_BITSIZE (mode
) - 1), h2
= 0;
724 if (h2
!= 0 || l2
< 0 || l2
>= GET_MODE_BITSIZE (mode
))
727 if (code
== LSHIFTRT
|| code
== ASHIFTRT
)
728 rshift_double (l1
, h1
, l2
, GET_MODE_BITSIZE (mode
), &lv
, &hv
,
730 else if (code
== ASHIFT
)
731 lshift_double (l1
, h1
, l2
, GET_MODE_BITSIZE (mode
), &lv
, &hv
, 1);
732 else if (code
== ROTATE
)
733 lrotate_double (l1
, h1
, l2
, GET_MODE_BITSIZE (mode
), &lv
, &hv
);
734 else /* code == ROTATERT */
735 rrotate_double (l1
, h1
, l2
, GET_MODE_BITSIZE (mode
), &lv
, &hv
);
742 return immed_double_const (lv
, hv
, mode
);
745 if (GET_CODE (op0
) != CONST_INT
|| GET_CODE (op1
) != CONST_INT
746 || width
> HOST_BITS_PER_WIDE_INT
|| width
== 0)
748 /* Even if we can't compute a constant result,
749 there are some cases worth simplifying. */
754 /* In IEEE floating point, x+0 is not the same as x. Similarly
755 for the other optimizations below. */
756 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
757 && FLOAT_MODE_P (mode
) && ! flag_fast_math
)
760 if (op1
== CONST0_RTX (mode
))
763 /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */
764 if (GET_CODE (op0
) == NEG
)
765 return simplify_gen_binary (MINUS
, mode
, op1
, XEXP (op0
, 0));
766 else if (GET_CODE (op1
) == NEG
)
767 return simplify_gen_binary (MINUS
, mode
, op0
, XEXP (op1
, 0));
769 /* Handle both-operands-constant cases. We can only add
770 CONST_INTs to constants since the sum of relocatable symbols
771 can't be handled by most assemblers. Don't add CONST_INT
772 to CONST_INT since overflow won't be computed properly if wider
773 than HOST_BITS_PER_WIDE_INT. */
775 if (CONSTANT_P (op0
) && GET_MODE (op0
) != VOIDmode
776 && GET_CODE (op1
) == CONST_INT
)
777 return plus_constant (op0
, INTVAL (op1
));
778 else if (CONSTANT_P (op1
) && GET_MODE (op1
) != VOIDmode
779 && GET_CODE (op0
) == CONST_INT
)
780 return plus_constant (op1
, INTVAL (op0
));
782 /* See if this is something like X * C - X or vice versa or
783 if the multiplication is written as a shift. If so, we can
784 distribute and make a new multiply, shift, or maybe just
785 have X (if C is 2 in the example above). But don't make
786 real multiply if we didn't have one before. */
788 if (! FLOAT_MODE_P (mode
))
790 HOST_WIDE_INT coeff0
= 1, coeff1
= 1;
791 rtx lhs
= op0
, rhs
= op1
;
794 if (GET_CODE (lhs
) == NEG
)
795 coeff0
= -1, lhs
= XEXP (lhs
, 0);
796 else if (GET_CODE (lhs
) == MULT
797 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
)
799 coeff0
= INTVAL (XEXP (lhs
, 1)), lhs
= XEXP (lhs
, 0);
802 else if (GET_CODE (lhs
) == ASHIFT
803 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
804 && INTVAL (XEXP (lhs
, 1)) >= 0
805 && INTVAL (XEXP (lhs
, 1)) < HOST_BITS_PER_WIDE_INT
)
807 coeff0
= ((HOST_WIDE_INT
) 1) << INTVAL (XEXP (lhs
, 1));
811 if (GET_CODE (rhs
) == NEG
)
812 coeff1
= -1, rhs
= XEXP (rhs
, 0);
813 else if (GET_CODE (rhs
) == MULT
814 && GET_CODE (XEXP (rhs
, 1)) == CONST_INT
)
816 coeff1
= INTVAL (XEXP (rhs
, 1)), rhs
= XEXP (rhs
, 0);
819 else if (GET_CODE (rhs
) == ASHIFT
820 && GET_CODE (XEXP (rhs
, 1)) == CONST_INT
821 && INTVAL (XEXP (rhs
, 1)) >= 0
822 && INTVAL (XEXP (rhs
, 1)) < HOST_BITS_PER_WIDE_INT
)
824 coeff1
= ((HOST_WIDE_INT
) 1) << INTVAL (XEXP (rhs
, 1));
828 if (rtx_equal_p (lhs
, rhs
))
830 tem
= simplify_gen_binary (MULT
, mode
, lhs
,
831 GEN_INT (coeff0
+ coeff1
));
832 return (GET_CODE (tem
) == MULT
&& ! had_mult
) ? 0 : tem
;
836 /* If one of the operands is a PLUS or a MINUS, see if we can
837 simplify this by the associative law.
838 Don't use the associative law for floating point.
839 The inaccuracy makes it nonassociative,
840 and subtle programs can break if operations are associated. */
842 if (INTEGRAL_MODE_P (mode
)
843 && (GET_CODE (op0
) == PLUS
|| GET_CODE (op0
) == MINUS
844 || GET_CODE (op1
) == PLUS
|| GET_CODE (op1
) == MINUS
)
845 && (tem
= simplify_plus_minus (code
, mode
, op0
, op1
)) != 0)
851 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
852 using cc0, in which case we want to leave it as a COMPARE
853 so we can distinguish it from a register-register-copy.
855 In IEEE floating point, x-0 is not the same as x. */
857 if ((TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
858 || ! FLOAT_MODE_P (mode
) || flag_fast_math
)
859 && op1
== CONST0_RTX (mode
))
862 /* Do nothing here. */
867 /* None of these optimizations can be done for IEEE
869 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
870 && FLOAT_MODE_P (mode
) && ! flag_fast_math
)
873 /* We can't assume x-x is 0 even with non-IEEE floating point,
874 but since it is zero except in very strange circumstances, we
875 will treat it as zero with -ffast-math. */
876 if (rtx_equal_p (op0
, op1
)
877 && ! side_effects_p (op0
)
878 && (! FLOAT_MODE_P (mode
) || flag_fast_math
))
879 return CONST0_RTX (mode
);
881 /* Change subtraction from zero into negation. */
882 if (op0
== CONST0_RTX (mode
))
883 return gen_rtx_NEG (mode
, op1
);
885 /* (-1 - a) is ~a. */
886 if (op0
== constm1_rtx
)
887 return gen_rtx_NOT (mode
, op1
);
889 /* Subtracting 0 has no effect. */
890 if (op1
== CONST0_RTX (mode
))
893 /* See if this is something like X * C - X or vice versa or
894 if the multiplication is written as a shift. If so, we can
895 distribute and make a new multiply, shift, or maybe just
896 have X (if C is 2 in the example above). But don't make
897 real multiply if we didn't have one before. */
899 if (! FLOAT_MODE_P (mode
))
901 HOST_WIDE_INT coeff0
= 1, coeff1
= 1;
902 rtx lhs
= op0
, rhs
= op1
;
905 if (GET_CODE (lhs
) == NEG
)
906 coeff0
= -1, lhs
= XEXP (lhs
, 0);
907 else if (GET_CODE (lhs
) == MULT
908 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
)
910 coeff0
= INTVAL (XEXP (lhs
, 1)), lhs
= XEXP (lhs
, 0);
913 else if (GET_CODE (lhs
) == ASHIFT
914 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
915 && INTVAL (XEXP (lhs
, 1)) >= 0
916 && INTVAL (XEXP (lhs
, 1)) < HOST_BITS_PER_WIDE_INT
)
918 coeff0
= ((HOST_WIDE_INT
) 1) << INTVAL (XEXP (lhs
, 1));
922 if (GET_CODE (rhs
) == NEG
)
923 coeff1
= - 1, rhs
= XEXP (rhs
, 0);
924 else if (GET_CODE (rhs
) == MULT
925 && GET_CODE (XEXP (rhs
, 1)) == CONST_INT
)
927 coeff1
= INTVAL (XEXP (rhs
, 1)), rhs
= XEXP (rhs
, 0);
930 else if (GET_CODE (rhs
) == ASHIFT
931 && GET_CODE (XEXP (rhs
, 1)) == CONST_INT
932 && INTVAL (XEXP (rhs
, 1)) >= 0
933 && INTVAL (XEXP (rhs
, 1)) < HOST_BITS_PER_WIDE_INT
)
935 coeff1
= ((HOST_WIDE_INT
) 1) << INTVAL (XEXP (rhs
, 1));
939 if (rtx_equal_p (lhs
, rhs
))
941 tem
= simplify_gen_binary (MULT
, mode
, lhs
,
942 GEN_INT (coeff0
- coeff1
));
943 return (GET_CODE (tem
) == MULT
&& ! had_mult
) ? 0 : tem
;
947 /* (a - (-b)) -> (a + b). */
948 if (GET_CODE (op1
) == NEG
)
949 return simplify_gen_binary (PLUS
, mode
, op0
, XEXP (op1
, 0));
951 /* If one of the operands is a PLUS or a MINUS, see if we can
952 simplify this by the associative law.
953 Don't use the associative law for floating point.
954 The inaccuracy makes it nonassociative,
955 and subtle programs can break if operations are associated. */
957 if (INTEGRAL_MODE_P (mode
)
958 && (GET_CODE (op0
) == PLUS
|| GET_CODE (op0
) == MINUS
959 || GET_CODE (op1
) == PLUS
|| GET_CODE (op1
) == MINUS
)
960 && (tem
= simplify_plus_minus (code
, mode
, op0
, op1
)) != 0)
963 /* Don't let a relocatable value get a negative coeff. */
964 if (GET_CODE (op1
) == CONST_INT
&& GET_MODE (op0
) != VOIDmode
)
965 return plus_constant (op0
, - INTVAL (op1
));
967 /* (x - (x & y)) -> (x & ~y) */
968 if (GET_CODE (op1
) == AND
)
970 if (rtx_equal_p (op0
, XEXP (op1
, 0)))
971 return simplify_gen_binary (AND
, mode
, op0
,
972 gen_rtx_NOT (mode
, XEXP (op1
, 1)));
973 if (rtx_equal_p (op0
, XEXP (op1
, 1)))
974 return simplify_gen_binary (AND
, mode
, op0
,
975 gen_rtx_NOT (mode
, XEXP (op1
, 0)));
980 if (op1
== constm1_rtx
)
982 tem
= simplify_unary_operation (NEG
, mode
, op0
, mode
);
984 return tem
? tem
: gen_rtx_NEG (mode
, op0
);
987 /* In IEEE floating point, x*0 is not always 0. */
988 if ((TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
989 || ! FLOAT_MODE_P (mode
) || flag_fast_math
)
990 && op1
== CONST0_RTX (mode
)
991 && ! side_effects_p (op0
))
994 /* In IEEE floating point, x*1 is not equivalent to x for nans.
995 However, ANSI says we can drop signals,
996 so we can do this anyway. */
997 if (op1
== CONST1_RTX (mode
))
1000 /* Convert multiply by constant power of two into shift unless
1001 we are still generating RTL. This test is a kludge. */
1002 if (GET_CODE (op1
) == CONST_INT
1003 && (val
= exact_log2 (INTVAL (op1
))) >= 0
1004 /* If the mode is larger than the host word size, and the
1005 uppermost bit is set, then this isn't a power of two due
1006 to implicit sign extension. */
1007 && (width
<= HOST_BITS_PER_WIDE_INT
1008 || val
!= HOST_BITS_PER_WIDE_INT
- 1)
1009 && ! rtx_equal_function_value_matters
)
1010 return gen_rtx_ASHIFT (mode
, op0
, GEN_INT (val
));
1012 if (GET_CODE (op1
) == CONST_DOUBLE
1013 && GET_MODE_CLASS (GET_MODE (op1
)) == MODE_FLOAT
)
1017 int op1is2
, op1ism1
;
1019 if (setjmp (handler
))
1022 set_float_handler (handler
);
1023 REAL_VALUE_FROM_CONST_DOUBLE (d
, op1
);
1024 op1is2
= REAL_VALUES_EQUAL (d
, dconst2
);
1025 op1ism1
= REAL_VALUES_EQUAL (d
, dconstm1
);
1026 set_float_handler (NULL_PTR
);
1028 /* x*2 is x+x and x*(-1) is -x */
1029 if (op1is2
&& GET_MODE (op0
) == mode
)
1030 return gen_rtx_PLUS (mode
, op0
, copy_rtx (op0
));
1032 else if (op1ism1
&& GET_MODE (op0
) == mode
)
1033 return gen_rtx_NEG (mode
, op0
);
1038 if (op1
== const0_rtx
)
1040 if (GET_CODE (op1
) == CONST_INT
1041 && (INTVAL (op1
) & GET_MODE_MASK (mode
)) == GET_MODE_MASK (mode
))
1043 if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1045 /* A | (~A) -> -1 */
1046 if (((GET_CODE (op0
) == NOT
&& rtx_equal_p (XEXP (op0
, 0), op1
))
1047 || (GET_CODE (op1
) == NOT
&& rtx_equal_p (XEXP (op1
, 0), op0
)))
1048 && ! side_effects_p (op0
)
1049 && GET_MODE_CLASS (mode
) != MODE_CC
)
1054 if (op1
== const0_rtx
)
1056 if (GET_CODE (op1
) == CONST_INT
1057 && (INTVAL (op1
) & GET_MODE_MASK (mode
)) == GET_MODE_MASK (mode
))
1058 return gen_rtx_NOT (mode
, op0
);
1059 if (op0
== op1
&& ! side_effects_p (op0
)
1060 && GET_MODE_CLASS (mode
) != MODE_CC
)
1065 if (op1
== const0_rtx
&& ! side_effects_p (op0
))
1067 if (GET_CODE (op1
) == CONST_INT
1068 && (INTVAL (op1
) & GET_MODE_MASK (mode
)) == GET_MODE_MASK (mode
))
1070 if (op0
== op1
&& ! side_effects_p (op0
)
1071 && GET_MODE_CLASS (mode
) != MODE_CC
)
1074 if (((GET_CODE (op0
) == NOT
&& rtx_equal_p (XEXP (op0
, 0), op1
))
1075 || (GET_CODE (op1
) == NOT
&& rtx_equal_p (XEXP (op1
, 0), op0
)))
1076 && ! side_effects_p (op0
)
1077 && GET_MODE_CLASS (mode
) != MODE_CC
)
1082 /* Convert divide by power of two into shift (divide by 1 handled
1084 if (GET_CODE (op1
) == CONST_INT
1085 && (arg1
= exact_log2 (INTVAL (op1
))) > 0)
1086 return gen_rtx_LSHIFTRT (mode
, op0
, GEN_INT (arg1
));
1088 /* ... fall through ... */
1091 if (op1
== CONST1_RTX (mode
))
1094 /* In IEEE floating point, 0/x is not always 0. */
1095 if ((TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
1096 || ! FLOAT_MODE_P (mode
) || flag_fast_math
)
1097 && op0
== CONST0_RTX (mode
)
1098 && ! side_effects_p (op1
))
1101 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1102 /* Change division by a constant into multiplication. Only do
1103 this with -ffast-math until an expert says it is safe in
1105 else if (GET_CODE (op1
) == CONST_DOUBLE
1106 && GET_MODE_CLASS (GET_MODE (op1
)) == MODE_FLOAT
1107 && op1
!= CONST0_RTX (mode
)
1111 REAL_VALUE_FROM_CONST_DOUBLE (d
, op1
);
1113 if (! REAL_VALUES_EQUAL (d
, dconst0
))
1115 #if defined (REAL_ARITHMETIC)
1116 REAL_ARITHMETIC (d
, rtx_to_tree_code (DIV
), dconst1
, d
);
1117 return gen_rtx_MULT (mode
, op0
,
1118 CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
));
1121 gen_rtx_MULT (mode
, op0
,
1122 CONST_DOUBLE_FROM_REAL_VALUE (1./d
, mode
));
1130 /* Handle modulus by power of two (mod with 1 handled below). */
1131 if (GET_CODE (op1
) == CONST_INT
1132 && exact_log2 (INTVAL (op1
)) > 0)
1133 return gen_rtx_AND (mode
, op0
, GEN_INT (INTVAL (op1
) - 1));
1135 /* ... fall through ... */
1138 if ((op0
== const0_rtx
|| op1
== const1_rtx
)
1139 && ! side_effects_p (op0
) && ! side_effects_p (op1
))
1145 /* Rotating ~0 always results in ~0. */
1146 if (GET_CODE (op0
) == CONST_INT
&& width
<= HOST_BITS_PER_WIDE_INT
1147 && (unsigned HOST_WIDE_INT
) INTVAL (op0
) == GET_MODE_MASK (mode
)
1148 && ! side_effects_p (op1
))
1151 /* ... fall through ... */
1156 if (op1
== const0_rtx
)
1158 if (op0
== const0_rtx
&& ! side_effects_p (op1
))
1163 if (width
<= HOST_BITS_PER_WIDE_INT
&& GET_CODE (op1
) == CONST_INT
1164 && INTVAL (op1
) == (HOST_WIDE_INT
) 1 << (width
-1)
1165 && ! side_effects_p (op0
))
1167 else if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1172 if (width
<= HOST_BITS_PER_WIDE_INT
&& GET_CODE (op1
) == CONST_INT
1173 && ((unsigned HOST_WIDE_INT
) INTVAL (op1
)
1174 == (unsigned HOST_WIDE_INT
) GET_MODE_MASK (mode
) >> 1)
1175 && ! side_effects_p (op0
))
1177 else if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1182 if (op1
== const0_rtx
&& ! side_effects_p (op0
))
1184 else if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1189 if (op1
== constm1_rtx
&& ! side_effects_p (op0
))
1191 else if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1202 /* Get the integer argument values in two forms:
1203 zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S. */
1205 arg0
= INTVAL (op0
);
1206 arg1
= INTVAL (op1
);
1208 if (width
< HOST_BITS_PER_WIDE_INT
)
1210 arg0
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
1211 arg1
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
1214 if (arg0s
& ((HOST_WIDE_INT
) 1 << (width
- 1)))
1215 arg0s
|= ((HOST_WIDE_INT
) (-1) << width
);
1218 if (arg1s
& ((HOST_WIDE_INT
) 1 << (width
- 1)))
1219 arg1s
|= ((HOST_WIDE_INT
) (-1) << width
);
1227 /* Compute the value of the arithmetic. */
1232 val
= arg0s
+ arg1s
;
1236 val
= arg0s
- arg1s
;
1240 val
= arg0s
* arg1s
;
1246 val
= arg0s
/ arg1s
;
1252 val
= arg0s
% arg1s
;
1258 val
= (unsigned HOST_WIDE_INT
) arg0
/ arg1
;
1264 val
= (unsigned HOST_WIDE_INT
) arg0
% arg1
;
1280 /* If shift count is undefined, don't fold it; let the machine do
1281 what it wants. But truncate it if the machine will do that. */
1285 #ifdef SHIFT_COUNT_TRUNCATED
1286 if (SHIFT_COUNT_TRUNCATED
)
1290 val
= ((unsigned HOST_WIDE_INT
) arg0
) >> arg1
;
1297 #ifdef SHIFT_COUNT_TRUNCATED
1298 if (SHIFT_COUNT_TRUNCATED
)
1302 val
= ((unsigned HOST_WIDE_INT
) arg0
) << arg1
;
1309 #ifdef SHIFT_COUNT_TRUNCATED
1310 if (SHIFT_COUNT_TRUNCATED
)
1314 val
= arg0s
>> arg1
;
1316 /* Bootstrap compiler may not have sign extended the right shift.
1317 Manually extend the sign to insure bootstrap cc matches gcc. */
1318 if (arg0s
< 0 && arg1
> 0)
1319 val
|= ((HOST_WIDE_INT
) -1) << (HOST_BITS_PER_WIDE_INT
- arg1
);
1328 val
= ((((unsigned HOST_WIDE_INT
) arg0
) << (width
- arg1
))
1329 | (((unsigned HOST_WIDE_INT
) arg0
) >> arg1
));
1337 val
= ((((unsigned HOST_WIDE_INT
) arg0
) << arg1
)
1338 | (((unsigned HOST_WIDE_INT
) arg0
) >> (width
- arg1
)));
1342 /* Do nothing here. */
1346 val
= arg0s
<= arg1s
? arg0s
: arg1s
;
1350 val
= ((unsigned HOST_WIDE_INT
) arg0
1351 <= (unsigned HOST_WIDE_INT
) arg1
? arg0
: arg1
);
1355 val
= arg0s
> arg1s
? arg0s
: arg1s
;
1359 val
= ((unsigned HOST_WIDE_INT
) arg0
1360 > (unsigned HOST_WIDE_INT
) arg1
? arg0
: arg1
);
1367 val
= trunc_int_for_mode (val
, mode
);
1369 return GEN_INT (val
);
1372 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
1375 Rather than test for specific case, we do this by a brute-force method
1376 and do all possible simplifications until no more changes occur. Then
1377 we rebuild the operation. */
1380 simplify_plus_minus (code
, mode
, op0
, op1
)
1382 enum machine_mode mode
;
1388 int n_ops
= 2, input_ops
= 2, input_consts
= 0, n_consts
= 0;
1389 int first
= 1, negate
= 0, changed
;
1392 bzero ((char *) ops
, sizeof ops
);
1394 /* Set up the two operands and then expand them until nothing has been
1395 changed. If we run out of room in our array, give up; this should
1396 almost never happen. */
1398 ops
[0] = op0
, ops
[1] = op1
, negs
[0] = 0, negs
[1] = (code
== MINUS
);
1405 for (i
= 0; i
< n_ops
; i
++)
1406 switch (GET_CODE (ops
[i
]))
1413 ops
[n_ops
] = XEXP (ops
[i
], 1);
1414 negs
[n_ops
++] = GET_CODE (ops
[i
]) == MINUS
? !negs
[i
] : negs
[i
];
1415 ops
[i
] = XEXP (ops
[i
], 0);
1421 ops
[i
] = XEXP (ops
[i
], 0);
1422 negs
[i
] = ! negs
[i
];
1427 ops
[i
] = XEXP (ops
[i
], 0);
1433 /* ~a -> (-a - 1) */
1436 ops
[n_ops
] = constm1_rtx
;
1437 negs
[n_ops
++] = negs
[i
];
1438 ops
[i
] = XEXP (ops
[i
], 0);
1439 negs
[i
] = ! negs
[i
];
1446 ops
[i
] = GEN_INT (- INTVAL (ops
[i
])), negs
[i
] = 0, changed
= 1;
1454 /* If we only have two operands, we can't do anything. */
1458 /* Now simplify each pair of operands until nothing changes. The first
1459 time through just simplify constants against each other. */
1466 for (i
= 0; i
< n_ops
- 1; i
++)
1467 for (j
= i
+ 1; j
< n_ops
; j
++)
1468 if (ops
[i
] != 0 && ops
[j
] != 0
1469 && (! first
|| (CONSTANT_P (ops
[i
]) && CONSTANT_P (ops
[j
]))))
1471 rtx lhs
= ops
[i
], rhs
= ops
[j
];
1472 enum rtx_code ncode
= PLUS
;
1474 if (negs
[i
] && ! negs
[j
])
1475 lhs
= ops
[j
], rhs
= ops
[i
], ncode
= MINUS
;
1476 else if (! negs
[i
] && negs
[j
])
1479 tem
= simplify_binary_operation (ncode
, mode
, lhs
, rhs
);
1482 ops
[i
] = tem
, ops
[j
] = 0;
1483 negs
[i
] = negs
[i
] && negs
[j
];
1484 if (GET_CODE (tem
) == NEG
)
1485 ops
[i
] = XEXP (tem
, 0), negs
[i
] = ! negs
[i
];
1487 if (GET_CODE (ops
[i
]) == CONST_INT
&& negs
[i
])
1488 ops
[i
] = GEN_INT (- INTVAL (ops
[i
])), negs
[i
] = 0;
1496 /* Pack all the operands to the lower-numbered entries and give up if
1497 we didn't reduce the number of operands we had. Make sure we
1498 count a CONST as two operands. If we have the same number of
1499 operands, but have made more CONSTs than we had, this is also
1500 an improvement, so accept it. */
1502 for (i
= 0, j
= 0; j
< n_ops
; j
++)
1505 ops
[i
] = ops
[j
], negs
[i
++] = negs
[j
];
1506 if (GET_CODE (ops
[j
]) == CONST
)
1510 if (i
+ n_consts
> input_ops
1511 || (i
+ n_consts
== input_ops
&& n_consts
<= input_consts
))
1516 /* If we have a CONST_INT, put it last. */
1517 for (i
= 0; i
< n_ops
- 1; i
++)
1518 if (GET_CODE (ops
[i
]) == CONST_INT
)
1520 tem
= ops
[n_ops
- 1], ops
[n_ops
- 1] = ops
[i
] , ops
[i
] = tem
;
1521 j
= negs
[n_ops
- 1], negs
[n_ops
- 1] = negs
[i
], negs
[i
] = j
;
1524 /* Put a non-negated operand first. If there aren't any, make all
1525 operands positive and negate the whole thing later. */
1526 for (i
= 0; i
< n_ops
&& negs
[i
]; i
++)
1531 for (i
= 0; i
< n_ops
; i
++)
1537 tem
= ops
[0], ops
[0] = ops
[i
], ops
[i
] = tem
;
1538 j
= negs
[0], negs
[0] = negs
[i
], negs
[i
] = j
;
1541 /* Now make the result by performing the requested operations. */
1543 for (i
= 1; i
< n_ops
; i
++)
1544 result
= simplify_gen_binary (negs
[i
] ? MINUS
: PLUS
, mode
, result
, ops
[i
]);
1546 return negate
? gen_rtx_NEG (mode
, result
) : result
;
1551 rtx op0
, op1
; /* Input */
1552 int equal
, op0lt
, op1lt
; /* Output */
1556 check_fold_consts (data
)
1559 struct cfc_args
*args
= (struct cfc_args
*) data
;
1560 REAL_VALUE_TYPE d0
, d1
;
1562 REAL_VALUE_FROM_CONST_DOUBLE (d0
, args
->op0
);
1563 REAL_VALUE_FROM_CONST_DOUBLE (d1
, args
->op1
);
1564 args
->equal
= REAL_VALUES_EQUAL (d0
, d1
);
1565 args
->op0lt
= REAL_VALUES_LESS (d0
, d1
);
1566 args
->op1lt
= REAL_VALUES_LESS (d1
, d0
);
1569 /* Like simplify_binary_operation except used for relational operators.
1570 MODE is the mode of the operands, not that of the result. If MODE
1571 is VOIDmode, both operands must also be VOIDmode and we compare the
1572 operands in "infinite precision".
1574 If no simplification is possible, this function returns zero. Otherwise,
1575 it returns either const_true_rtx or const0_rtx. */
1578 simplify_relational_operation (code
, mode
, op0
, op1
)
1580 enum machine_mode mode
;
1583 int equal
, op0lt
, op0ltu
, op1lt
, op1ltu
;
1586 /* If op0 is a compare, extract the comparison arguments from it. */
1587 if (GET_CODE (op0
) == COMPARE
&& op1
== const0_rtx
)
1588 op1
= XEXP (op0
, 1), op0
= XEXP (op0
, 0);
1590 /* We can't simplify MODE_CC values since we don't know what the
1591 actual comparison is. */
1592 if (GET_MODE_CLASS (GET_MODE (op0
)) == MODE_CC
1599 /* Make sure the constant is second. */
1600 if ((CONSTANT_P (op0
) && ! CONSTANT_P (op1
))
1601 || (GET_CODE (op0
) == CONST_INT
&& GET_CODE (op1
) != CONST_INT
))
1603 tem
= op0
, op0
= op1
, op1
= tem
;
1604 code
= swap_condition (code
);
1607 /* For integer comparisons of A and B maybe we can simplify A - B and can
1608 then simplify a comparison of that with zero. If A and B are both either
1609 a register or a CONST_INT, this can't help; testing for these cases will
1610 prevent infinite recursion here and speed things up.
1612 If CODE is an unsigned comparison, then we can never do this optimization,
1613 because it gives an incorrect result if the subtraction wraps around zero.
1614 ANSI C defines unsigned operations such that they never overflow, and
1615 thus such cases can not be ignored. */
1617 if (INTEGRAL_MODE_P (mode
) && op1
!= const0_rtx
1618 && ! ((GET_CODE (op0
) == REG
|| GET_CODE (op0
) == CONST_INT
)
1619 && (GET_CODE (op1
) == REG
|| GET_CODE (op1
) == CONST_INT
))
1620 && 0 != (tem
= simplify_binary_operation (MINUS
, mode
, op0
, op1
))
1621 && code
!= GTU
&& code
!= GEU
&& code
!= LTU
&& code
!= LEU
)
1622 return simplify_relational_operation (signed_condition (code
),
1623 mode
, tem
, const0_rtx
);
1625 /* For non-IEEE floating-point, if the two operands are equal, we know the
1627 if (rtx_equal_p (op0
, op1
)
1628 && (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
1629 || ! FLOAT_MODE_P (GET_MODE (op0
)) || flag_fast_math
))
1630 equal
= 1, op0lt
= 0, op0ltu
= 0, op1lt
= 0, op1ltu
= 0;
1632 /* If the operands are floating-point constants, see if we can fold
1634 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1635 else if (GET_CODE (op0
) == CONST_DOUBLE
&& GET_CODE (op1
) == CONST_DOUBLE
1636 && GET_MODE_CLASS (GET_MODE (op0
)) == MODE_FLOAT
)
1638 struct cfc_args args
;
1640 /* Setup input for check_fold_consts() */
1644 if (do_float_handler(check_fold_consts
, (PTR
) &args
) == 0)
1645 /* We got an exception from check_fold_consts() */
1648 /* Receive output from check_fold_consts() */
1650 op0lt
= op0ltu
= args
.op0lt
;
1651 op1lt
= op1ltu
= args
.op1lt
;
1653 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1655 /* Otherwise, see if the operands are both integers. */
1656 else if ((GET_MODE_CLASS (mode
) == MODE_INT
|| mode
== VOIDmode
)
1657 && (GET_CODE (op0
) == CONST_DOUBLE
|| GET_CODE (op0
) == CONST_INT
)
1658 && (GET_CODE (op1
) == CONST_DOUBLE
|| GET_CODE (op1
) == CONST_INT
))
1660 int width
= GET_MODE_BITSIZE (mode
);
1661 HOST_WIDE_INT l0s
, h0s
, l1s
, h1s
;
1662 unsigned HOST_WIDE_INT l0u
, h0u
, l1u
, h1u
;
1664 /* Get the two words comprising each integer constant. */
1665 if (GET_CODE (op0
) == CONST_DOUBLE
)
1667 l0u
= l0s
= CONST_DOUBLE_LOW (op0
);
1668 h0u
= h0s
= CONST_DOUBLE_HIGH (op0
);
1672 l0u
= l0s
= INTVAL (op0
);
1673 h0u
= h0s
= l0s
< 0 ? -1 : 0;
1676 if (GET_CODE (op1
) == CONST_DOUBLE
)
1678 l1u
= l1s
= CONST_DOUBLE_LOW (op1
);
1679 h1u
= h1s
= CONST_DOUBLE_HIGH (op1
);
1683 l1u
= l1s
= INTVAL (op1
);
1684 h1u
= h1s
= l1s
< 0 ? -1 : 0;
1687 /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
1688 we have to sign or zero-extend the values. */
1689 if (width
!= 0 && width
<= HOST_BITS_PER_WIDE_INT
)
1690 h0u
= h1u
= 0, h0s
= l0s
< 0 ? -1 : 0, h1s
= l1s
< 0 ? -1 : 0;
1692 if (width
!= 0 && width
< HOST_BITS_PER_WIDE_INT
)
1694 l0u
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
1695 l1u
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
1697 if (l0s
& ((HOST_WIDE_INT
) 1 << (width
- 1)))
1698 l0s
|= ((HOST_WIDE_INT
) (-1) << width
);
1700 if (l1s
& ((HOST_WIDE_INT
) 1 << (width
- 1)))
1701 l1s
|= ((HOST_WIDE_INT
) (-1) << width
);
1704 equal
= (h0u
== h1u
&& l0u
== l1u
);
1705 op0lt
= (h0s
< h1s
|| (h0s
== h1s
&& l0s
< l1s
));
1706 op1lt
= (h1s
< h0s
|| (h1s
== h0s
&& l1s
< l0s
));
1707 op0ltu
= (h0u
< h1u
|| (h0u
== h1u
&& l0u
< l1u
));
1708 op1ltu
= (h1u
< h0u
|| (h1u
== h0u
&& l1u
< l0u
));
1711 /* Otherwise, there are some code-specific tests we can make. */
1717 /* References to the frame plus a constant or labels cannot
1718 be zero, but a SYMBOL_REF can due to #pragma weak. */
1719 if (((NONZERO_BASE_PLUS_P (op0
) && op1
== const0_rtx
)
1720 || GET_CODE (op0
) == LABEL_REF
)
1721 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1722 /* On some machines, the ap reg can be 0 sometimes. */
1723 && op0
!= arg_pointer_rtx
1730 if (((NONZERO_BASE_PLUS_P (op0
) && op1
== const0_rtx
)
1731 || GET_CODE (op0
) == LABEL_REF
)
1732 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1733 && op0
!= arg_pointer_rtx
1736 return const_true_rtx
;
1740 /* Unsigned values are never negative. */
1741 if (op1
== const0_rtx
)
1742 return const_true_rtx
;
1746 if (op1
== const0_rtx
)
1751 /* Unsigned values are never greater than the largest
1753 if (GET_CODE (op1
) == CONST_INT
1754 && (unsigned HOST_WIDE_INT
) INTVAL (op1
) == GET_MODE_MASK (mode
)
1755 && INTEGRAL_MODE_P (mode
))
1756 return const_true_rtx
;
1760 if (GET_CODE (op1
) == CONST_INT
1761 && (unsigned HOST_WIDE_INT
) INTVAL (op1
) == GET_MODE_MASK (mode
)
1762 && INTEGRAL_MODE_P (mode
))
1773 /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
1778 return equal
? const_true_rtx
: const0_rtx
;
1780 return ! equal
? const_true_rtx
: const0_rtx
;
1782 return op0lt
? const_true_rtx
: const0_rtx
;
1784 return op1lt
? const_true_rtx
: const0_rtx
;
1786 return op0ltu
? const_true_rtx
: const0_rtx
;
1788 return op1ltu
? const_true_rtx
: const0_rtx
;
1790 return equal
|| op0lt
? const_true_rtx
: const0_rtx
;
1792 return equal
|| op1lt
? const_true_rtx
: const0_rtx
;
1794 return equal
|| op0ltu
? const_true_rtx
: const0_rtx
;
1796 return equal
|| op1ltu
? const_true_rtx
: const0_rtx
;
1802 /* Simplify CODE, an operation with result mode MODE and three operands,
1803 OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
1804 a constant. Return 0 if no simplifications is possible. */
1807 simplify_ternary_operation (code
, mode
, op0_mode
, op0
, op1
, op2
)
1809 enum machine_mode mode
, op0_mode
;
1812 int width
= GET_MODE_BITSIZE (mode
);
1814 /* VOIDmode means "infinite" precision. */
1816 width
= HOST_BITS_PER_WIDE_INT
;
1822 if (GET_CODE (op0
) == CONST_INT
1823 && GET_CODE (op1
) == CONST_INT
1824 && GET_CODE (op2
) == CONST_INT
1825 && INTVAL (op1
) + INTVAL (op2
) <= GET_MODE_BITSIZE (op0_mode
)
1826 && width
<= HOST_BITS_PER_WIDE_INT
)
1828 /* Extracting a bit-field from a constant */
1829 HOST_WIDE_INT val
= INTVAL (op0
);
1831 if (BITS_BIG_ENDIAN
)
1832 val
>>= (GET_MODE_BITSIZE (op0_mode
)
1833 - INTVAL (op2
) - INTVAL (op1
));
1835 val
>>= INTVAL (op2
);
1837 if (HOST_BITS_PER_WIDE_INT
!= INTVAL (op1
))
1839 /* First zero-extend. */
1840 val
&= ((HOST_WIDE_INT
) 1 << INTVAL (op1
)) - 1;
1841 /* If desired, propagate sign bit. */
1842 if (code
== SIGN_EXTRACT
1843 && (val
& ((HOST_WIDE_INT
) 1 << (INTVAL (op1
) - 1))))
1844 val
|= ~ (((HOST_WIDE_INT
) 1 << INTVAL (op1
)) - 1);
1847 /* Clear the bits that don't belong in our mode,
1848 unless they and our sign bit are all one.
1849 So we get either a reasonable negative value or a reasonable
1850 unsigned value for this mode. */
1851 if (width
< HOST_BITS_PER_WIDE_INT
1852 && ((val
& ((HOST_WIDE_INT
) (-1) << (width
- 1)))
1853 != ((HOST_WIDE_INT
) (-1) << (width
- 1))))
1854 val
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
1856 return GEN_INT (val
);
1861 if (GET_CODE (op0
) == CONST_INT
)
1862 return op0
!= const0_rtx
? op1
: op2
;
1864 /* Convert a == b ? b : a to "a". */
1865 if (GET_CODE (op0
) == NE
&& ! side_effects_p (op0
)
1866 && rtx_equal_p (XEXP (op0
, 0), op1
)
1867 && rtx_equal_p (XEXP (op0
, 1), op2
))
1869 else if (GET_CODE (op0
) == EQ
&& ! side_effects_p (op0
)
1870 && rtx_equal_p (XEXP (op0
, 1), op1
)
1871 && rtx_equal_p (XEXP (op0
, 0), op2
))
1873 else if (GET_RTX_CLASS (GET_CODE (op0
)) == '<' && ! side_effects_p (op0
))
1876 temp
= simplify_relational_operation (GET_CODE (op0
), op0_mode
,
1877 XEXP (op0
, 0), XEXP (op0
, 1));
1878 /* See if any simplifications were possible. */
1879 if (temp
== const0_rtx
)
1881 else if (temp
== const1_rtx
)
1893 /* Simplify X, an rtx expression.
1895 Return the simplified expression or NULL if no simplifications
1898 This is the preferred entry point into the simplification routines;
1899 however, we still allow passes to call the more specific routines.
1901 Right now GCC has three (yes, three) major bodies of RTL simplficiation
1902 code that need to be unified.
1904 1. fold_rtx in cse.c. This code uses various CSE specific
1905 information to aid in RTL simplification.
1907 2. simplify_rtx in combine.c. Similar to fold_rtx, except that
1908 it uses combine specific information to aid in RTL
1911 3. The routines in this file.
1914 Long term we want to only have one body of simplification code; to
1915 get to that state I recommend the following steps:
1917 1. Pour over fold_rtx & simplify_rtx and move any simplifications
1918 which are not pass dependent state into these routines.
1920 2. As code is moved by #1, change fold_rtx & simplify_rtx to
1921 use this routine whenever possible.
1923 3. Allow for pass dependent state to be provided to these
1924 routines and add simplifications based on the pass dependent
1925 state. Remove code from cse.c & combine.c that becomes
1928 It will take time, but ultimately the compiler will be easier to
1929 maintain and improve. It's totally silly that when we add a
1930 simplification that it needs to be added to 4 places (3 for RTL
1931 simplification and 1 for tree simplification. */
1938 enum machine_mode mode
;
1940 mode
= GET_MODE (x
);
1941 code
= GET_CODE (x
);
1943 switch (GET_RTX_CLASS (code
))
1946 return simplify_unary_operation (code
, mode
,
1947 XEXP (x
, 0), GET_MODE (XEXP (x
, 0)));
1950 return simplify_binary_operation (code
, mode
, XEXP (x
, 0), XEXP (x
, 1));
1954 return simplify_ternary_operation (code
, mode
, GET_MODE (XEXP (x
, 0)),
1955 XEXP (x
, 0), XEXP (x
, 1), XEXP (x
, 2));
1958 return simplify_relational_operation (code
, GET_MODE (XEXP (x
, 0)),
1959 XEXP (x
, 0), XEXP (x
, 1));
1965 static int entry_and_rtx_equal_p
PARAMS ((const void *, const void *));
1966 static unsigned int get_value_hash
PARAMS ((const void *));
1967 static struct elt_list
*new_elt_list
PARAMS ((struct elt_list
*, cselib_val
*));
1968 static struct elt_loc_list
*new_elt_loc_list
PARAMS ((struct elt_loc_list
*, rtx
));
1969 static void unchain_one_value
PARAMS ((cselib_val
*));
1970 static void unchain_one_elt_list
PARAMS ((struct elt_list
**));
1971 static void unchain_one_elt_loc_list
PARAMS ((struct elt_loc_list
**));
1972 static void clear_table
PARAMS ((void));
1973 static int check_value_useless
PARAMS ((cselib_val
*));
1974 static int discard_useless_locs
PARAMS ((void **, void *));
1975 static int discard_useless_values
PARAMS ((void **, void *));
1976 static void remove_useless_values
PARAMS ((void));
1977 static unsigned int hash_rtx
PARAMS ((rtx
, enum machine_mode
, int));
1978 static cselib_val
*new_cselib_val
PARAMS ((unsigned int, enum machine_mode
));
1979 static void add_mem_for_addr
PARAMS ((cselib_val
*, cselib_val
*, rtx
));
1980 static cselib_val
*cselib_lookup_mem
PARAMS ((rtx
, int));
1981 static rtx cselib_subst_to_values
PARAMS ((rtx
));
1982 static void cselib_invalidate_regno
PARAMS ((int, enum machine_mode
));
1983 static int cselib_mem_conflict_p
PARAMS ((rtx
, rtx
));
1984 static int cselib_invalidate_mem_1
PARAMS ((void **, void *));
1985 static void cselib_invalidate_mem
PARAMS ((rtx
));
1986 static void cselib_invalidate_rtx
PARAMS ((rtx
, rtx
, void *));
1987 static void cselib_record_set
PARAMS ((rtx
, cselib_val
*, cselib_val
*));
1988 static void cselib_record_sets
PARAMS ((rtx
));
1990 /* There are three ways in which cselib can look up an rtx:
1991 - for a REG, the reg_values table (which is indexed by regno) is used
1992 - for a MEM, we recursively look up its address and then follow the
1993 addr_list of that value
1994 - for everything else, we compute a hash value and go through the hash
1995 table. Since different rtx's can still have the same hash value,
1996 this involves walking the table entries for a given value and comparing
1997 the locations of the entries with the rtx we are looking up. */
1999 /* A table that enables us to look up elts by their value. */
2000 static htab_t hash_table
;
2002 /* This is a global so we don't have to pass this through every function.
2003 It is used in new_elt_loc_list to set SETTING_INSN. */
2004 static rtx cselib_current_insn
;
2006 /* Every new unknown value gets a unique number. */
2007 static unsigned int next_unknown_value
;
2009 /* The number of registers we had when the varrays were last resized. */
2010 static int cselib_nregs
;
2012 /* Count values without known locations. Whenever this grows too big, we
2013 remove these useless values from the table. */
2014 static int n_useless_values
;
2016 /* Number of useless values before we remove them from the hash table. */
2017 #define MAX_USELESS_VALUES 32
2019 /* This table maps from register number to values. It does not contain
2020 pointers to cselib_val structures, but rather elt_lists. The purpose is
2021 to be able to refer to the same register in different modes. */
2022 static varray_type reg_values
;
2023 #define REG_VALUES(I) VARRAY_ELT_LIST (reg_values, (I))
2025 /* We pass this to cselib_invalidate_mem to invalidate all of
2026 memory for a non-const call instruction. */
2029 /* Memory for our structures is allocated from this obstack. */
2030 static struct obstack cselib_obstack
;
2032 /* Used to quickly free all memory. */
2033 static char *cselib_startobj
;
2035 /* Caches for unused structures. */
2036 static cselib_val
*empty_vals
;
2037 static struct elt_list
*empty_elt_lists
;
2038 static struct elt_loc_list
*empty_elt_loc_lists
;
2040 /* Allocate a struct elt_list and fill in its two elements with the
2042 static struct elt_list
*
2043 new_elt_list (next
, elt
)
2044 struct elt_list
*next
;
2047 struct elt_list
*el
= empty_elt_lists
;
2049 empty_elt_lists
= el
->next
;
2051 el
= (struct elt_list
*) obstack_alloc (&cselib_obstack
,
2052 sizeof (struct elt_list
));
2058 /* Allocate a struct elt_loc_list and fill in its two elements with the
2060 static struct elt_loc_list
*
2061 new_elt_loc_list (next
, loc
)
2062 struct elt_loc_list
*next
;
2065 struct elt_loc_list
*el
= empty_elt_loc_lists
;
2067 empty_elt_loc_lists
= el
->next
;
2069 el
= (struct elt_loc_list
*) obstack_alloc (&cselib_obstack
,
2070 sizeof (struct elt_loc_list
));
2073 el
->setting_insn
= cselib_current_insn
;
2077 /* The elt_list at *PL is no longer needed. Unchain it and free its
2080 unchain_one_elt_list (pl
)
2081 struct elt_list
**pl
;
2083 struct elt_list
*l
= *pl
;
2085 l
->next
= empty_elt_lists
;
2086 empty_elt_lists
= l
;
2089 /* Likewise for elt_loc_lists. */
2091 unchain_one_elt_loc_list (pl
)
2092 struct elt_loc_list
**pl
;
2094 struct elt_loc_list
*l
= *pl
;
2096 l
->next
= empty_elt_loc_lists
;
2097 empty_elt_loc_lists
= l
;
2100 /* Likewise for cselib_vals. This also frees the addr_list associated with
2103 unchain_one_value (v
)
2106 while (v
->addr_list
)
2107 unchain_one_elt_list (&v
->addr_list
);
2109 v
->u
.next_free
= empty_vals
;
2113 /* Remove all entries from the hash table. Also used during
2119 for (i
= 0; i
< cselib_nregs
; i
++)
2122 htab_empty (hash_table
);
2123 obstack_free (&cselib_obstack
, cselib_startobj
);
2126 empty_elt_lists
= 0;
2127 empty_elt_loc_lists
= 0;
2128 n_useless_values
= 0;
2130 next_unknown_value
= 0;
2133 /* The equality test for our hash table. The first argument ENTRY is a table
2134 element (i.e. a cselib_val), while the second arg X is an rtx. */
2136 entry_and_rtx_equal_p (entry
, x_arg
)
2137 const void *entry
, *x_arg
;
2139 struct elt_loc_list
*l
;
2140 cselib_val
*v
= (cselib_val
*)entry
;
2143 /* We don't guarantee that distinct rtx's have different hash values,
2144 so we need to do a comparison. */
2145 for (l
= v
->locs
; l
; l
= l
->next
)
2146 if (rtx_equal_for_cselib_p (l
->loc
, x
))
2151 /* The hash function for our hash table. The value is always computed with
2152 hash_rtx when adding an element; this function just extracts the hash
2153 value from a cselib_val structure. */
2155 get_value_hash (entry
)
2158 cselib_val
*v
= (cselib_val
*) entry
;
2162 /* If there are no more locations that hold a value, the value has become
2163 useless. See whether that is the case for V. Return 1 if this has
2164 just become useless. */
2166 check_value_useless (v
)
2175 /* This is a marker to indicate that the value will be reclaimed. */
2181 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
2182 only return true for values which point to a cselib_val whose value
2183 element has been set to zero, which implies the cselib_val will be
2186 references_value_p (x
, only_useless
)
2190 enum rtx_code code
= GET_CODE (x
);
2191 const char *fmt
= GET_RTX_FORMAT (code
);
2194 if (GET_CODE (x
) == VALUE
2195 && (! only_useless
|| CSELIB_VAL_PTR (x
)->value
== 0))
2198 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2202 if (references_value_p (XEXP (x
, i
), only_useless
))
2205 else if (fmt
[i
] == 'E')
2209 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2210 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
2218 /* Set by discard_useless_locs if it deleted the last location of any
2220 static int values_became_useless
;
2222 /* For all locations found in X, delete locations that reference useless
2223 values (i.e. values without any location). Called through
2226 discard_useless_locs (x
, info
)
2228 void *info ATTRIBUTE_UNUSED
;
2230 cselib_val
*v
= (cselib_val
*)*x
;
2231 struct elt_loc_list
**p
= &v
->locs
;
2235 if (references_value_p ((*p
)->loc
, 1))
2236 unchain_one_elt_loc_list (p
);
2240 if (check_value_useless (v
))
2241 values_became_useless
= 1;
2246 /* If X is a value with no locations, remove it from the hashtable. */
2249 discard_useless_values (x
, info
)
2251 void *info ATTRIBUTE_UNUSED
;
2253 cselib_val
*v
= (cselib_val
*)*x
;
2257 htab_clear_slot (hash_table
, x
);
2258 unchain_one_value (v
);
2264 /* Clean out useless values (i.e. those which no longer have locations
2265 associated with them) from the hash table. */
2267 remove_useless_values ()
2269 /* First pass: eliminate locations that reference the value. That in
2270 turn can make more values useless. */
2273 values_became_useless
= 0;
2274 htab_traverse (hash_table
, discard_useless_locs
, 0);
2276 while (values_became_useless
);
2278 /* Second pass: actually remove the values. */
2279 htab_traverse (hash_table
, discard_useless_values
, 0);
2281 if (n_useless_values
!= 0)
2285 /* Return nonzero if we can prove that X and Y contain the same value, taking
2286 our gathered information into account. */
2288 rtx_equal_for_cselib_p (x
, y
)
2295 if (GET_CODE (x
) == REG
|| GET_CODE (x
) == MEM
)
2297 cselib_val
*e
= cselib_lookup (x
, VOIDmode
, 0);
2301 if (GET_CODE (y
) == REG
|| GET_CODE (y
) == MEM
)
2303 cselib_val
*e
= cselib_lookup (y
, VOIDmode
, 0);
2311 if (GET_CODE (x
) == VALUE
&& GET_CODE (y
) == VALUE
)
2312 return CSELIB_VAL_PTR (x
) == CSELIB_VAL_PTR (y
);
2314 if (GET_CODE (x
) == VALUE
)
2316 cselib_val
*e
= CSELIB_VAL_PTR (x
);
2317 struct elt_loc_list
*l
;
2319 for (l
= e
->locs
; l
; l
= l
->next
)
2323 /* Avoid infinite recursion. */
2324 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
2327 if (rtx_equal_for_cselib_p (t
, y
))
2334 if (GET_CODE (y
) == VALUE
)
2336 cselib_val
*e
= CSELIB_VAL_PTR (y
);
2337 struct elt_loc_list
*l
;
2339 for (l
= e
->locs
; l
; l
= l
->next
)
2343 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
2346 if (rtx_equal_for_cselib_p (x
, t
))
2353 if (GET_CODE (x
) != GET_CODE (y
)
2354 || GET_MODE (x
) != GET_MODE (y
))
2357 /* This won't be handled correctly by the code below. */
2358 if (GET_CODE (x
) == LABEL_REF
)
2359 return XEXP (x
, 0) == XEXP (y
, 0);
2361 code
= GET_CODE (x
);
2362 fmt
= GET_RTX_FORMAT (code
);
2364 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2370 if (XWINT (x
, i
) != XWINT (y
, i
))
2376 if (XINT (x
, i
) != XINT (y
, i
))
2382 /* Two vectors must have the same length. */
2383 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
2386 /* And the corresponding elements must match. */
2387 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2388 if (! rtx_equal_for_cselib_p (XVECEXP (x
, i
, j
),
2394 if (! rtx_equal_for_cselib_p (XEXP (x
, i
), XEXP (y
, i
)))
2400 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
2405 /* These are just backpointers, so they don't matter. */
2412 /* It is believed that rtx's at this level will never
2413 contain anything but integers and other rtx's,
2414 except for within LABEL_REFs and SYMBOL_REFs. */
2422 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
2423 For registers and memory locations, we look up their cselib_val structure
2424 and return its VALUE element.
2425 Possible reasons for return 0 are: the object is volatile, or we couldn't
2426 find a register or memory location in the table and CREATE is zero. If
2427 CREATE is nonzero, table elts are created for regs and mem.
2428 MODE is used in hashing for CONST_INTs only;
2429 otherwise the mode of X is used. */
2431 hash_rtx (x
, mode
, create
)
2433 enum machine_mode mode
;
2440 unsigned int hash
= 0;
2442 /* repeat is used to turn tail-recursion into iteration. */
2444 code
= GET_CODE (x
);
2445 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
2451 e
= cselib_lookup (x
, GET_MODE (x
), create
);
2459 unsigned HOST_WIDE_INT tem
= INTVAL (x
);
2460 hash
+= ((unsigned) CONST_INT
<< 7) + (unsigned) mode
+ tem
;
2461 return hash
? hash
: CONST_INT
;
2465 /* This is like the general case, except that it only counts
2466 the integers representing the constant. */
2467 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
2468 if (GET_MODE (x
) != VOIDmode
)
2469 for (i
= 2; i
< GET_RTX_LENGTH (CONST_DOUBLE
); i
++)
2471 unsigned HOST_WIDE_INT tem
= XWINT (x
, i
);
2475 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
2476 + (unsigned) CONST_DOUBLE_HIGH (x
));
2477 return hash
? hash
: CONST_DOUBLE
;
2479 /* Assume there is only one rtx object for any given label. */
2482 += ((unsigned) LABEL_REF
<< 7) + (unsigned long) XEXP (x
, 0);
2483 return hash
? hash
: LABEL_REF
;
2487 += ((unsigned) SYMBOL_REF
<< 7) + (unsigned long) XSTR (x
, 0);
2488 return hash
? hash
: SYMBOL_REF
;
2497 case UNSPEC_VOLATILE
:
2501 if (MEM_VOLATILE_P (x
))
2510 i
= GET_RTX_LENGTH (code
) - 1;
2511 fmt
= GET_RTX_FORMAT (code
);
2516 unsigned int tem_hash
;
2517 rtx tem
= XEXP (x
, i
);
2519 /* If we are about to do the last recursive call
2520 needed at this level, change it into iteration.
2521 This function is called enough to be worth it. */
2527 tem_hash
= hash_rtx (tem
, 0, create
);
2532 else if (fmt
[i
] == 'E')
2533 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2535 unsigned int tem_hash
= hash_rtx (XVECEXP (x
, i
, j
), 0, create
);
2540 else if (fmt
[i
] == 's')
2542 unsigned char *p
= (unsigned char *) XSTR (x
, i
);
2547 else if (fmt
[i
] == 'i')
2549 unsigned int tem
= XINT (x
, i
);
2552 else if (fmt
[i
] == '0' || fmt
[i
] == 't')
2557 return hash
? hash
: 1 + GET_CODE (x
);
2560 /* Create a new value structure for VALUE and initialize it. The mode of the
2563 new_cselib_val (value
, mode
)
2565 enum machine_mode mode
;
2567 cselib_val
*e
= empty_vals
;
2569 empty_vals
= e
->u
.next_free
;
2571 e
= (cselib_val
*) obstack_alloc (&cselib_obstack
, sizeof (cselib_val
));
2575 e
->u
.val_rtx
= gen_rtx_VALUE (mode
);
2576 CSELIB_VAL_PTR (e
->u
.val_rtx
) = e
;
2583 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
2584 contains the data at this address. X is a MEM that represents the
2585 value. Update the two value structures to represent this situation. */
2587 add_mem_for_addr (addr_elt
, mem_elt
, x
)
2588 cselib_val
*addr_elt
, *mem_elt
;
2592 struct elt_loc_list
*l
;
2594 /* Avoid duplicates. */
2595 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
2596 if (GET_CODE (l
->loc
) == MEM
2597 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
2600 new = gen_rtx_MEM (GET_MODE (x
), addr_elt
->u
.val_rtx
);
2601 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
2603 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x
);
2604 MEM_COPY_ATTRIBUTES (new, x
);
2606 mem_elt
->locs
= new_elt_loc_list (mem_elt
->locs
, new);
2609 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
2610 If CREATE, make a new one if we haven't seen it before. */
2612 cselib_lookup_mem (x
, create
)
2618 cselib_val
*mem_elt
;
2621 if (MEM_VOLATILE_P (x
) || GET_MODE (x
) == BLKmode
)
2623 if (FLOAT_MODE_P (GET_MODE (x
)) && flag_float_store
)
2626 /* Look up the value for the address. */
2627 addr
= cselib_lookup (XEXP (x
, 0), GET_MODE (x
), create
);
2631 /* Find a value that describes a value of our mode at that address. */
2632 for (l
= addr
->addr_list
; l
; l
= l
->next
)
2633 if (GET_MODE (l
->elt
->u
.val_rtx
) == GET_MODE (x
))
2637 mem_elt
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
2638 add_mem_for_addr (addr
, mem_elt
, x
);
2639 slot
= htab_find_slot_with_hash (hash_table
, x
, mem_elt
->value
, 1);
2644 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
2645 with VALUE expressions. This way, it becomes independent of changes
2646 to registers and memory.
2647 X isn't actually modified; if modifications are needed, new rtl is
2648 allocated. However, the return value can share rtl with X. */
2650 cselib_subst_to_values (x
)
2653 enum rtx_code code
= GET_CODE (x
);
2654 const char *fmt
= GET_RTX_FORMAT (code
);
2664 for (l
= REG_VALUES (i
); l
; l
= l
->next
)
2665 if (GET_MODE (l
->elt
->u
.val_rtx
) == GET_MODE (x
))
2666 return l
->elt
->u
.val_rtx
;
2670 e
= cselib_lookup_mem (x
, 0);
2673 return e
->u
.val_rtx
;
2675 /* CONST_DOUBLEs must be special-cased here so that we won't try to
2676 look up the CONST_DOUBLE_MEM inside. */
2685 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2689 rtx t
= cselib_subst_to_values (XEXP (x
, i
));
2690 if (t
!= XEXP (x
, i
) && x
== copy
)
2691 copy
= shallow_copy_rtx (x
);
2694 else if (fmt
[i
] == 'E')
2698 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2700 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
));
2701 if (t
!= XVECEXP (x
, i
, j
) && XVEC (x
, i
) == XVEC (copy
, i
))
2704 copy
= shallow_copy_rtx (x
);
2705 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (x
, i
));
2706 for (k
= 0; k
< j
; k
++)
2707 XVECEXP (copy
, i
, k
) = XVECEXP (x
, i
, k
);
2709 XVECEXP (copy
, i
, j
) = t
;
2716 /* Look up the rtl expression X in our tables and return the value it has.
2717 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
2718 we create a new one if possible, using mode MODE if X doesn't have a mode
2719 (i.e. because it's a constant). */
2721 cselib_lookup (x
, mode
, create
)
2723 enum machine_mode mode
;
2728 unsigned int hashval
;
2730 if (GET_MODE (x
) != VOIDmode
)
2731 mode
= GET_MODE (x
);
2733 if (GET_CODE (x
) == VALUE
)
2734 return CSELIB_VAL_PTR (x
);
2736 if (GET_CODE (x
) == REG
)
2740 for (l
= REG_VALUES (i
); l
; l
= l
->next
)
2741 if (mode
== GET_MODE (l
->elt
->u
.val_rtx
))
2745 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
2746 e
->locs
= new_elt_loc_list (e
->locs
, x
);
2747 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), e
);
2748 slot
= htab_find_slot_with_hash (hash_table
, x
, e
->value
, 1);
2753 if (GET_CODE (x
) == MEM
)
2754 return cselib_lookup_mem (x
, create
);
2756 hashval
= hash_rtx (x
, mode
, create
);
2757 /* Can't even create if hashing is not possible. */
2761 slot
= htab_find_slot_with_hash (hash_table
, x
, hashval
, create
);
2764 e
= (cselib_val
*) *slot
;
2768 e
= new_cselib_val (hashval
, mode
);
2769 /* We have to fill the slot before calling cselib_subst_to_values:
2770 the hash table is inconsistent until we do so, and
2771 cselib_subst_to_values will need to do lookups. */
2773 e
->locs
= new_elt_loc_list (e
->locs
, cselib_subst_to_values (x
));
2777 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2778 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2779 is used to determine how many hard registers are being changed. If MODE
2780 is VOIDmode, then only REGNO is being changed; this is used when
2781 invalidating call clobbered registers across a call. */
2783 cselib_invalidate_regno (regno
, mode
)
2785 enum machine_mode mode
;
2790 /* If we see pseudos after reload, something is _wrong_. */
2791 if (reload_completed
&& regno
>= FIRST_PSEUDO_REGISTER
2792 && reg_renumber
[regno
] >= 0)
2795 /* Determine the range of registers that must be invalidated. For
2796 pseudos, only REGNO is affected. For hard regs, we must take MODE
2797 into account, and we must also invalidate lower register numbers
2798 if they contain values that overlap REGNO. */
2799 endregno
= regno
+ 1;
2800 if (regno
< FIRST_PSEUDO_REGISTER
&& mode
!= VOIDmode
)
2801 endregno
= regno
+ HARD_REGNO_NREGS (regno
, mode
);
2803 for (i
= 0; i
< endregno
; i
++)
2805 struct elt_list
**l
= ®_VALUES (i
);
2807 /* Go through all known values for this reg; if it overlaps the range
2808 we're invalidating, remove the value. */
2811 cselib_val
*v
= (*l
)->elt
;
2812 struct elt_loc_list
**p
;
2815 if (i
< FIRST_PSEUDO_REGISTER
)
2816 this_last
+= HARD_REGNO_NREGS (i
, GET_MODE (v
->u
.val_rtx
)) - 1;
2817 if (this_last
< regno
)
2822 /* We have an overlap. */
2823 unchain_one_elt_list (l
);
2825 /* Now, we clear the mapping from value to reg. It must exist, so
2826 this code will crash intentionally if it doesn't. */
2827 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
2830 if (GET_CODE (x
) == REG
&& REGNO (x
) == i
)
2832 unchain_one_elt_loc_list (p
);
2836 check_value_useless (v
);
2841 /* The memory at address MEM_BASE is being changed.
2842 Return whether this change will invalidate VAL. */
2844 cselib_mem_conflict_p (mem_base
, val
)
2852 code
= GET_CODE (val
);
2855 /* Get rid of a few simple cases quickly. */
2868 if (GET_MODE (mem_base
) == BLKmode
2869 || GET_MODE (val
) == BLKmode
)
2871 if (anti_dependence (val
, mem_base
))
2873 /* The address may contain nested MEMs. */
2880 fmt
= GET_RTX_FORMAT (code
);
2882 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2886 if (cselib_mem_conflict_p (mem_base
, XEXP (val
, i
)))
2889 else if (fmt
[i
] == 'E')
2893 for (j
= 0; j
< XVECLEN (val
, i
); j
++)
2894 if (cselib_mem_conflict_p (mem_base
, XVECEXP (val
, i
, j
)))
2902 /* For the value found in SLOT, walk its locations to determine if any overlap
2903 INFO (which is a MEM rtx). */
2905 cselib_invalidate_mem_1 (slot
, info
)
2909 cselib_val
*v
= (cselib_val
*) *slot
;
2910 rtx mem_rtx
= (rtx
) info
;
2911 struct elt_loc_list
**p
= &v
->locs
;
2916 struct elt_list
**mem_chain
;
2919 /* MEMs may occur in locations only at the top level; below
2920 that every MEM or REG is substituted by its VALUE. */
2921 if (GET_CODE (x
) != MEM
2922 || ! cselib_mem_conflict_p (mem_rtx
, x
))
2928 /* This one overlaps. */
2929 /* We must have a mapping from this MEM's address to the
2930 value (E). Remove that, too. */
2931 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0);
2932 mem_chain
= &addr
->addr_list
;
2935 if ((*mem_chain
)->elt
== v
)
2937 unchain_one_elt_list (mem_chain
);
2940 mem_chain
= &(*mem_chain
)->next
;
2942 unchain_one_elt_loc_list (p
);
2944 check_value_useless (v
);
2948 /* Invalidate any locations in the table which are changed because of a
2949 store to MEM_RTX. If this is called because of a non-const call
2950 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2952 cselib_invalidate_mem (mem_rtx
)
2955 htab_traverse (hash_table
, cselib_invalidate_mem_1
, mem_rtx
);
2958 /* Invalidate DEST, which is being assigned to or clobbered. The second and
2959 the third parameter exist so that this function can be passed to
2960 note_stores; they are ignored. */
2962 cselib_invalidate_rtx (dest
, ignore
, data
)
2964 rtx ignore ATTRIBUTE_UNUSED
;
2965 void *data ATTRIBUTE_UNUSED
;
2967 while (GET_CODE (dest
) == STRICT_LOW_PART
2968 || GET_CODE (dest
) == SIGN_EXTRACT
2969 || GET_CODE (dest
) == ZERO_EXTRACT
2970 || GET_CODE (dest
) == SUBREG
)
2971 dest
= XEXP (dest
, 0);
2973 if (GET_CODE (dest
) == REG
)
2974 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
2975 else if (GET_CODE (dest
) == MEM
)
2976 cselib_invalidate_mem (dest
);
2978 /* Some machines don't define AUTO_INC_DEC, but they still use push
2979 instructions. We need to catch that case here in order to
2980 invalidate the stack pointer correctly. Note that invalidating
2981 the stack pointer is different from invalidating DEST. */
2982 if (push_operand (dest
, GET_MODE (dest
)))
2983 cselib_invalidate_rtx (stack_pointer_rtx
, NULL_RTX
, NULL
);
2986 /* Record the result of a SET instruction. DEST is being set; the source
2987 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2988 describes its address. */
2990 cselib_record_set (dest
, src_elt
, dest_addr_elt
)
2992 cselib_val
*src_elt
, *dest_addr_elt
;
2994 int dreg
= GET_CODE (dest
) == REG
? REGNO (dest
) : -1;
2996 if (src_elt
== 0 || side_effects_p (dest
))
3001 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
3002 src_elt
->locs
= new_elt_loc_list (src_elt
->locs
, dest
);
3004 else if (GET_CODE (dest
) == MEM
&& dest_addr_elt
!= 0)
3005 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
3008 /* Describe a single set that is part of an insn. */
3013 cselib_val
*src_elt
;
3014 cselib_val
*dest_addr_elt
;
3017 /* There is no good way to determine how many elements there can be
3018 in a PARALLEL. Since it's fairly cheap, use a really large number. */
3019 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
3021 /* Record the effects of any sets in INSN. */
3023 cselib_record_sets (insn
)
3028 struct set sets
[MAX_SETS
];
3029 rtx body
= PATTERN (insn
);
3031 body
= PATTERN (insn
);
3032 /* Find all sets. */
3033 if (GET_CODE (body
) == SET
)
3035 sets
[0].src
= SET_SRC (body
);
3036 sets
[0].dest
= SET_DEST (body
);
3039 else if (GET_CODE (body
) == PARALLEL
)
3041 /* Look through the PARALLEL and record the values being
3042 set, if possible. Also handle any CLOBBERs. */
3043 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
3045 rtx x
= XVECEXP (body
, 0, i
);
3047 if (GET_CODE (x
) == SET
)
3049 sets
[n_sets
].src
= SET_SRC (x
);
3050 sets
[n_sets
].dest
= SET_DEST (x
);
3056 /* Look up the values that are read. Do this before invalidating the
3057 locations that are written. */
3058 for (i
= 0; i
< n_sets
; i
++)
3060 sets
[i
].src_elt
= cselib_lookup (sets
[i
].src
, GET_MODE (sets
[i
].dest
),
3062 if (GET_CODE (sets
[i
].dest
) == MEM
)
3063 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (sets
[i
].dest
, 0), Pmode
,
3066 sets
[i
].dest_addr_elt
= 0;
3069 /* Invalidate all locations written by this insn. Note that the elts we
3070 looked up in the previous loop aren't affected, just some of their
3071 locations may go away. */
3072 note_stores (body
, cselib_invalidate_rtx
, NULL
);
3074 /* Now enter the equivalences in our tables. */
3075 for (i
= 0; i
< n_sets
; i
++)
3076 cselib_record_set (sets
[i
].dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
3079 /* Record the effects of INSN. */
3081 cselib_process_insn (insn
)
3086 cselib_current_insn
= insn
;
3088 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
3089 if (GET_CODE (insn
) == CODE_LABEL
3090 || (GET_CODE (insn
) == NOTE
3091 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_SETJMP
)
3092 || (GET_CODE (insn
) == INSN
3093 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
3094 && MEM_VOLATILE_P (PATTERN (insn
))))
3100 if (GET_RTX_CLASS (GET_CODE (insn
)) != 'i')
3102 cselib_current_insn
= 0;
3105 /* If this is a call instruction, forget anything stored in a
3106 call clobbered register, or, if this is not a const call, in
3108 if (GET_CODE (insn
) == CALL_INSN
)
3110 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
3111 if (call_used_regs
[i
])
3112 cselib_invalidate_regno (i
, VOIDmode
);
3114 if (! CONST_CALL_P (insn
))
3115 cselib_invalidate_mem (callmem
);
3118 cselib_record_sets (insn
);
3121 /* Clobber any registers which appear in REG_INC notes. We
3122 could keep track of the changes to their values, but it is
3123 unlikely to help. */
3127 for (x
= REG_NOTES (insn
); x
; x
= XEXP (x
, 1))
3128 if (REG_NOTE_KIND (x
) == REG_INC
)
3129 cselib_invalidate_rtx (XEXP (x
, 0), NULL_RTX
, NULL
);
3133 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
3134 after we have processed the insn. */
3135 if (GET_CODE (insn
) == CALL_INSN
)
3139 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
3140 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
3141 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0), NULL_RTX
,
3145 cselib_current_insn
= 0;
3147 if (n_useless_values
> MAX_USELESS_VALUES
)
3148 remove_useless_values ();
3151 /* Make sure our varrays are big enough. Not called from any cselib routines;
3152 it must be called by the user if it allocated new registers. */
3154 cselib_update_varray_sizes ()
3156 int nregs
= max_reg_num ();
3157 if (nregs
== cselib_nregs
)
3159 cselib_nregs
= nregs
;
3160 VARRAY_GROW (reg_values
, nregs
);
3163 /* Initialize cselib for one pass. The caller must also call
3164 init_alias_analysis. */
3168 /* These are only created once. */
3171 extern struct obstack permanent_obstack
;
3172 gcc_obstack_init (&cselib_obstack
);
3173 cselib_startobj
= obstack_alloc (&cselib_obstack
, 0);
3175 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3176 callmem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
3178 ggc_add_rtx_root (&callmem
, 1);
3181 cselib_nregs
= max_reg_num ();
3182 VARRAY_ELT_LIST_INIT (reg_values
, cselib_nregs
, "reg_values");
3183 hash_table
= htab_create (31, get_value_hash
, entry_and_rtx_equal_p
, NULL
);
3187 /* Called when the current user is done with cselib. */
3192 htab_delete (hash_table
);