Debian Codegraph @

gcc/simplify-rtx.cc

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