simplify-rtx: Parity of parity is parity
[gcc.git] / gcc / simplify-rtx.c
1 /* RTL simplification functions for GNU compiler.
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20
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
40 /* Simplification and canonicalization of RTL. */
41
42 /* Much code operates on (low, high) pairs; the low value is an
43 unsigned wide int, the high value a signed wide int. We
44 occasionally need to sign extend from low to high as if low were a
45 signed wide int. */
46 #define HWI_SIGN_EXTEND(low) \
47 ((((HOST_WIDE_INT) low) < 0) ? HOST_WIDE_INT_M1 : HOST_WIDE_INT_0)
48
49 static bool plus_minus_operand_p (const_rtx);
50 static rtx simplify_plus_minus (enum rtx_code, machine_mode, rtx, rtx);
51 static rtx simplify_associative_operation (enum rtx_code, machine_mode,
52 rtx, rtx);
53 static rtx simplify_relational_operation_1 (enum rtx_code, machine_mode,
54 machine_mode, rtx, rtx);
55 static rtx simplify_unary_operation_1 (enum rtx_code, machine_mode, rtx);
56 static rtx simplify_binary_operation_1 (enum rtx_code, machine_mode,
57 rtx, rtx, rtx, rtx);
58 \f
59 /* Negate I, which satisfies poly_int_rtx_p. MODE is the mode of I. */
60
61 static rtx
62 neg_poly_int_rtx (machine_mode mode, const_rtx i)
63 {
64 return immed_wide_int_const (-wi::to_poly_wide (i, mode), mode);
65 }
66
67 /* Test whether expression, X, is an immediate constant that represents
68 the most significant bit of machine mode MODE. */
69
70 bool
71 mode_signbit_p (machine_mode mode, const_rtx x)
72 {
73 unsigned HOST_WIDE_INT val;
74 unsigned int width;
75 scalar_int_mode int_mode;
76
77 if (!is_int_mode (mode, &int_mode))
78 return false;
79
80 width = GET_MODE_PRECISION (int_mode);
81 if (width == 0)
82 return false;
83
84 if (width <= HOST_BITS_PER_WIDE_INT
85 && CONST_INT_P (x))
86 val = INTVAL (x);
87 #if TARGET_SUPPORTS_WIDE_INT
88 else if (CONST_WIDE_INT_P (x))
89 {
90 unsigned int i;
91 unsigned int elts = CONST_WIDE_INT_NUNITS (x);
92 if (elts != (width + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
93 return false;
94 for (i = 0; i < elts - 1; i++)
95 if (CONST_WIDE_INT_ELT (x, i) != 0)
96 return false;
97 val = CONST_WIDE_INT_ELT (x, elts - 1);
98 width %= HOST_BITS_PER_WIDE_INT;
99 if (width == 0)
100 width = HOST_BITS_PER_WIDE_INT;
101 }
102 #else
103 else if (width <= HOST_BITS_PER_DOUBLE_INT
104 && CONST_DOUBLE_AS_INT_P (x)
105 && CONST_DOUBLE_LOW (x) == 0)
106 {
107 val = CONST_DOUBLE_HIGH (x);
108 width -= HOST_BITS_PER_WIDE_INT;
109 }
110 #endif
111 else
112 /* X is not an integer constant. */
113 return false;
114
115 if (width < HOST_BITS_PER_WIDE_INT)
116 val &= (HOST_WIDE_INT_1U << width) - 1;
117 return val == (HOST_WIDE_INT_1U << (width - 1));
118 }
119
120 /* Test whether VAL is equal to the most significant bit of mode MODE
121 (after masking with the mode mask of MODE). Returns false if the
122 precision of MODE is too large to handle. */
123
124 bool
125 val_signbit_p (machine_mode mode, unsigned HOST_WIDE_INT val)
126 {
127 unsigned int width;
128 scalar_int_mode int_mode;
129
130 if (!is_int_mode (mode, &int_mode))
131 return false;
132
133 width = GET_MODE_PRECISION (int_mode);
134 if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
135 return false;
136
137 val &= GET_MODE_MASK (int_mode);
138 return val == (HOST_WIDE_INT_1U << (width - 1));
139 }
140
141 /* Test whether the most significant bit of mode MODE is set in VAL.
142 Returns false if the precision of MODE is too large to handle. */
143 bool
144 val_signbit_known_set_p (machine_mode mode, unsigned HOST_WIDE_INT val)
145 {
146 unsigned int width;
147
148 scalar_int_mode int_mode;
149 if (!is_int_mode (mode, &int_mode))
150 return false;
151
152 width = GET_MODE_PRECISION (int_mode);
153 if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
154 return false;
155
156 val &= HOST_WIDE_INT_1U << (width - 1);
157 return val != 0;
158 }
159
160 /* Test whether the most significant bit of mode MODE is clear in VAL.
161 Returns false if the precision of MODE is too large to handle. */
162 bool
163 val_signbit_known_clear_p (machine_mode mode, unsigned HOST_WIDE_INT val)
164 {
165 unsigned int width;
166
167 scalar_int_mode int_mode;
168 if (!is_int_mode (mode, &int_mode))
169 return false;
170
171 width = GET_MODE_PRECISION (int_mode);
172 if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
173 return false;
174
175 val &= HOST_WIDE_INT_1U << (width - 1);
176 return val == 0;
177 }
178 \f
179 /* Make a binary operation by properly ordering the operands and
180 seeing if the expression folds. */
181
182 rtx
183 simplify_gen_binary (enum rtx_code code, machine_mode mode, rtx op0,
184 rtx op1)
185 {
186 rtx tem;
187
188 /* If this simplifies, do it. */
189 tem = simplify_binary_operation (code, mode, op0, op1);
190 if (tem)
191 return tem;
192
193 /* Put complex operands first and constants second if commutative. */
194 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
195 && swap_commutative_operands_p (op0, op1))
196 std::swap (op0, op1);
197
198 return gen_rtx_fmt_ee (code, mode, op0, op1);
199 }
200 \f
201 /* If X is a MEM referencing the constant pool, return the real value.
202 Otherwise return X. */
203 rtx
204 avoid_constant_pool_reference (rtx x)
205 {
206 rtx c, tmp, addr;
207 machine_mode cmode;
208 poly_int64 offset = 0;
209
210 switch (GET_CODE (x))
211 {
212 case MEM:
213 break;
214
215 case FLOAT_EXTEND:
216 /* Handle float extensions of constant pool references. */
217 tmp = XEXP (x, 0);
218 c = avoid_constant_pool_reference (tmp);
219 if (c != tmp && CONST_DOUBLE_AS_FLOAT_P (c))
220 return const_double_from_real_value (*CONST_DOUBLE_REAL_VALUE (c),
221 GET_MODE (x));
222 return x;
223
224 default:
225 return x;
226 }
227
228 if (GET_MODE (x) == BLKmode)
229 return x;
230
231 addr = XEXP (x, 0);
232
233 /* Call target hook to avoid the effects of -fpic etc.... */
234 addr = targetm.delegitimize_address (addr);
235
236 /* Split the address into a base and integer offset. */
237 addr = strip_offset (addr, &offset);
238
239 if (GET_CODE (addr) == LO_SUM)
240 addr = XEXP (addr, 1);
241
242 /* If this is a constant pool reference, we can turn it into its
243 constant and hope that simplifications happen. */
244 if (GET_CODE (addr) == SYMBOL_REF
245 && CONSTANT_POOL_ADDRESS_P (addr))
246 {
247 c = get_pool_constant (addr);
248 cmode = get_pool_mode (addr);
249
250 /* If we're accessing the constant in a different mode than it was
251 originally stored, attempt to fix that up via subreg simplifications.
252 If that fails we have no choice but to return the original memory. */
253 if (known_eq (offset, 0) && cmode == GET_MODE (x))
254 return c;
255 else if (known_in_range_p (offset, 0, GET_MODE_SIZE (cmode)))
256 {
257 rtx tem = simplify_subreg (GET_MODE (x), c, cmode, offset);
258 if (tem && CONSTANT_P (tem))
259 return tem;
260 }
261 }
262
263 return x;
264 }
265 \f
266 /* Simplify a MEM based on its attributes. This is the default
267 delegitimize_address target hook, and it's recommended that every
268 overrider call it. */
269
270 rtx
271 delegitimize_mem_from_attrs (rtx x)
272 {
273 /* MEMs without MEM_OFFSETs may have been offset, so we can't just
274 use their base addresses as equivalent. */
275 if (MEM_P (x)
276 && MEM_EXPR (x)
277 && MEM_OFFSET_KNOWN_P (x))
278 {
279 tree decl = MEM_EXPR (x);
280 machine_mode mode = GET_MODE (x);
281 poly_int64 offset = 0;
282
283 switch (TREE_CODE (decl))
284 {
285 default:
286 decl = NULL;
287 break;
288
289 case VAR_DECL:
290 break;
291
292 case ARRAY_REF:
293 case ARRAY_RANGE_REF:
294 case COMPONENT_REF:
295 case BIT_FIELD_REF:
296 case REALPART_EXPR:
297 case IMAGPART_EXPR:
298 case VIEW_CONVERT_EXPR:
299 {
300 poly_int64 bitsize, bitpos, bytepos, toffset_val = 0;
301 tree toffset;
302 int unsignedp, reversep, volatilep = 0;
303
304 decl
305 = get_inner_reference (decl, &bitsize, &bitpos, &toffset, &mode,
306 &unsignedp, &reversep, &volatilep);
307 if (maybe_ne (bitsize, GET_MODE_BITSIZE (mode))
308 || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
309 || (toffset && !poly_int_tree_p (toffset, &toffset_val)))
310 decl = NULL;
311 else
312 offset += bytepos + toffset_val;
313 break;
314 }
315 }
316
317 if (decl
318 && mode == GET_MODE (x)
319 && VAR_P (decl)
320 && (TREE_STATIC (decl)
321 || DECL_THREAD_LOCAL_P (decl))
322 && DECL_RTL_SET_P (decl)
323 && MEM_P (DECL_RTL (decl)))
324 {
325 rtx newx;
326
327 offset += MEM_OFFSET (x);
328
329 newx = DECL_RTL (decl);
330
331 if (MEM_P (newx))
332 {
333 rtx n = XEXP (newx, 0), o = XEXP (x, 0);
334 poly_int64 n_offset, o_offset;
335
336 /* Avoid creating a new MEM needlessly if we already had
337 the same address. We do if there's no OFFSET and the
338 old address X is identical to NEWX, or if X is of the
339 form (plus NEWX OFFSET), or the NEWX is of the form
340 (plus Y (const_int Z)) and X is that with the offset
341 added: (plus Y (const_int Z+OFFSET)). */
342 n = strip_offset (n, &n_offset);
343 o = strip_offset (o, &o_offset);
344 if (!(known_eq (o_offset, n_offset + offset)
345 && rtx_equal_p (o, n)))
346 x = adjust_address_nv (newx, mode, offset);
347 }
348 else if (GET_MODE (x) == GET_MODE (newx)
349 && known_eq (offset, 0))
350 x = newx;
351 }
352 }
353
354 return x;
355 }
356 \f
357 /* Make a unary operation by first seeing if it folds and otherwise making
358 the specified operation. */
359
360 rtx
361 simplify_gen_unary (enum rtx_code code, machine_mode mode, rtx op,
362 machine_mode op_mode)
363 {
364 rtx tem;
365
366 /* If this simplifies, use it. */
367 if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
368 return tem;
369
370 return gen_rtx_fmt_e (code, mode, op);
371 }
372
373 /* Likewise for ternary operations. */
374
375 rtx
376 simplify_gen_ternary (enum rtx_code code, machine_mode mode,
377 machine_mode op0_mode, rtx op0, rtx op1, rtx op2)
378 {
379 rtx tem;
380
381 /* If this simplifies, use it. */
382 if ((tem = simplify_ternary_operation (code, mode, op0_mode,
383 op0, op1, op2)) != 0)
384 return tem;
385
386 return gen_rtx_fmt_eee (code, mode, op0, op1, op2);
387 }
388
389 /* Likewise, for relational operations.
390 CMP_MODE specifies mode comparison is done in. */
391
392 rtx
393 simplify_gen_relational (enum rtx_code code, machine_mode mode,
394 machine_mode cmp_mode, rtx op0, rtx op1)
395 {
396 rtx tem;
397
398 if ((tem = simplify_relational_operation (code, mode, cmp_mode,
399 op0, op1)) != 0)
400 return tem;
401
402 return gen_rtx_fmt_ee (code, mode, op0, op1);
403 }
404 \f
405 /* If FN is NULL, replace all occurrences of OLD_RTX in X with copy_rtx (DATA)
406 and simplify the result. If FN is non-NULL, call this callback on each
407 X, if it returns non-NULL, replace X with its return value and simplify the
408 result. */
409
410 rtx
411 simplify_replace_fn_rtx (rtx x, const_rtx old_rtx,
412 rtx (*fn) (rtx, const_rtx, void *), void *data)
413 {
414 enum rtx_code code = GET_CODE (x);
415 machine_mode mode = GET_MODE (x);
416 machine_mode op_mode;
417 const char *fmt;
418 rtx op0, op1, op2, newx, op;
419 rtvec vec, newvec;
420 int i, j;
421
422 if (__builtin_expect (fn != NULL, 0))
423 {
424 newx = fn (x, old_rtx, data);
425 if (newx)
426 return newx;
427 }
428 else if (rtx_equal_p (x, old_rtx))
429 return copy_rtx ((rtx) data);
430
431 switch (GET_RTX_CLASS (code))
432 {
433 case RTX_UNARY:
434 op0 = XEXP (x, 0);
435 op_mode = GET_MODE (op0);
436 op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
437 if (op0 == XEXP (x, 0))
438 return x;
439 return simplify_gen_unary (code, mode, op0, op_mode);
440
441 case RTX_BIN_ARITH:
442 case RTX_COMM_ARITH:
443 op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
444 op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
445 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
446 return x;
447 return simplify_gen_binary (code, mode, op0, op1);
448
449 case RTX_COMPARE:
450 case RTX_COMM_COMPARE:
451 op0 = XEXP (x, 0);
452 op1 = XEXP (x, 1);
453 op_mode = GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
454 op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
455 op1 = simplify_replace_fn_rtx (op1, old_rtx, fn, data);
456 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
457 return x;
458 return simplify_gen_relational (code, mode, op_mode, op0, op1);
459
460 case RTX_TERNARY:
461 case RTX_BITFIELD_OPS:
462 op0 = XEXP (x, 0);
463 op_mode = GET_MODE (op0);
464 op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
465 op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
466 op2 = simplify_replace_fn_rtx (XEXP (x, 2), old_rtx, fn, data);
467 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1) && op2 == XEXP (x, 2))
468 return x;
469 if (op_mode == VOIDmode)
470 op_mode = GET_MODE (op0);
471 return simplify_gen_ternary (code, mode, op_mode, op0, op1, op2);
472
473 case RTX_EXTRA:
474 if (code == SUBREG)
475 {
476 op0 = simplify_replace_fn_rtx (SUBREG_REG (x), old_rtx, fn, data);
477 if (op0 == SUBREG_REG (x))
478 return x;
479 op0 = simplify_gen_subreg (GET_MODE (x), op0,
480 GET_MODE (SUBREG_REG (x)),
481 SUBREG_BYTE (x));
482 return op0 ? op0 : x;
483 }
484 break;
485
486 case RTX_OBJ:
487 if (code == MEM)
488 {
489 op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
490 if (op0 == XEXP (x, 0))
491 return x;
492 return replace_equiv_address_nv (x, op0);
493 }
494 else if (code == LO_SUM)
495 {
496 op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
497 op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
498
499 /* (lo_sum (high x) y) -> y where x and y have the same base. */
500 if (GET_CODE (op0) == HIGH)
501 {
502 rtx base0, base1, offset0, offset1;
503 split_const (XEXP (op0, 0), &base0, &offset0);
504 split_const (op1, &base1, &offset1);
505 if (rtx_equal_p (base0, base1))
506 return op1;
507 }
508
509 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
510 return x;
511 return gen_rtx_LO_SUM (mode, op0, op1);
512 }
513 break;
514
515 default:
516 break;
517 }
518
519 newx = x;
520 fmt = GET_RTX_FORMAT (code);
521 for (i = 0; fmt[i]; i++)
522 switch (fmt[i])
523 {
524 case 'E':
525 vec = XVEC (x, i);
526 newvec = XVEC (newx, i);
527 for (j = 0; j < GET_NUM_ELEM (vec); j++)
528 {
529 op = simplify_replace_fn_rtx (RTVEC_ELT (vec, j),
530 old_rtx, fn, data);
531 if (op != RTVEC_ELT (vec, j))
532 {
533 if (newvec == vec)
534 {
535 newvec = shallow_copy_rtvec (vec);
536 if (x == newx)
537 newx = shallow_copy_rtx (x);
538 XVEC (newx, i) = newvec;
539 }
540 RTVEC_ELT (newvec, j) = op;
541 }
542 }
543 break;
544
545 case 'e':
546 if (XEXP (x, i))
547 {
548 op = simplify_replace_fn_rtx (XEXP (x, i), old_rtx, fn, data);
549 if (op != XEXP (x, i))
550 {
551 if (x == newx)
552 newx = shallow_copy_rtx (x);
553 XEXP (newx, i) = op;
554 }
555 }
556 break;
557 }
558 return newx;
559 }
560
561 /* Replace all occurrences of OLD_RTX in X with NEW_RTX and try to simplify the
562 resulting RTX. Return a new RTX which is as simplified as possible. */
563
564 rtx
565 simplify_replace_rtx (rtx x, const_rtx old_rtx, rtx new_rtx)
566 {
567 return simplify_replace_fn_rtx (x, old_rtx, 0, new_rtx);
568 }
569 \f
570 /* Try to simplify a MODE truncation of OP, which has OP_MODE.
571 Only handle cases where the truncated value is inherently an rvalue.
572
573 RTL provides two ways of truncating a value:
574
575 1. a lowpart subreg. This form is only a truncation when both
576 the outer and inner modes (here MODE and OP_MODE respectively)
577 are scalar integers, and only then when the subreg is used as
578 an rvalue.
579
580 It is only valid to form such truncating subregs if the
581 truncation requires no action by the target. The onus for
582 proving this is on the creator of the subreg -- e.g. the
583 caller to simplify_subreg or simplify_gen_subreg -- and typically
584 involves either TRULY_NOOP_TRUNCATION_MODES_P or truncated_to_mode.
585
586 2. a TRUNCATE. This form handles both scalar and compound integers.
587
588 The first form is preferred where valid. However, the TRUNCATE
589 handling in simplify_unary_operation turns the second form into the
590 first form when TRULY_NOOP_TRUNCATION_MODES_P or truncated_to_mode allow,
591 so it is generally safe to form rvalue truncations using:
592
593 simplify_gen_unary (TRUNCATE, ...)
594
595 and leave simplify_unary_operation to work out which representation
596 should be used.
597
598 Because of the proof requirements on (1), simplify_truncation must
599 also use simplify_gen_unary (TRUNCATE, ...) to truncate parts of OP,
600 regardless of whether the outer truncation came from a SUBREG or a
601 TRUNCATE. For example, if the caller has proven that an SImode
602 truncation of:
603
604 (and:DI X Y)
605
606 is a no-op and can be represented as a subreg, it does not follow
607 that SImode truncations of X and Y are also no-ops. On a target
608 like 64-bit MIPS that requires SImode values to be stored in
609 sign-extended form, an SImode truncation of:
610
611 (and:DI (reg:DI X) (const_int 63))
612
613 is trivially a no-op because only the lower 6 bits can be set.
614 However, X is still an arbitrary 64-bit number and so we cannot
615 assume that truncating it too is a no-op. */
616
617 static rtx
618 simplify_truncation (machine_mode mode, rtx op,
619 machine_mode op_mode)
620 {
621 unsigned int precision = GET_MODE_UNIT_PRECISION (mode);
622 unsigned int op_precision = GET_MODE_UNIT_PRECISION (op_mode);
623 scalar_int_mode int_mode, int_op_mode, subreg_mode;
624
625 gcc_assert (precision <= op_precision);
626
627 /* Optimize truncations of zero and sign extended values. */
628 if (GET_CODE (op) == ZERO_EXTEND
629 || GET_CODE (op) == SIGN_EXTEND)
630 {
631 /* There are three possibilities. If MODE is the same as the
632 origmode, we can omit both the extension and the subreg.
633 If MODE is not larger than the origmode, we can apply the
634 truncation without the extension. Finally, if the outermode
635 is larger than the origmode, we can just extend to the appropriate
636 mode. */
637 machine_mode origmode = GET_MODE (XEXP (op, 0));
638 if (mode == origmode)
639 return XEXP (op, 0);
640 else if (precision <= GET_MODE_UNIT_PRECISION (origmode))
641 return simplify_gen_unary (TRUNCATE, mode,
642 XEXP (op, 0), origmode);
643 else
644 return simplify_gen_unary (GET_CODE (op), mode,
645 XEXP (op, 0), origmode);
646 }
647
648 /* If the machine can perform operations in the truncated mode, distribute
649 the truncation, i.e. simplify (truncate:QI (op:SI (x:SI) (y:SI))) into
650 (op:QI (truncate:QI (x:SI)) (truncate:QI (y:SI))). */
651 if (1
652 && (!WORD_REGISTER_OPERATIONS || precision >= BITS_PER_WORD)
653 && (GET_CODE (op) == PLUS
654 || GET_CODE (op) == MINUS
655 || GET_CODE (op) == MULT))
656 {
657 rtx op0 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0), op_mode);
658 if (op0)
659 {
660 rtx op1 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 1), op_mode);
661 if (op1)
662 return simplify_gen_binary (GET_CODE (op), mode, op0, op1);
663 }
664 }
665
666 /* Simplify (truncate:QI (lshiftrt:SI (sign_extend:SI (x:QI)) C)) into
667 to (ashiftrt:QI (x:QI) C), where C is a suitable small constant and
668 the outer subreg is effectively a truncation to the original mode. */
669 if ((GET_CODE (op) == LSHIFTRT
670 || GET_CODE (op) == ASHIFTRT)
671 /* Ensure that OP_MODE is at least twice as wide as MODE
672 to avoid the possibility that an outer LSHIFTRT shifts by more
673 than the sign extension's sign_bit_copies and introduces zeros
674 into the high bits of the result. */
675 && 2 * precision <= op_precision
676 && CONST_INT_P (XEXP (op, 1))
677 && GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
678 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
679 && UINTVAL (XEXP (op, 1)) < precision)
680 return simplify_gen_binary (ASHIFTRT, mode,
681 XEXP (XEXP (op, 0), 0), XEXP (op, 1));
682
683 /* Likewise (truncate:QI (lshiftrt:SI (zero_extend:SI (x:QI)) C)) into
684 to (lshiftrt:QI (x:QI) C), where C is a suitable small constant and
685 the outer subreg is effectively a truncation to the original mode. */
686 if ((GET_CODE (op) == LSHIFTRT
687 || GET_CODE (op) == ASHIFTRT)
688 && CONST_INT_P (XEXP (op, 1))
689 && GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
690 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
691 && UINTVAL (XEXP (op, 1)) < precision)
692 return simplify_gen_binary (LSHIFTRT, mode,
693 XEXP (XEXP (op, 0), 0), XEXP (op, 1));
694
695 /* Likewise (truncate:QI (ashift:SI (zero_extend:SI (x:QI)) C)) into
696 to (ashift:QI (x:QI) C), where C is a suitable small constant and
697 the outer subreg is effectively a truncation to the original mode. */
698 if (GET_CODE (op) == ASHIFT
699 && CONST_INT_P (XEXP (op, 1))
700 && (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
701 || GET_CODE (XEXP (op, 0)) == SIGN_EXTEND)
702 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
703 && UINTVAL (XEXP (op, 1)) < precision)
704 return simplify_gen_binary (ASHIFT, mode,
705 XEXP (XEXP (op, 0), 0), XEXP (op, 1));
706
707 /* Likewise (truncate:QI (and:SI (lshiftrt:SI (x:SI) C) C2)) into
708 (and:QI (lshiftrt:QI (truncate:QI (x:SI)) C) C2) for suitable C
709 and C2. */
710 if (GET_CODE (op) == AND
711 && (GET_CODE (XEXP (op, 0)) == LSHIFTRT
712 || GET_CODE (XEXP (op, 0)) == ASHIFTRT)
713 && CONST_INT_P (XEXP (XEXP (op, 0), 1))
714 && CONST_INT_P (XEXP (op, 1)))
715 {
716 rtx op0 = (XEXP (XEXP (op, 0), 0));
717 rtx shift_op = XEXP (XEXP (op, 0), 1);
718 rtx mask_op = XEXP (op, 1);
719 unsigned HOST_WIDE_INT shift = UINTVAL (shift_op);
720 unsigned HOST_WIDE_INT mask = UINTVAL (mask_op);
721
722 if (shift < precision
723 /* If doing this transform works for an X with all bits set,
724 it works for any X. */
725 && ((GET_MODE_MASK (mode) >> shift) & mask)
726 == ((GET_MODE_MASK (op_mode) >> shift) & mask)
727 && (op0 = simplify_gen_unary (TRUNCATE, mode, op0, op_mode))
728 && (op0 = simplify_gen_binary (LSHIFTRT, mode, op0, shift_op)))
729 {
730 mask_op = GEN_INT (trunc_int_for_mode (mask, mode));
731 return simplify_gen_binary (AND, mode, op0, mask_op);
732 }
733 }
734
735 /* Turn (truncate:M1 (*_extract:M2 (reg:M2) (len) (pos))) into
736 (*_extract:M1 (truncate:M1 (reg:M2)) (len) (pos')) if possible without
737 changing len. */
738 if ((GET_CODE (op) == ZERO_EXTRACT || GET_CODE (op) == SIGN_EXTRACT)
739 && REG_P (XEXP (op, 0))
740 && GET_MODE (XEXP (op, 0)) == GET_MODE (op)
741 && CONST_INT_P (XEXP (op, 1))
742 && CONST_INT_P (XEXP (op, 2)))
743 {
744 rtx op0 = XEXP (op, 0);
745 unsigned HOST_WIDE_INT len = UINTVAL (XEXP (op, 1));
746 unsigned HOST_WIDE_INT pos = UINTVAL (XEXP (op, 2));
747 if (BITS_BIG_ENDIAN && pos >= op_precision - precision)
748 {
749 op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
750 if (op0)
751 {
752 pos -= op_precision - precision;
753 return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
754 XEXP (op, 1), GEN_INT (pos));
755 }
756 }
757 else if (!BITS_BIG_ENDIAN && precision >= len + pos)
758 {
759 op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
760 if (op0)
761 return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
762 XEXP (op, 1), XEXP (op, 2));
763 }
764 }
765
766 /* Recognize a word extraction from a multi-word subreg. */
767 if ((GET_CODE (op) == LSHIFTRT
768 || GET_CODE (op) == ASHIFTRT)
769 && SCALAR_INT_MODE_P (mode)
770 && SCALAR_INT_MODE_P (op_mode)
771 && precision >= BITS_PER_WORD
772 && 2 * precision <= op_precision
773 && CONST_INT_P (XEXP (op, 1))
774 && (INTVAL (XEXP (op, 1)) & (precision - 1)) == 0
775 && UINTVAL (XEXP (op, 1)) < op_precision)
776 {
777 poly_int64 byte = subreg_lowpart_offset (mode, op_mode);
778 int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
779 return simplify_gen_subreg (mode, XEXP (op, 0), op_mode,
780 (WORDS_BIG_ENDIAN
781 ? byte - shifted_bytes
782 : byte + shifted_bytes));
783 }
784
785 /* If we have a TRUNCATE of a right shift of MEM, make a new MEM
786 and try replacing the TRUNCATE and shift with it. Don't do this
787 if the MEM has a mode-dependent address. */
788 if ((GET_CODE (op) == LSHIFTRT
789 || GET_CODE (op) == ASHIFTRT)
790 && is_a <scalar_int_mode> (mode, &int_mode)
791 && is_a <scalar_int_mode> (op_mode, &int_op_mode)
792 && MEM_P (XEXP (op, 0))
793 && CONST_INT_P (XEXP (op, 1))
794 && INTVAL (XEXP (op, 1)) % GET_MODE_BITSIZE (int_mode) == 0
795 && INTVAL (XEXP (op, 1)) > 0
796 && INTVAL (XEXP (op, 1)) < GET_MODE_BITSIZE (int_op_mode)
797 && ! mode_dependent_address_p (XEXP (XEXP (op, 0), 0),
798 MEM_ADDR_SPACE (XEXP (op, 0)))
799 && ! MEM_VOLATILE_P (XEXP (op, 0))
800 && (GET_MODE_SIZE (int_mode) >= UNITS_PER_WORD
801 || WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN))
802 {
803 poly_int64 byte = subreg_lowpart_offset (int_mode, int_op_mode);
804 int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
805 return adjust_address_nv (XEXP (op, 0), int_mode,
806 (WORDS_BIG_ENDIAN
807 ? byte - shifted_bytes
808 : byte + shifted_bytes));
809 }
810
811 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
812 (OP:SI foo:SI) if OP is NEG or ABS. */
813 if ((GET_CODE (op) == ABS
814 || GET_CODE (op) == NEG)
815 && (GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
816 || GET_CODE (XEXP (op, 0)) == ZERO_EXTEND)
817 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
818 return simplify_gen_unary (GET_CODE (op), mode,
819 XEXP (XEXP (op, 0), 0), mode);
820
821 /* (truncate:A (subreg:B (truncate:C X) 0)) is
822 (truncate:A X). */
823 if (GET_CODE (op) == SUBREG
824 && is_a <scalar_int_mode> (mode, &int_mode)
825 && SCALAR_INT_MODE_P (op_mode)
826 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &subreg_mode)
827 && GET_CODE (SUBREG_REG (op)) == TRUNCATE
828 && subreg_lowpart_p (op))
829 {
830 rtx inner = XEXP (SUBREG_REG (op), 0);
831 if (GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (subreg_mode))
832 return simplify_gen_unary (TRUNCATE, int_mode, inner,
833 GET_MODE (inner));
834 else
835 /* If subreg above is paradoxical and C is narrower
836 than A, return (subreg:A (truncate:C X) 0). */
837 return simplify_gen_subreg (int_mode, SUBREG_REG (op), subreg_mode, 0);
838 }
839
840 /* (truncate:A (truncate:B X)) is (truncate:A X). */
841 if (GET_CODE (op) == TRUNCATE)
842 return simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0),
843 GET_MODE (XEXP (op, 0)));
844
845 /* (truncate:A (ior X C)) is (const_int -1) if C is equal to that already,
846 in mode A. */
847 if (GET_CODE (op) == IOR
848 && SCALAR_INT_MODE_P (mode)
849 && SCALAR_INT_MODE_P (op_mode)
850 && CONST_INT_P (XEXP (op, 1))
851 && trunc_int_for_mode (INTVAL (XEXP (op, 1)), mode) == -1)
852 return constm1_rtx;
853
854 return NULL_RTX;
855 }
856 \f
857 /* Try to simplify a unary operation CODE whose output mode is to be
858 MODE with input operand OP whose mode was originally OP_MODE.
859 Return zero if no simplification can be made. */
860 rtx
861 simplify_unary_operation (enum rtx_code code, machine_mode mode,
862 rtx op, machine_mode op_mode)
863 {
864 rtx trueop, tem;
865
866 trueop = avoid_constant_pool_reference (op);
867
868 tem = simplify_const_unary_operation (code, mode, trueop, op_mode);
869 if (tem)
870 return tem;
871
872 return simplify_unary_operation_1 (code, mode, op);
873 }
874
875 /* Return true if FLOAT or UNSIGNED_FLOAT operation OP is known
876 to be exact. */
877
878 static bool
879 exact_int_to_float_conversion_p (const_rtx op)
880 {
881 int out_bits = significand_size (GET_MODE_INNER (GET_MODE (op)));
882 machine_mode op0_mode = GET_MODE (XEXP (op, 0));
883 /* Constants shouldn't reach here. */
884 gcc_assert (op0_mode != VOIDmode);
885 int in_prec = GET_MODE_UNIT_PRECISION (op0_mode);
886 int in_bits = in_prec;
887 if (HWI_COMPUTABLE_MODE_P (op0_mode))
888 {
889 unsigned HOST_WIDE_INT nonzero = nonzero_bits (XEXP (op, 0), op0_mode);
890 if (GET_CODE (op) == FLOAT)
891 in_bits -= num_sign_bit_copies (XEXP (op, 0), op0_mode);
892 else if (GET_CODE (op) == UNSIGNED_FLOAT)
893 in_bits = wi::min_precision (wi::uhwi (nonzero, in_prec), UNSIGNED);
894 else
895 gcc_unreachable ();
896 in_bits -= wi::ctz (wi::uhwi (nonzero, in_prec));
897 }
898 return in_bits <= out_bits;
899 }
900
901 /* Perform some simplifications we can do even if the operands
902 aren't constant. */
903 static rtx
904 simplify_unary_operation_1 (enum rtx_code code, machine_mode mode, rtx op)
905 {
906 enum rtx_code reversed;
907 rtx temp, elt, base, step;
908 scalar_int_mode inner, int_mode, op_mode, op0_mode;
909
910 switch (code)
911 {
912 case NOT:
913 /* (not (not X)) == X. */
914 if (GET_CODE (op) == NOT)
915 return XEXP (op, 0);
916
917 /* (not (eq X Y)) == (ne X Y), etc. if BImode or the result of the
918 comparison is all ones. */
919 if (COMPARISON_P (op)
920 && (mode == BImode || STORE_FLAG_VALUE == -1)
921 && ((reversed = reversed_comparison_code (op, NULL)) != UNKNOWN))
922 return simplify_gen_relational (reversed, mode, VOIDmode,
923 XEXP (op, 0), XEXP (op, 1));
924
925 /* (not (plus X -1)) can become (neg X). */
926 if (GET_CODE (op) == PLUS
927 && XEXP (op, 1) == constm1_rtx)
928 return simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
929
930 /* Similarly, (not (neg X)) is (plus X -1). Only do this for
931 modes that have CONSTM1_RTX, i.e. MODE_INT, MODE_PARTIAL_INT
932 and MODE_VECTOR_INT. */
933 if (GET_CODE (op) == NEG && CONSTM1_RTX (mode))
934 return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
935 CONSTM1_RTX (mode));
936
937 /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
938 if (GET_CODE (op) == XOR
939 && CONST_INT_P (XEXP (op, 1))
940 && (temp = simplify_unary_operation (NOT, mode,
941 XEXP (op, 1), mode)) != 0)
942 return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
943
944 /* (not (plus X C)) for signbit C is (xor X D) with D = ~C. */
945 if (GET_CODE (op) == PLUS
946 && CONST_INT_P (XEXP (op, 1))
947 && mode_signbit_p (mode, XEXP (op, 1))
948 && (temp = simplify_unary_operation (NOT, mode,
949 XEXP (op, 1), mode)) != 0)
950 return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
951
952
953 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for
954 operands other than 1, but that is not valid. We could do a
955 similar simplification for (not (lshiftrt C X)) where C is
956 just the sign bit, but this doesn't seem common enough to
957 bother with. */
958 if (GET_CODE (op) == ASHIFT
959 && XEXP (op, 0) == const1_rtx)
960 {
961 temp = simplify_gen_unary (NOT, mode, const1_rtx, mode);
962 return simplify_gen_binary (ROTATE, mode, temp, XEXP (op, 1));
963 }
964
965 /* (not (ashiftrt foo C)) where C is the number of bits in FOO
966 minus 1 is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1,
967 so we can perform the above simplification. */
968 if (STORE_FLAG_VALUE == -1
969 && is_a <scalar_int_mode> (mode, &int_mode)
970 && GET_CODE (op) == ASHIFTRT
971 && CONST_INT_P (XEXP (op, 1))
972 && INTVAL (XEXP (op, 1)) == GET_MODE_PRECISION (int_mode) - 1)
973 return simplify_gen_relational (GE, int_mode, VOIDmode,
974 XEXP (op, 0), const0_rtx);
975
976
977 if (partial_subreg_p (op)
978 && subreg_lowpart_p (op)
979 && GET_CODE (SUBREG_REG (op)) == ASHIFT
980 && XEXP (SUBREG_REG (op), 0) == const1_rtx)
981 {
982 machine_mode inner_mode = GET_MODE (SUBREG_REG (op));
983 rtx x;
984
985 x = gen_rtx_ROTATE (inner_mode,
986 simplify_gen_unary (NOT, inner_mode, const1_rtx,
987 inner_mode),
988 XEXP (SUBREG_REG (op), 1));
989 temp = rtl_hooks.gen_lowpart_no_emit (mode, x);
990 if (temp)
991 return temp;
992 }
993
994 /* Apply De Morgan's laws to reduce number of patterns for machines
995 with negating logical insns (and-not, nand, etc.). If result has
996 only one NOT, put it first, since that is how the patterns are
997 coded. */
998 if (GET_CODE (op) == IOR || GET_CODE (op) == AND)
999 {
1000 rtx in1 = XEXP (op, 0), in2 = XEXP (op, 1);
1001 machine_mode op_mode;
1002
1003 op_mode = GET_MODE (in1);
1004 in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
1005
1006 op_mode = GET_MODE (in2);
1007 if (op_mode == VOIDmode)
1008 op_mode = mode;
1009 in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
1010
1011 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
1012 std::swap (in1, in2);
1013
1014 return gen_rtx_fmt_ee (GET_CODE (op) == IOR ? AND : IOR,
1015 mode, in1, in2);
1016 }
1017
1018 /* (not (bswap x)) -> (bswap (not x)). */
1019 if (GET_CODE (op) == BSWAP)
1020 {
1021 rtx x = simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1022 return simplify_gen_unary (BSWAP, mode, x, mode);
1023 }
1024 break;
1025
1026 case NEG:
1027 /* (neg (neg X)) == X. */
1028 if (GET_CODE (op) == NEG)
1029 return XEXP (op, 0);
1030
1031 /* (neg (x ? (neg y) : y)) == !x ? (neg y) : y.
1032 If comparison is not reversible use
1033 x ? y : (neg y). */
1034 if (GET_CODE (op) == IF_THEN_ELSE)
1035 {
1036 rtx cond = XEXP (op, 0);
1037 rtx true_rtx = XEXP (op, 1);
1038 rtx false_rtx = XEXP (op, 2);
1039
1040 if ((GET_CODE (true_rtx) == NEG
1041 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
1042 || (GET_CODE (false_rtx) == NEG
1043 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx)))
1044 {
1045 if (reversed_comparison_code (cond, NULL) != UNKNOWN)
1046 temp = reversed_comparison (cond, mode);
1047 else
1048 {
1049 temp = cond;
1050 std::swap (true_rtx, false_rtx);
1051 }
1052 return simplify_gen_ternary (IF_THEN_ELSE, mode,
1053 mode, temp, true_rtx, false_rtx);
1054 }
1055 }
1056
1057 /* (neg (plus X 1)) can become (not X). */
1058 if (GET_CODE (op) == PLUS
1059 && XEXP (op, 1) == const1_rtx)
1060 return simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1061
1062 /* Similarly, (neg (not X)) is (plus X 1). */
1063 if (GET_CODE (op) == NOT)
1064 return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
1065 CONST1_RTX (mode));
1066
1067 /* (neg (minus X Y)) can become (minus Y X). This transformation
1068 isn't safe for modes with signed zeros, since if X and Y are
1069 both +0, (minus Y X) is the same as (minus X Y). If the
1070 rounding mode is towards +infinity (or -infinity) then the two
1071 expressions will be rounded differently. */
1072 if (GET_CODE (op) == MINUS
1073 && !HONOR_SIGNED_ZEROS (mode)
1074 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1075 return simplify_gen_binary (MINUS, mode, XEXP (op, 1), XEXP (op, 0));
1076
1077 if (GET_CODE (op) == PLUS
1078 && !HONOR_SIGNED_ZEROS (mode)
1079 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1080 {
1081 /* (neg (plus A C)) is simplified to (minus -C A). */
1082 if (CONST_SCALAR_INT_P (XEXP (op, 1))
1083 || CONST_DOUBLE_AS_FLOAT_P (XEXP (op, 1)))
1084 {
1085 temp = simplify_unary_operation (NEG, mode, XEXP (op, 1), mode);
1086 if (temp)
1087 return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 0));
1088 }
1089
1090 /* (neg (plus A B)) is canonicalized to (minus (neg A) B). */
1091 temp = simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
1092 return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 1));
1093 }
1094
1095 /* (neg (mult A B)) becomes (mult A (neg B)).
1096 This works even for floating-point values. */
1097 if (GET_CODE (op) == MULT
1098 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1099 {
1100 temp = simplify_gen_unary (NEG, mode, XEXP (op, 1), mode);
1101 return simplify_gen_binary (MULT, mode, XEXP (op, 0), temp);
1102 }
1103
1104 /* NEG commutes with ASHIFT since it is multiplication. Only do
1105 this if we can then eliminate the NEG (e.g., if the operand
1106 is a constant). */
1107 if (GET_CODE (op) == ASHIFT)
1108 {
1109 temp = simplify_unary_operation (NEG, mode, XEXP (op, 0), mode);
1110 if (temp)
1111 return simplify_gen_binary (ASHIFT, mode, temp, XEXP (op, 1));
1112 }
1113
1114 /* (neg (ashiftrt X C)) can be replaced by (lshiftrt X C) when
1115 C is equal to the width of MODE minus 1. */
1116 if (GET_CODE (op) == ASHIFTRT
1117 && CONST_INT_P (XEXP (op, 1))
1118 && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1119 return simplify_gen_binary (LSHIFTRT, mode,
1120 XEXP (op, 0), XEXP (op, 1));
1121
1122 /* (neg (lshiftrt X C)) can be replaced by (ashiftrt X C) when
1123 C is equal to the width of MODE minus 1. */
1124 if (GET_CODE (op) == LSHIFTRT
1125 && CONST_INT_P (XEXP (op, 1))
1126 && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1127 return simplify_gen_binary (ASHIFTRT, mode,
1128 XEXP (op, 0), XEXP (op, 1));
1129
1130 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
1131 if (GET_CODE (op) == XOR
1132 && XEXP (op, 1) == const1_rtx
1133 && nonzero_bits (XEXP (op, 0), mode) == 1)
1134 return plus_constant (mode, XEXP (op, 0), -1);
1135
1136 /* (neg (lt x 0)) is (ashiftrt X C) if STORE_FLAG_VALUE is 1. */
1137 /* (neg (lt x 0)) is (lshiftrt X C) if STORE_FLAG_VALUE is -1. */
1138 if (GET_CODE (op) == LT
1139 && XEXP (op, 1) == const0_rtx
1140 && is_a <scalar_int_mode> (GET_MODE (XEXP (op, 0)), &inner))
1141 {
1142 int_mode = as_a <scalar_int_mode> (mode);
1143 int isize = GET_MODE_PRECISION (inner);
1144 if (STORE_FLAG_VALUE == 1)
1145 {
1146 temp = simplify_gen_binary (ASHIFTRT, inner, XEXP (op, 0),
1147 gen_int_shift_amount (inner,
1148 isize - 1));
1149 if (int_mode == inner)
1150 return temp;
1151 if (GET_MODE_PRECISION (int_mode) > isize)
1152 return simplify_gen_unary (SIGN_EXTEND, int_mode, temp, inner);
1153 return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1154 }
1155 else if (STORE_FLAG_VALUE == -1)
1156 {
1157 temp = simplify_gen_binary (LSHIFTRT, inner, XEXP (op, 0),
1158 gen_int_shift_amount (inner,
1159 isize - 1));
1160 if (int_mode == inner)
1161 return temp;
1162 if (GET_MODE_PRECISION (int_mode) > isize)
1163 return simplify_gen_unary (ZERO_EXTEND, int_mode, temp, inner);
1164 return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1165 }
1166 }
1167
1168 if (vec_series_p (op, &base, &step))
1169 {
1170 /* Only create a new series if we can simplify both parts. In other
1171 cases this isn't really a simplification, and it's not necessarily
1172 a win to replace a vector operation with a scalar operation. */
1173 scalar_mode inner_mode = GET_MODE_INNER (mode);
1174 base = simplify_unary_operation (NEG, inner_mode, base, inner_mode);
1175 if (base)
1176 {
1177 step = simplify_unary_operation (NEG, inner_mode,
1178 step, inner_mode);
1179 if (step)
1180 return gen_vec_series (mode, base, step);
1181 }
1182 }
1183 break;
1184
1185 case TRUNCATE:
1186 /* Don't optimize (lshiftrt (mult ...)) as it would interfere
1187 with the umulXi3_highpart patterns. */
1188 if (GET_CODE (op) == LSHIFTRT
1189 && GET_CODE (XEXP (op, 0)) == MULT)
1190 break;
1191
1192 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
1193 {
1194 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op)))
1195 {
1196 temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1197 if (temp)
1198 return temp;
1199 }
1200 /* We can't handle truncation to a partial integer mode here
1201 because we don't know the real bitsize of the partial
1202 integer mode. */
1203 break;
1204 }
1205
1206 if (GET_MODE (op) != VOIDmode)
1207 {
1208 temp = simplify_truncation (mode, op, GET_MODE (op));
1209 if (temp)
1210 return temp;
1211 }
1212
1213 /* If we know that the value is already truncated, we can
1214 replace the TRUNCATE with a SUBREG. */
1215 if (known_eq (GET_MODE_NUNITS (mode), 1)
1216 && (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op))
1217 || truncated_to_mode (mode, op)))
1218 {
1219 temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1220 if (temp)
1221 return temp;
1222 }
1223
1224 /* A truncate of a comparison can be replaced with a subreg if
1225 STORE_FLAG_VALUE permits. This is like the previous test,
1226 but it works even if the comparison is done in a mode larger
1227 than HOST_BITS_PER_WIDE_INT. */
1228 if (HWI_COMPUTABLE_MODE_P (mode)
1229 && COMPARISON_P (op)
1230 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
1231 {
1232 temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1233 if (temp)
1234 return temp;
1235 }
1236
1237 /* A truncate of a memory is just loading the low part of the memory
1238 if we are not changing the meaning of the address. */
1239 if (GET_CODE (op) == MEM
1240 && !VECTOR_MODE_P (mode)
1241 && !MEM_VOLATILE_P (op)
1242 && !mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op)))
1243 {
1244 temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1245 if (temp)
1246 return temp;
1247 }
1248
1249 break;
1250
1251 case FLOAT_TRUNCATE:
1252 if (DECIMAL_FLOAT_MODE_P (mode))
1253 break;
1254
1255 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
1256 if (GET_CODE (op) == FLOAT_EXTEND
1257 && GET_MODE (XEXP (op, 0)) == mode)
1258 return XEXP (op, 0);
1259
1260 /* (float_truncate:SF (float_truncate:DF foo:XF))
1261 = (float_truncate:SF foo:XF).
1262 This may eliminate double rounding, so it is unsafe.
1263
1264 (float_truncate:SF (float_extend:XF foo:DF))
1265 = (float_truncate:SF foo:DF).
1266
1267 (float_truncate:DF (float_extend:XF foo:SF))
1268 = (float_extend:DF foo:SF). */
1269 if ((GET_CODE (op) == FLOAT_TRUNCATE
1270 && flag_unsafe_math_optimizations)
1271 || GET_CODE (op) == FLOAT_EXTEND)
1272 return simplify_gen_unary (GET_MODE_UNIT_SIZE (GET_MODE (XEXP (op, 0)))
1273 > GET_MODE_UNIT_SIZE (mode)
1274 ? FLOAT_TRUNCATE : FLOAT_EXTEND,
1275 mode,
1276 XEXP (op, 0), mode);
1277
1278 /* (float_truncate (float x)) is (float x) */
1279 if ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1280 && (flag_unsafe_math_optimizations
1281 || exact_int_to_float_conversion_p (op)))
1282 return simplify_gen_unary (GET_CODE (op), mode,
1283 XEXP (op, 0),
1284 GET_MODE (XEXP (op, 0)));
1285
1286 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
1287 (OP:SF foo:SF) if OP is NEG or ABS. */
1288 if ((GET_CODE (op) == ABS
1289 || GET_CODE (op) == NEG)
1290 && GET_CODE (XEXP (op, 0)) == FLOAT_EXTEND
1291 && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
1292 return simplify_gen_unary (GET_CODE (op), mode,
1293 XEXP (XEXP (op, 0), 0), mode);
1294
1295 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
1296 is (float_truncate:SF x). */
1297 if (GET_CODE (op) == SUBREG
1298 && subreg_lowpart_p (op)
1299 && GET_CODE (SUBREG_REG (op)) == FLOAT_TRUNCATE)
1300 return SUBREG_REG (op);
1301 break;
1302
1303 case FLOAT_EXTEND:
1304 if (DECIMAL_FLOAT_MODE_P (mode))
1305 break;
1306
1307 /* (float_extend (float_extend x)) is (float_extend x)
1308
1309 (float_extend (float x)) is (float x) assuming that double
1310 rounding can't happen.
1311 */
1312 if (GET_CODE (op) == FLOAT_EXTEND
1313 || ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1314 && exact_int_to_float_conversion_p (op)))
1315 return simplify_gen_unary (GET_CODE (op), mode,
1316 XEXP (op, 0),
1317 GET_MODE (XEXP (op, 0)));
1318
1319 break;
1320
1321 case ABS:
1322 /* (abs (neg <foo>)) -> (abs <foo>) */
1323 if (GET_CODE (op) == NEG)
1324 return simplify_gen_unary (ABS, mode, XEXP (op, 0),
1325 GET_MODE (XEXP (op, 0)));
1326
1327 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
1328 do nothing. */
1329 if (GET_MODE (op) == VOIDmode)
1330 break;
1331
1332 /* If operand is something known to be positive, ignore the ABS. */
1333 if (GET_CODE (op) == FFS || GET_CODE (op) == ABS
1334 || val_signbit_known_clear_p (GET_MODE (op),
1335 nonzero_bits (op, GET_MODE (op))))
1336 return op;
1337
1338 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
1339 if (is_a <scalar_int_mode> (mode, &int_mode)
1340 && (num_sign_bit_copies (op, int_mode)
1341 == GET_MODE_PRECISION (int_mode)))
1342 return gen_rtx_NEG (int_mode, op);
1343
1344 break;
1345
1346 case FFS:
1347 /* (ffs (*_extend <X>)) = (ffs <X>) */
1348 if (GET_CODE (op) == SIGN_EXTEND
1349 || GET_CODE (op) == ZERO_EXTEND)
1350 return simplify_gen_unary (FFS, mode, XEXP (op, 0),
1351 GET_MODE (XEXP (op, 0)));
1352 break;
1353
1354 case POPCOUNT:
1355 switch (GET_CODE (op))
1356 {
1357 case BSWAP:
1358 case ZERO_EXTEND:
1359 /* (popcount (zero_extend <X>)) = (popcount <X>) */
1360 return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1361 GET_MODE (XEXP (op, 0)));
1362
1363 case ROTATE:
1364 case ROTATERT:
1365 /* Rotations don't affect popcount. */
1366 if (!side_effects_p (XEXP (op, 1)))
1367 return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1368 GET_MODE (XEXP (op, 0)));
1369 break;
1370
1371 default:
1372 break;
1373 }
1374 break;
1375
1376 case PARITY:
1377 switch (GET_CODE (op))
1378 {
1379 case NOT:
1380 case BSWAP:
1381 case ZERO_EXTEND:
1382 case SIGN_EXTEND:
1383 return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1384 GET_MODE (XEXP (op, 0)));
1385
1386 case ROTATE:
1387 case ROTATERT:
1388 /* Rotations don't affect parity. */
1389 if (!side_effects_p (XEXP (op, 1)))
1390 return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1391 GET_MODE (XEXP (op, 0)));
1392 break;
1393
1394 case PARITY:
1395 /* (parity (parity x)) -> parity (x). */
1396 return op;
1397
1398 default:
1399 break;
1400 }
1401 break;
1402
1403 case BSWAP:
1404 /* (bswap (bswap x)) -> x. */
1405 if (GET_CODE (op) == BSWAP)
1406 return XEXP (op, 0);
1407 break;
1408
1409 case FLOAT:
1410 /* (float (sign_extend <X>)) = (float <X>). */
1411 if (GET_CODE (op) == SIGN_EXTEND)
1412 return simplify_gen_unary (FLOAT, mode, XEXP (op, 0),
1413 GET_MODE (XEXP (op, 0)));
1414 break;
1415
1416 case SIGN_EXTEND:
1417 /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
1418 becomes just the MINUS if its mode is MODE. This allows
1419 folding switch statements on machines using casesi (such as
1420 the VAX). */
1421 if (GET_CODE (op) == TRUNCATE
1422 && GET_MODE (XEXP (op, 0)) == mode
1423 && GET_CODE (XEXP (op, 0)) == MINUS
1424 && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
1425 && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
1426 return XEXP (op, 0);
1427
1428 /* Extending a widening multiplication should be canonicalized to
1429 a wider widening multiplication. */
1430 if (GET_CODE (op) == MULT)
1431 {
1432 rtx lhs = XEXP (op, 0);
1433 rtx rhs = XEXP (op, 1);
1434 enum rtx_code lcode = GET_CODE (lhs);
1435 enum rtx_code rcode = GET_CODE (rhs);
1436
1437 /* Widening multiplies usually extend both operands, but sometimes
1438 they use a shift to extract a portion of a register. */
1439 if ((lcode == SIGN_EXTEND
1440 || (lcode == ASHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1441 && (rcode == SIGN_EXTEND
1442 || (rcode == ASHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1443 {
1444 machine_mode lmode = GET_MODE (lhs);
1445 machine_mode rmode = GET_MODE (rhs);
1446 int bits;
1447
1448 if (lcode == ASHIFTRT)
1449 /* Number of bits not shifted off the end. */
1450 bits = (GET_MODE_UNIT_PRECISION (lmode)
1451 - INTVAL (XEXP (lhs, 1)));
1452 else /* lcode == SIGN_EXTEND */
1453 /* Size of inner mode. */
1454 bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1455
1456 if (rcode == ASHIFTRT)
1457 bits += (GET_MODE_UNIT_PRECISION (rmode)
1458 - INTVAL (XEXP (rhs, 1)));
1459 else /* rcode == SIGN_EXTEND */
1460 bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1461
1462 /* We can only widen multiplies if the result is mathematiclly
1463 equivalent. I.e. if overflow was impossible. */
1464 if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1465 return simplify_gen_binary
1466 (MULT, mode,
1467 simplify_gen_unary (SIGN_EXTEND, mode, lhs, lmode),
1468 simplify_gen_unary (SIGN_EXTEND, mode, rhs, rmode));
1469 }
1470 }
1471
1472 /* Check for a sign extension of a subreg of a promoted
1473 variable, where the promotion is sign-extended, and the
1474 target mode is the same as the variable's promotion. */
1475 if (GET_CODE (op) == SUBREG
1476 && SUBREG_PROMOTED_VAR_P (op)
1477 && SUBREG_PROMOTED_SIGNED_P (op)
1478 && !paradoxical_subreg_p (mode, GET_MODE (SUBREG_REG (op))))
1479 {
1480 temp = rtl_hooks.gen_lowpart_no_emit (mode, SUBREG_REG (op));
1481 if (temp)
1482 return temp;
1483 }
1484
1485 /* (sign_extend:M (sign_extend:N <X>)) is (sign_extend:M <X>).
1486 (sign_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1487 if (GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND)
1488 {
1489 gcc_assert (GET_MODE_UNIT_PRECISION (mode)
1490 > GET_MODE_UNIT_PRECISION (GET_MODE (op)));
1491 return simplify_gen_unary (GET_CODE (op), mode, XEXP (op, 0),
1492 GET_MODE (XEXP (op, 0)));
1493 }
1494
1495 /* (sign_extend:M (ashiftrt:N (ashift <X> (const_int I)) (const_int I)))
1496 is (sign_extend:M (subreg:O <X>)) if there is mode with
1497 GET_MODE_BITSIZE (N) - I bits.
1498 (sign_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1499 is similarly (zero_extend:M (subreg:O <X>)). */
1500 if ((GET_CODE (op) == ASHIFTRT || GET_CODE (op) == LSHIFTRT)
1501 && GET_CODE (XEXP (op, 0)) == ASHIFT
1502 && is_a <scalar_int_mode> (mode, &int_mode)
1503 && CONST_INT_P (XEXP (op, 1))
1504 && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1505 && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1506 GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1507 {
1508 scalar_int_mode tmode;
1509 gcc_assert (GET_MODE_PRECISION (int_mode)
1510 > GET_MODE_PRECISION (op_mode));
1511 if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1512 - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1513 {
1514 rtx inner =
1515 rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1516 if (inner)
1517 return simplify_gen_unary (GET_CODE (op) == ASHIFTRT
1518 ? SIGN_EXTEND : ZERO_EXTEND,
1519 int_mode, inner, tmode);
1520 }
1521 }
1522
1523 /* (sign_extend:M (lshiftrt:N <X> (const_int I))) is better as
1524 (zero_extend:M (lshiftrt:N <X> (const_int I))) if I is not 0. */
1525 if (GET_CODE (op) == LSHIFTRT
1526 && CONST_INT_P (XEXP (op, 1))
1527 && XEXP (op, 1) != const0_rtx)
1528 return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1529
1530 #if defined(POINTERS_EXTEND_UNSIGNED)
1531 /* As we do not know which address space the pointer is referring to,
1532 we can do this only if the target does not support different pointer
1533 or address modes depending on the address space. */
1534 if (target_default_pointer_address_modes_p ()
1535 && ! POINTERS_EXTEND_UNSIGNED
1536 && mode == Pmode && GET_MODE (op) == ptr_mode
1537 && (CONSTANT_P (op)
1538 || (GET_CODE (op) == SUBREG
1539 && REG_P (SUBREG_REG (op))
1540 && REG_POINTER (SUBREG_REG (op))
1541 && GET_MODE (SUBREG_REG (op)) == Pmode))
1542 && !targetm.have_ptr_extend ())
1543 {
1544 temp
1545 = convert_memory_address_addr_space_1 (Pmode, op,
1546 ADDR_SPACE_GENERIC, false,
1547 true);
1548 if (temp)
1549 return temp;
1550 }
1551 #endif
1552 break;
1553
1554 case ZERO_EXTEND:
1555 /* Check for a zero extension of a subreg of a promoted
1556 variable, where the promotion is zero-extended, and the
1557 target mode is the same as the variable's promotion. */
1558 if (GET_CODE (op) == SUBREG
1559 && SUBREG_PROMOTED_VAR_P (op)
1560 && SUBREG_PROMOTED_UNSIGNED_P (op)
1561 && !paradoxical_subreg_p (mode, GET_MODE (SUBREG_REG (op))))
1562 {
1563 temp = rtl_hooks.gen_lowpart_no_emit (mode, SUBREG_REG (op));
1564 if (temp)
1565 return temp;
1566 }
1567
1568 /* Extending a widening multiplication should be canonicalized to
1569 a wider widening multiplication. */
1570 if (GET_CODE (op) == MULT)
1571 {
1572 rtx lhs = XEXP (op, 0);
1573 rtx rhs = XEXP (op, 1);
1574 enum rtx_code lcode = GET_CODE (lhs);
1575 enum rtx_code rcode = GET_CODE (rhs);
1576
1577 /* Widening multiplies usually extend both operands, but sometimes
1578 they use a shift to extract a portion of a register. */
1579 if ((lcode == ZERO_EXTEND
1580 || (lcode == LSHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1581 && (rcode == ZERO_EXTEND
1582 || (rcode == LSHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1583 {
1584 machine_mode lmode = GET_MODE (lhs);
1585 machine_mode rmode = GET_MODE (rhs);
1586 int bits;
1587
1588 if (lcode == LSHIFTRT)
1589 /* Number of bits not shifted off the end. */
1590 bits = (GET_MODE_UNIT_PRECISION (lmode)
1591 - INTVAL (XEXP (lhs, 1)));
1592 else /* lcode == ZERO_EXTEND */
1593 /* Size of inner mode. */
1594 bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1595
1596 if (rcode == LSHIFTRT)
1597 bits += (GET_MODE_UNIT_PRECISION (rmode)
1598 - INTVAL (XEXP (rhs, 1)));
1599 else /* rcode == ZERO_EXTEND */
1600 bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1601
1602 /* We can only widen multiplies if the result is mathematiclly
1603 equivalent. I.e. if overflow was impossible. */
1604 if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1605 return simplify_gen_binary
1606 (MULT, mode,
1607 simplify_gen_unary (ZERO_EXTEND, mode, lhs, lmode),
1608 simplify_gen_unary (ZERO_EXTEND, mode, rhs, rmode));
1609 }
1610 }
1611
1612 /* (zero_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1613 if (GET_CODE (op) == ZERO_EXTEND)
1614 return simplify_gen_unary (ZERO_EXTEND, mode, XEXP (op, 0),
1615 GET_MODE (XEXP (op, 0)));
1616
1617 /* (zero_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1618 is (zero_extend:M (subreg:O <X>)) if there is mode with
1619 GET_MODE_PRECISION (N) - I bits. */
1620 if (GET_CODE (op) == LSHIFTRT
1621 && GET_CODE (XEXP (op, 0)) == ASHIFT
1622 && is_a <scalar_int_mode> (mode, &int_mode)
1623 && CONST_INT_P (XEXP (op, 1))
1624 && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1625 && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1626 GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1627 {
1628 scalar_int_mode tmode;
1629 if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1630 - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1631 {
1632 rtx inner =
1633 rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1634 if (inner)
1635 return simplify_gen_unary (ZERO_EXTEND, int_mode,
1636 inner, tmode);
1637 }
1638 }
1639
1640 /* (zero_extend:M (subreg:N <X:O>)) is <X:O> (for M == O) or
1641 (zero_extend:M <X:O>), if X doesn't have any non-zero bits outside
1642 of mode N. E.g.
1643 (zero_extend:SI (subreg:QI (and:SI (reg:SI) (const_int 63)) 0)) is
1644 (and:SI (reg:SI) (const_int 63)). */
1645 if (partial_subreg_p (op)
1646 && is_a <scalar_int_mode> (mode, &int_mode)
1647 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &op0_mode)
1648 && GET_MODE_PRECISION (op0_mode) <= HOST_BITS_PER_WIDE_INT
1649 && GET_MODE_PRECISION (int_mode) >= GET_MODE_PRECISION (op0_mode)
1650 && subreg_lowpart_p (op)
1651 && (nonzero_bits (SUBREG_REG (op), op0_mode)
1652 & ~GET_MODE_MASK (GET_MODE (op))) == 0)
1653 {
1654 if (GET_MODE_PRECISION (int_mode) == GET_MODE_PRECISION (op0_mode))
1655 return SUBREG_REG (op);
1656 return simplify_gen_unary (ZERO_EXTEND, int_mode, SUBREG_REG (op),
1657 op0_mode);
1658 }
1659
1660 #if defined(POINTERS_EXTEND_UNSIGNED)
1661 /* As we do not know which address space the pointer is referring to,
1662 we can do this only if the target does not support different pointer
1663 or address modes depending on the address space. */
1664 if (target_default_pointer_address_modes_p ()
1665 && POINTERS_EXTEND_UNSIGNED > 0
1666 && mode == Pmode && GET_MODE (op) == ptr_mode
1667 && (CONSTANT_P (op)
1668 || (GET_CODE (op) == SUBREG
1669 && REG_P (SUBREG_REG (op))
1670 && REG_POINTER (SUBREG_REG (op))
1671 && GET_MODE (SUBREG_REG (op)) == Pmode))
1672 && !targetm.have_ptr_extend ())
1673 {
1674 temp
1675 = convert_memory_address_addr_space_1 (Pmode, op,
1676 ADDR_SPACE_GENERIC, false,
1677 true);
1678 if (temp)
1679 return temp;
1680 }
1681 #endif
1682 break;
1683
1684 default:
1685 break;
1686 }
1687
1688 if (VECTOR_MODE_P (mode)
1689 && vec_duplicate_p (op, &elt)
1690 && code != VEC_DUPLICATE)
1691 {
1692 /* Try applying the operator to ELT and see if that simplifies.
1693 We can duplicate the result if so.
1694
1695 The reason we don't use simplify_gen_unary is that it isn't
1696 necessarily a win to convert things like:
1697
1698 (neg:V (vec_duplicate:V (reg:S R)))
1699
1700 to:
1701
1702 (vec_duplicate:V (neg:S (reg:S R)))
1703
1704 The first might be done entirely in vector registers while the
1705 second might need a move between register files. */
1706 temp = simplify_unary_operation (code, GET_MODE_INNER (mode),
1707 elt, GET_MODE_INNER (GET_MODE (op)));
1708 if (temp)
1709 return gen_vec_duplicate (mode, temp);
1710 }
1711
1712 return 0;
1713 }
1714
1715 /* Try to compute the value of a unary operation CODE whose output mode is to
1716 be MODE with input operand OP whose mode was originally OP_MODE.
1717 Return zero if the value cannot be computed. */
1718 rtx
1719 simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
1720 rtx op, machine_mode op_mode)
1721 {
1722 scalar_int_mode result_mode;
1723
1724 if (code == VEC_DUPLICATE)
1725 {
1726 gcc_assert (VECTOR_MODE_P (mode));
1727 if (GET_MODE (op) != VOIDmode)
1728 {
1729 if (!VECTOR_MODE_P (GET_MODE (op)))
1730 gcc_assert (GET_MODE_INNER (mode) == GET_MODE (op));
1731 else
1732 gcc_assert (GET_MODE_INNER (mode) == GET_MODE_INNER
1733 (GET_MODE (op)));
1734 }
1735 if (CONST_SCALAR_INT_P (op) || CONST_DOUBLE_AS_FLOAT_P (op))
1736 return gen_const_vec_duplicate (mode, op);
1737 if (GET_CODE (op) == CONST_VECTOR
1738 && (CONST_VECTOR_DUPLICATE_P (op)
1739 || CONST_VECTOR_NUNITS (op).is_constant ()))
1740 {
1741 unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op)
1742 ? CONST_VECTOR_NPATTERNS (op)
1743 : CONST_VECTOR_NUNITS (op).to_constant ());
1744 gcc_assert (multiple_p (GET_MODE_NUNITS (mode), npatterns));
1745 rtx_vector_builder builder (mode, npatterns, 1);
1746 for (unsigned i = 0; i < npatterns; i++)
1747 builder.quick_push (CONST_VECTOR_ELT (op, i));
1748 return builder.build ();
1749 }
1750 }
1751
1752 if (VECTOR_MODE_P (mode)
1753 && GET_CODE (op) == CONST_VECTOR
1754 && known_eq (GET_MODE_NUNITS (mode), CONST_VECTOR_NUNITS (op)))
1755 {
1756 gcc_assert (GET_MODE (op) == op_mode);
1757
1758 rtx_vector_builder builder;
1759 if (!builder.new_unary_operation (mode, op, false))
1760 return 0;
1761
1762 unsigned int count = builder.encoded_nelts ();
1763 for (unsigned int i = 0; i < count; i++)
1764 {
1765 rtx x = simplify_unary_operation (code, GET_MODE_INNER (mode),
1766 CONST_VECTOR_ELT (op, i),
1767 GET_MODE_INNER (op_mode));
1768 if (!x || !valid_for_const_vector_p (mode, x))
1769 return 0;
1770 builder.quick_push (x);
1771 }
1772 return builder.build ();
1773 }
1774
1775 /* The order of these tests is critical so that, for example, we don't
1776 check the wrong mode (input vs. output) for a conversion operation,
1777 such as FIX. At some point, this should be simplified. */
1778
1779 if (code == FLOAT && CONST_SCALAR_INT_P (op))
1780 {
1781 REAL_VALUE_TYPE d;
1782
1783 if (op_mode == VOIDmode)
1784 {
1785 /* CONST_INT have VOIDmode as the mode. We assume that all
1786 the bits of the constant are significant, though, this is
1787 a dangerous assumption as many times CONST_INTs are
1788 created and used with garbage in the bits outside of the
1789 precision of the implied mode of the const_int. */
1790 op_mode = MAX_MODE_INT;
1791 }
1792
1793 real_from_integer (&d, mode, rtx_mode_t (op, op_mode), SIGNED);
1794
1795 /* Avoid the folding if flag_signaling_nans is on and
1796 operand is a signaling NaN. */
1797 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
1798 return 0;
1799
1800 d = real_value_truncate (mode, d);
1801 return const_double_from_real_value (d, mode);
1802 }
1803 else if (code == UNSIGNED_FLOAT && CONST_SCALAR_INT_P (op))
1804 {
1805 REAL_VALUE_TYPE d;
1806
1807 if (op_mode == VOIDmode)
1808 {
1809 /* CONST_INT have VOIDmode as the mode. We assume that all
1810 the bits of the constant are significant, though, this is
1811 a dangerous assumption as many times CONST_INTs are
1812 created and used with garbage in the bits outside of the
1813 precision of the implied mode of the const_int. */
1814 op_mode = MAX_MODE_INT;
1815 }
1816
1817 real_from_integer (&d, mode, rtx_mode_t (op, op_mode), UNSIGNED);
1818
1819 /* Avoid the folding if flag_signaling_nans is on and
1820 operand is a signaling NaN. */
1821 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
1822 return 0;
1823
1824 d = real_value_truncate (mode, d);
1825 return const_double_from_real_value (d, mode);
1826 }
1827
1828 if (CONST_SCALAR_INT_P (op) && is_a <scalar_int_mode> (mode, &result_mode))
1829 {
1830 unsigned int width = GET_MODE_PRECISION (result_mode);
1831 if (width > MAX_BITSIZE_MODE_ANY_INT)
1832 return 0;
1833
1834 wide_int result;
1835 scalar_int_mode imode = (op_mode == VOIDmode
1836 ? result_mode
1837 : as_a <scalar_int_mode> (op_mode));
1838 rtx_mode_t op0 = rtx_mode_t (op, imode);
1839 int int_value;
1840
1841 #if TARGET_SUPPORTS_WIDE_INT == 0
1842 /* This assert keeps the simplification from producing a result
1843 that cannot be represented in a CONST_DOUBLE but a lot of
1844 upstream callers expect that this function never fails to
1845 simplify something and so you if you added this to the test
1846 above the code would die later anyway. If this assert
1847 happens, you just need to make the port support wide int. */
1848 gcc_assert (width <= HOST_BITS_PER_DOUBLE_INT);
1849 #endif
1850
1851 switch (code)
1852 {
1853 case NOT:
1854 result = wi::bit_not (op0);
1855 break;
1856
1857 case NEG:
1858 result = wi::neg (op0);
1859 break;
1860
1861 case ABS:
1862 result = wi::abs (op0);
1863 break;
1864
1865 case FFS:
1866 result = wi::shwi (wi::ffs (op0), result_mode);
1867 break;
1868
1869 case CLZ:
1870 if (wi::ne_p (op0, 0))
1871 int_value = wi::clz (op0);
1872 else if (! CLZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
1873 return NULL_RTX;
1874 result = wi::shwi (int_value, result_mode);
1875 break;
1876
1877 case CLRSB:
1878 result = wi::shwi (wi::clrsb (op0), result_mode);
1879 break;
1880
1881 case CTZ:
1882 if (wi::ne_p (op0, 0))
1883 int_value = wi::ctz (op0);
1884 else if (! CTZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
1885 return NULL_RTX;
1886 result = wi::shwi (int_value, result_mode);
1887 break;
1888
1889 case POPCOUNT:
1890 result = wi::shwi (wi::popcount (op0), result_mode);
1891 break;
1892
1893 case PARITY:
1894 result = wi::shwi (wi::parity (op0), result_mode);
1895 break;
1896
1897 case BSWAP:
1898 result = wide_int (op0).bswap ();
1899 break;
1900
1901 case TRUNCATE:
1902 case ZERO_EXTEND:
1903 result = wide_int::from (op0, width, UNSIGNED);
1904 break;
1905
1906 case SIGN_EXTEND:
1907 result = wide_int::from (op0, width, SIGNED);
1908 break;
1909
1910 case SQRT:
1911 default:
1912 return 0;
1913 }
1914
1915 return immed_wide_int_const (result, result_mode);
1916 }
1917
1918 else if (CONST_DOUBLE_AS_FLOAT_P (op)
1919 && SCALAR_FLOAT_MODE_P (mode)
1920 && SCALAR_FLOAT_MODE_P (GET_MODE (op)))
1921 {
1922 REAL_VALUE_TYPE d = *CONST_DOUBLE_REAL_VALUE (op);
1923 switch (code)
1924 {
1925 case SQRT:
1926 return 0;
1927 case ABS:
1928 d = real_value_abs (&d);
1929 break;
1930 case NEG:
1931 d = real_value_negate (&d);
1932 break;
1933 case FLOAT_TRUNCATE:
1934 /* Don't perform the operation if flag_signaling_nans is on
1935 and the operand is a signaling NaN. */
1936 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
1937 return NULL_RTX;
1938 d = real_value_truncate (mode, d);
1939 break;
1940 case FLOAT_EXTEND:
1941 /* Don't perform the operation if flag_signaling_nans is on
1942 and the operand is a signaling NaN. */
1943 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
1944 return NULL_RTX;
1945 /* All this does is change the mode, unless changing
1946 mode class. */
1947 if (GET_MODE_CLASS (mode) != GET_MODE_CLASS (GET_MODE (op)))
1948 real_convert (&d, mode, &d);
1949 break;
1950 case FIX:
1951 /* Don't perform the operation if flag_signaling_nans is on
1952 and the operand is a signaling NaN. */
1953 if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
1954 return NULL_RTX;
1955 real_arithmetic (&d, FIX_TRUNC_EXPR, &d, NULL);
1956 break;
1957 case NOT:
1958 {
1959 long tmp[4];
1960 int i;
1961
1962 real_to_target (tmp, &d, GET_MODE (op));
1963 for (i = 0; i < 4; i++)
1964 tmp[i] = ~tmp[i];
1965 real_from_target (&d, tmp, mode);
1966 break;
1967 }
1968 default:
1969 gcc_unreachable ();
1970 }
1971 return const_double_from_real_value (d, mode);
1972 }
1973 else if (CONST_DOUBLE_AS_FLOAT_P (op)
1974 && SCALAR_FLOAT_MODE_P (GET_MODE (op))
1975 && is_int_mode (mode, &result_mode))
1976 {
1977 unsigned int width = GET_MODE_PRECISION (result_mode);
1978 if (width > MAX_BITSIZE_MODE_ANY_INT)
1979 return 0;
1980
1981 /* Although the overflow semantics of RTL's FIX and UNSIGNED_FIX
1982 operators are intentionally left unspecified (to ease implementation
1983 by target backends), for consistency, this routine implements the
1984 same semantics for constant folding as used by the middle-end. */
1985
1986 /* This was formerly used only for non-IEEE float.
1987 eggert@twinsun.com says it is safe for IEEE also. */
1988 REAL_VALUE_TYPE t;
1989 const REAL_VALUE_TYPE *x = CONST_DOUBLE_REAL_VALUE (op);
1990 wide_int wmax, wmin;
1991 /* This is part of the abi to real_to_integer, but we check
1992 things before making this call. */
1993 bool fail;
1994
1995 switch (code)
1996 {
1997 case FIX:
1998 if (REAL_VALUE_ISNAN (*x))
1999 return const0_rtx;
2000
2001 /* Test against the signed upper bound. */
2002 wmax = wi::max_value (width, SIGNED);
2003 real_from_integer (&t, VOIDmode, wmax, SIGNED);
2004 if (real_less (&t, x))
2005 return immed_wide_int_const (wmax, mode);
2006
2007 /* Test against the signed lower bound. */
2008 wmin = wi::min_value (width, SIGNED);
2009 real_from_integer (&t, VOIDmode, wmin, SIGNED);
2010 if (real_less (x, &t))
2011 return immed_wide_int_const (wmin, mode);
2012
2013 return immed_wide_int_const (real_to_integer (x, &fail, width),
2014 mode);
2015
2016 case UNSIGNED_FIX:
2017 if (REAL_VALUE_ISNAN (*x) || REAL_VALUE_NEGATIVE (*x))
2018 return const0_rtx;
2019
2020 /* Test against the unsigned upper bound. */
2021 wmax = wi::max_value (width, UNSIGNED);
2022 real_from_integer (&t, VOIDmode, wmax, UNSIGNED);
2023 if (real_less (&t, x))
2024 return immed_wide_int_const (wmax, mode);
2025
2026 return immed_wide_int_const (real_to_integer (x, &fail, width),
2027 mode);
2028
2029 default:
2030 gcc_unreachable ();
2031 }
2032 }
2033
2034 /* Handle polynomial integers. */
2035 else if (CONST_POLY_INT_P (op))
2036 {
2037 poly_wide_int result;
2038 switch (code)
2039 {
2040 case NEG:
2041 result = -const_poly_int_value (op);
2042 break;
2043
2044 case NOT:
2045 result = ~const_poly_int_value (op);
2046 break;
2047
2048 default:
2049 return NULL_RTX;
2050 }
2051 return immed_wide_int_const (result, mode);
2052 }
2053
2054 return NULL_RTX;
2055 }
2056 \f
2057 /* Subroutine of simplify_binary_operation to simplify a binary operation
2058 CODE that can commute with byte swapping, with result mode MODE and
2059 operating on OP0 and OP1. CODE is currently one of AND, IOR or XOR.
2060 Return zero if no simplification or canonicalization is possible. */
2061
2062 static rtx
2063 simplify_byte_swapping_operation (enum rtx_code code, machine_mode mode,
2064 rtx op0, rtx op1)
2065 {
2066 rtx tem;
2067
2068 /* (op (bswap x) C1)) -> (bswap (op x C2)) with C2 swapped. */
2069 if (GET_CODE (op0) == BSWAP && CONST_SCALAR_INT_P (op1))
2070 {
2071 tem = simplify_gen_binary (code, mode, XEXP (op0, 0),
2072 simplify_gen_unary (BSWAP, mode, op1, mode));
2073 return simplify_gen_unary (BSWAP, mode, tem, mode);
2074 }
2075
2076 /* (op (bswap x) (bswap y)) -> (bswap (op x y)). */
2077 if (GET_CODE (op0) == BSWAP && GET_CODE (op1) == BSWAP)
2078 {
2079 tem = simplify_gen_binary (code, mode, XEXP (op0, 0), XEXP (op1, 0));
2080 return simplify_gen_unary (BSWAP, mode, tem, mode);
2081 }
2082
2083 return NULL_RTX;
2084 }
2085
2086 /* Subroutine of simplify_binary_operation to simplify a commutative,
2087 associative binary operation CODE with result mode MODE, operating
2088 on OP0 and OP1. CODE is currently one of PLUS, MULT, AND, IOR, XOR,
2089 SMIN, SMAX, UMIN or UMAX. Return zero if no simplification or
2090 canonicalization is possible. */
2091
2092 static rtx
2093 simplify_associative_operation (enum rtx_code code, machine_mode mode,
2094 rtx op0, rtx op1)
2095 {
2096 rtx tem;
2097
2098 /* Linearize the operator to the left. */
2099 if (GET_CODE (op1) == code)
2100 {
2101 /* "(a op b) op (c op d)" becomes "((a op b) op c) op d)". */
2102 if (GET_CODE (op0) == code)
2103 {
2104 tem = simplify_gen_binary (code, mode, op0, XEXP (op1, 0));
2105 return simplify_gen_binary (code, mode, tem, XEXP (op1, 1));
2106 }
2107
2108 /* "a op (b op c)" becomes "(b op c) op a". */
2109 if (! swap_commutative_operands_p (op1, op0))
2110 return simplify_gen_binary (code, mode, op1, op0);
2111
2112 std::swap (op0, op1);
2113 }
2114
2115 if (GET_CODE (op0) == code)
2116 {
2117 /* Canonicalize "(x op c) op y" as "(x op y) op c". */
2118 if (swap_commutative_operands_p (XEXP (op0, 1), op1))
2119 {
2120 tem = simplify_gen_binary (code, mode, XEXP (op0, 0), op1);
2121 return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2122 }
2123
2124 /* Attempt to simplify "(a op b) op c" as "a op (b op c)". */
2125 tem = simplify_binary_operation (code, mode, XEXP (op0, 1), op1);
2126 if (tem != 0)
2127 return simplify_gen_binary (code, mode, XEXP (op0, 0), tem);
2128
2129 /* Attempt to simplify "(a op b) op c" as "(a op c) op b". */
2130 tem = simplify_binary_operation (code, mode, XEXP (op0, 0), op1);
2131 if (tem != 0)
2132 return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2133 }
2134
2135 return 0;
2136 }
2137
2138 /* Return a mask describing the COMPARISON. */
2139 static int
2140 comparison_to_mask (enum rtx_code comparison)
2141 {
2142 switch (comparison)
2143 {
2144 case LT:
2145 return 8;
2146 case GT:
2147 return 4;
2148 case EQ:
2149 return 2;
2150 case UNORDERED:
2151 return 1;
2152
2153 case LTGT:
2154 return 12;
2155 case LE:
2156 return 10;
2157 case GE:
2158 return 6;
2159 case UNLT:
2160 return 9;
2161 case UNGT:
2162 return 5;
2163 case UNEQ:
2164 return 3;
2165
2166 case ORDERED:
2167 return 14;
2168 case NE:
2169 return 13;
2170 case UNLE:
2171 return 11;
2172 case UNGE:
2173 return 7;
2174
2175 default:
2176 gcc_unreachable ();
2177 }
2178 }
2179
2180 /* Return a comparison corresponding to the MASK. */
2181 static enum rtx_code
2182 mask_to_comparison (int mask)
2183 {
2184 switch (mask)
2185 {
2186 case 8:
2187 return LT;
2188 case 4:
2189 return GT;
2190 case 2:
2191 return EQ;
2192 case 1:
2193 return UNORDERED;
2194
2195 case 12:
2196 return LTGT;
2197 case 10:
2198 return LE;
2199 case 6:
2200 return GE;
2201 case 9:
2202 return UNLT;
2203 case 5:
2204 return UNGT;
2205 case 3:
2206 return UNEQ;
2207
2208 case 14:
2209 return ORDERED;
2210 case 13:
2211 return NE;
2212 case 11:
2213 return UNLE;
2214 case 7:
2215 return UNGE;
2216
2217 default:
2218 gcc_unreachable ();
2219 }
2220 }
2221
2222 /* Return true if CODE is valid for comparisons of mode MODE, false
2223 otherwise.
2224
2225 It is always safe to return false, even if the code was valid for the
2226 given mode as that will merely suppress optimizations. */
2227
2228 static bool
2229 comparison_code_valid_for_mode (enum rtx_code code, enum machine_mode mode)
2230 {
2231 switch (code)
2232 {
2233 /* These are valid for integral, floating and vector modes. */
2234 case NE:
2235 case EQ:
2236 case GE:
2237 case GT:
2238 case LE:
2239 case LT:
2240 return (INTEGRAL_MODE_P (mode)
2241 || FLOAT_MODE_P (mode)
2242 || VECTOR_MODE_P (mode));
2243
2244 /* These are valid for floating point modes. */
2245 case LTGT:
2246 case UNORDERED:
2247 case ORDERED:
2248 case UNEQ:
2249 case UNGE:
2250 case UNGT:
2251 case UNLE:
2252 case UNLT:
2253 return FLOAT_MODE_P (mode);
2254
2255 /* These are filtered out in simplify_logical_operation, but
2256 we check for them too as a matter of safety. They are valid
2257 for integral and vector modes. */
2258 case GEU:
2259 case GTU:
2260 case LEU:
2261 case LTU:
2262 return INTEGRAL_MODE_P (mode) || VECTOR_MODE_P (mode);
2263
2264 default:
2265 gcc_unreachable ();
2266 }
2267 }
2268
2269 /* Simplify a logical operation CODE with result mode MODE, operating on OP0
2270 and OP1, which should be both relational operations. Return 0 if no such
2271 simplification is possible. */
2272 rtx
2273 simplify_logical_relational_operation (enum rtx_code code, machine_mode mode,
2274 rtx op0, rtx op1)
2275 {
2276 /* We only handle IOR of two relational operations. */
2277 if (code != IOR)
2278 return 0;
2279
2280 if (!(COMPARISON_P (op0) && COMPARISON_P (op1)))
2281 return 0;
2282
2283 if (!(rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2284 && rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))))
2285 return 0;
2286
2287 enum rtx_code code0 = GET_CODE (op0);
2288 enum rtx_code code1 = GET_CODE (op1);
2289
2290 /* We don't handle unsigned comparisons currently. */
2291 if (code0 == LTU || code0 == GTU || code0 == LEU || code0 == GEU)
2292 return 0;
2293 if (code1 == LTU || code1 == GTU || code1 == LEU || code1 == GEU)
2294 return 0;
2295
2296 int mask0 = comparison_to_mask (code0);
2297 int mask1 = comparison_to_mask (code1);
2298
2299 int mask = mask0 | mask1;
2300
2301 if (mask == 15)
2302 return const_true_rtx;
2303
2304 code = mask_to_comparison (mask);
2305
2306 /* Many comparison codes are only valid for certain mode classes. */
2307 if (!comparison_code_valid_for_mode (code, mode))
2308 return 0;
2309
2310 op0 = XEXP (op1, 0);
2311 op1 = XEXP (op1, 1);
2312
2313 return simplify_gen_relational (code, mode, VOIDmode, op0, op1);
2314 }
2315
2316 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
2317 and OP1. Return 0 if no simplification is possible.
2318
2319 Don't use this for relational operations such as EQ or LT.
2320 Use simplify_relational_operation instead. */
2321 rtx
2322 simplify_binary_operation (enum rtx_code code, machine_mode mode,
2323 rtx op0, rtx op1)
2324 {
2325 rtx trueop0, trueop1;
2326 rtx tem;
2327
2328 /* Relational operations don't work here. We must know the mode
2329 of the operands in order to do the comparison correctly.
2330 Assuming a full word can give incorrect results.
2331 Consider comparing 128 with -128 in QImode. */
2332 gcc_assert (GET_RTX_CLASS (code) != RTX_COMPARE);
2333 gcc_assert (GET_RTX_CLASS (code) != RTX_COMM_COMPARE);
2334
2335 /* Make sure the constant is second. */
2336 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
2337 && swap_commutative_operands_p (op0, op1))
2338 std::swap (op0, op1);
2339
2340 trueop0 = avoid_constant_pool_reference (op0);
2341 trueop1 = avoid_constant_pool_reference (op1);
2342
2343 tem = simplify_const_binary_operation (code, mode, trueop0, trueop1);
2344 if (tem)
2345 return tem;
2346 tem = simplify_binary_operation_1 (code, mode, op0, op1, trueop0, trueop1);
2347
2348 if (tem)
2349 return tem;
2350
2351 /* If the above steps did not result in a simplification and op0 or op1
2352 were constant pool references, use the referenced constants directly. */
2353 if (trueop0 != op0 || trueop1 != op1)
2354 return simplify_gen_binary (code, mode, trueop0, trueop1);
2355
2356 return NULL_RTX;
2357 }
2358
2359 /* Subroutine of simplify_binary_operation_1 that looks for cases in
2360 which OP0 and OP1 are both vector series or vector duplicates
2361 (which are really just series with a step of 0). If so, try to
2362 form a new series by applying CODE to the bases and to the steps.
2363 Return null if no simplification is possible.
2364
2365 MODE is the mode of the operation and is known to be a vector
2366 integer mode. */
2367
2368 static rtx
2369 simplify_binary_operation_series (rtx_code code, machine_mode mode,
2370 rtx op0, rtx op1)
2371 {
2372 rtx base0, step0;
2373 if (vec_duplicate_p (op0, &base0))
2374 step0 = const0_rtx;
2375 else if (!vec_series_p (op0, &base0, &step0))
2376 return NULL_RTX;
2377
2378 rtx base1, step1;
2379 if (vec_duplicate_p (op1, &base1))
2380 step1 = const0_rtx;
2381 else if (!vec_series_p (op1, &base1, &step1))
2382 return NULL_RTX;
2383
2384 /* Only create a new series if we can simplify both parts. In other
2385 cases this isn't really a simplification, and it's not necessarily
2386 a win to replace a vector operation with a scalar operation. */
2387 scalar_mode inner_mode = GET_MODE_INNER (mode);
2388 rtx new_base = simplify_binary_operation (code, inner_mode, base0, base1);
2389 if (!new_base)
2390 return NULL_RTX;
2391
2392 rtx new_step = simplify_binary_operation (code, inner_mode, step0, step1);
2393 if (!new_step)
2394 return NULL_RTX;
2395
2396 return gen_vec_series (mode, new_base, new_step);
2397 }
2398
2399 /* Subroutine of simplify_binary_operation. Simplify a binary operation
2400 CODE with result mode MODE, operating on OP0 and OP1. If OP0 and/or
2401 OP1 are constant pool references, TRUEOP0 and TRUEOP1 represent the
2402 actual constants. */
2403
2404 static rtx
2405 simplify_binary_operation_1 (enum rtx_code code, machine_mode mode,
2406 rtx op0, rtx op1, rtx trueop0, rtx trueop1)
2407 {
2408 rtx tem, reversed, opleft, opright, elt0, elt1;
2409 HOST_WIDE_INT val;
2410 scalar_int_mode int_mode, inner_mode;
2411 poly_int64 offset;
2412
2413 /* Even if we can't compute a constant result,
2414 there are some cases worth simplifying. */
2415
2416 switch (code)
2417 {
2418 case PLUS:
2419 /* Maybe simplify x + 0 to x. The two expressions are equivalent
2420 when x is NaN, infinite, or finite and nonzero. They aren't
2421 when x is -0 and the rounding mode is not towards -infinity,
2422 since (-0) + 0 is then 0. */
2423 if (!HONOR_SIGNED_ZEROS (mode) && trueop1 == CONST0_RTX (mode))
2424 return op0;
2425
2426 /* ((-a) + b) -> (b - a) and similarly for (a + (-b)). These
2427 transformations are safe even for IEEE. */
2428 if (GET_CODE (op0) == NEG)
2429 return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
2430 else if (GET_CODE (op1) == NEG)
2431 return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
2432
2433 /* (~a) + 1 -> -a */
2434 if (INTEGRAL_MODE_P (mode)
2435 && GET_CODE (op0) == NOT
2436 && trueop1 == const1_rtx)
2437 return simplify_gen_unary (NEG, mode, XEXP (op0, 0), mode);
2438
2439 /* Handle both-operands-constant cases. We can only add
2440 CONST_INTs to constants since the sum of relocatable symbols
2441 can't be handled by most assemblers. Don't add CONST_INT
2442 to CONST_INT since overflow won't be computed properly if wider
2443 than HOST_BITS_PER_WIDE_INT. */
2444
2445 if ((GET_CODE (op0) == CONST
2446 || GET_CODE (op0) == SYMBOL_REF
2447 || GET_CODE (op0) == LABEL_REF)
2448 && poly_int_rtx_p (op1, &offset))
2449 return plus_constant (mode, op0, offset);
2450 else if ((GET_CODE (op1) == CONST
2451 || GET_CODE (op1) == SYMBOL_REF
2452 || GET_CODE (op1) == LABEL_REF)
2453 && poly_int_rtx_p (op0, &offset))
2454 return plus_constant (mode, op1, offset);
2455
2456 /* See if this is something like X * C - X or vice versa or
2457 if the multiplication is written as a shift. If so, we can
2458 distribute and make a new multiply, shift, or maybe just
2459 have X (if C is 2 in the example above). But don't make
2460 something more expensive than we had before. */
2461
2462 if (is_a <scalar_int_mode> (mode, &int_mode))
2463 {
2464 rtx lhs = op0, rhs = op1;
2465
2466 wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
2467 wide_int coeff1 = wi::one (GET_MODE_PRECISION (int_mode));
2468
2469 if (GET_CODE (lhs) == NEG)
2470 {
2471 coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
2472 lhs = XEXP (lhs, 0);
2473 }
2474 else if (GET_CODE (lhs) == MULT
2475 && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
2476 {
2477 coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
2478 lhs = XEXP (lhs, 0);
2479 }
2480 else if (GET_CODE (lhs) == ASHIFT
2481 && CONST_INT_P (XEXP (lhs, 1))
2482 && INTVAL (XEXP (lhs, 1)) >= 0
2483 && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
2484 {
2485 coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
2486 GET_MODE_PRECISION (int_mode));
2487 lhs = XEXP (lhs, 0);
2488 }
2489
2490 if (GET_CODE (rhs) == NEG)
2491 {
2492 coeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
2493 rhs = XEXP (rhs, 0);
2494 }
2495 else if (GET_CODE (rhs) == MULT
2496 && CONST_INT_P (XEXP (rhs, 1)))
2497 {
2498 coeff1 = rtx_mode_t (XEXP (rhs, 1), int_mode);
2499 rhs = XEXP (rhs, 0);
2500 }
2501 else if (GET_CODE (rhs) == ASHIFT
2502 && CONST_INT_P (XEXP (rhs, 1))
2503 && INTVAL (XEXP (rhs, 1)) >= 0
2504 && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
2505 {
2506 coeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
2507 GET_MODE_PRECISION (int_mode));
2508 rhs = XEXP (rhs, 0);
2509 }
2510
2511 if (rtx_equal_p (lhs, rhs))
2512 {
2513 rtx orig = gen_rtx_PLUS (int_mode, op0, op1);
2514 rtx coeff;
2515 bool speed = optimize_function_for_speed_p (cfun);
2516
2517 coeff = immed_wide_int_const (coeff0 + coeff1, int_mode);
2518
2519 tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
2520 return (set_src_cost (tem, int_mode, speed)
2521 <= set_src_cost (orig, int_mode, speed) ? tem : 0);
2522 }
2523 }
2524
2525 /* (plus (xor X C1) C2) is (xor X (C1^C2)) if C2 is signbit. */
2526 if (CONST_SCALAR_INT_P (op1)
2527 && GET_CODE (op0) == XOR
2528 && CONST_SCALAR_INT_P (XEXP (op0, 1))
2529 && mode_signbit_p (mode, op1))
2530 return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
2531 simplify_gen_binary (XOR, mode, op1,
2532 XEXP (op0, 1)));
2533
2534 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)). */
2535 if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2536 && GET_CODE (op0) == MULT
2537 && GET_CODE (XEXP (op0, 0)) == NEG)
2538 {
2539 rtx in1, in2;
2540
2541 in1 = XEXP (XEXP (op0, 0), 0);
2542 in2 = XEXP (op0, 1);
2543 return simplify_gen_binary (MINUS, mode, op1,
2544 simplify_gen_binary (MULT, mode,
2545 in1, in2));
2546 }
2547
2548 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
2549 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
2550 is 1. */
2551 if (COMPARISON_P (op0)
2552 && ((STORE_FLAG_VALUE == -1 && trueop1 == const1_rtx)
2553 || (STORE_FLAG_VALUE == 1 && trueop1 == constm1_rtx))
2554 && (reversed = reversed_comparison (op0, mode)))
2555 return
2556 simplify_gen_unary (NEG, mode, reversed, mode);
2557
2558 /* If one of the operands is a PLUS or a MINUS, see if we can
2559 simplify this by the associative law.
2560 Don't use the associative law for floating point.
2561 The inaccuracy makes it nonassociative,
2562 and subtle programs can break if operations are associated. */
2563
2564 if (INTEGRAL_MODE_P (mode)
2565 && (plus_minus_operand_p (op0)
2566 || plus_minus_operand_p (op1))
2567 && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
2568 return tem;
2569
2570 /* Reassociate floating point addition only when the user
2571 specifies associative math operations. */
2572 if (FLOAT_MODE_P (mode)
2573 && flag_associative_math)
2574 {
2575 tem = simplify_associative_operation (code, mode, op0, op1);
2576 if (tem)
2577 return tem;
2578 }
2579
2580 /* Handle vector series. */
2581 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
2582 {
2583 tem = simplify_binary_operation_series (code, mode, op0, op1);
2584 if (tem)
2585 return tem;
2586 }
2587 break;
2588
2589 case COMPARE:
2590 /* Convert (compare (gt (flags) 0) (lt (flags) 0)) to (flags). */
2591 if (((GET_CODE (op0) == GT && GET_CODE (op1) == LT)
2592 || (GET_CODE (op0) == GTU && GET_CODE (op1) == LTU))
2593 && XEXP (op0, 1) == const0_rtx && XEXP (op1, 1) == const0_rtx)
2594 {
2595 rtx xop00 = XEXP (op0, 0);
2596 rtx xop10 = XEXP (op1, 0);
2597
2598 if (GET_CODE (xop00) == CC0 && GET_CODE (xop10) == CC0)
2599 return xop00;
2600
2601 if (REG_P (xop00) && REG_P (xop10)
2602 && REGNO (xop00) == REGNO (xop10)
2603 && GET_MODE (xop00) == mode
2604 && GET_MODE (xop10) == mode
2605 && GET_MODE_CLASS (mode) == MODE_CC)
2606 return xop00;
2607 }
2608 break;
2609
2610 case MINUS:
2611 /* We can't assume x-x is 0 even with non-IEEE floating point,
2612 but since it is zero except in very strange circumstances, we
2613 will treat it as zero with -ffinite-math-only. */
2614 if (rtx_equal_p (trueop0, trueop1)
2615 && ! side_effects_p (op0)
2616 && (!FLOAT_MODE_P (mode) || !HONOR_NANS (mode)))
2617 return CONST0_RTX (mode);
2618
2619 /* Change subtraction from zero into negation. (0 - x) is the
2620 same as -x when x is NaN, infinite, or finite and nonzero.
2621 But if the mode has signed zeros, and does not round towards
2622 -infinity, then 0 - 0 is 0, not -0. */
2623 if (!HONOR_SIGNED_ZEROS (mode) && trueop0 == CONST0_RTX (mode))
2624 return simplify_gen_unary (NEG, mode, op1, mode);
2625
2626 /* (-1 - a) is ~a, unless the expression contains symbolic
2627 constants, in which case not retaining additions and
2628 subtractions could cause invalid assembly to be produced. */
2629 if (trueop0 == constm1_rtx
2630 && !contains_symbolic_reference_p (op1))
2631 return simplify_gen_unary (NOT, mode, op1, mode);
2632
2633 /* Subtracting 0 has no effect unless the mode has signed zeros
2634 and supports rounding towards -infinity. In such a case,
2635 0 - 0 is -0. */
2636 if (!(HONOR_SIGNED_ZEROS (mode)
2637 && HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2638 && trueop1 == CONST0_RTX (mode))
2639 return op0;
2640
2641 /* See if this is something like X * C - X or vice versa or
2642 if the multiplication is written as a shift. If so, we can
2643 distribute and make a new multiply, shift, or maybe just
2644 have X (if C is 2 in the example above). But don't make
2645 something more expensive than we had before. */
2646
2647 if (is_a <scalar_int_mode> (mode, &int_mode))
2648 {
2649 rtx lhs = op0, rhs = op1;
2650
2651 wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
2652 wide_int negcoeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
2653
2654 if (GET_CODE (lhs) == NEG)
2655 {
2656 coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
2657 lhs = XEXP (lhs, 0);
2658 }
2659 else if (GET_CODE (lhs) == MULT
2660 && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
2661 {
2662 coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
2663 lhs = XEXP (lhs, 0);
2664 }
2665 else if (GET_CODE (lhs) == ASHIFT
2666 && CONST_INT_P (XEXP (lhs, 1))
2667 && INTVAL (XEXP (lhs, 1)) >= 0
2668 && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
2669 {
2670 coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
2671 GET_MODE_PRECISION (int_mode));
2672 lhs = XEXP (lhs, 0);
2673 }
2674
2675 if (GET_CODE (rhs) == NEG)
2676 {
2677 negcoeff1 = wi::one (GET_MODE_PRECISION (int_mode));
2678 rhs = XEXP (rhs, 0);
2679 }
2680 else if (GET_CODE (rhs) == MULT
2681 && CONST_INT_P (XEXP (rhs, 1)))
2682 {
2683 negcoeff1 = wi::neg (rtx_mode_t (XEXP (rhs, 1), int_mode));
2684 rhs = XEXP (rhs, 0);
2685 }
2686 else if (GET_CODE (rhs) == ASHIFT
2687 && CONST_INT_P (XEXP (rhs, 1))
2688 && INTVAL (XEXP (rhs, 1)) >= 0
2689 && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
2690 {
2691 negcoeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
2692 GET_MODE_PRECISION (int_mode));
2693 negcoeff1 = -negcoeff1;
2694 rhs = XEXP (rhs, 0);
2695 }
2696
2697 if (rtx_equal_p (lhs, rhs))
2698 {
2699 rtx orig = gen_rtx_MINUS (int_mode, op0, op1);
2700 rtx coeff;
2701 bool speed = optimize_function_for_speed_p (cfun);
2702
2703 coeff = immed_wide_int_const (coeff0 + negcoeff1, int_mode);
2704
2705 tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
2706 return (set_src_cost (tem, int_mode, speed)
2707 <= set_src_cost (orig, int_mode, speed) ? tem : 0);
2708 }
2709 }
2710
2711 /* (a - (-b)) -> (a + b). True even for IEEE. */
2712 if (GET_CODE (op1) == NEG)
2713 return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
2714
2715 /* (-x - c) may be simplified as (-c - x). */
2716 if (GET_CODE (op0) == NEG
2717 && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1)))
2718 {
2719 tem = simplify_unary_operation (NEG, mode, op1, mode);
2720 if (tem)
2721 return simplify_gen_binary (MINUS, mode, tem, XEXP (op0, 0));
2722 }
2723
2724 if ((GET_CODE (op0) == CONST
2725 || GET_CODE (op0) == SYMBOL_REF
2726 || GET_CODE (op0) == LABEL_REF)
2727 && poly_int_rtx_p (op1, &offset))
2728 return plus_constant (mode, op0, trunc_int_for_mode (-offset, mode));
2729
2730 /* Don't let a relocatable value get a negative coeff. */
2731 if (poly_int_rtx_p (op1) && GET_MODE (op0) != VOIDmode)
2732 return simplify_gen_binary (PLUS, mode,
2733 op0,
2734 neg_poly_int_rtx (mode, op1));
2735
2736 /* (x - (x & y)) -> (x & ~y) */
2737 if (INTEGRAL_MODE_P (mode) && GET_CODE (op1) == AND)
2738 {
2739 if (rtx_equal_p (op0, XEXP (op1, 0)))
2740 {
2741 tem = simplify_gen_unary (NOT, mode, XEXP (op1, 1),
2742 GET_MODE (XEXP (op1, 1)));
2743 return simplify_gen_binary (AND, mode, op0, tem);
2744 }
2745 if (rtx_equal_p (op0, XEXP (op1, 1)))
2746 {
2747 tem = simplify_gen_unary (NOT, mode, XEXP (op1, 0),
2748 GET_MODE (XEXP (op1, 0)));
2749 return simplify_gen_binary (AND, mode, op0, tem);
2750 }
2751 }
2752
2753 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
2754 by reversing the comparison code if valid. */
2755 if (STORE_FLAG_VALUE == 1
2756 && trueop0 == const1_rtx
2757 && COMPARISON_P (op1)
2758 && (reversed = reversed_comparison (op1, mode)))
2759 return reversed;
2760
2761 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A). */
2762 if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2763 && GET_CODE (op1) == MULT
2764 && GET_CODE (XEXP (op1, 0)) == NEG)
2765 {
2766 rtx in1, in2;
2767
2768 in1 = XEXP (XEXP (op1, 0), 0);
2769 in2 = XEXP (op1, 1);
2770 return simplify_gen_binary (PLUS, mode,
2771 simplify_gen_binary (MULT, mode,
2772 in1, in2),
2773 op0);
2774 }
2775
2776 /* Canonicalize (minus (neg A) (mult B C)) to
2777 (minus (mult (neg B) C) A). */
2778 if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2779 && GET_CODE (op1) == MULT
2780 && GET_CODE (op0) == NEG)
2781 {
2782 rtx in1, in2;
2783
2784 in1 = simplify_gen_unary (NEG, mode, XEXP (op1, 0), mode);
2785 in2 = XEXP (op1, 1);
2786 return simplify_gen_binary (MINUS, mode,
2787 simplify_gen_binary (MULT, mode,
2788 in1, in2),
2789 XEXP (op0, 0));
2790 }
2791
2792 /* If one of the operands is a PLUS or a MINUS, see if we can
2793 simplify this by the associative law. This will, for example,
2794 canonicalize (minus A (plus B C)) to (minus (minus A B) C).
2795 Don't use the associative law for floating point.
2796 The inaccuracy makes it nonassociative,
2797 and subtle programs can break if operations are associated. */
2798
2799 if (INTEGRAL_MODE_P (mode)
2800 && (plus_minus_operand_p (op0)
2801 || plus_minus_operand_p (op1))
2802 && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
2803 return tem;
2804
2805 /* Handle vector series. */
2806 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
2807 {
2808 tem = simplify_binary_operation_series (code, mode, op0, op1);
2809 if (tem)
2810 return tem;
2811 }
2812 break;
2813
2814 case MULT:
2815 if (trueop1 == constm1_rtx)
2816 return simplify_gen_unary (NEG, mode, op0, mode);
2817
2818 if (GET_CODE (op0) == NEG)
2819 {
2820 rtx temp = simplify_unary_operation (NEG, mode, op1, mode);
2821 /* If op1 is a MULT as well and simplify_unary_operation
2822 just moved the NEG to the second operand, simplify_gen_binary
2823 below could through simplify_associative_operation move
2824 the NEG around again and recurse endlessly. */
2825 if (temp
2826 && GET_CODE (op1) == MULT
2827 && GET_CODE (temp) == MULT
2828 && XEXP (op1, 0) == XEXP (temp, 0)
2829 && GET_CODE (XEXP (temp, 1)) == NEG
2830 && XEXP (op1, 1) == XEXP (XEXP (temp, 1), 0))
2831 temp = NULL_RTX;
2832 if (temp)
2833 return simplify_gen_binary (MULT, mode, XEXP (op0, 0), temp);
2834 }
2835 if (GET_CODE (op1) == NEG)
2836 {
2837 rtx temp = simplify_unary_operation (NEG, mode, op0, mode);
2838 /* If op0 is a MULT as well and simplify_unary_operation
2839 just moved the NEG to the second operand, simplify_gen_binary
2840 below could through simplify_associative_operation move
2841 the NEG around again and recurse endlessly. */
2842 if (temp
2843 && GET_CODE (op0) == MULT
2844 && GET_CODE (temp) == MULT
2845 && XEXP (op0, 0) == XEXP (temp, 0)
2846 && GET_CODE (XEXP (temp, 1)) == NEG
2847 && XEXP (op0, 1) == XEXP (XEXP (temp, 1), 0))
2848 temp = NULL_RTX;
2849 if (temp)
2850 return simplify_gen_binary (MULT, mode, temp, XEXP (op1, 0));
2851 }
2852
2853 /* Maybe simplify x * 0 to 0. The reduction is not valid if
2854 x is NaN, since x * 0 is then also NaN. Nor is it valid
2855 when the mode has signed zeros, since multiplying a negative
2856 number by 0 will give -0, not 0. */
2857 if (!HONOR_NANS (mode)
2858 && !HONOR_SIGNED_ZEROS (mode)
2859 && trueop1 == CONST0_RTX (mode)
2860 && ! side_effects_p (op0))
2861 return op1;
2862
2863 /* In IEEE floating point, x*1 is not equivalent to x for
2864 signalling NaNs. */
2865 if (!HONOR_SNANS (mode)
2866 && trueop1 == CONST1_RTX (mode))
2867 return op0;
2868
2869 /* Convert multiply by constant power of two into shift. */
2870 if (CONST_SCALAR_INT_P (trueop1))
2871 {
2872 val = wi::exact_log2 (rtx_mode_t (trueop1, mode));
2873 if (val >= 0)
2874 return simplify_gen_binary (ASHIFT, mode, op0,
2875 gen_int_shift_amount (mode, val));
2876 }
2877
2878 /* x*2 is x+x and x*(-1) is -x */
2879 if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
2880 && SCALAR_FLOAT_MODE_P (GET_MODE (trueop1))
2881 && !DECIMAL_FLOAT_MODE_P (GET_MODE (trueop1))
2882 && GET_MODE (op0) == mode)
2883 {
2884 const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
2885
2886 if (real_equal (d1, &dconst2))
2887 return simplify_gen_binary (PLUS, mode, op0, copy_rtx (op0));
2888
2889 if (!HONOR_SNANS (mode)
2890 && real_equal (d1, &dconstm1))
2891 return simplify_gen_unary (NEG, mode, op0, mode);
2892 }
2893
2894 /* Optimize -x * -x as x * x. */
2895 if (FLOAT_MODE_P (mode)
2896 && GET_CODE (op0) == NEG
2897 && GET_CODE (op1) == NEG
2898 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2899 && !side_effects_p (XEXP (op0, 0)))
2900 return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
2901
2902 /* Likewise, optimize abs(x) * abs(x) as x * x. */
2903 if (SCALAR_FLOAT_MODE_P (mode)
2904 && GET_CODE (op0) == ABS
2905 && GET_CODE (op1) == ABS
2906 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2907 && !side_effects_p (XEXP (op0, 0)))
2908 return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
2909
2910 /* Reassociate multiplication, but for floating point MULTs
2911 only when the user specifies unsafe math optimizations. */
2912 if (! FLOAT_MODE_P (mode)
2913 || flag_unsafe_math_optimizations)
2914 {
2915 tem = simplify_associative_operation (code, mode, op0, op1);
2916 if (tem)
2917 return tem;
2918 }
2919 break;
2920
2921 case IOR:
2922 if (trueop1 == CONST0_RTX (mode))
2923 return op0;
2924 if (INTEGRAL_MODE_P (mode)
2925 && trueop1 == CONSTM1_RTX (mode)
2926 && !side_effects_p (op0))
2927 return op1;
2928 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
2929 return op0;
2930 /* A | (~A) -> -1 */
2931 if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
2932 || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
2933 && ! side_effects_p (op0)
2934 && SCALAR_INT_MODE_P (mode))
2935 return constm1_rtx;
2936
2937 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
2938 if (CONST_INT_P (op1)
2939 && HWI_COMPUTABLE_MODE_P (mode)
2940 && (nonzero_bits (op0, mode) & ~UINTVAL (op1)) == 0
2941 && !side_effects_p (op0))
2942 return op1;
2943
2944 /* Canonicalize (X & C1) | C2. */
2945 if (GET_CODE (op0) == AND
2946 && CONST_INT_P (trueop1)
2947 && CONST_INT_P (XEXP (op0, 1)))
2948 {
2949 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
2950 HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
2951 HOST_WIDE_INT c2 = INTVAL (trueop1);
2952
2953 /* If (C1&C2) == C1, then (X&C1)|C2 becomes C2. */
2954 if ((c1 & c2) == c1
2955 && !side_effects_p (XEXP (op0, 0)))
2956 return trueop1;
2957
2958 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
2959 if (((c1|c2) & mask) == mask)
2960 return simplify_gen_binary (IOR, mode, XEXP (op0, 0), op1);
2961 }
2962
2963 /* Convert (A & B) | A to A. */
2964 if (GET_CODE (op0) == AND
2965 && (rtx_equal_p (XEXP (op0, 0), op1)
2966 || rtx_equal_p (XEXP (op0, 1), op1))
2967 && ! side_effects_p (XEXP (op0, 0))
2968 && ! side_effects_p (XEXP (op0, 1)))
2969 return op1;
2970
2971 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
2972 mode size to (rotate A CX). */
2973
2974 if (GET_CODE (op1) == ASHIFT
2975 || GET_CODE (op1) == SUBREG)
2976 {
2977 opleft = op1;
2978 opright = op0;
2979 }
2980 else
2981 {
2982 opright = op1;
2983 opleft = op0;
2984 }
2985
2986 if (GET_CODE (opleft) == ASHIFT && GET_CODE (opright) == LSHIFTRT
2987 && rtx_equal_p (XEXP (opleft, 0), XEXP (opright, 0))
2988 && CONST_INT_P (XEXP (opleft, 1))
2989 && CONST_INT_P (XEXP (opright, 1))
2990 && (INTVAL (XEXP (opleft, 1)) + INTVAL (XEXP (opright, 1))
2991 == GET_MODE_UNIT_PRECISION (mode)))
2992 return gen_rtx_ROTATE (mode, XEXP (opright, 0), XEXP (opleft, 1));
2993
2994 /* Same, but for ashift that has been "simplified" to a wider mode
2995 by simplify_shift_const. */
2996
2997 if (GET_CODE (opleft) == SUBREG
2998 && is_a <scalar_int_mode> (mode, &int_mode)
2999 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (opleft)),
3000 &inner_mode)
3001 && GET_CODE (SUBREG_REG (opleft)) == ASHIFT
3002 && GET_CODE (opright) == LSHIFTRT
3003 && GET_CODE (XEXP (opright, 0)) == SUBREG
3004 && known_eq (SUBREG_BYTE (opleft), SUBREG_BYTE (XEXP (opright, 0)))
3005 && GET_MODE_SIZE (int_mode) < GET_MODE_SIZE (inner_mode)
3006 && rtx_equal_p (XEXP (SUBREG_REG (opleft), 0),
3007 SUBREG_REG (XEXP (opright, 0)))
3008 && CONST_INT_P (XEXP (SUBREG_REG (opleft), 1))
3009 && CONST_INT_P (XEXP (opright, 1))
3010 && (INTVAL (XEXP (SUBREG_REG (opleft), 1))
3011 + INTVAL (XEXP (opright, 1))
3012 == GET_MODE_PRECISION (int_mode)))
3013 return gen_rtx_ROTATE (int_mode, XEXP (opright, 0),
3014 XEXP (SUBREG_REG (opleft), 1));
3015
3016 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
3017 a (sign_extend (plus ...)). Then check if OP1 is a CONST_INT and
3018 the PLUS does not affect any of the bits in OP1: then we can do
3019 the IOR as a PLUS and we can associate. This is valid if OP1
3020 can be safely shifted left C bits. */
3021 if (CONST_INT_P (trueop1) && GET_CODE (op0) == ASHIFTRT
3022 && GET_CODE (XEXP (op0, 0)) == PLUS
3023 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
3024 && CONST_INT_P (XEXP (op0, 1))
3025 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
3026 {
3027 int count = INTVAL (XEXP (op0, 1));
3028 HOST_WIDE_INT mask = UINTVAL (trueop1) << count;
3029
3030 if (mask >> count == INTVAL (trueop1)
3031 && trunc_int_for_mode (mask, mode) == mask
3032 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
3033 return simplify_gen_binary (ASHIFTRT, mode,
3034 plus_constant (mode, XEXP (op0, 0),
3035 mask),
3036 XEXP (op0, 1));
3037 }
3038
3039 /* The following happens with bitfield merging.
3040 (X & C) | ((X | Y) & ~C) -> X | (Y & ~C) */
3041 if (GET_CODE (op0) == AND
3042 && GET_CODE (op1) == AND
3043 && CONST_INT_P (XEXP (op0, 1))
3044 && CONST_INT_P (XEXP (op1, 1))
3045 && (INTVAL (XEXP (op0, 1))
3046 == ~INTVAL (XEXP (op1, 1))))
3047 {
3048 /* The IOR may be on both sides. */
3049 rtx top0 = NULL_RTX, top1 = NULL_RTX;
3050 if (GET_CODE (XEXP (op1, 0)) == IOR)
3051 top0 = op0, top1 = op1;
3052 else if (GET_CODE (XEXP (op0, 0)) == IOR)
3053 top0 = op1, top1 = op0;
3054 if (top0 && top1)
3055 {
3056 /* X may be on either side of the inner IOR. */
3057 rtx tem = NULL_RTX;
3058 if (rtx_equal_p (XEXP (top0, 0),
3059 XEXP (XEXP (top1, 0), 0)))
3060 tem = XEXP (XEXP (top1, 0), 1);
3061 else if (rtx_equal_p (XEXP (top0, 0),
3062 XEXP (XEXP (top1, 0), 1)))
3063 tem = XEXP (XEXP (top1, 0), 0);
3064 if (tem)
3065 return simplify_gen_binary (IOR, mode, XEXP (top0, 0),
3066 simplify_gen_binary
3067 (AND, mode, tem, XEXP (top1, 1)));
3068 }
3069 }
3070
3071 tem = simplify_byte_swapping_operation (code, mode, op0, op1);
3072 if (tem)
3073 return tem;
3074
3075 tem = simplify_associative_operation (code, mode, op0, op1);
3076 if (tem)
3077 return tem;
3078
3079 tem = simplify_logical_relational_operation (code, mode, op0, op1);
3080 if (tem)
3081 return tem;
3082 break;
3083
3084 case XOR:
3085 if (trueop1 == CONST0_RTX (mode))
3086 return op0;
3087 if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
3088 return simplify_gen_unary (NOT, mode, op0, mode);
3089 if (rtx_equal_p (trueop0, trueop1)
3090 && ! side_effects_p (op0)
3091 && GET_MODE_CLASS (mode) != MODE_CC)
3092 return CONST0_RTX (mode);
3093
3094 /* Canonicalize XOR of the most significant bit to PLUS. */
3095 if (CONST_SCALAR_INT_P (op1)
3096 && mode_signbit_p (mode, op1))
3097 return simplify_gen_binary (PLUS, mode, op0, op1);
3098 /* (xor (plus X C1) C2) is (xor X (C1^C2)) if C1 is signbit. */
3099 if (CONST_SCALAR_INT_P (op1)
3100 && GET_CODE (op0) == PLUS
3101 && CONST_SCALAR_INT_P (XEXP (op0, 1))
3102 && mode_signbit_p (mode, XEXP (op0, 1)))
3103 return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3104 simplify_gen_binary (XOR, mode, op1,
3105 XEXP (op0, 1)));
3106
3107 /* If we are XORing two things that have no bits in common,
3108 convert them into an IOR. This helps to detect rotation encoded
3109 using those methods and possibly other simplifications. */
3110
3111 if (HWI_COMPUTABLE_MODE_P (mode)
3112 && (nonzero_bits (op0, mode)
3113 & nonzero_bits (op1, mode)) == 0)
3114 return (simplify_gen_binary (IOR, mode, op0, op1));
3115
3116 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
3117 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
3118 (NOT y). */
3119 {
3120 int num_negated = 0;
3121
3122 if (GET_CODE (op0) == NOT)
3123 num_negated++, op0 = XEXP (op0, 0);
3124 if (GET_CODE (op1) == NOT)
3125 num_negated++, op1 = XEXP (op1, 0);
3126
3127 if (num_negated == 2)
3128 return simplify_gen_binary (XOR, mode, op0, op1);
3129 else if (num_negated == 1)
3130 return simplify_gen_unary (NOT, mode,
3131 simplify_gen_binary (XOR, mode, op0, op1),
3132 mode);
3133 }
3134
3135 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
3136 correspond to a machine insn or result in further simplifications
3137 if B is a constant. */
3138
3139 if (GET_CODE (op0) == AND
3140 && rtx_equal_p (XEXP (op0, 1), op1)
3141 && ! side_effects_p (op1))
3142 return simplify_gen_binary (AND, mode,
3143 simplify_gen_unary (NOT, mode,
3144 XEXP (op0, 0), mode),
3145 op1);
3146
3147 else if (GET_CODE (op0) == AND
3148 && rtx_equal_p (XEXP (op0, 0), op1)
3149 && ! side_effects_p (op1))
3150 return simplify_gen_binary (AND, mode,
3151 simplify_gen_unary (NOT, mode,
3152 XEXP (op0, 1), mode),
3153 op1);
3154
3155 /* Given (xor (ior (xor A B) C) D), where B, C and D are
3156 constants, simplify to (xor (ior A C) (B&~C)^D), canceling
3157 out bits inverted twice and not set by C. Similarly, given
3158 (xor (and (xor A B) C) D), simplify without inverting C in
3159 the xor operand: (xor (and A C) (B&C)^D).
3160 */
3161 else if ((GET_CODE (op0) == IOR || GET_CODE (op0) == AND)
3162 && GET_CODE (XEXP (op0, 0)) == XOR
3163 && CONST_INT_P (op1)
3164 && CONST_INT_P (XEXP (op0, 1))
3165 && CONST_INT_P (XEXP (XEXP (op0, 0), 1)))
3166 {
3167 enum rtx_code op = GET_CODE (op0);
3168 rtx a = XEXP (XEXP (op0, 0), 0);
3169 rtx b = XEXP (XEXP (op0, 0), 1);
3170 rtx c = XEXP (op0, 1);
3171 rtx d = op1;
3172 HOST_WIDE_INT bval = INTVAL (b);
3173 HOST_WIDE_INT cval = INTVAL (c);
3174 HOST_WIDE_INT dval = INTVAL (d);
3175 HOST_WIDE_INT xcval;
3176
3177 if (op == IOR)
3178 xcval = ~cval;
3179 else
3180 xcval = cval;
3181
3182 return simplify_gen_binary (XOR, mode,
3183 simplify_gen_binary (op, mode, a, c),
3184 gen_int_mode ((bval & xcval) ^ dval,
3185 mode));
3186 }
3187
3188 /* Given (xor (and A B) C), using P^Q == (~P&Q) | (~Q&P),
3189 we can transform like this:
3190 (A&B)^C == ~(A&B)&C | ~C&(A&B)
3191 == (~A|~B)&C | ~C&(A&B) * DeMorgan's Law
3192 == ~A&C | ~B&C | A&(~C&B) * Distribute and re-order
3193 Attempt a few simplifications when B and C are both constants. */
3194 if (GET_CODE (op0) == AND
3195 && CONST_INT_P (op1)
3196 && CONST_INT_P (XEXP (op0, 1)))
3197 {
3198 rtx a = XEXP (op0, 0);
3199 rtx b = XEXP (op0, 1);
3200 rtx c = op1;
3201 HOST_WIDE_INT bval = INTVAL (b);
3202 HOST_WIDE_INT cval = INTVAL (c);
3203
3204 /* Instead of computing ~A&C, we compute its negated value,
3205 ~(A|~C). If it yields -1, ~A&C is zero, so we can
3206 optimize for sure. If it does not simplify, we still try
3207 to compute ~A&C below, but since that always allocates
3208 RTL, we don't try that before committing to returning a
3209 simplified expression. */
3210 rtx n_na_c = simplify_binary_operation (IOR, mode, a,
3211 GEN_INT (~cval));
3212
3213 if ((~cval & bval) == 0)
3214 {
3215 rtx na_c = NULL_RTX;
3216 if (n_na_c)
3217 na_c = simplify_gen_unary (NOT, mode, n_na_c, mode);
3218 else
3219 {
3220 /* If ~A does not simplify, don't bother: we don't
3221 want to simplify 2 operations into 3, and if na_c
3222 were to simplify with na, n_na_c would have
3223 simplified as well. */
3224 rtx na = simplify_unary_operation (NOT, mode, a, mode);
3225 if (na)
3226 na_c = simplify_gen_binary (AND, mode, na, c);
3227 }
3228
3229 /* Try to simplify ~A&C | ~B&C. */
3230 if (na_c != NULL_RTX)
3231 return simplify_gen_binary (IOR, mode, na_c,
3232 gen_int_mode (~bval & cval, mode));
3233 }
3234 else
3235 {
3236 /* If ~A&C is zero, simplify A&(~C&B) | ~B&C. */
3237 if (n_na_c == CONSTM1_RTX (mode))
3238 {
3239 rtx a_nc_b = simplify_gen_binary (AND, mode, a,
3240 gen_int_mode (~cval & bval,
3241 mode));
3242 return simplify_gen_binary (IOR, mode, a_nc_b,
3243 gen_int_mode (~bval & cval,
3244 mode));
3245 }
3246 }
3247 }
3248
3249 /* If we have (xor (and (xor A B) C) A) with C a constant we can instead
3250 do (ior (and A ~C) (and B C)) which is a machine instruction on some
3251 machines, and also has shorter instruction path length. */
3252 if (GET_CODE (op0) == AND
3253 && GET_CODE (XEXP (op0, 0)) == XOR
3254 && CONST_INT_P (XEXP (op0, 1))
3255 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), trueop1))
3256 {
3257 rtx a = trueop1;
3258 rtx b = XEXP (XEXP (op0, 0), 1);
3259 rtx c = XEXP (op0, 1);
3260 rtx nc = simplify_gen_unary (NOT, mode, c, mode);
3261 rtx a_nc = simplify_gen_binary (AND, mode, a, nc);
3262 rtx bc = simplify_gen_binary (AND, mode, b, c);
3263 return simplify_gen_binary (IOR, mode, a_nc, bc);
3264 }
3265 /* Similarly, (xor (and (xor A B) C) B) as (ior (and A C) (and B ~C)) */
3266 else if (GET_CODE (op0) == AND
3267 && GET_CODE (XEXP (op0, 0)) == XOR
3268 && CONST_INT_P (XEXP (op0, 1))
3269 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), trueop1))
3270 {
3271 rtx a = XEXP (XEXP (op0, 0), 0);
3272 rtx b = trueop1;
3273 rtx c = XEXP (op0, 1);
3274 rtx nc = simplify_gen_unary (NOT, mode, c, mode);
3275 rtx b_nc = simplify_gen_binary (AND, mode, b, nc);
3276 rtx ac = simplify_gen_binary (AND, mode, a, c);
3277 return simplify_gen_binary (IOR, mode, ac, b_nc);
3278 }
3279
3280 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
3281 comparison if STORE_FLAG_VALUE is 1. */
3282 if (STORE_FLAG_VALUE == 1
3283 && trueop1 == const1_rtx
3284 && COMPARISON_P (op0)
3285 && (reversed = reversed_comparison (op0, mode)))
3286 return reversed;
3287
3288 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
3289 is (lt foo (const_int 0)), so we can perform the above
3290 simplification if STORE_FLAG_VALUE is 1. */
3291
3292 if (is_a <scalar_int_mode> (mode, &int_mode)
3293 && STORE_FLAG_VALUE == 1
3294 && trueop1 == const1_rtx
3295 && GET_CODE (op0) == LSHIFTRT
3296 && CONST_INT_P (XEXP (op0, 1))
3297 && INTVAL (XEXP (op0, 1)) == GET_MODE_PRECISION (int_mode) - 1)
3298 return gen_rtx_GE (int_mode, XEXP (op0, 0), const0_rtx);
3299
3300 /* (xor (comparison foo bar) (const_int sign-bit))
3301 when STORE_FLAG_VALUE is the sign bit. */
3302 if (is_a <scalar_int_mode> (mode, &int_mode)
3303 && val_signbit_p (int_mode, STORE_FLAG_VALUE)
3304 && trueop1 == const_true_rtx
3305 && COMPARISON_P (op0)
3306 && (reversed = reversed_comparison (op0, int_mode)))
3307 return reversed;
3308
3309 tem = simplify_byte_swapping_operation (code, mode, op0, op1);
3310 if (tem)
3311 return tem;
3312
3313 tem = simplify_associative_operation (code, mode, op0, op1);
3314 if (tem)
3315 return tem;
3316 break;
3317
3318 case AND:
3319 if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
3320 return trueop1;
3321 if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
3322 return op0;
3323 if (HWI_COMPUTABLE_MODE_P (mode))
3324 {
3325 HOST_WIDE_INT nzop0 = nonzero_bits (trueop0, mode);
3326 HOST_WIDE_INT nzop1;
3327 if (CONST_INT_P (trueop1))
3328 {
3329 HOST_WIDE_INT val1 = INTVAL (trueop1);
3330 /* If we are turning off bits already known off in OP0, we need
3331 not do an AND. */
3332 if ((nzop0 & ~val1) == 0)
3333 return op0;
3334 }
3335 nzop1 = nonzero_bits (trueop1, mode);
3336 /* If we are clearing all the nonzero bits, the result is zero. */
3337 if ((nzop1 & nzop0) == 0
3338 && !side_effects_p (op0) && !side_effects_p (op1))
3339 return CONST0_RTX (mode);
3340 }
3341 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0)
3342 && GET_MODE_CLASS (mode) != MODE_CC)
3343 return op0;
3344 /* A & (~A) -> 0 */
3345 if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
3346 || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
3347 && ! side_effects_p (op0)
3348 && GET_MODE_CLASS (mode) != MODE_CC)
3349 return CONST0_RTX (mode);
3350
3351 /* Transform (and (extend X) C) into (zero_extend (and X C)) if
3352 there are no nonzero bits of C outside of X's mode. */
3353 if ((GET_CODE (op0) == SIGN_EXTEND
3354 || GET_CODE (op0) == ZERO_EXTEND)
3355 && CONST_INT_P (trueop1)
3356 && HWI_COMPUTABLE_MODE_P (mode)
3357 && (~GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))
3358 & UINTVAL (trueop1)) == 0)
3359 {
3360 machine_mode imode = GET_MODE (XEXP (op0, 0));
3361 tem = simplify_gen_binary (AND, imode, XEXP (op0, 0),
3362 gen_int_mode (INTVAL (trueop1),
3363 imode));
3364 return simplify_gen_unary (ZERO_EXTEND, mode, tem, imode);
3365 }
3366
3367 /* Transform (and (truncate X) C) into (truncate (and X C)). This way
3368 we might be able to further simplify the AND with X and potentially
3369 remove the truncation altogether. */
3370 if (GET_CODE (op0) == TRUNCATE && CONST_INT_P (trueop1))
3371 {
3372 rtx x = XEXP (op0, 0);
3373 machine_mode xmode = GET_MODE (x);
3374 tem = simplify_gen_binary (AND, xmode, x,
3375 gen_int_mode (INTVAL (trueop1), xmode));
3376 return simplify_gen_unary (TRUNCATE, mode, tem, xmode);
3377 }
3378
3379 /* Canonicalize (A | C1) & C2 as (A & C2) | (C1 & C2). */
3380 if (GET_CODE (op0) == IOR
3381 && CONST_INT_P (trueop1)
3382 && CONST_INT_P (XEXP (op0, 1)))
3383 {
3384 HOST_WIDE_INT tmp = INTVAL (trueop1) & INTVAL (XEXP (op0, 1));
3385 return simplify_gen_binary (IOR, mode,
3386 simplify_gen_binary (AND, mode,
3387 XEXP (op0, 0), op1),
3388 gen_int_mode (tmp, mode));
3389 }
3390
3391 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
3392 insn (and may simplify more). */
3393 if (GET_CODE (op0) == XOR
3394 && rtx_equal_p (XEXP (op0, 0), op1)
3395 && ! side_effects_p (op1))
3396 return simplify_gen_binary (AND, mode,
3397 simplify_gen_unary (NOT, mode,
3398 XEXP (op0, 1), mode),
3399 op1);
3400
3401 if (GET_CODE (op0) == XOR
3402 && rtx_equal_p (XEXP (op0, 1), op1)
3403 && ! side_effects_p (op1))
3404 return simplify_gen_binary (AND, mode,
3405 simplify_gen_unary (NOT, mode,
3406 XEXP (op0, 0), mode),
3407 op1);
3408
3409 /* Similarly for (~(A ^ B)) & A. */
3410 if (GET_CODE (op0) == NOT
3411 && GET_CODE (XEXP (op0, 0)) == XOR
3412 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
3413 && ! side_effects_p (op1))
3414 return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
3415
3416 if (GET_CODE (op0) == NOT
3417 && GET_CODE (XEXP (op0, 0)) == XOR
3418 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
3419 && ! side_effects_p (op1))
3420 return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
3421
3422 /* Convert (A | B) & A to A. */
3423 if (GET_CODE (op0) == IOR
3424 && (rtx_equal_p (XEXP (op0, 0), op1)
3425 || rtx_equal_p (XEXP (op0, 1), op1))
3426 && ! side_effects_p (XEXP (op0, 0))
3427 && ! side_effects_p (XEXP (op0, 1)))
3428 return op1;
3429
3430 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
3431 ((A & N) + B) & M -> (A + B) & M
3432 Similarly if (N & M) == 0,
3433 ((A | N) + B) & M -> (A + B) & M
3434 and for - instead of + and/or ^ instead of |.
3435 Also, if (N & M) == 0, then
3436 (A +- N) & M -> A & M. */
3437 if (CONST_INT_P (trueop1)
3438 && HWI_COMPUTABLE_MODE_P (mode)
3439 && ~UINTVAL (trueop1)
3440 && (UINTVAL (trueop1) & (UINTVAL (trueop1) + 1)) == 0
3441 && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS))
3442 {
3443 rtx pmop[2];
3444 int which;
3445
3446 pmop[0] = XEXP (op0, 0);
3447 pmop[1] = XEXP (op0, 1);
3448
3449 if (CONST_INT_P (pmop[1])
3450 && (UINTVAL (pmop[1]) & UINTVAL (trueop1)) == 0)
3451 return simplify_gen_binary (AND, mode, pmop[0], op1);
3452
3453 for (which = 0; which < 2; which++)
3454 {
3455 tem = pmop[which];
3456 switch (GET_CODE (tem))
3457 {
3458 case AND:
3459 if (CONST_INT_P (XEXP (tem, 1))
3460 && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1))
3461 == UINTVAL (trueop1))
3462 pmop[which] = XEXP (tem, 0);
3463 break;
3464 case IOR:
3465 case XOR:
3466 if (CONST_INT_P (XEXP (tem, 1))
3467 && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1)) == 0)
3468 pmop[which] = XEXP (tem, 0);
3469 break;
3470 default:
3471 break;
3472 }
3473 }
3474
3475 if (pmop[0] != XEXP (op0, 0) || pmop[1] != XEXP (op0, 1))
3476 {
3477 tem = simplify_gen_binary (GET_CODE (op0), mode,
3478 pmop[0], pmop[1]);
3479 return simplify_gen_binary (code, mode, tem, op1);
3480 }
3481 }
3482
3483 /* (and X (ior (not X) Y) -> (and X Y) */
3484 if (GET_CODE (op1) == IOR
3485 && GET_CODE (XEXP (op1, 0)) == NOT
3486 && rtx_equal_p (op0, XEXP (XEXP (op1, 0), 0)))
3487 return simplify_gen_binary (AND, mode, op0, XEXP (op1, 1));
3488
3489 /* (and (ior (not X) Y) X) -> (and X Y) */
3490 if (GET_CODE (op0) == IOR
3491 && GET_CODE (XEXP (op0, 0)) == NOT
3492 && rtx_equal_p (op1, XEXP (XEXP (op0, 0), 0)))
3493 return simplify_gen_binary (AND, mode, op1, XEXP (op0, 1));
3494
3495 /* (and X (ior Y (not X)) -> (and X Y) */
3496 if (GET_CODE (op1) == IOR
3497 && GET_CODE (XEXP (op1, 1)) == NOT
3498 && rtx_equal_p (op0, XEXP (XEXP (op1, 1), 0)))
3499 return simplify_gen_binary (AND, mode, op0, XEXP (op1, 0));
3500
3501 /* (and (ior Y (not X)) X) -> (and X Y) */
3502 if (GET_CODE (op0) == IOR
3503 && GET_CODE (XEXP (op0, 1)) == NOT
3504 && rtx_equal_p (op1, XEXP (XEXP (op0, 1), 0)))
3505 return simplify_gen_binary (AND, mode, op1, XEXP (op0, 0));
3506
3507 tem = simplify_byte_swapping_operation (code, mode, op0, op1);
3508 if (tem)
3509 return tem;
3510
3511 tem = simplify_associative_operation (code, mode, op0, op1);
3512 if (tem)
3513 return tem;
3514 break;
3515
3516 case UDIV:
3517 /* 0/x is 0 (or x&0 if x has side-effects). */
3518 if (trueop0 == CONST0_RTX (mode)
3519 && !cfun->can_throw_non_call_exceptions)
3520 {
3521 if (side_effects_p (op1))
3522 return simplify_gen_binary (AND, mode, op1, trueop0);
3523 return trueop0;
3524 }
3525 /* x/1 is x. */
3526 if (trueop1 == CONST1_RTX (mode))
3527 {
3528 tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
3529 if (tem)
3530 return tem;
3531 }
3532 /* Convert divide by power of two into shift. */
3533 if (CONST_INT_P (trueop1)
3534 && (val = exact_log2 (UINTVAL (trueop1))) > 0)
3535 return simplify_gen_binary (LSHIFTRT, mode, op0,
3536 gen_int_shift_amount (mode, val));
3537 break;
3538
3539 case DIV:
3540 /* Handle floating point and integers separately. */
3541 if (SCALAR_FLOAT_MODE_P (mode))
3542 {
3543 /* Maybe change 0.0 / x to 0.0. This transformation isn't
3544 safe for modes with NaNs, since 0.0 / 0.0 will then be
3545 NaN rather than 0.0. Nor is it safe for modes with signed
3546 zeros, since dividing 0 by a negative number gives -0.0 */
3547 if (trueop0 == CONST0_RTX (mode)
3548 && !HONOR_NANS (mode)
3549 && !HONOR_SIGNED_ZEROS (mode)
3550 && ! side_effects_p (op1))
3551 return op0;
3552 /* x/1.0 is x. */
3553 if (trueop1 == CONST1_RTX (mode)
3554 && !HONOR_SNANS (mode))
3555 return op0;
3556
3557 if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
3558 && trueop1 != CONST0_RTX (mode))
3559 {
3560 const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
3561
3562 /* x/-1.0 is -x. */
3563 if (real_equal (d1, &dconstm1)
3564 && !HONOR_SNANS (mode))
3565 return simplify_gen_unary (NEG, mode, op0, mode);
3566
3567 /* Change FP division by a constant into multiplication.
3568 Only do this with -freciprocal-math. */
3569 if (flag_reciprocal_math
3570 && !real_equal (d1, &dconst0))
3571 {
3572 REAL_VALUE_TYPE d;
3573 real_arithmetic (&d, RDIV_EXPR, &dconst1, d1);
3574 tem = const_double_from_real_value (d, mode);
3575 return simplify_gen_binary (MULT, mode, op0, tem);
3576 }
3577 }
3578 }
3579 else if (SCALAR_INT_MODE_P (mode))
3580 {
3581 /* 0/x is 0 (or x&0 if x has side-effects). */
3582 if (trueop0 == CONST0_RTX (mode)
3583 && !cfun->can_throw_non_call_exceptions)
3584 {
3585 if (side_effects_p (op1))
3586 return simplify_gen_binary (AND, mode, op1, trueop0);
3587 return trueop0;
3588 }
3589 /* x/1 is x. */
3590 if (trueop1 == CONST1_RTX (mode))
3591 {
3592 tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
3593 if (tem)
3594 return tem;
3595 }
3596 /* x/-1 is -x. */
3597 if (trueop1 == constm1_rtx)
3598 {
3599 rtx x = rtl_hooks.gen_lowpart_no_emit (mode, op0);
3600 if (x)
3601 return simplify_gen_unary (NEG, mode, x, mode);
3602 }
3603 }
3604 break;
3605
3606 case UMOD:
3607 /* 0%x is 0 (or x&0 if x has side-effects). */
3608 if (trueop0 == CONST0_RTX (mode))
3609 {
3610 if (side_effects_p (op1))
3611 return simplify_gen_binary (AND, mode, op1, trueop0);
3612 return trueop0;
3613 }
3614 /* x%1 is 0 (of x&0 if x has side-effects). */
3615 if (trueop1 == CONST1_RTX (mode))
3616 {
3617 if (side_effects_p (op0))
3618 return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
3619 return CONST0_RTX (mode);
3620 }
3621 /* Implement modulus by power of two as AND. */
3622 if (CONST_INT_P (trueop1)
3623 && exact_log2 (UINTVAL (trueop1)) > 0)
3624 return simplify_gen_binary (AND, mode, op0,
3625 gen_int_mode (UINTVAL (trueop1) - 1,
3626 mode));
3627 break;
3628
3629 case MOD:
3630 /* 0%x is 0 (or x&0 if x has side-effects). */
3631 if (trueop0 == CONST0_RTX (mode))
3632 {
3633 if (side_effects_p (op1))
3634 return simplify_gen_binary (AND, mode, op1, trueop0);
3635 return trueop0;
3636 }
3637 /* x%1 and x%-1 is 0 (or x&0 if x has side-effects). */
3638 if (trueop1 == CONST1_RTX (mode) || trueop1 == constm1_rtx)
3639 {
3640 if (side_effects_p (op0))
3641 return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
3642 return CONST0_RTX (mode);
3643 }
3644 break;
3645
3646 case ROTATERT:
3647 case ROTATE:
3648 /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
3649 prefer left rotation, if op1 is from bitsize / 2 + 1 to
3650 bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
3651 amount instead. */
3652 #if defined(HAVE_rotate) && defined(HAVE_rotatert)
3653 if (CONST_INT_P (trueop1)
3654 && IN_RANGE (INTVAL (trueop1),
3655 GET_MODE_UNIT_PRECISION (mode) / 2 + (code == ROTATE),
3656 GET_MODE_UNIT_PRECISION (mode) - 1))
3657 {
3658 int new_amount = GET_MODE_UNIT_PRECISION (mode) - INTVAL (trueop1);
3659 rtx new_amount_rtx = gen_int_shift_amount (mode, new_amount);
3660 return simplify_gen_binary (code == ROTATE ? ROTATERT : ROTATE,
3661 mode, op0, new_amount_rtx);
3662 }
3663 #endif
3664 /* FALLTHRU */
3665 case ASHIFTRT:
3666 if (trueop1 == CONST0_RTX (mode))
3667 return op0;
3668 if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
3669 return op0;
3670 /* Rotating ~0 always results in ~0. */
3671 if (CONST_INT_P (trueop0)
3672 && HWI_COMPUTABLE_MODE_P (mode)
3673 && UINTVAL (trueop0) == GET_MODE_MASK (mode)
3674 && ! side_effects_p (op1))
3675 return op0;
3676
3677 canonicalize_shift:
3678 /* Given:
3679 scalar modes M1, M2
3680 scalar constants c1, c2
3681 size (M2) > size (M1)
3682 c1 == size (M2) - size (M1)
3683 optimize:
3684 ([a|l]shiftrt:M1 (subreg:M1 (lshiftrt:M2 (reg:M2) (const_int <c1>))
3685 <low_part>)
3686 (const_int <c2>))
3687 to:
3688 (subreg:M1 ([a|l]shiftrt:M2 (reg:M2) (const_int <c1 + c2>))
3689 <low_part>). */
3690 if ((code == ASHIFTRT || code == LSHIFTRT)
3691 && is_a <scalar_int_mode> (mode, &int_mode)
3692 && SUBREG_P (op0)
3693 && CONST_INT_P (op1)
3694 && GET_CODE (SUBREG_REG (op0)) == LSHIFTRT
3695 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
3696 &inner_mode)
3697 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1))
3698 && GET_MODE_BITSIZE (inner_mode) > GET_MODE_BITSIZE (int_mode)
3699 && (INTVAL (XEXP (SUBREG_REG (op0), 1))
3700 == GET_MODE_BITSIZE (inner_mode) - GET_MODE_BITSIZE (int_mode))
3701 && subreg_lowpart_p (op0))
3702 {
3703 rtx tmp = gen_int_shift_amount
3704 (inner_mode, INTVAL (XEXP (SUBREG_REG (op0), 1)) + INTVAL (op1));
3705
3706 /* Combine would usually zero out the value when combining two
3707 local shifts and the range becomes larger or equal to the mode.
3708 However since we fold away one of the shifts here combine won't
3709 see it so we should immediately zero the result if it's out of
3710 range. */
3711 if (code == LSHIFTRT
3712 && INTVAL (tmp) >= GET_MODE_BITSIZE (inner_mode))
3713 tmp = const0_rtx;
3714 else
3715 tmp = simplify_gen_binary (code,
3716 inner_mode,
3717 XEXP (SUBREG_REG (op0), 0),
3718 tmp);
3719
3720 return lowpart_subreg (int_mode, tmp, inner_mode);
3721 }
3722
3723 if (SHIFT_COUNT_TRUNCATED && CONST_INT_P (op1))
3724 {
3725 val = INTVAL (op1) & (GET_MODE_UNIT_PRECISION (mode) - 1);
3726 if (val != INTVAL (op1))
3727 return simplify_gen_binary (code, mode, op0,
3728 gen_int_shift_amount (mode, val));
3729 }
3730 break;
3731
3732 case ASHIFT:
3733 case SS_ASHIFT:
3734 case US_ASHIFT:
3735 if (trueop1 == CONST0_RTX (mode))
3736 return op0;
3737 if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
3738 return op0;
3739 goto canonicalize_shift;
3740
3741 case LSHIFTRT:
3742 if (trueop1 == CONST0_RTX (mode))
3743 return op0;
3744 if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
3745 return op0;
3746 /* Optimize (lshiftrt (clz X) C) as (eq X 0). */
3747 if (GET_CODE (op0) == CLZ
3748 && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
3749 && CONST_INT_P (trueop1)
3750 && STORE_FLAG_VALUE == 1
3751 && INTVAL (trueop1) < GET_MODE_UNIT_PRECISION (mode))
3752 {
3753 unsigned HOST_WIDE_INT zero_val = 0;
3754
3755 if (CLZ_DEFINED_VALUE_AT_ZERO (inner_mode, zero_val)
3756 && zero_val == GET_MODE_PRECISION (inner_mode)
3757 && INTVAL (trueop1) == exact_log2 (zero_val))
3758 return simplify_gen_relational (EQ, mode, inner_mode,
3759 XEXP (op0, 0), const0_rtx);
3760 }
3761 goto canonicalize_shift;
3762
3763 case SMIN:
3764 if (HWI_COMPUTABLE_MODE_P (mode)
3765 && mode_signbit_p (mode, trueop1)
3766 && ! side_effects_p (op0))
3767 return op1;
3768 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3769 return op0;
3770 tem = simplify_associative_operation (code, mode, op0, op1);
3771 if (tem)
3772 return tem;
3773 break;
3774
3775 case SMAX:
3776 if (HWI_COMPUTABLE_MODE_P (mode)
3777 && CONST_INT_P (trueop1)
3778 && (UINTVAL (trueop1) == GET_MODE_MASK (mode) >> 1)
3779 && ! side_effects_p (op0))
3780 return op1;
3781 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3782 return op0;
3783 tem = simplify_associative_operation (code, mode, op0, op1);
3784 if (tem)
3785 return tem;
3786 break;
3787
3788 case UMIN:
3789 if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
3790 return op1;
3791 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3792 return op0;
3793 tem = simplify_associative_operation (code, mode, op0, op1);
3794 if (tem)
3795 return tem;
3796 break;
3797
3798 case UMAX:
3799 if (trueop1 == constm1_rtx && ! side_effects_p (op0))
3800 return op1;
3801 if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3802 return op0;
3803 tem = simplify_associative_operation (code, mode, op0, op1);
3804 if (tem)
3805 return tem;
3806 break;
3807
3808 case SS_PLUS:
3809 case US_PLUS:
3810 case SS_MINUS:
3811 case US_MINUS:
3812 case SS_MULT:
3813 case US_MULT:
3814 case SS_DIV:
3815 case US_DIV:
3816 /* ??? There are simplifications that can be done. */
3817 return 0;
3818
3819 case VEC_SERIES:
3820 if (op1 == CONST0_RTX (GET_MODE_INNER (mode)))
3821 return gen_vec_duplicate (mode, op0);
3822 if (valid_for_const_vector_p (mode, op0)
3823 && valid_for_const_vector_p (mode, op1))
3824 return gen_const_vec_series (mode, op0, op1);
3825 return 0;
3826
3827 case VEC_SELECT:
3828 if (!VECTOR_MODE_P (mode))
3829 {
3830 gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
3831 gcc_assert (mode == GET_MODE_INNER (GET_MODE (trueop0)));
3832 gcc_assert (GET_CODE (trueop1) == PARALLEL);
3833 gcc_assert (XVECLEN (trueop1, 0) == 1);
3834
3835 /* We can't reason about selections made at runtime. */
3836 if (!CONST_INT_P (XVECEXP (trueop1, 0, 0)))
3837 return 0;
3838
3839 if (vec_duplicate_p (trueop0, &elt0))
3840 return elt0;
3841
3842 if (GET_CODE (trueop0) == CONST_VECTOR)
3843 return CONST_VECTOR_ELT (trueop0, INTVAL (XVECEXP
3844 (trueop1, 0, 0)));
3845
3846 /* Extract a scalar element from a nested VEC_SELECT expression
3847 (with optional nested VEC_CONCAT expression). Some targets
3848 (i386) extract scalar element from a vector using chain of
3849 nested VEC_SELECT expressions. When input operand is a memory
3850 operand, this operation can be simplified to a simple scalar
3851 load from an offseted memory address. */
3852 int n_elts;
3853 if (GET_CODE (trueop0) == VEC_SELECT
3854 && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
3855 .is_constant (&n_elts)))
3856 {
3857 rtx op0 = XEXP (trueop0, 0);
3858 rtx op1 = XEXP (trueop0, 1);
3859
3860 int i = INTVAL (XVECEXP (trueop1, 0, 0));
3861 int elem;
3862
3863 rtvec vec;
3864 rtx tmp_op, tmp;
3865
3866 gcc_assert (GET_CODE (op1) == PARALLEL);
3867 gcc_assert (i < n_elts);
3868
3869 /* Select element, pointed by nested selector. */
3870 elem = INTVAL (XVECEXP (op1, 0, i));
3871
3872 /* Handle the case when nested VEC_SELECT wraps VEC_CONCAT. */
3873 if (GET_CODE (op0) == VEC_CONCAT)
3874 {
3875 rtx op00 = XEXP (op0, 0);
3876 rtx op01 = XEXP (op0, 1);
3877
3878 machine_mode mode00, mode01;
3879 int n_elts00, n_elts01;
3880
3881 mode00 = GET_MODE (op00);
3882 mode01 = GET_MODE (op01);
3883
3884 /* Find out the number of elements of each operand.
3885 Since the concatenated result has a constant number
3886 of elements, the operands must too. */
3887 n_elts00 = GET_MODE_NUNITS (mode00).to_constant ();
3888 n_elts01 = GET_MODE_NUNITS (mode01).to_constant ();
3889
3890 gcc_assert (n_elts == n_elts00 + n_elts01);
3891
3892 /* Select correct operand of VEC_CONCAT
3893 and adjust selector. */
3894 if (elem < n_elts01)
3895 tmp_op = op00;
3896 else
3897 {
3898 tmp_op = op01;
3899 elem -= n_elts00;
3900 }
3901 }
3902 else
3903 tmp_op = op0;
3904
3905 vec = rtvec_alloc (1);
3906 RTVEC_ELT (vec, 0) = GEN_INT (elem);
3907
3908 tmp = gen_rtx_fmt_ee (code, mode,
3909 tmp_op, gen_rtx_PARALLEL (VOIDmode, vec));
3910 return tmp;
3911 }
3912 }
3913 else
3914 {
3915 gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
3916 gcc_assert (GET_MODE_INNER (mode)
3917 == GET_MODE_INNER (GET_MODE (trueop0)));
3918 gcc_assert (GET_CODE (trueop1) == PARALLEL);
3919
3920 if (vec_duplicate_p (trueop0, &elt0))
3921 /* It doesn't matter which elements are selected by trueop1,
3922 because they are all the same. */
3923 return gen_vec_duplicate (mode, elt0);
3924
3925 if (GET_CODE (trueop0) == CONST_VECTOR)
3926 {
3927 unsigned n_elts = XVECLEN (trueop1, 0);
3928 rtvec v = rtvec_alloc (n_elts);
3929 unsigned int i;
3930
3931 gcc_assert (known_eq (n_elts, GET_MODE_NUNITS (mode)));
3932 for (i = 0; i < n_elts; i++)
3933 {
3934 rtx x = XVECEXP (trueop1, 0, i);
3935
3936 if (!CONST_INT_P (x))
3937 return 0;
3938
3939 RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0,
3940 INTVAL (x));
3941 }
3942
3943 return gen_rtx_CONST_VECTOR (mode, v);
3944 }
3945
3946 /* Recognize the identity. */
3947 if (GET_MODE (trueop0) == mode)
3948 {
3949 bool maybe_ident = true;
3950 for (int i = 0; i < XVECLEN (trueop1, 0); i++)
3951 {
3952 rtx j = XVECEXP (trueop1, 0, i);
3953 if (!CONST_INT_P (j) || INTVAL (j) != i)
3954 {
3955 maybe_ident = false;
3956 break;
3957 }
3958 }
3959 if (maybe_ident)
3960 return trueop0;
3961 }
3962
3963 /* If we build {a,b} then permute it, build the result directly. */
3964 if (XVECLEN (trueop1, 0) == 2
3965 && CONST_INT_P (XVECEXP (trueop1, 0, 0))
3966 && CONST_INT_P (XVECEXP (trueop1, 0, 1))
3967 && GET_CODE (trueop0) == VEC_CONCAT
3968 && GET_CODE (XEXP (trueop0, 0)) == VEC_CONCAT
3969 && GET_MODE (XEXP (trueop0, 0)) == mode
3970 && GET_CODE (XEXP (trueop0, 1)) == VEC_CONCAT
3971 && GET_MODE (XEXP (trueop0, 1)) == mode)
3972 {
3973 unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
3974 unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
3975 rtx subop0, subop1;
3976
3977 gcc_assert (i0 < 4 && i1 < 4);
3978 subop0 = XEXP (XEXP (trueop0, i0 / 2), i0 % 2);
3979 subop1 = XEXP (XEXP (trueop0, i1 / 2), i1 % 2);
3980
3981 return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
3982 }
3983
3984 if (XVECLEN (trueop1, 0) == 2
3985 && CONST_INT_P (XVECEXP (trueop1, 0, 0))
3986 && CONST_INT_P (XVECEXP (trueop1, 0, 1))
3987 && GET_CODE (trueop0) == VEC_CONCAT
3988 && GET_MODE (trueop0) == mode)
3989 {
3990 unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
3991 unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
3992 rtx subop0, subop1;
3993
3994 gcc_assert (i0 < 2 && i1 < 2);
3995 subop0 = XEXP (trueop0, i0);
3996 subop1 = XEXP (trueop0, i1);
3997
3998 return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
3999 }
4000
4001 /* If we select one half of a vec_concat, return that. */
4002 int l0, l1;
4003 if (GET_CODE (trueop0) == VEC_CONCAT
4004 && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
4005 .is_constant (&l0))
4006 && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 1)))
4007 .is_constant (&l1))
4008 && CONST_INT_P (XVECEXP (trueop1, 0, 0)))
4009 {
4010 rtx subop0 = XEXP (trueop0, 0);
4011 rtx subop1 = XEXP (trueop0, 1);
4012 machine_mode mode0 = GET_MODE (subop0);
4013 machine_mode mode1 = GET_MODE (subop1);
4014 int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
4015 if (i0 == 0 && !side_effects_p (op1) && mode == mode0)
4016 {
4017 bool success = true;
4018 for (int i = 1; i < l0; ++i)
4019 {
4020 rtx j = XVECEXP (trueop1, 0, i);
4021 if (!CONST_INT_P (j) || INTVAL (j) != i)
4022 {
4023 success = false;
4024 break;
4025 }
4026 }
4027 if (success)
4028 return subop0;
4029 }
4030 if (i0 == l0 && !side_effects_p (op0) && mode == mode1)
4031 {
4032 bool success = true;
4033 for (int i = 1; i < l1; ++i)
4034 {
4035 rtx j = XVECEXP (trueop1, 0, i);
4036 if (!CONST_INT_P (j) || INTVAL (j) != i0 + i)
4037 {
4038 success = false;
4039 break;
4040 }
4041 }
4042 if (success)
4043 return subop1;
4044 }
4045 }
4046 }
4047
4048 if (XVECLEN (trueop1, 0) == 1
4049 && CONST_INT_P (XVECEXP (trueop1, 0, 0))
4050 && GET_CODE (trueop0) == VEC_CONCAT)
4051 {
4052 rtx vec = trueop0;
4053 offset = INTVAL (XVECEXP (trueop1, 0, 0)) * GET_MODE_SIZE (mode);
4054
4055 /* Try to find the element in the VEC_CONCAT. */
4056 while (GET_MODE (vec) != mode
4057 && GET_CODE (vec) == VEC_CONCAT)
4058 {
4059 poly_int64 vec_size;
4060
4061 if (CONST_INT_P (XEXP (vec, 0)))
4062 {
4063 /* vec_concat of two const_ints doesn't make sense with
4064 respect to modes. */
4065 if (CONST_INT_P (XEXP (vec, 1)))
4066 return 0;
4067
4068 vec_size = GET_MODE_SIZE (GET_MODE (trueop0))
4069 - GET_MODE_SIZE (GET_MODE (XEXP (vec, 1)));
4070 }
4071 else
4072 vec_size = GET_MODE_SIZE (GET_MODE (XEXP (vec, 0)));
4073
4074 if (known_lt (offset, vec_size))
4075 vec = XEXP (vec, 0);
4076 else if (known_ge (offset, vec_size))
4077 {
4078 offset -= vec_size;
4079 vec = XEXP (vec, 1);
4080 }
4081 else
4082 break;
4083 vec = avoid_constant_pool_reference (vec);
4084 }
4085
4086 if (GET_MODE (vec) == mode)
4087 return vec;
4088 }
4089
4090 /* If we select elements in a vec_merge that all come from the same
4091 operand, select from that operand directly. */
4092 if (GET_CODE (op0) == VEC_MERGE)
4093 {
4094 rtx trueop02 = avoid_constant_pool_reference (XEXP (op0, 2));
4095 if (CONST_INT_P (trueop02))
4096 {
4097 unsigned HOST_WIDE_INT sel = UINTVAL (trueop02);
4098 bool all_operand0 = true;
4099 bool all_operand1 = true;
4100 for (int i = 0; i < XVECLEN (trueop1, 0); i++)
4101 {
4102 rtx j = XVECEXP (trueop1, 0, i);
4103 if (sel & (HOST_WIDE_INT_1U << UINTVAL (j)))
4104 all_operand1 = false;
4105 else
4106 all_operand0 = false;
4107 }
4108 if (all_operand0 && !side_effects_p (XEXP (op0, 1)))
4109 return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 0), op1);
4110 if (all_operand1 && !side_effects_p (XEXP (op0, 0)))
4111 return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 1), op1);
4112 }
4113 }
4114
4115 /* If we have two nested selects that are inverses of each
4116 other, replace them with the source operand. */
4117 if (GET_CODE (trueop0) == VEC_SELECT
4118 && GET_MODE (XEXP (trueop0, 0)) == mode)
4119 {
4120 rtx op0_subop1 = XEXP (trueop0, 1);
4121 gcc_assert (GET_CODE (op0_subop1) == PARALLEL);
4122 gcc_assert (known_eq (XVECLEN (trueop1, 0), GET_MODE_NUNITS (mode)));
4123
4124 /* Apply the outer ordering vector to the inner one. (The inner
4125 ordering vector is expressly permitted to be of a different
4126 length than the outer one.) If the result is { 0, 1, ..., n-1 }
4127 then the two VEC_SELECTs cancel. */
4128 for (int i = 0; i < XVECLEN (trueop1, 0); ++i)
4129 {
4130 rtx x = XVECEXP (trueop1, 0, i);
4131 if (!CONST_INT_P (x))
4132 return 0;
4133 rtx y = XVECEXP (op0_subop1, 0, INTVAL (x));
4134 if (!CONST_INT_P (y) || i != INTVAL (y))
4135 return 0;
4136 }
4137 return XEXP (trueop0, 0);
4138 }
4139
4140 return 0;
4141 case VEC_CONCAT:
4142 {
4143 machine_mode op0_mode = (GET_MODE (trueop0) != VOIDmode
4144 ? GET_MODE (trueop0)
4145 : GET_MODE_INNER (mode));
4146 machine_mode op1_mode = (GET_MODE (trueop1) != VOIDmode
4147 ? GET_MODE (trueop1)
4148 : GET_MODE_INNER (mode));
4149
4150 gcc_assert (VECTOR_MODE_P (mode));
4151 gcc_assert (known_eq (GET_MODE_SIZE (op0_mode)
4152 + GET_MODE_SIZE (op1_mode),
4153 GET_MODE_SIZE (mode)));
4154
4155 if (VECTOR_MODE_P (op0_mode))
4156 gcc_assert (GET_MODE_INNER (mode)
4157 == GET_MODE_INNER (op0_mode));
4158 else
4159 gcc_assert (GET_MODE_INNER (mode) == op0_mode);
4160
4161 if (VECTOR_MODE_P (op1_mode))
4162 gcc_assert (GET_MODE_INNER (mode)
4163 == GET_MODE_INNER (op1_mode));
4164 else
4165 gcc_assert (GET_MODE_INNER (mode) == op1_mode);
4166
4167 unsigned int n_elts, in_n_elts;
4168 if ((GET_CODE (trueop0) == CONST_VECTOR
4169 || CONST_SCALAR_INT_P (trueop0)
4170 || CONST_DOUBLE_AS_FLOAT_P (trueop0))
4171 && (GET_CODE (trueop1) == CONST_VECTOR
4172 || CONST_SCALAR_INT_P (trueop1)
4173 || CONST_DOUBLE_AS_FLOAT_P (trueop1))
4174 && GET_MODE_NUNITS (mode).is_constant (&n_elts)
4175 && GET_MODE_NUNITS (op0_mode).is_constant (&in_n_elts))
4176 {
4177 rtvec v = rtvec_alloc (n_elts);
4178 unsigned int i;
4179 for (i = 0; i < n_elts; i++)
4180 {
4181 if (i < in_n_elts)
4182 {
4183 if (!VECTOR_MODE_P (op0_mode))
4184 RTVEC_ELT (v, i) = trueop0;
4185 else
4186 RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0, i);
4187 }
4188 else
4189 {
4190 if (!VECTOR_MODE_P (op1_mode))
4191 RTVEC_ELT (v, i) = trueop1;
4192 else
4193 RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop1,
4194 i - in_n_elts);
4195 }
4196 }
4197
4198 return gen_rtx_CONST_VECTOR (mode, v);
4199 }
4200
4201 /* Try to merge two VEC_SELECTs from the same vector into a single one.
4202 Restrict the transformation to avoid generating a VEC_SELECT with a
4203 mode unrelated to its operand. */
4204 if (GET_CODE (trueop0) == VEC_SELECT
4205 && GET_CODE (trueop1) == VEC_SELECT
4206 && rtx_equal_p (XEXP (trueop0, 0), XEXP (trueop1, 0))
4207 && GET_MODE (XEXP (trueop0, 0)) == mode)
4208 {
4209 rtx par0 = XEXP (trueop0, 1);
4210 rtx par1 = XEXP (trueop1, 1);
4211 int len0 = XVECLEN (par0, 0);
4212 int len1 = XVECLEN (par1, 0);
4213 rtvec vec = rtvec_alloc (len0 + len1);
4214 for (int i = 0; i < len0; i++)
4215 RTVEC_ELT (vec, i) = XVECEXP (par0, 0, i);
4216 for (int i = 0; i < len1; i++)
4217 RTVEC_ELT (vec, len0 + i) = XVECEXP (par1, 0, i);
4218 return simplify_gen_binary (VEC_SELECT, mode, XEXP (trueop0, 0),
4219 gen_rtx_PARALLEL (VOIDmode, vec));
4220 }
4221 }
4222 return 0;
4223
4224 default:
4225 gcc_unreachable ();
4226 }
4227
4228 if (mode == GET_MODE (op0)
4229 && mode == GET_MODE (op1)
4230 && vec_duplicate_p (op0, &elt0)
4231 && vec_duplicate_p (op1, &elt1))
4232 {
4233 /* Try applying the operator to ELT and see if that simplifies.
4234 We can duplicate the result if so.
4235
4236 The reason we don't use simplify_gen_binary is that it isn't
4237 necessarily a win to convert things like:
4238
4239 (plus:V (vec_duplicate:V (reg:S R1))
4240 (vec_duplicate:V (reg:S R2)))
4241
4242 to:
4243
4244 (vec_duplicate:V (plus:S (reg:S R1) (reg:S R2)))
4245
4246 The first might be done entirely in vector registers while the
4247 second might need a move between register files. */
4248 tem = simplify_binary_operation (code, GET_MODE_INNER (mode),
4249 elt0, elt1);
4250 if (tem)
4251 return gen_vec_duplicate (mode, tem);
4252 }
4253
4254 return 0;
4255 }
4256
4257 /* Return true if binary operation OP distributes over addition in operand
4258 OPNO, with the other operand being held constant. OPNO counts from 1. */
4259
4260 static bool
4261 distributes_over_addition_p (rtx_code op, int opno)
4262 {
4263 switch (op)
4264 {
4265 case PLUS:
4266 case MINUS:
4267 case MULT:
4268 return true;
4269
4270 case ASHIFT:
4271 return opno == 1;
4272
4273 default:
4274 return false;
4275 }
4276 }
4277
4278 rtx
4279 simplify_const_binary_operation (enum rtx_code code, machine_mode mode,
4280 rtx op0, rtx op1)
4281 {
4282 if (VECTOR_MODE_P (mode)
4283 && code != VEC_CONCAT
4284 && GET_CODE (op0) == CONST_VECTOR
4285 && GET_CODE (op1) == CONST_VECTOR)
4286 {
4287 bool step_ok_p;
4288 if (CONST_VECTOR_STEPPED_P (op0)
4289 && CONST_VECTOR_STEPPED_P (op1))
4290 /* We can operate directly on the encoding if:
4291
4292 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
4293 implies
4294 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
4295
4296 Addition and subtraction are the supported operators
4297 for which this is true. */
4298 step_ok_p = (code == PLUS || code == MINUS);
4299 else if (CONST_VECTOR_STEPPED_P (op0))
4300 /* We can operate directly on stepped encodings if:
4301
4302 a3 - a2 == a2 - a1
4303 implies:
4304 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
4305
4306 which is true if (x -> x op c) distributes over addition. */
4307 step_ok_p = distributes_over_addition_p (code, 1);
4308 else
4309 /* Similarly in reverse. */
4310 step_ok_p = distributes_over_addition_p (code, 2);
4311 rtx_vector_builder builder;
4312 if (!builder.new_binary_operation (mode, op0, op1, step_ok_p))
4313 return 0;
4314
4315 unsigned int count = builder.encoded_nelts ();
4316 for (unsigned int i = 0; i < count; i++)
4317 {
4318 rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
4319 CONST_VECTOR_ELT (op0, i),
4320 CONST_VECTOR_ELT (op1, i));
4321 if (!x || !valid_for_const_vector_p (mode, x))
4322 return 0;
4323 builder.quick_push (x);
4324 }
4325 return builder.build ();
4326 }
4327
4328 if (VECTOR_MODE_P (mode)
4329 && code == VEC_CONCAT
4330 && (CONST_SCALAR_INT_P (op0)
4331 || CONST_FIXED_P (op0)
4332 || CONST_DOUBLE_AS_FLOAT_P (op0))
4333 && (CONST_SCALAR_INT_P (op1)
4334 || CONST_DOUBLE_AS_FLOAT_P (op1)
4335 || CONST_FIXED_P (op1)))
4336 {
4337 /* Both inputs have a constant number of elements, so the result
4338 must too. */
4339 unsigned n_elts = GET_MODE_NUNITS (mode).to_constant ();
4340 rtvec v = rtvec_alloc (n_elts);
4341
4342 gcc_assert (n_elts >= 2);
4343 if (n_elts == 2)
4344 {
4345 gcc_assert (GET_CODE (op0) != CONST_VECTOR);
4346 gcc_assert (GET_CODE (op1) != CONST_VECTOR);
4347
4348 RTVEC_ELT (v, 0) = op0;
4349 RTVEC_ELT (v, 1) = op1;
4350 }
4351 else
4352 {
4353 unsigned op0_n_elts = GET_MODE_NUNITS (GET_MODE (op0)).to_constant ();
4354 unsigned op1_n_elts = GET_MODE_NUNITS (GET_MODE (op1)).to_constant ();
4355 unsigned i;
4356
4357 gcc_assert (GET_CODE (op0) == CONST_VECTOR);
4358 gcc_assert (GET_CODE (op1) == CONST_VECTOR);
4359 gcc_assert (op0_n_elts + op1_n_elts == n_elts);
4360
4361 for (i = 0; i < op0_n_elts; ++i)
4362 RTVEC_ELT (v, i) = CONST_VECTOR_ELT (op0, i);
4363 for (i = 0; i < op1_n_elts; ++i)
4364 RTVEC_ELT (v, op0_n_elts+i) = CONST_VECTOR_ELT (op1, i);
4365 }
4366
4367 return gen_rtx_CONST_VECTOR (mode, v);
4368 }
4369
4370 if (SCALAR_FLOAT_MODE_P (mode)
4371 && CONST_DOUBLE_AS_FLOAT_P (op0)
4372 && CONST_DOUBLE_AS_FLOAT_P (op1)
4373 && mode == GET_MODE (op0) && mode == GET_MODE (op1))
4374 {
4375 if (code == AND
4376 || code == IOR
4377 || code == XOR)
4378 {
4379 long tmp0[4];
4380 long tmp1[4];
4381 REAL_VALUE_TYPE r;
4382 int i;
4383
4384 real_to_target (tmp0, CONST_DOUBLE_REAL_VALUE (op0),
4385 GET_MODE (op0));
4386 real_to_target (tmp1, CONST_DOUBLE_REAL_VALUE (op1),
4387 GET_MODE (op1));
4388 for (i = 0; i < 4; i++)
4389 {
4390 switch (code)
4391 {
4392 case AND:
4393 tmp0[i] &= tmp1[i];
4394 break;
4395 case IOR:
4396 tmp0[i] |= tmp1[i];
4397 break;
4398 case XOR:
4399 tmp0[i] ^= tmp1[i];
4400 break;
4401 default:
4402 gcc_unreachable ();
4403 }
4404 }
4405 real_from_target (&r, tmp0, mode);
4406 return const_double_from_real_value (r, mode);
4407 }
4408 else
4409 {
4410 REAL_VALUE_TYPE f0, f1, value, result;
4411 const REAL_VALUE_TYPE *opr0, *opr1;
4412 bool inexact;
4413
4414 opr0 = CONST_DOUBLE_REAL_VALUE (op0);
4415 opr1 = CONST_DOUBLE_REAL_VALUE (op1);
4416
4417 if (HONOR_SNANS (mode)
4418 && (REAL_VALUE_ISSIGNALING_NAN (*opr0)
4419 || REAL_VALUE_ISSIGNALING_NAN (*opr1)))
4420 return 0;
4421
4422 real_convert (&f0, mode, opr0);
4423 real_convert (&f1, mode, opr1);
4424
4425 if (code == DIV
4426 && real_equal (&f1, &dconst0)
4427 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
4428 return 0;
4429
4430 if (MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
4431 && flag_trapping_math
4432 && REAL_VALUE_ISINF (f0) && REAL_VALUE_ISINF (f1))
4433 {
4434 int s0 = REAL_VALUE_NEGATIVE (f0);
4435 int s1 = REAL_VALUE_NEGATIVE (f1);
4436
4437 switch (code)
4438 {
4439 case PLUS:
4440 /* Inf + -Inf = NaN plus exception. */
4441 if (s0 != s1)
4442 return 0;
4443 break;
4444 case MINUS:
4445 /* Inf - Inf = NaN plus exception. */
4446 if (s0 == s1)
4447 return 0;
4448 break;
4449 case DIV:
4450 /* Inf / Inf = NaN plus exception. */
4451 return 0;
4452 default:
4453 break;
4454 }
4455 }
4456
4457 if (code == MULT && MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
4458 && flag_trapping_math
4459 && ((REAL_VALUE_ISINF (f0) && real_equal (&f1, &dconst0))
4460 || (REAL_VALUE_ISINF (f1)
4461 && real_equal (&f0, &dconst0))))
4462 /* Inf * 0 = NaN plus exception. */
4463 return 0;
4464
4465 inexact = real_arithmetic (&value, rtx_to_tree_code (code),
4466 &f0, &f1);
4467 real_convert (&result, mode, &value);
4468
4469 /* Don't constant fold this floating point operation if
4470 the result has overflowed and flag_trapping_math. */
4471
4472 if (flag_trapping_math
4473 && MODE_HAS_INFINITIES (mode)
4474 && REAL_VALUE_ISINF (result)
4475 && !REAL_VALUE_ISINF (f0)
4476 && !REAL_VALUE_ISINF (f1))
4477 /* Overflow plus exception. */
4478 return 0;
4479
4480 /* Don't constant fold this floating point operation if the
4481 result may dependent upon the run-time rounding mode and
4482 flag_rounding_math is set, or if GCC's software emulation
4483 is unable to accurately represent the result. */
4484
4485 if ((flag_rounding_math
4486 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
4487 && (inexact || !real_identical (&result, &value)))
4488 return NULL_RTX;
4489
4490 return const_double_from_real_value (result, mode);
4491 }
4492 }
4493
4494 /* We can fold some multi-word operations. */
4495 scalar_int_mode int_mode;
4496 if (is_a <scalar_int_mode> (mode, &int_mode)
4497 && CONST_SCALAR_INT_P (op0)
4498 && CONST_SCALAR_INT_P (op1)
4499 && GET_MODE_PRECISION (int_mode) <= MAX_BITSIZE_MODE_ANY_INT)
4500 {
4501 wide_int result;
4502 wi::overflow_type overflow;
4503 rtx_mode_t pop0 = rtx_mode_t (op0, int_mode);
4504 rtx_mode_t pop1 = rtx_mode_t (op1, int_mode);
4505
4506 #if TARGET_SUPPORTS_WIDE_INT == 0
4507 /* This assert keeps the simplification from producing a result
4508 that cannot be represented in a CONST_DOUBLE but a lot of
4509 upstream callers expect that this function never fails to
4510 simplify something and so you if you added this to the test
4511 above the code would die later anyway. If this assert
4512 happens, you just need to make the port support wide int. */
4513 gcc_assert (GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_DOUBLE_INT);
4514 #endif
4515 switch (code)
4516 {
4517 case MINUS:
4518 result = wi::sub (pop0, pop1);
4519 break;
4520
4521 case PLUS:
4522 result = wi::add (pop0, pop1);
4523 break;
4524
4525 case MULT:
4526 result = wi::mul (pop0, pop1);
4527 break;
4528
4529 case DIV:
4530 result = wi::div_trunc (pop0, pop1, SIGNED, &overflow);
4531 if (overflow)
4532 return NULL_RTX;
4533 break;
4534
4535 case MOD:
4536 result = wi::mod_trunc (pop0, pop1, SIGNED, &overflow);
4537 if (overflow)
4538 return NULL_RTX;
4539 break;
4540
4541 case UDIV:
4542 result = wi::div_trunc (pop0, pop1, UNSIGNED, &overflow);
4543 if (overflow)
4544 return NULL_RTX;
4545 break;
4546
4547 case UMOD:
4548 result = wi::mod_trunc (pop0, pop1, UNSIGNED, &overflow);
4549 if (overflow)
4550 return NULL_RTX;
4551 break;
4552
4553 case AND:
4554 result = wi::bit_and (pop0, pop1);
4555 break;
4556
4557 case IOR:
4558 result = wi::bit_or (pop0, pop1);
4559 break;
4560
4561 case XOR:
4562 result = wi::bit_xor (pop0, pop1);
4563 break;
4564
4565 case SMIN:
4566 result = wi::smin (pop0, pop1);
4567 break;
4568
4569 case SMAX:
4570 result = wi::smax (pop0, pop1);
4571 break;
4572
4573 case UMIN:
4574 result = wi::umin (pop0, pop1);
4575 break;
4576
4577 case UMAX:
4578 result = wi::umax (pop0, pop1);
4579 break;
4580
4581 case LSHIFTRT:
4582 case ASHIFTRT:
4583 case ASHIFT:
4584 {
4585 wide_int wop1 = pop1;
4586 if (SHIFT_COUNT_TRUNCATED)
4587 wop1 = wi::umod_trunc (wop1, GET_MODE_PRECISION (int_mode));
4588 else if (wi::geu_p (wop1, GET_MODE_PRECISION (int_mode)))
4589 return NULL_RTX;
4590
4591 switch (code)
4592 {
4593 case LSHIFTRT:
4594 result = wi::lrshift (pop0, wop1);
4595 break;
4596
4597 case ASHIFTRT:
4598 result = wi::arshift (pop0, wop1);
4599 break;
4600
4601 case ASHIFT:
4602 result = wi::lshift (pop0, wop1);
4603 break;
4604
4605 default:
4606 gcc_unreachable ();
4607 }
4608 break;
4609 }
4610 case ROTATE:
4611 case ROTATERT:
4612 {
4613 if (wi::neg_p (pop1))
4614 return NULL_RTX;
4615
4616 switch (code)
4617 {
4618 case ROTATE:
4619 result = wi::lrotate (pop0, pop1);
4620 break;
4621
4622 case ROTATERT:
4623 result = wi::rrotate (pop0, pop1);
4624 break;
4625
4626 default:
4627 gcc_unreachable ();
4628 }
4629 break;
4630 }
4631 default:
4632 return NULL_RTX;
4633 }
4634 return immed_wide_int_const (result, int_mode);
4635 }
4636
4637 /* Handle polynomial integers. */
4638 if (NUM_POLY_INT_COEFFS > 1
4639 && is_a <scalar_int_mode> (mode, &int_mode)
4640 && poly_int_rtx_p (op0)
4641 && poly_int_rtx_p (op1))
4642 {
4643 poly_wide_int result;
4644 switch (code)
4645 {
4646 case PLUS:
4647 result = wi::to_poly_wide (op0, mode) + wi::to_poly_wide (op1, mode);
4648 break;
4649
4650 case MINUS:
4651 result = wi::to_poly_wide (op0, mode) - wi::to_poly_wide (op1, mode);
4652 break;
4653
4654 case MULT:
4655 if (CONST_SCALAR_INT_P (op1))
4656 result = wi::to_poly_wide (op0, mode) * rtx_mode_t (op1, mode);
4657 else
4658 return NULL_RTX;
4659 break;
4660
4661 case ASHIFT:
4662 if (CONST_SCALAR_INT_P (op1))
4663 {
4664 wide_int shift = rtx_mode_t (op1, mode);
4665 if (SHIFT_COUNT_TRUNCATED)
4666 shift = wi::umod_trunc (shift, GET_MODE_PRECISION (int_mode));
4667 else if (wi::geu_p (shift, GET_MODE_PRECISION (int_mode)))
4668 return NULL_RTX;
4669 result = wi::to_poly_wide (op0, mode) << shift;
4670 }
4671 else
4672 return NULL_RTX;
4673 break;
4674
4675 case IOR:
4676 if (!CONST_SCALAR_INT_P (op1)
4677 || !can_ior_p (wi::to_poly_wide (op0, mode),
4678 rtx_mode_t (op1, mode), &result))
4679 return NULL_RTX;
4680 break;
4681
4682 default:
4683 return NULL_RTX;
4684 }
4685 return immed_wide_int_const (result, int_mode);
4686 }
4687
4688 return NULL_RTX;
4689 }
4690
4691
4692 \f
4693 /* Return a positive integer if X should sort after Y. The value
4694 returned is 1 if and only if X and Y are both regs. */
4695
4696 static int
4697 simplify_plus_minus_op_data_cmp (rtx x, rtx y)
4698 {
4699 int result;
4700
4701 result = (commutative_operand_precedence (y)
4702 - commutative_operand_precedence (x));
4703 if (result)
4704 return result + result;
4705
4706 /* Group together equal REGs to do more simplification. */
4707 if (REG_P (x) && REG_P (y))
4708 return REGNO (x) > REGNO (y);
4709
4710 return 0;
4711 }
4712
4713 /* Simplify and canonicalize a PLUS or MINUS, at least one of whose
4714 operands may be another PLUS or MINUS.
4715
4716 Rather than test for specific case, we do this by a brute-force method
4717 and do all possible simplifications until no more changes occur. Then
4718 we rebuild the operation.
4719
4720 May return NULL_RTX when no changes were made. */
4721
4722 static rtx
4723 simplify_plus_minus (enum rtx_code code, machine_mode mode, rtx op0,
4724 rtx op1)
4725 {
4726 struct simplify_plus_minus_op_data
4727 {
4728 rtx op;
4729 short neg;
4730 } ops[16];
4731 rtx result, tem;
4732 int n_ops = 2;
4733 int changed, n_constants, canonicalized = 0;
4734 int i, j;
4735
4736 memset (ops, 0, sizeof ops);
4737
4738 /* Set up the two operands and then expand them until nothing has been
4739 changed. If we run out of room in our array, give up; this should
4740 almost never happen. */
4741
4742 ops[0].op = op0;
4743 ops[0].neg = 0;
4744 ops[1].op = op1;
4745 ops[1].neg = (code == MINUS);
4746
4747 do
4748 {
4749 changed = 0;
4750 n_constants = 0;
4751
4752 for (i = 0; i < n_ops; i++)
4753 {
4754 rtx this_op = ops[i].op;
4755 int this_neg = ops[i].neg;
4756 enum rtx_code this_code = GET_CODE (this_op);
4757
4758 switch (this_code)
4759 {
4760 case PLUS:
4761 case MINUS:
4762 if (n_ops == ARRAY_SIZE (ops))
4763 return NULL_RTX;
4764
4765 ops[n_ops].op = XEXP (this_op, 1);
4766 ops[n_ops].neg = (this_code == MINUS) ^ this_neg;
4767 n_ops++;
4768
4769 ops[i].op = XEXP (this_op, 0);
4770 changed = 1;
4771 /* If this operand was negated then we will potentially
4772 canonicalize the expression. Similarly if we don't
4773 place the operands adjacent we're re-ordering the
4774 expression and thus might be performing a
4775 canonicalization. Ignore register re-ordering.
4776 ??? It might be better to shuffle the ops array here,
4777 but then (plus (plus (A, B), plus (C, D))) wouldn't
4778 be seen as non-canonical. */
4779 if (this_neg
4780 || (i != n_ops - 2
4781 && !(REG_P (ops[i].op) && REG_P (ops[n_ops - 1].op))))
4782 canonicalized = 1;
4783 break;
4784
4785 case NEG:
4786 ops[i].op = XEXP (this_op, 0);
4787 ops[i].neg = ! this_neg;
4788 changed = 1;
4789 canonicalized = 1;
4790 break;
4791
4792 case CONST:
4793 if (n_ops != ARRAY_SIZE (ops)
4794 && GET_CODE (XEXP (this_op, 0)) == PLUS
4795 && CONSTANT_P (XEXP (XEXP (this_op, 0), 0))
4796 && CONSTANT_P (XEXP (XEXP (this_op, 0), 1)))
4797 {
4798 ops[i].op = XEXP (XEXP (this_op, 0), 0);
4799 ops[n_ops].op = XEXP (XEXP (this_op, 0), 1);
4800 ops[n_ops].neg = this_neg;
4801 n_ops++;
4802 changed = 1;
4803 canonicalized = 1;
4804 }
4805 break;
4806
4807 case NOT:
4808 /* ~a -> (-a - 1) */
4809 if (n_ops != ARRAY_SIZE (ops))
4810 {
4811 ops[n_ops].op = CONSTM1_RTX (mode);
4812 ops[n_ops++].neg = this_neg;
4813 ops[i].op = XEXP (this_op, 0);
4814 ops[i].neg = !this_neg;
4815 changed = 1;
4816 canonicalized = 1;
4817 }
4818 break;
4819
4820 CASE_CONST_SCALAR_INT:
4821 case CONST_POLY_INT:
4822 n_constants++;
4823 if (this_neg)
4824 {
4825 ops[i].op = neg_poly_int_rtx (mode, this_op);
4826 ops[i].neg = 0;
4827 changed = 1;
4828 canonicalized = 1;
4829 }
4830 break;
4831
4832 default:
4833 break;
4834 }
4835 }
4836 }
4837 while (changed);
4838
4839 if (n_constants > 1)
4840 canonicalized = 1;
4841
4842 gcc_assert (n_ops >= 2);
4843
4844 /* If we only have two operands, we can avoid the loops. */
4845 if (n_ops == 2)
4846 {
4847 enum rtx_code code = ops[0].neg || ops[1].neg ? MINUS : PLUS;
4848 rtx lhs, rhs;
4849
4850 /* Get the two operands. Be careful with the order, especially for
4851 the cases where code == MINUS. */
4852 if (ops[0].neg && ops[1].neg)
4853 {
4854 lhs = gen_rtx_NEG (mode, ops[0].op);
4855 rhs = ops[1].op;
4856 }
4857 else if (ops[0].neg)
4858 {
4859 lhs = ops[1].op;
4860 rhs = ops[0].op;
4861 }
4862 else
4863 {
4864 lhs = ops[0].op;
4865 rhs = ops[1].op;
4866 }
4867
4868 return simplify_const_binary_operation (code, mode, lhs, rhs);
4869 }
4870
4871 /* Now simplify each pair of operands until nothing changes. */
4872 while (1)
4873 {
4874 /* Insertion sort is good enough for a small array. */
4875 for (i = 1; i < n_ops; i++)
4876 {
4877 struct simplify_plus_minus_op_data save;
4878 int cmp;
4879
4880 j = i - 1;
4881 cmp = simplify_plus_minus_op_data_cmp (ops[j].op, ops[i].op);
4882 if (cmp <= 0)
4883 continue;
4884 /* Just swapping registers doesn't count as canonicalization. */
4885 if (cmp != 1)
4886 canonicalized = 1;
4887
4888 save = ops[i];
4889 do
4890 ops[j + 1] = ops[j];
4891 while (j--
4892 && simplify_plus_minus_op_data_cmp (ops[j].op, save.op) > 0);
4893 ops[j + 1] = save;
4894 }
4895
4896 changed = 0;
4897 for (i = n_ops - 1; i > 0; i--)
4898 for (j = i - 1; j >= 0; j--)
4899 {
4900 rtx lhs = ops[j].op, rhs = ops[i].op;
4901 int lneg = ops[j].neg, rneg = ops[i].neg;
4902
4903 if (lhs != 0 && rhs != 0)
4904 {
4905 enum rtx_code ncode = PLUS;
4906
4907 if (lneg != rneg)
4908 {
4909 ncode = MINUS;
4910 if (lneg)
4911 std::swap (lhs, rhs);
4912 }
4913 else if (swap_commutative_operands_p (lhs, rhs))
4914 std::swap (lhs, rhs);
4915
4916 if ((GET_CODE (lhs) == CONST || CONST_INT_P (lhs))
4917 && (GET_CODE (rhs) == CONST || CONST_INT_P (rhs)))
4918 {
4919 rtx tem_lhs, tem_rhs;
4920
4921 tem_lhs = GET_CODE (lhs) == CONST ? XEXP (lhs, 0) : lhs;
4922 tem_rhs = GET_CODE (rhs) == CONST ? XEXP (rhs, 0) : rhs;
4923 tem = simplify_binary_operation (ncode, mode, tem_lhs,
4924 tem_rhs);
4925
4926 if (tem && !CONSTANT_P (tem))
4927 tem = gen_rtx_CONST (GET_MODE (tem), tem);
4928 }
4929 else
4930 tem = simplify_binary_operation (ncode, mode, lhs, rhs);
4931
4932 if (tem)
4933 {
4934 /* Reject "simplifications" that just wrap the two
4935 arguments in a CONST. Failure to do so can result
4936 in infinite recursion with simplify_binary_operation
4937 when it calls us to simplify CONST operations.
4938 Also, if we find such a simplification, don't try
4939 any more combinations with this rhs: We must have
4940 something like symbol+offset, ie. one of the
4941 trivial CONST expressions we handle later. */
4942 if (GET_CODE (tem) == CONST
4943 && GET_CODE (XEXP (tem, 0)) == ncode
4944 && XEXP (XEXP (tem, 0), 0) == lhs
4945 && XEXP (XEXP (tem, 0), 1) == rhs)
4946 break;
4947 lneg &= rneg;
4948 if (GET_CODE (tem) == NEG)
4949 tem = XEXP (tem, 0), lneg = !lneg;
4950 if (poly_int_rtx_p (tem) && lneg)
4951 tem = neg_poly_int_rtx (mode, tem), lneg = 0;
4952
4953 ops[i].op = tem;
4954 ops[i].neg = lneg;
4955 ops[j].op = NULL_RTX;
4956 changed = 1;
4957 canonicalized = 1;
4958 }
4959 }
4960 }
4961
4962 if (!changed)
4963 break;
4964
4965 /* Pack all the operands to the lower-numbered entries. */
4966 for (i = 0, j = 0; j < n_ops; j++)
4967 if (ops[j].op)
4968 {
4969 ops[i] = ops[j];
4970 i++;
4971 }
4972 n_ops = i;
4973 }
4974
4975 /* If nothing changed, check that rematerialization of rtl instructions
4976 is still required. */
4977 if (!canonicalized)
4978 {
4979 /* Perform rematerialization if only all operands are registers and
4980 all operations are PLUS. */
4981 /* ??? Also disallow (non-global, non-frame) fixed registers to work
4982 around rs6000 and how it uses the CA register. See PR67145. */
4983 for (i = 0; i < n_ops; i++)
4984 if (ops[i].neg
4985 || !REG_P (ops[i].op)
4986 || (REGNO (ops[i].op) < FIRST_PSEUDO_REGISTER
4987 && fixed_regs[REGNO (ops[i].op)]
4988 && !global_regs[REGNO (ops[i].op)]
4989 && ops[i].op != frame_pointer_rtx
4990 && ops[i].op != arg_pointer_rtx
4991 && ops[i].op != stack_pointer_rtx))
4992 return NULL_RTX;
4993 goto gen_result;
4994 }
4995
4996 /* Create (minus -C X) instead of (neg (const (plus X C))). */
4997 if (n_ops == 2
4998 && CONST_INT_P (ops[1].op)
4999 && CONSTANT_P (ops[0].op)
5000 && ops[0].neg)
5001 return gen_rtx_fmt_ee (MINUS, mode, ops[1].op, ops[0].op);
5002
5003 /* We suppressed creation of trivial CONST expressions in the
5004 combination loop to avoid recursion. Create one manually now.
5005 The combination loop should have ensured that there is exactly
5006 one CONST_INT, and the sort will have ensured that it is last
5007 in the array and that any other constant will be next-to-last. */
5008
5009 if (n_ops > 1
5010 && poly_int_rtx_p (ops[n_ops - 1].op)
5011 && CONSTANT_P (ops[n_ops - 2].op))
5012 {
5013 rtx value = ops[n_ops - 1].op;
5014 if (ops[n_ops - 1].neg ^ ops[n_ops - 2].neg)
5015 value = neg_poly_int_rtx (mode, value);
5016 if (CONST_INT_P (value))
5017 {
5018 ops[n_ops - 2].op = plus_constant (mode, ops[n_ops - 2].op,
5019 INTVAL (value));
5020 n_ops--;
5021 }
5022 }
5023
5024 /* Put a non-negated operand first, if possible. */
5025
5026 for (i = 0; i < n_ops && ops[i].neg; i++)
5027 continue;
5028 if (i == n_ops)
5029 ops[0].op = gen_rtx_NEG (mode, ops[0].op);
5030 else if (i != 0)
5031 {
5032 tem = ops[0].op;
5033 ops[0] = ops[i];
5034 ops[i].op = tem;
5035 ops[i].neg = 1;
5036 }
5037
5038 /* Now make the result by performing the requested operations. */
5039 gen_result:
5040 result = ops[0].op;
5041 for (i = 1; i < n_ops; i++)
5042 result = gen_rtx_fmt_ee (ops[i].neg ? MINUS : PLUS,
5043 mode, result, ops[i].op);
5044
5045 return result;
5046 }
5047
5048 /* Check whether an operand is suitable for calling simplify_plus_minus. */
5049 static bool
5050 plus_minus_operand_p (const_rtx x)
5051 {
5052 return GET_CODE (x) == PLUS
5053 || GET_CODE (x) == MINUS
5054 || (GET_CODE (x) == CONST
5055 && GET_CODE (XEXP (x, 0)) == PLUS
5056 && CONSTANT_P (XEXP (XEXP (x, 0), 0))
5057 && CONSTANT_P (XEXP (XEXP (x, 0), 1)));
5058 }
5059
5060 /* Like simplify_binary_operation except used for relational operators.
5061 MODE is the mode of the result. If MODE is VOIDmode, both operands must
5062 not also be VOIDmode.
5063
5064 CMP_MODE specifies in which mode the comparison is done in, so it is
5065 the mode of the operands. If CMP_MODE is VOIDmode, it is taken from
5066 the operands or, if both are VOIDmode, the operands are compared in
5067 "infinite precision". */
5068 rtx
5069 simplify_relational_operation (enum rtx_code code, machine_mode mode,
5070 machine_mode cmp_mode, rtx op0, rtx op1)
5071 {
5072 rtx tem, trueop0, trueop1;
5073
5074 if (cmp_mode == VOIDmode)
5075 cmp_mode = GET_MODE (op0);
5076 if (cmp_mode == VOIDmode)
5077 cmp_mode = GET_MODE (op1);
5078
5079 tem = simplify_const_relational_operation (code, cmp_mode, op0, op1);
5080 if (tem)
5081 {
5082 if (SCALAR_FLOAT_MODE_P (mode))
5083 {
5084 if (tem == const0_rtx)
5085 return CONST0_RTX (mode);
5086 #ifdef FLOAT_STORE_FLAG_VALUE
5087 {
5088 REAL_VALUE_TYPE val;
5089 val = FLOAT_STORE_FLAG_VALUE (mode);
5090 return const_double_from_real_value (val, mode);
5091 }
5092 #else
5093 return NULL_RTX;
5094 #endif
5095 }
5096 if (VECTOR_MODE_P (mode))
5097 {
5098 if (tem == const0_rtx)
5099 return CONST0_RTX (mode);
5100 #ifdef VECTOR_STORE_FLAG_VALUE
5101 {
5102 rtx val = VECTOR_STORE_FLAG_VALUE (mode);
5103 if (val == NULL_RTX)
5104 return NULL_RTX;
5105 if (val == const1_rtx)
5106 return CONST1_RTX (mode);
5107
5108 return gen_const_vec_duplicate (mode, val);
5109 }
5110 #else
5111 return NULL_RTX;
5112 #endif
5113 }
5114 /* For vector comparison with scalar int result, it is unknown
5115 if the target means here a comparison into an integral bitmask,
5116 or comparison where all comparisons true mean const_true_rtx
5117 whole result, or where any comparisons true mean const_true_rtx
5118 whole result. For const0_rtx all the cases are the same. */
5119 if (VECTOR_MODE_P (cmp_mode)
5120 && SCALAR_INT_MODE_P (mode)
5121 && tem == const_true_rtx)
5122 return NULL_RTX;
5123
5124 return tem;
5125 }
5126
5127 /* For the following tests, ensure const0_rtx is op1. */
5128 if (swap_commutative_operands_p (op0, op1)
5129 || (op0 == const0_rtx && op1 != const0_rtx))
5130 std::swap (op0, op1), code = swap_condition (code);
5131
5132 /* If op0 is a compare, extract the comparison arguments from it. */
5133 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
5134 return simplify_gen_relational (code, mode, VOIDmode,
5135 XEXP (op0, 0), XEXP (op0, 1));
5136
5137 if (GET_MODE_CLASS (cmp_mode) == MODE_CC
5138 || CC0_P (op0))
5139 return NULL_RTX;
5140
5141 trueop0 = avoid_constant_pool_reference (op0);
5142 trueop1 = avoid_constant_pool_reference (op1);
5143 return simplify_relational_operation_1 (code, mode, cmp_mode,
5144 trueop0, trueop1);
5145 }
5146
5147 /* This part of simplify_relational_operation is only used when CMP_MODE
5148 is not in class MODE_CC (i.e. it is a real comparison).
5149
5150 MODE is the mode of the result, while CMP_MODE specifies in which
5151 mode the comparison is done in, so it is the mode of the operands. */
5152
5153 static rtx
5154 simplify_relational_operation_1 (enum rtx_code code, machine_mode mode,
5155 machine_mode cmp_mode, rtx op0, rtx op1)
5156 {
5157 enum rtx_code op0code = GET_CODE (op0);
5158
5159 if (op1 == const0_rtx && COMPARISON_P (op0))
5160 {
5161 /* If op0 is a comparison, extract the comparison arguments
5162 from it. */
5163 if (code == NE)
5164 {
5165 if (GET_MODE (op0) == mode)
5166 return simplify_rtx (op0);
5167 else
5168 return simplify_gen_relational (GET_CODE (op0), mode, VOIDmode,
5169 XEXP (op0, 0), XEXP (op0, 1));
5170 }
5171 else if (code == EQ)
5172 {
5173 enum rtx_code new_code = reversed_comparison_code (op0, NULL);
5174 if (new_code != UNKNOWN)
5175 return simplify_gen_relational (new_code, mode, VOIDmode,
5176 XEXP (op0, 0), XEXP (op0, 1));
5177 }
5178 }
5179
5180 /* (LTU/GEU (PLUS a C) C), where C is constant, can be simplified to
5181 (GEU/LTU a -C). Likewise for (LTU/GEU (PLUS a C) a). */
5182 if ((code == LTU || code == GEU)
5183 && GET_CODE (op0) == PLUS
5184 && CONST_INT_P (XEXP (op0, 1))
5185 && (rtx_equal_p (op1, XEXP (op0, 0))
5186 || rtx_equal_p (op1, XEXP (op0, 1)))
5187 /* (LTU/GEU (PLUS a 0) 0) is not the same as (GEU/LTU a 0). */
5188 && XEXP (op0, 1) != const0_rtx)
5189 {
5190 rtx new_cmp
5191 = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
5192 return simplify_gen_relational ((code == LTU ? GEU : LTU), mode,
5193 cmp_mode, XEXP (op0, 0), new_cmp);
5194 }
5195
5196 /* (GTU (PLUS a C) (C - 1)) where C is a non-zero constant can be
5197 transformed into (LTU a -C). */
5198 if (code == GTU && GET_CODE (op0) == PLUS && CONST_INT_P (op1)
5199 && CONST_INT_P (XEXP (op0, 1))
5200 && (UINTVAL (op1) == UINTVAL (XEXP (op0, 1)) - 1)
5201 && XEXP (op0, 1) != const0_rtx)
5202 {
5203 rtx new_cmp
5204 = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
5205 return simplify_gen_relational (LTU, mode, cmp_mode,
5206 XEXP (op0, 0), new_cmp);
5207 }
5208
5209 /* Canonicalize (LTU/GEU (PLUS a b) b) as (LTU/GEU (PLUS a b) a). */
5210 if ((code == LTU || code == GEU)
5211 && GET_CODE (op0) == PLUS
5212 && rtx_equal_p (op1, XEXP (op0, 1))
5213 /* Don't recurse "infinitely" for (LTU/GEU (PLUS b b) b). */
5214 && !rtx_equal_p (op1, XEXP (op0, 0)))
5215 return simplify_gen_relational (code, mode, cmp_mode, op0,
5216 copy_rtx (XEXP (op0, 0)));
5217
5218 if (op1 == const0_rtx)
5219 {
5220 /* Canonicalize (GTU x 0) as (NE x 0). */
5221 if (code == GTU)
5222 return simplify_gen_relational (NE, mode, cmp_mode, op0, op1);
5223 /* Canonicalize (LEU x 0) as (EQ x 0). */
5224 if (code == LEU)
5225 return simplify_gen_relational (EQ, mode, cmp_mode, op0, op1);
5226 }
5227 else if (op1 == const1_rtx)
5228 {
5229 switch (code)
5230 {
5231 case GE:
5232 /* Canonicalize (GE x 1) as (GT x 0). */
5233 return simplify_gen_relational (GT, mode, cmp_mode,
5234 op0, const0_rtx);
5235 case GEU:
5236 /* Canonicalize (GEU x 1) as (NE x 0). */
5237 return simplify_gen_relational (NE, mode, cmp_mode,
5238 op0, const0_rtx);
5239 case LT:
5240 /* Canonicalize (LT x 1) as (LE x 0). */
5241 return simplify_gen_relational (LE, mode, cmp_mode,
5242 op0, const0_rtx);
5243 case LTU:
5244 /* Canonicalize (LTU x 1) as (EQ x 0). */
5245 return simplify_gen_relational (EQ, mode, cmp_mode,
5246 op0, const0_rtx);
5247 default:
5248 break;
5249 }
5250 }
5251 else if (op1 == constm1_rtx)
5252 {
5253 /* Canonicalize (LE x -1) as (LT x 0). */
5254 if (code == LE)
5255 return simplify_gen_relational (LT, mode, cmp_mode, op0, const0_rtx);
5256 /* Canonicalize (GT x -1) as (GE x 0). */
5257 if (code == GT)
5258 return simplify_gen_relational (GE, mode, cmp_mode, op0, const0_rtx);
5259 }
5260
5261 /* (eq/ne (plus x cst1) cst2) simplifies to (eq/ne x (cst2 - cst1)) */
5262 if ((code == EQ || code == NE)
5263 && (op0code == PLUS || op0code == MINUS)
5264 && CONSTANT_P (op1)
5265 && CONSTANT_P (XEXP (op0, 1))
5266 && (INTEGRAL_MODE_P (cmp_mode) || flag_unsafe_math_optimizations))
5267 {
5268 rtx x = XEXP (op0, 0);
5269 rtx c = XEXP (op0, 1);
5270 enum rtx_code invcode = op0code == PLUS ? MINUS : PLUS;
5271 rtx tem = simplify_gen_binary (invcode, cmp_mode, op1, c);
5272
5273 /* Detect an infinite recursive condition, where we oscillate at this
5274 simplification case between:
5275 A + B == C <---> C - B == A,
5276 where A, B, and C are all constants with non-simplifiable expressions,
5277 usually SYMBOL_REFs. */
5278 if (GET_CODE (tem) == invcode
5279 && CONSTANT_P (x)
5280 && rtx_equal_p (c, XEXP (tem, 1)))
5281 return NULL_RTX;
5282
5283 return simplify_gen_relational (code, mode, cmp_mode, x, tem);
5284 }
5285
5286 /* (ne:SI (zero_extract:SI FOO (const_int 1) BAR) (const_int 0))) is
5287 the same as (zero_extract:SI FOO (const_int 1) BAR). */
5288 scalar_int_mode int_mode, int_cmp_mode;
5289 if (code == NE
5290 && op1 == const0_rtx
5291 && is_int_mode (mode, &int_mode)
5292 && is_a <scalar_int_mode> (cmp_mode, &int_cmp_mode)
5293 /* ??? Work-around BImode bugs in the ia64 backend. */
5294 && int_mode != BImode
5295 && int_cmp_mode != BImode
5296 && nonzero_bits (op0, int_cmp_mode) == 1
5297 && STORE_FLAG_VALUE == 1)
5298 return GET_MODE_SIZE (int_mode) > GET_MODE_SIZE (int_cmp_mode)
5299 ? simplify_gen_unary (ZERO_EXTEND, int_mode, op0, int_cmp_mode)
5300 : lowpart_subreg (int_mode, op0, int_cmp_mode);
5301
5302 /* (eq/ne (xor x y) 0) simplifies to (eq/ne x y). */
5303 if ((code == EQ || code == NE)
5304 && op1 == const0_rtx
5305 && op0code == XOR)
5306 return simplify_gen_relational (code, mode, cmp_mode,
5307 XEXP (op0, 0), XEXP (op0, 1));
5308
5309 /* (eq/ne (xor x y) x) simplifies to (eq/ne y 0). */
5310 if ((code == EQ || code == NE)
5311 && op0code == XOR
5312 && rtx_equal_p (XEXP (op0, 0), op1)
5313 && !side_effects_p (XEXP (op0, 0)))
5314 return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 1),
5315 CONST0_RTX (mode));
5316
5317 /* Likewise (eq/ne (xor x y) y) simplifies to (eq/ne x 0). */
5318 if ((code == EQ || code == NE)
5319 && op0code == XOR
5320 && rtx_equal_p (XEXP (op0, 1), op1)
5321 && !side_effects_p (XEXP (op0, 1)))
5322 return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
5323 CONST0_RTX (mode));
5324
5325 /* (eq/ne (xor x C1) C2) simplifies to (eq/ne x (C1^C2)). */
5326 if ((code == EQ || code == NE)
5327 && op0code == XOR
5328 && CONST_SCALAR_INT_P (op1)
5329 && CONST_SCALAR_INT_P (XEXP (op0, 1)))
5330 return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
5331 simplify_gen_binary (XOR, cmp_mode,
5332 XEXP (op0, 1), op1));
5333
5334 /* Simplify eq/ne (and/ior x y) x/y) for targets with a BICS instruction or
5335 constant folding if x/y is a constant. */
5336 if ((code == EQ || code == NE)
5337 && (op0code == AND || op0code == IOR)
5338 && !side_effects_p (op1)
5339 && op1 != CONST0_RTX (cmp_mode))
5340 {
5341 /* Both (eq/ne (and x y) x) and (eq/ne (ior x y) y) simplify to
5342 (eq/ne (and (not y) x) 0). */
5343 if ((op0code == AND && rtx_equal_p (XEXP (op0, 0), op1))
5344 || (op0code == IOR && rtx_equal_p (XEXP (op0, 1), op1)))
5345 {
5346 rtx not_y = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 1),
5347 cmp_mode);
5348 rtx lhs = simplify_gen_binary (AND, cmp_mode, not_y, XEXP (op0, 0));
5349
5350 return simplify_gen_relational (code, mode, cmp_mode, lhs,
5351 CONST0_RTX (cmp_mode));
5352 }
5353
5354 /* Both (eq/ne (and x y) y) and (eq/ne (ior x y) x) simplify to
5355 (eq/ne (and (not x) y) 0). */
5356 if ((op0code == AND && rtx_equal_p (XEXP (op0, 1), op1))
5357 || (op0code == IOR && rtx_equal_p (XEXP (op0, 0), op1)))
5358 {
5359 rtx not_x = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 0),
5360 cmp_mode);
5361 rtx lhs = simplify_gen_binary (AND, cmp_mode, not_x, XEXP (op0, 1));
5362
5363 return simplify_gen_relational (code, mode, cmp_mode, lhs,
5364 CONST0_RTX (cmp_mode));
5365 }
5366 }
5367
5368 /* (eq/ne (bswap x) C1) simplifies to (eq/ne x C2) with C2 swapped. */
5369 if ((code == EQ || code == NE)
5370 && GET_CODE (op0) == BSWAP
5371 && CONST_SCALAR_INT_P (op1))
5372 return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
5373 simplify_gen_unary (BSWAP, cmp_mode,
5374 op1, cmp_mode));
5375
5376 /* (eq/ne (bswap x) (bswap y)) simplifies to (eq/ne x y). */
5377 if ((code == EQ || code == NE)
5378 && GET_CODE (op0) == BSWAP
5379 && GET_CODE (op1) == BSWAP)
5380 return simplify_gen_relational (code, mode, cmp_mode,
5381 XEXP (op0, 0), XEXP (op1, 0));
5382
5383 if (op0code == POPCOUNT && op1 == const0_rtx)
5384 switch (code)
5385 {
5386 case EQ:
5387 case LE:
5388 case LEU:
5389 /* (eq (popcount x) (const_int 0)) -> (eq x (const_int 0)). */
5390 return simplify_gen_relational (EQ, mode, GET_MODE (XEXP (op0, 0)),
5391 XEXP (op0, 0), const0_rtx);
5392
5393 case NE:
5394 case GT:
5395 case GTU:
5396 /* (ne (popcount x) (const_int 0)) -> (ne x (const_int 0)). */
5397 return simplify_gen_relational (NE, mode, GET_MODE (XEXP (op0, 0)),
5398 XEXP (op0, 0), const0_rtx);
5399
5400 default:
5401 break;
5402 }
5403
5404 return NULL_RTX;
5405 }
5406
5407 enum
5408 {
5409 CMP_EQ = 1,
5410 CMP_LT = 2,
5411 CMP_GT = 4,
5412 CMP_LTU = 8,
5413 CMP_GTU = 16
5414 };
5415
5416
5417 /* Convert the known results for EQ, LT, GT, LTU, GTU contained in
5418 KNOWN_RESULT to a CONST_INT, based on the requested comparison CODE
5419 For KNOWN_RESULT to make sense it should be either CMP_EQ, or the
5420 logical OR of one of (CMP_LT, CMP_GT) and one of (CMP_LTU, CMP_GTU).
5421 For floating-point comparisons, assume that the operands were ordered. */
5422
5423 static rtx
5424 comparison_result (enum rtx_code code, int known_results)
5425 {
5426 switch (code)
5427 {
5428 case EQ:
5429 case UNEQ:
5430 return (known_results & CMP_EQ) ? const_true_rtx : const0_rtx;
5431 case NE:
5432 case LTGT:
5433 return (known_results & CMP_EQ) ? const0_rtx : const_true_rtx;
5434
5435 case LT:
5436 case UNLT:
5437 return (known_results & CMP_LT) ? const_true_rtx : const0_rtx;
5438 case GE:
5439 case UNGE:
5440 return (known_results & CMP_LT) ? const0_rtx : const_true_rtx;
5441
5442 case GT:
5443 case UNGT:
5444 return (known_results & CMP_GT) ? const_true_rtx : const0_rtx;
5445 case LE:
5446 case UNLE:
5447 return (known_results & CMP_GT) ? const0_rtx : const_true_rtx;
5448
5449 case LTU:
5450 return (known_results & CMP_LTU) ? const_true_rtx : const0_rtx;
5451 case GEU:
5452 return (known_results & CMP_LTU) ? const0_rtx : const_true_rtx;
5453
5454 case GTU:
5455 return (known_results & CMP_GTU) ? const_true_rtx : const0_rtx;
5456 case LEU:
5457 return (known_results & CMP_GTU) ? const0_rtx : const_true_rtx;
5458
5459 case ORDERED:
5460 return const_true_rtx;
5461 case UNORDERED:
5462 return const0_rtx;
5463 default:
5464 gcc_unreachable ();
5465 }
5466 }
5467
5468 /* Check if the given comparison (done in the given MODE) is actually
5469 a tautology or a contradiction. If the mode is VOIDmode, the
5470 comparison is done in "infinite precision". If no simplification
5471 is possible, this function returns zero. Otherwise, it returns
5472 either const_true_rtx or const0_rtx. */
5473
5474 rtx
5475 simplify_const_relational_operation (enum rtx_code code,
5476 machine_mode mode,
5477 rtx op0, rtx op1)
5478 {
5479 rtx tem;
5480 rtx trueop0;
5481 rtx trueop1;
5482
5483 gcc_assert (mode != VOIDmode
5484 || (GET_MODE (op0) == VOIDmode
5485 && GET_MODE (op1) == VOIDmode));
5486
5487 /* If op0 is a compare, extract the comparison arguments from it. */
5488 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
5489 {
5490 op1 = XEXP (op0, 1);
5491 op0 = XEXP (op0, 0);
5492
5493 if (GET_MODE (op0) != VOIDmode)
5494 mode = GET_MODE (op0);
5495 else if (GET_MODE (op1) != VOIDmode)
5496 mode = GET_MODE (op1);
5497 else
5498 return 0;
5499 }
5500
5501 /* We can't simplify MODE_CC values since we don't know what the
5502 actual comparison is. */
5503 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC || CC0_P (op0))
5504 return 0;
5505
5506 /* Make sure the constant is second. */
5507 if (swap_commutative_operands_p (op0, op1))
5508 {
5509 std::swap (op0, op1);
5510 code = swap_condition (code);
5511 }
5512
5513 trueop0 = avoid_constant_pool_reference (op0);
5514 trueop1 = avoid_constant_pool_reference (op1);
5515
5516 /* For integer comparisons of A and B maybe we can simplify A - B and can
5517 then simplify a comparison of that with zero. If A and B are both either
5518 a register or a CONST_INT, this can't help; testing for these cases will
5519 prevent infinite recursion here and speed things up.
5520
5521 We can only do this for EQ and NE comparisons as otherwise we may
5522 lose or introduce overflow which we cannot disregard as undefined as
5523 we do not know the signedness of the operation on either the left or
5524 the right hand side of the comparison. */
5525
5526 if (INTEGRAL_MODE_P (mode) && trueop1 != const0_rtx
5527 && (code == EQ || code == NE)
5528 && ! ((REG_P (op0) || CONST_INT_P (trueop0))
5529 && (REG_P (op1) || CONST_INT_P (trueop1)))
5530 && (tem = simplify_binary_operation (MINUS, mode, op0, op1)) != 0
5531 /* We cannot do this if tem is a nonzero address. */
5532 && ! nonzero_address_p (tem))
5533 return simplify_const_relational_operation (signed_condition (code),
5534 mode, tem, const0_rtx);
5535
5536 if (! HONOR_NANS (mode) && code == ORDERED)
5537 return const_true_rtx;
5538
5539 if (! HONOR_NANS (mode) && code == UNORDERED)
5540 return const0_rtx;
5541
5542 /* For modes without NaNs, if the two operands are equal, we know the
5543 result except if they have side-effects. Even with NaNs we know
5544 the result of unordered comparisons and, if signaling NaNs are
5545 irrelevant, also the result of LT/GT/LTGT. */
5546 if ((! HONOR_NANS (trueop0)
5547 || code == UNEQ || code == UNLE || code == UNGE
5548 || ((code == LT || code == GT || code == LTGT)
5549 && ! HONOR_SNANS (trueop0)))
5550 && rtx_equal_p (trueop0, trueop1)
5551 && ! side_effects_p (trueop0))
5552 return comparison_result (code, CMP_EQ);
5553
5554 /* If the operands are floating-point constants, see if we can fold
5555 the result. */
5556 if (CONST_DOUBLE_AS_FLOAT_P (trueop0)
5557 && CONST_DOUBLE_AS_FLOAT_P (trueop1)
5558 && SCALAR_FLOAT_MODE_P (GET_MODE (trueop0)))
5559 {
5560 const REAL_VALUE_TYPE *d0 = CONST_DOUBLE_REAL_VALUE (trueop0);
5561 const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
5562
5563 /* Comparisons are unordered iff at least one of the values is NaN. */
5564 if (REAL_VALUE_ISNAN (*d0) || REAL_VALUE_ISNAN (*d1))
5565 switch (code)
5566 {
5567 case UNEQ:
5568 case UNLT:
5569 case UNGT:
5570 case UNLE:
5571 case UNGE:
5572 case NE:
5573 case UNORDERED:
5574 return const_true_rtx;
5575 case EQ:
5576 case LT:
5577 case GT:
5578 case LE:
5579 case GE:
5580 case LTGT:
5581 case ORDERED:
5582 return const0_rtx;
5583 default:
5584 return 0;
5585 }
5586
5587 return comparison_result (code,
5588 (real_equal (d0, d1) ? CMP_EQ :
5589 real_less (d0, d1) ? CMP_LT : CMP_GT));
5590 }
5591
5592 /* Otherwise, see if the operands are both integers. */
5593 if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
5594 && CONST_SCALAR_INT_P (trueop0) && CONST_SCALAR_INT_P (trueop1))
5595 {
5596 /* It would be nice if we really had a mode here. However, the
5597 largest int representable on the target is as good as
5598 infinite. */
5599 machine_mode cmode = (mode == VOIDmode) ? MAX_MODE_INT : mode;
5600 rtx_mode_t ptrueop0 = rtx_mode_t (trueop0, cmode);
5601 rtx_mode_t ptrueop1 = rtx_mode_t (trueop1, cmode);
5602
5603 if (wi::eq_p (ptrueop0, ptrueop1))
5604 return comparison_result (code, CMP_EQ);
5605 else
5606 {
5607 int cr = wi::lts_p (ptrueop0, ptrueop1) ? CMP_LT : CMP_GT;
5608 cr |= wi::ltu_p (ptrueop0, ptrueop1) ? CMP_LTU : CMP_GTU;
5609 return comparison_result (code, cr);
5610 }
5611 }
5612
5613 /* Optimize comparisons with upper and lower bounds. */
5614 scalar_int_mode int_mode;
5615 if (CONST_INT_P (trueop1)
5616 && is_a <scalar_int_mode> (mode, &int_mode)
5617 && HWI_COMPUTABLE_MODE_P (int_mode)
5618 && !side_effects_p (trueop0))
5619 {
5620 int sign;
5621 unsigned HOST_WIDE_INT nonzero = nonzero_bits (trueop0, int_mode);
5622 HOST_WIDE_INT val = INTVAL (trueop1);
5623 HOST_WIDE_INT mmin, mmax;
5624
5625 if (code == GEU
5626 || code == LEU
5627 || code == GTU
5628 || code == LTU)
5629 sign = 0;
5630 else
5631 sign = 1;
5632
5633 /* Get a reduced range if the sign bit is zero. */
5634 if (nonzero <= (GET_MODE_MASK (int_mode) >> 1))
5635 {
5636 mmin = 0;
5637 mmax = nonzero;
5638 }
5639 else
5640 {
5641 rtx mmin_rtx, mmax_rtx;
5642 get_mode_bounds (int_mode, sign, int_mode, &mmin_rtx, &mmax_rtx);
5643
5644 mmin = INTVAL (mmin_rtx);
5645 mmax = INTVAL (mmax_rtx);
5646 if (sign)
5647 {
5648 unsigned int sign_copies
5649 = num_sign_bit_copies (trueop0, int_mode);
5650
5651 mmin >>= (sign_copies - 1);
5652 mmax >>= (sign_copies - 1);
5653 }
5654 }
5655
5656 switch (code)
5657 {
5658 /* x >= y is always true for y <= mmin, always false for y > mmax. */
5659 case GEU:
5660 if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
5661 return const_true_rtx;
5662 if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
5663 return const0_rtx;
5664 break;
5665 case GE:
5666 if (val <= mmin)
5667 return const_true_rtx;
5668 if (val > mmax)
5669 return const0_rtx;
5670 break;
5671
5672 /* x <= y is always true for y >= mmax, always false for y < mmin. */
5673 case LEU:
5674 if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
5675 return const_true_rtx;
5676 if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
5677 return const0_rtx;
5678 break;
5679 case LE:
5680 if (val >= mmax)
5681 return const_true_rtx;
5682 if (val < mmin)
5683 return const0_rtx;
5684 break;
5685
5686 case EQ:
5687 /* x == y is always false for y out of range. */
5688 if (val < mmin || val > mmax)
5689 return const0_rtx;
5690 break;
5691
5692 /* x > y is always false for y >= mmax, always true for y < mmin. */
5693 case GTU:
5694 if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
5695 return const0_rtx;
5696 if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
5697 return const_true_rtx;
5698 break;
5699 case GT:
5700 if (val >= mmax)
5701 return const0_rtx;
5702 if (val < mmin)
5703 return const_true_rtx;
5704 break;
5705
5706 /* x < y is always false for y <= mmin, always true for y > mmax. */
5707 case LTU:
5708 if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
5709 return const0_rtx;
5710 if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
5711 return const_true_rtx;
5712 break;
5713 case LT:
5714 if (val <= mmin)
5715 return const0_rtx;
5716 if (val > mmax)
5717 return const_true_rtx;
5718 break;
5719
5720 case NE:
5721 /* x != y is always true for y out of range. */
5722 if (val < mmin || val > mmax)
5723 return const_true_rtx;
5724 break;
5725
5726 default:
5727 break;
5728 }
5729 }
5730
5731 /* Optimize integer comparisons with zero. */
5732 if (is_a <scalar_int_mode> (mode, &int_mode)
5733 && trueop1 == const0_rtx
5734 && !side_effects_p (trueop0))
5735 {
5736 /* Some addresses are known to be nonzero. We don't know
5737 their sign, but equality comparisons are known. */
5738 if (nonzero_address_p (trueop0))
5739 {
5740 if (code == EQ || code == LEU)
5741 return const0_rtx;
5742 if (code == NE || code == GTU)
5743 return const_true_rtx;
5744 }
5745
5746 /* See if the first operand is an IOR with a constant. If so, we
5747 may be able to determine the result of this comparison. */
5748 if (GET_CODE (op0) == IOR)
5749 {
5750 rtx inner_const = avoid_constant_pool_reference (XEXP (op0, 1));
5751 if (CONST_INT_P (inner_const) && inner_const != const0_rtx)
5752 {
5753 int sign_bitnum = GET_MODE_PRECISION (int_mode) - 1;
5754 int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum
5755 && (UINTVAL (inner_const)
5756 & (HOST_WIDE_INT_1U
5757 << sign_bitnum)));
5758
5759 switch (code)
5760 {
5761 case EQ:
5762 case LEU:
5763 return const0_rtx;
5764 case NE:
5765 case GTU:
5766 return const_true_rtx;
5767 case LT:
5768 case LE:
5769 if (has_sign)
5770 return const_true_rtx;
5771 break;
5772 case GT:
5773 case GE:
5774 if (has_sign)
5775 return const0_rtx;
5776 break;
5777 default:
5778 break;
5779 }
5780 }
5781 }
5782 }
5783
5784 /* Optimize comparison of ABS with zero. */
5785 if (trueop1 == CONST0_RTX (mode) && !side_effects_p (trueop0)
5786 && (GET_CODE (trueop0) == ABS
5787 || (GET_CODE (trueop0) == FLOAT_EXTEND
5788 && GET_CODE (XEXP (trueop0, 0)) == ABS)))
5789 {
5790 switch (code)
5791 {
5792 case LT:
5793 /* Optimize abs(x) < 0.0. */
5794 if (!INTEGRAL_MODE_P (mode) && !HONOR_SNANS (mode))
5795 return const0_rtx;
5796 break;
5797
5798 case GE:
5799 /* Optimize abs(x) >= 0.0. */
5800 if (!INTEGRAL_MODE_P (mode) && !HONOR_NANS (mode))
5801 return const_true_rtx;
5802 break;
5803
5804 case UNGE:
5805 /* Optimize ! (abs(x) < 0.0). */
5806 return const_true_rtx;
5807
5808 default:
5809 break;
5810 }
5811 }
5812
5813 return 0;
5814 }
5815
5816 /* Recognize expressions of the form (X CMP 0) ? VAL : OP (X)
5817 where OP is CLZ or CTZ and VAL is the value from CLZ_DEFINED_VALUE_AT_ZERO
5818 or CTZ_DEFINED_VALUE_AT_ZERO respectively and return OP (X) if the expression
5819 can be simplified to that or NULL_RTX if not.
5820 Assume X is compared against zero with CMP_CODE and the true
5821 arm is TRUE_VAL and the false arm is FALSE_VAL. */
5822
5823 static rtx
5824 simplify_cond_clz_ctz (rtx x, rtx_code cmp_code, rtx true_val, rtx false_val)
5825 {
5826 if (cmp_code != EQ && cmp_code != NE)
5827 return NULL_RTX;
5828
5829 /* Result on X == 0 and X !=0 respectively. */
5830 rtx on_zero, on_nonzero;
5831 if (cmp_code == EQ)
5832 {
5833 on_zero = true_val;
5834 on_nonzero = false_val;
5835 }
5836 else
5837 {
5838 on_zero = false_val;
5839 on_nonzero = true_val;
5840 }
5841
5842 rtx_code op_code = GET_CODE (on_nonzero);
5843 if ((op_code != CLZ && op_code != CTZ)
5844 || !rtx_equal_p (XEXP (on_nonzero, 0), x)
5845 || !CONST_INT_P (on_zero))
5846 return NULL_RTX;
5847
5848 HOST_WIDE_INT op_val;
5849 scalar_int_mode mode ATTRIBUTE_UNUSED
5850 = as_a <scalar_int_mode> (GET_MODE (XEXP (on_nonzero, 0)));
5851 if (((op_code == CLZ && CLZ_DEFINED_VALUE_AT_ZERO (mode, op_val))
5852 || (op_code == CTZ && CTZ_DEFINED_VALUE_AT_ZERO (mode, op_val)))
5853 && op_val == INTVAL (on_zero))
5854 return on_nonzero;
5855
5856 return NULL_RTX;
5857 }
5858
5859 /* Try to simplify X given that it appears within operand OP of a
5860 VEC_MERGE operation whose mask is MASK. X need not use the same
5861 vector mode as the VEC_MERGE, but it must have the same number of
5862 elements.
5863
5864 Return the simplified X on success, otherwise return NULL_RTX. */
5865
5866 rtx
5867 simplify_merge_mask (rtx x, rtx mask, int op)
5868 {
5869 gcc_assert (VECTOR_MODE_P (GET_MODE (x)));
5870 poly_uint64 nunits = GET_MODE_NUNITS (GET_MODE (x));
5871 if (GET_CODE (x) == VEC_MERGE && rtx_equal_p (XEXP (x, 2), mask))
5872 {
5873 if (side_effects_p (XEXP (x, 1 - op)))
5874 return NULL_RTX;
5875
5876 return XEXP (x, op);
5877 }
5878 if (UNARY_P (x)
5879 && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
5880 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits))
5881 {
5882 rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
5883 if (top0)
5884 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), top0,
5885 GET_MODE (XEXP (x, 0)));
5886 }
5887 if (BINARY_P (x)
5888 && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
5889 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
5890 && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
5891 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits))
5892 {
5893 rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
5894 rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
5895 if (top0 || top1)
5896 {
5897 if (COMPARISON_P (x))
5898 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
5899 GET_MODE (XEXP (x, 0)) != VOIDmode
5900 ? GET_MODE (XEXP (x, 0))
5901 : GET_MODE (XEXP (x, 1)),
5902 top0 ? top0 : XEXP (x, 0),
5903 top1 ? top1 : XEXP (x, 1));
5904 else
5905 return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
5906 top0 ? top0 : XEXP (x, 0),
5907 top1 ? top1 : XEXP (x, 1));
5908 }
5909 }
5910 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_TERNARY
5911 && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
5912 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
5913 && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
5914 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits)
5915 && VECTOR_MODE_P (GET_MODE (XEXP (x, 2)))
5916 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 2))), nunits))
5917 {
5918 rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
5919 rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
5920 rtx top2 = simplify_merge_mask (XEXP (x, 2), mask, op);
5921 if (top0 || top1 || top2)
5922 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
5923 GET_MODE (XEXP (x, 0)),
5924 top0 ? top0 : XEXP (x, 0),
5925 top1 ? top1 : XEXP (x, 1),
5926 top2 ? top2 : XEXP (x, 2));
5927 }
5928 return NULL_RTX;
5929 }
5930
5931 \f
5932 /* Simplify CODE, an operation with result mode MODE and three operands,
5933 OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
5934 a constant. Return 0 if no simplifications is possible. */
5935
5936 rtx
5937 simplify_ternary_operation (enum rtx_code code, machine_mode mode,
5938 machine_mode op0_mode, rtx op0, rtx op1,
5939 rtx op2)
5940 {
5941 bool any_change = false;
5942 rtx tem, trueop2;
5943 scalar_int_mode int_mode, int_op0_mode;
5944 unsigned int n_elts;
5945
5946 switch (code)
5947 {
5948 case FMA:
5949 /* Simplify negations around the multiplication. */
5950 /* -a * -b + c => a * b + c. */
5951 if (GET_CODE (op0) == NEG)
5952 {
5953 tem = simplify_unary_operation (NEG, mode, op1, mode);
5954 if (tem)
5955 op1 = tem, op0 = XEXP (op0, 0), any_change = true;
5956 }
5957 else if (GET_CODE (op1) == NEG)
5958 {
5959 tem = simplify_unary_operation (NEG, mode, op0, mode);
5960 if (tem)
5961 op0 = tem, op1 = XEXP (op1, 0), any_change = true;
5962 }
5963
5964 /* Canonicalize the two multiplication operands. */
5965 /* a * -b + c => -b * a + c. */
5966 if (swap_commutative_operands_p (op0, op1))
5967 std::swap (op0, op1), any_change = true;
5968
5969 if (any_change)
5970 return gen_rtx_FMA (mode, op0, op1, op2);
5971 return NULL_RTX;
5972
5973 case SIGN_EXTRACT:
5974 case ZERO_EXTRACT:
5975 if (CONST_INT_P (op0)
5976 && CONST_INT_P (op1)
5977 && CONST_INT_P (op2)
5978 && is_a <scalar_int_mode> (mode, &int_mode)
5979 && INTVAL (op1) + INTVAL (op2) <= GET_MODE_PRECISION (int_mode)
5980 && HWI_COMPUTABLE_MODE_P (int_mode))
5981 {
5982 /* Extracting a bit-field from a constant */
5983 unsigned HOST_WIDE_INT val = UINTVAL (op0);
5984 HOST_WIDE_INT op1val = INTVAL (op1);
5985 HOST_WIDE_INT op2val = INTVAL (op2);
5986 if (!BITS_BIG_ENDIAN)
5987 val >>= op2val;
5988 else if (is_a <scalar_int_mode> (op0_mode, &int_op0_mode))
5989 val >>= GET_MODE_PRECISION (int_op0_mode) - op2val - op1val;
5990 else
5991 /* Not enough information to calculate the bit position. */
5992 break;
5993
5994 if (HOST_BITS_PER_WIDE_INT != op1val)
5995 {
5996 /* First zero-extend. */
5997 val &= (HOST_WIDE_INT_1U << op1val) - 1;
5998 /* If desired, propagate sign bit. */
5999 if (code == SIGN_EXTRACT
6000 && (val & (HOST_WIDE_INT_1U << (op1val - 1)))
6001 != 0)
6002 val |= ~ ((HOST_WIDE_INT_1U << op1val) - 1);
6003 }
6004
6005 return gen_int_mode (val, int_mode);
6006 }
6007 break;
6008
6009 case IF_THEN_ELSE:
6010 if (CONST_INT_P (op0))
6011 return op0 != const0_rtx ? op1 : op2;
6012
6013 /* Convert c ? a : a into "a". */
6014 if (rtx_equal_p (op1, op2) && ! side_effects_p (op0))
6015 return op1;
6016
6017 /* Convert a != b ? a : b into "a". */
6018 if (GET_CODE (op0) == NE
6019 && ! side_effects_p (op0)
6020 && ! HONOR_NANS (mode)
6021 && ! HONOR_SIGNED_ZEROS (mode)
6022 && ((rtx_equal_p (XEXP (op0, 0), op1)
6023 && rtx_equal_p (XEXP (op0, 1), op2))
6024 || (rtx_equal_p (XEXP (op0, 0), op2)
6025 && rtx_equal_p (XEXP (op0, 1), op1))))
6026 return op1;
6027
6028 /* Convert a == b ? a : b into "b". */
6029 if (GET_CODE (op0) == EQ
6030 && ! side_effects_p (op0)
6031 && ! HONOR_NANS (mode)
6032 && ! HONOR_SIGNED_ZEROS (mode)
6033 && ((rtx_equal_p (XEXP (op0, 0), op1)
6034 && rtx_equal_p (XEXP (op0, 1), op2))
6035 || (rtx_equal_p (XEXP (op0, 0), op2)
6036 && rtx_equal_p (XEXP (op0, 1), op1))))
6037 return op2;
6038
6039 /* Convert (!c) != {0,...,0} ? a : b into
6040 c != {0,...,0} ? b : a for vector modes. */
6041 if (VECTOR_MODE_P (GET_MODE (op1))
6042 && GET_CODE (op0) == NE
6043 && GET_CODE (XEXP (op0, 0)) == NOT
6044 && GET_CODE (XEXP (op0, 1)) == CONST_VECTOR)
6045 {
6046 rtx cv = XEXP (op0, 1);
6047 int nunits;
6048 bool ok = true;
6049 if (!CONST_VECTOR_NUNITS (cv).is_constant (&nunits))
6050 ok = false;
6051 else
6052 for (int i = 0; i < nunits; ++i)
6053 if (CONST_VECTOR_ELT (cv, i) != const0_rtx)
6054 {
6055 ok = false;
6056 break;
6057 }
6058 if (ok)
6059 {
6060 rtx new_op0 = gen_rtx_NE (GET_MODE (op0),
6061 XEXP (XEXP (op0, 0), 0),
6062 XEXP (op0, 1));
6063 rtx retval = gen_rtx_IF_THEN_ELSE (mode, new_op0, op2, op1);
6064 return retval;
6065 }
6066 }
6067
6068 /* Convert x == 0 ? N : clz (x) into clz (x) when
6069 CLZ_DEFINED_VALUE_AT_ZERO is defined to N for the mode of x.
6070 Similarly for ctz (x). */
6071 if (COMPARISON_P (op0) && !side_effects_p (op0)
6072 && XEXP (op0, 1) == const0_rtx)
6073 {
6074 rtx simplified
6075 = simplify_cond_clz_ctz (XEXP (op0, 0), GET_CODE (op0),
6076 op1, op2);
6077 if (simplified)
6078 return simplified;
6079 }
6080
6081 if (COMPARISON_P (op0) && ! side_effects_p (op0))
6082 {
6083 machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
6084 ? GET_MODE (XEXP (op0, 1))
6085 : GET_MODE (XEXP (op0, 0)));
6086 rtx temp;
6087
6088 /* Look for happy constants in op1 and op2. */
6089 if (CONST_INT_P (op1) && CONST_INT_P (op2))
6090 {
6091 HOST_WIDE_INT t = INTVAL (op1);
6092 HOST_WIDE_INT f = INTVAL (op2);
6093
6094 if (t == STORE_FLAG_VALUE && f == 0)
6095 code = GET_CODE (op0);
6096 else if (t == 0 && f == STORE_FLAG_VALUE)
6097 {
6098 enum rtx_code tmp;
6099 tmp = reversed_comparison_code (op0, NULL);
6100 if (tmp == UNKNOWN)
6101 break;
6102 code = tmp;
6103 }
6104 else
6105 break;
6106
6107 return simplify_gen_relational (code, mode, cmp_mode,
6108 XEXP (op0, 0), XEXP (op0, 1));
6109 }
6110
6111 temp = simplify_relational_operation (GET_CODE (op0), op0_mode,
6112 cmp_mode, XEXP (op0, 0),
6113 XEXP (op0, 1));
6114
6115 /* See if any simplifications were possible. */
6116 if (temp)
6117 {
6118 if (CONST_INT_P (temp))
6119 return temp == const0_rtx ? op2 : op1;
6120 else if (temp)
6121 return gen_rtx_IF_THEN_ELSE (mode, temp, op1, op2);
6122 }
6123 }
6124 break;
6125
6126 case VEC_MERGE:
6127 gcc_assert (GET_MODE (op0) == mode);
6128 gcc_assert (GET_MODE (op1) == mode);
6129 gcc_assert (VECTOR_MODE_P (mode));
6130 trueop2 = avoid_constant_pool_reference (op2);
6131 if (CONST_INT_P (trueop2)
6132 && GET_MODE_NUNITS (mode).is_constant (&n_elts))
6133 {
6134 unsigned HOST_WIDE_INT sel = UINTVAL (trueop2);
6135 unsigned HOST_WIDE_INT mask;
6136 if (n_elts == HOST_BITS_PER_WIDE_INT)
6137 mask = -1;
6138 else
6139 mask = (HOST_WIDE_INT_1U << n_elts) - 1;
6140
6141 if (!(sel & mask) && !side_effects_p (op0))
6142 return op1;
6143 if ((sel & mask) == mask && !side_effects_p (op1))
6144 return op0;
6145
6146 rtx trueop0 = avoid_constant_pool_reference (op0);
6147 rtx trueop1 = avoid_constant_pool_reference (op1);
6148 if (GET_CODE (trueop0) == CONST_VECTOR
6149 && GET_CODE (trueop1) == CONST_VECTOR)
6150 {
6151 rtvec v = rtvec_alloc (n_elts);
6152 unsigned int i;
6153
6154 for (i = 0; i < n_elts; i++)
6155 RTVEC_ELT (v, i) = ((sel & (HOST_WIDE_INT_1U << i))
6156 ? CONST_VECTOR_ELT (trueop0, i)
6157 : CONST_VECTOR_ELT (trueop1, i));
6158 return gen_rtx_CONST_VECTOR (mode, v);
6159 }
6160
6161 /* Replace (vec_merge (vec_merge a b m) c n) with (vec_merge b c n)
6162 if no element from a appears in the result. */
6163 if (GET_CODE (op0) == VEC_MERGE)
6164 {
6165 tem = avoid_constant_pool_reference (XEXP (op0, 2));
6166 if (CONST_INT_P (tem))
6167 {
6168 unsigned HOST_WIDE_INT sel0 = UINTVAL (tem);
6169 if (!(sel & sel0 & mask) && !side_effects_p (XEXP (op0, 0)))
6170 return simplify_gen_ternary (code, mode, mode,
6171 XEXP (op0, 1), op1, op2);
6172 if (!(sel & ~sel0 & mask) && !side_effects_p (XEXP (op0, 1)))
6173 return simplify_gen_ternary (code, mode, mode,
6174 XEXP (op0, 0), op1, op2);
6175 }
6176 }
6177 if (GET_CODE (op1) == VEC_MERGE)
6178 {
6179 tem = avoid_constant_pool_reference (XEXP (op1, 2));
6180 if (CONST_INT_P (tem))
6181 {
6182 unsigned HOST_WIDE_INT sel1 = UINTVAL (tem);
6183 if (!(~sel & sel1 & mask) && !side_effects_p (XEXP (op1, 0)))
6184 return simplify_gen_ternary (code, mode, mode,
6185 op0, XEXP (op1, 1), op2);
6186 if (!(~sel & ~sel1 & mask) && !side_effects_p (XEXP (op1, 1)))
6187 return simplify_gen_ternary (code, mode, mode,
6188 op0, XEXP (op1, 0), op2);
6189 }
6190 }
6191
6192 /* Replace (vec_merge (vec_duplicate (vec_select a parallel (i))) a 1 << i)
6193 with a. */
6194 if (GET_CODE (op0) == VEC_DUPLICATE
6195 && GET_CODE (XEXP (op0, 0)) == VEC_SELECT
6196 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == PARALLEL
6197 && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (op0, 0))), 1))
6198 {
6199 tem = XVECEXP ((XEXP (XEXP (op0, 0), 1)), 0, 0);
6200 if (CONST_INT_P (tem) && CONST_INT_P (op2))
6201 {
6202 if (XEXP (XEXP (op0, 0), 0) == op1
6203 && UINTVAL (op2) == HOST_WIDE_INT_1U << UINTVAL (tem))
6204 return op1;
6205 }
6206 }
6207 /* Replace (vec_merge (vec_duplicate (X)) (const_vector [A, B])
6208 (const_int N))
6209 with (vec_concat (X) (B)) if N == 1 or
6210 (vec_concat (A) (X)) if N == 2. */
6211 if (GET_CODE (op0) == VEC_DUPLICATE
6212 && GET_CODE (op1) == CONST_VECTOR
6213 && known_eq (CONST_VECTOR_NUNITS (op1), 2)
6214 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6215 && IN_RANGE (sel, 1, 2))
6216 {
6217 rtx newop0 = XEXP (op0, 0);
6218 rtx newop1 = CONST_VECTOR_ELT (op1, 2 - sel);
6219 if (sel == 2)
6220 std::swap (newop0, newop1);
6221 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
6222 }
6223 /* Replace (vec_merge (vec_duplicate x) (vec_concat (y) (z)) (const_int N))
6224 with (vec_concat x z) if N == 1, or (vec_concat y x) if N == 2.
6225 Only applies for vectors of two elements. */
6226 if (GET_CODE (op0) == VEC_DUPLICATE
6227 && GET_CODE (op1) == VEC_CONCAT
6228 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6229 && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
6230 && IN_RANGE (sel, 1, 2))
6231 {
6232 rtx newop0 = XEXP (op0, 0);
6233 rtx newop1 = XEXP (op1, 2 - sel);
6234 rtx otherop = XEXP (op1, sel - 1);
6235 if (sel == 2)
6236 std::swap (newop0, newop1);
6237 /* Don't want to throw away the other part of the vec_concat if
6238 it has side-effects. */
6239 if (!side_effects_p (otherop))
6240 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
6241 }
6242
6243 /* Replace:
6244
6245 (vec_merge:outer (vec_duplicate:outer x:inner)
6246 (subreg:outer y:inner 0)
6247 (const_int N))
6248
6249 with (vec_concat:outer x:inner y:inner) if N == 1,
6250 or (vec_concat:outer y:inner x:inner) if N == 2.
6251
6252 Implicitly, this means we have a paradoxical subreg, but such
6253 a check is cheap, so make it anyway.
6254
6255 Only applies for vectors of two elements. */
6256 if (GET_CODE (op0) == VEC_DUPLICATE
6257 && GET_CODE (op1) == SUBREG
6258 && GET_MODE (op1) == GET_MODE (op0)
6259 && GET_MODE (SUBREG_REG (op1)) == GET_MODE (XEXP (op0, 0))
6260 && paradoxical_subreg_p (op1)
6261 && subreg_lowpart_p (op1)
6262 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6263 && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
6264 && IN_RANGE (sel, 1, 2))
6265 {
6266 rtx newop0 = XEXP (op0, 0);
6267 rtx newop1 = SUBREG_REG (op1);
6268 if (sel == 2)
6269 std::swap (newop0, newop1);
6270 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
6271 }
6272
6273 /* Same as above but with switched operands:
6274 Replace (vec_merge:outer (subreg:outer x:inner 0)
6275 (vec_duplicate:outer y:inner)
6276 (const_int N))
6277
6278 with (vec_concat:outer x:inner y:inner) if N == 1,
6279 or (vec_concat:outer y:inner x:inner) if N == 2. */
6280 if (GET_CODE (op1) == VEC_DUPLICATE
6281 && GET_CODE (op0) == SUBREG
6282 && GET_MODE (op0) == GET_MODE (op1)
6283 && GET_MODE (SUBREG_REG (op0)) == GET_MODE (XEXP (op1, 0))
6284 && paradoxical_subreg_p (op0)
6285 && subreg_lowpart_p (op0)
6286 && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
6287 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6288 && IN_RANGE (sel, 1, 2))
6289 {
6290 rtx newop0 = SUBREG_REG (op0);
6291 rtx newop1 = XEXP (op1, 0);
6292 if (sel == 2)
6293 std::swap (newop0, newop1);
6294 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
6295 }
6296
6297 /* Replace (vec_merge (vec_duplicate x) (vec_duplicate y)
6298 (const_int n))
6299 with (vec_concat x y) or (vec_concat y x) depending on value
6300 of N. */
6301 if (GET_CODE (op0) == VEC_DUPLICATE
6302 && GET_CODE (op1) == VEC_DUPLICATE
6303 && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
6304 && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
6305 && IN_RANGE (sel, 1, 2))
6306 {
6307 rtx newop0 = XEXP (op0, 0);
6308 rtx newop1 = XEXP (op1, 0);
6309 if (sel == 2)
6310 std::swap (newop0, newop1);
6311
6312 return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
6313 }
6314 }
6315
6316 if (rtx_equal_p (op0, op1)
6317 && !side_effects_p (op2) && !side_effects_p (op1))
6318 return op0;
6319
6320 if (!side_effects_p (op2))
6321 {
6322 rtx top0
6323 = may_trap_p (op0) ? NULL_RTX : simplify_merge_mask (op0, op2, 0);
6324 rtx top1
6325 = may_trap_p (op1) ? NULL_RTX : simplify_merge_mask (op1, op2, 1);
6326 if (top0 || top1)
6327 return simplify_gen_ternary (code, mode, mode,
6328 top0 ? top0 : op0,
6329 top1 ? top1 : op1, op2);
6330 }
6331
6332 break;
6333
6334 default:
6335 gcc_unreachable ();
6336 }
6337
6338 return 0;
6339 }
6340
6341 /* Try to calculate NUM_BYTES bytes of the target memory image of X,
6342 starting at byte FIRST_BYTE. Return true on success and add the
6343 bytes to BYTES, such that each byte has BITS_PER_UNIT bits and such
6344 that the bytes follow target memory order. Leave BYTES unmodified
6345 on failure.
6346
6347 MODE is the mode of X. The caller must reserve NUM_BYTES bytes in
6348 BYTES before calling this function. */
6349
6350 bool
6351 native_encode_rtx (machine_mode mode, rtx x, vec<target_unit> &bytes,
6352 unsigned int first_byte, unsigned int num_bytes)
6353 {
6354 /* Check the mode is sensible. */
6355 gcc_assert (GET_MODE (x) == VOIDmode
6356 ? is_a <scalar_int_mode> (mode)
6357 : mode == GET_MODE (x));
6358
6359 if (GET_CODE (x) == CONST_VECTOR)
6360 {
6361 /* CONST_VECTOR_ELT follows target memory order, so no shuffling
6362 is necessary. The only complication is that MODE_VECTOR_BOOL
6363 vectors can have several elements per byte. */
6364 unsigned int elt_bits = vector_element_size (GET_MODE_BITSIZE (mode),
6365 GET_MODE_NUNITS (mode));
6366 unsigned int elt = first_byte * BITS_PER_UNIT / elt_bits;
6367 if (elt_bits < BITS_PER_UNIT)
6368 {
6369 /* This is the only case in which elements can be smaller than
6370 a byte. */
6371 gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
6372 for (unsigned int i = 0; i < num_bytes; ++i)
6373 {
6374 target_unit value = 0;
6375 for (unsigned int j = 0; j < BITS_PER_UNIT; j += elt_bits)
6376 {
6377 value |= (INTVAL (CONST_VECTOR_ELT (x, elt)) & 1) << j;
6378 elt += 1;
6379 }
6380 bytes.quick_push (value);
6381 }
6382 return true;
6383 }
6384
6385 unsigned int start = bytes.length ();
6386 unsigned int elt_bytes = GET_MODE_UNIT_SIZE (mode);
6387 /* Make FIRST_BYTE relative to ELT. */
6388 first_byte %= elt_bytes;
6389 while (num_bytes > 0)
6390 {
6391 /* Work out how many bytes we want from element ELT. */
6392 unsigned int chunk_bytes = MIN (num_bytes, elt_bytes - first_byte);
6393 if (!native_encode_rtx (GET_MODE_INNER (mode),
6394 CONST_VECTOR_ELT (x, elt), bytes,
6395 first_byte, chunk_bytes))
6396 {
6397 bytes.truncate (start);
6398 return false;
6399 }
6400 elt += 1;
6401 first_byte = 0;
6402 num_bytes -= chunk_bytes;
6403 }
6404 return true;
6405 }
6406
6407 /* All subsequent cases are limited to scalars. */
6408 scalar_mode smode;
6409 if (!is_a <scalar_mode> (mode, &smode))
6410 return false;
6411
6412 /* Make sure that the region is in range. */
6413 unsigned int end_byte = first_byte + num_bytes;
6414 unsigned int mode_bytes = GET_MODE_SIZE (smode);
6415 gcc_assert (end_byte <= mode_bytes);
6416
6417 if (CONST_SCALAR_INT_P (x))
6418 {
6419 /* The target memory layout is affected by both BYTES_BIG_ENDIAN
6420 and WORDS_BIG_ENDIAN. Use the subreg machinery to get the lsb
6421 position of each byte. */
6422 rtx_mode_t value (x, smode);
6423 wide_int_ref value_wi (value);
6424 for (unsigned int byte = first_byte; byte < end_byte; ++byte)
6425 {
6426 /* Always constant because the inputs are. */
6427 unsigned int lsb
6428 = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
6429 /* Operate directly on the encoding rather than using
6430 wi::extract_uhwi, so that we preserve the sign or zero
6431 extension for modes that are not a whole number of bits in
6432 size. (Zero extension is only used for the combination of
6433 innermode == BImode && STORE_FLAG_VALUE == 1). */
6434 unsigned int elt = lsb / HOST_BITS_PER_WIDE_INT;
6435 unsigned int shift = lsb % HOST_BITS_PER_WIDE_INT;
6436 unsigned HOST_WIDE_INT uhwi = value_wi.elt (elt);
6437 bytes.quick_push (uhwi >> shift);
6438 }
6439 return true;
6440 }
6441
6442 if (CONST_DOUBLE_P (x))
6443 {
6444 /* real_to_target produces an array of integers in target memory order.
6445 All integers before the last one have 32 bits; the last one may
6446 have 32 bits or fewer, depending on whether the mode bitsize
6447 is divisible by 32. Each of these integers is then laid out
6448 in target memory as any other integer would be. */
6449 long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
6450 real_to_target (el32, CONST_DOUBLE_REAL_VALUE (x), smode);
6451
6452 /* The (maximum) number of target bytes per element of el32. */
6453 unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
6454 gcc_assert (bytes_per_el32 != 0);
6455
6456 /* Build up the integers in a similar way to the CONST_SCALAR_INT_P
6457 handling above. */
6458 for (unsigned int byte = first_byte; byte < end_byte; ++byte)
6459 {
6460 unsigned int index = byte / bytes_per_el32;
6461 unsigned int subbyte = byte % bytes_per_el32;
6462 unsigned int int_bytes = MIN (bytes_per_el32,
6463 mode_bytes - index * bytes_per_el32);
6464 /* Always constant because the inputs are. */
6465 unsigned int lsb
6466 = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
6467 bytes.quick_push ((unsigned long) el32[index] >> lsb);
6468 }
6469 return true;
6470 }
6471
6472 if (GET_CODE (x) == CONST_FIXED)
6473 {
6474 for (unsigned int byte = first_byte; byte < end_byte; ++byte)
6475 {
6476 /* Always constant because the inputs are. */
6477 unsigned int lsb
6478 = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
6479 unsigned HOST_WIDE_INT piece = CONST_FIXED_VALUE_LOW (x);
6480 if (lsb >= HOST_BITS_PER_WIDE_INT)
6481 {
6482 lsb -= HOST_BITS_PER_WIDE_INT;
6483 piece = CONST_FIXED_VALUE_HIGH (x);
6484 }
6485 bytes.quick_push (piece >> lsb);
6486 }
6487 return true;
6488 }
6489
6490 return false;
6491 }
6492
6493 /* Read a vector of mode MODE from the target memory image given by BYTES,
6494 starting at byte FIRST_BYTE. The vector is known to be encodable using
6495 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each,
6496 and BYTES is known to have enough bytes to supply NPATTERNS *
6497 NELTS_PER_PATTERN vector elements. Each element of BYTES contains
6498 BITS_PER_UNIT bits and the bytes are in target memory order.
6499
6500 Return the vector on success, otherwise return NULL_RTX. */
6501
6502 rtx
6503 native_decode_vector_rtx (machine_mode mode, vec<target_unit> bytes,
6504 unsigned int first_byte, unsigned int npatterns,
6505 unsigned int nelts_per_pattern)
6506 {
6507 rtx_vector_builder builder (mode, npatterns, nelts_per_pattern);
6508
6509 unsigned int elt_bits = vector_element_size (GET_MODE_BITSIZE (mode),
6510 GET_MODE_NUNITS (mode));
6511 if (elt_bits < BITS_PER_UNIT)
6512 {
6513 /* This is the only case in which elements can be smaller than a byte.
6514 Element 0 is always in the lsb of the containing byte. */
6515 gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
6516 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
6517 {
6518 unsigned int bit_index = first_byte * BITS_PER_UNIT + i * elt_bits;
6519 unsigned int byte_index = bit_index / BITS_PER_UNIT;
6520 unsigned int lsb = bit_index % BITS_PER_UNIT;
6521 builder.quick_push (bytes[byte_index] & (1 << lsb)
6522 ? CONST1_RTX (BImode)
6523 : CONST0_RTX (BImode));
6524 }
6525 }
6526 else
6527 {
6528 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
6529 {
6530 rtx x = native_decode_rtx (GET_MODE_INNER (mode), bytes, first_byte);
6531 if (!x)
6532 return NULL_RTX;
6533 builder.quick_push (x);
6534 first_byte += elt_bits / BITS_PER_UNIT;
6535 }
6536 }
6537 return builder.build ();
6538 }
6539
6540 /* Read an rtx of mode MODE from the target memory image given by BYTES,
6541 starting at byte FIRST_BYTE. Each element of BYTES contains BITS_PER_UNIT
6542 bits and the bytes are in target memory order. The image has enough
6543 values to specify all bytes of MODE.
6544
6545 Return the rtx on success, otherwise return NULL_RTX. */
6546
6547 rtx
6548 native_decode_rtx (machine_mode mode, vec<target_unit> bytes,
6549 unsigned int first_byte)
6550 {
6551 if (VECTOR_MODE_P (mode))
6552 {
6553 /* If we know at compile time how many elements there are,
6554 pull each element directly from BYTES. */
6555 unsigned int nelts;
6556 if (GET_MODE_NUNITS (mode).is_constant (&nelts))
6557 return native_decode_vector_rtx (mode, bytes, first_byte, nelts, 1);
6558 return NULL_RTX;
6559 }
6560
6561 scalar_int_mode imode;
6562 if (is_a <scalar_int_mode> (mode, &imode)
6563 && GET_MODE_PRECISION (imode) <= MAX_BITSIZE_MODE_ANY_INT)
6564 {
6565 /* Pull the bytes msb first, so that we can use simple
6566 shift-and-insert wide_int operations. */
6567 unsigned int size = GET_MODE_SIZE (imode);
6568 wide_int result (wi::zero (GET_MODE_PRECISION (imode)));
6569 for (unsigned int i = 0; i < size; ++i)
6570 {
6571 unsigned int lsb = (size - i - 1) * BITS_PER_UNIT;
6572 /* Always constant because the inputs are. */
6573 unsigned int subbyte
6574 = subreg_size_offset_from_lsb (1, size, lsb).to_constant ();
6575 result <<= BITS_PER_UNIT;
6576 result |= bytes[first_byte + subbyte];
6577 }
6578 return immed_wide_int_const (result, imode);
6579 }
6580
6581 scalar_float_mode fmode;
6582 if (is_a <scalar_float_mode> (mode, &fmode))
6583 {
6584 /* We need to build an array of integers in target memory order.
6585 All integers before the last one have 32 bits; the last one may
6586 have 32 bits or fewer, depending on whether the mode bitsize
6587 is divisible by 32. */
6588 long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
6589 unsigned int num_el32 = CEIL (GET_MODE_BITSIZE (fmode), 32);
6590 memset (el32, 0, num_el32 * sizeof (long));
6591
6592 /* The (maximum) number of target bytes per element of el32. */
6593 unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
6594 gcc_assert (bytes_per_el32 != 0);
6595
6596 unsigned int mode_bytes = GET_MODE_SIZE (fmode);
6597 for (unsigned int byte = 0; byte < mode_bytes; ++byte)
6598 {
6599 unsigned int index = byte / bytes_per_el32;
6600 unsigned int subbyte = byte % bytes_per_el32;
6601 unsigned int int_bytes = MIN (bytes_per_el32,
6602 mode_bytes - index * bytes_per_el32);
6603 /* Always constant because the inputs are. */
6604 unsigned int lsb
6605 = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
6606 el32[index] |= (unsigned long) bytes[first_byte + byte] << lsb;
6607 }
6608 REAL_VALUE_TYPE r;
6609 real_from_target (&r, el32, fmode);
6610 return const_double_from_real_value (r, fmode);
6611 }
6612
6613 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
6614 {
6615 scalar_mode smode = as_a <scalar_mode> (mode);
6616 FIXED_VALUE_TYPE f;
6617 f.data.low = 0;
6618 f.data.high = 0;
6619 f.mode = smode;
6620
6621 unsigned int mode_bytes = GET_MODE_SIZE (smode);
6622 for (unsigned int byte = 0; byte < mode_bytes; ++byte)
6623 {
6624 /* Always constant because the inputs are. */
6625 unsigned int lsb
6626 = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
6627 unsigned HOST_WIDE_INT unit = bytes[first_byte + byte];
6628 if (lsb >= HOST_BITS_PER_WIDE_INT)
6629 f.data.high |= unit << (lsb - HOST_BITS_PER_WIDE_INT);
6630 else
6631 f.data.low |= unit << lsb;
6632 }
6633 return CONST_FIXED_FROM_FIXED_VALUE (f, mode);
6634 }
6635
6636 return NULL_RTX;
6637 }
6638
6639 /* Simplify a byte offset BYTE into CONST_VECTOR X. The main purpose
6640 is to convert a runtime BYTE value into a constant one. */
6641
6642 static poly_uint64
6643 simplify_const_vector_byte_offset (rtx x, poly_uint64 byte)
6644 {
6645 /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
6646 machine_mode mode = GET_MODE (x);
6647 unsigned int elt_bits = vector_element_size (GET_MODE_BITSIZE (mode),
6648 GET_MODE_NUNITS (mode));
6649 /* The number of bits needed to encode one element from each pattern. */
6650 unsigned int sequence_bits = CONST_VECTOR_NPATTERNS (x) * elt_bits;
6651
6652 /* Identify the start point in terms of a sequence number and a byte offset
6653 within that sequence. */
6654 poly_uint64 first_sequence;
6655 unsigned HOST_WIDE_INT subbit;
6656 if (can_div_trunc_p (byte * BITS_PER_UNIT, sequence_bits,
6657 &first_sequence, &subbit))
6658 {
6659 unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
6660 if (nelts_per_pattern == 1)
6661 /* This is a duplicated vector, so the value of FIRST_SEQUENCE
6662 doesn't matter. */
6663 byte = subbit / BITS_PER_UNIT;
6664 else if (nelts_per_pattern == 2 && known_gt (first_sequence, 0U))
6665 {
6666 /* The subreg drops the first element from each pattern and
6667 only uses the second element. Find the first sequence
6668 that starts on a byte boundary. */
6669 subbit += least_common_multiple (sequence_bits, BITS_PER_UNIT);
6670 byte = subbit / BITS_PER_UNIT;
6671 }
6672 }
6673 return byte;
6674 }
6675
6676 /* Subroutine of simplify_subreg in which:
6677
6678 - X is known to be a CONST_VECTOR
6679 - OUTERMODE is known to be a vector mode
6680
6681 Try to handle the subreg by operating on the CONST_VECTOR encoding
6682 rather than on each individual element of the CONST_VECTOR.
6683
6684 Return the simplified subreg on success, otherwise return NULL_RTX. */
6685
6686 static rtx
6687 simplify_const_vector_subreg (machine_mode outermode, rtx x,
6688 machine_mode innermode, unsigned int first_byte)
6689 {
6690 /* Paradoxical subregs of vectors have dubious semantics. */
6691 if (paradoxical_subreg_p (outermode, innermode))
6692 return NULL_RTX;
6693
6694 /* We can only preserve the semantics of a stepped pattern if the new
6695 vector element is the same as the original one. */
6696 if (CONST_VECTOR_STEPPED_P (x)
6697 && GET_MODE_INNER (outermode) != GET_MODE_INNER (innermode))
6698 return NULL_RTX;
6699
6700 /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
6701 unsigned int x_elt_bits
6702 = vector_element_size (GET_MODE_BITSIZE (innermode),
6703 GET_MODE_NUNITS (innermode));
6704 unsigned int out_elt_bits
6705 = vector_element_size (GET_MODE_BITSIZE (outermode),
6706 GET_MODE_NUNITS (outermode));
6707
6708 /* The number of bits needed to encode one element from every pattern
6709 of the original vector. */
6710 unsigned int x_sequence_bits = CONST_VECTOR_NPATTERNS (x) * x_elt_bits;
6711
6712 /* The number of bits needed to encode one element from every pattern
6713 of the result. */
6714 unsigned int out_sequence_bits
6715 = least_common_multiple (x_sequence_bits, out_elt_bits);
6716
6717 /* Work out the number of interleaved patterns in the output vector
6718 and the number of encoded elements per pattern. */
6719 unsigned int out_npatterns = out_sequence_bits / out_elt_bits;
6720 unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
6721
6722 /* The encoding scheme requires the number of elements to be a multiple
6723 of the number of patterns, so that each pattern appears at least once
6724 and so that the same number of elements appear from each pattern. */
6725 bool ok_p = multiple_p (GET_MODE_NUNITS (outermode), out_npatterns);
6726 unsigned int const_nunits;
6727 if (GET_MODE_NUNITS (outermode).is_constant (&const_nunits)
6728 && (!ok_p || out_npatterns * nelts_per_pattern > const_nunits))
6729 {
6730 /* Either the encoding is invalid, or applying it would give us
6731 more elements than we need. Just encode each element directly. */
6732 out_npatterns = const_nunits;
6733 nelts_per_pattern = 1;
6734 }
6735 else if (!ok_p)
6736 return NULL_RTX;
6737
6738 /* Get enough bytes of X to form the new encoding. */
6739 unsigned int buffer_bits = out_npatterns * nelts_per_pattern * out_elt_bits;
6740 unsigned int buffer_bytes = CEIL (buffer_bits, BITS_PER_UNIT);
6741 auto_vec<target_unit, 128> buffer (buffer_bytes);
6742 if (!native_encode_rtx (innermode, x, buffer, first_byte, buffer_bytes))
6743 return NULL_RTX;
6744
6745 /* Reencode the bytes as OUTERMODE. */
6746 return native_decode_vector_rtx (outermode, buffer, 0, out_npatterns,
6747 nelts_per_pattern);
6748 }
6749
6750 /* Try to simplify a subreg of a constant by encoding the subreg region
6751 as a sequence of target bytes and reading them back in the new mode.
6752 Return the new value on success, otherwise return null.
6753
6754 The subreg has outer mode OUTERMODE, inner mode INNERMODE, inner value X
6755 and byte offset FIRST_BYTE. */
6756
6757 static rtx
6758 simplify_immed_subreg (fixed_size_mode outermode, rtx x,
6759 machine_mode innermode, unsigned int first_byte)
6760 {
6761 unsigned int buffer_bytes = GET_MODE_SIZE (outermode);
6762 auto_vec<target_unit, 128> buffer (buffer_bytes);
6763
6764 /* Some ports misuse CCmode. */
6765 if (GET_MODE_CLASS (outermode) == MODE_CC && CONST_INT_P (x))
6766 return x;
6767
6768 /* Paradoxical subregs read undefined values for bytes outside of the
6769 inner value. However, we have traditionally always sign-extended
6770 integer constants and zero-extended others. */
6771 unsigned int inner_bytes = buffer_bytes;
6772 if (paradoxical_subreg_p (outermode, innermode))
6773 {
6774 if (!GET_MODE_SIZE (innermode).is_constant (&inner_bytes))
6775 return NULL_RTX;
6776
6777 target_unit filler = 0;
6778 if (CONST_SCALAR_INT_P (x) && wi::neg_p (rtx_mode_t (x, innermode)))
6779 filler = -1;
6780
6781 /* Add any leading bytes due to big-endian layout. The number of
6782 bytes must be constant because both modes have constant size. */
6783 unsigned int leading_bytes
6784 = -byte_lowpart_offset (outermode, innermode).to_constant ();
6785 for (unsigned int i = 0; i < leading_bytes; ++i)
6786 buffer.quick_push (filler);
6787
6788 if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
6789 return NULL_RTX;
6790
6791 /* Add any trailing bytes due to little-endian layout. */
6792 while (buffer.length () < buffer_bytes)
6793 buffer.quick_push (filler);
6794 }
6795 else
6796 {
6797 if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
6798 return NULL_RTX;
6799 }
6800 return native_decode_rtx (outermode, buffer, 0);
6801 }
6802
6803 /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
6804 Return 0 if no simplifications are possible. */
6805 rtx
6806 simplify_subreg (machine_mode outermode, rtx op,
6807 machine_mode innermode, poly_uint64 byte)
6808 {
6809 /* Little bit of sanity checking. */
6810 gcc_assert (innermode != VOIDmode);
6811 gcc_assert (outermode != VOIDmode);
6812 gcc_assert (innermode != BLKmode);
6813 gcc_assert (outermode != BLKmode);
6814
6815 gcc_assert (GET_MODE (op) == innermode
6816 || GET_MODE (op) == VOIDmode);
6817
6818 poly_uint64 outersize = GET_MODE_SIZE (outermode);
6819 if (!multiple_p (byte, outersize))
6820 return NULL_RTX;
6821
6822 poly_uint64 innersize = GET_MODE_SIZE (innermode);
6823 if (maybe_ge (byte, innersize))
6824 return NULL_RTX;
6825
6826 if (outermode == innermode && known_eq (byte, 0U))
6827 return op;
6828
6829 if (GET_CODE (op) == CONST_VECTOR)
6830 byte = simplify_const_vector_byte_offset (op, byte);
6831
6832 if (multiple_p (byte, GET_MODE_UNIT_SIZE (innermode)))
6833 {
6834 rtx elt;
6835
6836 if (VECTOR_MODE_P (outermode)
6837 && GET_MODE_INNER (outermode) == GET_MODE_INNER (innermode)
6838 && vec_duplicate_p (op, &elt))
6839 return gen_vec_duplicate (outermode, elt);
6840
6841 if (outermode == GET_MODE_INNER (innermode)
6842 && vec_duplicate_p (op, &elt))
6843 return elt;
6844 }
6845
6846 if (CONST_SCALAR_INT_P (op)
6847 || CONST_DOUBLE_AS_FLOAT_P (op)
6848 || CONST_FIXED_P (op)
6849 || GET_CODE (op) == CONST_VECTOR)
6850 {
6851 unsigned HOST_WIDE_INT cbyte;
6852 if (byte.is_constant (&cbyte))
6853 {
6854 if (GET_CODE (op) == CONST_VECTOR && VECTOR_MODE_P (outermode))
6855 {
6856 rtx tmp = simplify_const_vector_subreg (outermode, op,
6857 innermode, cbyte);
6858 if (tmp)
6859 return tmp;
6860 }
6861
6862 fixed_size_mode fs_outermode;
6863 if (is_a <fixed_size_mode> (outermode, &fs_outermode))
6864 return simplify_immed_subreg (fs_outermode, op, innermode, cbyte);
6865 }
6866 }
6867
6868 /* Changing mode twice with SUBREG => just change it once,
6869 or not at all if changing back op starting mode. */
6870 if (GET_CODE (op) == SUBREG)
6871 {
6872 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
6873 poly_uint64 innermostsize = GET_MODE_SIZE (innermostmode);
6874 rtx newx;
6875
6876 if (outermode == innermostmode
6877 && known_eq (byte, 0U)
6878 && known_eq (SUBREG_BYTE (op), 0))
6879 return SUBREG_REG (op);
6880
6881 /* Work out the memory offset of the final OUTERMODE value relative
6882 to the inner value of OP. */
6883 poly_int64 mem_offset = subreg_memory_offset (outermode,
6884 innermode, byte);
6885 poly_int64 op_mem_offset = subreg_memory_offset (op);
6886 poly_int64 final_offset = mem_offset + op_mem_offset;
6887
6888 /* See whether resulting subreg will be paradoxical. */
6889 if (!paradoxical_subreg_p (outermode, innermostmode))
6890 {
6891 /* Bail out in case resulting subreg would be incorrect. */
6892 if (maybe_lt (final_offset, 0)
6893 || maybe_ge (poly_uint64 (final_offset), innermostsize)
6894 || !multiple_p (final_offset, outersize))
6895 return NULL_RTX;
6896 }
6897 else
6898 {
6899 poly_int64 required_offset = subreg_memory_offset (outermode,
6900 innermostmode, 0);
6901 if (maybe_ne (final_offset, required_offset))
6902 return NULL_RTX;
6903 /* Paradoxical subregs always have byte offset 0. */
6904 final_offset = 0;
6905 }
6906
6907 /* Recurse for further possible simplifications. */
6908 newx = simplify_subreg (outermode, SUBREG_REG (op), innermostmode,
6909 final_offset);
6910 if (newx)
6911 return newx;
6912 if (validate_subreg (outermode, innermostmode,
6913 SUBREG_REG (op), final_offset))
6914 {
6915 newx = gen_rtx_SUBREG (outermode, SUBREG_REG (op), final_offset);
6916 if (SUBREG_PROMOTED_VAR_P (op)
6917 && SUBREG_PROMOTED_SIGN (op) >= 0
6918 && GET_MODE_CLASS (outermode) == MODE_INT
6919 && known_ge (outersize, innersize)
6920 && known_le (outersize, innermostsize)
6921 && subreg_lowpart_p (newx))
6922 {
6923 SUBREG_PROMOTED_VAR_P (newx) = 1;
6924 SUBREG_PROMOTED_SET (newx, SUBREG_PROMOTED_GET (op));
6925 }
6926 return newx;
6927 }
6928 return NULL_RTX;
6929 }
6930
6931 /* SUBREG of a hard register => just change the register number
6932 and/or mode. If the hard register is not valid in that mode,
6933 suppress this simplification. If the hard register is the stack,
6934 frame, or argument pointer, leave this as a SUBREG. */
6935
6936 if (REG_P (op) && HARD_REGISTER_P (op))
6937 {
6938 unsigned int regno, final_regno;
6939
6940 regno = REGNO (op);
6941 final_regno = simplify_subreg_regno (regno, innermode, byte, outermode);
6942 if (HARD_REGISTER_NUM_P (final_regno))
6943 {
6944 rtx x = gen_rtx_REG_offset (op, outermode, final_regno,
6945 subreg_memory_offset (outermode,
6946 innermode, byte));
6947
6948 /* Propagate original regno. We don't have any way to specify
6949 the offset inside original regno, so do so only for lowpart.
6950 The information is used only by alias analysis that cannot
6951 grog partial register anyway. */
6952
6953 if (known_eq (subreg_lowpart_offset (outermode, innermode), byte))
6954 ORIGINAL_REGNO (x) = ORIGINAL_REGNO (op);
6955 return x;
6956 }
6957 }
6958
6959 /* If we have a SUBREG of a register that we are replacing and we are
6960 replacing it with a MEM, make a new MEM and try replacing the
6961 SUBREG with it. Don't do this if the MEM has a mode-dependent address
6962 or if we would be widening it. */
6963
6964 if (MEM_P (op)
6965 && ! mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op))
6966 /* Allow splitting of volatile memory references in case we don't
6967 have instruction to move the whole thing. */
6968 && (! MEM_VOLATILE_P (op)
6969 || ! have_insn_for (SET, innermode))
6970 && known_le (outersize, innersize))
6971 return adjust_address_nv (op, outermode, byte);
6972
6973 /* Handle complex or vector values represented as CONCAT or VEC_CONCAT
6974 of two parts. */
6975 if (GET_CODE (op) == CONCAT
6976 || GET_CODE (op) == VEC_CONCAT)
6977 {
6978 poly_uint64 final_offset;
6979 rtx part, res;
6980
6981 machine_mode part_mode = GET_MODE (XEXP (op, 0));
6982 if (part_mode == VOIDmode)
6983 part_mode = GET_MODE_INNER (GET_MODE (op));
6984 poly_uint64 part_size = GET_MODE_SIZE (part_mode);
6985 if (known_lt (byte, part_size))
6986 {
6987 part = XEXP (op, 0);
6988 final_offset = byte;
6989 }
6990 else if (known_ge (byte, part_size))
6991 {
6992 part = XEXP (op, 1);
6993 final_offset = byte - part_size;
6994 }
6995 else
6996 return NULL_RTX;
6997
6998 if (maybe_gt (final_offset + outersize, part_size))
6999 return NULL_RTX;
7000
7001 part_mode = GET_MODE (part);
7002 if (part_mode == VOIDmode)
7003 part_mode = GET_MODE_INNER (GET_MODE (op));
7004 res = simplify_subreg (outermode, part, part_mode, final_offset);
7005 if (res)
7006 return res;
7007 if (validate_subreg (outermode, part_mode, part, final_offset))
7008 return gen_rtx_SUBREG (outermode, part, final_offset);
7009 return NULL_RTX;
7010 }
7011
7012 /* Simplify
7013 (subreg (vec_merge (X)
7014 (vector)
7015 (const_int ((1 << N) | M)))
7016 (N * sizeof (outermode)))
7017 to
7018 (subreg (X) (N * sizeof (outermode)))
7019 */
7020 unsigned int idx;
7021 if (constant_multiple_p (byte, GET_MODE_SIZE (outermode), &idx)
7022 && idx < HOST_BITS_PER_WIDE_INT
7023 && GET_CODE (op) == VEC_MERGE
7024 && GET_MODE_INNER (innermode) == outermode
7025 && CONST_INT_P (XEXP (op, 2))
7026 && (UINTVAL (XEXP (op, 2)) & (HOST_WIDE_INT_1U << idx)) != 0)
7027 return simplify_gen_subreg (outermode, XEXP (op, 0), innermode, byte);
7028
7029 /* A SUBREG resulting from a zero extension may fold to zero if
7030 it extracts higher bits that the ZERO_EXTEND's source bits. */
7031 if (GET_CODE (op) == ZERO_EXTEND && SCALAR_INT_MODE_P (innermode))
7032 {
7033 poly_uint64 bitpos = subreg_lsb_1 (outermode, innermode, byte);
7034 if (known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (XEXP (op, 0)))))
7035 return CONST0_RTX (outermode);
7036 }
7037
7038 scalar_int_mode int_outermode, int_innermode;
7039 if (is_a <scalar_int_mode> (outermode, &int_outermode)
7040 && is_a <scalar_int_mode> (innermode, &int_innermode)
7041 && known_eq (byte, subreg_lowpart_offset (int_outermode, int_innermode)))
7042 {
7043 /* Handle polynomial integers. The upper bits of a paradoxical
7044 subreg are undefined, so this is safe regardless of whether
7045 we're truncating or extending. */
7046 if (CONST_POLY_INT_P (op))
7047 {
7048 poly_wide_int val
7049 = poly_wide_int::from (const_poly_int_value (op),
7050 GET_MODE_PRECISION (int_outermode),
7051 SIGNED);
7052 return immed_wide_int_const (val, int_outermode);
7053 }
7054
7055 if (GET_MODE_PRECISION (int_outermode)
7056 < GET_MODE_PRECISION (int_innermode))
7057 {
7058 rtx tem = simplify_truncation (int_outermode, op, int_innermode);
7059 if (tem)
7060 return tem;
7061 }
7062 }
7063
7064 /* If OP is a vector comparison and the subreg is not changing the
7065 number of elements or the size of the elements, change the result
7066 of the comparison to the new mode. */
7067 if (COMPARISON_P (op)
7068 && VECTOR_MODE_P (outermode)
7069 && VECTOR_MODE_P (innermode)
7070 && known_eq (GET_MODE_NUNITS (outermode), GET_MODE_NUNITS (innermode))
7071 && known_eq (GET_MODE_UNIT_SIZE (outermode),
7072 GET_MODE_UNIT_SIZE (innermode)))
7073 return simplify_gen_relational (GET_CODE (op), outermode, innermode,
7074 XEXP (op, 0), XEXP (op, 1));
7075 return NULL_RTX;
7076 }
7077
7078 /* Make a SUBREG operation or equivalent if it folds. */
7079
7080 rtx
7081 simplify_gen_subreg (machine_mode outermode, rtx op,
7082 machine_mode innermode, poly_uint64 byte)
7083 {
7084 rtx newx;
7085
7086 newx = simplify_subreg (outermode, op, innermode, byte);
7087 if (newx)
7088 return newx;
7089
7090 if (GET_CODE (op) == SUBREG
7091 || GET_CODE (op) == CONCAT
7092 || GET_MODE (op) == VOIDmode)
7093 return NULL_RTX;
7094
7095 if (validate_subreg (outermode, innermode, op, byte))
7096 return gen_rtx_SUBREG (outermode, op, byte);
7097
7098 return NULL_RTX;
7099 }
7100
7101 /* Generates a subreg to get the least significant part of EXPR (in mode
7102 INNER_MODE) to OUTER_MODE. */
7103
7104 rtx
7105 lowpart_subreg (machine_mode outer_mode, rtx expr,
7106 machine_mode inner_mode)
7107 {
7108 return simplify_gen_subreg (outer_mode, expr, inner_mode,
7109 subreg_lowpart_offset (outer_mode, inner_mode));
7110 }
7111
7112 /* Simplify X, an rtx expression.
7113
7114 Return the simplified expression or NULL if no simplifications
7115 were possible.
7116
7117 This is the preferred entry point into the simplification routines;
7118 however, we still allow passes to call the more specific routines.
7119
7120 Right now GCC has three (yes, three) major bodies of RTL simplification
7121 code that need to be unified.
7122
7123 1. fold_rtx in cse.c. This code uses various CSE specific
7124 information to aid in RTL simplification.
7125
7126 2. simplify_rtx in combine.c. Similar to fold_rtx, except that
7127 it uses combine specific information to aid in RTL
7128 simplification.
7129
7130 3. The routines in this file.
7131
7132
7133 Long term we want to only have one body of simplification code; to
7134 get to that state I recommend the following steps:
7135
7136 1. Pour over fold_rtx & simplify_rtx and move any simplifications
7137 which are not pass dependent state into these routines.
7138
7139 2. As code is moved by #1, change fold_rtx & simplify_rtx to
7140 use this routine whenever possible.
7141
7142 3. Allow for pass dependent state to be provided to these
7143 routines and add simplifications based on the pass dependent
7144 state. Remove code from cse.c & combine.c that becomes
7145 redundant/dead.
7146
7147 It will take time, but ultimately the compiler will be easier to
7148 maintain and improve. It's totally silly that when we add a
7149 simplification that it needs to be added to 4 places (3 for RTL
7150 simplification and 1 for tree simplification. */
7151
7152 rtx
7153 simplify_rtx (const_rtx x)
7154 {
7155 const enum rtx_code code = GET_CODE (x);
7156 const machine_mode mode = GET_MODE (x);
7157
7158 switch (GET_RTX_CLASS (code))
7159 {
7160 case RTX_UNARY:
7161 return simplify_unary_operation (code, mode,
7162 XEXP (x, 0), GET_MODE (XEXP (x, 0)));
7163 case RTX_COMM_ARITH:
7164 if (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7165 return simplify_gen_binary (code, mode, XEXP (x, 1), XEXP (x, 0));
7166
7167 /* Fall through. */
7168
7169 case RTX_BIN_ARITH:
7170 return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
7171
7172 case RTX_TERNARY:
7173 case RTX_BITFIELD_OPS:
7174 return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
7175 XEXP (x, 0), XEXP (x, 1),
7176 XEXP (x, 2));
7177
7178 case RTX_COMPARE:
7179 case RTX_COMM_COMPARE:
7180 return simplify_relational_operation (code, mode,
7181 ((GET_MODE (XEXP (x, 0))
7182 != VOIDmode)
7183 ? GET_MODE (XEXP (x, 0))
7184 : GET_MODE (XEXP (x, 1))),
7185 XEXP (x, 0),
7186 XEXP (x, 1));
7187
7188 case RTX_EXTRA:
7189 if (code == SUBREG)
7190 return simplify_subreg (mode, SUBREG_REG (x),
7191 GET_MODE (SUBREG_REG (x)),
7192 SUBREG_BYTE (x));
7193 break;
7194
7195 case RTX_OBJ:
7196 if (code == LO_SUM)
7197 {
7198 /* Convert (lo_sum (high FOO) FOO) to FOO. */
7199 if (GET_CODE (XEXP (x, 0)) == HIGH
7200 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
7201 return XEXP (x, 1);
7202 }
7203 break;
7204
7205 default:
7206 break;
7207 }
7208 return NULL;
7209 }
7210
7211 #if CHECKING_P
7212
7213 namespace selftest {
7214
7215 /* Make a unique pseudo REG of mode MODE for use by selftests. */
7216
7217 static rtx
7218 make_test_reg (machine_mode mode)
7219 {
7220 static int test_reg_num = LAST_VIRTUAL_REGISTER + 1;
7221
7222 return gen_rtx_REG (mode, test_reg_num++);
7223 }
7224
7225 /* Test vector simplifications involving VEC_DUPLICATE in which the
7226 operands and result have vector mode MODE. SCALAR_REG is a pseudo
7227 register that holds one element of MODE. */
7228
7229 static void
7230 test_vector_ops_duplicate (machine_mode mode, rtx scalar_reg)
7231 {
7232 scalar_mode inner_mode = GET_MODE_INNER (mode);
7233 rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
7234 poly_uint64 nunits = GET_MODE_NUNITS (mode);
7235 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
7236 {
7237 /* Test some simple unary cases with VEC_DUPLICATE arguments. */
7238 rtx not_scalar_reg = gen_rtx_NOT (inner_mode, scalar_reg);
7239 rtx duplicate_not = gen_rtx_VEC_DUPLICATE (mode, not_scalar_reg);
7240 ASSERT_RTX_EQ (duplicate,
7241 simplify_unary_operation (NOT, mode,
7242 duplicate_not, mode));
7243
7244 rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
7245 rtx duplicate_neg = gen_rtx_VEC_DUPLICATE (mode, neg_scalar_reg);
7246 ASSERT_RTX_EQ (duplicate,
7247 simplify_unary_operation (NEG, mode,
7248 duplicate_neg, mode));
7249
7250 /* Test some simple binary cases with VEC_DUPLICATE arguments. */
7251 ASSERT_RTX_EQ (duplicate,
7252 simplify_binary_operation (PLUS, mode, duplicate,
7253 CONST0_RTX (mode)));
7254
7255 ASSERT_RTX_EQ (duplicate,
7256 simplify_binary_operation (MINUS, mode, duplicate,
7257 CONST0_RTX (mode)));
7258
7259 ASSERT_RTX_PTR_EQ (CONST0_RTX (mode),
7260 simplify_binary_operation (MINUS, mode, duplicate,
7261 duplicate));
7262 }
7263
7264 /* Test a scalar VEC_SELECT of a VEC_DUPLICATE. */
7265 rtx zero_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
7266 ASSERT_RTX_PTR_EQ (scalar_reg,
7267 simplify_binary_operation (VEC_SELECT, inner_mode,
7268 duplicate, zero_par));
7269
7270 unsigned HOST_WIDE_INT const_nunits;
7271 if (nunits.is_constant (&const_nunits))
7272 {
7273 /* And again with the final element. */
7274 rtx last_index = gen_int_mode (const_nunits - 1, word_mode);
7275 rtx last_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, last_index));
7276 ASSERT_RTX_PTR_EQ (scalar_reg,
7277 simplify_binary_operation (VEC_SELECT, inner_mode,
7278 duplicate, last_par));
7279
7280 /* Test a scalar subreg of a VEC_MERGE of a VEC_DUPLICATE. */
7281 rtx vector_reg = make_test_reg (mode);
7282 for (unsigned HOST_WIDE_INT i = 0; i < const_nunits; i++)
7283 {
7284 if (i >= HOST_BITS_PER_WIDE_INT)
7285 break;
7286 rtx mask = GEN_INT ((HOST_WIDE_INT_1U << i) | (i + 1));
7287 rtx vm = gen_rtx_VEC_MERGE (mode, duplicate, vector_reg, mask);
7288 poly_uint64 offset = i * GET_MODE_SIZE (inner_mode);
7289 ASSERT_RTX_EQ (scalar_reg,
7290 simplify_gen_subreg (inner_mode, vm,
7291 mode, offset));
7292 }
7293 }
7294
7295 /* Test a scalar subreg of a VEC_DUPLICATE. */
7296 poly_uint64 offset = subreg_lowpart_offset (inner_mode, mode);
7297 ASSERT_RTX_EQ (scalar_reg,
7298 simplify_gen_subreg (inner_mode, duplicate,
7299 mode, offset));
7300
7301 machine_mode narrower_mode;
7302 if (maybe_ne (nunits, 2U)
7303 && multiple_p (nunits, 2)
7304 && mode_for_vector (inner_mode, 2).exists (&narrower_mode)
7305 && VECTOR_MODE_P (narrower_mode))
7306 {
7307 /* Test VEC_DUPLICATE of a vector. */
7308 rtx_vector_builder nbuilder (narrower_mode, 2, 1);
7309 nbuilder.quick_push (const0_rtx);
7310 nbuilder.quick_push (const1_rtx);
7311 rtx_vector_builder builder (mode, 2, 1);
7312 builder.quick_push (const0_rtx);
7313 builder.quick_push (const1_rtx);
7314 ASSERT_RTX_EQ (builder.build (),
7315 simplify_unary_operation (VEC_DUPLICATE, mode,
7316 nbuilder.build (),
7317 narrower_mode));
7318
7319 /* Test VEC_SELECT of a vector. */
7320 rtx vec_par
7321 = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, const1_rtx, const0_rtx));
7322 rtx narrower_duplicate
7323 = gen_rtx_VEC_DUPLICATE (narrower_mode, scalar_reg);
7324 ASSERT_RTX_EQ (narrower_duplicate,
7325 simplify_binary_operation (VEC_SELECT, narrower_mode,
7326 duplicate, vec_par));
7327
7328 /* Test a vector subreg of a VEC_DUPLICATE. */
7329 poly_uint64 offset = subreg_lowpart_offset (narrower_mode, mode);
7330 ASSERT_RTX_EQ (narrower_duplicate,
7331 simplify_gen_subreg (narrower_mode, duplicate,
7332 mode, offset));
7333 }
7334 }
7335
7336 /* Test vector simplifications involving VEC_SERIES in which the
7337 operands and result have vector mode MODE. SCALAR_REG is a pseudo
7338 register that holds one element of MODE. */
7339
7340 static void
7341 test_vector_ops_series (machine_mode mode, rtx scalar_reg)
7342 {
7343 /* Test unary cases with VEC_SERIES arguments. */
7344 scalar_mode inner_mode = GET_MODE_INNER (mode);
7345 rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
7346 rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
7347 rtx series_0_r = gen_rtx_VEC_SERIES (mode, const0_rtx, scalar_reg);
7348 rtx series_0_nr = gen_rtx_VEC_SERIES (mode, const0_rtx, neg_scalar_reg);
7349 rtx series_nr_1 = gen_rtx_VEC_SERIES (mode, neg_scalar_reg, const1_rtx);
7350 rtx series_r_m1 = gen_rtx_VEC_SERIES (mode, scalar_reg, constm1_rtx);
7351 rtx series_r_r = gen_rtx_VEC_SERIES (mode, scalar_reg, scalar_reg);
7352 rtx series_nr_nr = gen_rtx_VEC_SERIES (mode, neg_scalar_reg,
7353 neg_scalar_reg);
7354 ASSERT_RTX_EQ (series_0_r,
7355 simplify_unary_operation (NEG, mode, series_0_nr, mode));
7356 ASSERT_RTX_EQ (series_r_m1,
7357 simplify_unary_operation (NEG, mode, series_nr_1, mode));
7358 ASSERT_RTX_EQ (series_r_r,
7359 simplify_unary_operation (NEG, mode, series_nr_nr, mode));
7360
7361 /* Test that a VEC_SERIES with a zero step is simplified away. */
7362 ASSERT_RTX_EQ (duplicate,
7363 simplify_binary_operation (VEC_SERIES, mode,
7364 scalar_reg, const0_rtx));
7365
7366 /* Test PLUS and MINUS with VEC_SERIES. */
7367 rtx series_0_1 = gen_const_vec_series (mode, const0_rtx, const1_rtx);
7368 rtx series_0_m1 = gen_const_vec_series (mode, const0_rtx, constm1_rtx);
7369 rtx series_r_1 = gen_rtx_VEC_SERIES (mode, scalar_reg, const1_rtx);
7370 ASSERT_RTX_EQ (series_r_r,
7371 simplify_binary_operation (PLUS, mode, series_0_r,
7372 duplicate));
7373 ASSERT_RTX_EQ (series_r_1,
7374 simplify_binary_operation (PLUS, mode, duplicate,
7375 series_0_1));
7376 ASSERT_RTX_EQ (series_r_m1,
7377 simplify_binary_operation (PLUS, mode, duplicate,
7378 series_0_m1));
7379 ASSERT_RTX_EQ (series_0_r,
7380 simplify_binary_operation (MINUS, mode, series_r_r,
7381 duplicate));
7382 ASSERT_RTX_EQ (series_r_m1,
7383 simplify_binary_operation (MINUS, mode, duplicate,
7384 series_0_1));
7385 ASSERT_RTX_EQ (series_r_1,
7386 simplify_binary_operation (MINUS, mode, duplicate,
7387 series_0_m1));
7388 ASSERT_RTX_EQ (series_0_m1,
7389 simplify_binary_operation (VEC_SERIES, mode, const0_rtx,
7390 constm1_rtx));
7391
7392 /* Test NEG on constant vector series. */
7393 ASSERT_RTX_EQ (series_0_m1,
7394 simplify_unary_operation (NEG, mode, series_0_1, mode));
7395 ASSERT_RTX_EQ (series_0_1,
7396 simplify_unary_operation (NEG, mode, series_0_m1, mode));
7397
7398 /* Test PLUS and MINUS on constant vector series. */
7399 rtx scalar2 = gen_int_mode (2, inner_mode);
7400 rtx scalar3 = gen_int_mode (3, inner_mode);
7401 rtx series_1_1 = gen_const_vec_series (mode, const1_rtx, const1_rtx);
7402 rtx series_0_2 = gen_const_vec_series (mode, const0_rtx, scalar2);
7403 rtx series_1_3 = gen_const_vec_series (mode, const1_rtx, scalar3);
7404 ASSERT_RTX_EQ (series_1_1,
7405 simplify_binary_operation (PLUS, mode, series_0_1,
7406 CONST1_RTX (mode)));
7407 ASSERT_RTX_EQ (series_0_m1,
7408 simplify_binary_operation (PLUS, mode, CONST0_RTX (mode),
7409 series_0_m1));
7410 ASSERT_RTX_EQ (series_1_3,
7411 simplify_binary_operation (PLUS, mode, series_1_1,
7412 series_0_2));
7413 ASSERT_RTX_EQ (series_0_1,
7414 simplify_binary_operation (MINUS, mode, series_1_1,
7415 CONST1_RTX (mode)));
7416 ASSERT_RTX_EQ (series_1_1,
7417 simplify_binary_operation (MINUS, mode, CONST1_RTX (mode),
7418 series_0_m1));
7419 ASSERT_RTX_EQ (series_1_1,
7420 simplify_binary_operation (MINUS, mode, series_1_3,
7421 series_0_2));
7422
7423 /* Test MULT between constant vectors. */
7424 rtx vec2 = gen_const_vec_duplicate (mode, scalar2);
7425 rtx vec3 = gen_const_vec_duplicate (mode, scalar3);
7426 rtx scalar9 = gen_int_mode (9, inner_mode);
7427 rtx series_3_9 = gen_const_vec_series (mode, scalar3, scalar9);
7428 ASSERT_RTX_EQ (series_0_2,
7429 simplify_binary_operation (MULT, mode, series_0_1, vec2));
7430 ASSERT_RTX_EQ (series_3_9,
7431 simplify_binary_operation (MULT, mode, vec3, series_1_3));
7432 if (!GET_MODE_NUNITS (mode).is_constant ())
7433 ASSERT_FALSE (simplify_binary_operation (MULT, mode, series_0_1,
7434 series_0_1));
7435
7436 /* Test ASHIFT between constant vectors. */
7437 ASSERT_RTX_EQ (series_0_2,
7438 simplify_binary_operation (ASHIFT, mode, series_0_1,
7439 CONST1_RTX (mode)));
7440 if (!GET_MODE_NUNITS (mode).is_constant ())
7441 ASSERT_FALSE (simplify_binary_operation (ASHIFT, mode, CONST1_RTX (mode),
7442 series_0_1));
7443 }
7444
7445 /* Verify simplify_merge_mask works correctly. */
7446
7447 static void
7448 test_vec_merge (machine_mode mode)
7449 {
7450 rtx op0 = make_test_reg (mode);
7451 rtx op1 = make_test_reg (mode);
7452 rtx op2 = make_test_reg (mode);
7453 rtx op3 = make_test_reg (mode);
7454 rtx op4 = make_test_reg (mode);
7455 rtx op5 = make_test_reg (mode);
7456 rtx mask1 = make_test_reg (SImode);
7457 rtx mask2 = make_test_reg (SImode);
7458 rtx vm1 = gen_rtx_VEC_MERGE (mode, op0, op1, mask1);
7459 rtx vm2 = gen_rtx_VEC_MERGE (mode, op2, op3, mask1);
7460 rtx vm3 = gen_rtx_VEC_MERGE (mode, op4, op5, mask1);
7461
7462 /* Simple vec_merge. */
7463 ASSERT_EQ (op0, simplify_merge_mask (vm1, mask1, 0));
7464 ASSERT_EQ (op1, simplify_merge_mask (vm1, mask1, 1));
7465 ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 0));
7466 ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 1));
7467
7468 /* Nested vec_merge.
7469 It's tempting to make this simplify right down to opN, but we don't
7470 because all the simplify_* functions assume that the operands have
7471 already been simplified. */
7472 rtx nvm = gen_rtx_VEC_MERGE (mode, vm1, vm2, mask1);
7473 ASSERT_EQ (vm1, simplify_merge_mask (nvm, mask1, 0));
7474 ASSERT_EQ (vm2, simplify_merge_mask (nvm, mask1, 1));
7475
7476 /* Intermediate unary op. */
7477 rtx unop = gen_rtx_NOT (mode, vm1);
7478 ASSERT_RTX_EQ (gen_rtx_NOT (mode, op0),
7479 simplify_merge_mask (unop, mask1, 0));
7480 ASSERT_RTX_EQ (gen_rtx_NOT (mode, op1),
7481 simplify_merge_mask (unop, mask1, 1));
7482
7483 /* Intermediate binary op. */
7484 rtx binop = gen_rtx_PLUS (mode, vm1, vm2);
7485 ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op0, op2),
7486 simplify_merge_mask (binop, mask1, 0));
7487 ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op1, op3),
7488 simplify_merge_mask (binop, mask1, 1));
7489
7490 /* Intermediate ternary op. */
7491 rtx tenop = gen_rtx_FMA (mode, vm1, vm2, vm3);
7492 ASSERT_RTX_EQ (gen_rtx_FMA (mode, op0, op2, op4),
7493 simplify_merge_mask (tenop, mask1, 0));
7494 ASSERT_RTX_EQ (gen_rtx_FMA (mode, op1, op3, op5),
7495 simplify_merge_mask (tenop, mask1, 1));
7496
7497 /* Side effects. */
7498 rtx badop0 = gen_rtx_PRE_INC (mode, op0);
7499 rtx badvm = gen_rtx_VEC_MERGE (mode, badop0, op1, mask1);
7500 ASSERT_EQ (badop0, simplify_merge_mask (badvm, mask1, 0));
7501 ASSERT_EQ (NULL_RTX, simplify_merge_mask (badvm, mask1, 1));
7502
7503 /* Called indirectly. */
7504 ASSERT_RTX_EQ (gen_rtx_VEC_MERGE (mode, op0, op3, mask1),
7505 simplify_rtx (nvm));
7506 }
7507
7508 /* Test subregs of integer vector constant X, trying elements in
7509 the range [ELT_BIAS, ELT_BIAS + constant_lower_bound (NELTS)),
7510 where NELTS is the number of elements in X. Subregs involving
7511 elements [ELT_BIAS, ELT_BIAS + FIRST_VALID) are expected to fail. */
7512
7513 static void
7514 test_vector_subregs_modes (rtx x, poly_uint64 elt_bias = 0,
7515 unsigned int first_valid = 0)
7516 {
7517 machine_mode inner_mode = GET_MODE (x);
7518 scalar_mode int_mode = GET_MODE_INNER (inner_mode);
7519
7520 for (unsigned int modei = 0; modei < NUM_MACHINE_MODES; ++modei)
7521 {
7522 machine_mode outer_mode = (machine_mode) modei;
7523 if (!VECTOR_MODE_P (outer_mode))
7524 continue;
7525
7526 unsigned int outer_nunits;
7527 if (GET_MODE_INNER (outer_mode) == int_mode
7528 && GET_MODE_NUNITS (outer_mode).is_constant (&outer_nunits)
7529 && multiple_p (GET_MODE_NUNITS (inner_mode), outer_nunits))
7530 {
7531 /* Test subregs in which the outer mode is a smaller,
7532 constant-sized vector of the same element type. */
7533 unsigned int limit
7534 = constant_lower_bound (GET_MODE_NUNITS (inner_mode));
7535 for (unsigned int elt = 0; elt < limit; elt += outer_nunits)
7536 {
7537 rtx expected = NULL_RTX;
7538 if (elt >= first_valid)
7539 {
7540 rtx_vector_builder builder (outer_mode, outer_nunits, 1);
7541 for (unsigned int i = 0; i < outer_nunits; ++i)
7542 builder.quick_push (CONST_VECTOR_ELT (x, elt + i));
7543 expected = builder.build ();
7544 }
7545 poly_uint64 byte = (elt_bias + elt) * GET_MODE_SIZE (int_mode);
7546 ASSERT_RTX_EQ (expected,
7547 simplify_subreg (outer_mode, x,
7548 inner_mode, byte));
7549 }
7550 }
7551 else if (known_eq (GET_MODE_SIZE (outer_mode),
7552 GET_MODE_SIZE (inner_mode))
7553 && known_eq (elt_bias, 0U)
7554 && (GET_MODE_CLASS (outer_mode) != MODE_VECTOR_BOOL
7555 || known_eq (GET_MODE_BITSIZE (outer_mode),
7556 GET_MODE_NUNITS (outer_mode)))
7557 && (!FLOAT_MODE_P (outer_mode)
7558 || (FLOAT_MODE_FORMAT (outer_mode)->ieee_bits
7559 == GET_MODE_UNIT_PRECISION (outer_mode)))
7560 && (GET_MODE_SIZE (inner_mode).is_constant ()
7561 || !CONST_VECTOR_STEPPED_P (x)))
7562 {
7563 /* Try converting to OUTER_MODE and back. */
7564 rtx outer_x = simplify_subreg (outer_mode, x, inner_mode, 0);
7565 ASSERT_TRUE (outer_x != NULL_RTX);
7566 ASSERT_RTX_EQ (x, simplify_subreg (inner_mode, outer_x,
7567 outer_mode, 0));
7568 }
7569 }
7570
7571 if (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
7572 {
7573 /* Test each byte in the element range. */
7574 unsigned int limit
7575 = constant_lower_bound (GET_MODE_SIZE (inner_mode));
7576 for (unsigned int i = 0; i < limit; ++i)
7577 {
7578 unsigned int elt = i / GET_MODE_SIZE (int_mode);
7579 rtx expected = NULL_RTX;
7580 if (elt >= first_valid)
7581 {
7582 unsigned int byte_shift = i % GET_MODE_SIZE (int_mode);
7583 if (BYTES_BIG_ENDIAN)
7584 byte_shift = GET_MODE_SIZE (int_mode) - byte_shift - 1;
7585 rtx_mode_t vec_elt (CONST_VECTOR_ELT (x, elt), int_mode);
7586 wide_int shifted_elt
7587 = wi::lrshift (vec_elt, byte_shift * BITS_PER_UNIT);
7588 expected = immed_wide_int_const (shifted_elt, QImode);
7589 }
7590 poly_uint64 byte = elt_bias * GET_MODE_SIZE (int_mode) + i;
7591 ASSERT_RTX_EQ (expected,
7592 simplify_subreg (QImode, x, inner_mode, byte));
7593 }
7594 }
7595 }
7596
7597 /* Test constant subregs of integer vector mode INNER_MODE, using 1
7598 element per pattern. */
7599
7600 static void
7601 test_vector_subregs_repeating (machine_mode inner_mode)
7602 {
7603 poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
7604 unsigned int min_nunits = constant_lower_bound (nunits);
7605 scalar_mode int_mode = GET_MODE_INNER (inner_mode);
7606 unsigned int count = gcd (min_nunits, 8);
7607
7608 rtx_vector_builder builder (inner_mode, count, 1);
7609 for (unsigned int i = 0; i < count; ++i)
7610 builder.quick_push (gen_int_mode (8 - i, int_mode));
7611 rtx x = builder.build ();
7612
7613 test_vector_subregs_modes (x);
7614 if (!nunits.is_constant ())
7615 test_vector_subregs_modes (x, nunits - min_nunits);
7616 }
7617
7618 /* Test constant subregs of integer vector mode INNER_MODE, using 2
7619 elements per pattern. */
7620
7621 static void
7622 test_vector_subregs_fore_back (machine_mode inner_mode)
7623 {
7624 poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
7625 unsigned int min_nunits = constant_lower_bound (nunits);
7626 scalar_mode int_mode = GET_MODE_INNER (inner_mode);
7627 unsigned int count = gcd (min_nunits, 4);
7628
7629 rtx_vector_builder builder (inner_mode, count, 2);
7630 for (unsigned int i = 0; i < count; ++i)
7631 builder.quick_push (gen_int_mode (i, int_mode));
7632 for (unsigned int i = 0; i < count; ++i)
7633 builder.quick_push (gen_int_mode (-(int) i, int_mode));
7634 rtx x = builder.build ();
7635
7636 test_vector_subregs_modes (x);
7637 if (!nunits.is_constant ())
7638 test_vector_subregs_modes (x, nunits - min_nunits, count);
7639 }
7640
7641 /* Test constant subregs of integer vector mode INNER_MODE, using 3
7642 elements per pattern. */
7643
7644 static void
7645 test_vector_subregs_stepped (machine_mode inner_mode)
7646 {
7647 /* Build { 0, 1, 2, 3, ... }. */
7648 scalar_mode int_mode = GET_MODE_INNER (inner_mode);
7649 rtx_vector_builder builder (inner_mode, 1, 3);
7650 for (unsigned int i = 0; i < 3; ++i)
7651 builder.quick_push (gen_int_mode (i, int_mode));
7652 rtx x = builder.build ();
7653
7654 test_vector_subregs_modes (x);
7655 }
7656
7657 /* Test constant subregs of integer vector mode INNER_MODE. */
7658
7659 static void
7660 test_vector_subregs (machine_mode inner_mode)
7661 {
7662 test_vector_subregs_repeating (inner_mode);
7663 test_vector_subregs_fore_back (inner_mode);
7664 test_vector_subregs_stepped (inner_mode);
7665 }
7666
7667 /* Verify some simplifications involving vectors. */
7668
7669 static void
7670 test_vector_ops ()
7671 {
7672 for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
7673 {
7674 machine_mode mode = (machine_mode) i;
7675 if (VECTOR_MODE_P (mode))
7676 {
7677 rtx scalar_reg = make_test_reg (GET_MODE_INNER (mode));
7678 test_vector_ops_duplicate (mode, scalar_reg);
7679 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
7680 && maybe_gt (GET_MODE_NUNITS (mode), 2))
7681 {
7682 test_vector_ops_series (mode, scalar_reg);
7683 test_vector_subregs (mode);
7684 }
7685 test_vec_merge (mode);
7686 }
7687 }
7688 }
7689
7690 template<unsigned int N>
7691 struct simplify_const_poly_int_tests
7692 {
7693 static void run ();
7694 };
7695
7696 template<>
7697 struct simplify_const_poly_int_tests<1>
7698 {
7699 static void run () {}
7700 };
7701
7702 /* Test various CONST_POLY_INT properties. */
7703
7704 template<unsigned int N>
7705 void
7706 simplify_const_poly_int_tests<N>::run ()
7707 {
7708 rtx x1 = gen_int_mode (poly_int64 (1, 1), QImode);
7709 rtx x2 = gen_int_mode (poly_int64 (-80, 127), QImode);
7710 rtx x3 = gen_int_mode (poly_int64 (-79, -128), QImode);
7711 rtx x4 = gen_int_mode (poly_int64 (5, 4), QImode);
7712 rtx x5 = gen_int_mode (poly_int64 (30, 24), QImode);
7713 rtx x6 = gen_int_mode (poly_int64 (20, 16), QImode);
7714 rtx x7 = gen_int_mode (poly_int64 (7, 4), QImode);
7715 rtx x8 = gen_int_mode (poly_int64 (30, 24), HImode);
7716 rtx x9 = gen_int_mode (poly_int64 (-30, -24), HImode);
7717 rtx x10 = gen_int_mode (poly_int64 (-31, -24), HImode);
7718 rtx two = GEN_INT (2);
7719 rtx six = GEN_INT (6);
7720 poly_uint64 offset = subreg_lowpart_offset (QImode, HImode);
7721
7722 /* These tests only try limited operation combinations. Fuller arithmetic
7723 testing is done directly on poly_ints. */
7724 ASSERT_EQ (simplify_unary_operation (NEG, HImode, x8, HImode), x9);
7725 ASSERT_EQ (simplify_unary_operation (NOT, HImode, x8, HImode), x10);
7726 ASSERT_EQ (simplify_unary_operation (TRUNCATE, QImode, x8, HImode), x5);
7727 ASSERT_EQ (simplify_binary_operation (PLUS, QImode, x1, x2), x3);
7728 ASSERT_EQ (simplify_binary_operation (MINUS, QImode, x3, x1), x2);
7729 ASSERT_EQ (simplify_binary_operation (MULT, QImode, x4, six), x5);
7730 ASSERT_EQ (simplify_binary_operation (MULT, QImode, six, x4), x5);
7731 ASSERT_EQ (simplify_binary_operation (ASHIFT, QImode, x4, two), x6);
7732 ASSERT_EQ (simplify_binary_operation (IOR, QImode, x4, two), x7);
7733 ASSERT_EQ (simplify_subreg (HImode, x5, QImode, 0), x8);
7734 ASSERT_EQ (simplify_subreg (QImode, x8, HImode, offset), x5);
7735 }
7736
7737 /* Run all of the selftests within this file. */
7738
7739 void
7740 simplify_rtx_c_tests ()
7741 {
7742 test_vector_ops ();
7743 simplify_const_poly_int_tests<NUM_POLY_INT_COEFFS>::run ();
7744 }
7745
7746 } // namespace selftest
7747
7748 #endif /* CHECKING_P */