* Makefile.in (SYSTEM_H): Define.
[official-gcc.git] / gcc / simplify-rtx.c
blobdd7d2e1e6b5ef6d156acdee618cc653fda4f7f90
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)
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 #include "system.h"
25 #include <setjmp.h>
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "flags.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "function.h"
36 #include "expr.h"
37 #include "toplev.h"
38 #include "output.h"
39 #include "ggc.h"
41 /* Simplification and canonicalization of RTL. */
43 /* Nonzero if X has the form (PLUS frame-pointer integer). We check for
44 virtual regs here because the simplify_*_operation routines are called
45 by integrate.c, which is called before virtual register instantiation.
47 ?!? FIXED_BASE_PLUS_P and NONZERO_BASE_PLUS_P need to move into
48 a header file so that their definitions can be shared with the
49 simplification routines in simplify-rtx.c. Until then, do not
50 change these macros without also changing the copy in simplify-rtx.c. */
52 #define FIXED_BASE_PLUS_P(X) \
53 ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \
54 || ((X) == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])\
55 || (X) == virtual_stack_vars_rtx \
56 || (X) == virtual_incoming_args_rtx \
57 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
58 && (XEXP (X, 0) == frame_pointer_rtx \
59 || XEXP (X, 0) == hard_frame_pointer_rtx \
60 || ((X) == arg_pointer_rtx \
61 && fixed_regs[ARG_POINTER_REGNUM]) \
62 || XEXP (X, 0) == virtual_stack_vars_rtx \
63 || XEXP (X, 0) == virtual_incoming_args_rtx)) \
64 || GET_CODE (X) == ADDRESSOF)
66 /* Similar, but also allows reference to the stack pointer.
68 This used to include FIXED_BASE_PLUS_P, however, we can't assume that
69 arg_pointer_rtx by itself is nonzero, because on at least one machine,
70 the i960, the arg pointer is zero when it is unused. */
72 #define NONZERO_BASE_PLUS_P(X) \
73 ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \
74 || (X) == virtual_stack_vars_rtx \
75 || (X) == virtual_incoming_args_rtx \
76 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
77 && (XEXP (X, 0) == frame_pointer_rtx \
78 || XEXP (X, 0) == hard_frame_pointer_rtx \
79 || ((X) == arg_pointer_rtx \
80 && fixed_regs[ARG_POINTER_REGNUM]) \
81 || XEXP (X, 0) == virtual_stack_vars_rtx \
82 || XEXP (X, 0) == virtual_incoming_args_rtx)) \
83 || (X) == stack_pointer_rtx \
84 || (X) == virtual_stack_dynamic_rtx \
85 || (X) == virtual_outgoing_args_rtx \
86 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
87 && (XEXP (X, 0) == stack_pointer_rtx \
88 || XEXP (X, 0) == virtual_stack_dynamic_rtx \
89 || XEXP (X, 0) == virtual_outgoing_args_rtx)) \
90 || GET_CODE (X) == ADDRESSOF)
92 /* Much code operates on (low, high) pairs; the low value is an
93 unsigned wide int, the high value a signed wide int. We
94 occasionally need to sign extend from low to high as if low were a
95 signed wide int. */
96 #define HWI_SIGN_EXTEND(low) \
97 ((((HOST_WIDE_INT) low) < 0) ? ((HOST_WIDE_INT) -1) : ((HOST_WIDE_INT) 0))
99 static rtx simplify_plus_minus PARAMS ((enum rtx_code,
100 enum machine_mode, rtx, rtx));
101 static void check_fold_consts PARAMS ((PTR));
103 /* Make a binary operation by properly ordering the operands and
104 seeing if the expression folds. */
107 simplify_gen_binary (code, mode, op0, op1)
108 enum rtx_code code;
109 enum machine_mode mode;
110 rtx op0, op1;
112 rtx tem;
114 /* Put complex operands first and constants second if commutative. */
115 if (GET_RTX_CLASS (code) == 'c'
116 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
117 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
118 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
119 || (GET_CODE (op0) == SUBREG
120 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
121 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
122 tem = op0, op0 = op1, op1 = tem;
124 /* If this simplifies, do it. */
125 tem = simplify_binary_operation (code, mode, op0, op1);
127 if (tem)
128 return tem;
130 /* Handle addition and subtraction of CONST_INT specially. Otherwise,
131 just form the operation. */
133 if (code == PLUS && GET_CODE (op1) == CONST_INT
134 && GET_MODE (op0) != VOIDmode)
135 return plus_constant (op0, INTVAL (op1));
136 else if (code == MINUS && GET_CODE (op1) == CONST_INT
137 && GET_MODE (op0) != VOIDmode)
138 return plus_constant (op0, - INTVAL (op1));
139 else
140 return gen_rtx_fmt_ee (code, mode, op0, op1);
143 /* Make a unary operation by first seeing if it folds and otherwise making
144 the specified operation. */
147 simplify_gen_unary (code, mode, op, op_mode)
148 enum rtx_code code;
149 enum machine_mode mode;
150 rtx op;
151 enum machine_mode op_mode;
153 rtx tem;
155 /* If this simplifies, use it. */
156 if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
157 return tem;
159 return gen_rtx_fmt_e (code, mode, op);
162 /* Likewise for ternary operations. */
165 simplify_gen_ternary (code, mode, op0_mode, op0, op1, op2)
166 enum rtx_code code;
167 enum machine_mode mode, op0_mode;
168 rtx op0, op1, op2;
170 rtx tem;
172 /* If this simplifies, use it. */
173 if (0 != (tem = simplify_ternary_operation (code, mode, op0_mode,
174 op0, op1, op2)))
175 return tem;
177 return gen_rtx_fmt_eee (code, mode, op0, op1, op2);
180 /* Likewise, for relational operations. */
183 simplify_gen_relational (code, mode, op0, op1)
184 enum rtx_code code;
185 enum machine_mode mode;
186 rtx op0, op1;
188 rtx tem;
190 if ((tem = simplify_relational_operation (code, mode, op0, op1)) != 0)
191 return tem;
193 /* Put complex operands first and constants second. */
194 if ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
195 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
196 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
197 || (GET_CODE (op0) == SUBREG
198 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
199 && GET_RTX_CLASS (GET_CODE (op1)) != 'o'))
200 tem = op0, op0 = op1, op1 = tem, code = swap_condition (code);
202 return gen_rtx_fmt_ee (code, mode, op0, op1);
205 /* Replace all occurrences of OLD in X with NEW and try to simplify the
206 resulting RTX. Return a new RTX which is as simplified as possible. */
209 simplify_replace_rtx (x, old, new)
210 rtx x;
211 rtx old;
212 rtx new;
214 enum rtx_code code = GET_CODE (x);
215 enum machine_mode mode = GET_MODE (x);
217 /* If X is OLD, return NEW. Otherwise, if this is an expression, try
218 to build a new expression substituting recursively. If we can't do
219 anything, return our input. */
221 if (x == old)
222 return new;
224 switch (GET_RTX_CLASS (code))
226 case '1':
228 enum machine_mode op_mode = GET_MODE (XEXP (x, 0));
229 rtx op = (XEXP (x, 0) == old
230 ? new : simplify_replace_rtx (XEXP (x, 0), old, new));
232 return simplify_gen_unary (code, mode, op, op_mode);
235 case '2':
236 case 'c':
237 return
238 simplify_gen_binary (code, mode,
239 simplify_replace_rtx (XEXP (x, 0), old, new),
240 simplify_replace_rtx (XEXP (x, 1), old, new));
242 case '3':
243 case 'b':
244 return
245 simplify_gen_ternary (code, mode, GET_MODE (XEXP (x, 0)),
246 simplify_replace_rtx (XEXP (x, 0), old, new),
247 simplify_replace_rtx (XEXP (x, 1), old, new),
248 simplify_replace_rtx (XEXP (x, 2), old, new));
250 case 'x':
251 /* The only case we try to handle is a lowpart SUBREG of a single-word
252 CONST_INT. */
253 if (code == SUBREG && subreg_lowpart_p (x) && old == SUBREG_REG (x)
254 && GET_CODE (new) == CONST_INT
255 && GET_MODE_SIZE (GET_MODE (old)) <= UNITS_PER_WORD)
256 return GEN_INT (INTVAL (new) & GET_MODE_MASK (mode));
258 return x;
260 default:
261 return x;
265 /* Try to simplify a unary operation CODE whose output mode is to be
266 MODE with input operand OP whose mode was originally OP_MODE.
267 Return zero if no simplification can be made. */
270 simplify_unary_operation (code, mode, op, op_mode)
271 enum rtx_code code;
272 enum machine_mode mode;
273 rtx op;
274 enum machine_mode op_mode;
276 unsigned int width = GET_MODE_BITSIZE (mode);
278 /* The order of these tests is critical so that, for example, we don't
279 check the wrong mode (input vs. output) for a conversion operation,
280 such as FIX. At some point, this should be simplified. */
282 #if !defined(REAL_IS_NOT_DOUBLE) || defined(REAL_ARITHMETIC)
284 if (code == FLOAT && GET_MODE (op) == VOIDmode
285 && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
287 HOST_WIDE_INT hv, lv;
288 REAL_VALUE_TYPE d;
290 if (GET_CODE (op) == CONST_INT)
291 lv = INTVAL (op), hv = HWI_SIGN_EXTEND (lv);
292 else
293 lv = CONST_DOUBLE_LOW (op), hv = CONST_DOUBLE_HIGH (op);
295 #ifdef REAL_ARITHMETIC
296 REAL_VALUE_FROM_INT (d, lv, hv, mode);
297 #else
298 if (hv < 0)
300 d = (double) (~ hv);
301 d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
302 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
303 d += (double) (unsigned HOST_WIDE_INT) (~ lv);
304 d = (- d - 1.0);
306 else
308 d = (double) hv;
309 d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
310 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
311 d += (double) (unsigned HOST_WIDE_INT) lv;
313 #endif /* REAL_ARITHMETIC */
314 d = real_value_truncate (mode, d);
315 return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
317 else if (code == UNSIGNED_FLOAT && GET_MODE (op) == VOIDmode
318 && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
320 HOST_WIDE_INT hv, lv;
321 REAL_VALUE_TYPE d;
323 if (GET_CODE (op) == CONST_INT)
324 lv = INTVAL (op), hv = HWI_SIGN_EXTEND (lv);
325 else
326 lv = CONST_DOUBLE_LOW (op), hv = CONST_DOUBLE_HIGH (op);
328 if (op_mode == VOIDmode)
330 /* We don't know how to interpret negative-looking numbers in
331 this case, so don't try to fold those. */
332 if (hv < 0)
333 return 0;
335 else if (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT * 2)
337 else
338 hv = 0, lv &= GET_MODE_MASK (op_mode);
340 #ifdef REAL_ARITHMETIC
341 REAL_VALUE_FROM_UNSIGNED_INT (d, lv, hv, mode);
342 #else
344 d = (double) (unsigned HOST_WIDE_INT) hv;
345 d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
346 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
347 d += (double) (unsigned HOST_WIDE_INT) lv;
348 #endif /* REAL_ARITHMETIC */
349 d = real_value_truncate (mode, d);
350 return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
352 #endif
354 if (GET_CODE (op) == CONST_INT
355 && width <= HOST_BITS_PER_WIDE_INT && width > 0)
357 register HOST_WIDE_INT arg0 = INTVAL (op);
358 register HOST_WIDE_INT val;
360 switch (code)
362 case NOT:
363 val = ~ arg0;
364 break;
366 case NEG:
367 val = - arg0;
368 break;
370 case ABS:
371 val = (arg0 >= 0 ? arg0 : - arg0);
372 break;
374 case FFS:
375 /* Don't use ffs here. Instead, get low order bit and then its
376 number. If arg0 is zero, this will return 0, as desired. */
377 arg0 &= GET_MODE_MASK (mode);
378 val = exact_log2 (arg0 & (- arg0)) + 1;
379 break;
381 case TRUNCATE:
382 val = arg0;
383 break;
385 case ZERO_EXTEND:
386 if (op_mode == VOIDmode)
387 op_mode = mode;
388 if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
390 /* If we were really extending the mode,
391 we would have to distinguish between zero-extension
392 and sign-extension. */
393 if (width != GET_MODE_BITSIZE (op_mode))
394 abort ();
395 val = arg0;
397 else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
398 val = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
399 else
400 return 0;
401 break;
403 case SIGN_EXTEND:
404 if (op_mode == VOIDmode)
405 op_mode = mode;
406 if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
408 /* If we were really extending the mode,
409 we would have to distinguish between zero-extension
410 and sign-extension. */
411 if (width != GET_MODE_BITSIZE (op_mode))
412 abort ();
413 val = arg0;
415 else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
418 = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
419 if (val
420 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1)))
421 val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
423 else
424 return 0;
425 break;
427 case SQRT:
428 case FLOAT_EXTEND:
429 case FLOAT_TRUNCATE:
430 return 0;
432 default:
433 abort ();
436 val = trunc_int_for_mode (val, mode);
438 return GEN_INT (val);
441 /* We can do some operations on integer CONST_DOUBLEs. Also allow
442 for a DImode operation on a CONST_INT. */
443 else if (GET_MODE (op) == VOIDmode && width <= HOST_BITS_PER_INT * 2
444 && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
446 unsigned HOST_WIDE_INT l1, lv;
447 HOST_WIDE_INT h1, hv;
449 if (GET_CODE (op) == CONST_DOUBLE)
450 l1 = CONST_DOUBLE_LOW (op), h1 = CONST_DOUBLE_HIGH (op);
451 else
452 l1 = INTVAL (op), h1 = HWI_SIGN_EXTEND (l1);
454 switch (code)
456 case NOT:
457 lv = ~ l1;
458 hv = ~ h1;
459 break;
461 case NEG:
462 neg_double (l1, h1, &lv, &hv);
463 break;
465 case ABS:
466 if (h1 < 0)
467 neg_double (l1, h1, &lv, &hv);
468 else
469 lv = l1, hv = h1;
470 break;
472 case FFS:
473 hv = 0;
474 if (l1 == 0)
475 lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & (-h1)) + 1;
476 else
477 lv = exact_log2 (l1 & (-l1)) + 1;
478 break;
480 case TRUNCATE:
481 /* This is just a change-of-mode, so do nothing. */
482 lv = l1, hv = h1;
483 break;
485 case ZERO_EXTEND:
486 if (op_mode == VOIDmode
487 || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
488 return 0;
490 hv = 0;
491 lv = l1 & GET_MODE_MASK (op_mode);
492 break;
494 case SIGN_EXTEND:
495 if (op_mode == VOIDmode
496 || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
497 return 0;
498 else
500 lv = l1 & GET_MODE_MASK (op_mode);
501 if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
502 && (lv & ((HOST_WIDE_INT) 1
503 << (GET_MODE_BITSIZE (op_mode) - 1))) != 0)
504 lv -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
506 hv = HWI_SIGN_EXTEND (lv);
508 break;
510 case SQRT:
511 return 0;
513 default:
514 return 0;
517 return immed_double_const (lv, hv, mode);
520 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
521 else if (GET_CODE (op) == CONST_DOUBLE
522 && GET_MODE_CLASS (mode) == MODE_FLOAT)
524 REAL_VALUE_TYPE d;
525 jmp_buf handler;
526 rtx x;
528 if (setjmp (handler))
529 /* There used to be a warning here, but that is inadvisable.
530 People may want to cause traps, and the natural way
531 to do it should not get a warning. */
532 return 0;
534 set_float_handler (handler);
536 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
538 switch (code)
540 case NEG:
541 d = REAL_VALUE_NEGATE (d);
542 break;
544 case ABS:
545 if (REAL_VALUE_NEGATIVE (d))
546 d = REAL_VALUE_NEGATE (d);
547 break;
549 case FLOAT_TRUNCATE:
550 d = real_value_truncate (mode, d);
551 break;
553 case FLOAT_EXTEND:
554 /* All this does is change the mode. */
555 break;
557 case FIX:
558 d = REAL_VALUE_RNDZINT (d);
559 break;
561 case UNSIGNED_FIX:
562 d = REAL_VALUE_UNSIGNED_RNDZINT (d);
563 break;
565 case SQRT:
566 return 0;
568 default:
569 abort ();
572 x = CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
573 set_float_handler (NULL_PTR);
574 return x;
577 else if (GET_CODE (op) == CONST_DOUBLE
578 && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT
579 && GET_MODE_CLASS (mode) == MODE_INT
580 && width <= HOST_BITS_PER_WIDE_INT && width > 0)
582 REAL_VALUE_TYPE d;
583 jmp_buf handler;
584 HOST_WIDE_INT val;
586 if (setjmp (handler))
587 return 0;
589 set_float_handler (handler);
591 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
593 switch (code)
595 case FIX:
596 val = REAL_VALUE_FIX (d);
597 break;
599 case UNSIGNED_FIX:
600 val = REAL_VALUE_UNSIGNED_FIX (d);
601 break;
603 default:
604 abort ();
607 set_float_handler (NULL_PTR);
609 val = trunc_int_for_mode (val, mode);
611 return GEN_INT (val);
613 #endif
614 /* This was formerly used only for non-IEEE float.
615 eggert@twinsun.com says it is safe for IEEE also. */
616 else
618 enum rtx_code reversed;
619 /* There are some simplifications we can do even if the operands
620 aren't constant. */
621 switch (code)
623 case NOT:
624 /* (not (not X)) == X. */
625 if (GET_CODE (op) == NOT)
626 return XEXP (op, 0);
628 /* (not (eq X Y)) == (ne X Y), etc. */
629 if (mode == BImode && GET_RTX_CLASS (GET_CODE (op)) == '<'
630 && ((reversed = reversed_comparison_code (op, NULL_RTX))
631 != UNKNOWN))
632 return gen_rtx_fmt_ee (reversed,
633 op_mode, XEXP (op, 0), XEXP (op, 1));
634 break;
636 case NEG:
637 /* (neg (neg X)) == X. */
638 if (GET_CODE (op) == NEG)
639 return XEXP (op, 0);
640 break;
642 case SIGN_EXTEND:
643 /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
644 becomes just the MINUS if its mode is MODE. This allows
645 folding switch statements on machines using casesi (such as
646 the Vax). */
647 if (GET_CODE (op) == TRUNCATE
648 && GET_MODE (XEXP (op, 0)) == mode
649 && GET_CODE (XEXP (op, 0)) == MINUS
650 && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
651 && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
652 return XEXP (op, 0);
654 #ifdef POINTERS_EXTEND_UNSIGNED
655 if (! POINTERS_EXTEND_UNSIGNED
656 && mode == Pmode && GET_MODE (op) == ptr_mode
657 && (CONSTANT_P (op)
658 || (GET_CODE (op) == SUBREG
659 && GET_CODE (SUBREG_REG (op)) == REG
660 && REG_POINTER (SUBREG_REG (op))
661 && GET_MODE (SUBREG_REG (op)) == Pmode)))
662 return convert_memory_address (Pmode, op);
663 #endif
664 break;
666 #ifdef POINTERS_EXTEND_UNSIGNED
667 case ZERO_EXTEND:
668 if (POINTERS_EXTEND_UNSIGNED
669 && mode == Pmode && GET_MODE (op) == ptr_mode
670 && (CONSTANT_P (op)
671 || (GET_CODE (op) == SUBREG
672 && GET_CODE (SUBREG_REG (op)) == REG
673 && REG_POINTER (SUBREG_REG (op))
674 && GET_MODE (SUBREG_REG (op)) == Pmode)))
675 return convert_memory_address (Pmode, op);
676 break;
677 #endif
679 default:
680 break;
683 return 0;
687 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
688 and OP1. Return 0 if no simplification is possible.
690 Don't use this for relational operations such as EQ or LT.
691 Use simplify_relational_operation instead. */
694 simplify_binary_operation (code, mode, op0, op1)
695 enum rtx_code code;
696 enum machine_mode mode;
697 rtx op0, op1;
699 register HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
700 HOST_WIDE_INT val;
701 unsigned int width = GET_MODE_BITSIZE (mode);
702 rtx tem;
704 /* Relational operations don't work here. We must know the mode
705 of the operands in order to do the comparison correctly.
706 Assuming a full word can give incorrect results.
707 Consider comparing 128 with -128 in QImode. */
709 if (GET_RTX_CLASS (code) == '<')
710 abort ();
712 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
713 if (GET_MODE_CLASS (mode) == MODE_FLOAT
714 && GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
715 && mode == GET_MODE (op0) && mode == GET_MODE (op1))
717 REAL_VALUE_TYPE f0, f1, value;
718 jmp_buf handler;
720 if (setjmp (handler))
721 return 0;
723 set_float_handler (handler);
725 REAL_VALUE_FROM_CONST_DOUBLE (f0, op0);
726 REAL_VALUE_FROM_CONST_DOUBLE (f1, op1);
727 f0 = real_value_truncate (mode, f0);
728 f1 = real_value_truncate (mode, f1);
730 #ifdef REAL_ARITHMETIC
731 #ifndef REAL_INFINITY
732 if (code == DIV && REAL_VALUES_EQUAL (f1, dconst0))
733 return 0;
734 #endif
735 REAL_ARITHMETIC (value, rtx_to_tree_code (code), f0, f1);
736 #else
737 switch (code)
739 case PLUS:
740 value = f0 + f1;
741 break;
742 case MINUS:
743 value = f0 - f1;
744 break;
745 case MULT:
746 value = f0 * f1;
747 break;
748 case DIV:
749 #ifndef REAL_INFINITY
750 if (f1 == 0)
751 return 0;
752 #endif
753 value = f0 / f1;
754 break;
755 case SMIN:
756 value = MIN (f0, f1);
757 break;
758 case SMAX:
759 value = MAX (f0, f1);
760 break;
761 default:
762 abort ();
764 #endif
766 value = real_value_truncate (mode, value);
767 set_float_handler (NULL_PTR);
768 return CONST_DOUBLE_FROM_REAL_VALUE (value, mode);
770 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
772 /* We can fold some multi-word operations. */
773 if (GET_MODE_CLASS (mode) == MODE_INT
774 && width == HOST_BITS_PER_WIDE_INT * 2
775 && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
776 && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
778 unsigned HOST_WIDE_INT l1, l2, lv;
779 HOST_WIDE_INT h1, h2, hv;
781 if (GET_CODE (op0) == CONST_DOUBLE)
782 l1 = CONST_DOUBLE_LOW (op0), h1 = CONST_DOUBLE_HIGH (op0);
783 else
784 l1 = INTVAL (op0), h1 = HWI_SIGN_EXTEND (l1);
786 if (GET_CODE (op1) == CONST_DOUBLE)
787 l2 = CONST_DOUBLE_LOW (op1), h2 = CONST_DOUBLE_HIGH (op1);
788 else
789 l2 = INTVAL (op1), h2 = HWI_SIGN_EXTEND (l2);
791 switch (code)
793 case MINUS:
794 /* A - B == A + (-B). */
795 neg_double (l2, h2, &lv, &hv);
796 l2 = lv, h2 = hv;
798 /* .. fall through ... */
800 case PLUS:
801 add_double (l1, h1, l2, h2, &lv, &hv);
802 break;
804 case MULT:
805 mul_double (l1, h1, l2, h2, &lv, &hv);
806 break;
808 case DIV: case MOD: case UDIV: case UMOD:
809 /* We'd need to include tree.h to do this and it doesn't seem worth
810 it. */
811 return 0;
813 case AND:
814 lv = l1 & l2, hv = h1 & h2;
815 break;
817 case IOR:
818 lv = l1 | l2, hv = h1 | h2;
819 break;
821 case XOR:
822 lv = l1 ^ l2, hv = h1 ^ h2;
823 break;
825 case SMIN:
826 if (h1 < h2
827 || (h1 == h2
828 && ((unsigned HOST_WIDE_INT) l1
829 < (unsigned HOST_WIDE_INT) l2)))
830 lv = l1, hv = h1;
831 else
832 lv = l2, hv = h2;
833 break;
835 case SMAX:
836 if (h1 > h2
837 || (h1 == h2
838 && ((unsigned HOST_WIDE_INT) l1
839 > (unsigned HOST_WIDE_INT) l2)))
840 lv = l1, hv = h1;
841 else
842 lv = l2, hv = h2;
843 break;
845 case UMIN:
846 if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2
847 || (h1 == h2
848 && ((unsigned HOST_WIDE_INT) l1
849 < (unsigned HOST_WIDE_INT) l2)))
850 lv = l1, hv = h1;
851 else
852 lv = l2, hv = h2;
853 break;
855 case UMAX:
856 if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2
857 || (h1 == h2
858 && ((unsigned HOST_WIDE_INT) l1
859 > (unsigned HOST_WIDE_INT) l2)))
860 lv = l1, hv = h1;
861 else
862 lv = l2, hv = h2;
863 break;
865 case LSHIFTRT: case ASHIFTRT:
866 case ASHIFT:
867 case ROTATE: case ROTATERT:
868 #ifdef SHIFT_COUNT_TRUNCATED
869 if (SHIFT_COUNT_TRUNCATED)
870 l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0;
871 #endif
873 if (h2 != 0 || l2 >= GET_MODE_BITSIZE (mode))
874 return 0;
876 if (code == LSHIFTRT || code == ASHIFTRT)
877 rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
878 code == ASHIFTRT);
879 else if (code == ASHIFT)
880 lshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv, 1);
881 else if (code == ROTATE)
882 lrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
883 else /* code == ROTATERT */
884 rrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
885 break;
887 default:
888 return 0;
891 return immed_double_const (lv, hv, mode);
894 if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT
895 || width > HOST_BITS_PER_WIDE_INT || width == 0)
897 /* Even if we can't compute a constant result,
898 there are some cases worth simplifying. */
900 switch (code)
902 case PLUS:
903 /* In IEEE floating point, x+0 is not the same as x. Similarly
904 for the other optimizations below. */
905 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
906 && FLOAT_MODE_P (mode) && ! flag_unsafe_math_optimizations)
907 break;
909 if (op1 == CONST0_RTX (mode))
910 return op0;
912 /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */
913 if (GET_CODE (op0) == NEG)
914 return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
915 else if (GET_CODE (op1) == NEG)
916 return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
918 /* Handle both-operands-constant cases. We can only add
919 CONST_INTs to constants since the sum of relocatable symbols
920 can't be handled by most assemblers. Don't add CONST_INT
921 to CONST_INT since overflow won't be computed properly if wider
922 than HOST_BITS_PER_WIDE_INT. */
924 if (CONSTANT_P (op0) && GET_MODE (op0) != VOIDmode
925 && GET_CODE (op1) == CONST_INT)
926 return plus_constant (op0, INTVAL (op1));
927 else if (CONSTANT_P (op1) && GET_MODE (op1) != VOIDmode
928 && GET_CODE (op0) == CONST_INT)
929 return plus_constant (op1, INTVAL (op0));
931 /* See if this is something like X * C - X or vice versa or
932 if the multiplication is written as a shift. If so, we can
933 distribute and make a new multiply, shift, or maybe just
934 have X (if C is 2 in the example above). But don't make
935 real multiply if we didn't have one before. */
937 if (! FLOAT_MODE_P (mode))
939 HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
940 rtx lhs = op0, rhs = op1;
941 int had_mult = 0;
943 if (GET_CODE (lhs) == NEG)
944 coeff0 = -1, lhs = XEXP (lhs, 0);
945 else if (GET_CODE (lhs) == MULT
946 && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
948 coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
949 had_mult = 1;
951 else if (GET_CODE (lhs) == ASHIFT
952 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
953 && INTVAL (XEXP (lhs, 1)) >= 0
954 && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
956 coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
957 lhs = XEXP (lhs, 0);
960 if (GET_CODE (rhs) == NEG)
961 coeff1 = -1, rhs = XEXP (rhs, 0);
962 else if (GET_CODE (rhs) == MULT
963 && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
965 coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
966 had_mult = 1;
968 else if (GET_CODE (rhs) == ASHIFT
969 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
970 && INTVAL (XEXP (rhs, 1)) >= 0
971 && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
973 coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
974 rhs = XEXP (rhs, 0);
977 if (rtx_equal_p (lhs, rhs))
979 tem = simplify_gen_binary (MULT, mode, lhs,
980 GEN_INT (coeff0 + coeff1));
981 return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
985 /* If one of the operands is a PLUS or a MINUS, see if we can
986 simplify this by the associative law.
987 Don't use the associative law for floating point.
988 The inaccuracy makes it nonassociative,
989 and subtle programs can break if operations are associated. */
991 if (INTEGRAL_MODE_P (mode)
992 && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
993 || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
994 && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
995 return tem;
996 break;
998 case COMPARE:
999 #ifdef HAVE_cc0
1000 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
1001 using cc0, in which case we want to leave it as a COMPARE
1002 so we can distinguish it from a register-register-copy.
1004 In IEEE floating point, x-0 is not the same as x. */
1006 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1007 || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1008 && op1 == CONST0_RTX (mode))
1009 return op0;
1010 #endif
1012 /* Convert (compare (gt (flags) 0) (lt (flags) 0)) to (flags). */
1013 if (((GET_CODE (op0) == GT && GET_CODE (op1) == LT)
1014 || (GET_CODE (op0) == GTU && GET_CODE (op1) == LTU))
1015 && XEXP (op0, 1) == const0_rtx && XEXP (op1, 1) == const0_rtx)
1017 rtx xop00 = XEXP (op0, 0);
1018 rtx xop10 = XEXP (op1, 0);
1020 #ifdef HAVE_cc0
1021 if (GET_CODE (xop00) == CC0 && GET_CODE (xop10) == CC0)
1022 #else
1023 if (GET_CODE (xop00) == REG && GET_CODE (xop10) == REG
1024 && GET_MODE (xop00) == GET_MODE (xop10)
1025 && REGNO (xop00) == REGNO (xop10)
1026 && GET_MODE_CLASS (GET_MODE (xop00)) == MODE_CC
1027 && GET_MODE_CLASS (GET_MODE (xop10)) == MODE_CC)
1028 #endif
1029 return xop00;
1032 break;
1033 case MINUS:
1034 /* None of these optimizations can be done for IEEE
1035 floating point. */
1036 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1037 && FLOAT_MODE_P (mode) && ! flag_unsafe_math_optimizations)
1038 break;
1040 /* We can't assume x-x is 0 even with non-IEEE floating point,
1041 but since it is zero except in very strange circumstances, we
1042 will treat it as zero with -funsafe-math-optimizations. */
1043 if (rtx_equal_p (op0, op1)
1044 && ! side_effects_p (op0)
1045 && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations))
1046 return CONST0_RTX (mode);
1048 /* Change subtraction from zero into negation. */
1049 if (op0 == CONST0_RTX (mode))
1050 return gen_rtx_NEG (mode, op1);
1052 /* (-1 - a) is ~a. */
1053 if (op0 == constm1_rtx)
1054 return gen_rtx_NOT (mode, op1);
1056 /* Subtracting 0 has no effect. */
1057 if (op1 == CONST0_RTX (mode))
1058 return op0;
1060 /* See if this is something like X * C - X or vice versa or
1061 if the multiplication is written as a shift. If so, we can
1062 distribute and make a new multiply, shift, or maybe just
1063 have X (if C is 2 in the example above). But don't make
1064 real multiply if we didn't have one before. */
1066 if (! FLOAT_MODE_P (mode))
1068 HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
1069 rtx lhs = op0, rhs = op1;
1070 int had_mult = 0;
1072 if (GET_CODE (lhs) == NEG)
1073 coeff0 = -1, lhs = XEXP (lhs, 0);
1074 else if (GET_CODE (lhs) == MULT
1075 && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
1077 coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
1078 had_mult = 1;
1080 else if (GET_CODE (lhs) == ASHIFT
1081 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
1082 && INTVAL (XEXP (lhs, 1)) >= 0
1083 && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
1085 coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
1086 lhs = XEXP (lhs, 0);
1089 if (GET_CODE (rhs) == NEG)
1090 coeff1 = - 1, rhs = XEXP (rhs, 0);
1091 else if (GET_CODE (rhs) == MULT
1092 && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
1094 coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
1095 had_mult = 1;
1097 else if (GET_CODE (rhs) == ASHIFT
1098 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
1099 && INTVAL (XEXP (rhs, 1)) >= 0
1100 && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
1102 coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
1103 rhs = XEXP (rhs, 0);
1106 if (rtx_equal_p (lhs, rhs))
1108 tem = simplify_gen_binary (MULT, mode, lhs,
1109 GEN_INT (coeff0 - coeff1));
1110 return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
1114 /* (a - (-b)) -> (a + b). */
1115 if (GET_CODE (op1) == NEG)
1116 return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
1118 /* If one of the operands is a PLUS or a MINUS, see if we can
1119 simplify this by the associative law.
1120 Don't use the associative law for floating point.
1121 The inaccuracy makes it nonassociative,
1122 and subtle programs can break if operations are associated. */
1124 if (INTEGRAL_MODE_P (mode)
1125 && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1126 || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
1127 && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
1128 return tem;
1130 /* Don't let a relocatable value get a negative coeff. */
1131 if (GET_CODE (op1) == CONST_INT && GET_MODE (op0) != VOIDmode)
1132 return plus_constant (op0, - INTVAL (op1));
1134 /* (x - (x & y)) -> (x & ~y) */
1135 if (GET_CODE (op1) == AND)
1137 if (rtx_equal_p (op0, XEXP (op1, 0)))
1138 return simplify_gen_binary (AND, mode, op0,
1139 gen_rtx_NOT (mode, XEXP (op1, 1)));
1140 if (rtx_equal_p (op0, XEXP (op1, 1)))
1141 return simplify_gen_binary (AND, mode, op0,
1142 gen_rtx_NOT (mode, XEXP (op1, 0)));
1144 break;
1146 case MULT:
1147 if (op1 == constm1_rtx)
1149 tem = simplify_unary_operation (NEG, mode, op0, mode);
1151 return tem ? tem : gen_rtx_NEG (mode, op0);
1154 /* In IEEE floating point, x*0 is not always 0. */
1155 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1156 || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1157 && op1 == CONST0_RTX (mode)
1158 && ! side_effects_p (op0))
1159 return op1;
1161 /* In IEEE floating point, x*1 is not equivalent to x for nans.
1162 However, ANSI says we can drop signals,
1163 so we can do this anyway. */
1164 if (op1 == CONST1_RTX (mode))
1165 return op0;
1167 /* Convert multiply by constant power of two into shift unless
1168 we are still generating RTL. This test is a kludge. */
1169 if (GET_CODE (op1) == CONST_INT
1170 && (val = exact_log2 (INTVAL (op1))) >= 0
1171 /* If the mode is larger than the host word size, and the
1172 uppermost bit is set, then this isn't a power of two due
1173 to implicit sign extension. */
1174 && (width <= HOST_BITS_PER_WIDE_INT
1175 || val != HOST_BITS_PER_WIDE_INT - 1)
1176 && ! rtx_equal_function_value_matters)
1177 return gen_rtx_ASHIFT (mode, op0, GEN_INT (val));
1179 if (GET_CODE (op1) == CONST_DOUBLE
1180 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT)
1182 REAL_VALUE_TYPE d;
1183 jmp_buf handler;
1184 int op1is2, op1ism1;
1186 if (setjmp (handler))
1187 return 0;
1189 set_float_handler (handler);
1190 REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
1191 op1is2 = REAL_VALUES_EQUAL (d, dconst2);
1192 op1ism1 = REAL_VALUES_EQUAL (d, dconstm1);
1193 set_float_handler (NULL_PTR);
1195 /* x*2 is x+x and x*(-1) is -x */
1196 if (op1is2 && GET_MODE (op0) == mode)
1197 return gen_rtx_PLUS (mode, op0, copy_rtx (op0));
1199 else if (op1ism1 && GET_MODE (op0) == mode)
1200 return gen_rtx_NEG (mode, op0);
1202 break;
1204 case IOR:
1205 if (op1 == const0_rtx)
1206 return op0;
1207 if (GET_CODE (op1) == CONST_INT
1208 && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1209 return op1;
1210 if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1211 return op0;
1212 /* A | (~A) -> -1 */
1213 if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1214 || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1215 && ! side_effects_p (op0)
1216 && GET_MODE_CLASS (mode) != MODE_CC)
1217 return constm1_rtx;
1218 break;
1220 case XOR:
1221 if (op1 == const0_rtx)
1222 return op0;
1223 if (GET_CODE (op1) == CONST_INT
1224 && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1225 return gen_rtx_NOT (mode, op0);
1226 if (op0 == op1 && ! side_effects_p (op0)
1227 && GET_MODE_CLASS (mode) != MODE_CC)
1228 return const0_rtx;
1229 break;
1231 case AND:
1232 if (op1 == const0_rtx && ! side_effects_p (op0))
1233 return const0_rtx;
1234 if (GET_CODE (op1) == CONST_INT
1235 && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1236 return op0;
1237 if (op0 == op1 && ! side_effects_p (op0)
1238 && GET_MODE_CLASS (mode) != MODE_CC)
1239 return op0;
1240 /* A & (~A) -> 0 */
1241 if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1242 || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1243 && ! side_effects_p (op0)
1244 && GET_MODE_CLASS (mode) != MODE_CC)
1245 return const0_rtx;
1246 break;
1248 case UDIV:
1249 /* Convert divide by power of two into shift (divide by 1 handled
1250 below). */
1251 if (GET_CODE (op1) == CONST_INT
1252 && (arg1 = exact_log2 (INTVAL (op1))) > 0)
1253 return gen_rtx_LSHIFTRT (mode, op0, GEN_INT (arg1));
1255 /* ... fall through ... */
1257 case DIV:
1258 if (op1 == CONST1_RTX (mode))
1259 return op0;
1261 /* In IEEE floating point, 0/x is not always 0. */
1262 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1263 || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1264 && op0 == CONST0_RTX (mode)
1265 && ! side_effects_p (op1))
1266 return op0;
1268 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1269 /* Change division by a constant into multiplication. Only do
1270 this with -funsafe-math-optimizations. */
1271 else if (GET_CODE (op1) == CONST_DOUBLE
1272 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT
1273 && op1 != CONST0_RTX (mode)
1274 && flag_unsafe_math_optimizations)
1276 REAL_VALUE_TYPE d;
1277 REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
1279 if (! REAL_VALUES_EQUAL (d, dconst0))
1281 #if defined (REAL_ARITHMETIC)
1282 REAL_ARITHMETIC (d, rtx_to_tree_code (DIV), dconst1, d);
1283 return gen_rtx_MULT (mode, op0,
1284 CONST_DOUBLE_FROM_REAL_VALUE (d, mode));
1285 #else
1286 return
1287 gen_rtx_MULT (mode, op0,
1288 CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode));
1289 #endif
1292 #endif
1293 break;
1295 case UMOD:
1296 /* Handle modulus by power of two (mod with 1 handled below). */
1297 if (GET_CODE (op1) == CONST_INT
1298 && exact_log2 (INTVAL (op1)) > 0)
1299 return gen_rtx_AND (mode, op0, GEN_INT (INTVAL (op1) - 1));
1301 /* ... fall through ... */
1303 case MOD:
1304 if ((op0 == const0_rtx || op1 == const1_rtx)
1305 && ! side_effects_p (op0) && ! side_effects_p (op1))
1306 return const0_rtx;
1307 break;
1309 case ROTATERT:
1310 case ROTATE:
1311 /* Rotating ~0 always results in ~0. */
1312 if (GET_CODE (op0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT
1313 && (unsigned HOST_WIDE_INT) INTVAL (op0) == GET_MODE_MASK (mode)
1314 && ! side_effects_p (op1))
1315 return op0;
1317 /* ... fall through ... */
1319 case ASHIFT:
1320 case ASHIFTRT:
1321 case LSHIFTRT:
1322 if (op1 == const0_rtx)
1323 return op0;
1324 if (op0 == const0_rtx && ! side_effects_p (op1))
1325 return op0;
1326 break;
1328 case SMIN:
1329 if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT
1330 && INTVAL (op1) == (HOST_WIDE_INT) 1 << (width -1)
1331 && ! side_effects_p (op0))
1332 return op1;
1333 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1334 return op0;
1335 break;
1337 case SMAX:
1338 if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT
1339 && ((unsigned HOST_WIDE_INT) INTVAL (op1)
1340 == (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode) >> 1)
1341 && ! side_effects_p (op0))
1342 return op1;
1343 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1344 return op0;
1345 break;
1347 case UMIN:
1348 if (op1 == const0_rtx && ! side_effects_p (op0))
1349 return op1;
1350 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1351 return op0;
1352 break;
1354 case UMAX:
1355 if (op1 == constm1_rtx && ! side_effects_p (op0))
1356 return op1;
1357 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1358 return op0;
1359 break;
1361 default:
1362 abort ();
1365 return 0;
1368 /* Get the integer argument values in two forms:
1369 zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S. */
1371 arg0 = INTVAL (op0);
1372 arg1 = INTVAL (op1);
1374 if (width < HOST_BITS_PER_WIDE_INT)
1376 arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
1377 arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
1379 arg0s = arg0;
1380 if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1381 arg0s |= ((HOST_WIDE_INT) (-1) << width);
1383 arg1s = arg1;
1384 if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1385 arg1s |= ((HOST_WIDE_INT) (-1) << width);
1387 else
1389 arg0s = arg0;
1390 arg1s = arg1;
1393 /* Compute the value of the arithmetic. */
1395 switch (code)
1397 case PLUS:
1398 val = arg0s + arg1s;
1399 break;
1401 case MINUS:
1402 val = arg0s - arg1s;
1403 break;
1405 case MULT:
1406 val = arg0s * arg1s;
1407 break;
1409 case DIV:
1410 if (arg1s == 0)
1411 return 0;
1412 val = arg0s / arg1s;
1413 break;
1415 case MOD:
1416 if (arg1s == 0)
1417 return 0;
1418 val = arg0s % arg1s;
1419 break;
1421 case UDIV:
1422 if (arg1 == 0)
1423 return 0;
1424 val = (unsigned HOST_WIDE_INT) arg0 / arg1;
1425 break;
1427 case UMOD:
1428 if (arg1 == 0)
1429 return 0;
1430 val = (unsigned HOST_WIDE_INT) arg0 % arg1;
1431 break;
1433 case AND:
1434 val = arg0 & arg1;
1435 break;
1437 case IOR:
1438 val = arg0 | arg1;
1439 break;
1441 case XOR:
1442 val = arg0 ^ arg1;
1443 break;
1445 case LSHIFTRT:
1446 /* If shift count is undefined, don't fold it; let the machine do
1447 what it wants. But truncate it if the machine will do that. */
1448 if (arg1 < 0)
1449 return 0;
1451 #ifdef SHIFT_COUNT_TRUNCATED
1452 if (SHIFT_COUNT_TRUNCATED)
1453 arg1 %= width;
1454 #endif
1456 val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
1457 break;
1459 case ASHIFT:
1460 if (arg1 < 0)
1461 return 0;
1463 #ifdef SHIFT_COUNT_TRUNCATED
1464 if (SHIFT_COUNT_TRUNCATED)
1465 arg1 %= width;
1466 #endif
1468 val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
1469 break;
1471 case ASHIFTRT:
1472 if (arg1 < 0)
1473 return 0;
1475 #ifdef SHIFT_COUNT_TRUNCATED
1476 if (SHIFT_COUNT_TRUNCATED)
1477 arg1 %= width;
1478 #endif
1480 val = arg0s >> arg1;
1482 /* Bootstrap compiler may not have sign extended the right shift.
1483 Manually extend the sign to insure bootstrap cc matches gcc. */
1484 if (arg0s < 0 && arg1 > 0)
1485 val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
1487 break;
1489 case ROTATERT:
1490 if (arg1 < 0)
1491 return 0;
1493 arg1 %= width;
1494 val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
1495 | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
1496 break;
1498 case ROTATE:
1499 if (arg1 < 0)
1500 return 0;
1502 arg1 %= width;
1503 val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
1504 | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
1505 break;
1507 case COMPARE:
1508 /* Do nothing here. */
1509 return 0;
1511 case SMIN:
1512 val = arg0s <= arg1s ? arg0s : arg1s;
1513 break;
1515 case UMIN:
1516 val = ((unsigned HOST_WIDE_INT) arg0
1517 <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1518 break;
1520 case SMAX:
1521 val = arg0s > arg1s ? arg0s : arg1s;
1522 break;
1524 case UMAX:
1525 val = ((unsigned HOST_WIDE_INT) arg0
1526 > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1527 break;
1529 default:
1530 abort ();
1533 val = trunc_int_for_mode (val, mode);
1535 return GEN_INT (val);
1538 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
1539 PLUS or MINUS.
1541 Rather than test for specific case, we do this by a brute-force method
1542 and do all possible simplifications until no more changes occur. Then
1543 we rebuild the operation. */
1545 static rtx
1546 simplify_plus_minus (code, mode, op0, op1)
1547 enum rtx_code code;
1548 enum machine_mode mode;
1549 rtx op0, op1;
1551 rtx ops[8];
1552 int negs[8];
1553 rtx result, tem;
1554 int n_ops = 2, input_ops = 2, input_consts = 0, n_consts = 0;
1555 int first = 1, negate = 0, changed;
1556 int i, j;
1558 memset ((char *) ops, 0, sizeof ops);
1560 /* Set up the two operands and then expand them until nothing has been
1561 changed. If we run out of room in our array, give up; this should
1562 almost never happen. */
1564 ops[0] = op0, ops[1] = op1, negs[0] = 0, negs[1] = (code == MINUS);
1566 changed = 1;
1567 while (changed)
1569 changed = 0;
1571 for (i = 0; i < n_ops; i++)
1572 switch (GET_CODE (ops[i]))
1574 case PLUS:
1575 case MINUS:
1576 if (n_ops == 7)
1577 return 0;
1579 ops[n_ops] = XEXP (ops[i], 1);
1580 negs[n_ops++] = GET_CODE (ops[i]) == MINUS ? !negs[i] : negs[i];
1581 ops[i] = XEXP (ops[i], 0);
1582 input_ops++;
1583 changed = 1;
1584 break;
1586 case NEG:
1587 ops[i] = XEXP (ops[i], 0);
1588 negs[i] = ! negs[i];
1589 changed = 1;
1590 break;
1592 case CONST:
1593 ops[i] = XEXP (ops[i], 0);
1594 input_consts++;
1595 changed = 1;
1596 break;
1598 case NOT:
1599 /* ~a -> (-a - 1) */
1600 if (n_ops != 7)
1602 ops[n_ops] = constm1_rtx;
1603 negs[n_ops++] = negs[i];
1604 ops[i] = XEXP (ops[i], 0);
1605 negs[i] = ! negs[i];
1606 changed = 1;
1608 break;
1610 case CONST_INT:
1611 if (negs[i])
1612 ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0, changed = 1;
1613 break;
1615 default:
1616 break;
1620 /* If we only have two operands, we can't do anything. */
1621 if (n_ops <= 2)
1622 return 0;
1624 /* Now simplify each pair of operands until nothing changes. The first
1625 time through just simplify constants against each other. */
1627 changed = 1;
1628 while (changed)
1630 changed = first;
1632 for (i = 0; i < n_ops - 1; i++)
1633 for (j = i + 1; j < n_ops; j++)
1634 if (ops[i] != 0 && ops[j] != 0
1635 && (! first || (CONSTANT_P (ops[i]) && CONSTANT_P (ops[j]))))
1637 rtx lhs = ops[i], rhs = ops[j];
1638 enum rtx_code ncode = PLUS;
1640 if (negs[i] && ! negs[j])
1641 lhs = ops[j], rhs = ops[i], ncode = MINUS;
1642 else if (! negs[i] && negs[j])
1643 ncode = MINUS;
1645 tem = simplify_binary_operation (ncode, mode, lhs, rhs);
1646 if (tem)
1648 ops[i] = tem, ops[j] = 0;
1649 negs[i] = negs[i] && negs[j];
1650 if (GET_CODE (tem) == NEG)
1651 ops[i] = XEXP (tem, 0), negs[i] = ! negs[i];
1653 if (GET_CODE (ops[i]) == CONST_INT && negs[i])
1654 ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0;
1655 changed = 1;
1659 first = 0;
1662 /* Pack all the operands to the lower-numbered entries and give up if
1663 we didn't reduce the number of operands we had. Make sure we
1664 count a CONST as two operands. If we have the same number of
1665 operands, but have made more CONSTs than we had, this is also
1666 an improvement, so accept it. */
1668 for (i = 0, j = 0; j < n_ops; j++)
1669 if (ops[j] != 0)
1671 ops[i] = ops[j], negs[i++] = negs[j];
1672 if (GET_CODE (ops[j]) == CONST)
1673 n_consts++;
1676 if (i + n_consts > input_ops
1677 || (i + n_consts == input_ops && n_consts <= input_consts))
1678 return 0;
1680 n_ops = i;
1682 /* If we have a CONST_INT, put it last. */
1683 for (i = 0; i < n_ops - 1; i++)
1684 if (GET_CODE (ops[i]) == CONST_INT)
1686 tem = ops[n_ops - 1], ops[n_ops - 1] = ops[i] , ops[i] = tem;
1687 j = negs[n_ops - 1], negs[n_ops - 1] = negs[i], negs[i] = j;
1690 /* Put a non-negated operand first. If there aren't any, make all
1691 operands positive and negate the whole thing later. */
1692 for (i = 0; i < n_ops && negs[i]; i++)
1695 if (i == n_ops)
1697 for (i = 0; i < n_ops; i++)
1698 negs[i] = 0;
1699 negate = 1;
1701 else if (i != 0)
1703 tem = ops[0], ops[0] = ops[i], ops[i] = tem;
1704 j = negs[0], negs[0] = negs[i], negs[i] = j;
1707 /* Now make the result by performing the requested operations. */
1708 result = ops[0];
1709 for (i = 1; i < n_ops; i++)
1710 result = simplify_gen_binary (negs[i] ? MINUS : PLUS, mode, result, ops[i]);
1712 return negate ? gen_rtx_NEG (mode, result) : result;
1715 struct cfc_args
1717 rtx op0, op1; /* Input */
1718 int equal, op0lt, op1lt; /* Output */
1719 int unordered;
1722 static void
1723 check_fold_consts (data)
1724 PTR data;
1726 struct cfc_args *args = (struct cfc_args *) data;
1727 REAL_VALUE_TYPE d0, d1;
1729 /* We may possibly raise an exception while reading the value. */
1730 args->unordered = 1;
1731 REAL_VALUE_FROM_CONST_DOUBLE (d0, args->op0);
1732 REAL_VALUE_FROM_CONST_DOUBLE (d1, args->op1);
1734 /* Comparisons of Inf versus Inf are ordered. */
1735 if (REAL_VALUE_ISNAN (d0)
1736 || REAL_VALUE_ISNAN (d1))
1737 return;
1738 args->equal = REAL_VALUES_EQUAL (d0, d1);
1739 args->op0lt = REAL_VALUES_LESS (d0, d1);
1740 args->op1lt = REAL_VALUES_LESS (d1, d0);
1741 args->unordered = 0;
1744 /* Like simplify_binary_operation except used for relational operators.
1745 MODE is the mode of the operands, not that of the result. If MODE
1746 is VOIDmode, both operands must also be VOIDmode and we compare the
1747 operands in "infinite precision".
1749 If no simplification is possible, this function returns zero. Otherwise,
1750 it returns either const_true_rtx or const0_rtx. */
1753 simplify_relational_operation (code, mode, op0, op1)
1754 enum rtx_code code;
1755 enum machine_mode mode;
1756 rtx op0, op1;
1758 int equal, op0lt, op0ltu, op1lt, op1ltu;
1759 rtx tem;
1761 if (mode == VOIDmode
1762 && (GET_MODE (op0) != VOIDmode
1763 || GET_MODE (op1) != VOIDmode))
1764 abort ();
1766 /* If op0 is a compare, extract the comparison arguments from it. */
1767 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
1768 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
1770 /* We can't simplify MODE_CC values since we don't know what the
1771 actual comparison is. */
1772 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC
1773 #ifdef HAVE_cc0
1774 || op0 == cc0_rtx
1775 #endif
1777 return 0;
1779 /* Make sure the constant is second. */
1780 if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
1781 || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
1783 tem = op0, op0 = op1, op1 = tem;
1784 code = swap_condition (code);
1787 /* For integer comparisons of A and B maybe we can simplify A - B and can
1788 then simplify a comparison of that with zero. If A and B are both either
1789 a register or a CONST_INT, this can't help; testing for these cases will
1790 prevent infinite recursion here and speed things up.
1792 If CODE is an unsigned comparison, then we can never do this optimization,
1793 because it gives an incorrect result if the subtraction wraps around zero.
1794 ANSI C defines unsigned operations such that they never overflow, and
1795 thus such cases can not be ignored. */
1797 if (INTEGRAL_MODE_P (mode) && op1 != const0_rtx
1798 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == CONST_INT)
1799 && (GET_CODE (op1) == REG || GET_CODE (op1) == CONST_INT))
1800 && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1))
1801 && code != GTU && code != GEU && code != LTU && code != LEU)
1802 return simplify_relational_operation (signed_condition (code),
1803 mode, tem, const0_rtx);
1805 if (flag_unsafe_math_optimizations && code == ORDERED)
1806 return const_true_rtx;
1808 if (flag_unsafe_math_optimizations && code == UNORDERED)
1809 return const0_rtx;
1811 /* For non-IEEE floating-point, if the two operands are equal, we know the
1812 result. */
1813 if (rtx_equal_p (op0, op1)
1814 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1815 || ! FLOAT_MODE_P (GET_MODE (op0))
1816 || flag_unsafe_math_optimizations))
1817 equal = 1, op0lt = 0, op0ltu = 0, op1lt = 0, op1ltu = 0;
1819 /* If the operands are floating-point constants, see if we can fold
1820 the result. */
1821 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1822 else if (GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
1823 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
1825 struct cfc_args args;
1827 /* Setup input for check_fold_consts() */
1828 args.op0 = op0;
1829 args.op1 = op1;
1832 if (!do_float_handler (check_fold_consts, (PTR) &args))
1833 args.unordered = 1;
1835 if (args.unordered)
1836 switch (code)
1838 case UNEQ:
1839 case UNLT:
1840 case UNGT:
1841 case UNLE:
1842 case UNGE:
1843 case NE:
1844 case UNORDERED:
1845 return const_true_rtx;
1846 case EQ:
1847 case LT:
1848 case GT:
1849 case LE:
1850 case GE:
1851 case LTGT:
1852 case ORDERED:
1853 return const0_rtx;
1854 default:
1855 return 0;
1858 /* Receive output from check_fold_consts() */
1859 equal = args.equal;
1860 op0lt = op0ltu = args.op0lt;
1861 op1lt = op1ltu = args.op1lt;
1863 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1865 /* Otherwise, see if the operands are both integers. */
1866 else if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
1867 && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
1868 && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
1870 int width = GET_MODE_BITSIZE (mode);
1871 HOST_WIDE_INT l0s, h0s, l1s, h1s;
1872 unsigned HOST_WIDE_INT l0u, h0u, l1u, h1u;
1874 /* Get the two words comprising each integer constant. */
1875 if (GET_CODE (op0) == CONST_DOUBLE)
1877 l0u = l0s = CONST_DOUBLE_LOW (op0);
1878 h0u = h0s = CONST_DOUBLE_HIGH (op0);
1880 else
1882 l0u = l0s = INTVAL (op0);
1883 h0u = h0s = HWI_SIGN_EXTEND (l0s);
1886 if (GET_CODE (op1) == CONST_DOUBLE)
1888 l1u = l1s = CONST_DOUBLE_LOW (op1);
1889 h1u = h1s = CONST_DOUBLE_HIGH (op1);
1891 else
1893 l1u = l1s = INTVAL (op1);
1894 h1u = h1s = HWI_SIGN_EXTEND (l1s);
1897 /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
1898 we have to sign or zero-extend the values. */
1899 if (width != 0 && width < HOST_BITS_PER_WIDE_INT)
1901 l0u &= ((HOST_WIDE_INT) 1 << width) - 1;
1902 l1u &= ((HOST_WIDE_INT) 1 << width) - 1;
1904 if (l0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1905 l0s |= ((HOST_WIDE_INT) (-1) << width);
1907 if (l1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1908 l1s |= ((HOST_WIDE_INT) (-1) << width);
1910 if (width != 0 && width <= HOST_BITS_PER_WIDE_INT)
1911 h0u = h1u = 0, h0s = HWI_SIGN_EXTEND (l0s), h1s = HWI_SIGN_EXTEND (l1s);
1913 equal = (h0u == h1u && l0u == l1u);
1914 op0lt = (h0s < h1s || (h0s == h1s && l0u < l1u));
1915 op1lt = (h1s < h0s || (h1s == h0s && l1u < l0u));
1916 op0ltu = (h0u < h1u || (h0u == h1u && l0u < l1u));
1917 op1ltu = (h1u < h0u || (h1u == h0u && l1u < l0u));
1920 /* Otherwise, there are some code-specific tests we can make. */
1921 else
1923 switch (code)
1925 case EQ:
1926 /* References to the frame plus a constant or labels cannot
1927 be zero, but a SYMBOL_REF can due to #pragma weak. */
1928 if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1929 || GET_CODE (op0) == LABEL_REF)
1930 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1931 /* On some machines, the ap reg can be 0 sometimes. */
1932 && op0 != arg_pointer_rtx
1933 #endif
1935 return const0_rtx;
1936 break;
1938 case NE:
1939 if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1940 || GET_CODE (op0) == LABEL_REF)
1941 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1942 && op0 != arg_pointer_rtx
1943 #endif
1945 return const_true_rtx;
1946 break;
1948 case GEU:
1949 /* Unsigned values are never negative. */
1950 if (op1 == const0_rtx)
1951 return const_true_rtx;
1952 break;
1954 case LTU:
1955 if (op1 == const0_rtx)
1956 return const0_rtx;
1957 break;
1959 case LEU:
1960 /* Unsigned values are never greater than the largest
1961 unsigned value. */
1962 if (GET_CODE (op1) == CONST_INT
1963 && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1964 && INTEGRAL_MODE_P (mode))
1965 return const_true_rtx;
1966 break;
1968 case GTU:
1969 if (GET_CODE (op1) == CONST_INT
1970 && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1971 && INTEGRAL_MODE_P (mode))
1972 return const0_rtx;
1973 break;
1975 default:
1976 break;
1979 return 0;
1982 /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
1983 as appropriate. */
1984 switch (code)
1986 case EQ:
1987 case UNEQ:
1988 return equal ? const_true_rtx : const0_rtx;
1989 case NE:
1990 case LTGT:
1991 return ! equal ? const_true_rtx : const0_rtx;
1992 case LT:
1993 case UNLT:
1994 return op0lt ? const_true_rtx : const0_rtx;
1995 case GT:
1996 case UNGT:
1997 return op1lt ? const_true_rtx : const0_rtx;
1998 case LTU:
1999 return op0ltu ? const_true_rtx : const0_rtx;
2000 case GTU:
2001 return op1ltu ? const_true_rtx : const0_rtx;
2002 case LE:
2003 case UNLE:
2004 return equal || op0lt ? const_true_rtx : const0_rtx;
2005 case GE:
2006 case UNGE:
2007 return equal || op1lt ? const_true_rtx : const0_rtx;
2008 case LEU:
2009 return equal || op0ltu ? const_true_rtx : const0_rtx;
2010 case GEU:
2011 return equal || op1ltu ? const_true_rtx : const0_rtx;
2012 case ORDERED:
2013 return const_true_rtx;
2014 case UNORDERED:
2015 return const0_rtx;
2016 default:
2017 abort ();
2021 /* Simplify CODE, an operation with result mode MODE and three operands,
2022 OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
2023 a constant. Return 0 if no simplifications is possible. */
2026 simplify_ternary_operation (code, mode, op0_mode, op0, op1, op2)
2027 enum rtx_code code;
2028 enum machine_mode mode, op0_mode;
2029 rtx op0, op1, op2;
2031 unsigned int width = GET_MODE_BITSIZE (mode);
2033 /* VOIDmode means "infinite" precision. */
2034 if (width == 0)
2035 width = HOST_BITS_PER_WIDE_INT;
2037 switch (code)
2039 case SIGN_EXTRACT:
2040 case ZERO_EXTRACT:
2041 if (GET_CODE (op0) == CONST_INT
2042 && GET_CODE (op1) == CONST_INT
2043 && GET_CODE (op2) == CONST_INT
2044 && ((unsigned) INTVAL (op1) + (unsigned) INTVAL (op2) <= width)
2045 && width <= (unsigned) HOST_BITS_PER_WIDE_INT)
2047 /* Extracting a bit-field from a constant */
2048 HOST_WIDE_INT val = INTVAL (op0);
2050 if (BITS_BIG_ENDIAN)
2051 val >>= (GET_MODE_BITSIZE (op0_mode)
2052 - INTVAL (op2) - INTVAL (op1));
2053 else
2054 val >>= INTVAL (op2);
2056 if (HOST_BITS_PER_WIDE_INT != INTVAL (op1))
2058 /* First zero-extend. */
2059 val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1;
2060 /* If desired, propagate sign bit. */
2061 if (code == SIGN_EXTRACT
2062 && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1))))
2063 val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1);
2066 /* Clear the bits that don't belong in our mode,
2067 unless they and our sign bit are all one.
2068 So we get either a reasonable negative value or a reasonable
2069 unsigned value for this mode. */
2070 if (width < HOST_BITS_PER_WIDE_INT
2071 && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
2072 != ((HOST_WIDE_INT) (-1) << (width - 1))))
2073 val &= ((HOST_WIDE_INT) 1 << width) - 1;
2075 return GEN_INT (val);
2077 break;
2079 case IF_THEN_ELSE:
2080 if (GET_CODE (op0) == CONST_INT)
2081 return op0 != const0_rtx ? op1 : op2;
2083 /* Convert a == b ? b : a to "a". */
2084 if (GET_CODE (op0) == NE && ! side_effects_p (op0)
2085 && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
2086 && rtx_equal_p (XEXP (op0, 0), op1)
2087 && rtx_equal_p (XEXP (op0, 1), op2))
2088 return op1;
2089 else if (GET_CODE (op0) == EQ && ! side_effects_p (op0)
2090 && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
2091 && rtx_equal_p (XEXP (op0, 1), op1)
2092 && rtx_equal_p (XEXP (op0, 0), op2))
2093 return op2;
2094 else if (GET_RTX_CLASS (GET_CODE (op0)) == '<' && ! side_effects_p (op0))
2096 enum machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
2097 ? GET_MODE (XEXP (op0, 1))
2098 : GET_MODE (XEXP (op0, 0)));
2099 rtx temp;
2100 if (cmp_mode == VOIDmode)
2101 cmp_mode = op0_mode;
2102 temp = simplify_relational_operation (GET_CODE (op0), cmp_mode,
2103 XEXP (op0, 0), XEXP (op0, 1));
2105 /* See if any simplifications were possible. */
2106 if (temp == const0_rtx)
2107 return op2;
2108 else if (temp == const1_rtx)
2109 return op1;
2110 else if (temp)
2111 op0 = temp;
2113 /* Look for happy constants in op1 and op2. */
2114 if (GET_CODE (op1) == CONST_INT && GET_CODE (op2) == CONST_INT)
2116 HOST_WIDE_INT t = INTVAL (op1);
2117 HOST_WIDE_INT f = INTVAL (op2);
2119 if (t == STORE_FLAG_VALUE && f == 0)
2120 code = GET_CODE (op0);
2121 else if (t == 0 && f == STORE_FLAG_VALUE)
2123 enum rtx_code tmp;
2124 tmp = reversed_comparison_code (op0, NULL_RTX);
2125 if (tmp == UNKNOWN)
2126 break;
2127 code = tmp;
2129 else
2130 break;
2132 return gen_rtx_fmt_ee (code, mode, XEXP (op0, 0), XEXP (op0, 1));
2135 break;
2137 default:
2138 abort ();
2141 return 0;
2144 /* Simplify X, an rtx expression.
2146 Return the simplified expression or NULL if no simplifications
2147 were possible.
2149 This is the preferred entry point into the simplification routines;
2150 however, we still allow passes to call the more specific routines.
2152 Right now GCC has three (yes, three) major bodies of RTL simplficiation
2153 code that need to be unified.
2155 1. fold_rtx in cse.c. This code uses various CSE specific
2156 information to aid in RTL simplification.
2158 2. simplify_rtx in combine.c. Similar to fold_rtx, except that
2159 it uses combine specific information to aid in RTL
2160 simplification.
2162 3. The routines in this file.
2165 Long term we want to only have one body of simplification code; to
2166 get to that state I recommend the following steps:
2168 1. Pour over fold_rtx & simplify_rtx and move any simplifications
2169 which are not pass dependent state into these routines.
2171 2. As code is moved by #1, change fold_rtx & simplify_rtx to
2172 use this routine whenever possible.
2174 3. Allow for pass dependent state to be provided to these
2175 routines and add simplifications based on the pass dependent
2176 state. Remove code from cse.c & combine.c that becomes
2177 redundant/dead.
2179 It will take time, but ultimately the compiler will be easier to
2180 maintain and improve. It's totally silly that when we add a
2181 simplification that it needs to be added to 4 places (3 for RTL
2182 simplification and 1 for tree simplification. */
2185 simplify_rtx (x)
2186 rtx x;
2188 enum rtx_code code = GET_CODE (x);
2189 enum machine_mode mode = GET_MODE (x);
2191 switch (GET_RTX_CLASS (code))
2193 case '1':
2194 return simplify_unary_operation (code, mode,
2195 XEXP (x, 0), GET_MODE (XEXP (x, 0)));
2196 case '2':
2197 case 'c':
2198 return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
2200 case '3':
2201 case 'b':
2202 return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
2203 XEXP (x, 0), XEXP (x, 1),
2204 XEXP (x, 2));
2206 case '<':
2207 return simplify_relational_operation (code,
2208 ((GET_MODE (XEXP (x, 0))
2209 != VOIDmode)
2210 ? GET_MODE (XEXP (x, 0))
2211 : GET_MODE (XEXP (x, 1))),
2212 XEXP (x, 0), XEXP (x, 1));
2213 default:
2214 return NULL;