1 /* RTL simplification functions for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
29 #include "fold-const.h"
34 #include "insn-config.h"
36 #include "insn-codes.h"
45 #include "diagnostic-core.h"
48 /* Simplification and canonicalization of RTL. */
50 /* Much code operates on (low, high) pairs; the low value is an
51 unsigned wide int, the high value a signed wide int. We
52 occasionally need to sign extend from low to high as if low were a
54 #define HWI_SIGN_EXTEND(low) \
55 ((((HOST_WIDE_INT) low) < 0) ? ((HOST_WIDE_INT) -1) : ((HOST_WIDE_INT) 0))
57 static rtx
neg_const_int (machine_mode
, const_rtx
);
58 static bool plus_minus_operand_p (const_rtx
);
59 static rtx
simplify_plus_minus (enum rtx_code
, machine_mode
, rtx
, rtx
);
60 static rtx
simplify_immed_subreg (machine_mode
, rtx
, machine_mode
,
62 static rtx
simplify_associative_operation (enum rtx_code
, machine_mode
,
64 static rtx
simplify_relational_operation_1 (enum rtx_code
, machine_mode
,
65 machine_mode
, rtx
, rtx
);
66 static rtx
simplify_unary_operation_1 (enum rtx_code
, machine_mode
, rtx
);
67 static rtx
simplify_binary_operation_1 (enum rtx_code
, machine_mode
,
70 /* Negate a CONST_INT rtx, truncating (because a conversion from a
71 maximally negative number can overflow). */
73 neg_const_int (machine_mode mode
, const_rtx i
)
75 return gen_int_mode (-(unsigned HOST_WIDE_INT
) INTVAL (i
), mode
);
78 /* Test whether expression, X, is an immediate constant that represents
79 the most significant bit of machine mode MODE. */
82 mode_signbit_p (machine_mode mode
, const_rtx x
)
84 unsigned HOST_WIDE_INT val
;
87 if (GET_MODE_CLASS (mode
) != MODE_INT
)
90 width
= GET_MODE_PRECISION (mode
);
94 if (width
<= HOST_BITS_PER_WIDE_INT
97 #if TARGET_SUPPORTS_WIDE_INT
98 else if (CONST_WIDE_INT_P (x
))
101 unsigned int elts
= CONST_WIDE_INT_NUNITS (x
);
102 if (elts
!= (width
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
)
104 for (i
= 0; i
< elts
- 1; i
++)
105 if (CONST_WIDE_INT_ELT (x
, i
) != 0)
107 val
= CONST_WIDE_INT_ELT (x
, elts
- 1);
108 width
%= HOST_BITS_PER_WIDE_INT
;
110 width
= HOST_BITS_PER_WIDE_INT
;
113 else if (width
<= HOST_BITS_PER_DOUBLE_INT
114 && CONST_DOUBLE_AS_INT_P (x
)
115 && CONST_DOUBLE_LOW (x
) == 0)
117 val
= CONST_DOUBLE_HIGH (x
);
118 width
-= HOST_BITS_PER_WIDE_INT
;
122 /* X is not an integer constant. */
125 if (width
< HOST_BITS_PER_WIDE_INT
)
126 val
&= ((unsigned HOST_WIDE_INT
) 1 << width
) - 1;
127 return val
== ((unsigned HOST_WIDE_INT
) 1 << (width
- 1));
130 /* Test whether VAL is equal to the most significant bit of mode MODE
131 (after masking with the mode mask of MODE). Returns false if the
132 precision of MODE is too large to handle. */
135 val_signbit_p (machine_mode mode
, unsigned HOST_WIDE_INT val
)
139 if (GET_MODE_CLASS (mode
) != MODE_INT
)
142 width
= GET_MODE_PRECISION (mode
);
143 if (width
== 0 || width
> HOST_BITS_PER_WIDE_INT
)
146 val
&= GET_MODE_MASK (mode
);
147 return val
== ((unsigned HOST_WIDE_INT
) 1 << (width
- 1));
150 /* Test whether the most significant bit of mode MODE is set in VAL.
151 Returns false if the precision of MODE is too large to handle. */
153 val_signbit_known_set_p (machine_mode mode
, unsigned HOST_WIDE_INT val
)
157 if (GET_MODE_CLASS (mode
) != MODE_INT
)
160 width
= GET_MODE_PRECISION (mode
);
161 if (width
== 0 || width
> HOST_BITS_PER_WIDE_INT
)
164 val
&= (unsigned HOST_WIDE_INT
) 1 << (width
- 1);
168 /* Test whether the most significant bit of mode MODE is clear in VAL.
169 Returns false if the precision of MODE is too large to handle. */
171 val_signbit_known_clear_p (machine_mode mode
, unsigned HOST_WIDE_INT val
)
175 if (GET_MODE_CLASS (mode
) != MODE_INT
)
178 width
= GET_MODE_PRECISION (mode
);
179 if (width
== 0 || width
> HOST_BITS_PER_WIDE_INT
)
182 val
&= (unsigned HOST_WIDE_INT
) 1 << (width
- 1);
186 /* Make a binary operation by properly ordering the operands and
187 seeing if the expression folds. */
190 simplify_gen_binary (enum rtx_code code
, machine_mode mode
, rtx op0
,
195 /* If this simplifies, do it. */
196 tem
= simplify_binary_operation (code
, mode
, op0
, op1
);
200 /* Put complex operands first and constants second if commutative. */
201 if (GET_RTX_CLASS (code
) == RTX_COMM_ARITH
202 && swap_commutative_operands_p (op0
, op1
))
203 std::swap (op0
, op1
);
205 return gen_rtx_fmt_ee (code
, mode
, op0
, op1
);
208 /* If X is a MEM referencing the constant pool, return the real value.
209 Otherwise return X. */
211 avoid_constant_pool_reference (rtx x
)
215 HOST_WIDE_INT offset
= 0;
217 switch (GET_CODE (x
))
223 /* Handle float extensions of constant pool references. */
225 c
= avoid_constant_pool_reference (tmp
);
226 if (c
!= tmp
&& CONST_DOUBLE_AS_FLOAT_P (c
))
230 REAL_VALUE_FROM_CONST_DOUBLE (d
, c
);
231 return CONST_DOUBLE_FROM_REAL_VALUE (d
, GET_MODE (x
));
239 if (GET_MODE (x
) == BLKmode
)
244 /* Call target hook to avoid the effects of -fpic etc.... */
245 addr
= targetm
.delegitimize_address (addr
);
247 /* Split the address into a base and integer offset. */
248 if (GET_CODE (addr
) == CONST
249 && GET_CODE (XEXP (addr
, 0)) == PLUS
250 && CONST_INT_P (XEXP (XEXP (addr
, 0), 1)))
252 offset
= INTVAL (XEXP (XEXP (addr
, 0), 1));
253 addr
= XEXP (XEXP (addr
, 0), 0);
256 if (GET_CODE (addr
) == LO_SUM
)
257 addr
= XEXP (addr
, 1);
259 /* If this is a constant pool reference, we can turn it into its
260 constant and hope that simplifications happen. */
261 if (GET_CODE (addr
) == SYMBOL_REF
262 && CONSTANT_POOL_ADDRESS_P (addr
))
264 c
= get_pool_constant (addr
);
265 cmode
= get_pool_mode (addr
);
267 /* If we're accessing the constant in a different mode than it was
268 originally stored, attempt to fix that up via subreg simplifications.
269 If that fails we have no choice but to return the original memory. */
270 if ((offset
!= 0 || cmode
!= GET_MODE (x
))
271 && offset
>= 0 && offset
< GET_MODE_SIZE (cmode
))
273 rtx tem
= simplify_subreg (GET_MODE (x
), c
, cmode
, offset
);
274 if (tem
&& CONSTANT_P (tem
))
284 /* Simplify a MEM based on its attributes. This is the default
285 delegitimize_address target hook, and it's recommended that every
286 overrider call it. */
289 delegitimize_mem_from_attrs (rtx x
)
291 /* MEMs without MEM_OFFSETs may have been offset, so we can't just
292 use their base addresses as equivalent. */
295 && MEM_OFFSET_KNOWN_P (x
))
297 tree decl
= MEM_EXPR (x
);
298 machine_mode mode
= GET_MODE (x
);
299 HOST_WIDE_INT offset
= 0;
301 switch (TREE_CODE (decl
))
311 case ARRAY_RANGE_REF
:
316 case VIEW_CONVERT_EXPR
:
318 HOST_WIDE_INT bitsize
, bitpos
;
320 int unsignedp
, volatilep
= 0;
322 decl
= get_inner_reference (decl
, &bitsize
, &bitpos
, &toffset
,
323 &mode
, &unsignedp
, &volatilep
, false);
324 if (bitsize
!= GET_MODE_BITSIZE (mode
)
325 || (bitpos
% BITS_PER_UNIT
)
326 || (toffset
&& !tree_fits_shwi_p (toffset
)))
330 offset
+= bitpos
/ BITS_PER_UNIT
;
332 offset
+= tree_to_shwi (toffset
);
339 && mode
== GET_MODE (x
)
340 && TREE_CODE (decl
) == VAR_DECL
341 && (TREE_STATIC (decl
)
342 || DECL_THREAD_LOCAL_P (decl
))
343 && DECL_RTL_SET_P (decl
)
344 && MEM_P (DECL_RTL (decl
)))
348 offset
+= MEM_OFFSET (x
);
350 newx
= DECL_RTL (decl
);
354 rtx n
= XEXP (newx
, 0), o
= XEXP (x
, 0);
356 /* Avoid creating a new MEM needlessly if we already had
357 the same address. We do if there's no OFFSET and the
358 old address X is identical to NEWX, or if X is of the
359 form (plus NEWX OFFSET), or the NEWX is of the form
360 (plus Y (const_int Z)) and X is that with the offset
361 added: (plus Y (const_int Z+OFFSET)). */
363 || (GET_CODE (o
) == PLUS
364 && GET_CODE (XEXP (o
, 1)) == CONST_INT
365 && (offset
== INTVAL (XEXP (o
, 1))
366 || (GET_CODE (n
) == PLUS
367 && GET_CODE (XEXP (n
, 1)) == CONST_INT
368 && (INTVAL (XEXP (n
, 1)) + offset
369 == INTVAL (XEXP (o
, 1)))
370 && (n
= XEXP (n
, 0))))
371 && (o
= XEXP (o
, 0))))
372 && rtx_equal_p (o
, n
)))
373 x
= adjust_address_nv (newx
, mode
, offset
);
375 else if (GET_MODE (x
) == GET_MODE (newx
)
384 /* Make a unary operation by first seeing if it folds and otherwise making
385 the specified operation. */
388 simplify_gen_unary (enum rtx_code code
, machine_mode mode
, rtx op
,
389 machine_mode op_mode
)
393 /* If this simplifies, use it. */
394 if ((tem
= simplify_unary_operation (code
, mode
, op
, op_mode
)) != 0)
397 return gen_rtx_fmt_e (code
, mode
, op
);
400 /* Likewise for ternary operations. */
403 simplify_gen_ternary (enum rtx_code code
, machine_mode mode
,
404 machine_mode op0_mode
, rtx op0
, rtx op1
, rtx op2
)
408 /* If this simplifies, use it. */
409 if (0 != (tem
= simplify_ternary_operation (code
, mode
, op0_mode
,
413 return gen_rtx_fmt_eee (code
, mode
, op0
, op1
, op2
);
416 /* Likewise, for relational operations.
417 CMP_MODE specifies mode comparison is done in. */
420 simplify_gen_relational (enum rtx_code code
, machine_mode mode
,
421 machine_mode cmp_mode
, rtx op0
, rtx op1
)
425 if (0 != (tem
= simplify_relational_operation (code
, mode
, cmp_mode
,
429 return gen_rtx_fmt_ee (code
, mode
, op0
, op1
);
432 /* If FN is NULL, replace all occurrences of OLD_RTX in X with copy_rtx (DATA)
433 and simplify the result. If FN is non-NULL, call this callback on each
434 X, if it returns non-NULL, replace X with its return value and simplify the
438 simplify_replace_fn_rtx (rtx x
, const_rtx old_rtx
,
439 rtx (*fn
) (rtx
, const_rtx
, void *), void *data
)
441 enum rtx_code code
= GET_CODE (x
);
442 machine_mode mode
= GET_MODE (x
);
443 machine_mode op_mode
;
445 rtx op0
, op1
, op2
, newx
, op
;
449 if (__builtin_expect (fn
!= NULL
, 0))
451 newx
= fn (x
, old_rtx
, data
);
455 else if (rtx_equal_p (x
, old_rtx
))
456 return copy_rtx ((rtx
) data
);
458 switch (GET_RTX_CLASS (code
))
462 op_mode
= GET_MODE (op0
);
463 op0
= simplify_replace_fn_rtx (op0
, old_rtx
, fn
, data
);
464 if (op0
== XEXP (x
, 0))
466 return simplify_gen_unary (code
, mode
, op0
, op_mode
);
470 op0
= simplify_replace_fn_rtx (XEXP (x
, 0), old_rtx
, fn
, data
);
471 op1
= simplify_replace_fn_rtx (XEXP (x
, 1), old_rtx
, fn
, data
);
472 if (op0
== XEXP (x
, 0) && op1
== XEXP (x
, 1))
474 return simplify_gen_binary (code
, mode
, op0
, op1
);
477 case RTX_COMM_COMPARE
:
480 op_mode
= GET_MODE (op0
) != VOIDmode
? GET_MODE (op0
) : GET_MODE (op1
);
481 op0
= simplify_replace_fn_rtx (op0
, old_rtx
, fn
, data
);
482 op1
= simplify_replace_fn_rtx (op1
, old_rtx
, fn
, data
);
483 if (op0
== XEXP (x
, 0) && op1
== XEXP (x
, 1))
485 return simplify_gen_relational (code
, mode
, op_mode
, op0
, op1
);
488 case RTX_BITFIELD_OPS
:
490 op_mode
= GET_MODE (op0
);
491 op0
= simplify_replace_fn_rtx (op0
, old_rtx
, fn
, data
);
492 op1
= simplify_replace_fn_rtx (XEXP (x
, 1), old_rtx
, fn
, data
);
493 op2
= simplify_replace_fn_rtx (XEXP (x
, 2), old_rtx
, fn
, data
);
494 if (op0
== XEXP (x
, 0) && op1
== XEXP (x
, 1) && op2
== XEXP (x
, 2))
496 if (op_mode
== VOIDmode
)
497 op_mode
= GET_MODE (op0
);
498 return simplify_gen_ternary (code
, mode
, op_mode
, op0
, op1
, op2
);
503 op0
= simplify_replace_fn_rtx (SUBREG_REG (x
), old_rtx
, fn
, data
);
504 if (op0
== SUBREG_REG (x
))
506 op0
= simplify_gen_subreg (GET_MODE (x
), op0
,
507 GET_MODE (SUBREG_REG (x
)),
509 return op0
? op0
: x
;
516 op0
= simplify_replace_fn_rtx (XEXP (x
, 0), old_rtx
, fn
, data
);
517 if (op0
== XEXP (x
, 0))
519 return replace_equiv_address_nv (x
, op0
);
521 else if (code
== LO_SUM
)
523 op0
= simplify_replace_fn_rtx (XEXP (x
, 0), old_rtx
, fn
, data
);
524 op1
= simplify_replace_fn_rtx (XEXP (x
, 1), old_rtx
, fn
, data
);
526 /* (lo_sum (high x) y) -> y where x and y have the same base. */
527 if (GET_CODE (op0
) == HIGH
)
529 rtx base0
, base1
, offset0
, offset1
;
530 split_const (XEXP (op0
, 0), &base0
, &offset0
);
531 split_const (op1
, &base1
, &offset1
);
532 if (rtx_equal_p (base0
, base1
))
536 if (op0
== XEXP (x
, 0) && op1
== XEXP (x
, 1))
538 return gen_rtx_LO_SUM (mode
, op0
, op1
);
547 fmt
= GET_RTX_FORMAT (code
);
548 for (i
= 0; fmt
[i
]; i
++)
553 newvec
= XVEC (newx
, i
);
554 for (j
= 0; j
< GET_NUM_ELEM (vec
); j
++)
556 op
= simplify_replace_fn_rtx (RTVEC_ELT (vec
, j
),
558 if (op
!= RTVEC_ELT (vec
, j
))
562 newvec
= shallow_copy_rtvec (vec
);
564 newx
= shallow_copy_rtx (x
);
565 XVEC (newx
, i
) = newvec
;
567 RTVEC_ELT (newvec
, j
) = op
;
575 op
= simplify_replace_fn_rtx (XEXP (x
, i
), old_rtx
, fn
, data
);
576 if (op
!= XEXP (x
, i
))
579 newx
= shallow_copy_rtx (x
);
588 /* Replace all occurrences of OLD_RTX in X with NEW_RTX and try to simplify the
589 resulting RTX. Return a new RTX which is as simplified as possible. */
592 simplify_replace_rtx (rtx x
, const_rtx old_rtx
, rtx new_rtx
)
594 return simplify_replace_fn_rtx (x
, old_rtx
, 0, new_rtx
);
597 /* Try to simplify a MODE truncation of OP, which has OP_MODE.
598 Only handle cases where the truncated value is inherently an rvalue.
600 RTL provides two ways of truncating a value:
602 1. a lowpart subreg. This form is only a truncation when both
603 the outer and inner modes (here MODE and OP_MODE respectively)
604 are scalar integers, and only then when the subreg is used as
607 It is only valid to form such truncating subregs if the
608 truncation requires no action by the target. The onus for
609 proving this is on the creator of the subreg -- e.g. the
610 caller to simplify_subreg or simplify_gen_subreg -- and typically
611 involves either TRULY_NOOP_TRUNCATION_MODES_P or truncated_to_mode.
613 2. a TRUNCATE. This form handles both scalar and compound integers.
615 The first form is preferred where valid. However, the TRUNCATE
616 handling in simplify_unary_operation turns the second form into the
617 first form when TRULY_NOOP_TRUNCATION_MODES_P or truncated_to_mode allow,
618 so it is generally safe to form rvalue truncations using:
620 simplify_gen_unary (TRUNCATE, ...)
622 and leave simplify_unary_operation to work out which representation
625 Because of the proof requirements on (1), simplify_truncation must
626 also use simplify_gen_unary (TRUNCATE, ...) to truncate parts of OP,
627 regardless of whether the outer truncation came from a SUBREG or a
628 TRUNCATE. For example, if the caller has proven that an SImode
633 is a no-op and can be represented as a subreg, it does not follow
634 that SImode truncations of X and Y are also no-ops. On a target
635 like 64-bit MIPS that requires SImode values to be stored in
636 sign-extended form, an SImode truncation of:
638 (and:DI (reg:DI X) (const_int 63))
640 is trivially a no-op because only the lower 6 bits can be set.
641 However, X is still an arbitrary 64-bit number and so we cannot
642 assume that truncating it too is a no-op. */
645 simplify_truncation (machine_mode mode
, rtx op
,
646 machine_mode op_mode
)
648 unsigned int precision
= GET_MODE_UNIT_PRECISION (mode
);
649 unsigned int op_precision
= GET_MODE_UNIT_PRECISION (op_mode
);
650 gcc_assert (precision
<= op_precision
);
652 /* Optimize truncations of zero and sign extended values. */
653 if (GET_CODE (op
) == ZERO_EXTEND
654 || GET_CODE (op
) == SIGN_EXTEND
)
656 /* There are three possibilities. If MODE is the same as the
657 origmode, we can omit both the extension and the subreg.
658 If MODE is not larger than the origmode, we can apply the
659 truncation without the extension. Finally, if the outermode
660 is larger than the origmode, we can just extend to the appropriate
662 machine_mode origmode
= GET_MODE (XEXP (op
, 0));
663 if (mode
== origmode
)
665 else if (precision
<= GET_MODE_UNIT_PRECISION (origmode
))
666 return simplify_gen_unary (TRUNCATE
, mode
,
667 XEXP (op
, 0), origmode
);
669 return simplify_gen_unary (GET_CODE (op
), mode
,
670 XEXP (op
, 0), origmode
);
673 /* If the machine can perform operations in the truncated mode, distribute
674 the truncation, i.e. simplify (truncate:QI (op:SI (x:SI) (y:SI))) into
675 (op:QI (truncate:QI (x:SI)) (truncate:QI (y:SI))). */
677 && (!WORD_REGISTER_OPERATIONS
|| precision
>= BITS_PER_WORD
)
678 && (GET_CODE (op
) == PLUS
679 || GET_CODE (op
) == MINUS
680 || GET_CODE (op
) == MULT
))
682 rtx op0
= simplify_gen_unary (TRUNCATE
, mode
, XEXP (op
, 0), op_mode
);
685 rtx op1
= simplify_gen_unary (TRUNCATE
, mode
, XEXP (op
, 1), op_mode
);
687 return simplify_gen_binary (GET_CODE (op
), mode
, op0
, op1
);
691 /* Simplify (truncate:QI (lshiftrt:SI (sign_extend:SI (x:QI)) C)) into
692 to (ashiftrt:QI (x:QI) C), where C is a suitable small constant and
693 the outer subreg is effectively a truncation to the original mode. */
694 if ((GET_CODE (op
) == LSHIFTRT
695 || GET_CODE (op
) == ASHIFTRT
)
696 /* Ensure that OP_MODE is at least twice as wide as MODE
697 to avoid the possibility that an outer LSHIFTRT shifts by more
698 than the sign extension's sign_bit_copies and introduces zeros
699 into the high bits of the result. */
700 && 2 * precision
<= op_precision
701 && CONST_INT_P (XEXP (op
, 1))
702 && GET_CODE (XEXP (op
, 0)) == SIGN_EXTEND
703 && GET_MODE (XEXP (XEXP (op
, 0), 0)) == mode
704 && UINTVAL (XEXP (op
, 1)) < precision
)
705 return simplify_gen_binary (ASHIFTRT
, mode
,
706 XEXP (XEXP (op
, 0), 0), XEXP (op
, 1));
708 /* Likewise (truncate:QI (lshiftrt:SI (zero_extend:SI (x:QI)) C)) into
709 to (lshiftrt:QI (x:QI) C), where C is a suitable small constant and
710 the outer subreg is effectively a truncation to the original mode. */
711 if ((GET_CODE (op
) == LSHIFTRT
712 || GET_CODE (op
) == ASHIFTRT
)
713 && CONST_INT_P (XEXP (op
, 1))
714 && GET_CODE (XEXP (op
, 0)) == ZERO_EXTEND
715 && GET_MODE (XEXP (XEXP (op
, 0), 0)) == mode
716 && UINTVAL (XEXP (op
, 1)) < precision
)
717 return simplify_gen_binary (LSHIFTRT
, mode
,
718 XEXP (XEXP (op
, 0), 0), XEXP (op
, 1));
720 /* Likewise (truncate:QI (ashift:SI (zero_extend:SI (x:QI)) C)) into
721 to (ashift:QI (x:QI) C), where C is a suitable small constant and
722 the outer subreg is effectively a truncation to the original mode. */
723 if (GET_CODE (op
) == ASHIFT
724 && CONST_INT_P (XEXP (op
, 1))
725 && (GET_CODE (XEXP (op
, 0)) == ZERO_EXTEND
726 || GET_CODE (XEXP (op
, 0)) == SIGN_EXTEND
)
727 && GET_MODE (XEXP (XEXP (op
, 0), 0)) == mode
728 && UINTVAL (XEXP (op
, 1)) < precision
)
729 return simplify_gen_binary (ASHIFT
, mode
,
730 XEXP (XEXP (op
, 0), 0), XEXP (op
, 1));
732 /* Recognize a word extraction from a multi-word subreg. */
733 if ((GET_CODE (op
) == LSHIFTRT
734 || GET_CODE (op
) == ASHIFTRT
)
735 && SCALAR_INT_MODE_P (mode
)
736 && SCALAR_INT_MODE_P (op_mode
)
737 && precision
>= BITS_PER_WORD
738 && 2 * precision
<= op_precision
739 && CONST_INT_P (XEXP (op
, 1))
740 && (INTVAL (XEXP (op
, 1)) & (precision
- 1)) == 0
741 && UINTVAL (XEXP (op
, 1)) < op_precision
)
743 int byte
= subreg_lowpart_offset (mode
, op_mode
);
744 int shifted_bytes
= INTVAL (XEXP (op
, 1)) / BITS_PER_UNIT
;
745 return simplify_gen_subreg (mode
, XEXP (op
, 0), op_mode
,
747 ? byte
- shifted_bytes
748 : byte
+ shifted_bytes
));
751 /* If we have a TRUNCATE of a right shift of MEM, make a new MEM
752 and try replacing the TRUNCATE and shift with it. Don't do this
753 if the MEM has a mode-dependent address. */
754 if ((GET_CODE (op
) == LSHIFTRT
755 || GET_CODE (op
) == ASHIFTRT
)
756 && SCALAR_INT_MODE_P (op_mode
)
757 && MEM_P (XEXP (op
, 0))
758 && CONST_INT_P (XEXP (op
, 1))
759 && (INTVAL (XEXP (op
, 1)) % GET_MODE_BITSIZE (mode
)) == 0
760 && INTVAL (XEXP (op
, 1)) > 0
761 && INTVAL (XEXP (op
, 1)) < GET_MODE_BITSIZE (op_mode
)
762 && ! mode_dependent_address_p (XEXP (XEXP (op
, 0), 0),
763 MEM_ADDR_SPACE (XEXP (op
, 0)))
764 && ! MEM_VOLATILE_P (XEXP (op
, 0))
765 && (GET_MODE_SIZE (mode
) >= UNITS_PER_WORD
766 || WORDS_BIG_ENDIAN
== BYTES_BIG_ENDIAN
))
768 int byte
= subreg_lowpart_offset (mode
, op_mode
);
769 int shifted_bytes
= INTVAL (XEXP (op
, 1)) / BITS_PER_UNIT
;
770 return adjust_address_nv (XEXP (op
, 0), mode
,
772 ? byte
- shifted_bytes
773 : byte
+ shifted_bytes
));
776 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
777 (OP:SI foo:SI) if OP is NEG or ABS. */
778 if ((GET_CODE (op
) == ABS
779 || GET_CODE (op
) == NEG
)
780 && (GET_CODE (XEXP (op
, 0)) == SIGN_EXTEND
781 || GET_CODE (XEXP (op
, 0)) == ZERO_EXTEND
)
782 && GET_MODE (XEXP (XEXP (op
, 0), 0)) == mode
)
783 return simplify_gen_unary (GET_CODE (op
), mode
,
784 XEXP (XEXP (op
, 0), 0), mode
);
786 /* (truncate:A (subreg:B (truncate:C X) 0)) is
788 if (GET_CODE (op
) == SUBREG
789 && SCALAR_INT_MODE_P (mode
)
790 && SCALAR_INT_MODE_P (op_mode
)
791 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op
)))
792 && GET_CODE (SUBREG_REG (op
)) == TRUNCATE
793 && subreg_lowpart_p (op
))
795 rtx inner
= XEXP (SUBREG_REG (op
), 0);
796 if (GET_MODE_PRECISION (mode
)
797 <= GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op
))))
798 return simplify_gen_unary (TRUNCATE
, mode
, inner
, GET_MODE (inner
));
800 /* If subreg above is paradoxical and C is narrower
801 than A, return (subreg:A (truncate:C X) 0). */
802 return simplify_gen_subreg (mode
, SUBREG_REG (op
),
803 GET_MODE (SUBREG_REG (op
)), 0);
806 /* (truncate:A (truncate:B X)) is (truncate:A X). */
807 if (GET_CODE (op
) == TRUNCATE
)
808 return simplify_gen_unary (TRUNCATE
, mode
, XEXP (op
, 0),
809 GET_MODE (XEXP (op
, 0)));
814 /* Try to simplify a unary operation CODE whose output mode is to be
815 MODE with input operand OP whose mode was originally OP_MODE.
816 Return zero if no simplification can be made. */
818 simplify_unary_operation (enum rtx_code code
, machine_mode mode
,
819 rtx op
, machine_mode op_mode
)
823 trueop
= avoid_constant_pool_reference (op
);
825 tem
= simplify_const_unary_operation (code
, mode
, trueop
, op_mode
);
829 return simplify_unary_operation_1 (code
, mode
, op
);
832 /* Perform some simplifications we can do even if the operands
835 simplify_unary_operation_1 (enum rtx_code code
, machine_mode mode
, rtx op
)
837 enum rtx_code reversed
;
843 /* (not (not X)) == X. */
844 if (GET_CODE (op
) == NOT
)
847 /* (not (eq X Y)) == (ne X Y), etc. if BImode or the result of the
848 comparison is all ones. */
849 if (COMPARISON_P (op
)
850 && (mode
== BImode
|| STORE_FLAG_VALUE
== -1)
851 && ((reversed
= reversed_comparison_code (op
, NULL_RTX
)) != UNKNOWN
))
852 return simplify_gen_relational (reversed
, mode
, VOIDmode
,
853 XEXP (op
, 0), XEXP (op
, 1));
855 /* (not (plus X -1)) can become (neg X). */
856 if (GET_CODE (op
) == PLUS
857 && XEXP (op
, 1) == constm1_rtx
)
858 return simplify_gen_unary (NEG
, mode
, XEXP (op
, 0), mode
);
860 /* Similarly, (not (neg X)) is (plus X -1). */
861 if (GET_CODE (op
) == NEG
)
862 return simplify_gen_binary (PLUS
, mode
, XEXP (op
, 0),
865 /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
866 if (GET_CODE (op
) == XOR
867 && CONST_INT_P (XEXP (op
, 1))
868 && (temp
= simplify_unary_operation (NOT
, mode
,
869 XEXP (op
, 1), mode
)) != 0)
870 return simplify_gen_binary (XOR
, mode
, XEXP (op
, 0), temp
);
872 /* (not (plus X C)) for signbit C is (xor X D) with D = ~C. */
873 if (GET_CODE (op
) == PLUS
874 && CONST_INT_P (XEXP (op
, 1))
875 && mode_signbit_p (mode
, XEXP (op
, 1))
876 && (temp
= simplify_unary_operation (NOT
, mode
,
877 XEXP (op
, 1), mode
)) != 0)
878 return simplify_gen_binary (XOR
, mode
, XEXP (op
, 0), temp
);
881 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for
882 operands other than 1, but that is not valid. We could do a
883 similar simplification for (not (lshiftrt C X)) where C is
884 just the sign bit, but this doesn't seem common enough to
886 if (GET_CODE (op
) == ASHIFT
887 && XEXP (op
, 0) == const1_rtx
)
889 temp
= simplify_gen_unary (NOT
, mode
, const1_rtx
, mode
);
890 return simplify_gen_binary (ROTATE
, mode
, temp
, XEXP (op
, 1));
893 /* (not (ashiftrt foo C)) where C is the number of bits in FOO
894 minus 1 is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1,
895 so we can perform the above simplification. */
896 if (STORE_FLAG_VALUE
== -1
897 && GET_CODE (op
) == ASHIFTRT
898 && CONST_INT_P (XEXP (op
, 1))
899 && INTVAL (XEXP (op
, 1)) == GET_MODE_PRECISION (mode
) - 1)
900 return simplify_gen_relational (GE
, mode
, VOIDmode
,
901 XEXP (op
, 0), const0_rtx
);
904 if (GET_CODE (op
) == SUBREG
905 && subreg_lowpart_p (op
)
906 && (GET_MODE_SIZE (GET_MODE (op
))
907 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op
))))
908 && GET_CODE (SUBREG_REG (op
)) == ASHIFT
909 && XEXP (SUBREG_REG (op
), 0) == const1_rtx
)
911 machine_mode inner_mode
= GET_MODE (SUBREG_REG (op
));
914 x
= gen_rtx_ROTATE (inner_mode
,
915 simplify_gen_unary (NOT
, inner_mode
, const1_rtx
,
917 XEXP (SUBREG_REG (op
), 1));
918 temp
= rtl_hooks
.gen_lowpart_no_emit (mode
, x
);
923 /* Apply De Morgan's laws to reduce number of patterns for machines
924 with negating logical insns (and-not, nand, etc.). If result has
925 only one NOT, put it first, since that is how the patterns are
927 if (GET_CODE (op
) == IOR
|| GET_CODE (op
) == AND
)
929 rtx in1
= XEXP (op
, 0), in2
= XEXP (op
, 1);
930 machine_mode op_mode
;
932 op_mode
= GET_MODE (in1
);
933 in1
= simplify_gen_unary (NOT
, op_mode
, in1
, op_mode
);
935 op_mode
= GET_MODE (in2
);
936 if (op_mode
== VOIDmode
)
938 in2
= simplify_gen_unary (NOT
, op_mode
, in2
, op_mode
);
940 if (GET_CODE (in2
) == NOT
&& GET_CODE (in1
) != NOT
)
941 std::swap (in1
, in2
);
943 return gen_rtx_fmt_ee (GET_CODE (op
) == IOR
? AND
: IOR
,
947 /* (not (bswap x)) -> (bswap (not x)). */
948 if (GET_CODE (op
) == BSWAP
)
950 rtx x
= simplify_gen_unary (NOT
, mode
, XEXP (op
, 0), mode
);
951 return simplify_gen_unary (BSWAP
, mode
, x
, mode
);
956 /* (neg (neg X)) == X. */
957 if (GET_CODE (op
) == NEG
)
960 /* (neg (plus X 1)) can become (not X). */
961 if (GET_CODE (op
) == PLUS
962 && XEXP (op
, 1) == const1_rtx
)
963 return simplify_gen_unary (NOT
, mode
, XEXP (op
, 0), mode
);
965 /* Similarly, (neg (not X)) is (plus X 1). */
966 if (GET_CODE (op
) == NOT
)
967 return simplify_gen_binary (PLUS
, mode
, XEXP (op
, 0),
970 /* (neg (minus X Y)) can become (minus Y X). This transformation
971 isn't safe for modes with signed zeros, since if X and Y are
972 both +0, (minus Y X) is the same as (minus X Y). If the
973 rounding mode is towards +infinity (or -infinity) then the two
974 expressions will be rounded differently. */
975 if (GET_CODE (op
) == MINUS
976 && !HONOR_SIGNED_ZEROS (mode
)
977 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode
))
978 return simplify_gen_binary (MINUS
, mode
, XEXP (op
, 1), XEXP (op
, 0));
980 if (GET_CODE (op
) == PLUS
981 && !HONOR_SIGNED_ZEROS (mode
)
982 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode
))
984 /* (neg (plus A C)) is simplified to (minus -C A). */
985 if (CONST_SCALAR_INT_P (XEXP (op
, 1))
986 || CONST_DOUBLE_AS_FLOAT_P (XEXP (op
, 1)))
988 temp
= simplify_unary_operation (NEG
, mode
, XEXP (op
, 1), mode
);
990 return simplify_gen_binary (MINUS
, mode
, temp
, XEXP (op
, 0));
993 /* (neg (plus A B)) is canonicalized to (minus (neg A) B). */
994 temp
= simplify_gen_unary (NEG
, mode
, XEXP (op
, 0), mode
);
995 return simplify_gen_binary (MINUS
, mode
, temp
, XEXP (op
, 1));
998 /* (neg (mult A B)) becomes (mult A (neg B)).
999 This works even for floating-point values. */
1000 if (GET_CODE (op
) == MULT
1001 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode
))
1003 temp
= simplify_gen_unary (NEG
, mode
, XEXP (op
, 1), mode
);
1004 return simplify_gen_binary (MULT
, mode
, XEXP (op
, 0), temp
);
1007 /* NEG commutes with ASHIFT since it is multiplication. Only do
1008 this if we can then eliminate the NEG (e.g., if the operand
1010 if (GET_CODE (op
) == ASHIFT
)
1012 temp
= simplify_unary_operation (NEG
, mode
, XEXP (op
, 0), mode
);
1014 return simplify_gen_binary (ASHIFT
, mode
, temp
, XEXP (op
, 1));
1017 /* (neg (ashiftrt X C)) can be replaced by (lshiftrt X C) when
1018 C is equal to the width of MODE minus 1. */
1019 if (GET_CODE (op
) == ASHIFTRT
1020 && CONST_INT_P (XEXP (op
, 1))
1021 && INTVAL (XEXP (op
, 1)) == GET_MODE_PRECISION (mode
) - 1)
1022 return simplify_gen_binary (LSHIFTRT
, mode
,
1023 XEXP (op
, 0), XEXP (op
, 1));
1025 /* (neg (lshiftrt X C)) can be replaced by (ashiftrt X C) when
1026 C is equal to the width of MODE minus 1. */
1027 if (GET_CODE (op
) == LSHIFTRT
1028 && CONST_INT_P (XEXP (op
, 1))
1029 && INTVAL (XEXP (op
, 1)) == GET_MODE_PRECISION (mode
) - 1)
1030 return simplify_gen_binary (ASHIFTRT
, mode
,
1031 XEXP (op
, 0), XEXP (op
, 1));
1033 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
1034 if (GET_CODE (op
) == XOR
1035 && XEXP (op
, 1) == const1_rtx
1036 && nonzero_bits (XEXP (op
, 0), mode
) == 1)
1037 return plus_constant (mode
, XEXP (op
, 0), -1);
1039 /* (neg (lt x 0)) is (ashiftrt X C) if STORE_FLAG_VALUE is 1. */
1040 /* (neg (lt x 0)) is (lshiftrt X C) if STORE_FLAG_VALUE is -1. */
1041 if (GET_CODE (op
) == LT
1042 && XEXP (op
, 1) == const0_rtx
1043 && SCALAR_INT_MODE_P (GET_MODE (XEXP (op
, 0))))
1045 machine_mode inner
= GET_MODE (XEXP (op
, 0));
1046 int isize
= GET_MODE_PRECISION (inner
);
1047 if (STORE_FLAG_VALUE
== 1)
1049 temp
= simplify_gen_binary (ASHIFTRT
, inner
, XEXP (op
, 0),
1050 GEN_INT (isize
- 1));
1053 if (GET_MODE_PRECISION (mode
) > isize
)
1054 return simplify_gen_unary (SIGN_EXTEND
, mode
, temp
, inner
);
1055 return simplify_gen_unary (TRUNCATE
, mode
, temp
, inner
);
1057 else if (STORE_FLAG_VALUE
== -1)
1059 temp
= simplify_gen_binary (LSHIFTRT
, inner
, XEXP (op
, 0),
1060 GEN_INT (isize
- 1));
1063 if (GET_MODE_PRECISION (mode
) > isize
)
1064 return simplify_gen_unary (ZERO_EXTEND
, mode
, temp
, inner
);
1065 return simplify_gen_unary (TRUNCATE
, mode
, temp
, inner
);
1071 /* Don't optimize (lshiftrt (mult ...)) as it would interfere
1072 with the umulXi3_highpart patterns. */
1073 if (GET_CODE (op
) == LSHIFTRT
1074 && GET_CODE (XEXP (op
, 0)) == MULT
)
1077 if (GET_MODE_CLASS (mode
) == MODE_PARTIAL_INT
)
1079 if (TRULY_NOOP_TRUNCATION_MODES_P (mode
, GET_MODE (op
)))
1081 temp
= rtl_hooks
.gen_lowpart_no_emit (mode
, op
);
1085 /* We can't handle truncation to a partial integer mode here
1086 because we don't know the real bitsize of the partial
1091 if (GET_MODE (op
) != VOIDmode
)
1093 temp
= simplify_truncation (mode
, op
, GET_MODE (op
));
1098 /* If we know that the value is already truncated, we can
1099 replace the TRUNCATE with a SUBREG. */
1100 if (GET_MODE_NUNITS (mode
) == 1
1101 && (TRULY_NOOP_TRUNCATION_MODES_P (mode
, GET_MODE (op
))
1102 || truncated_to_mode (mode
, op
)))
1104 temp
= rtl_hooks
.gen_lowpart_no_emit (mode
, op
);
1109 /* A truncate of a comparison can be replaced with a subreg if
1110 STORE_FLAG_VALUE permits. This is like the previous test,
1111 but it works even if the comparison is done in a mode larger
1112 than HOST_BITS_PER_WIDE_INT. */
1113 if (HWI_COMPUTABLE_MODE_P (mode
)
1114 && COMPARISON_P (op
)
1115 && (STORE_FLAG_VALUE
& ~GET_MODE_MASK (mode
)) == 0)
1117 temp
= rtl_hooks
.gen_lowpart_no_emit (mode
, op
);
1122 /* A truncate of a memory is just loading the low part of the memory
1123 if we are not changing the meaning of the address. */
1124 if (GET_CODE (op
) == MEM
1125 && !VECTOR_MODE_P (mode
)
1126 && !MEM_VOLATILE_P (op
)
1127 && !mode_dependent_address_p (XEXP (op
, 0), MEM_ADDR_SPACE (op
)))
1129 temp
= rtl_hooks
.gen_lowpart_no_emit (mode
, op
);
1136 case FLOAT_TRUNCATE
:
1137 if (DECIMAL_FLOAT_MODE_P (mode
))
1140 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
1141 if (GET_CODE (op
) == FLOAT_EXTEND
1142 && GET_MODE (XEXP (op
, 0)) == mode
)
1143 return XEXP (op
, 0);
1145 /* (float_truncate:SF (float_truncate:DF foo:XF))
1146 = (float_truncate:SF foo:XF).
1147 This may eliminate double rounding, so it is unsafe.
1149 (float_truncate:SF (float_extend:XF foo:DF))
1150 = (float_truncate:SF foo:DF).
1152 (float_truncate:DF (float_extend:XF foo:SF))
1153 = (float_extend:DF foo:SF). */
1154 if ((GET_CODE (op
) == FLOAT_TRUNCATE
1155 && flag_unsafe_math_optimizations
)
1156 || GET_CODE (op
) == FLOAT_EXTEND
)
1157 return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (op
,
1159 > GET_MODE_SIZE (mode
)
1160 ? FLOAT_TRUNCATE
: FLOAT_EXTEND
,
1162 XEXP (op
, 0), mode
);
1164 /* (float_truncate (float x)) is (float x) */
1165 if ((GET_CODE (op
) == FLOAT
|| GET_CODE (op
) == UNSIGNED_FLOAT
)
1166 && (flag_unsafe_math_optimizations
1167 || (SCALAR_FLOAT_MODE_P (GET_MODE (op
))
1168 && ((unsigned)significand_size (GET_MODE (op
))
1169 >= (GET_MODE_PRECISION (GET_MODE (XEXP (op
, 0)))
1170 - num_sign_bit_copies (XEXP (op
, 0),
1171 GET_MODE (XEXP (op
, 0))))))))
1172 return simplify_gen_unary (GET_CODE (op
), mode
,
1174 GET_MODE (XEXP (op
, 0)));
1176 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
1177 (OP:SF foo:SF) if OP is NEG or ABS. */
1178 if ((GET_CODE (op
) == ABS
1179 || GET_CODE (op
) == NEG
)
1180 && GET_CODE (XEXP (op
, 0)) == FLOAT_EXTEND
1181 && GET_MODE (XEXP (XEXP (op
, 0), 0)) == mode
)
1182 return simplify_gen_unary (GET_CODE (op
), mode
,
1183 XEXP (XEXP (op
, 0), 0), mode
);
1185 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
1186 is (float_truncate:SF x). */
1187 if (GET_CODE (op
) == SUBREG
1188 && subreg_lowpart_p (op
)
1189 && GET_CODE (SUBREG_REG (op
)) == FLOAT_TRUNCATE
)
1190 return SUBREG_REG (op
);
1194 if (DECIMAL_FLOAT_MODE_P (mode
))
1197 /* (float_extend (float_extend x)) is (float_extend x)
1199 (float_extend (float x)) is (float x) assuming that double
1200 rounding can't happen.
1202 if (GET_CODE (op
) == FLOAT_EXTEND
1203 || ((GET_CODE (op
) == FLOAT
|| GET_CODE (op
) == UNSIGNED_FLOAT
)
1204 && SCALAR_FLOAT_MODE_P (GET_MODE (op
))
1205 && ((unsigned)significand_size (GET_MODE (op
))
1206 >= (GET_MODE_PRECISION (GET_MODE (XEXP (op
, 0)))
1207 - num_sign_bit_copies (XEXP (op
, 0),
1208 GET_MODE (XEXP (op
, 0)))))))
1209 return simplify_gen_unary (GET_CODE (op
), mode
,
1211 GET_MODE (XEXP (op
, 0)));
1216 /* (abs (neg <foo>)) -> (abs <foo>) */
1217 if (GET_CODE (op
) == NEG
)
1218 return simplify_gen_unary (ABS
, mode
, XEXP (op
, 0),
1219 GET_MODE (XEXP (op
, 0)));
1221 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
1223 if (GET_MODE (op
) == VOIDmode
)
1226 /* If operand is something known to be positive, ignore the ABS. */
1227 if (GET_CODE (op
) == FFS
|| GET_CODE (op
) == ABS
1228 || val_signbit_known_clear_p (GET_MODE (op
),
1229 nonzero_bits (op
, GET_MODE (op
))))
1232 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
1233 if (num_sign_bit_copies (op
, mode
) == GET_MODE_PRECISION (mode
))
1234 return gen_rtx_NEG (mode
, op
);
1239 /* (ffs (*_extend <X>)) = (ffs <X>) */
1240 if (GET_CODE (op
) == SIGN_EXTEND
1241 || GET_CODE (op
) == ZERO_EXTEND
)
1242 return simplify_gen_unary (FFS
, mode
, XEXP (op
, 0),
1243 GET_MODE (XEXP (op
, 0)));
1247 switch (GET_CODE (op
))
1251 /* (popcount (zero_extend <X>)) = (popcount <X>) */
1252 return simplify_gen_unary (POPCOUNT
, mode
, XEXP (op
, 0),
1253 GET_MODE (XEXP (op
, 0)));
1257 /* Rotations don't affect popcount. */
1258 if (!side_effects_p (XEXP (op
, 1)))
1259 return simplify_gen_unary (POPCOUNT
, mode
, XEXP (op
, 0),
1260 GET_MODE (XEXP (op
, 0)));
1269 switch (GET_CODE (op
))
1275 return simplify_gen_unary (PARITY
, mode
, XEXP (op
, 0),
1276 GET_MODE (XEXP (op
, 0)));
1280 /* Rotations don't affect parity. */
1281 if (!side_effects_p (XEXP (op
, 1)))
1282 return simplify_gen_unary (PARITY
, mode
, XEXP (op
, 0),
1283 GET_MODE (XEXP (op
, 0)));
1292 /* (bswap (bswap x)) -> x. */
1293 if (GET_CODE (op
) == BSWAP
)
1294 return XEXP (op
, 0);
1298 /* (float (sign_extend <X>)) = (float <X>). */
1299 if (GET_CODE (op
) == SIGN_EXTEND
)
1300 return simplify_gen_unary (FLOAT
, mode
, XEXP (op
, 0),
1301 GET_MODE (XEXP (op
, 0)));
1305 /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
1306 becomes just the MINUS if its mode is MODE. This allows
1307 folding switch statements on machines using casesi (such as
1309 if (GET_CODE (op
) == TRUNCATE
1310 && GET_MODE (XEXP (op
, 0)) == mode
1311 && GET_CODE (XEXP (op
, 0)) == MINUS
1312 && GET_CODE (XEXP (XEXP (op
, 0), 0)) == LABEL_REF
1313 && GET_CODE (XEXP (XEXP (op
, 0), 1)) == LABEL_REF
)
1314 return XEXP (op
, 0);
1316 /* Extending a widening multiplication should be canonicalized to
1317 a wider widening multiplication. */
1318 if (GET_CODE (op
) == MULT
)
1320 rtx lhs
= XEXP (op
, 0);
1321 rtx rhs
= XEXP (op
, 1);
1322 enum rtx_code lcode
= GET_CODE (lhs
);
1323 enum rtx_code rcode
= GET_CODE (rhs
);
1325 /* Widening multiplies usually extend both operands, but sometimes
1326 they use a shift to extract a portion of a register. */
1327 if ((lcode
== SIGN_EXTEND
1328 || (lcode
== ASHIFTRT
&& CONST_INT_P (XEXP (lhs
, 1))))
1329 && (rcode
== SIGN_EXTEND
1330 || (rcode
== ASHIFTRT
&& CONST_INT_P (XEXP (rhs
, 1)))))
1332 machine_mode lmode
= GET_MODE (lhs
);
1333 machine_mode rmode
= GET_MODE (rhs
);
1336 if (lcode
== ASHIFTRT
)
1337 /* Number of bits not shifted off the end. */
1338 bits
= GET_MODE_PRECISION (lmode
) - INTVAL (XEXP (lhs
, 1));
1339 else /* lcode == SIGN_EXTEND */
1340 /* Size of inner mode. */
1341 bits
= GET_MODE_PRECISION (GET_MODE (XEXP (lhs
, 0)));
1343 if (rcode
== ASHIFTRT
)
1344 bits
+= GET_MODE_PRECISION (rmode
) - INTVAL (XEXP (rhs
, 1));
1345 else /* rcode == SIGN_EXTEND */
1346 bits
+= GET_MODE_PRECISION (GET_MODE (XEXP (rhs
, 0)));
1348 /* We can only widen multiplies if the result is mathematiclly
1349 equivalent. I.e. if overflow was impossible. */
1350 if (bits
<= GET_MODE_PRECISION (GET_MODE (op
)))
1351 return simplify_gen_binary
1353 simplify_gen_unary (SIGN_EXTEND
, mode
, lhs
, lmode
),
1354 simplify_gen_unary (SIGN_EXTEND
, mode
, rhs
, rmode
));
1358 /* Check for a sign extension of a subreg of a promoted
1359 variable, where the promotion is sign-extended, and the
1360 target mode is the same as the variable's promotion. */
1361 if (GET_CODE (op
) == SUBREG
1362 && SUBREG_PROMOTED_VAR_P (op
)
1363 && SUBREG_PROMOTED_SIGNED_P (op
)
1364 && GET_MODE_SIZE (mode
) <= GET_MODE_SIZE (GET_MODE (XEXP (op
, 0))))
1366 temp
= rtl_hooks
.gen_lowpart_no_emit (mode
, op
);
1371 /* (sign_extend:M (sign_extend:N <X>)) is (sign_extend:M <X>).
1372 (sign_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1373 if (GET_CODE (op
) == SIGN_EXTEND
|| GET_CODE (op
) == ZERO_EXTEND
)
1375 gcc_assert (GET_MODE_PRECISION (mode
)
1376 > GET_MODE_PRECISION (GET_MODE (op
)));
1377 return simplify_gen_unary (GET_CODE (op
), mode
, XEXP (op
, 0),
1378 GET_MODE (XEXP (op
, 0)));
1381 /* (sign_extend:M (ashiftrt:N (ashift <X> (const_int I)) (const_int I)))
1382 is (sign_extend:M (subreg:O <X>)) if there is mode with
1383 GET_MODE_BITSIZE (N) - I bits.
1384 (sign_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1385 is similarly (zero_extend:M (subreg:O <X>)). */
1386 if ((GET_CODE (op
) == ASHIFTRT
|| GET_CODE (op
) == LSHIFTRT
)
1387 && GET_CODE (XEXP (op
, 0)) == ASHIFT
1388 && CONST_INT_P (XEXP (op
, 1))
1389 && XEXP (XEXP (op
, 0), 1) == XEXP (op
, 1)
1390 && GET_MODE_BITSIZE (GET_MODE (op
)) > INTVAL (XEXP (op
, 1)))
1393 = mode_for_size (GET_MODE_BITSIZE (GET_MODE (op
))
1394 - INTVAL (XEXP (op
, 1)), MODE_INT
, 1);
1395 gcc_assert (GET_MODE_BITSIZE (mode
)
1396 > GET_MODE_BITSIZE (GET_MODE (op
)));
1397 if (tmode
!= BLKmode
)
1400 rtl_hooks
.gen_lowpart_no_emit (tmode
, XEXP (XEXP (op
, 0), 0));
1402 return simplify_gen_unary (GET_CODE (op
) == ASHIFTRT
1403 ? SIGN_EXTEND
: ZERO_EXTEND
,
1404 mode
, inner
, tmode
);
1408 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
1409 /* As we do not know which address space the pointer is referring to,
1410 we can do this only if the target does not support different pointer
1411 or address modes depending on the address space. */
1412 if (target_default_pointer_address_modes_p ()
1413 && ! POINTERS_EXTEND_UNSIGNED
1414 && mode
== Pmode
&& GET_MODE (op
) == ptr_mode
1416 || (GET_CODE (op
) == SUBREG
1417 && REG_P (SUBREG_REG (op
))
1418 && REG_POINTER (SUBREG_REG (op
))
1419 && GET_MODE (SUBREG_REG (op
)) == Pmode
)))
1420 return convert_memory_address (Pmode
, op
);
1425 /* Check for a zero extension of a subreg of a promoted
1426 variable, where the promotion is zero-extended, and the
1427 target mode is the same as the variable's promotion. */
1428 if (GET_CODE (op
) == SUBREG
1429 && SUBREG_PROMOTED_VAR_P (op
)
1430 && SUBREG_PROMOTED_UNSIGNED_P (op
)
1431 && GET_MODE_SIZE (mode
) <= GET_MODE_SIZE (GET_MODE (XEXP (op
, 0))))
1433 temp
= rtl_hooks
.gen_lowpart_no_emit (mode
, op
);
1438 /* Extending a widening multiplication should be canonicalized to
1439 a wider widening multiplication. */
1440 if (GET_CODE (op
) == MULT
)
1442 rtx lhs
= XEXP (op
, 0);
1443 rtx rhs
= XEXP (op
, 1);
1444 enum rtx_code lcode
= GET_CODE (lhs
);
1445 enum rtx_code rcode
= GET_CODE (rhs
);
1447 /* Widening multiplies usually extend both operands, but sometimes
1448 they use a shift to extract a portion of a register. */
1449 if ((lcode
== ZERO_EXTEND
1450 || (lcode
== LSHIFTRT
&& CONST_INT_P (XEXP (lhs
, 1))))
1451 && (rcode
== ZERO_EXTEND
1452 || (rcode
== LSHIFTRT
&& CONST_INT_P (XEXP (rhs
, 1)))))
1454 machine_mode lmode
= GET_MODE (lhs
);
1455 machine_mode rmode
= GET_MODE (rhs
);
1458 if (lcode
== LSHIFTRT
)
1459 /* Number of bits not shifted off the end. */
1460 bits
= GET_MODE_PRECISION (lmode
) - INTVAL (XEXP (lhs
, 1));
1461 else /* lcode == ZERO_EXTEND */
1462 /* Size of inner mode. */
1463 bits
= GET_MODE_PRECISION (GET_MODE (XEXP (lhs
, 0)));
1465 if (rcode
== LSHIFTRT
)
1466 bits
+= GET_MODE_PRECISION (rmode
) - INTVAL (XEXP (rhs
, 1));
1467 else /* rcode == ZERO_EXTEND */
1468 bits
+= GET_MODE_PRECISION (GET_MODE (XEXP (rhs
, 0)));
1470 /* We can only widen multiplies if the result is mathematiclly
1471 equivalent. I.e. if overflow was impossible. */
1472 if (bits
<= GET_MODE_PRECISION (GET_MODE (op
)))
1473 return simplify_gen_binary
1475 simplify_gen_unary (ZERO_EXTEND
, mode
, lhs
, lmode
),
1476 simplify_gen_unary (ZERO_EXTEND
, mode
, rhs
, rmode
));
1480 /* (zero_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1481 if (GET_CODE (op
) == ZERO_EXTEND
)
1482 return simplify_gen_unary (ZERO_EXTEND
, mode
, XEXP (op
, 0),
1483 GET_MODE (XEXP (op
, 0)));
1485 /* (zero_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1486 is (zero_extend:M (subreg:O <X>)) if there is mode with
1487 GET_MODE_PRECISION (N) - I bits. */
1488 if (GET_CODE (op
) == LSHIFTRT
1489 && GET_CODE (XEXP (op
, 0)) == ASHIFT
1490 && CONST_INT_P (XEXP (op
, 1))
1491 && XEXP (XEXP (op
, 0), 1) == XEXP (op
, 1)
1492 && GET_MODE_PRECISION (GET_MODE (op
)) > INTVAL (XEXP (op
, 1)))
1495 = mode_for_size (GET_MODE_PRECISION (GET_MODE (op
))
1496 - INTVAL (XEXP (op
, 1)), MODE_INT
, 1);
1497 if (tmode
!= BLKmode
)
1500 rtl_hooks
.gen_lowpart_no_emit (tmode
, XEXP (XEXP (op
, 0), 0));
1502 return simplify_gen_unary (ZERO_EXTEND
, mode
, inner
, tmode
);
1506 /* (zero_extend:M (subreg:N <X:O>)) is <X:O> (for M == O) or
1507 (zero_extend:M <X:O>), if X doesn't have any non-zero bits outside
1509 (zero_extend:SI (subreg:QI (and:SI (reg:SI) (const_int 63)) 0)) is
1510 (and:SI (reg:SI) (const_int 63)). */
1511 if (GET_CODE (op
) == SUBREG
1512 && GET_MODE_PRECISION (GET_MODE (op
))
1513 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op
)))
1514 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op
)))
1515 <= HOST_BITS_PER_WIDE_INT
1516 && GET_MODE_PRECISION (mode
)
1517 >= GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op
)))
1518 && subreg_lowpart_p (op
)
1519 && (nonzero_bits (SUBREG_REG (op
), GET_MODE (SUBREG_REG (op
)))
1520 & ~GET_MODE_MASK (GET_MODE (op
))) == 0)
1522 if (GET_MODE_PRECISION (mode
)
1523 == GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op
))))
1524 return SUBREG_REG (op
);
1525 return simplify_gen_unary (ZERO_EXTEND
, mode
, SUBREG_REG (op
),
1526 GET_MODE (SUBREG_REG (op
)));
1529 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
1530 /* As we do not know which address space the pointer is referring to,
1531 we can do this only if the target does not support different pointer
1532 or address modes depending on the address space. */
1533 if (target_default_pointer_address_modes_p ()
1534 && POINTERS_EXTEND_UNSIGNED
> 0
1535 && mode
== Pmode
&& GET_MODE (op
) == ptr_mode
1537 || (GET_CODE (op
) == SUBREG
1538 && REG_P (SUBREG_REG (op
))
1539 && REG_POINTER (SUBREG_REG (op
))
1540 && GET_MODE (SUBREG_REG (op
)) == Pmode
)))
1541 return convert_memory_address (Pmode
, op
);
1552 /* Try to compute the value of a unary operation CODE whose output mode is to
1553 be MODE with input operand OP whose mode was originally OP_MODE.
1554 Return zero if the value cannot be computed. */
1556 simplify_const_unary_operation (enum rtx_code code
, machine_mode mode
,
1557 rtx op
, machine_mode op_mode
)
1559 unsigned int width
= GET_MODE_PRECISION (mode
);
1561 if (code
== VEC_DUPLICATE
)
1563 gcc_assert (VECTOR_MODE_P (mode
));
1564 if (GET_MODE (op
) != VOIDmode
)
1566 if (!VECTOR_MODE_P (GET_MODE (op
)))
1567 gcc_assert (GET_MODE_INNER (mode
) == GET_MODE (op
));
1569 gcc_assert (GET_MODE_INNER (mode
) == GET_MODE_INNER
1572 if (CONST_SCALAR_INT_P (op
) || CONST_DOUBLE_AS_FLOAT_P (op
)
1573 || GET_CODE (op
) == CONST_VECTOR
)
1575 int elt_size
= GET_MODE_SIZE (GET_MODE_INNER (mode
));
1576 unsigned n_elts
= (GET_MODE_SIZE (mode
) / elt_size
);
1577 rtvec v
= rtvec_alloc (n_elts
);
1580 if (GET_CODE (op
) != CONST_VECTOR
)
1581 for (i
= 0; i
< n_elts
; i
++)
1582 RTVEC_ELT (v
, i
) = op
;
1585 machine_mode inmode
= GET_MODE (op
);
1586 int in_elt_size
= GET_MODE_SIZE (GET_MODE_INNER (inmode
));
1587 unsigned in_n_elts
= (GET_MODE_SIZE (inmode
) / in_elt_size
);
1589 gcc_assert (in_n_elts
< n_elts
);
1590 gcc_assert ((n_elts
% in_n_elts
) == 0);
1591 for (i
= 0; i
< n_elts
; i
++)
1592 RTVEC_ELT (v
, i
) = CONST_VECTOR_ELT (op
, i
% in_n_elts
);
1594 return gen_rtx_CONST_VECTOR (mode
, v
);
1598 if (VECTOR_MODE_P (mode
) && GET_CODE (op
) == CONST_VECTOR
)
1600 int elt_size
= GET_MODE_SIZE (GET_MODE_INNER (mode
));
1601 unsigned n_elts
= (GET_MODE_SIZE (mode
) / elt_size
);
1602 machine_mode opmode
= GET_MODE (op
);
1603 int op_elt_size
= GET_MODE_SIZE (GET_MODE_INNER (opmode
));
1604 unsigned op_n_elts
= (GET_MODE_SIZE (opmode
) / op_elt_size
);
1605 rtvec v
= rtvec_alloc (n_elts
);
1608 gcc_assert (op_n_elts
== n_elts
);
1609 for (i
= 0; i
< n_elts
; i
++)
1611 rtx x
= simplify_unary_operation (code
, GET_MODE_INNER (mode
),
1612 CONST_VECTOR_ELT (op
, i
),
1613 GET_MODE_INNER (opmode
));
1616 RTVEC_ELT (v
, i
) = x
;
1618 return gen_rtx_CONST_VECTOR (mode
, v
);
1621 /* The order of these tests is critical so that, for example, we don't
1622 check the wrong mode (input vs. output) for a conversion operation,
1623 such as FIX. At some point, this should be simplified. */
1625 if (code
== FLOAT
&& CONST_SCALAR_INT_P (op
))
1629 if (op_mode
== VOIDmode
)
1631 /* CONST_INT have VOIDmode as the mode. We assume that all
1632 the bits of the constant are significant, though, this is
1633 a dangerous assumption as many times CONST_INTs are
1634 created and used with garbage in the bits outside of the
1635 precision of the implied mode of the const_int. */
1636 op_mode
= MAX_MODE_INT
;
1639 real_from_integer (&d
, mode
, std::make_pair (op
, op_mode
), SIGNED
);
1640 d
= real_value_truncate (mode
, d
);
1641 return CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
1643 else if (code
== UNSIGNED_FLOAT
&& CONST_SCALAR_INT_P (op
))
1647 if (op_mode
== VOIDmode
)
1649 /* CONST_INT have VOIDmode as the mode. We assume that all
1650 the bits of the constant are significant, though, this is
1651 a dangerous assumption as many times CONST_INTs are
1652 created and used with garbage in the bits outside of the
1653 precision of the implied mode of the const_int. */
1654 op_mode
= MAX_MODE_INT
;
1657 real_from_integer (&d
, mode
, std::make_pair (op
, op_mode
), UNSIGNED
);
1658 d
= real_value_truncate (mode
, d
);
1659 return CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
1662 if (CONST_SCALAR_INT_P (op
) && width
> 0)
1665 machine_mode imode
= op_mode
== VOIDmode
? mode
: op_mode
;
1666 rtx_mode_t op0
= std::make_pair (op
, imode
);
1669 #if TARGET_SUPPORTS_WIDE_INT == 0
1670 /* This assert keeps the simplification from producing a result
1671 that cannot be represented in a CONST_DOUBLE but a lot of
1672 upstream callers expect that this function never fails to
1673 simplify something and so you if you added this to the test
1674 above the code would die later anyway. If this assert
1675 happens, you just need to make the port support wide int. */
1676 gcc_assert (width
<= HOST_BITS_PER_DOUBLE_INT
);
1682 result
= wi::bit_not (op0
);
1686 result
= wi::neg (op0
);
1690 result
= wi::abs (op0
);
1694 result
= wi::shwi (wi::ffs (op0
), mode
);
1698 if (wi::ne_p (op0
, 0))
1699 int_value
= wi::clz (op0
);
1700 else if (! CLZ_DEFINED_VALUE_AT_ZERO (mode
, int_value
))
1701 int_value
= GET_MODE_PRECISION (mode
);
1702 result
= wi::shwi (int_value
, mode
);
1706 result
= wi::shwi (wi::clrsb (op0
), mode
);
1710 if (wi::ne_p (op0
, 0))
1711 int_value
= wi::ctz (op0
);
1712 else if (! CTZ_DEFINED_VALUE_AT_ZERO (mode
, int_value
))
1713 int_value
= GET_MODE_PRECISION (mode
);
1714 result
= wi::shwi (int_value
, mode
);
1718 result
= wi::shwi (wi::popcount (op0
), mode
);
1722 result
= wi::shwi (wi::parity (op0
), mode
);
1726 result
= wide_int (op0
).bswap ();
1731 result
= wide_int::from (op0
, width
, UNSIGNED
);
1735 result
= wide_int::from (op0
, width
, SIGNED
);
1743 return immed_wide_int_const (result
, mode
);
1746 else if (CONST_DOUBLE_AS_FLOAT_P (op
)
1747 && SCALAR_FLOAT_MODE_P (mode
)
1748 && SCALAR_FLOAT_MODE_P (GET_MODE (op
)))
1751 REAL_VALUE_FROM_CONST_DOUBLE (d
, op
);
1758 d
= real_value_abs (&d
);
1761 d
= real_value_negate (&d
);
1763 case FLOAT_TRUNCATE
:
1764 d
= real_value_truncate (mode
, d
);
1767 /* All this does is change the mode, unless changing
1769 if (GET_MODE_CLASS (mode
) != GET_MODE_CLASS (GET_MODE (op
)))
1770 real_convert (&d
, mode
, &d
);
1773 real_arithmetic (&d
, FIX_TRUNC_EXPR
, &d
, NULL
);
1780 real_to_target (tmp
, &d
, GET_MODE (op
));
1781 for (i
= 0; i
< 4; i
++)
1783 real_from_target (&d
, tmp
, mode
);
1789 return CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
1791 else if (CONST_DOUBLE_AS_FLOAT_P (op
)
1792 && SCALAR_FLOAT_MODE_P (GET_MODE (op
))
1793 && GET_MODE_CLASS (mode
) == MODE_INT
1796 /* Although the overflow semantics of RTL's FIX and UNSIGNED_FIX
1797 operators are intentionally left unspecified (to ease implementation
1798 by target backends), for consistency, this routine implements the
1799 same semantics for constant folding as used by the middle-end. */
1801 /* This was formerly used only for non-IEEE float.
1802 eggert@twinsun.com says it is safe for IEEE also. */
1803 REAL_VALUE_TYPE x
, t
;
1804 REAL_VALUE_FROM_CONST_DOUBLE (x
, op
);
1805 wide_int wmax
, wmin
;
1806 /* This is part of the abi to real_to_integer, but we check
1807 things before making this call. */
1813 if (REAL_VALUE_ISNAN (x
))
1816 /* Test against the signed upper bound. */
1817 wmax
= wi::max_value (width
, SIGNED
);
1818 real_from_integer (&t
, VOIDmode
, wmax
, SIGNED
);
1819 if (REAL_VALUES_LESS (t
, x
))
1820 return immed_wide_int_const (wmax
, mode
);
1822 /* Test against the signed lower bound. */
1823 wmin
= wi::min_value (width
, SIGNED
);
1824 real_from_integer (&t
, VOIDmode
, wmin
, SIGNED
);
1825 if (REAL_VALUES_LESS (x
, t
))
1826 return immed_wide_int_const (wmin
, mode
);
1828 return immed_wide_int_const (real_to_integer (&x
, &fail
, width
), mode
);
1832 if (REAL_VALUE_ISNAN (x
) || REAL_VALUE_NEGATIVE (x
))
1835 /* Test against the unsigned upper bound. */
1836 wmax
= wi::max_value (width
, UNSIGNED
);
1837 real_from_integer (&t
, VOIDmode
, wmax
, UNSIGNED
);
1838 if (REAL_VALUES_LESS (t
, x
))
1839 return immed_wide_int_const (wmax
, mode
);
1841 return immed_wide_int_const (real_to_integer (&x
, &fail
, width
),
1853 /* Subroutine of simplify_binary_operation to simplify a binary operation
1854 CODE that can commute with byte swapping, with result mode MODE and
1855 operating on OP0 and OP1. CODE is currently one of AND, IOR or XOR.
1856 Return zero if no simplification or canonicalization is possible. */
1859 simplify_byte_swapping_operation (enum rtx_code code
, machine_mode mode
,
1864 /* (op (bswap x) C1)) -> (bswap (op x C2)) with C2 swapped. */
1865 if (GET_CODE (op0
) == BSWAP
&& CONST_SCALAR_INT_P (op1
))
1867 tem
= simplify_gen_binary (code
, mode
, XEXP (op0
, 0),
1868 simplify_gen_unary (BSWAP
, mode
, op1
, mode
));
1869 return simplify_gen_unary (BSWAP
, mode
, tem
, mode
);
1872 /* (op (bswap x) (bswap y)) -> (bswap (op x y)). */
1873 if (GET_CODE (op0
) == BSWAP
&& GET_CODE (op1
) == BSWAP
)
1875 tem
= simplify_gen_binary (code
, mode
, XEXP (op0
, 0), XEXP (op1
, 0));
1876 return simplify_gen_unary (BSWAP
, mode
, tem
, mode
);
1882 /* Subroutine of simplify_binary_operation to simplify a commutative,
1883 associative binary operation CODE with result mode MODE, operating
1884 on OP0 and OP1. CODE is currently one of PLUS, MULT, AND, IOR, XOR,
1885 SMIN, SMAX, UMIN or UMAX. Return zero if no simplification or
1886 canonicalization is possible. */
1889 simplify_associative_operation (enum rtx_code code
, machine_mode mode
,
1894 /* Linearize the operator to the left. */
1895 if (GET_CODE (op1
) == code
)
1897 /* "(a op b) op (c op d)" becomes "((a op b) op c) op d)". */
1898 if (GET_CODE (op0
) == code
)
1900 tem
= simplify_gen_binary (code
, mode
, op0
, XEXP (op1
, 0));
1901 return simplify_gen_binary (code
, mode
, tem
, XEXP (op1
, 1));
1904 /* "a op (b op c)" becomes "(b op c) op a". */
1905 if (! swap_commutative_operands_p (op1
, op0
))
1906 return simplify_gen_binary (code
, mode
, op1
, op0
);
1908 std::swap (op0
, op1
);
1911 if (GET_CODE (op0
) == code
)
1913 /* Canonicalize "(x op c) op y" as "(x op y) op c". */
1914 if (swap_commutative_operands_p (XEXP (op0
, 1), op1
))
1916 tem
= simplify_gen_binary (code
, mode
, XEXP (op0
, 0), op1
);
1917 return simplify_gen_binary (code
, mode
, tem
, XEXP (op0
, 1));
1920 /* Attempt to simplify "(a op b) op c" as "a op (b op c)". */
1921 tem
= simplify_binary_operation (code
, mode
, XEXP (op0
, 1), op1
);
1923 return simplify_gen_binary (code
, mode
, XEXP (op0
, 0), tem
);
1925 /* Attempt to simplify "(a op b) op c" as "(a op c) op b". */
1926 tem
= simplify_binary_operation (code
, mode
, XEXP (op0
, 0), op1
);
1928 return simplify_gen_binary (code
, mode
, tem
, XEXP (op0
, 1));
1935 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
1936 and OP1. Return 0 if no simplification is possible.
1938 Don't use this for relational operations such as EQ or LT.
1939 Use simplify_relational_operation instead. */
1941 simplify_binary_operation (enum rtx_code code
, machine_mode mode
,
1944 rtx trueop0
, trueop1
;
1947 /* Relational operations don't work here. We must know the mode
1948 of the operands in order to do the comparison correctly.
1949 Assuming a full word can give incorrect results.
1950 Consider comparing 128 with -128 in QImode. */
1951 gcc_assert (GET_RTX_CLASS (code
) != RTX_COMPARE
);
1952 gcc_assert (GET_RTX_CLASS (code
) != RTX_COMM_COMPARE
);
1954 /* Make sure the constant is second. */
1955 if (GET_RTX_CLASS (code
) == RTX_COMM_ARITH
1956 && swap_commutative_operands_p (op0
, op1
))
1957 std::swap (op0
, op1
);
1959 trueop0
= avoid_constant_pool_reference (op0
);
1960 trueop1
= avoid_constant_pool_reference (op1
);
1962 tem
= simplify_const_binary_operation (code
, mode
, trueop0
, trueop1
);
1965 return simplify_binary_operation_1 (code
, mode
, op0
, op1
, trueop0
, trueop1
);
1968 /* Subroutine of simplify_binary_operation. Simplify a binary operation
1969 CODE with result mode MODE, operating on OP0 and OP1. If OP0 and/or
1970 OP1 are constant pool references, TRUEOP0 and TRUEOP1 represent the
1971 actual constants. */
1974 simplify_binary_operation_1 (enum rtx_code code
, machine_mode mode
,
1975 rtx op0
, rtx op1
, rtx trueop0
, rtx trueop1
)
1977 rtx tem
, reversed
, opleft
, opright
;
1979 unsigned int width
= GET_MODE_PRECISION (mode
);
1981 /* Even if we can't compute a constant result,
1982 there are some cases worth simplifying. */
1987 /* Maybe simplify x + 0 to x. The two expressions are equivalent
1988 when x is NaN, infinite, or finite and nonzero. They aren't
1989 when x is -0 and the rounding mode is not towards -infinity,
1990 since (-0) + 0 is then 0. */
1991 if (!HONOR_SIGNED_ZEROS (mode
) && trueop1
== CONST0_RTX (mode
))
1994 /* ((-a) + b) -> (b - a) and similarly for (a + (-b)). These
1995 transformations are safe even for IEEE. */
1996 if (GET_CODE (op0
) == NEG
)
1997 return simplify_gen_binary (MINUS
, mode
, op1
, XEXP (op0
, 0));
1998 else if (GET_CODE (op1
) == NEG
)
1999 return simplify_gen_binary (MINUS
, mode
, op0
, XEXP (op1
, 0));
2001 /* (~a) + 1 -> -a */
2002 if (INTEGRAL_MODE_P (mode
)
2003 && GET_CODE (op0
) == NOT
2004 && trueop1
== const1_rtx
)
2005 return simplify_gen_unary (NEG
, mode
, XEXP (op0
, 0), mode
);
2007 /* Handle both-operands-constant cases. We can only add
2008 CONST_INTs to constants since the sum of relocatable symbols
2009 can't be handled by most assemblers. Don't add CONST_INT
2010 to CONST_INT since overflow won't be computed properly if wider
2011 than HOST_BITS_PER_WIDE_INT. */
2013 if ((GET_CODE (op0
) == CONST
2014 || GET_CODE (op0
) == SYMBOL_REF
2015 || GET_CODE (op0
) == LABEL_REF
)
2016 && CONST_INT_P (op1
))
2017 return plus_constant (mode
, op0
, INTVAL (op1
));
2018 else if ((GET_CODE (op1
) == CONST
2019 || GET_CODE (op1
) == SYMBOL_REF
2020 || GET_CODE (op1
) == LABEL_REF
)
2021 && CONST_INT_P (op0
))
2022 return plus_constant (mode
, op1
, INTVAL (op0
));
2024 /* See if this is something like X * C - X or vice versa or
2025 if the multiplication is written as a shift. If so, we can
2026 distribute and make a new multiply, shift, or maybe just
2027 have X (if C is 2 in the example above). But don't make
2028 something more expensive than we had before. */
2030 if (SCALAR_INT_MODE_P (mode
))
2032 rtx lhs
= op0
, rhs
= op1
;
2034 wide_int coeff0
= wi::one (GET_MODE_PRECISION (mode
));
2035 wide_int coeff1
= wi::one (GET_MODE_PRECISION (mode
));
2037 if (GET_CODE (lhs
) == NEG
)
2039 coeff0
= wi::minus_one (GET_MODE_PRECISION (mode
));
2040 lhs
= XEXP (lhs
, 0);
2042 else if (GET_CODE (lhs
) == MULT
2043 && CONST_SCALAR_INT_P (XEXP (lhs
, 1)))
2045 coeff0
= std::make_pair (XEXP (lhs
, 1), mode
);
2046 lhs
= XEXP (lhs
, 0);
2048 else if (GET_CODE (lhs
) == ASHIFT
2049 && CONST_INT_P (XEXP (lhs
, 1))
2050 && INTVAL (XEXP (lhs
, 1)) >= 0
2051 && INTVAL (XEXP (lhs
, 1)) < GET_MODE_PRECISION (mode
))
2053 coeff0
= wi::set_bit_in_zero (INTVAL (XEXP (lhs
, 1)),
2054 GET_MODE_PRECISION (mode
));
2055 lhs
= XEXP (lhs
, 0);
2058 if (GET_CODE (rhs
) == NEG
)
2060 coeff1
= wi::minus_one (GET_MODE_PRECISION (mode
));
2061 rhs
= XEXP (rhs
, 0);
2063 else if (GET_CODE (rhs
) == MULT
2064 && CONST_INT_P (XEXP (rhs
, 1)))
2066 coeff1
= std::make_pair (XEXP (rhs
, 1), mode
);
2067 rhs
= XEXP (rhs
, 0);
2069 else if (GET_CODE (rhs
) == ASHIFT
2070 && CONST_INT_P (XEXP (rhs
, 1))
2071 && INTVAL (XEXP (rhs
, 1)) >= 0
2072 && INTVAL (XEXP (rhs
, 1)) < GET_MODE_PRECISION (mode
))
2074 coeff1
= wi::set_bit_in_zero (INTVAL (XEXP (rhs
, 1)),
2075 GET_MODE_PRECISION (mode
));
2076 rhs
= XEXP (rhs
, 0);
2079 if (rtx_equal_p (lhs
, rhs
))
2081 rtx orig
= gen_rtx_PLUS (mode
, op0
, op1
);
2083 bool speed
= optimize_function_for_speed_p (cfun
);
2085 coeff
= immed_wide_int_const (coeff0
+ coeff1
, mode
);
2087 tem
= simplify_gen_binary (MULT
, mode
, lhs
, coeff
);
2088 return (set_src_cost (tem
, mode
, speed
)
2089 <= set_src_cost (orig
, mode
, speed
) ? tem
: 0);
2093 /* (plus (xor X C1) C2) is (xor X (C1^C2)) if C2 is signbit. */
2094 if (CONST_SCALAR_INT_P (op1
)
2095 && GET_CODE (op0
) == XOR
2096 && CONST_SCALAR_INT_P (XEXP (op0
, 1))
2097 && mode_signbit_p (mode
, op1
))
2098 return simplify_gen_binary (XOR
, mode
, XEXP (op0
, 0),
2099 simplify_gen_binary (XOR
, mode
, op1
,
2102 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)). */
2103 if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode
)
2104 && GET_CODE (op0
) == MULT
2105 && GET_CODE (XEXP (op0
, 0)) == NEG
)
2109 in1
= XEXP (XEXP (op0
, 0), 0);
2110 in2
= XEXP (op0
, 1);
2111 return simplify_gen_binary (MINUS
, mode
, op1
,
2112 simplify_gen_binary (MULT
, mode
,
2116 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
2117 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
2119 if (COMPARISON_P (op0
)
2120 && ((STORE_FLAG_VALUE
== -1 && trueop1
== const1_rtx
)
2121 || (STORE_FLAG_VALUE
== 1 && trueop1
== constm1_rtx
))
2122 && (reversed
= reversed_comparison (op0
, mode
)))
2124 simplify_gen_unary (NEG
, mode
, reversed
, mode
);
2126 /* If one of the operands is a PLUS or a MINUS, see if we can
2127 simplify this by the associative law.
2128 Don't use the associative law for floating point.
2129 The inaccuracy makes it nonassociative,
2130 and subtle programs can break if operations are associated. */
2132 if (INTEGRAL_MODE_P (mode
)
2133 && (plus_minus_operand_p (op0
)
2134 || plus_minus_operand_p (op1
))
2135 && (tem
= simplify_plus_minus (code
, mode
, op0
, op1
)) != 0)
2138 /* Reassociate floating point addition only when the user
2139 specifies associative math operations. */
2140 if (FLOAT_MODE_P (mode
)
2141 && flag_associative_math
)
2143 tem
= simplify_associative_operation (code
, mode
, op0
, op1
);
2150 /* Convert (compare (gt (flags) 0) (lt (flags) 0)) to (flags). */
2151 if (((GET_CODE (op0
) == GT
&& GET_CODE (op1
) == LT
)
2152 || (GET_CODE (op0
) == GTU
&& GET_CODE (op1
) == LTU
))
2153 && XEXP (op0
, 1) == const0_rtx
&& XEXP (op1
, 1) == const0_rtx
)
2155 rtx xop00
= XEXP (op0
, 0);
2156 rtx xop10
= XEXP (op1
, 0);
2158 if (GET_CODE (xop00
) == CC0
&& GET_CODE (xop10
) == CC0
)
2161 if (REG_P (xop00
) && REG_P (xop10
)
2162 && GET_MODE (xop00
) == GET_MODE (xop10
)
2163 && REGNO (xop00
) == REGNO (xop10
)
2164 && GET_MODE_CLASS (GET_MODE (xop00
)) == MODE_CC
2165 && GET_MODE_CLASS (GET_MODE (xop10
)) == MODE_CC
)
2171 /* We can't assume x-x is 0 even with non-IEEE floating point,
2172 but since it is zero except in very strange circumstances, we
2173 will treat it as zero with -ffinite-math-only. */
2174 if (rtx_equal_p (trueop0
, trueop1
)
2175 && ! side_effects_p (op0
)
2176 && (!FLOAT_MODE_P (mode
) || !HONOR_NANS (mode
)))
2177 return CONST0_RTX (mode
);
2179 /* Change subtraction from zero into negation. (0 - x) is the
2180 same as -x when x is NaN, infinite, or finite and nonzero.
2181 But if the mode has signed zeros, and does not round towards
2182 -infinity, then 0 - 0 is 0, not -0. */
2183 if (!HONOR_SIGNED_ZEROS (mode
) && trueop0
== CONST0_RTX (mode
))
2184 return simplify_gen_unary (NEG
, mode
, op1
, mode
);
2186 /* (-1 - a) is ~a. */
2187 if (trueop0
== constm1_rtx
)
2188 return simplify_gen_unary (NOT
, mode
, op1
, mode
);
2190 /* Subtracting 0 has no effect unless the mode has signed zeros
2191 and supports rounding towards -infinity. In such a case,
2193 if (!(HONOR_SIGNED_ZEROS (mode
)
2194 && HONOR_SIGN_DEPENDENT_ROUNDING (mode
))
2195 && trueop1
== CONST0_RTX (mode
))
2198 /* See if this is something like X * C - X or vice versa or
2199 if the multiplication is written as a shift. If so, we can
2200 distribute and make a new multiply, shift, or maybe just
2201 have X (if C is 2 in the example above). But don't make
2202 something more expensive than we had before. */
2204 if (SCALAR_INT_MODE_P (mode
))
2206 rtx lhs
= op0
, rhs
= op1
;
2208 wide_int coeff0
= wi::one (GET_MODE_PRECISION (mode
));
2209 wide_int negcoeff1
= wi::minus_one (GET_MODE_PRECISION (mode
));
2211 if (GET_CODE (lhs
) == NEG
)
2213 coeff0
= wi::minus_one (GET_MODE_PRECISION (mode
));
2214 lhs
= XEXP (lhs
, 0);
2216 else if (GET_CODE (lhs
) == MULT
2217 && CONST_SCALAR_INT_P (XEXP (lhs
, 1)))
2219 coeff0
= std::make_pair (XEXP (lhs
, 1), mode
);
2220 lhs
= XEXP (lhs
, 0);
2222 else if (GET_CODE (lhs
) == ASHIFT
2223 && CONST_INT_P (XEXP (lhs
, 1))
2224 && INTVAL (XEXP (lhs
, 1)) >= 0
2225 && INTVAL (XEXP (lhs
, 1)) < GET_MODE_PRECISION (mode
))
2227 coeff0
= wi::set_bit_in_zero (INTVAL (XEXP (lhs
, 1)),
2228 GET_MODE_PRECISION (mode
));
2229 lhs
= XEXP (lhs
, 0);
2232 if (GET_CODE (rhs
) == NEG
)
2234 negcoeff1
= wi::one (GET_MODE_PRECISION (mode
));
2235 rhs
= XEXP (rhs
, 0);
2237 else if (GET_CODE (rhs
) == MULT
2238 && CONST_INT_P (XEXP (rhs
, 1)))
2240 negcoeff1
= wi::neg (std::make_pair (XEXP (rhs
, 1), mode
));
2241 rhs
= XEXP (rhs
, 0);
2243 else if (GET_CODE (rhs
) == ASHIFT
2244 && CONST_INT_P (XEXP (rhs
, 1))
2245 && INTVAL (XEXP (rhs
, 1)) >= 0
2246 && INTVAL (XEXP (rhs
, 1)) < GET_MODE_PRECISION (mode
))
2248 negcoeff1
= wi::set_bit_in_zero (INTVAL (XEXP (rhs
, 1)),
2249 GET_MODE_PRECISION (mode
));
2250 negcoeff1
= -negcoeff1
;
2251 rhs
= XEXP (rhs
, 0);
2254 if (rtx_equal_p (lhs
, rhs
))
2256 rtx orig
= gen_rtx_MINUS (mode
, op0
, op1
);
2258 bool speed
= optimize_function_for_speed_p (cfun
);
2260 coeff
= immed_wide_int_const (coeff0
+ negcoeff1
, mode
);
2262 tem
= simplify_gen_binary (MULT
, mode
, lhs
, coeff
);
2263 return (set_src_cost (tem
, mode
, speed
)
2264 <= set_src_cost (orig
, mode
, speed
) ? tem
: 0);
2268 /* (a - (-b)) -> (a + b). True even for IEEE. */
2269 if (GET_CODE (op1
) == NEG
)
2270 return simplify_gen_binary (PLUS
, mode
, op0
, XEXP (op1
, 0));
2272 /* (-x - c) may be simplified as (-c - x). */
2273 if (GET_CODE (op0
) == NEG
2274 && (CONST_SCALAR_INT_P (op1
) || CONST_DOUBLE_AS_FLOAT_P (op1
)))
2276 tem
= simplify_unary_operation (NEG
, mode
, op1
, mode
);
2278 return simplify_gen_binary (MINUS
, mode
, tem
, XEXP (op0
, 0));
2281 /* Don't let a relocatable value get a negative coeff. */
2282 if (CONST_INT_P (op1
) && GET_MODE (op0
) != VOIDmode
)
2283 return simplify_gen_binary (PLUS
, mode
,
2285 neg_const_int (mode
, op1
));
2287 /* (x - (x & y)) -> (x & ~y) */
2288 if (INTEGRAL_MODE_P (mode
) && GET_CODE (op1
) == AND
)
2290 if (rtx_equal_p (op0
, XEXP (op1
, 0)))
2292 tem
= simplify_gen_unary (NOT
, mode
, XEXP (op1
, 1),
2293 GET_MODE (XEXP (op1
, 1)));
2294 return simplify_gen_binary (AND
, mode
, op0
, tem
);
2296 if (rtx_equal_p (op0
, XEXP (op1
, 1)))
2298 tem
= simplify_gen_unary (NOT
, mode
, XEXP (op1
, 0),
2299 GET_MODE (XEXP (op1
, 0)));
2300 return simplify_gen_binary (AND
, mode
, op0
, tem
);
2304 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
2305 by reversing the comparison code if valid. */
2306 if (STORE_FLAG_VALUE
== 1
2307 && trueop0
== const1_rtx
2308 && COMPARISON_P (op1
)
2309 && (reversed
= reversed_comparison (op1
, mode
)))
2312 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A). */
2313 if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode
)
2314 && GET_CODE (op1
) == MULT
2315 && GET_CODE (XEXP (op1
, 0)) == NEG
)
2319 in1
= XEXP (XEXP (op1
, 0), 0);
2320 in2
= XEXP (op1
, 1);
2321 return simplify_gen_binary (PLUS
, mode
,
2322 simplify_gen_binary (MULT
, mode
,
2327 /* Canonicalize (minus (neg A) (mult B C)) to
2328 (minus (mult (neg B) C) A). */
2329 if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode
)
2330 && GET_CODE (op1
) == MULT
2331 && GET_CODE (op0
) == NEG
)
2335 in1
= simplify_gen_unary (NEG
, mode
, XEXP (op1
, 0), mode
);
2336 in2
= XEXP (op1
, 1);
2337 return simplify_gen_binary (MINUS
, mode
,
2338 simplify_gen_binary (MULT
, mode
,
2343 /* If one of the operands is a PLUS or a MINUS, see if we can
2344 simplify this by the associative law. This will, for example,
2345 canonicalize (minus A (plus B C)) to (minus (minus A B) C).
2346 Don't use the associative law for floating point.
2347 The inaccuracy makes it nonassociative,
2348 and subtle programs can break if operations are associated. */
2350 if (INTEGRAL_MODE_P (mode
)
2351 && (plus_minus_operand_p (op0
)
2352 || plus_minus_operand_p (op1
))
2353 && (tem
= simplify_plus_minus (code
, mode
, op0
, op1
)) != 0)
2358 if (trueop1
== constm1_rtx
)
2359 return simplify_gen_unary (NEG
, mode
, op0
, mode
);
2361 if (GET_CODE (op0
) == NEG
)
2363 rtx temp
= simplify_unary_operation (NEG
, mode
, op1
, mode
);
2364 /* If op1 is a MULT as well and simplify_unary_operation
2365 just moved the NEG to the second operand, simplify_gen_binary
2366 below could through simplify_associative_operation move
2367 the NEG around again and recurse endlessly. */
2369 && GET_CODE (op1
) == MULT
2370 && GET_CODE (temp
) == MULT
2371 && XEXP (op1
, 0) == XEXP (temp
, 0)
2372 && GET_CODE (XEXP (temp
, 1)) == NEG
2373 && XEXP (op1
, 1) == XEXP (XEXP (temp
, 1), 0))
2376 return simplify_gen_binary (MULT
, mode
, XEXP (op0
, 0), temp
);
2378 if (GET_CODE (op1
) == NEG
)
2380 rtx temp
= simplify_unary_operation (NEG
, mode
, op0
, mode
);
2381 /* If op0 is a MULT as well and simplify_unary_operation
2382 just moved the NEG to the second operand, simplify_gen_binary
2383 below could through simplify_associative_operation move
2384 the NEG around again and recurse endlessly. */
2386 && GET_CODE (op0
) == MULT
2387 && GET_CODE (temp
) == MULT
2388 && XEXP (op0
, 0) == XEXP (temp
, 0)
2389 && GET_CODE (XEXP (temp
, 1)) == NEG
2390 && XEXP (op0
, 1) == XEXP (XEXP (temp
, 1), 0))
2393 return simplify_gen_binary (MULT
, mode
, temp
, XEXP (op1
, 0));
2396 /* Maybe simplify x * 0 to 0. The reduction is not valid if
2397 x is NaN, since x * 0 is then also NaN. Nor is it valid
2398 when the mode has signed zeros, since multiplying a negative
2399 number by 0 will give -0, not 0. */
2400 if (!HONOR_NANS (mode
)
2401 && !HONOR_SIGNED_ZEROS (mode
)
2402 && trueop1
== CONST0_RTX (mode
)
2403 && ! side_effects_p (op0
))
2406 /* In IEEE floating point, x*1 is not equivalent to x for
2408 if (!HONOR_SNANS (mode
)
2409 && trueop1
== CONST1_RTX (mode
))
2412 /* Convert multiply by constant power of two into shift. */
2413 if (CONST_SCALAR_INT_P (trueop1
))
2415 val
= wi::exact_log2 (std::make_pair (trueop1
, mode
));
2417 return simplify_gen_binary (ASHIFT
, mode
, op0
, GEN_INT (val
));
2420 /* x*2 is x+x and x*(-1) is -x */
2421 if (CONST_DOUBLE_AS_FLOAT_P (trueop1
)
2422 && SCALAR_FLOAT_MODE_P (GET_MODE (trueop1
))
2423 && !DECIMAL_FLOAT_MODE_P (GET_MODE (trueop1
))
2424 && GET_MODE (op0
) == mode
)
2427 REAL_VALUE_FROM_CONST_DOUBLE (d
, trueop1
);
2429 if (REAL_VALUES_EQUAL (d
, dconst2
))
2430 return simplify_gen_binary (PLUS
, mode
, op0
, copy_rtx (op0
));
2432 if (!HONOR_SNANS (mode
)
2433 && REAL_VALUES_EQUAL (d
, dconstm1
))
2434 return simplify_gen_unary (NEG
, mode
, op0
, mode
);
2437 /* Optimize -x * -x as x * x. */
2438 if (FLOAT_MODE_P (mode
)
2439 && GET_CODE (op0
) == NEG
2440 && GET_CODE (op1
) == NEG
2441 && rtx_equal_p (XEXP (op0
, 0), XEXP (op1
, 0))
2442 && !side_effects_p (XEXP (op0
, 0)))
2443 return simplify_gen_binary (MULT
, mode
, XEXP (op0
, 0), XEXP (op1
, 0));
2445 /* Likewise, optimize abs(x) * abs(x) as x * x. */
2446 if (SCALAR_FLOAT_MODE_P (mode
)
2447 && GET_CODE (op0
) == ABS
2448 && GET_CODE (op1
) == ABS
2449 && rtx_equal_p (XEXP (op0
, 0), XEXP (op1
, 0))
2450 && !side_effects_p (XEXP (op0
, 0)))
2451 return simplify_gen_binary (MULT
, mode
, XEXP (op0
, 0), XEXP (op1
, 0));
2453 /* Reassociate multiplication, but for floating point MULTs
2454 only when the user specifies unsafe math optimizations. */
2455 if (! FLOAT_MODE_P (mode
)
2456 || flag_unsafe_math_optimizations
)
2458 tem
= simplify_associative_operation (code
, mode
, op0
, op1
);
2465 if (trueop1
== CONST0_RTX (mode
))
2467 if (INTEGRAL_MODE_P (mode
)
2468 && trueop1
== CONSTM1_RTX (mode
)
2469 && !side_effects_p (op0
))
2471 if (rtx_equal_p (trueop0
, trueop1
) && ! side_effects_p (op0
))
2473 /* A | (~A) -> -1 */
2474 if (((GET_CODE (op0
) == NOT
&& rtx_equal_p (XEXP (op0
, 0), op1
))
2475 || (GET_CODE (op1
) == NOT
&& rtx_equal_p (XEXP (op1
, 0), op0
)))
2476 && ! side_effects_p (op0
)
2477 && SCALAR_INT_MODE_P (mode
))
2480 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
2481 if (CONST_INT_P (op1
)
2482 && HWI_COMPUTABLE_MODE_P (mode
)
2483 && (nonzero_bits (op0
, mode
) & ~UINTVAL (op1
)) == 0
2484 && !side_effects_p (op0
))
2487 /* Canonicalize (X & C1) | C2. */
2488 if (GET_CODE (op0
) == AND
2489 && CONST_INT_P (trueop1
)
2490 && CONST_INT_P (XEXP (op0
, 1)))
2492 HOST_WIDE_INT mask
= GET_MODE_MASK (mode
);
2493 HOST_WIDE_INT c1
= INTVAL (XEXP (op0
, 1));
2494 HOST_WIDE_INT c2
= INTVAL (trueop1
);
2496 /* If (C1&C2) == C1, then (X&C1)|C2 becomes X. */
2498 && !side_effects_p (XEXP (op0
, 0)))
2501 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
2502 if (((c1
|c2
) & mask
) == mask
)
2503 return simplify_gen_binary (IOR
, mode
, XEXP (op0
, 0), op1
);
2505 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2. */
2506 if (((c1
& ~c2
) & mask
) != (c1
& mask
))
2508 tem
= simplify_gen_binary (AND
, mode
, XEXP (op0
, 0),
2509 gen_int_mode (c1
& ~c2
, mode
));
2510 return simplify_gen_binary (IOR
, mode
, tem
, op1
);
2514 /* Convert (A & B) | A to A. */
2515 if (GET_CODE (op0
) == AND
2516 && (rtx_equal_p (XEXP (op0
, 0), op1
)
2517 || rtx_equal_p (XEXP (op0
, 1), op1
))
2518 && ! side_effects_p (XEXP (op0
, 0))
2519 && ! side_effects_p (XEXP (op0
, 1)))
2522 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
2523 mode size to (rotate A CX). */
2525 if (GET_CODE (op1
) == ASHIFT
2526 || GET_CODE (op1
) == SUBREG
)
2537 if (GET_CODE (opleft
) == ASHIFT
&& GET_CODE (opright
) == LSHIFTRT
2538 && rtx_equal_p (XEXP (opleft
, 0), XEXP (opright
, 0))
2539 && CONST_INT_P (XEXP (opleft
, 1))
2540 && CONST_INT_P (XEXP (opright
, 1))
2541 && (INTVAL (XEXP (opleft
, 1)) + INTVAL (XEXP (opright
, 1))
2542 == GET_MODE_PRECISION (mode
)))
2543 return gen_rtx_ROTATE (mode
, XEXP (opright
, 0), XEXP (opleft
, 1));
2545 /* Same, but for ashift that has been "simplified" to a wider mode
2546 by simplify_shift_const. */
2548 if (GET_CODE (opleft
) == SUBREG
2549 && GET_CODE (SUBREG_REG (opleft
)) == ASHIFT
2550 && GET_CODE (opright
) == LSHIFTRT
2551 && GET_CODE (XEXP (opright
, 0)) == SUBREG
2552 && GET_MODE (opleft
) == GET_MODE (XEXP (opright
, 0))
2553 && SUBREG_BYTE (opleft
) == SUBREG_BYTE (XEXP (opright
, 0))
2554 && (GET_MODE_SIZE (GET_MODE (opleft
))
2555 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (opleft
))))
2556 && rtx_equal_p (XEXP (SUBREG_REG (opleft
), 0),
2557 SUBREG_REG (XEXP (opright
, 0)))
2558 && CONST_INT_P (XEXP (SUBREG_REG (opleft
), 1))
2559 && CONST_INT_P (XEXP (opright
, 1))
2560 && (INTVAL (XEXP (SUBREG_REG (opleft
), 1)) + INTVAL (XEXP (opright
, 1))
2561 == GET_MODE_PRECISION (mode
)))
2562 return gen_rtx_ROTATE (mode
, XEXP (opright
, 0),
2563 XEXP (SUBREG_REG (opleft
), 1));
2565 /* If we have (ior (and (X C1) C2)), simplify this by making
2566 C1 as small as possible if C1 actually changes. */
2567 if (CONST_INT_P (op1
)
2568 && (HWI_COMPUTABLE_MODE_P (mode
)
2569 || INTVAL (op1
) > 0)
2570 && GET_CODE (op0
) == AND
2571 && CONST_INT_P (XEXP (op0
, 1))
2572 && CONST_INT_P (op1
)
2573 && (UINTVAL (XEXP (op0
, 1)) & UINTVAL (op1
)) != 0)
2575 rtx tmp
= simplify_gen_binary (AND
, mode
, XEXP (op0
, 0),
2576 gen_int_mode (UINTVAL (XEXP (op0
, 1))
2579 return simplify_gen_binary (IOR
, mode
, tmp
, op1
);
2582 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
2583 a (sign_extend (plus ...)). Then check if OP1 is a CONST_INT and
2584 the PLUS does not affect any of the bits in OP1: then we can do
2585 the IOR as a PLUS and we can associate. This is valid if OP1
2586 can be safely shifted left C bits. */
2587 if (CONST_INT_P (trueop1
) && GET_CODE (op0
) == ASHIFTRT
2588 && GET_CODE (XEXP (op0
, 0)) == PLUS
2589 && CONST_INT_P (XEXP (XEXP (op0
, 0), 1))
2590 && CONST_INT_P (XEXP (op0
, 1))
2591 && INTVAL (XEXP (op0
, 1)) < HOST_BITS_PER_WIDE_INT
)
2593 int count
= INTVAL (XEXP (op0
, 1));
2594 HOST_WIDE_INT mask
= INTVAL (trueop1
) << count
;
2596 if (mask
>> count
== INTVAL (trueop1
)
2597 && trunc_int_for_mode (mask
, mode
) == mask
2598 && (mask
& nonzero_bits (XEXP (op0
, 0), mode
)) == 0)
2599 return simplify_gen_binary (ASHIFTRT
, mode
,
2600 plus_constant (mode
, XEXP (op0
, 0),
2605 tem
= simplify_byte_swapping_operation (code
, mode
, op0
, op1
);
2609 tem
= simplify_associative_operation (code
, mode
, op0
, op1
);
2615 if (trueop1
== CONST0_RTX (mode
))
2617 if (INTEGRAL_MODE_P (mode
) && trueop1
== CONSTM1_RTX (mode
))
2618 return simplify_gen_unary (NOT
, mode
, op0
, mode
);
2619 if (rtx_equal_p (trueop0
, trueop1
)
2620 && ! side_effects_p (op0
)
2621 && GET_MODE_CLASS (mode
) != MODE_CC
)
2622 return CONST0_RTX (mode
);
2624 /* Canonicalize XOR of the most significant bit to PLUS. */
2625 if (CONST_SCALAR_INT_P (op1
)
2626 && mode_signbit_p (mode
, op1
))
2627 return simplify_gen_binary (PLUS
, mode
, op0
, op1
);
2628 /* (xor (plus X C1) C2) is (xor X (C1^C2)) if C1 is signbit. */
2629 if (CONST_SCALAR_INT_P (op1
)
2630 && GET_CODE (op0
) == PLUS
2631 && CONST_SCALAR_INT_P (XEXP (op0
, 1))
2632 && mode_signbit_p (mode
, XEXP (op0
, 1)))
2633 return simplify_gen_binary (XOR
, mode
, XEXP (op0
, 0),
2634 simplify_gen_binary (XOR
, mode
, op1
,
2637 /* If we are XORing two things that have no bits in common,
2638 convert them into an IOR. This helps to detect rotation encoded
2639 using those methods and possibly other simplifications. */
2641 if (HWI_COMPUTABLE_MODE_P (mode
)
2642 && (nonzero_bits (op0
, mode
)
2643 & nonzero_bits (op1
, mode
)) == 0)
2644 return (simplify_gen_binary (IOR
, mode
, op0
, op1
));
2646 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
2647 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
2650 int num_negated
= 0;
2652 if (GET_CODE (op0
) == NOT
)
2653 num_negated
++, op0
= XEXP (op0
, 0);
2654 if (GET_CODE (op1
) == NOT
)
2655 num_negated
++, op1
= XEXP (op1
, 0);
2657 if (num_negated
== 2)
2658 return simplify_gen_binary (XOR
, mode
, op0
, op1
);
2659 else if (num_negated
== 1)
2660 return simplify_gen_unary (NOT
, mode
,
2661 simplify_gen_binary (XOR
, mode
, op0
, op1
),
2665 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
2666 correspond to a machine insn or result in further simplifications
2667 if B is a constant. */
2669 if (GET_CODE (op0
) == AND
2670 && rtx_equal_p (XEXP (op0
, 1), op1
)
2671 && ! side_effects_p (op1
))
2672 return simplify_gen_binary (AND
, mode
,
2673 simplify_gen_unary (NOT
, mode
,
2674 XEXP (op0
, 0), mode
),
2677 else if (GET_CODE (op0
) == AND
2678 && rtx_equal_p (XEXP (op0
, 0), op1
)
2679 && ! side_effects_p (op1
))
2680 return simplify_gen_binary (AND
, mode
,
2681 simplify_gen_unary (NOT
, mode
,
2682 XEXP (op0
, 1), mode
),
2685 /* Given (xor (ior (xor A B) C) D), where B, C and D are
2686 constants, simplify to (xor (ior A C) (B&~C)^D), canceling
2687 out bits inverted twice and not set by C. Similarly, given
2688 (xor (and (xor A B) C) D), simplify without inverting C in
2689 the xor operand: (xor (and A C) (B&C)^D).
2691 else if ((GET_CODE (op0
) == IOR
|| GET_CODE (op0
) == AND
)
2692 && GET_CODE (XEXP (op0
, 0)) == XOR
2693 && CONST_INT_P (op1
)
2694 && CONST_INT_P (XEXP (op0
, 1))
2695 && CONST_INT_P (XEXP (XEXP (op0
, 0), 1)))
2697 enum rtx_code op
= GET_CODE (op0
);
2698 rtx a
= XEXP (XEXP (op0
, 0), 0);
2699 rtx b
= XEXP (XEXP (op0
, 0), 1);
2700 rtx c
= XEXP (op0
, 1);
2702 HOST_WIDE_INT bval
= INTVAL (b
);
2703 HOST_WIDE_INT cval
= INTVAL (c
);
2704 HOST_WIDE_INT dval
= INTVAL (d
);
2705 HOST_WIDE_INT xcval
;
2712 return simplify_gen_binary (XOR
, mode
,
2713 simplify_gen_binary (op
, mode
, a
, c
),
2714 gen_int_mode ((bval
& xcval
) ^ dval
,
2718 /* Given (xor (and A B) C), using P^Q == (~P&Q) | (~Q&P),
2719 we can transform like this:
2720 (A&B)^C == ~(A&B)&C | ~C&(A&B)
2721 == (~A|~B)&C | ~C&(A&B) * DeMorgan's Law
2722 == ~A&C | ~B&C | A&(~C&B) * Distribute and re-order
2723 Attempt a few simplifications when B and C are both constants. */
2724 if (GET_CODE (op0
) == AND
2725 && CONST_INT_P (op1
)
2726 && CONST_INT_P (XEXP (op0
, 1)))
2728 rtx a
= XEXP (op0
, 0);
2729 rtx b
= XEXP (op0
, 1);
2731 HOST_WIDE_INT bval
= INTVAL (b
);
2732 HOST_WIDE_INT cval
= INTVAL (c
);
2734 /* Instead of computing ~A&C, we compute its negated value,
2735 ~(A|~C). If it yields -1, ~A&C is zero, so we can
2736 optimize for sure. If it does not simplify, we still try
2737 to compute ~A&C below, but since that always allocates
2738 RTL, we don't try that before committing to returning a
2739 simplified expression. */
2740 rtx n_na_c
= simplify_binary_operation (IOR
, mode
, a
,
2743 if ((~cval
& bval
) == 0)
2745 rtx na_c
= NULL_RTX
;
2747 na_c
= simplify_gen_unary (NOT
, mode
, n_na_c
, mode
);
2750 /* If ~A does not simplify, don't bother: we don't
2751 want to simplify 2 operations into 3, and if na_c
2752 were to simplify with na, n_na_c would have
2753 simplified as well. */
2754 rtx na
= simplify_unary_operation (NOT
, mode
, a
, mode
);
2756 na_c
= simplify_gen_binary (AND
, mode
, na
, c
);
2759 /* Try to simplify ~A&C | ~B&C. */
2760 if (na_c
!= NULL_RTX
)
2761 return simplify_gen_binary (IOR
, mode
, na_c
,
2762 gen_int_mode (~bval
& cval
, mode
));
2766 /* If ~A&C is zero, simplify A&(~C&B) | ~B&C. */
2767 if (n_na_c
== CONSTM1_RTX (mode
))
2769 rtx a_nc_b
= simplify_gen_binary (AND
, mode
, a
,
2770 gen_int_mode (~cval
& bval
,
2772 return simplify_gen_binary (IOR
, mode
, a_nc_b
,
2773 gen_int_mode (~bval
& cval
,
2779 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
2780 comparison if STORE_FLAG_VALUE is 1. */
2781 if (STORE_FLAG_VALUE
== 1
2782 && trueop1
== const1_rtx
2783 && COMPARISON_P (op0
)
2784 && (reversed
= reversed_comparison (op0
, mode
)))
2787 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
2788 is (lt foo (const_int 0)), so we can perform the above
2789 simplification if STORE_FLAG_VALUE is 1. */
2791 if (STORE_FLAG_VALUE
== 1
2792 && trueop1
== const1_rtx
2793 && GET_CODE (op0
) == LSHIFTRT
2794 && CONST_INT_P (XEXP (op0
, 1))
2795 && INTVAL (XEXP (op0
, 1)) == GET_MODE_PRECISION (mode
) - 1)
2796 return gen_rtx_GE (mode
, XEXP (op0
, 0), const0_rtx
);
2798 /* (xor (comparison foo bar) (const_int sign-bit))
2799 when STORE_FLAG_VALUE is the sign bit. */
2800 if (val_signbit_p (mode
, STORE_FLAG_VALUE
)
2801 && trueop1
== const_true_rtx
2802 && COMPARISON_P (op0
)
2803 && (reversed
= reversed_comparison (op0
, mode
)))
2806 tem
= simplify_byte_swapping_operation (code
, mode
, op0
, op1
);
2810 tem
= simplify_associative_operation (code
, mode
, op0
, op1
);
2816 if (trueop1
== CONST0_RTX (mode
) && ! side_effects_p (op0
))
2818 if (INTEGRAL_MODE_P (mode
) && trueop1
== CONSTM1_RTX (mode
))
2820 if (HWI_COMPUTABLE_MODE_P (mode
))
2822 HOST_WIDE_INT nzop0
= nonzero_bits (trueop0
, mode
);
2823 HOST_WIDE_INT nzop1
;
2824 if (CONST_INT_P (trueop1
))
2826 HOST_WIDE_INT val1
= INTVAL (trueop1
);
2827 /* If we are turning off bits already known off in OP0, we need
2829 if ((nzop0
& ~val1
) == 0)
2832 nzop1
= nonzero_bits (trueop1
, mode
);
2833 /* If we are clearing all the nonzero bits, the result is zero. */
2834 if ((nzop1
& nzop0
) == 0
2835 && !side_effects_p (op0
) && !side_effects_p (op1
))
2836 return CONST0_RTX (mode
);
2838 if (rtx_equal_p (trueop0
, trueop1
) && ! side_effects_p (op0
)
2839 && GET_MODE_CLASS (mode
) != MODE_CC
)
2842 if (((GET_CODE (op0
) == NOT
&& rtx_equal_p (XEXP (op0
, 0), op1
))
2843 || (GET_CODE (op1
) == NOT
&& rtx_equal_p (XEXP (op1
, 0), op0
)))
2844 && ! side_effects_p (op0
)
2845 && GET_MODE_CLASS (mode
) != MODE_CC
)
2846 return CONST0_RTX (mode
);
2848 /* Transform (and (extend X) C) into (zero_extend (and X C)) if
2849 there are no nonzero bits of C outside of X's mode. */
2850 if ((GET_CODE (op0
) == SIGN_EXTEND
2851 || GET_CODE (op0
) == ZERO_EXTEND
)
2852 && CONST_INT_P (trueop1
)
2853 && HWI_COMPUTABLE_MODE_P (mode
)
2854 && (~GET_MODE_MASK (GET_MODE (XEXP (op0
, 0)))
2855 & UINTVAL (trueop1
)) == 0)
2857 machine_mode imode
= GET_MODE (XEXP (op0
, 0));
2858 tem
= simplify_gen_binary (AND
, imode
, XEXP (op0
, 0),
2859 gen_int_mode (INTVAL (trueop1
),
2861 return simplify_gen_unary (ZERO_EXTEND
, mode
, tem
, imode
);
2864 /* Transform (and (truncate X) C) into (truncate (and X C)). This way
2865 we might be able to further simplify the AND with X and potentially
2866 remove the truncation altogether. */
2867 if (GET_CODE (op0
) == TRUNCATE
&& CONST_INT_P (trueop1
))
2869 rtx x
= XEXP (op0
, 0);
2870 machine_mode xmode
= GET_MODE (x
);
2871 tem
= simplify_gen_binary (AND
, xmode
, x
,
2872 gen_int_mode (INTVAL (trueop1
), xmode
));
2873 return simplify_gen_unary (TRUNCATE
, mode
, tem
, xmode
);
2876 /* Canonicalize (A | C1) & C2 as (A & C2) | (C1 & C2). */
2877 if (GET_CODE (op0
) == IOR
2878 && CONST_INT_P (trueop1
)
2879 && CONST_INT_P (XEXP (op0
, 1)))
2881 HOST_WIDE_INT tmp
= INTVAL (trueop1
) & INTVAL (XEXP (op0
, 1));
2882 return simplify_gen_binary (IOR
, mode
,
2883 simplify_gen_binary (AND
, mode
,
2884 XEXP (op0
, 0), op1
),
2885 gen_int_mode (tmp
, mode
));
2888 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
2889 insn (and may simplify more). */
2890 if (GET_CODE (op0
) == XOR
2891 && rtx_equal_p (XEXP (op0
, 0), op1
)
2892 && ! side_effects_p (op1
))
2893 return simplify_gen_binary (AND
, mode
,
2894 simplify_gen_unary (NOT
, mode
,
2895 XEXP (op0
, 1), mode
),
2898 if (GET_CODE (op0
) == XOR
2899 && rtx_equal_p (XEXP (op0
, 1), op1
)
2900 && ! side_effects_p (op1
))
2901 return simplify_gen_binary (AND
, mode
,
2902 simplify_gen_unary (NOT
, mode
,
2903 XEXP (op0
, 0), mode
),
2906 /* Similarly for (~(A ^ B)) & A. */
2907 if (GET_CODE (op0
) == NOT
2908 && GET_CODE (XEXP (op0
, 0)) == XOR
2909 && rtx_equal_p (XEXP (XEXP (op0
, 0), 0), op1
)
2910 && ! side_effects_p (op1
))
2911 return simplify_gen_binary (AND
, mode
, XEXP (XEXP (op0
, 0), 1), op1
);
2913 if (GET_CODE (op0
) == NOT
2914 && GET_CODE (XEXP (op0
, 0)) == XOR
2915 && rtx_equal_p (XEXP (XEXP (op0
, 0), 1), op1
)
2916 && ! side_effects_p (op1
))
2917 return simplify_gen_binary (AND
, mode
, XEXP (XEXP (op0
, 0), 0), op1
);
2919 /* Convert (A | B) & A to A. */
2920 if (GET_CODE (op0
) == IOR
2921 && (rtx_equal_p (XEXP (op0
, 0), op1
)
2922 || rtx_equal_p (XEXP (op0
, 1), op1
))
2923 && ! side_effects_p (XEXP (op0
, 0))
2924 && ! side_effects_p (XEXP (op0
, 1)))
2927 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
2928 ((A & N) + B) & M -> (A + B) & M
2929 Similarly if (N & M) == 0,
2930 ((A | N) + B) & M -> (A + B) & M
2931 and for - instead of + and/or ^ instead of |.
2932 Also, if (N & M) == 0, then
2933 (A +- N) & M -> A & M. */
2934 if (CONST_INT_P (trueop1
)
2935 && HWI_COMPUTABLE_MODE_P (mode
)
2936 && ~UINTVAL (trueop1
)
2937 && (UINTVAL (trueop1
) & (UINTVAL (trueop1
) + 1)) == 0
2938 && (GET_CODE (op0
) == PLUS
|| GET_CODE (op0
) == MINUS
))
2943 pmop
[0] = XEXP (op0
, 0);
2944 pmop
[1] = XEXP (op0
, 1);
2946 if (CONST_INT_P (pmop
[1])
2947 && (UINTVAL (pmop
[1]) & UINTVAL (trueop1
)) == 0)
2948 return simplify_gen_binary (AND
, mode
, pmop
[0], op1
);
2950 for (which
= 0; which
< 2; which
++)
2953 switch (GET_CODE (tem
))
2956 if (CONST_INT_P (XEXP (tem
, 1))
2957 && (UINTVAL (XEXP (tem
, 1)) & UINTVAL (trueop1
))
2958 == UINTVAL (trueop1
))
2959 pmop
[which
] = XEXP (tem
, 0);
2963 if (CONST_INT_P (XEXP (tem
, 1))
2964 && (UINTVAL (XEXP (tem
, 1)) & UINTVAL (trueop1
)) == 0)
2965 pmop
[which
] = XEXP (tem
, 0);
2972 if (pmop
[0] != XEXP (op0
, 0) || pmop
[1] != XEXP (op0
, 1))
2974 tem
= simplify_gen_binary (GET_CODE (op0
), mode
,
2976 return simplify_gen_binary (code
, mode
, tem
, op1
);
2980 /* (and X (ior (not X) Y) -> (and X Y) */
2981 if (GET_CODE (op1
) == IOR
2982 && GET_CODE (XEXP (op1
, 0)) == NOT
2983 && rtx_equal_p (op0
, XEXP (XEXP (op1
, 0), 0)))
2984 return simplify_gen_binary (AND
, mode
, op0
, XEXP (op1
, 1));
2986 /* (and (ior (not X) Y) X) -> (and X Y) */
2987 if (GET_CODE (op0
) == IOR
2988 && GET_CODE (XEXP (op0
, 0)) == NOT
2989 && rtx_equal_p (op1
, XEXP (XEXP (op0
, 0), 0)))
2990 return simplify_gen_binary (AND
, mode
, op1
, XEXP (op0
, 1));
2992 /* (and X (ior Y (not X)) -> (and X Y) */
2993 if (GET_CODE (op1
) == IOR
2994 && GET_CODE (XEXP (op1
, 1)) == NOT
2995 && rtx_equal_p (op0
, XEXP (XEXP (op1
, 1), 0)))
2996 return simplify_gen_binary (AND
, mode
, op0
, XEXP (op1
, 0));
2998 /* (and (ior Y (not X)) X) -> (and X Y) */
2999 if (GET_CODE (op0
) == IOR
3000 && GET_CODE (XEXP (op0
, 1)) == NOT
3001 && rtx_equal_p (op1
, XEXP (XEXP (op0
, 1), 0)))
3002 return simplify_gen_binary (AND
, mode
, op1
, XEXP (op0
, 0));
3004 tem
= simplify_byte_swapping_operation (code
, mode
, op0
, op1
);
3008 tem
= simplify_associative_operation (code
, mode
, op0
, op1
);
3014 /* 0/x is 0 (or x&0 if x has side-effects). */
3015 if (trueop0
== CONST0_RTX (mode
))
3017 if (side_effects_p (op1
))
3018 return simplify_gen_binary (AND
, mode
, op1
, trueop0
);
3022 if (trueop1
== CONST1_RTX (mode
))
3024 tem
= rtl_hooks
.gen_lowpart_no_emit (mode
, op0
);
3028 /* Convert divide by power of two into shift. */
3029 if (CONST_INT_P (trueop1
)
3030 && (val
= exact_log2 (UINTVAL (trueop1
))) > 0)
3031 return simplify_gen_binary (LSHIFTRT
, mode
, op0
, GEN_INT (val
));
3035 /* Handle floating point and integers separately. */
3036 if (SCALAR_FLOAT_MODE_P (mode
))
3038 /* Maybe change 0.0 / x to 0.0. This transformation isn't
3039 safe for modes with NaNs, since 0.0 / 0.0 will then be
3040 NaN rather than 0.0. Nor is it safe for modes with signed
3041 zeros, since dividing 0 by a negative number gives -0.0 */
3042 if (trueop0
== CONST0_RTX (mode
)
3043 && !HONOR_NANS (mode
)
3044 && !HONOR_SIGNED_ZEROS (mode
)
3045 && ! side_effects_p (op1
))
3048 if (trueop1
== CONST1_RTX (mode
)
3049 && !HONOR_SNANS (mode
))
3052 if (CONST_DOUBLE_AS_FLOAT_P (trueop1
)
3053 && trueop1
!= CONST0_RTX (mode
))
3056 REAL_VALUE_FROM_CONST_DOUBLE (d
, trueop1
);
3059 if (REAL_VALUES_EQUAL (d
, dconstm1
)
3060 && !HONOR_SNANS (mode
))
3061 return simplify_gen_unary (NEG
, mode
, op0
, mode
);
3063 /* Change FP division by a constant into multiplication.
3064 Only do this with -freciprocal-math. */
3065 if (flag_reciprocal_math
3066 && !REAL_VALUES_EQUAL (d
, dconst0
))
3068 REAL_ARITHMETIC (d
, RDIV_EXPR
, dconst1
, d
);
3069 tem
= CONST_DOUBLE_FROM_REAL_VALUE (d
, mode
);
3070 return simplify_gen_binary (MULT
, mode
, op0
, tem
);
3074 else if (SCALAR_INT_MODE_P (mode
))
3076 /* 0/x is 0 (or x&0 if x has side-effects). */
3077 if (trueop0
== CONST0_RTX (mode
)
3078 && !cfun
->can_throw_non_call_exceptions
)
3080 if (side_effects_p (op1
))
3081 return simplify_gen_binary (AND
, mode
, op1
, trueop0
);
3085 if (trueop1
== CONST1_RTX (mode
))
3087 tem
= rtl_hooks
.gen_lowpart_no_emit (mode
, op0
);
3092 if (trueop1
== constm1_rtx
)
3094 rtx x
= rtl_hooks
.gen_lowpart_no_emit (mode
, op0
);
3096 return simplify_gen_unary (NEG
, mode
, x
, mode
);
3102 /* 0%x is 0 (or x&0 if x has side-effects). */
3103 if (trueop0
== CONST0_RTX (mode
))
3105 if (side_effects_p (op1
))
3106 return simplify_gen_binary (AND
, mode
, op1
, trueop0
);
3109 /* x%1 is 0 (of x&0 if x has side-effects). */
3110 if (trueop1
== CONST1_RTX (mode
))
3112 if (side_effects_p (op0
))
3113 return simplify_gen_binary (AND
, mode
, op0
, CONST0_RTX (mode
));
3114 return CONST0_RTX (mode
);
3116 /* Implement modulus by power of two as AND. */
3117 if (CONST_INT_P (trueop1
)
3118 && exact_log2 (UINTVAL (trueop1
)) > 0)
3119 return simplify_gen_binary (AND
, mode
, op0
,
3120 gen_int_mode (INTVAL (op1
) - 1, mode
));
3124 /* 0%x is 0 (or x&0 if x has side-effects). */
3125 if (trueop0
== CONST0_RTX (mode
))
3127 if (side_effects_p (op1
))
3128 return simplify_gen_binary (AND
, mode
, op1
, trueop0
);
3131 /* x%1 and x%-1 is 0 (or x&0 if x has side-effects). */
3132 if (trueop1
== CONST1_RTX (mode
) || trueop1
== constm1_rtx
)
3134 if (side_effects_p (op0
))
3135 return simplify_gen_binary (AND
, mode
, op0
, CONST0_RTX (mode
));
3136 return CONST0_RTX (mode
);
3142 /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
3143 prefer left rotation, if op1 is from bitsize / 2 + 1 to
3144 bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
3146 #if defined(HAVE_rotate) && defined(HAVE_rotatert)
3147 if (CONST_INT_P (trueop1
)
3148 && IN_RANGE (INTVAL (trueop1
),
3149 GET_MODE_PRECISION (mode
) / 2 + (code
== ROTATE
),
3150 GET_MODE_PRECISION (mode
) - 1))
3151 return simplify_gen_binary (code
== ROTATE
? ROTATERT
: ROTATE
,
3152 mode
, op0
, GEN_INT (GET_MODE_PRECISION (mode
)
3153 - INTVAL (trueop1
)));
3157 if (trueop1
== CONST0_RTX (mode
))
3159 if (trueop0
== CONST0_RTX (mode
) && ! side_effects_p (op1
))
3161 /* Rotating ~0 always results in ~0. */
3162 if (CONST_INT_P (trueop0
) && width
<= HOST_BITS_PER_WIDE_INT
3163 && UINTVAL (trueop0
) == GET_MODE_MASK (mode
)
3164 && ! side_effects_p (op1
))
3168 scalar constants c1, c2
3169 size (M2) > size (M1)
3170 c1 == size (M2) - size (M1)
3172 (ashiftrt:M1 (subreg:M1 (lshiftrt:M2 (reg:M2) (const_int <c1>))
3176 (subreg:M1 (ashiftrt:M2 (reg:M2) (const_int <c1 + c2>))
3178 if (code
== ASHIFTRT
3179 && !VECTOR_MODE_P (mode
)
3181 && CONST_INT_P (op1
)
3182 && GET_CODE (SUBREG_REG (op0
)) == LSHIFTRT
3183 && !VECTOR_MODE_P (GET_MODE (SUBREG_REG (op0
)))
3184 && CONST_INT_P (XEXP (SUBREG_REG (op0
), 1))
3185 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0
)))
3186 > GET_MODE_BITSIZE (mode
))
3187 && (INTVAL (XEXP (SUBREG_REG (op0
), 1))
3188 == (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0
)))
3189 - GET_MODE_BITSIZE (mode
)))
3190 && subreg_lowpart_p (op0
))
3192 rtx tmp
= GEN_INT (INTVAL (XEXP (SUBREG_REG (op0
), 1))
3194 machine_mode inner_mode
= GET_MODE (SUBREG_REG (op0
));
3195 tmp
= simplify_gen_binary (ASHIFTRT
,
3196 GET_MODE (SUBREG_REG (op0
)),
3197 XEXP (SUBREG_REG (op0
), 0),
3199 return simplify_gen_subreg (mode
, tmp
, inner_mode
,
3200 subreg_lowpart_offset (mode
,
3204 if (SHIFT_COUNT_TRUNCATED
&& CONST_INT_P (op1
))
3206 val
= INTVAL (op1
) & (GET_MODE_PRECISION (mode
) - 1);
3207 if (val
!= INTVAL (op1
))
3208 return simplify_gen_binary (code
, mode
, op0
, GEN_INT (val
));
3215 if (trueop1
== CONST0_RTX (mode
))
3217 if (trueop0
== CONST0_RTX (mode
) && ! side_effects_p (op1
))
3219 goto canonicalize_shift
;
3222 if (trueop1
== CONST0_RTX (mode
))
3224 if (trueop0
== CONST0_RTX (mode
) && ! side_effects_p (op1
))
3226 /* Optimize (lshiftrt (clz X) C) as (eq X 0). */
3227 if (GET_CODE (op0
) == CLZ
3228 && CONST_INT_P (trueop1
)
3229 && STORE_FLAG_VALUE
== 1
3230 && INTVAL (trueop1
) < (HOST_WIDE_INT
)width
)
3232 machine_mode imode
= GET_MODE (XEXP (op0
, 0));
3233 unsigned HOST_WIDE_INT zero_val
= 0;
3235 if (CLZ_DEFINED_VALUE_AT_ZERO (imode
, zero_val
)
3236 && zero_val
== GET_MODE_PRECISION (imode
)
3237 && INTVAL (trueop1
) == exact_log2 (zero_val
))
3238 return simplify_gen_relational (EQ
, mode
, imode
,
3239 XEXP (op0
, 0), const0_rtx
);
3241 goto canonicalize_shift
;
3244 if (width
<= HOST_BITS_PER_WIDE_INT
3245 && mode_signbit_p (mode
, trueop1
)
3246 && ! side_effects_p (op0
))
3248 if (rtx_equal_p (trueop0
, trueop1
) && ! side_effects_p (op0
))
3250 tem
= simplify_associative_operation (code
, mode
, op0
, op1
);
3256 if (width
<= HOST_BITS_PER_WIDE_INT
3257 && CONST_INT_P (trueop1
)
3258 && (UINTVAL (trueop1
) == GET_MODE_MASK (mode
) >> 1)
3259 && ! side_effects_p (op0
))
3261 if (rtx_equal_p (trueop0
, trueop1
) && ! side_effects_p (op0
))
3263 tem
= simplify_associative_operation (code
, mode
, op0
, op1
);
3269 if (trueop1
== CONST0_RTX (mode
) && ! side_effects_p (op0
))
3271 if (rtx_equal_p (trueop0
, trueop1
) && ! side_effects_p (op0
))
3273 tem
= simplify_associative_operation (code
, mode
, op0
, op1
);
3279 if (trueop1
== constm1_rtx
&& ! side_effects_p (op0
))
3281 if (rtx_equal_p (trueop0
, trueop1
) && ! side_effects_p (op0
))
3283 tem
= simplify_associative_operation (code
, mode
, op0
, op1
);
3296 /* ??? There are simplifications that can be done. */
3300 if (!VECTOR_MODE_P (mode
))
3302 gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0
)));
3303 gcc_assert (mode
== GET_MODE_INNER (GET_MODE (trueop0
)));
3304 gcc_assert (GET_CODE (trueop1
) == PARALLEL
);
3305 gcc_assert (XVECLEN (trueop1
, 0) == 1);
3306 gcc_assert (CONST_INT_P (XVECEXP (trueop1
, 0, 0)));
3308 if (GET_CODE (trueop0
) == CONST_VECTOR
)
3309 return CONST_VECTOR_ELT (trueop0
, INTVAL (XVECEXP
3312 /* Extract a scalar element from a nested VEC_SELECT expression
3313 (with optional nested VEC_CONCAT expression). Some targets
3314 (i386) extract scalar element from a vector using chain of
3315 nested VEC_SELECT expressions. When input operand is a memory
3316 operand, this operation can be simplified to a simple scalar
3317 load from an offseted memory address. */
3318 if (GET_CODE (trueop0
) == VEC_SELECT
)
3320 rtx op0
= XEXP (trueop0
, 0);
3321 rtx op1
= XEXP (trueop0
, 1);
3323 machine_mode opmode
= GET_MODE (op0
);
3324 int elt_size
= GET_MODE_SIZE (GET_MODE_INNER (opmode
));
3325 int n_elts
= GET_MODE_SIZE (opmode
) / elt_size
;
3327 int i
= INTVAL (XVECEXP (trueop1
, 0, 0));
3333 gcc_assert (GET_CODE (op1
) == PARALLEL
);
3334 gcc_assert (i
< n_elts
);
3336 /* Select element, pointed by nested selector. */
3337 elem
= INTVAL (XVECEXP (op1
, 0, i
));
3339 /* Handle the case when nested VEC_SELECT wraps VEC_CONCAT. */
3340 if (GET_CODE (op0
) == VEC_CONCAT
)
3342 rtx op00
= XEXP (op0
, 0);
3343 rtx op01
= XEXP (op0
, 1);
3345 machine_mode mode00
, mode01
;
3346 int n_elts00
, n_elts01
;
3348 mode00
= GET_MODE (op00
);
3349 mode01
= GET_MODE (op01
);
3351 /* Find out number of elements of each operand. */
3352 if (VECTOR_MODE_P (mode00
))
3354 elt_size
= GET_MODE_SIZE (GET_MODE_INNER (mode00
));
3355 n_elts00
= GET_MODE_SIZE (mode00
) / elt_size
;
3360 if (VECTOR_MODE_P (mode01
))
3362 elt_size
= GET_MODE_SIZE (GET_MODE_INNER (mode01
));
3363 n_elts01
= GET_MODE_SIZE (mode01
) / elt_size
;
3368 gcc_assert (n_elts
== n_elts00
+ n_elts01
);
3370 /* Select correct operand of VEC_CONCAT
3371 and adjust selector. */
3372 if (elem
< n_elts01
)
3383 vec
= rtvec_alloc (1);
3384 RTVEC_ELT (vec
, 0) = GEN_INT (elem
);
3386 tmp
= gen_rtx_fmt_ee (code
, mode
,
3387 tmp_op
, gen_rtx_PARALLEL (VOIDmode
, vec
));
3390 if (GET_CODE (trueop0
) == VEC_DUPLICATE
3391 && GET_MODE (XEXP (trueop0
, 0)) == mode
)
3392 return XEXP (trueop0
, 0);
3396 gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0
)));
3397 gcc_assert (GET_MODE_INNER (mode
)
3398 == GET_MODE_INNER (GET_MODE (trueop0
)));
3399 gcc_assert (GET_CODE (trueop1
) == PARALLEL
);
3401 if (GET_CODE (trueop0
) == CONST_VECTOR
)
3403 int elt_size
= GET_MODE_SIZE (GET_MODE_INNER (mode
));
3404 unsigned n_elts
= (GET_MODE_SIZE (mode
) / elt_size
);
3405 rtvec v
= rtvec_alloc (n_elts
);
3408 gcc_assert (XVECLEN (trueop1
, 0) == (int) n_elts
);
3409 for (i
= 0; i
< n_elts
; i
++)
3411 rtx x
= XVECEXP (trueop1
, 0, i
);
3413 gcc_assert (CONST_INT_P (x
));
3414 RTVEC_ELT (v
, i
) = CONST_VECTOR_ELT (trueop0
,
3418 return gen_rtx_CONST_VECTOR (mode
, v
);
3421 /* Recognize the identity. */
3422 if (GET_MODE (trueop0
) == mode
)
3424 bool maybe_ident
= true;
3425 for (int i
= 0; i
< XVECLEN (trueop1
, 0); i
++)
3427 rtx j
= XVECEXP (trueop1
, 0, i
);
3428 if (!CONST_INT_P (j
) || INTVAL (j
) != i
)
3430 maybe_ident
= false;
3438 /* If we build {a,b} then permute it, build the result directly. */
3439 if (XVECLEN (trueop1
, 0) == 2
3440 && CONST_INT_P (XVECEXP (trueop1
, 0, 0))
3441 && CONST_INT_P (XVECEXP (trueop1
, 0, 1))
3442 && GET_CODE (trueop0
) == VEC_CONCAT
3443 && GET_CODE (XEXP (trueop0
, 0)) == VEC_CONCAT
3444 && GET_MODE (XEXP (trueop0
, 0)) == mode
3445 && GET_CODE (XEXP (trueop0
, 1)) == VEC_CONCAT
3446 && GET_MODE (XEXP (trueop0
, 1)) == mode
)
3448 unsigned int i0
= INTVAL (XVECEXP (trueop1
, 0, 0));
3449 unsigned int i1
= INTVAL (XVECEXP (trueop1
, 0, 1));
3452 gcc_assert (i0
< 4 && i1
< 4);
3453 subop0
= XEXP (XEXP (trueop0
, i0
/ 2), i0
% 2);
3454 subop1
= XEXP (XEXP (trueop0
, i1
/ 2), i1
% 2);
3456 return simplify_gen_binary (VEC_CONCAT
, mode
, subop0
, subop1
);
3459 if (XVECLEN (trueop1
, 0) == 2
3460 && CONST_INT_P (XVECEXP (trueop1
, 0, 0))
3461 && CONST_INT_P (XVECEXP (trueop1
, 0, 1))
3462 && GET_CODE (trueop0
) == VEC_CONCAT
3463 && GET_MODE (trueop0
) == mode
)
3465 unsigned int i0
= INTVAL (XVECEXP (trueop1
, 0, 0));
3466 unsigned int i1
= INTVAL (XVECEXP (trueop1
, 0, 1));
3469 gcc_assert (i0
< 2 && i1
< 2);
3470 subop0
= XEXP (trueop0
, i0
);
3471 subop1
= XEXP (trueop0
, i1
);
3473 return simplify_gen_binary (VEC_CONCAT
, mode
, subop0
, subop1
);
3476 /* If we select one half of a vec_concat, return that. */
3477 if (GET_CODE (trueop0
) == VEC_CONCAT
3478 && CONST_INT_P (XVECEXP (trueop1
, 0, 0)))
3480 rtx subop0
= XEXP (trueop0
, 0);
3481 rtx subop1
= XEXP (trueop0
, 1);
3482 machine_mode mode0
= GET_MODE (subop0
);
3483 machine_mode mode1
= GET_MODE (subop1
);
3484 int li
= GET_MODE_SIZE (GET_MODE_INNER (mode0
));
3485 int l0
= GET_MODE_SIZE (mode0
) / li
;
3486 int l1
= GET_MODE_SIZE (mode1
) / li
;
3487 int i0
= INTVAL (XVECEXP (trueop1
, 0, 0));
3488 if (i0
== 0 && !side_effects_p (op1
) && mode
== mode0
)
3490 bool success
= true;
3491 for (int i
= 1; i
< l0
; ++i
)
3493 rtx j
= XVECEXP (trueop1
, 0, i
);
3494 if (!CONST_INT_P (j
) || INTVAL (j
) != i
)
3503 if (i0
== l0
&& !side_effects_p (op0
) && mode
== mode1
)
3505 bool success
= true;
3506 for (int i
= 1; i
< l1
; ++i
)
3508 rtx j
= XVECEXP (trueop1
, 0, i
);
3509 if (!CONST_INT_P (j
) || INTVAL (j
) != i0
+ i
)
3521 if (XVECLEN (trueop1
, 0) == 1
3522 && CONST_INT_P (XVECEXP (trueop1
, 0, 0))
3523 && GET_CODE (trueop0
) == VEC_CONCAT
)
3526 int offset
= INTVAL (XVECEXP (trueop1
, 0, 0)) * GET_MODE_SIZE (mode
);
3528 /* Try to find the element in the VEC_CONCAT. */
3529 while (GET_MODE (vec
) != mode
3530 && GET_CODE (vec
) == VEC_CONCAT
)
3532 HOST_WIDE_INT vec_size
;
3534 if (CONST_INT_P (XEXP (vec
, 0)))
3536 /* vec_concat of two const_ints doesn't make sense with
3537 respect to modes. */
3538 if (CONST_INT_P (XEXP (vec
, 1)))
3541 vec_size
= GET_MODE_SIZE (GET_MODE (trueop0
))
3542 - GET_MODE_SIZE (GET_MODE (XEXP (vec
, 1)));
3545 vec_size
= GET_MODE_SIZE (GET_MODE (XEXP (vec
, 0)));
3547 if (offset
< vec_size
)
3548 vec
= XEXP (vec
, 0);
3552 vec
= XEXP (vec
, 1);
3554 vec
= avoid_constant_pool_reference (vec
);
3557 if (GET_MODE (vec
) == mode
)
3561 /* If we select elements in a vec_merge that all come from the same
3562 operand, select from that operand directly. */
3563 if (GET_CODE (op0
) == VEC_MERGE
)
3565 rtx trueop02
= avoid_constant_pool_reference (XEXP (op0
, 2));
3566 if (CONST_INT_P (trueop02
))
3568 unsigned HOST_WIDE_INT sel
= UINTVAL (trueop02
);
3569 bool all_operand0
= true;
3570 bool all_operand1
= true;
3571 for (int i
= 0; i
< XVECLEN (trueop1
, 0); i
++)
3573 rtx j
= XVECEXP (trueop1
, 0, i
);
3574 if (sel
& (1 << UINTVAL (j
)))
3575 all_operand1
= false;
3577 all_operand0
= false;
3579 if (all_operand0
&& !side_effects_p (XEXP (op0
, 1)))
3580 return simplify_gen_binary (VEC_SELECT
, mode
, XEXP (op0
, 0), op1
);
3581 if (all_operand1
&& !side_effects_p (XEXP (op0
, 0)))
3582 return simplify_gen_binary (VEC_SELECT
, mode
, XEXP (op0
, 1), op1
);
3586 /* If we have two nested selects that are inverses of each
3587 other, replace them with the source operand. */
3588 if (GET_CODE (trueop0
) == VEC_SELECT
3589 && GET_MODE (XEXP (trueop0
, 0)) == mode
)
3591 rtx op0_subop1
= XEXP (trueop0
, 1);
3592 gcc_assert (GET_CODE (op0_subop1
) == PARALLEL
);
3593 gcc_assert (XVECLEN (trueop1
, 0) == GET_MODE_NUNITS (mode
));
3595 /* Apply the outer ordering vector to the inner one. (The inner
3596 ordering vector is expressly permitted to be of a different
3597 length than the outer one.) If the result is { 0, 1, ..., n-1 }
3598 then the two VEC_SELECTs cancel. */
3599 for (int i
= 0; i
< XVECLEN (trueop1
, 0); ++i
)
3601 rtx x
= XVECEXP (trueop1
, 0, i
);
3602 if (!CONST_INT_P (x
))
3604 rtx y
= XVECEXP (op0_subop1
, 0, INTVAL (x
));
3605 if (!CONST_INT_P (y
) || i
!= INTVAL (y
))
3608 return XEXP (trueop0
, 0);
3614 machine_mode op0_mode
= (GET_MODE (trueop0
) != VOIDmode
3615 ? GET_MODE (trueop0
)
3616 : GET_MODE_INNER (mode
));
3617 machine_mode op1_mode
= (GET_MODE (trueop1
) != VOIDmode
3618 ? GET_MODE (trueop1
)
3619 : GET_MODE_INNER (mode
));
3621 gcc_assert (VECTOR_MODE_P (mode
));
3622 gcc_assert (GET_MODE_SIZE (op0_mode
) + GET_MODE_SIZE (op1_mode
)
3623 == GET_MODE_SIZE (mode
));
3625 if (VECTOR_MODE_P (op0_mode
))
3626 gcc_assert (GET_MODE_INNER (mode
)
3627 == GET_MODE_INNER (op0_mode
));
3629 gcc_assert (GET_MODE_INNER (mode
) == op0_mode
);
3631 if (VECTOR_MODE_P (op1_mode
))
3632 gcc_assert (GET_MODE_INNER (mode
)
3633 == GET_MODE_INNER (op1_mode
));
3635 gcc_assert (GET_MODE_INNER (mode
) == op1_mode
);
3637 if ((GET_CODE (trueop0
) == CONST_VECTOR
3638 || CONST_SCALAR_INT_P (trueop0
)
3639 || CONST_DOUBLE_AS_FLOAT_P (trueop0
))
3640 && (GET_CODE (trueop1
) == CONST_VECTOR
3641 || CONST_SCALAR_INT_P (trueop1
)
3642 || CONST_DOUBLE_AS_FLOAT_P (trueop1
)))
3644 int elt_size
= GET_MODE_SIZE (GET_MODE_INNER (mode
));
3645 unsigned n_elts
= (GET_MODE_SIZE (mode
) / elt_size
);
3646 rtvec v
= rtvec_alloc (n_elts
);
3648 unsigned in_n_elts
= 1;
3650 if (VECTOR_MODE_P (op0_mode
))
3651 in_n_elts
= (GET_MODE_SIZE (op0_mode
) / elt_size
);
3652 for (i
= 0; i
< n_elts
; i
++)
3656 if (!VECTOR_MODE_P (op0_mode
))
3657 RTVEC_ELT (v
, i
) = trueop0
;
3659 RTVEC_ELT (v
, i
) = CONST_VECTOR_ELT (trueop0
, i
);
3663 if (!VECTOR_MODE_P (op1_mode
))
3664 RTVEC_ELT (v
, i
) = trueop1
;
3666 RTVEC_ELT (v
, i
) = CONST_VECTOR_ELT (trueop1
,
3671 return gen_rtx_CONST_VECTOR (mode
, v
);
3674 /* Try to merge two VEC_SELECTs from the same vector into a single one.
3675 Restrict the transformation to avoid generating a VEC_SELECT with a
3676 mode unrelated to its operand. */
3677 if (GET_CODE (trueop0
) == VEC_SELECT
3678 && GET_CODE (trueop1
) == VEC_SELECT
3679 && rtx_equal_p (XEXP (trueop0
, 0), XEXP (trueop1
, 0))
3680 && GET_MODE (XEXP (trueop0
, 0)) == mode
)
3682 rtx par0
= XEXP (trueop0
, 1);
3683 rtx par1
= XEXP (trueop1
, 1);
3684 int len0
= XVECLEN (par0
, 0);
3685 int len1
= XVECLEN (par1
, 0);
3686 rtvec vec
= rtvec_alloc (len0
+ len1
);
3687 for (int i
= 0; i
< len0
; i
++)
3688 RTVEC_ELT (vec
, i
) = XVECEXP (par0
, 0, i
);
3689 for (int i
= 0; i
< len1
; i
++)
3690 RTVEC_ELT (vec
, len0
+ i
) = XVECEXP (par1
, 0, i
);
3691 return simplify_gen_binary (VEC_SELECT
, mode
, XEXP (trueop0
, 0),
3692 gen_rtx_PARALLEL (VOIDmode
, vec
));
3705 simplify_const_binary_operation (enum rtx_code code
, machine_mode mode
,
3708 unsigned int width
= GET_MODE_PRECISION (mode
);
3710 if (VECTOR_MODE_P (mode
)
3711 && code
!= VEC_CONCAT
3712 && GET_CODE (op0
) == CONST_VECTOR
3713 && GET_CODE (op1
) == CONST_VECTOR
)
3715 unsigned n_elts
= GET_MODE_NUNITS (mode
);
3716 machine_mode op0mode
= GET_MODE (op0
);
3717 unsigned op0_n_elts
= GET_MODE_NUNITS (op0mode
);
3718 machine_mode op1mode
= GET_MODE (op1
);
3719 unsigned op1_n_elts
= GET_MODE_NUNITS (op1mode
);
3720 rtvec v
= rtvec_alloc (n_elts
);
3723 gcc_assert (op0_n_elts
== n_elts
);
3724 gcc_assert (op1_n_elts
== n_elts
);
3725 for (i
= 0; i
< n_elts
; i
++)
3727 rtx x
= simplify_binary_operation (code
, GET_MODE_INNER (mode
),
3728 CONST_VECTOR_ELT (op0
, i
),
3729 CONST_VECTOR_ELT (op1
, i
));
3732 RTVEC_ELT (v
, i
) = x
;
3735 return gen_rtx_CONST_VECTOR (mode
, v
);
3738 if (VECTOR_MODE_P (mode
)
3739 && code
== VEC_CONCAT
3740 && (CONST_SCALAR_INT_P (op0
)
3741 || GET_CODE (op0
) == CONST_FIXED
3742 || CONST_DOUBLE_AS_FLOAT_P (op0
))
3743 && (CONST_SCALAR_INT_P (op1
)
3744 || CONST_DOUBLE_AS_FLOAT_P (op1
)
3745 || GET_CODE (op1
) == CONST_FIXED
))
3747 unsigned n_elts
= GET_MODE_NUNITS (mode
);
3748 rtvec v
= rtvec_alloc (n_elts
);
3750 gcc_assert (n_elts
>= 2);
3753 gcc_assert (GET_CODE (op0
) != CONST_VECTOR
);
3754 gcc_assert (GET_CODE (op1
) != CONST_VECTOR
);
3756 RTVEC_ELT (v
, 0) = op0
;
3757 RTVEC_ELT (v
, 1) = op1
;
3761 unsigned op0_n_elts
= GET_MODE_NUNITS (GET_MODE (op0
));
3762 unsigned op1_n_elts
= GET_MODE_NUNITS (GET_MODE (op1
));
3765 gcc_assert (GET_CODE (op0
) == CONST_VECTOR
);
3766 gcc_assert (GET_CODE (op1
) == CONST_VECTOR
);
3767 gcc_assert (op0_n_elts
+ op1_n_elts
== n_elts
);
3769 for (i
= 0; i
< op0_n_elts
; ++i
)
3770 RTVEC_ELT (v
, i
) = XVECEXP (op0
, 0, i
);
3771 for (i
= 0; i
< op1_n_elts
; ++i
)
3772 RTVEC_ELT (v
, op0_n_elts
+i
) = XVECEXP (op1
, 0, i
);
3775 return gen_rtx_CONST_VECTOR (mode
, v
);
3778 if (SCALAR_FLOAT_MODE_P (mode
)
3779 && CONST_DOUBLE_AS_FLOAT_P (op0
)
3780 && CONST_DOUBLE_AS_FLOAT_P (op1
)
3781 && mode
== GET_MODE (op0
) && mode
== GET_MODE (op1
))
3792 real_to_target (tmp0
, CONST_DOUBLE_REAL_VALUE (op0
),
3794 real_to_target (tmp1
, CONST_DOUBLE_REAL_VALUE (op1
),
3796 for (i
= 0; i
< 4; i
++)
3813 real_from_target (&r
, tmp0
, mode
);
3814 return CONST_DOUBLE_FROM_REAL_VALUE (r
, mode
);
3818 REAL_VALUE_TYPE f0
, f1
, value
, result
;
3821 REAL_VALUE_FROM_CONST_DOUBLE (f0
, op0
);
3822 REAL_VALUE_FROM_CONST_DOUBLE (f1
, op1
);
3823 real_convert (&f0
, mode
, &f0
);
3824 real_convert (&f1
, mode
, &f1
);
3826 if (HONOR_SNANS (mode
)
3827 && (REAL_VALUE_ISNAN (f0
) || REAL_VALUE_ISNAN (f1
)))
3831 && REAL_VALUES_EQUAL (f1
, dconst0
)
3832 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
3835 if (MODE_HAS_INFINITIES (mode
) && HONOR_NANS (mode
)
3836 && flag_trapping_math
3837 && REAL_VALUE_ISINF (f0
) && REAL_VALUE_ISINF (f1
))
3839 int s0
= REAL_VALUE_NEGATIVE (f0
);
3840 int s1
= REAL_VALUE_NEGATIVE (f1
);
3845 /* Inf + -Inf = NaN plus exception. */
3850 /* Inf - Inf = NaN plus exception. */
3855 /* Inf / Inf = NaN plus exception. */
3862 if (code
== MULT
&& MODE_HAS_INFINITIES (mode
) && HONOR_NANS (mode
)
3863 && flag_trapping_math
3864 && ((REAL_VALUE_ISINF (f0
) && REAL_VALUES_EQUAL (f1
, dconst0
))
3865 || (REAL_VALUE_ISINF (f1
)
3866 && REAL_VALUES_EQUAL (f0
, dconst0
))))
3867 /* Inf * 0 = NaN plus exception. */
3870 inexact
= real_arithmetic (&value
, rtx_to_tree_code (code
),
3872 real_convert (&result
, mode
, &value
);
3874 /* Don't constant fold this floating point operation if
3875 the result has overflowed and flag_trapping_math. */
3877 if (flag_trapping_math
3878 && MODE_HAS_INFINITIES (mode
)
3879 && REAL_VALUE_ISINF (result
)
3880 && !REAL_VALUE_ISINF (f0
)
3881 && !REAL_VALUE_ISINF (f1
))
3882 /* Overflow plus exception. */
3885 /* Don't constant fold this floating point operation if the
3886 result may dependent upon the run-time rounding mode and
3887 flag_rounding_math is set, or if GCC's software emulation
3888 is unable to accurately represent the result. */
3890 if ((flag_rounding_math
3891 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
3892 && (inexact
|| !real_identical (&result
, &value
)))
3895 return CONST_DOUBLE_FROM_REAL_VALUE (result
, mode
);
3899 /* We can fold some multi-word operations. */
3900 if ((GET_MODE_CLASS (mode
) == MODE_INT
3901 || GET_MODE_CLASS (mode
) == MODE_PARTIAL_INT
)
3902 && CONST_SCALAR_INT_P (op0
)
3903 && CONST_SCALAR_INT_P (op1
))
3907 rtx_mode_t pop0
= std::make_pair (op0
, mode
);
3908 rtx_mode_t pop1
= std::make_pair (op1
, mode
);
3910 #if TARGET_SUPPORTS_WIDE_INT == 0
3911 /* This assert keeps the simplification from producing a result
3912 that cannot be represented in a CONST_DOUBLE but a lot of
3913 upstream callers expect that this function never fails to
3914 simplify something and so you if you added this to the test
3915 above the code would die later anyway. If this assert
3916 happens, you just need to make the port support wide int. */
3917 gcc_assert (width
<= HOST_BITS_PER_DOUBLE_INT
);
3922 result
= wi::sub (pop0
, pop1
);
3926 result
= wi::add (pop0
, pop1
);
3930 result
= wi::mul (pop0
, pop1
);
3934 result
= wi::div_trunc (pop0
, pop1
, SIGNED
, &overflow
);
3940 result
= wi::mod_trunc (pop0
, pop1
, SIGNED
, &overflow
);
3946 result
= wi::div_trunc (pop0
, pop1
, UNSIGNED
, &overflow
);
3952 result
= wi::mod_trunc (pop0
, pop1
, UNSIGNED
, &overflow
);
3958 result
= wi::bit_and (pop0
, pop1
);
3962 result
= wi::bit_or (pop0
, pop1
);
3966 result
= wi::bit_xor (pop0
, pop1
);
3970 result
= wi::smin (pop0
, pop1
);
3974 result
= wi::smax (pop0
, pop1
);
3978 result
= wi::umin (pop0
, pop1
);
3982 result
= wi::umax (pop0
, pop1
);
3989 wide_int wop1
= pop1
;
3990 if (SHIFT_COUNT_TRUNCATED
)
3991 wop1
= wi::umod_trunc (wop1
, width
);
3992 else if (wi::geu_p (wop1
, width
))
3998 result
= wi::lrshift (pop0
, wop1
);
4002 result
= wi::arshift (pop0
, wop1
);
4006 result
= wi::lshift (pop0
, wop1
);
4017 if (wi::neg_p (pop1
))
4023 result
= wi::lrotate (pop0
, pop1
);
4027 result
= wi::rrotate (pop0
, pop1
);
4038 return immed_wide_int_const (result
, mode
);
4046 /* Return a positive integer if X should sort after Y. The value
4047 returned is 1 if and only if X and Y are both regs. */
4050 simplify_plus_minus_op_data_cmp (rtx x
, rtx y
)
4054 result
= (commutative_operand_precedence (y
)
4055 - commutative_operand_precedence (x
));
4057 return result
+ result
;
4059 /* Group together equal REGs to do more simplification. */
4060 if (REG_P (x
) && REG_P (y
))
4061 return REGNO (x
) > REGNO (y
);
4066 /* Simplify and canonicalize a PLUS or MINUS, at least one of whose
4067 operands may be another PLUS or MINUS.
4069 Rather than test for specific case, we do this by a brute-force method
4070 and do all possible simplifications until no more changes occur. Then
4071 we rebuild the operation.
4073 May return NULL_RTX when no changes were made. */
4076 simplify_plus_minus (enum rtx_code code
, machine_mode mode
, rtx op0
,
4079 struct simplify_plus_minus_op_data
4086 int changed
, n_constants
, canonicalized
= 0;
4089 memset (ops
, 0, sizeof ops
);
4091 /* Set up the two operands and then expand them until nothing has been
4092 changed. If we run out of room in our array, give up; this should
4093 almost never happen. */
4098 ops
[1].neg
= (code
== MINUS
);
4105 for (i
= 0; i
< n_ops
; i
++)
4107 rtx this_op
= ops
[i
].op
;
4108 int this_neg
= ops
[i
].neg
;
4109 enum rtx_code this_code
= GET_CODE (this_op
);
4115 if (n_ops
== ARRAY_SIZE (ops
))
4118 ops
[n_ops
].op
= XEXP (this_op
, 1);
4119 ops
[n_ops
].neg
= (this_code
== MINUS
) ^ this_neg
;
4122 ops
[i
].op
= XEXP (this_op
, 0);
4124 /* If this operand was negated then we will potentially
4125 canonicalize the expression. Similarly if we don't
4126 place the operands adjacent we're re-ordering the
4127 expression and thus might be performing a
4128 canonicalization. Ignore register re-ordering.
4129 ??? It might be better to shuffle the ops array here,
4130 but then (plus (plus (A, B), plus (C, D))) wouldn't
4131 be seen as non-canonical. */
4134 && !(REG_P (ops
[i
].op
) && REG_P (ops
[n_ops
- 1].op
))))
4139 ops
[i
].op
= XEXP (this_op
, 0);
4140 ops
[i
].neg
= ! this_neg
;
4146 if (n_ops
!= ARRAY_SIZE (ops
)
4147 && GET_CODE (XEXP (this_op
, 0)) == PLUS
4148 && CONSTANT_P (XEXP (XEXP (this_op
, 0), 0))
4149 && CONSTANT_P (XEXP (XEXP (this_op
, 0), 1)))
4151 ops
[i
].op
= XEXP (XEXP (this_op
, 0), 0);
4152 ops
[n_ops
].op
= XEXP (XEXP (this_op
, 0), 1);
4153 ops
[n_ops
].neg
= this_neg
;
4161 /* ~a -> (-a - 1) */
4162 if (n_ops
!= ARRAY_SIZE (ops
))
4164 ops
[n_ops
].op
= CONSTM1_RTX (mode
);
4165 ops
[n_ops
++].neg
= this_neg
;
4166 ops
[i
].op
= XEXP (this_op
, 0);
4167 ops
[i
].neg
= !this_neg
;
4177 ops
[i
].op
= neg_const_int (mode
, this_op
);
4191 if (n_constants
> 1)
4194 gcc_assert (n_ops
>= 2);
4196 /* If we only have two operands, we can avoid the loops. */
4199 enum rtx_code code
= ops
[0].neg
|| ops
[1].neg
? MINUS
: PLUS
;
4202 /* Get the two operands. Be careful with the order, especially for
4203 the cases where code == MINUS. */
4204 if (ops
[0].neg
&& ops
[1].neg
)
4206 lhs
= gen_rtx_NEG (mode
, ops
[0].op
);
4209 else if (ops
[0].neg
)
4220 return simplify_const_binary_operation (code
, mode
, lhs
, rhs
);
4223 /* Now simplify each pair of operands until nothing changes. */
4226 /* Insertion sort is good enough for a small array. */
4227 for (i
= 1; i
< n_ops
; i
++)
4229 struct simplify_plus_minus_op_data save
;
4233 cmp
= simplify_plus_minus_op_data_cmp (ops
[j
].op
, ops
[i
].op
);
4236 /* Just swapping registers doesn't count as canonicalization. */
4242 ops
[j
+ 1] = ops
[j
];
4244 && simplify_plus_minus_op_data_cmp (ops
[j
].op
, save
.op
) > 0);
4249 for (i
= n_ops
- 1; i
> 0; i
--)
4250 for (j
= i
- 1; j
>= 0; j
--)
4252 rtx lhs
= ops
[j
].op
, rhs
= ops
[i
].op
;
4253 int lneg
= ops
[j
].neg
, rneg
= ops
[i
].neg
;
4255 if (lhs
!= 0 && rhs
!= 0)
4257 enum rtx_code ncode
= PLUS
;
4263 std::swap (lhs
, rhs
);
4265 else if (swap_commutative_operands_p (lhs
, rhs
))
4266 std::swap (lhs
, rhs
);
4268 if ((GET_CODE (lhs
) == CONST
|| CONST_INT_P (lhs
))
4269 && (GET_CODE (rhs
) == CONST
|| CONST_INT_P (rhs
)))
4271 rtx tem_lhs
, tem_rhs
;
4273 tem_lhs
= GET_CODE (lhs
) == CONST
? XEXP (lhs
, 0) : lhs
;
4274 tem_rhs
= GET_CODE (rhs
) == CONST
? XEXP (rhs
, 0) : rhs
;
4275 tem
= simplify_binary_operation (ncode
, mode
, tem_lhs
,
4278 if (tem
&& !CONSTANT_P (tem
))
4279 tem
= gen_rtx_CONST (GET_MODE (tem
), tem
);
4282 tem
= simplify_binary_operation (ncode
, mode
, lhs
, rhs
);
4286 /* Reject "simplifications" that just wrap the two
4287 arguments in a CONST. Failure to do so can result
4288 in infinite recursion with simplify_binary_operation
4289 when it calls us to simplify CONST operations.
4290 Also, if we find such a simplification, don't try
4291 any more combinations with this rhs: We must have
4292 something like symbol+offset, ie. one of the
4293 trivial CONST expressions we handle later. */
4294 if (GET_CODE (tem
) == CONST
4295 && GET_CODE (XEXP (tem
, 0)) == ncode
4296 && XEXP (XEXP (tem
, 0), 0) == lhs
4297 && XEXP (XEXP (tem
, 0), 1) == rhs
)
4300 if (GET_CODE (tem
) == NEG
)
4301 tem
= XEXP (tem
, 0), lneg
= !lneg
;
4302 if (CONST_INT_P (tem
) && lneg
)
4303 tem
= neg_const_int (mode
, tem
), lneg
= 0;
4307 ops
[j
].op
= NULL_RTX
;
4317 /* Pack all the operands to the lower-numbered entries. */
4318 for (i
= 0, j
= 0; j
< n_ops
; j
++)
4327 /* If nothing changed, fail. */
4331 /* Create (minus -C X) instead of (neg (const (plus X C))). */
4333 && CONST_INT_P (ops
[1].op
)
4334 && CONSTANT_P (ops
[0].op
)
4336 return gen_rtx_fmt_ee (MINUS
, mode
, ops
[1].op
, ops
[0].op
);
4338 /* We suppressed creation of trivial CONST expressions in the
4339 combination loop to avoid recursion. Create one manually now.
4340 The combination loop should have ensured that there is exactly
4341 one CONST_INT, and the sort will have ensured that it is last
4342 in the array and that any other constant will be next-to-last. */
4345 && CONST_INT_P (ops
[n_ops
- 1].op
)
4346 && CONSTANT_P (ops
[n_ops
- 2].op
))
4348 rtx value
= ops
[n_ops
- 1].op
;
4349 if (ops
[n_ops
- 1].neg
^ ops
[n_ops
- 2].neg
)
4350 value
= neg_const_int (mode
, value
);
4351 ops
[n_ops
- 2].op
= plus_constant (mode
, ops
[n_ops
- 2].op
,
4356 /* Put a non-negated operand first, if possible. */
4358 for (i
= 0; i
< n_ops
&& ops
[i
].neg
; i
++)
4361 ops
[0].op
= gen_rtx_NEG (mode
, ops
[0].op
);
4370 /* Now make the result by performing the requested operations. */
4372 for (i
= 1; i
< n_ops
; i
++)
4373 result
= gen_rtx_fmt_ee (ops
[i
].neg
? MINUS
: PLUS
,
4374 mode
, result
, ops
[i
].op
);
4379 /* Check whether an operand is suitable for calling simplify_plus_minus. */
4381 plus_minus_operand_p (const_rtx x
)
4383 return GET_CODE (x
) == PLUS
4384 || GET_CODE (x
) == MINUS
4385 || (GET_CODE (x
) == CONST
4386 && GET_CODE (XEXP (x
, 0)) == PLUS
4387 && CONSTANT_P (XEXP (XEXP (x
, 0), 0))
4388 && CONSTANT_P (XEXP (XEXP (x
, 0), 1)));
4391 /* Like simplify_binary_operation except used for relational operators.
4392 MODE is the mode of the result. If MODE is VOIDmode, both operands must
4393 not also be VOIDmode.
4395 CMP_MODE specifies in which mode the comparison is done in, so it is
4396 the mode of the operands. If CMP_MODE is VOIDmode, it is taken from
4397 the operands or, if both are VOIDmode, the operands are compared in
4398 "infinite precision". */
4400 simplify_relational_operation (enum rtx_code code
, machine_mode mode
,
4401 machine_mode cmp_mode
, rtx op0
, rtx op1
)
4403 rtx tem
, trueop0
, trueop1
;
4405 if (cmp_mode
== VOIDmode
)
4406 cmp_mode
= GET_MODE (op0
);
4407 if (cmp_mode
== VOIDmode
)
4408 cmp_mode
= GET_MODE (op1
);
4410 tem
= simplify_const_relational_operation (code
, cmp_mode
, op0
, op1
);
4413 if (SCALAR_FLOAT_MODE_P (mode
))
4415 if (tem
== const0_rtx
)
4416 return CONST0_RTX (mode
);
4417 #ifdef FLOAT_STORE_FLAG_VALUE
4419 REAL_VALUE_TYPE val
;
4420 val
= FLOAT_STORE_FLAG_VALUE (mode
);
4421 return CONST_DOUBLE_FROM_REAL_VALUE (val
, mode
);
4427 if (VECTOR_MODE_P (mode
))
4429 if (tem
== const0_rtx
)
4430 return CONST0_RTX (mode
);
4431 #ifdef VECTOR_STORE_FLAG_VALUE
4436 rtx val
= VECTOR_STORE_FLAG_VALUE (mode
);
4437 if (val
== NULL_RTX
)
4439 if (val
== const1_rtx
)
4440 return CONST1_RTX (mode
);
4442 units
= GET_MODE_NUNITS (mode
);
4443 v
= rtvec_alloc (units
);
4444 for (i
= 0; i
< units
; i
++)
4445 RTVEC_ELT (v
, i
) = val
;
4446 return gen_rtx_raw_CONST_VECTOR (mode
, v
);
4456 /* For the following tests, ensure const0_rtx is op1. */
4457 if (swap_commutative_operands_p (op0
, op1
)
4458 || (op0
== const0_rtx
&& op1
!= const0_rtx
))
4459 std::swap (op0
, op1
), code
= swap_condition (code
);
4461 /* If op0 is a compare, extract the comparison arguments from it. */
4462 if (GET_CODE (op0
) == COMPARE
&& op1
== const0_rtx
)
4463 return simplify_gen_relational (code
, mode
, VOIDmode
,
4464 XEXP (op0
, 0), XEXP (op0
, 1));
4466 if (GET_MODE_CLASS (cmp_mode
) == MODE_CC
4470 trueop0
= avoid_constant_pool_reference (op0
);
4471 trueop1
= avoid_constant_pool_reference (op1
);
4472 return simplify_relational_operation_1 (code
, mode
, cmp_mode
,
4476 /* This part of simplify_relational_operation is only used when CMP_MODE
4477 is not in class MODE_CC (i.e. it is a real comparison).
4479 MODE is the mode of the result, while CMP_MODE specifies in which
4480 mode the comparison is done in, so it is the mode of the operands. */
4483 simplify_relational_operation_1 (enum rtx_code code
, machine_mode mode
,
4484 machine_mode cmp_mode
, rtx op0
, rtx op1
)
4486 enum rtx_code op0code
= GET_CODE (op0
);
4488 if (op1
== const0_rtx
&& COMPARISON_P (op0
))
4490 /* If op0 is a comparison, extract the comparison arguments
4494 if (GET_MODE (op0
) == mode
)
4495 return simplify_rtx (op0
);
4497 return simplify_gen_relational (GET_CODE (op0
), mode
, VOIDmode
,
4498 XEXP (op0
, 0), XEXP (op0
, 1));
4500 else if (code
== EQ
)
4502 enum rtx_code new_code
= reversed_comparison_code (op0
, NULL_RTX
);
4503 if (new_code
!= UNKNOWN
)
4504 return simplify_gen_relational (new_code
, mode
, VOIDmode
,
4505 XEXP (op0
, 0), XEXP (op0
, 1));
4509 /* (LTU/GEU (PLUS a C) C), where C is constant, can be simplified to
4510 (GEU/LTU a -C). Likewise for (LTU/GEU (PLUS a C) a). */
4511 if ((code
== LTU
|| code
== GEU
)
4512 && GET_CODE (op0
) == PLUS
4513 && CONST_INT_P (XEXP (op0
, 1))
4514 && (rtx_equal_p (op1
, XEXP (op0
, 0))
4515 || rtx_equal_p (op1
, XEXP (op0
, 1)))
4516 /* (LTU/GEU (PLUS a 0) 0) is not the same as (GEU/LTU a 0). */
4517 && XEXP (op0
, 1) != const0_rtx
)
4520 = simplify_gen_unary (NEG
, cmp_mode
, XEXP (op0
, 1), cmp_mode
);
4521 return simplify_gen_relational ((code
== LTU
? GEU
: LTU
), mode
,
4522 cmp_mode
, XEXP (op0
, 0), new_cmp
);
4525 /* Canonicalize (LTU/GEU (PLUS a b) b) as (LTU/GEU (PLUS a b) a). */
4526 if ((code
== LTU
|| code
== GEU
)
4527 && GET_CODE (op0
) == PLUS
4528 && rtx_equal_p (op1
, XEXP (op0
, 1))
4529 /* Don't recurse "infinitely" for (LTU/GEU (PLUS b b) b). */
4530 && !rtx_equal_p (op1
, XEXP (op0
, 0)))
4531 return simplify_gen_relational (code
, mode
, cmp_mode
, op0
,
4532 copy_rtx (XEXP (op0
, 0)));
4534 if (op1
== const0_rtx
)
4536 /* Canonicalize (GTU x 0) as (NE x 0). */
4538 return simplify_gen_relational (NE
, mode
, cmp_mode
, op0
, op1
);
4539 /* Canonicalize (LEU x 0) as (EQ x 0). */
4541 return simplify_gen_relational (EQ
, mode
, cmp_mode
, op0
, op1
);
4543 else if (op1
== const1_rtx
)
4548 /* Canonicalize (GE x 1) as (GT x 0). */
4549 return simplify_gen_relational (GT
, mode
, cmp_mode
,
4552 /* Canonicalize (GEU x 1) as (NE x 0). */
4553 return simplify_gen_relational (NE
, mode
, cmp_mode
,
4556 /* Canonicalize (LT x 1) as (LE x 0). */
4557 return simplify_gen_relational (LE
, mode
, cmp_mode
,
4560 /* Canonicalize (LTU x 1) as (EQ x 0). */
4561 return simplify_gen_relational (EQ
, mode
, cmp_mode
,
4567 else if (op1
== constm1_rtx
)
4569 /* Canonicalize (LE x -1) as (LT x 0). */
4571 return simplify_gen_relational (LT
, mode
, cmp_mode
, op0
, const0_rtx
);
4572 /* Canonicalize (GT x -1) as (GE x 0). */
4574 return simplify_gen_relational (GE
, mode
, cmp_mode
, op0
, const0_rtx
);
4577 /* (eq/ne (plus x cst1) cst2) simplifies to (eq/ne x (cst2 - cst1)) */
4578 if ((code
== EQ
|| code
== NE
)
4579 && (op0code
== PLUS
|| op0code
== MINUS
)
4581 && CONSTANT_P (XEXP (op0
, 1))
4582 && (INTEGRAL_MODE_P (cmp_mode
) || flag_unsafe_math_optimizations
))
4584 rtx x
= XEXP (op0
, 0);
4585 rtx c
= XEXP (op0
, 1);
4586 enum rtx_code invcode
= op0code
== PLUS
? MINUS
: PLUS
;
4587 rtx tem
= simplify_gen_binary (invcode
, cmp_mode
, op1
, c
);
4589 /* Detect an infinite recursive condition, where we oscillate at this
4590 simplification case between:
4591 A + B == C <---> C - B == A,
4592 where A, B, and C are all constants with non-simplifiable expressions,
4593 usually SYMBOL_REFs. */
4594 if (GET_CODE (tem
) == invcode
4596 && rtx_equal_p (c
, XEXP (tem
, 1)))
4599 return simplify_gen_relational (code
, mode
, cmp_mode
, x
, tem
);
4602 /* (ne:SI (zero_extract:SI FOO (const_int 1) BAR) (const_int 0))) is
4603 the same as (zero_extract:SI FOO (const_int 1) BAR). */
4605 && op1
== const0_rtx
4606 && GET_MODE_CLASS (mode
) == MODE_INT
4607 && cmp_mode
!= VOIDmode
4608 /* ??? Work-around BImode bugs in the ia64 backend. */
4610 && cmp_mode
!= BImode
4611 && nonzero_bits (op0
, cmp_mode
) == 1
4612 && STORE_FLAG_VALUE
== 1)
4613 return GET_MODE_SIZE (mode
) > GET_MODE_SIZE (cmp_mode
)
4614 ? simplify_gen_unary (ZERO_EXTEND
, mode
, op0
, cmp_mode
)
4615 : lowpart_subreg (mode
, op0
, cmp_mode
);
4617 /* (eq/ne (xor x y) 0) simplifies to (eq/ne x y). */
4618 if ((code
== EQ
|| code
== NE
)
4619 && op1
== const0_rtx
4621 return simplify_gen_relational (code
, mode
, cmp_mode
,
4622 XEXP (op0
, 0), XEXP (op0
, 1));
4624 /* (eq/ne (xor x y) x) simplifies to (eq/ne y 0). */
4625 if ((code
== EQ
|| code
== NE
)
4627 && rtx_equal_p (XEXP (op0
, 0), op1
)
4628 && !side_effects_p (XEXP (op0
, 0)))
4629 return simplify_gen_relational (code
, mode
, cmp_mode
, XEXP (op0
, 1),
4632 /* Likewise (eq/ne (xor x y) y) simplifies to (eq/ne x 0). */
4633 if ((code
== EQ
|| code
== NE
)
4635 && rtx_equal_p (XEXP (op0
, 1), op1
)
4636 && !side_effects_p (XEXP (op0
, 1)))
4637 return simplify_gen_relational (code
, mode
, cmp_mode
, XEXP (op0
, 0),
4640 /* (eq/ne (xor x C1) C2) simplifies to (eq/ne x (C1^C2)). */
4641 if ((code
== EQ
|| code
== NE
)
4643 && CONST_SCALAR_INT_P (op1
)
4644 && CONST_SCALAR_INT_P (XEXP (op0
, 1)))
4645 return simplify_gen_relational (code
, mode
, cmp_mode
, XEXP (op0
, 0),
4646 simplify_gen_binary (XOR
, cmp_mode
,
4647 XEXP (op0
, 1), op1
));
4649 /* (eq/ne (and x y) x) simplifies to (eq/ne (and (not y) x) 0), which
4650 can be implemented with a BICS instruction on some targets, or
4651 constant-folded if y is a constant. */
4652 if ((code
== EQ
|| code
== NE
)
4654 && rtx_equal_p (XEXP (op0
, 0), op1
)
4655 && !side_effects_p (op1
)
4656 && op1
!= CONST0_RTX (cmp_mode
))
4658 rtx not_y
= simplify_gen_unary (NOT
, cmp_mode
, XEXP (op0
, 1), cmp_mode
);
4659 rtx lhs
= simplify_gen_binary (AND
, cmp_mode
, not_y
, XEXP (op0
, 0));
4661 return simplify_gen_relational (code
, mode
, cmp_mode
, lhs
,
4662 CONST0_RTX (cmp_mode
));
4665 /* Likewise for (eq/ne (and x y) y). */
4666 if ((code
== EQ
|| code
== NE
)
4668 && rtx_equal_p (XEXP (op0
, 1), op1
)
4669 && !side_effects_p (op1
)
4670 && op1
!= CONST0_RTX (cmp_mode
))
4672 rtx not_x
= simplify_gen_unary (NOT
, cmp_mode
, XEXP (op0
, 0), cmp_mode
);
4673 rtx lhs
= simplify_gen_binary (AND
, cmp_mode
, not_x
, XEXP (op0
, 1));
4675 return simplify_gen_relational (code
, mode
, cmp_mode
, lhs
,
4676 CONST0_RTX (cmp_mode
));
4679 /* (eq/ne (bswap x) C1) simplifies to (eq/ne x C2) with C2 swapped. */
4680 if ((code
== EQ
|| code
== NE
)
4681 && GET_CODE (op0
) == BSWAP
4682 && CONST_SCALAR_INT_P (op1
))
4683 return simplify_gen_relational (code
, mode
, cmp_mode
, XEXP (op0
, 0),
4684 simplify_gen_unary (BSWAP
, cmp_mode
,
4687 /* (eq/ne (bswap x) (bswap y)) simplifies to (eq/ne x y). */
4688 if ((code
== EQ
|| code
== NE
)
4689 && GET_CODE (op0
) == BSWAP
4690 && GET_CODE (op1
) == BSWAP
)
4691 return simplify_gen_relational (code
, mode
, cmp_mode
,
4692 XEXP (op0
, 0), XEXP (op1
, 0));
4694 if (op0code
== POPCOUNT
&& op1
== const0_rtx
)
4700 /* (eq (popcount x) (const_int 0)) -> (eq x (const_int 0)). */
4701 return simplify_gen_relational (EQ
, mode
, GET_MODE (XEXP (op0
, 0)),
4702 XEXP (op0
, 0), const0_rtx
);
4707 /* (ne (popcount x) (const_int 0)) -> (ne x (const_int 0)). */
4708 return simplify_gen_relational (NE
, mode
, GET_MODE (XEXP (op0
, 0)),
4709 XEXP (op0
, 0), const0_rtx
);
4728 /* Convert the known results for EQ, LT, GT, LTU, GTU contained in
4729 KNOWN_RESULT to a CONST_INT, based on the requested comparison CODE
4730 For KNOWN_RESULT to make sense it should be either CMP_EQ, or the
4731 logical OR of one of (CMP_LT, CMP_GT) and one of (CMP_LTU, CMP_GTU).
4732 For floating-point comparisons, assume that the operands were ordered. */
4735 comparison_result (enum rtx_code code
, int known_results
)
4741 return (known_results
& CMP_EQ
) ? const_true_rtx
: const0_rtx
;
4744 return (known_results
& CMP_EQ
) ? const0_rtx
: const_true_rtx
;
4748 return (known_results
& CMP_LT
) ? const_true_rtx
: const0_rtx
;
4751 return (known_results
& CMP_LT
) ? const0_rtx
: const_true_rtx
;
4755 return (known_results
& CMP_GT
) ? const_true_rtx
: const0_rtx
;
4758 return (known_results
& CMP_GT
) ? const0_rtx
: const_true_rtx
;
4761 return (known_results
& CMP_LTU
) ? const_true_rtx
: const0_rtx
;
4763 return (known_results
& CMP_LTU
) ? const0_rtx
: const_true_rtx
;
4766 return (known_results
& CMP_GTU
) ? const_true_rtx
: const0_rtx
;
4768 return (known_results
& CMP_GTU
) ? const0_rtx
: const_true_rtx
;
4771 return const_true_rtx
;
4779 /* Check if the given comparison (done in the given MODE) is actually
4780 a tautology or a contradiction. If the mode is VOID_mode, the
4781 comparison is done in "infinite precision". If no simplification
4782 is possible, this function returns zero. Otherwise, it returns
4783 either const_true_rtx or const0_rtx. */
4786 simplify_const_relational_operation (enum rtx_code code
,
4794 gcc_assert (mode
!= VOIDmode
4795 || (GET_MODE (op0
) == VOIDmode
4796 && GET_MODE (op1
) == VOIDmode
));
4798 /* If op0 is a compare, extract the comparison arguments from it. */
4799 if (GET_CODE (op0
) == COMPARE
&& op1
== const0_rtx
)
4801 op1
= XEXP (op0
, 1);
4802 op0
= XEXP (op0
, 0);
4804 if (GET_MODE (op0
) != VOIDmode
)
4805 mode
= GET_MODE (op0
);
4806 else if (GET_MODE (op1
) != VOIDmode
)
4807 mode
= GET_MODE (op1
);
4812 /* We can't simplify MODE_CC values since we don't know what the
4813 actual comparison is. */
4814 if (GET_MODE_CLASS (GET_MODE (op0
)) == MODE_CC
|| CC0_P (op0
))
4817 /* Make sure the constant is second. */
4818 if (swap_commutative_operands_p (op0
, op1
))
4820 std::swap (op0
, op1
);
4821 code
= swap_condition (code
);
4824 trueop0
= avoid_constant_pool_reference (op0
);
4825 trueop1
= avoid_constant_pool_reference (op1
);
4827 /* For integer comparisons of A and B maybe we can simplify A - B and can
4828 then simplify a comparison of that with zero. If A and B are both either
4829 a register or a CONST_INT, this can't help; testing for these cases will
4830 prevent infinite recursion here and speed things up.
4832 We can only do this for EQ and NE comparisons as otherwise we may
4833 lose or introduce overflow which we cannot disregard as undefined as
4834 we do not know the signedness of the operation on either the left or
4835 the right hand side of the comparison. */
4837 if (INTEGRAL_MODE_P (mode
) && trueop1
!= const0_rtx
4838 && (code
== EQ
|| code
== NE
)
4839 && ! ((REG_P (op0
) || CONST_INT_P (trueop0
))
4840 && (REG_P (op1
) || CONST_INT_P (trueop1
)))
4841 && 0 != (tem
= simplify_binary_operation (MINUS
, mode
, op0
, op1
))
4842 /* We cannot do this if tem is a nonzero address. */
4843 && ! nonzero_address_p (tem
))
4844 return simplify_const_relational_operation (signed_condition (code
),
4845 mode
, tem
, const0_rtx
);
4847 if (! HONOR_NANS (mode
) && code
== ORDERED
)
4848 return const_true_rtx
;
4850 if (! HONOR_NANS (mode
) && code
== UNORDERED
)
4853 /* For modes without NaNs, if the two operands are equal, we know the
4854 result except if they have side-effects. Even with NaNs we know
4855 the result of unordered comparisons and, if signaling NaNs are
4856 irrelevant, also the result of LT/GT/LTGT. */
4857 if ((! HONOR_NANS (trueop0
)
4858 || code
== UNEQ
|| code
== UNLE
|| code
== UNGE
4859 || ((code
== LT
|| code
== GT
|| code
== LTGT
)
4860 && ! HONOR_SNANS (trueop0
)))
4861 && rtx_equal_p (trueop0
, trueop1
)
4862 && ! side_effects_p (trueop0
))
4863 return comparison_result (code
, CMP_EQ
);
4865 /* If the operands are floating-point constants, see if we can fold
4867 if (CONST_DOUBLE_AS_FLOAT_P (trueop0
)
4868 && CONST_DOUBLE_AS_FLOAT_P (trueop1
)
4869 && SCALAR_FLOAT_MODE_P (GET_MODE (trueop0
)))
4871 REAL_VALUE_TYPE d0
, d1
;
4873 REAL_VALUE_FROM_CONST_DOUBLE (d0
, trueop0
);
4874 REAL_VALUE_FROM_CONST_DOUBLE (d1
, trueop1
);
4876 /* Comparisons are unordered iff at least one of the values is NaN. */
4877 if (REAL_VALUE_ISNAN (d0
) || REAL_VALUE_ISNAN (d1
))
4887 return const_true_rtx
;
4900 return comparison_result (code
,
4901 (REAL_VALUES_EQUAL (d0
, d1
) ? CMP_EQ
:
4902 REAL_VALUES_LESS (d0
, d1
) ? CMP_LT
: CMP_GT
));
4905 /* Otherwise, see if the operands are both integers. */
4906 if ((GET_MODE_CLASS (mode
) == MODE_INT
|| mode
== VOIDmode
)
4907 && CONST_SCALAR_INT_P (trueop0
) && CONST_SCALAR_INT_P (trueop1
))
4909 /* It would be nice if we really had a mode here. However, the
4910 largest int representable on the target is as good as
4912 machine_mode cmode
= (mode
== VOIDmode
) ? MAX_MODE_INT
: mode
;
4913 rtx_mode_t ptrueop0
= std::make_pair (trueop0
, cmode
);
4914 rtx_mode_t ptrueop1
= std::make_pair (trueop1
, cmode
);
4916 if (wi::eq_p (ptrueop0
, ptrueop1
))
4917 return comparison_result (code
, CMP_EQ
);
4920 int cr
= wi::lts_p (ptrueop0
, ptrueop1
) ? CMP_LT
: CMP_GT
;
4921 cr
|= wi::ltu_p (ptrueop0
, ptrueop1
) ? CMP_LTU
: CMP_GTU
;
4922 return comparison_result (code
, cr
);
4926 /* Optimize comparisons with upper and lower bounds. */
4927 if (HWI_COMPUTABLE_MODE_P (mode
)
4928 && CONST_INT_P (trueop1
))
4931 unsigned HOST_WIDE_INT nonzero
= nonzero_bits (trueop0
, mode
);
4932 HOST_WIDE_INT val
= INTVAL (trueop1
);
4933 HOST_WIDE_INT mmin
, mmax
;
4943 /* Get a reduced range if the sign bit is zero. */
4944 if (nonzero
<= (GET_MODE_MASK (mode
) >> 1))
4951 rtx mmin_rtx
, mmax_rtx
;
4952 get_mode_bounds (mode
, sign
, mode
, &mmin_rtx
, &mmax_rtx
);
4954 mmin
= INTVAL (mmin_rtx
);
4955 mmax
= INTVAL (mmax_rtx
);
4958 unsigned int sign_copies
= num_sign_bit_copies (trueop0
, mode
);
4960 mmin
>>= (sign_copies
- 1);
4961 mmax
>>= (sign_copies
- 1);
4967 /* x >= y is always true for y <= mmin, always false for y > mmax. */
4969 if ((unsigned HOST_WIDE_INT
) val
<= (unsigned HOST_WIDE_INT
) mmin
)
4970 return const_true_rtx
;
4971 if ((unsigned HOST_WIDE_INT
) val
> (unsigned HOST_WIDE_INT
) mmax
)
4976 return const_true_rtx
;
4981 /* x <= y is always true for y >= mmax, always false for y < mmin. */
4983 if ((unsigned HOST_WIDE_INT
) val
>= (unsigned HOST_WIDE_INT
) mmax
)
4984 return const_true_rtx
;
4985 if ((unsigned HOST_WIDE_INT
) val
< (unsigned HOST_WIDE_INT
) mmin
)
4990 return const_true_rtx
;
4996 /* x == y is always false for y out of range. */
4997 if (val
< mmin
|| val
> mmax
)
5001 /* x > y is always false for y >= mmax, always true for y < mmin. */
5003 if ((unsigned HOST_WIDE_INT
) val
>= (unsigned HOST_WIDE_INT
) mmax
)
5005 if ((unsigned HOST_WIDE_INT
) val
< (unsigned HOST_WIDE_INT
) mmin
)
5006 return const_true_rtx
;
5012 return const_true_rtx
;
5015 /* x < y is always false for y <= mmin, always true for y > mmax. */
5017 if ((unsigned HOST_WIDE_INT
) val
<= (unsigned HOST_WIDE_INT
) mmin
)
5019 if ((unsigned HOST_WIDE_INT
) val
> (unsigned HOST_WIDE_INT
) mmax
)
5020 return const_true_rtx
;
5026 return const_true_rtx
;
5030 /* x != y is always true for y out of range. */
5031 if (val
< mmin
|| val
> mmax
)
5032 return const_true_rtx
;
5040 /* Optimize integer comparisons with zero. */
5041 if (trueop1
== const0_rtx
)
5043 /* Some addresses are known to be nonzero. We don't know
5044 their sign, but equality comparisons are known. */
5045 if (nonzero_address_p (trueop0
))
5047 if (code
== EQ
|| code
== LEU
)
5049 if (code
== NE
|| code
== GTU
)
5050 return const_true_rtx
;
5053 /* See if the first operand is an IOR with a constant. If so, we
5054 may be able to determine the result of this comparison. */
5055 if (GET_CODE (op0
) == IOR
)
5057 rtx inner_const
= avoid_constant_pool_reference (XEXP (op0
, 1));
5058 if (CONST_INT_P (inner_const
) && inner_const
!= const0_rtx
)
5060 int sign_bitnum
= GET_MODE_PRECISION (mode
) - 1;
5061 int has_sign
= (HOST_BITS_PER_WIDE_INT
>= sign_bitnum
5062 && (UINTVAL (inner_const
)
5063 & ((unsigned HOST_WIDE_INT
) 1
5073 return const_true_rtx
;
5077 return const_true_rtx
;
5091 /* Optimize comparison of ABS with zero. */
5092 if (trueop1
== CONST0_RTX (mode
)
5093 && (GET_CODE (trueop0
) == ABS
5094 || (GET_CODE (trueop0
) == FLOAT_EXTEND
5095 && GET_CODE (XEXP (trueop0
, 0)) == ABS
)))
5100 /* Optimize abs(x) < 0.0. */
5101 if (!HONOR_SNANS (mode
)
5102 && (!INTEGRAL_MODE_P (mode
)
5103 || (!flag_wrapv
&& !flag_trapv
&& flag_strict_overflow
)))
5105 if (INTEGRAL_MODE_P (mode
)
5106 && (issue_strict_overflow_warning
5107 (WARN_STRICT_OVERFLOW_CONDITIONAL
)))
5108 warning (OPT_Wstrict_overflow
,
5109 ("assuming signed overflow does not occur when "
5110 "assuming abs (x) < 0 is false"));
5116 /* Optimize abs(x) >= 0.0. */
5117 if (!HONOR_NANS (mode
)
5118 && (!INTEGRAL_MODE_P (mode
)
5119 || (!flag_wrapv
&& !flag_trapv
&& flag_strict_overflow
)))
5121 if (INTEGRAL_MODE_P (mode
)
5122 && (issue_strict_overflow_warning
5123 (WARN_STRICT_OVERFLOW_CONDITIONAL
)))
5124 warning (OPT_Wstrict_overflow
,
5125 ("assuming signed overflow does not occur when "
5126 "assuming abs (x) >= 0 is true"));
5127 return const_true_rtx
;
5132 /* Optimize ! (abs(x) < 0.0). */
5133 return const_true_rtx
;
5143 /* Simplify CODE, an operation with result mode MODE and three operands,
5144 OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
5145 a constant. Return 0 if no simplifications is possible. */
5148 simplify_ternary_operation (enum rtx_code code
, machine_mode mode
,
5149 machine_mode op0_mode
, rtx op0
, rtx op1
,
5152 unsigned int width
= GET_MODE_PRECISION (mode
);
5153 bool any_change
= false;
5156 /* VOIDmode means "infinite" precision. */
5158 width
= HOST_BITS_PER_WIDE_INT
;
5163 /* Simplify negations around the multiplication. */
5164 /* -a * -b + c => a * b + c. */
5165 if (GET_CODE (op0
) == NEG
)
5167 tem
= simplify_unary_operation (NEG
, mode
, op1
, mode
);
5169 op1
= tem
, op0
= XEXP (op0
, 0), any_change
= true;
5171 else if (GET_CODE (op1
) == NEG
)
5173 tem
= simplify_unary_operation (NEG
, mode
, op0
, mode
);
5175 op0
= tem
, op1
= XEXP (op1
, 0), any_change
= true;
5178 /* Canonicalize the two multiplication operands. */
5179 /* a * -b + c => -b * a + c. */
5180 if (swap_commutative_operands_p (op0
, op1
))
5181 std::swap (op0
, op1
), any_change
= true;
5184 return gen_rtx_FMA (mode
, op0
, op1
, op2
);
5189 if (CONST_INT_P (op0
)
5190 && CONST_INT_P (op1
)
5191 && CONST_INT_P (op2
)
5192 && ((unsigned) INTVAL (op1
) + (unsigned) INTVAL (op2
) <= width
)
5193 && width
<= (unsigned) HOST_BITS_PER_WIDE_INT
)
5195 /* Extracting a bit-field from a constant */
5196 unsigned HOST_WIDE_INT val
= UINTVAL (op0
);
5197 HOST_WIDE_INT op1val
= INTVAL (op1
);
5198 HOST_WIDE_INT op2val
= INTVAL (op2
);
5199 if (BITS_BIG_ENDIAN
)
5200 val
>>= GET_MODE_PRECISION (op0_mode
) - op2val
- op1val
;
5204 if (HOST_BITS_PER_WIDE_INT
!= op1val
)
5206 /* First zero-extend. */
5207 val
&= ((unsigned HOST_WIDE_INT
) 1 << op1val
) - 1;
5208 /* If desired, propagate sign bit. */
5209 if (code
== SIGN_EXTRACT
5210 && (val
& ((unsigned HOST_WIDE_INT
) 1 << (op1val
- 1)))
5212 val
|= ~ (((unsigned HOST_WIDE_INT
) 1 << op1val
) - 1);
5215 return gen_int_mode (val
, mode
);
5220 if (CONST_INT_P (op0
))
5221 return op0
!= const0_rtx
? op1
: op2
;
5223 /* Convert c ? a : a into "a". */
5224 if (rtx_equal_p (op1
, op2
) && ! side_effects_p (op0
))
5227 /* Convert a != b ? a : b into "a". */
5228 if (GET_CODE (op0
) == NE
5229 && ! side_effects_p (op0
)
5230 && ! HONOR_NANS (mode
)
5231 && ! HONOR_SIGNED_ZEROS (mode
)
5232 && ((rtx_equal_p (XEXP (op0
, 0), op1
)
5233 && rtx_equal_p (XEXP (op0
, 1), op2
))
5234 || (rtx_equal_p (XEXP (op0
, 0), op2
)
5235 && rtx_equal_p (XEXP (op0
, 1), op1
))))
5238 /* Convert a == b ? a : b into "b". */
5239 if (GET_CODE (op0
) == EQ
5240 && ! side_effects_p (op0
)
5241 && ! HONOR_NANS (mode
)
5242 && ! HONOR_SIGNED_ZEROS (mode
)
5243 && ((rtx_equal_p (XEXP (op0
, 0), op1
)
5244 && rtx_equal_p (XEXP (op0
, 1), op2
))
5245 || (rtx_equal_p (XEXP (op0
, 0), op2
)
5246 && rtx_equal_p (XEXP (op0
, 1), op1
))))
5249 if (COMPARISON_P (op0
) && ! side_effects_p (op0
))
5251 machine_mode cmp_mode
= (GET_MODE (XEXP (op0
, 0)) == VOIDmode
5252 ? GET_MODE (XEXP (op0
, 1))
5253 : GET_MODE (XEXP (op0
, 0)));
5256 /* Look for happy constants in op1 and op2. */
5257 if (CONST_INT_P (op1
) && CONST_INT_P (op2
))
5259 HOST_WIDE_INT t
= INTVAL (op1
);
5260 HOST_WIDE_INT f
= INTVAL (op2
);
5262 if (t
== STORE_FLAG_VALUE
&& f
== 0)
5263 code
= GET_CODE (op0
);
5264 else if (t
== 0 && f
== STORE_FLAG_VALUE
)
5267 tmp
= reversed_comparison_code (op0
, NULL_RTX
);
5275 return simplify_gen_relational (code
, mode
, cmp_mode
,
5276 XEXP (op0
, 0), XEXP (op0
, 1));
5279 if (cmp_mode
== VOIDmode
)
5280 cmp_mode
= op0_mode
;
5281 temp
= simplify_relational_operation (GET_CODE (op0
), op0_mode
,
5282 cmp_mode
, XEXP (op0
, 0),
5285 /* See if any simplifications were possible. */
5288 if (CONST_INT_P (temp
))
5289 return temp
== const0_rtx
? op2
: op1
;
5291 return gen_rtx_IF_THEN_ELSE (mode
, temp
, op1
, op2
);
5297 gcc_assert (GET_MODE (op0
) == mode
);
5298 gcc_assert (GET_MODE (op1
) == mode
);
5299 gcc_assert (VECTOR_MODE_P (mode
));
5300 trueop2
= avoid_constant_pool_reference (op2
);
5301 if (CONST_INT_P (trueop2
))
5303 int elt_size
= GET_MODE_SIZE (GET_MODE_INNER (mode
));
5304 unsigned n_elts
= (GET_MODE_SIZE (mode
) / elt_size
);
5305 unsigned HOST_WIDE_INT sel
= UINTVAL (trueop2
);
5306 unsigned HOST_WIDE_INT mask
;
5307 if (n_elts
== HOST_BITS_PER_WIDE_INT
)
5310 mask
= ((unsigned HOST_WIDE_INT
) 1 << n_elts
) - 1;
5312 if (!(sel
& mask
) && !side_effects_p (op0
))
5314 if ((sel
& mask
) == mask
&& !side_effects_p (op1
))
5317 rtx trueop0
= avoid_constant_pool_reference (op0
);
5318 rtx trueop1
= avoid_constant_pool_reference (op1
);
5319 if (GET_CODE (trueop0
) == CONST_VECTOR
5320 && GET_CODE (trueop1
) == CONST_VECTOR
)
5322 rtvec v
= rtvec_alloc (n_elts
);
5325 for (i
= 0; i
< n_elts
; i
++)
5326 RTVEC_ELT (v
, i
) = ((sel
& ((unsigned HOST_WIDE_INT
) 1 << i
))
5327 ? CONST_VECTOR_ELT (trueop0
, i
)
5328 : CONST_VECTOR_ELT (trueop1
, i
));
5329 return gen_rtx_CONST_VECTOR (mode
, v
);
5332 /* Replace (vec_merge (vec_merge a b m) c n) with (vec_merge b c n)
5333 if no element from a appears in the result. */
5334 if (GET_CODE (op0
) == VEC_MERGE
)
5336 tem
= avoid_constant_pool_reference (XEXP (op0
, 2));
5337 if (CONST_INT_P (tem
))
5339 unsigned HOST_WIDE_INT sel0
= UINTVAL (tem
);
5340 if (!(sel
& sel0
& mask
) && !side_effects_p (XEXP (op0
, 0)))
5341 return simplify_gen_ternary (code
, mode
, mode
,
5342 XEXP (op0
, 1), op1
, op2
);
5343 if (!(sel
& ~sel0
& mask
) && !side_effects_p (XEXP (op0
, 1)))
5344 return simplify_gen_ternary (code
, mode
, mode
,
5345 XEXP (op0
, 0), op1
, op2
);
5348 if (GET_CODE (op1
) == VEC_MERGE
)
5350 tem
= avoid_constant_pool_reference (XEXP (op1
, 2));
5351 if (CONST_INT_P (tem
))
5353 unsigned HOST_WIDE_INT sel1
= UINTVAL (tem
);
5354 if (!(~sel
& sel1
& mask
) && !side_effects_p (XEXP (op1
, 0)))
5355 return simplify_gen_ternary (code
, mode
, mode
,
5356 op0
, XEXP (op1
, 1), op2
);
5357 if (!(~sel
& ~sel1
& mask
) && !side_effects_p (XEXP (op1
, 1)))
5358 return simplify_gen_ternary (code
, mode
, mode
,
5359 op0
, XEXP (op1
, 0), op2
);
5363 /* Replace (vec_merge (vec_duplicate (vec_select a parallel (i))) a 1 << i)
5365 if (GET_CODE (op0
) == VEC_DUPLICATE
5366 && GET_CODE (XEXP (op0
, 0)) == VEC_SELECT
5367 && GET_CODE (XEXP (XEXP (op0
, 0), 1)) == PARALLEL
5368 && mode_nunits
[GET_MODE (XEXP (op0
, 0))] == 1)
5370 tem
= XVECEXP ((XEXP (XEXP (op0
, 0), 1)), 0, 0);
5371 if (CONST_INT_P (tem
) && CONST_INT_P (op2
))
5373 if (XEXP (XEXP (op0
, 0), 0) == op1
5374 && UINTVAL (op2
) == HOST_WIDE_INT_1U
<< UINTVAL (tem
))
5380 if (rtx_equal_p (op0
, op1
)
5381 && !side_effects_p (op2
) && !side_effects_p (op1
))
5393 /* Evaluate a SUBREG of a CONST_INT or CONST_WIDE_INT or CONST_DOUBLE
5394 or CONST_FIXED or CONST_VECTOR, returning another CONST_INT or
5395 CONST_WIDE_INT or CONST_DOUBLE or CONST_FIXED or CONST_VECTOR.
5397 Works by unpacking OP into a collection of 8-bit values
5398 represented as a little-endian array of 'unsigned char', selecting by BYTE,
5399 and then repacking them again for OUTERMODE. */
5402 simplify_immed_subreg (machine_mode outermode
, rtx op
,
5403 machine_mode innermode
, unsigned int byte
)
5407 value_mask
= (1 << value_bit
) - 1
5409 unsigned char value
[MAX_BITSIZE_MODE_ANY_MODE
/ value_bit
];
5418 rtvec result_v
= NULL
;
5419 enum mode_class outer_class
;
5420 machine_mode outer_submode
;
5423 /* Some ports misuse CCmode. */
5424 if (GET_MODE_CLASS (outermode
) == MODE_CC
&& CONST_INT_P (op
))
5427 /* We have no way to represent a complex constant at the rtl level. */
5428 if (COMPLEX_MODE_P (outermode
))
5431 /* We support any size mode. */
5432 max_bitsize
= MAX (GET_MODE_BITSIZE (outermode
),
5433 GET_MODE_BITSIZE (innermode
));
5435 /* Unpack the value. */
5437 if (GET_CODE (op
) == CONST_VECTOR
)
5439 num_elem
= CONST_VECTOR_NUNITS (op
);
5440 elems
= &CONST_VECTOR_ELT (op
, 0);
5441 elem_bitsize
= GET_MODE_BITSIZE (GET_MODE_INNER (innermode
));
5447 elem_bitsize
= max_bitsize
;
5449 /* If this asserts, it is too complicated; reducing value_bit may help. */
5450 gcc_assert (BITS_PER_UNIT
% value_bit
== 0);
5451 /* I don't know how to handle endianness of sub-units. */
5452 gcc_assert (elem_bitsize
% BITS_PER_UNIT
== 0);
5454 for (elem
= 0; elem
< num_elem
; elem
++)
5457 rtx el
= elems
[elem
];
5459 /* Vectors are kept in target memory order. (This is probably
5462 unsigned byte
= (elem
* elem_bitsize
) / BITS_PER_UNIT
;
5463 unsigned ibyte
= (((num_elem
- 1 - elem
) * elem_bitsize
)
5465 unsigned word_byte
= WORDS_BIG_ENDIAN
? ibyte
: byte
;
5466 unsigned subword_byte
= BYTES_BIG_ENDIAN
? ibyte
: byte
;
5467 unsigned bytele
= (subword_byte
% UNITS_PER_WORD
5468 + (word_byte
/ UNITS_PER_WORD
) * UNITS_PER_WORD
);
5469 vp
= value
+ (bytele
* BITS_PER_UNIT
) / value_bit
;
5472 switch (GET_CODE (el
))
5476 i
< HOST_BITS_PER_WIDE_INT
&& i
< elem_bitsize
;
5478 *vp
++ = INTVAL (el
) >> i
;
5479 /* CONST_INTs are always logically sign-extended. */
5480 for (; i
< elem_bitsize
; i
+= value_bit
)
5481 *vp
++ = INTVAL (el
) < 0 ? -1 : 0;
5484 case CONST_WIDE_INT
:
5486 rtx_mode_t val
= std::make_pair (el
, innermode
);
5487 unsigned char extend
= wi::sign_mask (val
);
5489 for (i
= 0; i
< elem_bitsize
; i
+= value_bit
)
5490 *vp
++ = wi::extract_uhwi (val
, i
, value_bit
);
5491 for (; i
< elem_bitsize
; i
+= value_bit
)
5497 if (TARGET_SUPPORTS_WIDE_INT
== 0 && GET_MODE (el
) == VOIDmode
)
5499 unsigned char extend
= 0;
5500 /* If this triggers, someone should have generated a
5501 CONST_INT instead. */
5502 gcc_assert (elem_bitsize
> HOST_BITS_PER_WIDE_INT
);
5504 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
+= value_bit
)
5505 *vp
++ = CONST_DOUBLE_LOW (el
) >> i
;
5506 while (i
< HOST_BITS_PER_DOUBLE_INT
&& i
< elem_bitsize
)
5509 = CONST_DOUBLE_HIGH (el
) >> (i
- HOST_BITS_PER_WIDE_INT
);
5513 if (CONST_DOUBLE_HIGH (el
) >> (HOST_BITS_PER_WIDE_INT
- 1))
5515 for (; i
< elem_bitsize
; i
+= value_bit
)
5520 /* This is big enough for anything on the platform. */
5521 long tmp
[MAX_BITSIZE_MODE_ANY_MODE
/ 32];
5522 int bitsize
= GET_MODE_BITSIZE (GET_MODE (el
));
5524 gcc_assert (SCALAR_FLOAT_MODE_P (GET_MODE (el
)));
5525 gcc_assert (bitsize
<= elem_bitsize
);
5526 gcc_assert (bitsize
% value_bit
== 0);
5528 real_to_target (tmp
, CONST_DOUBLE_REAL_VALUE (el
),
5531 /* real_to_target produces its result in words affected by
5532 FLOAT_WORDS_BIG_ENDIAN. However, we ignore this,
5533 and use WORDS_BIG_ENDIAN instead; see the documentation
5534 of SUBREG in rtl.texi. */
5535 for (i
= 0; i
< bitsize
; i
+= value_bit
)
5538 if (WORDS_BIG_ENDIAN
)
5539 ibase
= bitsize
- 1 - i
;
5542 *vp
++ = tmp
[ibase
/ 32] >> i
% 32;
5545 /* It shouldn't matter what's done here, so fill it with
5547 for (; i
< elem_bitsize
; i
+= value_bit
)
5553 if (elem_bitsize
<= HOST_BITS_PER_WIDE_INT
)
5555 for (i
= 0; i
< elem_bitsize
; i
+= value_bit
)
5556 *vp
++ = CONST_FIXED_VALUE_LOW (el
) >> i
;
5560 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
+= value_bit
)
5561 *vp
++ = CONST_FIXED_VALUE_LOW (el
) >> i
;
5562 for (; i
< HOST_BITS_PER_DOUBLE_INT
&& i
< elem_bitsize
;
5564 *vp
++ = CONST_FIXED_VALUE_HIGH (el
)
5565 >> (i
- HOST_BITS_PER_WIDE_INT
);
5566 for (; i
< elem_bitsize
; i
+= value_bit
)
5576 /* Now, pick the right byte to start with. */
5577 /* Renumber BYTE so that the least-significant byte is byte 0. A special
5578 case is paradoxical SUBREGs, which shouldn't be adjusted since they
5579 will already have offset 0. */
5580 if (GET_MODE_SIZE (innermode
) >= GET_MODE_SIZE (outermode
))
5582 unsigned ibyte
= (GET_MODE_SIZE (innermode
) - GET_MODE_SIZE (outermode
)
5584 unsigned word_byte
= WORDS_BIG_ENDIAN
? ibyte
: byte
;
5585 unsigned subword_byte
= BYTES_BIG_ENDIAN
? ibyte
: byte
;
5586 byte
= (subword_byte
% UNITS_PER_WORD
5587 + (word_byte
/ UNITS_PER_WORD
) * UNITS_PER_WORD
);
5590 /* BYTE should still be inside OP. (Note that BYTE is unsigned,
5591 so if it's become negative it will instead be very large.) */
5592 gcc_assert (byte
< GET_MODE_SIZE (innermode
));
5594 /* Convert from bytes to chunks of size value_bit. */
5595 value_start
= byte
* (BITS_PER_UNIT
/ value_bit
);
5597 /* Re-pack the value. */
5599 if (VECTOR_MODE_P (outermode
))
5601 num_elem
= GET_MODE_NUNITS (outermode
);
5602 result_v
= rtvec_alloc (num_elem
);
5603 elems
= &RTVEC_ELT (result_v
, 0);
5604 outer_submode
= GET_MODE_INNER (outermode
);
5610 outer_submode
= outermode
;
5613 outer_class
= GET_MODE_CLASS (outer_submode
);
5614 elem_bitsize
= GET_MODE_BITSIZE (outer_submode
);
5616 gcc_assert (elem_bitsize
% value_bit
== 0);
5617 gcc_assert (elem_bitsize
+ value_start
* value_bit
<= max_bitsize
);
5619 for (elem
= 0; elem
< num_elem
; elem
++)
5623 /* Vectors are stored in target memory order. (This is probably
5626 unsigned byte
= (elem
* elem_bitsize
) / BITS_PER_UNIT
;
5627 unsigned ibyte
= (((num_elem
- 1 - elem
) * elem_bitsize
)
5629 unsigned word_byte
= WORDS_BIG_ENDIAN
? ibyte
: byte
;
5630 unsigned subword_byte
= BYTES_BIG_ENDIAN
? ibyte
: byte
;
5631 unsigned bytele
= (subword_byte
% UNITS_PER_WORD
5632 + (word_byte
/ UNITS_PER_WORD
) * UNITS_PER_WORD
);
5633 vp
= value
+ value_start
+ (bytele
* BITS_PER_UNIT
) / value_bit
;
5636 switch (outer_class
)
5639 case MODE_PARTIAL_INT
:
5644 = (GET_MODE_BITSIZE (outer_submode
) + HOST_BITS_PER_WIDE_INT
- 1)
5645 / HOST_BITS_PER_WIDE_INT
;
5646 HOST_WIDE_INT tmp
[MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
];
5649 if (GET_MODE_PRECISION (outer_submode
) > MAX_BITSIZE_MODE_ANY_INT
)
5651 for (u
= 0; u
< units
; u
++)
5653 unsigned HOST_WIDE_INT buf
= 0;
5655 i
< HOST_BITS_PER_WIDE_INT
&& base
+ i
< elem_bitsize
;
5657 buf
|= (unsigned HOST_WIDE_INT
)(*vp
++ & value_mask
) << i
;
5660 base
+= HOST_BITS_PER_WIDE_INT
;
5662 r
= wide_int::from_array (tmp
, units
,
5663 GET_MODE_PRECISION (outer_submode
));
5664 #if TARGET_SUPPORTS_WIDE_INT == 0
5665 /* Make sure r will fit into CONST_INT or CONST_DOUBLE. */
5666 if (wi::min_precision (r
, SIGNED
) > HOST_BITS_PER_DOUBLE_INT
)
5669 elems
[elem
] = immed_wide_int_const (r
, outer_submode
);
5674 case MODE_DECIMAL_FLOAT
:
5677 long tmp
[MAX_BITSIZE_MODE_ANY_MODE
/ 32];
5679 /* real_from_target wants its input in words affected by
5680 FLOAT_WORDS_BIG_ENDIAN. However, we ignore this,
5681 and use WORDS_BIG_ENDIAN instead; see the documentation
5682 of SUBREG in rtl.texi. */
5683 for (i
= 0; i
< max_bitsize
/ 32; i
++)
5685 for (i
= 0; i
< elem_bitsize
; i
+= value_bit
)
5688 if (WORDS_BIG_ENDIAN
)
5689 ibase
= elem_bitsize
- 1 - i
;
5692 tmp
[ibase
/ 32] |= (*vp
++ & value_mask
) << i
% 32;
5695 real_from_target (&r
, tmp
, outer_submode
);
5696 elems
[elem
] = CONST_DOUBLE_FROM_REAL_VALUE (r
, outer_submode
);
5708 f
.mode
= outer_submode
;
5711 i
< HOST_BITS_PER_WIDE_INT
&& i
< elem_bitsize
;
5713 f
.data
.low
|= (unsigned HOST_WIDE_INT
)(*vp
++ & value_mask
) << i
;
5714 for (; i
< elem_bitsize
; i
+= value_bit
)
5715 f
.data
.high
|= ((unsigned HOST_WIDE_INT
)(*vp
++ & value_mask
)
5716 << (i
- HOST_BITS_PER_WIDE_INT
));
5718 elems
[elem
] = CONST_FIXED_FROM_FIXED_VALUE (f
, outer_submode
);
5726 if (VECTOR_MODE_P (outermode
))
5727 return gen_rtx_CONST_VECTOR (outermode
, result_v
);
5732 /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
5733 Return 0 if no simplifications are possible. */
5735 simplify_subreg (machine_mode outermode
, rtx op
,
5736 machine_mode innermode
, unsigned int byte
)
5738 /* Little bit of sanity checking. */
5739 gcc_assert (innermode
!= VOIDmode
);
5740 gcc_assert (outermode
!= VOIDmode
);
5741 gcc_assert (innermode
!= BLKmode
);
5742 gcc_assert (outermode
!= BLKmode
);
5744 gcc_assert (GET_MODE (op
) == innermode
5745 || GET_MODE (op
) == VOIDmode
);
5747 if ((byte
% GET_MODE_SIZE (outermode
)) != 0)
5750 if (byte
>= GET_MODE_SIZE (innermode
))
5753 if (outermode
== innermode
&& !byte
)
5756 if (CONST_SCALAR_INT_P (op
)
5757 || CONST_DOUBLE_AS_FLOAT_P (op
)
5758 || GET_CODE (op
) == CONST_FIXED
5759 || GET_CODE (op
) == CONST_VECTOR
)
5760 return simplify_immed_subreg (outermode
, op
, innermode
, byte
);
5762 /* Changing mode twice with SUBREG => just change it once,
5763 or not at all if changing back op starting mode. */
5764 if (GET_CODE (op
) == SUBREG
)
5766 machine_mode innermostmode
= GET_MODE (SUBREG_REG (op
));
5767 int final_offset
= byte
+ SUBREG_BYTE (op
);
5770 if (outermode
== innermostmode
5771 && byte
== 0 && SUBREG_BYTE (op
) == 0)
5772 return SUBREG_REG (op
);
5774 /* The SUBREG_BYTE represents offset, as if the value were stored
5775 in memory. Irritating exception is paradoxical subreg, where
5776 we define SUBREG_BYTE to be 0. On big endian machines, this
5777 value should be negative. For a moment, undo this exception. */
5778 if (byte
== 0 && GET_MODE_SIZE (innermode
) < GET_MODE_SIZE (outermode
))
5780 int difference
= (GET_MODE_SIZE (innermode
) - GET_MODE_SIZE (outermode
));
5781 if (WORDS_BIG_ENDIAN
)
5782 final_offset
+= (difference
/ UNITS_PER_WORD
) * UNITS_PER_WORD
;
5783 if (BYTES_BIG_ENDIAN
)
5784 final_offset
+= difference
% UNITS_PER_WORD
;
5786 if (SUBREG_BYTE (op
) == 0
5787 && GET_MODE_SIZE (innermostmode
) < GET_MODE_SIZE (innermode
))
5789 int difference
= (GET_MODE_SIZE (innermostmode
) - GET_MODE_SIZE (innermode
));
5790 if (WORDS_BIG_ENDIAN
)
5791 final_offset
+= (difference
/ UNITS_PER_WORD
) * UNITS_PER_WORD
;
5792 if (BYTES_BIG_ENDIAN
)
5793 final_offset
+= difference
% UNITS_PER_WORD
;
5796 /* See whether resulting subreg will be paradoxical. */
5797 if (GET_MODE_SIZE (innermostmode
) > GET_MODE_SIZE (outermode
))
5799 /* In nonparadoxical subregs we can't handle negative offsets. */
5800 if (final_offset
< 0)
5802 /* Bail out in case resulting subreg would be incorrect. */
5803 if (final_offset
% GET_MODE_SIZE (outermode
)
5804 || (unsigned) final_offset
>= GET_MODE_SIZE (innermostmode
))
5810 int difference
= (GET_MODE_SIZE (innermostmode
) - GET_MODE_SIZE (outermode
));
5812 /* In paradoxical subreg, see if we are still looking on lower part.
5813 If so, our SUBREG_BYTE will be 0. */
5814 if (WORDS_BIG_ENDIAN
)
5815 offset
+= (difference
/ UNITS_PER_WORD
) * UNITS_PER_WORD
;
5816 if (BYTES_BIG_ENDIAN
)
5817 offset
+= difference
% UNITS_PER_WORD
;
5818 if (offset
== final_offset
)
5824 /* Recurse for further possible simplifications. */
5825 newx
= simplify_subreg (outermode
, SUBREG_REG (op
), innermostmode
,
5829 if (validate_subreg (outermode
, innermostmode
,
5830 SUBREG_REG (op
), final_offset
))
5832 newx
= gen_rtx_SUBREG (outermode
, SUBREG_REG (op
), final_offset
);
5833 if (SUBREG_PROMOTED_VAR_P (op
)
5834 && SUBREG_PROMOTED_SIGN (op
) >= 0
5835 && GET_MODE_CLASS (outermode
) == MODE_INT
5836 && IN_RANGE (GET_MODE_SIZE (outermode
),
5837 GET_MODE_SIZE (innermode
),
5838 GET_MODE_SIZE (innermostmode
))
5839 && subreg_lowpart_p (newx
))
5841 SUBREG_PROMOTED_VAR_P (newx
) = 1;
5842 SUBREG_PROMOTED_SET (newx
, SUBREG_PROMOTED_GET (op
));
5849 /* SUBREG of a hard register => just change the register number
5850 and/or mode. If the hard register is not valid in that mode,
5851 suppress this simplification. If the hard register is the stack,
5852 frame, or argument pointer, leave this as a SUBREG. */
5854 if (REG_P (op
) && HARD_REGISTER_P (op
))
5856 unsigned int regno
, final_regno
;
5859 final_regno
= simplify_subreg_regno (regno
, innermode
, byte
, outermode
);
5860 if (HARD_REGISTER_NUM_P (final_regno
))
5863 int final_offset
= byte
;
5865 /* Adjust offset for paradoxical subregs. */
5867 && GET_MODE_SIZE (innermode
) < GET_MODE_SIZE (outermode
))
5869 int difference
= (GET_MODE_SIZE (innermode
)
5870 - GET_MODE_SIZE (outermode
));
5871 if (WORDS_BIG_ENDIAN
)
5872 final_offset
+= (difference
/ UNITS_PER_WORD
) * UNITS_PER_WORD
;
5873 if (BYTES_BIG_ENDIAN
)
5874 final_offset
+= difference
% UNITS_PER_WORD
;
5877 x
= gen_rtx_REG_offset (op
, outermode
, final_regno
, final_offset
);
5879 /* Propagate original regno. We don't have any way to specify
5880 the offset inside original regno, so do so only for lowpart.
5881 The information is used only by alias analysis that can not
5882 grog partial register anyway. */
5884 if (subreg_lowpart_offset (outermode
, innermode
) == byte
)
5885 ORIGINAL_REGNO (x
) = ORIGINAL_REGNO (op
);
5890 /* If we have a SUBREG of a register that we are replacing and we are
5891 replacing it with a MEM, make a new MEM and try replacing the
5892 SUBREG with it. Don't do this if the MEM has a mode-dependent address
5893 or if we would be widening it. */
5896 && ! mode_dependent_address_p (XEXP (op
, 0), MEM_ADDR_SPACE (op
))
5897 /* Allow splitting of volatile memory references in case we don't
5898 have instruction to move the whole thing. */
5899 && (! MEM_VOLATILE_P (op
)
5900 || ! have_insn_for (SET
, innermode
))
5901 && GET_MODE_SIZE (outermode
) <= GET_MODE_SIZE (GET_MODE (op
)))
5902 return adjust_address_nv (op
, outermode
, byte
);
5904 /* Handle complex values represented as CONCAT
5905 of real and imaginary part. */
5906 if (GET_CODE (op
) == CONCAT
)
5908 unsigned int part_size
, final_offset
;
5911 part_size
= GET_MODE_UNIT_SIZE (GET_MODE (XEXP (op
, 0)));
5912 if (byte
< part_size
)
5914 part
= XEXP (op
, 0);
5915 final_offset
= byte
;
5919 part
= XEXP (op
, 1);
5920 final_offset
= byte
- part_size
;
5923 if (final_offset
+ GET_MODE_SIZE (outermode
) > part_size
)
5926 res
= simplify_subreg (outermode
, part
, GET_MODE (part
), final_offset
);
5929 if (validate_subreg (outermode
, GET_MODE (part
), part
, final_offset
))
5930 return gen_rtx_SUBREG (outermode
, part
, final_offset
);
5934 /* A SUBREG resulting from a zero extension may fold to zero if
5935 it extracts higher bits that the ZERO_EXTEND's source bits. */
5936 if (GET_CODE (op
) == ZERO_EXTEND
&& SCALAR_INT_MODE_P (innermode
))
5938 unsigned int bitpos
= subreg_lsb_1 (outermode
, innermode
, byte
);
5939 if (bitpos
>= GET_MODE_PRECISION (GET_MODE (XEXP (op
, 0))))
5940 return CONST0_RTX (outermode
);
5943 if (SCALAR_INT_MODE_P (outermode
)
5944 && SCALAR_INT_MODE_P (innermode
)
5945 && GET_MODE_PRECISION (outermode
) < GET_MODE_PRECISION (innermode
)
5946 && byte
== subreg_lowpart_offset (outermode
, innermode
))
5948 rtx tem
= simplify_truncation (outermode
, op
, innermode
);
5956 /* Make a SUBREG operation or equivalent if it folds. */
5959 simplify_gen_subreg (machine_mode outermode
, rtx op
,
5960 machine_mode innermode
, unsigned int byte
)
5964 newx
= simplify_subreg (outermode
, op
, innermode
, byte
);
5968 if (GET_CODE (op
) == SUBREG
5969 || GET_CODE (op
) == CONCAT
5970 || GET_MODE (op
) == VOIDmode
)
5973 if (validate_subreg (outermode
, innermode
, op
, byte
))
5974 return gen_rtx_SUBREG (outermode
, op
, byte
);
5979 /* Simplify X, an rtx expression.
5981 Return the simplified expression or NULL if no simplifications
5984 This is the preferred entry point into the simplification routines;
5985 however, we still allow passes to call the more specific routines.
5987 Right now GCC has three (yes, three) major bodies of RTL simplification
5988 code that need to be unified.
5990 1. fold_rtx in cse.c. This code uses various CSE specific
5991 information to aid in RTL simplification.
5993 2. simplify_rtx in combine.c. Similar to fold_rtx, except that
5994 it uses combine specific information to aid in RTL
5997 3. The routines in this file.
6000 Long term we want to only have one body of simplification code; to
6001 get to that state I recommend the following steps:
6003 1. Pour over fold_rtx & simplify_rtx and move any simplifications
6004 which are not pass dependent state into these routines.
6006 2. As code is moved by #1, change fold_rtx & simplify_rtx to
6007 use this routine whenever possible.
6009 3. Allow for pass dependent state to be provided to these
6010 routines and add simplifications based on the pass dependent
6011 state. Remove code from cse.c & combine.c that becomes
6014 It will take time, but ultimately the compiler will be easier to
6015 maintain and improve. It's totally silly that when we add a
6016 simplification that it needs to be added to 4 places (3 for RTL
6017 simplification and 1 for tree simplification. */
6020 simplify_rtx (const_rtx x
)
6022 const enum rtx_code code
= GET_CODE (x
);
6023 const machine_mode mode
= GET_MODE (x
);
6025 switch (GET_RTX_CLASS (code
))
6028 return simplify_unary_operation (code
, mode
,
6029 XEXP (x
, 0), GET_MODE (XEXP (x
, 0)));
6030 case RTX_COMM_ARITH
:
6031 if (swap_commutative_operands_p (XEXP (x
, 0), XEXP (x
, 1)))
6032 return simplify_gen_binary (code
, mode
, XEXP (x
, 1), XEXP (x
, 0));
6034 /* Fall through.... */
6037 return simplify_binary_operation (code
, mode
, XEXP (x
, 0), XEXP (x
, 1));
6040 case RTX_BITFIELD_OPS
:
6041 return simplify_ternary_operation (code
, mode
, GET_MODE (XEXP (x
, 0)),
6042 XEXP (x
, 0), XEXP (x
, 1),
6046 case RTX_COMM_COMPARE
:
6047 return simplify_relational_operation (code
, mode
,
6048 ((GET_MODE (XEXP (x
, 0))
6050 ? GET_MODE (XEXP (x
, 0))
6051 : GET_MODE (XEXP (x
, 1))),
6057 return simplify_subreg (mode
, SUBREG_REG (x
),
6058 GET_MODE (SUBREG_REG (x
)),
6065 /* Convert (lo_sum (high FOO) FOO) to FOO. */
6066 if (GET_CODE (XEXP (x
, 0)) == HIGH
6067 && rtx_equal_p (XEXP (XEXP (x
, 0), 0), XEXP (x
, 1)))