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