1 /* RTL simplification functions for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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. */
30 #include "hard-reg-set.h"
33 #include "insn-config.h"
44 /* Simplification and canonicalization of RTL. */
46 /* Nonzero if X has the form (PLUS frame-pointer integer). We check for
47 virtual regs here because the simplify_*_operation routines are called
48 by integrate.c, which is called before virtual register instantiation.
50 ?!? FIXED_BASE_PLUS_P and NONZERO_BASE_PLUS_P need to move into
51 a header file so that their definitions can be shared with the
52 simplification routines in simplify-rtx.c. Until then, do not
53 change these macros without also changing the copy in simplify-rtx.c. */
55 #define FIXED_BASE_PLUS_P(X) \
56 ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \
57 || ((X) == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])\
58 || (X) == virtual_stack_vars_rtx \
59 || (X) == virtual_incoming_args_rtx \
60 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
61 && (XEXP (X, 0) == frame_pointer_rtx \
62 || XEXP (X, 0) == hard_frame_pointer_rtx \
63 || ((X) == arg_pointer_rtx \
64 && fixed_regs[ARG_POINTER_REGNUM]) \
65 || XEXP (X, 0) == virtual_stack_vars_rtx \
66 || XEXP (X, 0) == virtual_incoming_args_rtx)) \
67 || GET_CODE (X) == ADDRESSOF)
69 /* Similar, but also allows reference to the stack pointer.
71 This used to include FIXED_BASE_PLUS_P, however, we can't assume that
72 arg_pointer_rtx by itself is nonzero, because on at least one machine,
73 the i960, the arg pointer is zero when it is unused. */
75 #define NONZERO_BASE_PLUS_P(X) \
76 ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \
77 || (X) == virtual_stack_vars_rtx \
78 || (X) == virtual_incoming_args_rtx \
79 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
80 && (XEXP (X, 0) == frame_pointer_rtx \
81 || XEXP (X, 0) == hard_frame_pointer_rtx \
82 || ((X) == arg_pointer_rtx \
83 && fixed_regs[ARG_POINTER_REGNUM]) \
84 || XEXP (X, 0) == virtual_stack_vars_rtx \
85 || XEXP (X, 0) == virtual_incoming_args_rtx)) \
86 || (X) == stack_pointer_rtx \
87 || (X) == virtual_stack_dynamic_rtx \
88 || (X) == virtual_outgoing_args_rtx \
89 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
90 && (XEXP (X, 0) == stack_pointer_rtx \
91 || XEXP (X, 0) == virtual_stack_dynamic_rtx \
92 || XEXP (X, 0) == virtual_outgoing_args_rtx)) \
93 || GET_CODE (X) == ADDRESSOF)
95 /* Much code operates on (low, high) pairs; the low value is an
96 unsigned wide int, the high value a signed wide int. We
97 occasionally need to sign extend from low to high as if low were a
99 #define HWI_SIGN_EXTEND(low) \
100 ((((HOST_WIDE_INT) low) < 0) ? ((HOST_WIDE_INT) -1) : ((HOST_WIDE_INT) 0))
102 static rtx simplify_plus_minus
PARAMS ((enum rtx_code
,
103 enum machine_mode
, rtx
, rtx
));
104 static void check_fold_consts
PARAMS ((PTR
));
105 static int entry_and_rtx_equal_p
PARAMS ((const void *, const void *));
106 static unsigned int get_value_hash
PARAMS ((const void *));
107 static struct elt_list
*new_elt_list
PARAMS ((struct elt_list
*,
109 static struct elt_loc_list
*new_elt_loc_list
PARAMS ((struct elt_loc_list
*,
111 static void unchain_one_value
PARAMS ((cselib_val
*));
112 static void unchain_one_elt_list
PARAMS ((struct elt_list
**));
113 static void unchain_one_elt_loc_list
PARAMS ((struct elt_loc_list
**));
114 static void clear_table
PARAMS ((int));
115 static int discard_useless_locs
PARAMS ((void **, void *));
116 static int discard_useless_values
PARAMS ((void **, void *));
117 static void remove_useless_values
PARAMS ((void));
118 static rtx wrap_constant
PARAMS ((enum machine_mode
, rtx
));
119 static unsigned int hash_rtx
PARAMS ((rtx
, enum machine_mode
, int));
120 static cselib_val
*new_cselib_val
PARAMS ((unsigned int,
122 static void add_mem_for_addr
PARAMS ((cselib_val
*, cselib_val
*,
124 static cselib_val
*cselib_lookup_mem
PARAMS ((rtx
, int));
125 static rtx cselib_subst_to_values
PARAMS ((rtx
));
126 static void cselib_invalidate_regno
PARAMS ((unsigned int,
128 static int cselib_mem_conflict_p
PARAMS ((rtx
, rtx
));
129 static int cselib_invalidate_mem_1
PARAMS ((void **, void *));
130 static void cselib_invalidate_mem
PARAMS ((rtx
));
131 static void cselib_invalidate_rtx
PARAMS ((rtx
, rtx
, void *));
132 static void cselib_record_set
PARAMS ((rtx
, cselib_val
*,
134 static void cselib_record_sets
PARAMS ((rtx
));
136 /* There are three ways in which cselib can look up an rtx:
137 - for a REG, the reg_values table (which is indexed by regno) is used
138 - for a MEM, we recursively look up its address and then follow the
139 addr_list of that value
140 - for everything else, we compute a hash value and go through the hash
141 table. Since different rtx's can still have the same hash value,
142 this involves walking the table entries for a given value and comparing
143 the locations of the entries with the rtx we are looking up. */
145 /* A table that enables us to look up elts by their value. */
146 static htab_t hash_table
;
148 /* This is a global so we don't have to pass this through every function.
149 It is used in new_elt_loc_list to set SETTING_INSN. */
150 static rtx cselib_current_insn
;
152 /* Every new unknown value gets a unique number. */
153 static unsigned int next_unknown_value
;
155 /* The number of registers we had when the varrays were last resized. */
156 static unsigned int cselib_nregs
;
158 /* Count values without known locations. Whenever this grows too big, we
159 remove these useless values from the table. */
160 static int n_useless_values
;
162 /* Number of useless values before we remove them from the hash table. */
163 #define MAX_USELESS_VALUES 32
165 /* This table maps from register number to values. It does not contain
166 pointers to cselib_val structures, but rather elt_lists. The purpose is
167 to be able to refer to the same register in different modes. */
168 static varray_type reg_values
;
169 #define REG_VALUES(I) VARRAY_ELT_LIST (reg_values, (I))
171 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
172 in clear_table() for fast emptying. */
173 static varray_type used_regs
;
175 /* We pass this to cselib_invalidate_mem to invalidate all of
176 memory for a non-const call instruction. */
179 /* Memory for our structures is allocated from this obstack. */
180 static struct obstack cselib_obstack
;
182 /* Used to quickly free all memory. */
183 static char *cselib_startobj
;
185 /* Caches for unused structures. */
186 static cselib_val
*empty_vals
;
187 static struct elt_list
*empty_elt_lists
;
188 static struct elt_loc_list
*empty_elt_loc_lists
;
190 /* Set by discard_useless_locs if it deleted the last location of any
192 static int values_became_useless
;
194 /* Make a binary operation by properly ordering the operands and
195 seeing if the expression folds. */
198 simplify_gen_binary (code
, mode
, op0
, op1
)
200 enum machine_mode mode
;
205 /* Put complex operands first and constants second if commutative. */
206 if (GET_RTX_CLASS (code
) == 'c'
207 && ((CONSTANT_P (op0
) && GET_CODE (op1
) != CONST_INT
)
208 || (GET_RTX_CLASS (GET_CODE (op0
)) == 'o'
209 && GET_RTX_CLASS (GET_CODE (op1
)) != 'o')
210 || (GET_CODE (op0
) == SUBREG
211 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0
))) == 'o'
212 && GET_RTX_CLASS (GET_CODE (op1
)) != 'o')))
213 tem
= op0
, op0
= op1
, op1
= tem
;
215 /* If this simplifies, do it. */
216 tem
= simplify_binary_operation (code
, mode
, op0
, op1
);
221 /* Handle addition and subtraction of CONST_INT specially. Otherwise,
222 just form the operation. */
224 if (code
== PLUS
&& GET_CODE (op1
) == CONST_INT
225 && GET_MODE (op0
) != VOIDmode
)
226 return plus_constant (op0
, INTVAL (op1
));
227 else if (code
== MINUS
&& GET_CODE (op1
) == CONST_INT
228 && GET_MODE (op0
) != VOIDmode
)
229 return plus_constant (op0
, - INTVAL (op1
));
231 return gen_rtx_fmt_ee (code
, mode
, op0
, op1
);
234 /* Try to simplify a unary operation CODE whose output mode is to be
235 MODE with input operand OP whose mode was originally OP_MODE.
236 Return zero if no simplification can be made. */
239 simplify_unary_operation (code
, mode
, op
, op_mode
)
241 enum machine_mode mode
;
243 enum machine_mode op_mode
;
245 unsigned int width
= GET_MODE_BITSIZE (mode
);
247 /* The order of these tests is critical so that, for example, we don't
248 check the wrong mode (input vs. output) for a conversion operation,
249 such as FIX. At some point, this should be simplified. */
251 #if !defined(REAL_IS_NOT_DOUBLE) || defined(REAL_ARITHMETIC)
253 if (code
== FLOAT
&& GET_MODE (op
) == VOIDmode
254 && (GET_CODE (op
) == CONST_DOUBLE
|| GET_CODE (op
) == CONST_INT
))
256 HOST_WIDE_INT hv
, lv
;
259 if (GET_CODE (op
) == CONST_INT
)
260 lv
= INTVAL (op
), hv
= HWI_SIGN_EXTEND (lv
);
262 lv
= CONST_DOUBLE_LOW (op
), hv
= CONST_DOUBLE_HIGH (op
);
264 #ifdef REAL_ARITHMETIC
265 REAL_VALUE_FROM_INT (d
, lv
, hv
, mode
);
270 d
*= ((double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2))
271 * (double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2)));
272 d
+= (double) (unsigned HOST_WIDE_INT
) (~ lv
);
278 d
*= ((double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2))
279 * (double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2)));
280 d
+= (double) (unsigned HOST_WIDE_INT
) lv
;
282 #endif /* REAL_ARITHMETIC */
283 d
= real_value_truncate (mode
, d
);
284 return CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
286 else if (code
== UNSIGNED_FLOAT
&& GET_MODE (op
) == VOIDmode
287 && (GET_CODE (op
) == CONST_DOUBLE
|| GET_CODE (op
) == CONST_INT
))
289 HOST_WIDE_INT hv
, lv
;
292 if (GET_CODE (op
) == CONST_INT
)
293 lv
= INTVAL (op
), hv
= HWI_SIGN_EXTEND (lv
);
295 lv
= CONST_DOUBLE_LOW (op
), hv
= CONST_DOUBLE_HIGH (op
);
297 if (op_mode
== VOIDmode
)
299 /* We don't know how to interpret negative-looking numbers in
300 this case, so don't try to fold those. */
304 else if (GET_MODE_BITSIZE (op_mode
) >= HOST_BITS_PER_WIDE_INT
* 2)
307 hv
= 0, lv
&= GET_MODE_MASK (op_mode
);
309 #ifdef REAL_ARITHMETIC
310 REAL_VALUE_FROM_UNSIGNED_INT (d
, lv
, hv
, mode
);
313 d
= (double) (unsigned HOST_WIDE_INT
) hv
;
314 d
*= ((double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2))
315 * (double) ((HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2)));
316 d
+= (double) (unsigned HOST_WIDE_INT
) lv
;
317 #endif /* REAL_ARITHMETIC */
318 d
= real_value_truncate (mode
, d
);
319 return CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
323 if (GET_CODE (op
) == CONST_INT
324 && width
<= HOST_BITS_PER_WIDE_INT
&& width
> 0)
326 register HOST_WIDE_INT arg0
= INTVAL (op
);
327 register HOST_WIDE_INT val
;
340 val
= (arg0
>= 0 ? arg0
: - arg0
);
344 /* Don't use ffs here. Instead, get low order bit and then its
345 number. If arg0 is zero, this will return 0, as desired. */
346 arg0
&= GET_MODE_MASK (mode
);
347 val
= exact_log2 (arg0
& (- arg0
)) + 1;
355 if (op_mode
== VOIDmode
)
357 if (GET_MODE_BITSIZE (op_mode
) == HOST_BITS_PER_WIDE_INT
)
359 /* If we were really extending the mode,
360 we would have to distinguish between zero-extension
361 and sign-extension. */
362 if (width
!= GET_MODE_BITSIZE (op_mode
))
366 else if (GET_MODE_BITSIZE (op_mode
) < HOST_BITS_PER_WIDE_INT
)
367 val
= arg0
& ~((HOST_WIDE_INT
) (-1) << GET_MODE_BITSIZE (op_mode
));
373 if (op_mode
== VOIDmode
)
375 if (GET_MODE_BITSIZE (op_mode
) == HOST_BITS_PER_WIDE_INT
)
377 /* If we were really extending the mode,
378 we would have to distinguish between zero-extension
379 and sign-extension. */
380 if (width
!= GET_MODE_BITSIZE (op_mode
))
384 else if (GET_MODE_BITSIZE (op_mode
) < HOST_BITS_PER_WIDE_INT
)
387 = arg0
& ~((HOST_WIDE_INT
) (-1) << GET_MODE_BITSIZE (op_mode
));
389 & ((HOST_WIDE_INT
) 1 << (GET_MODE_BITSIZE (op_mode
) - 1)))
390 val
-= (HOST_WIDE_INT
) 1 << GET_MODE_BITSIZE (op_mode
);
405 val
= trunc_int_for_mode (val
, mode
);
407 return GEN_INT (val
);
410 /* We can do some operations on integer CONST_DOUBLEs. Also allow
411 for a DImode operation on a CONST_INT. */
412 else if (GET_MODE (op
) == VOIDmode
&& width
<= HOST_BITS_PER_INT
* 2
413 && (GET_CODE (op
) == CONST_DOUBLE
|| GET_CODE (op
) == CONST_INT
))
415 unsigned HOST_WIDE_INT l1
, lv
;
416 HOST_WIDE_INT h1
, hv
;
418 if (GET_CODE (op
) == CONST_DOUBLE
)
419 l1
= CONST_DOUBLE_LOW (op
), h1
= CONST_DOUBLE_HIGH (op
);
421 l1
= INTVAL (op
), h1
= HWI_SIGN_EXTEND (l1
);
431 neg_double (l1
, h1
, &lv
, &hv
);
436 neg_double (l1
, h1
, &lv
, &hv
);
444 lv
= HOST_BITS_PER_WIDE_INT
+ exact_log2 (h1
& (-h1
)) + 1;
446 lv
= exact_log2 (l1
& (-l1
)) + 1;
450 /* This is just a change-of-mode, so do nothing. */
455 if (op_mode
== VOIDmode
456 || GET_MODE_BITSIZE (op_mode
) > HOST_BITS_PER_WIDE_INT
)
460 lv
= l1
& GET_MODE_MASK (op_mode
);
464 if (op_mode
== VOIDmode
465 || GET_MODE_BITSIZE (op_mode
) > HOST_BITS_PER_WIDE_INT
)
469 lv
= l1
& GET_MODE_MASK (op_mode
);
470 if (GET_MODE_BITSIZE (op_mode
) < HOST_BITS_PER_WIDE_INT
471 && (lv
& ((HOST_WIDE_INT
) 1
472 << (GET_MODE_BITSIZE (op_mode
) - 1))) != 0)
473 lv
-= (HOST_WIDE_INT
) 1 << GET_MODE_BITSIZE (op_mode
);
475 hv
= HWI_SIGN_EXTEND (lv
);
486 return immed_double_const (lv
, hv
, mode
);
489 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
490 else if (GET_CODE (op
) == CONST_DOUBLE
491 && GET_MODE_CLASS (mode
) == MODE_FLOAT
)
497 if (setjmp (handler
))
498 /* There used to be a warning here, but that is inadvisable.
499 People may want to cause traps, and the natural way
500 to do it should not get a warning. */
503 set_float_handler (handler
);
505 REAL_VALUE_FROM_CONST_DOUBLE (d
, op
);
510 d
= REAL_VALUE_NEGATE (d
);
514 if (REAL_VALUE_NEGATIVE (d
))
515 d
= REAL_VALUE_NEGATE (d
);
519 d
= real_value_truncate (mode
, d
);
523 /* All this does is change the mode. */
527 d
= REAL_VALUE_RNDZINT (d
);
531 d
= REAL_VALUE_UNSIGNED_RNDZINT (d
);
541 x
= CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
542 set_float_handler (NULL_PTR
);
546 else if (GET_CODE (op
) == CONST_DOUBLE
547 && GET_MODE_CLASS (GET_MODE (op
)) == MODE_FLOAT
548 && GET_MODE_CLASS (mode
) == MODE_INT
549 && width
<= HOST_BITS_PER_WIDE_INT
&& width
> 0)
555 if (setjmp (handler
))
558 set_float_handler (handler
);
560 REAL_VALUE_FROM_CONST_DOUBLE (d
, op
);
565 val
= REAL_VALUE_FIX (d
);
569 val
= REAL_VALUE_UNSIGNED_FIX (d
);
576 set_float_handler (NULL_PTR
);
578 val
= trunc_int_for_mode (val
, mode
);
580 return GEN_INT (val
);
583 /* This was formerly used only for non-IEEE float.
584 eggert@twinsun.com says it is safe for IEEE also. */
587 enum rtx_code reversed
;
588 /* There are some simplifications we can do even if the operands
593 /* (not (not X)) == X. */
594 if (GET_CODE (op
) == NOT
)
597 /* (not (eq X Y)) == (ne X Y), etc. */
598 if (mode
== BImode
&& GET_RTX_CLASS (GET_CODE (op
)) == '<'
599 && ((reversed
= reversed_comparison_code (op
, NULL_RTX
))
601 return gen_rtx_fmt_ee (reversed
,
602 op_mode
, XEXP (op
, 0), XEXP (op
, 1));
606 /* (neg (neg X)) == X. */
607 if (GET_CODE (op
) == NEG
)
612 /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
613 becomes just the MINUS if its mode is MODE. This allows
614 folding switch statements on machines using casesi (such as
616 if (GET_CODE (op
) == TRUNCATE
617 && GET_MODE (XEXP (op
, 0)) == mode
618 && GET_CODE (XEXP (op
, 0)) == MINUS
619 && GET_CODE (XEXP (XEXP (op
, 0), 0)) == LABEL_REF
620 && GET_CODE (XEXP (XEXP (op
, 0), 1)) == LABEL_REF
)
623 #ifdef POINTERS_EXTEND_UNSIGNED
624 if (! POINTERS_EXTEND_UNSIGNED
625 && mode
== Pmode
&& GET_MODE (op
) == ptr_mode
627 || (GET_CODE (op
) == SUBREG
628 && GET_CODE (SUBREG_REG (op
)) == REG
629 && REG_POINTER (SUBREG_REG (op
))
630 && GET_MODE (SUBREG_REG (op
)) == Pmode
)))
631 return convert_memory_address (Pmode
, op
);
635 #ifdef POINTERS_EXTEND_UNSIGNED
637 if (POINTERS_EXTEND_UNSIGNED
638 && mode
== Pmode
&& GET_MODE (op
) == ptr_mode
640 || (GET_CODE (op
) == SUBREG
641 && GET_CODE (SUBREG_REG (op
)) == REG
642 && REG_POINTER (SUBREG_REG (op
))
643 && GET_MODE (SUBREG_REG (op
)) == Pmode
)))
644 return convert_memory_address (Pmode
, op
);
656 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
657 and OP1. Return 0 if no simplification is possible.
659 Don't use this for relational operations such as EQ or LT.
660 Use simplify_relational_operation instead. */
663 simplify_binary_operation (code
, mode
, op0
, op1
)
665 enum machine_mode mode
;
668 register HOST_WIDE_INT arg0
, arg1
, arg0s
, arg1s
;
670 unsigned int width
= GET_MODE_BITSIZE (mode
);
673 /* Relational operations don't work here. We must know the mode
674 of the operands in order to do the comparison correctly.
675 Assuming a full word can give incorrect results.
676 Consider comparing 128 with -128 in QImode. */
678 if (GET_RTX_CLASS (code
) == '<')
681 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
682 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
683 && GET_CODE (op0
) == CONST_DOUBLE
&& GET_CODE (op1
) == CONST_DOUBLE
684 && mode
== GET_MODE (op0
) && mode
== GET_MODE (op1
))
686 REAL_VALUE_TYPE f0
, f1
, value
;
689 if (setjmp (handler
))
692 set_float_handler (handler
);
694 REAL_VALUE_FROM_CONST_DOUBLE (f0
, op0
);
695 REAL_VALUE_FROM_CONST_DOUBLE (f1
, op1
);
696 f0
= real_value_truncate (mode
, f0
);
697 f1
= real_value_truncate (mode
, f1
);
699 #ifdef REAL_ARITHMETIC
700 #ifndef REAL_INFINITY
701 if (code
== DIV
&& REAL_VALUES_EQUAL (f1
, dconst0
))
704 REAL_ARITHMETIC (value
, rtx_to_tree_code (code
), f0
, f1
);
718 #ifndef REAL_INFINITY
725 value
= MIN (f0
, f1
);
728 value
= MAX (f0
, f1
);
735 value
= real_value_truncate (mode
, value
);
736 set_float_handler (NULL_PTR
);
737 return CONST_DOUBLE_FROM_REAL_VALUE (value
, mode
);
739 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
741 /* We can fold some multi-word operations. */
742 if (GET_MODE_CLASS (mode
) == MODE_INT
743 && width
== HOST_BITS_PER_WIDE_INT
* 2
744 && (GET_CODE (op0
) == CONST_DOUBLE
|| GET_CODE (op0
) == CONST_INT
)
745 && (GET_CODE (op1
) == CONST_DOUBLE
|| GET_CODE (op1
) == CONST_INT
))
747 unsigned HOST_WIDE_INT l1
, l2
, lv
;
748 HOST_WIDE_INT h1
, h2
, hv
;
750 if (GET_CODE (op0
) == CONST_DOUBLE
)
751 l1
= CONST_DOUBLE_LOW (op0
), h1
= CONST_DOUBLE_HIGH (op0
);
753 l1
= INTVAL (op0
), h1
= HWI_SIGN_EXTEND (l1
);
755 if (GET_CODE (op1
) == CONST_DOUBLE
)
756 l2
= CONST_DOUBLE_LOW (op1
), h2
= CONST_DOUBLE_HIGH (op1
);
758 l2
= INTVAL (op1
), h2
= HWI_SIGN_EXTEND (l2
);
763 /* A - B == A + (-B). */
764 neg_double (l2
, h2
, &lv
, &hv
);
767 /* .. fall through ... */
770 add_double (l1
, h1
, l2
, h2
, &lv
, &hv
);
774 mul_double (l1
, h1
, l2
, h2
, &lv
, &hv
);
777 case DIV
: case MOD
: case UDIV
: case UMOD
:
778 /* We'd need to include tree.h to do this and it doesn't seem worth
783 lv
= l1
& l2
, hv
= h1
& h2
;
787 lv
= l1
| l2
, hv
= h1
| h2
;
791 lv
= l1
^ l2
, hv
= h1
^ h2
;
797 && ((unsigned HOST_WIDE_INT
) l1
798 < (unsigned HOST_WIDE_INT
) l2
)))
807 && ((unsigned HOST_WIDE_INT
) l1
808 > (unsigned HOST_WIDE_INT
) l2
)))
815 if ((unsigned HOST_WIDE_INT
) h1
< (unsigned HOST_WIDE_INT
) h2
817 && ((unsigned HOST_WIDE_INT
) l1
818 < (unsigned HOST_WIDE_INT
) l2
)))
825 if ((unsigned HOST_WIDE_INT
) h1
> (unsigned HOST_WIDE_INT
) h2
827 && ((unsigned HOST_WIDE_INT
) l1
828 > (unsigned HOST_WIDE_INT
) l2
)))
834 case LSHIFTRT
: case ASHIFTRT
:
836 case ROTATE
: case ROTATERT
:
837 #ifdef SHIFT_COUNT_TRUNCATED
838 if (SHIFT_COUNT_TRUNCATED
)
839 l2
&= (GET_MODE_BITSIZE (mode
) - 1), h2
= 0;
842 if (h2
!= 0 || l2
>= GET_MODE_BITSIZE (mode
))
845 if (code
== LSHIFTRT
|| code
== ASHIFTRT
)
846 rshift_double (l1
, h1
, l2
, GET_MODE_BITSIZE (mode
), &lv
, &hv
,
848 else if (code
== ASHIFT
)
849 lshift_double (l1
, h1
, l2
, GET_MODE_BITSIZE (mode
), &lv
, &hv
, 1);
850 else if (code
== ROTATE
)
851 lrotate_double (l1
, h1
, l2
, GET_MODE_BITSIZE (mode
), &lv
, &hv
);
852 else /* code == ROTATERT */
853 rrotate_double (l1
, h1
, l2
, GET_MODE_BITSIZE (mode
), &lv
, &hv
);
860 return immed_double_const (lv
, hv
, mode
);
863 if (GET_CODE (op0
) != CONST_INT
|| GET_CODE (op1
) != CONST_INT
864 || width
> HOST_BITS_PER_WIDE_INT
|| width
== 0)
866 /* Even if we can't compute a constant result,
867 there are some cases worth simplifying. */
872 /* In IEEE floating point, x+0 is not the same as x. Similarly
873 for the other optimizations below. */
874 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
875 && FLOAT_MODE_P (mode
) && ! flag_fast_math
)
878 if (op1
== CONST0_RTX (mode
))
881 /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */
882 if (GET_CODE (op0
) == NEG
)
883 return simplify_gen_binary (MINUS
, mode
, op1
, XEXP (op0
, 0));
884 else if (GET_CODE (op1
) == NEG
)
885 return simplify_gen_binary (MINUS
, mode
, op0
, XEXP (op1
, 0));
887 /* Handle both-operands-constant cases. We can only add
888 CONST_INTs to constants since the sum of relocatable symbols
889 can't be handled by most assemblers. Don't add CONST_INT
890 to CONST_INT since overflow won't be computed properly if wider
891 than HOST_BITS_PER_WIDE_INT. */
893 if (CONSTANT_P (op0
) && GET_MODE (op0
) != VOIDmode
894 && GET_CODE (op1
) == CONST_INT
)
895 return plus_constant (op0
, INTVAL (op1
));
896 else if (CONSTANT_P (op1
) && GET_MODE (op1
) != VOIDmode
897 && GET_CODE (op0
) == CONST_INT
)
898 return plus_constant (op1
, INTVAL (op0
));
900 /* See if this is something like X * C - X or vice versa or
901 if the multiplication is written as a shift. If so, we can
902 distribute and make a new multiply, shift, or maybe just
903 have X (if C is 2 in the example above). But don't make
904 real multiply if we didn't have one before. */
906 if (! FLOAT_MODE_P (mode
))
908 HOST_WIDE_INT coeff0
= 1, coeff1
= 1;
909 rtx lhs
= op0
, rhs
= op1
;
912 if (GET_CODE (lhs
) == NEG
)
913 coeff0
= -1, lhs
= XEXP (lhs
, 0);
914 else if (GET_CODE (lhs
) == MULT
915 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
)
917 coeff0
= INTVAL (XEXP (lhs
, 1)), lhs
= XEXP (lhs
, 0);
920 else if (GET_CODE (lhs
) == ASHIFT
921 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
922 && INTVAL (XEXP (lhs
, 1)) >= 0
923 && INTVAL (XEXP (lhs
, 1)) < HOST_BITS_PER_WIDE_INT
)
925 coeff0
= ((HOST_WIDE_INT
) 1) << INTVAL (XEXP (lhs
, 1));
929 if (GET_CODE (rhs
) == NEG
)
930 coeff1
= -1, rhs
= XEXP (rhs
, 0);
931 else if (GET_CODE (rhs
) == MULT
932 && GET_CODE (XEXP (rhs
, 1)) == CONST_INT
)
934 coeff1
= INTVAL (XEXP (rhs
, 1)), rhs
= XEXP (rhs
, 0);
937 else if (GET_CODE (rhs
) == ASHIFT
938 && GET_CODE (XEXP (rhs
, 1)) == CONST_INT
939 && INTVAL (XEXP (rhs
, 1)) >= 0
940 && INTVAL (XEXP (rhs
, 1)) < HOST_BITS_PER_WIDE_INT
)
942 coeff1
= ((HOST_WIDE_INT
) 1) << INTVAL (XEXP (rhs
, 1));
946 if (rtx_equal_p (lhs
, rhs
))
948 tem
= simplify_gen_binary (MULT
, mode
, lhs
,
949 GEN_INT (coeff0
+ coeff1
));
950 return (GET_CODE (tem
) == MULT
&& ! had_mult
) ? 0 : tem
;
954 /* If one of the operands is a PLUS or a MINUS, see if we can
955 simplify this by the associative law.
956 Don't use the associative law for floating point.
957 The inaccuracy makes it nonassociative,
958 and subtle programs can break if operations are associated. */
960 if (INTEGRAL_MODE_P (mode
)
961 && (GET_CODE (op0
) == PLUS
|| GET_CODE (op0
) == MINUS
962 || GET_CODE (op1
) == PLUS
|| GET_CODE (op1
) == MINUS
)
963 && (tem
= simplify_plus_minus (code
, mode
, op0
, op1
)) != 0)
969 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
970 using cc0, in which case we want to leave it as a COMPARE
971 so we can distinguish it from a register-register-copy.
973 In IEEE floating point, x-0 is not the same as x. */
975 if ((TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
976 || ! FLOAT_MODE_P (mode
) || flag_fast_math
)
977 && op1
== CONST0_RTX (mode
))
981 /* Convert (compare (gt (flags) 0) (lt (flags) 0)) to (flags). */
982 if (((GET_CODE (op0
) == GT
&& GET_CODE (op1
) == LT
)
983 || (GET_CODE (op0
) == GTU
&& GET_CODE (op1
) == LTU
))
984 && XEXP (op0
, 1) == const0_rtx
&& XEXP (op1
, 1) == const0_rtx
)
986 rtx xop00
= XEXP (op0
, 0);
987 rtx xop10
= XEXP (op1
, 0);
990 if (GET_CODE (xop00
) == CC0
&& GET_CODE (xop10
) == CC0
)
992 if (GET_CODE (xop00
) == REG
&& GET_CODE (xop10
) == REG
993 && GET_MODE (xop00
) == GET_MODE (xop10
)
994 && REGNO (xop00
) == REGNO (xop10
)
995 && GET_MODE_CLASS (GET_MODE (xop00
)) == MODE_CC
996 && GET_MODE_CLASS (GET_MODE (xop10
)) == MODE_CC
)
1003 /* None of these optimizations can be done for IEEE
1005 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
1006 && FLOAT_MODE_P (mode
) && ! flag_fast_math
)
1009 /* We can't assume x-x is 0 even with non-IEEE floating point,
1010 but since it is zero except in very strange circumstances, we
1011 will treat it as zero with -ffast-math. */
1012 if (rtx_equal_p (op0
, op1
)
1013 && ! side_effects_p (op0
)
1014 && (! FLOAT_MODE_P (mode
) || flag_fast_math
))
1015 return CONST0_RTX (mode
);
1017 /* Change subtraction from zero into negation. */
1018 if (op0
== CONST0_RTX (mode
))
1019 return gen_rtx_NEG (mode
, op1
);
1021 /* (-1 - a) is ~a. */
1022 if (op0
== constm1_rtx
)
1023 return gen_rtx_NOT (mode
, op1
);
1025 /* Subtracting 0 has no effect. */
1026 if (op1
== CONST0_RTX (mode
))
1029 /* See if this is something like X * C - X or vice versa or
1030 if the multiplication is written as a shift. If so, we can
1031 distribute and make a new multiply, shift, or maybe just
1032 have X (if C is 2 in the example above). But don't make
1033 real multiply if we didn't have one before. */
1035 if (! FLOAT_MODE_P (mode
))
1037 HOST_WIDE_INT coeff0
= 1, coeff1
= 1;
1038 rtx lhs
= op0
, rhs
= op1
;
1041 if (GET_CODE (lhs
) == NEG
)
1042 coeff0
= -1, lhs
= XEXP (lhs
, 0);
1043 else if (GET_CODE (lhs
) == MULT
1044 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
)
1046 coeff0
= INTVAL (XEXP (lhs
, 1)), lhs
= XEXP (lhs
, 0);
1049 else if (GET_CODE (lhs
) == ASHIFT
1050 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
1051 && INTVAL (XEXP (lhs
, 1)) >= 0
1052 && INTVAL (XEXP (lhs
, 1)) < HOST_BITS_PER_WIDE_INT
)
1054 coeff0
= ((HOST_WIDE_INT
) 1) << INTVAL (XEXP (lhs
, 1));
1055 lhs
= XEXP (lhs
, 0);
1058 if (GET_CODE (rhs
) == NEG
)
1059 coeff1
= - 1, rhs
= XEXP (rhs
, 0);
1060 else if (GET_CODE (rhs
) == MULT
1061 && GET_CODE (XEXP (rhs
, 1)) == CONST_INT
)
1063 coeff1
= INTVAL (XEXP (rhs
, 1)), rhs
= XEXP (rhs
, 0);
1066 else if (GET_CODE (rhs
) == ASHIFT
1067 && GET_CODE (XEXP (rhs
, 1)) == CONST_INT
1068 && INTVAL (XEXP (rhs
, 1)) >= 0
1069 && INTVAL (XEXP (rhs
, 1)) < HOST_BITS_PER_WIDE_INT
)
1071 coeff1
= ((HOST_WIDE_INT
) 1) << INTVAL (XEXP (rhs
, 1));
1072 rhs
= XEXP (rhs
, 0);
1075 if (rtx_equal_p (lhs
, rhs
))
1077 tem
= simplify_gen_binary (MULT
, mode
, lhs
,
1078 GEN_INT (coeff0
- coeff1
));
1079 return (GET_CODE (tem
) == MULT
&& ! had_mult
) ? 0 : tem
;
1083 /* (a - (-b)) -> (a + b). */
1084 if (GET_CODE (op1
) == NEG
)
1085 return simplify_gen_binary (PLUS
, mode
, op0
, XEXP (op1
, 0));
1087 /* If one of the operands is a PLUS or a MINUS, see if we can
1088 simplify this by the associative law.
1089 Don't use the associative law for floating point.
1090 The inaccuracy makes it nonassociative,
1091 and subtle programs can break if operations are associated. */
1093 if (INTEGRAL_MODE_P (mode
)
1094 && (GET_CODE (op0
) == PLUS
|| GET_CODE (op0
) == MINUS
1095 || GET_CODE (op1
) == PLUS
|| GET_CODE (op1
) == MINUS
)
1096 && (tem
= simplify_plus_minus (code
, mode
, op0
, op1
)) != 0)
1099 /* Don't let a relocatable value get a negative coeff. */
1100 if (GET_CODE (op1
) == CONST_INT
&& GET_MODE (op0
) != VOIDmode
)
1101 return plus_constant (op0
, - INTVAL (op1
));
1103 /* (x - (x & y)) -> (x & ~y) */
1104 if (GET_CODE (op1
) == AND
)
1106 if (rtx_equal_p (op0
, XEXP (op1
, 0)))
1107 return simplify_gen_binary (AND
, mode
, op0
,
1108 gen_rtx_NOT (mode
, XEXP (op1
, 1)));
1109 if (rtx_equal_p (op0
, XEXP (op1
, 1)))
1110 return simplify_gen_binary (AND
, mode
, op0
,
1111 gen_rtx_NOT (mode
, XEXP (op1
, 0)));
1116 if (op1
== constm1_rtx
)
1118 tem
= simplify_unary_operation (NEG
, mode
, op0
, mode
);
1120 return tem
? tem
: gen_rtx_NEG (mode
, op0
);
1123 /* In IEEE floating point, x*0 is not always 0. */
1124 if ((TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
1125 || ! FLOAT_MODE_P (mode
) || flag_fast_math
)
1126 && op1
== CONST0_RTX (mode
)
1127 && ! side_effects_p (op0
))
1130 /* In IEEE floating point, x*1 is not equivalent to x for nans.
1131 However, ANSI says we can drop signals,
1132 so we can do this anyway. */
1133 if (op1
== CONST1_RTX (mode
))
1136 /* Convert multiply by constant power of two into shift unless
1137 we are still generating RTL. This test is a kludge. */
1138 if (GET_CODE (op1
) == CONST_INT
1139 && (val
= exact_log2 (INTVAL (op1
))) >= 0
1140 /* If the mode is larger than the host word size, and the
1141 uppermost bit is set, then this isn't a power of two due
1142 to implicit sign extension. */
1143 && (width
<= HOST_BITS_PER_WIDE_INT
1144 || val
!= HOST_BITS_PER_WIDE_INT
- 1)
1145 && ! rtx_equal_function_value_matters
)
1146 return gen_rtx_ASHIFT (mode
, op0
, GEN_INT (val
));
1148 if (GET_CODE (op1
) == CONST_DOUBLE
1149 && GET_MODE_CLASS (GET_MODE (op1
)) == MODE_FLOAT
)
1153 int op1is2
, op1ism1
;
1155 if (setjmp (handler
))
1158 set_float_handler (handler
);
1159 REAL_VALUE_FROM_CONST_DOUBLE (d
, op1
);
1160 op1is2
= REAL_VALUES_EQUAL (d
, dconst2
);
1161 op1ism1
= REAL_VALUES_EQUAL (d
, dconstm1
);
1162 set_float_handler (NULL_PTR
);
1164 /* x*2 is x+x and x*(-1) is -x */
1165 if (op1is2
&& GET_MODE (op0
) == mode
)
1166 return gen_rtx_PLUS (mode
, op0
, copy_rtx (op0
));
1168 else if (op1ism1
&& GET_MODE (op0
) == mode
)
1169 return gen_rtx_NEG (mode
, op0
);
1174 if (op1
== const0_rtx
)
1176 if (GET_CODE (op1
) == CONST_INT
1177 && (INTVAL (op1
) & GET_MODE_MASK (mode
)) == GET_MODE_MASK (mode
))
1179 if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1181 /* A | (~A) -> -1 */
1182 if (((GET_CODE (op0
) == NOT
&& rtx_equal_p (XEXP (op0
, 0), op1
))
1183 || (GET_CODE (op1
) == NOT
&& rtx_equal_p (XEXP (op1
, 0), op0
)))
1184 && ! side_effects_p (op0
)
1185 && GET_MODE_CLASS (mode
) != MODE_CC
)
1190 if (op1
== const0_rtx
)
1192 if (GET_CODE (op1
) == CONST_INT
1193 && (INTVAL (op1
) & GET_MODE_MASK (mode
)) == GET_MODE_MASK (mode
))
1194 return gen_rtx_NOT (mode
, op0
);
1195 if (op0
== op1
&& ! side_effects_p (op0
)
1196 && GET_MODE_CLASS (mode
) != MODE_CC
)
1201 if (op1
== const0_rtx
&& ! side_effects_p (op0
))
1203 if (GET_CODE (op1
) == CONST_INT
1204 && (INTVAL (op1
) & GET_MODE_MASK (mode
)) == GET_MODE_MASK (mode
))
1206 if (op0
== op1
&& ! side_effects_p (op0
)
1207 && GET_MODE_CLASS (mode
) != MODE_CC
)
1210 if (((GET_CODE (op0
) == NOT
&& rtx_equal_p (XEXP (op0
, 0), op1
))
1211 || (GET_CODE (op1
) == NOT
&& rtx_equal_p (XEXP (op1
, 0), op0
)))
1212 && ! side_effects_p (op0
)
1213 && GET_MODE_CLASS (mode
) != MODE_CC
)
1218 /* Convert divide by power of two into shift (divide by 1 handled
1220 if (GET_CODE (op1
) == CONST_INT
1221 && (arg1
= exact_log2 (INTVAL (op1
))) > 0)
1222 return gen_rtx_LSHIFTRT (mode
, op0
, GEN_INT (arg1
));
1224 /* ... fall through ... */
1227 if (op1
== CONST1_RTX (mode
))
1230 /* In IEEE floating point, 0/x is not always 0. */
1231 if ((TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
1232 || ! FLOAT_MODE_P (mode
) || flag_fast_math
)
1233 && op0
== CONST0_RTX (mode
)
1234 && ! side_effects_p (op1
))
1237 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1238 /* Change division by a constant into multiplication. Only do
1239 this with -ffast-math until an expert says it is safe in
1241 else if (GET_CODE (op1
) == CONST_DOUBLE
1242 && GET_MODE_CLASS (GET_MODE (op1
)) == MODE_FLOAT
1243 && op1
!= CONST0_RTX (mode
)
1247 REAL_VALUE_FROM_CONST_DOUBLE (d
, op1
);
1249 if (! REAL_VALUES_EQUAL (d
, dconst0
))
1251 #if defined (REAL_ARITHMETIC)
1252 REAL_ARITHMETIC (d
, rtx_to_tree_code (DIV
), dconst1
, d
);
1253 return gen_rtx_MULT (mode
, op0
,
1254 CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
));
1257 gen_rtx_MULT (mode
, op0
,
1258 CONST_DOUBLE_FROM_REAL_VALUE (1./d
, mode
));
1266 /* Handle modulus by power of two (mod with 1 handled below). */
1267 if (GET_CODE (op1
) == CONST_INT
1268 && exact_log2 (INTVAL (op1
)) > 0)
1269 return gen_rtx_AND (mode
, op0
, GEN_INT (INTVAL (op1
) - 1));
1271 /* ... fall through ... */
1274 if ((op0
== const0_rtx
|| op1
== const1_rtx
)
1275 && ! side_effects_p (op0
) && ! side_effects_p (op1
))
1281 /* Rotating ~0 always results in ~0. */
1282 if (GET_CODE (op0
) == CONST_INT
&& width
<= HOST_BITS_PER_WIDE_INT
1283 && (unsigned HOST_WIDE_INT
) INTVAL (op0
) == GET_MODE_MASK (mode
)
1284 && ! side_effects_p (op1
))
1287 /* ... fall through ... */
1292 if (op1
== const0_rtx
)
1294 if (op0
== const0_rtx
&& ! side_effects_p (op1
))
1299 if (width
<= HOST_BITS_PER_WIDE_INT
&& GET_CODE (op1
) == CONST_INT
1300 && INTVAL (op1
) == (HOST_WIDE_INT
) 1 << (width
-1)
1301 && ! side_effects_p (op0
))
1303 else if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1308 if (width
<= HOST_BITS_PER_WIDE_INT
&& GET_CODE (op1
) == CONST_INT
1309 && ((unsigned HOST_WIDE_INT
) INTVAL (op1
)
1310 == (unsigned HOST_WIDE_INT
) GET_MODE_MASK (mode
) >> 1)
1311 && ! side_effects_p (op0
))
1313 else if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1318 if (op1
== const0_rtx
&& ! side_effects_p (op0
))
1320 else if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1325 if (op1
== constm1_rtx
&& ! side_effects_p (op0
))
1327 else if (rtx_equal_p (op0
, op1
) && ! side_effects_p (op0
))
1338 /* Get the integer argument values in two forms:
1339 zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S. */
1341 arg0
= INTVAL (op0
);
1342 arg1
= INTVAL (op1
);
1344 if (width
< HOST_BITS_PER_WIDE_INT
)
1346 arg0
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
1347 arg1
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
1350 if (arg0s
& ((HOST_WIDE_INT
) 1 << (width
- 1)))
1351 arg0s
|= ((HOST_WIDE_INT
) (-1) << width
);
1354 if (arg1s
& ((HOST_WIDE_INT
) 1 << (width
- 1)))
1355 arg1s
|= ((HOST_WIDE_INT
) (-1) << width
);
1363 /* Compute the value of the arithmetic. */
1368 val
= arg0s
+ arg1s
;
1372 val
= arg0s
- arg1s
;
1376 val
= arg0s
* arg1s
;
1381 || (arg0s
== (HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)
1384 val
= arg0s
/ arg1s
;
1389 || (arg0s
== (HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)
1392 val
= arg0s
% arg1s
;
1397 || (arg0s
== (HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)
1400 val
= (unsigned HOST_WIDE_INT
) arg0
/ arg1
;
1405 || (arg0s
== (HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)
1408 val
= (unsigned HOST_WIDE_INT
) arg0
% arg1
;
1424 /* If shift count is undefined, don't fold it; let the machine do
1425 what it wants. But truncate it if the machine will do that. */
1429 #ifdef SHIFT_COUNT_TRUNCATED
1430 if (SHIFT_COUNT_TRUNCATED
)
1434 val
= ((unsigned HOST_WIDE_INT
) arg0
) >> arg1
;
1441 #ifdef SHIFT_COUNT_TRUNCATED
1442 if (SHIFT_COUNT_TRUNCATED
)
1446 val
= ((unsigned HOST_WIDE_INT
) arg0
) << arg1
;
1453 #ifdef SHIFT_COUNT_TRUNCATED
1454 if (SHIFT_COUNT_TRUNCATED
)
1458 val
= arg0s
>> arg1
;
1460 /* Bootstrap compiler may not have sign extended the right shift.
1461 Manually extend the sign to insure bootstrap cc matches gcc. */
1462 if (arg0s
< 0 && arg1
> 0)
1463 val
|= ((HOST_WIDE_INT
) -1) << (HOST_BITS_PER_WIDE_INT
- arg1
);
1472 val
= ((((unsigned HOST_WIDE_INT
) arg0
) << (width
- arg1
))
1473 | (((unsigned HOST_WIDE_INT
) arg0
) >> arg1
));
1481 val
= ((((unsigned HOST_WIDE_INT
) arg0
) << arg1
)
1482 | (((unsigned HOST_WIDE_INT
) arg0
) >> (width
- arg1
)));
1486 /* Do nothing here. */
1490 val
= arg0s
<= arg1s
? arg0s
: arg1s
;
1494 val
= ((unsigned HOST_WIDE_INT
) arg0
1495 <= (unsigned HOST_WIDE_INT
) arg1
? arg0
: arg1
);
1499 val
= arg0s
> arg1s
? arg0s
: arg1s
;
1503 val
= ((unsigned HOST_WIDE_INT
) arg0
1504 > (unsigned HOST_WIDE_INT
) arg1
? arg0
: arg1
);
1511 val
= trunc_int_for_mode (val
, mode
);
1513 return GEN_INT (val
);
1516 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
1519 Rather than test for specific case, we do this by a brute-force method
1520 and do all possible simplifications until no more changes occur. Then
1521 we rebuild the operation. */
1524 simplify_plus_minus (code
, mode
, op0
, op1
)
1526 enum machine_mode mode
;
1532 int n_ops
= 2, input_ops
= 2, input_consts
= 0, n_consts
= 0;
1533 int first
= 1, negate
= 0, changed
;
1536 memset ((char *) ops
, 0, sizeof ops
);
1538 /* Set up the two operands and then expand them until nothing has been
1539 changed. If we run out of room in our array, give up; this should
1540 almost never happen. */
1542 ops
[0] = op0
, ops
[1] = op1
, negs
[0] = 0, negs
[1] = (code
== MINUS
);
1549 for (i
= 0; i
< n_ops
; i
++)
1550 switch (GET_CODE (ops
[i
]))
1557 ops
[n_ops
] = XEXP (ops
[i
], 1);
1558 negs
[n_ops
++] = GET_CODE (ops
[i
]) == MINUS
? !negs
[i
] : negs
[i
];
1559 ops
[i
] = XEXP (ops
[i
], 0);
1565 ops
[i
] = XEXP (ops
[i
], 0);
1566 negs
[i
] = ! negs
[i
];
1571 ops
[i
] = XEXP (ops
[i
], 0);
1577 /* ~a -> (-a - 1) */
1580 ops
[n_ops
] = constm1_rtx
;
1581 negs
[n_ops
++] = negs
[i
];
1582 ops
[i
] = XEXP (ops
[i
], 0);
1583 negs
[i
] = ! negs
[i
];
1590 ops
[i
] = GEN_INT (- INTVAL (ops
[i
])), negs
[i
] = 0, changed
= 1;
1598 /* If we only have two operands, we can't do anything. */
1602 /* Now simplify each pair of operands until nothing changes. The first
1603 time through just simplify constants against each other. */
1610 for (i
= 0; i
< n_ops
- 1; i
++)
1611 for (j
= i
+ 1; j
< n_ops
; j
++)
1612 if (ops
[i
] != 0 && ops
[j
] != 0
1613 && (! first
|| (CONSTANT_P (ops
[i
]) && CONSTANT_P (ops
[j
]))))
1615 rtx lhs
= ops
[i
], rhs
= ops
[j
];
1616 enum rtx_code ncode
= PLUS
;
1618 if (negs
[i
] && ! negs
[j
])
1619 lhs
= ops
[j
], rhs
= ops
[i
], ncode
= MINUS
;
1620 else if (! negs
[i
] && negs
[j
])
1623 tem
= simplify_binary_operation (ncode
, mode
, lhs
, rhs
);
1626 ops
[i
] = tem
, ops
[j
] = 0;
1627 negs
[i
] = negs
[i
] && negs
[j
];
1628 if (GET_CODE (tem
) == NEG
)
1629 ops
[i
] = XEXP (tem
, 0), negs
[i
] = ! negs
[i
];
1631 if (GET_CODE (ops
[i
]) == CONST_INT
&& negs
[i
])
1632 ops
[i
] = GEN_INT (- INTVAL (ops
[i
])), negs
[i
] = 0;
1640 /* Pack all the operands to the lower-numbered entries and give up if
1641 we didn't reduce the number of operands we had. Make sure we
1642 count a CONST as two operands. If we have the same number of
1643 operands, but have made more CONSTs than we had, this is also
1644 an improvement, so accept it. */
1646 for (i
= 0, j
= 0; j
< n_ops
; j
++)
1649 ops
[i
] = ops
[j
], negs
[i
++] = negs
[j
];
1650 if (GET_CODE (ops
[j
]) == CONST
)
1654 if (i
+ n_consts
> input_ops
1655 || (i
+ n_consts
== input_ops
&& n_consts
<= input_consts
))
1660 /* If we have a CONST_INT, put it last. */
1661 for (i
= 0; i
< n_ops
- 1; i
++)
1662 if (GET_CODE (ops
[i
]) == CONST_INT
)
1664 tem
= ops
[n_ops
- 1], ops
[n_ops
- 1] = ops
[i
] , ops
[i
] = tem
;
1665 j
= negs
[n_ops
- 1], negs
[n_ops
- 1] = negs
[i
], negs
[i
] = j
;
1668 /* Put a non-negated operand first. If there aren't any, make all
1669 operands positive and negate the whole thing later. */
1670 for (i
= 0; i
< n_ops
&& negs
[i
]; i
++)
1675 for (i
= 0; i
< n_ops
; i
++)
1681 tem
= ops
[0], ops
[0] = ops
[i
], ops
[i
] = tem
;
1682 j
= negs
[0], negs
[0] = negs
[i
], negs
[i
] = j
;
1685 /* Now make the result by performing the requested operations. */
1687 for (i
= 1; i
< n_ops
; i
++)
1688 result
= simplify_gen_binary (negs
[i
] ? MINUS
: PLUS
, mode
, result
, ops
[i
]);
1690 return negate
? gen_rtx_NEG (mode
, result
) : result
;
1695 rtx op0
, op1
; /* Input */
1696 int equal
, op0lt
, op1lt
; /* Output */
1701 check_fold_consts (data
)
1704 struct cfc_args
*args
= (struct cfc_args
*) data
;
1705 REAL_VALUE_TYPE d0
, d1
;
1707 /* We may possibly raise an exception while reading the value. */
1708 args
->unordered
= 1;
1709 REAL_VALUE_FROM_CONST_DOUBLE (d0
, args
->op0
);
1710 REAL_VALUE_FROM_CONST_DOUBLE (d1
, args
->op1
);
1712 /* Comparisons of Inf versus Inf are ordered. */
1713 if (REAL_VALUE_ISNAN (d0
)
1714 || REAL_VALUE_ISNAN (d1
))
1716 args
->equal
= REAL_VALUES_EQUAL (d0
, d1
);
1717 args
->op0lt
= REAL_VALUES_LESS (d0
, d1
);
1718 args
->op1lt
= REAL_VALUES_LESS (d1
, d0
);
1719 args
->unordered
= 0;
1722 /* Like simplify_binary_operation except used for relational operators.
1723 MODE is the mode of the operands, not that of the result. If MODE
1724 is VOIDmode, both operands must also be VOIDmode and we compare the
1725 operands in "infinite precision".
1727 If no simplification is possible, this function returns zero. Otherwise,
1728 it returns either const_true_rtx or const0_rtx. */
1731 simplify_relational_operation (code
, mode
, op0
, op1
)
1733 enum machine_mode mode
;
1736 int equal
, op0lt
, op0ltu
, op1lt
, op1ltu
;
1739 if (mode
== VOIDmode
1740 && (GET_MODE (op0
) != VOIDmode
1741 || GET_MODE (op1
) != VOIDmode
))
1744 /* If op0 is a compare, extract the comparison arguments from it. */
1745 if (GET_CODE (op0
) == COMPARE
&& op1
== const0_rtx
)
1746 op1
= XEXP (op0
, 1), op0
= XEXP (op0
, 0);
1748 /* We can't simplify MODE_CC values since we don't know what the
1749 actual comparison is. */
1750 if (GET_MODE_CLASS (GET_MODE (op0
)) == MODE_CC
1757 /* Make sure the constant is second. */
1758 if ((CONSTANT_P (op0
) && ! CONSTANT_P (op1
))
1759 || (GET_CODE (op0
) == CONST_INT
&& GET_CODE (op1
) != CONST_INT
))
1761 tem
= op0
, op0
= op1
, op1
= tem
;
1762 code
= swap_condition (code
);
1765 /* For integer comparisons of A and B maybe we can simplify A - B and can
1766 then simplify a comparison of that with zero. If A and B are both either
1767 a register or a CONST_INT, this can't help; testing for these cases will
1768 prevent infinite recursion here and speed things up.
1770 If CODE is an unsigned comparison, then we can never do this optimization,
1771 because it gives an incorrect result if the subtraction wraps around zero.
1772 ANSI C defines unsigned operations such that they never overflow, and
1773 thus such cases can not be ignored. */
1775 if (INTEGRAL_MODE_P (mode
) && op1
!= const0_rtx
1776 && ! ((GET_CODE (op0
) == REG
|| GET_CODE (op0
) == CONST_INT
)
1777 && (GET_CODE (op1
) == REG
|| GET_CODE (op1
) == CONST_INT
))
1778 && 0 != (tem
= simplify_binary_operation (MINUS
, mode
, op0
, op1
))
1779 && code
!= GTU
&& code
!= GEU
&& code
!= LTU
&& code
!= LEU
)
1780 return simplify_relational_operation (signed_condition (code
),
1781 mode
, tem
, const0_rtx
);
1783 if (flag_fast_math
&& code
== ORDERED
)
1784 return const_true_rtx
;
1786 if (flag_fast_math
&& code
== UNORDERED
)
1789 /* For non-IEEE floating-point, if the two operands are equal, we know the
1791 if (rtx_equal_p (op0
, op1
)
1792 && (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
1793 || ! FLOAT_MODE_P (GET_MODE (op0
)) || flag_fast_math
))
1794 equal
= 1, op0lt
= 0, op0ltu
= 0, op1lt
= 0, op1ltu
= 0;
1796 /* If the operands are floating-point constants, see if we can fold
1798 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1799 else if (GET_CODE (op0
) == CONST_DOUBLE
&& GET_CODE (op1
) == CONST_DOUBLE
1800 && GET_MODE_CLASS (GET_MODE (op0
)) == MODE_FLOAT
)
1802 struct cfc_args args
;
1804 /* Setup input for check_fold_consts() */
1809 if (!do_float_handler(check_fold_consts
, (PTR
) &args
))
1822 return const_true_rtx
;
1835 /* Receive output from check_fold_consts() */
1837 op0lt
= op0ltu
= args
.op0lt
;
1838 op1lt
= op1ltu
= args
.op1lt
;
1840 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1842 /* Otherwise, see if the operands are both integers. */
1843 else if ((GET_MODE_CLASS (mode
) == MODE_INT
|| mode
== VOIDmode
)
1844 && (GET_CODE (op0
) == CONST_DOUBLE
|| GET_CODE (op0
) == CONST_INT
)
1845 && (GET_CODE (op1
) == CONST_DOUBLE
|| GET_CODE (op1
) == CONST_INT
))
1847 int width
= GET_MODE_BITSIZE (mode
);
1848 HOST_WIDE_INT l0s
, h0s
, l1s
, h1s
;
1849 unsigned HOST_WIDE_INT l0u
, h0u
, l1u
, h1u
;
1851 /* Get the two words comprising each integer constant. */
1852 if (GET_CODE (op0
) == CONST_DOUBLE
)
1854 l0u
= l0s
= CONST_DOUBLE_LOW (op0
);
1855 h0u
= h0s
= CONST_DOUBLE_HIGH (op0
);
1859 l0u
= l0s
= INTVAL (op0
);
1860 h0u
= h0s
= HWI_SIGN_EXTEND (l0s
);
1863 if (GET_CODE (op1
) == CONST_DOUBLE
)
1865 l1u
= l1s
= CONST_DOUBLE_LOW (op1
);
1866 h1u
= h1s
= CONST_DOUBLE_HIGH (op1
);
1870 l1u
= l1s
= INTVAL (op1
);
1871 h1u
= h1s
= HWI_SIGN_EXTEND (l1s
);
1874 /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
1875 we have to sign or zero-extend the values. */
1876 if (width
!= 0 && width
< HOST_BITS_PER_WIDE_INT
)
1878 l0u
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
1879 l1u
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
1881 if (l0s
& ((HOST_WIDE_INT
) 1 << (width
- 1)))
1882 l0s
|= ((HOST_WIDE_INT
) (-1) << width
);
1884 if (l1s
& ((HOST_WIDE_INT
) 1 << (width
- 1)))
1885 l1s
|= ((HOST_WIDE_INT
) (-1) << width
);
1887 if (width
!= 0 && width
<= HOST_BITS_PER_WIDE_INT
)
1888 h0u
= h1u
= 0, h0s
= HWI_SIGN_EXTEND (l0s
), h1s
= HWI_SIGN_EXTEND (l1s
);
1890 equal
= (h0u
== h1u
&& l0u
== l1u
);
1891 op0lt
= (h0s
< h1s
|| (h0s
== h1s
&& l0u
< l1u
));
1892 op1lt
= (h1s
< h0s
|| (h1s
== h0s
&& l1u
< l0u
));
1893 op0ltu
= (h0u
< h1u
|| (h0u
== h1u
&& l0u
< l1u
));
1894 op1ltu
= (h1u
< h0u
|| (h1u
== h0u
&& l1u
< l0u
));
1897 /* Otherwise, there are some code-specific tests we can make. */
1903 /* References to the frame plus a constant or labels cannot
1904 be zero, but a SYMBOL_REF can due to #pragma weak. */
1905 if (((NONZERO_BASE_PLUS_P (op0
) && op1
== const0_rtx
)
1906 || GET_CODE (op0
) == LABEL_REF
)
1907 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1908 /* On some machines, the ap reg can be 0 sometimes. */
1909 && op0
!= arg_pointer_rtx
1916 if (((NONZERO_BASE_PLUS_P (op0
) && op1
== const0_rtx
)
1917 || GET_CODE (op0
) == LABEL_REF
)
1918 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1919 && op0
!= arg_pointer_rtx
1922 return const_true_rtx
;
1926 /* Unsigned values are never negative. */
1927 if (op1
== const0_rtx
)
1928 return const_true_rtx
;
1932 if (op1
== const0_rtx
)
1937 /* Unsigned values are never greater than the largest
1939 if (GET_CODE (op1
) == CONST_INT
1940 && (unsigned HOST_WIDE_INT
) INTVAL (op1
) == GET_MODE_MASK (mode
)
1941 && INTEGRAL_MODE_P (mode
))
1942 return const_true_rtx
;
1946 if (GET_CODE (op1
) == CONST_INT
1947 && (unsigned HOST_WIDE_INT
) INTVAL (op1
) == GET_MODE_MASK (mode
)
1948 && INTEGRAL_MODE_P (mode
))
1959 /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
1965 return equal
? const_true_rtx
: const0_rtx
;
1968 return ! equal
? const_true_rtx
: const0_rtx
;
1971 return op0lt
? const_true_rtx
: const0_rtx
;
1974 return op1lt
? const_true_rtx
: const0_rtx
;
1976 return op0ltu
? const_true_rtx
: const0_rtx
;
1978 return op1ltu
? const_true_rtx
: const0_rtx
;
1981 return equal
|| op0lt
? const_true_rtx
: const0_rtx
;
1984 return equal
|| op1lt
? const_true_rtx
: const0_rtx
;
1986 return equal
|| op0ltu
? const_true_rtx
: const0_rtx
;
1988 return equal
|| op1ltu
? const_true_rtx
: const0_rtx
;
1990 return const_true_rtx
;
1998 /* Simplify CODE, an operation with result mode MODE and three operands,
1999 OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
2000 a constant. Return 0 if no simplifications is possible. */
2003 simplify_ternary_operation (code
, mode
, op0_mode
, op0
, op1
, op2
)
2005 enum machine_mode mode
, op0_mode
;
2008 unsigned int width
= GET_MODE_BITSIZE (mode
);
2010 /* VOIDmode means "infinite" precision. */
2012 width
= HOST_BITS_PER_WIDE_INT
;
2018 if (GET_CODE (op0
) == CONST_INT
2019 && GET_CODE (op1
) == CONST_INT
2020 && GET_CODE (op2
) == CONST_INT
2021 && ((unsigned) INTVAL (op1
) + (unsigned) INTVAL (op2
) <= width
)
2022 && width
<= (unsigned) HOST_BITS_PER_WIDE_INT
)
2024 /* Extracting a bit-field from a constant */
2025 HOST_WIDE_INT val
= INTVAL (op0
);
2027 if (BITS_BIG_ENDIAN
)
2028 val
>>= (GET_MODE_BITSIZE (op0_mode
)
2029 - INTVAL (op2
) - INTVAL (op1
));
2031 val
>>= INTVAL (op2
);
2033 if (HOST_BITS_PER_WIDE_INT
!= INTVAL (op1
))
2035 /* First zero-extend. */
2036 val
&= ((HOST_WIDE_INT
) 1 << INTVAL (op1
)) - 1;
2037 /* If desired, propagate sign bit. */
2038 if (code
== SIGN_EXTRACT
2039 && (val
& ((HOST_WIDE_INT
) 1 << (INTVAL (op1
) - 1))))
2040 val
|= ~ (((HOST_WIDE_INT
) 1 << INTVAL (op1
)) - 1);
2043 /* Clear the bits that don't belong in our mode,
2044 unless they and our sign bit are all one.
2045 So we get either a reasonable negative value or a reasonable
2046 unsigned value for this mode. */
2047 if (width
< HOST_BITS_PER_WIDE_INT
2048 && ((val
& ((HOST_WIDE_INT
) (-1) << (width
- 1)))
2049 != ((HOST_WIDE_INT
) (-1) << (width
- 1))))
2050 val
&= ((HOST_WIDE_INT
) 1 << width
) - 1;
2052 return GEN_INT (val
);
2057 if (GET_CODE (op0
) == CONST_INT
)
2058 return op0
!= const0_rtx
? op1
: op2
;
2060 /* Convert a == b ? b : a to "a". */
2061 if (GET_CODE (op0
) == NE
&& ! side_effects_p (op0
)
2062 && (! FLOAT_MODE_P (mode
) || flag_fast_math
)
2063 && rtx_equal_p (XEXP (op0
, 0), op1
)
2064 && rtx_equal_p (XEXP (op0
, 1), op2
))
2066 else if (GET_CODE (op0
) == EQ
&& ! side_effects_p (op0
)
2067 && (! FLOAT_MODE_P (mode
) || flag_fast_math
)
2068 && rtx_equal_p (XEXP (op0
, 1), op1
)
2069 && rtx_equal_p (XEXP (op0
, 0), op2
))
2071 else if (GET_RTX_CLASS (GET_CODE (op0
)) == '<' && ! side_effects_p (op0
))
2073 enum machine_mode cmp_mode
= (GET_MODE (XEXP (op0
, 0)) == VOIDmode
2074 ? GET_MODE (XEXP (op0
, 1))
2075 : GET_MODE (XEXP (op0
, 0)));
2077 if (cmp_mode
== VOIDmode
)
2078 cmp_mode
= op0_mode
;
2079 temp
= simplify_relational_operation (GET_CODE (op0
), cmp_mode
,
2080 XEXP (op0
, 0), XEXP (op0
, 1));
2082 /* See if any simplifications were possible. */
2083 if (temp
== const0_rtx
)
2085 else if (temp
== const1_rtx
)
2090 /* Look for happy constants in op1 and op2. */
2091 if (GET_CODE (op1
) == CONST_INT
&& GET_CODE (op2
) == CONST_INT
)
2093 HOST_WIDE_INT t
= INTVAL (op1
);
2094 HOST_WIDE_INT f
= INTVAL (op2
);
2096 if (t
== STORE_FLAG_VALUE
&& f
== 0)
2097 code
= GET_CODE (op0
);
2098 else if (t
== 0 && f
== STORE_FLAG_VALUE
)
2101 tmp
= reversed_comparison_code (op0
, NULL_RTX
);
2109 return gen_rtx_fmt_ee (code
, mode
, XEXP (op0
, 0), XEXP (op0
, 1));
2121 /* Simplify X, an rtx expression.
2123 Return the simplified expression or NULL if no simplifications
2126 This is the preferred entry point into the simplification routines;
2127 however, we still allow passes to call the more specific routines.
2129 Right now GCC has three (yes, three) major bodies of RTL simplficiation
2130 code that need to be unified.
2132 1. fold_rtx in cse.c. This code uses various CSE specific
2133 information to aid in RTL simplification.
2135 2. simplify_rtx in combine.c. Similar to fold_rtx, except that
2136 it uses combine specific information to aid in RTL
2139 3. The routines in this file.
2142 Long term we want to only have one body of simplification code; to
2143 get to that state I recommend the following steps:
2145 1. Pour over fold_rtx & simplify_rtx and move any simplifications
2146 which are not pass dependent state into these routines.
2148 2. As code is moved by #1, change fold_rtx & simplify_rtx to
2149 use this routine whenever possible.
2151 3. Allow for pass dependent state to be provided to these
2152 routines and add simplifications based on the pass dependent
2153 state. Remove code from cse.c & combine.c that becomes
2156 It will take time, but ultimately the compiler will be easier to
2157 maintain and improve. It's totally silly that when we add a
2158 simplification that it needs to be added to 4 places (3 for RTL
2159 simplification and 1 for tree simplification. */
2166 enum machine_mode mode
;
2168 mode
= GET_MODE (x
);
2169 code
= GET_CODE (x
);
2171 switch (GET_RTX_CLASS (code
))
2174 return simplify_unary_operation (code
, mode
,
2175 XEXP (x
, 0), GET_MODE (XEXP (x
, 0)));
2178 return simplify_binary_operation (code
, mode
, XEXP (x
, 0), XEXP (x
, 1));
2182 return simplify_ternary_operation (code
, mode
, GET_MODE (XEXP (x
, 0)),
2183 XEXP (x
, 0), XEXP (x
, 1), XEXP (x
, 2));
2186 return simplify_relational_operation (code
,
2187 (GET_MODE (XEXP (x
, 0)) != VOIDmode
2188 ? GET_MODE (XEXP (x
, 0))
2189 : GET_MODE (XEXP (x
, 1))),
2190 XEXP (x
, 0), XEXP (x
, 1));
2197 /* Allocate a struct elt_list and fill in its two elements with the
2200 static struct elt_list
*
2201 new_elt_list (next
, elt
)
2202 struct elt_list
*next
;
2205 struct elt_list
*el
= empty_elt_lists
;
2208 empty_elt_lists
= el
->next
;
2210 el
= (struct elt_list
*) obstack_alloc (&cselib_obstack
,
2211 sizeof (struct elt_list
));
2217 /* Allocate a struct elt_loc_list and fill in its two elements with the
2220 static struct elt_loc_list
*
2221 new_elt_loc_list (next
, loc
)
2222 struct elt_loc_list
*next
;
2225 struct elt_loc_list
*el
= empty_elt_loc_lists
;
2228 empty_elt_loc_lists
= el
->next
;
2230 el
= (struct elt_loc_list
*) obstack_alloc (&cselib_obstack
,
2231 sizeof (struct elt_loc_list
));
2234 el
->setting_insn
= cselib_current_insn
;
2238 /* The elt_list at *PL is no longer needed. Unchain it and free its
2242 unchain_one_elt_list (pl
)
2243 struct elt_list
**pl
;
2245 struct elt_list
*l
= *pl
;
2248 l
->next
= empty_elt_lists
;
2249 empty_elt_lists
= l
;
2252 /* Likewise for elt_loc_lists. */
2255 unchain_one_elt_loc_list (pl
)
2256 struct elt_loc_list
**pl
;
2258 struct elt_loc_list
*l
= *pl
;
2261 l
->next
= empty_elt_loc_lists
;
2262 empty_elt_loc_lists
= l
;
2265 /* Likewise for cselib_vals. This also frees the addr_list associated with
2269 unchain_one_value (v
)
2272 while (v
->addr_list
)
2273 unchain_one_elt_list (&v
->addr_list
);
2275 v
->u
.next_free
= empty_vals
;
2279 /* Remove all entries from the hash table. Also used during
2280 initialization. If CLEAR_ALL isn't set, then only clear the entries
2281 which are known to have been used. */
2284 clear_table (clear_all
)
2290 for (i
= 0; i
< cselib_nregs
; i
++)
2293 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (used_regs
); i
++)
2294 REG_VALUES (VARRAY_UINT (used_regs
, i
)) = 0;
2296 VARRAY_POP_ALL (used_regs
);
2298 htab_empty (hash_table
);
2299 obstack_free (&cselib_obstack
, cselib_startobj
);
2302 empty_elt_lists
= 0;
2303 empty_elt_loc_lists
= 0;
2304 n_useless_values
= 0;
2306 next_unknown_value
= 0;
2309 /* The equality test for our hash table. The first argument ENTRY is a table
2310 element (i.e. a cselib_val), while the second arg X is an rtx. We know
2311 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
2312 CONST of an appropriate mode. */
2315 entry_and_rtx_equal_p (entry
, x_arg
)
2316 const void *entry
, *x_arg
;
2318 struct elt_loc_list
*l
;
2319 const cselib_val
*v
= (const cselib_val
*) entry
;
2320 rtx x
= (rtx
) x_arg
;
2321 enum machine_mode mode
= GET_MODE (x
);
2323 if (GET_CODE (x
) == CONST_INT
2324 || (mode
== VOIDmode
&& GET_CODE (x
) == CONST_DOUBLE
))
2326 if (mode
!= GET_MODE (v
->u
.val_rtx
))
2329 /* Unwrap X if necessary. */
2330 if (GET_CODE (x
) == CONST
2331 && (GET_CODE (XEXP (x
, 0)) == CONST_INT
2332 || GET_CODE (XEXP (x
, 0)) == CONST_DOUBLE
))
2335 /* We don't guarantee that distinct rtx's have different hash values,
2336 so we need to do a comparison. */
2337 for (l
= v
->locs
; l
; l
= l
->next
)
2338 if (rtx_equal_for_cselib_p (l
->loc
, x
))
2344 /* The hash function for our hash table. The value is always computed with
2345 hash_rtx when adding an element; this function just extracts the hash
2346 value from a cselib_val structure. */
2349 get_value_hash (entry
)
2352 const cselib_val
*v
= (const cselib_val
*) entry
;
2356 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
2357 only return true for values which point to a cselib_val whose value
2358 element has been set to zero, which implies the cselib_val will be
2362 references_value_p (x
, only_useless
)
2366 enum rtx_code code
= GET_CODE (x
);
2367 const char *fmt
= GET_RTX_FORMAT (code
);
2370 if (GET_CODE (x
) == VALUE
2371 && (! only_useless
|| CSELIB_VAL_PTR (x
)->locs
== 0))
2374 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2376 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
2378 else if (fmt
[i
] == 'E')
2379 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2380 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
2387 /* For all locations found in X, delete locations that reference useless
2388 values (i.e. values without any location). Called through
2392 discard_useless_locs (x
, info
)
2394 void *info ATTRIBUTE_UNUSED
;
2396 cselib_val
*v
= (cselib_val
*)*x
;
2397 struct elt_loc_list
**p
= &v
->locs
;
2398 int had_locs
= v
->locs
!= 0;
2402 if (references_value_p ((*p
)->loc
, 1))
2403 unchain_one_elt_loc_list (p
);
2408 if (had_locs
&& v
->locs
== 0)
2411 values_became_useless
= 1;
2416 /* If X is a value with no locations, remove it from the hashtable. */
2419 discard_useless_values (x
, info
)
2421 void *info ATTRIBUTE_UNUSED
;
2423 cselib_val
*v
= (cselib_val
*)*x
;
2427 htab_clear_slot (hash_table
, x
);
2428 unchain_one_value (v
);
2435 /* Clean out useless values (i.e. those which no longer have locations
2436 associated with them) from the hash table. */
2439 remove_useless_values ()
2441 /* First pass: eliminate locations that reference the value. That in
2442 turn can make more values useless. */
2445 values_became_useless
= 0;
2446 htab_traverse (hash_table
, discard_useless_locs
, 0);
2448 while (values_became_useless
);
2450 /* Second pass: actually remove the values. */
2451 htab_traverse (hash_table
, discard_useless_values
, 0);
2453 if (n_useless_values
!= 0)
2457 /* Return nonzero if we can prove that X and Y contain the same value, taking
2458 our gathered information into account. */
2461 rtx_equal_for_cselib_p (x
, y
)
2468 if (GET_CODE (x
) == REG
|| GET_CODE (x
) == MEM
)
2470 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0);
2476 if (GET_CODE (y
) == REG
|| GET_CODE (y
) == MEM
)
2478 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0);
2487 if (GET_CODE (x
) == VALUE
&& GET_CODE (y
) == VALUE
)
2488 return CSELIB_VAL_PTR (x
) == CSELIB_VAL_PTR (y
);
2490 if (GET_CODE (x
) == VALUE
)
2492 cselib_val
*e
= CSELIB_VAL_PTR (x
);
2493 struct elt_loc_list
*l
;
2495 for (l
= e
->locs
; l
; l
= l
->next
)
2499 /* Avoid infinite recursion. */
2500 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
2502 else if (rtx_equal_for_cselib_p (t
, y
))
2509 if (GET_CODE (y
) == VALUE
)
2511 cselib_val
*e
= CSELIB_VAL_PTR (y
);
2512 struct elt_loc_list
*l
;
2514 for (l
= e
->locs
; l
; l
= l
->next
)
2518 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
2520 else if (rtx_equal_for_cselib_p (x
, t
))
2527 if (GET_CODE (x
) != GET_CODE (y
) || GET_MODE (x
) != GET_MODE (y
))
2530 /* This won't be handled correctly by the code below. */
2531 if (GET_CODE (x
) == LABEL_REF
)
2532 return XEXP (x
, 0) == XEXP (y
, 0);
2534 code
= GET_CODE (x
);
2535 fmt
= GET_RTX_FORMAT (code
);
2537 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2544 if (XWINT (x
, i
) != XWINT (y
, i
))
2550 if (XINT (x
, i
) != XINT (y
, i
))
2556 /* Two vectors must have the same length. */
2557 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
2560 /* And the corresponding elements must match. */
2561 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2562 if (! rtx_equal_for_cselib_p (XVECEXP (x
, i
, j
),
2568 if (! rtx_equal_for_cselib_p (XEXP (x
, i
), XEXP (y
, i
)))
2574 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
2579 /* These are just backpointers, so they don't matter. */
2586 /* It is believed that rtx's at this level will never
2587 contain anything but integers and other rtx's,
2588 except for within LABEL_REFs and SYMBOL_REFs. */
2596 /* We need to pass down the mode of constants through the hash table
2597 functions. For that purpose, wrap them in a CONST of the appropriate
2600 wrap_constant (mode
, x
)
2601 enum machine_mode mode
;
2604 if (GET_CODE (x
) != CONST_INT
2605 && (GET_CODE (x
) != CONST_DOUBLE
|| GET_MODE (x
) != VOIDmode
))
2607 if (mode
== VOIDmode
)
2609 return gen_rtx_CONST (mode
, x
);
2612 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
2613 For registers and memory locations, we look up their cselib_val structure
2614 and return its VALUE element.
2615 Possible reasons for return 0 are: the object is volatile, or we couldn't
2616 find a register or memory location in the table and CREATE is zero. If
2617 CREATE is nonzero, table elts are created for regs and mem.
2618 MODE is used in hashing for CONST_INTs only;
2619 otherwise the mode of X is used. */
2622 hash_rtx (x
, mode
, create
)
2624 enum machine_mode mode
;
2631 unsigned int hash
= 0;
2633 /* repeat is used to turn tail-recursion into iteration. */
2635 code
= GET_CODE (x
);
2636 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
2642 e
= cselib_lookup (x
, GET_MODE (x
), create
);
2649 hash
+= ((unsigned) CONST_INT
<< 7) + (unsigned) mode
+ INTVAL (x
);
2650 return hash
? hash
: (unsigned int) CONST_INT
;
2653 /* This is like the general case, except that it only counts
2654 the integers representing the constant. */
2655 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
2656 if (GET_MODE (x
) != VOIDmode
)
2657 for (i
= 2; i
< GET_RTX_LENGTH (CONST_DOUBLE
); i
++)
2658 hash
+= XWINT (x
, i
);
2660 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
2661 + (unsigned) CONST_DOUBLE_HIGH (x
));
2662 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
2664 /* Assume there is only one rtx object for any given label. */
2667 += ((unsigned) LABEL_REF
<< 7) + (unsigned long) XEXP (x
, 0);
2668 return hash
? hash
: (unsigned int) LABEL_REF
;
2672 += ((unsigned) SYMBOL_REF
<< 7) + (unsigned long) XSTR (x
, 0);
2673 return hash
? hash
: (unsigned int) SYMBOL_REF
;
2684 case UNSPEC_VOLATILE
:
2688 if (MEM_VOLATILE_P (x
))
2697 i
= GET_RTX_LENGTH (code
) - 1;
2698 fmt
= GET_RTX_FORMAT (code
);
2703 rtx tem
= XEXP (x
, i
);
2704 unsigned int tem_hash
;
2706 /* If we are about to do the last recursive call
2707 needed at this level, change it into iteration.
2708 This function is called enough to be worth it. */
2715 tem_hash
= hash_rtx (tem
, 0, create
);
2721 else if (fmt
[i
] == 'E')
2722 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2724 unsigned int tem_hash
= hash_rtx (XVECEXP (x
, i
, j
), 0, create
);
2731 else if (fmt
[i
] == 's')
2733 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
2739 else if (fmt
[i
] == 'i')
2740 hash
+= XINT (x
, i
);
2741 else if (fmt
[i
] == '0' || fmt
[i
] == 't')
2747 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
2750 /* Create a new value structure for VALUE and initialize it. The mode of the
2754 new_cselib_val (value
, mode
)
2756 enum machine_mode mode
;
2758 cselib_val
*e
= empty_vals
;
2761 empty_vals
= e
->u
.next_free
;
2763 e
= (cselib_val
*) obstack_alloc (&cselib_obstack
, sizeof (cselib_val
));
2769 e
->u
.val_rtx
= gen_rtx_VALUE (mode
);
2770 CSELIB_VAL_PTR (e
->u
.val_rtx
) = e
;
2776 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
2777 contains the data at this address. X is a MEM that represents the
2778 value. Update the two value structures to represent this situation. */
2781 add_mem_for_addr (addr_elt
, mem_elt
, x
)
2782 cselib_val
*addr_elt
, *mem_elt
;
2786 struct elt_loc_list
*l
;
2788 /* Avoid duplicates. */
2789 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
2790 if (GET_CODE (l
->loc
) == MEM
2791 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
2794 new = gen_rtx_MEM (GET_MODE (x
), addr_elt
->u
.val_rtx
);
2795 MEM_COPY_ATTRIBUTES (new, x
);
2797 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
2798 mem_elt
->locs
= new_elt_loc_list (mem_elt
->locs
, new);
2801 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
2802 If CREATE, make a new one if we haven't seen it before. */
2805 cselib_lookup_mem (x
, create
)
2809 enum machine_mode mode
= GET_MODE (x
);
2812 cselib_val
*mem_elt
;
2815 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
2816 || (FLOAT_MODE_P (mode
) && flag_float_store
))
2819 /* Look up the value for the address. */
2820 addr
= cselib_lookup (XEXP (x
, 0), mode
, create
);
2824 /* Find a value that describes a value of our mode at that address. */
2825 for (l
= addr
->addr_list
; l
; l
= l
->next
)
2826 if (GET_MODE (l
->elt
->u
.val_rtx
) == mode
)
2832 mem_elt
= new_cselib_val (++next_unknown_value
, mode
);
2833 add_mem_for_addr (addr
, mem_elt
, x
);
2834 slot
= htab_find_slot_with_hash (hash_table
, wrap_constant (mode
, x
),
2835 mem_elt
->value
, INSERT
);
2840 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
2841 with VALUE expressions. This way, it becomes independent of changes
2842 to registers and memory.
2843 X isn't actually modified; if modifications are needed, new rtl is
2844 allocated. However, the return value can share rtl with X. */
2847 cselib_subst_to_values (x
)
2850 enum rtx_code code
= GET_CODE (x
);
2851 const char *fmt
= GET_RTX_FORMAT (code
);
2860 for (l
= REG_VALUES (REGNO (x
)); l
; l
= l
->next
)
2861 if (GET_MODE (l
->elt
->u
.val_rtx
) == GET_MODE (x
))
2862 return l
->elt
->u
.val_rtx
;
2867 e
= cselib_lookup_mem (x
, 0);
2870 return e
->u
.val_rtx
;
2872 /* CONST_DOUBLEs must be special-cased here so that we won't try to
2873 look up the CONST_DOUBLE_MEM inside. */
2882 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2886 rtx t
= cselib_subst_to_values (XEXP (x
, i
));
2888 if (t
!= XEXP (x
, i
) && x
== copy
)
2889 copy
= shallow_copy_rtx (x
);
2893 else if (fmt
[i
] == 'E')
2897 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2899 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
));
2901 if (t
!= XVECEXP (x
, i
, j
) && XVEC (x
, i
) == XVEC (copy
, i
))
2904 copy
= shallow_copy_rtx (x
);
2906 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (x
, i
));
2907 for (k
= 0; k
< j
; k
++)
2908 XVECEXP (copy
, i
, k
) = XVECEXP (x
, i
, k
);
2911 XVECEXP (copy
, i
, j
) = t
;
2919 /* Look up the rtl expression X in our tables and return the value it has.
2920 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
2921 we create a new one if possible, using mode MODE if X doesn't have a mode
2922 (i.e. because it's a constant). */
2925 cselib_lookup (x
, mode
, create
)
2927 enum machine_mode mode
;
2932 unsigned int hashval
;
2934 if (GET_MODE (x
) != VOIDmode
)
2935 mode
= GET_MODE (x
);
2937 if (GET_CODE (x
) == VALUE
)
2938 return CSELIB_VAL_PTR (x
);
2940 if (GET_CODE (x
) == REG
)
2943 unsigned int i
= REGNO (x
);
2945 for (l
= REG_VALUES (i
); l
; l
= l
->next
)
2946 if (mode
== GET_MODE (l
->elt
->u
.val_rtx
))
2952 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
2953 e
->locs
= new_elt_loc_list (e
->locs
, x
);
2954 if (REG_VALUES (i
) == 0)
2955 VARRAY_PUSH_UINT (used_regs
, i
);
2956 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), e
);
2957 slot
= htab_find_slot_with_hash (hash_table
, x
, e
->value
, INSERT
);
2962 if (GET_CODE (x
) == MEM
)
2963 return cselib_lookup_mem (x
, create
);
2965 hashval
= hash_rtx (x
, mode
, create
);
2966 /* Can't even create if hashing is not possible. */
2970 slot
= htab_find_slot_with_hash (hash_table
, wrap_constant (mode
, x
),
2971 hashval
, create
? INSERT
: NO_INSERT
);
2975 e
= (cselib_val
*) *slot
;
2979 e
= new_cselib_val (hashval
, mode
);
2981 /* We have to fill the slot before calling cselib_subst_to_values:
2982 the hash table is inconsistent until we do so, and
2983 cselib_subst_to_values will need to do lookups. */
2985 e
->locs
= new_elt_loc_list (e
->locs
, cselib_subst_to_values (x
));
2989 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2990 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2991 is used to determine how many hard registers are being changed. If MODE
2992 is VOIDmode, then only REGNO is being changed; this is used when
2993 invalidating call clobbered registers across a call. */
2996 cselib_invalidate_regno (regno
, mode
)
2998 enum machine_mode mode
;
3000 unsigned int endregno
;
3003 /* If we see pseudos after reload, something is _wrong_. */
3004 if (reload_completed
&& regno
>= FIRST_PSEUDO_REGISTER
3005 && reg_renumber
[regno
] >= 0)
3008 /* Determine the range of registers that must be invalidated. For
3009 pseudos, only REGNO is affected. For hard regs, we must take MODE
3010 into account, and we must also invalidate lower register numbers
3011 if they contain values that overlap REGNO. */
3012 endregno
= regno
+ 1;
3013 if (regno
< FIRST_PSEUDO_REGISTER
&& mode
!= VOIDmode
)
3014 endregno
= regno
+ HARD_REGNO_NREGS (regno
, mode
);
3016 for (i
= 0; i
< endregno
; i
++)
3018 struct elt_list
**l
= ®_VALUES (i
);
3020 /* Go through all known values for this reg; if it overlaps the range
3021 we're invalidating, remove the value. */
3024 cselib_val
*v
= (*l
)->elt
;
3025 struct elt_loc_list
**p
;
3026 unsigned int this_last
= i
;
3028 if (i
< FIRST_PSEUDO_REGISTER
)
3029 this_last
+= HARD_REGNO_NREGS (i
, GET_MODE (v
->u
.val_rtx
)) - 1;
3031 if (this_last
< regno
)
3037 /* We have an overlap. */
3038 unchain_one_elt_list (l
);
3040 /* Now, we clear the mapping from value to reg. It must exist, so
3041 this code will crash intentionally if it doesn't. */
3042 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
3046 if (GET_CODE (x
) == REG
&& REGNO (x
) == i
)
3048 unchain_one_elt_loc_list (p
);
3058 /* The memory at address MEM_BASE is being changed.
3059 Return whether this change will invalidate VAL. */
3062 cselib_mem_conflict_p (mem_base
, val
)
3070 code
= GET_CODE (val
);
3073 /* Get rid of a few simple cases quickly. */
3086 if (GET_MODE (mem_base
) == BLKmode
3087 || GET_MODE (val
) == BLKmode
3088 || anti_dependence (val
, mem_base
))
3091 /* The address may contain nested MEMs. */
3098 fmt
= GET_RTX_FORMAT (code
);
3099 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3103 if (cselib_mem_conflict_p (mem_base
, XEXP (val
, i
)))
3106 else if (fmt
[i
] == 'E')
3107 for (j
= 0; j
< XVECLEN (val
, i
); j
++)
3108 if (cselib_mem_conflict_p (mem_base
, XVECEXP (val
, i
, j
)))
3115 /* For the value found in SLOT, walk its locations to determine if any overlap
3116 INFO (which is a MEM rtx). */
3119 cselib_invalidate_mem_1 (slot
, info
)
3123 cselib_val
*v
= (cselib_val
*) *slot
;
3124 rtx mem_rtx
= (rtx
) info
;
3125 struct elt_loc_list
**p
= &v
->locs
;
3126 int had_locs
= v
->locs
!= 0;
3132 struct elt_list
**mem_chain
;
3134 /* MEMs may occur in locations only at the top level; below
3135 that every MEM or REG is substituted by its VALUE. */
3136 if (GET_CODE (x
) != MEM
3137 || ! cselib_mem_conflict_p (mem_rtx
, x
))
3143 /* This one overlaps. */
3144 /* We must have a mapping from this MEM's address to the
3145 value (E). Remove that, too. */
3146 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0);
3147 mem_chain
= &addr
->addr_list
;
3150 if ((*mem_chain
)->elt
== v
)
3152 unchain_one_elt_list (mem_chain
);
3156 mem_chain
= &(*mem_chain
)->next
;
3159 unchain_one_elt_loc_list (p
);
3162 if (had_locs
&& v
->locs
== 0)
3168 /* Invalidate any locations in the table which are changed because of a
3169 store to MEM_RTX. If this is called because of a non-const call
3170 instruction, MEM_RTX is (mem:BLK const0_rtx). */
3173 cselib_invalidate_mem (mem_rtx
)
3176 htab_traverse (hash_table
, cselib_invalidate_mem_1
, mem_rtx
);
3179 /* Invalidate DEST, which is being assigned to or clobbered. The second and
3180 the third parameter exist so that this function can be passed to
3181 note_stores; they are ignored. */
3184 cselib_invalidate_rtx (dest
, ignore
, data
)
3186 rtx ignore ATTRIBUTE_UNUSED
;
3187 void *data ATTRIBUTE_UNUSED
;
3189 while (GET_CODE (dest
) == STRICT_LOW_PART
|| GET_CODE (dest
) == SIGN_EXTRACT
3190 || GET_CODE (dest
) == ZERO_EXTRACT
|| GET_CODE (dest
) == SUBREG
)
3191 dest
= XEXP (dest
, 0);
3193 if (GET_CODE (dest
) == REG
)
3194 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
3195 else if (GET_CODE (dest
) == MEM
)
3196 cselib_invalidate_mem (dest
);
3198 /* Some machines don't define AUTO_INC_DEC, but they still use push
3199 instructions. We need to catch that case here in order to
3200 invalidate the stack pointer correctly. Note that invalidating
3201 the stack pointer is different from invalidating DEST. */
3202 if (push_operand (dest
, GET_MODE (dest
)))
3203 cselib_invalidate_rtx (stack_pointer_rtx
, NULL_RTX
, NULL
);
3206 /* Record the result of a SET instruction. DEST is being set; the source
3207 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
3208 describes its address. */
3211 cselib_record_set (dest
, src_elt
, dest_addr_elt
)
3213 cselib_val
*src_elt
, *dest_addr_elt
;
3215 int dreg
= GET_CODE (dest
) == REG
? (int) REGNO (dest
) : -1;
3217 if (src_elt
== 0 || side_effects_p (dest
))
3222 if (REG_VALUES (dreg
) == 0)
3223 VARRAY_PUSH_UINT (used_regs
, dreg
);
3225 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
3226 if (src_elt
->locs
== 0)
3228 src_elt
->locs
= new_elt_loc_list (src_elt
->locs
, dest
);
3230 else if (GET_CODE (dest
) == MEM
&& dest_addr_elt
!= 0)
3232 if (src_elt
->locs
== 0)
3234 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
3238 /* Describe a single set that is part of an insn. */
3243 cselib_val
*src_elt
;
3244 cselib_val
*dest_addr_elt
;
3247 /* There is no good way to determine how many elements there can be
3248 in a PARALLEL. Since it's fairly cheap, use a really large number. */
3249 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
3251 /* Record the effects of any sets in INSN. */
3253 cselib_record_sets (insn
)
3258 struct set sets
[MAX_SETS
];
3259 rtx body
= PATTERN (insn
);
3261 body
= PATTERN (insn
);
3262 /* Find all sets. */
3263 if (GET_CODE (body
) == SET
)
3265 sets
[0].src
= SET_SRC (body
);
3266 sets
[0].dest
= SET_DEST (body
);
3269 else if (GET_CODE (body
) == PARALLEL
)
3271 /* Look through the PARALLEL and record the values being
3272 set, if possible. Also handle any CLOBBERs. */
3273 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
3275 rtx x
= XVECEXP (body
, 0, i
);
3277 if (GET_CODE (x
) == SET
)
3279 sets
[n_sets
].src
= SET_SRC (x
);
3280 sets
[n_sets
].dest
= SET_DEST (x
);
3286 /* Look up the values that are read. Do this before invalidating the
3287 locations that are written. */
3288 for (i
= 0; i
< n_sets
; i
++)
3290 rtx dest
= sets
[i
].dest
;
3292 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
3293 the low part after invalidating any knowledge about larger modes. */
3294 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
3295 sets
[i
].dest
= dest
= XEXP (dest
, 0);
3297 /* We don't know how to record anything but REG or MEM. */
3298 if (GET_CODE (dest
) == REG
|| GET_CODE (dest
) == MEM
)
3300 sets
[i
].src_elt
= cselib_lookup (sets
[i
].src
, GET_MODE (dest
), 1);
3301 if (GET_CODE (dest
) == MEM
)
3302 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0), Pmode
, 1);
3304 sets
[i
].dest_addr_elt
= 0;
3308 /* Invalidate all locations written by this insn. Note that the elts we
3309 looked up in the previous loop aren't affected, just some of their
3310 locations may go away. */
3311 note_stores (body
, cselib_invalidate_rtx
, NULL
);
3313 /* Now enter the equivalences in our tables. */
3314 for (i
= 0; i
< n_sets
; i
++)
3316 rtx dest
= sets
[i
].dest
;
3317 if (GET_CODE (dest
) == REG
|| GET_CODE (dest
) == MEM
)
3318 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
3322 /* Record the effects of INSN. */
3325 cselib_process_insn (insn
)
3331 cselib_current_insn
= insn
;
3333 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
3334 if (GET_CODE (insn
) == CODE_LABEL
3335 || (GET_CODE (insn
) == NOTE
3336 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_SETJMP
)
3337 || (GET_CODE (insn
) == INSN
3338 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
3339 && MEM_VOLATILE_P (PATTERN (insn
))))
3345 if (! INSN_P (insn
))
3347 cselib_current_insn
= 0;
3351 /* If this is a call instruction, forget anything stored in a
3352 call clobbered register, or, if this is not a const call, in
3354 if (GET_CODE (insn
) == CALL_INSN
)
3356 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
3357 if (call_used_regs
[i
])
3358 cselib_invalidate_regno (i
, VOIDmode
);
3360 if (! CONST_CALL_P (insn
))
3361 cselib_invalidate_mem (callmem
);
3364 cselib_record_sets (insn
);
3367 /* Clobber any registers which appear in REG_INC notes. We
3368 could keep track of the changes to their values, but it is
3369 unlikely to help. */
3370 for (x
= REG_NOTES (insn
); x
; x
= XEXP (x
, 1))
3371 if (REG_NOTE_KIND (x
) == REG_INC
)
3372 cselib_invalidate_rtx (XEXP (x
, 0), NULL_RTX
, NULL
);
3375 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
3376 after we have processed the insn. */
3377 if (GET_CODE (insn
) == CALL_INSN
)
3378 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
3379 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
3380 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0), NULL_RTX
, NULL
);
3382 cselib_current_insn
= 0;
3384 if (n_useless_values
> MAX_USELESS_VALUES
)
3385 remove_useless_values ();
3388 /* Make sure our varrays are big enough. Not called from any cselib routines;
3389 it must be called by the user if it allocated new registers. */
3392 cselib_update_varray_sizes ()
3394 unsigned int nregs
= max_reg_num ();
3396 if (nregs
== cselib_nregs
)
3399 cselib_nregs
= nregs
;
3400 VARRAY_GROW (reg_values
, nregs
);
3401 VARRAY_GROW (used_regs
, nregs
);
3404 /* Initialize cselib for one pass. The caller must also call
3405 init_alias_analysis. */
3410 /* These are only created once. */
3413 gcc_obstack_init (&cselib_obstack
);
3414 cselib_startobj
= obstack_alloc (&cselib_obstack
, 0);
3416 callmem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
3417 ggc_add_rtx_root (&callmem
, 1);
3420 cselib_nregs
= max_reg_num ();
3421 VARRAY_ELT_LIST_INIT (reg_values
, cselib_nregs
, "reg_values");
3422 VARRAY_UINT_INIT (used_regs
, cselib_nregs
, "used_regs");
3423 hash_table
= htab_create (31, get_value_hash
, entry_and_rtx_equal_p
, NULL
);
3427 /* Called when the current user is done with cselib. */
3433 VARRAY_FREE (reg_values
);
3434 VARRAY_FREE (used_regs
);
3435 htab_delete (hash_table
);