remove extraneous code checked in with previous delta
[official-gcc.git] / gcc / simplify-rtx.c
blob08abf69975758e139ec8f4f36faebe43a446dca9
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)
10 any later version.
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. */
23 #include "config.h"
24 /* stdio.h must precede rtl.h for FFS. */
25 #include "system.h"
26 #include <setjmp.h>
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "flags.h"
33 #include "real.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "function.h"
37 #include "expr.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "ggc.h"
41 #include "obstack.h"
42 #include "hashtab.h"
43 #include "cselib.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,
98 rtx, rtx));
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)
106 enum rtx_code code;
107 enum machine_mode mode;
108 rtx op0, op1;
110 rtx tem;
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);
125 if (tem)
126 return tem;
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));
137 else
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)
147 enum rtx_code code;
148 enum machine_mode mode;
149 rtx op;
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;
164 REAL_VALUE_TYPE d;
166 if (GET_CODE (op) == CONST_INT)
167 lv = INTVAL (op), hv = INTVAL (op) < 0 ? -1 : 0;
168 else
169 lv = CONST_DOUBLE_LOW (op), hv = CONST_DOUBLE_HIGH (op);
171 #ifdef REAL_ARITHMETIC
172 REAL_VALUE_FROM_INT (d, lv, hv, mode);
173 #else
174 if (hv < 0)
176 d = (double) (~ hv);
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);
180 d = (- d - 1.0);
182 else
184 d = (double) hv;
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;
197 REAL_VALUE_TYPE d;
199 if (GET_CODE (op) == CONST_INT)
200 lv = INTVAL (op), hv = INTVAL (op) < 0 ? -1 : 0;
201 else
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. */
208 if (hv < 0)
209 return 0;
211 else if (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT * 2)
213 else
214 hv = 0, lv &= GET_MODE_MASK (op_mode);
216 #ifdef REAL_ARITHMETIC
217 REAL_VALUE_FROM_UNSIGNED_INT (d, lv, hv, mode);
218 #else
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);
228 #endif
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;
236 switch (code)
238 case NOT:
239 val = ~ arg0;
240 break;
242 case NEG:
243 val = - arg0;
244 break;
246 case ABS:
247 val = (arg0 >= 0 ? arg0 : - arg0);
248 break;
250 case FFS:
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;
255 break;
257 case TRUNCATE:
258 val = arg0;
259 break;
261 case ZERO_EXTEND:
262 if (op_mode == VOIDmode)
263 op_mode = mode;
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))
270 abort ();
271 val = arg0;
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));
275 else
276 return 0;
277 break;
279 case SIGN_EXTEND:
280 if (op_mode == VOIDmode)
281 op_mode = mode;
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))
288 abort ();
289 val = arg0;
291 else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
294 = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
295 if (val
296 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1)))
297 val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
299 else
300 return 0;
301 break;
303 case SQRT:
304 return 0;
306 default:
307 abort ();
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);
324 else
325 l1 = INTVAL (op), h1 = l1 < 0 ? -1 : 0;
327 switch (code)
329 case NOT:
330 lv = ~ l1;
331 hv = ~ h1;
332 break;
334 case NEG:
335 neg_double (l1, h1, &lv, &hv);
336 break;
338 case ABS:
339 if (h1 < 0)
340 neg_double (l1, h1, &lv, &hv);
341 else
342 lv = l1, hv = h1;
343 break;
345 case FFS:
346 hv = 0;
347 if (l1 == 0)
348 lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & (-h1)) + 1;
349 else
350 lv = exact_log2 (l1 & (-l1)) + 1;
351 break;
353 case TRUNCATE:
354 /* This is just a change-of-mode, so do nothing. */
355 lv = l1, hv = h1;
356 break;
358 case ZERO_EXTEND:
359 if (op_mode == VOIDmode
360 || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
361 return 0;
363 hv = 0;
364 lv = l1 & GET_MODE_MASK (op_mode);
365 break;
367 case SIGN_EXTEND:
368 if (op_mode == VOIDmode
369 || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
370 return 0;
371 else
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;
381 break;
383 case SQRT:
384 return 0;
386 default:
387 return 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)
397 REAL_VALUE_TYPE d;
398 jmp_buf handler;
399 rtx x;
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. */
405 return 0;
407 set_float_handler (handler);
409 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
411 switch (code)
413 case NEG:
414 d = REAL_VALUE_NEGATE (d);
415 break;
417 case ABS:
418 if (REAL_VALUE_NEGATIVE (d))
419 d = REAL_VALUE_NEGATE (d);
420 break;
422 case FLOAT_TRUNCATE:
423 d = real_value_truncate (mode, d);
424 break;
426 case FLOAT_EXTEND:
427 /* All this does is change the mode. */
428 break;
430 case FIX:
431 d = REAL_VALUE_RNDZINT (d);
432 break;
434 case UNSIGNED_FIX:
435 d = REAL_VALUE_UNSIGNED_RNDZINT (d);
436 break;
438 case SQRT:
439 return 0;
441 default:
442 abort ();
445 x = CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
446 set_float_handler (NULL_PTR);
447 return x;
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)
455 REAL_VALUE_TYPE d;
456 jmp_buf handler;
457 HOST_WIDE_INT val;
459 if (setjmp (handler))
460 return 0;
462 set_float_handler (handler);
464 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
466 switch (code)
468 case FIX:
469 val = REAL_VALUE_FIX (d);
470 break;
472 case UNSIGNED_FIX:
473 val = REAL_VALUE_UNSIGNED_FIX (d);
474 break;
476 default:
477 abort ();
480 set_float_handler (NULL_PTR);
482 val = trunc_int_for_mode (val, mode);
484 return GEN_INT (val);
486 #endif
487 /* This was formerly used only for non-IEEE float.
488 eggert@twinsun.com says it is safe for IEEE also. */
489 else
491 /* There are some simplifications we can do even if the operands
492 aren't constant. */
493 switch (code)
495 case NEG:
496 case NOT:
497 /* (not (not X)) == X, similarly for NEG. */
498 if (GET_CODE (op) == code)
499 return XEXP (op, 0);
500 break;
502 case SIGN_EXTEND:
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
506 the Vax). */
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)
512 return XEXP (op, 0);
514 #ifdef POINTERS_EXTEND_UNSIGNED
515 if (! POINTERS_EXTEND_UNSIGNED
516 && mode == Pmode && GET_MODE (op) == ptr_mode
517 && CONSTANT_P (op))
518 return convert_memory_address (Pmode, op);
519 #endif
520 break;
522 #ifdef POINTERS_EXTEND_UNSIGNED
523 case ZERO_EXTEND:
524 if (POINTERS_EXTEND_UNSIGNED
525 && mode == Pmode && GET_MODE (op) == ptr_mode
526 && CONSTANT_P (op))
527 return convert_memory_address (Pmode, op);
528 break;
529 #endif
531 default:
532 break;
535 return 0;
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)
547 enum rtx_code code;
548 enum machine_mode mode;
549 rtx op0, op1;
551 register HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
552 HOST_WIDE_INT val;
553 int width = GET_MODE_BITSIZE (mode);
554 rtx tem;
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) == '<')
562 abort ();
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;
570 jmp_buf handler;
572 if (setjmp (handler))
573 return 0;
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))
585 return 0;
586 #endif
587 REAL_ARITHMETIC (value, rtx_to_tree_code (code), f0, f1);
588 #else
589 switch (code)
591 case PLUS:
592 value = f0 + f1;
593 break;
594 case MINUS:
595 value = f0 - f1;
596 break;
597 case MULT:
598 value = f0 * f1;
599 break;
600 case DIV:
601 #ifndef REAL_INFINITY
602 if (f1 == 0)
603 return 0;
604 #endif
605 value = f0 / f1;
606 break;
607 case SMIN:
608 value = MIN (f0, f1);
609 break;
610 case SMAX:
611 value = MAX (f0, f1);
612 break;
613 default:
614 abort ();
616 #endif
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);
634 else
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);
639 else
640 l2 = INTVAL (op1), h2 = l2 < 0 ? -1 : 0;
642 switch (code)
644 case MINUS:
645 /* A - B == A + (-B). */
646 neg_double (l2, h2, &lv, &hv);
647 l2 = lv, h2 = hv;
649 /* .. fall through ... */
651 case PLUS:
652 add_double (l1, h1, l2, h2, &lv, &hv);
653 break;
655 case MULT:
656 mul_double (l1, h1, l2, h2, &lv, &hv);
657 break;
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
661 it. */
662 return 0;
664 case AND:
665 lv = l1 & l2, hv = h1 & h2;
666 break;
668 case IOR:
669 lv = l1 | l2, hv = h1 | h2;
670 break;
672 case XOR:
673 lv = l1 ^ l2, hv = h1 ^ h2;
674 break;
676 case SMIN:
677 if (h1 < h2
678 || (h1 == h2
679 && ((unsigned HOST_WIDE_INT) l1
680 < (unsigned HOST_WIDE_INT) l2)))
681 lv = l1, hv = h1;
682 else
683 lv = l2, hv = h2;
684 break;
686 case SMAX:
687 if (h1 > h2
688 || (h1 == h2
689 && ((unsigned HOST_WIDE_INT) l1
690 > (unsigned HOST_WIDE_INT) l2)))
691 lv = l1, hv = h1;
692 else
693 lv = l2, hv = h2;
694 break;
696 case UMIN:
697 if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2
698 || (h1 == h2
699 && ((unsigned HOST_WIDE_INT) l1
700 < (unsigned HOST_WIDE_INT) l2)))
701 lv = l1, hv = h1;
702 else
703 lv = l2, hv = h2;
704 break;
706 case UMAX:
707 if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2
708 || (h1 == h2
709 && ((unsigned HOST_WIDE_INT) l1
710 > (unsigned HOST_WIDE_INT) l2)))
711 lv = l1, hv = h1;
712 else
713 lv = l2, hv = h2;
714 break;
716 case LSHIFTRT: case ASHIFTRT:
717 case ASHIFT:
718 case ROTATE: case ROTATERT:
719 #ifdef SHIFT_COUNT_TRUNCATED
720 if (SHIFT_COUNT_TRUNCATED)
721 l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0;
722 #endif
724 if (h2 != 0 || l2 < 0 || l2 >= GET_MODE_BITSIZE (mode))
725 return 0;
727 if (code == LSHIFTRT || code == ASHIFTRT)
728 rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
729 code == ASHIFTRT);
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);
736 break;
738 default:
739 return 0;
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. */
751 switch (code)
753 case PLUS:
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)
758 break;
760 if (op1 == CONST0_RTX (mode))
761 return op0;
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;
792 int had_mult = 0;
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);
800 had_mult = 1;
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));
808 lhs = XEXP (lhs, 0);
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);
817 had_mult = 1;
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));
825 rhs = XEXP (rhs, 0);
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)
846 return tem;
847 break;
849 case COMPARE:
850 #ifdef HAVE_cc0
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))
860 return op0;
861 #else
862 /* Do nothing here. */
863 #endif
864 break;
866 case MINUS:
867 /* None of these optimizations can be done for IEEE
868 floating point. */
869 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
870 && FLOAT_MODE_P (mode) && ! flag_fast_math)
871 break;
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))
891 return op0;
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;
903 int had_mult = 0;
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);
911 had_mult = 1;
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));
919 lhs = XEXP (lhs, 0);
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);
928 had_mult = 1;
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));
936 rhs = XEXP (rhs, 0);
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)
961 return tem;
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)));
977 break;
979 case MULT:
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))
992 return op1;
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))
998 return op0;
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)
1015 REAL_VALUE_TYPE d;
1016 jmp_buf handler;
1017 int op1is2, op1ism1;
1019 if (setjmp (handler))
1020 return 0;
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);
1035 break;
1037 case IOR:
1038 if (op1 == const0_rtx)
1039 return op0;
1040 if (GET_CODE (op1) == CONST_INT
1041 && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1042 return op1;
1043 if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1044 return 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)
1050 return constm1_rtx;
1051 break;
1053 case XOR:
1054 if (op1 == const0_rtx)
1055 return op0;
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)
1061 return const0_rtx;
1062 break;
1064 case AND:
1065 if (op1 == const0_rtx && ! side_effects_p (op0))
1066 return const0_rtx;
1067 if (GET_CODE (op1) == CONST_INT
1068 && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1069 return op0;
1070 if (op0 == op1 && ! side_effects_p (op0)
1071 && GET_MODE_CLASS (mode) != MODE_CC)
1072 return op0;
1073 /* A & (~A) -> 0 */
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)
1078 return const0_rtx;
1079 break;
1081 case UDIV:
1082 /* Convert divide by power of two into shift (divide by 1 handled
1083 below). */
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 ... */
1090 case DIV:
1091 if (op1 == CONST1_RTX (mode))
1092 return op0;
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))
1099 return op0;
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
1104 general. */
1105 else if (GET_CODE (op1) == CONST_DOUBLE
1106 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT
1107 && op1 != CONST0_RTX (mode)
1108 && flag_fast_math)
1110 REAL_VALUE_TYPE d;
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));
1119 #else
1120 return
1121 gen_rtx_MULT (mode, op0,
1122 CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode));
1123 #endif
1126 #endif
1127 break;
1129 case UMOD:
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 ... */
1137 case MOD:
1138 if ((op0 == const0_rtx || op1 == const1_rtx)
1139 && ! side_effects_p (op0) && ! side_effects_p (op1))
1140 return const0_rtx;
1141 break;
1143 case ROTATERT:
1144 case ROTATE:
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))
1149 return op0;
1151 /* ... fall through ... */
1153 case ASHIFT:
1154 case ASHIFTRT:
1155 case LSHIFTRT:
1156 if (op1 == const0_rtx)
1157 return op0;
1158 if (op0 == const0_rtx && ! side_effects_p (op1))
1159 return op0;
1160 break;
1162 case SMIN:
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))
1166 return op1;
1167 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1168 return op0;
1169 break;
1171 case SMAX:
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))
1176 return op1;
1177 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1178 return op0;
1179 break;
1181 case UMIN:
1182 if (op1 == const0_rtx && ! side_effects_p (op0))
1183 return op1;
1184 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1185 return op0;
1186 break;
1188 case UMAX:
1189 if (op1 == constm1_rtx && ! side_effects_p (op0))
1190 return op1;
1191 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1192 return op0;
1193 break;
1195 default:
1196 abort ();
1199 return 0;
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;
1213 arg0s = arg0;
1214 if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1215 arg0s |= ((HOST_WIDE_INT) (-1) << width);
1217 arg1s = arg1;
1218 if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1219 arg1s |= ((HOST_WIDE_INT) (-1) << width);
1221 else
1223 arg0s = arg0;
1224 arg1s = arg1;
1227 /* Compute the value of the arithmetic. */
1229 switch (code)
1231 case PLUS:
1232 val = arg0s + arg1s;
1233 break;
1235 case MINUS:
1236 val = arg0s - arg1s;
1237 break;
1239 case MULT:
1240 val = arg0s * arg1s;
1241 break;
1243 case DIV:
1244 if (arg1s == 0)
1245 return 0;
1246 val = arg0s / arg1s;
1247 break;
1249 case MOD:
1250 if (arg1s == 0)
1251 return 0;
1252 val = arg0s % arg1s;
1253 break;
1255 case UDIV:
1256 if (arg1 == 0)
1257 return 0;
1258 val = (unsigned HOST_WIDE_INT) arg0 / arg1;
1259 break;
1261 case UMOD:
1262 if (arg1 == 0)
1263 return 0;
1264 val = (unsigned HOST_WIDE_INT) arg0 % arg1;
1265 break;
1267 case AND:
1268 val = arg0 & arg1;
1269 break;
1271 case IOR:
1272 val = arg0 | arg1;
1273 break;
1275 case XOR:
1276 val = arg0 ^ arg1;
1277 break;
1279 case LSHIFTRT:
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. */
1282 if (arg1 < 0)
1283 return 0;
1285 #ifdef SHIFT_COUNT_TRUNCATED
1286 if (SHIFT_COUNT_TRUNCATED)
1287 arg1 %= width;
1288 #endif
1290 val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
1291 break;
1293 case ASHIFT:
1294 if (arg1 < 0)
1295 return 0;
1297 #ifdef SHIFT_COUNT_TRUNCATED
1298 if (SHIFT_COUNT_TRUNCATED)
1299 arg1 %= width;
1300 #endif
1302 val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
1303 break;
1305 case ASHIFTRT:
1306 if (arg1 < 0)
1307 return 0;
1309 #ifdef SHIFT_COUNT_TRUNCATED
1310 if (SHIFT_COUNT_TRUNCATED)
1311 arg1 %= width;
1312 #endif
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);
1321 break;
1323 case ROTATERT:
1324 if (arg1 < 0)
1325 return 0;
1327 arg1 %= width;
1328 val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
1329 | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
1330 break;
1332 case ROTATE:
1333 if (arg1 < 0)
1334 return 0;
1336 arg1 %= width;
1337 val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
1338 | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
1339 break;
1341 case COMPARE:
1342 /* Do nothing here. */
1343 return 0;
1345 case SMIN:
1346 val = arg0s <= arg1s ? arg0s : arg1s;
1347 break;
1349 case UMIN:
1350 val = ((unsigned HOST_WIDE_INT) arg0
1351 <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1352 break;
1354 case SMAX:
1355 val = arg0s > arg1s ? arg0s : arg1s;
1356 break;
1358 case UMAX:
1359 val = ((unsigned HOST_WIDE_INT) arg0
1360 > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1361 break;
1363 default:
1364 abort ();
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
1373 PLUS or MINUS.
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. */
1379 static rtx
1380 simplify_plus_minus (code, mode, op0, op1)
1381 enum rtx_code code;
1382 enum machine_mode mode;
1383 rtx op0, op1;
1385 rtx ops[8];
1386 int negs[8];
1387 rtx result, tem;
1388 int n_ops = 2, input_ops = 2, input_consts = 0, n_consts = 0;
1389 int first = 1, negate = 0, changed;
1390 int i, j;
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);
1400 changed = 1;
1401 while (changed)
1403 changed = 0;
1405 for (i = 0; i < n_ops; i++)
1406 switch (GET_CODE (ops[i]))
1408 case PLUS:
1409 case MINUS:
1410 if (n_ops == 7)
1411 return 0;
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);
1416 input_ops++;
1417 changed = 1;
1418 break;
1420 case NEG:
1421 ops[i] = XEXP (ops[i], 0);
1422 negs[i] = ! negs[i];
1423 changed = 1;
1424 break;
1426 case CONST:
1427 ops[i] = XEXP (ops[i], 0);
1428 input_consts++;
1429 changed = 1;
1430 break;
1432 case NOT:
1433 /* ~a -> (-a - 1) */
1434 if (n_ops != 7)
1436 ops[n_ops] = constm1_rtx;
1437 negs[n_ops++] = negs[i];
1438 ops[i] = XEXP (ops[i], 0);
1439 negs[i] = ! negs[i];
1440 changed = 1;
1442 break;
1444 case CONST_INT:
1445 if (negs[i])
1446 ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0, changed = 1;
1447 break;
1449 default:
1450 break;
1454 /* If we only have two operands, we can't do anything. */
1455 if (n_ops <= 2)
1456 return 0;
1458 /* Now simplify each pair of operands until nothing changes. The first
1459 time through just simplify constants against each other. */
1461 changed = 1;
1462 while (changed)
1464 changed = first;
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])
1477 ncode = MINUS;
1479 tem = simplify_binary_operation (ncode, mode, lhs, rhs);
1480 if (tem)
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;
1489 changed = 1;
1493 first = 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++)
1503 if (ops[j] != 0)
1505 ops[i] = ops[j], negs[i++] = negs[j];
1506 if (GET_CODE (ops[j]) == CONST)
1507 n_consts++;
1510 if (i + n_consts > input_ops
1511 || (i + n_consts == input_ops && n_consts <= input_consts))
1512 return 0;
1514 n_ops = i;
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++)
1529 if (i == n_ops)
1531 for (i = 0; i < n_ops; i++)
1532 negs[i] = 0;
1533 negate = 1;
1535 else if (i != 0)
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. */
1542 result = ops[0];
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;
1549 struct cfc_args
1551 rtx op0, op1; /* Input */
1552 int equal, op0lt, op1lt; /* Output */
1555 static void
1556 check_fold_consts (data)
1557 PTR 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)
1579 enum rtx_code code;
1580 enum machine_mode mode;
1581 rtx op0, op1;
1583 int equal, op0lt, op0ltu, op1lt, op1ltu;
1584 rtx tem;
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
1593 #ifdef HAVE_cc0
1594 || op0 == cc0_rtx
1595 #endif
1597 return 0;
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
1626 result. */
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
1633 the result. */
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() */
1641 args.op0 = op0;
1642 args.op1 = op1;
1644 if (do_float_handler(check_fold_consts, (PTR) &args) == 0)
1645 /* We got an exception from check_fold_consts() */
1646 return 0;
1648 /* Receive output from check_fold_consts() */
1649 equal = args.equal;
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);
1670 else
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);
1681 else
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. */
1712 else
1714 switch (code)
1716 case EQ:
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
1724 #endif
1726 return const0_rtx;
1727 break;
1729 case NE:
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
1734 #endif
1736 return const_true_rtx;
1737 break;
1739 case GEU:
1740 /* Unsigned values are never negative. */
1741 if (op1 == const0_rtx)
1742 return const_true_rtx;
1743 break;
1745 case LTU:
1746 if (op1 == const0_rtx)
1747 return const0_rtx;
1748 break;
1750 case LEU:
1751 /* Unsigned values are never greater than the largest
1752 unsigned value. */
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;
1757 break;
1759 case GTU:
1760 if (GET_CODE (op1) == CONST_INT
1761 && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1762 && INTEGRAL_MODE_P (mode))
1763 return const0_rtx;
1764 break;
1766 default:
1767 break;
1770 return 0;
1773 /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
1774 as appropriate. */
1775 switch (code)
1777 case EQ:
1778 return equal ? const_true_rtx : const0_rtx;
1779 case NE:
1780 return ! equal ? const_true_rtx : const0_rtx;
1781 case LT:
1782 return op0lt ? const_true_rtx : const0_rtx;
1783 case GT:
1784 return op1lt ? const_true_rtx : const0_rtx;
1785 case LTU:
1786 return op0ltu ? const_true_rtx : const0_rtx;
1787 case GTU:
1788 return op1ltu ? const_true_rtx : const0_rtx;
1789 case LE:
1790 return equal || op0lt ? const_true_rtx : const0_rtx;
1791 case GE:
1792 return equal || op1lt ? const_true_rtx : const0_rtx;
1793 case LEU:
1794 return equal || op0ltu ? const_true_rtx : const0_rtx;
1795 case GEU:
1796 return equal || op1ltu ? const_true_rtx : const0_rtx;
1797 default:
1798 abort ();
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)
1808 enum rtx_code code;
1809 enum machine_mode mode, op0_mode;
1810 rtx op0, op1, op2;
1812 int width = GET_MODE_BITSIZE (mode);
1814 /* VOIDmode means "infinite" precision. */
1815 if (width == 0)
1816 width = HOST_BITS_PER_WIDE_INT;
1818 switch (code)
1820 case SIGN_EXTRACT:
1821 case ZERO_EXTRACT:
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));
1834 else
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);
1858 break;
1860 case IF_THEN_ELSE:
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))
1868 return op1;
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))
1872 return op2;
1873 else if (GET_RTX_CLASS (GET_CODE (op0)) == '<' && ! side_effects_p (op0))
1875 rtx temp;
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)
1880 return op2;
1881 else if (temp == const1_rtx)
1882 return op1;
1884 break;
1886 default:
1887 abort ();
1890 return 0;
1893 /* Simplify X, an rtx expression.
1895 Return the simplified expression or NULL if no simplifications
1896 were possible.
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
1909 simplification.
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
1926 redundant/dead.
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. */
1934 simplify_rtx (x)
1935 rtx x;
1937 enum rtx_code code;
1938 enum machine_mode mode;
1940 mode = GET_MODE (x);
1941 code = GET_CODE (x);
1943 switch (GET_RTX_CLASS (code))
1945 case '1':
1946 return simplify_unary_operation (code, mode,
1947 XEXP (x, 0), GET_MODE (XEXP (x, 0)));
1948 case '2':
1949 case 'c':
1950 return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
1952 case '3':
1953 case 'b':
1954 return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
1955 XEXP (x, 0), XEXP (x, 1), XEXP (x, 2));
1957 case '<':
1958 return simplify_relational_operation (code, GET_MODE (XEXP (x, 0)),
1959 XEXP (x, 0), XEXP (x, 1));
1960 default:
1961 return NULL;
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. */
2027 static rtx callmem;
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
2041 arguments. */
2042 static struct elt_list *
2043 new_elt_list (next, elt)
2044 struct elt_list *next;
2045 cselib_val *elt;
2047 struct elt_list *el = empty_elt_lists;
2048 if (el)
2049 empty_elt_lists = el->next;
2050 else
2051 el = (struct elt_list *) obstack_alloc (&cselib_obstack,
2052 sizeof (struct elt_list));
2053 el->next = next;
2054 el->elt = elt;
2055 return el;
2058 /* Allocate a struct elt_loc_list and fill in its two elements with the
2059 arguments. */
2060 static struct elt_loc_list *
2061 new_elt_loc_list (next, loc)
2062 struct elt_loc_list *next;
2063 rtx loc;
2065 struct elt_loc_list *el = empty_elt_loc_lists;
2066 if (el)
2067 empty_elt_loc_lists = el->next;
2068 else
2069 el = (struct elt_loc_list *) obstack_alloc (&cselib_obstack,
2070 sizeof (struct elt_loc_list));
2071 el->next = next;
2072 el->loc = loc;
2073 el->setting_insn = cselib_current_insn;
2074 return el;
2077 /* The elt_list at *PL is no longer needed. Unchain it and free its
2078 storage. */
2079 static void
2080 unchain_one_elt_list (pl)
2081 struct elt_list **pl;
2083 struct elt_list *l = *pl;
2084 *pl = l->next;
2085 l->next = empty_elt_lists;
2086 empty_elt_lists = l;
2089 /* Likewise for elt_loc_lists. */
2090 static void
2091 unchain_one_elt_loc_list (pl)
2092 struct elt_loc_list **pl;
2094 struct elt_loc_list *l = *pl;
2095 *pl = l->next;
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
2101 V. */
2102 static void
2103 unchain_one_value (v)
2104 cselib_val *v;
2106 while (v->addr_list)
2107 unchain_one_elt_list (&v->addr_list);
2109 v->u.next_free = empty_vals;
2110 empty_vals = v;
2113 /* Remove all entries from the hash table. Also used during
2114 initialization. */
2115 static void
2116 clear_table ()
2118 int i;
2119 for (i = 0; i < cselib_nregs; i++)
2120 REG_VALUES (i) = 0;
2122 htab_empty (hash_table);
2123 obstack_free (&cselib_obstack, cselib_startobj);
2125 empty_vals = 0;
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. */
2135 static int
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;
2141 rtx x = (rtx)x_arg;
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))
2147 return 1;
2148 return 0;
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. */
2154 static unsigned int
2155 get_value_hash (entry)
2156 const void *entry;
2158 cselib_val *v = (cselib_val *) entry;
2159 return v->value;
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. */
2165 static int
2166 check_value_useless (v)
2167 cselib_val *v;
2169 if (v->locs != 0)
2170 return 0;
2172 if (v->value == 0)
2173 return 0;
2175 /* This is a marker to indicate that the value will be reclaimed. */
2176 v->value = 0;
2177 n_useless_values++;
2178 return 1;
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
2184 removed. */
2186 references_value_p (x, only_useless)
2187 rtx x;
2188 int only_useless;
2190 enum rtx_code code = GET_CODE (x);
2191 const char *fmt = GET_RTX_FORMAT (code);
2192 int i;
2194 if (GET_CODE (x) == VALUE
2195 && (! only_useless || CSELIB_VAL_PTR (x)->value == 0))
2196 return 1;
2198 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2200 if (fmt[i] == 'e')
2202 if (references_value_p (XEXP (x, i), only_useless))
2203 return 1;
2205 else if (fmt[i] == 'E')
2207 int j;
2209 for (j = 0; j < XVECLEN (x, i); j++)
2210 if (references_value_p (XVECEXP (x, i, j), only_useless))
2211 return 1;
2215 return 0;
2218 /* Set by discard_useless_locs if it deleted the last location of any
2219 value. */
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
2224 htab_traverse. */
2225 static int
2226 discard_useless_locs (x, info)
2227 void **x;
2228 void *info ATTRIBUTE_UNUSED;
2230 cselib_val *v = (cselib_val *)*x;
2231 struct elt_loc_list **p = &v->locs;
2233 while (*p)
2235 if (references_value_p ((*p)->loc, 1))
2236 unchain_one_elt_loc_list (p);
2237 else
2238 p = &(*p)->next;
2240 if (check_value_useless (v))
2241 values_became_useless = 1;
2243 return 1;
2246 /* If X is a value with no locations, remove it from the hashtable. */
2248 static int
2249 discard_useless_values (x, info)
2250 void **x;
2251 void *info ATTRIBUTE_UNUSED;
2253 cselib_val *v = (cselib_val *)*x;
2255 if (v->value == 0)
2257 htab_clear_slot (hash_table, x);
2258 unchain_one_value (v);
2259 n_useless_values--;
2261 return 1;
2264 /* Clean out useless values (i.e. those which no longer have locations
2265 associated with them) from the hash table. */
2266 static void
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)
2282 abort ();
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)
2289 rtx x, y;
2291 enum rtx_code code;
2292 const char *fmt;
2293 int i;
2295 if (GET_CODE (x) == REG || GET_CODE (x) == MEM)
2297 cselib_val *e = cselib_lookup (x, VOIDmode, 0);
2298 if (e)
2299 x = e->u.val_rtx;
2301 if (GET_CODE (y) == REG || GET_CODE (y) == MEM)
2303 cselib_val *e = cselib_lookup (y, VOIDmode, 0);
2304 if (e)
2305 y = e->u.val_rtx;
2308 if (x == y)
2309 return 1;
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)
2321 rtx t = l->loc;
2323 /* Avoid infinite recursion. */
2324 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
2325 continue;
2327 if (rtx_equal_for_cselib_p (t, y))
2328 return 1;
2331 return 0;
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)
2341 rtx t = l->loc;
2343 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
2344 continue;
2346 if (rtx_equal_for_cselib_p (x, t))
2347 return 1;
2350 return 0;
2353 if (GET_CODE (x) != GET_CODE (y)
2354 || GET_MODE (x) != GET_MODE (y))
2355 return 0;
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--)
2366 int j;
2367 switch (fmt[i])
2369 case 'w':
2370 if (XWINT (x, i) != XWINT (y, i))
2371 return 0;
2372 break;
2374 case 'n':
2375 case 'i':
2376 if (XINT (x, i) != XINT (y, i))
2377 return 0;
2378 break;
2380 case 'V':
2381 case 'E':
2382 /* Two vectors must have the same length. */
2383 if (XVECLEN (x, i) != XVECLEN (y, i))
2384 return 0;
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),
2389 XVECEXP (y, i, j)))
2390 return 0;
2391 break;
2393 case 'e':
2394 if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
2395 return 0;
2396 break;
2398 case 'S':
2399 case 's':
2400 if (strcmp (XSTR (x, i), XSTR (y, i)))
2401 return 0;
2402 break;
2404 case 'u':
2405 /* These are just backpointers, so they don't matter. */
2406 break;
2408 case '0':
2409 case 't':
2410 break;
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. */
2415 default:
2416 abort ();
2419 return 1;
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. */
2430 static unsigned int
2431 hash_rtx (x, mode, create)
2432 rtx x;
2433 enum machine_mode mode;
2434 int create;
2436 cselib_val *e;
2437 int i, j;
2438 enum rtx_code code;
2439 const char *fmt;
2440 unsigned int hash = 0;
2442 /* repeat is used to turn tail-recursion into iteration. */
2443 repeat:
2444 code = GET_CODE (x);
2445 hash += (unsigned) code + (unsigned) GET_MODE (x);
2447 switch (code)
2449 case MEM:
2450 case REG:
2451 e = cselib_lookup (x, GET_MODE (x), create);
2452 if (! e)
2453 return 0;
2454 hash += e->value;
2455 return hash;
2457 case CONST_INT:
2459 unsigned HOST_WIDE_INT tem = INTVAL (x);
2460 hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + tem;
2461 return hash ? hash : CONST_INT;
2464 case CONST_DOUBLE:
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);
2472 hash += tem;
2474 else
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. */
2480 case LABEL_REF:
2481 hash
2482 += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0);
2483 return hash ? hash : LABEL_REF;
2485 case SYMBOL_REF:
2486 hash
2487 += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0);
2488 return hash ? hash : SYMBOL_REF;
2490 case PRE_DEC:
2491 case PRE_INC:
2492 case POST_DEC:
2493 case POST_INC:
2494 case PC:
2495 case CC0:
2496 case CALL:
2497 case UNSPEC_VOLATILE:
2498 return 0;
2500 case ASM_OPERANDS:
2501 if (MEM_VOLATILE_P (x))
2502 return 0;
2504 break;
2506 default:
2507 break;
2510 i = GET_RTX_LENGTH (code) - 1;
2511 fmt = GET_RTX_FORMAT (code);
2512 for (; i >= 0; i--)
2514 if (fmt[i] == 'e')
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. */
2522 if (i == 0)
2524 x = tem;
2525 goto repeat;
2527 tem_hash = hash_rtx (tem, 0, create);
2528 if (tem_hash == 0)
2529 return 0;
2530 hash += tem_hash;
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);
2536 if (tem_hash == 0)
2537 return 0;
2538 hash += tem_hash;
2540 else if (fmt[i] == 's')
2542 unsigned char *p = (unsigned char *) XSTR (x, i);
2543 if (p)
2544 while (*p)
2545 hash += *p++;
2547 else if (fmt[i] == 'i')
2549 unsigned int tem = XINT (x, i);
2550 hash += tem;
2552 else if (fmt[i] == '0' || fmt[i] == 't')
2553 /* unused */;
2554 else
2555 abort ();
2557 return hash ? hash : 1 + GET_CODE (x);
2560 /* Create a new value structure for VALUE and initialize it. The mode of the
2561 value is MODE. */
2562 static cselib_val *
2563 new_cselib_val (value, mode)
2564 unsigned int value;
2565 enum machine_mode mode;
2567 cselib_val *e = empty_vals;
2568 if (e)
2569 empty_vals = e->u.next_free;
2570 else
2571 e = (cselib_val *) obstack_alloc (&cselib_obstack, sizeof (cselib_val));
2572 if (value == 0)
2573 abort ();
2574 e->value = value;
2575 e->u.val_rtx = gen_rtx_VALUE (mode);
2576 CSELIB_VAL_PTR (e->u.val_rtx) = e;
2578 e->addr_list = 0;
2579 e->locs = 0;
2580 return 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. */
2586 static void
2587 add_mem_for_addr (addr_elt, mem_elt, x)
2588 cselib_val *addr_elt, *mem_elt;
2589 rtx x;
2591 rtx new;
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)
2598 return;
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. */
2611 static cselib_val *
2612 cselib_lookup_mem (x, create)
2613 rtx x;
2614 int create;
2616 void **slot;
2617 cselib_val *addr;
2618 cselib_val *mem_elt;
2619 struct elt_list *l;
2621 if (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode)
2622 return 0;
2623 if (FLOAT_MODE_P (GET_MODE (x)) && flag_float_store)
2624 return 0;
2626 /* Look up the value for the address. */
2627 addr = cselib_lookup (XEXP (x, 0), GET_MODE (x), create);
2628 if (! addr)
2629 return 0;
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))
2634 return l->elt;
2635 if (! create)
2636 return 0;
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);
2640 *slot = mem_elt;
2641 return mem_elt;
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. */
2649 static rtx
2650 cselib_subst_to_values (x)
2651 rtx x;
2653 enum rtx_code code = GET_CODE (x);
2654 const char *fmt = GET_RTX_FORMAT (code);
2655 cselib_val *e;
2656 struct elt_list *l;
2657 rtx copy = x;
2658 int i;
2660 switch (code)
2662 case REG:
2663 i = REGNO (x);
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;
2667 abort ();
2669 case MEM:
2670 e = cselib_lookup_mem (x, 0);
2671 if (! e)
2672 abort ();
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. */
2677 case CONST_DOUBLE:
2678 case CONST_INT:
2679 return x;
2681 default:
2682 break;
2685 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2687 if (fmt[i] == 'e')
2689 rtx t = cselib_subst_to_values (XEXP (x, i));
2690 if (t != XEXP (x, i) && x == copy)
2691 copy = shallow_copy_rtx (x);
2692 XEXP (copy, i) = t;
2694 else if (fmt[i] == 'E')
2696 int j, k;
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))
2703 if (x == copy)
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;
2713 return copy;
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). */
2720 cselib_val *
2721 cselib_lookup (x, mode, create)
2722 rtx x;
2723 enum machine_mode mode;
2724 int create;
2726 void **slot;
2727 cselib_val *e;
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)
2738 struct elt_list *l;
2739 int i = REGNO (x);
2740 for (l = REG_VALUES (i); l; l = l->next)
2741 if (mode == GET_MODE (l->elt->u.val_rtx))
2742 return l->elt;
2743 if (! create)
2744 return 0;
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);
2749 *slot = e;
2750 return e;
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. */
2758 if (! hashval)
2759 return 0;
2761 slot = htab_find_slot_with_hash (hash_table, x, hashval, create);
2762 if (slot == 0)
2763 return 0;
2764 e = (cselib_val *) *slot;
2765 if (e)
2766 return e;
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. */
2772 *slot = (void *) e;
2773 e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
2774 return e;
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. */
2782 static void
2783 cselib_invalidate_regno (regno, mode)
2784 int regno;
2785 enum machine_mode mode;
2787 int endregno;
2788 int i;
2790 /* If we see pseudos after reload, something is _wrong_. */
2791 if (reload_completed && regno >= FIRST_PSEUDO_REGISTER
2792 && reg_renumber[regno] >= 0)
2793 abort ();
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 = &REG_VALUES (i);
2807 /* Go through all known values for this reg; if it overlaps the range
2808 we're invalidating, remove the value. */
2809 while (*l)
2811 cselib_val *v = (*l)->elt;
2812 struct elt_loc_list **p;
2813 int this_last = i;
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)
2819 l = &(*l)->next;
2820 continue;
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)
2829 rtx x = (*p)->loc;
2830 if (GET_CODE (x) == REG && REGNO (x) == i)
2832 unchain_one_elt_loc_list (p);
2833 break;
2836 check_value_useless (v);
2841 /* The memory at address MEM_BASE is being changed.
2842 Return whether this change will invalidate VAL. */
2843 static int
2844 cselib_mem_conflict_p (mem_base, val)
2845 rtx mem_base;
2846 rtx val;
2848 enum rtx_code code;
2849 const char *fmt;
2850 int i;
2852 code = GET_CODE (val);
2853 switch (code)
2855 /* Get rid of a few simple cases quickly. */
2856 case REG:
2857 case PC:
2858 case CC0:
2859 case SCRATCH:
2860 case CONST:
2861 case CONST_INT:
2862 case CONST_DOUBLE:
2863 case SYMBOL_REF:
2864 case LABEL_REF:
2865 return 0;
2867 case MEM:
2868 if (GET_MODE (mem_base) == BLKmode
2869 || GET_MODE (val) == BLKmode)
2870 return 1;
2871 if (anti_dependence (val, mem_base))
2872 return 1;
2873 /* The address may contain nested MEMs. */
2874 break;
2876 default:
2877 break;
2880 fmt = GET_RTX_FORMAT (code);
2882 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2884 if (fmt[i] == 'e')
2886 if (cselib_mem_conflict_p (mem_base, XEXP (val, i)))
2887 return 1;
2889 else if (fmt[i] == 'E')
2891 int j;
2893 for (j = 0; j < XVECLEN (val, i); j++)
2894 if (cselib_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
2895 return 1;
2899 return 0;
2902 /* For the value found in SLOT, walk its locations to determine if any overlap
2903 INFO (which is a MEM rtx). */
2904 static int
2905 cselib_invalidate_mem_1 (slot, info)
2906 void **slot;
2907 void *info;
2909 cselib_val *v = (cselib_val *) *slot;
2910 rtx mem_rtx = (rtx) info;
2911 struct elt_loc_list **p = &v->locs;
2913 while (*p)
2915 cselib_val *addr;
2916 struct elt_list **mem_chain;
2917 rtx x = (*p)->loc;
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))
2924 p = &(*p)->next;
2925 continue;
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;
2933 for (;;)
2935 if ((*mem_chain)->elt == v)
2937 unchain_one_elt_list (mem_chain);
2938 break;
2940 mem_chain = &(*mem_chain)->next;
2942 unchain_one_elt_loc_list (p);
2944 check_value_useless (v);
2945 return 1;
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). */
2951 static void
2952 cselib_invalidate_mem (mem_rtx)
2953 rtx 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. */
2961 static void
2962 cselib_invalidate_rtx (dest, ignore, data)
2963 rtx dest;
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. */
2989 static void
2990 cselib_record_set (dest, src_elt, dest_addr_elt)
2991 rtx dest;
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))
2997 return;
2999 if (dreg >= 0)
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. */
3009 struct set
3011 rtx src;
3012 rtx dest;
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. */
3022 static void
3023 cselib_record_sets (insn)
3024 rtx insn;
3026 int n_sets = 0;
3027 int i;
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);
3037 n_sets = 1;
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);
3051 n_sets++;
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,
3065 else
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. */
3080 void
3081 cselib_process_insn (insn)
3082 rtx insn;
3084 int i;
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))))
3096 clear_table ();
3097 return;
3100 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
3102 cselib_current_insn = 0;
3103 return;
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
3107 memory. */
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);
3120 #ifdef AUTO_INC_DEC
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. */
3125 rtx x;
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);
3131 #endif
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)
3137 rtx x;
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,
3142 NULL);
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. */
3153 void
3154 cselib_update_varray_sizes ()
3156 int nregs = max_reg_num ();
3157 if (nregs == cselib_nregs)
3158 return;
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. */
3165 void
3166 cselib_init ()
3168 /* These are only created once. */
3169 if (! callmem)
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);
3177 pop_obstacks ();
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);
3184 clear_table ();
3187 /* Called when the current user is done with cselib. */
3188 void
3189 cselib_finish ()
3191 clear_table ();
3192 htab_delete (hash_table);