Handle const_int in expand_single_bit_test
[official-gcc.git] / gcc / simplify-rtx.cc
blob276be67aa67247dd46361ab9badc46ab089d6df0
1 /* RTL simplification functions for GNU compiler.
2 Copyright (C) 1987-2023 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
9 version.
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
14 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "predict.h"
29 #include "memmodel.h"
30 #include "optabs.h"
31 #include "emit-rtl.h"
32 #include "recog.h"
33 #include "diagnostic-core.h"
34 #include "varasm.h"
35 #include "flags.h"
36 #include "selftest.h"
37 #include "selftest-rtl.h"
38 #include "rtx-vector-builder.h"
39 #include "rtlanal.h"
41 /* Simplification and canonicalization of RTL. */
43 /* Much code operates on (low, high) pairs; the low value is an
44 unsigned wide int, the high value a signed wide int. We
45 occasionally need to sign extend from low to high as if low were a
46 signed wide int. */
47 #define HWI_SIGN_EXTEND(low) \
48 ((((HOST_WIDE_INT) low) < 0) ? HOST_WIDE_INT_M1 : HOST_WIDE_INT_0)
50 static bool plus_minus_operand_p (const_rtx);
52 /* Negate I, which satisfies poly_int_rtx_p. MODE is the mode of I. */
54 static rtx
55 neg_poly_int_rtx (machine_mode mode, const_rtx i)
57 return immed_wide_int_const (-wi::to_poly_wide (i, mode), mode);
60 /* Test whether expression, X, is an immediate constant that represents
61 the most significant bit of machine mode MODE. */
63 bool
64 mode_signbit_p (machine_mode mode, const_rtx x)
66 unsigned HOST_WIDE_INT val;
67 unsigned int width;
68 scalar_int_mode int_mode;
70 if (!is_int_mode (mode, &int_mode))
71 return false;
73 width = GET_MODE_PRECISION (int_mode);
74 if (width == 0)
75 return false;
77 if (width <= HOST_BITS_PER_WIDE_INT
78 && CONST_INT_P (x))
79 val = INTVAL (x);
80 #if TARGET_SUPPORTS_WIDE_INT
81 else if (CONST_WIDE_INT_P (x))
83 unsigned int i;
84 unsigned int elts = CONST_WIDE_INT_NUNITS (x);
85 if (elts != (width + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
86 return false;
87 for (i = 0; i < elts - 1; i++)
88 if (CONST_WIDE_INT_ELT (x, i) != 0)
89 return false;
90 val = CONST_WIDE_INT_ELT (x, elts - 1);
91 width %= HOST_BITS_PER_WIDE_INT;
92 if (width == 0)
93 width = HOST_BITS_PER_WIDE_INT;
95 #else
96 else if (width <= HOST_BITS_PER_DOUBLE_INT
97 && CONST_DOUBLE_AS_INT_P (x)
98 && CONST_DOUBLE_LOW (x) == 0)
100 val = CONST_DOUBLE_HIGH (x);
101 width -= HOST_BITS_PER_WIDE_INT;
103 #endif
104 else
105 /* X is not an integer constant. */
106 return false;
108 if (width < HOST_BITS_PER_WIDE_INT)
109 val &= (HOST_WIDE_INT_1U << width) - 1;
110 return val == (HOST_WIDE_INT_1U << (width - 1));
113 /* Test whether VAL is equal to the most significant bit of mode MODE
114 (after masking with the mode mask of MODE). Returns false if the
115 precision of MODE is too large to handle. */
117 bool
118 val_signbit_p (machine_mode mode, unsigned HOST_WIDE_INT val)
120 unsigned int width;
121 scalar_int_mode int_mode;
123 if (!is_int_mode (mode, &int_mode))
124 return false;
126 width = GET_MODE_PRECISION (int_mode);
127 if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
128 return false;
130 val &= GET_MODE_MASK (int_mode);
131 return val == (HOST_WIDE_INT_1U << (width - 1));
134 /* Test whether the most significant bit of mode MODE is set in VAL.
135 Returns false if the precision of MODE is too large to handle. */
136 bool
137 val_signbit_known_set_p (machine_mode mode, unsigned HOST_WIDE_INT val)
139 unsigned int width;
141 scalar_int_mode int_mode;
142 if (!is_int_mode (mode, &int_mode))
143 return false;
145 width = GET_MODE_PRECISION (int_mode);
146 if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
147 return false;
149 val &= HOST_WIDE_INT_1U << (width - 1);
150 return val != 0;
153 /* Test whether the most significant bit of mode MODE is clear in VAL.
154 Returns false if the precision of MODE is too large to handle. */
155 bool
156 val_signbit_known_clear_p (machine_mode mode, unsigned HOST_WIDE_INT val)
158 unsigned int width;
160 scalar_int_mode int_mode;
161 if (!is_int_mode (mode, &int_mode))
162 return false;
164 width = GET_MODE_PRECISION (int_mode);
165 if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
166 return false;
168 val &= HOST_WIDE_INT_1U << (width - 1);
169 return val == 0;
172 /* Make a binary operation by properly ordering the operands and
173 seeing if the expression folds. */
176 simplify_context::simplify_gen_binary (rtx_code code, machine_mode mode,
177 rtx op0, rtx op1)
179 rtx tem;
181 /* If this simplifies, do it. */
182 tem = simplify_binary_operation (code, mode, op0, op1);
183 if (tem)
184 return tem;
186 /* Put complex operands first and constants second if commutative. */
187 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
188 && swap_commutative_operands_p (op0, op1))
189 std::swap (op0, op1);
191 return gen_rtx_fmt_ee (code, mode, op0, op1);
194 /* If X is a MEM referencing the constant pool, return the real value.
195 Otherwise return X. */
197 avoid_constant_pool_reference (rtx x)
199 rtx c, tmp, addr;
200 machine_mode cmode;
201 poly_int64 offset = 0;
203 switch (GET_CODE (x))
205 case MEM:
206 break;
208 case FLOAT_EXTEND:
209 /* Handle float extensions of constant pool references. */
210 tmp = XEXP (x, 0);
211 c = avoid_constant_pool_reference (tmp);
212 if (c != tmp && CONST_DOUBLE_AS_FLOAT_P (c))
213 return const_double_from_real_value (*CONST_DOUBLE_REAL_VALUE (c),
214 GET_MODE (x));
215 return x;
217 default:
218 return x;
221 if (GET_MODE (x) == BLKmode)
222 return x;
224 addr = XEXP (x, 0);
226 /* Call target hook to avoid the effects of -fpic etc.... */
227 addr = targetm.delegitimize_address (addr);
229 /* Split the address into a base and integer offset. */
230 addr = strip_offset (addr, &offset);
232 if (GET_CODE (addr) == LO_SUM)
233 addr = XEXP (addr, 1);
235 /* If this is a constant pool reference, we can turn it into its
236 constant and hope that simplifications happen. */
237 if (GET_CODE (addr) == SYMBOL_REF
238 && CONSTANT_POOL_ADDRESS_P (addr))
240 c = get_pool_constant (addr);
241 cmode = get_pool_mode (addr);
243 /* If we're accessing the constant in a different mode than it was
244 originally stored, attempt to fix that up via subreg simplifications.
245 If that fails we have no choice but to return the original memory. */
246 if (known_eq (offset, 0) && cmode == GET_MODE (x))
247 return c;
248 else if (known_in_range_p (offset, 0, GET_MODE_SIZE (cmode)))
250 rtx tem = simplify_subreg (GET_MODE (x), c, cmode, offset);
251 if (tem && CONSTANT_P (tem))
252 return tem;
256 return x;
259 /* Simplify a MEM based on its attributes. This is the default
260 delegitimize_address target hook, and it's recommended that every
261 overrider call it. */
264 delegitimize_mem_from_attrs (rtx x)
266 /* MEMs without MEM_OFFSETs may have been offset, so we can't just
267 use their base addresses as equivalent. */
268 if (MEM_P (x)
269 && MEM_EXPR (x)
270 && MEM_OFFSET_KNOWN_P (x))
272 tree decl = MEM_EXPR (x);
273 machine_mode mode = GET_MODE (x);
274 poly_int64 offset = 0;
276 switch (TREE_CODE (decl))
278 default:
279 decl = NULL;
280 break;
282 case VAR_DECL:
283 break;
285 case ARRAY_REF:
286 case ARRAY_RANGE_REF:
287 case COMPONENT_REF:
288 case BIT_FIELD_REF:
289 case REALPART_EXPR:
290 case IMAGPART_EXPR:
291 case VIEW_CONVERT_EXPR:
293 poly_int64 bitsize, bitpos, bytepos, toffset_val = 0;
294 tree toffset;
295 int unsignedp, reversep, volatilep = 0;
297 decl
298 = get_inner_reference (decl, &bitsize, &bitpos, &toffset, &mode,
299 &unsignedp, &reversep, &volatilep);
300 if (maybe_ne (bitsize, GET_MODE_BITSIZE (mode))
301 || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
302 || (toffset && !poly_int_tree_p (toffset, &toffset_val)))
303 decl = NULL;
304 else
305 offset += bytepos + toffset_val;
306 break;
310 if (decl
311 && mode == GET_MODE (x)
312 && VAR_P (decl)
313 && (TREE_STATIC (decl)
314 || DECL_THREAD_LOCAL_P (decl))
315 && DECL_RTL_SET_P (decl)
316 && MEM_P (DECL_RTL (decl)))
318 rtx newx;
320 offset += MEM_OFFSET (x);
322 newx = DECL_RTL (decl);
324 if (MEM_P (newx))
326 rtx n = XEXP (newx, 0), o = XEXP (x, 0);
327 poly_int64 n_offset, o_offset;
329 /* Avoid creating a new MEM needlessly if we already had
330 the same address. We do if there's no OFFSET and the
331 old address X is identical to NEWX, or if X is of the
332 form (plus NEWX OFFSET), or the NEWX is of the form
333 (plus Y (const_int Z)) and X is that with the offset
334 added: (plus Y (const_int Z+OFFSET)). */
335 n = strip_offset (n, &n_offset);
336 o = strip_offset (o, &o_offset);
337 if (!(known_eq (o_offset, n_offset + offset)
338 && rtx_equal_p (o, n)))
339 x = adjust_address_nv (newx, mode, offset);
341 else if (GET_MODE (x) == GET_MODE (newx)
342 && known_eq (offset, 0))
343 x = newx;
347 return x;
350 /* Make a unary operation by first seeing if it folds and otherwise making
351 the specified operation. */
354 simplify_context::simplify_gen_unary (rtx_code code, machine_mode mode, rtx op,
355 machine_mode op_mode)
357 rtx tem;
359 /* If this simplifies, use it. */
360 if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
361 return tem;
363 return gen_rtx_fmt_e (code, mode, op);
366 /* Likewise for ternary operations. */
369 simplify_context::simplify_gen_ternary (rtx_code code, machine_mode mode,
370 machine_mode op0_mode,
371 rtx op0, rtx op1, rtx op2)
373 rtx tem;
375 /* If this simplifies, use it. */
376 if ((tem = simplify_ternary_operation (code, mode, op0_mode,
377 op0, op1, op2)) != 0)
378 return tem;
380 return gen_rtx_fmt_eee (code, mode, op0, op1, op2);
383 /* Likewise, for relational operations.
384 CMP_MODE specifies mode comparison is done in. */
387 simplify_context::simplify_gen_relational (rtx_code code, machine_mode mode,
388 machine_mode cmp_mode,
389 rtx op0, rtx op1)
391 rtx tem;
393 if ((tem = simplify_relational_operation (code, mode, cmp_mode,
394 op0, op1)) != 0)
395 return tem;
397 return gen_rtx_fmt_ee (code, mode, op0, op1);
400 /* If FN is NULL, replace all occurrences of OLD_RTX in X with copy_rtx (DATA)
401 and simplify the result. If FN is non-NULL, call this callback on each
402 X, if it returns non-NULL, replace X with its return value and simplify the
403 result. */
406 simplify_replace_fn_rtx (rtx x, const_rtx old_rtx,
407 rtx (*fn) (rtx, const_rtx, void *), void *data)
409 enum rtx_code code = GET_CODE (x);
410 machine_mode mode = GET_MODE (x);
411 machine_mode op_mode;
412 const char *fmt;
413 rtx op0, op1, op2, newx, op;
414 rtvec vec, newvec;
415 int i, j;
417 if (UNLIKELY (fn != NULL))
419 newx = fn (x, old_rtx, data);
420 if (newx)
421 return newx;
423 else if (rtx_equal_p (x, old_rtx))
424 return copy_rtx ((rtx) data);
426 switch (GET_RTX_CLASS (code))
428 case RTX_UNARY:
429 op0 = XEXP (x, 0);
430 op_mode = GET_MODE (op0);
431 op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
432 if (op0 == XEXP (x, 0))
433 return x;
434 return simplify_gen_unary (code, mode, op0, op_mode);
436 case RTX_BIN_ARITH:
437 case RTX_COMM_ARITH:
438 op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
439 op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
440 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
441 return x;
442 return simplify_gen_binary (code, mode, op0, op1);
444 case RTX_COMPARE:
445 case RTX_COMM_COMPARE:
446 op0 = XEXP (x, 0);
447 op1 = XEXP (x, 1);
448 op_mode = GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
449 op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
450 op1 = simplify_replace_fn_rtx (op1, old_rtx, fn, data);
451 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
452 return x;
453 return simplify_gen_relational (code, mode, op_mode, op0, op1);
455 case RTX_TERNARY:
456 case RTX_BITFIELD_OPS:
457 op0 = XEXP (x, 0);
458 op_mode = GET_MODE (op0);
459 op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
460 op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
461 op2 = simplify_replace_fn_rtx (XEXP (x, 2), old_rtx, fn, data);
462 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1) && op2 == XEXP (x, 2))
463 return x;
464 if (op_mode == VOIDmode)
465 op_mode = GET_MODE (op0);
466 return simplify_gen_ternary (code, mode, op_mode, op0, op1, op2);
468 case RTX_EXTRA:
469 if (code == SUBREG)
471 op0 = simplify_replace_fn_rtx (SUBREG_REG (x), old_rtx, fn, data);
472 if (op0 == SUBREG_REG (x))
473 return x;
474 op0 = simplify_gen_subreg (GET_MODE (x), op0,
475 GET_MODE (SUBREG_REG (x)),
476 SUBREG_BYTE (x));
477 return op0 ? op0 : x;
479 break;
481 case RTX_OBJ:
482 if (code == MEM)
484 op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
485 if (op0 == XEXP (x, 0))
486 return x;
487 return replace_equiv_address_nv (x, op0);
489 else if (code == LO_SUM)
491 op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
492 op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
494 /* (lo_sum (high x) y) -> y where x and y have the same base. */
495 if (GET_CODE (op0) == HIGH)
497 rtx base0, base1, offset0, offset1;
498 split_const (XEXP (op0, 0), &base0, &offset0);
499 split_const (op1, &base1, &offset1);
500 if (rtx_equal_p (base0, base1))
501 return op1;
504 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
505 return x;
506 return gen_rtx_LO_SUM (mode, op0, op1);
508 break;
510 default:
511 break;
514 newx = x;
515 fmt = GET_RTX_FORMAT (code);
516 for (i = 0; fmt[i]; i++)
517 switch (fmt[i])
519 case 'E':
520 vec = XVEC (x, i);
521 newvec = XVEC (newx, i);
522 for (j = 0; j < GET_NUM_ELEM (vec); j++)
524 op = simplify_replace_fn_rtx (RTVEC_ELT (vec, j),
525 old_rtx, fn, data);
526 if (op != RTVEC_ELT (vec, j))
528 if (newvec == vec)
530 newvec = shallow_copy_rtvec (vec);
531 if (x == newx)
532 newx = shallow_copy_rtx (x);
533 XVEC (newx, i) = newvec;
535 RTVEC_ELT (newvec, j) = op;
538 break;
540 case 'e':
541 if (XEXP (x, i))
543 op = simplify_replace_fn_rtx (XEXP (x, i), old_rtx, fn, data);
544 if (op != XEXP (x, i))
546 if (x == newx)
547 newx = shallow_copy_rtx (x);
548 XEXP (newx, i) = op;
551 break;
553 return newx;
556 /* Replace all occurrences of OLD_RTX in X with NEW_RTX and try to simplify the
557 resulting RTX. Return a new RTX which is as simplified as possible. */
560 simplify_replace_rtx (rtx x, const_rtx old_rtx, rtx new_rtx)
562 return simplify_replace_fn_rtx (x, old_rtx, 0, new_rtx);
565 /* Try to simplify a MODE truncation of OP, which has OP_MODE.
566 Only handle cases where the truncated value is inherently an rvalue.
568 RTL provides two ways of truncating a value:
570 1. a lowpart subreg. This form is only a truncation when both
571 the outer and inner modes (here MODE and OP_MODE respectively)
572 are scalar integers, and only then when the subreg is used as
573 an rvalue.
575 It is only valid to form such truncating subregs if the
576 truncation requires no action by the target. The onus for
577 proving this is on the creator of the subreg -- e.g. the
578 caller to simplify_subreg or simplify_gen_subreg -- and typically
579 involves either TRULY_NOOP_TRUNCATION_MODES_P or truncated_to_mode.
581 2. a TRUNCATE. This form handles both scalar and compound integers.
583 The first form is preferred where valid. However, the TRUNCATE
584 handling in simplify_unary_operation turns the second form into the
585 first form when TRULY_NOOP_TRUNCATION_MODES_P or truncated_to_mode allow,
586 so it is generally safe to form rvalue truncations using:
588 simplify_gen_unary (TRUNCATE, ...)
590 and leave simplify_unary_operation to work out which representation
591 should be used.
593 Because of the proof requirements on (1), simplify_truncation must
594 also use simplify_gen_unary (TRUNCATE, ...) to truncate parts of OP,
595 regardless of whether the outer truncation came from a SUBREG or a
596 TRUNCATE. For example, if the caller has proven that an SImode
597 truncation of:
599 (and:DI X Y)
601 is a no-op and can be represented as a subreg, it does not follow
602 that SImode truncations of X and Y are also no-ops. On a target
603 like 64-bit MIPS that requires SImode values to be stored in
604 sign-extended form, an SImode truncation of:
606 (and:DI (reg:DI X) (const_int 63))
608 is trivially a no-op because only the lower 6 bits can be set.
609 However, X is still an arbitrary 64-bit number and so we cannot
610 assume that truncating it too is a no-op. */
613 simplify_context::simplify_truncation (machine_mode mode, rtx op,
614 machine_mode op_mode)
616 unsigned int precision = GET_MODE_UNIT_PRECISION (mode);
617 unsigned int op_precision = GET_MODE_UNIT_PRECISION (op_mode);
618 scalar_int_mode int_mode, int_op_mode, subreg_mode;
620 gcc_assert (precision <= op_precision);
622 /* Optimize truncations of zero and sign extended values. */
623 if (GET_CODE (op) == ZERO_EXTEND
624 || GET_CODE (op) == SIGN_EXTEND)
626 /* There are three possibilities. If MODE is the same as the
627 origmode, we can omit both the extension and the subreg.
628 If MODE is not larger than the origmode, we can apply the
629 truncation without the extension. Finally, if the outermode
630 is larger than the origmode, we can just extend to the appropriate
631 mode. */
632 machine_mode origmode = GET_MODE (XEXP (op, 0));
633 if (mode == origmode)
634 return XEXP (op, 0);
635 else if (precision <= GET_MODE_UNIT_PRECISION (origmode))
636 return simplify_gen_unary (TRUNCATE, mode,
637 XEXP (op, 0), origmode);
638 else
639 return simplify_gen_unary (GET_CODE (op), mode,
640 XEXP (op, 0), origmode);
643 /* If the machine can perform operations in the truncated mode, distribute
644 the truncation, i.e. simplify (truncate:QI (op:SI (x:SI) (y:SI))) into
645 (op:QI (truncate:QI (x:SI)) (truncate:QI (y:SI))). */
646 if (1
647 && (!WORD_REGISTER_OPERATIONS || precision >= BITS_PER_WORD)
648 && (GET_CODE (op) == PLUS
649 || GET_CODE (op) == MINUS
650 || GET_CODE (op) == MULT))
652 rtx op0 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0), op_mode);
653 if (op0)
655 rtx op1 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 1), op_mode);
656 if (op1)
657 return simplify_gen_binary (GET_CODE (op), mode, op0, op1);
661 /* Simplify (truncate:QI (lshiftrt:SI (sign_extend:SI (x:QI)) C)) into
662 to (ashiftrt:QI (x:QI) C), where C is a suitable small constant and
663 the outer subreg is effectively a truncation to the original mode. */
664 if ((GET_CODE (op) == LSHIFTRT
665 || GET_CODE (op) == ASHIFTRT)
666 /* Ensure that OP_MODE is at least twice as wide as MODE
667 to avoid the possibility that an outer LSHIFTRT shifts by more
668 than the sign extension's sign_bit_copies and introduces zeros
669 into the high bits of the result. */
670 && 2 * precision <= op_precision
671 && CONST_INT_P (XEXP (op, 1))
672 && GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
673 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
674 && UINTVAL (XEXP (op, 1)) < precision)
675 return simplify_gen_binary (ASHIFTRT, mode,
676 XEXP (XEXP (op, 0), 0), XEXP (op, 1));
678 /* Likewise (truncate:QI (lshiftrt:SI (zero_extend:SI (x:QI)) C)) into
679 to (lshiftrt:QI (x:QI) C), where C is a suitable small constant and
680 the outer subreg is effectively a truncation to the original mode. */
681 if ((GET_CODE (op) == LSHIFTRT
682 || GET_CODE (op) == ASHIFTRT)
683 && CONST_INT_P (XEXP (op, 1))
684 && GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
685 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
686 && UINTVAL (XEXP (op, 1)) < precision)
687 return simplify_gen_binary (LSHIFTRT, mode,
688 XEXP (XEXP (op, 0), 0), XEXP (op, 1));
690 /* Likewise (truncate:QI (ashift:SI (zero_extend:SI (x:QI)) C)) into
691 to (ashift:QI (x:QI) C), where C is a suitable small constant and
692 the outer subreg is effectively a truncation to the original mode. */
693 if (GET_CODE (op) == ASHIFT
694 && CONST_INT_P (XEXP (op, 1))
695 && (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
696 || GET_CODE (XEXP (op, 0)) == SIGN_EXTEND)
697 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
698 && UINTVAL (XEXP (op, 1)) < precision)
699 return simplify_gen_binary (ASHIFT, mode,
700 XEXP (XEXP (op, 0), 0), XEXP (op, 1));
702 /* Likewise (truncate:QI (and:SI (lshiftrt:SI (x:SI) C) C2)) into
703 (and:QI (lshiftrt:QI (truncate:QI (x:SI)) C) C2) for suitable C
704 and C2. */
705 if (GET_CODE (op) == AND
706 && (GET_CODE (XEXP (op, 0)) == LSHIFTRT
707 || GET_CODE (XEXP (op, 0)) == ASHIFTRT)
708 && CONST_INT_P (XEXP (XEXP (op, 0), 1))
709 && CONST_INT_P (XEXP (op, 1)))
711 rtx op0 = (XEXP (XEXP (op, 0), 0));
712 rtx shift_op = XEXP (XEXP (op, 0), 1);
713 rtx mask_op = XEXP (op, 1);
714 unsigned HOST_WIDE_INT shift = UINTVAL (shift_op);
715 unsigned HOST_WIDE_INT mask = UINTVAL (mask_op);
717 if (shift < precision
718 /* If doing this transform works for an X with all bits set,
719 it works for any X. */
720 && ((GET_MODE_MASK (mode) >> shift) & mask)
721 == ((GET_MODE_MASK (op_mode) >> shift) & mask)
722 && (op0 = simplify_gen_unary (TRUNCATE, mode, op0, op_mode))
723 && (op0 = simplify_gen_binary (LSHIFTRT, mode, op0, shift_op)))
725 mask_op = GEN_INT (trunc_int_for_mode (mask, mode));
726 return simplify_gen_binary (AND, mode, op0, mask_op);
730 /* Turn (truncate:M1 (*_extract:M2 (reg:M2) (len) (pos))) into
731 (*_extract:M1 (truncate:M1 (reg:M2)) (len) (pos')) if possible without
732 changing len. */
733 if ((GET_CODE (op) == ZERO_EXTRACT || GET_CODE (op) == SIGN_EXTRACT)
734 && REG_P (XEXP (op, 0))
735 && GET_MODE (XEXP (op, 0)) == GET_MODE (op)
736 && CONST_INT_P (XEXP (op, 1))
737 && CONST_INT_P (XEXP (op, 2)))
739 rtx op0 = XEXP (op, 0);
740 unsigned HOST_WIDE_INT len = UINTVAL (XEXP (op, 1));
741 unsigned HOST_WIDE_INT pos = UINTVAL (XEXP (op, 2));
742 if (BITS_BIG_ENDIAN && pos >= op_precision - precision)
744 op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
745 if (op0)
747 pos -= op_precision - precision;
748 return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
749 XEXP (op, 1), GEN_INT (pos));
752 else if (!BITS_BIG_ENDIAN && precision >= len + pos)
754 op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
755 if (op0)
756 return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
757 XEXP (op, 1), XEXP (op, 2));
761 /* Recognize a word extraction from a multi-word subreg. */
762 if ((GET_CODE (op) == LSHIFTRT
763 || GET_CODE (op) == ASHIFTRT)
764 && SCALAR_INT_MODE_P (mode)
765 && SCALAR_INT_MODE_P (op_mode)
766 && precision >= BITS_PER_WORD
767 && 2 * precision <= op_precision
768 && CONST_INT_P (XEXP (op, 1))
769 && (INTVAL (XEXP (op, 1)) & (precision - 1)) == 0
770 && UINTVAL (XEXP (op, 1)) < op_precision)
772 poly_int64 byte = subreg_lowpart_offset (mode, op_mode);
773 int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
774 return simplify_gen_subreg (mode, XEXP (op, 0), op_mode,
775 (WORDS_BIG_ENDIAN
776 ? byte - shifted_bytes
777 : byte + shifted_bytes));
780 /* If we have a TRUNCATE of a right shift of MEM, make a new MEM
781 and try replacing the TRUNCATE and shift with it. Don't do this
782 if the MEM has a mode-dependent address. */
783 if ((GET_CODE (op) == LSHIFTRT
784 || GET_CODE (op) == ASHIFTRT)
785 && is_a <scalar_int_mode> (mode, &int_mode)
786 && is_a <scalar_int_mode> (op_mode, &int_op_mode)
787 && MEM_P (XEXP (op, 0))
788 && CONST_INT_P (XEXP (op, 1))
789 && INTVAL (XEXP (op, 1)) % GET_MODE_BITSIZE (int_mode) == 0
790 && INTVAL (XEXP (op, 1)) > 0
791 && INTVAL (XEXP (op, 1)) < GET_MODE_BITSIZE (int_op_mode)
792 && ! mode_dependent_address_p (XEXP (XEXP (op, 0), 0),
793 MEM_ADDR_SPACE (XEXP (op, 0)))
794 && ! MEM_VOLATILE_P (XEXP (op, 0))
795 && (GET_MODE_SIZE (int_mode) >= UNITS_PER_WORD
796 || WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN))
798 poly_int64 byte = subreg_lowpart_offset (int_mode, int_op_mode);
799 int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
800 return adjust_address_nv (XEXP (op, 0), int_mode,
801 (WORDS_BIG_ENDIAN
802 ? byte - shifted_bytes
803 : byte + shifted_bytes));
806 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
807 (OP:SI foo:SI) if OP is NEG or ABS. */
808 if ((GET_CODE (op) == ABS
809 || GET_CODE (op) == NEG)
810 && (GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
811 || GET_CODE (XEXP (op, 0)) == ZERO_EXTEND)
812 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
813 return simplify_gen_unary (GET_CODE (op), mode,
814 XEXP (XEXP (op, 0), 0), mode);
816 /* Simplifications of (truncate:A (subreg:B X 0)). */
817 if (GET_CODE (op) == SUBREG
818 && is_a <scalar_int_mode> (mode, &int_mode)
819 && SCALAR_INT_MODE_P (op_mode)
820 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &subreg_mode)
821 && subreg_lowpart_p (op))
823 /* (truncate:A (subreg:B (truncate:C X) 0)) is (truncate:A X). */
824 if (GET_CODE (SUBREG_REG (op)) == TRUNCATE)
826 rtx inner = XEXP (SUBREG_REG (op), 0);
827 if (GET_MODE_PRECISION (int_mode)
828 <= GET_MODE_PRECISION (subreg_mode))
829 return simplify_gen_unary (TRUNCATE, int_mode, inner,
830 GET_MODE (inner));
831 else
832 /* If subreg above is paradoxical and C is narrower
833 than A, return (subreg:A (truncate:C X) 0). */
834 return simplify_gen_subreg (int_mode, SUBREG_REG (op),
835 subreg_mode, 0);
838 /* Simplifications of (truncate:A (subreg:B X:C 0)) with
839 paradoxical subregs (B is wider than C). */
840 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
842 unsigned int int_op_prec = GET_MODE_PRECISION (int_op_mode);
843 unsigned int subreg_prec = GET_MODE_PRECISION (subreg_mode);
844 if (int_op_prec > subreg_prec)
846 if (int_mode == subreg_mode)
847 return SUBREG_REG (op);
848 if (GET_MODE_PRECISION (int_mode) < subreg_prec)
849 return simplify_gen_unary (TRUNCATE, int_mode,
850 SUBREG_REG (op), subreg_mode);
852 /* Simplification of (truncate:A (subreg:B X:C 0)) where
853 A is narrower than B and B is narrower than C. */
854 else if (int_op_prec < subreg_prec
855 && GET_MODE_PRECISION (int_mode) < int_op_prec)
856 return simplify_gen_unary (TRUNCATE, int_mode,
857 SUBREG_REG (op), subreg_mode);
861 /* (truncate:A (truncate:B X)) is (truncate:A X). */
862 if (GET_CODE (op) == TRUNCATE)
863 return simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0),
864 GET_MODE (XEXP (op, 0)));
866 /* (truncate:A (ior X C)) is (const_int -1) if C is equal to that already,
867 in mode A. */
868 if (GET_CODE (op) == IOR
869 && SCALAR_INT_MODE_P (mode)
870 && SCALAR_INT_MODE_P (op_mode)
871 && CONST_INT_P (XEXP (op, 1))
872 && trunc_int_for_mode (INTVAL (XEXP (op, 1)), mode) == -1)
873 return constm1_rtx;
875 return NULL_RTX;
878 /* Try to simplify a unary operation CODE whose output mode is to be
879 MODE with input operand OP whose mode was originally OP_MODE.
880 Return zero if no simplification can be made. */
882 simplify_context::simplify_unary_operation (rtx_code code, machine_mode mode,
883 rtx op, machine_mode op_mode)
885 rtx trueop, tem;
887 trueop = avoid_constant_pool_reference (op);
889 tem = simplify_const_unary_operation (code, mode, trueop, op_mode);
890 if (tem)
891 return tem;
893 return simplify_unary_operation_1 (code, mode, op);
896 /* Return true if FLOAT or UNSIGNED_FLOAT operation OP is known
897 to be exact. */
899 static bool
900 exact_int_to_float_conversion_p (const_rtx op)
902 machine_mode op0_mode = GET_MODE (XEXP (op, 0));
903 /* Constants can reach here with -frounding-math, if they do then
904 the conversion isn't exact. */
905 if (op0_mode == VOIDmode)
906 return false;
907 int out_bits = significand_size (GET_MODE_INNER (GET_MODE (op)));
908 int in_prec = GET_MODE_UNIT_PRECISION (op0_mode);
909 int in_bits = in_prec;
910 if (HWI_COMPUTABLE_MODE_P (op0_mode))
912 unsigned HOST_WIDE_INT nonzero = nonzero_bits (XEXP (op, 0), op0_mode);
913 if (GET_CODE (op) == FLOAT)
914 in_bits -= num_sign_bit_copies (XEXP (op, 0), op0_mode);
915 else if (GET_CODE (op) == UNSIGNED_FLOAT)
916 in_bits = wi::min_precision (wi::uhwi (nonzero, in_prec), UNSIGNED);
917 else
918 gcc_unreachable ();
919 in_bits -= wi::ctz (wi::uhwi (nonzero, in_prec));
921 return in_bits <= out_bits;
924 /* Perform some simplifications we can do even if the operands
925 aren't constant. */
927 simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode,
928 rtx op)
930 enum rtx_code reversed;
931 rtx temp, elt, base, step;
932 scalar_int_mode inner, int_mode, op_mode, op0_mode;
934 switch (code)
936 case NOT:
937 /* (not (not X)) == X. */
938 if (GET_CODE (op) == NOT)
939 return XEXP (op, 0);
941 /* (not (eq X Y)) == (ne X Y), etc. if BImode or the result of the
942 comparison is all ones. */
943 if (COMPARISON_P (op)
944 && (mode == BImode || STORE_FLAG_VALUE == -1)
945 && ((reversed = reversed_comparison_code (op, NULL)) != UNKNOWN))
946 return simplify_gen_relational (reversed, mode, VOIDmode,
947 XEXP (op, 0), XEXP (op, 1));
949 /* (not (plus X -1)) can become (neg X). */
950 if (GET_CODE (op) == PLUS
951 && XEXP (op, 1) == constm1_rtx)
952 return simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
954 /* Similarly, (not (neg X)) is (plus X -1). Only do this for
955 modes that have CONSTM1_RTX, i.e. MODE_INT, MODE_PARTIAL_INT
956 and MODE_VECTOR_INT. */
957 if (GET_CODE (op) == NEG && CONSTM1_RTX (mode))
958 return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
959 CONSTM1_RTX (mode));
961 /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
962 if (GET_CODE (op) == XOR
963 && CONST_INT_P (XEXP (op, 1))
964 && (temp = simplify_unary_operation (NOT, mode,
965 XEXP (op, 1), mode)) != 0)
966 return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
968 /* (not (plus X C)) for signbit C is (xor X D) with D = ~C. */
969 if (GET_CODE (op) == PLUS
970 && CONST_INT_P (XEXP (op, 1))
971 && mode_signbit_p (mode, XEXP (op, 1))
972 && (temp = simplify_unary_operation (NOT, mode,
973 XEXP (op, 1), mode)) != 0)
974 return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
977 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for
978 operands other than 1, but that is not valid. We could do a
979 similar simplification for (not (lshiftrt C X)) where C is
980 just the sign bit, but this doesn't seem common enough to
981 bother with. */
982 if (GET_CODE (op) == ASHIFT
983 && XEXP (op, 0) == const1_rtx)
985 temp = simplify_gen_unary (NOT, mode, const1_rtx, mode);
986 return simplify_gen_binary (ROTATE, mode, temp, XEXP (op, 1));
989 /* (not (ashiftrt foo C)) where C is the number of bits in FOO
990 minus 1 is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1,
991 so we can perform the above simplification. */
992 if (STORE_FLAG_VALUE == -1
993 && is_a <scalar_int_mode> (mode, &int_mode)
994 && GET_CODE (op) == ASHIFTRT
995 && CONST_INT_P (XEXP (op, 1))
996 && INTVAL (XEXP (op, 1)) == GET_MODE_PRECISION (int_mode) - 1)
997 return simplify_gen_relational (GE, int_mode, VOIDmode,
998 XEXP (op, 0), const0_rtx);
1001 if (partial_subreg_p (op)
1002 && subreg_lowpart_p (op)
1003 && GET_CODE (SUBREG_REG (op)) == ASHIFT
1004 && XEXP (SUBREG_REG (op), 0) == const1_rtx)
1006 machine_mode inner_mode = GET_MODE (SUBREG_REG (op));
1007 rtx x;
1009 x = gen_rtx_ROTATE (inner_mode,
1010 simplify_gen_unary (NOT, inner_mode, const1_rtx,
1011 inner_mode),
1012 XEXP (SUBREG_REG (op), 1));
1013 temp = rtl_hooks.gen_lowpart_no_emit (mode, x);
1014 if (temp)
1015 return temp;
1018 /* Apply De Morgan's laws to reduce number of patterns for machines
1019 with negating logical insns (and-not, nand, etc.). If result has
1020 only one NOT, put it first, since that is how the patterns are
1021 coded. */
1022 if (GET_CODE (op) == IOR || GET_CODE (op) == AND)
1024 rtx in1 = XEXP (op, 0), in2 = XEXP (op, 1);
1025 machine_mode op_mode;
1027 op_mode = GET_MODE (in1);
1028 in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
1030 op_mode = GET_MODE (in2);
1031 if (op_mode == VOIDmode)
1032 op_mode = mode;
1033 in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
1035 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
1036 std::swap (in1, in2);
1038 return gen_rtx_fmt_ee (GET_CODE (op) == IOR ? AND : IOR,
1039 mode, in1, in2);
1042 /* (not (bswap x)) -> (bswap (not x)). */
1043 if (GET_CODE (op) == BSWAP || GET_CODE (op) == BITREVERSE)
1045 rtx x = simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1046 return simplify_gen_unary (GET_CODE (op), mode, x, mode);
1048 break;
1050 case NEG:
1051 /* (neg (neg X)) == X. */
1052 if (GET_CODE (op) == NEG)
1053 return XEXP (op, 0);
1055 /* (neg (x ? (neg y) : y)) == !x ? (neg y) : y.
1056 If comparison is not reversible use
1057 x ? y : (neg y). */
1058 if (GET_CODE (op) == IF_THEN_ELSE)
1060 rtx cond = XEXP (op, 0);
1061 rtx true_rtx = XEXP (op, 1);
1062 rtx false_rtx = XEXP (op, 2);
1064 if ((GET_CODE (true_rtx) == NEG
1065 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
1066 || (GET_CODE (false_rtx) == NEG
1067 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx)))
1069 if (reversed_comparison_code (cond, NULL) != UNKNOWN)
1070 temp = reversed_comparison (cond, mode);
1071 else
1073 temp = cond;
1074 std::swap (true_rtx, false_rtx);
1076 return simplify_gen_ternary (IF_THEN_ELSE, mode,
1077 mode, temp, true_rtx, false_rtx);
1081 /* (neg (plus X 1)) can become (not X). */
1082 if (GET_CODE (op) == PLUS
1083 && XEXP (op, 1) == const1_rtx)
1084 return simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1086 /* Similarly, (neg (not X)) is (plus X 1). */
1087 if (GET_CODE (op) == NOT)
1088 return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
1089 CONST1_RTX (mode));
1091 /* (neg (minus X Y)) can become (minus Y X). This transformation
1092 isn't safe for modes with signed zeros, since if X and Y are
1093 both +0, (minus Y X) is the same as (minus X Y). If the
1094 rounding mode is towards +infinity (or -infinity) then the two
1095 expressions will be rounded differently. */
1096 if (GET_CODE (op) == MINUS
1097 && !HONOR_SIGNED_ZEROS (mode)
1098 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1099 return simplify_gen_binary (MINUS, mode, XEXP (op, 1), XEXP (op, 0));
1101 if (GET_CODE (op) == PLUS
1102 && !HONOR_SIGNED_ZEROS (mode)
1103 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1105 /* (neg (plus A C)) is simplified to (minus -C A). */
1106 if (CONST_SCALAR_INT_P (XEXP (op, 1))
1107 || CONST_DOUBLE_AS_FLOAT_P (XEXP (op, 1)))
1109 temp = simplify_unary_operation (NEG, mode, XEXP (op, 1), mode);
1110 if (temp)
1111 return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 0));
1114 /* (neg (plus A B)) is canonicalized to (minus (neg A) B). */
1115 temp = simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
1116 return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 1));
1119 /* (neg (mult A B)) becomes (mult A (neg B)).
1120 This works even for floating-point values. */
1121 if (GET_CODE (op) == MULT
1122 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1124 temp = simplify_gen_unary (NEG, mode, XEXP (op, 1), mode);
1125 return simplify_gen_binary (MULT, mode, XEXP (op, 0), temp);
1128 /* NEG commutes with ASHIFT since it is multiplication. Only do
1129 this if we can then eliminate the NEG (e.g., if the operand
1130 is a constant). */
1131 if (GET_CODE (op) == ASHIFT)
1133 temp = simplify_unary_operation (NEG, mode, XEXP (op, 0), mode);
1134 if (temp)
1135 return simplify_gen_binary (ASHIFT, mode, temp, XEXP (op, 1));
1138 /* (neg (ashiftrt X C)) can be replaced by (lshiftrt X C) when
1139 C is equal to the width of MODE minus 1. */
1140 if (GET_CODE (op) == ASHIFTRT
1141 && CONST_INT_P (XEXP (op, 1))
1142 && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1143 return simplify_gen_binary (LSHIFTRT, mode,
1144 XEXP (op, 0), XEXP (op, 1));
1146 /* (neg (lshiftrt X C)) can be replaced by (ashiftrt X C) when
1147 C is equal to the width of MODE minus 1. */
1148 if (GET_CODE (op) == LSHIFTRT
1149 && CONST_INT_P (XEXP (op, 1))
1150 && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1151 return simplify_gen_binary (ASHIFTRT, mode,
1152 XEXP (op, 0), XEXP (op, 1));
1154 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
1155 if (GET_CODE (op) == XOR
1156 && XEXP (op, 1) == const1_rtx
1157 && nonzero_bits (XEXP (op, 0), mode) == 1)
1158 return plus_constant (mode, XEXP (op, 0), -1);
1160 /* (neg (lt x 0)) is (ashiftrt X C) if STORE_FLAG_VALUE is 1. */
1161 /* (neg (lt x 0)) is (lshiftrt X C) if STORE_FLAG_VALUE is -1. */
1162 if (GET_CODE (op) == LT
1163 && XEXP (op, 1) == const0_rtx
1164 && is_a <scalar_int_mode> (GET_MODE (XEXP (op, 0)), &inner))
1166 int_mode = as_a <scalar_int_mode> (mode);
1167 int isize = GET_MODE_PRECISION (inner);
1168 if (STORE_FLAG_VALUE == 1)
1170 temp = simplify_gen_binary (ASHIFTRT, inner, XEXP (op, 0),
1171 gen_int_shift_amount (inner,
1172 isize - 1));
1173 if (int_mode == inner)
1174 return temp;
1175 if (GET_MODE_PRECISION (int_mode) > isize)
1176 return simplify_gen_unary (SIGN_EXTEND, int_mode, temp, inner);
1177 return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1179 else if (STORE_FLAG_VALUE == -1)
1181 temp = simplify_gen_binary (LSHIFTRT, inner, XEXP (op, 0),
1182 gen_int_shift_amount (inner,
1183 isize - 1));
1184 if (int_mode == inner)
1185 return temp;
1186 if (GET_MODE_PRECISION (int_mode) > isize)
1187 return simplify_gen_unary (ZERO_EXTEND, int_mode, temp, inner);
1188 return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1192 if (vec_series_p (op, &base, &step))
1194 /* Only create a new series if we can simplify both parts. In other
1195 cases this isn't really a simplification, and it's not necessarily
1196 a win to replace a vector operation with a scalar operation. */
1197 scalar_mode inner_mode = GET_MODE_INNER (mode);
1198 base = simplify_unary_operation (NEG, inner_mode, base, inner_mode);
1199 if (base)
1201 step = simplify_unary_operation (NEG, inner_mode,
1202 step, inner_mode);
1203 if (step)
1204 return gen_vec_series (mode, base, step);
1207 break;
1209 case TRUNCATE:
1210 /* Don't optimize (lshiftrt (mult ...)) as it would interfere
1211 with the umulXi3_highpart patterns. */
1212 if (GET_CODE (op) == LSHIFTRT
1213 && GET_CODE (XEXP (op, 0)) == MULT)
1214 break;
1216 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
1218 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op)))
1220 temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1221 if (temp)
1222 return temp;
1224 /* We can't handle truncation to a partial integer mode here
1225 because we don't know the real bitsize of the partial
1226 integer mode. */
1227 break;
1230 if (GET_MODE (op) != VOIDmode)
1232 temp = simplify_truncation (mode, op, GET_MODE (op));
1233 if (temp)
1234 return temp;
1237 /* If we know that the value is already truncated, we can
1238 replace the TRUNCATE with a SUBREG. */
1239 if (known_eq (GET_MODE_NUNITS (mode), 1)
1240 && (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op))
1241 || truncated_to_mode (mode, op)))
1243 temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1244 if (temp)
1245 return temp;
1248 /* A truncate of a comparison can be replaced with a subreg if
1249 STORE_FLAG_VALUE permits. This is like the previous test,
1250 but it works even if the comparison is done in a mode larger
1251 than HOST_BITS_PER_WIDE_INT. */
1252 if (HWI_COMPUTABLE_MODE_P (mode)
1253 && COMPARISON_P (op)
1254 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
1255 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op)))
1257 temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1258 if (temp)
1259 return temp;
1262 /* A truncate of a memory is just loading the low part of the memory
1263 if we are not changing the meaning of the address. */
1264 if (GET_CODE (op) == MEM
1265 && !VECTOR_MODE_P (mode)
1266 && !MEM_VOLATILE_P (op)
1267 && !mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op)))
1269 temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1270 if (temp)
1271 return temp;
1274 /* Check for useless truncation. */
1275 if (GET_MODE (op) == mode)
1276 return op;
1277 break;
1279 case FLOAT_TRUNCATE:
1280 /* Check for useless truncation. */
1281 if (GET_MODE (op) == mode)
1282 return op;
1284 if (DECIMAL_FLOAT_MODE_P (mode))
1285 break;
1287 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
1288 if (GET_CODE (op) == FLOAT_EXTEND
1289 && GET_MODE (XEXP (op, 0)) == mode)
1290 return XEXP (op, 0);
1292 /* (float_truncate:SF (float_truncate:DF foo:XF))
1293 = (float_truncate:SF foo:XF).
1294 This may eliminate double rounding, so it is unsafe.
1296 (float_truncate:SF (float_extend:XF foo:DF))
1297 = (float_truncate:SF foo:DF).
1299 (float_truncate:DF (float_extend:XF foo:SF))
1300 = (float_extend:DF foo:SF). */
1301 if ((GET_CODE (op) == FLOAT_TRUNCATE
1302 && flag_unsafe_math_optimizations)
1303 || GET_CODE (op) == FLOAT_EXTEND)
1304 return simplify_gen_unary (GET_MODE_UNIT_SIZE (GET_MODE (XEXP (op, 0)))
1305 > GET_MODE_UNIT_SIZE (mode)
1306 ? FLOAT_TRUNCATE : FLOAT_EXTEND,
1307 mode,
1308 XEXP (op, 0), mode);
1310 /* (float_truncate (float x)) is (float x) */
1311 if ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1312 && (flag_unsafe_math_optimizations
1313 || exact_int_to_float_conversion_p (op)))
1314 return simplify_gen_unary (GET_CODE (op), mode,
1315 XEXP (op, 0),
1316 GET_MODE (XEXP (op, 0)));
1318 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
1319 (OP:SF foo:SF) if OP is NEG or ABS. */
1320 if ((GET_CODE (op) == ABS
1321 || GET_CODE (op) == NEG)
1322 && GET_CODE (XEXP (op, 0)) == FLOAT_EXTEND
1323 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
1324 return simplify_gen_unary (GET_CODE (op), mode,
1325 XEXP (XEXP (op, 0), 0), mode);
1327 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
1328 is (float_truncate:SF x). */
1329 if (GET_CODE (op) == SUBREG
1330 && subreg_lowpart_p (op)
1331 && GET_CODE (SUBREG_REG (op)) == FLOAT_TRUNCATE)
1332 return SUBREG_REG (op);
1333 break;
1335 case FLOAT_EXTEND:
1336 /* Check for useless extension. */
1337 if (GET_MODE (op) == mode)
1338 return op;
1340 if (DECIMAL_FLOAT_MODE_P (mode))
1341 break;
1343 /* (float_extend (float_extend x)) is (float_extend x)
1345 (float_extend (float x)) is (float x) assuming that double
1346 rounding can't happen.
1348 if (GET_CODE (op) == FLOAT_EXTEND
1349 || ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1350 && exact_int_to_float_conversion_p (op)))
1351 return simplify_gen_unary (GET_CODE (op), mode,
1352 XEXP (op, 0),
1353 GET_MODE (XEXP (op, 0)));
1355 break;
1357 case ABS:
1358 /* (abs (neg <foo>)) -> (abs <foo>) */
1359 if (GET_CODE (op) == NEG)
1360 return simplify_gen_unary (ABS, mode, XEXP (op, 0),
1361 GET_MODE (XEXP (op, 0)));
1363 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
1364 do nothing. */
1365 if (GET_MODE (op) == VOIDmode)
1366 break;
1368 /* If operand is something known to be positive, ignore the ABS. */
1369 if (val_signbit_known_clear_p (GET_MODE (op),
1370 nonzero_bits (op, GET_MODE (op))))
1371 return op;
1373 /* Using nonzero_bits doesn't (currently) work for modes wider than
1374 HOST_WIDE_INT, so the following transformations help simplify
1375 ABS for TImode and wider. */
1376 switch (GET_CODE (op))
1378 case ABS:
1379 case CLRSB:
1380 case FFS:
1381 case PARITY:
1382 case POPCOUNT:
1383 case SS_ABS:
1384 return op;
1386 case LSHIFTRT:
1387 if (CONST_INT_P (XEXP (op, 1))
1388 && INTVAL (XEXP (op, 1)) > 0
1389 && is_a <scalar_int_mode> (mode, &int_mode)
1390 && INTVAL (XEXP (op, 1)) < GET_MODE_PRECISION (int_mode))
1391 return op;
1392 break;
1394 default:
1395 break;
1398 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
1399 if (is_a <scalar_int_mode> (mode, &int_mode)
1400 && (num_sign_bit_copies (op, int_mode)
1401 == GET_MODE_PRECISION (int_mode)))
1402 return gen_rtx_NEG (int_mode, op);
1404 break;
1406 case FFS:
1407 /* (ffs (*_extend <X>)) = (*_extend (ffs <X>)). */
1408 if (GET_CODE (op) == SIGN_EXTEND
1409 || GET_CODE (op) == ZERO_EXTEND)
1411 temp = simplify_gen_unary (FFS, GET_MODE (XEXP (op, 0)),
1412 XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1413 return simplify_gen_unary (GET_CODE (op), mode, temp,
1414 GET_MODE (temp));
1416 break;
1418 case POPCOUNT:
1419 switch (GET_CODE (op))
1421 case BSWAP:
1422 case BITREVERSE:
1423 /* (popcount (bswap <X>)) = (popcount <X>). */
1424 return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1425 GET_MODE (XEXP (op, 0)));
1427 case ZERO_EXTEND:
1428 /* (popcount (zero_extend <X>)) = (zero_extend (popcount <X>)). */
1429 temp = simplify_gen_unary (POPCOUNT, GET_MODE (XEXP (op, 0)),
1430 XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1431 return simplify_gen_unary (ZERO_EXTEND, mode, temp,
1432 GET_MODE (temp));
1434 case ROTATE:
1435 case ROTATERT:
1436 /* Rotations don't affect popcount. */
1437 if (!side_effects_p (XEXP (op, 1)))
1438 return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1439 GET_MODE (XEXP (op, 0)));
1440 break;
1442 default:
1443 break;
1445 break;
1447 case PARITY:
1448 switch (GET_CODE (op))
1450 case NOT:
1451 case BSWAP:
1452 case BITREVERSE:
1453 return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1454 GET_MODE (XEXP (op, 0)));
1456 case ZERO_EXTEND:
1457 case SIGN_EXTEND:
1458 temp = simplify_gen_unary (PARITY, GET_MODE (XEXP (op, 0)),
1459 XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1460 return simplify_gen_unary (GET_CODE (op), mode, temp,
1461 GET_MODE (temp));
1463 case ROTATE:
1464 case ROTATERT:
1465 /* Rotations don't affect parity. */
1466 if (!side_effects_p (XEXP (op, 1)))
1467 return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1468 GET_MODE (XEXP (op, 0)));
1469 break;
1471 case PARITY:
1472 /* (parity (parity x)) -> parity (x). */
1473 return op;
1475 default:
1476 break;
1478 break;
1480 case BSWAP:
1481 /* (bswap (bswap x)) -> x. */
1482 if (GET_CODE (op) == BSWAP)
1483 return XEXP (op, 0);
1484 break;
1486 case BITREVERSE:
1487 /* (bitreverse (bitreverse x)) -> x. */
1488 if (GET_CODE (op) == BITREVERSE)
1489 return XEXP (op, 0);
1490 break;
1492 case FLOAT:
1493 /* (float (sign_extend <X>)) = (float <X>). */
1494 if (GET_CODE (op) == SIGN_EXTEND)
1495 return simplify_gen_unary (FLOAT, mode, XEXP (op, 0),
1496 GET_MODE (XEXP (op, 0)));
1497 break;
1499 case SIGN_EXTEND:
1500 /* Check for useless extension. */
1501 if (GET_MODE (op) == mode)
1502 return op;
1504 /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
1505 becomes just the MINUS if its mode is MODE. This allows
1506 folding switch statements on machines using casesi (such as
1507 the VAX). */
1508 if (GET_CODE (op) == TRUNCATE
1509 && GET_MODE (XEXP (op, 0)) == mode
1510 && GET_CODE (XEXP (op, 0)) == MINUS
1511 && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
1512 && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
1513 return XEXP (op, 0);
1515 /* Extending a widening multiplication should be canonicalized to
1516 a wider widening multiplication. */
1517 if (GET_CODE (op) == MULT)
1519 rtx lhs = XEXP (op, 0);
1520 rtx rhs = XEXP (op, 1);
1521 enum rtx_code lcode = GET_CODE (lhs);
1522 enum rtx_code rcode = GET_CODE (rhs);
1524 /* Widening multiplies usually extend both operands, but sometimes
1525 they use a shift to extract a portion of a register. */
1526 if ((lcode == SIGN_EXTEND
1527 || (lcode == ASHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1528 && (rcode == SIGN_EXTEND
1529 || (rcode == ASHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1531 machine_mode lmode = GET_MODE (lhs);
1532 machine_mode rmode = GET_MODE (rhs);
1533 int bits;
1535 if (lcode == ASHIFTRT)
1536 /* Number of bits not shifted off the end. */
1537 bits = (GET_MODE_UNIT_PRECISION (lmode)
1538 - INTVAL (XEXP (lhs, 1)));
1539 else /* lcode == SIGN_EXTEND */
1540 /* Size of inner mode. */
1541 bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1543 if (rcode == ASHIFTRT)
1544 bits += (GET_MODE_UNIT_PRECISION (rmode)
1545 - INTVAL (XEXP (rhs, 1)));
1546 else /* rcode == SIGN_EXTEND */
1547 bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1549 /* We can only widen multiplies if the result is mathematiclly
1550 equivalent. I.e. if overflow was impossible. */
1551 if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1552 return simplify_gen_binary
1553 (MULT, mode,
1554 simplify_gen_unary (SIGN_EXTEND, mode, lhs, lmode),
1555 simplify_gen_unary (SIGN_EXTEND, mode, rhs, rmode));
1559 /* Check for a sign extension of a subreg of a promoted
1560 variable, where the promotion is sign-extended, and the
1561 target mode is the same as the variable's promotion. */
1562 if (GET_CODE (op) == SUBREG
1563 && SUBREG_PROMOTED_VAR_P (op)
1564 && SUBREG_PROMOTED_SIGNED_P (op))
1566 rtx subreg = SUBREG_REG (op);
1567 machine_mode subreg_mode = GET_MODE (subreg);
1568 if (!paradoxical_subreg_p (mode, subreg_mode))
1570 temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1571 if (temp)
1573 /* Preserve SUBREG_PROMOTED_VAR_P. */
1574 if (partial_subreg_p (temp))
1576 SUBREG_PROMOTED_VAR_P (temp) = 1;
1577 SUBREG_PROMOTED_SET (temp, SRP_SIGNED);
1579 return temp;
1582 else
1583 /* Sign-extending a sign-extended subreg. */
1584 return simplify_gen_unary (SIGN_EXTEND, mode,
1585 subreg, subreg_mode);
1588 /* (sign_extend:M (sign_extend:N <X>)) is (sign_extend:M <X>).
1589 (sign_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1590 if (GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND)
1592 gcc_assert (GET_MODE_UNIT_PRECISION (mode)
1593 > GET_MODE_UNIT_PRECISION (GET_MODE (op)));
1594 return simplify_gen_unary (GET_CODE (op), mode, XEXP (op, 0),
1595 GET_MODE (XEXP (op, 0)));
1598 /* (sign_extend:M (ashiftrt:N (ashift <X> (const_int I)) (const_int I)))
1599 is (sign_extend:M (subreg:O <X>)) if there is mode with
1600 GET_MODE_BITSIZE (N) - I bits.
1601 (sign_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1602 is similarly (zero_extend:M (subreg:O <X>)). */
1603 if ((GET_CODE (op) == ASHIFTRT || GET_CODE (op) == LSHIFTRT)
1604 && GET_CODE (XEXP (op, 0)) == ASHIFT
1605 && is_a <scalar_int_mode> (mode, &int_mode)
1606 && CONST_INT_P (XEXP (op, 1))
1607 && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1608 && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1609 GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1611 scalar_int_mode tmode;
1612 gcc_assert (GET_MODE_PRECISION (int_mode)
1613 > GET_MODE_PRECISION (op_mode));
1614 if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1615 - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1617 rtx inner =
1618 rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1619 if (inner)
1620 return simplify_gen_unary (GET_CODE (op) == ASHIFTRT
1621 ? SIGN_EXTEND : ZERO_EXTEND,
1622 int_mode, inner, tmode);
1626 /* (sign_extend:M (lshiftrt:N <X> (const_int I))) is better as
1627 (zero_extend:M (lshiftrt:N <X> (const_int I))) if I is not 0. */
1628 if (GET_CODE (op) == LSHIFTRT
1629 && CONST_INT_P (XEXP (op, 1))
1630 && XEXP (op, 1) != const0_rtx)
1631 return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1633 /* (sign_extend:M (truncate:N (lshiftrt:O <X> (const_int I)))) where
1634 I is GET_MODE_PRECISION(O) - GET_MODE_PRECISION(N), simplifies to
1635 (ashiftrt:M <X> (const_int I)) if modes M and O are the same, and
1636 (truncate:M (ashiftrt:O <X> (const_int I))) if M is narrower than
1637 O, and (sign_extend:M (ashiftrt:O <X> (const_int I))) if M is
1638 wider than O. */
1639 if (GET_CODE (op) == TRUNCATE
1640 && GET_CODE (XEXP (op, 0)) == LSHIFTRT
1641 && CONST_INT_P (XEXP (XEXP (op, 0), 1)))
1643 scalar_int_mode m_mode, n_mode, o_mode;
1644 rtx old_shift = XEXP (op, 0);
1645 if (is_a <scalar_int_mode> (mode, &m_mode)
1646 && is_a <scalar_int_mode> (GET_MODE (op), &n_mode)
1647 && is_a <scalar_int_mode> (GET_MODE (old_shift), &o_mode)
1648 && GET_MODE_PRECISION (o_mode) - GET_MODE_PRECISION (n_mode)
1649 == INTVAL (XEXP (old_shift, 1)))
1651 rtx new_shift = simplify_gen_binary (ASHIFTRT,
1652 GET_MODE (old_shift),
1653 XEXP (old_shift, 0),
1654 XEXP (old_shift, 1));
1655 if (GET_MODE_PRECISION (m_mode) > GET_MODE_PRECISION (o_mode))
1656 return simplify_gen_unary (SIGN_EXTEND, mode, new_shift,
1657 GET_MODE (new_shift));
1658 if (mode != GET_MODE (new_shift))
1659 return simplify_gen_unary (TRUNCATE, mode, new_shift,
1660 GET_MODE (new_shift));
1661 return new_shift;
1665 /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when
1666 we know the sign bit of OP must be clear. */
1667 if (val_signbit_known_clear_p (GET_MODE (op),
1668 nonzero_bits (op, GET_MODE (op))))
1669 return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1671 /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1672 if (GET_CODE (op) == SUBREG
1673 && subreg_lowpart_p (op)
1674 && GET_MODE (SUBREG_REG (op)) == mode
1675 && is_a <scalar_int_mode> (mode, &int_mode)
1676 && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1677 && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1678 && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1679 && (nonzero_bits (SUBREG_REG (op), mode)
1680 & ~(GET_MODE_MASK (op_mode) >> 1)) == 0)
1681 return SUBREG_REG (op);
1683 #if defined(POINTERS_EXTEND_UNSIGNED)
1684 /* As we do not know which address space the pointer is referring to,
1685 we can do this only if the target does not support different pointer
1686 or address modes depending on the address space. */
1687 if (target_default_pointer_address_modes_p ()
1688 && ! POINTERS_EXTEND_UNSIGNED
1689 && mode == Pmode && GET_MODE (op) == ptr_mode
1690 && (CONSTANT_P (op)
1691 || (GET_CODE (op) == SUBREG
1692 && REG_P (SUBREG_REG (op))
1693 && REG_POINTER (SUBREG_REG (op))
1694 && GET_MODE (SUBREG_REG (op)) == Pmode))
1695 && !targetm.have_ptr_extend ())
1697 temp
1698 = convert_memory_address_addr_space_1 (Pmode, op,
1699 ADDR_SPACE_GENERIC, false,
1700 true);
1701 if (temp)
1702 return temp;
1704 #endif
1705 break;
1707 case ZERO_EXTEND:
1708 /* Check for useless extension. */
1709 if (GET_MODE (op) == mode)
1710 return op;
1712 /* Check for a zero extension of a subreg of a promoted
1713 variable, where the promotion is zero-extended, and the
1714 target mode is the same as the variable's promotion. */
1715 if (GET_CODE (op) == SUBREG
1716 && SUBREG_PROMOTED_VAR_P (op)
1717 && SUBREG_PROMOTED_UNSIGNED_P (op))
1719 rtx subreg = SUBREG_REG (op);
1720 machine_mode subreg_mode = GET_MODE (subreg);
1721 if (!paradoxical_subreg_p (mode, subreg_mode))
1723 temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1724 if (temp)
1726 /* Preserve SUBREG_PROMOTED_VAR_P. */
1727 if (partial_subreg_p (temp))
1729 SUBREG_PROMOTED_VAR_P (temp) = 1;
1730 SUBREG_PROMOTED_SET (temp, SRP_UNSIGNED);
1732 return temp;
1735 else
1736 /* Zero-extending a zero-extended subreg. */
1737 return simplify_gen_unary (ZERO_EXTEND, mode,
1738 subreg, subreg_mode);
1741 /* Extending a widening multiplication should be canonicalized to
1742 a wider widening multiplication. */
1743 if (GET_CODE (op) == MULT)
1745 rtx lhs = XEXP (op, 0);
1746 rtx rhs = XEXP (op, 1);
1747 enum rtx_code lcode = GET_CODE (lhs);
1748 enum rtx_code rcode = GET_CODE (rhs);
1750 /* Widening multiplies usually extend both operands, but sometimes
1751 they use a shift to extract a portion of a register. */
1752 if ((lcode == ZERO_EXTEND
1753 || (lcode == LSHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1754 && (rcode == ZERO_EXTEND
1755 || (rcode == LSHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1757 machine_mode lmode = GET_MODE (lhs);
1758 machine_mode rmode = GET_MODE (rhs);
1759 int bits;
1761 if (lcode == LSHIFTRT)
1762 /* Number of bits not shifted off the end. */
1763 bits = (GET_MODE_UNIT_PRECISION (lmode)
1764 - INTVAL (XEXP (lhs, 1)));
1765 else /* lcode == ZERO_EXTEND */
1766 /* Size of inner mode. */
1767 bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1769 if (rcode == LSHIFTRT)
1770 bits += (GET_MODE_UNIT_PRECISION (rmode)
1771 - INTVAL (XEXP (rhs, 1)));
1772 else /* rcode == ZERO_EXTEND */
1773 bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1775 /* We can only widen multiplies if the result is mathematiclly
1776 equivalent. I.e. if overflow was impossible. */
1777 if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1778 return simplify_gen_binary
1779 (MULT, mode,
1780 simplify_gen_unary (ZERO_EXTEND, mode, lhs, lmode),
1781 simplify_gen_unary (ZERO_EXTEND, mode, rhs, rmode));
1785 /* (zero_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1786 if (GET_CODE (op) == ZERO_EXTEND)
1787 return simplify_gen_unary (ZERO_EXTEND, mode, XEXP (op, 0),
1788 GET_MODE (XEXP (op, 0)));
1790 /* (zero_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1791 is (zero_extend:M (subreg:O <X>)) if there is mode with
1792 GET_MODE_PRECISION (N) - I bits. */
1793 if (GET_CODE (op) == LSHIFTRT
1794 && GET_CODE (XEXP (op, 0)) == ASHIFT
1795 && is_a <scalar_int_mode> (mode, &int_mode)
1796 && CONST_INT_P (XEXP (op, 1))
1797 && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1798 && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1799 GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1801 scalar_int_mode tmode;
1802 if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1803 - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1805 rtx inner =
1806 rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1807 if (inner)
1808 return simplify_gen_unary (ZERO_EXTEND, int_mode,
1809 inner, tmode);
1813 /* (zero_extend:M (subreg:N <X:O>)) is <X:O> (for M == O) or
1814 (zero_extend:M <X:O>), if X doesn't have any non-zero bits outside
1815 of mode N. E.g.
1816 (zero_extend:SI (subreg:QI (and:SI (reg:SI) (const_int 63)) 0)) is
1817 (and:SI (reg:SI) (const_int 63)). */
1818 if (partial_subreg_p (op)
1819 && is_a <scalar_int_mode> (mode, &int_mode)
1820 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &op0_mode)
1821 && GET_MODE_PRECISION (op0_mode) <= HOST_BITS_PER_WIDE_INT
1822 && GET_MODE_PRECISION (int_mode) >= GET_MODE_PRECISION (op0_mode)
1823 && subreg_lowpart_p (op)
1824 && (nonzero_bits (SUBREG_REG (op), op0_mode)
1825 & ~GET_MODE_MASK (GET_MODE (op))) == 0)
1827 if (GET_MODE_PRECISION (int_mode) == GET_MODE_PRECISION (op0_mode))
1828 return SUBREG_REG (op);
1829 return simplify_gen_unary (ZERO_EXTEND, int_mode, SUBREG_REG (op),
1830 op0_mode);
1833 /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1834 if (GET_CODE (op) == SUBREG
1835 && subreg_lowpart_p (op)
1836 && GET_MODE (SUBREG_REG (op)) == mode
1837 && is_a <scalar_int_mode> (mode, &int_mode)
1838 && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1839 && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1840 && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1841 && (nonzero_bits (SUBREG_REG (op), mode)
1842 & ~GET_MODE_MASK (op_mode)) == 0)
1843 return SUBREG_REG (op);
1845 #if defined(POINTERS_EXTEND_UNSIGNED)
1846 /* As we do not know which address space the pointer is referring to,
1847 we can do this only if the target does not support different pointer
1848 or address modes depending on the address space. */
1849 if (target_default_pointer_address_modes_p ()
1850 && POINTERS_EXTEND_UNSIGNED > 0
1851 && mode == Pmode && GET_MODE (op) == ptr_mode
1852 && (CONSTANT_P (op)
1853 || (GET_CODE (op) == SUBREG
1854 && REG_P (SUBREG_REG (op))
1855 && REG_POINTER (SUBREG_REG (op))
1856 && GET_MODE (SUBREG_REG (op)) == Pmode))
1857 && !targetm.have_ptr_extend ())
1859 temp
1860 = convert_memory_address_addr_space_1 (Pmode, op,
1861 ADDR_SPACE_GENERIC, false,
1862 true);
1863 if (temp)
1864 return temp;
1866 #endif
1867 break;
1869 default:
1870 break;
1873 if (VECTOR_MODE_P (mode)
1874 && vec_duplicate_p (op, &elt)
1875 && code != VEC_DUPLICATE)
1877 if (code == SIGN_EXTEND || code == ZERO_EXTEND)
1878 /* Enforce a canonical order of VEC_DUPLICATE wrt other unary
1879 operations by promoting VEC_DUPLICATE to the root of the expression
1880 (as far as possible). */
1881 temp = simplify_gen_unary (code, GET_MODE_INNER (mode),
1882 elt, GET_MODE_INNER (GET_MODE (op)));
1883 else
1884 /* Try applying the operator to ELT and see if that simplifies.
1885 We can duplicate the result if so.
1887 The reason we traditionally haven't used simplify_gen_unary
1888 for these codes is that it didn't necessarily seem to be a
1889 win to convert things like:
1891 (neg:V (vec_duplicate:V (reg:S R)))
1895 (vec_duplicate:V (neg:S (reg:S R)))
1897 The first might be done entirely in vector registers while the
1898 second might need a move between register files.
1900 However, there also cases where promoting the vec_duplicate is
1901 more efficient, and there is definite value in having a canonical
1902 form when matching instruction patterns. We should consider
1903 extending the simplify_gen_unary code above to more cases. */
1904 temp = simplify_unary_operation (code, GET_MODE_INNER (mode),
1905 elt, GET_MODE_INNER (GET_MODE (op)));
1906 if (temp)
1907 return gen_vec_duplicate (mode, temp);
1910 return 0;
1913 /* Try to compute the value of a unary operation CODE whose output mode is to
1914 be MODE with input operand OP whose mode was originally OP_MODE.
1915 Return zero if the value cannot be computed. */
1917 simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
1918 rtx op, machine_mode op_mode)
1920 scalar_int_mode result_mode;
1922 if (code == VEC_DUPLICATE)
1924 gcc_assert (VECTOR_MODE_P (mode));
1925 if (GET_MODE (op) != VOIDmode)
1927 if (!VECTOR_MODE_P (GET_MODE (op)))
1928 gcc_assert (GET_MODE_INNER (mode) == GET_MODE (op));
1929 else
1930 gcc_assert (GET_MODE_INNER (mode) == GET_MODE_INNER
1931 (GET_MODE (op)));
1933 if (CONST_SCALAR_INT_P (op) || CONST_DOUBLE_AS_FLOAT_P (op))
1934 return gen_const_vec_duplicate (mode, op);
1935 if (GET_CODE (op) == CONST_VECTOR
1936 && (CONST_VECTOR_DUPLICATE_P (op)
1937 || CONST_VECTOR_NUNITS (op).is_constant ()))
1939 unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op)
1940 ? CONST_VECTOR_NPATTERNS (op)
1941 : CONST_VECTOR_NUNITS (op).to_constant ());
1942 gcc_assert (multiple_p (GET_MODE_NUNITS (mode), npatterns));
1943 rtx_vector_builder builder (mode, npatterns, 1);
1944 for (unsigned i = 0; i < npatterns; i++)
1945 builder.quick_push (CONST_VECTOR_ELT (op, i));
1946 return builder.build ();
1950 if (VECTOR_MODE_P (mode)
1951 && GET_CODE (op) == CONST_VECTOR
1952 && known_eq (GET_MODE_NUNITS (mode), CONST_VECTOR_NUNITS (op)))
1954 gcc_assert (GET_MODE (op) == op_mode);
1956 rtx_vector_builder builder;
1957 if (!builder.new_unary_operation (mode, op, false))
1958 return 0;
1960 unsigned int count = builder.encoded_nelts ();
1961 for (unsigned int i = 0; i < count; i++)
1963 rtx x = simplify_unary_operation (code, GET_MODE_INNER (mode),
1964 CONST_VECTOR_ELT (op, i),
1965 GET_MODE_INNER (op_mode));
1966 if (!x || !valid_for_const_vector_p (mode, x))
1967 return 0;
1968 builder.quick_push (x);
1970 return builder.build ();
1973 /* The order of these tests is critical so that, for example, we don't
1974 check the wrong mode (input vs. output) for a conversion operation,
1975 such as FIX. At some point, this should be simplified. */
1977 if (code == FLOAT && CONST_SCALAR_INT_P (op))
1979 REAL_VALUE_TYPE d;
1981 if (op_mode == VOIDmode)
1983 /* CONST_INT have VOIDmode as the mode. We assume that all
1984 the bits of the constant are significant, though, this is
1985 a dangerous assumption as many times CONST_INTs are
1986 created and used with garbage in the bits outside of the
1987 precision of the implied mode of the const_int. */
1988 op_mode = MAX_MODE_INT;
1991 real_from_integer (&d, mode, rtx_mode_t (op, op_mode), SIGNED);
1993 /* Avoid the folding if flag_signaling_nans is on and
1994 operand is a signaling NaN. */
1995 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
1996 return 0;
1998 d = real_value_truncate (mode, d);
2000 /* Avoid the folding if flag_rounding_math is on and the
2001 conversion is not exact. */
2002 if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2004 bool fail = false;
2005 wide_int w = real_to_integer (&d, &fail,
2006 GET_MODE_PRECISION
2007 (as_a <scalar_int_mode> (op_mode)));
2008 if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
2009 return 0;
2012 return const_double_from_real_value (d, mode);
2014 else if (code == UNSIGNED_FLOAT && CONST_SCALAR_INT_P (op))
2016 REAL_VALUE_TYPE d;
2018 if (op_mode == VOIDmode)
2020 /* CONST_INT have VOIDmode as the mode. We assume that all
2021 the bits of the constant are significant, though, this is
2022 a dangerous assumption as many times CONST_INTs are
2023 created and used with garbage in the bits outside of the
2024 precision of the implied mode of the const_int. */
2025 op_mode = MAX_MODE_INT;
2028 real_from_integer (&d, mode, rtx_mode_t (op, op_mode), UNSIGNED);
2030 /* Avoid the folding if flag_signaling_nans is on and
2031 operand is a signaling NaN. */
2032 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2033 return 0;
2035 d = real_value_truncate (mode, d);
2037 /* Avoid the folding if flag_rounding_math is on and the
2038 conversion is not exact. */
2039 if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2041 bool fail = false;
2042 wide_int w = real_to_integer (&d, &fail,
2043 GET_MODE_PRECISION
2044 (as_a <scalar_int_mode> (op_mode)));
2045 if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
2046 return 0;
2049 return const_double_from_real_value (d, mode);
2052 if (CONST_SCALAR_INT_P (op) && is_a <scalar_int_mode> (mode, &result_mode))
2054 unsigned int width = GET_MODE_PRECISION (result_mode);
2055 if (width > MAX_BITSIZE_MODE_ANY_INT)
2056 return 0;
2058 wide_int result;
2059 scalar_int_mode imode = (op_mode == VOIDmode
2060 ? result_mode
2061 : as_a <scalar_int_mode> (op_mode));
2062 rtx_mode_t op0 = rtx_mode_t (op, imode);
2063 int int_value;
2065 #if TARGET_SUPPORTS_WIDE_INT == 0
2066 /* This assert keeps the simplification from producing a result
2067 that cannot be represented in a CONST_DOUBLE but a lot of
2068 upstream callers expect that this function never fails to
2069 simplify something and so you if you added this to the test
2070 above the code would die later anyway. If this assert
2071 happens, you just need to make the port support wide int. */
2072 gcc_assert (width <= HOST_BITS_PER_DOUBLE_INT);
2073 #endif
2075 switch (code)
2077 case NOT:
2078 result = wi::bit_not (op0);
2079 break;
2081 case NEG:
2082 result = wi::neg (op0);
2083 break;
2085 case ABS:
2086 result = wi::abs (op0);
2087 break;
2089 case FFS:
2090 result = wi::shwi (wi::ffs (op0), result_mode);
2091 break;
2093 case CLZ:
2094 if (wi::ne_p (op0, 0))
2095 int_value = wi::clz (op0);
2096 else if (! CLZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
2097 return NULL_RTX;
2098 result = wi::shwi (int_value, result_mode);
2099 break;
2101 case CLRSB:
2102 result = wi::shwi (wi::clrsb (op0), result_mode);
2103 break;
2105 case CTZ:
2106 if (wi::ne_p (op0, 0))
2107 int_value = wi::ctz (op0);
2108 else if (! CTZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
2109 return NULL_RTX;
2110 result = wi::shwi (int_value, result_mode);
2111 break;
2113 case POPCOUNT:
2114 result = wi::shwi (wi::popcount (op0), result_mode);
2115 break;
2117 case PARITY:
2118 result = wi::shwi (wi::parity (op0), result_mode);
2119 break;
2121 case BSWAP:
2122 result = wi::bswap (op0);
2123 break;
2125 case BITREVERSE:
2126 result = wi::bitreverse (op0);
2127 break;
2129 case TRUNCATE:
2130 case ZERO_EXTEND:
2131 result = wide_int::from (op0, width, UNSIGNED);
2132 break;
2134 case SIGN_EXTEND:
2135 result = wide_int::from (op0, width, SIGNED);
2136 break;
2138 case SS_NEG:
2139 if (wi::only_sign_bit_p (op0))
2140 result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED);
2141 else
2142 result = wi::neg (op0);
2143 break;
2145 case SS_ABS:
2146 if (wi::only_sign_bit_p (op0))
2147 result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED);
2148 else
2149 result = wi::abs (op0);
2150 break;
2152 case SQRT:
2153 default:
2154 return 0;
2157 return immed_wide_int_const (result, result_mode);
2160 else if (CONST_DOUBLE_AS_FLOAT_P (op)
2161 && SCALAR_FLOAT_MODE_P (mode)
2162 && SCALAR_FLOAT_MODE_P (GET_MODE (op)))
2164 REAL_VALUE_TYPE d = *CONST_DOUBLE_REAL_VALUE (op);
2165 switch (code)
2167 case SQRT:
2168 return 0;
2169 case ABS:
2170 d = real_value_abs (&d);
2171 break;
2172 case NEG:
2173 d = real_value_negate (&d);
2174 break;
2175 case FLOAT_TRUNCATE:
2176 /* Don't perform the operation if flag_signaling_nans is on
2177 and the operand is a signaling NaN. */
2178 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2179 return NULL_RTX;
2180 /* Or if flag_rounding_math is on and the truncation is not
2181 exact. */
2182 if (HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2183 && !exact_real_truncate (mode, &d))
2184 return NULL_RTX;
2185 d = real_value_truncate (mode, d);
2186 break;
2187 case FLOAT_EXTEND:
2188 /* Don't perform the operation if flag_signaling_nans is on
2189 and the operand is a signaling NaN. */
2190 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2191 return NULL_RTX;
2192 /* All this does is change the mode, unless changing
2193 mode class. */
2194 if (GET_MODE_CLASS (mode) != GET_MODE_CLASS (GET_MODE (op)))
2195 real_convert (&d, mode, &d);
2196 break;
2197 case FIX:
2198 /* Don't perform the operation if flag_signaling_nans is on
2199 and the operand is a signaling NaN. */
2200 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2201 return NULL_RTX;
2202 real_arithmetic (&d, FIX_TRUNC_EXPR, &d, NULL);
2203 break;
2204 case NOT:
2206 long tmp[4];
2207 int i;
2209 real_to_target (tmp, &d, GET_MODE (op));
2210 for (i = 0; i < 4; i++)
2211 tmp[i] = ~tmp[i];
2212 real_from_target (&d, tmp, mode);
2213 break;
2215 default:
2216 gcc_unreachable ();
2218 return const_double_from_real_value (d, mode);
2220 else if (CONST_DOUBLE_AS_FLOAT_P (op)
2221 && SCALAR_FLOAT_MODE_P (GET_MODE (op))
2222 && is_int_mode (mode, &result_mode))
2224 unsigned int width = GET_MODE_PRECISION (result_mode);
2225 if (width > MAX_BITSIZE_MODE_ANY_INT)
2226 return 0;
2228 /* Although the overflow semantics of RTL's FIX and UNSIGNED_FIX
2229 operators are intentionally left unspecified (to ease implementation
2230 by target backends), for consistency, this routine implements the
2231 same semantics for constant folding as used by the middle-end. */
2233 /* This was formerly used only for non-IEEE float.
2234 eggert@twinsun.com says it is safe for IEEE also. */
2235 REAL_VALUE_TYPE t;
2236 const REAL_VALUE_TYPE *x = CONST_DOUBLE_REAL_VALUE (op);
2237 wide_int wmax, wmin;
2238 /* This is part of the abi to real_to_integer, but we check
2239 things before making this call. */
2240 bool fail;
2242 switch (code)
2244 case FIX:
2245 if (REAL_VALUE_ISNAN (*x))
2246 return const0_rtx;
2248 /* Test against the signed upper bound. */
2249 wmax = wi::max_value (width, SIGNED);
2250 real_from_integer (&t, VOIDmode, wmax, SIGNED);
2251 if (real_less (&t, x))
2252 return immed_wide_int_const (wmax, mode);
2254 /* Test against the signed lower bound. */
2255 wmin = wi::min_value (width, SIGNED);
2256 real_from_integer (&t, VOIDmode, wmin, SIGNED);
2257 if (real_less (x, &t))
2258 return immed_wide_int_const (wmin, mode);
2260 return immed_wide_int_const (real_to_integer (x, &fail, width),
2261 mode);
2263 case UNSIGNED_FIX:
2264 if (REAL_VALUE_ISNAN (*x) || REAL_VALUE_NEGATIVE (*x))
2265 return const0_rtx;
2267 /* Test against the unsigned upper bound. */
2268 wmax = wi::max_value (width, UNSIGNED);
2269 real_from_integer (&t, VOIDmode, wmax, UNSIGNED);
2270 if (real_less (&t, x))
2271 return immed_wide_int_const (wmax, mode);
2273 return immed_wide_int_const (real_to_integer (x, &fail, width),
2274 mode);
2276 default:
2277 gcc_unreachable ();
2281 /* Handle polynomial integers. */
2282 else if (CONST_POLY_INT_P (op))
2284 poly_wide_int result;
2285 switch (code)
2287 case NEG:
2288 result = -const_poly_int_value (op);
2289 break;
2291 case NOT:
2292 result = ~const_poly_int_value (op);
2293 break;
2295 default:
2296 return NULL_RTX;
2298 return immed_wide_int_const (result, mode);
2301 return NULL_RTX;
2304 /* Subroutine of simplify_binary_operation to simplify a binary operation
2305 CODE that can commute with byte swapping, with result mode MODE and
2306 operating on OP0 and OP1. CODE is currently one of AND, IOR or XOR.
2307 Return zero if no simplification or canonicalization is possible. */
2310 simplify_context::simplify_byte_swapping_operation (rtx_code code,
2311 machine_mode mode,
2312 rtx op0, rtx op1)
2314 rtx tem;
2316 /* (op (bswap x) C1)) -> (bswap (op x C2)) with C2 swapped. */
2317 if (GET_CODE (op0) == BSWAP && CONST_SCALAR_INT_P (op1))
2319 tem = simplify_gen_binary (code, mode, XEXP (op0, 0),
2320 simplify_gen_unary (BSWAP, mode, op1, mode));
2321 return simplify_gen_unary (BSWAP, mode, tem, mode);
2324 /* (op (bswap x) (bswap y)) -> (bswap (op x y)). */
2325 if (GET_CODE (op0) == BSWAP && GET_CODE (op1) == BSWAP)
2327 tem = simplify_gen_binary (code, mode, XEXP (op0, 0), XEXP (op1, 0));
2328 return simplify_gen_unary (BSWAP, mode, tem, mode);
2331 return NULL_RTX;
2334 /* Subroutine of simplify_binary_operation to simplify a commutative,
2335 associative binary operation CODE with result mode MODE, operating
2336 on OP0 and OP1. CODE is currently one of PLUS, MULT, AND, IOR, XOR,
2337 SMIN, SMAX, UMIN or UMAX. Return zero if no simplification or
2338 canonicalization is possible. */
2341 simplify_context::simplify_associative_operation (rtx_code code,
2342 machine_mode mode,
2343 rtx op0, rtx op1)
2345 rtx tem;
2347 /* Normally expressions simplified by simplify-rtx.cc are combined
2348 at most from a few machine instructions and therefore the
2349 expressions should be fairly small. During var-tracking
2350 we can see arbitrarily large expressions though and reassociating
2351 those can be quadratic, so punt after encountering max_assoc_count
2352 simplify_associative_operation calls during outermost simplify_*
2353 call. */
2354 if (++assoc_count >= max_assoc_count)
2355 return NULL_RTX;
2357 /* Linearize the operator to the left. */
2358 if (GET_CODE (op1) == code)
2360 /* "(a op b) op (c op d)" becomes "((a op b) op c) op d)". */
2361 if (GET_CODE (op0) == code)
2363 tem = simplify_gen_binary (code, mode, op0, XEXP (op1, 0));
2364 return simplify_gen_binary (code, mode, tem, XEXP (op1, 1));
2367 /* "a op (b op c)" becomes "(b op c) op a". */
2368 if (! swap_commutative_operands_p (op1, op0))
2369 return simplify_gen_binary (code, mode, op1, op0);
2371 std::swap (op0, op1);
2374 if (GET_CODE (op0) == code)
2376 /* Canonicalize "(x op c) op y" as "(x op y) op c". */
2377 if (swap_commutative_operands_p (XEXP (op0, 1), op1))
2379 tem = simplify_gen_binary (code, mode, XEXP (op0, 0), op1);
2380 return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2383 /* Attempt to simplify "(a op b) op c" as "a op (b op c)". */
2384 tem = simplify_binary_operation (code, mode, XEXP (op0, 1), op1);
2385 if (tem != 0)
2386 return simplify_gen_binary (code, mode, XEXP (op0, 0), tem);
2388 /* Attempt to simplify "(a op b) op c" as "(a op c) op b". */
2389 tem = simplify_binary_operation (code, mode, XEXP (op0, 0), op1);
2390 if (tem != 0)
2391 return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2394 return 0;
2397 /* Return a mask describing the COMPARISON. */
2398 static int
2399 comparison_to_mask (enum rtx_code comparison)
2401 switch (comparison)
2403 case LT:
2404 return 8;
2405 case GT:
2406 return 4;
2407 case EQ:
2408 return 2;
2409 case UNORDERED:
2410 return 1;
2412 case LTGT:
2413 return 12;
2414 case LE:
2415 return 10;
2416 case GE:
2417 return 6;
2418 case UNLT:
2419 return 9;
2420 case UNGT:
2421 return 5;
2422 case UNEQ:
2423 return 3;
2425 case ORDERED:
2426 return 14;
2427 case NE:
2428 return 13;
2429 case UNLE:
2430 return 11;
2431 case UNGE:
2432 return 7;
2434 default:
2435 gcc_unreachable ();
2439 /* Return a comparison corresponding to the MASK. */
2440 static enum rtx_code
2441 mask_to_comparison (int mask)
2443 switch (mask)
2445 case 8:
2446 return LT;
2447 case 4:
2448 return GT;
2449 case 2:
2450 return EQ;
2451 case 1:
2452 return UNORDERED;
2454 case 12:
2455 return LTGT;
2456 case 10:
2457 return LE;
2458 case 6:
2459 return GE;
2460 case 9:
2461 return UNLT;
2462 case 5:
2463 return UNGT;
2464 case 3:
2465 return UNEQ;
2467 case 14:
2468 return ORDERED;
2469 case 13:
2470 return NE;
2471 case 11:
2472 return UNLE;
2473 case 7:
2474 return UNGE;
2476 default:
2477 gcc_unreachable ();
2481 /* Return true if CODE is valid for comparisons of mode MODE, false
2482 otherwise.
2484 It is always safe to return false, even if the code was valid for the
2485 given mode as that will merely suppress optimizations. */
2487 static bool
2488 comparison_code_valid_for_mode (enum rtx_code code, enum machine_mode mode)
2490 switch (code)
2492 /* These are valid for integral, floating and vector modes. */
2493 case NE:
2494 case EQ:
2495 case GE:
2496 case GT:
2497 case LE:
2498 case LT:
2499 return (INTEGRAL_MODE_P (mode)
2500 || FLOAT_MODE_P (mode)
2501 || VECTOR_MODE_P (mode));
2503 /* These are valid for floating point modes. */
2504 case LTGT:
2505 case UNORDERED:
2506 case ORDERED:
2507 case UNEQ:
2508 case UNGE:
2509 case UNGT:
2510 case UNLE:
2511 case UNLT:
2512 return FLOAT_MODE_P (mode);
2514 /* These are filtered out in simplify_logical_operation, but
2515 we check for them too as a matter of safety. They are valid
2516 for integral and vector modes. */
2517 case GEU:
2518 case GTU:
2519 case LEU:
2520 case LTU:
2521 return INTEGRAL_MODE_P (mode) || VECTOR_MODE_P (mode);
2523 default:
2524 gcc_unreachable ();
2528 /* Canonicalize RES, a scalar const0_rtx/const_true_rtx to the right
2529 false/true value of comparison with MODE where comparison operands
2530 have CMP_MODE. */
2532 static rtx
2533 relational_result (machine_mode mode, machine_mode cmp_mode, rtx res)
2535 if (SCALAR_FLOAT_MODE_P (mode))
2537 if (res == const0_rtx)
2538 return CONST0_RTX (mode);
2539 #ifdef FLOAT_STORE_FLAG_VALUE
2540 REAL_VALUE_TYPE val = FLOAT_STORE_FLAG_VALUE (mode);
2541 return const_double_from_real_value (val, mode);
2542 #else
2543 return NULL_RTX;
2544 #endif
2546 if (VECTOR_MODE_P (mode))
2548 if (res == const0_rtx)
2549 return CONST0_RTX (mode);
2550 #ifdef VECTOR_STORE_FLAG_VALUE
2551 rtx val = VECTOR_STORE_FLAG_VALUE (mode);
2552 if (val == NULL_RTX)
2553 return NULL_RTX;
2554 if (val == const1_rtx)
2555 return CONST1_RTX (mode);
2557 return gen_const_vec_duplicate (mode, val);
2558 #else
2559 return NULL_RTX;
2560 #endif
2562 /* For vector comparison with scalar int result, it is unknown
2563 if the target means here a comparison into an integral bitmask,
2564 or comparison where all comparisons true mean const_true_rtx
2565 whole result, or where any comparisons true mean const_true_rtx
2566 whole result. For const0_rtx all the cases are the same. */
2567 if (VECTOR_MODE_P (cmp_mode)
2568 && SCALAR_INT_MODE_P (mode)
2569 && res == const_true_rtx)
2570 return NULL_RTX;
2572 return res;
2575 /* Simplify a logical operation CODE with result mode MODE, operating on OP0
2576 and OP1, which should be both relational operations. Return 0 if no such
2577 simplification is possible. */
2579 simplify_context::simplify_logical_relational_operation (rtx_code code,
2580 machine_mode mode,
2581 rtx op0, rtx op1)
2583 /* We only handle IOR of two relational operations. */
2584 if (code != IOR)
2585 return 0;
2587 if (!(COMPARISON_P (op0) && COMPARISON_P (op1)))
2588 return 0;
2590 if (!(rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2591 && rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))))
2592 return 0;
2594 enum rtx_code code0 = GET_CODE (op0);
2595 enum rtx_code code1 = GET_CODE (op1);
2597 /* We don't handle unsigned comparisons currently. */
2598 if (code0 == LTU || code0 == GTU || code0 == LEU || code0 == GEU)
2599 return 0;
2600 if (code1 == LTU || code1 == GTU || code1 == LEU || code1 == GEU)
2601 return 0;
2603 int mask0 = comparison_to_mask (code0);
2604 int mask1 = comparison_to_mask (code1);
2606 int mask = mask0 | mask1;
2608 if (mask == 15)
2609 return relational_result (mode, GET_MODE (op0), const_true_rtx);
2611 code = mask_to_comparison (mask);
2613 /* Many comparison codes are only valid for certain mode classes. */
2614 if (!comparison_code_valid_for_mode (code, mode))
2615 return 0;
2617 op0 = XEXP (op1, 0);
2618 op1 = XEXP (op1, 1);
2620 return simplify_gen_relational (code, mode, VOIDmode, op0, op1);
2623 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
2624 and OP1. Return 0 if no simplification is possible.
2626 Don't use this for relational operations such as EQ or LT.
2627 Use simplify_relational_operation instead. */
2629 simplify_context::simplify_binary_operation (rtx_code code, machine_mode mode,
2630 rtx op0, rtx op1)
2632 rtx trueop0, trueop1;
2633 rtx tem;
2635 /* Relational operations don't work here. We must know the mode
2636 of the operands in order to do the comparison correctly.
2637 Assuming a full word can give incorrect results.
2638 Consider comparing 128 with -128 in QImode. */
2639 gcc_assert (GET_RTX_CLASS (code) != RTX_COMPARE);
2640 gcc_assert (GET_RTX_CLASS (code) != RTX_COMM_COMPARE);
2642 /* Make sure the constant is second. */
2643 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
2644 && swap_commutative_operands_p (op0, op1))
2645 std::swap (op0, op1);
2647 trueop0 = avoid_constant_pool_reference (op0);
2648 trueop1 = avoid_constant_pool_reference (op1);
2650 tem = simplify_const_binary_operation (code, mode, trueop0, trueop1);
2651 if (tem)
2652 return tem;
2653 tem = simplify_binary_operation_1 (code, mode, op0, op1, trueop0, trueop1);
2655 if (tem)
2656 return tem;
2658 /* If the above steps did not result in a simplification and op0 or op1
2659 were constant pool references, use the referenced constants directly. */
2660 if (trueop0 != op0 || trueop1 != op1)
2661 return simplify_gen_binary (code, mode, trueop0, trueop1);
2663 return NULL_RTX;
2666 /* Subroutine of simplify_binary_operation_1 that looks for cases in
2667 which OP0 and OP1 are both vector series or vector duplicates
2668 (which are really just series with a step of 0). If so, try to
2669 form a new series by applying CODE to the bases and to the steps.
2670 Return null if no simplification is possible.
2672 MODE is the mode of the operation and is known to be a vector
2673 integer mode. */
2676 simplify_context::simplify_binary_operation_series (rtx_code code,
2677 machine_mode mode,
2678 rtx op0, rtx op1)
2680 rtx base0, step0;
2681 if (vec_duplicate_p (op0, &base0))
2682 step0 = const0_rtx;
2683 else if (!vec_series_p (op0, &base0, &step0))
2684 return NULL_RTX;
2686 rtx base1, step1;
2687 if (vec_duplicate_p (op1, &base1))
2688 step1 = const0_rtx;
2689 else if (!vec_series_p (op1, &base1, &step1))
2690 return NULL_RTX;
2692 /* Only create a new series if we can simplify both parts. In other
2693 cases this isn't really a simplification, and it's not necessarily
2694 a win to replace a vector operation with a scalar operation. */
2695 scalar_mode inner_mode = GET_MODE_INNER (mode);
2696 rtx new_base = simplify_binary_operation (code, inner_mode, base0, base1);
2697 if (!new_base)
2698 return NULL_RTX;
2700 rtx new_step = simplify_binary_operation (code, inner_mode, step0, step1);
2701 if (!new_step)
2702 return NULL_RTX;
2704 return gen_vec_series (mode, new_base, new_step);
2707 /* Subroutine of simplify_binary_operation_1. Un-distribute a binary
2708 operation CODE with result mode MODE, operating on OP0 and OP1.
2709 e.g. simplify (xor (and A C) (and (B C)) to (and (xor (A B) C).
2710 Returns NULL_RTX if no simplification is possible. */
2713 simplify_context::simplify_distributive_operation (rtx_code code,
2714 machine_mode mode,
2715 rtx op0, rtx op1)
2717 enum rtx_code op = GET_CODE (op0);
2718 gcc_assert (GET_CODE (op1) == op);
2720 if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))
2721 && ! side_effects_p (XEXP (op0, 1)))
2722 return simplify_gen_binary (op, mode,
2723 simplify_gen_binary (code, mode,
2724 XEXP (op0, 0),
2725 XEXP (op1, 0)),
2726 XEXP (op0, 1));
2728 if (GET_RTX_CLASS (op) == RTX_COMM_ARITH)
2730 if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2731 && ! side_effects_p (XEXP (op0, 0)))
2732 return simplify_gen_binary (op, mode,
2733 simplify_gen_binary (code, mode,
2734 XEXP (op0, 1),
2735 XEXP (op1, 1)),
2736 XEXP (op0, 0));
2737 if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 1))
2738 && ! side_effects_p (XEXP (op0, 0)))
2739 return simplify_gen_binary (op, mode,
2740 simplify_gen_binary (code, mode,
2741 XEXP (op0, 1),
2742 XEXP (op1, 0)),
2743 XEXP (op0, 0));
2744 if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 0))
2745 && ! side_effects_p (XEXP (op0, 1)))
2746 return simplify_gen_binary (op, mode,
2747 simplify_gen_binary (code, mode,
2748 XEXP (op0, 0),
2749 XEXP (op1, 1)),
2750 XEXP (op0, 1));
2753 return NULL_RTX;
2756 /* Return TRUE if a rotate in mode MODE with a constant count in OP1
2757 should be reversed.
2759 If the rotate should not be reversed, return FALSE.
2761 LEFT indicates if this is a rotate left or a rotate right. */
2763 bool
2764 reverse_rotate_by_imm_p (machine_mode mode, unsigned int left, rtx op1)
2766 if (!CONST_INT_P (op1))
2767 return false;
2769 /* Some targets may only be able to rotate by a constant
2770 in one direction. So we need to query the optab interface
2771 to see what is possible. */
2772 optab binoptab = left ? rotl_optab : rotr_optab;
2773 optab re_binoptab = left ? rotr_optab : rotl_optab;
2774 enum insn_code icode = optab_handler (binoptab, mode);
2775 enum insn_code re_icode = optab_handler (re_binoptab, mode);
2777 /* If the target can not support the reversed optab, then there
2778 is nothing to do. */
2779 if (re_icode == CODE_FOR_nothing)
2780 return false;
2782 /* If the target does not support the requested rotate-by-immediate,
2783 then we want to try reversing the rotate. We also want to try
2784 reversing to minimize the count. */
2785 if ((icode == CODE_FOR_nothing)
2786 || (!insn_operand_matches (icode, 2, op1))
2787 || (IN_RANGE (INTVAL (op1),
2788 GET_MODE_UNIT_PRECISION (mode) / 2 + left,
2789 GET_MODE_UNIT_PRECISION (mode) - 1)))
2790 return (insn_operand_matches (re_icode, 2, op1));
2791 return false;
2794 /* Subroutine of simplify_binary_operation. Simplify a binary operation
2795 CODE with result mode MODE, operating on OP0 and OP1. If OP0 and/or
2796 OP1 are constant pool references, TRUEOP0 and TRUEOP1 represent the
2797 actual constants. */
2800 simplify_context::simplify_binary_operation_1 (rtx_code code,
2801 machine_mode mode,
2802 rtx op0, rtx op1,
2803 rtx trueop0, rtx trueop1)
2805 rtx tem, reversed, opleft, opright, elt0, elt1;
2806 HOST_WIDE_INT val;
2807 scalar_int_mode int_mode, inner_mode;
2808 poly_int64 offset;
2810 /* Even if we can't compute a constant result,
2811 there are some cases worth simplifying. */
2813 switch (code)
2815 case PLUS:
2816 /* Maybe simplify x + 0 to x. The two expressions are equivalent
2817 when x is NaN, infinite, or finite and nonzero. They aren't
2818 when x is -0 and the rounding mode is not towards -infinity,
2819 since (-0) + 0 is then 0. */
2820 if (!HONOR_SIGNED_ZEROS (mode) && trueop1 == CONST0_RTX (mode))
2821 return op0;
2823 /* ((-a) + b) -> (b - a) and similarly for (a + (-b)). These
2824 transformations are safe even for IEEE. */
2825 if (GET_CODE (op0) == NEG)
2826 return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
2827 else if (GET_CODE (op1) == NEG)
2828 return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
2830 /* (~a) + 1 -> -a */
2831 if (INTEGRAL_MODE_P (mode)
2832 && GET_CODE (op0) == NOT
2833 && trueop1 == const1_rtx)
2834 return simplify_gen_unary (NEG, mode, XEXP (op0, 0), mode);
2836 /* Handle both-operands-constant cases. We can only add
2837 CONST_INTs to constants since the sum of relocatable symbols
2838 can't be handled by most assemblers. Don't add CONST_INT
2839 to CONST_INT since overflow won't be computed properly if wider
2840 than HOST_BITS_PER_WIDE_INT. */
2842 if ((GET_CODE (op0) == CONST
2843 || GET_CODE (op0) == SYMBOL_REF
2844 || GET_CODE (op0) == LABEL_REF)
2845 && poly_int_rtx_p (op1, &offset))
2846 return plus_constant (mode, op0, offset);
2847 else if ((GET_CODE (op1) == CONST
2848 || GET_CODE (op1) == SYMBOL_REF
2849 || GET_CODE (op1) == LABEL_REF)
2850 && poly_int_rtx_p (op0, &offset))
2851 return plus_constant (mode, op1, offset);
2853 /* See if this is something like X * C - X or vice versa or
2854 if the multiplication is written as a shift. If so, we can
2855 distribute and make a new multiply, shift, or maybe just
2856 have X (if C is 2 in the example above). But don't make
2857 something more expensive than we had before. */
2859 if (is_a <scalar_int_mode> (mode, &int_mode))
2861 rtx lhs = op0, rhs = op1;
2863 wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
2864 wide_int coeff1 = wi::one (GET_MODE_PRECISION (int_mode));
2866 if (GET_CODE (lhs) == NEG)
2868 coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
2869 lhs = XEXP (lhs, 0);
2871 else if (GET_CODE (lhs) == MULT
2872 && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
2874 coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
2875 lhs = XEXP (lhs, 0);
2877 else if (GET_CODE (lhs) == ASHIFT
2878 && CONST_INT_P (XEXP (lhs, 1))
2879 && INTVAL (XEXP (lhs, 1)) >= 0
2880 && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
2882 coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
2883 GET_MODE_PRECISION (int_mode));
2884 lhs = XEXP (lhs, 0);
2887 if (GET_CODE (rhs) == NEG)
2889 coeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
2890 rhs = XEXP (rhs, 0);
2892 else if (GET_CODE (rhs) == MULT
2893 && CONST_INT_P (XEXP (rhs, 1)))
2895 coeff1 = rtx_mode_t (XEXP (rhs, 1), int_mode);
2896 rhs = XEXP (rhs, 0);
2898 else if (GET_CODE (rhs) == ASHIFT
2899 && CONST_INT_P (XEXP (rhs, 1))
2900 && INTVAL (XEXP (rhs, 1)) >= 0
2901 && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
2903 coeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
2904 GET_MODE_PRECISION (int_mode));
2905 rhs = XEXP (rhs, 0);
2908 if (rtx_equal_p (lhs, rhs))
2910 rtx orig = gen_rtx_PLUS (int_mode, op0, op1);
2911 rtx coeff;
2912 bool speed = optimize_function_for_speed_p (cfun);
2914 coeff = immed_wide_int_const (coeff0 + coeff1, int_mode);
2916 tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
2917 return (set_src_cost (tem, int_mode, speed)
2918 <= set_src_cost (orig, int_mode, speed) ? tem : 0);
2921 /* Optimize (X - 1) * Y + Y to X * Y. */
2922 lhs = op0;
2923 rhs = op1;
2924 if (GET_CODE (op0) == MULT)
2926 if (((GET_CODE (XEXP (op0, 0)) == PLUS
2927 && XEXP (XEXP (op0, 0), 1) == constm1_rtx)
2928 || (GET_CODE (XEXP (op0, 0)) == MINUS
2929 && XEXP (XEXP (op0, 0), 1) == const1_rtx))
2930 && rtx_equal_p (XEXP (op0, 1), op1))
2931 lhs = XEXP (XEXP (op0, 0), 0);
2932 else if (((GET_CODE (XEXP (op0, 1)) == PLUS
2933 && XEXP (XEXP (op0, 1), 1) == constm1_rtx)
2934 || (GET_CODE (XEXP (op0, 1)) == MINUS
2935 && XEXP (XEXP (op0, 1), 1) == const1_rtx))
2936 && rtx_equal_p (XEXP (op0, 0), op1))
2937 lhs = XEXP (XEXP (op0, 1), 0);
2939 else if (GET_CODE (op1) == MULT)
2941 if (((GET_CODE (XEXP (op1, 0)) == PLUS
2942 && XEXP (XEXP (op1, 0), 1) == constm1_rtx)
2943 || (GET_CODE (XEXP (op1, 0)) == MINUS
2944 && XEXP (XEXP (op1, 0), 1) == const1_rtx))
2945 && rtx_equal_p (XEXP (op1, 1), op0))
2946 rhs = XEXP (XEXP (op1, 0), 0);
2947 else if (((GET_CODE (XEXP (op1, 1)) == PLUS
2948 && XEXP (XEXP (op1, 1), 1) == constm1_rtx)
2949 || (GET_CODE (XEXP (op1, 1)) == MINUS
2950 && XEXP (XEXP (op1, 1), 1) == const1_rtx))
2951 && rtx_equal_p (XEXP (op1, 0), op0))
2952 rhs = XEXP (XEXP (op1, 1), 0);
2954 if (lhs != op0 || rhs != op1)
2955 return simplify_gen_binary (MULT, int_mode, lhs, rhs);
2958 /* (plus (xor X C1) C2) is (xor X (C1^C2)) if C2 is signbit. */
2959 if (CONST_SCALAR_INT_P (op1)
2960 && GET_CODE (op0) == XOR
2961 && CONST_SCALAR_INT_P (XEXP (op0, 1))
2962 && mode_signbit_p (mode, op1))
2963 return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
2964 simplify_gen_binary (XOR, mode, op1,
2965 XEXP (op0, 1)));
2967 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)). */
2968 if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2969 && GET_CODE (op0) == MULT
2970 && GET_CODE (XEXP (op0, 0)) == NEG)
2972 rtx in1, in2;
2974 in1 = XEXP (XEXP (op0, 0), 0);
2975 in2 = XEXP (op0, 1);
2976 return simplify_gen_binary (MINUS, mode, op1,
2977 simplify_gen_binary (MULT, mode,
2978 in1, in2));
2981 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
2982 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
2983 is 1. */
2984 if (COMPARISON_P (op0)
2985 && ((STORE_FLAG_VALUE == -1 && trueop1 == const1_rtx)
2986 || (STORE_FLAG_VALUE == 1 && trueop1 == constm1_rtx))
2987 && (reversed = reversed_comparison (op0, mode)))
2988 return
2989 simplify_gen_unary (NEG, mode, reversed, mode);
2991 /* If one of the operands is a PLUS or a MINUS, see if we can
2992 simplify this by the associative law.
2993 Don't use the associative law for floating point.
2994 The inaccuracy makes it nonassociative,
2995 and subtle programs can break if operations are associated. */
2997 if (INTEGRAL_MODE_P (mode)
2998 && (plus_minus_operand_p (op0)
2999 || plus_minus_operand_p (op1))
3000 && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3001 return tem;
3003 /* Reassociate floating point addition only when the user
3004 specifies associative math operations. */
3005 if (FLOAT_MODE_P (mode)
3006 && flag_associative_math)
3008 tem = simplify_associative_operation (code, mode, op0, op1);
3009 if (tem)
3010 return tem;
3013 /* Handle vector series. */
3014 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3016 tem = simplify_binary_operation_series (code, mode, op0, op1);
3017 if (tem)
3018 return tem;
3020 break;
3022 case COMPARE:
3023 /* Convert (compare (gt (flags) 0) (lt (flags) 0)) to (flags). */
3024 if (((GET_CODE (op0) == GT && GET_CODE (op1) == LT)
3025 || (GET_CODE (op0) == GTU && GET_CODE (op1) == LTU))
3026 && XEXP (op0, 1) == const0_rtx && XEXP (op1, 1) == const0_rtx)
3028 rtx xop00 = XEXP (op0, 0);
3029 rtx xop10 = XEXP (op1, 0);
3031 if (REG_P (xop00) && REG_P (xop10)
3032 && REGNO (xop00) == REGNO (xop10)
3033 && GET_MODE (xop00) == mode
3034 && GET_MODE (xop10) == mode
3035 && GET_MODE_CLASS (mode) == MODE_CC)
3036 return xop00;
3038 break;
3040 case MINUS:
3041 /* We can't assume x-x is 0 even with non-IEEE floating point,
3042 but since it is zero except in very strange circumstances, we
3043 will treat it as zero with -ffinite-math-only. */
3044 if (rtx_equal_p (trueop0, trueop1)
3045 && ! side_effects_p (op0)
3046 && (!FLOAT_MODE_P (mode) || !HONOR_NANS (mode)))
3047 return CONST0_RTX (mode);
3049 /* Change subtraction from zero into negation. (0 - x) is the
3050 same as -x when x is NaN, infinite, or finite and nonzero.
3051 But if the mode has signed zeros, and does not round towards
3052 -infinity, then 0 - 0 is 0, not -0. */
3053 if (!HONOR_SIGNED_ZEROS (mode) && trueop0 == CONST0_RTX (mode))
3054 return simplify_gen_unary (NEG, mode, op1, mode);
3056 /* (-1 - a) is ~a, unless the expression contains symbolic
3057 constants, in which case not retaining additions and
3058 subtractions could cause invalid assembly to be produced. */
3059 if (trueop0 == constm1_rtx
3060 && !contains_symbolic_reference_p (op1))
3061 return simplify_gen_unary (NOT, mode, op1, mode);
3063 /* Subtracting 0 has no effect unless the mode has signalling NaNs,
3064 or has signed zeros and supports rounding towards -infinity.
3065 In such a case, 0 - 0 is -0. */
3066 if (!(HONOR_SIGNED_ZEROS (mode)
3067 && HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3068 && !HONOR_SNANS (mode)
3069 && trueop1 == CONST0_RTX (mode))
3070 return op0;
3072 /* See if this is something like X * C - X or vice versa or
3073 if the multiplication is written as a shift. If so, we can
3074 distribute and make a new multiply, shift, or maybe just
3075 have X (if C is 2 in the example above). But don't make
3076 something more expensive than we had before. */
3078 if (is_a <scalar_int_mode> (mode, &int_mode))
3080 rtx lhs = op0, rhs = op1;
3082 wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3083 wide_int negcoeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3085 if (GET_CODE (lhs) == NEG)
3087 coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3088 lhs = XEXP (lhs, 0);
3090 else if (GET_CODE (lhs) == MULT
3091 && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3093 coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3094 lhs = XEXP (lhs, 0);
3096 else if (GET_CODE (lhs) == ASHIFT
3097 && CONST_INT_P (XEXP (lhs, 1))
3098 && INTVAL (XEXP (lhs, 1)) >= 0
3099 && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3101 coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3102 GET_MODE_PRECISION (int_mode));
3103 lhs = XEXP (lhs, 0);
3106 if (GET_CODE (rhs) == NEG)
3108 negcoeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3109 rhs = XEXP (rhs, 0);
3111 else if (GET_CODE (rhs) == MULT
3112 && CONST_INT_P (XEXP (rhs, 1)))
3114 negcoeff1 = wi::neg (rtx_mode_t (XEXP (rhs, 1), int_mode));
3115 rhs = XEXP (rhs, 0);
3117 else if (GET_CODE (rhs) == ASHIFT
3118 && CONST_INT_P (XEXP (rhs, 1))
3119 && INTVAL (XEXP (rhs, 1)) >= 0
3120 && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3122 negcoeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3123 GET_MODE_PRECISION (int_mode));
3124 negcoeff1 = -negcoeff1;
3125 rhs = XEXP (rhs, 0);
3128 if (rtx_equal_p (lhs, rhs))
3130 rtx orig = gen_rtx_MINUS (int_mode, op0, op1);
3131 rtx coeff;
3132 bool speed = optimize_function_for_speed_p (cfun);
3134 coeff = immed_wide_int_const (coeff0 + negcoeff1, int_mode);
3136 tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3137 return (set_src_cost (tem, int_mode, speed)
3138 <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3141 /* Optimize (X + 1) * Y - Y to X * Y. */
3142 lhs = op0;
3143 if (GET_CODE (op0) == MULT)
3145 if (((GET_CODE (XEXP (op0, 0)) == PLUS
3146 && XEXP (XEXP (op0, 0), 1) == const1_rtx)
3147 || (GET_CODE (XEXP (op0, 0)) == MINUS
3148 && XEXP (XEXP (op0, 0), 1) == constm1_rtx))
3149 && rtx_equal_p (XEXP (op0, 1), op1))
3150 lhs = XEXP (XEXP (op0, 0), 0);
3151 else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3152 && XEXP (XEXP (op0, 1), 1) == const1_rtx)
3153 || (GET_CODE (XEXP (op0, 1)) == MINUS
3154 && XEXP (XEXP (op0, 1), 1) == constm1_rtx))
3155 && rtx_equal_p (XEXP (op0, 0), op1))
3156 lhs = XEXP (XEXP (op0, 1), 0);
3158 if (lhs != op0)
3159 return simplify_gen_binary (MULT, int_mode, lhs, op1);
3162 /* (a - (-b)) -> (a + b). True even for IEEE. */
3163 if (GET_CODE (op1) == NEG)
3164 return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
3166 /* (-x - c) may be simplified as (-c - x). */
3167 if (GET_CODE (op0) == NEG
3168 && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1)))
3170 tem = simplify_unary_operation (NEG, mode, op1, mode);
3171 if (tem)
3172 return simplify_gen_binary (MINUS, mode, tem, XEXP (op0, 0));
3175 if ((GET_CODE (op0) == CONST
3176 || GET_CODE (op0) == SYMBOL_REF
3177 || GET_CODE (op0) == LABEL_REF)
3178 && poly_int_rtx_p (op1, &offset))
3179 return plus_constant (mode, op0, trunc_int_for_mode (-offset, mode));
3181 /* Don't let a relocatable value get a negative coeff. */
3182 if (poly_int_rtx_p (op1) && GET_MODE (op0) != VOIDmode)
3183 return simplify_gen_binary (PLUS, mode,
3184 op0,
3185 neg_poly_int_rtx (mode, op1));
3187 /* (x - (x & y)) -> (x & ~y) */
3188 if (INTEGRAL_MODE_P (mode) && GET_CODE (op1) == AND)
3190 if (rtx_equal_p (op0, XEXP (op1, 0)))
3192 tem = simplify_gen_unary (NOT, mode, XEXP (op1, 1),
3193 GET_MODE (XEXP (op1, 1)));
3194 return simplify_gen_binary (AND, mode, op0, tem);
3196 if (rtx_equal_p (op0, XEXP (op1, 1)))
3198 tem = simplify_gen_unary (NOT, mode, XEXP (op1, 0),
3199 GET_MODE (XEXP (op1, 0)));
3200 return simplify_gen_binary (AND, mode, op0, tem);
3204 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3205 by reversing the comparison code if valid. */
3206 if (STORE_FLAG_VALUE == 1
3207 && trueop0 == const1_rtx
3208 && COMPARISON_P (op1)
3209 && (reversed = reversed_comparison (op1, mode)))
3210 return reversed;
3212 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A). */
3213 if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3214 && GET_CODE (op1) == MULT
3215 && GET_CODE (XEXP (op1, 0)) == NEG)
3217 rtx in1, in2;
3219 in1 = XEXP (XEXP (op1, 0), 0);
3220 in2 = XEXP (op1, 1);
3221 return simplify_gen_binary (PLUS, mode,
3222 simplify_gen_binary (MULT, mode,
3223 in1, in2),
3224 op0);
3227 /* Canonicalize (minus (neg A) (mult B C)) to
3228 (minus (mult (neg B) C) A). */
3229 if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3230 && GET_CODE (op1) == MULT
3231 && GET_CODE (op0) == NEG)
3233 rtx in1, in2;
3235 in1 = simplify_gen_unary (NEG, mode, XEXP (op1, 0), mode);
3236 in2 = XEXP (op1, 1);
3237 return simplify_gen_binary (MINUS, mode,
3238 simplify_gen_binary (MULT, mode,
3239 in1, in2),
3240 XEXP (op0, 0));
3243 /* If one of the operands is a PLUS or a MINUS, see if we can
3244 simplify this by the associative law. This will, for example,
3245 canonicalize (minus A (plus B C)) to (minus (minus A B) C).
3246 Don't use the associative law for floating point.
3247 The inaccuracy makes it nonassociative,
3248 and subtle programs can break if operations are associated. */
3250 if (INTEGRAL_MODE_P (mode)
3251 && (plus_minus_operand_p (op0)
3252 || plus_minus_operand_p (op1))
3253 && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3254 return tem;
3256 /* Handle vector series. */
3257 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3259 tem = simplify_binary_operation_series (code, mode, op0, op1);
3260 if (tem)
3261 return tem;
3263 break;
3265 case MULT:
3266 if (trueop1 == constm1_rtx)
3267 return simplify_gen_unary (NEG, mode, op0, mode);
3269 if (GET_CODE (op0) == NEG)
3271 rtx temp = simplify_unary_operation (NEG, mode, op1, mode);
3272 /* If op1 is a MULT as well and simplify_unary_operation
3273 just moved the NEG to the second operand, simplify_gen_binary
3274 below could through simplify_associative_operation move
3275 the NEG around again and recurse endlessly. */
3276 if (temp
3277 && GET_CODE (op1) == MULT
3278 && GET_CODE (temp) == MULT
3279 && XEXP (op1, 0) == XEXP (temp, 0)
3280 && GET_CODE (XEXP (temp, 1)) == NEG
3281 && XEXP (op1, 1) == XEXP (XEXP (temp, 1), 0))
3282 temp = NULL_RTX;
3283 if (temp)
3284 return simplify_gen_binary (MULT, mode, XEXP (op0, 0), temp);
3286 if (GET_CODE (op1) == NEG)
3288 rtx temp = simplify_unary_operation (NEG, mode, op0, mode);
3289 /* If op0 is a MULT as well and simplify_unary_operation
3290 just moved the NEG to the second operand, simplify_gen_binary
3291 below could through simplify_associative_operation move
3292 the NEG around again and recurse endlessly. */
3293 if (temp
3294 && GET_CODE (op0) == MULT
3295 && GET_CODE (temp) == MULT
3296 && XEXP (op0, 0) == XEXP (temp, 0)
3297 && GET_CODE (XEXP (temp, 1)) == NEG
3298 && XEXP (op0, 1) == XEXP (XEXP (temp, 1), 0))
3299 temp = NULL_RTX;
3300 if (temp)
3301 return simplify_gen_binary (MULT, mode, temp, XEXP (op1, 0));
3304 /* Maybe simplify x * 0 to 0. The reduction is not valid if
3305 x is NaN, since x * 0 is then also NaN. Nor is it valid
3306 when the mode has signed zeros, since multiplying a negative
3307 number by 0 will give -0, not 0. */
3308 if (!HONOR_NANS (mode)
3309 && !HONOR_SIGNED_ZEROS (mode)
3310 && trueop1 == CONST0_RTX (mode)
3311 && ! side_effects_p (op0))
3312 return op1;
3314 /* In IEEE floating point, x*1 is not equivalent to x for
3315 signalling NaNs. */
3316 if (!HONOR_SNANS (mode)
3317 && trueop1 == CONST1_RTX (mode))
3318 return op0;
3320 /* Convert multiply by constant power of two into shift. */
3321 if (mem_depth == 0 && CONST_SCALAR_INT_P (trueop1))
3323 val = wi::exact_log2 (rtx_mode_t (trueop1, mode));
3324 if (val >= 0)
3325 return simplify_gen_binary (ASHIFT, mode, op0,
3326 gen_int_shift_amount (mode, val));
3329 /* x*2 is x+x and x*(-1) is -x */
3330 if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
3331 && SCALAR_FLOAT_MODE_P (GET_MODE (trueop1))
3332 && !DECIMAL_FLOAT_MODE_P (GET_MODE (trueop1))
3333 && GET_MODE (op0) == mode)
3335 const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
3337 if (real_equal (d1, &dconst2))
3338 return simplify_gen_binary (PLUS, mode, op0, copy_rtx (op0));
3340 if (!HONOR_SNANS (mode)
3341 && real_equal (d1, &dconstm1))
3342 return simplify_gen_unary (NEG, mode, op0, mode);
3345 /* Optimize -x * -x as x * x. */
3346 if (FLOAT_MODE_P (mode)
3347 && GET_CODE (op0) == NEG
3348 && GET_CODE (op1) == NEG
3349 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3350 && !side_effects_p (XEXP (op0, 0)))
3351 return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3353 /* Likewise, optimize abs(x) * abs(x) as x * x. */
3354 if (SCALAR_FLOAT_MODE_P (mode)
3355 && GET_CODE (op0) == ABS
3356 && GET_CODE (op1) == ABS
3357 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3358 && !side_effects_p (XEXP (op0, 0)))
3359 return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3361 /* Reassociate multiplication, but for floating point MULTs
3362 only when the user specifies unsafe math optimizations. */
3363 if (! FLOAT_MODE_P (mode)
3364 || flag_unsafe_math_optimizations)
3366 tem = simplify_associative_operation (code, mode, op0, op1);
3367 if (tem)
3368 return tem;
3370 break;
3372 case IOR:
3373 if (trueop1 == CONST0_RTX (mode))
3374 return op0;
3375 if (INTEGRAL_MODE_P (mode)
3376 && trueop1 == CONSTM1_RTX (mode)
3377 && !side_effects_p (op0))
3378 return op1;
3379 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3380 return op0;
3381 /* A | (~A) -> -1 */
3382 if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
3383 || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
3384 && ! side_effects_p (op0)
3385 && GET_MODE_CLASS (mode) != MODE_CC)
3386 return CONSTM1_RTX (mode);
3388 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
3389 if (CONST_INT_P (op1)
3390 && HWI_COMPUTABLE_MODE_P (mode)
3391 && (nonzero_bits (op0, mode) & ~UINTVAL (op1)) == 0
3392 && !side_effects_p (op0))
3393 return op1;
3395 /* Canonicalize (X & C1) | C2. */
3396 if (GET_CODE (op0) == AND
3397 && CONST_INT_P (trueop1)
3398 && CONST_INT_P (XEXP (op0, 1)))
3400 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
3401 HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
3402 HOST_WIDE_INT c2 = INTVAL (trueop1);
3404 /* If (C1&C2) == C1, then (X&C1)|C2 becomes C2. */
3405 if ((c1 & c2) == c1
3406 && !side_effects_p (XEXP (op0, 0)))
3407 return trueop1;
3409 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
3410 if (((c1|c2) & mask) == mask)
3411 return simplify_gen_binary (IOR, mode, XEXP (op0, 0), op1);
3414 /* Convert (A & B) | A to A. */
3415 if (GET_CODE (op0) == AND
3416 && (rtx_equal_p (XEXP (op0, 0), op1)
3417 || rtx_equal_p (XEXP (op0, 1), op1))
3418 && ! side_effects_p (XEXP (op0, 0))
3419 && ! side_effects_p (XEXP (op0, 1)))
3420 return op1;
3422 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3423 mode size to (rotate A CX). */
3425 if (GET_CODE (op1) == ASHIFT
3426 || GET_CODE (op1) == SUBREG)
3428 opleft = op1;
3429 opright = op0;
3431 else
3433 opright = op1;
3434 opleft = op0;
3437 if (GET_CODE (opleft) == ASHIFT && GET_CODE (opright) == LSHIFTRT
3438 && rtx_equal_p (XEXP (opleft, 0), XEXP (opright, 0))
3439 && CONST_INT_P (XEXP (opleft, 1))
3440 && CONST_INT_P (XEXP (opright, 1))
3441 && (INTVAL (XEXP (opleft, 1)) + INTVAL (XEXP (opright, 1))
3442 == GET_MODE_UNIT_PRECISION (mode)))
3443 return gen_rtx_ROTATE (mode, XEXP (opright, 0), XEXP (opleft, 1));
3445 /* Same, but for ashift that has been "simplified" to a wider mode
3446 by simplify_shift_const. */
3448 if (GET_CODE (opleft) == SUBREG
3449 && is_a <scalar_int_mode> (mode, &int_mode)
3450 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (opleft)),
3451 &inner_mode)
3452 && GET_CODE (SUBREG_REG (opleft)) == ASHIFT
3453 && GET_CODE (opright) == LSHIFTRT
3454 && GET_CODE (XEXP (opright, 0)) == SUBREG
3455 && known_eq (SUBREG_BYTE (opleft), SUBREG_BYTE (XEXP (opright, 0)))
3456 && GET_MODE_SIZE (int_mode) < GET_MODE_SIZE (inner_mode)
3457 && rtx_equal_p (XEXP (SUBREG_REG (opleft), 0),
3458 SUBREG_REG (XEXP (opright, 0)))
3459 && CONST_INT_P (XEXP (SUBREG_REG (opleft), 1))
3460 && CONST_INT_P (XEXP (opright, 1))
3461 && (INTVAL (XEXP (SUBREG_REG (opleft), 1))
3462 + INTVAL (XEXP (opright, 1))
3463 == GET_MODE_PRECISION (int_mode)))
3464 return gen_rtx_ROTATE (int_mode, XEXP (opright, 0),
3465 XEXP (SUBREG_REG (opleft), 1));
3467 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
3468 a (sign_extend (plus ...)). Then check if OP1 is a CONST_INT and
3469 the PLUS does not affect any of the bits in OP1: then we can do
3470 the IOR as a PLUS and we can associate. This is valid if OP1
3471 can be safely shifted left C bits. */
3472 if (CONST_INT_P (trueop1) && GET_CODE (op0) == ASHIFTRT
3473 && GET_CODE (XEXP (op0, 0)) == PLUS
3474 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
3475 && CONST_INT_P (XEXP (op0, 1))
3476 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
3478 int count = INTVAL (XEXP (op0, 1));
3479 HOST_WIDE_INT mask = UINTVAL (trueop1) << count;
3481 if (mask >> count == INTVAL (trueop1)
3482 && trunc_int_for_mode (mask, mode) == mask
3483 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
3484 return simplify_gen_binary (ASHIFTRT, mode,
3485 plus_constant (mode, XEXP (op0, 0),
3486 mask),
3487 XEXP (op0, 1));
3490 /* The following happens with bitfield merging.
3491 (X & C) | ((X | Y) & ~C) -> X | (Y & ~C) */
3492 if (GET_CODE (op0) == AND
3493 && GET_CODE (op1) == AND
3494 && CONST_INT_P (XEXP (op0, 1))
3495 && CONST_INT_P (XEXP (op1, 1))
3496 && (INTVAL (XEXP (op0, 1))
3497 == ~INTVAL (XEXP (op1, 1))))
3499 /* The IOR may be on both sides. */
3500 rtx top0 = NULL_RTX, top1 = NULL_RTX;
3501 if (GET_CODE (XEXP (op1, 0)) == IOR)
3502 top0 = op0, top1 = op1;
3503 else if (GET_CODE (XEXP (op0, 0)) == IOR)
3504 top0 = op1, top1 = op0;
3505 if (top0 && top1)
3507 /* X may be on either side of the inner IOR. */
3508 rtx tem = NULL_RTX;
3509 if (rtx_equal_p (XEXP (top0, 0),
3510 XEXP (XEXP (top1, 0), 0)))
3511 tem = XEXP (XEXP (top1, 0), 1);
3512 else if (rtx_equal_p (XEXP (top0, 0),
3513 XEXP (XEXP (top1, 0), 1)))
3514 tem = XEXP (XEXP (top1, 0), 0);
3515 if (tem)
3516 return simplify_gen_binary (IOR, mode, XEXP (top0, 0),
3517 simplify_gen_binary
3518 (AND, mode, tem, XEXP (top1, 1)));
3522 /* Convert (ior (and A C) (and B C)) into (and (ior A B) C). */
3523 if (GET_CODE (op0) == GET_CODE (op1)
3524 && (GET_CODE (op0) == AND
3525 || GET_CODE (op0) == IOR
3526 || GET_CODE (op0) == LSHIFTRT
3527 || GET_CODE (op0) == ASHIFTRT
3528 || GET_CODE (op0) == ASHIFT
3529 || GET_CODE (op0) == ROTATE
3530 || GET_CODE (op0) == ROTATERT))
3532 tem = simplify_distributive_operation (code, mode, op0, op1);
3533 if (tem)
3534 return tem;
3537 tem = simplify_byte_swapping_operation (code, mode, op0, op1);
3538 if (tem)
3539 return tem;
3541 tem = simplify_associative_operation (code, mode, op0, op1);
3542 if (tem)
3543 return tem;
3545 tem = simplify_logical_relational_operation (code, mode, op0, op1);
3546 if (tem)
3547 return tem;
3548 break;
3550 case XOR:
3551 if (trueop1 == CONST0_RTX (mode))
3552 return op0;
3553 if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
3554 return simplify_gen_unary (NOT, mode, op0, mode);
3555 if (rtx_equal_p (trueop0, trueop1)
3556 && ! side_effects_p (op0)
3557 && GET_MODE_CLASS (mode) != MODE_CC)
3558 return CONST0_RTX (mode);
3560 /* Canonicalize XOR of the most significant bit to PLUS. */
3561 if (CONST_SCALAR_INT_P (op1)
3562 && mode_signbit_p (mode, op1))
3563 return simplify_gen_binary (PLUS, mode, op0, op1);
3564 /* (xor (plus X C1) C2) is (xor X (C1^C2)) if C1 is signbit. */
3565 if (CONST_SCALAR_INT_P (op1)
3566 && GET_CODE (op0) == PLUS
3567 && CONST_SCALAR_INT_P (XEXP (op0, 1))
3568 && mode_signbit_p (mode, XEXP (op0, 1)))
3569 return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3570 simplify_gen_binary (XOR, mode, op1,
3571 XEXP (op0, 1)));
3573 /* If we are XORing two things that have no bits in common,
3574 convert them into an IOR. This helps to detect rotation encoded
3575 using those methods and possibly other simplifications. */
3577 if (HWI_COMPUTABLE_MODE_P (mode)
3578 && (nonzero_bits (op0, mode)
3579 & nonzero_bits (op1, mode)) == 0)
3580 return (simplify_gen_binary (IOR, mode, op0, op1));
3582 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
3583 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
3584 (NOT y). */
3586 int num_negated = 0;
3588 if (GET_CODE (op0) == NOT)
3589 num_negated++, op0 = XEXP (op0, 0);
3590 if (GET_CODE (op1) == NOT)
3591 num_negated++, op1 = XEXP (op1, 0);
3593 if (num_negated == 2)
3594 return simplify_gen_binary (XOR, mode, op0, op1);
3595 else if (num_negated == 1)
3596 return simplify_gen_unary (NOT, mode,
3597 simplify_gen_binary (XOR, mode, op0, op1),
3598 mode);
3601 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
3602 correspond to a machine insn or result in further simplifications
3603 if B is a constant. */
3605 if (GET_CODE (op0) == AND
3606 && rtx_equal_p (XEXP (op0, 1), op1)
3607 && ! side_effects_p (op1))
3608 return simplify_gen_binary (AND, mode,
3609 simplify_gen_unary (NOT, mode,
3610 XEXP (op0, 0), mode),
3611 op1);
3613 else if (GET_CODE (op0) == AND
3614 && rtx_equal_p (XEXP (op0, 0), op1)
3615 && ! side_effects_p (op1))
3616 return simplify_gen_binary (AND, mode,
3617 simplify_gen_unary (NOT, mode,
3618 XEXP (op0, 1), mode),
3619 op1);
3621 /* Given (xor (ior (xor A B) C) D), where B, C and D are
3622 constants, simplify to (xor (ior A C) (B&~C)^D), canceling
3623 out bits inverted twice and not set by C. Similarly, given
3624 (xor (and (xor A B) C) D), simplify without inverting C in
3625 the xor operand: (xor (and A C) (B&C)^D).
3627 else if ((GET_CODE (op0) == IOR || GET_CODE (op0) == AND)
3628 && GET_CODE (XEXP (op0, 0)) == XOR
3629 && CONST_INT_P (op1)
3630 && CONST_INT_P (XEXP (op0, 1))
3631 && CONST_INT_P (XEXP (XEXP (op0, 0), 1)))
3633 enum rtx_code op = GET_CODE (op0);
3634 rtx a = XEXP (XEXP (op0, 0), 0);
3635 rtx b = XEXP (XEXP (op0, 0), 1);
3636 rtx c = XEXP (op0, 1);
3637 rtx d = op1;
3638 HOST_WIDE_INT bval = INTVAL (b);
3639 HOST_WIDE_INT cval = INTVAL (c);
3640 HOST_WIDE_INT dval = INTVAL (d);
3641 HOST_WIDE_INT xcval;
3643 if (op == IOR)
3644 xcval = ~cval;
3645 else
3646 xcval = cval;
3648 return simplify_gen_binary (XOR, mode,
3649 simplify_gen_binary (op, mode, a, c),
3650 gen_int_mode ((bval & xcval) ^ dval,
3651 mode));
3654 /* Given (xor (and A B) C), using P^Q == (~P&Q) | (~Q&P),
3655 we can transform like this:
3656 (A&B)^C == ~(A&B)&C | ~C&(A&B)
3657 == (~A|~B)&C | ~C&(A&B) * DeMorgan's Law
3658 == ~A&C | ~B&C | A&(~C&B) * Distribute and re-order
3659 Attempt a few simplifications when B and C are both constants. */
3660 if (GET_CODE (op0) == AND
3661 && CONST_INT_P (op1)
3662 && CONST_INT_P (XEXP (op0, 1)))
3664 rtx a = XEXP (op0, 0);
3665 rtx b = XEXP (op0, 1);
3666 rtx c = op1;
3667 HOST_WIDE_INT bval = INTVAL (b);
3668 HOST_WIDE_INT cval = INTVAL (c);
3670 /* Instead of computing ~A&C, we compute its negated value,
3671 ~(A|~C). If it yields -1, ~A&C is zero, so we can
3672 optimize for sure. If it does not simplify, we still try
3673 to compute ~A&C below, but since that always allocates
3674 RTL, we don't try that before committing to returning a
3675 simplified expression. */
3676 rtx n_na_c = simplify_binary_operation (IOR, mode, a,
3677 GEN_INT (~cval));
3679 if ((~cval & bval) == 0)
3681 rtx na_c = NULL_RTX;
3682 if (n_na_c)
3683 na_c = simplify_gen_unary (NOT, mode, n_na_c, mode);
3684 else
3686 /* If ~A does not simplify, don't bother: we don't
3687 want to simplify 2 operations into 3, and if na_c
3688 were to simplify with na, n_na_c would have
3689 simplified as well. */
3690 rtx na = simplify_unary_operation (NOT, mode, a, mode);
3691 if (na)
3692 na_c = simplify_gen_binary (AND, mode, na, c);
3695 /* Try to simplify ~A&C | ~B&C. */
3696 if (na_c != NULL_RTX)
3697 return simplify_gen_binary (IOR, mode, na_c,
3698 gen_int_mode (~bval & cval, mode));
3700 else
3702 /* If ~A&C is zero, simplify A&(~C&B) | ~B&C. */
3703 if (n_na_c == CONSTM1_RTX (mode))
3705 rtx a_nc_b = simplify_gen_binary (AND, mode, a,
3706 gen_int_mode (~cval & bval,
3707 mode));
3708 return simplify_gen_binary (IOR, mode, a_nc_b,
3709 gen_int_mode (~bval & cval,
3710 mode));
3715 /* If we have (xor (and (xor A B) C) A) with C a constant we can instead
3716 do (ior (and A ~C) (and B C)) which is a machine instruction on some
3717 machines, and also has shorter instruction path length. */
3718 if (GET_CODE (op0) == AND
3719 && GET_CODE (XEXP (op0, 0)) == XOR
3720 && CONST_INT_P (XEXP (op0, 1))
3721 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), trueop1))
3723 rtx a = trueop1;
3724 rtx b = XEXP (XEXP (op0, 0), 1);
3725 rtx c = XEXP (op0, 1);
3726 rtx nc = simplify_gen_unary (NOT, mode, c, mode);
3727 rtx a_nc = simplify_gen_binary (AND, mode, a, nc);
3728 rtx bc = simplify_gen_binary (AND, mode, b, c);
3729 return simplify_gen_binary (IOR, mode, a_nc, bc);
3731 /* Similarly, (xor (and (xor A B) C) B) as (ior (and A C) (and B ~C)) */
3732 else if (GET_CODE (op0) == AND
3733 && GET_CODE (XEXP (op0, 0)) == XOR
3734 && CONST_INT_P (XEXP (op0, 1))
3735 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), trueop1))
3737 rtx a = XEXP (XEXP (op0, 0), 0);
3738 rtx b = trueop1;
3739 rtx c = XEXP (op0, 1);
3740 rtx nc = simplify_gen_unary (NOT, mode, c, mode);
3741 rtx b_nc = simplify_gen_binary (AND, mode, b, nc);
3742 rtx ac = simplify_gen_binary (AND, mode, a, c);
3743 return simplify_gen_binary (IOR, mode, ac, b_nc);
3746 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
3747 comparison if STORE_FLAG_VALUE is 1. */
3748 if (STORE_FLAG_VALUE == 1
3749 && trueop1 == const1_rtx
3750 && COMPARISON_P (op0)
3751 && (reversed = reversed_comparison (op0, mode)))
3752 return reversed;
3754 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
3755 is (lt foo (const_int 0)), so we can perform the above
3756 simplification if STORE_FLAG_VALUE is 1. */
3758 if (is_a <scalar_int_mode> (mode, &int_mode)
3759 && STORE_FLAG_VALUE == 1
3760 && trueop1 == const1_rtx
3761 && GET_CODE (op0) == LSHIFTRT
3762 && CONST_INT_P (XEXP (op0, 1))
3763 && INTVAL (XEXP (op0, 1)) == GET_MODE_PRECISION (int_mode) - 1)
3764 return gen_rtx_GE (int_mode, XEXP (op0, 0), const0_rtx);
3766 /* (xor (comparison foo bar) (const_int sign-bit))
3767 when STORE_FLAG_VALUE is the sign bit. */
3768 if (is_a <scalar_int_mode> (mode, &int_mode)
3769 && val_signbit_p (int_mode, STORE_FLAG_VALUE)
3770 && trueop1 == const_true_rtx
3771 && COMPARISON_P (op0)
3772 && (reversed = reversed_comparison (op0, int_mode)))
3773 return reversed;
3775 /* Convert (xor (and A C) (and B C)) into (and (xor A B) C). */
3776 if (GET_CODE (op0) == GET_CODE (op1)
3777 && (GET_CODE (op0) == AND
3778 || GET_CODE (op0) == LSHIFTRT
3779 || GET_CODE (op0) == ASHIFTRT
3780 || GET_CODE (op0) == ASHIFT
3781 || GET_CODE (op0) == ROTATE
3782 || GET_CODE (op0) == ROTATERT))
3784 tem = simplify_distributive_operation (code, mode, op0, op1);
3785 if (tem)
3786 return tem;
3789 tem = simplify_byte_swapping_operation (code, mode, op0, op1);
3790 if (tem)
3791 return tem;
3793 tem = simplify_associative_operation (code, mode, op0, op1);
3794 if (tem)
3795 return tem;
3796 break;
3798 case AND:
3799 if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
3800 return trueop1;
3801 if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
3802 return op0;
3803 if (HWI_COMPUTABLE_MODE_P (mode))
3805 /* When WORD_REGISTER_OPERATIONS is true, we need to know the
3806 nonzero bits in WORD_MODE rather than MODE. */
3807 scalar_int_mode tmode = as_a <scalar_int_mode> (mode);
3808 if (WORD_REGISTER_OPERATIONS
3809 && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3810 tmode = word_mode;
3811 HOST_WIDE_INT nzop0 = nonzero_bits (trueop0, tmode);
3812 HOST_WIDE_INT nzop1;
3813 if (CONST_INT_P (trueop1))
3815 HOST_WIDE_INT val1 = INTVAL (trueop1);
3816 /* If we are turning off bits already known off in OP0, we need
3817 not do an AND. */
3818 if ((nzop0 & ~val1) == 0)
3819 return op0;
3821 nzop1 = nonzero_bits (trueop1, mode);
3822 /* If we are clearing all the nonzero bits, the result is zero. */
3823 if ((nzop1 & nzop0) == 0
3824 && !side_effects_p (op0) && !side_effects_p (op1))
3825 return CONST0_RTX (mode);
3827 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0)
3828 && GET_MODE_CLASS (mode) != MODE_CC)
3829 return op0;
3830 /* A & (~A) -> 0 */
3831 if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
3832 || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
3833 && ! side_effects_p (op0)
3834 && GET_MODE_CLASS (mode) != MODE_CC)
3835 return CONST0_RTX (mode);
3837 /* Transform (and (extend X) C) into (zero_extend (and X C)) if
3838 there are no nonzero bits of C outside of X's mode. */
3839 if ((GET_CODE (op0) == SIGN_EXTEND
3840 || GET_CODE (op0) == ZERO_EXTEND)
3841 && CONST_SCALAR_INT_P (trueop1)
3842 && is_a <scalar_int_mode> (mode, &int_mode)
3843 && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
3844 && (wi::mask (GET_MODE_PRECISION (inner_mode), true,
3845 GET_MODE_PRECISION (int_mode))
3846 & rtx_mode_t (trueop1, mode)) == 0)
3848 machine_mode imode = GET_MODE (XEXP (op0, 0));
3849 tem = immed_wide_int_const (rtx_mode_t (trueop1, mode), imode);
3850 tem = simplify_gen_binary (AND, imode, XEXP (op0, 0), tem);
3851 return simplify_gen_unary (ZERO_EXTEND, mode, tem, imode);
3854 /* Transform (and (truncate X) C) into (truncate (and X C)). This way
3855 we might be able to further simplify the AND with X and potentially
3856 remove the truncation altogether. */
3857 if (GET_CODE (op0) == TRUNCATE && CONST_INT_P (trueop1))
3859 rtx x = XEXP (op0, 0);
3860 machine_mode xmode = GET_MODE (x);
3861 tem = simplify_gen_binary (AND, xmode, x,
3862 gen_int_mode (INTVAL (trueop1), xmode));
3863 return simplify_gen_unary (TRUNCATE, mode, tem, xmode);
3866 /* Canonicalize (A | C1) & C2 as (A & C2) | (C1 & C2). */
3867 if (GET_CODE (op0) == IOR
3868 && CONST_INT_P (trueop1)
3869 && CONST_INT_P (XEXP (op0, 1)))
3871 HOST_WIDE_INT tmp = INTVAL (trueop1) & INTVAL (XEXP (op0, 1));
3872 return simplify_gen_binary (IOR, mode,
3873 simplify_gen_binary (AND, mode,
3874 XEXP (op0, 0), op1),
3875 gen_int_mode (tmp, mode));
3878 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
3879 insn (and may simplify more). */
3880 if (GET_CODE (op0) == XOR
3881 && rtx_equal_p (XEXP (op0, 0), op1)
3882 && ! side_effects_p (op1))
3883 return simplify_gen_binary (AND, mode,
3884 simplify_gen_unary (NOT, mode,
3885 XEXP (op0, 1), mode),
3886 op1);
3888 if (GET_CODE (op0) == XOR
3889 && rtx_equal_p (XEXP (op0, 1), op1)
3890 && ! side_effects_p (op1))
3891 return simplify_gen_binary (AND, mode,
3892 simplify_gen_unary (NOT, mode,
3893 XEXP (op0, 0), mode),
3894 op1);
3896 /* Similarly for (~(A ^ B)) & A. */
3897 if (GET_CODE (op0) == NOT
3898 && GET_CODE (XEXP (op0, 0)) == XOR
3899 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
3900 && ! side_effects_p (op1))
3901 return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
3903 if (GET_CODE (op0) == NOT
3904 && GET_CODE (XEXP (op0, 0)) == XOR
3905 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
3906 && ! side_effects_p (op1))
3907 return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
3909 /* Convert (A | B) & A to A. */
3910 if (GET_CODE (op0) == IOR
3911 && (rtx_equal_p (XEXP (op0, 0), op1)
3912 || rtx_equal_p (XEXP (op0, 1), op1))
3913 && ! side_effects_p (XEXP (op0, 0))
3914 && ! side_effects_p (XEXP (op0, 1)))
3915 return op1;
3917 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
3918 ((A & N) + B) & M -> (A + B) & M
3919 Similarly if (N & M) == 0,
3920 ((A | N) + B) & M -> (A + B) & M
3921 and for - instead of + and/or ^ instead of |.
3922 Also, if (N & M) == 0, then
3923 (A +- N) & M -> A & M. */
3924 if (CONST_INT_P (trueop1)
3925 && HWI_COMPUTABLE_MODE_P (mode)
3926 && ~UINTVAL (trueop1)
3927 && (UINTVAL (trueop1) & (UINTVAL (trueop1) + 1)) == 0
3928 && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS))
3930 rtx pmop[2];
3931 int which;
3933 pmop[0] = XEXP (op0, 0);
3934 pmop[1] = XEXP (op0, 1);
3936 if (CONST_INT_P (pmop[1])
3937 && (UINTVAL (pmop[1]) & UINTVAL (trueop1)) == 0)
3938 return simplify_gen_binary (AND, mode, pmop[0], op1);
3940 for (which = 0; which < 2; which++)
3942 tem = pmop[which];
3943 switch (GET_CODE (tem))
3945 case AND:
3946 if (CONST_INT_P (XEXP (tem, 1))
3947 && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1))
3948 == UINTVAL (trueop1))
3949 pmop[which] = XEXP (tem, 0);
3950 break;
3951 case IOR:
3952 case XOR:
3953 if (CONST_INT_P (XEXP (tem, 1))
3954 && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1)) == 0)
3955 pmop[which] = XEXP (tem, 0);
3956 break;
3957 default:
3958 break;
3962 if (pmop[0] != XEXP (op0, 0) || pmop[1] != XEXP (op0, 1))
3964 tem = simplify_gen_binary (GET_CODE (op0), mode,
3965 pmop[0], pmop[1]);
3966 return simplify_gen_binary (code, mode, tem, op1);
3970 /* (and X (ior (not X) Y) -> (and X Y) */
3971 if (GET_CODE (op1) == IOR
3972 && GET_CODE (XEXP (op1, 0)) == NOT
3973 && rtx_equal_p (op0, XEXP (XEXP (op1, 0), 0)))
3974 return simplify_gen_binary (AND, mode, op0, XEXP (op1, 1));
3976 /* (and (ior (not X) Y) X) -> (and X Y) */
3977 if (GET_CODE (op0) == IOR
3978 && GET_CODE (XEXP (op0, 0)) == NOT
3979 && rtx_equal_p (op1, XEXP (XEXP (op0, 0), 0)))
3980 return simplify_gen_binary (AND, mode, op1, XEXP (op0, 1));
3982 /* (and X (ior Y (not X)) -> (and X Y) */
3983 if (GET_CODE (op1) == IOR
3984 && GET_CODE (XEXP (op1, 1)) == NOT
3985 && rtx_equal_p (op0, XEXP (XEXP (op1, 1), 0)))
3986 return simplify_gen_binary (AND, mode, op0, XEXP (op1, 0));
3988 /* (and (ior Y (not X)) X) -> (and X Y) */
3989 if (GET_CODE (op0) == IOR
3990 && GET_CODE (XEXP (op0, 1)) == NOT
3991 && rtx_equal_p (op1, XEXP (XEXP (op0, 1), 0)))
3992 return simplify_gen_binary (AND, mode, op1, XEXP (op0, 0));
3994 /* Convert (and (ior A C) (ior B C)) into (ior (and A B) C). */
3995 if (GET_CODE (op0) == GET_CODE (op1)
3996 && (GET_CODE (op0) == AND
3997 || GET_CODE (op0) == IOR
3998 || GET_CODE (op0) == LSHIFTRT
3999 || GET_CODE (op0) == ASHIFTRT
4000 || GET_CODE (op0) == ASHIFT
4001 || GET_CODE (op0) == ROTATE
4002 || GET_CODE (op0) == ROTATERT))
4004 tem = simplify_distributive_operation (code, mode, op0, op1);
4005 if (tem)
4006 return tem;
4009 tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4010 if (tem)
4011 return tem;
4013 tem = simplify_associative_operation (code, mode, op0, op1);
4014 if (tem)
4015 return tem;
4016 break;
4018 case UDIV:
4019 /* 0/x is 0 (or x&0 if x has side-effects). */
4020 if (trueop0 == CONST0_RTX (mode)
4021 && !cfun->can_throw_non_call_exceptions)
4023 if (side_effects_p (op1))
4024 return simplify_gen_binary (AND, mode, op1, trueop0);
4025 return trueop0;
4027 /* x/1 is x. */
4028 if (trueop1 == CONST1_RTX (mode))
4030 tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4031 if (tem)
4032 return tem;
4034 /* Convert divide by power of two into shift. */
4035 if (CONST_INT_P (trueop1)
4036 && (val = exact_log2 (UINTVAL (trueop1))) > 0)
4037 return simplify_gen_binary (LSHIFTRT, mode, op0,
4038 gen_int_shift_amount (mode, val));
4039 break;
4041 case DIV:
4042 /* Handle floating point and integers separately. */
4043 if (SCALAR_FLOAT_MODE_P (mode))
4045 /* Maybe change 0.0 / x to 0.0. This transformation isn't
4046 safe for modes with NaNs, since 0.0 / 0.0 will then be
4047 NaN rather than 0.0. Nor is it safe for modes with signed
4048 zeros, since dividing 0 by a negative number gives -0.0 */
4049 if (trueop0 == CONST0_RTX (mode)
4050 && !HONOR_NANS (mode)
4051 && !HONOR_SIGNED_ZEROS (mode)
4052 && ! side_effects_p (op1))
4053 return op0;
4054 /* x/1.0 is x. */
4055 if (trueop1 == CONST1_RTX (mode)
4056 && !HONOR_SNANS (mode))
4057 return op0;
4059 if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
4060 && trueop1 != CONST0_RTX (mode))
4062 const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
4064 /* x/-1.0 is -x. */
4065 if (real_equal (d1, &dconstm1)
4066 && !HONOR_SNANS (mode))
4067 return simplify_gen_unary (NEG, mode, op0, mode);
4069 /* Change FP division by a constant into multiplication.
4070 Only do this with -freciprocal-math. */
4071 if (flag_reciprocal_math
4072 && !real_equal (d1, &dconst0))
4074 REAL_VALUE_TYPE d;
4075 real_arithmetic (&d, RDIV_EXPR, &dconst1, d1);
4076 tem = const_double_from_real_value (d, mode);
4077 return simplify_gen_binary (MULT, mode, op0, tem);
4081 else if (SCALAR_INT_MODE_P (mode))
4083 /* 0/x is 0 (or x&0 if x has side-effects). */
4084 if (trueop0 == CONST0_RTX (mode)
4085 && !cfun->can_throw_non_call_exceptions)
4087 if (side_effects_p (op1))
4088 return simplify_gen_binary (AND, mode, op1, trueop0);
4089 return trueop0;
4091 /* x/1 is x. */
4092 if (trueop1 == CONST1_RTX (mode))
4094 tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4095 if (tem)
4096 return tem;
4098 /* x/-1 is -x. */
4099 if (trueop1 == constm1_rtx)
4101 rtx x = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4102 if (x)
4103 return simplify_gen_unary (NEG, mode, x, mode);
4106 break;
4108 case UMOD:
4109 /* 0%x is 0 (or x&0 if x has side-effects). */
4110 if (trueop0 == CONST0_RTX (mode))
4112 if (side_effects_p (op1))
4113 return simplify_gen_binary (AND, mode, op1, trueop0);
4114 return trueop0;
4116 /* x%1 is 0 (of x&0 if x has side-effects). */
4117 if (trueop1 == CONST1_RTX (mode))
4119 if (side_effects_p (op0))
4120 return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4121 return CONST0_RTX (mode);
4123 /* Implement modulus by power of two as AND. */
4124 if (CONST_INT_P (trueop1)
4125 && exact_log2 (UINTVAL (trueop1)) > 0)
4126 return simplify_gen_binary (AND, mode, op0,
4127 gen_int_mode (UINTVAL (trueop1) - 1,
4128 mode));
4129 break;
4131 case MOD:
4132 /* 0%x is 0 (or x&0 if x has side-effects). */
4133 if (trueop0 == CONST0_RTX (mode))
4135 if (side_effects_p (op1))
4136 return simplify_gen_binary (AND, mode, op1, trueop0);
4137 return trueop0;
4139 /* x%1 and x%-1 is 0 (or x&0 if x has side-effects). */
4140 if (trueop1 == CONST1_RTX (mode) || trueop1 == constm1_rtx)
4142 if (side_effects_p (op0))
4143 return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4144 return CONST0_RTX (mode);
4146 break;
4148 case ROTATERT:
4149 case ROTATE:
4150 if (trueop1 == CONST0_RTX (mode))
4151 return op0;
4152 /* Canonicalize rotates by constant amount. If the condition of
4153 reversing direction is met, then reverse the direction. */
4154 #if defined(HAVE_rotate) && defined(HAVE_rotatert)
4155 if (reverse_rotate_by_imm_p (mode, (code == ROTATE), trueop1))
4157 int new_amount = GET_MODE_UNIT_PRECISION (mode) - INTVAL (trueop1);
4158 rtx new_amount_rtx = gen_int_shift_amount (mode, new_amount);
4159 return simplify_gen_binary (code == ROTATE ? ROTATERT : ROTATE,
4160 mode, op0, new_amount_rtx);
4162 #endif
4163 /* FALLTHRU */
4164 case ASHIFTRT:
4165 if (trueop1 == CONST0_RTX (mode))
4166 return op0;
4167 if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4168 return op0;
4169 /* Rotating ~0 always results in ~0. */
4170 if (CONST_INT_P (trueop0)
4171 && HWI_COMPUTABLE_MODE_P (mode)
4172 && UINTVAL (trueop0) == GET_MODE_MASK (mode)
4173 && ! side_effects_p (op1))
4174 return op0;
4176 canonicalize_shift:
4177 /* Given:
4178 scalar modes M1, M2
4179 scalar constants c1, c2
4180 size (M2) > size (M1)
4181 c1 == size (M2) - size (M1)
4182 optimize:
4183 ([a|l]shiftrt:M1 (subreg:M1 (lshiftrt:M2 (reg:M2) (const_int <c1>))
4184 <low_part>)
4185 (const_int <c2>))
4187 (subreg:M1 ([a|l]shiftrt:M2 (reg:M2) (const_int <c1 + c2>))
4188 <low_part>). */
4189 if ((code == ASHIFTRT || code == LSHIFTRT)
4190 && is_a <scalar_int_mode> (mode, &int_mode)
4191 && SUBREG_P (op0)
4192 && CONST_INT_P (op1)
4193 && GET_CODE (SUBREG_REG (op0)) == LSHIFTRT
4194 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
4195 &inner_mode)
4196 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1))
4197 && GET_MODE_BITSIZE (inner_mode) > GET_MODE_BITSIZE (int_mode)
4198 && (INTVAL (XEXP (SUBREG_REG (op0), 1))
4199 == GET_MODE_BITSIZE (inner_mode) - GET_MODE_BITSIZE (int_mode))
4200 && subreg_lowpart_p (op0))
4202 rtx tmp = gen_int_shift_amount
4203 (inner_mode, INTVAL (XEXP (SUBREG_REG (op0), 1)) + INTVAL (op1));
4205 /* Combine would usually zero out the value when combining two
4206 local shifts and the range becomes larger or equal to the mode.
4207 However since we fold away one of the shifts here combine won't
4208 see it so we should immediately zero the result if it's out of
4209 range. */
4210 if (code == LSHIFTRT
4211 && INTVAL (tmp) >= GET_MODE_BITSIZE (inner_mode))
4212 tmp = const0_rtx;
4213 else
4214 tmp = simplify_gen_binary (code,
4215 inner_mode,
4216 XEXP (SUBREG_REG (op0), 0),
4217 tmp);
4219 return lowpart_subreg (int_mode, tmp, inner_mode);
4222 if (SHIFT_COUNT_TRUNCATED && CONST_INT_P (op1))
4224 val = INTVAL (op1) & (GET_MODE_UNIT_PRECISION (mode) - 1);
4225 if (val != INTVAL (op1))
4226 return simplify_gen_binary (code, mode, op0,
4227 gen_int_shift_amount (mode, val));
4229 break;
4231 case SS_ASHIFT:
4232 if (CONST_INT_P (trueop0)
4233 && HWI_COMPUTABLE_MODE_P (mode)
4234 && (UINTVAL (trueop0) == (GET_MODE_MASK (mode) >> 1)
4235 || mode_signbit_p (mode, trueop0))
4236 && ! side_effects_p (op1))
4237 return op0;
4238 goto simplify_ashift;
4240 case US_ASHIFT:
4241 if (CONST_INT_P (trueop0)
4242 && HWI_COMPUTABLE_MODE_P (mode)
4243 && UINTVAL (trueop0) == GET_MODE_MASK (mode)
4244 && ! side_effects_p (op1))
4245 return op0;
4246 /* FALLTHRU */
4248 case ASHIFT:
4249 simplify_ashift:
4250 if (trueop1 == CONST0_RTX (mode))
4251 return op0;
4252 if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4253 return op0;
4254 if (mem_depth
4255 && code == ASHIFT
4256 && CONST_INT_P (trueop1)
4257 && is_a <scalar_int_mode> (mode, &int_mode)
4258 && IN_RANGE (UINTVAL (trueop1),
4259 1, GET_MODE_PRECISION (int_mode) - 1))
4261 auto c = (wi::one (GET_MODE_PRECISION (int_mode))
4262 << UINTVAL (trueop1));
4263 rtx new_op1 = immed_wide_int_const (c, int_mode);
4264 return simplify_gen_binary (MULT, int_mode, op0, new_op1);
4266 goto canonicalize_shift;
4268 case LSHIFTRT:
4269 if (trueop1 == CONST0_RTX (mode))
4270 return op0;
4271 if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4272 return op0;
4273 /* Optimize (lshiftrt (clz X) C) as (eq X 0). */
4274 if (GET_CODE (op0) == CLZ
4275 && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
4276 && CONST_INT_P (trueop1)
4277 && STORE_FLAG_VALUE == 1
4278 && INTVAL (trueop1) < GET_MODE_UNIT_PRECISION (mode))
4280 unsigned HOST_WIDE_INT zero_val = 0;
4282 if (CLZ_DEFINED_VALUE_AT_ZERO (inner_mode, zero_val)
4283 && zero_val == GET_MODE_PRECISION (inner_mode)
4284 && INTVAL (trueop1) == exact_log2 (zero_val))
4285 return simplify_gen_relational (EQ, mode, inner_mode,
4286 XEXP (op0, 0), const0_rtx);
4288 goto canonicalize_shift;
4290 case SMIN:
4291 if (HWI_COMPUTABLE_MODE_P (mode)
4292 && mode_signbit_p (mode, trueop1)
4293 && ! side_effects_p (op0))
4294 return op1;
4295 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4296 return op0;
4297 tem = simplify_associative_operation (code, mode, op0, op1);
4298 if (tem)
4299 return tem;
4300 break;
4302 case SMAX:
4303 if (HWI_COMPUTABLE_MODE_P (mode)
4304 && CONST_INT_P (trueop1)
4305 && (UINTVAL (trueop1) == GET_MODE_MASK (mode) >> 1)
4306 && ! side_effects_p (op0))
4307 return op1;
4308 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4309 return op0;
4310 tem = simplify_associative_operation (code, mode, op0, op1);
4311 if (tem)
4312 return tem;
4313 break;
4315 case UMIN:
4316 if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
4317 return op1;
4318 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4319 return op0;
4320 tem = simplify_associative_operation (code, mode, op0, op1);
4321 if (tem)
4322 return tem;
4323 break;
4325 case UMAX:
4326 if (trueop1 == constm1_rtx && ! side_effects_p (op0))
4327 return op1;
4328 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4329 return op0;
4330 tem = simplify_associative_operation (code, mode, op0, op1);
4331 if (tem)
4332 return tem;
4333 break;
4335 case SS_PLUS:
4336 case US_PLUS:
4337 case SS_MINUS:
4338 case US_MINUS:
4339 /* Simplify x +/- 0 to x, if possible. */
4340 if (trueop1 == CONST0_RTX (mode))
4341 return op0;
4342 return 0;
4344 case SS_MULT:
4345 case US_MULT:
4346 /* Simplify x * 0 to 0, if possible. */
4347 if (trueop1 == CONST0_RTX (mode)
4348 && !side_effects_p (op0))
4349 return op1;
4351 /* Simplify x * 1 to x, if possible. */
4352 if (trueop1 == CONST1_RTX (mode))
4353 return op0;
4354 return 0;
4356 case SMUL_HIGHPART:
4357 case UMUL_HIGHPART:
4358 /* Simplify x * 0 to 0, if possible. */
4359 if (trueop1 == CONST0_RTX (mode)
4360 && !side_effects_p (op0))
4361 return op1;
4362 return 0;
4364 case SS_DIV:
4365 case US_DIV:
4366 /* Simplify x / 1 to x, if possible. */
4367 if (trueop1 == CONST1_RTX (mode))
4368 return op0;
4369 return 0;
4371 case COPYSIGN:
4372 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4373 return op0;
4374 if (CONST_DOUBLE_AS_FLOAT_P (trueop1))
4376 REAL_VALUE_TYPE f1;
4377 real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
4378 rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
4379 if (REAL_VALUE_NEGATIVE (f1))
4380 tmp = simplify_gen_unary (NEG, mode, op0, mode);
4381 return tmp;
4383 if (GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
4384 return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
4385 if (GET_CODE (op1) == ABS
4386 && ! side_effects_p (op1))
4387 return simplify_gen_unary (ABS, mode, op0, mode);
4388 if (GET_CODE (op0) == COPYSIGN
4389 && ! side_effects_p (XEXP (op0, 1)))
4390 return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
4391 if (GET_CODE (op1) == COPYSIGN
4392 && ! side_effects_p (XEXP (op1, 0)))
4393 return simplify_gen_binary (COPYSIGN, mode, op0, XEXP (op1, 1));
4394 return 0;
4396 case VEC_SERIES:
4397 if (op1 == CONST0_RTX (GET_MODE_INNER (mode)))
4398 return gen_vec_duplicate (mode, op0);
4399 if (valid_for_const_vector_p (mode, op0)
4400 && valid_for_const_vector_p (mode, op1))
4401 return gen_const_vec_series (mode, op0, op1);
4402 return 0;
4404 case VEC_SELECT:
4405 if (!VECTOR_MODE_P (mode))
4407 gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
4408 gcc_assert (mode == GET_MODE_INNER (GET_MODE (trueop0)));
4409 gcc_assert (GET_CODE (trueop1) == PARALLEL);
4410 gcc_assert (XVECLEN (trueop1, 0) == 1);
4412 /* We can't reason about selections made at runtime. */
4413 if (!CONST_INT_P (XVECEXP (trueop1, 0, 0)))
4414 return 0;
4416 if (vec_duplicate_p (trueop0, &elt0))
4417 return elt0;
4419 if (GET_CODE (trueop0) == CONST_VECTOR)
4420 return CONST_VECTOR_ELT (trueop0, INTVAL (XVECEXP
4421 (trueop1, 0, 0)));
4423 /* Extract a scalar element from a nested VEC_SELECT expression
4424 (with optional nested VEC_CONCAT expression). Some targets
4425 (i386) extract scalar element from a vector using chain of
4426 nested VEC_SELECT expressions. When input operand is a memory
4427 operand, this operation can be simplified to a simple scalar
4428 load from an offseted memory address. */
4429 int n_elts;
4430 if (GET_CODE (trueop0) == VEC_SELECT
4431 && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
4432 .is_constant (&n_elts)))
4434 rtx op0 = XEXP (trueop0, 0);
4435 rtx op1 = XEXP (trueop0, 1);
4437 int i = INTVAL (XVECEXP (trueop1, 0, 0));
4438 int elem;
4440 rtvec vec;
4441 rtx tmp_op, tmp;
4443 gcc_assert (GET_CODE (op1) == PARALLEL);
4444 gcc_assert (i < n_elts);
4446 /* Select element, pointed by nested selector. */
4447 elem = INTVAL (XVECEXP (op1, 0, i));
4449 /* Handle the case when nested VEC_SELECT wraps VEC_CONCAT. */
4450 if (GET_CODE (op0) == VEC_CONCAT)
4452 rtx op00 = XEXP (op0, 0);
4453 rtx op01 = XEXP (op0, 1);
4455 machine_mode mode00, mode01;
4456 int n_elts00, n_elts01;
4458 mode00 = GET_MODE (op00);
4459 mode01 = GET_MODE (op01);
4461 /* Find out the number of elements of each operand.
4462 Since the concatenated result has a constant number
4463 of elements, the operands must too. */
4464 n_elts00 = GET_MODE_NUNITS (mode00).to_constant ();
4465 n_elts01 = GET_MODE_NUNITS (mode01).to_constant ();
4467 gcc_assert (n_elts == n_elts00 + n_elts01);
4469 /* Select correct operand of VEC_CONCAT
4470 and adjust selector. */
4471 if (elem < n_elts01)
4472 tmp_op = op00;
4473 else
4475 tmp_op = op01;
4476 elem -= n_elts00;
4479 else
4480 tmp_op = op0;
4482 vec = rtvec_alloc (1);
4483 RTVEC_ELT (vec, 0) = GEN_INT (elem);
4485 tmp = gen_rtx_fmt_ee (code, mode,
4486 tmp_op, gen_rtx_PARALLEL (VOIDmode, vec));
4487 return tmp;
4490 else
4492 gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
4493 gcc_assert (GET_MODE_INNER (mode)
4494 == GET_MODE_INNER (GET_MODE (trueop0)));
4495 gcc_assert (GET_CODE (trueop1) == PARALLEL);
4497 if (vec_duplicate_p (trueop0, &elt0))
4498 /* It doesn't matter which elements are selected by trueop1,
4499 because they are all the same. */
4500 return gen_vec_duplicate (mode, elt0);
4502 if (GET_CODE (trueop0) == CONST_VECTOR)
4504 unsigned n_elts = XVECLEN (trueop1, 0);
4505 rtvec v = rtvec_alloc (n_elts);
4506 unsigned int i;
4508 gcc_assert (known_eq (n_elts, GET_MODE_NUNITS (mode)));
4509 for (i = 0; i < n_elts; i++)
4511 rtx x = XVECEXP (trueop1, 0, i);
4513 if (!CONST_INT_P (x))
4514 return 0;
4516 RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0,
4517 INTVAL (x));
4520 return gen_rtx_CONST_VECTOR (mode, v);
4523 /* Recognize the identity. */
4524 if (GET_MODE (trueop0) == mode)
4526 bool maybe_ident = true;
4527 for (int i = 0; i < XVECLEN (trueop1, 0); i++)
4529 rtx j = XVECEXP (trueop1, 0, i);
4530 if (!CONST_INT_P (j) || INTVAL (j) != i)
4532 maybe_ident = false;
4533 break;
4536 if (maybe_ident)
4537 return trueop0;
4540 /* If we select a low-part subreg, return that. */
4541 if (vec_series_lowpart_p (mode, GET_MODE (trueop0), trueop1))
4543 rtx new_rtx = lowpart_subreg (mode, trueop0,
4544 GET_MODE (trueop0));
4545 if (new_rtx != NULL_RTX)
4546 return new_rtx;
4549 /* If we build {a,b} then permute it, build the result directly. */
4550 if (XVECLEN (trueop1, 0) == 2
4551 && CONST_INT_P (XVECEXP (trueop1, 0, 0))
4552 && CONST_INT_P (XVECEXP (trueop1, 0, 1))
4553 && GET_CODE (trueop0) == VEC_CONCAT
4554 && GET_CODE (XEXP (trueop0, 0)) == VEC_CONCAT
4555 && GET_MODE (XEXP (trueop0, 0)) == mode
4556 && GET_CODE (XEXP (trueop0, 1)) == VEC_CONCAT
4557 && GET_MODE (XEXP (trueop0, 1)) == mode)
4559 unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
4560 unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
4561 rtx subop0, subop1;
4563 gcc_assert (i0 < 4 && i1 < 4);
4564 subop0 = XEXP (XEXP (trueop0, i0 / 2), i0 % 2);
4565 subop1 = XEXP (XEXP (trueop0, i1 / 2), i1 % 2);
4567 return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
4570 if (XVECLEN (trueop1, 0) == 2
4571 && CONST_INT_P (XVECEXP (trueop1, 0, 0))
4572 && CONST_INT_P (XVECEXP (trueop1, 0, 1))
4573 && GET_CODE (trueop0) == VEC_CONCAT
4574 && GET_MODE (trueop0) == mode)
4576 unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
4577 unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
4578 rtx subop0, subop1;
4580 gcc_assert (i0 < 2 && i1 < 2);
4581 subop0 = XEXP (trueop0, i0);
4582 subop1 = XEXP (trueop0, i1);
4584 return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
4587 /* If we select one half of a vec_concat, return that. */
4588 int l0, l1;
4589 if (GET_CODE (trueop0) == VEC_CONCAT
4590 && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
4591 .is_constant (&l0))
4592 && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 1)))
4593 .is_constant (&l1))
4594 && CONST_INT_P (XVECEXP (trueop1, 0, 0)))
4596 rtx subop0 = XEXP (trueop0, 0);
4597 rtx subop1 = XEXP (trueop0, 1);
4598 machine_mode mode0 = GET_MODE (subop0);
4599 machine_mode mode1 = GET_MODE (subop1);
4600 int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
4601 if (i0 == 0 && !side_effects_p (op1) && mode == mode0)
4603 bool success = true;
4604 for (int i = 1; i < l0; ++i)
4606 rtx j = XVECEXP (trueop1, 0, i);
4607 if (!CONST_INT_P (j) || INTVAL (j) != i)
4609 success = false;
4610 break;
4613 if (success)
4614 return subop0;
4616 if (i0 == l0 && !side_effects_p (op0) && mode == mode1)
4618 bool success = true;
4619 for (int i = 1; i < l1; ++i)
4621 rtx j = XVECEXP (trueop1, 0, i);
4622 if (!CONST_INT_P (j) || INTVAL (j) != i0 + i)
4624 success = false;
4625 break;
4628 if (success)
4629 return subop1;
4633 /* Simplify vec_select of a subreg of X to just a vec_select of X
4634 when X has same component mode as vec_select. */
4635 unsigned HOST_WIDE_INT subreg_offset = 0;
4636 if (GET_CODE (trueop0) == SUBREG
4637 && GET_MODE_INNER (mode)
4638 == GET_MODE_INNER (GET_MODE (SUBREG_REG (trueop0)))
4639 && GET_MODE_NUNITS (mode).is_constant (&l1)
4640 && constant_multiple_p (subreg_memory_offset (trueop0),
4641 GET_MODE_UNIT_BITSIZE (mode),
4642 &subreg_offset))
4644 poly_uint64 nunits
4645 = GET_MODE_NUNITS (GET_MODE (SUBREG_REG (trueop0)));
4646 bool success = true;
4647 for (int i = 0; i != l1; i++)
4649 rtx idx = XVECEXP (trueop1, 0, i);
4650 if (!CONST_INT_P (idx)
4651 || maybe_ge (UINTVAL (idx) + subreg_offset, nunits))
4653 success = false;
4654 break;
4658 if (success)
4660 rtx par = trueop1;
4661 if (subreg_offset)
4663 rtvec vec = rtvec_alloc (l1);
4664 for (int i = 0; i < l1; i++)
4665 RTVEC_ELT (vec, i)
4666 = GEN_INT (INTVAL (XVECEXP (trueop1, 0, i))
4667 + subreg_offset);
4668 par = gen_rtx_PARALLEL (VOIDmode, vec);
4670 return gen_rtx_VEC_SELECT (mode, SUBREG_REG (trueop0), par);
4675 if (XVECLEN (trueop1, 0) == 1
4676 && CONST_INT_P (XVECEXP (trueop1, 0, 0))
4677 && GET_CODE (trueop0) == VEC_CONCAT)
4679 rtx vec = trueop0;
4680 offset = INTVAL (XVECEXP (trueop1, 0, 0)) * GET_MODE_SIZE (mode);
4682 /* Try to find the element in the VEC_CONCAT. */
4683 while (GET_MODE (vec) != mode
4684 && GET_CODE (vec) == VEC_CONCAT)
4686 poly_int64 vec_size;
4688 if (CONST_INT_P (XEXP (vec, 0)))
4690 /* vec_concat of two const_ints doesn't make sense with
4691 respect to modes. */
4692 if (CONST_INT_P (XEXP (vec, 1)))
4693 return 0;
4695 vec_size = GET_MODE_SIZE (GET_MODE (trueop0))
4696 - GET_MODE_SIZE (GET_MODE (XEXP (vec, 1)));
4698 else
4699 vec_size = GET_MODE_SIZE (GET_MODE (XEXP (vec, 0)));
4701 if (known_lt (offset, vec_size))
4702 vec = XEXP (vec, 0);
4703 else if (known_ge (offset, vec_size))
4705 offset -= vec_size;
4706 vec = XEXP (vec, 1);
4708 else
4709 break;
4710 vec = avoid_constant_pool_reference (vec);
4713 if (GET_MODE (vec) == mode)
4714 return vec;
4717 /* If we select elements in a vec_merge that all come from the same
4718 operand, select from that operand directly. */
4719 if (GET_CODE (op0) == VEC_MERGE)
4721 rtx trueop02 = avoid_constant_pool_reference (XEXP (op0, 2));
4722 if (CONST_INT_P (trueop02))
4724 unsigned HOST_WIDE_INT sel = UINTVAL (trueop02);
4725 bool all_operand0 = true;
4726 bool all_operand1 = true;
4727 for (int i = 0; i < XVECLEN (trueop1, 0); i++)
4729 rtx j = XVECEXP (trueop1, 0, i);
4730 if (sel & (HOST_WIDE_INT_1U << UINTVAL (j)))
4731 all_operand1 = false;
4732 else
4733 all_operand0 = false;
4735 if (all_operand0 && !side_effects_p (XEXP (op0, 1)))
4736 return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 0), op1);
4737 if (all_operand1 && !side_effects_p (XEXP (op0, 0)))
4738 return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 1), op1);
4742 /* If we have two nested selects that are inverses of each
4743 other, replace them with the source operand. */
4744 if (GET_CODE (trueop0) == VEC_SELECT
4745 && GET_MODE (XEXP (trueop0, 0)) == mode)
4747 rtx op0_subop1 = XEXP (trueop0, 1);
4748 gcc_assert (GET_CODE (op0_subop1) == PARALLEL);
4749 gcc_assert (known_eq (XVECLEN (trueop1, 0), GET_MODE_NUNITS (mode)));
4751 /* Apply the outer ordering vector to the inner one. (The inner
4752 ordering vector is expressly permitted to be of a different
4753 length than the outer one.) If the result is { 0, 1, ..., n-1 }
4754 then the two VEC_SELECTs cancel. */
4755 for (int i = 0; i < XVECLEN (trueop1, 0); ++i)
4757 rtx x = XVECEXP (trueop1, 0, i);
4758 if (!CONST_INT_P (x))
4759 return 0;
4760 rtx y = XVECEXP (op0_subop1, 0, INTVAL (x));
4761 if (!CONST_INT_P (y) || i != INTVAL (y))
4762 return 0;
4764 return XEXP (trueop0, 0);
4767 return 0;
4768 case VEC_CONCAT:
4770 machine_mode op0_mode = (GET_MODE (trueop0) != VOIDmode
4771 ? GET_MODE (trueop0)
4772 : GET_MODE_INNER (mode));
4773 machine_mode op1_mode = (GET_MODE (trueop1) != VOIDmode
4774 ? GET_MODE (trueop1)
4775 : GET_MODE_INNER (mode));
4777 gcc_assert (VECTOR_MODE_P (mode));
4778 gcc_assert (known_eq (GET_MODE_SIZE (op0_mode)
4779 + GET_MODE_SIZE (op1_mode),
4780 GET_MODE_SIZE (mode)));
4782 if (VECTOR_MODE_P (op0_mode))
4783 gcc_assert (GET_MODE_INNER (mode)
4784 == GET_MODE_INNER (op0_mode));
4785 else
4786 gcc_assert (GET_MODE_INNER (mode) == op0_mode);
4788 if (VECTOR_MODE_P (op1_mode))
4789 gcc_assert (GET_MODE_INNER (mode)
4790 == GET_MODE_INNER (op1_mode));
4791 else
4792 gcc_assert (GET_MODE_INNER (mode) == op1_mode);
4794 unsigned int n_elts, in_n_elts;
4795 if ((GET_CODE (trueop0) == CONST_VECTOR
4796 || CONST_SCALAR_INT_P (trueop0)
4797 || CONST_DOUBLE_AS_FLOAT_P (trueop0))
4798 && (GET_CODE (trueop1) == CONST_VECTOR
4799 || CONST_SCALAR_INT_P (trueop1)
4800 || CONST_DOUBLE_AS_FLOAT_P (trueop1))
4801 && GET_MODE_NUNITS (mode).is_constant (&n_elts)
4802 && GET_MODE_NUNITS (op0_mode).is_constant (&in_n_elts))
4804 rtvec v = rtvec_alloc (n_elts);
4805 unsigned int i;
4806 for (i = 0; i < n_elts; i++)
4808 if (i < in_n_elts)
4810 if (!VECTOR_MODE_P (op0_mode))
4811 RTVEC_ELT (v, i) = trueop0;
4812 else
4813 RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0, i);
4815 else
4817 if (!VECTOR_MODE_P (op1_mode))
4818 RTVEC_ELT (v, i) = trueop1;
4819 else
4820 RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop1,
4821 i - in_n_elts);
4825 return gen_rtx_CONST_VECTOR (mode, v);
4828 /* Try to merge two VEC_SELECTs from the same vector into a single one.
4829 Restrict the transformation to avoid generating a VEC_SELECT with a
4830 mode unrelated to its operand. */
4831 if (GET_CODE (trueop0) == VEC_SELECT
4832 && GET_CODE (trueop1) == VEC_SELECT
4833 && rtx_equal_p (XEXP (trueop0, 0), XEXP (trueop1, 0))
4834 && GET_MODE_INNER (GET_MODE (XEXP (trueop0, 0)))
4835 == GET_MODE_INNER(mode))
4837 rtx par0 = XEXP (trueop0, 1);
4838 rtx par1 = XEXP (trueop1, 1);
4839 int len0 = XVECLEN (par0, 0);
4840 int len1 = XVECLEN (par1, 0);
4841 rtvec vec = rtvec_alloc (len0 + len1);
4842 for (int i = 0; i < len0; i++)
4843 RTVEC_ELT (vec, i) = XVECEXP (par0, 0, i);
4844 for (int i = 0; i < len1; i++)
4845 RTVEC_ELT (vec, len0 + i) = XVECEXP (par1, 0, i);
4846 return simplify_gen_binary (VEC_SELECT, mode, XEXP (trueop0, 0),
4847 gen_rtx_PARALLEL (VOIDmode, vec));
4850 return 0;
4852 default:
4853 gcc_unreachable ();
4856 if (mode == GET_MODE (op0)
4857 && mode == GET_MODE (op1)
4858 && vec_duplicate_p (op0, &elt0)
4859 && vec_duplicate_p (op1, &elt1))
4861 /* Try applying the operator to ELT and see if that simplifies.
4862 We can duplicate the result if so.
4864 The reason we don't use simplify_gen_binary is that it isn't
4865 necessarily a win to convert things like:
4867 (plus:V (vec_duplicate:V (reg:S R1))
4868 (vec_duplicate:V (reg:S R2)))
4872 (vec_duplicate:V (plus:S (reg:S R1) (reg:S R2)))
4874 The first might be done entirely in vector registers while the
4875 second might need a move between register files. */
4876 tem = simplify_binary_operation (code, GET_MODE_INNER (mode),
4877 elt0, elt1);
4878 if (tem)
4879 return gen_vec_duplicate (mode, tem);
4882 return 0;
4885 /* Return true if binary operation OP distributes over addition in operand
4886 OPNO, with the other operand being held constant. OPNO counts from 1. */
4888 static bool
4889 distributes_over_addition_p (rtx_code op, int opno)
4891 switch (op)
4893 case PLUS:
4894 case MINUS:
4895 case MULT:
4896 return true;
4898 case ASHIFT:
4899 return opno == 1;
4901 default:
4902 return false;
4907 simplify_const_binary_operation (enum rtx_code code, machine_mode mode,
4908 rtx op0, rtx op1)
4910 if (VECTOR_MODE_P (mode)
4911 && code != VEC_CONCAT
4912 && GET_CODE (op0) == CONST_VECTOR
4913 && GET_CODE (op1) == CONST_VECTOR)
4915 bool step_ok_p;
4916 if (CONST_VECTOR_STEPPED_P (op0)
4917 && CONST_VECTOR_STEPPED_P (op1))
4918 /* We can operate directly on the encoding if:
4920 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
4921 implies
4922 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
4924 Addition and subtraction are the supported operators
4925 for which this is true. */
4926 step_ok_p = (code == PLUS || code == MINUS);
4927 else if (CONST_VECTOR_STEPPED_P (op0))
4928 /* We can operate directly on stepped encodings if:
4930 a3 - a2 == a2 - a1
4931 implies:
4932 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
4934 which is true if (x -> x op c) distributes over addition. */
4935 step_ok_p = distributes_over_addition_p (code, 1);
4936 else
4937 /* Similarly in reverse. */
4938 step_ok_p = distributes_over_addition_p (code, 2);
4939 rtx_vector_builder builder;
4940 if (!builder.new_binary_operation (mode, op0, op1, step_ok_p))
4941 return 0;
4943 unsigned int count = builder.encoded_nelts ();
4944 for (unsigned int i = 0; i < count; i++)
4946 rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
4947 CONST_VECTOR_ELT (op0, i),
4948 CONST_VECTOR_ELT (op1, i));
4949 if (!x || !valid_for_const_vector_p (mode, x))
4950 return 0;
4951 builder.quick_push (x);
4953 return builder.build ();
4956 if (VECTOR_MODE_P (mode)
4957 && code == VEC_CONCAT
4958 && (CONST_SCALAR_INT_P (op0)
4959 || CONST_FIXED_P (op0)
4960 || CONST_DOUBLE_AS_FLOAT_P (op0))
4961 && (CONST_SCALAR_INT_P (op1)
4962 || CONST_DOUBLE_AS_FLOAT_P (op1)
4963 || CONST_FIXED_P (op1)))
4965 /* Both inputs have a constant number of elements, so the result
4966 must too. */
4967 unsigned n_elts = GET_MODE_NUNITS (mode).to_constant ();
4968 rtvec v = rtvec_alloc (n_elts);
4970 gcc_assert (n_elts >= 2);
4971 if (n_elts == 2)
4973 gcc_assert (GET_CODE (op0) != CONST_VECTOR);
4974 gcc_assert (GET_CODE (op1) != CONST_VECTOR);
4976 RTVEC_ELT (v, 0) = op0;
4977 RTVEC_ELT (v, 1) = op1;
4979 else
4981 unsigned op0_n_elts = GET_MODE_NUNITS (GET_MODE (op0)).to_constant ();
4982 unsigned op1_n_elts = GET_MODE_NUNITS (GET_MODE (op1)).to_constant ();
4983 unsigned i;
4985 gcc_assert (GET_CODE (op0) == CONST_VECTOR);
4986 gcc_assert (GET_CODE (op1) == CONST_VECTOR);
4987 gcc_assert (op0_n_elts + op1_n_elts == n_elts);
4989 for (i = 0; i < op0_n_elts; ++i)
4990 RTVEC_ELT (v, i) = CONST_VECTOR_ELT (op0, i);
4991 for (i = 0; i < op1_n_elts; ++i)
4992 RTVEC_ELT (v, op0_n_elts+i) = CONST_VECTOR_ELT (op1, i);
4995 return gen_rtx_CONST_VECTOR (mode, v);
4998 if (SCALAR_FLOAT_MODE_P (mode)
4999 && CONST_DOUBLE_AS_FLOAT_P (op0)
5000 && CONST_DOUBLE_AS_FLOAT_P (op1)
5001 && mode == GET_MODE (op0) && mode == GET_MODE (op1))
5003 if (code == AND
5004 || code == IOR
5005 || code == XOR)
5007 long tmp0[4];
5008 long tmp1[4];
5009 REAL_VALUE_TYPE r;
5010 int i;
5012 real_to_target (tmp0, CONST_DOUBLE_REAL_VALUE (op0),
5013 GET_MODE (op0));
5014 real_to_target (tmp1, CONST_DOUBLE_REAL_VALUE (op1),
5015 GET_MODE (op1));
5016 for (i = 0; i < 4; i++)
5018 switch (code)
5020 case AND:
5021 tmp0[i] &= tmp1[i];
5022 break;
5023 case IOR:
5024 tmp0[i] |= tmp1[i];
5025 break;
5026 case XOR:
5027 tmp0[i] ^= tmp1[i];
5028 break;
5029 default:
5030 gcc_unreachable ();
5033 real_from_target (&r, tmp0, mode);
5034 return const_double_from_real_value (r, mode);
5036 else if (code == COPYSIGN)
5038 REAL_VALUE_TYPE f0, f1;
5039 real_convert (&f0, mode, CONST_DOUBLE_REAL_VALUE (op0));
5040 real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (op1));
5041 real_copysign (&f0, &f1);
5042 return const_double_from_real_value (f0, mode);
5044 else
5046 REAL_VALUE_TYPE f0, f1, value, result;
5047 const REAL_VALUE_TYPE *opr0, *opr1;
5048 bool inexact;
5050 opr0 = CONST_DOUBLE_REAL_VALUE (op0);
5051 opr1 = CONST_DOUBLE_REAL_VALUE (op1);
5053 if (HONOR_SNANS (mode)
5054 && (REAL_VALUE_ISSIGNALING_NAN (*opr0)
5055 || REAL_VALUE_ISSIGNALING_NAN (*opr1)))
5056 return 0;
5058 real_convert (&f0, mode, opr0);
5059 real_convert (&f1, mode, opr1);
5061 if (code == DIV
5062 && real_equal (&f1, &dconst0)
5063 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
5064 return 0;
5066 if (MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
5067 && flag_trapping_math
5068 && REAL_VALUE_ISINF (f0) && REAL_VALUE_ISINF (f1))
5070 int s0 = REAL_VALUE_NEGATIVE (f0);
5071 int s1 = REAL_VALUE_NEGATIVE (f1);
5073 switch (code)
5075 case PLUS:
5076 /* Inf + -Inf = NaN plus exception. */
5077 if (s0 != s1)
5078 return 0;
5079 break;
5080 case MINUS:
5081 /* Inf - Inf = NaN plus exception. */
5082 if (s0 == s1)
5083 return 0;
5084 break;
5085 case DIV:
5086 /* Inf / Inf = NaN plus exception. */
5087 return 0;
5088 default:
5089 break;
5093 if (code == MULT && MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
5094 && flag_trapping_math
5095 && ((REAL_VALUE_ISINF (f0) && real_equal (&f1, &dconst0))
5096 || (REAL_VALUE_ISINF (f1)
5097 && real_equal (&f0, &dconst0))))
5098 /* Inf * 0 = NaN plus exception. */
5099 return 0;
5101 inexact = real_arithmetic (&value, rtx_to_tree_code (code),
5102 &f0, &f1);
5103 real_convert (&result, mode, &value);
5105 /* Don't constant fold this floating point operation if
5106 the result has overflowed and flag_trapping_math. */
5108 if (flag_trapping_math
5109 && MODE_HAS_INFINITIES (mode)
5110 && REAL_VALUE_ISINF (result)
5111 && !REAL_VALUE_ISINF (f0)
5112 && !REAL_VALUE_ISINF (f1))
5113 /* Overflow plus exception. */
5114 return 0;
5116 /* Don't constant fold this floating point operation if the
5117 result may dependent upon the run-time rounding mode and
5118 flag_rounding_math is set, or if GCC's software emulation
5119 is unable to accurately represent the result. */
5121 if ((flag_rounding_math
5122 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
5123 && (inexact || !real_identical (&result, &value)))
5124 return NULL_RTX;
5126 return const_double_from_real_value (result, mode);
5130 /* We can fold some multi-word operations. */
5131 scalar_int_mode int_mode;
5132 if (is_a <scalar_int_mode> (mode, &int_mode)
5133 && CONST_SCALAR_INT_P (op0)
5134 && CONST_SCALAR_INT_P (op1)
5135 && GET_MODE_PRECISION (int_mode) <= MAX_BITSIZE_MODE_ANY_INT)
5137 wide_int result;
5138 wi::overflow_type overflow;
5139 rtx_mode_t pop0 = rtx_mode_t (op0, int_mode);
5140 rtx_mode_t pop1 = rtx_mode_t (op1, int_mode);
5142 #if TARGET_SUPPORTS_WIDE_INT == 0
5143 /* This assert keeps the simplification from producing a result
5144 that cannot be represented in a CONST_DOUBLE but a lot of
5145 upstream callers expect that this function never fails to
5146 simplify something and so you if you added this to the test
5147 above the code would die later anyway. If this assert
5148 happens, you just need to make the port support wide int. */
5149 gcc_assert (GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_DOUBLE_INT);
5150 #endif
5151 switch (code)
5153 case MINUS:
5154 result = wi::sub (pop0, pop1);
5155 break;
5157 case PLUS:
5158 result = wi::add (pop0, pop1);
5159 break;
5161 case MULT:
5162 result = wi::mul (pop0, pop1);
5163 break;
5165 case DIV:
5166 result = wi::div_trunc (pop0, pop1, SIGNED, &overflow);
5167 if (overflow)
5168 return NULL_RTX;
5169 break;
5171 case MOD:
5172 result = wi::mod_trunc (pop0, pop1, SIGNED, &overflow);
5173 if (overflow)
5174 return NULL_RTX;
5175 break;
5177 case UDIV:
5178 result = wi::div_trunc (pop0, pop1, UNSIGNED, &overflow);
5179 if (overflow)
5180 return NULL_RTX;
5181 break;
5183 case UMOD:
5184 result = wi::mod_trunc (pop0, pop1, UNSIGNED, &overflow);
5185 if (overflow)
5186 return NULL_RTX;
5187 break;
5189 case AND:
5190 result = wi::bit_and (pop0, pop1);
5191 break;
5193 case IOR:
5194 result = wi::bit_or (pop0, pop1);
5195 break;
5197 case XOR:
5198 result = wi::bit_xor (pop0, pop1);
5199 break;
5201 case SMIN:
5202 result = wi::smin (pop0, pop1);
5203 break;
5205 case SMAX:
5206 result = wi::smax (pop0, pop1);
5207 break;
5209 case UMIN:
5210 result = wi::umin (pop0, pop1);
5211 break;
5213 case UMAX:
5214 result = wi::umax (pop0, pop1);
5215 break;
5217 case LSHIFTRT:
5218 case ASHIFTRT:
5219 case ASHIFT:
5220 case SS_ASHIFT:
5221 case US_ASHIFT:
5223 /* The shift count might be in SImode while int_mode might
5224 be narrower. On IA-64 it is even DImode. If the shift
5225 count is too large and doesn't fit into int_mode, we'd
5226 ICE. So, if int_mode is narrower than word, use
5227 word_mode for the shift count. */
5228 if (GET_MODE (op1) == VOIDmode
5229 && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD)
5230 pop1 = rtx_mode_t (op1, word_mode);
5232 wide_int wop1 = pop1;
5233 if (SHIFT_COUNT_TRUNCATED)
5234 wop1 = wi::umod_trunc (wop1, GET_MODE_PRECISION (int_mode));
5235 else if (wi::geu_p (wop1, GET_MODE_PRECISION (int_mode)))
5236 return NULL_RTX;
5238 switch (code)
5240 case LSHIFTRT:
5241 result = wi::lrshift (pop0, wop1);
5242 break;
5244 case ASHIFTRT:
5245 result = wi::arshift (pop0, wop1);
5246 break;
5248 case ASHIFT:
5249 result = wi::lshift (pop0, wop1);
5250 break;
5252 case SS_ASHIFT:
5253 if (wi::leu_p (wop1, wi::clrsb (pop0)))
5254 result = wi::lshift (pop0, wop1);
5255 else if (wi::neg_p (pop0))
5256 result = wi::min_value (int_mode, SIGNED);
5257 else
5258 result = wi::max_value (int_mode, SIGNED);
5259 break;
5261 case US_ASHIFT:
5262 if (wi::eq_p (pop0, 0))
5263 result = pop0;
5264 else if (wi::leu_p (wop1, wi::clz (pop0)))
5265 result = wi::lshift (pop0, wop1);
5266 else
5267 result = wi::max_value (int_mode, UNSIGNED);
5268 break;
5270 default:
5271 gcc_unreachable ();
5273 break;
5275 case ROTATE:
5276 case ROTATERT:
5278 /* The rotate count might be in SImode while int_mode might
5279 be narrower. On IA-64 it is even DImode. If the shift
5280 count is too large and doesn't fit into int_mode, we'd
5281 ICE. So, if int_mode is narrower than word, use
5282 word_mode for the shift count. */
5283 if (GET_MODE (op1) == VOIDmode
5284 && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD)
5285 pop1 = rtx_mode_t (op1, word_mode);
5287 if (wi::neg_p (pop1))
5288 return NULL_RTX;
5290 switch (code)
5292 case ROTATE:
5293 result = wi::lrotate (pop0, pop1);
5294 break;
5296 case ROTATERT:
5297 result = wi::rrotate (pop0, pop1);
5298 break;
5300 default:
5301 gcc_unreachable ();
5303 break;
5306 case SS_PLUS:
5307 result = wi::add (pop0, pop1, SIGNED, &overflow);
5308 clamp_signed_saturation:
5309 if (overflow == wi::OVF_OVERFLOW)
5310 result = wi::max_value (GET_MODE_PRECISION (int_mode), SIGNED);
5311 else if (overflow == wi::OVF_UNDERFLOW)
5312 result = wi::min_value (GET_MODE_PRECISION (int_mode), SIGNED);
5313 else if (overflow != wi::OVF_NONE)
5314 return NULL_RTX;
5315 break;
5317 case US_PLUS:
5318 result = wi::add (pop0, pop1, UNSIGNED, &overflow);
5319 clamp_unsigned_saturation:
5320 if (overflow != wi::OVF_NONE)
5321 result = wi::max_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
5322 break;
5324 case SS_MINUS:
5325 result = wi::sub (pop0, pop1, SIGNED, &overflow);
5326 goto clamp_signed_saturation;
5328 case US_MINUS:
5329 result = wi::sub (pop0, pop1, UNSIGNED, &overflow);
5330 if (overflow != wi::OVF_NONE)
5331 result = wi::min_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
5332 break;
5334 case SS_MULT:
5335 result = wi::mul (pop0, pop1, SIGNED, &overflow);
5336 goto clamp_signed_saturation;
5338 case US_MULT:
5339 result = wi::mul (pop0, pop1, UNSIGNED, &overflow);
5340 goto clamp_unsigned_saturation;
5342 case SMUL_HIGHPART:
5343 result = wi::mul_high (pop0, pop1, SIGNED);
5344 break;
5346 case UMUL_HIGHPART:
5347 result = wi::mul_high (pop0, pop1, UNSIGNED);
5348 break;
5350 default:
5351 return NULL_RTX;
5353 return immed_wide_int_const (result, int_mode);
5356 /* Handle polynomial integers. */
5357 if (NUM_POLY_INT_COEFFS > 1
5358 && is_a <scalar_int_mode> (mode, &int_mode)
5359 && poly_int_rtx_p (op0)
5360 && poly_int_rtx_p (op1))
5362 poly_wide_int result;
5363 switch (code)
5365 case PLUS:
5366 result = wi::to_poly_wide (op0, mode) + wi::to_poly_wide (op1, mode);
5367 break;
5369 case MINUS:
5370 result = wi::to_poly_wide (op0, mode) - wi::to_poly_wide (op1, mode);
5371 break;
5373 case MULT:
5374 if (CONST_SCALAR_INT_P (op1))
5375 result = wi::to_poly_wide (op0, mode) * rtx_mode_t (op1, mode);
5376 else
5377 return NULL_RTX;
5378 break;
5380 case ASHIFT:
5381 if (CONST_SCALAR_INT_P (op1))
5383 wide_int shift
5384 = rtx_mode_t (op1,
5385 GET_MODE (op1) == VOIDmode
5386 && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD
5387 ? word_mode : mode);
5388 if (SHIFT_COUNT_TRUNCATED)
5389 shift = wi::umod_trunc (shift, GET_MODE_PRECISION (int_mode));
5390 else if (wi::geu_p (shift, GET_MODE_PRECISION (int_mode)))
5391 return NULL_RTX;
5392 result = wi::to_poly_wide (op0, mode) << shift;
5394 else
5395 return NULL_RTX;
5396 break;
5398 case IOR:
5399 if (!CONST_SCALAR_INT_P (op1)
5400 || !can_ior_p (wi::to_poly_wide (op0, mode),
5401 rtx_mode_t (op1, mode), &result))
5402 return NULL_RTX;
5403 break;
5405 default:
5406 return NULL_RTX;
5408 return immed_wide_int_const (result, int_mode);
5411 return NULL_RTX;
5416 /* Return a positive integer if X should sort after Y. The value
5417 returned is 1 if and only if X and Y are both regs. */
5419 static int
5420 simplify_plus_minus_op_data_cmp (rtx x, rtx y)
5422 int result;
5424 result = (commutative_operand_precedence (y)
5425 - commutative_operand_precedence (x));
5426 if (result)
5427 return result + result;
5429 /* Group together equal REGs to do more simplification. */
5430 if (REG_P (x) && REG_P (y))
5431 return REGNO (x) > REGNO (y);
5433 return 0;
5436 /* Simplify and canonicalize a PLUS or MINUS, at least one of whose
5437 operands may be another PLUS or MINUS.
5439 Rather than test for specific case, we do this by a brute-force method
5440 and do all possible simplifications until no more changes occur. Then
5441 we rebuild the operation.
5443 May return NULL_RTX when no changes were made. */
5446 simplify_context::simplify_plus_minus (rtx_code code, machine_mode mode,
5447 rtx op0, rtx op1)
5449 struct simplify_plus_minus_op_data
5451 rtx op;
5452 short neg;
5453 } ops[16];
5454 rtx result, tem;
5455 int n_ops = 2;
5456 int changed, n_constants, canonicalized = 0;
5457 int i, j;
5459 memset (ops, 0, sizeof ops);
5461 /* Set up the two operands and then expand them until nothing has been
5462 changed. If we run out of room in our array, give up; this should
5463 almost never happen. */
5465 ops[0].op = op0;
5466 ops[0].neg = 0;
5467 ops[1].op = op1;
5468 ops[1].neg = (code == MINUS);
5472 changed = 0;
5473 n_constants = 0;
5475 for (i = 0; i < n_ops; i++)
5477 rtx this_op = ops[i].op;
5478 int this_neg = ops[i].neg;
5479 enum rtx_code this_code = GET_CODE (this_op);
5481 switch (this_code)
5483 case PLUS:
5484 case MINUS:
5485 if (n_ops == ARRAY_SIZE (ops))
5486 return NULL_RTX;
5488 ops[n_ops].op = XEXP (this_op, 1);
5489 ops[n_ops].neg = (this_code == MINUS) ^ this_neg;
5490 n_ops++;
5492 ops[i].op = XEXP (this_op, 0);
5493 changed = 1;
5494 /* If this operand was negated then we will potentially
5495 canonicalize the expression. Similarly if we don't
5496 place the operands adjacent we're re-ordering the
5497 expression and thus might be performing a
5498 canonicalization. Ignore register re-ordering.
5499 ??? It might be better to shuffle the ops array here,
5500 but then (plus (plus (A, B), plus (C, D))) wouldn't
5501 be seen as non-canonical. */
5502 if (this_neg
5503 || (i != n_ops - 2
5504 && !(REG_P (ops[i].op) && REG_P (ops[n_ops - 1].op))))
5505 canonicalized = 1;
5506 break;
5508 case NEG:
5509 ops[i].op = XEXP (this_op, 0);
5510 ops[i].neg = ! this_neg;
5511 changed = 1;
5512 canonicalized = 1;
5513 break;
5515 case CONST:
5516 if (n_ops != ARRAY_SIZE (ops)
5517 && GET_CODE (XEXP (this_op, 0)) == PLUS
5518 && CONSTANT_P (XEXP (XEXP (this_op, 0), 0))
5519 && CONSTANT_P (XEXP (XEXP (this_op, 0), 1)))
5521 ops[i].op = XEXP (XEXP (this_op, 0), 0);
5522 ops[n_ops].op = XEXP (XEXP (this_op, 0), 1);
5523 ops[n_ops].neg = this_neg;
5524 n_ops++;
5525 changed = 1;
5526 canonicalized = 1;
5528 break;
5530 case NOT:
5531 /* ~a -> (-a - 1) */
5532 if (n_ops != ARRAY_SIZE (ops))
5534 ops[n_ops].op = CONSTM1_RTX (mode);
5535 ops[n_ops++].neg = this_neg;
5536 ops[i].op = XEXP (this_op, 0);
5537 ops[i].neg = !this_neg;
5538 changed = 1;
5539 canonicalized = 1;
5541 break;
5543 CASE_CONST_SCALAR_INT:
5544 case CONST_POLY_INT:
5545 n_constants++;
5546 if (this_neg)
5548 ops[i].op = neg_poly_int_rtx (mode, this_op);
5549 ops[i].neg = 0;
5550 changed = 1;
5551 canonicalized = 1;
5553 break;
5555 default:
5556 break;
5560 while (changed);
5562 if (n_constants > 1)
5563 canonicalized = 1;
5565 gcc_assert (n_ops >= 2);
5567 /* If we only have two operands, we can avoid the loops. */
5568 if (n_ops == 2)
5570 enum rtx_code code = ops[0].neg || ops[1].neg ? MINUS : PLUS;
5571 rtx lhs, rhs;
5573 /* Get the two operands. Be careful with the order, especially for
5574 the cases where code == MINUS. */
5575 if (ops[0].neg && ops[1].neg)
5577 lhs = gen_rtx_NEG (mode, ops[0].op);
5578 rhs = ops[1].op;
5580 else if (ops[0].neg)
5582 lhs = ops[1].op;
5583 rhs = ops[0].op;
5585 else
5587 lhs = ops[0].op;
5588 rhs = ops[1].op;
5591 return simplify_const_binary_operation (code, mode, lhs, rhs);
5594 /* Now simplify each pair of operands until nothing changes. */
5595 while (1)
5597 /* Insertion sort is good enough for a small array. */
5598 for (i = 1; i < n_ops; i++)
5600 struct simplify_plus_minus_op_data save;
5601 int cmp;
5603 j = i - 1;
5604 cmp = simplify_plus_minus_op_data_cmp (ops[j].op, ops[i].op);
5605 if (cmp <= 0)
5606 continue;
5607 /* Just swapping registers doesn't count as canonicalization. */
5608 if (cmp != 1)
5609 canonicalized = 1;
5611 save = ops[i];
5613 ops[j + 1] = ops[j];
5614 while (j--
5615 && simplify_plus_minus_op_data_cmp (ops[j].op, save.op) > 0);
5616 ops[j + 1] = save;
5619 changed = 0;
5620 for (i = n_ops - 1; i > 0; i--)
5621 for (j = i - 1; j >= 0; j--)
5623 rtx lhs = ops[j].op, rhs = ops[i].op;
5624 int lneg = ops[j].neg, rneg = ops[i].neg;
5626 if (lhs != 0 && rhs != 0)
5628 enum rtx_code ncode = PLUS;
5630 if (lneg != rneg)
5632 ncode = MINUS;
5633 if (lneg)
5634 std::swap (lhs, rhs);
5636 else if (swap_commutative_operands_p (lhs, rhs))
5637 std::swap (lhs, rhs);
5639 if ((GET_CODE (lhs) == CONST || CONST_INT_P (lhs))
5640 && (GET_CODE (rhs) == CONST || CONST_INT_P (rhs)))
5642 rtx tem_lhs, tem_rhs;
5644 tem_lhs = GET_CODE (lhs) == CONST ? XEXP (lhs, 0) : lhs;
5645 tem_rhs = GET_CODE (rhs) == CONST ? XEXP (rhs, 0) : rhs;
5646 tem = simplify_binary_operation (ncode, mode, tem_lhs,
5647 tem_rhs);
5649 if (tem && !CONSTANT_P (tem))
5650 tem = gen_rtx_CONST (GET_MODE (tem), tem);
5652 else
5653 tem = simplify_binary_operation (ncode, mode, lhs, rhs);
5655 if (tem)
5657 /* Reject "simplifications" that just wrap the two
5658 arguments in a CONST. Failure to do so can result
5659 in infinite recursion with simplify_binary_operation
5660 when it calls us to simplify CONST operations.
5661 Also, if we find such a simplification, don't try
5662 any more combinations with this rhs: We must have
5663 something like symbol+offset, ie. one of the
5664 trivial CONST expressions we handle later. */
5665 if (GET_CODE (tem) == CONST
5666 && GET_CODE (XEXP (tem, 0)) == ncode
5667 && XEXP (XEXP (tem, 0), 0) == lhs
5668 && XEXP (XEXP (tem, 0), 1) == rhs)
5669 break;
5670 lneg &= rneg;
5671 if (GET_CODE (tem) == NEG)
5672 tem = XEXP (tem, 0), lneg = !lneg;
5673 if (poly_int_rtx_p (tem) && lneg)
5674 tem = neg_poly_int_rtx (mode, tem), lneg = 0;
5676 ops[i].op = tem;
5677 ops[i].neg = lneg;
5678 ops[j].op = NULL_RTX;
5679 changed = 1;
5680 canonicalized = 1;
5685 if (!changed)
5686 break;
5688 /* Pack all the operands to the lower-numbered entries. */
5689 for (i = 0, j = 0; j < n_ops; j++)
5690 if (ops[j].op)
5692 ops[i] = ops[j];
5693 i++;
5695 n_ops = i;
5698 /* If nothing changed, check that rematerialization of rtl instructions
5699 is still required. */
5700 if (!canonicalized)
5702 /* Perform rematerialization if only all operands are registers and
5703 all operations are PLUS. */
5704 /* ??? Also disallow (non-global, non-frame) fixed registers to work
5705 around rs6000 and how it uses the CA register. See PR67145. */
5706 for (i = 0; i < n_ops; i++)
5707 if (ops[i].neg
5708 || !REG_P (ops[i].op)
5709 || (REGNO (ops[i].op) < FIRST_PSEUDO_REGISTER
5710 && fixed_regs[REGNO (ops[i].op)]
5711 && !global_regs[REGNO (ops[i].op)]
5712 && ops[i].op != frame_pointer_rtx
5713 && ops[i].op != arg_pointer_rtx
5714 && ops[i].op != stack_pointer_rtx))
5715 return NULL_RTX;
5716 goto gen_result;
5719 /* Create (minus -C X) instead of (neg (const (plus X C))). */
5720 if (n_ops == 2
5721 && CONST_INT_P (ops[1].op)
5722 && CONSTANT_P (ops[0].op)
5723 && ops[0].neg)
5724 return gen_rtx_fmt_ee (MINUS, mode, ops[1].op, ops[0].op);
5726 /* We suppressed creation of trivial CONST expressions in the
5727 combination loop to avoid recursion. Create one manually now.
5728 The combination loop should have ensured that there is exactly
5729 one CONST_INT, and the sort will have ensured that it is last
5730 in the array and that any other constant will be next-to-last. */
5732 if (n_ops > 1
5733 && poly_int_rtx_p (ops[n_ops - 1].op)
5734 && CONSTANT_P (ops[n_ops - 2].op))
5736 rtx value = ops[n_ops - 1].op;
5737 if (ops[n_ops - 1].neg ^ ops[n_ops - 2].neg)
5738 value = neg_poly_int_rtx (mode, value);
5739 if (CONST_INT_P (value))
5741 ops[n_ops - 2].op = plus_constant (mode, ops[n_ops - 2].op,
5742 INTVAL (value));
5743 n_ops--;
5747 /* Put a non-negated operand first, if possible. */
5749 for (i = 0; i < n_ops && ops[i].neg; i++)
5750 continue;
5751 if (i == n_ops)
5752 ops[0].op = gen_rtx_NEG (mode, ops[0].op);
5753 else if (i != 0)
5755 tem = ops[0].op;
5756 ops[0] = ops[i];
5757 ops[i].op = tem;
5758 ops[i].neg = 1;
5761 /* Now make the result by performing the requested operations. */
5762 gen_result:
5763 result = ops[0].op;
5764 for (i = 1; i < n_ops; i++)
5765 result = gen_rtx_fmt_ee (ops[i].neg ? MINUS : PLUS,
5766 mode, result, ops[i].op);
5768 return result;
5771 /* Check whether an operand is suitable for calling simplify_plus_minus. */
5772 static bool
5773 plus_minus_operand_p (const_rtx x)
5775 return GET_CODE (x) == PLUS
5776 || GET_CODE (x) == MINUS
5777 || (GET_CODE (x) == CONST
5778 && GET_CODE (XEXP (x, 0)) == PLUS
5779 && CONSTANT_P (XEXP (XEXP (x, 0), 0))
5780 && CONSTANT_P (XEXP (XEXP (x, 0), 1)));
5783 /* Like simplify_binary_operation except used for relational operators.
5784 MODE is the mode of the result. If MODE is VOIDmode, both operands must
5785 not also be VOIDmode.
5787 CMP_MODE specifies in which mode the comparison is done in, so it is
5788 the mode of the operands. If CMP_MODE is VOIDmode, it is taken from
5789 the operands or, if both are VOIDmode, the operands are compared in
5790 "infinite precision". */
5792 simplify_context::simplify_relational_operation (rtx_code code,
5793 machine_mode mode,
5794 machine_mode cmp_mode,
5795 rtx op0, rtx op1)
5797 rtx tem, trueop0, trueop1;
5799 if (cmp_mode == VOIDmode)
5800 cmp_mode = GET_MODE (op0);
5801 if (cmp_mode == VOIDmode)
5802 cmp_mode = GET_MODE (op1);
5804 tem = simplify_const_relational_operation (code, cmp_mode, op0, op1);
5805 if (tem)
5806 return relational_result (mode, cmp_mode, tem);
5808 /* For the following tests, ensure const0_rtx is op1. */
5809 if (swap_commutative_operands_p (op0, op1)
5810 || (op0 == const0_rtx && op1 != const0_rtx))
5811 std::swap (op0, op1), code = swap_condition (code);
5813 /* If op0 is a compare, extract the comparison arguments from it. */
5814 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
5815 return simplify_gen_relational (code, mode, VOIDmode,
5816 XEXP (op0, 0), XEXP (op0, 1));
5818 if (GET_MODE_CLASS (cmp_mode) == MODE_CC)
5819 return NULL_RTX;
5821 trueop0 = avoid_constant_pool_reference (op0);
5822 trueop1 = avoid_constant_pool_reference (op1);
5823 return simplify_relational_operation_1 (code, mode, cmp_mode,
5824 trueop0, trueop1);
5827 /* This part of simplify_relational_operation is only used when CMP_MODE
5828 is not in class MODE_CC (i.e. it is a real comparison).
5830 MODE is the mode of the result, while CMP_MODE specifies in which
5831 mode the comparison is done in, so it is the mode of the operands. */
5834 simplify_context::simplify_relational_operation_1 (rtx_code code,
5835 machine_mode mode,
5836 machine_mode cmp_mode,
5837 rtx op0, rtx op1)
5839 enum rtx_code op0code = GET_CODE (op0);
5841 if (op1 == const0_rtx && COMPARISON_P (op0))
5843 /* If op0 is a comparison, extract the comparison arguments
5844 from it. */
5845 if (code == NE)
5847 if (GET_MODE (op0) == mode)
5848 return simplify_rtx (op0);
5849 else
5850 return simplify_gen_relational (GET_CODE (op0), mode, VOIDmode,
5851 XEXP (op0, 0), XEXP (op0, 1));
5853 else if (code == EQ)
5855 enum rtx_code new_code = reversed_comparison_code (op0, NULL);
5856 if (new_code != UNKNOWN)
5857 return simplify_gen_relational (new_code, mode, VOIDmode,
5858 XEXP (op0, 0), XEXP (op0, 1));
5862 /* (LTU/GEU (PLUS a C) C), where C is constant, can be simplified to
5863 (GEU/LTU a -C). Likewise for (LTU/GEU (PLUS a C) a). */
5864 if ((code == LTU || code == GEU)
5865 && GET_CODE (op0) == PLUS
5866 && CONST_INT_P (XEXP (op0, 1))
5867 && (rtx_equal_p (op1, XEXP (op0, 0))
5868 || rtx_equal_p (op1, XEXP (op0, 1)))
5869 /* (LTU/GEU (PLUS a 0) 0) is not the same as (GEU/LTU a 0). */
5870 && XEXP (op0, 1) != const0_rtx)
5872 rtx new_cmp
5873 = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
5874 return simplify_gen_relational ((code == LTU ? GEU : LTU), mode,
5875 cmp_mode, XEXP (op0, 0), new_cmp);
5878 /* (GTU (PLUS a C) (C - 1)) where C is a non-zero constant can be
5879 transformed into (LTU a -C). */
5880 if (code == GTU && GET_CODE (op0) == PLUS && CONST_INT_P (op1)
5881 && CONST_INT_P (XEXP (op0, 1))
5882 && (UINTVAL (op1) == UINTVAL (XEXP (op0, 1)) - 1)
5883 && XEXP (op0, 1) != const0_rtx)
5885 rtx new_cmp
5886 = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
5887 return simplify_gen_relational (LTU, mode, cmp_mode,
5888 XEXP (op0, 0), new_cmp);
5891 /* Canonicalize (LTU/GEU (PLUS a b) b) as (LTU/GEU (PLUS a b) a). */
5892 if ((code == LTU || code == GEU)
5893 && GET_CODE (op0) == PLUS
5894 && rtx_equal_p (op1, XEXP (op0, 1))
5895 /* Don't recurse "infinitely" for (LTU/GEU (PLUS b b) b). */
5896 && !rtx_equal_p (op1, XEXP (op0, 0)))
5897 return simplify_gen_relational (code, mode, cmp_mode, op0,
5898 copy_rtx (XEXP (op0, 0)));
5900 if (op1 == const0_rtx)
5902 /* Canonicalize (GTU x 0) as (NE x 0). */
5903 if (code == GTU)
5904 return simplify_gen_relational (NE, mode, cmp_mode, op0, op1);
5905 /* Canonicalize (LEU x 0) as (EQ x 0). */
5906 if (code == LEU)
5907 return simplify_gen_relational (EQ, mode, cmp_mode, op0, op1);
5909 else if (op1 == const1_rtx)
5911 switch (code)
5913 case GE:
5914 /* Canonicalize (GE x 1) as (GT x 0). */
5915 return simplify_gen_relational (GT, mode, cmp_mode,
5916 op0, const0_rtx);
5917 case GEU:
5918 /* Canonicalize (GEU x 1) as (NE x 0). */
5919 return simplify_gen_relational (NE, mode, cmp_mode,
5920 op0, const0_rtx);
5921 case LT:
5922 /* Canonicalize (LT x 1) as (LE x 0). */
5923 return simplify_gen_relational (LE, mode, cmp_mode,
5924 op0, const0_rtx);
5925 case LTU:
5926 /* Canonicalize (LTU x 1) as (EQ x 0). */
5927 return simplify_gen_relational (EQ, mode, cmp_mode,
5928 op0, const0_rtx);
5929 default:
5930 break;
5933 else if (op1 == constm1_rtx)
5935 /* Canonicalize (LE x -1) as (LT x 0). */
5936 if (code == LE)
5937 return simplify_gen_relational (LT, mode, cmp_mode, op0, const0_rtx);
5938 /* Canonicalize (GT x -1) as (GE x 0). */
5939 if (code == GT)
5940 return simplify_gen_relational (GE, mode, cmp_mode, op0, const0_rtx);
5943 /* (eq/ne (plus x cst1) cst2) simplifies to (eq/ne x (cst2 - cst1)) */
5944 if ((code == EQ || code == NE)
5945 && (op0code == PLUS || op0code == MINUS)
5946 && CONSTANT_P (op1)
5947 && CONSTANT_P (XEXP (op0, 1))
5948 && (INTEGRAL_MODE_P (cmp_mode) || flag_unsafe_math_optimizations))
5950 rtx x = XEXP (op0, 0);
5951 rtx c = XEXP (op0, 1);
5952 enum rtx_code invcode = op0code == PLUS ? MINUS : PLUS;
5953 rtx tem = simplify_gen_binary (invcode, cmp_mode, op1, c);
5955 /* Detect an infinite recursive condition, where we oscillate at this
5956 simplification case between:
5957 A + B == C <---> C - B == A,
5958 where A, B, and C are all constants with non-simplifiable expressions,
5959 usually SYMBOL_REFs. */
5960 if (GET_CODE (tem) == invcode
5961 && CONSTANT_P (x)
5962 && rtx_equal_p (c, XEXP (tem, 1)))
5963 return NULL_RTX;
5965 return simplify_gen_relational (code, mode, cmp_mode, x, tem);
5968 /* (ne:SI (zero_extract:SI FOO (const_int 1) BAR) (const_int 0))) is
5969 the same as (zero_extract:SI FOO (const_int 1) BAR). */
5970 scalar_int_mode int_mode, int_cmp_mode;
5971 if (code == NE
5972 && op1 == const0_rtx
5973 && is_int_mode (mode, &int_mode)
5974 && is_a <scalar_int_mode> (cmp_mode, &int_cmp_mode)
5975 /* ??? Work-around BImode bugs in the ia64 backend. */
5976 && int_mode != BImode
5977 && int_cmp_mode != BImode
5978 && nonzero_bits (op0, int_cmp_mode) == 1
5979 && STORE_FLAG_VALUE == 1)
5980 return GET_MODE_SIZE (int_mode) > GET_MODE_SIZE (int_cmp_mode)
5981 ? simplify_gen_unary (ZERO_EXTEND, int_mode, op0, int_cmp_mode)
5982 : lowpart_subreg (int_mode, op0, int_cmp_mode);
5984 /* (eq/ne (xor x y) 0) simplifies to (eq/ne x y). */
5985 if ((code == EQ || code == NE)
5986 && op1 == const0_rtx
5987 && op0code == XOR)
5988 return simplify_gen_relational (code, mode, cmp_mode,
5989 XEXP (op0, 0), XEXP (op0, 1));
5991 /* (eq/ne (xor x y) x) simplifies to (eq/ne y 0). */
5992 if ((code == EQ || code == NE)
5993 && op0code == XOR
5994 && rtx_equal_p (XEXP (op0, 0), op1)
5995 && !side_effects_p (XEXP (op0, 0)))
5996 return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 1),
5997 CONST0_RTX (mode));
5999 /* Likewise (eq/ne (xor x y) y) simplifies to (eq/ne x 0). */
6000 if ((code == EQ || code == NE)
6001 && op0code == XOR
6002 && rtx_equal_p (XEXP (op0, 1), op1)
6003 && !side_effects_p (XEXP (op0, 1)))
6004 return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6005 CONST0_RTX (mode));
6007 /* (eq/ne (xor x C1) C2) simplifies to (eq/ne x (C1^C2)). */
6008 if ((code == EQ || code == NE)
6009 && op0code == XOR
6010 && CONST_SCALAR_INT_P (op1)
6011 && CONST_SCALAR_INT_P (XEXP (op0, 1)))
6012 return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6013 simplify_gen_binary (XOR, cmp_mode,
6014 XEXP (op0, 1), op1));
6016 /* Simplify eq/ne (and/ior x y) x/y) for targets with a BICS instruction or
6017 constant folding if x/y is a constant. */
6018 if ((code == EQ || code == NE)
6019 && (op0code == AND || op0code == IOR)
6020 && !side_effects_p (op1)
6021 && op1 != CONST0_RTX (cmp_mode))
6023 /* Both (eq/ne (and x y) x) and (eq/ne (ior x y) y) simplify to
6024 (eq/ne (and (not y) x) 0). */
6025 if ((op0code == AND && rtx_equal_p (XEXP (op0, 0), op1))
6026 || (op0code == IOR && rtx_equal_p (XEXP (op0, 1), op1)))
6028 rtx not_y = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 1),
6029 cmp_mode);
6030 rtx lhs = simplify_gen_binary (AND, cmp_mode, not_y, XEXP (op0, 0));
6032 return simplify_gen_relational (code, mode, cmp_mode, lhs,
6033 CONST0_RTX (cmp_mode));
6036 /* Both (eq/ne (and x y) y) and (eq/ne (ior x y) x) simplify to
6037 (eq/ne (and (not x) y) 0). */
6038 if ((op0code == AND && rtx_equal_p (XEXP (op0, 1), op1))
6039 || (op0code == IOR && rtx_equal_p (XEXP (op0, 0), op1)))
6041 rtx not_x = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 0),
6042 cmp_mode);
6043 rtx lhs = simplify_gen_binary (AND, cmp_mode, not_x, XEXP (op0, 1));
6045 return simplify_gen_relational (code, mode, cmp_mode, lhs,
6046 CONST0_RTX (cmp_mode));
6050 /* (eq/ne (bswap x) C1) simplifies to (eq/ne x C2) with C2 swapped. */
6051 if ((code == EQ || code == NE)
6052 && GET_CODE (op0) == BSWAP
6053 && CONST_SCALAR_INT_P (op1))
6054 return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6055 simplify_gen_unary (BSWAP, cmp_mode,
6056 op1, cmp_mode));
6058 /* (eq/ne (bswap x) (bswap y)) simplifies to (eq/ne x y). */
6059 if ((code == EQ || code == NE)
6060 && GET_CODE (op0) == BSWAP
6061 && GET_CODE (op1) == BSWAP)
6062 return simplify_gen_relational (code, mode, cmp_mode,
6063 XEXP (op0, 0), XEXP (op1, 0));
6065 if (op0code == POPCOUNT && op1 == const0_rtx)
6066 switch (code)
6068 case EQ:
6069 case LE:
6070 case LEU:
6071 /* (eq (popcount x) (const_int 0)) -> (eq x (const_int 0)). */
6072 return simplify_gen_relational (EQ, mode, GET_MODE (XEXP (op0, 0)),
6073 XEXP (op0, 0), const0_rtx);
6075 case NE:
6076 case GT:
6077 case GTU:
6078 /* (ne (popcount x) (const_int 0)) -> (ne x (const_int 0)). */
6079 return simplify_gen_relational (NE, mode, GET_MODE (XEXP (op0, 0)),
6080 XEXP (op0, 0), const0_rtx);
6082 default:
6083 break;
6086 return NULL_RTX;
6089 enum
6091 CMP_EQ = 1,
6092 CMP_LT = 2,
6093 CMP_GT = 4,
6094 CMP_LTU = 8,
6095 CMP_GTU = 16
6099 /* Convert the known results for EQ, LT, GT, LTU, GTU contained in
6100 KNOWN_RESULT to a CONST_INT, based on the requested comparison CODE
6101 For KNOWN_RESULT to make sense it should be either CMP_EQ, or the
6102 logical OR of one of (CMP_LT, CMP_GT) and one of (CMP_LTU, CMP_GTU).
6103 For floating-point comparisons, assume that the operands were ordered. */
6105 static rtx
6106 comparison_result (enum rtx_code code, int known_results)
6108 switch (code)
6110 case EQ:
6111 case UNEQ:
6112 return (known_results & CMP_EQ) ? const_true_rtx : const0_rtx;
6113 case NE:
6114 case LTGT:
6115 return (known_results & CMP_EQ) ? const0_rtx : const_true_rtx;
6117 case LT:
6118 case UNLT:
6119 return (known_results & CMP_LT) ? const_true_rtx : const0_rtx;
6120 case GE:
6121 case UNGE:
6122 return (known_results & CMP_LT) ? const0_rtx : const_true_rtx;
6124 case GT:
6125 case UNGT:
6126 return (known_results & CMP_GT) ? const_true_rtx : const0_rtx;
6127 case LE:
6128 case UNLE:
6129 return (known_results & CMP_GT) ? const0_rtx : const_true_rtx;
6131 case LTU:
6132 return (known_results & CMP_LTU) ? const_true_rtx : const0_rtx;
6133 case GEU:
6134 return (known_results & CMP_LTU) ? const0_rtx : const_true_rtx;
6136 case GTU:
6137 return (known_results & CMP_GTU) ? const_true_rtx : const0_rtx;
6138 case LEU:
6139 return (known_results & CMP_GTU) ? const0_rtx : const_true_rtx;
6141 case ORDERED:
6142 return const_true_rtx;
6143 case UNORDERED:
6144 return const0_rtx;
6145 default:
6146 gcc_unreachable ();
6150 /* Check if the given comparison (done in the given MODE) is actually
6151 a tautology or a contradiction. If the mode is VOIDmode, the
6152 comparison is done in "infinite precision". If no simplification
6153 is possible, this function returns zero. Otherwise, it returns
6154 either const_true_rtx or const0_rtx. */
6157 simplify_const_relational_operation (enum rtx_code code,
6158 machine_mode mode,
6159 rtx op0, rtx op1)
6161 rtx tem;
6162 rtx trueop0;
6163 rtx trueop1;
6165 gcc_assert (mode != VOIDmode
6166 || (GET_MODE (op0) == VOIDmode
6167 && GET_MODE (op1) == VOIDmode));
6169 /* We only handle MODE_CC comparisons that are COMPARE against zero. */
6170 if (GET_MODE_CLASS (mode) == MODE_CC
6171 && (op1 != const0_rtx
6172 || GET_CODE (op0) != COMPARE))
6173 return NULL_RTX;
6175 /* If op0 is a compare, extract the comparison arguments from it. */
6176 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6178 op1 = XEXP (op0, 1);
6179 op0 = XEXP (op0, 0);
6181 if (GET_MODE (op0) != VOIDmode)
6182 mode = GET_MODE (op0);
6183 else if (GET_MODE (op1) != VOIDmode)
6184 mode = GET_MODE (op1);
6185 else
6186 return 0;
6189 /* We can't simplify MODE_CC values since we don't know what the
6190 actual comparison is. */
6191 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6192 return 0;
6194 /* Make sure the constant is second. */
6195 if (swap_commutative_operands_p (op0, op1))
6197 std::swap (op0, op1);
6198 code = swap_condition (code);
6201 trueop0 = avoid_constant_pool_reference (op0);
6202 trueop1 = avoid_constant_pool_reference (op1);
6204 /* For integer comparisons of A and B maybe we can simplify A - B and can
6205 then simplify a comparison of that with zero. If A and B are both either
6206 a register or a CONST_INT, this can't help; testing for these cases will
6207 prevent infinite recursion here and speed things up.
6209 We can only do this for EQ and NE comparisons as otherwise we may
6210 lose or introduce overflow which we cannot disregard as undefined as
6211 we do not know the signedness of the operation on either the left or
6212 the right hand side of the comparison. */
6214 if (INTEGRAL_MODE_P (mode) && trueop1 != const0_rtx
6215 && (code == EQ || code == NE)
6216 && ! ((REG_P (op0) || CONST_INT_P (trueop0))
6217 && (REG_P (op1) || CONST_INT_P (trueop1)))
6218 && (tem = simplify_binary_operation (MINUS, mode, op0, op1)) != 0
6219 /* We cannot do this if tem is a nonzero address. */
6220 && ! nonzero_address_p (tem))
6221 return simplify_const_relational_operation (signed_condition (code),
6222 mode, tem, const0_rtx);
6224 if (! HONOR_NANS (mode) && code == ORDERED)
6225 return const_true_rtx;
6227 if (! HONOR_NANS (mode) && code == UNORDERED)
6228 return const0_rtx;
6230 /* For modes without NaNs, if the two operands are equal, we know the
6231 result except if they have side-effects. Even with NaNs we know
6232 the result of unordered comparisons and, if signaling NaNs are
6233 irrelevant, also the result of LT/GT/LTGT. */
6234 if ((! HONOR_NANS (trueop0)
6235 || code == UNEQ || code == UNLE || code == UNGE
6236 || ((code == LT || code == GT || code == LTGT)
6237 && ! HONOR_SNANS (trueop0)))
6238 && rtx_equal_p (trueop0, trueop1)
6239 && ! side_effects_p (trueop0))
6240 return comparison_result (code, CMP_EQ);
6242 /* If the operands are floating-point constants, see if we can fold
6243 the result. */
6244 if (CONST_DOUBLE_AS_FLOAT_P (trueop0)
6245 && CONST_DOUBLE_AS_FLOAT_P (trueop1)
6246 && SCALAR_FLOAT_MODE_P (GET_MODE (trueop0)))
6248 const REAL_VALUE_TYPE *d0 = CONST_DOUBLE_REAL_VALUE (trueop0);
6249 const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
6251 /* Comparisons are unordered iff at least one of the values is NaN. */
6252 if (REAL_VALUE_ISNAN (*d0) || REAL_VALUE_ISNAN (*d1))
6253 switch (code)
6255 case UNEQ:
6256 case UNLT:
6257 case UNGT:
6258 case UNLE:
6259 case UNGE:
6260 case NE:
6261 case UNORDERED:
6262 return const_true_rtx;
6263 case EQ:
6264 case LT:
6265 case GT:
6266 case LE:
6267 case GE:
6268 case LTGT:
6269 case ORDERED:
6270 return const0_rtx;
6271 default:
6272 return 0;
6275 return comparison_result (code,
6276 (real_equal (d0, d1) ? CMP_EQ :
6277 real_less (d0, d1) ? CMP_LT : CMP_GT));
6280 /* Otherwise, see if the operands are both integers. */
6281 if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
6282 && CONST_SCALAR_INT_P (trueop0) && CONST_SCALAR_INT_P (trueop1))
6284 /* It would be nice if we really had a mode here. However, the
6285 largest int representable on the target is as good as
6286 infinite. */
6287 machine_mode cmode = (mode == VOIDmode) ? MAX_MODE_INT : mode;
6288 rtx_mode_t ptrueop0 = rtx_mode_t (trueop0, cmode);
6289 rtx_mode_t ptrueop1 = rtx_mode_t (trueop1, cmode);
6291 if (wi::eq_p (ptrueop0, ptrueop1))
6292 return comparison_result (code, CMP_EQ);
6293 else
6295 int cr = wi::lts_p (ptrueop0, ptrueop1) ? CMP_LT : CMP_GT;
6296 cr |= wi::ltu_p (ptrueop0, ptrueop1) ? CMP_LTU : CMP_GTU;
6297 return comparison_result (code, cr);
6301 /* Optimize comparisons with upper and lower bounds. */
6302 scalar_int_mode int_mode;
6303 if (CONST_INT_P (trueop1)
6304 && is_a <scalar_int_mode> (mode, &int_mode)
6305 && HWI_COMPUTABLE_MODE_P (int_mode)
6306 && !side_effects_p (trueop0))
6308 int sign;
6309 unsigned HOST_WIDE_INT nonzero = nonzero_bits (trueop0, int_mode);
6310 HOST_WIDE_INT val = INTVAL (trueop1);
6311 HOST_WIDE_INT mmin, mmax;
6313 if (code == GEU
6314 || code == LEU
6315 || code == GTU
6316 || code == LTU)
6317 sign = 0;
6318 else
6319 sign = 1;
6321 /* Get a reduced range if the sign bit is zero. */
6322 if (nonzero <= (GET_MODE_MASK (int_mode) >> 1))
6324 mmin = 0;
6325 mmax = nonzero;
6327 else
6329 rtx mmin_rtx, mmax_rtx;
6330 get_mode_bounds (int_mode, sign, int_mode, &mmin_rtx, &mmax_rtx);
6332 mmin = INTVAL (mmin_rtx);
6333 mmax = INTVAL (mmax_rtx);
6334 if (sign)
6336 unsigned int sign_copies
6337 = num_sign_bit_copies (trueop0, int_mode);
6339 mmin >>= (sign_copies - 1);
6340 mmax >>= (sign_copies - 1);
6344 switch (code)
6346 /* x >= y is always true for y <= mmin, always false for y > mmax. */
6347 case GEU:
6348 if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
6349 return const_true_rtx;
6350 if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
6351 return const0_rtx;
6352 break;
6353 case GE:
6354 if (val <= mmin)
6355 return const_true_rtx;
6356 if (val > mmax)
6357 return const0_rtx;
6358 break;
6360 /* x <= y is always true for y >= mmax, always false for y < mmin. */
6361 case LEU:
6362 if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
6363 return const_true_rtx;
6364 if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
6365 return const0_rtx;
6366 break;
6367 case LE:
6368 if (val >= mmax)
6369 return const_true_rtx;
6370 if (val < mmin)
6371 return const0_rtx;
6372 break;
6374 case EQ:
6375 /* x == y is always false for y out of range. */
6376 if (val < mmin || val > mmax)
6377 return const0_rtx;
6378 break;
6380 /* x > y is always false for y >= mmax, always true for y < mmin. */
6381 case GTU:
6382 if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
6383 return const0_rtx;
6384 if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
6385 return const_true_rtx;
6386 break;
6387 case GT:
6388 if (val >= mmax)
6389 return const0_rtx;
6390 if (val < mmin)
6391 return const_true_rtx;
6392 break;
6394 /* x < y is always false for y <= mmin, always true for y > mmax. */
6395 case LTU:
6396 if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
6397 return const0_rtx;
6398 if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
6399 return const_true_rtx;
6400 break;
6401 case LT:
6402 if (val <= mmin)
6403 return const0_rtx;
6404 if (val > mmax)
6405 return const_true_rtx;
6406 break;
6408 case NE:
6409 /* x != y is always true for y out of range. */
6410 if (val < mmin || val > mmax)
6411 return const_true_rtx;
6412 break;
6414 default:
6415 break;
6419 /* Optimize integer comparisons with zero. */
6420 if (is_a <scalar_int_mode> (mode, &int_mode)
6421 && trueop1 == const0_rtx
6422 && !side_effects_p (trueop0))
6424 /* Some addresses are known to be nonzero. We don't know
6425 their sign, but equality comparisons are known. */
6426 if (nonzero_address_p (trueop0))
6428 if (code == EQ || code == LEU)
6429 return const0_rtx;
6430 if (code == NE || code == GTU)
6431 return const_true_rtx;
6434 /* See if the first operand is an IOR with a constant. If so, we
6435 may be able to determine the result of this comparison. */
6436 if (GET_CODE (op0) == IOR)
6438 rtx inner_const = avoid_constant_pool_reference (XEXP (op0, 1));
6439 if (CONST_INT_P (inner_const) && inner_const != const0_rtx)
6441 int sign_bitnum = GET_MODE_PRECISION (int_mode) - 1;
6442 int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum
6443 && (UINTVAL (inner_const)
6444 & (HOST_WIDE_INT_1U
6445 << sign_bitnum)));
6447 switch (code)
6449 case EQ:
6450 case LEU:
6451 return const0_rtx;
6452 case NE:
6453 case GTU:
6454 return const_true_rtx;
6455 case LT:
6456 case LE:
6457 if (has_sign)
6458 return const_true_rtx;
6459 break;
6460 case GT:
6461 case GE:
6462 if (has_sign)
6463 return const0_rtx;
6464 break;
6465 default:
6466 break;
6472 /* Optimize comparison of ABS with zero. */
6473 if (trueop1 == CONST0_RTX (mode) && !side_effects_p (trueop0)
6474 && (GET_CODE (trueop0) == ABS
6475 || (GET_CODE (trueop0) == FLOAT_EXTEND
6476 && GET_CODE (XEXP (trueop0, 0)) == ABS)))
6478 switch (code)
6480 case LT:
6481 /* Optimize abs(x) < 0.0. */
6482 if (!INTEGRAL_MODE_P (mode) && !HONOR_SNANS (mode))
6483 return const0_rtx;
6484 break;
6486 case GE:
6487 /* Optimize abs(x) >= 0.0. */
6488 if (!INTEGRAL_MODE_P (mode) && !HONOR_NANS (mode))
6489 return const_true_rtx;
6490 break;
6492 case UNGE:
6493 /* Optimize ! (abs(x) < 0.0). */
6494 return const_true_rtx;
6496 default:
6497 break;
6501 return 0;
6504 /* Recognize expressions of the form (X CMP 0) ? VAL : OP (X)
6505 where OP is CLZ or CTZ and VAL is the value from CLZ_DEFINED_VALUE_AT_ZERO
6506 or CTZ_DEFINED_VALUE_AT_ZERO respectively and return OP (X) if the expression
6507 can be simplified to that or NULL_RTX if not.
6508 Assume X is compared against zero with CMP_CODE and the true
6509 arm is TRUE_VAL and the false arm is FALSE_VAL. */
6512 simplify_context::simplify_cond_clz_ctz (rtx x, rtx_code cmp_code,
6513 rtx true_val, rtx false_val)
6515 if (cmp_code != EQ && cmp_code != NE)
6516 return NULL_RTX;
6518 /* Result on X == 0 and X !=0 respectively. */
6519 rtx on_zero, on_nonzero;
6520 if (cmp_code == EQ)
6522 on_zero = true_val;
6523 on_nonzero = false_val;
6525 else
6527 on_zero = false_val;
6528 on_nonzero = true_val;
6531 rtx_code op_code = GET_CODE (on_nonzero);
6532 if ((op_code != CLZ && op_code != CTZ)
6533 || !rtx_equal_p (XEXP (on_nonzero, 0), x)
6534 || !CONST_INT_P (on_zero))
6535 return NULL_RTX;
6537 HOST_WIDE_INT op_val;
6538 scalar_int_mode mode ATTRIBUTE_UNUSED
6539 = as_a <scalar_int_mode> (GET_MODE (XEXP (on_nonzero, 0)));
6540 if (((op_code == CLZ && CLZ_DEFINED_VALUE_AT_ZERO (mode, op_val))
6541 || (op_code == CTZ && CTZ_DEFINED_VALUE_AT_ZERO (mode, op_val)))
6542 && op_val == INTVAL (on_zero))
6543 return on_nonzero;
6545 return NULL_RTX;
6548 /* Try to simplify X given that it appears within operand OP of a
6549 VEC_MERGE operation whose mask is MASK. X need not use the same
6550 vector mode as the VEC_MERGE, but it must have the same number of
6551 elements.
6553 Return the simplified X on success, otherwise return NULL_RTX. */
6556 simplify_context::simplify_merge_mask (rtx x, rtx mask, int op)
6558 gcc_assert (VECTOR_MODE_P (GET_MODE (x)));
6559 poly_uint64 nunits = GET_MODE_NUNITS (GET_MODE (x));
6560 if (GET_CODE (x) == VEC_MERGE && rtx_equal_p (XEXP (x, 2), mask))
6562 if (side_effects_p (XEXP (x, 1 - op)))
6563 return NULL_RTX;
6565 return XEXP (x, op);
6567 if (UNARY_P (x)
6568 && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
6569 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits))
6571 rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
6572 if (top0)
6573 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), top0,
6574 GET_MODE (XEXP (x, 0)));
6576 if (BINARY_P (x)
6577 && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
6578 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
6579 && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
6580 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits))
6582 rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
6583 rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
6584 if (top0 || top1)
6586 if (COMPARISON_P (x))
6587 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
6588 GET_MODE (XEXP (x, 0)) != VOIDmode
6589 ? GET_MODE (XEXP (x, 0))
6590 : GET_MODE (XEXP (x, 1)),
6591 top0 ? top0 : XEXP (x, 0),
6592 top1 ? top1 : XEXP (x, 1));
6593 else
6594 return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
6595 top0 ? top0 : XEXP (x, 0),
6596 top1 ? top1 : XEXP (x, 1));
6599 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_TERNARY
6600 && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
6601 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
6602 && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
6603 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits)
6604 && VECTOR_MODE_P (GET_MODE (XEXP (x, 2)))
6605 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 2))), nunits))
6607 rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
6608 rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
6609 rtx top2 = simplify_merge_mask (XEXP (x, 2), mask, op);
6610 if (top0 || top1 || top2)
6611 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
6612 GET_MODE (XEXP (x, 0)),
6613 top0 ? top0 : XEXP (x, 0),
6614 top1 ? top1 : XEXP (x, 1),
6615 top2 ? top2 : XEXP (x, 2));
6617 return NULL_RTX;
6621 /* Simplify CODE, an operation with result mode MODE and three operands,
6622 OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
6623 a constant. Return 0 if no simplifications is possible. */
6626 simplify_context::simplify_ternary_operation (rtx_code code, machine_mode mode,
6627 machine_mode op0_mode,
6628 rtx op0, rtx op1, rtx op2)
6630 bool any_change = false;
6631 rtx tem, trueop2;
6632 scalar_int_mode int_mode, int_op0_mode;
6633 unsigned int n_elts;
6635 switch (code)
6637 case FMA:
6638 /* Simplify negations around the multiplication. */
6639 /* -a * -b + c => a * b + c. */
6640 if (GET_CODE (op0) == NEG)
6642 tem = simplify_unary_operation (NEG, mode, op1, mode);
6643 if (tem)
6644 op1 = tem, op0 = XEXP (op0, 0), any_change = true;
6646 else if (GET_CODE (op1) == NEG)
6648 tem = simplify_unary_operation (NEG, mode, op0, mode);
6649 if (tem)
6650 op0 = tem, op1 = XEXP (op1, 0), any_change = true;
6653 /* Canonicalize the two multiplication operands. */
6654 /* a * -b + c => -b * a + c. */
6655 if (swap_commutative_operands_p (op0, op1))
6656 std::swap (op0, op1), any_change = true;
6658 if (any_change)
6659 return gen_rtx_FMA (mode, op0, op1, op2);
6660 return NULL_RTX;
6662 case SIGN_EXTRACT:
6663 case ZERO_EXTRACT:
6664 if (CONST_INT_P (op0)
6665 && CONST_INT_P (op1)
6666 && CONST_INT_P (op2)
6667 && is_a <scalar_int_mode> (mode, &int_mode)
6668 && INTVAL (op1) + INTVAL (op2) <= GET_MODE_PRECISION (int_mode)
6669 && HWI_COMPUTABLE_MODE_P (int_mode))
6671 /* Extracting a bit-field from a constant */
6672 unsigned HOST_WIDE_INT val = UINTVAL (op0);
6673 HOST_WIDE_INT op1val = INTVAL (op1);
6674 HOST_WIDE_INT op2val = INTVAL (op2);
6675 if (!BITS_BIG_ENDIAN)
6676 val >>= op2val;
6677 else if (is_a <scalar_int_mode> (op0_mode, &int_op0_mode))
6678 val >>= GET_MODE_PRECISION (int_op0_mode) - op2val - op1val;
6679 else
6680 /* Not enough information to calculate the bit position. */
6681 break;
6683 if (HOST_BITS_PER_WIDE_INT != op1val)
6685 /* First zero-extend. */
6686 val &= (HOST_WIDE_INT_1U << op1val) - 1;
6687 /* If desired, propagate sign bit. */
6688 if (code == SIGN_EXTRACT
6689 && (val & (HOST_WIDE_INT_1U << (op1val - 1)))
6690 != 0)
6691 val |= ~ ((HOST_WIDE_INT_1U << op1val) - 1);
6694 return gen_int_mode (val, int_mode);
6696 break;
6698 case IF_THEN_ELSE:
6699 if (CONST_INT_P (op0))
6700 return op0 != const0_rtx ? op1 : op2;
6702 /* Convert c ? a : a into "a". */
6703 if (rtx_equal_p (op1, op2) && ! side_effects_p (op0))
6704 return op1;
6706 /* Convert a != b ? a : b into "a". */
6707 if (GET_CODE (op0) == NE
6708 && ! side_effects_p (op0)
6709 && ! HONOR_NANS (mode)
6710 && ! HONOR_SIGNED_ZEROS (mode)
6711 && ((rtx_equal_p (XEXP (op0, 0), op1)
6712 && rtx_equal_p (XEXP (op0, 1), op2))
6713 || (rtx_equal_p (XEXP (op0, 0), op2)
6714 && rtx_equal_p (XEXP (op0, 1), op1))))
6715 return op1;
6717 /* Convert a == b ? a : b into "b". */
6718 if (GET_CODE (op0) == EQ
6719 && ! side_effects_p (op0)
6720 && ! HONOR_NANS (mode)
6721 && ! HONOR_SIGNED_ZEROS (mode)
6722 && ((rtx_equal_p (XEXP (op0, 0), op1)
6723 && rtx_equal_p (XEXP (op0, 1), op2))
6724 || (rtx_equal_p (XEXP (op0, 0), op2)
6725 && rtx_equal_p (XEXP (op0, 1), op1))))
6726 return op2;
6728 /* Convert (!c) != {0,...,0} ? a : b into
6729 c != {0,...,0} ? b : a for vector modes. */
6730 if (VECTOR_MODE_P (GET_MODE (op1))
6731 && GET_CODE (op0) == NE
6732 && GET_CODE (XEXP (op0, 0)) == NOT
6733 && GET_CODE (XEXP (op0, 1)) == CONST_VECTOR)
6735 rtx cv = XEXP (op0, 1);
6736 int nunits;
6737 bool ok = true;
6738 if (!CONST_VECTOR_NUNITS (cv).is_constant (&nunits))
6739 ok = false;
6740 else
6741 for (int i = 0; i < nunits; ++i)
6742 if (CONST_VECTOR_ELT (cv, i) != const0_rtx)
6744 ok = false;
6745 break;
6747 if (ok)
6749 rtx new_op0 = gen_rtx_NE (GET_MODE (op0),
6750 XEXP (XEXP (op0, 0), 0),
6751 XEXP (op0, 1));
6752 rtx retval = gen_rtx_IF_THEN_ELSE (mode, new_op0, op2, op1);
6753 return retval;
6757 /* Convert x == 0 ? N : clz (x) into clz (x) when
6758 CLZ_DEFINED_VALUE_AT_ZERO is defined to N for the mode of x.
6759 Similarly for ctz (x). */
6760 if (COMPARISON_P (op0) && !side_effects_p (op0)
6761 && XEXP (op0, 1) == const0_rtx)
6763 rtx simplified
6764 = simplify_cond_clz_ctz (XEXP (op0, 0), GET_CODE (op0),
6765 op1, op2);
6766 if (simplified)
6767 return simplified;
6770 if (COMPARISON_P (op0) && ! side_effects_p (op0))
6772 machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
6773 ? GET_MODE (XEXP (op0, 1))
6774 : GET_MODE (XEXP (op0, 0)));
6775 rtx temp;
6777 /* Look for happy constants in op1 and op2. */
6778 if (CONST_INT_P (op1) && CONST_INT_P (op2))
6780 HOST_WIDE_INT t = INTVAL (op1);
6781 HOST_WIDE_INT f = INTVAL (op2);
6783 if (t == STORE_FLAG_VALUE && f == 0)
6784 code = GET_CODE (op0);
6785 else if (t == 0 && f == STORE_FLAG_VALUE)
6787 enum rtx_code tmp;
6788 tmp = reversed_comparison_code (op0, NULL);
6789 if (tmp == UNKNOWN)
6790 break;
6791 code = tmp;
6793 else
6794 break;
6796 return simplify_gen_relational (code, mode, cmp_mode,
6797 XEXP (op0, 0), XEXP (op0, 1));
6800 temp = simplify_relational_operation (GET_CODE (op0), op0_mode,
6801 cmp_mode, XEXP (op0, 0),
6802 XEXP (op0, 1));
6804 /* See if any simplifications were possible. */
6805 if (temp)
6807 if (CONST_INT_P (temp))
6808 return temp == const0_rtx ? op2 : op1;
6809 else if (temp)
6810 return gen_rtx_IF_THEN_ELSE (mode, temp, op1, op2);
6813 break;
6815 case VEC_MERGE:
6816 gcc_assert (GET_MODE (op0) == mode);
6817 gcc_assert (GET_MODE (op1) == mode);
6818 gcc_assert (VECTOR_MODE_P (mode));
6819 trueop2 = avoid_constant_pool_reference (op2);
6820 if (CONST_INT_P (trueop2)
6821 && GET_MODE_NUNITS (mode).is_constant (&n_elts))
6823 unsigned HOST_WIDE_INT sel = UINTVAL (trueop2);
6824 unsigned HOST_WIDE_INT mask;
6825 if (n_elts == HOST_BITS_PER_WIDE_INT)
6826 mask = -1;
6827 else
6828 mask = (HOST_WIDE_INT_1U << n_elts) - 1;
6830 if (!(sel & mask) && !side_effects_p (op0))
6831 return op1;
6832 if ((sel & mask) == mask && !side_effects_p (op1))
6833 return op0;
6835 rtx trueop0 = avoid_constant_pool_reference (op0);
6836 rtx trueop1 = avoid_constant_pool_reference (op1);
6837 if (GET_CODE (trueop0) == CONST_VECTOR
6838 && GET_CODE (trueop1) == CONST_VECTOR)
6840 rtvec v = rtvec_alloc (n_elts);
6841 unsigned int i;
6843 for (i = 0; i < n_elts; i++)
6844 RTVEC_ELT (v, i) = ((sel & (HOST_WIDE_INT_1U << i))
6845 ? CONST_VECTOR_ELT (trueop0, i)
6846 : CONST_VECTOR_ELT (trueop1, i));
6847 return gen_rtx_CONST_VECTOR (mode, v);
6850 /* Replace (vec_merge (vec_merge a b m) c n) with (vec_merge b c n)
6851 if no element from a appears in the result. */
6852 if (GET_CODE (op0) == VEC_MERGE)
6854 tem = avoid_constant_pool_reference (XEXP (op0, 2));
6855 if (CONST_INT_P (tem))
6857 unsigned HOST_WIDE_INT sel0 = UINTVAL (tem);
6858 if (!(sel & sel0 & mask) && !side_effects_p (XEXP (op0, 0)))
6859 return simplify_gen_ternary (code, mode, mode,
6860 XEXP (op0, 1), op1, op2);
6861 if (!(sel & ~sel0 & mask) && !side_effects_p (XEXP (op0, 1)))
6862 return simplify_gen_ternary (code, mode, mode,
6863 XEXP (op0, 0), op1, op2);
6866 if (GET_CODE (op1) == VEC_MERGE)
6868 tem = avoid_constant_pool_reference (XEXP (op1, 2));
6869 if (CONST_INT_P (tem))
6871 unsigned HOST_WIDE_INT sel1 = UINTVAL (tem);
6872 if (!(~sel & sel1 & mask) && !side_effects_p (XEXP (op1, 0)))
6873 return simplify_gen_ternary (code, mode, mode,
6874 op0, XEXP (op1, 1), op2);
6875 if (!(~sel & ~sel1 & mask) && !side_effects_p (XEXP (op1, 1)))
6876 return simplify_gen_ternary (code, mode, mode,
6877 op0, XEXP (op1, 0), op2);
6881 /* Replace (vec_merge (vec_duplicate (vec_select a parallel (i))) a 1 << i)
6882 with a. */
6883 if (GET_CODE (op0) == VEC_DUPLICATE
6884 && GET_CODE (XEXP (op0, 0)) == VEC_SELECT
6885 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == PARALLEL
6886 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (op0, 0))), 1))
6888 tem = XVECEXP ((XEXP (XEXP (op0, 0), 1)), 0, 0);
6889 if (CONST_INT_P (tem) && CONST_INT_P (op2))
6891 if (XEXP (XEXP (op0, 0), 0) == op1
6892 && UINTVAL (op2) == HOST_WIDE_INT_1U << UINTVAL (tem))
6893 return op1;
6896 /* Replace (vec_merge (vec_duplicate (X)) (const_vector [A, B])
6897 (const_int N))
6898 with (vec_concat (X) (B)) if N == 1 or
6899 (vec_concat (A) (X)) if N == 2. */
6900 if (GET_CODE (op0) == VEC_DUPLICATE
6901 && GET_CODE (op1) == CONST_VECTOR
6902 && known_eq (CONST_VECTOR_NUNITS (op1), 2)
6903 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6904 && IN_RANGE (sel, 1, 2))
6906 rtx newop0 = XEXP (op0, 0);
6907 rtx newop1 = CONST_VECTOR_ELT (op1, 2 - sel);
6908 if (sel == 2)
6909 std::swap (newop0, newop1);
6910 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
6912 /* Replace (vec_merge (vec_duplicate x) (vec_concat (y) (z)) (const_int N))
6913 with (vec_concat x z) if N == 1, or (vec_concat y x) if N == 2.
6914 Only applies for vectors of two elements. */
6915 if (GET_CODE (op0) == VEC_DUPLICATE
6916 && GET_CODE (op1) == VEC_CONCAT
6917 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6918 && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
6919 && IN_RANGE (sel, 1, 2))
6921 rtx newop0 = XEXP (op0, 0);
6922 rtx newop1 = XEXP (op1, 2 - sel);
6923 rtx otherop = XEXP (op1, sel - 1);
6924 if (sel == 2)
6925 std::swap (newop0, newop1);
6926 /* Don't want to throw away the other part of the vec_concat if
6927 it has side-effects. */
6928 if (!side_effects_p (otherop))
6929 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
6932 /* Replace:
6934 (vec_merge:outer (vec_duplicate:outer x:inner)
6935 (subreg:outer y:inner 0)
6936 (const_int N))
6938 with (vec_concat:outer x:inner y:inner) if N == 1,
6939 or (vec_concat:outer y:inner x:inner) if N == 2.
6941 Implicitly, this means we have a paradoxical subreg, but such
6942 a check is cheap, so make it anyway.
6944 Only applies for vectors of two elements. */
6945 if (GET_CODE (op0) == VEC_DUPLICATE
6946 && GET_CODE (op1) == SUBREG
6947 && GET_MODE (op1) == GET_MODE (op0)
6948 && GET_MODE (SUBREG_REG (op1)) == GET_MODE (XEXP (op0, 0))
6949 && paradoxical_subreg_p (op1)
6950 && subreg_lowpart_p (op1)
6951 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6952 && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
6953 && IN_RANGE (sel, 1, 2))
6955 rtx newop0 = XEXP (op0, 0);
6956 rtx newop1 = SUBREG_REG (op1);
6957 if (sel == 2)
6958 std::swap (newop0, newop1);
6959 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
6962 /* Same as above but with switched operands:
6963 Replace (vec_merge:outer (subreg:outer x:inner 0)
6964 (vec_duplicate:outer y:inner)
6965 (const_int N))
6967 with (vec_concat:outer x:inner y:inner) if N == 1,
6968 or (vec_concat:outer y:inner x:inner) if N == 2. */
6969 if (GET_CODE (op1) == VEC_DUPLICATE
6970 && GET_CODE (op0) == SUBREG
6971 && GET_MODE (op0) == GET_MODE (op1)
6972 && GET_MODE (SUBREG_REG (op0)) == GET_MODE (XEXP (op1, 0))
6973 && paradoxical_subreg_p (op0)
6974 && subreg_lowpart_p (op0)
6975 && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
6976 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6977 && IN_RANGE (sel, 1, 2))
6979 rtx newop0 = SUBREG_REG (op0);
6980 rtx newop1 = XEXP (op1, 0);
6981 if (sel == 2)
6982 std::swap (newop0, newop1);
6983 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
6986 /* Replace (vec_merge (vec_duplicate x) (vec_duplicate y)
6987 (const_int n))
6988 with (vec_concat x y) or (vec_concat y x) depending on value
6989 of N. */
6990 if (GET_CODE (op0) == VEC_DUPLICATE
6991 && GET_CODE (op1) == VEC_DUPLICATE
6992 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6993 && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
6994 && IN_RANGE (sel, 1, 2))
6996 rtx newop0 = XEXP (op0, 0);
6997 rtx newop1 = XEXP (op1, 0);
6998 if (sel == 2)
6999 std::swap (newop0, newop1);
7001 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7005 if (rtx_equal_p (op0, op1)
7006 && !side_effects_p (op2) && !side_effects_p (op1))
7007 return op0;
7009 if (!side_effects_p (op2))
7011 rtx top0
7012 = may_trap_p (op0) ? NULL_RTX : simplify_merge_mask (op0, op2, 0);
7013 rtx top1
7014 = may_trap_p (op1) ? NULL_RTX : simplify_merge_mask (op1, op2, 1);
7015 if (top0 || top1)
7016 return simplify_gen_ternary (code, mode, mode,
7017 top0 ? top0 : op0,
7018 top1 ? top1 : op1, op2);
7021 break;
7023 default:
7024 gcc_unreachable ();
7027 return 0;
7030 /* Try to calculate NUM_BYTES bytes of the target memory image of X,
7031 starting at byte FIRST_BYTE. Return true on success and add the
7032 bytes to BYTES, such that each byte has BITS_PER_UNIT bits and such
7033 that the bytes follow target memory order. Leave BYTES unmodified
7034 on failure.
7036 MODE is the mode of X. The caller must reserve NUM_BYTES bytes in
7037 BYTES before calling this function. */
7039 bool
7040 native_encode_rtx (machine_mode mode, rtx x, vec<target_unit> &bytes,
7041 unsigned int first_byte, unsigned int num_bytes)
7043 /* Check the mode is sensible. */
7044 gcc_assert (GET_MODE (x) == VOIDmode
7045 ? is_a <scalar_int_mode> (mode)
7046 : mode == GET_MODE (x));
7048 if (GET_CODE (x) == CONST_VECTOR)
7050 /* CONST_VECTOR_ELT follows target memory order, so no shuffling
7051 is necessary. The only complication is that MODE_VECTOR_BOOL
7052 vectors can have several elements per byte. */
7053 unsigned int elt_bits = vector_element_size (GET_MODE_BITSIZE (mode),
7054 GET_MODE_NUNITS (mode));
7055 unsigned int elt = first_byte * BITS_PER_UNIT / elt_bits;
7056 if (elt_bits < BITS_PER_UNIT)
7058 /* This is the only case in which elements can be smaller than
7059 a byte. */
7060 gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
7061 auto mask = GET_MODE_MASK (GET_MODE_INNER (mode));
7062 for (unsigned int i = 0; i < num_bytes; ++i)
7064 target_unit value = 0;
7065 for (unsigned int j = 0; j < BITS_PER_UNIT; j += elt_bits)
7067 value |= (INTVAL (CONST_VECTOR_ELT (x, elt)) & mask) << j;
7068 elt += 1;
7070 bytes.quick_push (value);
7072 return true;
7075 unsigned int start = bytes.length ();
7076 unsigned int elt_bytes = GET_MODE_UNIT_SIZE (mode);
7077 /* Make FIRST_BYTE relative to ELT. */
7078 first_byte %= elt_bytes;
7079 while (num_bytes > 0)
7081 /* Work out how many bytes we want from element ELT. */
7082 unsigned int chunk_bytes = MIN (num_bytes, elt_bytes - first_byte);
7083 if (!native_encode_rtx (GET_MODE_INNER (mode),
7084 CONST_VECTOR_ELT (x, elt), bytes,
7085 first_byte, chunk_bytes))
7087 bytes.truncate (start);
7088 return false;
7090 elt += 1;
7091 first_byte = 0;
7092 num_bytes -= chunk_bytes;
7094 return true;
7097 /* All subsequent cases are limited to scalars. */
7098 scalar_mode smode;
7099 if (!is_a <scalar_mode> (mode, &smode))
7100 return false;
7102 /* Make sure that the region is in range. */
7103 unsigned int end_byte = first_byte + num_bytes;
7104 unsigned int mode_bytes = GET_MODE_SIZE (smode);
7105 gcc_assert (end_byte <= mode_bytes);
7107 if (CONST_SCALAR_INT_P (x))
7109 /* The target memory layout is affected by both BYTES_BIG_ENDIAN
7110 and WORDS_BIG_ENDIAN. Use the subreg machinery to get the lsb
7111 position of each byte. */
7112 rtx_mode_t value (x, smode);
7113 wide_int_ref value_wi (value);
7114 for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7116 /* Always constant because the inputs are. */
7117 unsigned int lsb
7118 = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7119 /* Operate directly on the encoding rather than using
7120 wi::extract_uhwi, so that we preserve the sign or zero
7121 extension for modes that are not a whole number of bits in
7122 size. (Zero extension is only used for the combination of
7123 innermode == BImode && STORE_FLAG_VALUE == 1). */
7124 unsigned int elt = lsb / HOST_BITS_PER_WIDE_INT;
7125 unsigned int shift = lsb % HOST_BITS_PER_WIDE_INT;
7126 unsigned HOST_WIDE_INT uhwi = value_wi.elt (elt);
7127 bytes.quick_push (uhwi >> shift);
7129 return true;
7132 if (CONST_DOUBLE_P (x))
7134 /* real_to_target produces an array of integers in target memory order.
7135 All integers before the last one have 32 bits; the last one may
7136 have 32 bits or fewer, depending on whether the mode bitsize
7137 is divisible by 32. Each of these integers is then laid out
7138 in target memory as any other integer would be. */
7139 long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
7140 real_to_target (el32, CONST_DOUBLE_REAL_VALUE (x), smode);
7142 /* The (maximum) number of target bytes per element of el32. */
7143 unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
7144 gcc_assert (bytes_per_el32 != 0);
7146 /* Build up the integers in a similar way to the CONST_SCALAR_INT_P
7147 handling above. */
7148 for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7150 unsigned int index = byte / bytes_per_el32;
7151 unsigned int subbyte = byte % bytes_per_el32;
7152 unsigned int int_bytes = MIN (bytes_per_el32,
7153 mode_bytes - index * bytes_per_el32);
7154 /* Always constant because the inputs are. */
7155 unsigned int lsb
7156 = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
7157 bytes.quick_push ((unsigned long) el32[index] >> lsb);
7159 return true;
7162 if (GET_CODE (x) == CONST_FIXED)
7164 for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7166 /* Always constant because the inputs are. */
7167 unsigned int lsb
7168 = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7169 unsigned HOST_WIDE_INT piece = CONST_FIXED_VALUE_LOW (x);
7170 if (lsb >= HOST_BITS_PER_WIDE_INT)
7172 lsb -= HOST_BITS_PER_WIDE_INT;
7173 piece = CONST_FIXED_VALUE_HIGH (x);
7175 bytes.quick_push (piece >> lsb);
7177 return true;
7180 return false;
7183 /* Read a vector of mode MODE from the target memory image given by BYTES,
7184 starting at byte FIRST_BYTE. The vector is known to be encodable using
7185 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each,
7186 and BYTES is known to have enough bytes to supply NPATTERNS *
7187 NELTS_PER_PATTERN vector elements. Each element of BYTES contains
7188 BITS_PER_UNIT bits and the bytes are in target memory order.
7190 Return the vector on success, otherwise return NULL_RTX. */
7193 native_decode_vector_rtx (machine_mode mode, const vec<target_unit> &bytes,
7194 unsigned int first_byte, unsigned int npatterns,
7195 unsigned int nelts_per_pattern)
7197 rtx_vector_builder builder (mode, npatterns, nelts_per_pattern);
7199 unsigned int elt_bits = vector_element_size (GET_MODE_BITSIZE (mode),
7200 GET_MODE_NUNITS (mode));
7201 if (elt_bits < BITS_PER_UNIT)
7203 /* This is the only case in which elements can be smaller than a byte.
7204 Element 0 is always in the lsb of the containing byte. */
7205 gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
7206 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
7208 unsigned int bit_index = first_byte * BITS_PER_UNIT + i * elt_bits;
7209 unsigned int byte_index = bit_index / BITS_PER_UNIT;
7210 unsigned int lsb = bit_index % BITS_PER_UNIT;
7211 unsigned int value = bytes[byte_index] >> lsb;
7212 builder.quick_push (gen_int_mode (value, GET_MODE_INNER (mode)));
7215 else
7217 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
7219 rtx x = native_decode_rtx (GET_MODE_INNER (mode), bytes, first_byte);
7220 if (!x)
7221 return NULL_RTX;
7222 builder.quick_push (x);
7223 first_byte += elt_bits / BITS_PER_UNIT;
7226 return builder.build ();
7229 /* Read an rtx of mode MODE from the target memory image given by BYTES,
7230 starting at byte FIRST_BYTE. Each element of BYTES contains BITS_PER_UNIT
7231 bits and the bytes are in target memory order. The image has enough
7232 values to specify all bytes of MODE.
7234 Return the rtx on success, otherwise return NULL_RTX. */
7237 native_decode_rtx (machine_mode mode, const vec<target_unit> &bytes,
7238 unsigned int first_byte)
7240 if (VECTOR_MODE_P (mode))
7242 /* If we know at compile time how many elements there are,
7243 pull each element directly from BYTES. */
7244 unsigned int nelts;
7245 if (GET_MODE_NUNITS (mode).is_constant (&nelts))
7246 return native_decode_vector_rtx (mode, bytes, first_byte, nelts, 1);
7247 return NULL_RTX;
7250 scalar_int_mode imode;
7251 if (is_a <scalar_int_mode> (mode, &imode)
7252 && GET_MODE_PRECISION (imode) <= MAX_BITSIZE_MODE_ANY_INT)
7254 /* Pull the bytes msb first, so that we can use simple
7255 shift-and-insert wide_int operations. */
7256 unsigned int size = GET_MODE_SIZE (imode);
7257 wide_int result (wi::zero (GET_MODE_PRECISION (imode)));
7258 for (unsigned int i = 0; i < size; ++i)
7260 unsigned int lsb = (size - i - 1) * BITS_PER_UNIT;
7261 /* Always constant because the inputs are. */
7262 unsigned int subbyte
7263 = subreg_size_offset_from_lsb (1, size, lsb).to_constant ();
7264 result <<= BITS_PER_UNIT;
7265 result |= bytes[first_byte + subbyte];
7267 return immed_wide_int_const (result, imode);
7270 scalar_float_mode fmode;
7271 if (is_a <scalar_float_mode> (mode, &fmode))
7273 /* We need to build an array of integers in target memory order.
7274 All integers before the last one have 32 bits; the last one may
7275 have 32 bits or fewer, depending on whether the mode bitsize
7276 is divisible by 32. */
7277 long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
7278 unsigned int num_el32 = CEIL (GET_MODE_BITSIZE (fmode), 32);
7279 memset (el32, 0, num_el32 * sizeof (long));
7281 /* The (maximum) number of target bytes per element of el32. */
7282 unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
7283 gcc_assert (bytes_per_el32 != 0);
7285 unsigned int mode_bytes = GET_MODE_SIZE (fmode);
7286 for (unsigned int byte = 0; byte < mode_bytes; ++byte)
7288 unsigned int index = byte / bytes_per_el32;
7289 unsigned int subbyte = byte % bytes_per_el32;
7290 unsigned int int_bytes = MIN (bytes_per_el32,
7291 mode_bytes - index * bytes_per_el32);
7292 /* Always constant because the inputs are. */
7293 unsigned int lsb
7294 = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
7295 el32[index] |= (unsigned long) bytes[first_byte + byte] << lsb;
7297 REAL_VALUE_TYPE r;
7298 real_from_target (&r, el32, fmode);
7299 return const_double_from_real_value (r, fmode);
7302 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
7304 scalar_mode smode = as_a <scalar_mode> (mode);
7305 FIXED_VALUE_TYPE f;
7306 f.data.low = 0;
7307 f.data.high = 0;
7308 f.mode = smode;
7310 unsigned int mode_bytes = GET_MODE_SIZE (smode);
7311 for (unsigned int byte = 0; byte < mode_bytes; ++byte)
7313 /* Always constant because the inputs are. */
7314 unsigned int lsb
7315 = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7316 unsigned HOST_WIDE_INT unit = bytes[first_byte + byte];
7317 if (lsb >= HOST_BITS_PER_WIDE_INT)
7318 f.data.high |= unit << (lsb - HOST_BITS_PER_WIDE_INT);
7319 else
7320 f.data.low |= unit << lsb;
7322 return CONST_FIXED_FROM_FIXED_VALUE (f, mode);
7325 return NULL_RTX;
7328 /* Simplify a byte offset BYTE into CONST_VECTOR X. The main purpose
7329 is to convert a runtime BYTE value into a constant one. */
7331 static poly_uint64
7332 simplify_const_vector_byte_offset (rtx x, poly_uint64 byte)
7334 /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
7335 machine_mode mode = GET_MODE (x);
7336 unsigned int elt_bits = vector_element_size (GET_MODE_BITSIZE (mode),
7337 GET_MODE_NUNITS (mode));
7338 /* The number of bits needed to encode one element from each pattern. */
7339 unsigned int sequence_bits = CONST_VECTOR_NPATTERNS (x) * elt_bits;
7341 /* Identify the start point in terms of a sequence number and a byte offset
7342 within that sequence. */
7343 poly_uint64 first_sequence;
7344 unsigned HOST_WIDE_INT subbit;
7345 if (can_div_trunc_p (byte * BITS_PER_UNIT, sequence_bits,
7346 &first_sequence, &subbit))
7348 unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
7349 if (nelts_per_pattern == 1)
7350 /* This is a duplicated vector, so the value of FIRST_SEQUENCE
7351 doesn't matter. */
7352 byte = subbit / BITS_PER_UNIT;
7353 else if (nelts_per_pattern == 2 && known_gt (first_sequence, 0U))
7355 /* The subreg drops the first element from each pattern and
7356 only uses the second element. Find the first sequence
7357 that starts on a byte boundary. */
7358 subbit += least_common_multiple (sequence_bits, BITS_PER_UNIT);
7359 byte = subbit / BITS_PER_UNIT;
7362 return byte;
7365 /* Subroutine of simplify_subreg in which:
7367 - X is known to be a CONST_VECTOR
7368 - OUTERMODE is known to be a vector mode
7370 Try to handle the subreg by operating on the CONST_VECTOR encoding
7371 rather than on each individual element of the CONST_VECTOR.
7373 Return the simplified subreg on success, otherwise return NULL_RTX. */
7375 static rtx
7376 simplify_const_vector_subreg (machine_mode outermode, rtx x,
7377 machine_mode innermode, unsigned int first_byte)
7379 /* Paradoxical subregs of vectors have dubious semantics. */
7380 if (paradoxical_subreg_p (outermode, innermode))
7381 return NULL_RTX;
7383 /* We can only preserve the semantics of a stepped pattern if the new
7384 vector element is the same as the original one. */
7385 if (CONST_VECTOR_STEPPED_P (x)
7386 && GET_MODE_INNER (outermode) != GET_MODE_INNER (innermode))
7387 return NULL_RTX;
7389 /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
7390 unsigned int x_elt_bits
7391 = vector_element_size (GET_MODE_BITSIZE (innermode),
7392 GET_MODE_NUNITS (innermode));
7393 unsigned int out_elt_bits
7394 = vector_element_size (GET_MODE_BITSIZE (outermode),
7395 GET_MODE_NUNITS (outermode));
7397 /* The number of bits needed to encode one element from every pattern
7398 of the original vector. */
7399 unsigned int x_sequence_bits = CONST_VECTOR_NPATTERNS (x) * x_elt_bits;
7401 /* The number of bits needed to encode one element from every pattern
7402 of the result. */
7403 unsigned int out_sequence_bits
7404 = least_common_multiple (x_sequence_bits, out_elt_bits);
7406 /* Work out the number of interleaved patterns in the output vector
7407 and the number of encoded elements per pattern. */
7408 unsigned int out_npatterns = out_sequence_bits / out_elt_bits;
7409 unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
7411 /* The encoding scheme requires the number of elements to be a multiple
7412 of the number of patterns, so that each pattern appears at least once
7413 and so that the same number of elements appear from each pattern. */
7414 bool ok_p = multiple_p (GET_MODE_NUNITS (outermode), out_npatterns);
7415 unsigned int const_nunits;
7416 if (GET_MODE_NUNITS (outermode).is_constant (&const_nunits)
7417 && (!ok_p || out_npatterns * nelts_per_pattern > const_nunits))
7419 /* Either the encoding is invalid, or applying it would give us
7420 more elements than we need. Just encode each element directly. */
7421 out_npatterns = const_nunits;
7422 nelts_per_pattern = 1;
7424 else if (!ok_p)
7425 return NULL_RTX;
7427 /* Get enough bytes of X to form the new encoding. */
7428 unsigned int buffer_bits = out_npatterns * nelts_per_pattern * out_elt_bits;
7429 unsigned int buffer_bytes = CEIL (buffer_bits, BITS_PER_UNIT);
7430 auto_vec<target_unit, 128> buffer (buffer_bytes);
7431 if (!native_encode_rtx (innermode, x, buffer, first_byte, buffer_bytes))
7432 return NULL_RTX;
7434 /* Reencode the bytes as OUTERMODE. */
7435 return native_decode_vector_rtx (outermode, buffer, 0, out_npatterns,
7436 nelts_per_pattern);
7439 /* Try to simplify a subreg of a constant by encoding the subreg region
7440 as a sequence of target bytes and reading them back in the new mode.
7441 Return the new value on success, otherwise return null.
7443 The subreg has outer mode OUTERMODE, inner mode INNERMODE, inner value X
7444 and byte offset FIRST_BYTE. */
7446 static rtx
7447 simplify_immed_subreg (fixed_size_mode outermode, rtx x,
7448 machine_mode innermode, unsigned int first_byte)
7450 unsigned int buffer_bytes = GET_MODE_SIZE (outermode);
7451 auto_vec<target_unit, 128> buffer (buffer_bytes);
7453 /* Some ports misuse CCmode. */
7454 if (GET_MODE_CLASS (outermode) == MODE_CC && CONST_INT_P (x))
7455 return x;
7457 /* Paradoxical subregs read undefined values for bytes outside of the
7458 inner value. However, we have traditionally always sign-extended
7459 integer constants and zero-extended others. */
7460 unsigned int inner_bytes = buffer_bytes;
7461 if (paradoxical_subreg_p (outermode, innermode))
7463 if (!GET_MODE_SIZE (innermode).is_constant (&inner_bytes))
7464 return NULL_RTX;
7466 target_unit filler = 0;
7467 if (CONST_SCALAR_INT_P (x) && wi::neg_p (rtx_mode_t (x, innermode)))
7468 filler = -1;
7470 /* Add any leading bytes due to big-endian layout. The number of
7471 bytes must be constant because both modes have constant size. */
7472 unsigned int leading_bytes
7473 = -byte_lowpart_offset (outermode, innermode).to_constant ();
7474 for (unsigned int i = 0; i < leading_bytes; ++i)
7475 buffer.quick_push (filler);
7477 if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
7478 return NULL_RTX;
7480 /* Add any trailing bytes due to little-endian layout. */
7481 while (buffer.length () < buffer_bytes)
7482 buffer.quick_push (filler);
7484 else if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
7485 return NULL_RTX;
7486 rtx ret = native_decode_rtx (outermode, buffer, 0);
7487 if (ret && FLOAT_MODE_P (outermode))
7489 auto_vec<target_unit, 128> buffer2 (buffer_bytes);
7490 if (!native_encode_rtx (outermode, ret, buffer2, 0, buffer_bytes))
7491 return NULL_RTX;
7492 for (unsigned int i = 0; i < buffer_bytes; ++i)
7493 if (buffer[i] != buffer2[i])
7494 return NULL_RTX;
7496 return ret;
7499 /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
7500 Return 0 if no simplifications are possible. */
7502 simplify_context::simplify_subreg (machine_mode outermode, rtx op,
7503 machine_mode innermode, poly_uint64 byte)
7505 /* Little bit of sanity checking. */
7506 gcc_assert (innermode != VOIDmode);
7507 gcc_assert (outermode != VOIDmode);
7508 gcc_assert (innermode != BLKmode);
7509 gcc_assert (outermode != BLKmode);
7511 gcc_assert (GET_MODE (op) == innermode
7512 || GET_MODE (op) == VOIDmode);
7514 poly_uint64 outersize = GET_MODE_SIZE (outermode);
7515 if (!multiple_p (byte, outersize))
7516 return NULL_RTX;
7518 poly_uint64 innersize = GET_MODE_SIZE (innermode);
7519 if (maybe_ge (byte, innersize))
7520 return NULL_RTX;
7522 if (outermode == innermode && known_eq (byte, 0U))
7523 return op;
7525 if (GET_CODE (op) == CONST_VECTOR)
7526 byte = simplify_const_vector_byte_offset (op, byte);
7528 if (multiple_p (byte, GET_MODE_UNIT_SIZE (innermode)))
7530 rtx elt;
7532 if (VECTOR_MODE_P (outermode)
7533 && GET_MODE_INNER (outermode) == GET_MODE_INNER (innermode)
7534 && vec_duplicate_p (op, &elt))
7535 return gen_vec_duplicate (outermode, elt);
7537 if (outermode == GET_MODE_INNER (innermode)
7538 && vec_duplicate_p (op, &elt))
7539 return elt;
7542 if (CONST_SCALAR_INT_P (op)
7543 || CONST_DOUBLE_AS_FLOAT_P (op)
7544 || CONST_FIXED_P (op)
7545 || GET_CODE (op) == CONST_VECTOR)
7547 unsigned HOST_WIDE_INT cbyte;
7548 if (byte.is_constant (&cbyte))
7550 if (GET_CODE (op) == CONST_VECTOR && VECTOR_MODE_P (outermode))
7552 rtx tmp = simplify_const_vector_subreg (outermode, op,
7553 innermode, cbyte);
7554 if (tmp)
7555 return tmp;
7558 fixed_size_mode fs_outermode;
7559 if (is_a <fixed_size_mode> (outermode, &fs_outermode))
7560 return simplify_immed_subreg (fs_outermode, op, innermode, cbyte);
7564 /* Changing mode twice with SUBREG => just change it once,
7565 or not at all if changing back op starting mode. */
7566 if (GET_CODE (op) == SUBREG)
7568 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
7569 poly_uint64 innermostsize = GET_MODE_SIZE (innermostmode);
7570 rtx newx;
7572 if (outermode == innermostmode
7573 && known_eq (byte, 0U)
7574 && known_eq (SUBREG_BYTE (op), 0))
7575 return SUBREG_REG (op);
7577 /* Work out the memory offset of the final OUTERMODE value relative
7578 to the inner value of OP. */
7579 poly_int64 mem_offset = subreg_memory_offset (outermode,
7580 innermode, byte);
7581 poly_int64 op_mem_offset = subreg_memory_offset (op);
7582 poly_int64 final_offset = mem_offset + op_mem_offset;
7584 /* See whether resulting subreg will be paradoxical. */
7585 if (!paradoxical_subreg_p (outermode, innermostmode))
7587 /* Bail out in case resulting subreg would be incorrect. */
7588 if (maybe_lt (final_offset, 0)
7589 || maybe_ge (poly_uint64 (final_offset), innermostsize)
7590 || !multiple_p (final_offset, outersize))
7591 return NULL_RTX;
7593 else
7595 poly_int64 required_offset = subreg_memory_offset (outermode,
7596 innermostmode, 0);
7597 if (maybe_ne (final_offset, required_offset))
7598 return NULL_RTX;
7599 /* Paradoxical subregs always have byte offset 0. */
7600 final_offset = 0;
7603 /* Recurse for further possible simplifications. */
7604 newx = simplify_subreg (outermode, SUBREG_REG (op), innermostmode,
7605 final_offset);
7606 if (newx)
7607 return newx;
7608 if (validate_subreg (outermode, innermostmode,
7609 SUBREG_REG (op), final_offset))
7611 newx = gen_rtx_SUBREG (outermode, SUBREG_REG (op), final_offset);
7612 if (SUBREG_PROMOTED_VAR_P (op)
7613 && SUBREG_PROMOTED_SIGN (op) >= 0
7614 && GET_MODE_CLASS (outermode) == MODE_INT
7615 && known_ge (outersize, innersize)
7616 && known_le (outersize, innermostsize)
7617 && subreg_lowpart_p (newx))
7619 SUBREG_PROMOTED_VAR_P (newx) = 1;
7620 SUBREG_PROMOTED_SET (newx, SUBREG_PROMOTED_GET (op));
7622 return newx;
7624 return NULL_RTX;
7627 /* SUBREG of a hard register => just change the register number
7628 and/or mode. If the hard register is not valid in that mode,
7629 suppress this simplification. If the hard register is the stack,
7630 frame, or argument pointer, leave this as a SUBREG. */
7632 if (REG_P (op) && HARD_REGISTER_P (op))
7634 unsigned int regno, final_regno;
7636 regno = REGNO (op);
7637 final_regno = simplify_subreg_regno (regno, innermode, byte, outermode);
7638 if (HARD_REGISTER_NUM_P (final_regno))
7640 rtx x = gen_rtx_REG_offset (op, outermode, final_regno,
7641 subreg_memory_offset (outermode,
7642 innermode, byte));
7644 /* Propagate original regno. We don't have any way to specify
7645 the offset inside original regno, so do so only for lowpart.
7646 The information is used only by alias analysis that cannot
7647 grog partial register anyway. */
7649 if (known_eq (subreg_lowpart_offset (outermode, innermode), byte))
7650 ORIGINAL_REGNO (x) = ORIGINAL_REGNO (op);
7651 return x;
7655 /* If we have a SUBREG of a register that we are replacing and we are
7656 replacing it with a MEM, make a new MEM and try replacing the
7657 SUBREG with it. Don't do this if the MEM has a mode-dependent address
7658 or if we would be widening it. */
7660 if (MEM_P (op)
7661 && ! mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op))
7662 /* Allow splitting of volatile memory references in case we don't
7663 have instruction to move the whole thing. */
7664 && (! MEM_VOLATILE_P (op)
7665 || ! have_insn_for (SET, innermode))
7666 && !(STRICT_ALIGNMENT && MEM_ALIGN (op) < GET_MODE_ALIGNMENT (outermode))
7667 && known_le (outersize, innersize))
7668 return adjust_address_nv (op, outermode, byte);
7670 /* Handle complex or vector values represented as CONCAT or VEC_CONCAT
7671 of two parts. */
7672 if (GET_CODE (op) == CONCAT
7673 || GET_CODE (op) == VEC_CONCAT)
7675 poly_uint64 final_offset;
7676 rtx part, res;
7678 machine_mode part_mode = GET_MODE (XEXP (op, 0));
7679 if (part_mode == VOIDmode)
7680 part_mode = GET_MODE_INNER (GET_MODE (op));
7681 poly_uint64 part_size = GET_MODE_SIZE (part_mode);
7682 if (known_lt (byte, part_size))
7684 part = XEXP (op, 0);
7685 final_offset = byte;
7687 else if (known_ge (byte, part_size))
7689 part = XEXP (op, 1);
7690 final_offset = byte - part_size;
7692 else
7693 return NULL_RTX;
7695 if (maybe_gt (final_offset + outersize, part_size))
7696 return NULL_RTX;
7698 part_mode = GET_MODE (part);
7699 if (part_mode == VOIDmode)
7700 part_mode = GET_MODE_INNER (GET_MODE (op));
7701 res = simplify_subreg (outermode, part, part_mode, final_offset);
7702 if (res)
7703 return res;
7704 if (validate_subreg (outermode, part_mode, part, final_offset))
7705 return gen_rtx_SUBREG (outermode, part, final_offset);
7706 return NULL_RTX;
7709 /* Simplify
7710 (subreg (vec_merge (X)
7711 (vector)
7712 (const_int ((1 << N) | M)))
7713 (N * sizeof (outermode)))
7715 (subreg (X) (N * sizeof (outermode)))
7717 unsigned int idx;
7718 if (constant_multiple_p (byte, GET_MODE_SIZE (outermode), &idx)
7719 && idx < HOST_BITS_PER_WIDE_INT
7720 && GET_CODE (op) == VEC_MERGE
7721 && GET_MODE_INNER (innermode) == outermode
7722 && CONST_INT_P (XEXP (op, 2))
7723 && (UINTVAL (XEXP (op, 2)) & (HOST_WIDE_INT_1U << idx)) != 0)
7724 return simplify_gen_subreg (outermode, XEXP (op, 0), innermode, byte);
7726 /* A SUBREG resulting from a zero extension may fold to zero if
7727 it extracts higher bits that the ZERO_EXTEND's source bits. */
7728 if (GET_CODE (op) == ZERO_EXTEND && SCALAR_INT_MODE_P (innermode))
7730 poly_uint64 bitpos = subreg_lsb_1 (outermode, innermode, byte);
7731 if (known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (XEXP (op, 0)))))
7732 return CONST0_RTX (outermode);
7735 scalar_int_mode int_outermode, int_innermode;
7736 if (is_a <scalar_int_mode> (outermode, &int_outermode)
7737 && is_a <scalar_int_mode> (innermode, &int_innermode)
7738 && known_eq (byte, subreg_lowpart_offset (int_outermode, int_innermode)))
7740 /* Handle polynomial integers. The upper bits of a paradoxical
7741 subreg are undefined, so this is safe regardless of whether
7742 we're truncating or extending. */
7743 if (CONST_POLY_INT_P (op))
7745 poly_wide_int val
7746 = poly_wide_int::from (const_poly_int_value (op),
7747 GET_MODE_PRECISION (int_outermode),
7748 SIGNED);
7749 return immed_wide_int_const (val, int_outermode);
7752 if (GET_MODE_PRECISION (int_outermode)
7753 < GET_MODE_PRECISION (int_innermode))
7755 rtx tem = simplify_truncation (int_outermode, op, int_innermode);
7756 if (tem)
7757 return tem;
7761 /* If the outer mode is not integral, try taking a subreg with the equivalent
7762 integer outer mode and then bitcasting the result.
7763 Other simplifications rely on integer to integer subregs and we'd
7764 potentially miss out on optimizations otherwise. */
7765 if (known_gt (GET_MODE_SIZE (innermode),
7766 GET_MODE_SIZE (outermode))
7767 && SCALAR_INT_MODE_P (innermode)
7768 && !SCALAR_INT_MODE_P (outermode)
7769 && int_mode_for_size (GET_MODE_BITSIZE (outermode),
7770 0).exists (&int_outermode))
7772 rtx tem = simplify_subreg (int_outermode, op, innermode, byte);
7773 if (tem)
7774 return simplify_gen_subreg (outermode, tem, int_outermode, byte);
7777 /* If OP is a vector comparison and the subreg is not changing the
7778 number of elements or the size of the elements, change the result
7779 of the comparison to the new mode. */
7780 if (COMPARISON_P (op)
7781 && VECTOR_MODE_P (outermode)
7782 && VECTOR_MODE_P (innermode)
7783 && known_eq (GET_MODE_NUNITS (outermode), GET_MODE_NUNITS (innermode))
7784 && known_eq (GET_MODE_UNIT_SIZE (outermode),
7785 GET_MODE_UNIT_SIZE (innermode)))
7786 return simplify_gen_relational (GET_CODE (op), outermode, innermode,
7787 XEXP (op, 0), XEXP (op, 1));
7788 return NULL_RTX;
7791 /* Make a SUBREG operation or equivalent if it folds. */
7794 simplify_context::simplify_gen_subreg (machine_mode outermode, rtx op,
7795 machine_mode innermode,
7796 poly_uint64 byte)
7798 rtx newx;
7800 newx = simplify_subreg (outermode, op, innermode, byte);
7801 if (newx)
7802 return newx;
7804 if (GET_CODE (op) == SUBREG
7805 || GET_CODE (op) == CONCAT
7806 || GET_MODE (op) == VOIDmode)
7807 return NULL_RTX;
7809 if (MODE_COMPOSITE_P (outermode)
7810 && (CONST_SCALAR_INT_P (op)
7811 || CONST_DOUBLE_AS_FLOAT_P (op)
7812 || CONST_FIXED_P (op)
7813 || GET_CODE (op) == CONST_VECTOR))
7814 return NULL_RTX;
7816 if (validate_subreg (outermode, innermode, op, byte))
7817 return gen_rtx_SUBREG (outermode, op, byte);
7819 return NULL_RTX;
7822 /* Generates a subreg to get the least significant part of EXPR (in mode
7823 INNER_MODE) to OUTER_MODE. */
7826 simplify_context::lowpart_subreg (machine_mode outer_mode, rtx expr,
7827 machine_mode inner_mode)
7829 return simplify_gen_subreg (outer_mode, expr, inner_mode,
7830 subreg_lowpart_offset (outer_mode, inner_mode));
7833 /* Generate RTX to select element at INDEX out of vector OP. */
7836 simplify_context::simplify_gen_vec_select (rtx op, unsigned int index)
7838 gcc_assert (VECTOR_MODE_P (GET_MODE (op)));
7840 scalar_mode imode = GET_MODE_INNER (GET_MODE (op));
7842 if (known_eq (index * GET_MODE_SIZE (imode),
7843 subreg_lowpart_offset (imode, GET_MODE (op))))
7845 rtx res = lowpart_subreg (imode, op, GET_MODE (op));
7846 if (res)
7847 return res;
7850 rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (index)));
7851 return gen_rtx_VEC_SELECT (imode, op, tmp);
7855 /* Simplify X, an rtx expression.
7857 Return the simplified expression or NULL if no simplifications
7858 were possible.
7860 This is the preferred entry point into the simplification routines;
7861 however, we still allow passes to call the more specific routines.
7863 Right now GCC has three (yes, three) major bodies of RTL simplification
7864 code that need to be unified.
7866 1. fold_rtx in cse.cc. This code uses various CSE specific
7867 information to aid in RTL simplification.
7869 2. simplify_rtx in combine.cc. Similar to fold_rtx, except that
7870 it uses combine specific information to aid in RTL
7871 simplification.
7873 3. The routines in this file.
7876 Long term we want to only have one body of simplification code; to
7877 get to that state I recommend the following steps:
7879 1. Pour over fold_rtx & simplify_rtx and move any simplifications
7880 which are not pass dependent state into these routines.
7882 2. As code is moved by #1, change fold_rtx & simplify_rtx to
7883 use this routine whenever possible.
7885 3. Allow for pass dependent state to be provided to these
7886 routines and add simplifications based on the pass dependent
7887 state. Remove code from cse.cc & combine.cc that becomes
7888 redundant/dead.
7890 It will take time, but ultimately the compiler will be easier to
7891 maintain and improve. It's totally silly that when we add a
7892 simplification that it needs to be added to 4 places (3 for RTL
7893 simplification and 1 for tree simplification. */
7896 simplify_rtx (const_rtx x)
7898 const enum rtx_code code = GET_CODE (x);
7899 const machine_mode mode = GET_MODE (x);
7901 switch (GET_RTX_CLASS (code))
7903 case RTX_UNARY:
7904 return simplify_unary_operation (code, mode,
7905 XEXP (x, 0), GET_MODE (XEXP (x, 0)));
7906 case RTX_COMM_ARITH:
7907 if (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7908 return simplify_gen_binary (code, mode, XEXP (x, 1), XEXP (x, 0));
7910 /* Fall through. */
7912 case RTX_BIN_ARITH:
7913 return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
7915 case RTX_TERNARY:
7916 case RTX_BITFIELD_OPS:
7917 return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
7918 XEXP (x, 0), XEXP (x, 1),
7919 XEXP (x, 2));
7921 case RTX_COMPARE:
7922 case RTX_COMM_COMPARE:
7923 return simplify_relational_operation (code, mode,
7924 ((GET_MODE (XEXP (x, 0))
7925 != VOIDmode)
7926 ? GET_MODE (XEXP (x, 0))
7927 : GET_MODE (XEXP (x, 1))),
7928 XEXP (x, 0),
7929 XEXP (x, 1));
7931 case RTX_EXTRA:
7932 if (code == SUBREG)
7933 return simplify_subreg (mode, SUBREG_REG (x),
7934 GET_MODE (SUBREG_REG (x)),
7935 SUBREG_BYTE (x));
7936 break;
7938 case RTX_OBJ:
7939 if (code == LO_SUM)
7941 /* Convert (lo_sum (high FOO) FOO) to FOO. */
7942 if (GET_CODE (XEXP (x, 0)) == HIGH
7943 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
7944 return XEXP (x, 1);
7946 break;
7948 default:
7949 break;
7951 return NULL;
7954 #if CHECKING_P
7956 namespace selftest {
7958 /* Make a unique pseudo REG of mode MODE for use by selftests. */
7960 static rtx
7961 make_test_reg (machine_mode mode)
7963 static int test_reg_num = LAST_VIRTUAL_REGISTER + 1;
7965 return gen_rtx_REG (mode, test_reg_num++);
7968 static void
7969 test_scalar_int_ops (machine_mode mode)
7971 rtx op0 = make_test_reg (mode);
7972 rtx op1 = make_test_reg (mode);
7973 rtx six = GEN_INT (6);
7975 rtx neg_op0 = simplify_gen_unary (NEG, mode, op0, mode);
7976 rtx not_op0 = simplify_gen_unary (NOT, mode, op0, mode);
7977 rtx bswap_op0 = simplify_gen_unary (BSWAP, mode, op0, mode);
7979 rtx and_op0_op1 = simplify_gen_binary (AND, mode, op0, op1);
7980 rtx ior_op0_op1 = simplify_gen_binary (IOR, mode, op0, op1);
7981 rtx xor_op0_op1 = simplify_gen_binary (XOR, mode, op0, op1);
7983 rtx and_op0_6 = simplify_gen_binary (AND, mode, op0, six);
7984 rtx and_op1_6 = simplify_gen_binary (AND, mode, op1, six);
7986 /* Test some binary identities. */
7987 ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, op0, const0_rtx));
7988 ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, const0_rtx, op0));
7989 ASSERT_RTX_EQ (op0, simplify_gen_binary (MINUS, mode, op0, const0_rtx));
7990 ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, op0, const1_rtx));
7991 ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, const1_rtx, op0));
7992 ASSERT_RTX_EQ (op0, simplify_gen_binary (DIV, mode, op0, const1_rtx));
7993 ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, constm1_rtx));
7994 ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, constm1_rtx, op0));
7995 ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, const0_rtx));
7996 ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, const0_rtx, op0));
7997 ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, op0, const0_rtx));
7998 ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, const0_rtx, op0));
7999 ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFT, mode, op0, const0_rtx));
8000 ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATE, mode, op0, const0_rtx));
8001 ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFTRT, mode, op0, const0_rtx));
8002 ASSERT_RTX_EQ (op0, simplify_gen_binary (LSHIFTRT, mode, op0, const0_rtx));
8003 ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATERT, mode, op0, const0_rtx));
8005 /* Test some self-inverse operations. */
8006 ASSERT_RTX_EQ (op0, simplify_gen_unary (NEG, mode, neg_op0, mode));
8007 ASSERT_RTX_EQ (op0, simplify_gen_unary (NOT, mode, not_op0, mode));
8008 ASSERT_RTX_EQ (op0, simplify_gen_unary (BSWAP, mode, bswap_op0, mode));
8010 /* Test some reflexive operations. */
8011 ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, op0));
8012 ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, op0));
8013 ASSERT_RTX_EQ (op0, simplify_gen_binary (SMIN, mode, op0, op0));
8014 ASSERT_RTX_EQ (op0, simplify_gen_binary (SMAX, mode, op0, op0));
8015 ASSERT_RTX_EQ (op0, simplify_gen_binary (UMIN, mode, op0, op0));
8016 ASSERT_RTX_EQ (op0, simplify_gen_binary (UMAX, mode, op0, op0));
8018 ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (MINUS, mode, op0, op0));
8019 ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (XOR, mode, op0, op0));
8021 /* Test simplify_distributive_operation. */
8022 ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, xor_op0_op1, six),
8023 simplify_gen_binary (XOR, mode, and_op0_6, and_op1_6));
8024 ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, ior_op0_op1, six),
8025 simplify_gen_binary (IOR, mode, and_op0_6, and_op1_6));
8026 ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, and_op0_op1, six),
8027 simplify_gen_binary (AND, mode, and_op0_6, and_op1_6));
8029 /* Test useless extensions are eliminated. */
8030 ASSERT_RTX_EQ (op0, simplify_gen_unary (TRUNCATE, mode, op0, mode));
8031 ASSERT_RTX_EQ (op0, simplify_gen_unary (ZERO_EXTEND, mode, op0, mode));
8032 ASSERT_RTX_EQ (op0, simplify_gen_unary (SIGN_EXTEND, mode, op0, mode));
8033 ASSERT_RTX_EQ (op0, lowpart_subreg (mode, op0, mode));
8036 /* Verify some simplifications of integer extension/truncation.
8037 Machine mode BMODE is the guaranteed wider than SMODE. */
8039 static void
8040 test_scalar_int_ext_ops (machine_mode bmode, machine_mode smode)
8042 rtx sreg = make_test_reg (smode);
8044 /* Check truncation of extension. */
8045 ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8046 simplify_gen_unary (ZERO_EXTEND, bmode,
8047 sreg, smode),
8048 bmode),
8049 sreg);
8050 ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8051 simplify_gen_unary (SIGN_EXTEND, bmode,
8052 sreg, smode),
8053 bmode),
8054 sreg);
8055 ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8056 lowpart_subreg (bmode, sreg, smode),
8057 bmode),
8058 sreg);
8061 /* Verify more simplifications of integer extension/truncation.
8062 BMODE is wider than MMODE which is wider than SMODE. */
8064 static void
8065 test_scalar_int_ext_ops2 (machine_mode bmode, machine_mode mmode,
8066 machine_mode smode)
8068 rtx breg = make_test_reg (bmode);
8069 rtx mreg = make_test_reg (mmode);
8070 rtx sreg = make_test_reg (smode);
8072 /* Check truncate of truncate. */
8073 ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8074 simplify_gen_unary (TRUNCATE, mmode,
8075 breg, bmode),
8076 mmode),
8077 simplify_gen_unary (TRUNCATE, smode, breg, bmode));
8079 /* Check extension of extension. */
8080 ASSERT_RTX_EQ (simplify_gen_unary (ZERO_EXTEND, bmode,
8081 simplify_gen_unary (ZERO_EXTEND, mmode,
8082 sreg, smode),
8083 mmode),
8084 simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
8085 ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
8086 simplify_gen_unary (SIGN_EXTEND, mmode,
8087 sreg, smode),
8088 mmode),
8089 simplify_gen_unary (SIGN_EXTEND, bmode, sreg, smode));
8090 ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
8091 simplify_gen_unary (ZERO_EXTEND, mmode,
8092 sreg, smode),
8093 mmode),
8094 simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
8096 /* Check truncation of extension. */
8097 ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8098 simplify_gen_unary (ZERO_EXTEND, bmode,
8099 mreg, mmode),
8100 bmode),
8101 simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8102 ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8103 simplify_gen_unary (SIGN_EXTEND, bmode,
8104 mreg, mmode),
8105 bmode),
8106 simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8107 ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8108 lowpart_subreg (bmode, mreg, mmode),
8109 bmode),
8110 simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8114 /* Verify some simplifications involving scalar expressions. */
8116 static void
8117 test_scalar_ops ()
8119 for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
8121 machine_mode mode = (machine_mode) i;
8122 if (SCALAR_INT_MODE_P (mode) && mode != BImode)
8123 test_scalar_int_ops (mode);
8126 test_scalar_int_ext_ops (HImode, QImode);
8127 test_scalar_int_ext_ops (SImode, QImode);
8128 test_scalar_int_ext_ops (SImode, HImode);
8129 test_scalar_int_ext_ops (DImode, QImode);
8130 test_scalar_int_ext_ops (DImode, HImode);
8131 test_scalar_int_ext_ops (DImode, SImode);
8133 test_scalar_int_ext_ops2 (SImode, HImode, QImode);
8134 test_scalar_int_ext_ops2 (DImode, HImode, QImode);
8135 test_scalar_int_ext_ops2 (DImode, SImode, QImode);
8136 test_scalar_int_ext_ops2 (DImode, SImode, HImode);
8139 /* Test vector simplifications involving VEC_DUPLICATE in which the
8140 operands and result have vector mode MODE. SCALAR_REG is a pseudo
8141 register that holds one element of MODE. */
8143 static void
8144 test_vector_ops_duplicate (machine_mode mode, rtx scalar_reg)
8146 scalar_mode inner_mode = GET_MODE_INNER (mode);
8147 rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
8148 poly_uint64 nunits = GET_MODE_NUNITS (mode);
8149 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
8151 /* Test some simple unary cases with VEC_DUPLICATE arguments. */
8152 rtx not_scalar_reg = gen_rtx_NOT (inner_mode, scalar_reg);
8153 rtx duplicate_not = gen_rtx_VEC_DUPLICATE (mode, not_scalar_reg);
8154 ASSERT_RTX_EQ (duplicate,
8155 simplify_unary_operation (NOT, mode,
8156 duplicate_not, mode));
8158 rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
8159 rtx duplicate_neg = gen_rtx_VEC_DUPLICATE (mode, neg_scalar_reg);
8160 ASSERT_RTX_EQ (duplicate,
8161 simplify_unary_operation (NEG, mode,
8162 duplicate_neg, mode));
8164 /* Test some simple binary cases with VEC_DUPLICATE arguments. */
8165 ASSERT_RTX_EQ (duplicate,
8166 simplify_binary_operation (PLUS, mode, duplicate,
8167 CONST0_RTX (mode)));
8169 ASSERT_RTX_EQ (duplicate,
8170 simplify_binary_operation (MINUS, mode, duplicate,
8171 CONST0_RTX (mode)));
8173 ASSERT_RTX_PTR_EQ (CONST0_RTX (mode),
8174 simplify_binary_operation (MINUS, mode, duplicate,
8175 duplicate));
8178 /* Test a scalar VEC_SELECT of a VEC_DUPLICATE. */
8179 rtx zero_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
8180 ASSERT_RTX_PTR_EQ (scalar_reg,
8181 simplify_binary_operation (VEC_SELECT, inner_mode,
8182 duplicate, zero_par));
8184 unsigned HOST_WIDE_INT const_nunits;
8185 if (nunits.is_constant (&const_nunits))
8187 /* And again with the final element. */
8188 rtx last_index = gen_int_mode (const_nunits - 1, word_mode);
8189 rtx last_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, last_index));
8190 ASSERT_RTX_PTR_EQ (scalar_reg,
8191 simplify_binary_operation (VEC_SELECT, inner_mode,
8192 duplicate, last_par));
8194 /* Test a scalar subreg of a VEC_MERGE of a VEC_DUPLICATE. */
8195 /* Skip this test for vectors of booleans, because offset is in bytes,
8196 while vec_merge indices are in elements (usually bits). */
8197 if (GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL)
8199 rtx vector_reg = make_test_reg (mode);
8200 for (unsigned HOST_WIDE_INT i = 0; i < const_nunits; i++)
8202 if (i >= HOST_BITS_PER_WIDE_INT)
8203 break;
8204 rtx mask = GEN_INT ((HOST_WIDE_INT_1U << i) | (i + 1));
8205 rtx vm = gen_rtx_VEC_MERGE (mode, duplicate, vector_reg, mask);
8206 poly_uint64 offset = i * GET_MODE_SIZE (inner_mode);
8208 ASSERT_RTX_EQ (scalar_reg,
8209 simplify_gen_subreg (inner_mode, vm,
8210 mode, offset));
8215 /* Test a scalar subreg of a VEC_DUPLICATE. */
8216 poly_uint64 offset = subreg_lowpart_offset (inner_mode, mode);
8217 ASSERT_RTX_EQ (scalar_reg,
8218 simplify_gen_subreg (inner_mode, duplicate,
8219 mode, offset));
8221 machine_mode narrower_mode;
8222 if (maybe_ne (nunits, 2U)
8223 && multiple_p (nunits, 2)
8224 && mode_for_vector (inner_mode, 2).exists (&narrower_mode)
8225 && VECTOR_MODE_P (narrower_mode))
8227 /* Test VEC_DUPLICATE of a vector. */
8228 rtx_vector_builder nbuilder (narrower_mode, 2, 1);
8229 nbuilder.quick_push (const0_rtx);
8230 nbuilder.quick_push (const1_rtx);
8231 rtx_vector_builder builder (mode, 2, 1);
8232 builder.quick_push (const0_rtx);
8233 builder.quick_push (const1_rtx);
8234 ASSERT_RTX_EQ (builder.build (),
8235 simplify_unary_operation (VEC_DUPLICATE, mode,
8236 nbuilder.build (),
8237 narrower_mode));
8239 /* Test VEC_SELECT of a vector. */
8240 rtx vec_par
8241 = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, const1_rtx, const0_rtx));
8242 rtx narrower_duplicate
8243 = gen_rtx_VEC_DUPLICATE (narrower_mode, scalar_reg);
8244 ASSERT_RTX_EQ (narrower_duplicate,
8245 simplify_binary_operation (VEC_SELECT, narrower_mode,
8246 duplicate, vec_par));
8248 /* Test a vector subreg of a VEC_DUPLICATE. */
8249 poly_uint64 offset = subreg_lowpart_offset (narrower_mode, mode);
8250 ASSERT_RTX_EQ (narrower_duplicate,
8251 simplify_gen_subreg (narrower_mode, duplicate,
8252 mode, offset));
8256 /* Test vector simplifications involving VEC_SERIES in which the
8257 operands and result have vector mode MODE. SCALAR_REG is a pseudo
8258 register that holds one element of MODE. */
8260 static void
8261 test_vector_ops_series (machine_mode mode, rtx scalar_reg)
8263 /* Test unary cases with VEC_SERIES arguments. */
8264 scalar_mode inner_mode = GET_MODE_INNER (mode);
8265 rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
8266 rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
8267 rtx series_0_r = gen_rtx_VEC_SERIES (mode, const0_rtx, scalar_reg);
8268 rtx series_0_nr = gen_rtx_VEC_SERIES (mode, const0_rtx, neg_scalar_reg);
8269 rtx series_nr_1 = gen_rtx_VEC_SERIES (mode, neg_scalar_reg, const1_rtx);
8270 rtx series_r_m1 = gen_rtx_VEC_SERIES (mode, scalar_reg, constm1_rtx);
8271 rtx series_r_r = gen_rtx_VEC_SERIES (mode, scalar_reg, scalar_reg);
8272 rtx series_nr_nr = gen_rtx_VEC_SERIES (mode, neg_scalar_reg,
8273 neg_scalar_reg);
8274 ASSERT_RTX_EQ (series_0_r,
8275 simplify_unary_operation (NEG, mode, series_0_nr, mode));
8276 ASSERT_RTX_EQ (series_r_m1,
8277 simplify_unary_operation (NEG, mode, series_nr_1, mode));
8278 ASSERT_RTX_EQ (series_r_r,
8279 simplify_unary_operation (NEG, mode, series_nr_nr, mode));
8281 /* Test that a VEC_SERIES with a zero step is simplified away. */
8282 ASSERT_RTX_EQ (duplicate,
8283 simplify_binary_operation (VEC_SERIES, mode,
8284 scalar_reg, const0_rtx));
8286 /* Test PLUS and MINUS with VEC_SERIES. */
8287 rtx series_0_1 = gen_const_vec_series (mode, const0_rtx, const1_rtx);
8288 rtx series_0_m1 = gen_const_vec_series (mode, const0_rtx, constm1_rtx);
8289 rtx series_r_1 = gen_rtx_VEC_SERIES (mode, scalar_reg, const1_rtx);
8290 ASSERT_RTX_EQ (series_r_r,
8291 simplify_binary_operation (PLUS, mode, series_0_r,
8292 duplicate));
8293 ASSERT_RTX_EQ (series_r_1,
8294 simplify_binary_operation (PLUS, mode, duplicate,
8295 series_0_1));
8296 ASSERT_RTX_EQ (series_r_m1,
8297 simplify_binary_operation (PLUS, mode, duplicate,
8298 series_0_m1));
8299 ASSERT_RTX_EQ (series_0_r,
8300 simplify_binary_operation (MINUS, mode, series_r_r,
8301 duplicate));
8302 ASSERT_RTX_EQ (series_r_m1,
8303 simplify_binary_operation (MINUS, mode, duplicate,
8304 series_0_1));
8305 ASSERT_RTX_EQ (series_r_1,
8306 simplify_binary_operation (MINUS, mode, duplicate,
8307 series_0_m1));
8308 ASSERT_RTX_EQ (series_0_m1,
8309 simplify_binary_operation (VEC_SERIES, mode, const0_rtx,
8310 constm1_rtx));
8312 /* Test NEG on constant vector series. */
8313 ASSERT_RTX_EQ (series_0_m1,
8314 simplify_unary_operation (NEG, mode, series_0_1, mode));
8315 ASSERT_RTX_EQ (series_0_1,
8316 simplify_unary_operation (NEG, mode, series_0_m1, mode));
8318 /* Test PLUS and MINUS on constant vector series. */
8319 rtx scalar2 = gen_int_mode (2, inner_mode);
8320 rtx scalar3 = gen_int_mode (3, inner_mode);
8321 rtx series_1_1 = gen_const_vec_series (mode, const1_rtx, const1_rtx);
8322 rtx series_0_2 = gen_const_vec_series (mode, const0_rtx, scalar2);
8323 rtx series_1_3 = gen_const_vec_series (mode, const1_rtx, scalar3);
8324 ASSERT_RTX_EQ (series_1_1,
8325 simplify_binary_operation (PLUS, mode, series_0_1,
8326 CONST1_RTX (mode)));
8327 ASSERT_RTX_EQ (series_0_m1,
8328 simplify_binary_operation (PLUS, mode, CONST0_RTX (mode),
8329 series_0_m1));
8330 ASSERT_RTX_EQ (series_1_3,
8331 simplify_binary_operation (PLUS, mode, series_1_1,
8332 series_0_2));
8333 ASSERT_RTX_EQ (series_0_1,
8334 simplify_binary_operation (MINUS, mode, series_1_1,
8335 CONST1_RTX (mode)));
8336 ASSERT_RTX_EQ (series_1_1,
8337 simplify_binary_operation (MINUS, mode, CONST1_RTX (mode),
8338 series_0_m1));
8339 ASSERT_RTX_EQ (series_1_1,
8340 simplify_binary_operation (MINUS, mode, series_1_3,
8341 series_0_2));
8343 /* Test MULT between constant vectors. */
8344 rtx vec2 = gen_const_vec_duplicate (mode, scalar2);
8345 rtx vec3 = gen_const_vec_duplicate (mode, scalar3);
8346 rtx scalar9 = gen_int_mode (9, inner_mode);
8347 rtx series_3_9 = gen_const_vec_series (mode, scalar3, scalar9);
8348 ASSERT_RTX_EQ (series_0_2,
8349 simplify_binary_operation (MULT, mode, series_0_1, vec2));
8350 ASSERT_RTX_EQ (series_3_9,
8351 simplify_binary_operation (MULT, mode, vec3, series_1_3));
8352 if (!GET_MODE_NUNITS (mode).is_constant ())
8353 ASSERT_FALSE (simplify_binary_operation (MULT, mode, series_0_1,
8354 series_0_1));
8356 /* Test ASHIFT between constant vectors. */
8357 ASSERT_RTX_EQ (series_0_2,
8358 simplify_binary_operation (ASHIFT, mode, series_0_1,
8359 CONST1_RTX (mode)));
8360 if (!GET_MODE_NUNITS (mode).is_constant ())
8361 ASSERT_FALSE (simplify_binary_operation (ASHIFT, mode, CONST1_RTX (mode),
8362 series_0_1));
8365 static rtx
8366 simplify_merge_mask (rtx x, rtx mask, int op)
8368 return simplify_context ().simplify_merge_mask (x, mask, op);
8371 /* Verify simplify_merge_mask works correctly. */
8373 static void
8374 test_vec_merge (machine_mode mode)
8376 rtx op0 = make_test_reg (mode);
8377 rtx op1 = make_test_reg (mode);
8378 rtx op2 = make_test_reg (mode);
8379 rtx op3 = make_test_reg (mode);
8380 rtx op4 = make_test_reg (mode);
8381 rtx op5 = make_test_reg (mode);
8382 rtx mask1 = make_test_reg (SImode);
8383 rtx mask2 = make_test_reg (SImode);
8384 rtx vm1 = gen_rtx_VEC_MERGE (mode, op0, op1, mask1);
8385 rtx vm2 = gen_rtx_VEC_MERGE (mode, op2, op3, mask1);
8386 rtx vm3 = gen_rtx_VEC_MERGE (mode, op4, op5, mask1);
8388 /* Simple vec_merge. */
8389 ASSERT_EQ (op0, simplify_merge_mask (vm1, mask1, 0));
8390 ASSERT_EQ (op1, simplify_merge_mask (vm1, mask1, 1));
8391 ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 0));
8392 ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 1));
8394 /* Nested vec_merge.
8395 It's tempting to make this simplify right down to opN, but we don't
8396 because all the simplify_* functions assume that the operands have
8397 already been simplified. */
8398 rtx nvm = gen_rtx_VEC_MERGE (mode, vm1, vm2, mask1);
8399 ASSERT_EQ (vm1, simplify_merge_mask (nvm, mask1, 0));
8400 ASSERT_EQ (vm2, simplify_merge_mask (nvm, mask1, 1));
8402 /* Intermediate unary op. */
8403 rtx unop = gen_rtx_NOT (mode, vm1);
8404 ASSERT_RTX_EQ (gen_rtx_NOT (mode, op0),
8405 simplify_merge_mask (unop, mask1, 0));
8406 ASSERT_RTX_EQ (gen_rtx_NOT (mode, op1),
8407 simplify_merge_mask (unop, mask1, 1));
8409 /* Intermediate binary op. */
8410 rtx binop = gen_rtx_PLUS (mode, vm1, vm2);
8411 ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op0, op2),
8412 simplify_merge_mask (binop, mask1, 0));
8413 ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op1, op3),
8414 simplify_merge_mask (binop, mask1, 1));
8416 /* Intermediate ternary op. */
8417 rtx tenop = gen_rtx_FMA (mode, vm1, vm2, vm3);
8418 ASSERT_RTX_EQ (gen_rtx_FMA (mode, op0, op2, op4),
8419 simplify_merge_mask (tenop, mask1, 0));
8420 ASSERT_RTX_EQ (gen_rtx_FMA (mode, op1, op3, op5),
8421 simplify_merge_mask (tenop, mask1, 1));
8423 /* Side effects. */
8424 rtx badop0 = gen_rtx_PRE_INC (mode, op0);
8425 rtx badvm = gen_rtx_VEC_MERGE (mode, badop0, op1, mask1);
8426 ASSERT_EQ (badop0, simplify_merge_mask (badvm, mask1, 0));
8427 ASSERT_EQ (NULL_RTX, simplify_merge_mask (badvm, mask1, 1));
8429 /* Called indirectly. */
8430 ASSERT_RTX_EQ (gen_rtx_VEC_MERGE (mode, op0, op3, mask1),
8431 simplify_rtx (nvm));
8434 /* Test subregs of integer vector constant X, trying elements in
8435 the range [ELT_BIAS, ELT_BIAS + constant_lower_bound (NELTS)),
8436 where NELTS is the number of elements in X. Subregs involving
8437 elements [ELT_BIAS, ELT_BIAS + FIRST_VALID) are expected to fail. */
8439 static void
8440 test_vector_subregs_modes (rtx x, poly_uint64 elt_bias = 0,
8441 unsigned int first_valid = 0)
8443 machine_mode inner_mode = GET_MODE (x);
8444 scalar_mode int_mode = GET_MODE_INNER (inner_mode);
8446 for (unsigned int modei = 0; modei < NUM_MACHINE_MODES; ++modei)
8448 machine_mode outer_mode = (machine_mode) modei;
8449 if (!VECTOR_MODE_P (outer_mode))
8450 continue;
8452 unsigned int outer_nunits;
8453 if (GET_MODE_INNER (outer_mode) == int_mode
8454 && GET_MODE_NUNITS (outer_mode).is_constant (&outer_nunits)
8455 && multiple_p (GET_MODE_NUNITS (inner_mode), outer_nunits))
8457 /* Test subregs in which the outer mode is a smaller,
8458 constant-sized vector of the same element type. */
8459 unsigned int limit
8460 = constant_lower_bound (GET_MODE_NUNITS (inner_mode));
8461 for (unsigned int elt = 0; elt < limit; elt += outer_nunits)
8463 rtx expected = NULL_RTX;
8464 if (elt >= first_valid)
8466 rtx_vector_builder builder (outer_mode, outer_nunits, 1);
8467 for (unsigned int i = 0; i < outer_nunits; ++i)
8468 builder.quick_push (CONST_VECTOR_ELT (x, elt + i));
8469 expected = builder.build ();
8471 poly_uint64 byte = (elt_bias + elt) * GET_MODE_SIZE (int_mode);
8472 ASSERT_RTX_EQ (expected,
8473 simplify_subreg (outer_mode, x,
8474 inner_mode, byte));
8477 else if (known_eq (GET_MODE_SIZE (outer_mode),
8478 GET_MODE_SIZE (inner_mode))
8479 && known_eq (elt_bias, 0U)
8480 && (GET_MODE_CLASS (outer_mode) != MODE_VECTOR_BOOL
8481 || known_eq (GET_MODE_BITSIZE (outer_mode),
8482 GET_MODE_NUNITS (outer_mode)))
8483 && (!FLOAT_MODE_P (outer_mode)
8484 || (FLOAT_MODE_FORMAT (outer_mode)->ieee_bits
8485 == GET_MODE_UNIT_PRECISION (outer_mode)))
8486 && (GET_MODE_SIZE (inner_mode).is_constant ()
8487 || !CONST_VECTOR_STEPPED_P (x)))
8489 /* Try converting to OUTER_MODE and back. */
8490 rtx outer_x = simplify_subreg (outer_mode, x, inner_mode, 0);
8491 ASSERT_TRUE (outer_x != NULL_RTX);
8492 ASSERT_RTX_EQ (x, simplify_subreg (inner_mode, outer_x,
8493 outer_mode, 0));
8497 if (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
8499 /* Test each byte in the element range. */
8500 unsigned int limit
8501 = constant_lower_bound (GET_MODE_SIZE (inner_mode));
8502 for (unsigned int i = 0; i < limit; ++i)
8504 unsigned int elt = i / GET_MODE_SIZE (int_mode);
8505 rtx expected = NULL_RTX;
8506 if (elt >= first_valid)
8508 unsigned int byte_shift = i % GET_MODE_SIZE (int_mode);
8509 if (BYTES_BIG_ENDIAN)
8510 byte_shift = GET_MODE_SIZE (int_mode) - byte_shift - 1;
8511 rtx_mode_t vec_elt (CONST_VECTOR_ELT (x, elt), int_mode);
8512 wide_int shifted_elt
8513 = wi::lrshift (vec_elt, byte_shift * BITS_PER_UNIT);
8514 expected = immed_wide_int_const (shifted_elt, QImode);
8516 poly_uint64 byte = elt_bias * GET_MODE_SIZE (int_mode) + i;
8517 ASSERT_RTX_EQ (expected,
8518 simplify_subreg (QImode, x, inner_mode, byte));
8523 /* Test constant subregs of integer vector mode INNER_MODE, using 1
8524 element per pattern. */
8526 static void
8527 test_vector_subregs_repeating (machine_mode inner_mode)
8529 poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
8530 unsigned int min_nunits = constant_lower_bound (nunits);
8531 scalar_mode int_mode = GET_MODE_INNER (inner_mode);
8532 unsigned int count = gcd (min_nunits, 8);
8534 rtx_vector_builder builder (inner_mode, count, 1);
8535 for (unsigned int i = 0; i < count; ++i)
8536 builder.quick_push (gen_int_mode (8 - i, int_mode));
8537 rtx x = builder.build ();
8539 test_vector_subregs_modes (x);
8540 if (!nunits.is_constant ())
8541 test_vector_subregs_modes (x, nunits - min_nunits);
8544 /* Test constant subregs of integer vector mode INNER_MODE, using 2
8545 elements per pattern. */
8547 static void
8548 test_vector_subregs_fore_back (machine_mode inner_mode)
8550 poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
8551 unsigned int min_nunits = constant_lower_bound (nunits);
8552 scalar_mode int_mode = GET_MODE_INNER (inner_mode);
8553 unsigned int count = gcd (min_nunits, 4);
8555 rtx_vector_builder builder (inner_mode, count, 2);
8556 for (unsigned int i = 0; i < count; ++i)
8557 builder.quick_push (gen_int_mode (i, int_mode));
8558 for (unsigned int i = 0; i < count; ++i)
8559 builder.quick_push (gen_int_mode (-1 - (int) i, int_mode));
8560 rtx x = builder.build ();
8562 test_vector_subregs_modes (x);
8563 if (!nunits.is_constant ())
8564 test_vector_subregs_modes (x, nunits - min_nunits, count);
8567 /* Test constant subregs of integer vector mode INNER_MODE, using 3
8568 elements per pattern. */
8570 static void
8571 test_vector_subregs_stepped (machine_mode inner_mode)
8573 /* Build { 0, 1, 2, 3, ... }. */
8574 scalar_mode int_mode = GET_MODE_INNER (inner_mode);
8575 rtx_vector_builder builder (inner_mode, 1, 3);
8576 for (unsigned int i = 0; i < 3; ++i)
8577 builder.quick_push (gen_int_mode (i, int_mode));
8578 rtx x = builder.build ();
8580 test_vector_subregs_modes (x);
8583 /* Test constant subregs of integer vector mode INNER_MODE. */
8585 static void
8586 test_vector_subregs (machine_mode inner_mode)
8588 test_vector_subregs_repeating (inner_mode);
8589 test_vector_subregs_fore_back (inner_mode);
8590 test_vector_subregs_stepped (inner_mode);
8593 /* Verify some simplifications involving vectors. */
8595 static void
8596 test_vector_ops ()
8598 for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
8600 machine_mode mode = (machine_mode) i;
8601 if (VECTOR_MODE_P (mode))
8603 rtx scalar_reg = make_test_reg (GET_MODE_INNER (mode));
8604 test_vector_ops_duplicate (mode, scalar_reg);
8605 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
8606 && maybe_gt (GET_MODE_NUNITS (mode), 2))
8608 test_vector_ops_series (mode, scalar_reg);
8609 test_vector_subregs (mode);
8611 test_vec_merge (mode);
8616 template<unsigned int N>
8617 struct simplify_const_poly_int_tests
8619 static void run ();
8622 template<>
8623 struct simplify_const_poly_int_tests<1>
8625 static void run () {}
8628 /* Test various CONST_POLY_INT properties. */
8630 template<unsigned int N>
8631 void
8632 simplify_const_poly_int_tests<N>::run ()
8634 rtx x1 = gen_int_mode (poly_int64 (1, 1), QImode);
8635 rtx x2 = gen_int_mode (poly_int64 (-80, 127), QImode);
8636 rtx x3 = gen_int_mode (poly_int64 (-79, -128), QImode);
8637 rtx x4 = gen_int_mode (poly_int64 (5, 4), QImode);
8638 rtx x5 = gen_int_mode (poly_int64 (30, 24), QImode);
8639 rtx x6 = gen_int_mode (poly_int64 (20, 16), QImode);
8640 rtx x7 = gen_int_mode (poly_int64 (7, 4), QImode);
8641 rtx x8 = gen_int_mode (poly_int64 (30, 24), HImode);
8642 rtx x9 = gen_int_mode (poly_int64 (-30, -24), HImode);
8643 rtx x10 = gen_int_mode (poly_int64 (-31, -24), HImode);
8644 rtx two = GEN_INT (2);
8645 rtx six = GEN_INT (6);
8646 poly_uint64 offset = subreg_lowpart_offset (QImode, HImode);
8648 /* These tests only try limited operation combinations. Fuller arithmetic
8649 testing is done directly on poly_ints. */
8650 ASSERT_EQ (simplify_unary_operation (NEG, HImode, x8, HImode), x9);
8651 ASSERT_EQ (simplify_unary_operation (NOT, HImode, x8, HImode), x10);
8652 ASSERT_EQ (simplify_unary_operation (TRUNCATE, QImode, x8, HImode), x5);
8653 ASSERT_EQ (simplify_binary_operation (PLUS, QImode, x1, x2), x3);
8654 ASSERT_EQ (simplify_binary_operation (MINUS, QImode, x3, x1), x2);
8655 ASSERT_EQ (simplify_binary_operation (MULT, QImode, x4, six), x5);
8656 ASSERT_EQ (simplify_binary_operation (MULT, QImode, six, x4), x5);
8657 ASSERT_EQ (simplify_binary_operation (ASHIFT, QImode, x4, two), x6);
8658 ASSERT_EQ (simplify_binary_operation (IOR, QImode, x4, two), x7);
8659 ASSERT_EQ (simplify_subreg (HImode, x5, QImode, 0), x8);
8660 ASSERT_EQ (simplify_subreg (QImode, x8, HImode, offset), x5);
8663 /* Run all of the selftests within this file. */
8665 void
8666 simplify_rtx_cc_tests ()
8668 test_scalar_ops ();
8669 test_vector_ops ();
8670 simplify_const_poly_int_tests<NUM_POLY_INT_COEFFS>::run ();
8673 } // namespace selftest
8675 #endif /* CHECKING_P */