Line data Source code
1 : /* RTL simplification functions for GNU compiler.
2 : Copyright (C) 1987-2026 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 9233186 : neg_poly_int_rtx (machine_mode mode, const_rtx i)
56 : {
57 9233186 : 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 6307398 : mode_signbit_p (machine_mode mode, const_rtx x)
65 : {
66 6307398 : unsigned HOST_WIDE_INT val;
67 6307398 : unsigned int width;
68 6307398 : scalar_int_mode int_mode;
69 :
70 6307398 : if (!is_int_mode (mode, &int_mode))
71 : return false;
72 :
73 6307390 : width = GET_MODE_PRECISION (int_mode);
74 6307390 : if (width == 0)
75 : return false;
76 :
77 6307390 : if (width <= HOST_BITS_PER_WIDE_INT
78 6305857 : && CONST_INT_P (x))
79 6160381 : val = INTVAL (x);
80 : #if TARGET_SUPPORTS_WIDE_INT
81 147009 : else if (CONST_WIDE_INT_P (x))
82 : {
83 483 : unsigned int i;
84 483 : unsigned int elts = CONST_WIDE_INT_NUNITS (x);
85 483 : if (elts != (width + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
86 : return false;
87 906 : for (i = 0; i < elts - 1; i++)
88 483 : if (CONST_WIDE_INT_ELT (x, i) != 0)
89 : return false;
90 423 : val = CONST_WIDE_INT_ELT (x, elts - 1);
91 423 : width %= HOST_BITS_PER_WIDE_INT;
92 423 : 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 6160381 : if (width < HOST_BITS_PER_WIDE_INT)
109 5581434 : val &= (HOST_WIDE_INT_1U << width) - 1;
110 6160804 : 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 3977121 : val_signbit_p (machine_mode mode, unsigned HOST_WIDE_INT val)
119 : {
120 3977121 : unsigned int width;
121 3977121 : scalar_int_mode int_mode;
122 :
123 3977121 : if (!is_int_mode (mode, &int_mode))
124 : return false;
125 :
126 3977085 : width = GET_MODE_PRECISION (int_mode);
127 3977085 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
128 : return false;
129 :
130 3920331 : val &= GET_MODE_MASK (int_mode);
131 3920331 : 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 2708965 : val_signbit_known_set_p (machine_mode mode, unsigned HOST_WIDE_INT val)
138 : {
139 2708965 : unsigned int width;
140 :
141 2708965 : scalar_int_mode int_mode;
142 2708965 : if (!is_int_mode (mode, &int_mode))
143 : return false;
144 :
145 2645871 : width = GET_MODE_PRECISION (int_mode);
146 2645871 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
147 : return false;
148 :
149 2645871 : val &= HOST_WIDE_INT_1U << (width - 1);
150 2645871 : 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 7993153 : val_signbit_known_clear_p (machine_mode mode, unsigned HOST_WIDE_INT val)
157 : {
158 7993153 : unsigned int width;
159 :
160 7993153 : scalar_int_mode int_mode;
161 7993153 : if (!is_int_mode (mode, &int_mode))
162 : return false;
163 :
164 7629287 : width = GET_MODE_PRECISION (int_mode);
165 7629287 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
166 : return false;
167 :
168 7518694 : val &= HOST_WIDE_INT_1U << (width - 1);
169 7518694 : 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 124328892 : simplify_context::simplify_gen_binary (rtx_code code, machine_mode mode,
177 : rtx op0, rtx op1)
178 : {
179 124328892 : rtx tem;
180 :
181 : /* If this simplifies, do it. */
182 124328892 : tem = simplify_binary_operation (code, mode, op0, op1);
183 124328892 : if (tem)
184 : return tem;
185 :
186 : /* Put complex operands first and constants second if commutative. */
187 79462884 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
188 79462884 : && swap_commutative_operands_p (op0, op1))
189 : std::swap (op0, op1);
190 :
191 79462884 : 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 2814923986 : avoid_constant_pool_reference (rtx x)
198 : {
199 2814923986 : rtx c, tmp, addr;
200 2814923986 : machine_mode cmode;
201 2814923986 : poly_int64 offset = 0;
202 :
203 2814923986 : switch (GET_CODE (x))
204 : {
205 269928675 : case MEM:
206 269928675 : break;
207 :
208 918508 : case FLOAT_EXTEND:
209 : /* Handle float extensions of constant pool references. */
210 918508 : tmp = XEXP (x, 0);
211 918508 : c = avoid_constant_pool_reference (tmp);
212 918508 : if (c != tmp && CONST_DOUBLE_AS_FLOAT_P (c))
213 125547 : return const_double_from_real_value (*CONST_DOUBLE_REAL_VALUE (c),
214 125547 : GET_MODE (x));
215 : return x;
216 :
217 : default:
218 : return x;
219 : }
220 :
221 269928675 : if (GET_MODE (x) == BLKmode)
222 : return x;
223 :
224 265831084 : addr = XEXP (x, 0);
225 :
226 : /* Call target hook to avoid the effects of -fpic etc.... */
227 265831084 : addr = targetm.delegitimize_address (addr);
228 :
229 : /* Split the address into a base and integer offset. */
230 265831084 : addr = strip_offset (addr, &offset);
231 :
232 265831084 : if (GET_CODE (addr) == LO_SUM)
233 0 : 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 265831084 : if (GET_CODE (addr) == SYMBOL_REF
238 265831084 : && CONSTANT_POOL_ADDRESS_P (addr))
239 : {
240 5496884 : c = get_pool_constant (addr);
241 5496884 : 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 5496884 : if (known_eq (offset, 0) && cmode == GET_MODE (x))
247 : return c;
248 45816 : else if (known_in_range_p (offset, 0, GET_MODE_SIZE (cmode)))
249 : {
250 15272 : rtx tem = simplify_subreg (GET_MODE (x), c, cmode, offset);
251 15272 : if (tem && CONSTANT_P (tem))
252 15155 : 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 3570128749 : 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 3570128749 : if (MEM_P (x)
269 62746890 : && MEM_EXPR (x)
270 3608975010 : && MEM_OFFSET_KNOWN_P (x))
271 : {
272 35846906 : tree decl = MEM_EXPR (x);
273 35846906 : machine_mode mode = GET_MODE (x);
274 35846906 : poly_int64 offset = 0;
275 :
276 35846906 : switch (TREE_CODE (decl))
277 : {
278 : default:
279 : decl = NULL;
280 : break;
281 :
282 : case VAR_DECL:
283 : break;
284 :
285 10149416 : case ARRAY_REF:
286 10149416 : case ARRAY_RANGE_REF:
287 10149416 : case COMPONENT_REF:
288 10149416 : case BIT_FIELD_REF:
289 10149416 : case REALPART_EXPR:
290 10149416 : case IMAGPART_EXPR:
291 10149416 : case VIEW_CONVERT_EXPR:
292 10149416 : {
293 10149416 : poly_int64 bitsize, bitpos, bytepos, toffset_val = 0;
294 10149416 : tree toffset;
295 10149416 : int unsignedp, reversep, volatilep = 0;
296 :
297 10149416 : decl
298 10149416 : = get_inner_reference (decl, &bitsize, &bitpos, &toffset, &mode,
299 : &unsignedp, &reversep, &volatilep);
300 20298832 : if (maybe_ne (bitsize, GET_MODE_BITSIZE (mode))
301 10433403 : || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
302 19858041 : || (toffset && !poly_int_tree_p (toffset, &toffset_val)))
303 : decl = NULL;
304 : else
305 9424638 : offset += bytepos + toffset_val;
306 10149416 : break;
307 : }
308 : }
309 :
310 724778 : if (decl
311 21011698 : && mode == GET_MODE (x)
312 20737147 : && VAR_P (decl)
313 13502801 : && (TREE_STATIC (decl)
314 12267005 : || DECL_THREAD_LOCAL_P (decl))
315 1273500 : && DECL_RTL_SET_P (decl)
316 10697678 : && MEM_P (DECL_RTL (decl)))
317 : {
318 1273040 : rtx newx;
319 :
320 1273040 : offset += MEM_OFFSET (x);
321 :
322 1273040 : newx = DECL_RTL (decl);
323 :
324 1273040 : if (MEM_P (newx))
325 : {
326 1273040 : rtx n = XEXP (newx, 0), o = XEXP (x, 0);
327 1273040 : 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 1273040 : n = strip_offset (n, &n_offset);
336 1273040 : o = strip_offset (o, &o_offset);
337 2517889 : if (!(known_eq (o_offset, n_offset + offset)
338 1244849 : && rtx_equal_p (o, n)))
339 214162 : x = adjust_address_nv (newx, mode, offset);
340 : }
341 0 : else if (GET_MODE (x) == GET_MODE (newx)
342 0 : && known_eq (offset, 0))
343 : x = newx;
344 : }
345 : }
346 :
347 3570128749 : 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 7292552 : simplify_context::simplify_gen_unary (rtx_code code, machine_mode mode, rtx op,
355 : machine_mode op_mode)
356 : {
357 7292552 : rtx tem;
358 :
359 : /* If this simplifies, use it. */
360 7292552 : if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
361 : return tem;
362 :
363 2505203 : return gen_rtx_fmt_e (code, mode, op);
364 : }
365 :
366 : /* Likewise for ternary operations. */
367 :
368 : rtx
369 2956012 : 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 2956012 : rtx tem;
374 :
375 : /* If this simplifies, use it. */
376 2956012 : if ((tem = simplify_ternary_operation (code, mode, op0_mode,
377 : op0, op1, op2)) != 0)
378 : return tem;
379 :
380 2703537 : 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 22824469 : simplify_context::simplify_gen_relational (rtx_code code, machine_mode mode,
388 : machine_mode cmp_mode,
389 : rtx op0, rtx op1)
390 : {
391 22824469 : rtx tem;
392 :
393 22824469 : if ((tem = simplify_relational_operation (code, mode, cmp_mode,
394 : op0, op1)) != 0)
395 : return tem;
396 :
397 20134171 : 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 503708013 : simplify_replace_fn_rtx (rtx x, const_rtx old_rtx,
407 : rtx (*fn) (rtx, const_rtx, void *), void *data)
408 : {
409 503708013 : enum rtx_code code = GET_CODE (x);
410 503708013 : machine_mode mode = GET_MODE (x);
411 503708013 : machine_mode op_mode;
412 503708013 : const char *fmt;
413 503708013 : rtx op0, op1, op2, newx, op;
414 503708013 : rtvec vec, newvec;
415 503708013 : int i, j;
416 :
417 503708013 : if (UNLIKELY (fn != NULL))
418 : {
419 438729207 : newx = fn (x, old_rtx, data);
420 438729207 : if (newx)
421 : return newx;
422 : }
423 64978806 : else if (rtx_equal_p (x, old_rtx))
424 5692933 : return copy_rtx ((rtx) data);
425 :
426 396961811 : switch (GET_RTX_CLASS (code))
427 : {
428 2068578 : case RTX_UNARY:
429 2068578 : op0 = XEXP (x, 0);
430 2068578 : op_mode = GET_MODE (op0);
431 2068578 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
432 2068578 : if (op0 == XEXP (x, 0))
433 : return x;
434 655668 : return simplify_gen_unary (code, mode, op0, op_mode);
435 :
436 76211704 : case RTX_BIN_ARITH:
437 76211704 : case RTX_COMM_ARITH:
438 76211704 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
439 76211704 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
440 76211704 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
441 : return x;
442 23488559 : return simplify_gen_binary (code, mode, op0, op1);
443 :
444 10087850 : case RTX_COMPARE:
445 10087850 : case RTX_COMM_COMPARE:
446 10087850 : op0 = XEXP (x, 0);
447 10087850 : op1 = XEXP (x, 1);
448 10087850 : op_mode = GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
449 10087850 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
450 10087850 : op1 = simplify_replace_fn_rtx (op1, old_rtx, fn, data);
451 10087850 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
452 : return x;
453 2782021 : return simplify_gen_relational (code, mode, op_mode, op0, op1);
454 :
455 6351081 : case RTX_TERNARY:
456 6351081 : case RTX_BITFIELD_OPS:
457 6351081 : op0 = XEXP (x, 0);
458 6351081 : op_mode = GET_MODE (op0);
459 6351081 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
460 6351081 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
461 6351081 : op2 = simplify_replace_fn_rtx (XEXP (x, 2), old_rtx, fn, data);
462 6351081 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1) && op2 == XEXP (x, 2))
463 : return x;
464 2185606 : if (op_mode == VOIDmode)
465 2161992 : op_mode = GET_MODE (op0);
466 2185606 : return simplify_gen_ternary (code, mode, op_mode, op0, op1, op2);
467 :
468 89120078 : case RTX_EXTRA:
469 89120078 : if (code == SUBREG)
470 : {
471 619738 : op0 = simplify_replace_fn_rtx (SUBREG_REG (x), old_rtx, fn, data);
472 619738 : if (op0 == SUBREG_REG (x))
473 : return x;
474 131490 : op0 = simplify_gen_subreg (GET_MODE (x), op0,
475 65745 : GET_MODE (SUBREG_REG (x)),
476 65745 : SUBREG_BYTE (x));
477 65745 : return op0 ? op0 : x;
478 : }
479 : break;
480 :
481 63150154 : case RTX_OBJ:
482 63150154 : if (code == MEM)
483 : {
484 10738521 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
485 10738521 : if (op0 == XEXP (x, 0))
486 : return x;
487 160995 : return replace_equiv_address_nv (x, op0);
488 : }
489 52411633 : else if (code == LO_SUM)
490 : {
491 0 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
492 0 : 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 0 : if (GET_CODE (op0) == HIGH)
496 : {
497 0 : rtx base0, base1, offset0, offset1;
498 0 : split_const (XEXP (op0, 0), &base0, &offset0);
499 0 : split_const (op1, &base1, &offset1);
500 0 : if (rtx_equal_p (base0, base1))
501 0 : return op1;
502 : }
503 :
504 0 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
505 : return x;
506 0 : return gen_rtx_LO_SUM (mode, op0, op1);
507 : }
508 : break;
509 :
510 : default:
511 : break;
512 : }
513 :
514 290884339 : newx = x;
515 290884339 : fmt = GET_RTX_FORMAT (code);
516 628332048 : for (i = 0; fmt[i]; i++)
517 337447709 : switch (fmt[i])
518 : {
519 3282566 : case 'E':
520 3282566 : vec = XVEC (x, i);
521 3282566 : newvec = XVEC (newx, i);
522 13718123 : for (j = 0; j < GET_NUM_ELEM (vec); j++)
523 : {
524 10435557 : op = simplify_replace_fn_rtx (RTVEC_ELT (vec, j),
525 : old_rtx, fn, data);
526 10435557 : if (op != RTVEC_ELT (vec, j))
527 : {
528 358278 : if (newvec == vec)
529 : {
530 343842 : newvec = shallow_copy_rtvec (vec);
531 343842 : if (x == newx)
532 343842 : newx = shallow_copy_rtx (x);
533 343842 : XVEC (newx, i) = newvec;
534 : }
535 358278 : RTVEC_ELT (newvec, j) = op;
536 : }
537 : }
538 : break;
539 :
540 80678783 : case 'e':
541 80678783 : if (XEXP (x, i))
542 : {
543 80678733 : op = simplify_replace_fn_rtx (XEXP (x, i), old_rtx, fn, data);
544 80678733 : if (op != XEXP (x, i))
545 : {
546 4545805 : if (x == newx)
547 4542585 : newx = shallow_copy_rtx (x);
548 4545805 : 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 13216812 : simplify_replace_rtx (rtx x, const_rtx old_rtx, rtx new_rtx)
561 : {
562 13216812 : 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 23906041 : simplify_context::simplify_truncation (machine_mode mode, rtx op,
614 : machine_mode op_mode)
615 : {
616 23906041 : unsigned int precision = GET_MODE_UNIT_PRECISION (mode);
617 23906041 : unsigned int op_precision = GET_MODE_UNIT_PRECISION (op_mode);
618 23906041 : scalar_int_mode int_mode, int_op_mode, subreg_mode;
619 :
620 23906041 : gcc_assert (precision <= op_precision);
621 :
622 : /* Optimize truncations of zero and sign extended values. */
623 23906041 : if (GET_CODE (op) == ZERO_EXTEND
624 23906041 : || 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 384143 : machine_mode origmode = GET_MODE (XEXP (op, 0));
633 384143 : if (mode == origmode)
634 : return XEXP (op, 0);
635 27628 : else if (precision <= GET_MODE_UNIT_PRECISION (origmode))
636 10815 : return simplify_gen_unary (TRUNCATE, mode,
637 10815 : XEXP (op, 0), origmode);
638 : else
639 2999 : return simplify_gen_unary (GET_CODE (op), mode,
640 2999 : 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 23521898 : if (1
647 : && (!WORD_REGISTER_OPERATIONS || precision >= BITS_PER_WORD)
648 : && (GET_CODE (op) == PLUS
649 : || GET_CODE (op) == MINUS
650 23521898 : || GET_CODE (op) == MULT))
651 : {
652 1433126 : rtx op0 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0), op_mode);
653 1433126 : if (op0)
654 : {
655 1433126 : rtx op1 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 1), op_mode);
656 1433126 : if (op1)
657 1433126 : 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 22088772 : if ((GET_CODE (op) == LSHIFTRT
665 22088772 : || 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 1913724 : && 2 * precision <= op_precision
671 1913724 : && CONST_INT_P (XEXP (op, 1))
672 1812520 : && GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
673 31 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
674 28 : && UINTVAL (XEXP (op, 1)) < precision)
675 24 : return simplify_gen_binary (ASHIFTRT, mode,
676 24 : 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 22088748 : if ((GET_CODE (op) == LSHIFTRT
682 : || GET_CODE (op) == ASHIFTRT)
683 1913700 : && CONST_INT_P (XEXP (op, 1))
684 1812496 : && GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
685 938 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
686 938 : && UINTVAL (XEXP (op, 1)) < precision)
687 921 : return simplify_gen_binary (LSHIFTRT, mode,
688 921 : 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 22087827 : if (GET_CODE (op) == ASHIFT
694 842764 : && CONST_INT_P (XEXP (op, 1))
695 782959 : && (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
696 782959 : || GET_CODE (XEXP (op, 0)) == SIGN_EXTEND)
697 697 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
698 687 : && UINTVAL (XEXP (op, 1)) < precision)
699 642 : return simplify_gen_binary (ASHIFT, mode,
700 642 : 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 22087185 : if (GET_CODE (op) == AND
706 843039 : && (GET_CODE (XEXP (op, 0)) == LSHIFTRT
707 843039 : || GET_CODE (XEXP (op, 0)) == ASHIFTRT)
708 47267 : && CONST_INT_P (XEXP (XEXP (op, 0), 1))
709 47162 : && CONST_INT_P (XEXP (op, 1)))
710 : {
711 47129 : rtx op0 = (XEXP (XEXP (op, 0), 0));
712 47129 : rtx shift_op = XEXP (XEXP (op, 0), 1);
713 47129 : rtx mask_op = XEXP (op, 1);
714 47129 : unsigned HOST_WIDE_INT shift = UINTVAL (shift_op);
715 47129 : unsigned HOST_WIDE_INT mask = UINTVAL (mask_op);
716 :
717 47129 : if (shift < precision
718 : /* If doing this transform works for an X with all bits set,
719 : it works for any X. */
720 31820 : && ((GET_MODE_MASK (mode) >> shift) & mask)
721 31820 : == ((GET_MODE_MASK (op_mode) >> shift) & mask)
722 6931 : && (op0 = simplify_gen_unary (TRUNCATE, mode, op0, op_mode))
723 54060 : && (op0 = simplify_gen_binary (LSHIFTRT, mode, op0, shift_op)))
724 : {
725 6931 : mask_op = GEN_INT (trunc_int_for_mode (mask, mode));
726 6931 : return simplify_gen_binary (AND, mode, op0, mask_op);
727 : }
728 : }
729 :
730 : /* Turn (truncate:M1 (*_extract:M2 (reg:M3) (len) (pos))) into
731 : (*_extract:M1 (truncate:M1 (reg:M3)) (len) (pos')) if possible. */
732 22080254 : if ((GET_CODE (op) == ZERO_EXTRACT || GET_CODE (op) == SIGN_EXTRACT)
733 1141484 : && precision <= GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (op, 0)))
734 568958 : && CONST_INT_P (XEXP (op, 1))
735 22649212 : && CONST_INT_P (XEXP (op, 2)))
736 : {
737 533634 : rtx op0 = XEXP (op, 0);
738 533634 : unsigned HOST_WIDE_INT len = UINTVAL (XEXP (op, 1));
739 533634 : unsigned HOST_WIDE_INT pos = UINTVAL (XEXP (op, 2));
740 533634 : if (BITS_BIG_ENDIAN && pos >= op_precision - precision)
741 : {
742 : if (GET_MODE (op0) != mode)
743 : op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
744 : if (op0)
745 : {
746 : pos -= op_precision - precision;
747 : return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
748 : XEXP (op, 1), GEN_INT (pos));
749 : }
750 : }
751 533634 : else if (!BITS_BIG_ENDIAN && precision >= len + pos)
752 : {
753 184832 : if (GET_MODE (op0) != mode)
754 140390 : op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
755 140390 : if (op0)
756 184832 : return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
757 184832 : XEXP (op, 1), XEXP (op, 2));
758 : }
759 : }
760 :
761 : /* Recognize a word extraction from a multi-word subreg. */
762 21895422 : if ((GET_CODE (op) == LSHIFTRT
763 21895422 : || GET_CODE (op) == ASHIFTRT)
764 1912779 : && SCALAR_INT_MODE_P (mode)
765 1909562 : && SCALAR_INT_MODE_P (op_mode)
766 2047453 : && precision >= BITS_PER_WORD
767 61775 : && 2 * precision <= op_precision
768 61775 : && CONST_INT_P (XEXP (op, 1))
769 52286 : && (INTVAL (XEXP (op, 1)) & (precision - 1)) == 0
770 1816 : && UINTVAL (XEXP (op, 1)) < op_precision)
771 : {
772 1816 : poly_int64 byte = subreg_lowpart_offset (mode, op_mode);
773 1816 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
774 1816 : return simplify_gen_subreg (mode, XEXP (op, 0), op_mode,
775 : (WORDS_BIG_ENDIAN
776 1816 : ? byte - shifted_bytes
777 1816 : : 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 21893606 : if ((GET_CODE (op) == LSHIFTRT
784 : || GET_CODE (op) == ASHIFTRT)
785 1907746 : && is_a <scalar_int_mode> (mode, &int_mode)
786 21892993 : && is_a <scalar_int_mode> (op_mode, &int_op_mode)
787 1907746 : && MEM_P (XEXP (op, 0))
788 11276 : && CONST_INT_P (XEXP (op, 1))
789 20776 : && INTVAL (XEXP (op, 1)) % GET_MODE_BITSIZE (int_mode) == 0
790 656 : && INTVAL (XEXP (op, 1)) > 0
791 1312 : && INTVAL (XEXP (op, 1)) < GET_MODE_BITSIZE (int_op_mode)
792 656 : && ! mode_dependent_address_p (XEXP (XEXP (op, 0), 0),
793 656 : MEM_ADDR_SPACE (XEXP (op, 0)))
794 656 : && ! MEM_VOLATILE_P (XEXP (op, 0))
795 21893606 : && (GET_MODE_SIZE (int_mode) >= UNITS_PER_WORD
796 : || WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN))
797 : {
798 613 : poly_int64 byte = subreg_lowpart_offset (int_mode, int_op_mode);
799 613 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
800 613 : 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 21892993 : if ((GET_CODE (op) == ABS
809 21892993 : || GET_CODE (op) == NEG)
810 21331 : && (GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
811 21331 : || GET_CODE (XEXP (op, 0)) == ZERO_EXTEND)
812 17 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
813 1 : return simplify_gen_unary (GET_CODE (op), mode,
814 1 : XEXP (XEXP (op, 0), 0), mode);
815 :
816 : /* Simplifications of (truncate:A (subreg:B X 0)). */
817 21892992 : if (GET_CODE (op) == SUBREG
818 21893548 : && is_a <scalar_int_mode> (mode, &int_mode)
819 122019 : && SCALAR_INT_MODE_P (op_mode)
820 122019 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &subreg_mode)
821 22014511 : && subreg_lowpart_p (op))
822 : {
823 : /* (truncate:A (subreg:B (truncate:C X) 0)) is (truncate:A X). */
824 121499 : if (GET_CODE (SUBREG_REG (op)) == TRUNCATE)
825 : {
826 0 : rtx inner = XEXP (SUBREG_REG (op), 0);
827 0 : if (GET_MODE_PRECISION (int_mode)
828 0 : <= GET_MODE_PRECISION (subreg_mode))
829 0 : return simplify_gen_unary (TRUNCATE, int_mode, inner,
830 0 : 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 0 : 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 121499 : if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
841 : {
842 121499 : unsigned int int_op_prec = GET_MODE_PRECISION (int_op_mode);
843 121499 : unsigned int subreg_prec = GET_MODE_PRECISION (subreg_mode);
844 121499 : if (int_op_prec > subreg_prec)
845 : {
846 91218 : if (int_mode == subreg_mode)
847 : return SUBREG_REG (op);
848 63 : if (GET_MODE_PRECISION (int_mode) < subreg_prec)
849 27 : return simplify_gen_unary (TRUNCATE, int_mode,
850 27 : 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 30281 : else if (int_op_prec < subreg_prec
855 30281 : && GET_MODE_PRECISION (int_mode) < int_op_prec)
856 30281 : return simplify_gen_unary (TRUNCATE, int_mode,
857 30281 : SUBREG_REG (op), subreg_mode);
858 : }
859 : }
860 :
861 : /* (truncate:A (truncate:B X)) is (truncate:A X). */
862 21771529 : if (GET_CODE (op) == TRUNCATE)
863 0 : return simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0),
864 0 : 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 21771529 : if (GET_CODE (op) == IOR
869 36316 : && SCALAR_INT_MODE_P (mode)
870 36316 : && SCALAR_INT_MODE_P (op_mode)
871 36316 : && CONST_INT_P (XEXP (op, 1))
872 21780006 : && trunc_int_for_mode (INTVAL (XEXP (op, 1)), mode) == -1)
873 42 : 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 30287034 : simplify_context::simplify_unary_operation (rtx_code code, machine_mode mode,
883 : rtx op, machine_mode op_mode)
884 : {
885 30287034 : rtx trueop, tem;
886 :
887 30287034 : trueop = avoid_constant_pool_reference (op);
888 :
889 30287034 : tem = simplify_const_unary_operation (code, mode, trueop, op_mode);
890 30287034 : if (tem)
891 : return tem;
892 :
893 24843254 : 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 2797 : exact_int_to_float_conversion_p (const_rtx op)
901 : {
902 2797 : 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 2797 : if (op0_mode == VOIDmode)
906 : return false;
907 5592 : int out_bits = significand_size (GET_MODE_INNER (GET_MODE (op)));
908 2796 : int in_prec = GET_MODE_UNIT_PRECISION (op0_mode);
909 2796 : int in_bits = in_prec;
910 2796 : if (HWI_COMPUTABLE_MODE_P (op0_mode))
911 : {
912 2706 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (XEXP (op, 0), op0_mode);
913 2706 : if (GET_CODE (op) == FLOAT)
914 2585 : in_bits -= num_sign_bit_copies (XEXP (op, 0), op0_mode);
915 121 : else if (GET_CODE (op) == UNSIGNED_FLOAT)
916 121 : in_bits = wi::min_precision (wi::uhwi (nonzero, in_prec), UNSIGNED);
917 : else
918 0 : gcc_unreachable ();
919 2706 : in_bits -= wi::ctz (wi::uhwi (nonzero, in_prec));
920 : }
921 2796 : 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 24843254 : simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode,
928 : rtx op)
929 : {
930 24843254 : enum rtx_code reversed;
931 24843254 : rtx temp, elt, base, step;
932 24843254 : scalar_int_mode inner, int_mode, op_mode, op0_mode;
933 :
934 24843254 : switch (code)
935 : {
936 2410798 : case NOT:
937 : /* (not (not X)) == X. */
938 2410798 : if (GET_CODE (op) == NOT)
939 3188 : 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 2407610 : if (COMPARISON_P (op)
944 17215 : && ((SCALAR_INT_MODE_P (mode) && STORE_FLAG_VALUE == -1)
945 : #ifdef VECTOR_STORE_FLAG_VALUE
946 17215 : || (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
947 : && VECTOR_STORE_FLAG_VALUE (mode) == constm1_rtx)
948 : #endif
949 11946 : || mode == BImode)
950 2412879 : && ((reversed = reversed_comparison_code (op, NULL)) != UNKNOWN))
951 5104 : return simplify_gen_relational (reversed, mode, VOIDmode,
952 5104 : XEXP (op, 0), XEXP (op, 1));
953 :
954 : /* (not (neg (eq X Y))) is (neg (ne X Y)), etc. if the result of
955 : the comparison is one. */
956 2402506 : if (GET_CODE (op) == NEG
957 76894 : && COMPARISON_P (XEXP (op, 0))
958 6 : && ((SCALAR_INT_MODE_P (mode) && STORE_FLAG_VALUE == 1)
959 : #ifdef VECTOR_STORE_FLAG_VALUE
960 0 : || (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
961 0 : && VECTOR_STORE_FLAG_VALUE (mode) == const1_rtx)
962 : #endif
963 : )
964 2402512 : && ((reversed = reversed_comparison_code (XEXP (op, 0), NULL))
965 : != UNKNOWN))
966 : {
967 12 : temp = simplify_gen_relational (reversed, mode, VOIDmode,
968 : XEXP (XEXP (op, 0), 0),
969 6 : XEXP (XEXP (op, 0), 1));
970 6 : return simplify_gen_unary (NEG, mode, temp, mode);
971 : }
972 :
973 : /* (not (plus X -1)) can become (neg X). */
974 2402500 : if (GET_CODE (op) == PLUS
975 281361 : && XEXP (op, 1) == constm1_rtx)
976 4617 : return simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
977 :
978 : /* Similarly, (not (neg X)) is (plus X -1). Only do this for
979 : modes that have CONSTM1_RTX, i.e. MODE_INT, MODE_PARTIAL_INT
980 : and MODE_VECTOR_INT. */
981 2397883 : if (GET_CODE (op) == NEG && CONSTM1_RTX (mode))
982 76885 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
983 76885 : CONSTM1_RTX (mode));
984 :
985 : /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
986 2320998 : if (GET_CODE (op) == XOR
987 18693 : && CONST_INT_P (XEXP (op, 1))
988 2325548 : && (temp = simplify_unary_operation (NOT, mode,
989 : XEXP (op, 1), mode)) != 0)
990 4550 : return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
991 :
992 : /* (not (plus X C)) for signbit C is (xor X D) with D = ~C. */
993 2316448 : if (GET_CODE (op) == PLUS
994 276744 : && CONST_INT_P (XEXP (op, 1))
995 161211 : && mode_signbit_p (mode, XEXP (op, 1))
996 2320353 : && (temp = simplify_unary_operation (NOT, mode,
997 : XEXP (op, 1), mode)) != 0)
998 3905 : return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
999 :
1000 :
1001 : /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for
1002 : operands other than 1, but that is not valid. We could do a
1003 : similar simplification for (not (lshiftrt C X)) where C is
1004 : just the sign bit, but this doesn't seem common enough to
1005 : bother with. */
1006 2312543 : if (GET_CODE (op) == ASHIFT
1007 31902 : && XEXP (op, 0) == const1_rtx)
1008 : {
1009 1143 : temp = simplify_gen_unary (NOT, mode, const1_rtx, mode);
1010 1143 : return simplify_gen_binary (ROTATE, mode, temp, XEXP (op, 1));
1011 : }
1012 :
1013 : /* (not (ashiftrt foo C)) where C is the number of bits in FOO
1014 : minus 1 is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1,
1015 : so we can perform the above simplification. */
1016 2311400 : if (STORE_FLAG_VALUE == -1
1017 : && is_a <scalar_int_mode> (mode, &int_mode)
1018 : && GET_CODE (op) == ASHIFTRT
1019 : && CONST_INT_P (XEXP (op, 1))
1020 : && INTVAL (XEXP (op, 1)) == GET_MODE_PRECISION (int_mode) - 1)
1021 : return simplify_gen_relational (GE, int_mode, VOIDmode,
1022 : XEXP (op, 0), const0_rtx);
1023 :
1024 :
1025 2311400 : if (partial_subreg_p (op)
1026 609998 : && subreg_lowpart_p (op)
1027 609686 : && GET_CODE (SUBREG_REG (op)) == ASHIFT
1028 628241 : && XEXP (SUBREG_REG (op), 0) == const1_rtx)
1029 : {
1030 147 : machine_mode inner_mode = GET_MODE (SUBREG_REG (op));
1031 147 : rtx x;
1032 :
1033 147 : x = gen_rtx_ROTATE (inner_mode,
1034 : simplify_gen_unary (NOT, inner_mode, const1_rtx,
1035 : inner_mode),
1036 : XEXP (SUBREG_REG (op), 1));
1037 147 : temp = rtl_hooks.gen_lowpart_no_emit (mode, x);
1038 147 : if (temp)
1039 : return temp;
1040 : }
1041 :
1042 : /* Apply De Morgan's laws to reduce number of patterns for machines
1043 : with negating logical insns (and-not, nand, etc.). If result has
1044 : only one NOT, put it first, since that is how the patterns are
1045 : coded. */
1046 2311253 : if (GET_CODE (op) == IOR || GET_CODE (op) == AND)
1047 : {
1048 14412 : rtx in1 = XEXP (op, 0), in2 = XEXP (op, 1);
1049 14412 : machine_mode op_mode;
1050 :
1051 14412 : op_mode = GET_MODE (in1);
1052 14412 : in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
1053 :
1054 14412 : op_mode = GET_MODE (in2);
1055 14412 : if (op_mode == VOIDmode)
1056 5822 : op_mode = mode;
1057 14412 : in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
1058 :
1059 14412 : if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
1060 : std::swap (in1, in2);
1061 :
1062 28824 : return gen_rtx_fmt_ee (GET_CODE (op) == IOR ? AND : IOR,
1063 : mode, in1, in2);
1064 : }
1065 :
1066 : /* (not (bswap x)) -> (bswap (not x)). */
1067 2296841 : if (GET_CODE (op) == BSWAP || GET_CODE (op) == BITREVERSE)
1068 : {
1069 0 : rtx x = simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1070 0 : return simplify_gen_unary (GET_CODE (op), mode, x, mode);
1071 : }
1072 : break;
1073 :
1074 1850045 : case NEG:
1075 : /* (neg (neg X)) == X. */
1076 1850045 : if (GET_CODE (op) == NEG)
1077 6850 : return XEXP (op, 0);
1078 :
1079 : /* (neg (x ? (neg y) : y)) == !x ? (neg y) : y.
1080 : If comparison is not reversible use
1081 : x ? y : (neg y). */
1082 1843195 : if (GET_CODE (op) == IF_THEN_ELSE)
1083 : {
1084 3354 : rtx cond = XEXP (op, 0);
1085 3354 : rtx true_rtx = XEXP (op, 1);
1086 3354 : rtx false_rtx = XEXP (op, 2);
1087 :
1088 3354 : if ((GET_CODE (true_rtx) == NEG
1089 0 : && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
1090 3354 : || (GET_CODE (false_rtx) == NEG
1091 0 : && rtx_equal_p (XEXP (false_rtx, 0), true_rtx)))
1092 : {
1093 0 : if (reversed_comparison_code (cond, NULL) != UNKNOWN)
1094 0 : temp = reversed_comparison (cond, mode);
1095 : else
1096 : {
1097 : temp = cond;
1098 : std::swap (true_rtx, false_rtx);
1099 : }
1100 0 : return simplify_gen_ternary (IF_THEN_ELSE, mode,
1101 0 : mode, temp, true_rtx, false_rtx);
1102 : }
1103 : }
1104 :
1105 : /* (neg (plus X 1)) can become (not X). */
1106 1843195 : if (GET_CODE (op) == PLUS
1107 143331 : && XEXP (op, 1) == const1_rtx)
1108 57741 : return simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1109 :
1110 : /* Similarly, (neg (not X)) is (plus X 1). */
1111 1785454 : if (GET_CODE (op) == NOT)
1112 267 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
1113 267 : CONST1_RTX (mode));
1114 :
1115 : /* (neg (minus X Y)) can become (minus Y X). This transformation
1116 : isn't safe for modes with signed zeros, since if X and Y are
1117 : both +0, (minus Y X) is the same as (minus X Y). If the
1118 : rounding mode is towards +infinity (or -infinity) then the two
1119 : expressions will be rounded differently. */
1120 1785187 : if (GET_CODE (op) == MINUS
1121 25254 : && !HONOR_SIGNED_ZEROS (mode)
1122 1809031 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1123 23844 : return simplify_gen_binary (MINUS, mode, XEXP (op, 1), XEXP (op, 0));
1124 :
1125 1761343 : if (GET_CODE (op) == PLUS
1126 85590 : && !HONOR_SIGNED_ZEROS (mode)
1127 1846497 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1128 : {
1129 : /* (neg (plus A C)) is simplified to (minus -C A). */
1130 85154 : if (CONST_SCALAR_INT_P (XEXP (op, 1))
1131 5187 : || CONST_DOUBLE_AS_FLOAT_P (XEXP (op, 1)))
1132 : {
1133 79967 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 1), mode);
1134 79967 : if (temp)
1135 79967 : return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 0));
1136 : }
1137 :
1138 : /* (neg (plus A B)) is canonicalized to (minus (neg A) B). */
1139 5187 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
1140 5187 : return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 1));
1141 : }
1142 :
1143 : /* (neg (mult A B)) becomes (mult A (neg B)).
1144 : This works even for floating-point values. */
1145 1676189 : if (GET_CODE (op) == MULT
1146 1676189 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1147 : {
1148 26509 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 1), mode);
1149 26509 : return simplify_gen_binary (MULT, mode, XEXP (op, 0), temp);
1150 : }
1151 :
1152 : /* NEG commutes with ASHIFT since it is multiplication. Only do
1153 : this if we can then eliminate the NEG (e.g., if the operand
1154 : is a constant). */
1155 1649680 : if (GET_CODE (op) == ASHIFT)
1156 : {
1157 52091 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 0), mode);
1158 52091 : if (temp)
1159 11253 : return simplify_gen_binary (ASHIFT, mode, temp, XEXP (op, 1));
1160 : }
1161 :
1162 : /* (neg (ashiftrt X C)) can be replaced by (lshiftrt X C) when
1163 : C is equal to the width of MODE minus 1. */
1164 1638427 : if (GET_CODE (op) == ASHIFTRT
1165 33670 : && CONST_INT_P (XEXP (op, 1))
1166 1705659 : && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1167 912 : return simplify_gen_binary (LSHIFTRT, mode,
1168 912 : XEXP (op, 0), XEXP (op, 1));
1169 :
1170 : /* (neg (lshiftrt X C)) can be replaced by (ashiftrt X C) when
1171 : C is equal to the width of MODE minus 1. */
1172 1637515 : if (GET_CODE (op) == LSHIFTRT
1173 9172 : && CONST_INT_P (XEXP (op, 1))
1174 1655715 : && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1175 3619 : return simplify_gen_binary (ASHIFTRT, mode,
1176 3619 : XEXP (op, 0), XEXP (op, 1));
1177 :
1178 : /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
1179 1633896 : if (GET_CODE (op) == XOR
1180 10894 : && XEXP (op, 1) == const1_rtx
1181 1633985 : && nonzero_bits (XEXP (op, 0), mode) == 1)
1182 34 : return plus_constant (mode, XEXP (op, 0), -1);
1183 :
1184 : /* (neg (lt x 0)) is (ashiftrt X C) if STORE_FLAG_VALUE is 1. */
1185 : /* (neg (lt x 0)) is (lshiftrt X C) if STORE_FLAG_VALUE is -1. */
1186 1633862 : if (GET_CODE (op) == LT
1187 3196 : && XEXP (op, 1) == const0_rtx
1188 1636222 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op, 0)), &inner))
1189 : {
1190 544 : int_mode = as_a <scalar_int_mode> (mode);
1191 544 : int isize = GET_MODE_PRECISION (inner);
1192 544 : if (STORE_FLAG_VALUE == 1)
1193 : {
1194 544 : temp = simplify_gen_binary (ASHIFTRT, inner, XEXP (op, 0),
1195 : gen_int_shift_amount (inner,
1196 544 : isize - 1));
1197 544 : if (int_mode == inner)
1198 : return temp;
1199 315 : if (GET_MODE_PRECISION (int_mode) > isize)
1200 187 : return simplify_gen_unary (SIGN_EXTEND, int_mode, temp, inner);
1201 128 : return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1202 : }
1203 : else if (STORE_FLAG_VALUE == -1)
1204 : {
1205 : temp = simplify_gen_binary (LSHIFTRT, inner, XEXP (op, 0),
1206 : gen_int_shift_amount (inner,
1207 : isize - 1));
1208 : if (int_mode == inner)
1209 : return temp;
1210 : if (GET_MODE_PRECISION (int_mode) > isize)
1211 : return simplify_gen_unary (ZERO_EXTEND, int_mode, temp, inner);
1212 : return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1213 : }
1214 : }
1215 :
1216 1633318 : if (vec_series_p (op, &base, &step))
1217 : {
1218 : /* Only create a new series if we can simplify both parts. In other
1219 : cases this isn't really a simplification, and it's not necessarily
1220 : a win to replace a vector operation with a scalar operation. */
1221 276 : scalar_mode inner_mode = GET_MODE_INNER (mode);
1222 276 : base = simplify_unary_operation (NEG, inner_mode, base, inner_mode);
1223 276 : if (base)
1224 : {
1225 276 : step = simplify_unary_operation (NEG, inner_mode,
1226 : step, inner_mode);
1227 276 : if (step)
1228 276 : return gen_vec_series (mode, base, step);
1229 : }
1230 : }
1231 : break;
1232 :
1233 2298052 : case TRUNCATE:
1234 : /* Don't optimize (lshiftrt (mult ...)) as it would interfere
1235 : with the umulXi3_highpart patterns. */
1236 2298052 : if (GET_CODE (op) == LSHIFTRT
1237 20159 : && GET_CODE (XEXP (op, 0)) == MULT)
1238 : break;
1239 :
1240 2290884 : if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
1241 : {
1242 12 : if (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op)))
1243 : {
1244 12 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1245 12 : if (temp)
1246 : return temp;
1247 : }
1248 : /* We can't handle truncation to a partial integer mode here
1249 : because we don't know the real bitsize of the partial
1250 : integer mode. */
1251 : break;
1252 : }
1253 :
1254 2290872 : if (GET_MODE (op) != VOIDmode)
1255 : {
1256 2290872 : temp = simplify_truncation (mode, op, GET_MODE (op));
1257 2290872 : if (temp)
1258 : return temp;
1259 : }
1260 :
1261 : /* If we know that the value is already truncated, we can
1262 : replace the TRUNCATE with a SUBREG. */
1263 1985711 : if (known_eq (GET_MODE_NUNITS (mode), 1)
1264 1985711 : && (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op))
1265 0 : || truncated_to_mode (mode, op)))
1266 : {
1267 1973142 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1268 1973142 : if (temp)
1269 : return temp;
1270 : }
1271 :
1272 : /* A truncate of a comparison can be replaced with a subreg if
1273 : STORE_FLAG_VALUE permits. This is like the previous test,
1274 : but it works even if the comparison is done in a mode larger
1275 : than HOST_BITS_PER_WIDE_INT. */
1276 12742 : if (HWI_COMPUTABLE_MODE_P (mode)
1277 173 : && COMPARISON_P (op)
1278 0 : && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
1279 12742 : && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op)))
1280 : {
1281 0 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1282 0 : if (temp)
1283 : return temp;
1284 : }
1285 :
1286 : /* A truncate of a memory is just loading the low part of the memory
1287 : if we are not changing the meaning of the address. */
1288 12742 : if (GET_CODE (op) == MEM
1289 285 : && !VECTOR_MODE_P (mode)
1290 169 : && !MEM_VOLATILE_P (op)
1291 12905 : && !mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op)))
1292 : {
1293 163 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1294 163 : if (temp)
1295 : return temp;
1296 : }
1297 :
1298 : /* Check for useless truncation. */
1299 12742 : if (GET_MODE (op) == mode)
1300 : return op;
1301 : break;
1302 :
1303 174656 : case FLOAT_TRUNCATE:
1304 : /* Check for useless truncation. */
1305 174656 : if (GET_MODE (op) == mode)
1306 : return op;
1307 :
1308 174656 : if (DECIMAL_FLOAT_MODE_P (mode))
1309 : break;
1310 :
1311 : /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
1312 174502 : if (GET_CODE (op) == FLOAT_EXTEND
1313 5 : && GET_MODE (XEXP (op, 0)) == mode)
1314 : return XEXP (op, 0);
1315 :
1316 : /* (float_truncate:SF (float_truncate:DF foo:XF))
1317 : = (float_truncate:SF foo:XF).
1318 : This may eliminate double rounding, so it is unsafe.
1319 :
1320 : (float_truncate:SF (float_extend:XF foo:DF))
1321 : = (float_truncate:SF foo:DF).
1322 :
1323 : (float_truncate:DF (float_extend:XF foo:SF))
1324 : = (float_extend:DF foo:SF). */
1325 174500 : if ((GET_CODE (op) == FLOAT_TRUNCATE
1326 145 : && flag_unsafe_math_optimizations)
1327 174496 : || GET_CODE (op) == FLOAT_EXTEND)
1328 14 : return simplify_gen_unary (GET_MODE_UNIT_SIZE (GET_MODE (XEXP (op, 0)))
1329 7 : > GET_MODE_UNIT_SIZE (mode)
1330 : ? FLOAT_TRUNCATE : FLOAT_EXTEND,
1331 : mode,
1332 14 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1333 :
1334 : /* (float_truncate (float x)) is (float x) */
1335 174493 : if ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1336 174493 : && (flag_unsafe_math_optimizations
1337 1413 : || exact_int_to_float_conversion_p (op)))
1338 1412 : return simplify_gen_unary (GET_CODE (op), mode,
1339 : XEXP (op, 0),
1340 1412 : GET_MODE (XEXP (op, 0)));
1341 :
1342 : /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
1343 : (OP:SF foo:SF) if OP is NEG or ABS. */
1344 173081 : if ((GET_CODE (op) == ABS
1345 173081 : || GET_CODE (op) == NEG)
1346 210 : && GET_CODE (XEXP (op, 0)) == FLOAT_EXTEND
1347 28 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
1348 28 : return simplify_gen_unary (GET_CODE (op), mode,
1349 28 : XEXP (XEXP (op, 0), 0), mode);
1350 :
1351 : /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
1352 : is (float_truncate:SF x). */
1353 173053 : if (GET_CODE (op) == SUBREG
1354 307 : && subreg_lowpart_p (op)
1355 173357 : && GET_CODE (SUBREG_REG (op)) == FLOAT_TRUNCATE)
1356 : return SUBREG_REG (op);
1357 : break;
1358 :
1359 601566 : case FLOAT_EXTEND:
1360 : /* Check for useless extension. */
1361 601566 : if (GET_MODE (op) == mode)
1362 : return op;
1363 :
1364 601566 : if (DECIMAL_FLOAT_MODE_P (mode))
1365 : break;
1366 :
1367 : /* (float_extend (float_extend x)) is (float_extend x)
1368 :
1369 : (float_extend (float x)) is (float x) assuming that double
1370 : rounding can't happen.
1371 : */
1372 601463 : if (GET_CODE (op) == FLOAT_EXTEND
1373 601463 : || ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1374 1384 : && exact_int_to_float_conversion_p (op)))
1375 551 : return simplify_gen_unary (GET_CODE (op), mode,
1376 : XEXP (op, 0),
1377 551 : GET_MODE (XEXP (op, 0)));
1378 :
1379 : break;
1380 :
1381 333005 : case ABS:
1382 : /* (abs (neg <foo>)) -> (abs <foo>) */
1383 333005 : if (GET_CODE (op) == NEG)
1384 18 : return simplify_gen_unary (ABS, mode, XEXP (op, 0),
1385 18 : GET_MODE (XEXP (op, 0)));
1386 :
1387 : /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
1388 : do nothing. */
1389 332987 : if (GET_MODE (op) == VOIDmode)
1390 : break;
1391 :
1392 : /* If operand is something known to be positive, ignore the ABS. */
1393 332987 : if (val_signbit_known_clear_p (GET_MODE (op),
1394 : nonzero_bits (op, GET_MODE (op))))
1395 : return op;
1396 :
1397 : /* Using nonzero_bits doesn't (currently) work for modes wider than
1398 : HOST_WIDE_INT, so the following transformations help simplify
1399 : ABS for TImode and wider. */
1400 332841 : switch (GET_CODE (op))
1401 : {
1402 : case ABS:
1403 : case CLRSB:
1404 : case FFS:
1405 : case PARITY:
1406 : case POPCOUNT:
1407 : case SS_ABS:
1408 : return op;
1409 :
1410 0 : case LSHIFTRT:
1411 0 : if (CONST_INT_P (XEXP (op, 1))
1412 0 : && INTVAL (XEXP (op, 1)) > 0
1413 332841 : && is_a <scalar_int_mode> (mode, &int_mode)
1414 0 : && INTVAL (XEXP (op, 1)) < GET_MODE_PRECISION (int_mode))
1415 : return op;
1416 : break;
1417 :
1418 : default:
1419 : break;
1420 : }
1421 :
1422 : /* If operand is known to be only -1 or 0, convert ABS to NEG. */
1423 332841 : if (is_a <scalar_int_mode> (mode, &int_mode)
1424 55194 : && (num_sign_bit_copies (op, int_mode)
1425 55194 : == GET_MODE_PRECISION (int_mode)))
1426 14 : return gen_rtx_NEG (int_mode, op);
1427 :
1428 : break;
1429 :
1430 0 : case FFS:
1431 : /* (ffs (*_extend <X>)) = (*_extend (ffs <X>)). */
1432 0 : if (GET_CODE (op) == SIGN_EXTEND
1433 0 : || GET_CODE (op) == ZERO_EXTEND)
1434 : {
1435 0 : temp = simplify_gen_unary (FFS, GET_MODE (XEXP (op, 0)),
1436 0 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1437 0 : return simplify_gen_unary (GET_CODE (op), mode, temp,
1438 0 : GET_MODE (temp));
1439 : }
1440 : break;
1441 :
1442 3441 : case POPCOUNT:
1443 3441 : switch (GET_CODE (op))
1444 : {
1445 0 : case BSWAP:
1446 0 : case BITREVERSE:
1447 : /* (popcount (bswap <X>)) = (popcount <X>). */
1448 0 : return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1449 0 : GET_MODE (XEXP (op, 0)));
1450 :
1451 44 : case ZERO_EXTEND:
1452 : /* (popcount (zero_extend <X>)) = (zero_extend (popcount <X>)). */
1453 88 : temp = simplify_gen_unary (POPCOUNT, GET_MODE (XEXP (op, 0)),
1454 44 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1455 44 : return simplify_gen_unary (ZERO_EXTEND, mode, temp,
1456 44 : GET_MODE (temp));
1457 :
1458 0 : case ROTATE:
1459 0 : case ROTATERT:
1460 : /* Rotations don't affect popcount. */
1461 0 : if (!side_effects_p (XEXP (op, 1)))
1462 0 : return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1463 0 : GET_MODE (XEXP (op, 0)));
1464 : break;
1465 :
1466 : default:
1467 : break;
1468 : }
1469 : break;
1470 :
1471 0 : case PARITY:
1472 0 : switch (GET_CODE (op))
1473 : {
1474 0 : case NOT:
1475 0 : case BSWAP:
1476 0 : case BITREVERSE:
1477 0 : return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1478 0 : GET_MODE (XEXP (op, 0)));
1479 :
1480 0 : case ZERO_EXTEND:
1481 0 : case SIGN_EXTEND:
1482 0 : temp = simplify_gen_unary (PARITY, GET_MODE (XEXP (op, 0)),
1483 0 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1484 0 : return simplify_gen_unary (GET_CODE (op), mode, temp,
1485 0 : GET_MODE (temp));
1486 :
1487 0 : case ROTATE:
1488 0 : case ROTATERT:
1489 : /* Rotations don't affect parity. */
1490 0 : if (!side_effects_p (XEXP (op, 1)))
1491 0 : return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1492 0 : GET_MODE (XEXP (op, 0)));
1493 : break;
1494 :
1495 : case PARITY:
1496 : /* (parity (parity x)) -> parity (x). */
1497 : return op;
1498 :
1499 : default:
1500 : break;
1501 : }
1502 : break;
1503 :
1504 32335 : case BSWAP:
1505 : /* (bswap (bswap x)) -> x. */
1506 32335 : if (GET_CODE (op) == BSWAP)
1507 184 : return XEXP (op, 0);
1508 : /* Canonicalize (bswap (bitreverse x)) as (bitreverse (bswap x)). */
1509 32151 : if (GET_CODE (op) == BITREVERSE)
1510 0 : return simplify_gen_unary (BITREVERSE, mode,
1511 : simplify_gen_unary (BSWAP, mode,
1512 : XEXP (op, 0), mode),
1513 0 : mode);
1514 : break;
1515 :
1516 0 : case BITREVERSE:
1517 : /* (bitreverse (bitreverse x)) -> x. */
1518 0 : if (GET_CODE (op) == BITREVERSE)
1519 0 : return XEXP (op, 0);
1520 : break;
1521 :
1522 959297 : case FLOAT:
1523 : /* (float (sign_extend <X>)) = (float <X>). */
1524 959297 : if (GET_CODE (op) == SIGN_EXTEND)
1525 9706 : return simplify_gen_unary (FLOAT, mode, XEXP (op, 0),
1526 9706 : GET_MODE (XEXP (op, 0)));
1527 : break;
1528 :
1529 3118586 : case SIGN_EXTEND:
1530 : /* Check for useless extension. */
1531 3118586 : if (GET_MODE (op) == mode)
1532 : return op;
1533 :
1534 : /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
1535 : becomes just the MINUS if its mode is MODE. This allows
1536 : folding switch statements on machines using casesi (such as
1537 : the VAX). */
1538 3118546 : if (GET_CODE (op) == TRUNCATE
1539 62 : && GET_MODE (XEXP (op, 0)) == mode
1540 62 : && GET_CODE (XEXP (op, 0)) == MINUS
1541 0 : && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
1542 0 : && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
1543 : return XEXP (op, 0);
1544 :
1545 : /* Extending a widening multiplication should be canonicalized to
1546 : a wider widening multiplication. */
1547 3118546 : if (GET_CODE (op) == MULT)
1548 : {
1549 67787 : rtx lhs = XEXP (op, 0);
1550 67787 : rtx rhs = XEXP (op, 1);
1551 67787 : enum rtx_code lcode = GET_CODE (lhs);
1552 67787 : enum rtx_code rcode = GET_CODE (rhs);
1553 :
1554 : /* Widening multiplies usually extend both operands, but sometimes
1555 : they use a shift to extract a portion of a register. */
1556 67787 : if ((lcode == SIGN_EXTEND
1557 67658 : || (lcode == ASHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1558 888 : && (rcode == SIGN_EXTEND
1559 868 : || (rcode == ASHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1560 : {
1561 105 : machine_mode lmode = GET_MODE (lhs);
1562 105 : machine_mode rmode = GET_MODE (rhs);
1563 105 : int bits;
1564 :
1565 105 : if (lcode == ASHIFTRT)
1566 : /* Number of bits not shifted off the end. */
1567 89 : bits = (GET_MODE_UNIT_PRECISION (lmode)
1568 89 : - INTVAL (XEXP (lhs, 1)));
1569 : else /* lcode == SIGN_EXTEND */
1570 : /* Size of inner mode. */
1571 32 : bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1572 :
1573 105 : if (rcode == ASHIFTRT)
1574 85 : bits += (GET_MODE_UNIT_PRECISION (rmode)
1575 85 : - INTVAL (XEXP (rhs, 1)));
1576 : else /* rcode == SIGN_EXTEND */
1577 40 : bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1578 :
1579 : /* We can only widen multiplies if the result is mathematiclly
1580 : equivalent. I.e. if overflow was impossible. */
1581 210 : if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1582 48 : return simplify_gen_binary
1583 48 : (MULT, mode,
1584 : simplify_gen_unary (SIGN_EXTEND, mode, lhs, lmode),
1585 48 : simplify_gen_unary (SIGN_EXTEND, mode, rhs, rmode));
1586 : }
1587 : }
1588 :
1589 : /* Check for a sign extension of a subreg of a promoted
1590 : variable, where the promotion is sign-extended, and the
1591 : target mode is the same as the variable's promotion. */
1592 3118498 : if (GET_CODE (op) == SUBREG
1593 214197 : && SUBREG_PROMOTED_VAR_P (op)
1594 3123716 : && SUBREG_PROMOTED_SIGNED_P (op))
1595 : {
1596 0 : rtx subreg = SUBREG_REG (op);
1597 0 : machine_mode subreg_mode = GET_MODE (subreg);
1598 0 : if (!paradoxical_subreg_p (mode, subreg_mode))
1599 : {
1600 0 : temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1601 0 : if (temp)
1602 : {
1603 : /* Preserve SUBREG_PROMOTED_VAR_P. */
1604 0 : if (partial_subreg_p (temp))
1605 : {
1606 0 : SUBREG_PROMOTED_VAR_P (temp) = 1;
1607 0 : SUBREG_PROMOTED_SET (temp, SRP_SIGNED);
1608 : }
1609 : return temp;
1610 : }
1611 : }
1612 : else
1613 : /* Sign-extending a sign-extended subreg. */
1614 0 : return simplify_gen_unary (SIGN_EXTEND, mode,
1615 0 : subreg, subreg_mode);
1616 : }
1617 :
1618 : /* (sign_extend:M (sign_extend:N <X>)) is (sign_extend:M <X>).
1619 : (sign_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1620 3118498 : if (GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND)
1621 : {
1622 20106 : gcc_assert (GET_MODE_UNIT_PRECISION (mode)
1623 : > GET_MODE_UNIT_PRECISION (GET_MODE (op)));
1624 6702 : return simplify_gen_unary (GET_CODE (op), mode, XEXP (op, 0),
1625 6702 : GET_MODE (XEXP (op, 0)));
1626 : }
1627 :
1628 : /* (sign_extend:M (ashiftrt:N (ashift <X> (const_int I)) (const_int I)))
1629 : is (sign_extend:M (subreg:O <X>)) if there is mode with
1630 : GET_MODE_BITSIZE (N) - I bits.
1631 : (sign_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1632 : is similarly (zero_extend:M (subreg:O <X>)). */
1633 3111796 : if ((GET_CODE (op) == ASHIFTRT || GET_CODE (op) == LSHIFTRT)
1634 90797 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1635 3114528 : && is_a <scalar_int_mode> (mode, &int_mode)
1636 5355 : && CONST_INT_P (XEXP (op, 1))
1637 5355 : && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1638 3117022 : && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1639 5226 : GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1640 : {
1641 5226 : scalar_int_mode tmode;
1642 5226 : gcc_assert (GET_MODE_PRECISION (int_mode)
1643 : > GET_MODE_PRECISION (op_mode));
1644 5226 : if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1645 7829 : - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1646 : {
1647 2623 : rtx inner =
1648 2623 : rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1649 2623 : if (inner)
1650 2623 : return simplify_gen_unary (GET_CODE (op) == ASHIFTRT
1651 : ? SIGN_EXTEND : ZERO_EXTEND,
1652 2623 : int_mode, inner, tmode);
1653 : }
1654 : }
1655 :
1656 : /* (sign_extend:M (lshiftrt:N <X> (const_int I))) is better as
1657 : (zero_extend:M (lshiftrt:N <X> (const_int I))) if I is not 0. */
1658 3109173 : if (GET_CODE (op) == LSHIFTRT
1659 167 : && CONST_INT_P (XEXP (op, 1))
1660 167 : && XEXP (op, 1) != const0_rtx)
1661 167 : return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1662 :
1663 : /* (sign_extend:M (truncate:N (lshiftrt:O <X> (const_int I)))) where
1664 : I is GET_MODE_PRECISION(O) - GET_MODE_PRECISION(N), simplifies to
1665 : (ashiftrt:M <X> (const_int I)) if modes M and O are the same, and
1666 : (truncate:M (ashiftrt:O <X> (const_int I))) if M is narrower than
1667 : O, and (sign_extend:M (ashiftrt:O <X> (const_int I))) if M is
1668 : wider than O. */
1669 3109006 : if (GET_CODE (op) == TRUNCATE
1670 62 : && GET_CODE (XEXP (op, 0)) == LSHIFTRT
1671 0 : && CONST_INT_P (XEXP (XEXP (op, 0), 1)))
1672 : {
1673 0 : scalar_int_mode m_mode, n_mode, o_mode;
1674 0 : rtx old_shift = XEXP (op, 0);
1675 0 : if (is_a <scalar_int_mode> (mode, &m_mode)
1676 0 : && is_a <scalar_int_mode> (GET_MODE (op), &n_mode)
1677 0 : && is_a <scalar_int_mode> (GET_MODE (old_shift), &o_mode)
1678 0 : && GET_MODE_PRECISION (o_mode) - GET_MODE_PRECISION (n_mode)
1679 0 : == INTVAL (XEXP (old_shift, 1)))
1680 : {
1681 0 : rtx new_shift = simplify_gen_binary (ASHIFTRT,
1682 : GET_MODE (old_shift),
1683 : XEXP (old_shift, 0),
1684 : XEXP (old_shift, 1));
1685 0 : if (GET_MODE_PRECISION (m_mode) > GET_MODE_PRECISION (o_mode))
1686 0 : return simplify_gen_unary (SIGN_EXTEND, mode, new_shift,
1687 0 : GET_MODE (new_shift));
1688 0 : if (mode != GET_MODE (new_shift))
1689 0 : return simplify_gen_unary (TRUNCATE, mode, new_shift,
1690 0 : GET_MODE (new_shift));
1691 : return new_shift;
1692 : }
1693 : }
1694 :
1695 : /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when
1696 : we know the sign bit of OP must be clear. */
1697 3109006 : if (val_signbit_known_clear_p (GET_MODE (op),
1698 3109006 : nonzero_bits (op, GET_MODE (op))))
1699 39559 : return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1700 :
1701 : /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1702 3069447 : if (GET_CODE (op) == SUBREG
1703 213911 : && subreg_lowpart_p (op)
1704 213801 : && GET_MODE (SUBREG_REG (op)) == mode
1705 3251750 : && is_a <scalar_int_mode> (mode, &int_mode)
1706 189081 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1707 189081 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1708 187288 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1709 3256735 : && (nonzero_bits (SUBREG_REG (op), mode)
1710 187288 : & ~(GET_MODE_MASK (op_mode) >> 1)) == 0)
1711 6778 : return SUBREG_REG (op);
1712 :
1713 : #if defined(POINTERS_EXTEND_UNSIGNED)
1714 : /* As we do not know which address space the pointer is referring to,
1715 : we can do this only if the target does not support different pointer
1716 : or address modes depending on the address space. */
1717 3062669 : if (target_default_pointer_address_modes_p ()
1718 : && ! POINTERS_EXTEND_UNSIGNED
1719 : && mode == Pmode && GET_MODE (op) == ptr_mode
1720 : && (CONSTANT_P (op)
1721 : || (GET_CODE (op) == SUBREG
1722 : && REG_P (SUBREG_REG (op))
1723 : && REG_POINTER (SUBREG_REG (op))
1724 : && GET_MODE (SUBREG_REG (op)) == Pmode))
1725 : && !targetm.have_ptr_extend ())
1726 : {
1727 : temp
1728 : = convert_memory_address_addr_space_1 (Pmode, op,
1729 : ADDR_SPACE_GENERIC, false,
1730 : true);
1731 : if (temp)
1732 : return temp;
1733 : }
1734 : #endif
1735 : break;
1736 :
1737 11875866 : case ZERO_EXTEND:
1738 : /* Check for useless extension. */
1739 11875866 : if (GET_MODE (op) == mode)
1740 : return op;
1741 :
1742 : /* (zero_extend:SI (and:QI X (const))) -> (and:SI (lowpart:SI X) const)
1743 : where const does not sign bit set. */
1744 11875824 : if (GET_CODE (op) == AND
1745 130233 : && CONST_INT_P (XEXP (op, 1))
1746 98246 : && INTVAL (XEXP (op, 1)) > 0)
1747 : {
1748 89900 : rtx tem = rtl_hooks.gen_lowpart_no_emit (mode, XEXP (op, 0));
1749 89900 : if (tem)
1750 72903 : return simplify_gen_binary (AND, mode, tem, XEXP (op, 1));
1751 : }
1752 :
1753 : /* Check for a zero extension of a subreg of a promoted
1754 : variable, where the promotion is zero-extended, and the
1755 : target mode is the same as the variable's promotion. */
1756 11802921 : if (GET_CODE (op) == SUBREG
1757 1718406 : && SUBREG_PROMOTED_VAR_P (op)
1758 11803411 : && SUBREG_PROMOTED_UNSIGNED_P (op))
1759 : {
1760 490 : rtx subreg = SUBREG_REG (op);
1761 490 : machine_mode subreg_mode = GET_MODE (subreg);
1762 490 : if (!paradoxical_subreg_p (mode, subreg_mode))
1763 : {
1764 335 : temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1765 335 : if (temp)
1766 : {
1767 : /* Preserve SUBREG_PROMOTED_VAR_P. */
1768 335 : if (partial_subreg_p (temp))
1769 : {
1770 193 : SUBREG_PROMOTED_VAR_P (temp) = 1;
1771 193 : SUBREG_PROMOTED_SET (temp, SRP_UNSIGNED);
1772 : }
1773 : return temp;
1774 : }
1775 : }
1776 : else
1777 : /* Zero-extending a zero-extended subreg. */
1778 155 : return simplify_gen_unary (ZERO_EXTEND, mode,
1779 155 : subreg, subreg_mode);
1780 : }
1781 :
1782 : /* Extending a widening multiplication should be canonicalized to
1783 : a wider widening multiplication. */
1784 11802431 : if (GET_CODE (op) == MULT)
1785 : {
1786 187039 : rtx lhs = XEXP (op, 0);
1787 187039 : rtx rhs = XEXP (op, 1);
1788 187039 : enum rtx_code lcode = GET_CODE (lhs);
1789 187039 : enum rtx_code rcode = GET_CODE (rhs);
1790 :
1791 : /* Widening multiplies usually extend both operands, but sometimes
1792 : they use a shift to extract a portion of a register. */
1793 187039 : if ((lcode == ZERO_EXTEND
1794 186326 : || (lcode == LSHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1795 932 : && (rcode == ZERO_EXTEND
1796 879 : || (rcode == LSHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1797 : {
1798 65 : machine_mode lmode = GET_MODE (lhs);
1799 65 : machine_mode rmode = GET_MODE (rhs);
1800 65 : int bits;
1801 :
1802 65 : if (lcode == LSHIFTRT)
1803 : /* Number of bits not shifted off the end. */
1804 12 : bits = (GET_MODE_UNIT_PRECISION (lmode)
1805 12 : - INTVAL (XEXP (lhs, 1)));
1806 : else /* lcode == ZERO_EXTEND */
1807 : /* Size of inner mode. */
1808 106 : bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1809 :
1810 65 : if (rcode == LSHIFTRT)
1811 12 : bits += (GET_MODE_UNIT_PRECISION (rmode)
1812 12 : - INTVAL (XEXP (rhs, 1)));
1813 : else /* rcode == ZERO_EXTEND */
1814 106 : bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1815 :
1816 : /* We can only widen multiplies if the result is mathematiclly
1817 : equivalent. I.e. if overflow was impossible. */
1818 130 : if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1819 53 : return simplify_gen_binary
1820 53 : (MULT, mode,
1821 : simplify_gen_unary (ZERO_EXTEND, mode, lhs, lmode),
1822 53 : simplify_gen_unary (ZERO_EXTEND, mode, rhs, rmode));
1823 : }
1824 : }
1825 :
1826 : /* (zero_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1827 11802378 : if (GET_CODE (op) == ZERO_EXTEND)
1828 22670 : return simplify_gen_unary (ZERO_EXTEND, mode, XEXP (op, 0),
1829 22670 : GET_MODE (XEXP (op, 0)));
1830 :
1831 : /* (zero_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1832 : is (zero_extend:M (subreg:O <X>)) if there is mode with
1833 : GET_MODE_PRECISION (N) - I bits. */
1834 11779708 : if (GET_CODE (op) == LSHIFTRT
1835 52616 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1836 11779726 : && is_a <scalar_int_mode> (mode, &int_mode)
1837 18 : && CONST_INT_P (XEXP (op, 1))
1838 13 : && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1839 11779708 : && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1840 0 : GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1841 : {
1842 0 : scalar_int_mode tmode;
1843 0 : if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1844 0 : - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1845 : {
1846 0 : rtx inner =
1847 0 : rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1848 0 : if (inner)
1849 0 : return simplify_gen_unary (ZERO_EXTEND, int_mode,
1850 0 : inner, tmode);
1851 : }
1852 : }
1853 :
1854 : /* (zero_extend:M (subreg:N <X:O>)) is <X:O> (for M == O) or
1855 : (zero_extend:M <X:O>), if X doesn't have any non-zero bits outside
1856 : of mode N. E.g.
1857 : (zero_extend:SI (subreg:QI (and:SI (reg:SI) (const_int 63)) 0)) is
1858 : (and:SI (reg:SI) (const_int 63)). */
1859 11779708 : if (partial_subreg_p (op)
1860 13422603 : && is_a <scalar_int_mode> (mode, &int_mode)
1861 1664888 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &op0_mode)
1862 1664172 : && GET_MODE_PRECISION (op0_mode) <= HOST_BITS_PER_WIDE_INT
1863 1309330 : && GET_MODE_PRECISION (int_mode) >= GET_MODE_PRECISION (op0_mode)
1864 1288300 : && subreg_lowpart_p (op)
1865 2563766 : && (nonzero_bits (SUBREG_REG (op), op0_mode)
1866 845850 : & ~GET_MODE_MASK (GET_MODE (op))) == 0)
1867 : {
1868 21993 : if (GET_MODE_PRECISION (int_mode) == GET_MODE_PRECISION (op0_mode))
1869 14863 : return SUBREG_REG (op);
1870 7130 : return simplify_gen_unary (ZERO_EXTEND, int_mode, SUBREG_REG (op),
1871 7130 : op0_mode);
1872 : }
1873 :
1874 : /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1875 11757715 : if (GET_CODE (op) == SUBREG
1876 1695923 : && subreg_lowpart_p (op)
1877 984500 : && GET_MODE (SUBREG_REG (op)) == mode
1878 12594964 : && is_a <scalar_int_mode> (mode, &int_mode)
1879 837249 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1880 837249 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1881 752511 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1882 12510226 : && (nonzero_bits (SUBREG_REG (op), mode)
1883 752511 : & ~GET_MODE_MASK (op_mode)) == 0)
1884 0 : return SUBREG_REG (op);
1885 :
1886 : /* Trying to optimize:
1887 : (zero_extend:M (subreg:N (not:M (X:M)))) ->
1888 : (xor:M (zero_extend:M (subreg:N (X:M)), mask))
1889 : where the mask is GET_MODE_MASK (N).
1890 : For the cases when X:M doesn't have any non-zero bits
1891 : outside of mode N, (zero_extend:M (subreg:N (X:M))
1892 : will be simplified to just (X:M)
1893 : and whole optimization will be -> (xor:M (X:M, mask)). */
1894 11757715 : if (partial_subreg_p (op)
1895 1642895 : && GET_CODE (XEXP (op, 0)) == NOT
1896 1417 : && GET_MODE (XEXP (op, 0)) == mode
1897 1399 : && subreg_lowpart_p (op)
1898 11758066 : && HWI_COMPUTABLE_MODE_P (mode)
1899 352 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1900 1696275 : && (nonzero_bits (XEXP (XEXP (op, 0), 0), mode)
1901 352 : & ~GET_MODE_MASK (op_mode)) == 0)
1902 : {
1903 1 : unsigned HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
1904 2 : return simplify_gen_binary (XOR, mode,
1905 1 : XEXP (XEXP (op, 0), 0),
1906 1 : gen_int_mode (mask, mode));
1907 : }
1908 :
1909 : #if defined(POINTERS_EXTEND_UNSIGNED)
1910 : /* As we do not know which address space the pointer is referring to,
1911 : we can do this only if the target does not support different pointer
1912 : or address modes depending on the address space. */
1913 11757714 : if (target_default_pointer_address_modes_p ()
1914 : && POINTERS_EXTEND_UNSIGNED > 0
1915 13390869 : && mode == Pmode && GET_MODE (op) == ptr_mode
1916 977 : && (CONSTANT_P (op)
1917 956 : || (GET_CODE (op) == SUBREG
1918 0 : && REG_P (SUBREG_REG (op))
1919 0 : && REG_POINTER (SUBREG_REG (op))
1920 0 : && GET_MODE (SUBREG_REG (op)) == Pmode))
1921 11757735 : && !targetm.have_ptr_extend ())
1922 : {
1923 21 : temp
1924 21 : = convert_memory_address_addr_space_1 (Pmode, op,
1925 : ADDR_SPACE_GENERIC, false,
1926 : true);
1927 21 : if (temp)
1928 : return temp;
1929 : }
1930 : #endif
1931 : break;
1932 :
1933 797927 : case VEC_DUPLICATE:
1934 797927 : if (GET_CODE (op) == VEC_DUPLICATE)
1935 2 : return simplify_gen_unary (VEC_DUPLICATE, mode, XEXP (op, 0),
1936 2 : GET_MODE (XEXP (op, 0)));
1937 : break;
1938 :
1939 : default:
1940 : break;
1941 : }
1942 :
1943 20170974 : if (VECTOR_MODE_P (mode)
1944 1876974 : && vec_duplicate_p (op, &elt)
1945 22053811 : && code != VEC_DUPLICATE)
1946 : {
1947 5863 : if (code == SIGN_EXTEND || code == ZERO_EXTEND)
1948 : /* Enforce a canonical order of VEC_DUPLICATE wrt other unary
1949 : operations by promoting VEC_DUPLICATE to the root of the expression
1950 : (as far as possible). */
1951 4871 : temp = simplify_gen_unary (code, GET_MODE_INNER (mode),
1952 9742 : elt, GET_MODE_INNER (GET_MODE (op)));
1953 : else
1954 : /* Try applying the operator to ELT and see if that simplifies.
1955 : We can duplicate the result if so.
1956 :
1957 : The reason we traditionally haven't used simplify_gen_unary
1958 : for these codes is that it didn't necessarily seem to be a
1959 : win to convert things like:
1960 :
1961 : (neg:V (vec_duplicate:V (reg:S R)))
1962 :
1963 : to:
1964 :
1965 : (vec_duplicate:V (neg:S (reg:S R)))
1966 :
1967 : The first might be done entirely in vector registers while the
1968 : second might need a move between register files.
1969 :
1970 : However, there also cases where promoting the vec_duplicate is
1971 : more efficient, and there is definite value in having a canonical
1972 : form when matching instruction patterns. We should consider
1973 : extending the simplify_gen_unary code above to more cases. */
1974 992 : temp = simplify_unary_operation (code, GET_MODE_INNER (mode),
1975 1984 : elt, GET_MODE_INNER (GET_MODE (op)));
1976 5863 : if (temp)
1977 5403 : return gen_vec_duplicate (mode, temp);
1978 : }
1979 :
1980 : return 0;
1981 : }
1982 :
1983 : /* Try to compute the value of a unary operation CODE whose output mode is to
1984 : be MODE with input operand OP whose mode was originally OP_MODE.
1985 : Return zero if the value cannot be computed. */
1986 : rtx
1987 30288020 : simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
1988 : rtx op, machine_mode op_mode)
1989 : {
1990 30288020 : scalar_int_mode result_mode;
1991 :
1992 30288020 : if (code == VEC_DUPLICATE)
1993 : {
1994 1937469 : gcc_assert (VECTOR_MODE_P (mode));
1995 1937469 : if (GET_MODE (op) != VOIDmode)
1996 : {
1997 812018 : if (!VECTOR_MODE_P (GET_MODE (op)))
1998 1610658 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE (op));
1999 : else
2000 20067 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE_INNER
2001 : (GET_MODE (op)));
2002 : }
2003 1937469 : if (CONST_SCALAR_INT_P (op) || CONST_DOUBLE_AS_FLOAT_P (op))
2004 1138786 : return gen_const_vec_duplicate (mode, op);
2005 798683 : if (GET_CODE (op) == CONST_VECTOR
2006 798683 : && (CONST_VECTOR_DUPLICATE_P (op)
2007 756 : || CONST_VECTOR_NUNITS (op).is_constant ()))
2008 : {
2009 756 : unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op)
2010 756 : ? CONST_VECTOR_NPATTERNS (op)
2011 1510 : : CONST_VECTOR_NUNITS (op).to_constant ());
2012 2268 : gcc_assert (multiple_p (GET_MODE_NUNITS (mode), npatterns));
2013 756 : rtx_vector_builder builder (mode, npatterns, 1);
2014 3888 : for (unsigned i = 0; i < npatterns; i++)
2015 2376 : builder.quick_push (CONST_VECTOR_ELT (op, i));
2016 756 : return builder.build ();
2017 756 : }
2018 : }
2019 :
2020 27597155 : if (VECTOR_MODE_P (mode)
2021 1925935 : && GET_CODE (op) == CONST_VECTOR
2022 29254330 : && known_eq (GET_MODE_NUNITS (mode), CONST_VECTOR_NUNITS (op)))
2023 : {
2024 35284 : gcc_assert (GET_MODE (op) == op_mode);
2025 :
2026 35284 : rtx_vector_builder builder;
2027 35284 : if (!builder.new_unary_operation (mode, op, false))
2028 : return 0;
2029 :
2030 35284 : unsigned int count = builder.encoded_nelts ();
2031 152848 : for (unsigned int i = 0; i < count; i++)
2032 : {
2033 236180 : rtx x = simplify_unary_operation (code, GET_MODE_INNER (mode),
2034 : CONST_VECTOR_ELT (op, i),
2035 236180 : GET_MODE_INNER (op_mode));
2036 118090 : if (!x || !valid_for_const_vector_p (mode, x))
2037 526 : return 0;
2038 117564 : builder.quick_push (x);
2039 : }
2040 34758 : return builder.build ();
2041 35284 : }
2042 :
2043 : /* The order of these tests is critical so that, for example, we don't
2044 : check the wrong mode (input vs. output) for a conversion operation,
2045 : such as FIX. At some point, this should be simplified. */
2046 :
2047 29113194 : if (code == FLOAT && CONST_SCALAR_INT_P (op))
2048 : {
2049 8168 : REAL_VALUE_TYPE d;
2050 :
2051 8168 : if (op_mode == VOIDmode)
2052 : {
2053 : /* CONST_INT have VOIDmode as the mode. We assume that all
2054 : the bits of the constant are significant, though, this is
2055 : a dangerous assumption as many times CONST_INTs are
2056 : created and used with garbage in the bits outside of the
2057 : precision of the implied mode of the const_int. */
2058 64 : op_mode = MAX_MODE_INT;
2059 : }
2060 :
2061 8168 : real_from_integer (&d, mode, rtx_mode_t (op, op_mode), SIGNED);
2062 :
2063 : /* Avoid the folding if flag_signaling_nans is on and
2064 : operand is a signaling NaN. */
2065 8168 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2066 : return 0;
2067 :
2068 8168 : d = real_value_truncate (mode, d);
2069 :
2070 : /* Avoid the folding if flag_rounding_math is on and the
2071 : conversion is not exact. */
2072 8168 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2073 : {
2074 1011 : bool fail = false;
2075 1011 : wide_int w = real_to_integer (&d, &fail,
2076 : GET_MODE_PRECISION
2077 1011 : (as_a <scalar_int_mode> (op_mode)));
2078 2022 : if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
2079 905 : return 0;
2080 1011 : }
2081 :
2082 7263 : return const_double_from_real_value (d, mode);
2083 : }
2084 29105026 : else if (code == UNSIGNED_FLOAT && CONST_SCALAR_INT_P (op))
2085 : {
2086 2133 : REAL_VALUE_TYPE d;
2087 :
2088 2133 : if (op_mode == VOIDmode)
2089 : {
2090 : /* CONST_INT have VOIDmode as the mode. We assume that all
2091 : the bits of the constant are significant, though, this is
2092 : a dangerous assumption as many times CONST_INTs are
2093 : created and used with garbage in the bits outside of the
2094 : precision of the implied mode of the const_int. */
2095 8 : op_mode = MAX_MODE_INT;
2096 : }
2097 :
2098 2133 : real_from_integer (&d, mode, rtx_mode_t (op, op_mode), UNSIGNED);
2099 :
2100 : /* Avoid the folding if flag_signaling_nans is on and
2101 : operand is a signaling NaN. */
2102 2133 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2103 : return 0;
2104 :
2105 2133 : d = real_value_truncate (mode, d);
2106 :
2107 : /* Avoid the folding if flag_rounding_math is on and the
2108 : conversion is not exact. */
2109 2133 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2110 : {
2111 16 : bool fail = false;
2112 16 : wide_int w = real_to_integer (&d, &fail,
2113 : GET_MODE_PRECISION
2114 16 : (as_a <scalar_int_mode> (op_mode)));
2115 28 : if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
2116 16 : return 0;
2117 16 : }
2118 :
2119 2117 : return const_double_from_real_value (d, mode);
2120 : }
2121 :
2122 29102893 : if (CONST_SCALAR_INT_P (op) && is_a <scalar_int_mode> (mode, &result_mode))
2123 : {
2124 3848977 : unsigned int width = GET_MODE_PRECISION (result_mode);
2125 3848977 : if (width > MAX_BITSIZE_MODE_ANY_INT)
2126 : return 0;
2127 :
2128 3848977 : wide_int result;
2129 3848977 : scalar_int_mode imode = (op_mode == VOIDmode
2130 3848977 : ? result_mode
2131 3848696 : : as_a <scalar_int_mode> (op_mode));
2132 3848977 : rtx_mode_t op0 = rtx_mode_t (op, imode);
2133 3848977 : int int_value;
2134 :
2135 : #if TARGET_SUPPORTS_WIDE_INT == 0
2136 : /* This assert keeps the simplification from producing a result
2137 : that cannot be represented in a CONST_DOUBLE but a lot of
2138 : upstream callers expect that this function never fails to
2139 : simplify something and so you if you added this to the test
2140 : above the code would die later anyway. If this assert
2141 : happens, you just need to make the port support wide int. */
2142 : gcc_assert (width <= HOST_BITS_PER_DOUBLE_INT);
2143 : #endif
2144 :
2145 3848977 : switch (code)
2146 : {
2147 170405 : case NOT:
2148 170405 : result = wi::bit_not (op0);
2149 170405 : break;
2150 :
2151 1874075 : case NEG:
2152 1874075 : result = wi::neg (op0);
2153 1874075 : break;
2154 :
2155 7145 : case ABS:
2156 7145 : result = wi::abs (op0);
2157 7145 : break;
2158 :
2159 0 : case FFS:
2160 0 : result = wi::shwi (wi::ffs (op0), result_mode);
2161 0 : break;
2162 :
2163 175 : case CLZ:
2164 175 : if (wi::ne_p (op0, 0))
2165 45 : int_value = wi::clz (op0);
2166 260 : else if (! CLZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
2167 : return NULL_RTX;
2168 45 : result = wi::shwi (int_value, result_mode);
2169 45 : break;
2170 :
2171 0 : case CLRSB:
2172 0 : result = wi::shwi (wi::clrsb (op0), result_mode);
2173 0 : break;
2174 :
2175 0 : case CTZ:
2176 0 : if (wi::ne_p (op0, 0))
2177 0 : int_value = wi::ctz (op0);
2178 0 : else if (! CTZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
2179 : return NULL_RTX;
2180 0 : result = wi::shwi (int_value, result_mode);
2181 0 : break;
2182 :
2183 160 : case POPCOUNT:
2184 160 : result = wi::shwi (wi::popcount (op0), result_mode);
2185 160 : break;
2186 :
2187 0 : case PARITY:
2188 0 : result = wi::shwi (wi::parity (op0), result_mode);
2189 0 : break;
2190 :
2191 1853 : case BSWAP:
2192 1853 : result = wi::bswap (op0);
2193 1853 : break;
2194 :
2195 0 : case BITREVERSE:
2196 0 : result = wi::bitreverse (op0);
2197 0 : break;
2198 :
2199 1615434 : case TRUNCATE:
2200 1615434 : case ZERO_EXTEND:
2201 1615434 : result = wide_int::from (op0, width, UNSIGNED);
2202 1615434 : break;
2203 :
2204 14450 : case US_TRUNCATE:
2205 14450 : case SS_TRUNCATE:
2206 14450 : {
2207 14450 : signop sgn = code == US_TRUNCATE ? UNSIGNED : SIGNED;
2208 14450 : wide_int nmax
2209 14450 : = wide_int::from (wi::max_value (width, sgn),
2210 28900 : GET_MODE_PRECISION (imode), sgn);
2211 14450 : wide_int nmin
2212 14450 : = wide_int::from (wi::min_value (width, sgn),
2213 28900 : GET_MODE_PRECISION (imode), sgn);
2214 14450 : result = wi::min (wi::max (op0, nmin, sgn), nmax, sgn);
2215 14450 : result = wide_int::from (result, width, sgn);
2216 14450 : break;
2217 14450 : }
2218 165280 : case SIGN_EXTEND:
2219 165280 : result = wide_int::from (op0, width, SIGNED);
2220 165280 : break;
2221 :
2222 0 : case SS_NEG:
2223 0 : if (wi::only_sign_bit_p (op0))
2224 0 : result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED);
2225 : else
2226 0 : result = wi::neg (op0);
2227 : break;
2228 :
2229 0 : case SS_ABS:
2230 0 : if (wi::only_sign_bit_p (op0))
2231 0 : result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED);
2232 : else
2233 0 : result = wi::abs (op0);
2234 : break;
2235 :
2236 : case SQRT:
2237 : default:
2238 : return 0;
2239 : }
2240 :
2241 3848847 : return immed_wide_int_const (result, result_mode);
2242 3848977 : }
2243 :
2244 25253916 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2245 415010 : && SCALAR_FLOAT_MODE_P (mode)
2246 412960 : && SCALAR_FLOAT_MODE_P (GET_MODE (op)))
2247 : {
2248 412960 : REAL_VALUE_TYPE d = *CONST_DOUBLE_REAL_VALUE (op);
2249 412960 : switch (code)
2250 : {
2251 : case SQRT:
2252 : return 0;
2253 840 : case ABS:
2254 840 : d = real_value_abs (&d);
2255 840 : break;
2256 15852 : case NEG:
2257 15852 : d = real_value_negate (&d);
2258 15852 : break;
2259 2284 : case FLOAT_TRUNCATE:
2260 : /* Don't perform the operation if flag_signaling_nans is on
2261 : and the operand is a signaling NaN. */
2262 2284 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2263 : return NULL_RTX;
2264 : /* Or if flag_rounding_math is on and the truncation is not
2265 : exact. */
2266 2284 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2267 2284 : && !exact_real_truncate (mode, &d))
2268 231 : return NULL_RTX;
2269 2053 : d = real_value_truncate (mode, d);
2270 2053 : break;
2271 387299 : case FLOAT_EXTEND:
2272 : /* Don't perform the operation if flag_signaling_nans is on
2273 : and the operand is a signaling NaN. */
2274 387299 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2275 : return NULL_RTX;
2276 : /* All this does is change the mode, unless changing
2277 : mode class. */
2278 387297 : if (GET_MODE_CLASS (mode) != GET_MODE_CLASS (GET_MODE (op)))
2279 0 : real_convert (&d, mode, &d);
2280 : break;
2281 0 : case FIX:
2282 : /* Don't perform the operation if flag_signaling_nans is on
2283 : and the operand is a signaling NaN. */
2284 0 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2285 : return NULL_RTX;
2286 0 : real_arithmetic (&d, FIX_TRUNC_EXPR, &d, NULL);
2287 0 : break;
2288 6072 : case NOT:
2289 6072 : {
2290 6072 : long tmp[4];
2291 6072 : int i;
2292 :
2293 6072 : real_to_target (tmp, &d, GET_MODE (op));
2294 30360 : for (i = 0; i < 4; i++)
2295 24288 : tmp[i] = ~tmp[i];
2296 6072 : real_from_target (&d, tmp, mode);
2297 6072 : break;
2298 : }
2299 0 : default:
2300 0 : gcc_unreachable ();
2301 : }
2302 412114 : return const_double_from_real_value (d, mode);
2303 : }
2304 2050 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2305 2050 : && SCALAR_FLOAT_MODE_P (GET_MODE (op))
2306 24843006 : && is_int_mode (mode, &result_mode))
2307 : {
2308 2050 : unsigned int width = GET_MODE_PRECISION (result_mode);
2309 2050 : if (width > MAX_BITSIZE_MODE_ANY_INT)
2310 : return 0;
2311 :
2312 : /* Although the overflow semantics of RTL's FIX and UNSIGNED_FIX
2313 : operators are intentionally left unspecified (to ease implementation
2314 : by target backends), for consistency, this routine implements the
2315 : same semantics for constant folding as used by the middle-end. */
2316 :
2317 : /* This was formerly used only for non-IEEE float.
2318 : eggert@twinsun.com says it is safe for IEEE also. */
2319 2050 : REAL_VALUE_TYPE t;
2320 2050 : const REAL_VALUE_TYPE *x = CONST_DOUBLE_REAL_VALUE (op);
2321 2050 : wide_int wmax, wmin;
2322 : /* This is part of the abi to real_to_integer, but we check
2323 : things before making this call. */
2324 2050 : bool fail;
2325 :
2326 2050 : switch (code)
2327 : {
2328 2042 : case FIX:
2329 : /* According to IEEE standard, for conversions from floating point to
2330 : integer. When a NaN or infinite operand cannot be represented in
2331 : the destination format and this cannot otherwise be indicated, the
2332 : invalid operation exception shall be signaled. When a numeric
2333 : operand would convert to an integer outside the range of the
2334 : destination format, the invalid operation exception shall be
2335 : signaled if this situation cannot otherwise be indicated. */
2336 2042 : if (REAL_VALUE_ISNAN (*x))
2337 955 : return flag_trapping_math ? NULL_RTX : const0_rtx;
2338 :
2339 1087 : if (REAL_VALUE_ISINF (*x) && flag_trapping_math)
2340 : return NULL_RTX;
2341 :
2342 : /* Test against the signed upper bound. */
2343 127 : wmax = wi::max_value (width, SIGNED);
2344 127 : real_from_integer (&t, VOIDmode, wmax, SIGNED);
2345 127 : if (real_less (&t, x))
2346 3 : return (flag_trapping_math
2347 3 : ? NULL_RTX : immed_wide_int_const (wmax, mode));
2348 :
2349 : /* Test against the signed lower bound. */
2350 124 : wmin = wi::min_value (width, SIGNED);
2351 124 : real_from_integer (&t, VOIDmode, wmin, SIGNED);
2352 124 : if (real_less (x, &t))
2353 8 : return immed_wide_int_const (wmin, mode);
2354 :
2355 116 : return immed_wide_int_const (real_to_integer (x, &fail, width),
2356 : mode);
2357 :
2358 8 : case UNSIGNED_FIX:
2359 8 : if (REAL_VALUE_ISNAN (*x) || REAL_VALUE_NEGATIVE (*x))
2360 6 : return flag_trapping_math ? NULL_RTX : const0_rtx;
2361 :
2362 2 : if (REAL_VALUE_ISINF (*x) && flag_trapping_math)
2363 : return NULL_RTX;
2364 :
2365 : /* Test against the unsigned upper bound. */
2366 0 : wmax = wi::max_value (width, UNSIGNED);
2367 0 : real_from_integer (&t, VOIDmode, wmax, UNSIGNED);
2368 0 : if (real_less (&t, x))
2369 0 : return (flag_trapping_math
2370 0 : ? NULL_RTX : immed_wide_int_const (wmax, mode));
2371 :
2372 0 : return immed_wide_int_const (real_to_integer (x, &fail, width),
2373 : mode);
2374 :
2375 0 : default:
2376 0 : gcc_unreachable ();
2377 : }
2378 2050 : }
2379 :
2380 : /* Handle polynomial integers. */
2381 : else if (CONST_POLY_INT_P (op))
2382 : {
2383 : poly_wide_int result;
2384 : switch (code)
2385 : {
2386 : case NEG:
2387 : result = -const_poly_int_value (op);
2388 : break;
2389 :
2390 : case NOT:
2391 : result = ~const_poly_int_value (op);
2392 : break;
2393 :
2394 : default:
2395 : return NULL_RTX;
2396 : }
2397 : return immed_wide_int_const (result, mode);
2398 : }
2399 :
2400 : return NULL_RTX;
2401 : }
2402 :
2403 : /* Subroutine of simplify_binary_operation to simplify a binary operation
2404 : CODE that can commute with byte swapping, with result mode MODE and
2405 : operating on OP0 and OP1. CODE is currently one of AND, IOR or XOR.
2406 : Return zero if no simplification or canonicalization is possible. */
2407 :
2408 : rtx
2409 38595475 : simplify_context::simplify_byte_swapping_operation (rtx_code code,
2410 : machine_mode mode,
2411 : rtx op0, rtx op1)
2412 : {
2413 38595475 : rtx tem;
2414 :
2415 : /* (op (bswap x) C1)) -> (bswap (op x C2)) with C2 swapped. */
2416 38595475 : if (GET_CODE (op0) == BSWAP && CONST_SCALAR_INT_P (op1))
2417 : {
2418 287 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0),
2419 : simplify_gen_unary (BSWAP, mode, op1, mode));
2420 287 : return simplify_gen_unary (BSWAP, mode, tem, mode);
2421 : }
2422 :
2423 : /* (op (bswap x) (bswap y)) -> (bswap (op x y)). */
2424 38595188 : if (GET_CODE (op0) == BSWAP && GET_CODE (op1) == BSWAP)
2425 : {
2426 0 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0), XEXP (op1, 0));
2427 0 : return simplify_gen_unary (BSWAP, mode, tem, mode);
2428 : }
2429 :
2430 : return NULL_RTX;
2431 : }
2432 :
2433 : /* Subroutine of simplify_binary_operation to simplify a commutative,
2434 : associative binary operation CODE with result mode MODE, operating
2435 : on OP0 and OP1. CODE is currently one of PLUS, MULT, AND, IOR, XOR,
2436 : SMIN, SMAX, UMIN or UMAX. Return zero if no simplification or
2437 : canonicalization is possible. */
2438 :
2439 : rtx
2440 49682485 : simplify_context::simplify_associative_operation (rtx_code code,
2441 : machine_mode mode,
2442 : rtx op0, rtx op1)
2443 : {
2444 49682485 : rtx tem;
2445 :
2446 : /* Normally expressions simplified by simplify-rtx.cc are combined
2447 : at most from a few machine instructions and therefore the
2448 : expressions should be fairly small. During var-tracking
2449 : we can see arbitrarily large expressions though and reassociating
2450 : those can be quadratic, so punt after encountering max_assoc_count
2451 : simplify_associative_operation calls during outermost simplify_*
2452 : call. */
2453 49682485 : if (++assoc_count >= max_assoc_count)
2454 : return NULL_RTX;
2455 :
2456 : /* Linearize the operator to the left. */
2457 49677406 : if (GET_CODE (op1) == code)
2458 : {
2459 : /* "(a op b) op (c op d)" becomes "((a op b) op c) op d)". */
2460 19348 : if (GET_CODE (op0) == code)
2461 : {
2462 5451 : tem = simplify_gen_binary (code, mode, op0, XEXP (op1, 0));
2463 5451 : return simplify_gen_binary (code, mode, tem, XEXP (op1, 1));
2464 : }
2465 :
2466 : /* "a op (b op c)" becomes "(b op c) op a". */
2467 13897 : if (! swap_commutative_operands_p (op1, op0))
2468 13897 : return simplify_gen_binary (code, mode, op1, op0);
2469 :
2470 : std::swap (op0, op1);
2471 : }
2472 :
2473 49658058 : if (GET_CODE (op0) == code)
2474 : {
2475 : /* Canonicalize "(x op c) op y" as "(x op y) op c". */
2476 1258799 : if (swap_commutative_operands_p (XEXP (op0, 1), op1))
2477 : {
2478 267271 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0), op1);
2479 267271 : return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2480 : }
2481 :
2482 : /* Attempt to simplify "(a op b) op c" as "a op (b op c)". */
2483 991528 : tem = simplify_binary_operation (code, mode, XEXP (op0, 1), op1);
2484 991528 : if (tem != 0)
2485 70380 : return simplify_gen_binary (code, mode, XEXP (op0, 0), tem);
2486 :
2487 : /* Attempt to simplify "(a op b) op c" as "(a op c) op b". */
2488 921148 : tem = simplify_binary_operation (code, mode, XEXP (op0, 0), op1);
2489 921148 : if (tem != 0)
2490 37539 : return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2491 : }
2492 :
2493 : return 0;
2494 : }
2495 :
2496 : /* A comparison mask has one bit per outcome for which the comparison
2497 : holds. */
2498 : enum comparison_mask
2499 : {
2500 : CMP_BIT_UN = 1,
2501 : CMP_BIT_EQ = 2,
2502 : CMP_BIT_GT = 4,
2503 : CMP_BIT_LT = 8,
2504 : CMP_MASK_ORDERED = CMP_BIT_LT | CMP_BIT_GT | CMP_BIT_EQ,
2505 : CMP_MASK_ALL = CMP_MASK_ORDERED | CMP_BIT_UN
2506 : };
2507 :
2508 : /* If COMPARISON can be treated as an unsigned comparison, return a mask
2509 : that represents it. Return 0 otherwise. */
2510 : static int
2511 474032 : unsigned_comparison_to_mask (rtx_code comparison)
2512 : {
2513 0 : switch (comparison)
2514 : {
2515 : case LTU:
2516 : return CMP_BIT_LT;
2517 : case GTU:
2518 : return CMP_BIT_GT;
2519 : case EQ:
2520 : return CMP_BIT_EQ;
2521 :
2522 : case LEU:
2523 : return CMP_BIT_LT | CMP_BIT_EQ;
2524 : case GEU:
2525 : return CMP_BIT_GT | CMP_BIT_EQ;
2526 :
2527 : case NE:
2528 : return CMP_BIT_LT | CMP_BIT_GT;
2529 :
2530 : default:
2531 : return 0;
2532 : }
2533 : }
2534 :
2535 : /* Reverse the mapping in unsigned_comparison_to_mask, going from masks
2536 : to comparisons. */
2537 : static rtx_code
2538 6577 : mask_to_unsigned_comparison (int mask)
2539 : {
2540 6577 : switch (mask)
2541 : {
2542 : case CMP_BIT_LT:
2543 : return LTU;
2544 160 : case CMP_BIT_GT:
2545 160 : return GTU;
2546 2562 : case CMP_BIT_EQ:
2547 2562 : return EQ;
2548 :
2549 160 : case CMP_BIT_LT | CMP_BIT_EQ:
2550 160 : return LEU;
2551 160 : case CMP_BIT_GT | CMP_BIT_EQ:
2552 160 : return GEU;
2553 :
2554 3375 : case CMP_BIT_LT | CMP_BIT_GT:
2555 3375 : return NE;
2556 :
2557 0 : default:
2558 0 : gcc_unreachable ();
2559 : }
2560 : }
2561 :
2562 : /* Return true if CODE is LTU, GTU, LEU or GEU. */
2563 : static bool
2564 103375 : unsigned_inequality_p (rtx_code code)
2565 : {
2566 0 : switch (code)
2567 : {
2568 : case LTU:
2569 : case GTU:
2570 : case LEU:
2571 : case GEU:
2572 : return true;
2573 :
2574 95294 : default:
2575 0 : return false;
2576 : }
2577 : }
2578 :
2579 : /* Return a mask describing the COMPARISON. */
2580 : static int
2581 441022 : comparison_to_mask (enum rtx_code comparison)
2582 : {
2583 441022 : switch (comparison)
2584 : {
2585 : case LT:
2586 : return CMP_BIT_LT;
2587 38800 : case GT:
2588 38800 : return CMP_BIT_GT;
2589 8155 : case EQ:
2590 8155 : return CMP_BIT_EQ;
2591 29411 : case UNORDERED:
2592 29411 : return CMP_BIT_UN;
2593 :
2594 28008 : case LTGT:
2595 28008 : return CMP_BIT_LT | CMP_BIT_GT;
2596 39800 : case LE:
2597 39800 : return CMP_BIT_LT | CMP_BIT_EQ;
2598 39808 : case GE:
2599 39808 : return CMP_BIT_GT | CMP_BIT_EQ;
2600 28664 : case UNLT:
2601 28664 : return CMP_BIT_UN | CMP_BIT_LT;
2602 28632 : case UNGT:
2603 28632 : return CMP_BIT_UN | CMP_BIT_GT;
2604 29092 : case UNEQ:
2605 29092 : return CMP_BIT_UN | CMP_BIT_EQ;
2606 :
2607 29876 : case ORDERED:
2608 29876 : return CMP_MASK_ORDERED;
2609 41948 : case NE:
2610 41948 : return CMP_BIT_UN | CMP_BIT_LT | CMP_BIT_GT;
2611 30000 : case UNLE:
2612 30000 : return CMP_BIT_UN | CMP_BIT_LT | CMP_BIT_EQ;
2613 30004 : case UNGE:
2614 30004 : return CMP_BIT_UN | CMP_BIT_GT | CMP_BIT_EQ;
2615 :
2616 0 : default:
2617 0 : gcc_unreachable ();
2618 : }
2619 : }
2620 :
2621 : /* Return a comparison corresponding to the MASK. */
2622 : static enum rtx_code
2623 28247 : mask_to_comparison (int mask)
2624 : {
2625 28247 : switch (mask)
2626 : {
2627 : case CMP_BIT_LT:
2628 : return LT;
2629 : case CMP_BIT_GT:
2630 : return GT;
2631 : case CMP_BIT_EQ:
2632 : return EQ;
2633 : case CMP_BIT_UN:
2634 : return UNORDERED;
2635 :
2636 : case CMP_BIT_LT | CMP_BIT_GT:
2637 : return LTGT;
2638 : case CMP_BIT_LT | CMP_BIT_EQ:
2639 : return LE;
2640 : case CMP_BIT_GT | CMP_BIT_EQ:
2641 : return GE;
2642 : case CMP_BIT_UN | CMP_BIT_LT:
2643 : return UNLT;
2644 : case CMP_BIT_UN | CMP_BIT_GT:
2645 : return UNGT;
2646 : case CMP_BIT_UN | CMP_BIT_EQ:
2647 : return UNEQ;
2648 :
2649 : case CMP_MASK_ORDERED:
2650 : return ORDERED;
2651 : case CMP_BIT_UN | CMP_BIT_LT | CMP_BIT_GT:
2652 : return NE;
2653 : case CMP_BIT_UN | CMP_BIT_LT | CMP_BIT_EQ:
2654 : return UNLE;
2655 : case CMP_BIT_UN | CMP_BIT_GT | CMP_BIT_EQ:
2656 : return UNGE;
2657 :
2658 0 : default:
2659 0 : gcc_unreachable ();
2660 : }
2661 : }
2662 :
2663 : /* Return true if the comparison with mask MASK (see comparison_to_mask)
2664 : raises an invalid-operation exception for a quiet NaN operand, as LT,
2665 : GT, LE, GE and LTGT do: they are false for unordered operands and
2666 : depend on their order. EQ and ORDERED do not, and neither do the
2667 : constant results 0 and CMP_MASK_ALL. */
2668 :
2669 : static bool
2670 19904 : comparison_mask_signals_p (int mask)
2671 : {
2672 19904 : return ((mask & CMP_BIT_UN) == 0
2673 9436 : && (mask & (CMP_BIT_LT | CMP_BIT_GT)) != 0
2674 7608 : && mask != CMP_MASK_ORDERED);
2675 : }
2676 :
2677 : /* Canonicalize RES, a scalar const0_rtx/const_true_rtx to the right
2678 : false/true value of comparison with MODE where comparison operands
2679 : have CMP_MODE. */
2680 :
2681 : static rtx
2682 798955 : relational_result (machine_mode mode, machine_mode cmp_mode, rtx res)
2683 : {
2684 798955 : if (SCALAR_FLOAT_MODE_P (mode))
2685 : {
2686 167 : if (res == const0_rtx)
2687 163 : return CONST0_RTX (mode);
2688 : #ifdef FLOAT_STORE_FLAG_VALUE
2689 : REAL_VALUE_TYPE val = FLOAT_STORE_FLAG_VALUE (mode);
2690 : return const_double_from_real_value (val, mode);
2691 : #else
2692 : return NULL_RTX;
2693 : #endif
2694 : }
2695 798788 : if (VECTOR_MODE_P (mode))
2696 : {
2697 422 : if (res == const0_rtx)
2698 81 : return CONST0_RTX (mode);
2699 : #ifdef VECTOR_STORE_FLAG_VALUE
2700 341 : rtx val = VECTOR_STORE_FLAG_VALUE (mode);
2701 331 : if (val == NULL_RTX)
2702 : return NULL_RTX;
2703 331 : if (val == const1_rtx)
2704 0 : return CONST1_RTX (mode);
2705 :
2706 331 : return gen_const_vec_duplicate (mode, val);
2707 : #else
2708 : return NULL_RTX;
2709 : #endif
2710 : }
2711 : /* For vector comparison with scalar int result, it is unknown
2712 : if the target means here a comparison into an integral bitmask,
2713 : or comparison where all comparisons true mean const_true_rtx
2714 : whole result, or where any comparisons true mean const_true_rtx
2715 : whole result. For const0_rtx all the cases are the same. */
2716 798366 : if (VECTOR_MODE_P (cmp_mode)
2717 0 : && SCALAR_INT_MODE_P (mode)
2718 0 : && res == const_true_rtx)
2719 0 : return NULL_RTX;
2720 :
2721 : return res;
2722 : }
2723 :
2724 : /* Simplify a logical operation CODE with result mode MODE, operating on OP0
2725 : and OP1, in the case where both are relational operations. Assume that
2726 : OP0 is inverted if INVERT0_P is true.
2727 :
2728 : Return 0 if no such simplification is possible. */
2729 : rtx
2730 13663799 : simplify_context::simplify_logical_relational_operation (rtx_code code,
2731 : machine_mode mode,
2732 : rtx op0, rtx op1,
2733 : bool invert0_p)
2734 : {
2735 13663799 : if (!(COMPARISON_P (op0) && COMPARISON_P (op1)))
2736 : return 0;
2737 :
2738 113333 : if (!(rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2739 55694 : && rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))))
2740 : return 0;
2741 :
2742 55248 : if (side_effects_p (op0))
2743 : return 0;
2744 :
2745 55248 : enum rtx_code code0 = GET_CODE (op0);
2746 55248 : enum rtx_code code1 = GET_CODE (op1);
2747 55248 : machine_mode cmp_mode = GET_MODE (XEXP (op0, 0));
2748 55248 : if (cmp_mode == VOIDmode)
2749 0 : cmp_mode = GET_MODE (XEXP (op0, 1));
2750 :
2751 : /* Assume at first that the comparisons are on integers, and that the
2752 : operands are therefore ordered. */
2753 55248 : int all = CMP_MASK_ORDERED;
2754 55248 : int mask0 = unsigned_comparison_to_mask (code0);
2755 55248 : int mask1 = unsigned_comparison_to_mask (code1);
2756 55248 : bool unsigned_p = (unsigned_inequality_p (code0)
2757 55248 : || unsigned_inequality_p (code1));
2758 47167 : if (unsigned_p)
2759 : {
2760 : /* We only reach here when comparing integers. Reject mixtures of signed
2761 : and unsigned comparisons. */
2762 8081 : if (mask0 == 0 || mask1 == 0)
2763 : return 0;
2764 : }
2765 : else
2766 : {
2767 : /* See whether the operands might be unordered. Assume that all
2768 : results are possible for CC modes, and punt later if we don't get an
2769 : always-true or always-false answer. */
2770 47167 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC || HONOR_NANS (cmp_mode))
2771 : all = CMP_MASK_ALL;
2772 47167 : mask0 = comparison_to_mask (code0) & all;
2773 47167 : mask1 = comparison_to_mask (code1) & all;
2774 : }
2775 :
2776 53968 : if (invert0_p)
2777 37314 : mask0 = mask0 ^ all;
2778 :
2779 53968 : int mask;
2780 53968 : if (code == AND)
2781 14064 : mask = mask0 & mask1;
2782 39904 : else if (code == IOR)
2783 20647 : mask = mask0 | mask1;
2784 19257 : else if (code == XOR)
2785 19257 : mask = mask0 ^ mask1;
2786 : else
2787 : return 0;
2788 :
2789 : /* With trapping math, the result must signal for a quiet NaN exactly
2790 : when OP0 or OP1 does. Inverting OP0 does not change that. */
2791 53968 : if (HONOR_NANS (cmp_mode)
2792 10279 : && flag_trapping_math
2793 59128 : && ((comparison_mask_signals_p (comparison_to_mask (code0))
2794 6880 : || comparison_mask_signals_p (comparison_to_mask (code1)))
2795 5160 : != comparison_mask_signals_p (mask)))
2796 : return 0;
2797 :
2798 51240 : if (mask == all)
2799 7248 : return relational_result (mode, GET_MODE (op0), const_true_rtx);
2800 :
2801 43992 : if (mask == 0)
2802 4956 : return relational_result (mode, GET_MODE (op0), const0_rtx);
2803 :
2804 39036 : if (unsigned_p)
2805 6577 : code = mask_to_unsigned_comparison (mask);
2806 : else
2807 : {
2808 32459 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC)
2809 : return 0;
2810 :
2811 28247 : code = mask_to_comparison (mask);
2812 : /* LTGT and NE are arithmetically equivalent for ordered operands,
2813 : with NE being the canonical choice. */
2814 28247 : if (code == LTGT && all == CMP_MASK_ORDERED)
2815 3956 : code = NE;
2816 : }
2817 :
2818 34824 : op0 = XEXP (op1, 0);
2819 34824 : op1 = XEXP (op1, 1);
2820 :
2821 34824 : return simplify_gen_relational (code, mode, VOIDmode, op0, op1);
2822 : }
2823 :
2824 : /* We are going to IOR together OP0/OP1. If there is a common term in OP0/OP1
2825 : then we may be able to simplify the expression. We're primarily trying to
2826 : simplify down to IOR/XOR expression right now, but there may be other
2827 : simplifications we can do in the future.
2828 :
2829 : Return the simplified expression or NULL_RTX if no simplification was
2830 : possible. */
2831 : rtx
2832 27751398 : simplify_context::simplify_ior_with_common_term (machine_mode mode, rtx op0, rtx op1)
2833 : {
2834 : /* (ior X (plus/xor X C)) can be simplified into (ior X C) when
2835 : X and C have no bits in common. */
2836 27751398 : if ((GET_CODE (op1) == PLUS || GET_CODE (op1) == XOR)
2837 257973 : && rtx_equal_p (op0, XEXP (op1, 0))
2838 8336 : && ((nonzero_bits (op0, GET_MODE (op0))
2839 8336 : & nonzero_bits (XEXP (op1, 1), GET_MODE (op1))) == 0)
2840 27751398 : && !side_effects_p (op1))
2841 0 : return simplify_gen_binary (IOR, mode, op0, XEXP (op1, 1));
2842 :
2843 : /* (ior (and A C1) (and (not A) C2)) can be converted
2844 : into (and (xor A C2) (C1 + C2)) when there are no bits
2845 : in common between C1 and C2. */
2846 27751398 : if (GET_CODE (op0) == AND
2847 4424390 : && GET_CODE (op1) == AND
2848 419188 : && GET_CODE (XEXP (op1, 0)) == NOT
2849 20045 : && rtx_equal_p (XEXP (op0, 0), XEXP (XEXP (op1, 0), 0))
2850 3477 : && CONST_INT_P (XEXP (op0, 1))
2851 90 : && CONST_INT_P (XEXP (op1, 1))
2852 27751488 : && (INTVAL (XEXP (op0, 1)) & INTVAL (XEXP (op1, 1))) == 0)
2853 : {
2854 90 : rtx c = GEN_INT (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1)));
2855 :
2856 90 : rtx tem = simplify_gen_binary (XOR, mode, XEXP (op0, 0), XEXP (op1, 1));
2857 90 : if (tem)
2858 : {
2859 90 : tem = simplify_gen_binary (AND, mode, tem, c);
2860 :
2861 90 : if (tem)
2862 : return tem;
2863 : }
2864 : }
2865 :
2866 : /* Another variant seen on some target particularly those with
2867 : sub-word operations.
2868 :
2869 : (ior (and A C1) (plus (and A C2) C2)) can be simplified into
2870 : (and (xor (A C2) (C1 + C2).
2871 :
2872 : Where C2 is the sign bit for A's mode. So 0x80 for QI,
2873 : 0x8000 for HI, etc. In this case we know there is no carry
2874 : from the PLUS into relevant bits of the output. */
2875 27751308 : if (GET_CODE (op0) == AND
2876 4424300 : && GET_CODE (op1) == PLUS
2877 8056 : && GET_CODE (XEXP (op1, 0)) == AND
2878 330 : && rtx_equal_p (XEXP (op0, 0), XEXP (XEXP (op1, 0), 0))
2879 144 : && CONST_INT_P (XEXP (op0, 1))
2880 144 : && CONST_INT_P (XEXP (op1, 1))
2881 144 : && CONST_INT_P (XEXP (XEXP (op1, 0), 1))
2882 144 : && INTVAL (XEXP (op1, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
2883 36 : && GET_MODE_BITSIZE (GET_MODE (op1)).is_constant ()
2884 72 : && ((INTVAL (XEXP (op1, 1)) & GET_MODE_MASK (GET_MODE (op1)))
2885 72 : == HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (GET_MODE (op1)).to_constant () - 1))
2886 27751308 : && (INTVAL (XEXP (op0, 1)) & INTVAL (XEXP (op1, 1))) == 0)
2887 : {
2888 0 : rtx c = GEN_INT (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1)));
2889 :
2890 0 : rtx tem = simplify_gen_binary (XOR, mode, XEXP (op0, 0), XEXP (op1, 1));
2891 0 : if (tem)
2892 : {
2893 0 : tem = simplify_gen_binary (AND, mode, tem, c);
2894 0 : if (tem)
2895 : return tem;
2896 : }
2897 : }
2898 : return NULL_RTX;
2899 : }
2900 :
2901 :
2902 : /* Simplify a binary operation CODE with result mode MODE, operating on OP0
2903 : and OP1. Return 0 if no simplification is possible.
2904 :
2905 : Don't use this for relational operations such as EQ or LT.
2906 : Use simplify_relational_operation instead. */
2907 : rtx
2908 494627406 : simplify_context::simplify_binary_operation (rtx_code code, machine_mode mode,
2909 : rtx op0, rtx op1)
2910 : {
2911 494627406 : rtx trueop0, trueop1;
2912 494627406 : rtx tem;
2913 :
2914 : /* Relational operations don't work here. We must know the mode
2915 : of the operands in order to do the comparison correctly.
2916 : Assuming a full word can give incorrect results.
2917 : Consider comparing 128 with -128 in QImode. */
2918 494627406 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMPARE);
2919 494627406 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMM_COMPARE);
2920 :
2921 : /* Make sure the constant is second. */
2922 494627406 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
2923 494627406 : && swap_commutative_operands_p (op0, op1))
2924 : std::swap (op0, op1);
2925 :
2926 494627406 : trueop0 = avoid_constant_pool_reference (op0);
2927 494627406 : trueop1 = avoid_constant_pool_reference (op1);
2928 :
2929 494627406 : tem = simplify_const_binary_operation (code, mode, trueop0, trueop1);
2930 494627406 : if (tem)
2931 : return tem;
2932 464201930 : tem = simplify_binary_operation_1 (code, mode, op0, op1, trueop0, trueop1);
2933 :
2934 464201930 : if (tem)
2935 : return tem;
2936 :
2937 : /* If the above steps did not result in a simplification and op0 or op1
2938 : were constant pool references, use the referenced constants directly. */
2939 399244152 : if (trueop0 != op0 || trueop1 != op1)
2940 591501 : return simplify_gen_binary (code, mode, trueop0, trueop1);
2941 :
2942 : return NULL_RTX;
2943 : }
2944 :
2945 : /* Subroutine of simplify_binary_operation_1 that looks for cases in
2946 : which OP0 and OP1 are both vector series or vector duplicates
2947 : (which are really just series with a step of 0). If so, try to
2948 : form a new series by applying CODE to the bases and to the steps.
2949 : Return null if no simplification is possible.
2950 :
2951 : MODE is the mode of the operation and is known to be a vector
2952 : integer mode. */
2953 :
2954 : rtx
2955 2522093 : simplify_context::simplify_binary_operation_series (rtx_code code,
2956 : machine_mode mode,
2957 : rtx op0, rtx op1)
2958 : {
2959 2522093 : rtx base0, step0;
2960 2522093 : if (vec_duplicate_p (op0, &base0))
2961 74926 : step0 = const0_rtx;
2962 2447167 : else if (!vec_series_p (op0, &base0, &step0))
2963 : return NULL_RTX;
2964 :
2965 75673 : rtx base1, step1;
2966 75673 : if (vec_duplicate_p (op1, &base1))
2967 435 : step1 = const0_rtx;
2968 75238 : else if (!vec_series_p (op1, &base1, &step1))
2969 : return NULL_RTX;
2970 :
2971 : /* Only create a new series if we can simplify both parts. In other
2972 : cases this isn't really a simplification, and it's not necessarily
2973 : a win to replace a vector operation with a scalar operation. */
2974 5783 : scalar_mode inner_mode = GET_MODE_INNER (mode);
2975 5783 : rtx new_base = simplify_binary_operation (code, inner_mode, base0, base1);
2976 5783 : if (!new_base)
2977 : return NULL_RTX;
2978 :
2979 4780 : rtx new_step = simplify_binary_operation (code, inner_mode, step0, step1);
2980 4780 : if (!new_step)
2981 : return NULL_RTX;
2982 :
2983 4780 : return gen_vec_series (mode, new_base, new_step);
2984 : }
2985 :
2986 : /* Subroutine of simplify_binary_operation_1. Un-distribute a binary
2987 : operation CODE with result mode MODE, operating on OP0 and OP1.
2988 : e.g. simplify (xor (and A C) (and (B C)) to (and (xor (A B) C).
2989 : Returns NULL_RTX if no simplification is possible. */
2990 :
2991 : rtx
2992 1436900 : simplify_context::simplify_distributive_operation (rtx_code code,
2993 : machine_mode mode,
2994 : rtx op0, rtx op1)
2995 : {
2996 1436900 : enum rtx_code op = GET_CODE (op0);
2997 1436900 : gcc_assert (GET_CODE (op1) == op);
2998 :
2999 1436900 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))
3000 1436900 : && ! side_effects_p (XEXP (op0, 1)))
3001 360727 : return simplify_gen_binary (op, mode,
3002 : simplify_gen_binary (code, mode,
3003 : XEXP (op0, 0),
3004 : XEXP (op1, 0)),
3005 360727 : XEXP (op0, 1));
3006 :
3007 1076173 : if (GET_RTX_CLASS (op) == RTX_COMM_ARITH)
3008 : {
3009 1057606 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3010 1057606 : && ! side_effects_p (XEXP (op0, 0)))
3011 525305 : return simplify_gen_binary (op, mode,
3012 : simplify_gen_binary (code, mode,
3013 : XEXP (op0, 1),
3014 : XEXP (op1, 1)),
3015 525305 : XEXP (op0, 0));
3016 532301 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 1))
3017 532301 : && ! side_effects_p (XEXP (op0, 0)))
3018 54 : return simplify_gen_binary (op, mode,
3019 : simplify_gen_binary (code, mode,
3020 : XEXP (op0, 1),
3021 : XEXP (op1, 0)),
3022 54 : XEXP (op0, 0));
3023 532247 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 0))
3024 532247 : && ! side_effects_p (XEXP (op0, 1)))
3025 268312 : return simplify_gen_binary (op, mode,
3026 : simplify_gen_binary (code, mode,
3027 : XEXP (op0, 0),
3028 : XEXP (op1, 1)),
3029 268312 : XEXP (op0, 1));
3030 : }
3031 :
3032 : return NULL_RTX;
3033 : }
3034 :
3035 : /* Return TRUE if a rotate in mode MODE with a constant count in OP1
3036 : should be reversed.
3037 :
3038 : If the rotate should not be reversed, return FALSE.
3039 :
3040 : LEFT indicates if this is a rotate left or a rotate right. */
3041 :
3042 : bool
3043 150877 : reverse_rotate_by_imm_p (machine_mode mode, unsigned int left, rtx op1)
3044 : {
3045 150877 : if (!CONST_INT_P (op1))
3046 : return false;
3047 :
3048 : /* Some targets may only be able to rotate by a constant
3049 : in one direction. So we need to query the optab interface
3050 : to see what is possible. */
3051 112363 : optab binoptab = left ? rotl_optab : rotr_optab;
3052 48308 : optab re_binoptab = left ? rotr_optab : rotl_optab;
3053 112363 : enum insn_code icode = optab_handler (binoptab, mode);
3054 112363 : enum insn_code re_icode = optab_handler (re_binoptab, mode);
3055 :
3056 : /* If the target can not support the reversed optab, then there
3057 : is nothing to do. */
3058 112363 : if (re_icode == CODE_FOR_nothing)
3059 : return false;
3060 :
3061 : /* If the target does not support the requested rotate-by-immediate,
3062 : then we want to try reversing the rotate. We also want to try
3063 : reversing to minimize the count. */
3064 109881 : if ((icode == CODE_FOR_nothing)
3065 109881 : || (!insn_operand_matches (icode, 2, op1))
3066 549405 : || (IN_RANGE (INTVAL (op1),
3067 : GET_MODE_UNIT_PRECISION (mode) / 2 + left,
3068 : GET_MODE_UNIT_PRECISION (mode) - 1)))
3069 15465 : return (insn_operand_matches (re_icode, 2, op1));
3070 : return false;
3071 : }
3072 :
3073 : /* Analyse argument X to see if it represents an (ASHIFT X Y) operation
3074 : and return the expression to be shifted in SHIFT_OPND and the shift amount
3075 : in SHIFT_AMNT. This is primarily used to group handling of ASHIFT (X, CST)
3076 : and (PLUS (X, X)) in one place. If the expression is not equivalent to an
3077 : ASHIFT then return FALSE and set SHIFT_OPND and SHIFT_AMNT to NULL. */
3078 :
3079 : static bool
3080 544284128 : extract_ashift_operands_p (rtx x, rtx *shift_opnd, rtx *shift_amnt)
3081 : {
3082 544284128 : if (GET_CODE (x) == ASHIFT)
3083 : {
3084 14088054 : *shift_opnd = XEXP (x, 0);
3085 14088054 : *shift_amnt = XEXP (x, 1);
3086 14088054 : return true;
3087 : }
3088 530196074 : if (GET_CODE (x) == PLUS && rtx_equal_p (XEXP (x, 0), XEXP (x, 1)))
3089 : {
3090 11809 : *shift_opnd = XEXP (x, 0);
3091 11809 : *shift_amnt = CONST1_RTX (GET_MODE (x));
3092 11809 : return true;
3093 : }
3094 530184265 : *shift_opnd = NULL_RTX;
3095 530184265 : *shift_amnt = NULL_RTX;
3096 530184265 : return false;
3097 : }
3098 :
3099 : /* OP0 and OP1 are combined under an operation of mode MODE that can
3100 : potentially result in a ROTATE expression. Analyze the OP0 and OP1
3101 : and return the resulting ROTATE expression if so. Return NULL otherwise.
3102 : This is used in detecting the patterns (X << C1) [+,|,^] (X >> C2) where
3103 : C1 + C2 == GET_MODE_UNIT_PRECISION (mode).
3104 : (X << C1) and (C >> C2) would be OP0 and OP1. */
3105 :
3106 : static rtx
3107 275073358 : simplify_rotate_op (rtx op0, rtx op1, machine_mode mode)
3108 : {
3109 : /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3110 : mode size to (rotate A CX). */
3111 :
3112 275073358 : rtx opleft = op0;
3113 275073358 : rtx opright = op1;
3114 275073358 : rtx ashift_opnd, ashift_amnt;
3115 : /* In some cases the ASHIFT is not a direct ASHIFT. Look deeper and extract
3116 : the relevant operands here. */
3117 275073358 : bool ashift_op_p
3118 275073358 : = extract_ashift_operands_p (op1, &ashift_opnd, &ashift_amnt);
3119 :
3120 275073358 : if (ashift_op_p
3121 273276309 : || GET_CODE (op1) == SUBREG)
3122 : {
3123 : opleft = op1;
3124 : opright = op0;
3125 : }
3126 : else
3127 : {
3128 269210770 : opright = op1;
3129 269210770 : opleft = op0;
3130 269210770 : ashift_op_p
3131 269210770 : = extract_ashift_operands_p (opleft, &ashift_opnd, &ashift_amnt);
3132 : }
3133 :
3134 14099863 : if (ashift_op_p && GET_CODE (opright) == LSHIFTRT
3135 273347129 : && rtx_equal_p (ashift_opnd, XEXP (opright, 0)))
3136 : {
3137 9682 : rtx leftcst = unwrap_const_vec_duplicate (ashift_amnt);
3138 9682 : rtx rightcst = unwrap_const_vec_duplicate (XEXP (opright, 1));
3139 :
3140 5850 : if (CONST_INT_P (leftcst) && CONST_INT_P (rightcst)
3141 15532 : && (INTVAL (leftcst) + INTVAL (rightcst)
3142 5850 : == GET_MODE_UNIT_PRECISION (mode)))
3143 5095 : return gen_rtx_ROTATE (mode, XEXP (opright, 0), ashift_amnt);
3144 : }
3145 :
3146 : /* Same, but for ashift that has been "simplified" to a wider mode
3147 : by simplify_shift_const. */
3148 275068263 : scalar_int_mode int_mode, inner_mode;
3149 :
3150 275068263 : if (GET_CODE (opleft) == SUBREG
3151 280843500 : && is_a <scalar_int_mode> (mode, &int_mode)
3152 5770142 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (opleft)),
3153 : &inner_mode)
3154 5731102 : && GET_CODE (SUBREG_REG (opleft)) == ASHIFT
3155 191576 : && GET_CODE (opright) == LSHIFTRT
3156 946 : && GET_CODE (XEXP (opright, 0)) == SUBREG
3157 238 : && known_eq (SUBREG_BYTE (opleft), SUBREG_BYTE (XEXP (opright, 0)))
3158 472 : && GET_MODE_SIZE (int_mode) < GET_MODE_SIZE (inner_mode)
3159 229 : && rtx_equal_p (XEXP (SUBREG_REG (opleft), 0),
3160 229 : SUBREG_REG (XEXP (opright, 0)))
3161 13 : && CONST_INT_P (XEXP (SUBREG_REG (opleft), 1))
3162 13 : && CONST_INT_P (XEXP (opright, 1))
3163 275068263 : && (INTVAL (XEXP (SUBREG_REG (opleft), 1))
3164 13 : + INTVAL (XEXP (opright, 1))
3165 13 : == GET_MODE_PRECISION (int_mode)))
3166 1 : return gen_rtx_ROTATE (int_mode, XEXP (opright, 0),
3167 : XEXP (SUBREG_REG (opleft), 1));
3168 : return NULL_RTX;
3169 : }
3170 :
3171 : /* Returns true if OP0 and OP1 match the pattern (OP (plus (A - 1)) (neg A)),
3172 : and the pattern can be simplified (there are no side effects). */
3173 :
3174 : static bool
3175 40605286 : match_plus_neg_pattern (rtx op0, rtx op1, machine_mode mode)
3176 : {
3177 : /* Remove SUBREG from OP0 and OP1, if needed. */
3178 40605286 : if (GET_CODE (op0) == SUBREG
3179 7431295 : && GET_CODE (op1) == SUBREG
3180 304922 : && subreg_lowpart_p (op0)
3181 40909547 : && subreg_lowpart_p (op1))
3182 : {
3183 304244 : op0 = XEXP (op0, 0);
3184 304244 : op1 = XEXP (op1, 0);
3185 : }
3186 :
3187 : /* Check for the pattern (OP (plus (A - 1)) (neg A)). */
3188 40605286 : if (((GET_CODE (op1) == NEG
3189 3882 : && GET_CODE (op0) == PLUS
3190 2136 : && XEXP (op0, 1) == CONSTM1_RTX (mode))
3191 40604597 : || (GET_CODE (op0) == NEG
3192 78420 : && GET_CODE (op1) == PLUS
3193 0 : && XEXP (op1, 1) == CONSTM1_RTX (mode)))
3194 689 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3195 40605288 : && !side_effects_p (XEXP (op0, 0)))
3196 2 : return true;
3197 : return false;
3198 : }
3199 :
3200 : /* Check if OP matches the pattern of (subreg (not X)) and the subreg is
3201 : non-paradoxical. */
3202 :
3203 : static bool
3204 77193353 : non_paradoxical_subreg_not_p (rtx op)
3205 : {
3206 77193353 : return GET_CODE (op) == SUBREG
3207 8998351 : && !paradoxical_subreg_p (op)
3208 80013675 : && GET_CODE (SUBREG_REG (op)) == NOT;
3209 : }
3210 :
3211 : /* Convert (binop (subreg (not X)) Y) into (binop (not (subreg X)) Y), or
3212 : (binop X (subreg (not Y))) into (binop X (not (subreg Y))) to expose
3213 : opportunities to combine another binary logical operation with NOT. */
3214 :
3215 : static rtx
3216 38597907 : simplify_with_subreg_not (rtx_code binop, machine_mode mode, rtx op0, rtx op1)
3217 : {
3218 38597907 : rtx opn = NULL_RTX;
3219 38597907 : if (non_paradoxical_subreg_not_p (op0))
3220 : opn = op0;
3221 38595446 : else if (non_paradoxical_subreg_not_p (op1))
3222 : opn = op1;
3223 :
3224 2484 : if (opn == NULL_RTX)
3225 : return NULL_RTX;
3226 :
3227 4968 : rtx new_subreg = simplify_gen_subreg (mode,
3228 : XEXP (SUBREG_REG (opn), 0),
3229 2484 : GET_MODE (SUBREG_REG (opn)),
3230 2484 : SUBREG_BYTE (opn));
3231 :
3232 2484 : if (!new_subreg)
3233 : return NULL_RTX;
3234 :
3235 2432 : rtx new_not = simplify_gen_unary (NOT, mode, new_subreg, mode);
3236 2432 : if (opn == op0)
3237 2409 : return simplify_gen_binary (binop, mode, new_not, op1);
3238 : else
3239 23 : return simplify_gen_binary (binop, mode, op0, new_not);
3240 : }
3241 :
3242 : /* Return TRUE iff NOP is a negated form of OP, or vice-versa. */
3243 : static bool
3244 7312617 : negated_ops_p (rtx nop, rtx op)
3245 : {
3246 : /* Explicit negation. */
3247 7312617 : if (GET_CODE (nop) == NOT
3248 7312617 : && rtx_equal_p (XEXP (nop, 0), op))
3249 : return true;
3250 7309403 : if (GET_CODE (op) == NOT
3251 7309403 : && rtx_equal_p (XEXP (op, 0), nop))
3252 : return true;
3253 :
3254 : /* (~C <r A) is a negated form of (C << A) if C == 1. */
3255 7306378 : if (GET_CODE (op) == ASHIFT
3256 1513962 : && GET_CODE (nop) == ROTATE
3257 0 : && XEXP (op, 0) == CONST1_RTX (GET_MODE (op))
3258 0 : && CONST_INT_P (XEXP (nop, 0))
3259 0 : && INTVAL (XEXP (nop, 0)) == -2
3260 7306378 : && rtx_equal_p (XEXP (op, 1), XEXP (nop, 1)))
3261 : return true;
3262 7306378 : if (GET_CODE (nop) == ASHIFT
3263 154601 : && GET_CODE (op) == ROTATE
3264 0 : && XEXP (nop, 0) == CONST1_RTX (GET_MODE (op))
3265 0 : && CONST_INT_P (XEXP (nop, 0))
3266 0 : && INTVAL (XEXP (nop, 0)) == -2
3267 7306378 : && rtx_equal_p (XEXP (op, 1), XEXP (nop, 1)))
3268 : return true;
3269 :
3270 : /* ??? Should we consider rotations of C and ~C by the same amount? */
3271 :
3272 : return false;
3273 : }
3274 :
3275 : /* Subroutine of simplify_binary_operation. Simplify a binary operation
3276 : CODE with result mode MODE, operating on OP0 and OP1. If OP0 and/or
3277 : OP1 are constant pool references, TRUEOP0 and TRUEOP1 represent the
3278 : actual constants. */
3279 :
3280 : rtx
3281 464201930 : simplify_context::simplify_binary_operation_1 (rtx_code code,
3282 : machine_mode mode,
3283 : rtx op0, rtx op1,
3284 : rtx trueop0, rtx trueop1)
3285 : {
3286 464201930 : rtx tem, reversed, elt0, elt1;
3287 464201930 : HOST_WIDE_INT val;
3288 464201930 : scalar_int_mode int_mode, inner_mode;
3289 464201930 : poly_int64 offset;
3290 :
3291 : /* Even if we can't compute a constant result,
3292 : there are some cases worth simplifying. */
3293 :
3294 464201930 : switch (code)
3295 : {
3296 264775358 : case PLUS:
3297 : /* Maybe simplify x + 0 to x. The two expressions are equivalent
3298 : when x is NaN, infinite, or finite and nonzero. They aren't
3299 : when x is -0 and the rounding mode is not towards -infinity,
3300 : since (-0) + 0 is then 0. */
3301 525633199 : if (!HONOR_SIGNED_ZEROS (mode) && !HONOR_SNANS (mode)
3302 525633187 : && trueop1 == CONST0_RTX (mode))
3303 : return op0;
3304 :
3305 : /* ((-a) + b) -> (b - a) and similarly for (a + (-b)). These
3306 : transformations are safe even for IEEE. */
3307 263143307 : if (GET_CODE (op0) == NEG)
3308 65872 : return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
3309 263077435 : else if (GET_CODE (op1) == NEG)
3310 8684 : return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
3311 :
3312 : /* (~a) + 1 -> -a */
3313 263068751 : if (INTEGRAL_MODE_P (mode)
3314 258238589 : && GET_CODE (op0) == NOT
3315 1387754 : && trueop1 == const1_rtx)
3316 3905 : return simplify_gen_unary (NEG, mode, XEXP (op0, 0), mode);
3317 :
3318 : /* Handle both-operands-constant cases. We can only add
3319 : CONST_INTs to constants since the sum of relocatable symbols
3320 : can't be handled by most assemblers. Don't add CONST_INT
3321 : to CONST_INT since overflow won't be computed properly if wider
3322 : than HOST_BITS_PER_WIDE_INT. */
3323 :
3324 263064846 : if ((GET_CODE (op0) == CONST
3325 263064846 : || GET_CODE (op0) == SYMBOL_REF
3326 260293462 : || GET_CODE (op0) == LABEL_REF)
3327 263064846 : && poly_int_rtx_p (op1, &offset))
3328 2770419 : return plus_constant (mode, op0, offset);
3329 260294427 : else if ((GET_CODE (op1) == CONST
3330 260294427 : || GET_CODE (op1) == SYMBOL_REF
3331 256124751 : || GET_CODE (op1) == LABEL_REF)
3332 260294427 : && poly_int_rtx_p (op0, &offset))
3333 0 : return plus_constant (mode, op1, offset);
3334 :
3335 : /* See if this is something like X * C - X or vice versa or
3336 : if the multiplication is written as a shift. If so, we can
3337 : distribute and make a new multiply, shift, or maybe just
3338 : have X (if C is 2 in the example above). But don't make
3339 : something more expensive than we had before. */
3340 :
3341 260294427 : if (is_a <scalar_int_mode> (mode, &int_mode))
3342 : {
3343 253301208 : rtx lhs = op0, rhs = op1;
3344 :
3345 253301208 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3346 253301208 : wide_int coeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3347 :
3348 253301208 : if (GET_CODE (lhs) == NEG)
3349 : {
3350 0 : coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3351 0 : lhs = XEXP (lhs, 0);
3352 : }
3353 253301208 : else if (GET_CODE (lhs) == MULT
3354 9644455 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3355 : {
3356 8288472 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3357 8288472 : lhs = XEXP (lhs, 0);
3358 : }
3359 245012736 : else if (GET_CODE (lhs) == ASHIFT
3360 11143158 : && CONST_INT_P (XEXP (lhs, 1))
3361 11070542 : && INTVAL (XEXP (lhs, 1)) >= 0
3362 256083266 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3363 : {
3364 11070530 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3365 22141060 : GET_MODE_PRECISION (int_mode));
3366 11070530 : lhs = XEXP (lhs, 0);
3367 : }
3368 :
3369 253301208 : if (GET_CODE (rhs) == NEG)
3370 : {
3371 0 : coeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3372 0 : rhs = XEXP (rhs, 0);
3373 : }
3374 253301208 : else if (GET_CODE (rhs) == MULT
3375 358184 : && CONST_INT_P (XEXP (rhs, 1)))
3376 : {
3377 196716 : coeff1 = rtx_mode_t (XEXP (rhs, 1), int_mode);
3378 196716 : rhs = XEXP (rhs, 0);
3379 : }
3380 253104492 : else if (GET_CODE (rhs) == ASHIFT
3381 621161 : && CONST_INT_P (XEXP (rhs, 1))
3382 611342 : && INTVAL (XEXP (rhs, 1)) >= 0
3383 253715834 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3384 : {
3385 611342 : coeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3386 1222684 : GET_MODE_PRECISION (int_mode));
3387 611342 : rhs = XEXP (rhs, 0);
3388 : }
3389 :
3390 : /* Don't merge operands that have side effects. */
3391 253301208 : if (rtx_equal_p (lhs, rhs) && !side_effects_p (lhs))
3392 : {
3393 858448 : rtx orig = gen_rtx_PLUS (int_mode, op0, op1);
3394 858448 : rtx coeff;
3395 858448 : bool speed = optimize_function_for_speed_p (cfun);
3396 :
3397 858448 : coeff = immed_wide_int_const (coeff0 + coeff1, int_mode);
3398 :
3399 858448 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3400 858448 : return (set_src_cost (tem, int_mode, speed)
3401 858448 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3402 : }
3403 :
3404 : /* Optimize (X - 1) * Y + Y to X * Y. */
3405 252442760 : lhs = op0;
3406 252442760 : rhs = op1;
3407 252442760 : if (GET_CODE (op0) == MULT)
3408 : {
3409 9592769 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3410 632609 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx)
3411 9540103 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3412 52325 : && XEXP (XEXP (op0, 0), 1) == const1_rtx))
3413 9645435 : && rtx_equal_p (XEXP (op0, 1), op1))
3414 87 : lhs = XEXP (XEXP (op0, 0), 0);
3415 9592682 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3416 1680 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx)
3417 9592606 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3418 502 : && XEXP (XEXP (op0, 1), 1) == const1_rtx))
3419 9592758 : && rtx_equal_p (XEXP (op0, 0), op1))
3420 0 : lhs = XEXP (XEXP (op0, 1), 0);
3421 : }
3422 242849991 : else if (GET_CODE (op1) == MULT)
3423 : {
3424 128590 : if (((GET_CODE (XEXP (op1, 0)) == PLUS
3425 102 : && XEXP (XEXP (op1, 0), 1) == constm1_rtx)
3426 128582 : || (GET_CODE (XEXP (op1, 0)) == MINUS
3427 19 : && XEXP (XEXP (op1, 0), 1) == const1_rtx))
3428 128598 : && rtx_equal_p (XEXP (op1, 1), op0))
3429 0 : rhs = XEXP (XEXP (op1, 0), 0);
3430 128590 : else if (((GET_CODE (XEXP (op1, 1)) == PLUS
3431 45 : && XEXP (XEXP (op1, 1), 1) == constm1_rtx)
3432 128590 : || (GET_CODE (XEXP (op1, 1)) == MINUS
3433 4 : && XEXP (XEXP (op1, 1), 1) == const1_rtx))
3434 128590 : && rtx_equal_p (XEXP (op1, 0), op0))
3435 0 : rhs = XEXP (XEXP (op1, 1), 0);
3436 : }
3437 87 : if ((lhs != op0 && !side_effects_p (op1))
3438 252442761 : || (rhs != op1 && !side_effects_p (op0)))
3439 86 : return simplify_gen_binary (MULT, int_mode, lhs, rhs);
3440 253301208 : }
3441 :
3442 : /* (plus (xor X C1) C2) is (xor X (C1^C2)) if C2 is signbit. */
3443 259435893 : if (CONST_SCALAR_INT_P (op1)
3444 196868963 : && GET_CODE (op0) == XOR
3445 18691 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3446 259445686 : && mode_signbit_p (mode, op1))
3447 121 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3448 : simplify_gen_binary (XOR, mode, op1,
3449 121 : XEXP (op0, 1)));
3450 :
3451 : /* (plus (xor X C1) C2) is (xor X (C1^C2)) if X is either 0 or 1 and
3452 : 2 * ((X ^ C1) & C2) == 0; based on A + B == A ^ B + 2 * (A & B). */
3453 259435772 : if (CONST_SCALAR_INT_P (op1)
3454 196868842 : && GET_CODE (op0) == XOR
3455 18570 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3456 9672 : && nonzero_bits (XEXP (op0, 0), mode) == 1
3457 341 : && 2 * (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) == 0
3458 259435772 : && 2 * ((1 ^ INTVAL (XEXP (op0, 1))) & INTVAL (op1)) == 0)
3459 0 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3460 : simplify_gen_binary (XOR, mode, op1,
3461 0 : XEXP (op0, 1)));
3462 :
3463 : /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)). */
3464 259435772 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3465 259433302 : && GET_CODE (op0) == MULT
3466 269400510 : && GET_CODE (XEXP (op0, 0)) == NEG)
3467 : {
3468 4510 : rtx in1, in2;
3469 :
3470 4510 : in1 = XEXP (XEXP (op0, 0), 0);
3471 4510 : in2 = XEXP (op0, 1);
3472 4510 : return simplify_gen_binary (MINUS, mode, op1,
3473 : simplify_gen_binary (MULT, mode,
3474 4510 : in1, in2));
3475 : }
3476 :
3477 : /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3478 : C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3479 : is 1. */
3480 259431262 : if (COMPARISON_P (op0)
3481 1308200 : && ((STORE_FLAG_VALUE == -1 && trueop1 == const1_rtx)
3482 1308200 : || (STORE_FLAG_VALUE == 1 && trueop1 == constm1_rtx))
3483 259488971 : && (reversed = reversed_comparison (op0, mode)))
3484 57352 : return
3485 57352 : simplify_gen_unary (NEG, mode, reversed, mode);
3486 :
3487 : /* Convert (plus (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3488 : mode size to (rotate A CX). */
3489 259373910 : if ((tem = simplify_rotate_op (op0, op1, mode)))
3490 : return tem;
3491 :
3492 : /* If one of the operands is a PLUS or a MINUS, see if we can
3493 : simplify this by the associative law.
3494 : Don't use the associative law for floating point.
3495 : The inaccuracy makes it nonassociative,
3496 : and subtle programs can break if operations are associated. */
3497 :
3498 259372420 : if (INTEGRAL_MODE_P (mode)
3499 254542307 : && (plus_minus_operand_p (op0)
3500 220186627 : || plus_minus_operand_p (op1))
3501 35356235 : && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3502 : return tem;
3503 :
3504 : /* Reassociate floating point addition only when the user
3505 : specifies associative math operations. */
3506 224661948 : if (FLOAT_MODE_P (mode)
3507 4830113 : && flag_associative_math)
3508 : {
3509 912513 : tem = simplify_associative_operation (code, mode, op0, op1);
3510 912513 : if (tem)
3511 : return tem;
3512 : }
3513 :
3514 : /* Handle vector series. */
3515 224648236 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3516 : {
3517 1998741 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3518 1998741 : if (tem)
3519 : return tem;
3520 : }
3521 : break;
3522 :
3523 : case COMPARE:
3524 : break;
3525 :
3526 45426768 : case MINUS:
3527 : /* We can't assume x-x is 0 even with non-IEEE floating point.
3528 : This is unsafe if x is NaN or Inf, and with FE_DOWNWARD as the
3529 : rounding mode x - x may be -0.0. c.f. PR middle-end/98420. */
3530 45426768 : if (rtx_equal_p (trueop0, trueop1)
3531 214876 : && ! side_effects_p (op0)
3532 45641250 : && (!FLOAT_MODE_P (mode)
3533 2981 : || (!HONOR_NANS (mode)
3534 19 : && !HONOR_INFINITIES (mode)
3535 19 : && (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3536 6 : || !HONOR_SIGNED_ZEROS (mode)))))
3537 211514 : return CONST0_RTX (mode);
3538 :
3539 : /* Change subtraction from zero into negation. (0 - x) is the
3540 : same as -x when x is NaN, infinite, or finite and nonzero.
3541 : But if the mode has signed zeros, and does not round towards
3542 : -infinity, then 0 - 0 is 0, not -0. */
3543 45215254 : if (!HONOR_SIGNED_ZEROS (mode) && trueop0 == CONST0_RTX (mode))
3544 363564 : return simplify_gen_unary (NEG, mode, op1, mode);
3545 :
3546 : /* (-1 - a) is ~a, unless the expression contains symbolic
3547 : constants, in which case not retaining additions and
3548 : subtractions could cause invalid assembly to be produced. */
3549 44851690 : if (trueop0 == CONSTM1_RTX (mode)
3550 44851690 : && !contains_symbolic_reference_p (op1))
3551 614536 : return simplify_gen_unary (NOT, mode, op1, mode);
3552 :
3553 : /* Subtracting 0 has no effect unless the mode has signalling NaNs,
3554 : or has signed zeros and supports rounding towards -infinity.
3555 : In such a case, 0 - 0 is -0. */
3556 44980861 : if (!(HONOR_SIGNED_ZEROS (mode)
3557 743707 : && HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3558 44235883 : && !HONOR_SNANS (mode)
3559 88473001 : && trueop1 == CONST0_RTX (mode))
3560 : return op0;
3561 :
3562 : /* See if this is something like X * C - X or vice versa or
3563 : if the multiplication is written as a shift. If so, we can
3564 : distribute and make a new multiply, shift, or maybe just
3565 : have X (if C is 2 in the example above). But don't make
3566 : something more expensive than we had before. */
3567 :
3568 43302383 : if (is_a <scalar_int_mode> (mode, &int_mode))
3569 : {
3570 41890680 : rtx lhs = op0, rhs = op1;
3571 :
3572 41890680 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3573 41890680 : wide_int negcoeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3574 :
3575 41890680 : if (GET_CODE (lhs) == NEG)
3576 : {
3577 74420 : coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3578 74420 : lhs = XEXP (lhs, 0);
3579 : }
3580 41816260 : else if (GET_CODE (lhs) == MULT
3581 235790 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3582 : {
3583 80054 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3584 80054 : lhs = XEXP (lhs, 0);
3585 : }
3586 41736206 : else if (GET_CODE (lhs) == ASHIFT
3587 311201 : && CONST_INT_P (XEXP (lhs, 1))
3588 307943 : && INTVAL (XEXP (lhs, 1)) >= 0
3589 42044128 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3590 : {
3591 307922 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3592 615844 : GET_MODE_PRECISION (int_mode));
3593 307922 : lhs = XEXP (lhs, 0);
3594 : }
3595 :
3596 41890680 : if (GET_CODE (rhs) == NEG)
3597 : {
3598 13656 : negcoeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3599 13656 : rhs = XEXP (rhs, 0);
3600 : }
3601 41877024 : else if (GET_CODE (rhs) == MULT
3602 138125 : && CONST_INT_P (XEXP (rhs, 1)))
3603 : {
3604 98960 : negcoeff1 = wi::neg (rtx_mode_t (XEXP (rhs, 1), int_mode));
3605 98960 : rhs = XEXP (rhs, 0);
3606 : }
3607 41778064 : else if (GET_CODE (rhs) == ASHIFT
3608 405523 : && CONST_INT_P (XEXP (rhs, 1))
3609 404975 : && INTVAL (XEXP (rhs, 1)) >= 0
3610 42183039 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3611 : {
3612 404975 : negcoeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3613 809950 : GET_MODE_PRECISION (int_mode));
3614 404975 : negcoeff1 = -negcoeff1;
3615 404975 : rhs = XEXP (rhs, 0);
3616 : }
3617 :
3618 41890680 : if (rtx_equal_p (lhs, rhs) && !side_effects_p (lhs))
3619 : {
3620 94073 : rtx orig = gen_rtx_MINUS (int_mode, op0, op1);
3621 94073 : rtx coeff;
3622 94073 : bool speed = optimize_function_for_speed_p (cfun);
3623 :
3624 94073 : coeff = immed_wide_int_const (coeff0 + negcoeff1, int_mode);
3625 :
3626 94073 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3627 94073 : return (set_src_cost (tem, int_mode, speed)
3628 94073 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3629 : }
3630 :
3631 : /* Optimize (X + 1) * Y - Y to X * Y. */
3632 41796607 : lhs = op0;
3633 41796607 : if (GET_CODE (op0) == MULT)
3634 : {
3635 235328 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3636 4977 : && XEXP (XEXP (op0, 0), 1) == const1_rtx)
3637 233648 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3638 1665 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx))
3639 237008 : && rtx_equal_p (XEXP (op0, 1), op1))
3640 3 : lhs = XEXP (XEXP (op0, 0), 0);
3641 235325 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3642 30 : && XEXP (XEXP (op0, 1), 1) == const1_rtx)
3643 235317 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3644 84 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx))
3645 235333 : && rtx_equal_p (XEXP (op0, 0), op1))
3646 0 : lhs = XEXP (XEXP (op0, 1), 0);
3647 : }
3648 41796607 : if (lhs != op0 && !side_effects_p (op1))
3649 2 : return simplify_gen_binary (MULT, int_mode, lhs, op1);
3650 41890680 : }
3651 :
3652 : /* (a - (-b)) -> (a + b). True even for IEEE. */
3653 43208308 : if (GET_CODE (op1) == NEG)
3654 13602 : return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
3655 :
3656 : /* (-x - c) may be simplified as (-c - x). */
3657 43194706 : if (GET_CODE (op0) == NEG
3658 78563 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1)))
3659 : {
3660 724 : tem = simplify_unary_operation (NEG, mode, op1, mode);
3661 724 : if (tem)
3662 724 : return simplify_gen_binary (MINUS, mode, tem, XEXP (op0, 0));
3663 : }
3664 :
3665 43193982 : if ((GET_CODE (op0) == CONST
3666 43193982 : || GET_CODE (op0) == SYMBOL_REF
3667 37719401 : || GET_CODE (op0) == LABEL_REF)
3668 43193982 : && poly_int_rtx_p (op1, &offset))
3669 54207 : return plus_constant (mode, op0, trunc_int_for_mode (-offset, mode));
3670 :
3671 : /* Don't let a relocatable value get a negative coeff. */
3672 43139775 : if (is_a <scalar_int_mode> (mode)
3673 41728103 : && poly_int_rtx_p (op1)
3674 50451718 : && GET_MODE (op0) != VOIDmode)
3675 7311943 : return simplify_gen_binary (PLUS, mode,
3676 : op0,
3677 7311943 : neg_poly_int_rtx (mode, op1));
3678 :
3679 : /* (x - (x & y)) -> (x & ~y) */
3680 35827832 : if (INTEGRAL_MODE_P (mode) && GET_CODE (op1) == AND)
3681 : {
3682 225193 : if (rtx_equal_p (op0, XEXP (op1, 0)))
3683 : {
3684 590 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 1),
3685 295 : GET_MODE (XEXP (op1, 1)));
3686 295 : return simplify_gen_binary (AND, mode, op0, tem);
3687 : }
3688 224898 : if (rtx_equal_p (op0, XEXP (op1, 1)))
3689 : {
3690 2466 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 0),
3691 1233 : GET_MODE (XEXP (op1, 0)));
3692 1233 : return simplify_gen_binary (AND, mode, op0, tem);
3693 : }
3694 : }
3695 :
3696 : /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3697 : by reversing the comparison code if valid. */
3698 35826304 : if (STORE_FLAG_VALUE == 1
3699 35826304 : && trueop0 == const1_rtx
3700 1208814 : && COMPARISON_P (op1)
3701 35930967 : && (reversed = reversed_comparison (op1, mode)))
3702 : return reversed;
3703 :
3704 : /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A). */
3705 35721646 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3706 35720170 : && GET_CODE (op1) == MULT
3707 35960404 : && GET_CODE (XEXP (op1, 0)) == NEG)
3708 : {
3709 173 : rtx in1, in2;
3710 :
3711 173 : in1 = XEXP (XEXP (op1, 0), 0);
3712 173 : in2 = XEXP (op1, 1);
3713 173 : return simplify_gen_binary (PLUS, mode,
3714 : simplify_gen_binary (MULT, mode,
3715 : in1, in2),
3716 173 : op0);
3717 : }
3718 :
3719 : /* Canonicalize (minus (neg A) (mult B C)) to
3720 : (minus (mult (neg B) C) A). */
3721 35721473 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3722 35719997 : && GET_CODE (op1) == MULT
3723 35960058 : && GET_CODE (op0) == NEG)
3724 : {
3725 678 : rtx in1, in2;
3726 :
3727 678 : in1 = simplify_gen_unary (NEG, mode, XEXP (op1, 0), mode);
3728 678 : in2 = XEXP (op1, 1);
3729 678 : return simplify_gen_binary (MINUS, mode,
3730 : simplify_gen_binary (MULT, mode,
3731 : in1, in2),
3732 678 : XEXP (op0, 0));
3733 : }
3734 :
3735 : /* If one of the operands is a PLUS or a MINUS, see if we can
3736 : simplify this by the associative law. This will, for example,
3737 : canonicalize (minus A (plus B C)) to (minus (minus A B) C).
3738 : Don't use the associative law for floating point.
3739 : The inaccuracy makes it nonassociative,
3740 : and subtle programs can break if operations are associated. */
3741 :
3742 35720795 : if (INTEGRAL_MODE_P (mode)
3743 34855656 : && (plus_minus_operand_p (op0)
3744 31975417 : || plus_minus_operand_p (op1))
3745 4120564 : && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3746 : return tem;
3747 :
3748 : /* Handle vector series. */
3749 31751753 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3750 : {
3751 523352 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3752 523352 : if (tem)
3753 : return tem;
3754 : }
3755 : break;
3756 :
3757 13016265 : case MULT:
3758 13016265 : if (trueop1 == constm1_rtx)
3759 35236 : return simplify_gen_unary (NEG, mode, op0, mode);
3760 :
3761 12981029 : if (GET_CODE (op0) == NEG)
3762 : {
3763 39809 : rtx temp = simplify_unary_operation (NEG, mode, op1, mode);
3764 : /* If op1 is a MULT as well and simplify_unary_operation
3765 : just moved the NEG to the second operand, simplify_gen_binary
3766 : below could through simplify_associative_operation move
3767 : the NEG around again and recurse endlessly. */
3768 39809 : if (temp
3769 1894 : && GET_CODE (op1) == MULT
3770 0 : && GET_CODE (temp) == MULT
3771 0 : && XEXP (op1, 0) == XEXP (temp, 0)
3772 0 : && GET_CODE (XEXP (temp, 1)) == NEG
3773 0 : && XEXP (op1, 1) == XEXP (XEXP (temp, 1), 0))
3774 : temp = NULL_RTX;
3775 : if (temp)
3776 1894 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), temp);
3777 : }
3778 12979135 : if (GET_CODE (op1) == NEG)
3779 : {
3780 974 : rtx temp = simplify_unary_operation (NEG, mode, op0, mode);
3781 : /* If op0 is a MULT as well and simplify_unary_operation
3782 : just moved the NEG to the second operand, simplify_gen_binary
3783 : below could through simplify_associative_operation move
3784 : the NEG around again and recurse endlessly. */
3785 974 : if (temp
3786 408 : && GET_CODE (op0) == MULT
3787 305 : && GET_CODE (temp) == MULT
3788 305 : && XEXP (op0, 0) == XEXP (temp, 0)
3789 6 : && GET_CODE (XEXP (temp, 1)) == NEG
3790 5 : && XEXP (op0, 1) == XEXP (XEXP (temp, 1), 0))
3791 : temp = NULL_RTX;
3792 : if (temp)
3793 403 : return simplify_gen_binary (MULT, mode, temp, XEXP (op1, 0));
3794 : }
3795 :
3796 : /* Maybe simplify x * 0 to 0. The reduction is not valid if
3797 : x is NaN, since x * 0 is then also NaN. Nor is it valid
3798 : when the mode has signed zeros, since multiplying a negative
3799 : number by 0 will give -0, not 0. */
3800 12978732 : if (!HONOR_NANS (mode)
3801 11988647 : && !HONOR_SIGNED_ZEROS (mode)
3802 11988231 : && trueop1 == CONST0_RTX (mode)
3803 13025960 : && ! side_effects_p (op0))
3804 : return op1;
3805 :
3806 : /* In IEEE floating point, x*1 is not equivalent to x for
3807 : signalling NaNs. */
3808 12931504 : if (!HONOR_SNANS (mode)
3809 12931504 : && trueop1 == CONST1_RTX (mode))
3810 : return op0;
3811 :
3812 : /* Convert multiply by constant power of two into shift. */
3813 12386579 : if (mem_depth == 0 && CONST_SCALAR_INT_P (trueop1))
3814 : {
3815 6325069 : val = wi::exact_log2 (rtx_mode_t (trueop1, mode));
3816 6325069 : if (val >= 0)
3817 2965355 : return simplify_gen_binary (ASHIFT, mode, op0,
3818 2965355 : gen_int_shift_amount (mode, val));
3819 : }
3820 :
3821 : /* x*2 is x+x and x*(-1) is -x */
3822 9421224 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
3823 173035 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop1))
3824 173035 : && !DECIMAL_FLOAT_MODE_P (GET_MODE (trueop1))
3825 172751 : && GET_MODE (op0) == mode)
3826 : {
3827 172751 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
3828 :
3829 172751 : if (real_equal (d1, &dconst2))
3830 680 : return simplify_gen_binary (PLUS, mode, op0, copy_rtx (op0));
3831 :
3832 172071 : if (!HONOR_SNANS (mode)
3833 172071 : && real_equal (d1, &dconstm1))
3834 24 : return simplify_gen_unary (NEG, mode, op0, mode);
3835 : }
3836 :
3837 : /* Optimize -x * -x as x * x. */
3838 9420520 : if (FLOAT_MODE_P (mode)
3839 1414413 : && GET_CODE (op0) == NEG
3840 8336 : && GET_CODE (op1) == NEG
3841 0 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3842 0 : && !side_effects_p (XEXP (op0, 0)))
3843 0 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3844 :
3845 : /* Likewise, optimize abs(x) * abs(x) as x * x. */
3846 9420520 : if (SCALAR_FLOAT_MODE_P (mode)
3847 1108814 : && GET_CODE (op0) == ABS
3848 1488 : && GET_CODE (op1) == ABS
3849 1 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3850 9420520 : && !side_effects_p (XEXP (op0, 0)))
3851 0 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3852 :
3853 : /* Reassociate multiplication, but for floating point MULTs
3854 : only when the user specifies unsafe math optimizations. */
3855 9420520 : if (! FLOAT_MODE_P (mode)
3856 1414413 : || flag_unsafe_math_optimizations)
3857 : {
3858 8431547 : tem = simplify_associative_operation (code, mode, op0, op1);
3859 8431547 : if (tem)
3860 : return tem;
3861 : }
3862 : break;
3863 :
3864 15087032 : case IOR:
3865 15087032 : if (trueop1 == CONST0_RTX (mode))
3866 : return op0;
3867 14365027 : if (INTEGRAL_MODE_P (mode)
3868 14087487 : && trueop1 == CONSTM1_RTX (mode)
3869 10134 : && !side_effects_p (op0))
3870 : return op1;
3871 14354893 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3872 : return op0;
3873 : /* A | (~A) -> -1 */
3874 76665 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
3875 14343508 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
3876 0 : && ! side_effects_p (op0)
3877 14343508 : && GET_MODE_CLASS (mode) != MODE_CC)
3878 0 : return CONSTM1_RTX (mode);
3879 :
3880 : /* IOR of two single bit bitfields extracted from the same object.
3881 : Bitfields are represented as an AND based extraction */
3882 14343508 : if (GET_CODE (op0) == AND
3883 4454491 : && GET_CODE (op1) == AND
3884 : /* Verify both AND operands are logical right shifts. */
3885 293593 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
3886 2255 : && GET_CODE (XEXP (op1, 0)) == LSHIFTRT
3887 : /* Verify both bitfields are extracted from the same object. */
3888 260 : && XEXP (XEXP (op0, 0), 0) == XEXP (XEXP (op1, 0), 0)
3889 : /* Verify both fields are a single bit (could be generalized). */
3890 256 : && XEXP (op0, 1) == CONST1_RTX (mode)
3891 0 : && XEXP (op1, 1) == CONST1_RTX (mode)
3892 : /* Verify bit positions (for cases with variable bit position). */
3893 0 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
3894 0 : && CONST_INT_P (XEXP (XEXP (op1, 0), 1)))
3895 : {
3896 0 : unsigned HOST_WIDE_INT bitpos1 = INTVAL (XEXP (XEXP (op0, 0), 1));
3897 0 : unsigned HOST_WIDE_INT bitpos2 = INTVAL (XEXP (XEXP (op1, 0), 1));
3898 0 : unsigned HOST_WIDE_INT mask
3899 0 : = (HOST_WIDE_INT_1U << bitpos1) | (HOST_WIDE_INT_1U << bitpos2);
3900 :
3901 0 : rtx m = GEN_INT (mask);
3902 0 : rtx t = gen_rtx_AND (mode, XEXP (XEXP (op0, 0), 0), m);
3903 0 : t = gen_rtx_NE (mode, t, CONST0_RTX (mode));
3904 0 : return t;
3905 : }
3906 :
3907 : /* IOR of multiple single bit bitfields extracted from the same object
3908 : (building on previous case).
3909 : First bitfield is represented as an AND based extraction, as done
3910 : above. Second represented as NE based extraction, from
3911 : output above. */
3912 14343508 : if (GET_CODE (op0) == AND
3913 4454491 : && GET_CODE (op1) == NE
3914 : /* Verify AND operand is logical right shift. */
3915 3392 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
3916 : /* Verify NE operand is an AND (based on output above). */
3917 86 : && GET_CODE (XEXP (op1, 0)) == AND
3918 : /* Verify both bitfields are extracted from the same object. */
3919 0 : && XEXP (XEXP (op0, 0), 0) == XEXP (XEXP (op1, 0), 0)
3920 : /* Verify masking is with a single bit and that we have a NE 0
3921 : comparison for the other operand. */
3922 0 : && XEXP (op0, 1) == CONST1_RTX (mode)
3923 0 : && XEXP (op1, 1) == CONST0_RTX (mode)
3924 : /* Verify bit position. */
3925 0 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1)))
3926 : {
3927 0 : unsigned HOST_WIDE_INT bitpos1 = INTVAL (XEXP (XEXP (op0, 0), 1));
3928 0 : unsigned HOST_WIDE_INT mask
3929 0 : = (HOST_WIDE_INT_1U << bitpos1) | INTVAL (XEXP (XEXP (op1, 0), 1));
3930 :
3931 0 : rtx m = GEN_INT (mask);
3932 0 : rtx t = gen_rtx_AND (mode, XEXP (XEXP (op0, 0), 0), m);
3933 0 : t = gen_rtx_NE (mode, t, CONST0_RTX (mode));
3934 0 : return t;
3935 : }
3936 :
3937 : /* Convert (ior (plus (A - 1)) (neg A)) to -1. */
3938 14343508 : if (match_plus_neg_pattern (op0, op1, mode))
3939 0 : return CONSTM1_RTX (mode);
3940 :
3941 : /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
3942 14343508 : if (CONST_INT_P (op1)
3943 3737455 : && HWI_COMPUTABLE_MODE_P (mode)
3944 3680553 : && (nonzero_bits (op0, mode) & ~UINTVAL (op1)) == 0
3945 14567018 : && !side_effects_p (op0))
3946 : return op1;
3947 :
3948 : /* Canonicalize (X & C1) | C2. */
3949 14119998 : if (GET_CODE (op0) == AND
3950 4445606 : && CONST_INT_P (trueop1)
3951 787942 : && CONST_INT_P (XEXP (op0, 1)))
3952 : {
3953 564345 : HOST_WIDE_INT mask = GET_MODE_MASK (mode);
3954 564345 : HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
3955 564345 : HOST_WIDE_INT c2 = INTVAL (trueop1);
3956 :
3957 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes C2. */
3958 564345 : if ((c1 & c2) == c1
3959 564345 : && !side_effects_p (XEXP (op0, 0)))
3960 : return trueop1;
3961 :
3962 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
3963 564337 : if (((c1|c2) & mask) == mask)
3964 78385 : return simplify_gen_binary (IOR, mode, XEXP (op0, 0), op1);
3965 :
3966 : /* If (C1|C2) has a single bit clear, then adjust C1 so that
3967 : when split it'll match a single bit clear style insn.
3968 :
3969 : This could have been done with a target dependent splitter, but
3970 : then every target with single bit manipulation insns would need
3971 : to implement such splitters. */
3972 485952 : if (exact_log2 (~(c1 | c2)) >= 0)
3973 : {
3974 69601 : rtx temp = gen_rtx_AND (mode, XEXP (op0, 0), GEN_INT (c1 | c2));
3975 69601 : temp = gen_rtx_IOR (mode, temp, trueop1);
3976 69601 : return temp;
3977 : }
3978 : }
3979 :
3980 : /* Convert (A & B) | A to A. */
3981 13972004 : if (GET_CODE (op0) == AND
3982 4297612 : && (rtx_equal_p (XEXP (op0, 0), op1)
3983 4297127 : || rtx_equal_p (XEXP (op0, 1), op1))
3984 3678 : && ! side_effects_p (XEXP (op0, 0))
3985 13975682 : && ! side_effects_p (XEXP (op0, 1)))
3986 : return op1;
3987 :
3988 : /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3989 : mode size to (rotate A CX). */
3990 13968326 : tem = simplify_rotate_op (op0, op1, mode);
3991 13968326 : if (tem)
3992 : return tem;
3993 :
3994 : /* If OP0 is (ashiftrt (plus ...) C), it might actually be
3995 : a (sign_extend (plus ...)). Then check if OP1 is a CONST_INT and
3996 : the PLUS does not affect any of the bits in OP1: then we can do
3997 : the IOR as a PLUS and we can associate. This is valid if OP1
3998 : can be safely shifted left C bits. */
3999 13966100 : if (CONST_INT_P (trueop1) && GET_CODE (op0) == ASHIFTRT
4000 6186 : && GET_CODE (XEXP (op0, 0)) == PLUS
4001 74 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
4002 50 : && CONST_INT_P (XEXP (op0, 1))
4003 50 : && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
4004 : {
4005 50 : int count = INTVAL (XEXP (op0, 1));
4006 50 : HOST_WIDE_INT mask = UINTVAL (trueop1) << count;
4007 :
4008 50 : if (mask >> count == INTVAL (trueop1)
4009 43 : && trunc_int_for_mode (mask, mode) == mask
4010 80 : && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
4011 0 : return simplify_gen_binary (ASHIFTRT, mode,
4012 : plus_constant (mode, XEXP (op0, 0),
4013 0 : mask),
4014 : XEXP (op0, 1));
4015 : }
4016 :
4017 : /* The following happens with bitfield merging.
4018 : (X & C) | ((X | Y) & ~C) -> X | (Y & ~C) */
4019 13966100 : if (GET_CODE (op0) == AND
4020 4293934 : && GET_CODE (op1) == AND
4021 293593 : && CONST_INT_P (XEXP (op0, 1))
4022 132687 : && CONST_INT_P (XEXP (op1, 1))
4023 128264 : && (INTVAL (XEXP (op0, 1))
4024 128264 : == ~INTVAL (XEXP (op1, 1))))
4025 : {
4026 : /* The IOR/XOR may be on both sides. */
4027 37423 : rtx top0 = NULL_RTX, top1 = NULL_RTX;
4028 37423 : if (GET_CODE (XEXP (op1, 0)) == IOR
4029 37423 : || GET_CODE (XEXP (op1, 0)) == XOR)
4030 : top0 = op0, top1 = op1;
4031 37304 : else if (GET_CODE (XEXP (op0, 0)) == IOR
4032 37304 : || GET_CODE (XEXP (op0, 0)) == XOR)
4033 27 : top0 = op1, top1 = op0;
4034 37423 : if (top0 && top1)
4035 : {
4036 : /* X may be on either side of the inner IOR/XOR. */
4037 146 : rtx tem = NULL_RTX;
4038 146 : if (rtx_equal_p (XEXP (top0, 0),
4039 146 : XEXP (XEXP (top1, 0), 0)))
4040 76 : tem = XEXP (XEXP (top1, 0), 1);
4041 70 : else if (rtx_equal_p (XEXP (top0, 0),
4042 70 : XEXP (XEXP (top1, 0), 1)))
4043 13 : tem = XEXP (XEXP (top1, 0), 0);
4044 89 : if (tem)
4045 89 : return simplify_gen_binary (GET_CODE (XEXP (top1, 0)),
4046 : mode, XEXP (top0, 0),
4047 : simplify_gen_binary
4048 89 : (AND, mode, tem, XEXP (top1, 1)));
4049 : }
4050 : }
4051 :
4052 : /* Convert (ior (and A C) (and B C)) into (and (ior A B) C). */
4053 13966011 : if (GET_CODE (op0) == GET_CODE (op1)
4054 3078129 : && (GET_CODE (op0) == AND
4055 : || GET_CODE (op0) == IOR
4056 3078129 : || GET_CODE (op0) == LSHIFTRT
4057 2783642 : || GET_CODE (op0) == ASHIFTRT
4058 2783497 : || GET_CODE (op0) == ASHIFT
4059 2766665 : || GET_CODE (op0) == ROTATE
4060 2766665 : || GET_CODE (op0) == ROTATERT))
4061 : {
4062 311464 : tem = simplify_distributive_operation (code, mode, op0, op1);
4063 311464 : if (tem)
4064 : return tem;
4065 : }
4066 :
4067 : /* Convert (ior (and (not A) B) A) into A | B. */
4068 13881946 : if (GET_CODE (op0) == AND
4069 13881946 : && negated_ops_p (XEXP (op0, 0), op1))
4070 6202 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4071 :
4072 : /* op0/op1 may have a common term which in turn may allow simplification
4073 : of the the outer IOR. There are likely other cases we should
4074 : handle for the outer code as well as the form of the operands. */
4075 13875744 : tem = simplify_ior_with_common_term (mode, op0, op1);
4076 13875744 : if (tem)
4077 : return tem;
4078 :
4079 : /* IOR is commutative and we can't rely on canonicalization at this point,
4080 : so try again to simplify with the operands reversed. */
4081 13875654 : tem = simplify_ior_with_common_term (mode, op1, op0);
4082 13875654 : if (tem)
4083 : return tem;
4084 :
4085 13875654 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4086 13875654 : if (tem)
4087 : return tem;
4088 :
4089 13875649 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4090 13875649 : if (tem)
4091 : return tem;
4092 :
4093 13875643 : tem = simplify_associative_operation (code, mode, op0, op1);
4094 13875643 : if (tem)
4095 : return tem;
4096 :
4097 13614560 : tem = simplify_logical_relational_operation (code, mode, op0, op1);
4098 13614560 : if (tem)
4099 : return tem;
4100 : break;
4101 :
4102 2029099 : case XOR:
4103 2029099 : if (trueop1 == CONST0_RTX (mode))
4104 : return op0;
4105 1968551 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
4106 31206 : return simplify_gen_unary (NOT, mode, op0, mode);
4107 1937345 : if (rtx_equal_p (trueop0, trueop1)
4108 3076 : && ! side_effects_p (op0)
4109 1940417 : && GET_MODE_CLASS (mode) != MODE_CC)
4110 3072 : return CONST0_RTX (mode);
4111 :
4112 : /* Canonicalize XOR of the most significant bit to PLUS. */
4113 1934273 : if (CONST_SCALAR_INT_P (op1)
4114 1934273 : && mode_signbit_p (mode, op1))
4115 40868 : return simplify_gen_binary (PLUS, mode, op0, op1);
4116 : /* (xor (plus X C1) C2) is (xor X (C1^C2)) if C1 is signbit. */
4117 1893405 : if (CONST_SCALAR_INT_P (op1)
4118 509813 : && GET_CODE (op0) == PLUS
4119 2543 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
4120 1894925 : && mode_signbit_p (mode, XEXP (op0, 1)))
4121 189 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
4122 : simplify_gen_binary (XOR, mode, op1,
4123 189 : XEXP (op0, 1)));
4124 :
4125 : /* If we are XORing two things that have no bits in common,
4126 : convert them into an IOR. This helps to detect rotation encoded
4127 : using those methods and possibly other simplifications. */
4128 :
4129 1893216 : if (HWI_COMPUTABLE_MODE_P (mode)
4130 1492813 : && (nonzero_bits (op0, mode)
4131 1492813 : & nonzero_bits (op1, mode)) == 0)
4132 10260 : return (simplify_gen_binary (IOR, mode, op0, op1));
4133 :
4134 : /* Convert (xor (plus (A - 1)) (neg A)) to -1. */
4135 1882956 : if (match_plus_neg_pattern (op0, op1, mode))
4136 0 : return CONSTM1_RTX (mode);
4137 :
4138 : /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
4139 : Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
4140 : (NOT y). */
4141 1882956 : {
4142 1882956 : int num_negated = 0;
4143 :
4144 1882956 : if (GET_CODE (op0) == NOT)
4145 723 : num_negated++, op0 = XEXP (op0, 0);
4146 1882956 : if (GET_CODE (op1) == NOT)
4147 67 : num_negated++, op1 = XEXP (op1, 0);
4148 :
4149 67 : if (num_negated == 2)
4150 0 : return simplify_gen_binary (XOR, mode, op0, op1);
4151 1882956 : else if (num_negated == 1)
4152 790 : return simplify_gen_unary (NOT, mode,
4153 : simplify_gen_binary (XOR, mode, op0, op1),
4154 790 : mode);
4155 : }
4156 :
4157 : /* Convert (xor (and A B) B) to (and (not A) B). The latter may
4158 : correspond to a machine insn or result in further simplifications
4159 : if B is a constant. */
4160 :
4161 1882166 : if (GET_CODE (op0) == AND
4162 209075 : && rtx_equal_p (XEXP (op0, 1), op1)
4163 1905157 : && ! side_effects_p (op1))
4164 22991 : return simplify_gen_binary (AND, mode,
4165 : simplify_gen_unary (NOT, mode,
4166 : XEXP (op0, 0), mode),
4167 22991 : op1);
4168 :
4169 1859175 : else if (GET_CODE (op0) == AND
4170 186084 : && rtx_equal_p (XEXP (op0, 0), op1)
4171 1860990 : && ! side_effects_p (op1))
4172 1815 : return simplify_gen_binary (AND, mode,
4173 : simplify_gen_unary (NOT, mode,
4174 : XEXP (op0, 1), mode),
4175 1815 : op1);
4176 :
4177 : /* Similarly, convert (xor (ior A B) B) to (and (not B) A). */
4178 1857360 : else if (GET_CODE (op0) == IOR
4179 28708 : && rtx_equal_p (XEXP (op0, 1), op1)
4180 1857515 : && ! side_effects_p (op1))
4181 155 : return simplify_gen_binary (AND, mode,
4182 : simplify_gen_unary (NOT, mode,
4183 : op1, mode),
4184 155 : XEXP (op0, 0));
4185 1857205 : else if (GET_CODE (op0) == IOR
4186 28553 : && rtx_equal_p (XEXP (op0, 0), op1)
4187 1857213 : && ! side_effects_p (op1))
4188 8 : return simplify_gen_binary (AND, mode,
4189 : simplify_gen_unary (NOT, mode,
4190 : op1, mode),
4191 8 : XEXP (op0, 1));
4192 :
4193 : /* Given (xor (ior (xor A B) C) D), where B, C and D are
4194 : constants, simplify to (xor (ior A C) (B&~C)^D), canceling
4195 : out bits inverted twice and not set by C. Similarly, given
4196 : (xor (and (xor A B) C) D), simplify without inverting C in
4197 : the xor operand: (xor (and A C) (B&C)^D).
4198 : */
4199 1857197 : else if ((GET_CODE (op0) == IOR || GET_CODE (op0) == AND)
4200 212814 : && GET_CODE (XEXP (op0, 0)) == XOR
4201 8966 : && CONST_INT_P (op1)
4202 222 : && CONST_INT_P (XEXP (op0, 1))
4203 179 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1)))
4204 : {
4205 38 : enum rtx_code op = GET_CODE (op0);
4206 38 : rtx a = XEXP (XEXP (op0, 0), 0);
4207 38 : rtx b = XEXP (XEXP (op0, 0), 1);
4208 38 : rtx c = XEXP (op0, 1);
4209 38 : rtx d = op1;
4210 38 : HOST_WIDE_INT bval = INTVAL (b);
4211 38 : HOST_WIDE_INT cval = INTVAL (c);
4212 38 : HOST_WIDE_INT dval = INTVAL (d);
4213 38 : HOST_WIDE_INT xcval;
4214 :
4215 38 : if (op == IOR)
4216 8 : xcval = ~cval;
4217 : else
4218 : xcval = cval;
4219 :
4220 38 : return simplify_gen_binary (XOR, mode,
4221 : simplify_gen_binary (op, mode, a, c),
4222 38 : gen_int_mode ((bval & xcval) ^ dval,
4223 : mode));
4224 : }
4225 :
4226 : /* Given (xor (and A B) C), using P^Q == (~P&Q) | (~Q&P),
4227 : we can transform like this:
4228 : (A&B)^C == ~(A&B)&C | ~C&(A&B)
4229 : == (~A|~B)&C | ~C&(A&B) * DeMorgan's Law
4230 : == ~A&C | ~B&C | A&(~C&B) * Distribute and re-order
4231 : Attempt a few simplifications when B and C are both constants. */
4232 1857159 : if (GET_CODE (op0) == AND
4233 184239 : && CONST_INT_P (op1)
4234 14553 : && CONST_INT_P (XEXP (op0, 1)))
4235 : {
4236 11565 : rtx a = XEXP (op0, 0);
4237 11565 : rtx b = XEXP (op0, 1);
4238 11565 : rtx c = op1;
4239 11565 : HOST_WIDE_INT bval = INTVAL (b);
4240 11565 : HOST_WIDE_INT cval = INTVAL (c);
4241 :
4242 : /* Instead of computing ~A&C, we compute its negated value,
4243 : ~(A|~C). If it yields -1, ~A&C is zero, so we can
4244 : optimize for sure. If it does not simplify, we still try
4245 : to compute ~A&C below, but since that always allocates
4246 : RTL, we don't try that before committing to returning a
4247 : simplified expression. */
4248 11565 : rtx n_na_c = simplify_binary_operation (IOR, mode, a,
4249 : GEN_INT (~cval));
4250 :
4251 11565 : if ((~cval & bval) == 0)
4252 : {
4253 824 : rtx na_c = NULL_RTX;
4254 824 : if (n_na_c)
4255 0 : na_c = simplify_gen_unary (NOT, mode, n_na_c, mode);
4256 : else
4257 : {
4258 : /* If ~A does not simplify, don't bother: we don't
4259 : want to simplify 2 operations into 3, and if na_c
4260 : were to simplify with na, n_na_c would have
4261 : simplified as well. */
4262 824 : rtx na = simplify_unary_operation (NOT, mode, a, mode);
4263 824 : if (na)
4264 0 : na_c = simplify_gen_binary (AND, mode, na, c);
4265 : }
4266 :
4267 : /* Try to simplify ~A&C | ~B&C. */
4268 0 : if (na_c != NULL_RTX)
4269 0 : return simplify_gen_binary (IOR, mode, na_c,
4270 0 : gen_int_mode (~bval & cval, mode));
4271 : }
4272 : else
4273 : {
4274 : /* If ~A&C is zero, simplify A&(~C&B) | ~B&C. */
4275 10741 : if (n_na_c == CONSTM1_RTX (mode))
4276 : {
4277 0 : rtx a_nc_b = simplify_gen_binary (AND, mode, a,
4278 0 : gen_int_mode (~cval & bval,
4279 : mode));
4280 0 : return simplify_gen_binary (IOR, mode, a_nc_b,
4281 0 : gen_int_mode (~bval & cval,
4282 : mode));
4283 : }
4284 : }
4285 : }
4286 :
4287 : /* If we have (xor (and (xor A B) C) A) with C a constant we can instead
4288 : do (ior (and A ~C) (and B C)) which is a machine instruction on some
4289 : machines, and also has shorter instruction path length. */
4290 1857159 : if (GET_CODE (op0) == AND
4291 184239 : && GET_CODE (XEXP (op0, 0)) == XOR
4292 8476 : && CONST_INT_P (XEXP (op0, 1))
4293 1861685 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), trueop1))
4294 : {
4295 7 : rtx a = trueop1;
4296 7 : rtx b = XEXP (XEXP (op0, 0), 1);
4297 7 : rtx c = XEXP (op0, 1);
4298 7 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
4299 7 : rtx a_nc = simplify_gen_binary (AND, mode, a, nc);
4300 7 : rtx bc = simplify_gen_binary (AND, mode, b, c);
4301 7 : return simplify_gen_binary (IOR, mode, a_nc, bc);
4302 : }
4303 : /* Similarly, (xor (and (xor A B) C) B) as (ior (and A C) (and B ~C)) */
4304 1857152 : else if (GET_CODE (op0) == AND
4305 184232 : && GET_CODE (XEXP (op0, 0)) == XOR
4306 8469 : && CONST_INT_P (XEXP (op0, 1))
4307 1861671 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), trueop1))
4308 : {
4309 8 : rtx a = XEXP (XEXP (op0, 0), 0);
4310 8 : rtx b = trueop1;
4311 8 : rtx c = XEXP (op0, 1);
4312 8 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
4313 8 : rtx b_nc = simplify_gen_binary (AND, mode, b, nc);
4314 8 : rtx ac = simplify_gen_binary (AND, mode, a, c);
4315 8 : return simplify_gen_binary (IOR, mode, ac, b_nc);
4316 : }
4317 :
4318 : /* (xor (comparison foo bar) (const_int 1)) can become the reversed
4319 : comparison if STORE_FLAG_VALUE is 1. */
4320 1857144 : if (STORE_FLAG_VALUE == 1
4321 1857144 : && trueop1 == const1_rtx
4322 213643 : && COMPARISON_P (op0)
4323 1863934 : && (reversed = reversed_comparison (op0, mode)))
4324 : return reversed;
4325 :
4326 : /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4327 : is (lt foo (const_int 0)), so we can perform the above
4328 : simplification if STORE_FLAG_VALUE is 1. */
4329 :
4330 1850378 : if (is_a <scalar_int_mode> (mode, &int_mode)
4331 : && STORE_FLAG_VALUE == 1
4332 1506350 : && trueop1 == const1_rtx
4333 206877 : && GET_CODE (op0) == LSHIFTRT
4334 35377 : && CONST_INT_P (XEXP (op0, 1))
4335 35377 : && INTVAL (XEXP (op0, 1)) == GET_MODE_PRECISION (int_mode) - 1)
4336 34193 : return gen_rtx_GE (int_mode, XEXP (op0, 0), const0_rtx);
4337 :
4338 : /* (xor (comparison foo bar) (const_int sign-bit))
4339 : when STORE_FLAG_VALUE is the sign bit. */
4340 1816185 : if (is_a <scalar_int_mode> (mode, &int_mode)
4341 1472157 : && val_signbit_p (int_mode, STORE_FLAG_VALUE)
4342 0 : && trueop1 == const_true_rtx
4343 0 : && COMPARISON_P (op0)
4344 0 : && (reversed = reversed_comparison (op0, int_mode)))
4345 : return reversed;
4346 :
4347 : /* Convert (xor (and A C) (and B C)) into (and (xor A B) C). */
4348 1816185 : if (GET_CODE (op0) == GET_CODE (op1)
4349 659923 : && (GET_CODE (op0) == AND
4350 659923 : || GET_CODE (op0) == LSHIFTRT
4351 569365 : || GET_CODE (op0) == ASHIFTRT
4352 569251 : || GET_CODE (op0) == ASHIFT
4353 569117 : || GET_CODE (op0) == ROTATE
4354 568975 : || GET_CODE (op0) == ROTATERT))
4355 : {
4356 91482 : tem = simplify_distributive_operation (code, mode, op0, op1);
4357 91482 : if (tem)
4358 : return tem;
4359 : }
4360 :
4361 : /* Convert (xor (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4362 : mode size to (rotate A CX). */
4363 1731122 : tem = simplify_rotate_op (op0, op1, mode);
4364 1731122 : if (tem)
4365 : return tem;
4366 :
4367 : /* Convert (xor (and (not A) B) A) into A | B. */
4368 1729742 : if (GET_CODE (op0) == AND
4369 1729742 : && negated_ops_p (XEXP (op0, 0), op1))
4370 0 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4371 :
4372 : /* Convert (xor (and (rotate (~1) A) B) (ashift 1 A))
4373 : into B | (1 << A). */
4374 1729742 : if (SHIFT_COUNT_TRUNCATED
4375 : && GET_CODE (op0) == AND
4376 : && GET_CODE (XEXP (op0, 0)) == ROTATE
4377 : && CONST_INT_P (XEXP (XEXP (op0, 0), 0))
4378 : && INTVAL (XEXP (XEXP (op0, 0), 0)) == -2
4379 : && GET_CODE (op1) == ASHIFT
4380 : && CONST_INT_P (XEXP (op1, 0))
4381 : && INTVAL (XEXP (op1, 0)) == 1
4382 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), XEXP (op1, 1))
4383 : && !side_effects_p (XEXP (op1, 1)))
4384 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4385 :
4386 1729742 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4387 1729742 : if (tem)
4388 : return tem;
4389 :
4390 1729741 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4391 1729741 : if (tem)
4392 : return tem;
4393 :
4394 1729741 : tem = simplify_associative_operation (code, mode, op0, op1);
4395 1729741 : if (tem)
4396 : return tem;
4397 : break;
4398 :
4399 25579973 : case AND:
4400 25579973 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
4401 : return trueop1;
4402 25248472 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
4403 : return op0;
4404 24838184 : if (HWI_COMPUTABLE_MODE_P (mode))
4405 : {
4406 : /* When WORD_REGISTER_OPERATIONS is true, we need to know the
4407 : nonzero bits in WORD_MODE rather than MODE. */
4408 21783043 : scalar_int_mode tmode = as_a <scalar_int_mode> (mode);
4409 21783043 : if (WORD_REGISTER_OPERATIONS
4410 : && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
4411 : tmode = word_mode;
4412 21783043 : HOST_WIDE_INT nzop0 = nonzero_bits (trueop0, tmode);
4413 21783043 : HOST_WIDE_INT nzop1;
4414 21783043 : if (CONST_INT_P (trueop1))
4415 : {
4416 18434619 : HOST_WIDE_INT val1 = INTVAL (trueop1);
4417 : /* If we are turning off bits already known off in OP0, we need
4418 : not do an AND. */
4419 18434619 : if ((nzop0 & ~val1) == 0)
4420 450822 : return op0;
4421 :
4422 : /* Canonicalize (and (subreg (lshiftrt X shift)) mask) into
4423 : (and (lshiftrt (subreg X) shift) mask).
4424 :
4425 : Keeps shift and AND in the same mode, improving recognition.
4426 : Only applied when subreg is a lowpart, shift is valid,
4427 : and no precision is lost. */
4428 18070851 : if (SUBREG_P (op0)
4429 6461361 : && subreg_lowpart_p (op0)
4430 6444283 : && !paradoxical_subreg_p (op0)
4431 801972 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
4432 : /* simplify_subreg asserts the object being accessed is not
4433 : VOIDmode or BLKmode. We may have a REG_EQUAL note which
4434 : is not simplified and the source operand is a constant,
4435 : and thus VOIDmode. Guard against that. */
4436 117020 : && GET_MODE (XEXP (XEXP (op0, 0), 0)) != VOIDmode
4437 116970 : && GET_MODE (XEXP (XEXP (op0, 0), 0)) != BLKmode
4438 116970 : && !CONST_INT_P (XEXP (XEXP (op0, 0), 0))
4439 116970 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
4440 95951 : && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
4441 95951 : && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT
4442 18166777 : && ((INTVAL (XEXP (XEXP (op0, 0), 1))
4443 95926 : + floor_log2 (val1))
4444 18070851 : < GET_MODE_PRECISION (as_a <scalar_int_mode> (mode))))
4445 : {
4446 13405 : tem = XEXP (XEXP (op0, 0), 0);
4447 13405 : if (SUBREG_P (tem))
4448 : {
4449 296 : if (subreg_lowpart_p (tem))
4450 296 : tem = SUBREG_REG (tem);
4451 : else
4452 : tem = NULL_RTX;
4453 : }
4454 296 : if (tem != NULL_RTX)
4455 : {
4456 13405 : offset = subreg_lowpart_offset (mode, GET_MODE (tem));
4457 13405 : tem = simplify_gen_subreg (mode, tem, GET_MODE (tem),
4458 13405 : offset);
4459 13405 : if (tem)
4460 : {
4461 13405 : unsigned shiftamt = INTVAL (XEXP (XEXP (op0, 0), 1));
4462 13405 : rtx shiftamtrtx = gen_int_shift_amount (mode,
4463 13405 : shiftamt);
4464 13405 : op0 = simplify_gen_binary (LSHIFTRT, mode, tem,
4465 : shiftamtrtx);
4466 13405 : return simplify_gen_binary (AND, mode, op0, op1);
4467 : }
4468 : }
4469 : }
4470 : }
4471 21405870 : nzop1 = nonzero_bits (trueop1, mode);
4472 : /* If we are clearing all the nonzero bits, the result is zero. */
4473 21405870 : if ((nzop1 & nzop0) == 0
4474 21405870 : && !side_effects_p (op0) && !side_effects_p (op1))
4475 73649 : return CONST0_RTX (mode);
4476 : }
4477 24392180 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0)
4478 24392176 : && GET_MODE_CLASS (mode) != MODE_CC)
4479 : return op0;
4480 : /* A & (~A) -> 0 */
4481 633783 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
4482 24378869 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
4483 3727 : && ! side_effects_p (op0)
4484 24386274 : && GET_MODE_CLASS (mode) != MODE_CC)
4485 3726 : return CONST0_RTX (mode);
4486 :
4487 : /* Convert (and (plus (A - 1)) (neg A)) to 0. */
4488 24378822 : if (match_plus_neg_pattern (op0, op1, mode))
4489 2 : return CONST0_RTX (mode);
4490 :
4491 : /* Transform (and (extend X) C) into (zero_extend (and X C)) if
4492 : there are no nonzero bits of C outside of X's mode. */
4493 48757640 : if ((GET_CODE (op0) == SIGN_EXTEND
4494 24378820 : || GET_CODE (op0) == ZERO_EXTEND)
4495 95778 : && CONST_SCALAR_INT_P (trueop1)
4496 82535 : && is_a <scalar_int_mode> (mode, &int_mode)
4497 82535 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
4498 24461355 : && (wi::mask (GET_MODE_PRECISION (inner_mode), true,
4499 82535 : GET_MODE_PRECISION (int_mode))
4500 24461355 : & rtx_mode_t (trueop1, mode)) == 0)
4501 : {
4502 80460 : machine_mode imode = GET_MODE (XEXP (op0, 0));
4503 80460 : tem = immed_wide_int_const (rtx_mode_t (trueop1, mode), imode);
4504 80460 : tem = simplify_gen_binary (AND, imode, XEXP (op0, 0), tem);
4505 80460 : return simplify_gen_unary (ZERO_EXTEND, mode, tem, imode);
4506 : }
4507 :
4508 : /* Transform (and (truncate X) C) into (truncate (and X C)). This way
4509 : we might be able to further simplify the AND with X and potentially
4510 : remove the truncation altogether. */
4511 24298360 : if (GET_CODE (op0) == TRUNCATE && CONST_INT_P (trueop1))
4512 : {
4513 6 : rtx x = XEXP (op0, 0);
4514 6 : machine_mode xmode = GET_MODE (x);
4515 6 : tem = simplify_gen_binary (AND, xmode, x,
4516 6 : gen_int_mode (INTVAL (trueop1), xmode));
4517 6 : return simplify_gen_unary (TRUNCATE, mode, tem, xmode);
4518 : }
4519 :
4520 : /* Canonicalize (A | C1) & C2 as (A & C2) | (C1 & C2). */
4521 24298354 : if (GET_CODE (op0) == IOR
4522 1478489 : && CONST_INT_P (trueop1)
4523 225844 : && CONST_INT_P (XEXP (op0, 1)))
4524 : {
4525 142186 : HOST_WIDE_INT tmp = INTVAL (trueop1) & INTVAL (XEXP (op0, 1));
4526 142186 : return simplify_gen_binary (IOR, mode,
4527 : simplify_gen_binary (AND, mode,
4528 : XEXP (op0, 0), op1),
4529 142186 : gen_int_mode (tmp, mode));
4530 : }
4531 :
4532 : /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
4533 : insn (and may simplify more). */
4534 24156168 : if (GET_CODE (op0) == XOR
4535 172308 : && rtx_equal_p (XEXP (op0, 0), op1)
4536 24157818 : && ! side_effects_p (op1))
4537 1650 : return simplify_gen_binary (AND, mode,
4538 : simplify_gen_unary (NOT, mode,
4539 : XEXP (op0, 1), mode),
4540 1650 : op1);
4541 :
4542 24154518 : if (GET_CODE (op0) == XOR
4543 170658 : && rtx_equal_p (XEXP (op0, 1), op1)
4544 24159103 : && ! side_effects_p (op1))
4545 4585 : return simplify_gen_binary (AND, mode,
4546 : simplify_gen_unary (NOT, mode,
4547 : XEXP (op0, 0), mode),
4548 4585 : op1);
4549 :
4550 : /* Similarly for (~(A ^ B)) & A. */
4551 24149933 : if (GET_CODE (op0) == NOT
4552 630105 : && GET_CODE (XEXP (op0, 0)) == XOR
4553 3914 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4554 24149987 : && ! side_effects_p (op1))
4555 54 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4556 :
4557 24149879 : if (GET_CODE (op0) == NOT
4558 630051 : && GET_CODE (XEXP (op0, 0)) == XOR
4559 3860 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4560 24149916 : && ! side_effects_p (op1))
4561 37 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4562 :
4563 : /* Convert (A | B) & A to A. */
4564 24149842 : if (GET_CODE (op0) == IOR
4565 1336303 : && (rtx_equal_p (XEXP (op0, 0), op1)
4566 1335778 : || rtx_equal_p (XEXP (op0, 1), op1))
4567 743 : && ! side_effects_p (XEXP (op0, 0))
4568 24150585 : && ! side_effects_p (XEXP (op0, 1)))
4569 : return op1;
4570 :
4571 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
4572 : ((A & N) + B) & M -> (A + B) & M
4573 : Similarly if (N & M) == 0,
4574 : ((A | N) + B) & M -> (A + B) & M
4575 : and for - instead of + and/or ^ instead of |.
4576 : Also, if (N & M) == 0, then
4577 : (A +- N) & M -> A & M. */
4578 24149099 : if (CONST_INT_P (trueop1)
4579 17856794 : && HWI_COMPUTABLE_MODE_P (mode)
4580 17834815 : && ~UINTVAL (trueop1)
4581 17834815 : && (UINTVAL (trueop1) & (UINTVAL (trueop1) + 1)) == 0
4582 35965167 : && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS))
4583 : {
4584 988360 : rtx pmop[2];
4585 988360 : int which;
4586 :
4587 988360 : pmop[0] = XEXP (op0, 0);
4588 988360 : pmop[1] = XEXP (op0, 1);
4589 :
4590 988360 : if (CONST_INT_P (pmop[1])
4591 487981 : && (UINTVAL (pmop[1]) & UINTVAL (trueop1)) == 0)
4592 171157 : return simplify_gen_binary (AND, mode, pmop[0], op1);
4593 :
4594 2474643 : for (which = 0; which < 2; which++)
4595 : {
4596 1649762 : tem = pmop[which];
4597 1649762 : switch (GET_CODE (tem))
4598 : {
4599 14846 : case AND:
4600 14846 : if (CONST_INT_P (XEXP (tem, 1))
4601 13245 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1))
4602 : == UINTVAL (trueop1))
4603 7669 : pmop[which] = XEXP (tem, 0);
4604 : break;
4605 1338 : case IOR:
4606 1338 : case XOR:
4607 1338 : if (CONST_INT_P (XEXP (tem, 1))
4608 168 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1)) == 0)
4609 9 : pmop[which] = XEXP (tem, 0);
4610 : break;
4611 : default:
4612 : break;
4613 : }
4614 : }
4615 :
4616 824881 : if (pmop[0] != XEXP (op0, 0) || pmop[1] != XEXP (op0, 1))
4617 : {
4618 7678 : tem = simplify_gen_binary (GET_CODE (op0), mode,
4619 : pmop[0], pmop[1]);
4620 7678 : return simplify_gen_binary (code, mode, tem, op1);
4621 : }
4622 : }
4623 :
4624 : /* (and X (ior (not X) Y) -> (and X Y) */
4625 23977942 : if (GET_CODE (op1) == IOR
4626 1034168 : && GET_CODE (XEXP (op1, 0)) == NOT
4627 23983367 : && rtx_equal_p (op0, XEXP (XEXP (op1, 0), 0)))
4628 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 1));
4629 :
4630 : /* (and (ior (not X) Y) X) -> (and X Y) */
4631 23977942 : if (GET_CODE (op0) == IOR
4632 1335560 : && GET_CODE (XEXP (op0, 0)) == NOT
4633 24029972 : && rtx_equal_p (op1, XEXP (XEXP (op0, 0), 0)))
4634 6 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 1));
4635 :
4636 : /* (and X (ior Y (not X)) -> (and X Y) */
4637 23977936 : if (GET_CODE (op1) == IOR
4638 1034168 : && GET_CODE (XEXP (op1, 1)) == NOT
4639 23978500 : && rtx_equal_p (op0, XEXP (XEXP (op1, 1), 0)))
4640 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 0));
4641 :
4642 : /* (and (ior Y (not X)) X) -> (and X Y) */
4643 23977936 : if (GET_CODE (op0) == IOR
4644 1335554 : && GET_CODE (XEXP (op0, 1)) == NOT
4645 23985325 : && rtx_equal_p (op1, XEXP (XEXP (op0, 1), 0)))
4646 76 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 0));
4647 :
4648 : /* (and (ior/xor X Y) (not Y)) -> X & ~Y */
4649 23977860 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4650 23977860 : && negated_ops_p (op1, XEXP (op0, 1)))
4651 36 : return simplify_gen_binary (AND, mode, XEXP (op0, 0),
4652 : simplify_gen_unary (NOT, mode,
4653 : XEXP (op0, 1),
4654 36 : mode));
4655 : /* (and (ior/xor Y X) (not Y)) -> X & ~Y */
4656 23977824 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4657 23977824 : && negated_ops_p (op1, XEXP (op0, 0)))
4658 1 : return simplify_gen_binary (AND, mode, XEXP (op0, 1),
4659 : simplify_gen_unary (NOT, mode,
4660 : XEXP (op0, 0),
4661 1 : mode));
4662 :
4663 : /* Convert (and (ior A C) (ior B C)) into (ior (and A B) C). */
4664 23977823 : if (GET_CODE (op0) == GET_CODE (op1)
4665 2429404 : && (GET_CODE (op0) == AND
4666 : || GET_CODE (op0) == IOR
4667 2429404 : || GET_CODE (op0) == LSHIFTRT
4668 1395768 : || GET_CODE (op0) == ASHIFTRT
4669 1395616 : || GET_CODE (op0) == ASHIFT
4670 1395450 : || GET_CODE (op0) == ROTATE
4671 1395450 : || GET_CODE (op0) == ROTATERT))
4672 : {
4673 1033954 : tem = simplify_distributive_operation (code, mode, op0, op1);
4674 1033954 : if (tem)
4675 : return tem;
4676 : }
4677 :
4678 : /* (and:v4si
4679 : (ashiftrt:v4si A 16)
4680 : (const_vector: 0xffff x4))
4681 : is just (lshiftrt:v4si A 16). */
4682 22992553 : if (VECTOR_MODE_P (mode) && GET_CODE (op0) == ASHIFTRT
4683 4839 : && (CONST_INT_P (XEXP (op0, 1))
4684 2035 : || (GET_CODE (XEXP (op0, 1)) == CONST_VECTOR
4685 94 : && const_vec_duplicate_p (XEXP (op0, 1))
4686 0 : && CONST_INT_P (XVECEXP (XEXP (op0, 1), 0, 0))))
4687 2804 : && GET_CODE (op1) == CONST_VECTOR
4688 22992581 : && const_vec_duplicate_p (op1)
4689 22992623 : && CONST_INT_P (XVECEXP (op1, 0, 0)))
4690 : {
4691 136 : unsigned HOST_WIDE_INT shift_count
4692 : = (CONST_INT_P (XEXP (op0, 1))
4693 68 : ? UINTVAL (XEXP (op0, 1))
4694 0 : : UINTVAL (XVECEXP (XEXP (op0, 1), 0, 0)));
4695 68 : unsigned HOST_WIDE_INT inner_prec
4696 136 : = GET_MODE_PRECISION (GET_MODE_INNER (mode));
4697 :
4698 : /* Avoid UD shift count. */
4699 68 : if (shift_count < inner_prec
4700 56 : && (UINTVAL (XVECEXP (op1, 0, 0))
4701 56 : == (HOST_WIDE_INT_1U << (inner_prec - shift_count)) - 1))
4702 42 : return simplify_gen_binary (LSHIFTRT, mode, XEXP (op0, 0), XEXP (op0, 1));
4703 : }
4704 :
4705 22992511 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4706 22992511 : if (tem)
4707 : return tem;
4708 :
4709 22990085 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4710 22990085 : if (tem)
4711 : return tem;
4712 :
4713 22989804 : tem = simplify_associative_operation (code, mode, op0, op1);
4714 22989804 : if (tem)
4715 : return tem;
4716 : break;
4717 :
4718 902288 : case UDIV:
4719 : /* 0/x is 0 (or x&0 if x has side-effects). */
4720 902288 : if (trueop0 == CONST0_RTX (mode)
4721 392 : && !cfun->can_throw_non_call_exceptions)
4722 : {
4723 392 : if (side_effects_p (op1))
4724 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4725 : return trueop0;
4726 : }
4727 : /* x/1 is x. */
4728 901896 : if (trueop1 == CONST1_RTX (mode))
4729 : {
4730 242866 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4731 242866 : if (tem)
4732 : return tem;
4733 : }
4734 : /* Convert divide by power of two into shift. */
4735 659030 : if (CONST_INT_P (trueop1)
4736 984089 : && (val = exact_log2 (UINTVAL (trueop1))) > 0)
4737 325059 : return simplify_gen_binary (LSHIFTRT, mode, op0,
4738 325059 : gen_int_shift_amount (mode, val));
4739 : break;
4740 :
4741 1434672 : case DIV:
4742 : /* Handle floating point and integers separately. */
4743 1434672 : if (SCALAR_FLOAT_MODE_P (mode))
4744 : {
4745 : /* Maybe change 0.0 / x to 0.0. This transformation isn't
4746 : safe for modes with NaNs, since 0.0 / 0.0 will then be
4747 : NaN rather than 0.0. Nor is it safe for modes with signed
4748 : zeros, since dividing 0 by a negative number gives -0.0 */
4749 339115 : if (trueop0 == CONST0_RTX (mode)
4750 2820 : && !HONOR_NANS (mode)
4751 14 : && !HONOR_SIGNED_ZEROS (mode)
4752 339129 : && ! side_effects_p (op1))
4753 : return op0;
4754 : /* x/1.0 is x. */
4755 339101 : if (trueop1 == CONST1_RTX (mode)
4756 339101 : && !HONOR_SNANS (mode))
4757 : return op0;
4758 :
4759 339097 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
4760 34538 : && trueop1 != CONST0_RTX (mode))
4761 : {
4762 28464 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
4763 :
4764 : /* x/-1.0 is -x. */
4765 28464 : if (real_equal (d1, &dconstm1)
4766 28464 : && !HONOR_SNANS (mode))
4767 0 : return simplify_gen_unary (NEG, mode, op0, mode);
4768 :
4769 : /* Change FP division by a constant into multiplication.
4770 : Only do this with -freciprocal-math. */
4771 28464 : if (flag_reciprocal_math
4772 28464 : && !real_equal (d1, &dconst0))
4773 : {
4774 7 : REAL_VALUE_TYPE d;
4775 7 : real_arithmetic (&d, RDIV_EXPR, &dconst1, d1);
4776 7 : tem = const_double_from_real_value (d, mode);
4777 7 : return simplify_gen_binary (MULT, mode, op0, tem);
4778 : }
4779 : }
4780 : }
4781 1095557 : else if (SCALAR_INT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
4782 : {
4783 : /* 0/x is 0 (or x&0 if x has side-effects). */
4784 1063399 : if (trueop0 == CONST0_RTX (mode)
4785 1108 : && !cfun->can_throw_non_call_exceptions)
4786 : {
4787 1054 : if (side_effects_p (op1))
4788 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4789 : return trueop0;
4790 : }
4791 : /* x/1 is x. */
4792 1062345 : if (trueop1 == CONST1_RTX (mode))
4793 : {
4794 92 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4795 92 : if (tem)
4796 : return tem;
4797 : }
4798 : /* x/-1 is -x. */
4799 1062253 : if (trueop1 == CONSTM1_RTX (mode))
4800 : {
4801 622 : rtx x = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4802 622 : if (x)
4803 622 : return simplify_gen_unary (NEG, mode, x, mode);
4804 : }
4805 : }
4806 : break;
4807 :
4808 914926 : case UMOD:
4809 : /* 0%x is 0 (or x&0 if x has side-effects). */
4810 914926 : if (trueop0 == CONST0_RTX (mode))
4811 : {
4812 951 : if (side_effects_p (op1))
4813 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4814 : return trueop0;
4815 : }
4816 : /* x%1 is 0 (of x&0 if x has side-effects). */
4817 913975 : if (trueop1 == CONST1_RTX (mode))
4818 : {
4819 269373 : if (side_effects_p (op0))
4820 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4821 269373 : return CONST0_RTX (mode);
4822 : }
4823 : /* Implement modulus by power of two as AND. */
4824 644602 : if (CONST_INT_P (trueop1)
4825 943620 : && exact_log2 (UINTVAL (trueop1)) > 0)
4826 299018 : return simplify_gen_binary (AND, mode, op0,
4827 299018 : gen_int_mode (UINTVAL (trueop1) - 1,
4828 : mode));
4829 : break;
4830 :
4831 602787 : case MOD:
4832 : /* 0%x is 0 (or x&0 if x has side-effects). */
4833 602787 : if (trueop0 == CONST0_RTX (mode))
4834 : {
4835 1366 : if (side_effects_p (op1))
4836 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4837 : return trueop0;
4838 : }
4839 : /* x%1 and x%-1 is 0 (or x&0 if x has side-effects). */
4840 601421 : if (trueop1 == CONST1_RTX (mode) || trueop1 == constm1_rtx)
4841 : {
4842 600 : if (side_effects_p (op0))
4843 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4844 600 : return CONST0_RTX (mode);
4845 : }
4846 : break;
4847 :
4848 142750 : case ROTATERT:
4849 142750 : case ROTATE:
4850 142750 : if (trueop1 == CONST0_RTX (mode))
4851 : return op0;
4852 :
4853 : /* Reversing the direction removes a negated count modulo the
4854 : precision. Keep the mask so that the new count is in range even
4855 : on targets that do not truncate rotate counts. */
4856 142660 : if (is_a <scalar_int_mode> (mode, &int_mode)
4857 134494 : && exact_log2 (GET_MODE_PRECISION (int_mode)) >= 0)
4858 : {
4859 134390 : rtx count = op1;
4860 134390 : if ((SUBREG_P (count)
4861 23321 : && subreg_lowpart_p (count)
4862 23321 : && !paradoxical_subreg_p (count))
4863 134390 : || GET_CODE (count) == TRUNCATE)
4864 23465 : count = XEXP (count, 0);
4865 :
4866 134390 : scalar_int_mode count_mode;
4867 134390 : if (GET_CODE (count) == AND
4868 9072 : && GET_CODE (XEXP (count, 0)) == NEG
4869 736 : && CONST_INT_P (XEXP (count, 1))
4870 736 : && INTVAL (XEXP (count, 1))
4871 736 : == GET_MODE_PRECISION (int_mode) - 1
4872 504 : && is_a <scalar_int_mode> (GET_MODE (count), &inner_mode)
4873 142724 : && is_a <scalar_int_mode> (GET_MODE (op1), &count_mode)
4874 1008 : && GET_MODE_PRECISION (count_mode)
4875 1008 : > exact_log2 (GET_MODE_PRECISION (int_mode))
4876 134390 : && GET_MODE_PRECISION (inner_mode)
4877 440 : >= GET_MODE_PRECISION (count_mode))
4878 : {
4879 440 : tem = simplify_gen_binary (AND, inner_mode,
4880 : XEXP (XEXP (count, 0), 0),
4881 : XEXP (count, 1));
4882 440 : if (count != op1)
4883 240 : tem = simplify_gen_unary (TRUNCATE, count_mode, tem,
4884 : inner_mode);
4885 440 : return simplify_gen_binary (code == ROTATE ? ROTATERT : ROTATE,
4886 440 : mode, op0, tem);
4887 : }
4888 : }
4889 :
4890 : /* Canonicalize rotates by constant amount. If the condition of
4891 : reversing direction is met, then reverse the direction. */
4892 : #if defined(HAVE_rotate) && defined(HAVE_rotatert)
4893 142220 : if (reverse_rotate_by_imm_p (mode, (code == ROTATE), trueop1))
4894 : {
4895 12173 : int new_amount = GET_MODE_UNIT_PRECISION (mode) - INTVAL (trueop1);
4896 12173 : rtx new_amount_rtx = gen_int_shift_amount (mode, new_amount);
4897 12899 : return simplify_gen_binary (code == ROTATE ? ROTATERT : ROTATE,
4898 : mode, op0, new_amount_rtx);
4899 : }
4900 : #endif
4901 : /* ROTATE/ROTATERT:HI (X:HI, 8) is BSWAP:HI (X). Other combinations
4902 : such as SImode with a count of 16 do not correspond to RTL BSWAP
4903 : semantics. */
4904 130047 : tem = unwrap_const_vec_duplicate (trueop1);
4905 130047 : if (GET_MODE_UNIT_BITSIZE (mode) == (2 * BITS_PER_UNIT)
4906 130047 : && CONST_INT_P (tem) && INTVAL (tem) == BITS_PER_UNIT)
4907 666 : return simplify_gen_unary (BSWAP, mode, op0, mode);
4908 :
4909 : /* FALLTHRU */
4910 5609162 : case ASHIFTRT:
4911 5609162 : if (trueop1 == CONST0_RTX (mode))
4912 : return op0;
4913 5606826 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4914 : return op0;
4915 : /* Rotating ~0 always results in ~0. */
4916 5606645 : if (CONST_INT_P (trueop0)
4917 15559 : && HWI_COMPUTABLE_MODE_P (mode)
4918 15531 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
4919 5606645 : && ! side_effects_p (op1))
4920 : return op0;
4921 :
4922 32818268 : canonicalize_shift:
4923 : /* Given:
4924 : scalar modes M1, M2
4925 : scalar constants c1, c2
4926 : size (M2) > size (M1)
4927 : c1 == size (M2) - size (M1)
4928 : optimize:
4929 : ([a|l]shiftrt:M1 (subreg:M1 (lshiftrt:M2 (reg:M2) (const_int <c1>))
4930 : <low_part>)
4931 : (const_int <c2>))
4932 : to:
4933 : (subreg:M1 ([a|l]shiftrt:M2 (reg:M2) (const_int <c1 + c2>))
4934 : <low_part>). */
4935 32818268 : if ((code == ASHIFTRT || code == LSHIFTRT)
4936 12479649 : && is_a <scalar_int_mode> (mode, &int_mode)
4937 11693956 : && SUBREG_P (op0)
4938 1269949 : && CONST_INT_P (op1)
4939 1266967 : && GET_CODE (SUBREG_REG (op0)) == LSHIFTRT
4940 19483 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
4941 : &inner_mode)
4942 19483 : && CONST_INT_P (XEXP (SUBREG_REG (op0), 1))
4943 38580 : && GET_MODE_BITSIZE (inner_mode) > GET_MODE_BITSIZE (int_mode)
4944 19290 : && (INTVAL (XEXP (SUBREG_REG (op0), 1))
4945 38580 : == GET_MODE_BITSIZE (inner_mode) - GET_MODE_BITSIZE (int_mode))
4946 32837307 : && subreg_lowpart_p (op0))
4947 : {
4948 19039 : rtx tmp = gen_int_shift_amount
4949 19039 : (inner_mode, INTVAL (XEXP (SUBREG_REG (op0), 1)) + INTVAL (op1));
4950 :
4951 : /* Combine would usually zero out the value when combining two
4952 : local shifts and the range becomes larger or equal to the mode.
4953 : However since we fold away one of the shifts here combine won't
4954 : see it so we should immediately zero the result if it's out of
4955 : range. */
4956 19039 : if (code == LSHIFTRT
4957 34551 : && INTVAL (tmp) >= GET_MODE_BITSIZE (inner_mode))
4958 0 : tmp = const0_rtx;
4959 : else
4960 19039 : tmp = simplify_gen_binary (code,
4961 : inner_mode,
4962 19039 : XEXP (SUBREG_REG (op0), 0),
4963 : tmp);
4964 :
4965 19039 : return lowpart_subreg (int_mode, tmp, inner_mode);
4966 : }
4967 :
4968 32799229 : if (SHIFT_COUNT_TRUNCATED && CONST_INT_P (op1))
4969 : {
4970 : val = INTVAL (op1) & (GET_MODE_UNIT_PRECISION (mode) - 1);
4971 : if (val != INTVAL (op1))
4972 : return simplify_gen_binary (code, mode, op0,
4973 : gen_int_shift_amount (mode, val));
4974 : }
4975 :
4976 : /* Simplify:
4977 :
4978 : (code:M1
4979 : (subreg:M1
4980 : ([al]shiftrt:M2
4981 : (subreg:M2
4982 : (ashift:M1 X C1))
4983 : C2))
4984 : C3)
4985 :
4986 : to:
4987 :
4988 : (code:M1
4989 : ([al]shiftrt:M1
4990 : (ashift:M1 X C1+N)
4991 : C2+N)
4992 : C3)
4993 :
4994 : where M1 is N bits wider than M2. Optimizing the (subreg:M1 ...)
4995 : directly would be arithmetically correct, but restricting the
4996 : simplification to shifts by constants is more conservative,
4997 : since it is more likely to lead to further simplifications. */
4998 32799229 : if (is_a<scalar_int_mode> (mode, &int_mode)
4999 5974948 : && paradoxical_subreg_p (op0)
5000 5455698 : && is_a<scalar_int_mode> (GET_MODE (SUBREG_REG (op0)), &inner_mode)
5001 5426678 : && (GET_CODE (SUBREG_REG (op0)) == ASHIFTRT
5002 5426678 : || GET_CODE (SUBREG_REG (op0)) == LSHIFTRT)
5003 140443 : && CONST_INT_P (op1))
5004 : {
5005 140443 : auto xcode = GET_CODE (SUBREG_REG (op0));
5006 140443 : rtx xop0 = XEXP (SUBREG_REG (op0), 0);
5007 140443 : rtx xop1 = XEXP (SUBREG_REG (op0), 1);
5008 140443 : if (SUBREG_P (xop0)
5009 10132 : && GET_MODE (SUBREG_REG (xop0)) == mode
5010 9982 : && GET_CODE (SUBREG_REG (xop0)) == ASHIFT
5011 595 : && CONST_INT_P (xop1)
5012 141038 : && UINTVAL (xop1) < GET_MODE_PRECISION (inner_mode))
5013 : {
5014 595 : rtx yop0 = XEXP (SUBREG_REG (xop0), 0);
5015 595 : rtx yop1 = XEXP (SUBREG_REG (xop0), 1);
5016 595 : if (CONST_INT_P (yop1)
5017 595 : && UINTVAL (yop1) < GET_MODE_PRECISION (inner_mode))
5018 : {
5019 1190 : auto bias = (GET_MODE_BITSIZE (int_mode)
5020 595 : - GET_MODE_BITSIZE (inner_mode));
5021 595 : tem = simplify_gen_binary (ASHIFT, mode, yop0,
5022 595 : GEN_INT (INTVAL (yop1) + bias));
5023 595 : tem = simplify_gen_binary (xcode, mode, tem,
5024 595 : GEN_INT (INTVAL (xop1) + bias));
5025 595 : return simplify_gen_binary (code, mode, tem, op1);
5026 : }
5027 : }
5028 : }
5029 : break;
5030 :
5031 0 : case SS_ASHIFT:
5032 0 : if (CONST_INT_P (trueop0)
5033 0 : && HWI_COMPUTABLE_MODE_P (mode)
5034 0 : && (UINTVAL (trueop0) == (GET_MODE_MASK (mode) >> 1)
5035 0 : || mode_signbit_p (mode, trueop0))
5036 0 : && ! side_effects_p (op1))
5037 : return op0;
5038 0 : goto simplify_ashift;
5039 :
5040 0 : case US_ASHIFT:
5041 0 : if (CONST_INT_P (trueop0)
5042 0 : && HWI_COMPUTABLE_MODE_P (mode)
5043 0 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
5044 0 : && ! side_effects_p (op1))
5045 : return op0;
5046 : /* FALLTHRU */
5047 :
5048 20628726 : case ASHIFT:
5049 20628726 : simplify_ashift:
5050 20628726 : if (trueop1 == CONST0_RTX (mode))
5051 : return op0;
5052 20456118 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
5053 : return op0;
5054 20426295 : if (mem_depth
5055 217026 : && code == ASHIFT
5056 217026 : && CONST_INT_P (trueop1)
5057 217018 : && is_a <scalar_int_mode> (mode, &int_mode)
5058 20643301 : && IN_RANGE (UINTVAL (trueop1),
5059 : 1, GET_MODE_PRECISION (int_mode) - 1))
5060 : {
5061 217006 : auto c = (wi::one (GET_MODE_PRECISION (int_mode))
5062 217006 : << UINTVAL (trueop1));
5063 217006 : rtx new_op1 = immed_wide_int_const (c, int_mode);
5064 217006 : return simplify_gen_binary (MULT, int_mode, op0, new_op1);
5065 217006 : }
5066 :
5067 : /* If we're shifting left a signed bitfield extraction and the
5068 : shift count + bitfield size is a natural integral mode and
5069 : the field starts at offset 0 (counting from the LSB), then
5070 : this can be simplified to a sign extension of a left shift.
5071 :
5072 : Some ISAs (RISC-V 64-bit) have inherent support for such
5073 : instructions and it's better for various optimizations to
5074 : express as a SIGN_EXTEND rather than a shifted SIGN_EXTRACT. */
5075 20209289 : if (GET_CODE (op0) == SIGN_EXTRACT
5076 26 : && REG_P (XEXP (op0, 0))
5077 : /* The size of the bitfield, the location of the bitfield and
5078 : shift count must be CONST_INTs. */
5079 20 : && CONST_INT_P (op1)
5080 20 : && CONST_INT_P (XEXP (op0, 1))
5081 20 : && CONST_INT_P (XEXP (op0, 2)))
5082 : {
5083 20 : int size = INTVAL (op1) + INTVAL (XEXP (op0, 1));
5084 20 : machine_mode smaller_mode;
5085 : /* Now we need to verify the size of the bitfield plus the shift
5086 : count is an integral mode and smaller than MODE. This is
5087 : requirement for using SIGN_EXTEND. We also need to verify the
5088 : field starts at bit location 0 and that the subreg lowpart also
5089 : starts at zero. */
5090 20 : if (int_mode_for_size (size, size).exists (&smaller_mode)
5091 1 : && mode > smaller_mode
5092 20 : && (subreg_lowpart_offset (smaller_mode, mode).to_constant ()
5093 1 : == UINTVAL (XEXP (op0, 2)))
5094 1 : && XEXP (op0, 2) == CONST0_RTX (mode))
5095 : {
5096 : /* Everything passed. So we just need to get the subreg of the
5097 : original input, shift it and sign extend the result. */
5098 1 : rtx op = gen_lowpart (smaller_mode, XEXP (op0, 0));
5099 1 : rtx x = gen_rtx_ASHIFT (smaller_mode, op, op1);
5100 1 : return gen_rtx_SIGN_EXTEND (mode, x);
5101 : }
5102 : }
5103 20209288 : goto canonicalize_shift;
5104 :
5105 9017401 : case LSHIFTRT:
5106 9017401 : if (trueop1 == CONST0_RTX (mode))
5107 : return op0;
5108 7003915 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
5109 : return op0;
5110 : /* Optimize (lshiftrt (clz X) C) as (eq X 0). */
5111 7002335 : if (GET_CODE (op0) == CLZ
5112 0 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
5113 0 : && CONST_INT_P (trueop1)
5114 : && STORE_FLAG_VALUE == 1
5115 7002335 : && INTVAL (trueop1) < GET_MODE_UNIT_PRECISION (mode))
5116 : {
5117 0 : unsigned HOST_WIDE_INT zero_val = 0;
5118 :
5119 0 : if (CLZ_DEFINED_VALUE_AT_ZERO (inner_mode, zero_val)
5120 0 : && zero_val == GET_MODE_PRECISION (inner_mode)
5121 0 : && INTVAL (trueop1) == exact_log2 (zero_val))
5122 0 : return simplify_gen_relational (EQ, mode, inner_mode,
5123 0 : XEXP (op0, 0), const0_rtx);
5124 : }
5125 7002335 : goto canonicalize_shift;
5126 :
5127 229162 : case SMIN:
5128 229162 : if (HWI_COMPUTABLE_MODE_P (mode)
5129 208044 : && mode_signbit_p (mode, trueop1)
5130 0 : && ! side_effects_p (op0))
5131 : return op1;
5132 229162 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5133 : return op0;
5134 228984 : tem = simplify_associative_operation (code, mode, op0, op1);
5135 228984 : if (tem)
5136 : return tem;
5137 : break;
5138 :
5139 727214 : case SMAX:
5140 727214 : if (HWI_COMPUTABLE_MODE_P (mode)
5141 699066 : && CONST_INT_P (trueop1)
5142 665554 : && (UINTVAL (trueop1) == GET_MODE_MASK (mode) >> 1)
5143 0 : && ! side_effects_p (op0))
5144 : return op1;
5145 727214 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5146 : return op0;
5147 727109 : if (INTEGRAL_MODE_P (mode))
5148 : {
5149 717090 : if (GET_CODE (op0) == NEG && rtx_equal_p (XEXP (op0, 0), op1))
5150 123 : return simplify_gen_unary (ABS, mode, op1, mode);
5151 716967 : else if (GET_CODE (op1) == NEG && rtx_equal_p (XEXP (op1, 0), op0))
5152 0 : return simplify_gen_unary (ABS, mode, op0, mode);
5153 : }
5154 726986 : tem = simplify_associative_operation (code, mode, op0, op1);
5155 726986 : if (tem)
5156 : return tem;
5157 : break;
5158 :
5159 413723 : case UMIN:
5160 413723 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
5161 : return op1;
5162 413648 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5163 : return op0;
5164 413529 : tem = simplify_associative_operation (code, mode, op0, op1);
5165 413529 : if (tem)
5166 : return tem;
5167 : break;
5168 :
5169 373828 : case UMAX:
5170 373828 : if (trueop1 == constm1_rtx && ! side_effects_p (op0))
5171 : return op1;
5172 373828 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5173 : return op0;
5174 373738 : tem = simplify_associative_operation (code, mode, op0, op1);
5175 373738 : if (tem)
5176 : return tem;
5177 : break;
5178 :
5179 12139 : case SS_PLUS:
5180 12139 : case US_PLUS:
5181 12139 : case SS_MINUS:
5182 12139 : case US_MINUS:
5183 : /* Simplify x +/- 0 to x, if possible. */
5184 12139 : if (trueop1 == CONST0_RTX (mode))
5185 0 : return op0;
5186 : return 0;
5187 :
5188 0 : case SS_MULT:
5189 0 : case US_MULT:
5190 : /* Simplify x * 0 to 0, if possible. */
5191 0 : if (trueop1 == CONST0_RTX (mode)
5192 0 : && !side_effects_p (op0))
5193 : return op1;
5194 :
5195 : /* Simplify x * 1 to x, if possible. */
5196 0 : if (trueop1 == CONST1_RTX (mode))
5197 0 : return op0;
5198 : return 0;
5199 :
5200 201159 : case SMUL_HIGHPART:
5201 : /* Simplify x h* 0 to 0, if possible. */
5202 201159 : if (trueop1 == CONST0_RTX (mode)
5203 201159 : && !side_effects_p (op0))
5204 : return op1;
5205 : /* Simplify x h* 1 to sign(x). */
5206 201159 : if (trueop1 == const1_rtx
5207 201159 : && is_a <scalar_int_mode> (mode, &int_mode))
5208 : {
5209 0 : HOST_WIDE_INT bits = GET_MODE_PRECISION (int_mode) - 1;
5210 0 : return simplify_gen_binary (ASHIFTRT, mode, op0,
5211 0 : gen_int_shift_amount (mode, bits));
5212 : }
5213 : /* Simplify SMUL_HIGHPART by power of two as ASHIFRT. */
5214 201159 : if (CONST_INT_P (trueop1)
5215 89986 : && INTVAL (trueop1) > 1
5216 277820 : && is_a <scalar_int_mode> (mode, &int_mode))
5217 : {
5218 76661 : HOST_WIDE_INT prec = GET_MODE_PRECISION (int_mode);
5219 76661 : val = wi::exact_log2 (INTVAL (trueop1));
5220 76661 : if (val > 0 && val < prec)
5221 0 : return simplify_gen_binary (ASHIFTRT, mode, op0,
5222 : gen_int_shift_amount (mode,
5223 0 : prec - val));
5224 : }
5225 : return 0;
5226 :
5227 318125 : case UMUL_HIGHPART:
5228 : /* Simplify x h* 0 to 0, if possible. */
5229 318125 : if (trueop1 == CONST0_RTX (mode)
5230 318125 : && !side_effects_p (op0))
5231 : return op1;
5232 : /* Simplify x h* 1 to 0. */
5233 318051 : if (trueop1 == CONST1_RTX (mode)
5234 318051 : && !side_effects_p (op1))
5235 0 : return CONST0_RTX (mode);
5236 : /* Simplify UMUL_HIGHPART by power of two as LSHIFRT. */
5237 318051 : if (CONST_INT_P (trueop1)
5238 160441 : && UINTVAL (trueop1) > 1
5239 478492 : && is_a <scalar_int_mode> (mode, &int_mode))
5240 : {
5241 160441 : HOST_WIDE_INT prec = GET_MODE_PRECISION (int_mode);
5242 160441 : val = wi::exact_log2 (UINTVAL (trueop1));
5243 160441 : if (val > 0 && val < prec)
5244 58 : return simplify_gen_binary (LSHIFTRT, mode, op0,
5245 : gen_int_shift_amount (mode,
5246 58 : prec - val));
5247 : }
5248 : return 0;
5249 :
5250 0 : case SS_DIV:
5251 0 : case US_DIV:
5252 : /* Simplify x / 1 to x, if possible. */
5253 0 : if (trueop1 == CONST1_RTX (mode))
5254 0 : return op0;
5255 : return 0;
5256 :
5257 0 : case COPYSIGN:
5258 0 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5259 : return op0;
5260 0 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1))
5261 : {
5262 0 : REAL_VALUE_TYPE f1;
5263 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
5264 0 : rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
5265 0 : if (REAL_VALUE_NEGATIVE (f1))
5266 0 : tmp = simplify_unary_operation (NEG, mode, tmp, mode);
5267 0 : return tmp;
5268 : }
5269 0 : if (GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
5270 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
5271 0 : if (GET_CODE (op1) == ABS
5272 0 : && ! side_effects_p (op1))
5273 0 : return simplify_gen_unary (ABS, mode, op0, mode);
5274 0 : if (GET_CODE (op0) == COPYSIGN
5275 0 : && ! side_effects_p (XEXP (op0, 1)))
5276 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
5277 0 : if (GET_CODE (op1) == COPYSIGN
5278 0 : && ! side_effects_p (XEXP (op1, 0)))
5279 0 : return simplify_gen_binary (COPYSIGN, mode, op0, XEXP (op1, 1));
5280 : return 0;
5281 :
5282 1124 : case VEC_SERIES:
5283 2248 : if (op1 == CONST0_RTX (GET_MODE_INNER (mode)))
5284 92 : return gen_vec_duplicate (mode, op0);
5285 1032 : if (valid_for_const_vector_p (mode, op0)
5286 1032 : && valid_for_const_vector_p (mode, op1))
5287 93 : return gen_const_vec_series (mode, op0, op1);
5288 : return 0;
5289 :
5290 3732010 : case VEC_SELECT:
5291 3732010 : if (!VECTOR_MODE_P (mode))
5292 : {
5293 842630 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
5294 1685260 : gcc_assert (mode == GET_MODE_INNER (GET_MODE (trueop0)));
5295 842630 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
5296 842630 : gcc_assert (XVECLEN (trueop1, 0) == 1);
5297 :
5298 : /* We can't reason about selections made at runtime. */
5299 842630 : if (!CONST_INT_P (XVECEXP (trueop1, 0, 0)))
5300 464201930 : return 0;
5301 :
5302 842630 : if (vec_duplicate_p (trueop0, &elt0))
5303 2156 : return elt0;
5304 :
5305 840474 : if (GET_CODE (trueop0) == CONST_VECTOR)
5306 7377 : return CONST_VECTOR_ELT (trueop0, INTVAL (XVECEXP
5307 : (trueop1, 0, 0)));
5308 :
5309 : /* Extract a scalar element from a nested VEC_SELECT expression
5310 : (with optional nested VEC_CONCAT expression). Some targets
5311 : (i386) extract scalar element from a vector using chain of
5312 : nested VEC_SELECT expressions. When input operand is a memory
5313 : operand, this operation can be simplified to a simple scalar
5314 : load from an offsetted memory address. */
5315 833097 : int n_elts;
5316 833097 : if (GET_CODE (trueop0) == VEC_SELECT
5317 907502 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
5318 74405 : .is_constant (&n_elts)))
5319 : {
5320 74405 : rtx op0 = XEXP (trueop0, 0);
5321 74405 : rtx op1 = XEXP (trueop0, 1);
5322 :
5323 74405 : int i = INTVAL (XVECEXP (trueop1, 0, 0));
5324 74405 : int elem;
5325 :
5326 74405 : rtvec vec;
5327 74405 : rtx tmp_op, tmp;
5328 :
5329 74405 : gcc_assert (GET_CODE (op1) == PARALLEL);
5330 74405 : gcc_assert (i < XVECLEN (op1, 0));
5331 :
5332 : /* Select element, pointed by nested selector. */
5333 74405 : elem = INTVAL (XVECEXP (op1, 0, i));
5334 :
5335 74405 : gcc_assert (elem < n_elts);
5336 :
5337 : /* Handle the case when nested VEC_SELECT wraps VEC_CONCAT. */
5338 74405 : if (GET_CODE (op0) == VEC_CONCAT)
5339 : {
5340 30553 : rtx op00 = XEXP (op0, 0);
5341 30553 : rtx op01 = XEXP (op0, 1);
5342 :
5343 30553 : machine_mode mode00, mode01;
5344 30553 : int n_elts00, n_elts01;
5345 :
5346 30553 : mode00 = GET_MODE (op00);
5347 30553 : mode01 = GET_MODE (op01);
5348 :
5349 : /* Find out the number of elements of each operand.
5350 : Since the concatenated result has a constant number
5351 : of elements, the operands must too. */
5352 30553 : n_elts00 = GET_MODE_NUNITS (mode00).to_constant ();
5353 30553 : n_elts01 = GET_MODE_NUNITS (mode01).to_constant ();
5354 :
5355 30553 : gcc_assert (n_elts == n_elts00 + n_elts01);
5356 :
5357 : /* Select correct operand of VEC_CONCAT
5358 : and adjust selector. */
5359 30553 : if (elem < n_elts01)
5360 : tmp_op = op00;
5361 : else
5362 : {
5363 41 : tmp_op = op01;
5364 41 : elem -= n_elts00;
5365 : }
5366 : }
5367 : else
5368 : tmp_op = op0;
5369 :
5370 74405 : vec = rtvec_alloc (1);
5371 74405 : RTVEC_ELT (vec, 0) = GEN_INT (elem);
5372 :
5373 74405 : tmp = gen_rtx_fmt_ee (code, mode,
5374 : tmp_op, gen_rtx_PARALLEL (VOIDmode, vec));
5375 74405 : return tmp;
5376 : }
5377 : /* If we select one half of a vec_concat, return that. */
5378 758692 : else if (GET_CODE (trueop0) == VEC_CONCAT)
5379 : {
5380 721 : rtx subop0 = XEXP (trueop0, 0);
5381 721 : rtx subop1 = XEXP (trueop0, 1);
5382 721 : machine_mode mode0 = GET_MODE (subop0);
5383 721 : machine_mode mode1 = GET_MODE (subop1);
5384 721 : int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5385 721 : if (i0 == 0 && mode == mode0 && !side_effects_p (subop1))
5386 464201930 : return subop0;
5387 486 : if (known_eq (i0, GET_MODE_NUNITS (mode0))
5388 243 : && mode == mode1 && !side_effects_p (subop0))
5389 : return subop1;
5390 : }
5391 : }
5392 : else
5393 : {
5394 2889380 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
5395 8668140 : gcc_assert (GET_MODE_INNER (mode)
5396 : == GET_MODE_INNER (GET_MODE (trueop0)));
5397 2889380 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
5398 :
5399 2889380 : if (vec_duplicate_p (trueop0, &elt0))
5400 : /* It doesn't matter which elements are selected by trueop1,
5401 : because they are all the same. */
5402 17505 : return gen_vec_duplicate (mode, elt0);
5403 :
5404 2871875 : if (GET_CODE (trueop0) == CONST_VECTOR)
5405 : {
5406 18585 : unsigned n_elts = XVECLEN (trueop1, 0);
5407 18585 : rtvec v = rtvec_alloc (n_elts);
5408 18585 : unsigned int i;
5409 :
5410 37170 : gcc_assert (known_eq (n_elts, GET_MODE_NUNITS (mode)));
5411 85409 : for (i = 0; i < n_elts; i++)
5412 : {
5413 66824 : rtx x = XVECEXP (trueop1, 0, i);
5414 :
5415 66824 : if (!CONST_INT_P (x))
5416 : return 0;
5417 :
5418 66824 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0,
5419 : INTVAL (x));
5420 : }
5421 :
5422 18585 : return gen_rtx_CONST_VECTOR (mode, v);
5423 : }
5424 :
5425 : /* Recognize the identity. */
5426 2853290 : if (GET_MODE (trueop0) == mode)
5427 : {
5428 622787 : bool maybe_ident = true;
5429 622787 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
5430 : {
5431 622390 : rtx j = XVECEXP (trueop1, 0, i);
5432 622390 : if (!CONST_INT_P (j) || INTVAL (j) != i)
5433 : {
5434 : maybe_ident = false;
5435 : break;
5436 : }
5437 : }
5438 384820 : if (maybe_ident)
5439 : return trueop0;
5440 : }
5441 :
5442 : /* If we select a low-part subreg, return that. */
5443 2852893 : if (vec_series_lowpart_p (mode, GET_MODE (trueop0), trueop1))
5444 : {
5445 0 : rtx new_rtx = lowpart_subreg (mode, trueop0,
5446 0 : GET_MODE (trueop0));
5447 0 : if (new_rtx != NULL_RTX)
5448 : return new_rtx;
5449 : }
5450 :
5451 : /* If we build {a,b} then permute it, build the result directly. */
5452 2852893 : if (XVECLEN (trueop1, 0) == 2
5453 601538 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5454 601538 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
5455 601538 : && GET_CODE (trueop0) == VEC_CONCAT
5456 168864 : && GET_CODE (XEXP (trueop0, 0)) == VEC_CONCAT
5457 63 : && GET_MODE (XEXP (trueop0, 0)) == mode
5458 63 : && GET_CODE (XEXP (trueop0, 1)) == VEC_CONCAT
5459 52 : && GET_MODE (XEXP (trueop0, 1)) == mode)
5460 : {
5461 52 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5462 52 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
5463 52 : rtx subop0, subop1;
5464 :
5465 52 : gcc_assert (i0 < 4 && i1 < 4);
5466 52 : subop0 = XEXP (XEXP (trueop0, i0 / 2), i0 % 2);
5467 52 : subop1 = XEXP (XEXP (trueop0, i1 / 2), i1 % 2);
5468 :
5469 52 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
5470 : }
5471 :
5472 2852841 : if (XVECLEN (trueop1, 0) == 2
5473 601486 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5474 601486 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
5475 601486 : && GET_CODE (trueop0) == VEC_CONCAT
5476 168812 : && GET_MODE (trueop0) == mode)
5477 : {
5478 2 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5479 2 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
5480 2 : rtx subop0, subop1;
5481 :
5482 2 : gcc_assert (i0 < 2 && i1 < 2);
5483 2 : subop0 = XEXP (trueop0, i0);
5484 2 : subop1 = XEXP (trueop0, i1);
5485 :
5486 2 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
5487 : }
5488 :
5489 : /* If we select one half of a vec_concat, return that. */
5490 2852839 : int l0, l1;
5491 2852839 : if (GET_CODE (trueop0) == VEC_CONCAT
5492 3751210 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
5493 1875605 : .is_constant (&l0))
5494 3751210 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 1)))
5495 1875605 : .is_constant (&l1))
5496 4728444 : && CONST_INT_P (XVECEXP (trueop1, 0, 0)))
5497 : {
5498 1875605 : rtx subop0 = XEXP (trueop0, 0);
5499 1875605 : rtx subop1 = XEXP (trueop0, 1);
5500 1875605 : machine_mode mode0 = GET_MODE (subop0);
5501 1875605 : machine_mode mode1 = GET_MODE (subop1);
5502 1875605 : int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5503 1875605 : if (i0 == 0 && !side_effects_p (op1) && mode == mode0)
5504 : {
5505 1431421 : bool success = true;
5506 1431421 : for (int i = 1; i < l0; ++i)
5507 : {
5508 1431095 : rtx j = XVECEXP (trueop1, 0, i);
5509 1431095 : if (!CONST_INT_P (j) || INTVAL (j) != i)
5510 : {
5511 : success = false;
5512 : break;
5513 : }
5514 : }
5515 1215032 : if (success)
5516 : return subop0;
5517 : }
5518 1875279 : if (i0 == l0 && !side_effects_p (op0) && mode == mode1)
5519 : {
5520 1224 : bool success = true;
5521 1224 : for (int i = 1; i < l1; ++i)
5522 : {
5523 1177 : rtx j = XVECEXP (trueop1, 0, i);
5524 1177 : if (!CONST_INT_P (j) || INTVAL (j) != i0 + i)
5525 : {
5526 : success = false;
5527 : break;
5528 : }
5529 : }
5530 550 : if (success)
5531 : return subop1;
5532 : }
5533 : }
5534 :
5535 : /* Simplify vec_select of a subreg of X to just a vec_select of X
5536 : when X has same component mode as vec_select. */
5537 2852466 : unsigned HOST_WIDE_INT subreg_offset = 0;
5538 2852466 : if (GET_CODE (trueop0) == SUBREG
5539 383603 : && GET_MODE_INNER (mode)
5540 767206 : == GET_MODE_INNER (GET_MODE (SUBREG_REG (trueop0)))
5541 29914 : && GET_MODE_NUNITS (mode).is_constant (&l1)
5542 3236069 : && constant_multiple_p (subreg_memory_offset (trueop0),
5543 29914 : GET_MODE_UNIT_BITSIZE (mode),
5544 : &subreg_offset))
5545 : {
5546 14957 : poly_uint64 nunits
5547 29914 : = GET_MODE_NUNITS (GET_MODE (SUBREG_REG (trueop0)));
5548 14957 : bool success = true;
5549 91773 : for (int i = 0; i != l1; i++)
5550 : {
5551 87165 : rtx idx = XVECEXP (trueop1, 0, i);
5552 87165 : if (!CONST_INT_P (idx)
5553 87165 : || maybe_ge (UINTVAL (idx) + subreg_offset, nunits))
5554 : {
5555 : success = false;
5556 : break;
5557 : }
5558 : }
5559 :
5560 14957 : if (success)
5561 : {
5562 4608 : rtx par = trueop1;
5563 4608 : if (subreg_offset)
5564 : {
5565 0 : rtvec vec = rtvec_alloc (l1);
5566 0 : for (int i = 0; i < l1; i++)
5567 0 : RTVEC_ELT (vec, i)
5568 0 : = GEN_INT (INTVAL (XVECEXP (trueop1, 0, i))
5569 : + subreg_offset);
5570 0 : par = gen_rtx_PARALLEL (VOIDmode, vec);
5571 : }
5572 4608 : return gen_rtx_VEC_SELECT (mode, SUBREG_REG (trueop0), par);
5573 : }
5574 : }
5575 : }
5576 :
5577 3606014 : if (XVECLEN (trueop1, 0) == 1
5578 758240 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5579 758240 : && GET_CODE (trueop0) == VEC_CONCAT)
5580 : {
5581 185 : rtx vec = trueop0;
5582 370 : offset = INTVAL (XVECEXP (trueop1, 0, 0)) * GET_MODE_SIZE (mode);
5583 :
5584 : /* Try to find the element in the VEC_CONCAT. */
5585 185 : while (GET_MODE (vec) != mode
5586 370 : && GET_CODE (vec) == VEC_CONCAT)
5587 : {
5588 185 : poly_int64 vec_size;
5589 :
5590 185 : if (CONST_INT_P (XEXP (vec, 0)))
5591 : {
5592 : /* vec_concat of two const_ints doesn't make sense with
5593 : respect to modes. */
5594 0 : if (CONST_INT_P (XEXP (vec, 1)))
5595 464201930 : return 0;
5596 :
5597 0 : vec_size = GET_MODE_SIZE (GET_MODE (trueop0))
5598 0 : - GET_MODE_SIZE (GET_MODE (XEXP (vec, 1)));
5599 : }
5600 : else
5601 370 : vec_size = GET_MODE_SIZE (GET_MODE (XEXP (vec, 0)));
5602 :
5603 185 : if (known_lt (offset, vec_size))
5604 : vec = XEXP (vec, 0);
5605 15 : else if (known_ge (offset, vec_size))
5606 : {
5607 15 : offset -= vec_size;
5608 15 : vec = XEXP (vec, 1);
5609 : }
5610 : else
5611 : break;
5612 185 : vec = avoid_constant_pool_reference (vec);
5613 : }
5614 :
5615 185 : if (GET_MODE (vec) == mode)
5616 : return vec;
5617 : }
5618 :
5619 : /* If we select elements in a vec_merge that all come from the same
5620 : operand, select from that operand directly. */
5621 3606014 : if (GET_CODE (op0) == VEC_MERGE)
5622 : {
5623 9286 : rtx trueop02 = avoid_constant_pool_reference (XEXP (op0, 2));
5624 9286 : if (CONST_INT_P (trueop02))
5625 : {
5626 2871 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop02);
5627 2871 : bool all_operand0 = true;
5628 2871 : bool all_operand1 = true;
5629 10689 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
5630 : {
5631 7818 : rtx j = XVECEXP (trueop1, 0, i);
5632 7818 : if (sel & (HOST_WIDE_INT_1U << UINTVAL (j)))
5633 : all_operand1 = false;
5634 : else
5635 3504 : all_operand0 = false;
5636 : }
5637 2871 : if (all_operand0 && !side_effects_p (XEXP (op0, 1)))
5638 1340 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 0), op1);
5639 1531 : if (all_operand1 && !side_effects_p (XEXP (op0, 0)))
5640 55 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 1), op1);
5641 : }
5642 : }
5643 :
5644 : /* If we have two nested selects that are inverses of each
5645 : other, replace them with the source operand. */
5646 3604619 : if (GET_CODE (trueop0) == VEC_SELECT
5647 73913 : && GET_MODE (XEXP (trueop0, 0)) == mode)
5648 : {
5649 1048 : rtx op0_subop1 = XEXP (trueop0, 1);
5650 1048 : gcc_assert (GET_CODE (op0_subop1) == PARALLEL);
5651 2096 : gcc_assert (known_eq (XVECLEN (trueop1, 0), GET_MODE_NUNITS (mode)));
5652 : bool identical_p = true;
5653 :
5654 : /* Apply the outer ordering vector to the inner one. (The inner
5655 : ordering vector is expressly permitted to be of a different
5656 : length than the outer one.) If the result is { 0, 1, ..., n-1 }
5657 : then the two VEC_SELECTs cancel. */
5658 9068 : for (int i = 0; i < XVECLEN (trueop1, 0); ++i)
5659 : {
5660 8020 : rtx x = XVECEXP (trueop1, 0, i);
5661 8020 : if (!CONST_INT_P (x))
5662 : return 0;
5663 8020 : rtx y = XVECEXP (op0_subop1, 0, INTVAL (x));
5664 8020 : if (!CONST_INT_P (y))
5665 : return 0;
5666 8020 : if (i != INTVAL (y))
5667 5918 : identical_p = false;
5668 : }
5669 1048 : if (identical_p)
5670 : return XEXP (trueop0, 0);
5671 :
5672 : /* Otherwise a permutation of a permutation is a permutation. */
5673 1048 : int len = XVECLEN (trueop1, 0);
5674 1048 : rtvec vec = rtvec_alloc (len);
5675 10116 : for (int i = 0; i < len; ++i)
5676 : {
5677 8020 : rtx x = XVECEXP (trueop1, 0, i);
5678 8020 : rtx y = XVECEXP (op0_subop1, 0, INTVAL (x));
5679 8020 : RTVEC_ELT (vec, i) = y;
5680 : }
5681 1048 : return gen_rtx_fmt_ee (code, mode, XEXP (trueop0, 0),
5682 : gen_rtx_PARALLEL (VOIDmode, vec));
5683 : }
5684 :
5685 : return 0;
5686 4099613 : case VEC_CONCAT:
5687 4099613 : {
5688 4099613 : machine_mode op0_mode = (GET_MODE (trueop0) != VOIDmode
5689 4099613 : ? GET_MODE (trueop0)
5690 4099613 : : GET_MODE_INNER (mode));
5691 4099613 : machine_mode op1_mode = (GET_MODE (trueop1) != VOIDmode
5692 4099613 : ? GET_MODE (trueop1)
5693 4099613 : : GET_MODE_INNER (mode));
5694 :
5695 4099613 : gcc_assert (VECTOR_MODE_P (mode));
5696 16398452 : gcc_assert (known_eq (GET_MODE_SIZE (op0_mode)
5697 : + GET_MODE_SIZE (op1_mode),
5698 : GET_MODE_SIZE (mode)));
5699 :
5700 4099613 : if (VECTOR_MODE_P (op0_mode))
5701 6408396 : gcc_assert (GET_MODE_INNER (mode)
5702 : == GET_MODE_INNER (op0_mode));
5703 : else
5704 3926962 : gcc_assert (GET_MODE_INNER (mode) == op0_mode);
5705 :
5706 4099613 : if (VECTOR_MODE_P (op1_mode))
5707 6408396 : gcc_assert (GET_MODE_INNER (mode)
5708 : == GET_MODE_INNER (op1_mode));
5709 : else
5710 3926962 : gcc_assert (GET_MODE_INNER (mode) == op1_mode);
5711 :
5712 4099613 : unsigned int n_elts, in_n_elts;
5713 4099613 : if ((GET_CODE (trueop0) == CONST_VECTOR
5714 4099613 : || CONST_SCALAR_INT_P (trueop0)
5715 4072358 : || CONST_DOUBLE_AS_FLOAT_P (trueop0))
5716 28732 : && (GET_CODE (trueop1) == CONST_VECTOR
5717 28732 : || CONST_SCALAR_INT_P (trueop1)
5718 28732 : || CONST_DOUBLE_AS_FLOAT_P (trueop1))
5719 0 : && GET_MODE_NUNITS (mode).is_constant (&n_elts)
5720 4099613 : && GET_MODE_NUNITS (op0_mode).is_constant (&in_n_elts))
5721 : {
5722 0 : rtvec v = rtvec_alloc (n_elts);
5723 0 : unsigned int i;
5724 0 : for (i = 0; i < n_elts; i++)
5725 : {
5726 0 : if (i < in_n_elts)
5727 : {
5728 0 : if (!VECTOR_MODE_P (op0_mode))
5729 0 : RTVEC_ELT (v, i) = trueop0;
5730 : else
5731 0 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0, i);
5732 : }
5733 : else
5734 : {
5735 0 : if (!VECTOR_MODE_P (op1_mode))
5736 0 : RTVEC_ELT (v, i) = trueop1;
5737 : else
5738 0 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop1,
5739 : i - in_n_elts);
5740 : }
5741 : }
5742 :
5743 0 : return gen_rtx_CONST_VECTOR (mode, v);
5744 : }
5745 :
5746 : /* Try to merge two VEC_SELECTs from the same vector into a single one.
5747 : Restrict the transformation to avoid generating a VEC_SELECT with a
5748 : mode unrelated to its operand. */
5749 4099613 : if (GET_CODE (trueop0) == VEC_SELECT
5750 181386 : && GET_CODE (trueop1) == VEC_SELECT
5751 36297 : && rtx_equal_p (XEXP (trueop0, 0), XEXP (trueop1, 0))
5752 4118309 : && GET_MODE_INNER (GET_MODE (XEXP (trueop0, 0)))
5753 37392 : == GET_MODE_INNER(mode))
5754 : {
5755 18696 : rtx par0 = XEXP (trueop0, 1);
5756 18696 : rtx par1 = XEXP (trueop1, 1);
5757 18696 : int len0 = XVECLEN (par0, 0);
5758 18696 : int len1 = XVECLEN (par1, 0);
5759 18696 : rtvec vec = rtvec_alloc (len0 + len1);
5760 131786 : for (int i = 0; i < len0; i++)
5761 94394 : RTVEC_ELT (vec, i) = XVECEXP (par0, 0, i);
5762 113090 : for (int i = 0; i < len1; i++)
5763 94394 : RTVEC_ELT (vec, len0 + i) = XVECEXP (par1, 0, i);
5764 18696 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (trueop0, 0),
5765 18696 : gen_rtx_PARALLEL (VOIDmode, vec));
5766 : }
5767 : /* (vec_concat:N
5768 : (subreg:N/2 OP 0)
5769 : (subreg:N/2 OP N/2)) --> OP
5770 : i.e. where concatenating the first and second halves of the
5771 : same object OP. */
5772 4080917 : {
5773 4080917 : poly_uint64 offset = 0u;
5774 4080917 : rtx base0 = get_ref_base_and_offset (trueop0, &offset);
5775 4080917 : if (known_eq (offset, 0u)
5776 12240971 : && known_eq (GET_MODE_SIZE (GET_MODE (base0)),
5777 : GET_MODE_SIZE (mode)))
5778 : {
5779 68351 : rtx base1 = get_ref_base_and_offset (trueop1, &offset);
5780 68351 : if (rtx_equal_p (base0, base1)
5781 1214 : && known_eq (offset, GET_MODE_SIZE (op0_mode))
5782 1173 : && !side_effects_p (trueop0)
5783 69524 : && !side_effects_p (trueop1))
5784 : {
5785 1173 : tem = rtl_hooks.gen_lowpart_no_emit (mode, base0);
5786 1173 : if (tem)
5787 1173 : return tem;
5788 : }
5789 : }
5790 : }
5791 : }
5792 4079744 : return 0;
5793 :
5794 0 : default:
5795 0 : gcc_unreachable ();
5796 : }
5797 :
5798 390335517 : if (mode == GET_MODE (op0)
5799 335593728 : && mode == GET_MODE (op1)
5800 106978067 : && vec_duplicate_p (op0, &elt0)
5801 390455536 : && vec_duplicate_p (op1, &elt1))
5802 : {
5803 : /* Try applying the operator to ELT and see if that simplifies.
5804 : We can duplicate the result if so.
5805 :
5806 : The reason we don't use simplify_gen_binary is that it isn't
5807 : necessarily a win to convert things like:
5808 :
5809 : (plus:V (vec_duplicate:V (reg:S R1))
5810 : (vec_duplicate:V (reg:S R2)))
5811 :
5812 : to:
5813 :
5814 : (vec_duplicate:V (plus:S (reg:S R1) (reg:S R2)))
5815 :
5816 : The first might be done entirely in vector registers while the
5817 : second might need a move between register files. */
5818 172 : tem = simplify_binary_operation (code, GET_MODE_INNER (mode),
5819 : elt0, elt1);
5820 86 : if (tem)
5821 2 : return gen_vec_duplicate (mode, tem);
5822 : }
5823 :
5824 : return 0;
5825 : }
5826 :
5827 : /* Return true if binary operation OP distributes over addition in operand
5828 : OPNO, with the other operand being held constant. OPNO counts from 1. */
5829 :
5830 : static bool
5831 8639 : distributes_over_addition_p (rtx_code op, int opno)
5832 : {
5833 0 : switch (op)
5834 : {
5835 : case PLUS:
5836 : case MINUS:
5837 : case MULT:
5838 : return true;
5839 :
5840 0 : case ASHIFT:
5841 0 : return opno == 1;
5842 :
5843 0 : default:
5844 0 : return false;
5845 : }
5846 : }
5847 :
5848 : rtx
5849 498670604 : simplify_const_binary_operation (enum rtx_code code, machine_mode mode,
5850 : rtx op0, rtx op1)
5851 : {
5852 498670604 : if (VECTOR_MODE_P (mode)
5853 15712359 : && code != VEC_CONCAT
5854 11598173 : && GET_CODE (op0) == CONST_VECTOR
5855 188743 : && GET_CODE (op1) == CONST_VECTOR)
5856 : {
5857 9348 : bool step_ok_p;
5858 9348 : if (CONST_VECTOR_STEPPED_P (op0)
5859 9348 : && CONST_VECTOR_STEPPED_P (op1))
5860 : /* We can operate directly on the encoding if:
5861 :
5862 : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
5863 : implies
5864 : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
5865 :
5866 : Addition and subtraction are the supported operators
5867 : for which this is true. */
5868 709 : step_ok_p = (code == PLUS || code == MINUS);
5869 8639 : else if (CONST_VECTOR_STEPPED_P (op0))
5870 : /* We can operate directly on stepped encodings if:
5871 :
5872 : a3 - a2 == a2 - a1
5873 : implies:
5874 : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
5875 :
5876 : which is true if (x -> x op c) distributes over addition. */
5877 1317 : step_ok_p = distributes_over_addition_p (code, 1);
5878 : else
5879 : /* Similarly in reverse. */
5880 7322 : step_ok_p = distributes_over_addition_p (code, 2);
5881 9348 : rtx_vector_builder builder;
5882 9348 : if (!builder.new_binary_operation (mode, op0, op1, step_ok_p))
5883 : return 0;
5884 :
5885 9348 : unsigned int count = builder.encoded_nelts ();
5886 55902 : for (unsigned int i = 0; i < count; i++)
5887 : {
5888 93208 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
5889 : CONST_VECTOR_ELT (op0, i),
5890 46604 : CONST_VECTOR_ELT (op1, i));
5891 46604 : if (!x || !valid_for_const_vector_p (mode, x))
5892 50 : return 0;
5893 46554 : builder.quick_push (x);
5894 : }
5895 9298 : return builder.build ();
5896 9348 : }
5897 :
5898 498661256 : if (VECTOR_MODE_P (mode)
5899 15703011 : && code == VEC_CONCAT
5900 4114186 : && (CONST_SCALAR_INT_P (op0)
5901 4093275 : || CONST_FIXED_P (op0)
5902 4093275 : || CONST_DOUBLE_AS_FLOAT_P (op0)
5903 4089420 : || CONST_VECTOR_P (op0))
5904 43305 : && (CONST_SCALAR_INT_P (op1)
5905 40004 : || CONST_DOUBLE_AS_FLOAT_P (op1)
5906 37626 : || CONST_FIXED_P (op1)
5907 37626 : || CONST_VECTOR_P (op1)))
5908 : {
5909 : /* Both inputs have a constant number of elements, so the result
5910 : must too. */
5911 14573 : unsigned n_elts = GET_MODE_NUNITS (mode).to_constant ();
5912 14573 : rtvec v = rtvec_alloc (n_elts);
5913 :
5914 14573 : gcc_assert (n_elts >= 2);
5915 14573 : if (n_elts == 2)
5916 : {
5917 5679 : gcc_assert (GET_CODE (op0) != CONST_VECTOR);
5918 5679 : gcc_assert (GET_CODE (op1) != CONST_VECTOR);
5919 :
5920 5679 : RTVEC_ELT (v, 0) = op0;
5921 5679 : RTVEC_ELT (v, 1) = op1;
5922 : }
5923 : else
5924 : {
5925 8894 : unsigned op0_n_elts = GET_MODE_NUNITS (GET_MODE (op0)).to_constant ();
5926 8894 : unsigned op1_n_elts = GET_MODE_NUNITS (GET_MODE (op1)).to_constant ();
5927 8894 : unsigned i;
5928 :
5929 8894 : gcc_assert (GET_CODE (op0) == CONST_VECTOR);
5930 8894 : gcc_assert (GET_CODE (op1) == CONST_VECTOR);
5931 8894 : gcc_assert (op0_n_elts + op1_n_elts == n_elts);
5932 :
5933 53846 : for (i = 0; i < op0_n_elts; ++i)
5934 44952 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (op0, i);
5935 54038 : for (i = 0; i < op1_n_elts; ++i)
5936 45144 : RTVEC_ELT (v, op0_n_elts+i) = CONST_VECTOR_ELT (op1, i);
5937 : }
5938 :
5939 14573 : return gen_rtx_CONST_VECTOR (mode, v);
5940 : }
5941 :
5942 486335712 : if (VECTOR_MODE_P (mode)
5943 15688438 : && GET_CODE (op0) == CONST_VECTOR
5944 189040 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1))
5945 498646683 : && (CONST_VECTOR_DUPLICATE_P (op0)
5946 123368 : || CONST_VECTOR_NUNITS (op0).is_constant ()))
5947 : {
5948 123368 : switch (code)
5949 : {
5950 123368 : case PLUS:
5951 123368 : case MINUS:
5952 123368 : case MULT:
5953 123368 : case DIV:
5954 123368 : case MOD:
5955 123368 : case UDIV:
5956 123368 : case UMOD:
5957 123368 : case AND:
5958 123368 : case IOR:
5959 123368 : case XOR:
5960 123368 : case SMIN:
5961 123368 : case SMAX:
5962 123368 : case UMIN:
5963 123368 : case UMAX:
5964 123368 : case LSHIFTRT:
5965 123368 : case ASHIFTRT:
5966 123368 : case ASHIFT:
5967 123368 : case ROTATE:
5968 123368 : case ROTATERT:
5969 123368 : case SS_PLUS:
5970 123368 : case US_PLUS:
5971 123368 : case SS_MINUS:
5972 123368 : case US_MINUS:
5973 123368 : case SS_ASHIFT:
5974 123368 : case US_ASHIFT:
5975 123368 : case COPYSIGN:
5976 123368 : break;
5977 : default:
5978 : return NULL_RTX;
5979 : }
5980 :
5981 123368 : unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op0)
5982 123368 : ? CONST_VECTOR_NPATTERNS (op0)
5983 131398 : : CONST_VECTOR_NUNITS (op0).to_constant ());
5984 123368 : rtx_vector_builder builder (mode, npatterns, 1);
5985 383294 : for (unsigned i = 0; i < npatterns; i++)
5986 : {
5987 273116 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
5988 136558 : CONST_VECTOR_ELT (op0, i), op1);
5989 136558 : if (!x || !valid_for_const_vector_p (mode, x))
5990 0 : return 0;
5991 136558 : builder.quick_push (x);
5992 : }
5993 123368 : return builder.build ();
5994 : }
5995 :
5996 498523315 : if (SCALAR_FLOAT_MODE_P (mode)
5997 6501201 : && CONST_DOUBLE_AS_FLOAT_P (op0)
5998 81135 : && CONST_DOUBLE_AS_FLOAT_P (op1)
5999 11718 : && mode == GET_MODE (op0) && mode == GET_MODE (op1))
6000 : {
6001 11718 : if (code == AND
6002 : || code == IOR
6003 11718 : || code == XOR)
6004 : {
6005 2578 : long tmp0[4];
6006 2578 : long tmp1[4];
6007 2578 : REAL_VALUE_TYPE r;
6008 2578 : int i;
6009 :
6010 2578 : real_to_target (tmp0, CONST_DOUBLE_REAL_VALUE (op0),
6011 2578 : GET_MODE (op0));
6012 2578 : real_to_target (tmp1, CONST_DOUBLE_REAL_VALUE (op1),
6013 2578 : GET_MODE (op1));
6014 12890 : for (i = 0; i < 4; i++)
6015 : {
6016 10312 : switch (code)
6017 : {
6018 5376 : case AND:
6019 5376 : tmp0[i] &= tmp1[i];
6020 5376 : break;
6021 2568 : case IOR:
6022 2568 : tmp0[i] |= tmp1[i];
6023 2568 : break;
6024 2368 : case XOR:
6025 2368 : tmp0[i] ^= tmp1[i];
6026 2368 : break;
6027 : default:
6028 : gcc_unreachable ();
6029 : }
6030 : }
6031 2578 : real_from_target (&r, tmp0, mode);
6032 2578 : return const_double_from_real_value (r, mode);
6033 : }
6034 9140 : else if (code == COPYSIGN)
6035 : {
6036 0 : REAL_VALUE_TYPE f0, f1;
6037 0 : real_convert (&f0, mode, CONST_DOUBLE_REAL_VALUE (op0));
6038 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (op1));
6039 0 : real_copysign (&f0, &f1);
6040 0 : return const_double_from_real_value (f0, mode);
6041 : }
6042 : else
6043 : {
6044 9140 : REAL_VALUE_TYPE f0, f1, value, result;
6045 9140 : const REAL_VALUE_TYPE *opr0, *opr1;
6046 9140 : bool inexact;
6047 :
6048 9140 : opr0 = CONST_DOUBLE_REAL_VALUE (op0);
6049 9140 : opr1 = CONST_DOUBLE_REAL_VALUE (op1);
6050 :
6051 9140 : if (HONOR_SNANS (mode)
6052 9140 : && (REAL_VALUE_ISSIGNALING_NAN (*opr0)
6053 845 : || REAL_VALUE_ISSIGNALING_NAN (*opr1)))
6054 : return 0;
6055 :
6056 9130 : real_convert (&f0, mode, opr0);
6057 9130 : real_convert (&f1, mode, opr1);
6058 :
6059 9130 : if (code == DIV
6060 4161 : && real_equal (&f1, &dconst0)
6061 12772 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
6062 : return 0;
6063 :
6064 27361 : if (MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
6065 5402 : && flag_trapping_math
6066 5324 : && REAL_VALUE_ISINF (f0) && REAL_VALUE_ISINF (f1))
6067 : {
6068 9 : int s0 = REAL_VALUE_NEGATIVE (f0);
6069 9 : int s1 = REAL_VALUE_NEGATIVE (f1);
6070 :
6071 9 : switch (code)
6072 : {
6073 0 : case PLUS:
6074 : /* Inf + -Inf = NaN plus exception. */
6075 0 : if (s0 != s1)
6076 : return 0;
6077 : break;
6078 0 : case MINUS:
6079 : /* Inf - Inf = NaN plus exception. */
6080 0 : if (s0 == s1)
6081 : return 0;
6082 : break;
6083 : case DIV:
6084 : /* Inf / Inf = NaN plus exception. */
6085 : return 0;
6086 : default:
6087 : break;
6088 : }
6089 : }
6090 :
6091 7918 : if (code == MULT && MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
6092 1944 : && flag_trapping_math
6093 7379 : && ((REAL_VALUE_ISINF (f0) && real_equal (&f1, &dconst0))
6094 1888 : || (REAL_VALUE_ISINF (f1)
6095 10 : && real_equal (&f0, &dconst0))))
6096 : /* Inf * 0 = NaN plus exception. */
6097 : return 0;
6098 :
6099 5465 : inexact = real_arithmetic (&value, rtx_to_tree_code (code),
6100 : &f0, &f1);
6101 5465 : real_convert (&result, mode, &value);
6102 :
6103 : /* Don't constant fold this floating point operation if
6104 : the result has overflowed and flag_trapping_math. */
6105 :
6106 5465 : if (flag_trapping_math
6107 21188 : && MODE_HAS_INFINITIES (mode)
6108 5297 : && REAL_VALUE_ISINF (result)
6109 1141 : && !REAL_VALUE_ISINF (f0)
6110 6592 : && !REAL_VALUE_ISINF (f1))
6111 : /* Overflow plus exception. */
6112 1127 : return 0;
6113 :
6114 : /* Don't constant fold this floating point operation if the
6115 : result may dependent upon the run-time rounding mode and
6116 : flag_rounding_math is set, or if GCC's software emulation
6117 : is unable to accurately represent the result. When the
6118 : result is known to be exactly zero, rounding may affect
6119 : the sign for modes that support signed zeros. */
6120 :
6121 4338 : if ((flag_rounding_math
6122 27689 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
6123 4338 : && (inexact
6124 358 : || !real_identical (&result, &value)
6125 72 : || (real_iszero (&value) && HONOR_SIGNED_ZEROS (mode))))
6126 : return NULL_RTX;
6127 :
6128 3907 : return const_double_from_real_value (result, mode);
6129 : }
6130 : }
6131 :
6132 : /* We can fold some multi-word operations. */
6133 498511597 : scalar_int_mode int_mode;
6134 498511597 : if (is_a <scalar_int_mode> (mode, &int_mode)
6135 427401023 : && CONST_SCALAR_INT_P (op0)
6136 41334182 : && CONST_SCALAR_INT_P (op1)
6137 34317906 : && GET_MODE_PRECISION (int_mode) <= MAX_BITSIZE_MODE_ANY_INT)
6138 : {
6139 34317906 : wide_int result;
6140 34317906 : wi::overflow_type overflow;
6141 34317906 : rtx_mode_t pop0 = rtx_mode_t (op0, int_mode);
6142 34317906 : rtx_mode_t pop1 = rtx_mode_t (op1, int_mode);
6143 :
6144 : #if TARGET_SUPPORTS_WIDE_INT == 0
6145 : /* This assert keeps the simplification from producing a result
6146 : that cannot be represented in a CONST_DOUBLE but a lot of
6147 : upstream callers expect that this function never fails to
6148 : simplify something and so you if you added this to the test
6149 : above the code would die later anyway. If this assert
6150 : happens, you just need to make the port support wide int. */
6151 : gcc_assert (GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_DOUBLE_INT);
6152 : #endif
6153 34317906 : switch (code)
6154 : {
6155 1100729 : case MINUS:
6156 1100729 : result = wi::sub (pop0, pop1);
6157 1100729 : break;
6158 :
6159 26523727 : case PLUS:
6160 26523727 : result = wi::add (pop0, pop1);
6161 26523727 : break;
6162 :
6163 321932 : case MULT:
6164 321932 : result = wi::mul (pop0, pop1);
6165 321932 : break;
6166 :
6167 9095 : case DIV:
6168 9095 : result = wi::div_trunc (pop0, pop1, SIGNED, &overflow);
6169 9095 : if (overflow)
6170 : return NULL_RTX;
6171 : break;
6172 :
6173 1573 : case MOD:
6174 1573 : result = wi::mod_trunc (pop0, pop1, SIGNED, &overflow);
6175 1573 : if (overflow)
6176 : return NULL_RTX;
6177 : break;
6178 :
6179 6118 : case UDIV:
6180 6118 : result = wi::div_trunc (pop0, pop1, UNSIGNED, &overflow);
6181 6118 : if (overflow)
6182 : return NULL_RTX;
6183 : break;
6184 :
6185 16284 : case UMOD:
6186 16284 : result = wi::mod_trunc (pop0, pop1, UNSIGNED, &overflow);
6187 16284 : if (overflow)
6188 : return NULL_RTX;
6189 : break;
6190 :
6191 793504 : case AND:
6192 793504 : result = wi::bit_and (pop0, pop1);
6193 793504 : break;
6194 :
6195 285491 : case IOR:
6196 285491 : result = wi::bit_or (pop0, pop1);
6197 285491 : break;
6198 :
6199 44860 : case XOR:
6200 44860 : result = wi::bit_xor (pop0, pop1);
6201 44860 : break;
6202 :
6203 1763 : case SMIN:
6204 1763 : result = wi::smin (pop0, pop1);
6205 1763 : break;
6206 :
6207 1989 : case SMAX:
6208 1989 : result = wi::smax (pop0, pop1);
6209 1989 : break;
6210 :
6211 3240 : case UMIN:
6212 3240 : result = wi::umin (pop0, pop1);
6213 3240 : break;
6214 :
6215 3006 : case UMAX:
6216 3006 : result = wi::umax (pop0, pop1);
6217 3006 : break;
6218 :
6219 5163884 : case LSHIFTRT:
6220 5163884 : case ASHIFTRT:
6221 5163884 : case ASHIFT:
6222 5163884 : case SS_ASHIFT:
6223 5163884 : case US_ASHIFT:
6224 5163884 : {
6225 : /* The shift count might be in SImode while int_mode might
6226 : be narrower. On IA-64 it is even DImode. If the shift
6227 : count is too large and doesn't fit into int_mode, we'd
6228 : ICE. So, if int_mode is narrower than
6229 : HOST_BITS_PER_WIDE_INT, use DImode for the shift count. */
6230 5163884 : if (GET_MODE (op1) == VOIDmode
6231 5163884 : && GET_MODE_PRECISION (int_mode) < HOST_BITS_PER_WIDE_INT)
6232 1921017 : pop1 = rtx_mode_t (op1, DImode);
6233 :
6234 5163884 : wide_int wop1 = pop1;
6235 5163884 : if (SHIFT_COUNT_TRUNCATED)
6236 : wop1 = wi::umod_trunc (wop1, GET_MODE_PRECISION (int_mode));
6237 5163884 : else if (wi::geu_p (wop1, GET_MODE_PRECISION (int_mode)))
6238 64 : return NULL_RTX;
6239 :
6240 5163820 : switch (code)
6241 : {
6242 3005596 : case LSHIFTRT:
6243 3005596 : result = wi::lrshift (pop0, wop1);
6244 3005596 : break;
6245 :
6246 84747 : case ASHIFTRT:
6247 84747 : result = wi::arshift (pop0, wop1);
6248 84747 : break;
6249 :
6250 2073477 : case ASHIFT:
6251 2073477 : result = wi::lshift (pop0, wop1);
6252 2073477 : break;
6253 :
6254 0 : case SS_ASHIFT:
6255 0 : if (wi::leu_p (wop1, wi::clrsb (pop0)))
6256 0 : result = wi::lshift (pop0, wop1);
6257 0 : else if (wi::neg_p (pop0))
6258 0 : result = wi::min_value (int_mode, SIGNED);
6259 : else
6260 0 : result = wi::max_value (int_mode, SIGNED);
6261 : break;
6262 :
6263 0 : case US_ASHIFT:
6264 0 : if (wi::eq_p (pop0, 0))
6265 0 : result = pop0;
6266 0 : else if (wi::leu_p (wop1, wi::clz (pop0)))
6267 0 : result = wi::lshift (pop0, wop1);
6268 : else
6269 0 : result = wi::max_value (int_mode, UNSIGNED);
6270 : break;
6271 :
6272 : default:
6273 : gcc_unreachable ();
6274 : }
6275 5163820 : break;
6276 5163884 : }
6277 31429 : case ROTATE:
6278 31429 : case ROTATERT:
6279 31429 : {
6280 : /* The rotate count might be in SImode while int_mode might
6281 : be narrower. On IA-64 it is even DImode. If the shift
6282 : count is too large and doesn't fit into int_mode, we'd
6283 : ICE. So, if int_mode is narrower than
6284 : HOST_BITS_PER_WIDE_INT, use DImode for the shift count. */
6285 31429 : if (GET_MODE (op1) == VOIDmode
6286 31429 : && GET_MODE_PRECISION (int_mode) < HOST_BITS_PER_WIDE_INT)
6287 23759 : pop1 = rtx_mode_t (op1, DImode);
6288 :
6289 31429 : if (wi::neg_p (pop1))
6290 : return NULL_RTX;
6291 :
6292 31329 : switch (code)
6293 : {
6294 10361 : case ROTATE:
6295 10361 : result = wi::lrotate (pop0, pop1);
6296 10361 : break;
6297 :
6298 20968 : case ROTATERT:
6299 20968 : result = wi::rrotate (pop0, pop1);
6300 20968 : break;
6301 :
6302 : default:
6303 : gcc_unreachable ();
6304 : }
6305 : break;
6306 : }
6307 :
6308 2438 : case SS_PLUS:
6309 2438 : result = wi::add (pop0, pop1, SIGNED, &overflow);
6310 4820 : clamp_signed_saturation:
6311 4820 : if (overflow == wi::OVF_OVERFLOW)
6312 314 : result = wi::max_value (GET_MODE_PRECISION (int_mode), SIGNED);
6313 4506 : else if (overflow == wi::OVF_UNDERFLOW)
6314 278 : result = wi::min_value (GET_MODE_PRECISION (int_mode), SIGNED);
6315 4228 : else if (overflow != wi::OVF_NONE)
6316 : return NULL_RTX;
6317 : break;
6318 :
6319 2388 : case US_PLUS:
6320 2388 : result = wi::add (pop0, pop1, UNSIGNED, &overflow);
6321 2388 : clamp_unsigned_saturation:
6322 2388 : if (overflow != wi::OVF_NONE)
6323 502 : result = wi::max_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
6324 : break;
6325 :
6326 2382 : case SS_MINUS:
6327 2382 : result = wi::sub (pop0, pop1, SIGNED, &overflow);
6328 2382 : goto clamp_signed_saturation;
6329 :
6330 2068 : case US_MINUS:
6331 2068 : result = wi::sub (pop0, pop1, UNSIGNED, &overflow);
6332 2068 : if (overflow != wi::OVF_NONE)
6333 1350 : result = wi::min_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
6334 : break;
6335 :
6336 0 : case SS_MULT:
6337 0 : result = wi::mul (pop0, pop1, SIGNED, &overflow);
6338 0 : goto clamp_signed_saturation;
6339 :
6340 0 : case US_MULT:
6341 0 : result = wi::mul (pop0, pop1, UNSIGNED, &overflow);
6342 0 : goto clamp_unsigned_saturation;
6343 :
6344 6 : case SMUL_HIGHPART:
6345 6 : result = wi::mul_high (pop0, pop1, SIGNED);
6346 6 : break;
6347 :
6348 0 : case UMUL_HIGHPART:
6349 0 : result = wi::mul_high (pop0, pop1, UNSIGNED);
6350 0 : break;
6351 :
6352 : default:
6353 : return NULL_RTX;
6354 : }
6355 34314950 : return immed_wide_int_const (result, int_mode);
6356 34317906 : }
6357 :
6358 : /* Handle polynomial integers. */
6359 : if (NUM_POLY_INT_COEFFS > 1
6360 : && is_a <scalar_int_mode> (mode, &int_mode)
6361 : && poly_int_rtx_p (op0)
6362 : && poly_int_rtx_p (op1))
6363 : {
6364 : poly_wide_int result;
6365 : switch (code)
6366 : {
6367 : case PLUS:
6368 : result = wi::to_poly_wide (op0, mode) + wi::to_poly_wide (op1, mode);
6369 : break;
6370 :
6371 : case MINUS:
6372 : result = wi::to_poly_wide (op0, mode) - wi::to_poly_wide (op1, mode);
6373 : break;
6374 :
6375 : case MULT:
6376 : if (CONST_SCALAR_INT_P (op1))
6377 : result = wi::to_poly_wide (op0, mode) * rtx_mode_t (op1, mode);
6378 : else
6379 : return NULL_RTX;
6380 : break;
6381 :
6382 : case ASHIFT:
6383 : if (CONST_SCALAR_INT_P (op1))
6384 : {
6385 : wide_int shift
6386 : = rtx_mode_t (op1,
6387 : GET_MODE (op1) == VOIDmode
6388 : && (GET_MODE_PRECISION (int_mode)
6389 : < HOST_BITS_PER_WIDE_INT)
6390 : ? DImode : mode);
6391 : if (SHIFT_COUNT_TRUNCATED)
6392 : shift = wi::umod_trunc (shift, GET_MODE_PRECISION (int_mode));
6393 : else if (wi::geu_p (shift, GET_MODE_PRECISION (int_mode)))
6394 : return NULL_RTX;
6395 : result = wi::to_poly_wide (op0, mode) << shift;
6396 : }
6397 : else
6398 : return NULL_RTX;
6399 : break;
6400 :
6401 : case IOR:
6402 : if (!CONST_SCALAR_INT_P (op1)
6403 : || !can_ior_p (wi::to_poly_wide (op0, mode),
6404 : rtx_mode_t (op1, mode), &result))
6405 : return NULL_RTX;
6406 : break;
6407 :
6408 : default:
6409 : return NULL_RTX;
6410 : }
6411 : return immed_wide_int_const (result, int_mode);
6412 : }
6413 :
6414 : return NULL_RTX;
6415 : }
6416 :
6417 :
6418 :
6419 : /* Return a positive integer if X should sort after Y. The value
6420 : returned is 1 if and only if X and Y are both regs. */
6421 :
6422 : static int
6423 117189926 : simplify_plus_minus_op_data_cmp (rtx x, rtx y)
6424 : {
6425 117189926 : int result;
6426 :
6427 117189926 : result = (commutative_operand_precedence (y)
6428 117189926 : - commutative_operand_precedence (x));
6429 117189926 : if (result)
6430 81431481 : return result + result;
6431 :
6432 : /* Group together equal REGs to do more simplification. */
6433 35758445 : if (REG_P (x) && REG_P (y))
6434 9154358 : return REGNO (x) > REGNO (y);
6435 :
6436 : return 0;
6437 : }
6438 :
6439 : /* Simplify and canonicalize a PLUS or MINUS, at least one of whose
6440 : operands may be another PLUS or MINUS.
6441 :
6442 : Rather than test for specific case, we do this by a brute-force method
6443 : and do all possible simplifications until no more changes occur. Then
6444 : we rebuild the operation.
6445 :
6446 : May return NULL_RTX when no changes were made. */
6447 :
6448 : rtx
6449 39476799 : simplify_context::simplify_plus_minus (rtx_code code, machine_mode mode,
6450 : rtx op0, rtx op1)
6451 : {
6452 39476799 : struct simplify_plus_minus_op_data
6453 : {
6454 : rtx op;
6455 : short neg;
6456 : } ops[16];
6457 39476799 : rtx result, tem;
6458 39476799 : int n_ops = 2;
6459 39476799 : int changed, n_constants, canonicalized = 0;
6460 39476799 : int i, j;
6461 :
6462 39476799 : memset (ops, 0, sizeof ops);
6463 :
6464 : /* Set up the two operands and then expand them until nothing has been
6465 : changed. If we run out of room in our array, give up; this should
6466 : almost never happen. */
6467 :
6468 39476799 : ops[0].op = op0;
6469 39476799 : ops[0].neg = 0;
6470 39476799 : ops[1].op = op1;
6471 39476799 : ops[1].neg = (code == MINUS);
6472 :
6473 80282146 : do
6474 : {
6475 80282146 : changed = 0;
6476 80282146 : n_constants = 0;
6477 :
6478 324978472 : for (i = 0; i < n_ops; i++)
6479 : {
6480 244696342 : rtx this_op = ops[i].op;
6481 244696342 : int this_neg = ops[i].neg;
6482 244696342 : enum rtx_code this_code = GET_CODE (this_op);
6483 :
6484 244696342 : switch (this_code)
6485 : {
6486 39819141 : case PLUS:
6487 39819141 : case MINUS:
6488 39819141 : if (n_ops == ARRAY_SIZE (ops))
6489 : return NULL_RTX;
6490 :
6491 39819125 : ops[n_ops].op = XEXP (this_op, 1);
6492 39819125 : ops[n_ops].neg = (this_code == MINUS) ^ this_neg;
6493 39819125 : n_ops++;
6494 :
6495 39819125 : ops[i].op = XEXP (this_op, 0);
6496 39819125 : changed = 1;
6497 : /* If this operand was negated then we will potentially
6498 : canonicalize the expression. Similarly if we don't
6499 : place the operands adjacent we're re-ordering the
6500 : expression and thus might be performing a
6501 : canonicalization. Ignore register re-ordering.
6502 : ??? It might be better to shuffle the ops array here,
6503 : but then (plus (plus (A, B), plus (C, D))) wouldn't
6504 : be seen as non-canonical. */
6505 39819125 : if (this_neg
6506 39122565 : || (i != n_ops - 2
6507 38509248 : && !(REG_P (ops[i].op) && REG_P (ops[n_ops - 1].op))))
6508 244696326 : canonicalized = 1;
6509 : break;
6510 :
6511 2066 : case NEG:
6512 2066 : ops[i].op = XEXP (this_op, 0);
6513 2066 : ops[i].neg = ! this_neg;
6514 2066 : changed = 1;
6515 2066 : canonicalized = 1;
6516 2066 : break;
6517 :
6518 1601522 : case CONST:
6519 1601522 : if (n_ops != ARRAY_SIZE (ops)
6520 1601522 : && GET_CODE (XEXP (this_op, 0)) == PLUS
6521 1466903 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 0))
6522 1446219 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 1)))
6523 : {
6524 1446219 : ops[i].op = XEXP (XEXP (this_op, 0), 0);
6525 1446219 : ops[n_ops].op = XEXP (XEXP (this_op, 0), 1);
6526 1446219 : ops[n_ops].neg = this_neg;
6527 1446219 : n_ops++;
6528 1446219 : changed = 1;
6529 1446219 : canonicalized = 1;
6530 : }
6531 : break;
6532 :
6533 70131 : case NOT:
6534 : /* ~a -> (-a - 1) */
6535 70131 : if (n_ops != ARRAY_SIZE (ops))
6536 : {
6537 70131 : ops[n_ops].op = CONSTM1_RTX (mode);
6538 70131 : ops[n_ops++].neg = this_neg;
6539 70131 : ops[i].op = XEXP (this_op, 0);
6540 70131 : ops[i].neg = !this_neg;
6541 70131 : changed = 1;
6542 70131 : canonicalized = 1;
6543 : }
6544 : break;
6545 :
6546 121401228 : CASE_CONST_SCALAR_INT:
6547 121401228 : case CONST_POLY_INT:
6548 121401228 : n_constants++;
6549 121401228 : if (this_neg)
6550 : {
6551 1207931 : ops[i].op = neg_poly_int_rtx (mode, this_op);
6552 1207931 : ops[i].neg = 0;
6553 1207931 : changed = 1;
6554 1207931 : canonicalized = 1;
6555 : }
6556 : break;
6557 :
6558 : default:
6559 : break;
6560 : }
6561 : }
6562 : }
6563 80282130 : while (changed);
6564 :
6565 39476783 : if (n_constants > 1)
6566 24046054 : canonicalized = 1;
6567 :
6568 39476783 : gcc_assert (n_ops >= 2);
6569 :
6570 : /* If we only have two operands, we can avoid the loops. */
6571 39476783 : if (n_ops == 2)
6572 : {
6573 0 : enum rtx_code code = ops[0].neg || ops[1].neg ? MINUS : PLUS;
6574 0 : rtx lhs, rhs;
6575 :
6576 : /* Get the two operands. Be careful with the order, especially for
6577 : the cases where code == MINUS. */
6578 0 : if (ops[0].neg && ops[1].neg)
6579 : {
6580 0 : lhs = gen_rtx_NEG (mode, ops[0].op);
6581 0 : rhs = ops[1].op;
6582 : }
6583 0 : else if (ops[0].neg)
6584 : {
6585 0 : lhs = ops[1].op;
6586 0 : rhs = ops[0].op;
6587 : }
6588 : else
6589 : {
6590 0 : lhs = ops[0].op;
6591 0 : rhs = ops[1].op;
6592 : }
6593 :
6594 0 : return simplify_const_binary_operation (code, mode, lhs, rhs);
6595 : }
6596 :
6597 : /* Now simplify each pair of operands until nothing changes. */
6598 64528743 : while (1)
6599 : {
6600 : /* Insertion sort is good enough for a small array. */
6601 170841481 : for (i = 1; i < n_ops; i++)
6602 : {
6603 106312738 : struct simplify_plus_minus_op_data save;
6604 106312738 : int cmp;
6605 :
6606 106312738 : j = i - 1;
6607 106312738 : cmp = simplify_plus_minus_op_data_cmp (ops[j].op, ops[i].op);
6608 106312738 : if (cmp <= 0)
6609 93301444 : continue;
6610 : /* Just swapping registers doesn't count as canonicalization. */
6611 13011294 : if (cmp != 1)
6612 9847269 : canonicalized = 1;
6613 :
6614 13011294 : save = ops[i];
6615 15467960 : do
6616 15467960 : ops[j + 1] = ops[j];
6617 15467960 : while (j--
6618 28479254 : && simplify_plus_minus_op_data_cmp (ops[j].op, save.op) > 0);
6619 13011294 : ops[j + 1] = save;
6620 : }
6621 :
6622 64528743 : changed = 0;
6623 170841481 : for (i = n_ops - 1; i > 0; i--)
6624 255149206 : for (j = i - 1; j >= 0; j--)
6625 : {
6626 149722071 : rtx lhs = ops[j].op, rhs = ops[i].op;
6627 149722071 : int lneg = ops[j].neg, rneg = ops[i].neg;
6628 :
6629 149722071 : if (lhs != 0 && rhs != 0)
6630 : {
6631 123930359 : enum rtx_code ncode = PLUS;
6632 :
6633 123930359 : if (lneg != rneg)
6634 : {
6635 11642162 : ncode = MINUS;
6636 11642162 : if (lneg)
6637 7315431 : std::swap (lhs, rhs);
6638 : }
6639 112288197 : else if (swap_commutative_operands_p (lhs, rhs))
6640 425245 : std::swap (lhs, rhs);
6641 :
6642 123930359 : if ((GET_CODE (lhs) == CONST || CONST_INT_P (lhs))
6643 28974000 : && (GET_CODE (rhs) == CONST || CONST_INT_P (rhs)))
6644 : {
6645 24201897 : rtx tem_lhs, tem_rhs;
6646 :
6647 24201897 : tem_lhs = GET_CODE (lhs) == CONST ? XEXP (lhs, 0) : lhs;
6648 24201897 : tem_rhs = GET_CODE (rhs) == CONST ? XEXP (rhs, 0) : rhs;
6649 24201897 : tem = simplify_binary_operation (ncode, mode, tem_lhs,
6650 : tem_rhs);
6651 :
6652 24201897 : if (tem && !CONSTANT_P (tem))
6653 1725 : tem = gen_rtx_CONST (GET_MODE (tem), tem);
6654 : }
6655 : else
6656 99728462 : tem = simplify_binary_operation (ncode, mode, lhs, rhs);
6657 :
6658 99730187 : if (tem)
6659 : {
6660 : /* Reject "simplifications" that just wrap the two
6661 : arguments in a CONST. Failure to do so can result
6662 : in infinite recursion with simplify_binary_operation
6663 : when it calls us to simplify CONST operations.
6664 : Also, if we find such a simplification, don't try
6665 : any more combinations with this rhs: We must have
6666 : something like symbol+offset, ie. one of the
6667 : trivial CONST expressions we handle later. */
6668 26368577 : if (GET_CODE (tem) == CONST
6669 887328 : && GET_CODE (XEXP (tem, 0)) == ncode
6670 886777 : && XEXP (XEXP (tem, 0), 0) == lhs
6671 885603 : && XEXP (XEXP (tem, 0), 1) == rhs)
6672 : break;
6673 25482974 : lneg &= rneg;
6674 25482974 : if (GET_CODE (tem) == NEG)
6675 44914 : tem = XEXP (tem, 0), lneg = !lneg;
6676 25482974 : if (poly_int_rtx_p (tem) && lneg)
6677 0 : tem = neg_poly_int_rtx (mode, tem), lneg = 0;
6678 :
6679 25482974 : ops[i].op = tem;
6680 25482974 : ops[i].neg = lneg;
6681 25482974 : ops[j].op = NULL_RTX;
6682 25482974 : changed = 1;
6683 25482974 : canonicalized = 1;
6684 : }
6685 : }
6686 : }
6687 :
6688 64528743 : if (!changed)
6689 : break;
6690 :
6691 : /* Pack all the operands to the lower-numbered entries. */
6692 101087598 : for (i = 0, j = 0; j < n_ops; j++)
6693 76035638 : if (ops[j].op)
6694 : {
6695 50552664 : ops[i] = ops[j];
6696 50552664 : i++;
6697 : }
6698 : n_ops = i;
6699 : }
6700 :
6701 : /* If nothing changed, check that rematerialization of rtl instructions
6702 : is still required. */
6703 39476783 : if (!canonicalized)
6704 : {
6705 : /* Perform rematerialization if only all operands are registers and
6706 : all operations are PLUS. */
6707 : /* ??? Also disallow (non-global, non-frame) fixed registers to work
6708 : around rs6000 and how it uses the CA register. See PR67145. */
6709 5434545 : for (i = 0; i < n_ops; i++)
6710 4355697 : if (ops[i].neg
6711 4069747 : || !REG_P (ops[i].op)
6712 7914125 : || (REGNO (ops[i].op) < FIRST_PSEUDO_REGISTER
6713 339956 : && fixed_regs[REGNO (ops[i].op)]
6714 237 : && !global_regs[REGNO (ops[i].op)]
6715 237 : && ops[i].op != frame_pointer_rtx
6716 111 : && ops[i].op != arg_pointer_rtx
6717 100 : && ops[i].op != stack_pointer_rtx))
6718 : return NULL_RTX;
6719 1078848 : goto gen_result;
6720 : }
6721 :
6722 : /* Create (minus -C X) instead of (neg (const (plus X C))). */
6723 37600666 : if (n_ops == 2
6724 23468225 : && CONST_INT_P (ops[1].op)
6725 22806952 : && CONSTANT_P (ops[0].op)
6726 163 : && ops[0].neg)
6727 57 : return gen_rtx_fmt_ee (MINUS, mode, ops[1].op, ops[0].op);
6728 :
6729 : /* We suppressed creation of trivial CONST expressions in the
6730 : combination loop to avoid recursion. Create one manually now.
6731 : The combination loop should have ensured that there is exactly
6732 : one CONST_INT, and the sort will have ensured that it is last
6733 : in the array and that any other constant will be next-to-last. */
6734 :
6735 37600609 : if (n_ops > 1
6736 37098444 : && poly_int_rtx_p (ops[n_ops - 1].op)
6737 71924288 : && CONSTANT_P (ops[n_ops - 2].op))
6738 : {
6739 1520459 : rtx value = ops[n_ops - 1].op;
6740 1520459 : if (ops[n_ops - 1].neg ^ ops[n_ops - 2].neg)
6741 713312 : value = neg_poly_int_rtx (mode, value);
6742 1520459 : if (CONST_INT_P (value))
6743 : {
6744 3040918 : ops[n_ops - 2].op = plus_constant (mode, ops[n_ops - 2].op,
6745 1520459 : INTVAL (value));
6746 1520459 : n_ops--;
6747 : }
6748 : }
6749 :
6750 : /* Put a non-negated operand first, if possible. */
6751 :
6752 39319622 : for (i = 0; i < n_ops && ops[i].neg; i++)
6753 1719013 : continue;
6754 37600609 : if (i == n_ops)
6755 10172 : ops[0].op = gen_rtx_NEG (mode, ops[0].op);
6756 37590437 : else if (i != 0)
6757 : {
6758 1612024 : tem = ops[0].op;
6759 1612024 : ops[0] = ops[i];
6760 1612024 : ops[i].op = tem;
6761 1612024 : ops[i].neg = 1;
6762 : }
6763 :
6764 : /* Now make the result by performing the requested operations. */
6765 35978413 : gen_result:
6766 38679457 : result = ops[0].op;
6767 90884562 : for (i = 1; i < n_ops; i++)
6768 104410210 : result = gen_rtx_fmt_ee (ops[i].neg ? MINUS : PLUS,
6769 : mode, result, ops[i].op);
6770 :
6771 : return result;
6772 1719013 : }
6773 :
6774 : /* Check whether an operand is suitable for calling simplify_plus_minus. */
6775 : static bool
6776 541560007 : plus_minus_operand_p (const_rtx x)
6777 : {
6778 541560007 : return GET_CODE (x) == PLUS
6779 541560007 : || GET_CODE (x) == MINUS
6780 541560007 : || (GET_CODE (x) == CONST
6781 1965155 : && GET_CODE (XEXP (x, 0)) == PLUS
6782 1338914 : && CONSTANT_P (XEXP (XEXP (x, 0), 0))
6783 1265989 : && CONSTANT_P (XEXP (XEXP (x, 0), 1)));
6784 : }
6785 :
6786 : /* Like simplify_binary_operation except used for relational operators.
6787 : MODE is the mode of the result. If MODE is VOIDmode, both operands must
6788 : not also be VOIDmode.
6789 :
6790 : CMP_MODE specifies in which mode the comparison is done in, so it is
6791 : the mode of the operands. If CMP_MODE is VOIDmode, it is taken from
6792 : the operands or, if both are VOIDmode, the operands are compared in
6793 : "infinite precision". */
6794 : rtx
6795 133633372 : simplify_context::simplify_relational_operation (rtx_code code,
6796 : machine_mode mode,
6797 : machine_mode cmp_mode,
6798 : rtx op0, rtx op1)
6799 : {
6800 133633372 : rtx tem, trueop0, trueop1;
6801 :
6802 133633372 : if (cmp_mode == VOIDmode)
6803 29845603 : cmp_mode = GET_MODE (op0);
6804 29845603 : if (cmp_mode == VOIDmode)
6805 253340 : cmp_mode = GET_MODE (op1);
6806 :
6807 133633372 : tem = simplify_const_relational_operation (code, cmp_mode, op0, op1);
6808 133633372 : if (tem)
6809 786751 : return relational_result (mode, cmp_mode, tem);
6810 :
6811 : /* For the following tests, ensure const0_rtx is op1. */
6812 132846621 : if (swap_commutative_operands_p (op0, op1)
6813 132846621 : || (op0 == const0_rtx && op1 != const0_rtx))
6814 2605750 : std::swap (op0, op1), code = swap_condition (code);
6815 :
6816 : /* If op0 is a compare, extract the comparison arguments from it. */
6817 132846621 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6818 14772168 : return simplify_gen_relational (code, mode, VOIDmode,
6819 14772168 : XEXP (op0, 0), XEXP (op0, 1));
6820 :
6821 118074453 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC)
6822 : return NULL_RTX;
6823 :
6824 86944749 : trueop0 = avoid_constant_pool_reference (op0);
6825 86944749 : trueop1 = avoid_constant_pool_reference (op1);
6826 86944749 : return simplify_relational_operation_1 (code, mode, cmp_mode,
6827 86944749 : trueop0, trueop1);
6828 : }
6829 :
6830 : /* This part of simplify_relational_operation is only used when CMP_MODE
6831 : is not in class MODE_CC (i.e. it is a real comparison).
6832 :
6833 : MODE is the mode of the result, while CMP_MODE specifies in which
6834 : mode the comparison is done in, so it is the mode of the operands. */
6835 :
6836 : rtx
6837 86944749 : simplify_context::simplify_relational_operation_1 (rtx_code code,
6838 : machine_mode mode,
6839 : machine_mode cmp_mode,
6840 : rtx op0, rtx op1)
6841 : {
6842 86944749 : enum rtx_code op0code = GET_CODE (op0);
6843 :
6844 86944749 : if (op1 == const0_rtx && COMPARISON_P (op0))
6845 : {
6846 : /* If op0 is a comparison, extract the comparison arguments
6847 : from it. */
6848 303343 : if (code == NE)
6849 : {
6850 133789 : if (GET_MODE (op0) == mode)
6851 178 : return simplify_rtx (op0);
6852 : else
6853 133611 : return simplify_gen_relational (GET_CODE (op0), mode, VOIDmode,
6854 133611 : XEXP (op0, 0), XEXP (op0, 1));
6855 : }
6856 169554 : else if (code == EQ)
6857 : {
6858 137087 : enum rtx_code new_code = reversed_comparison_code (op0, NULL);
6859 137087 : if (new_code != UNKNOWN)
6860 136770 : return simplify_gen_relational (new_code, mode, VOIDmode,
6861 136770 : XEXP (op0, 0), XEXP (op0, 1));
6862 : }
6863 : }
6864 :
6865 : /* (LTU/GEU (PLUS a C) C), where C is constant, can be simplified to
6866 : (GEU/LTU a -C). Likewise for (LTU/GEU (PLUS a C) a). */
6867 86674190 : if ((code == LTU || code == GEU)
6868 5237061 : && GET_CODE (op0) == PLUS
6869 640274 : && CONST_INT_P (XEXP (op0, 1))
6870 422155 : && (rtx_equal_p (op1, XEXP (op0, 0))
6871 280745 : || rtx_equal_p (op1, XEXP (op0, 1)))
6872 : /* (LTU/GEU (PLUS a 0) 0) is not the same as (GEU/LTU a 0). */
6873 86882268 : && XEXP (op0, 1) != const0_rtx)
6874 : {
6875 208078 : rtx new_cmp
6876 208078 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6877 209644 : return simplify_gen_relational ((code == LTU ? GEU : LTU), mode,
6878 208078 : cmp_mode, XEXP (op0, 0), new_cmp);
6879 : }
6880 :
6881 : /* (GTU (PLUS a C) (C - 1)) where C is a non-zero constant can be
6882 : transformed into (LTU a -C). */
6883 86466112 : if (code == GTU && GET_CODE (op0) == PLUS && CONST_INT_P (op1)
6884 320185 : && CONST_INT_P (XEXP (op0, 1))
6885 240631 : && (UINTVAL (op1) == UINTVAL (XEXP (op0, 1)) - 1)
6886 18930 : && XEXP (op0, 1) != const0_rtx)
6887 : {
6888 18930 : rtx new_cmp
6889 18930 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6890 18930 : return simplify_gen_relational (LTU, mode, cmp_mode,
6891 18930 : XEXP (op0, 0), new_cmp);
6892 : }
6893 :
6894 : /* Canonicalize (LTU/GEU (PLUS a b) b) as (LTU/GEU (PLUS a b) a). */
6895 86447182 : if ((code == LTU || code == GEU)
6896 5028983 : && GET_CODE (op0) == PLUS
6897 432196 : && rtx_equal_p (op1, XEXP (op0, 1))
6898 : /* Don't recurse "infinitely" for (LTU/GEU (PLUS b b) b). */
6899 86454000 : && !rtx_equal_p (op1, XEXP (op0, 0)))
6900 6818 : return simplify_gen_relational (code, mode, cmp_mode, op0,
6901 6818 : copy_rtx (XEXP (op0, 0)));
6902 :
6903 86440364 : if (op1 == const0_rtx)
6904 : {
6905 : /* Canonicalize (GTU x 0) as (NE x 0). */
6906 37922594 : if (code == GTU)
6907 77223 : return simplify_gen_relational (NE, mode, cmp_mode, op0, op1);
6908 : /* Canonicalize (LEU x 0) as (EQ x 0). */
6909 37845371 : if (code == LEU)
6910 34525 : return simplify_gen_relational (EQ, mode, cmp_mode, op0, op1);
6911 :
6912 37810846 : if ((code == NE || code == EQ)
6913 : /* Verify op0 is IOR */
6914 33883021 : && GET_CODE (op0) == IOR
6915 : /* only enters if op1 is 0 */
6916 : /* Verify IOR operand is NE */
6917 585464 : && GET_CODE (XEXP (op0, 0)) == NE
6918 21066 : && GET_MODE (XEXP (XEXP (op0, 0), 0)) == cmp_mode
6919 : /* Verify second NE operand is 0 */
6920 374 : && XEXP (XEXP (op0, 0), 1) == CONST0_RTX (cmp_mode))
6921 : {
6922 31 : rtx t = gen_rtx_IOR (cmp_mode, XEXP (XEXP (op0, 0), 0), XEXP (op0, 1));
6923 31 : t = gen_rtx_fmt_ee (code, mode, t, CONST0_RTX (mode));
6924 31 : return t;
6925 : }
6926 :
6927 : }
6928 48517770 : else if (op1 == const1_rtx)
6929 : {
6930 3334167 : switch (code)
6931 : {
6932 9480 : case GE:
6933 : /* Canonicalize (GE x 1) as (GT x 0). */
6934 9480 : return simplify_gen_relational (GT, mode, cmp_mode,
6935 9480 : op0, const0_rtx);
6936 200530 : case GEU:
6937 : /* Canonicalize (GEU x 1) as (NE x 0). */
6938 200530 : return simplify_gen_relational (NE, mode, cmp_mode,
6939 200530 : op0, const0_rtx);
6940 6851 : case LT:
6941 : /* Canonicalize (LT x 1) as (LE x 0). */
6942 6851 : return simplify_gen_relational (LE, mode, cmp_mode,
6943 6851 : op0, const0_rtx);
6944 53739 : case LTU:
6945 : /* Canonicalize (LTU x 1) as (EQ x 0). */
6946 53739 : return simplify_gen_relational (EQ, mode, cmp_mode,
6947 53739 : op0, const0_rtx);
6948 : default:
6949 : break;
6950 : }
6951 : }
6952 45183603 : else if (op1 == constm1_rtx)
6953 : {
6954 : /* Canonicalize (LE x -1) as (LT x 0). */
6955 1178009 : if (code == LE)
6956 1581 : return simplify_gen_relational (LT, mode, cmp_mode, op0, const0_rtx);
6957 : /* Canonicalize (GT x -1) as (GE x 0). */
6958 1176428 : if (code == GT)
6959 5212 : return simplify_gen_relational (GE, mode, cmp_mode, op0, const0_rtx);
6960 : }
6961 :
6962 : /* (eq/ne (plus x cst1) cst2) simplifies to (eq/ne x (cst2 - cst1)) */
6963 82123367 : if ((code == EQ || code == NE)
6964 64100281 : && (op0code == PLUS || op0code == MINUS)
6965 2492658 : && CONSTANT_P (op1)
6966 925073 : && CONSTANT_P (XEXP (op0, 1))
6967 516322 : && (INTEGRAL_MODE_P (cmp_mode) || flag_unsafe_math_optimizations))
6968 : {
6969 516288 : rtx x = XEXP (op0, 0);
6970 516288 : rtx c = XEXP (op0, 1);
6971 516288 : enum rtx_code invcode = op0code == PLUS ? MINUS : PLUS;
6972 516288 : rtx tem = simplify_gen_binary (invcode, cmp_mode, op1, c);
6973 :
6974 : /* Detect an infinite recursive condition, where we oscillate at this
6975 : simplification case between:
6976 : A + B == C <---> C - B == A,
6977 : where A, B, and C are all constants with non-simplifiable expressions,
6978 : usually SYMBOL_REFs. */
6979 516288 : if (GET_CODE (tem) == invcode
6980 57 : && CONSTANT_P (x)
6981 516306 : && rtx_equal_p (c, XEXP (tem, 1)))
6982 : return NULL_RTX;
6983 :
6984 516270 : return simplify_gen_relational (code, mode, cmp_mode, x, tem);
6985 : }
6986 :
6987 : /* (eq/ne (plus (x) (y)) y) simplifies to (eq/ne x 0). */
6988 63583993 : if ((code == EQ || code == NE)
6989 63583993 : && op0code == PLUS
6990 1633973 : && rtx_equal_p (XEXP (op0, 1), op1)
6991 250 : && !side_effects_p (op1)
6992 250 : && (INTEGRAL_MODE_P (cmp_mode) || flag_unsafe_math_optimizations))
6993 226 : return simplify_gen_relational (code, mode, cmp_mode,
6994 226 : XEXP (op0, 0), CONST0_RTX (cmp_mode));
6995 :
6996 : /* (ne:SI (zero_extract:SI FOO (const_int 1) BAR) (const_int 0))) is
6997 : the same as (zero_extract:SI FOO (const_int 1) BAR). */
6998 85534678 : scalar_int_mode int_mode, int_cmp_mode;
6999 85534678 : if (code == NE
7000 34238118 : && op1 == const0_rtx
7001 2299444 : && is_int_mode (mode, &int_mode)
7002 87757600 : && is_a <scalar_int_mode> (cmp_mode, &int_cmp_mode)
7003 : /* ??? Work-around BImode bugs in the ia64 backend. */
7004 2299444 : && int_mode != BImode
7005 2299420 : && int_cmp_mode != BImode
7006 2299420 : && nonzero_bits (op0, int_cmp_mode) == 1
7007 85534678 : && STORE_FLAG_VALUE == 1)
7008 153044 : return GET_MODE_SIZE (int_mode) > GET_MODE_SIZE (int_cmp_mode)
7009 76522 : ? simplify_gen_unary (ZERO_EXTEND, int_mode, op0, int_cmp_mode)
7010 19297 : : lowpart_subreg (int_mode, op0, int_cmp_mode);
7011 :
7012 : /* (eq/ne (xor x y) 0) simplifies to (eq/ne x y). */
7013 85458156 : if ((code == EQ || code == NE)
7014 63507245 : && op1 == const0_rtx
7015 33731213 : && op0code == XOR)
7016 15041 : return simplify_gen_relational (code, mode, cmp_mode,
7017 15041 : XEXP (op0, 0), XEXP (op0, 1));
7018 :
7019 : /* (eq/ne (xor x y) x) simplifies to (eq/ne y 0). */
7020 63492204 : if ((code == EQ || code == NE)
7021 63492204 : && op0code == XOR
7022 5780 : && rtx_equal_p (XEXP (op0, 0), op1)
7023 6 : && !side_effects_p (XEXP (op0, 0)))
7024 0 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 1),
7025 0 : CONST0_RTX (mode));
7026 :
7027 : /* Likewise (eq/ne (xor x y) y) simplifies to (eq/ne x 0). */
7028 85443115 : if ((code == EQ || code == NE)
7029 63492204 : && op0code == XOR
7030 5780 : && rtx_equal_p (XEXP (op0, 1), op1)
7031 85443299 : && !side_effects_p (XEXP (op0, 1)))
7032 184 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
7033 184 : CONST0_RTX (mode));
7034 :
7035 : /* (eq/ne (xor x C1) C2) simplifies to (eq/ne x (C1^C2)). */
7036 85442931 : if ((code == EQ || code == NE)
7037 63492020 : && op0code == XOR
7038 5596 : && CONST_SCALAR_INT_P (op1)
7039 1479 : && CONST_SCALAR_INT_P (XEXP (op0, 1)))
7040 876 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
7041 : simplify_gen_binary (XOR, cmp_mode,
7042 876 : XEXP (op0, 1), op1));
7043 :
7044 : /* Simplify eq/ne (and/ior x y) x/y) for targets with a BICS instruction or
7045 : constant folding if x/y is a constant. */
7046 63491144 : if ((code == EQ || code == NE)
7047 63491144 : && (op0code == AND || op0code == IOR)
7048 3665129 : && !side_effects_p (op1)
7049 3665023 : && op1 != CONST0_RTX (cmp_mode))
7050 : {
7051 : /* Both (eq/ne (and x y) x) and (eq/ne (ior x y) y) simplify to
7052 : (eq/ne (and (not y) x) 0). */
7053 479947 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 0), op1))
7054 957020 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 1), op1)))
7055 : {
7056 24922 : rtx not_y = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 1),
7057 : cmp_mode);
7058 24922 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_y, XEXP (op0, 0));
7059 :
7060 24922 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
7061 24922 : CONST0_RTX (cmp_mode));
7062 : }
7063 :
7064 : /* Both (eq/ne (and x y) y) and (eq/ne (ior x y) x) simplify to
7065 : (eq/ne (and (not x) y) 0). */
7066 455127 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 1), op1))
7067 885699 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 0), op1)))
7068 : {
7069 46403 : rtx not_x = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 0),
7070 : cmp_mode);
7071 46403 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_x, XEXP (op0, 1));
7072 :
7073 46403 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
7074 46403 : CONST0_RTX (cmp_mode));
7075 : }
7076 : }
7077 :
7078 : /* Optimize (cmp (and/ior x C1) C2) depending on the CMP and C1 and C2's
7079 : relationship. */
7080 85370730 : if ((op0code == AND || op0code == IOR)
7081 3843952 : && CONST_INT_P (op1)
7082 3665366 : && CONST_INT_P (XEXP (op0, 1)))
7083 : {
7084 2387270 : unsigned HOST_WIDE_INT c1 = UINTVAL (XEXP (op0, 1));
7085 2387270 : unsigned HOST_WIDE_INT c2 = UINTVAL (op1);
7086 :
7087 : /* For AND operations:
7088 : - (x & c1) == c2 when some bits are set in c2 but not in c1 -> false
7089 : - (x & c1) != c2 when some bits are set in c2 but not in c1 -> true
7090 : - (x & c1) >= c2 when c1 is less than c2 -> false
7091 : - (x & c1) < c2 when c1 is less than c2 -> true
7092 : - (x & c1) > c2 when c1 is less than or equal to c2 -> false
7093 : - (x & c1) <= c2 when c1 is less than or equal to c2 -> true
7094 :
7095 : For IOR operations:
7096 : - (x | c1) == c2 when some bits are set in c1 but not in c2 -> false
7097 : - (x | c1) != c2 when some bits are set in c1 but not in c2 -> true
7098 : - (x | c1) <= c2 when c1 is greater than c2 -> false
7099 : - (x | c1) > c2 when c1 is greater than c2 -> true
7100 : - (x | c1) < c2 when c1 is greater than or equal to c2 -> false
7101 : - (x | c1) >= c2 when c1 is greater than or equal to c2 -> true */
7102 2387270 : if ((op0code == AND
7103 2383695 : && ((code == EQ && (c1 & c2) != c2)
7104 2383677 : || (code == GEU && c1 < c2)
7105 2383677 : || (code == GTU && c1 <= c2)))
7106 2387252 : || ((op0code == IOR
7107 3575 : && ((code == EQ && (c1 & c2) != c1)
7108 3575 : || (code == LEU && c1 > c2)
7109 3575 : || (code == LTU && c1 >= c2)))))
7110 18 : return const0_rtx;
7111 :
7112 2387252 : if ((op0code == AND
7113 2383677 : && ((code == NE && (c1 & c2) != c2)
7114 2383540 : || (code == LTU && c1 < c2)
7115 2383540 : || (code == LEU && c1 <= c2)))
7116 2387115 : || ((op0code == IOR
7117 3575 : && ((code == NE && (c1 & c2) != c1)
7118 3515 : || (code == GTU && c1 > c2)
7119 3515 : || (code == GEU && c1 >= c2)))))
7120 197 : return const_true_rtx;
7121 : }
7122 :
7123 : /* (eq/ne (bswap x) C1) simplifies to (eq/ne x C2) with C2 swapped. */
7124 85370515 : if ((code == EQ || code == NE)
7125 63419604 : && GET_CODE (op0) == BSWAP
7126 354 : && CONST_SCALAR_INT_P (op1))
7127 123 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
7128 : simplify_gen_unary (BSWAP, cmp_mode,
7129 123 : op1, cmp_mode));
7130 :
7131 : /* (eq/ne (bswap x) (bswap y)) simplifies to (eq/ne x y). */
7132 63419481 : if ((code == EQ || code == NE)
7133 63419481 : && GET_CODE (op0) == BSWAP
7134 231 : && GET_CODE (op1) == BSWAP)
7135 18 : return simplify_gen_relational (code, mode, cmp_mode,
7136 18 : XEXP (op0, 0), XEXP (op1, 0));
7137 :
7138 85370374 : if (op0code == POPCOUNT && op1 == const0_rtx)
7139 0 : switch (code)
7140 : {
7141 0 : case EQ:
7142 0 : case LE:
7143 0 : case LEU:
7144 : /* (eq (popcount x) (const_int 0)) -> (eq x (const_int 0)). */
7145 0 : return simplify_gen_relational (EQ, mode, GET_MODE (XEXP (op0, 0)),
7146 : XEXP (op0, 0),
7147 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
7148 :
7149 0 : case NE:
7150 0 : case GT:
7151 0 : case GTU:
7152 : /* (ne (popcount x) (const_int 0)) -> (ne x (const_int 0)). */
7153 0 : return simplify_gen_relational (NE, mode, GET_MODE (XEXP (op0, 0)),
7154 : XEXP (op0, 0),
7155 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
7156 :
7157 : default:
7158 : break;
7159 : }
7160 :
7161 : /* (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) -> (and:SI x 1). */
7162 85370374 : if (code == NE
7163 34114861 : && op1 == const0_rtx
7164 17557067 : && (op0code == TRUNCATE
7165 157840 : || (partial_subreg_p (op0)
7166 157113 : && subreg_lowpart_p (op0)))
7167 133427 : && SCALAR_INT_MODE_P (mode)
7168 85370374 : && STORE_FLAG_VALUE == 1)
7169 : {
7170 35093 : rtx tmp = XEXP (op0, 0);
7171 35093 : if (GET_CODE (tmp) == ASHIFT
7172 2675 : && GET_MODE (tmp) == mode
7173 241 : && CONST_INT_P (XEXP (tmp, 1))
7174 241 : && is_int_mode (GET_MODE (op0), &int_mode)
7175 35334 : && INTVAL (XEXP (tmp, 1)) == GET_MODE_PRECISION (int_mode) - 1)
7176 241 : return simplify_gen_binary (AND, mode, XEXP (tmp, 0), const1_rtx);
7177 : }
7178 :
7179 : /* For two unsigned booleans A and B:
7180 :
7181 : A > B == ~B & A
7182 : A >= B == ~B | A
7183 : A < B == ~A & B
7184 : A <= B == ~A | B
7185 : A == B == ~A ^ B (== ~B ^ A)
7186 : A != B == A ^ B
7187 :
7188 : For signed comparisons, we have to take STORE_FLAG_VALUE into account,
7189 : with the rules above applying for positive STORE_FLAG_VALUE and with
7190 : the relations reversed for negative STORE_FLAG_VALUE. */
7191 85370133 : if (is_a<scalar_int_mode> (cmp_mode)
7192 82577374 : && COMPARISON_P (op0)
7193 85525504 : && COMPARISON_P (op1))
7194 : {
7195 49239 : rtx t = NULL_RTX;
7196 49239 : if (code == GTU || code == (STORE_FLAG_VALUE > 0 ? GT : LT))
7197 7275 : t = simplify_logical_relational_operation (AND, mode, op1, op0, true);
7198 : else if (code == GEU || code == (STORE_FLAG_VALUE > 0 ? GE : LE))
7199 7272 : t = simplify_logical_relational_operation (IOR, mode, op1, op0, true);
7200 : else if (code == LTU || code == (STORE_FLAG_VALUE > 0 ? LT : GT))
7201 7304 : t = simplify_logical_relational_operation (AND, mode, op0, op1, true);
7202 : else if (code == LEU || code == (STORE_FLAG_VALUE > 0 ? LE : GE))
7203 7272 : t = simplify_logical_relational_operation (IOR, mode, op0, op1, true);
7204 : else if (code == EQ)
7205 9697 : t = simplify_logical_relational_operation (XOR, mode, op0, op1, true);
7206 : else if (code == NE)
7207 10419 : t = simplify_logical_relational_operation (XOR, mode, op0, op1);
7208 : if (t)
7209 : return t;
7210 : }
7211 :
7212 : return NULL_RTX;
7213 : }
7214 :
7215 : enum
7216 : {
7217 : CMP_EQ = 1,
7218 : CMP_LT = 2,
7219 : CMP_GT = 4,
7220 : CMP_LTU = 8,
7221 : CMP_GTU = 16
7222 : };
7223 :
7224 :
7225 : /* Convert the known results for EQ, LT, GT, LTU, GTU contained in
7226 : KNOWN_RESULT to a CONST_INT, based on the requested comparison CODE
7227 : For KNOWN_RESULT to make sense it should be either CMP_EQ, or the
7228 : logical OR of one of (CMP_LT, CMP_GT) and one of (CMP_LTU, CMP_GTU).
7229 : For floating-point comparisons, assume that the operands were ordered. */
7230 :
7231 : static rtx
7232 840431 : comparison_result (enum rtx_code code, int known_results)
7233 : {
7234 840431 : switch (code)
7235 : {
7236 151779 : case EQ:
7237 151779 : case UNEQ:
7238 151779 : return (known_results & CMP_EQ) ? const_true_rtx : const0_rtx;
7239 472051 : case NE:
7240 472051 : case LTGT:
7241 472051 : return (known_results & CMP_EQ) ? const0_rtx : const_true_rtx;
7242 :
7243 9305 : case LT:
7244 9305 : case UNLT:
7245 9305 : return (known_results & CMP_LT) ? const_true_rtx : const0_rtx;
7246 8688 : case GE:
7247 8688 : case UNGE:
7248 8688 : return (known_results & CMP_LT) ? const0_rtx : const_true_rtx;
7249 :
7250 12089 : case GT:
7251 12089 : case UNGT:
7252 12089 : return (known_results & CMP_GT) ? const_true_rtx : const0_rtx;
7253 13180 : case LE:
7254 13180 : case UNLE:
7255 13180 : return (known_results & CMP_GT) ? const0_rtx : const_true_rtx;
7256 :
7257 44178 : case LTU:
7258 44178 : return (known_results & CMP_LTU) ? const_true_rtx : const0_rtx;
7259 29027 : case GEU:
7260 29027 : return (known_results & CMP_LTU) ? const0_rtx : const_true_rtx;
7261 :
7262 69548 : case GTU:
7263 69548 : return (known_results & CMP_GTU) ? const_true_rtx : const0_rtx;
7264 30520 : case LEU:
7265 30520 : return (known_results & CMP_GTU) ? const0_rtx : const_true_rtx;
7266 :
7267 0 : case ORDERED:
7268 0 : return const_true_rtx;
7269 66 : case UNORDERED:
7270 66 : return const0_rtx;
7271 0 : default:
7272 0 : gcc_unreachable ();
7273 : }
7274 : }
7275 :
7276 : /* Check if the given comparison (done in the given MODE) is actually
7277 : a tautology or a contradiction. If the mode is VOIDmode, the
7278 : comparison is done in "infinite precision". If no simplification
7279 : is possible, this function returns zero. Otherwise, it returns
7280 : either const_true_rtx or const0_rtx. */
7281 :
7282 : rtx
7283 133840572 : simplify_const_relational_operation (enum rtx_code code,
7284 : machine_mode mode,
7285 : rtx op0, rtx op1)
7286 : {
7287 140960411 : rtx tem;
7288 140960411 : rtx trueop0;
7289 140960411 : rtx trueop1;
7290 :
7291 140960411 : gcc_assert (mode != VOIDmode
7292 : || (GET_MODE (op0) == VOIDmode
7293 : && GET_MODE (op1) == VOIDmode));
7294 :
7295 : /* We only handle MODE_CC comparisons that are COMPARE against zero. */
7296 140960411 : if (GET_MODE_CLASS (mode) == MODE_CC
7297 45908617 : && (op1 != const0_rtx
7298 45908617 : || GET_CODE (op0) != COMPARE))
7299 : return NULL_RTX;
7300 :
7301 : /* If op0 is a compare, extract the comparison arguments from it. */
7302 109830707 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
7303 : {
7304 14778913 : op1 = XEXP (op0, 1);
7305 14778913 : op0 = XEXP (op0, 0);
7306 :
7307 14778913 : if (GET_MODE (op0) != VOIDmode)
7308 14649514 : mode = GET_MODE (op0);
7309 129399 : else if (GET_MODE (op1) != VOIDmode)
7310 96911 : mode = GET_MODE (op1);
7311 : else
7312 : return 0;
7313 : }
7314 :
7315 : /* We can't simplify MODE_CC values since we don't know what the
7316 : actual comparison is. */
7317 109798219 : if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
7318 : return 0;
7319 :
7320 : /* Make sure the constant is second. */
7321 109798219 : if (swap_commutative_operands_p (op0, op1))
7322 : {
7323 2993178 : std::swap (op0, op1);
7324 2993178 : code = swap_condition (code);
7325 : }
7326 :
7327 109798219 : trueop0 = avoid_constant_pool_reference (op0);
7328 109798219 : trueop1 = avoid_constant_pool_reference (op1);
7329 :
7330 : /* For integer comparisons of A and B maybe we can simplify A - B and can
7331 : then simplify a comparison of that with zero. If A and B are both either
7332 : a register or a CONST_INT, this can't help; testing for these cases will
7333 : prevent infinite recursion here and speed things up.
7334 :
7335 : We can only do this for EQ and NE comparisons as otherwise we may
7336 : lose or introduce overflow which we cannot disregard as undefined as
7337 : we do not know the signedness of the operation on either the left or
7338 : the right hand side of the comparison. */
7339 :
7340 109798219 : if (INTEGRAL_MODE_P (mode)
7341 107050810 : && trueop1 != CONST0_RTX (mode)
7342 55062803 : && (code == EQ || code == NE)
7343 35121039 : && ! ((REG_P (op0)
7344 10225395 : || CONST_SCALAR_INT_P (trueop0)
7345 10177034 : || CONST_VECTOR_P (trueop0))
7346 24944025 : && (REG_P (op1)
7347 15022352 : || CONST_SCALAR_INT_P (trueop1)
7348 3461720 : || CONST_VECTOR_P (trueop1)))
7349 13635713 : && (tem = simplify_binary_operation (MINUS, mode, op0, op1)) != 0
7350 : /* We cannot do this if tem is a nonzero address. */
7351 7119841 : && ! nonzero_address_p (tem))
7352 7119839 : return simplify_const_relational_operation (signed_condition (code),
7353 7119839 : mode, tem, CONST0_RTX (mode));
7354 :
7355 102678380 : if (! HONOR_NANS (mode) && code == ORDERED)
7356 0 : return const_true_rtx;
7357 :
7358 102678380 : if (! HONOR_NANS (mode) && code == UNORDERED)
7359 8 : return const0_rtx;
7360 :
7361 : /* For modes without NaNs, if the two operands are equal, we know the
7362 : result except if they have side-effects. Even with NaNs we know
7363 : the result of unordered comparisons and, if signaling NaNs are
7364 : irrelevant, also the result of LT/GT/LTGT. */
7365 102678372 : if ((! HONOR_NANS (trueop0)
7366 2247062 : || code == UNEQ || code == UNLE || code == UNGE
7367 : || ((code == LT || code == GT || code == LTGT)
7368 939645 : && ! HONOR_SNANS (trueop0)))
7369 101479924 : && rtx_equal_p (trueop0, trueop1)
7370 103257127 : && ! side_effects_p (trueop0))
7371 578710 : return comparison_result (code, CMP_EQ);
7372 :
7373 : /* If the operands are floating-point constants, see if we can fold
7374 : the result. */
7375 102099662 : if (CONST_DOUBLE_AS_FLOAT_P (trueop0)
7376 1515 : && CONST_DOUBLE_AS_FLOAT_P (trueop1)
7377 1515 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop0)))
7378 : {
7379 1515 : const REAL_VALUE_TYPE *d0 = CONST_DOUBLE_REAL_VALUE (trueop0);
7380 1515 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
7381 :
7382 : /* Comparisons are unordered iff at least one of the values is NaN. */
7383 1515 : if (REAL_VALUE_ISNAN (*d0) || REAL_VALUE_ISNAN (*d1))
7384 173 : switch (code)
7385 : {
7386 0 : case UNEQ:
7387 0 : case UNLT:
7388 0 : case UNGT:
7389 0 : case UNLE:
7390 0 : case UNGE:
7391 0 : case NE:
7392 0 : case UNORDERED:
7393 0 : return const_true_rtx;
7394 173 : case EQ:
7395 173 : case LT:
7396 173 : case GT:
7397 173 : case LE:
7398 173 : case GE:
7399 173 : case LTGT:
7400 173 : case ORDERED:
7401 173 : return const0_rtx;
7402 : default:
7403 : return 0;
7404 : }
7405 :
7406 1512 : return comparison_result (code,
7407 1512 : (real_equal (d0, d1) ? CMP_EQ :
7408 1512 : real_less (d0, d1) ? CMP_LT : CMP_GT));
7409 : }
7410 :
7411 : /* Otherwise, see if the operands are both integers. */
7412 102098147 : if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
7413 98896280 : && CONST_SCALAR_INT_P (trueop0) && CONST_SCALAR_INT_P (trueop1))
7414 : {
7415 : /* It would be nice if we really had a mode here. However, the
7416 : largest int representable on the target is as good as
7417 : infinite. */
7418 260379 : machine_mode cmode = (mode == VOIDmode) ? MAX_MODE_INT : mode;
7419 260379 : rtx_mode_t ptrueop0 = rtx_mode_t (trueop0, cmode);
7420 260379 : rtx_mode_t ptrueop1 = rtx_mode_t (trueop1, cmode);
7421 :
7422 260379 : if (wi::eq_p (ptrueop0, ptrueop1))
7423 0 : return comparison_result (code, CMP_EQ);
7424 : else
7425 : {
7426 260379 : int cr = wi::lts_p (ptrueop0, ptrueop1) ? CMP_LT : CMP_GT;
7427 260379 : cr |= wi::ltu_p (ptrueop0, ptrueop1) ? CMP_LTU : CMP_GTU;
7428 260379 : return comparison_result (code, cr);
7429 : }
7430 : }
7431 :
7432 : /* Optimize comparisons with upper and lower bounds. */
7433 101837768 : scalar_int_mode int_mode;
7434 101837768 : if (CONST_INT_P (trueop1)
7435 71306885 : && is_a <scalar_int_mode> (mode, &int_mode)
7436 71306885 : && HWI_COMPUTABLE_MODE_P (int_mode)
7437 172681721 : && !side_effects_p (trueop0))
7438 : {
7439 70691465 : int sign;
7440 70691465 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (trueop0, int_mode);
7441 70691465 : HOST_WIDE_INT val = INTVAL (trueop1);
7442 70691465 : HOST_WIDE_INT mmin, mmax;
7443 :
7444 70691465 : if (code == GEU
7445 70691465 : || code == LEU
7446 67405061 : || code == GTU
7447 67405061 : || code == LTU)
7448 : sign = 0;
7449 : else
7450 70691465 : sign = 1;
7451 :
7452 : /* Get a reduced range if the sign bit is zero. */
7453 70691465 : if (nonzero <= (GET_MODE_MASK (int_mode) >> 1))
7454 : {
7455 6319617 : mmin = 0;
7456 6319617 : mmax = nonzero;
7457 : }
7458 : else
7459 : {
7460 64371848 : rtx mmin_rtx, mmax_rtx;
7461 64371848 : get_mode_bounds (int_mode, sign, int_mode, &mmin_rtx, &mmax_rtx);
7462 :
7463 64371848 : mmin = INTVAL (mmin_rtx);
7464 64371848 : mmax = INTVAL (mmax_rtx);
7465 64371848 : if (sign)
7466 : {
7467 58223516 : unsigned int sign_copies
7468 58223516 : = num_sign_bit_copies (trueop0, int_mode);
7469 :
7470 58223516 : mmin >>= (sign_copies - 1);
7471 58223516 : mmax >>= (sign_copies - 1);
7472 : }
7473 : }
7474 :
7475 70691465 : switch (code)
7476 : {
7477 : /* x >= y is always true for y <= mmin, always false for y > mmax. */
7478 543678 : case GEU:
7479 543678 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
7480 5697 : return const_true_rtx;
7481 537981 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
7482 48 : return const0_rtx;
7483 : break;
7484 983297 : case GE:
7485 983297 : if (val <= mmin)
7486 1966 : return const_true_rtx;
7487 981331 : if (val > mmax)
7488 0 : return const0_rtx;
7489 : break;
7490 :
7491 : /* x <= y is always true for y >= mmax, always false for y < mmin. */
7492 2742726 : case LEU:
7493 2742726 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
7494 15762 : return const_true_rtx;
7495 2726964 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
7496 0 : return const0_rtx;
7497 : break;
7498 2681343 : case LE:
7499 2681343 : if (val >= mmax)
7500 444 : return const_true_rtx;
7501 2680899 : if (val < mmin)
7502 0 : return const0_rtx;
7503 : break;
7504 :
7505 25720466 : case EQ:
7506 : /* x == y is always false for y out of range. */
7507 25720466 : if (val < mmin || val > mmax)
7508 446 : return const0_rtx;
7509 : break;
7510 :
7511 : /* x > y is always false for y >= mmax, always true for y < mmin. */
7512 2478581 : case GTU:
7513 2478581 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
7514 40381 : return const0_rtx;
7515 2438200 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
7516 0 : return const_true_rtx;
7517 : break;
7518 1918904 : case GT:
7519 1918904 : if (val >= mmax)
7520 244 : return const0_rtx;
7521 1918660 : if (val < mmin)
7522 2 : return const_true_rtx;
7523 : break;
7524 :
7525 : /* x < y is always false for y <= mmin, always true for y > mmax. */
7526 857092 : case LTU:
7527 857092 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
7528 3570 : return const0_rtx;
7529 853522 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
7530 78445 : return const_true_rtx;
7531 : break;
7532 1123821 : case LT:
7533 1123821 : if (val <= mmin)
7534 2287 : return const0_rtx;
7535 1121534 : if (val > mmax)
7536 3154 : return const_true_rtx;
7537 : break;
7538 :
7539 31641557 : case NE:
7540 : /* x != y is always true for y out of range. */
7541 31641557 : if (val < mmin || val > mmax)
7542 156 : return const_true_rtx;
7543 : break;
7544 :
7545 : default:
7546 : break;
7547 : }
7548 : }
7549 :
7550 : /* Optimize integer comparisons with zero. */
7551 101685166 : if (is_a <scalar_int_mode> (mode, &int_mode)
7552 98526380 : && trueop1 == const0_rtx
7553 51191531 : && !side_effects_p (trueop0))
7554 : {
7555 : /* Some addresses are known to be nonzero. We don't know
7556 : their sign, but equality comparisons are known. */
7557 51037027 : if (nonzero_address_p (trueop0))
7558 : {
7559 549 : if (code == EQ || code == LEU)
7560 286 : return const0_rtx;
7561 263 : if (code == NE || code == GTU)
7562 255 : return const_true_rtx;
7563 : }
7564 :
7565 : /* See if the first operand is an IOR with a constant. If so, we
7566 : may be able to determine the result of this comparison. */
7567 51036486 : if (GET_CODE (op0) == IOR)
7568 : {
7569 674701 : rtx inner_const = avoid_constant_pool_reference (XEXP (op0, 1));
7570 674701 : if (CONST_INT_P (inner_const) && inner_const != const0_rtx)
7571 : {
7572 207 : int sign_bitnum = GET_MODE_PRECISION (int_mode) - 1;
7573 414 : int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum
7574 207 : && (UINTVAL (inner_const)
7575 207 : & (HOST_WIDE_INT_1U
7576 : << sign_bitnum)));
7577 :
7578 207 : switch (code)
7579 : {
7580 : case EQ:
7581 : case LEU:
7582 : return const0_rtx;
7583 0 : case NE:
7584 0 : case GTU:
7585 0 : return const_true_rtx;
7586 13 : case LT:
7587 13 : case LE:
7588 13 : if (has_sign)
7589 2 : return const_true_rtx;
7590 : break;
7591 192 : case GT:
7592 192 : case GE:
7593 192 : if (has_sign)
7594 : return const0_rtx;
7595 : break;
7596 : default:
7597 : break;
7598 : }
7599 : }
7600 : }
7601 : }
7602 :
7603 : /* Optimize comparison of ABS with zero. */
7604 51539503 : if (trueop1 == CONST0_RTX (mode) && !side_effects_p (trueop0)
7605 153069115 : && (GET_CODE (trueop0) == ABS
7606 51384127 : || (GET_CODE (trueop0) == FLOAT_EXTEND
7607 86 : && GET_CODE (XEXP (trueop0, 0)) == ABS)))
7608 : {
7609 559 : switch (code)
7610 : {
7611 60 : case LT:
7612 : /* Optimize abs(x) < 0.0. */
7613 60 : if (!INTEGRAL_MODE_P (mode) && !HONOR_SNANS (mode))
7614 0 : return const0_rtx;
7615 : break;
7616 :
7617 42 : case GE:
7618 : /* Optimize abs(x) >= 0.0. */
7619 42 : if (!INTEGRAL_MODE_P (mode) && !HONOR_NANS (mode))
7620 0 : return const_true_rtx;
7621 : break;
7622 :
7623 0 : case UNGE:
7624 : /* Optimize ! (abs(x) < 0.0). */
7625 0 : return const_true_rtx;
7626 :
7627 : default:
7628 : break;
7629 : }
7630 : }
7631 :
7632 : return 0;
7633 : }
7634 :
7635 : /* Recognize expressions of the form (X CMP 0) ? VAL : OP (X)
7636 : where OP is CLZ or CTZ and VAL is the value from CLZ_DEFINED_VALUE_AT_ZERO
7637 : or CTZ_DEFINED_VALUE_AT_ZERO respectively and return OP (X) if the expression
7638 : can be simplified to that or NULL_RTX if not.
7639 : Assume X is compared against zero with CMP_CODE and the true
7640 : arm is TRUE_VAL and the false arm is FALSE_VAL. */
7641 :
7642 : rtx
7643 31807414 : simplify_context::simplify_cond_clz_ctz (rtx x, rtx_code cmp_code,
7644 : rtx true_val, rtx false_val)
7645 : {
7646 31807414 : if (cmp_code != EQ && cmp_code != NE)
7647 : return NULL_RTX;
7648 :
7649 : /* Result on X == 0 and X !=0 respectively. */
7650 22843531 : rtx on_zero, on_nonzero;
7651 22843531 : if (cmp_code == EQ)
7652 : {
7653 : on_zero = true_val;
7654 : on_nonzero = false_val;
7655 : }
7656 : else
7657 : {
7658 12447079 : on_zero = false_val;
7659 12447079 : on_nonzero = true_val;
7660 : }
7661 :
7662 22843531 : rtx_code op_code = GET_CODE (on_nonzero);
7663 22843531 : if ((op_code != CLZ && op_code != CTZ)
7664 2665 : || !rtx_equal_p (XEXP (on_nonzero, 0), x)
7665 22844786 : || !CONST_INT_P (on_zero))
7666 : return NULL_RTX;
7667 :
7668 294 : HOST_WIDE_INT op_val;
7669 294 : scalar_int_mode mode ATTRIBUTE_UNUSED
7670 294 : = as_a <scalar_int_mode> (GET_MODE (XEXP (on_nonzero, 0)));
7671 8 : if (((op_code == CLZ && CLZ_DEFINED_VALUE_AT_ZERO (mode, op_val))
7672 580 : || (op_code == CTZ && CTZ_DEFINED_VALUE_AT_ZERO (mode, op_val)))
7673 326 : && op_val == INTVAL (on_zero))
7674 0 : return on_nonzero;
7675 :
7676 : return NULL_RTX;
7677 : }
7678 :
7679 : /* Try to simplify X given that it appears within operand OP of a
7680 : VEC_MERGE operation whose mask is MASK. X need not use the same
7681 : vector mode as the VEC_MERGE, but it must have the same number of
7682 : elements.
7683 :
7684 : Return the simplified X on success, otherwise return NULL_RTX. */
7685 :
7686 : rtx
7687 2156012 : simplify_context::simplify_merge_mask (rtx x, rtx mask, int op)
7688 : {
7689 2156012 : gcc_assert (VECTOR_MODE_P (GET_MODE (x)));
7690 4312024 : poly_uint64 nunits = GET_MODE_NUNITS (GET_MODE (x));
7691 2156012 : if (GET_CODE (x) == VEC_MERGE && rtx_equal_p (XEXP (x, 2), mask))
7692 : {
7693 5491 : if (side_effects_p (XEXP (x, 1 - op)))
7694 : return NULL_RTX;
7695 :
7696 5267 : return XEXP (x, op);
7697 : }
7698 2150521 : if (UNARY_P (x)
7699 379368 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7700 2208285 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits))
7701 : {
7702 24236 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7703 24236 : if (top0)
7704 448 : return simplify_gen_unary (GET_CODE (x), GET_MODE (x), top0,
7705 448 : GET_MODE (XEXP (x, 0)));
7706 : }
7707 2150073 : if (BINARY_P (x)
7708 208671 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7709 416714 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7710 180988 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7711 2438219 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits))
7712 : {
7713 144073 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7714 144073 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7715 144073 : if (top0 || top1)
7716 : {
7717 952 : if (COMPARISON_P (x))
7718 0 : return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7719 0 : GET_MODE (XEXP (x, 0)) != VOIDmode
7720 : ? GET_MODE (XEXP (x, 0))
7721 0 : : GET_MODE (XEXP (x, 1)),
7722 : top0 ? top0 : XEXP (x, 0),
7723 0 : top1 ? top1 : XEXP (x, 1));
7724 : else
7725 952 : return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7726 : top0 ? top0 : XEXP (x, 0),
7727 952 : top1 ? top1 : XEXP (x, 1));
7728 : }
7729 : }
7730 2149121 : if (GET_RTX_CLASS (GET_CODE (x)) == RTX_TERNARY
7731 62839 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7732 125678 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7733 62839 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7734 125678 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits)
7735 62839 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 2)))
7736 2165929 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 2))), nunits))
7737 : {
7738 8404 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7739 8404 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7740 8404 : rtx top2 = simplify_merge_mask (XEXP (x, 2), mask, op);
7741 8404 : if (top0 || top1 || top2)
7742 448 : return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7743 448 : GET_MODE (XEXP (x, 0)),
7744 : top0 ? top0 : XEXP (x, 0),
7745 : top1 ? top1 : XEXP (x, 1),
7746 448 : top2 ? top2 : XEXP (x, 2));
7747 : }
7748 : return NULL_RTX;
7749 : }
7750 :
7751 :
7752 : /* Simplify CODE, an operation with result mode MODE and three operands,
7753 : OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
7754 : a constant. Return 0 if no simplifications is possible. */
7755 :
7756 : rtx
7757 44432693 : simplify_context::simplify_ternary_operation (rtx_code code, machine_mode mode,
7758 : machine_mode op0_mode,
7759 : rtx op0, rtx op1, rtx op2)
7760 : {
7761 44432693 : bool any_change = false;
7762 44432693 : rtx tem, trueop2;
7763 44432693 : scalar_int_mode int_mode, int_op0_mode;
7764 44432693 : unsigned int n_elts;
7765 :
7766 44432693 : switch (code)
7767 : {
7768 334269 : case FMA:
7769 : /* Simplify negations around the multiplication. */
7770 : /* -a * -b + c => a * b + c. */
7771 334269 : if (GET_CODE (op0) == NEG)
7772 : {
7773 91460 : tem = simplify_unary_operation (NEG, mode, op1, mode);
7774 91460 : if (tem)
7775 276 : op1 = tem, op0 = XEXP (op0, 0), any_change = true;
7776 : }
7777 242809 : else if (GET_CODE (op1) == NEG)
7778 : {
7779 1068 : tem = simplify_unary_operation (NEG, mode, op0, mode);
7780 1068 : if (tem)
7781 0 : op0 = tem, op1 = XEXP (op1, 0), any_change = true;
7782 : }
7783 :
7784 : /* Canonicalize the two multiplication operands. */
7785 : /* a * -b + c => -b * a + c. */
7786 334269 : if (swap_commutative_operands_p (op0, op1))
7787 : std::swap (op0, op1), any_change = true;
7788 :
7789 306532 : if (any_change)
7790 28004 : return gen_rtx_FMA (mode, op0, op1, op2);
7791 : return NULL_RTX;
7792 :
7793 803519 : case SIGN_EXTRACT:
7794 803519 : case ZERO_EXTRACT:
7795 803519 : if (CONST_INT_P (op0)
7796 17542 : && CONST_INT_P (op1)
7797 17542 : && CONST_INT_P (op2)
7798 44432725 : && is_a <scalar_int_mode> (mode, &int_mode)
7799 32 : && INTVAL (op1) + INTVAL (op2) <= GET_MODE_PRECISION (int_mode)
7800 803551 : && HWI_COMPUTABLE_MODE_P (int_mode))
7801 : {
7802 : /* Extracting a bit-field from a constant */
7803 32 : unsigned HOST_WIDE_INT val = UINTVAL (op0);
7804 32 : HOST_WIDE_INT op1val = INTVAL (op1);
7805 32 : HOST_WIDE_INT op2val = INTVAL (op2);
7806 32 : if (!BITS_BIG_ENDIAN)
7807 32 : val >>= op2val;
7808 : else if (is_a <scalar_int_mode> (op0_mode, &int_op0_mode))
7809 : val >>= GET_MODE_PRECISION (int_op0_mode) - op2val - op1val;
7810 : else
7811 : /* Not enough information to calculate the bit position. */
7812 : break;
7813 :
7814 32 : if (HOST_BITS_PER_WIDE_INT != op1val)
7815 : {
7816 : /* First zero-extend. */
7817 29 : val &= (HOST_WIDE_INT_1U << op1val) - 1;
7818 : /* If desired, propagate sign bit. */
7819 29 : if (code == SIGN_EXTRACT
7820 5 : && (val & (HOST_WIDE_INT_1U << (op1val - 1)))
7821 5 : != 0)
7822 2 : val |= ~ ((HOST_WIDE_INT_1U << op1val) - 1);
7823 : }
7824 :
7825 32 : return gen_int_mode (val, int_mode);
7826 : }
7827 : break;
7828 :
7829 42215021 : case IF_THEN_ELSE:
7830 42215021 : if (CONST_INT_P (op0))
7831 201440 : return op0 != const0_rtx ? op1 : op2;
7832 :
7833 : /* Convert c ? a : a into "a". Beware that two rtx_equal_p MEMs can
7834 : still carry different memory attributes, in particular incompatible
7835 : alias sets; returning one of them would narrow the aliasing of the
7836 : result to that operand's, which is unsound (PR125683). When the
7837 : attributes differ, fold to a copy that keeps only what both operands
7838 : guarantee, like merge_memattrs does when cross-jumping commons two
7839 : memory references. */
7840 42013581 : if (rtx_equal_p (op1, op2) && ! side_effects_p (op0))
7841 : {
7842 5428 : if (op1 == op2
7843 2976 : || !MEM_P (op1)
7844 5450 : || (mem_attrs_eq_p (get_mem_attrs (op1), get_mem_attrs (op2))
7845 8 : && MEM_READONLY_P (op1) == MEM_READONLY_P (op2)
7846 8 : && MEM_NOTRAP_P (op1) == MEM_NOTRAP_P (op2)
7847 8 : && MEM_POINTER (op1) == MEM_POINTER (op2)))
7848 : return op1;
7849 :
7850 : /* For BLKmode the size in MEM_ATTRS describes the access itself,
7851 : so it cannot be dropped. Volatility is not merged either: it
7852 : constrains when the access happens rather than describing the
7853 : memory, so unlike the flags below it cannot be weakened to what
7854 : both operands allow. Dropping it would lose a required access;
7855 : merge_memattrs and noce_try_cmove_arith instead set it, which is
7856 : sound but claims more than either operand did. Those two have to
7857 : put something on a reference they are already committed to, while
7858 : this fold is free to do nothing, and if-conversion never reaches
7859 : it with a volatile operand in any case: side_effects_p is true
7860 : for one, so noce_operand_ok rejects it. Decline the fold. */
7861 14 : if (GET_MODE (op1) != BLKmode
7862 14 : && MEM_VOLATILE_P (op1) == MEM_VOLATILE_P (op2))
7863 : {
7864 14 : rtx mem = shallow_copy_rtx (op1);
7865 :
7866 14 : if (MEM_ALIAS_SET (op1) != MEM_ALIAS_SET (op2))
7867 1 : set_mem_alias_set (mem, 0);
7868 :
7869 14 : if (!mem_expr_equal_p (MEM_EXPR (op1), MEM_EXPR (op2)))
7870 : {
7871 14 : set_mem_expr (mem, NULL_TREE);
7872 14 : clear_mem_offset (mem);
7873 : }
7874 0 : else if (MEM_OFFSET_KNOWN_P (op1) != MEM_OFFSET_KNOWN_P (op2)
7875 0 : || (MEM_OFFSET_KNOWN_P (op1)
7876 0 : && maybe_ne (MEM_OFFSET (op1), MEM_OFFSET (op2))))
7877 0 : clear_mem_offset (mem);
7878 :
7879 : /* Unlike merge_memattrs, which fixes up two references that
7880 : both stay in the stream, this returns a single reference
7881 : that stands in for either arm, so keep the size only when
7882 : both agree rather than taking the larger one. */
7883 28 : if (!MEM_SIZE_KNOWN_P (op1) || !MEM_SIZE_KNOWN_P (op2)
7884 28 : || maybe_ne (MEM_SIZE (op1), MEM_SIZE (op2)))
7885 0 : clear_mem_size (mem);
7886 :
7887 14 : set_mem_align (mem, MIN (MEM_ALIGN (op1), MEM_ALIGN (op2)));
7888 :
7889 : /* MEM_READONLY_P, MEM_NOTRAP_P and MEM_POINTER are rtx flag
7890 : bits rather than MEM_ATTRS fields, so shallow_copy_rtx has
7891 : already taken them from OP1 and they need clearing by hand.
7892 : Each asserts something about the reference, so the copy may
7893 : only keep it when both operands do, as merge_memattrs does
7894 : for the first two. */
7895 14 : if (MEM_READONLY_P (op1) != MEM_READONLY_P (op2))
7896 0 : MEM_READONLY_P (mem) = 0;
7897 14 : if (MEM_NOTRAP_P (op1) != MEM_NOTRAP_P (op2))
7898 0 : MEM_NOTRAP_P (mem) = 0;
7899 14 : if (MEM_POINTER (op1) != MEM_POINTER (op2))
7900 0 : MEM_POINTER (mem) = 0;
7901 :
7902 : return mem;
7903 : }
7904 : }
7905 :
7906 : /* Convert a != b ? a : b into "a". */
7907 42008153 : if (GET_CODE (op0) == NE
7908 16393166 : && ! side_effects_p (op0)
7909 16344065 : && ! HONOR_NANS (mode)
7910 16153570 : && ! HONOR_SIGNED_ZEROS (mode)
7911 58161723 : && ((rtx_equal_p (XEXP (op0, 0), op1)
7912 110648 : && rtx_equal_p (XEXP (op0, 1), op2))
7913 16153247 : || (rtx_equal_p (XEXP (op0, 0), op2)
7914 3952 : && rtx_equal_p (XEXP (op0, 1), op1))))
7915 : return op1;
7916 :
7917 : /* Convert a == b ? a : b into "b". */
7918 42007607 : if (GET_CODE (op0) == EQ
7919 13038559 : && ! side_effects_p (op0)
7920 13019084 : && ! HONOR_NANS (mode)
7921 12993948 : && ! HONOR_SIGNED_ZEROS (mode)
7922 55001555 : && ((rtx_equal_p (XEXP (op0, 0), op1)
7923 15443 : && rtx_equal_p (XEXP (op0, 1), op2))
7924 12993945 : || (rtx_equal_p (XEXP (op0, 0), op2)
7925 8139 : && rtx_equal_p (XEXP (op0, 1), op1))))
7926 : return op2;
7927 :
7928 : /* Convert a != 0 ? -a : 0 into "-a". */
7929 42007588 : if (GET_CODE (op0) == NE
7930 16392620 : && ! side_effects_p (op0)
7931 16343519 : && ! HONOR_NANS (mode)
7932 16153024 : && ! HONOR_SIGNED_ZEROS (mode)
7933 16153024 : && XEXP (op0, 1) == CONST0_RTX (mode)
7934 12256518 : && op2 == CONST0_RTX (mode)
7935 190079 : && GET_CODE (op1) == NEG
7936 42007637 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0)))
7937 : return op1;
7938 :
7939 : /* Convert a == 0 ? 0 : -a into "-a". */
7940 42007579 : if (GET_CODE (op0) == EQ
7941 13038540 : && ! side_effects_p (op0)
7942 13019065 : && ! HONOR_NANS (mode)
7943 12993929 : && ! HONOR_SIGNED_ZEROS (mode)
7944 12993929 : && op1 == CONST0_RTX (mode)
7945 43482 : && XEXP (op0, 1) == CONST0_RTX (mode)
7946 22745 : && GET_CODE (op2) == NEG
7947 42007585 : && rtx_equal_p (XEXP (op0, 0), XEXP (op2, 0)))
7948 : return op2;
7949 :
7950 : /* Convert (!c) != {0,...,0} ? a : b into
7951 : c != {0,...,0} ? b : a for vector modes. */
7952 42007573 : if (VECTOR_MODE_P (GET_MODE (op1))
7953 14643 : && GET_CODE (op0) == NE
7954 373 : && GET_CODE (XEXP (op0, 0)) == NOT
7955 0 : && GET_CODE (XEXP (op0, 1)) == CONST_VECTOR)
7956 : {
7957 0 : rtx cv = XEXP (op0, 1);
7958 0 : int nunits;
7959 0 : bool ok = true;
7960 0 : if (!CONST_VECTOR_NUNITS (cv).is_constant (&nunits))
7961 : ok = false;
7962 : else
7963 0 : for (int i = 0; i < nunits; ++i)
7964 0 : if (CONST_VECTOR_ELT (cv, i) != const0_rtx)
7965 : {
7966 : ok = false;
7967 : break;
7968 : }
7969 0 : if (ok)
7970 : {
7971 0 : rtx new_op0 = gen_rtx_NE (GET_MODE (op0),
7972 : XEXP (XEXP (op0, 0), 0),
7973 : XEXP (op0, 1));
7974 0 : rtx retval = gen_rtx_IF_THEN_ELSE (mode, new_op0, op2, op1);
7975 0 : return retval;
7976 : }
7977 : }
7978 :
7979 : /* Convert x == 0 ? N : clz (x) into clz (x) when
7980 : CLZ_DEFINED_VALUE_AT_ZERO is defined to N for the mode of x.
7981 : Similarly for ctz (x). */
7982 42006578 : if (COMPARISON_P (op0) && !side_effects_p (op0)
7983 83912733 : && XEXP (op0, 1) == const0_rtx)
7984 : {
7985 31807414 : rtx simplified
7986 31807414 : = simplify_cond_clz_ctz (XEXP (op0, 0), GET_CODE (op0),
7987 : op1, op2);
7988 31807414 : if (simplified)
7989 : return simplified;
7990 : }
7991 :
7992 42007573 : if (COMPARISON_P (op0) && ! side_effects_p (op0))
7993 : {
7994 83888756 : machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
7995 41905160 : ? GET_MODE (XEXP (op0, 1))
7996 : : GET_MODE (XEXP (op0, 0)));
7997 41905160 : rtx temp;
7998 :
7999 : /* Look for happy constants in op1 and op2. */
8000 41905160 : if (CONST_INT_P (op1) && CONST_INT_P (op2))
8001 : {
8002 234795 : HOST_WIDE_INT t = INTVAL (op1);
8003 234795 : HOST_WIDE_INT f = INTVAL (op2);
8004 :
8005 234795 : if (t == STORE_FLAG_VALUE && f == 0)
8006 55297 : code = GET_CODE (op0);
8007 179498 : else if (t == 0 && f == STORE_FLAG_VALUE)
8008 : {
8009 31509 : enum rtx_code tmp;
8010 31509 : tmp = reversed_comparison_code (op0, NULL);
8011 31509 : if (tmp == UNKNOWN)
8012 : break;
8013 : code = tmp;
8014 : }
8015 : else
8016 : break;
8017 :
8018 81555 : return simplify_gen_relational (code, mode, cmp_mode,
8019 81555 : XEXP (op0, 0), XEXP (op0, 1));
8020 : }
8021 :
8022 41670365 : temp = simplify_relational_operation (GET_CODE (op0), op0_mode,
8023 : cmp_mode, XEXP (op0, 0),
8024 : XEXP (op0, 1));
8025 :
8026 : /* See if any simplifications were possible. */
8027 41670365 : if (temp)
8028 : {
8029 7144 : if (CONST_INT_P (temp))
8030 827 : return temp == const0_rtx ? op2 : op1;
8031 6317 : else if (temp)
8032 6317 : return gen_rtx_IF_THEN_ELSE (mode, temp, op1, op2);
8033 : }
8034 : }
8035 : break;
8036 :
8037 1079884 : case VEC_MERGE:
8038 1079884 : gcc_assert (GET_MODE (op0) == mode);
8039 1079884 : gcc_assert (GET_MODE (op1) == mode);
8040 1079884 : gcc_assert (VECTOR_MODE_P (mode));
8041 1079884 : trueop2 = avoid_constant_pool_reference (op2);
8042 1079884 : if (CONST_INT_P (trueop2)
8043 1830759 : && GET_MODE_NUNITS (mode).is_constant (&n_elts))
8044 : {
8045 750875 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop2);
8046 750875 : unsigned HOST_WIDE_INT mask;
8047 750875 : if (n_elts == HOST_BITS_PER_WIDE_INT)
8048 : mask = -1;
8049 : else
8050 748392 : mask = (HOST_WIDE_INT_1U << n_elts) - 1;
8051 :
8052 750875 : if (!(sel & mask) && !side_effects_p (op0))
8053 : return op1;
8054 750450 : if ((sel & mask) == mask && !side_effects_p (op1))
8055 : return op0;
8056 :
8057 739272 : rtx trueop0 = avoid_constant_pool_reference (op0);
8058 739272 : rtx trueop1 = avoid_constant_pool_reference (op1);
8059 739272 : if (GET_CODE (trueop0) == CONST_VECTOR
8060 11147 : && GET_CODE (trueop1) == CONST_VECTOR)
8061 : {
8062 6790 : rtvec v = rtvec_alloc (n_elts);
8063 6790 : unsigned int i;
8064 :
8065 66426 : for (i = 0; i < n_elts; i++)
8066 52846 : RTVEC_ELT (v, i) = ((sel & (HOST_WIDE_INT_1U << i))
8067 52846 : ? CONST_VECTOR_ELT (trueop0, i)
8068 31009 : : CONST_VECTOR_ELT (trueop1, i));
8069 6790 : return gen_rtx_CONST_VECTOR (mode, v);
8070 : }
8071 :
8072 732482 : if (swap_commutative_operands_p (op0, op1)
8073 : /* Two operands have same precedence, then first bit of mask
8074 : select first operand. */
8075 732482 : || (!swap_commutative_operands_p (op1, op0) && !(sel & 1)))
8076 34748 : return simplify_gen_ternary (code, mode, mode, op1, op0,
8077 69496 : GEN_INT (~sel & mask));
8078 :
8079 : /* Replace (vec_merge (vec_merge a b m) c n) with (vec_merge b c n)
8080 : if no element from a appears in the result. */
8081 697734 : if (GET_CODE (op0) == VEC_MERGE)
8082 : {
8083 52310 : tem = avoid_constant_pool_reference (XEXP (op0, 2));
8084 52310 : if (CONST_INT_P (tem))
8085 : {
8086 36587 : unsigned HOST_WIDE_INT sel0 = UINTVAL (tem);
8087 36587 : if (!(sel & sel0 & mask) && !side_effects_p (XEXP (op0, 0)))
8088 99 : return simplify_gen_ternary (code, mode, mode,
8089 99 : XEXP (op0, 1), op1, op2);
8090 36488 : if (!(sel & ~sel0 & mask) && !side_effects_p (XEXP (op0, 1)))
8091 815 : return simplify_gen_ternary (code, mode, mode,
8092 815 : XEXP (op0, 0), op1, op2);
8093 :
8094 : /* Replace (vec_merge (vec_merge a b m) a n) with
8095 : (vec_merge a b (m|~n)). */
8096 35673 : if (rtx_equal_p (XEXP (op0, 0), op1)
8097 35673 : && ! side_effects_p (op1))
8098 463 : return simplify_gen_ternary (code, mode, mode,
8099 : op1, XEXP (op0, 1),
8100 926 : GEN_INT ((sel0 | ~sel) & mask));
8101 : /* Replace (vec_merge (vec_merge b a m) a n) with
8102 : (vec_merge b a (m&n)). */
8103 35210 : if (rtx_equal_p (XEXP (op0, 1), op1)
8104 35210 : && ! side_effects_p (op1))
8105 57 : return simplify_gen_ternary (code, mode, mode,
8106 : XEXP (op0, 0), op1,
8107 57 : GEN_INT (sel & sel0 & mask));
8108 : }
8109 : }
8110 696300 : if (GET_CODE (op1) == VEC_MERGE)
8111 : {
8112 580 : tem = avoid_constant_pool_reference (XEXP (op1, 2));
8113 580 : if (CONST_INT_P (tem))
8114 : {
8115 549 : unsigned HOST_WIDE_INT sel1 = UINTVAL (tem);
8116 549 : if (!(~sel & sel1 & mask) && !side_effects_p (XEXP (op1, 0)))
8117 518 : return simplify_gen_ternary (code, mode, mode,
8118 518 : op0, XEXP (op1, 1), op2);
8119 31 : if (!(~sel & ~sel1 & mask) && !side_effects_p (XEXP (op1, 1)))
8120 4 : return simplify_gen_ternary (code, mode, mode,
8121 4 : op0, XEXP (op1, 0), op2);
8122 :
8123 : /* Replace (vec_merge a (vec_merge a b m) n) with
8124 : (vec_merge a b (m|n)). */
8125 27 : if (rtx_equal_p (XEXP (op1, 0), op0)
8126 27 : && ! side_effects_p (op0))
8127 1 : return simplify_gen_ternary (code, mode, mode,
8128 : op0, XEXP (op1, 1),
8129 1 : GEN_INT ((sel | sel1) & mask));
8130 :
8131 : /* Replace (vec_merge a (vec_merge b a m) n) with
8132 : (vec_merge a b (~m|n)). */
8133 26 : if (rtx_equal_p (XEXP (op1, 1), op0)
8134 26 : && ! side_effects_p (op0))
8135 0 : return simplify_gen_ternary (code, mode, mode,
8136 : op0, XEXP (op1, 0),
8137 0 : GEN_INT ((sel | ~sel1) & mask));
8138 : }
8139 : }
8140 :
8141 : /* Replace (vec_merge (vec_duplicate (vec_select a parallel (i))) a 1 << i)
8142 : with a. */
8143 695777 : if (GET_CODE (op0) == VEC_DUPLICATE
8144 382432 : && GET_CODE (XEXP (op0, 0)) == VEC_SELECT
8145 1550 : && GET_CODE (XEXP (XEXP (op0, 0), 1)) == PARALLEL
8146 698877 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (op0, 0))), 1))
8147 : {
8148 1482 : tem = XVECEXP ((XEXP (XEXP (op0, 0), 1)), 0, 0);
8149 1482 : if (CONST_INT_P (tem) && CONST_INT_P (op2))
8150 : {
8151 1482 : if (XEXP (XEXP (op0, 0), 0) == op1
8152 2 : && UINTVAL (op2) == HOST_WIDE_INT_1U << UINTVAL (tem))
8153 : return op1;
8154 : }
8155 : }
8156 : /* Replace (vec_merge (vec_duplicate (X)) (const_vector [A, B])
8157 : (const_int N))
8158 : with (vec_concat (X) (B)) if N == 1 or
8159 : (vec_concat (A) (X)) if N == 2. */
8160 695775 : if (GET_CODE (op0) == VEC_DUPLICATE
8161 382430 : && GET_CODE (op1) == CONST_VECTOR
8162 547322 : && known_eq (CONST_VECTOR_NUNITS (op1), 2)
8163 1842 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8164 696696 : && IN_RANGE (sel, 1, 2))
8165 : {
8166 919 : rtx newop0 = XEXP (op0, 0);
8167 919 : rtx newop1 = CONST_VECTOR_ELT (op1, 2 - sel);
8168 919 : if (sel == 2)
8169 111 : std::swap (newop0, newop1);
8170 919 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8171 : }
8172 : /* Replace (vec_merge (vec_duplicate x) (vec_concat (y) (z)) (const_int N))
8173 : with (vec_concat x z) if N == 1, or (vec_concat y x) if N == 2.
8174 : Only applies for vectors of two elements. */
8175 694856 : if (GET_CODE (op0) == VEC_DUPLICATE
8176 381511 : && GET_CODE (op1) == VEC_CONCAT
8177 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8178 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
8179 694856 : && IN_RANGE (sel, 1, 2))
8180 : {
8181 0 : rtx newop0 = XEXP (op0, 0);
8182 0 : rtx newop1 = XEXP (op1, 2 - sel);
8183 0 : rtx otherop = XEXP (op1, sel - 1);
8184 0 : if (sel == 2)
8185 0 : std::swap (newop0, newop1);
8186 : /* Don't want to throw away the other part of the vec_concat if
8187 : it has side-effects. */
8188 0 : if (!side_effects_p (otherop))
8189 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8190 : }
8191 :
8192 : /* Replace:
8193 :
8194 : (vec_merge:outer (vec_duplicate:outer x:inner)
8195 : (subreg:outer y:inner 0)
8196 : (const_int N))
8197 :
8198 : with (vec_concat:outer x:inner y:inner) if N == 1,
8199 : or (vec_concat:outer y:inner x:inner) if N == 2.
8200 :
8201 : Implicitly, this means we have a paradoxical subreg, but such
8202 : a check is cheap, so make it anyway.
8203 :
8204 : Only applies for vectors of two elements. */
8205 694856 : if (GET_CODE (op0) == VEC_DUPLICATE
8206 381511 : && GET_CODE (op1) == SUBREG
8207 35333 : && GET_MODE (op1) == GET_MODE (op0)
8208 35333 : && GET_MODE (SUBREG_REG (op1)) == GET_MODE (XEXP (op0, 0))
8209 0 : && paradoxical_subreg_p (op1)
8210 0 : && subreg_lowpart_p (op1)
8211 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8212 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
8213 694856 : && IN_RANGE (sel, 1, 2))
8214 : {
8215 0 : rtx newop0 = XEXP (op0, 0);
8216 0 : rtx newop1 = SUBREG_REG (op1);
8217 0 : if (sel == 2)
8218 0 : std::swap (newop0, newop1);
8219 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8220 : }
8221 :
8222 : /* Same as above but with switched operands:
8223 : Replace (vec_merge:outer (subreg:outer x:inner 0)
8224 : (vec_duplicate:outer y:inner)
8225 : (const_int N))
8226 :
8227 : with (vec_concat:outer x:inner y:inner) if N == 1,
8228 : or (vec_concat:outer y:inner x:inner) if N == 2. */
8229 694856 : if (GET_CODE (op1) == VEC_DUPLICATE
8230 58763 : && GET_CODE (op0) == SUBREG
8231 20917 : && GET_MODE (op0) == GET_MODE (op1)
8232 20917 : && GET_MODE (SUBREG_REG (op0)) == GET_MODE (XEXP (op1, 0))
8233 0 : && paradoxical_subreg_p (op0)
8234 0 : && subreg_lowpart_p (op0)
8235 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
8236 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8237 694856 : && IN_RANGE (sel, 1, 2))
8238 : {
8239 0 : rtx newop0 = SUBREG_REG (op0);
8240 0 : rtx newop1 = XEXP (op1, 0);
8241 0 : if (sel == 2)
8242 0 : std::swap (newop0, newop1);
8243 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8244 : }
8245 :
8246 : /* Replace (vec_merge (vec_duplicate x) (vec_duplicate y)
8247 : (const_int n))
8248 : with (vec_concat x y) or (vec_concat y x) depending on value
8249 : of N. */
8250 694856 : if (GET_CODE (op0) == VEC_DUPLICATE
8251 381511 : && GET_CODE (op1) == VEC_DUPLICATE
8252 208 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8253 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
8254 694856 : && IN_RANGE (sel, 1, 2))
8255 : {
8256 0 : rtx newop0 = XEXP (op0, 0);
8257 0 : rtx newop1 = XEXP (op1, 0);
8258 0 : if (sel == 2)
8259 0 : std::swap (newop0, newop1);
8260 :
8261 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8262 : }
8263 : }
8264 :
8265 1023865 : if (rtx_equal_p (op0, op1)
8266 1023865 : && !side_effects_p (op2) && !side_effects_p (op1))
8267 : return op0;
8268 :
8269 1023568 : if (!side_effects_p (op2))
8270 : {
8271 1019622 : rtx top0
8272 1019622 : = may_trap_p (op0) ? NULL_RTX : simplify_merge_mask (op0, op2, 0);
8273 1019622 : rtx top1
8274 1019622 : = may_trap_p (op1) ? NULL_RTX : simplify_merge_mask (op1, op2, 1);
8275 1019622 : if (top0 || top1)
8276 998 : return simplify_gen_ternary (code, mode, mode,
8277 : top0 ? top0 : op0,
8278 819 : top1 ? top1 : op1, op2);
8279 : }
8280 :
8281 : break;
8282 :
8283 0 : default:
8284 0 : gcc_unreachable ();
8285 : }
8286 :
8287 : return 0;
8288 : }
8289 :
8290 : /* Try to calculate NUM_BYTES bytes of the target memory image of X,
8291 : starting at byte FIRST_BYTE. Return true on success and add the
8292 : bytes to BYTES, such that each byte has BITS_PER_UNIT bits and such
8293 : that the bytes follow target memory order. Leave BYTES unmodified
8294 : on failure.
8295 :
8296 : MODE is the mode of X. The caller must reserve NUM_BYTES bytes in
8297 : BYTES before calling this function. */
8298 :
8299 : bool
8300 14131854 : native_encode_rtx (machine_mode mode, rtx x, vec<target_unit> &bytes,
8301 : unsigned int first_byte, unsigned int num_bytes)
8302 : {
8303 : /* Check the mode is sensible. */
8304 14131854 : gcc_assert (GET_MODE (x) == VOIDmode
8305 : ? is_a <scalar_int_mode> (mode)
8306 : : mode == GET_MODE (x));
8307 :
8308 14131854 : if (GET_CODE (x) == CONST_VECTOR)
8309 : {
8310 : /* CONST_VECTOR_ELT follows target memory order, so no shuffling
8311 : is necessary. The only complication is that MODE_VECTOR_BOOL
8312 : vectors can have several elements per byte. */
8313 1147168 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
8314 : GET_MODE_NUNITS (mode));
8315 573584 : unsigned int elt = first_byte * BITS_PER_UNIT / elt_bits;
8316 573584 : if (elt_bits < BITS_PER_UNIT)
8317 : {
8318 : /* This is the only case in which elements can be smaller than
8319 : a byte. */
8320 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
8321 0 : auto mask = GET_MODE_MASK (GET_MODE_INNER (mode));
8322 0 : for (unsigned int i = 0; i < num_bytes; ++i)
8323 : {
8324 0 : target_unit value = 0;
8325 0 : for (unsigned int j = 0; j < BITS_PER_UNIT; j += elt_bits)
8326 : {
8327 0 : if (INTVAL (CONST_VECTOR_ELT (x, elt)))
8328 0 : value |= mask << j;
8329 0 : elt += 1;
8330 : }
8331 0 : bytes.quick_push (value);
8332 : }
8333 : return true;
8334 : }
8335 :
8336 573584 : unsigned int start = bytes.length ();
8337 573584 : unsigned int elt_bytes = GET_MODE_UNIT_SIZE (mode);
8338 : /* Make FIRST_BYTE relative to ELT. */
8339 573584 : first_byte %= elt_bytes;
8340 2848117 : while (num_bytes > 0)
8341 : {
8342 : /* Work out how many bytes we want from element ELT. */
8343 2274533 : unsigned int chunk_bytes = MIN (num_bytes, elt_bytes - first_byte);
8344 4549066 : if (!native_encode_rtx (GET_MODE_INNER (mode),
8345 : CONST_VECTOR_ELT (x, elt), bytes,
8346 : first_byte, chunk_bytes))
8347 : {
8348 0 : bytes.truncate (start);
8349 0 : return false;
8350 : }
8351 2274533 : elt += 1;
8352 2274533 : first_byte = 0;
8353 2274533 : num_bytes -= chunk_bytes;
8354 : }
8355 : return true;
8356 : }
8357 :
8358 : /* All subsequent cases are limited to scalars. */
8359 13558270 : scalar_mode smode;
8360 13558270 : if (!is_a <scalar_mode> (mode, &smode))
8361 : return false;
8362 :
8363 : /* Make sure that the region is in range. */
8364 13558270 : unsigned int end_byte = first_byte + num_bytes;
8365 13558270 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
8366 13558270 : gcc_assert (end_byte <= mode_bytes);
8367 :
8368 13558270 : if (CONST_SCALAR_INT_P (x))
8369 : {
8370 : /* The target memory layout is affected by both BYTES_BIG_ENDIAN
8371 : and WORDS_BIG_ENDIAN. Use the subreg machinery to get the lsb
8372 : position of each byte. */
8373 12862736 : rtx_mode_t value (x, smode);
8374 12862736 : wide_int_ref value_wi (value);
8375 67773200 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
8376 : {
8377 : /* Always constant because the inputs are. */
8378 42047728 : unsigned int lsb
8379 42047728 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
8380 : /* Operate directly on the encoding rather than using
8381 : wi::extract_uhwi, so that we preserve the sign or zero
8382 : extension for modes that are not a whole number of bits in
8383 : size. (Zero extension is only used for the combination of
8384 : innermode == BImode && STORE_FLAG_VALUE == 1). */
8385 42047728 : unsigned int elt = lsb / HOST_BITS_PER_WIDE_INT;
8386 42047728 : unsigned int shift = lsb % HOST_BITS_PER_WIDE_INT;
8387 42047728 : unsigned HOST_WIDE_INT uhwi = value_wi.elt (elt);
8388 42047728 : bytes.quick_push (uhwi >> shift);
8389 : }
8390 12862736 : return true;
8391 : }
8392 :
8393 695534 : if (CONST_DOUBLE_P (x))
8394 : {
8395 : /* real_to_target produces an array of integers in target memory order.
8396 : All integers before the last one have 32 bits; the last one may
8397 : have 32 bits or fewer, depending on whether the mode bitsize
8398 : is divisible by 32. Each of these integers is then laid out
8399 : in target memory as any other integer would be. */
8400 667418 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
8401 667418 : real_to_target (el32, CONST_DOUBLE_REAL_VALUE (x), smode);
8402 :
8403 : /* The (maximum) number of target bytes per element of el32. */
8404 667418 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
8405 667418 : gcc_assert (bytes_per_el32 != 0);
8406 :
8407 : /* Build up the integers in a similar way to the CONST_SCALAR_INT_P
8408 : handling above. */
8409 4560897 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
8410 : {
8411 3893479 : unsigned int index = byte / bytes_per_el32;
8412 3893479 : unsigned int subbyte = byte % bytes_per_el32;
8413 3893479 : unsigned int int_bytes = MIN (bytes_per_el32,
8414 : mode_bytes - index * bytes_per_el32);
8415 : /* Always constant because the inputs are. */
8416 3893479 : unsigned int lsb
8417 3893479 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
8418 3893479 : bytes.quick_push ((unsigned long) el32[index] >> lsb);
8419 : }
8420 667418 : return true;
8421 : }
8422 :
8423 28116 : if (GET_CODE (x) == CONST_FIXED)
8424 : {
8425 0 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
8426 : {
8427 : /* Always constant because the inputs are. */
8428 0 : unsigned int lsb
8429 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
8430 0 : unsigned HOST_WIDE_INT piece = CONST_FIXED_VALUE_LOW (x);
8431 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
8432 : {
8433 0 : lsb -= HOST_BITS_PER_WIDE_INT;
8434 0 : piece = CONST_FIXED_VALUE_HIGH (x);
8435 : }
8436 0 : bytes.quick_push (piece >> lsb);
8437 : }
8438 : return true;
8439 : }
8440 :
8441 : return false;
8442 : }
8443 :
8444 : /* Read a vector of mode MODE from the target memory image given by BYTES,
8445 : starting at byte FIRST_BYTE. The vector is known to be encodable using
8446 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each,
8447 : and BYTES is known to have enough bytes to supply NPATTERNS *
8448 : NELTS_PER_PATTERN vector elements. Each element of BYTES contains
8449 : BITS_PER_UNIT bits and the bytes are in target memory order.
8450 :
8451 : Return the vector on success, otherwise return NULL_RTX. */
8452 :
8453 : rtx
8454 300017 : native_decode_vector_rtx (machine_mode mode, const vec<target_unit> &bytes,
8455 : unsigned int first_byte, unsigned int npatterns,
8456 : unsigned int nelts_per_pattern)
8457 : {
8458 300017 : rtx_vector_builder builder (mode, npatterns, nelts_per_pattern);
8459 :
8460 600034 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
8461 : GET_MODE_NUNITS (mode));
8462 300017 : if (elt_bits < BITS_PER_UNIT)
8463 : {
8464 : /* This is the only case in which elements can be smaller than a byte.
8465 : Element 0 is always in the lsb of the containing byte. */
8466 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
8467 0 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8468 : {
8469 0 : unsigned int bit_index = first_byte * BITS_PER_UNIT + i * elt_bits;
8470 0 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8471 0 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8472 0 : unsigned int value = bytes[byte_index] >> lsb;
8473 0 : builder.quick_push (gen_int_mode (value, GET_MODE_INNER (mode)));
8474 : }
8475 : }
8476 : else
8477 : {
8478 1212259 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8479 : {
8480 1824484 : rtx x = native_decode_rtx (GET_MODE_INNER (mode), bytes, first_byte);
8481 912242 : if (!x)
8482 0 : return NULL_RTX;
8483 912242 : builder.quick_push (x);
8484 912242 : first_byte += elt_bits / BITS_PER_UNIT;
8485 : }
8486 : }
8487 300017 : return builder.build ();
8488 300017 : }
8489 :
8490 : /* Extract a PRECISION-bit integer from bytes [FIRST_BYTE, FIRST_BYTE + SIZE)
8491 : of target memory image BYTES. */
8492 :
8493 : wide_int
8494 11971247 : native_decode_int (const vec<target_unit> &bytes, unsigned int first_byte,
8495 : unsigned int size, unsigned int precision)
8496 : {
8497 : /* Pull the bytes msb first, so that we can use simple
8498 : shift-and-insert wide_int operations. */
8499 11971247 : wide_int result (wi::zero (precision));
8500 55443950 : for (unsigned int i = 0; i < size; ++i)
8501 : {
8502 43472703 : unsigned int lsb = (size - i - 1) * BITS_PER_UNIT;
8503 : /* Always constant because the inputs are. */
8504 43472703 : unsigned int subbyte
8505 43472703 : = subreg_size_offset_from_lsb (1, size, lsb).to_constant ();
8506 43472703 : result <<= BITS_PER_UNIT;
8507 43472703 : result |= bytes[first_byte + subbyte];
8508 : }
8509 11971247 : return result;
8510 : }
8511 :
8512 : /* Read an rtx of mode MODE from the target memory image given by BYTES,
8513 : starting at byte FIRST_BYTE. Each element of BYTES contains BITS_PER_UNIT
8514 : bits and the bytes are in target memory order. The image has enough
8515 : values to specify all bytes of MODE.
8516 :
8517 : Return the rtx on success, otherwise return NULL_RTX. */
8518 :
8519 : rtx
8520 12311835 : native_decode_rtx (machine_mode mode, const vec<target_unit> &bytes,
8521 : unsigned int first_byte)
8522 : {
8523 12311835 : if (VECTOR_MODE_P (mode))
8524 : {
8525 : /* If we know at compile time how many elements there are,
8526 : pull each element directly from BYTES. */
8527 94652 : unsigned int nelts;
8528 189304 : if (GET_MODE_NUNITS (mode).is_constant (&nelts))
8529 94652 : return native_decode_vector_rtx (mode, bytes, first_byte, nelts, 1);
8530 : return NULL_RTX;
8531 : }
8532 :
8533 12217183 : scalar_int_mode imode;
8534 12217183 : if (is_a <scalar_int_mode> (mode, &imode)
8535 11971247 : && GET_MODE_PRECISION (imode) <= MAX_BITSIZE_MODE_ANY_INT)
8536 : {
8537 11971247 : auto result = native_decode_int (bytes, first_byte,
8538 11971247 : GET_MODE_SIZE (imode),
8539 23942494 : GET_MODE_PRECISION (imode));
8540 11971247 : return immed_wide_int_const (result, imode);
8541 11971247 : }
8542 :
8543 245936 : scalar_float_mode fmode;
8544 245936 : if (is_a <scalar_float_mode> (mode, &fmode))
8545 : {
8546 : /* We need to build an array of integers in target memory order.
8547 : All integers before the last one have 32 bits; the last one may
8548 : have 32 bits or fewer, depending on whether the mode bitsize
8549 : is divisible by 32. */
8550 245906 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
8551 245906 : unsigned int num_el32 = CEIL (GET_MODE_BITSIZE (fmode), 32);
8552 245906 : memset (el32, 0, num_el32 * sizeof (long));
8553 :
8554 : /* The (maximum) number of target bytes per element of el32. */
8555 245906 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
8556 245906 : gcc_assert (bytes_per_el32 != 0);
8557 :
8558 245906 : unsigned int mode_bytes = GET_MODE_SIZE (fmode);
8559 1710372 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
8560 : {
8561 1464466 : unsigned int index = byte / bytes_per_el32;
8562 1464466 : unsigned int subbyte = byte % bytes_per_el32;
8563 1464466 : unsigned int int_bytes = MIN (bytes_per_el32,
8564 : mode_bytes - index * bytes_per_el32);
8565 : /* Always constant because the inputs are. */
8566 1464466 : unsigned int lsb
8567 1464466 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
8568 1464466 : el32[index] |= (unsigned long) bytes[first_byte + byte] << lsb;
8569 : }
8570 245906 : REAL_VALUE_TYPE r;
8571 245906 : real_from_target (&r, el32, fmode);
8572 245906 : return const_double_from_real_value (r, fmode);
8573 : }
8574 :
8575 30 : if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
8576 : {
8577 0 : scalar_mode smode = as_a <scalar_mode> (mode);
8578 0 : FIXED_VALUE_TYPE f;
8579 0 : f.data.low = 0;
8580 0 : f.data.high = 0;
8581 0 : f.mode = smode;
8582 :
8583 0 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
8584 0 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
8585 : {
8586 : /* Always constant because the inputs are. */
8587 0 : unsigned int lsb
8588 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
8589 0 : unsigned HOST_WIDE_INT unit = bytes[first_byte + byte];
8590 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
8591 0 : f.data.high |= unit << (lsb - HOST_BITS_PER_WIDE_INT);
8592 : else
8593 0 : f.data.low |= unit << lsb;
8594 : }
8595 0 : return CONST_FIXED_FROM_FIXED_VALUE (f, mode);
8596 : }
8597 :
8598 : return NULL_RTX;
8599 : }
8600 :
8601 : /* Simplify a byte offset BYTE into CONST_VECTOR X. The main purpose
8602 : is to convert a runtime BYTE value into a constant one. */
8603 :
8604 : static poly_uint64
8605 355517 : simplify_const_vector_byte_offset (rtx x, poly_uint64 byte)
8606 : {
8607 : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
8608 355517 : machine_mode mode = GET_MODE (x);
8609 711034 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
8610 : GET_MODE_NUNITS (mode));
8611 : /* The number of bits needed to encode one element from each pattern. */
8612 355517 : unsigned int sequence_bits = CONST_VECTOR_NPATTERNS (x) * elt_bits;
8613 :
8614 : /* Identify the start point in terms of a sequence number and a byte offset
8615 : within that sequence. */
8616 355517 : poly_uint64 first_sequence;
8617 355517 : unsigned HOST_WIDE_INT subbit;
8618 355517 : if (can_div_trunc_p (byte * BITS_PER_UNIT, sequence_bits,
8619 : &first_sequence, &subbit))
8620 : {
8621 355517 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
8622 355517 : if (nelts_per_pattern == 1)
8623 : /* This is a duplicated vector, so the value of FIRST_SEQUENCE
8624 : doesn't matter. */
8625 271898 : byte = subbit / BITS_PER_UNIT;
8626 83619 : else if (nelts_per_pattern == 2 && known_gt (first_sequence, 0U))
8627 : {
8628 : /* The subreg drops the first element from each pattern and
8629 : only uses the second element. Find the first sequence
8630 : that starts on a byte boundary. */
8631 5568 : subbit += least_common_multiple (sequence_bits, BITS_PER_UNIT);
8632 5568 : byte = subbit / BITS_PER_UNIT;
8633 : }
8634 : }
8635 355517 : return byte;
8636 : }
8637 :
8638 : /* Subroutine of simplify_subreg in which:
8639 :
8640 : - X is known to be a CONST_VECTOR
8641 : - OUTERMODE is known to be a vector mode
8642 :
8643 : Try to handle the subreg by operating on the CONST_VECTOR encoding
8644 : rather than on each individual element of the CONST_VECTOR.
8645 :
8646 : Return the simplified subreg on success, otherwise return NULL_RTX. */
8647 :
8648 : static rtx
8649 213525 : simplify_const_vector_subreg (machine_mode outermode, rtx x,
8650 : machine_mode innermode, unsigned int first_byte)
8651 : {
8652 : /* Paradoxical subregs of vectors have dubious semantics. */
8653 213525 : if (paradoxical_subreg_p (outermode, innermode))
8654 : return NULL_RTX;
8655 :
8656 : /* We can only preserve the semantics of a stepped pattern if the new
8657 : vector element is the same as the original one. */
8658 213354 : if (CONST_VECTOR_STEPPED_P (x)
8659 234560 : && GET_MODE_INNER (outermode) != GET_MODE_INNER (innermode))
8660 : return NULL_RTX;
8661 :
8662 : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
8663 205365 : unsigned int x_elt_bits
8664 205365 : = vector_element_size (GET_MODE_PRECISION (innermode),
8665 : GET_MODE_NUNITS (innermode));
8666 205365 : unsigned int out_elt_bits
8667 205365 : = vector_element_size (GET_MODE_PRECISION (outermode),
8668 : GET_MODE_NUNITS (outermode));
8669 :
8670 : /* The number of bits needed to encode one element from every pattern
8671 : of the original vector. */
8672 205365 : unsigned int x_sequence_bits = CONST_VECTOR_NPATTERNS (x) * x_elt_bits;
8673 :
8674 : /* The number of bits needed to encode one element from every pattern
8675 : of the result. */
8676 205365 : unsigned int out_sequence_bits
8677 205365 : = least_common_multiple (x_sequence_bits, out_elt_bits);
8678 :
8679 : /* Work out the number of interleaved patterns in the output vector
8680 : and the number of encoded elements per pattern. */
8681 205365 : unsigned int out_npatterns = out_sequence_bits / out_elt_bits;
8682 205365 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
8683 :
8684 : /* The encoding scheme requires the number of elements to be a multiple
8685 : of the number of patterns, so that each pattern appears at least once
8686 : and so that the same number of elements appear from each pattern. */
8687 410730 : bool ok_p = multiple_p (GET_MODE_NUNITS (outermode), out_npatterns);
8688 205365 : unsigned int const_nunits;
8689 410730 : if (GET_MODE_NUNITS (outermode).is_constant (&const_nunits)
8690 205365 : && (!ok_p || out_npatterns * nelts_per_pattern > const_nunits))
8691 : {
8692 : /* Either the encoding is invalid, or applying it would give us
8693 : more elements than we need. Just encode each element directly. */
8694 : out_npatterns = const_nunits;
8695 : nelts_per_pattern = 1;
8696 : }
8697 : else if (!ok_p)
8698 : return NULL_RTX;
8699 :
8700 : /* Get enough bytes of X to form the new encoding. */
8701 205365 : unsigned int buffer_bits = out_npatterns * nelts_per_pattern * out_elt_bits;
8702 205365 : unsigned int buffer_bytes = CEIL (buffer_bits, BITS_PER_UNIT);
8703 205365 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8704 205365 : if (!native_encode_rtx (innermode, x, buffer, first_byte, buffer_bytes))
8705 : return NULL_RTX;
8706 :
8707 : /* Re-encode the bytes as OUTERMODE. */
8708 205365 : return native_decode_vector_rtx (outermode, buffer, 0, out_npatterns,
8709 205365 : nelts_per_pattern);
8710 205365 : }
8711 :
8712 : /* Try to simplify a subreg of a constant by encoding the subreg region
8713 : as a sequence of target bytes and reading them back in the new mode.
8714 : Return the new value on success, otherwise return null.
8715 :
8716 : The subreg has outer mode OUTERMODE, inner mode INNERMODE, inner value X
8717 : and byte offset FIRST_BYTE. */
8718 :
8719 : static rtx
8720 11112841 : simplify_immed_subreg (fixed_size_mode outermode, rtx x,
8721 : machine_mode innermode, unsigned int first_byte)
8722 : {
8723 11112841 : unsigned int buffer_bytes = GET_MODE_SIZE (outermode);
8724 11112841 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8725 :
8726 : /* Some ports misuse CCmode. */
8727 11112841 : if (GET_MODE_CLASS (outermode) == MODE_CC && CONST_INT_P (x))
8728 : return x;
8729 :
8730 : /* Paradoxical subregs read undefined values for bytes outside of the
8731 : inner value. However, we have traditionally always sign-extended
8732 : integer constants and zero-extended others. */
8733 11110843 : unsigned int inner_bytes = buffer_bytes;
8734 11110843 : if (paradoxical_subreg_p (outermode, innermode))
8735 : {
8736 1104420 : if (!GET_MODE_SIZE (innermode).is_constant (&inner_bytes))
8737 0 : return NULL_RTX;
8738 :
8739 552210 : target_unit filler = 0;
8740 552210 : if (CONST_SCALAR_INT_P (x) && wi::neg_p (rtx_mode_t (x, innermode)))
8741 68010 : filler = -1;
8742 :
8743 : /* Add any leading bytes due to big-endian layout. The number of
8744 : bytes must be constant because both modes have constant size. */
8745 552210 : unsigned int leading_bytes
8746 552210 : = -byte_lowpart_offset (outermode, innermode).to_constant ();
8747 552210 : for (unsigned int i = 0; i < leading_bytes; ++i)
8748 0 : buffer.quick_push (filler);
8749 :
8750 552210 : if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8751 0 : return NULL_RTX;
8752 :
8753 : /* Add any trailing bytes due to little-endian layout. */
8754 7223070 : while (buffer.length () < buffer_bytes)
8755 3059325 : buffer.quick_push (filler);
8756 : }
8757 10558633 : else if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8758 : return NULL_RTX;
8759 11110843 : rtx ret = native_decode_rtx (outermode, buffer, 0);
8760 11110843 : if (ret && FLOAT_MODE_P (outermode))
8761 : {
8762 130119 : auto_vec<target_unit, 128> buffer2 (buffer_bytes);
8763 130119 : if (!native_encode_rtx (outermode, ret, buffer2, 0, buffer_bytes))
8764 : return NULL_RTX;
8765 1431722 : for (unsigned int i = 0; i < buffer_bytes; ++i)
8766 1301638 : if (buffer[i] != buffer2[i])
8767 : return NULL_RTX;
8768 130119 : }
8769 : return ret;
8770 11112841 : }
8771 :
8772 : /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
8773 : Return 0 if no simplifications are possible. */
8774 : rtx
8775 75719343 : simplify_context::simplify_subreg (machine_mode outermode, rtx op,
8776 : machine_mode innermode, poly_uint64 byte)
8777 : {
8778 : /* Little bit of sanity checking. */
8779 75719343 : gcc_assert (innermode != VOIDmode);
8780 75719343 : gcc_assert (outermode != VOIDmode);
8781 75719343 : gcc_assert (innermode != BLKmode);
8782 75719343 : gcc_assert (outermode != BLKmode);
8783 :
8784 75719343 : gcc_assert (GET_MODE (op) == innermode
8785 : || GET_MODE (op) == VOIDmode);
8786 :
8787 151438686 : poly_uint64 outersize = GET_MODE_SIZE (outermode);
8788 75719343 : if (!multiple_p (byte, outersize))
8789 : return NULL_RTX;
8790 :
8791 151438646 : poly_uint64 innersize = GET_MODE_SIZE (innermode);
8792 75719323 : if (maybe_ge (byte, innersize))
8793 : return NULL_RTX;
8794 :
8795 75719323 : if (outermode == innermode && known_eq (byte, 0U))
8796 4587018 : return op;
8797 :
8798 71132305 : if (GET_CODE (op) == CONST_VECTOR)
8799 355517 : byte = simplify_const_vector_byte_offset (op, byte);
8800 :
8801 142264610 : if (multiple_p (byte, GET_MODE_UNIT_SIZE (innermode)))
8802 : {
8803 65027515 : rtx elt;
8804 :
8805 55761905 : if (VECTOR_MODE_P (outermode)
8806 27796830 : && GET_MODE_INNER (outermode) == GET_MODE_INNER (innermode)
8807 67092005 : && vec_duplicate_p (op, &elt))
8808 13960 : return gen_vec_duplicate (outermode, elt);
8809 :
8810 65021847 : if (outermode == GET_MODE_INNER (innermode)
8811 65021847 : && vec_duplicate_p (op, &elt))
8812 8292 : return elt;
8813 : }
8814 :
8815 71118345 : if (CONST_SCALAR_INT_P (op)
8816 60204757 : || CONST_DOUBLE_AS_FLOAT_P (op)
8817 60147372 : || CONST_FIXED_P (op)
8818 60147372 : || GET_CODE (op) == CONST_VECTOR)
8819 : {
8820 11318206 : unsigned HOST_WIDE_INT cbyte;
8821 11318206 : if (byte.is_constant (&cbyte))
8822 : {
8823 11318206 : if (GET_CODE (op) == CONST_VECTOR && VECTOR_MODE_P (outermode))
8824 : {
8825 213525 : rtx tmp = simplify_const_vector_subreg (outermode, op,
8826 : innermode, cbyte);
8827 213525 : if (tmp)
8828 11318206 : return tmp;
8829 : }
8830 :
8831 11112841 : fixed_size_mode fs_outermode;
8832 11112841 : if (is_a <fixed_size_mode> (outermode, &fs_outermode))
8833 11112841 : return simplify_immed_subreg (fs_outermode, op, innermode, cbyte);
8834 : }
8835 : }
8836 :
8837 : /* Changing mode twice with SUBREG => just change it once,
8838 : or not at all if changing back op starting mode. */
8839 59800139 : if (GET_CODE (op) == SUBREG)
8840 : {
8841 1686967 : machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
8842 3373934 : poly_uint64 innermostsize = GET_MODE_SIZE (innermostmode);
8843 1686967 : rtx newx;
8844 :
8845 : /* Make sure that the relationship between the two subregs is
8846 : known at compile time. */
8847 1686967 : if (!ordered_p (outersize, innermostsize))
8848 : return NULL_RTX;
8849 :
8850 1686967 : if (outermode == innermostmode
8851 1019583 : && known_eq (byte, subreg_lowpart_offset (outermode, innermode))
8852 2706543 : && known_eq (SUBREG_BYTE (op),
8853 : subreg_lowpart_offset (innermode, innermostmode)))
8854 1019576 : return SUBREG_REG (op);
8855 :
8856 : /* Work out the memory offset of the final OUTERMODE value relative
8857 : to the inner value of OP. */
8858 667391 : poly_int64 mem_offset = subreg_memory_offset (outermode,
8859 : innermode, byte);
8860 667391 : poly_int64 op_mem_offset = subreg_memory_offset (op);
8861 667391 : poly_int64 final_offset = mem_offset + op_mem_offset;
8862 :
8863 : /* See whether resulting subreg will be paradoxical. */
8864 667391 : if (!paradoxical_subreg_p (outermode, innermostmode))
8865 : {
8866 : /* Bail out in case resulting subreg would be incorrect. */
8867 1038528 : if (maybe_lt (final_offset, 0)
8868 1038521 : || maybe_ge (poly_uint64 (final_offset), innermostsize)
8869 1038521 : || !multiple_p (final_offset, outersize))
8870 : return NULL_RTX;
8871 : }
8872 : else
8873 : {
8874 148127 : poly_int64 required_offset = subreg_memory_offset (outermode,
8875 : innermostmode, 0);
8876 148127 : if (maybe_ne (final_offset, required_offset))
8877 1 : return NULL_RTX;
8878 : /* Paradoxical subregs always have byte offset 0. */
8879 148126 : final_offset = 0;
8880 : }
8881 :
8882 : /* Recurse for further possible simplifications. */
8883 667375 : newx = simplify_subreg (outermode, SUBREG_REG (op), innermostmode,
8884 667375 : final_offset);
8885 667375 : if (newx)
8886 : return newx;
8887 666963 : if (validate_subreg (outermode, innermostmode,
8888 666963 : SUBREG_REG (op), final_offset))
8889 : {
8890 611225 : newx = gen_rtx_SUBREG (outermode, SUBREG_REG (op), final_offset);
8891 611225 : if (SUBREG_PROMOTED_VAR_P (op)
8892 787 : && SUBREG_PROMOTED_SIGN (op) >= 0
8893 787 : && GET_MODE_CLASS (outermode) == MODE_INT
8894 783 : && known_ge (outersize, innersize)
8895 363 : && known_le (outersize, innermostsize)
8896 611283 : && subreg_lowpart_p (newx))
8897 : {
8898 58 : SUBREG_PROMOTED_VAR_P (newx) = 1;
8899 58 : SUBREG_PROMOTED_SET (newx, SUBREG_PROMOTED_GET (op));
8900 : }
8901 : return newx;
8902 : }
8903 : return NULL_RTX;
8904 : }
8905 :
8906 : /* SUBREG of a hard register => just change the register number
8907 : and/or mode. If the hard register is not valid in that mode,
8908 : suppress this simplification. If the hard register is the stack,
8909 : frame, or argument pointer, leave this as a SUBREG. */
8910 :
8911 58113172 : if (REG_P (op) && HARD_REGISTER_P (op))
8912 : {
8913 11090061 : unsigned int regno, final_regno;
8914 :
8915 11090061 : regno = REGNO (op);
8916 11090061 : final_regno = simplify_subreg_regno (regno, innermode, byte, outermode);
8917 11090061 : if (HARD_REGISTER_NUM_P (final_regno))
8918 : {
8919 11065099 : rtx x = gen_rtx_REG_offset (op, outermode, final_regno,
8920 : subreg_memory_offset (outermode,
8921 : innermode, byte));
8922 :
8923 : /* Propagate original regno. We don't have any way to specify
8924 : the offset inside original regno, so do so only for lowpart.
8925 : The information is used only by alias analysis that cannot
8926 : grog partial register anyway. */
8927 :
8928 11065099 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte))
8929 8261381 : ORIGINAL_REGNO (x) = ORIGINAL_REGNO (op);
8930 : return x;
8931 : }
8932 : }
8933 :
8934 : /* If we have a SUBREG of a register that we are replacing and we are
8935 : replacing it with a MEM, make a new MEM and try replacing the
8936 : SUBREG with it. Don't do this if the MEM has a mode-dependent address
8937 : or if we would be widening it. */
8938 :
8939 47048073 : if (MEM_P (op)
8940 1681888 : && ! mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op))
8941 : /* Allow splitting of volatile memory references in case we don't
8942 : have instruction to move the whole thing. */
8943 1681885 : && (! MEM_VOLATILE_P (op)
8944 45822 : || ! have_insn_for (SET, innermode))
8945 : && !(STRICT_ALIGNMENT && MEM_ALIGN (op) < GET_MODE_ALIGNMENT (outermode))
8946 1636063 : && known_le (outersize, innersize)
8947 : /* You can't get the Nth element of a vector of addresses (i.e.
8948 : gather/scatter) by adding an offset to the base. */
8949 47873749 : && !VECTOR_MODE_P (GET_MODE (XEXP (op, 0))))
8950 825676 : return adjust_address_nv (op, outermode, byte);
8951 :
8952 : /* Simplify (subreg:<m> (mem:<vec_m> (reg:<vec_p> addr)) x)
8953 : to (mem:<m> (subreg:<p> (reg:<vec_p>)) y) */
8954 46222397 : if (MEM_P (op)
8955 856212 : && VECTOR_MODE_P (innermode)
8956 13210 : && VECTOR_MODE_P (GET_MODE (XEXP (op, 0)))
8957 46222397 : && GET_MODE_INNER (innermode) == outermode)
8958 : {
8959 0 : poly_uint64 index;
8960 0 : if (multiple_p (byte, GET_MODE_SIZE (outermode), &index))
8961 : {
8962 0 : rtx addr = XEXP (op, 0);
8963 0 : machine_mode addrmode = GET_MODE (addr);
8964 0 : scalar_mode addr_elt_mode = GET_MODE_INNER (addrmode);
8965 0 : poly_uint64 addr_byte = index * GET_MODE_SIZE (addr_elt_mode);
8966 :
8967 0 : rtx new_addr = simplify_gen_subreg (addr_elt_mode, addr, addrmode,
8968 : addr_byte);
8969 0 : if (new_addr)
8970 0 : return change_address_nv (op, outermode, new_addr);
8971 : }
8972 : }
8973 :
8974 : /* Handle complex or vector values represented as CONCAT or VEC_CONCAT
8975 : of two parts. */
8976 46222397 : if (GET_CODE (op) == CONCAT
8977 46222397 : || GET_CODE (op) == VEC_CONCAT)
8978 : {
8979 258768 : poly_uint64 final_offset;
8980 258768 : rtx part, res;
8981 :
8982 258768 : machine_mode part_mode = GET_MODE (XEXP (op, 0));
8983 258768 : if (part_mode == VOIDmode)
8984 21 : part_mode = GET_MODE_INNER (GET_MODE (op));
8985 517536 : poly_uint64 part_size = GET_MODE_SIZE (part_mode);
8986 258768 : if (known_lt (byte, part_size))
8987 : {
8988 257245 : part = XEXP (op, 0);
8989 257245 : final_offset = byte;
8990 : }
8991 1523 : else if (known_ge (byte, part_size))
8992 : {
8993 1523 : part = XEXP (op, 1);
8994 1523 : final_offset = byte - part_size;
8995 : }
8996 : else
8997 : return NULL_RTX;
8998 :
8999 258768 : if (maybe_gt (final_offset + outersize, part_size))
9000 : return NULL_RTX;
9001 :
9002 129022 : part_mode = GET_MODE (part);
9003 129022 : if (part_mode == VOIDmode)
9004 0 : part_mode = GET_MODE_INNER (GET_MODE (op));
9005 129022 : res = simplify_subreg (outermode, part, part_mode, final_offset);
9006 129022 : if (res)
9007 : return res;
9008 397 : if (GET_MODE (part) != VOIDmode
9009 397 : && validate_subreg (outermode, part_mode, part, final_offset))
9010 333 : return gen_rtx_SUBREG (outermode, part, final_offset);
9011 : return NULL_RTX;
9012 : }
9013 :
9014 : /* Simplify
9015 : (subreg (vec_merge (X)
9016 : (vector)
9017 : (const_int ((1 << N) | M)))
9018 : (N * sizeof (outermode)))
9019 : to
9020 : (subreg (X) (N * sizeof (outermode)))
9021 : */
9022 45963629 : unsigned int idx;
9023 91927258 : if (constant_multiple_p (byte, GET_MODE_SIZE (outermode), &idx)
9024 45963629 : && idx < HOST_BITS_PER_WIDE_INT
9025 45963629 : && GET_CODE (op) == VEC_MERGE
9026 539748 : && GET_MODE_INNER (innermode) == outermode
9027 4849 : && CONST_INT_P (XEXP (op, 2))
9028 45967905 : && (UINTVAL (XEXP (op, 2)) & (HOST_WIDE_INT_1U << idx)) != 0)
9029 4267 : return simplify_gen_subreg (outermode, XEXP (op, 0), innermode, byte);
9030 :
9031 : /* A SUBREG resulting from a zero extension may fold to zero if
9032 : it extracts higher bits that the ZERO_EXTEND's source bits. */
9033 45959362 : if (GET_CODE (op) == ZERO_EXTEND && SCALAR_INT_MODE_P (innermode))
9034 : {
9035 242212 : poly_uint64 bitpos = subreg_lsb_1 (outermode, innermode, byte);
9036 242212 : if (known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (XEXP (op, 0)))))
9037 56175 : return CONST0_RTX (outermode);
9038 : }
9039 :
9040 : /* Optimize SUBREGS of scalar integral ASHIFT by a valid constant. */
9041 45903187 : if (GET_CODE (op) == ASHIFT
9042 1081418 : && SCALAR_INT_MODE_P (innermode)
9043 998480 : && CONST_INT_P (XEXP (op, 1))
9044 915702 : && INTVAL (XEXP (op, 1)) > 0
9045 47900307 : && known_gt (GET_MODE_BITSIZE (innermode), INTVAL (XEXP (op, 1))))
9046 : {
9047 915702 : HOST_WIDE_INT val = INTVAL (XEXP (op, 1));
9048 : /* A lowpart SUBREG of a ASHIFT by a constant may fold to zero. */
9049 915702 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte)
9050 1793678 : && known_le (GET_MODE_BITSIZE (outermode), val))
9051 195814 : return CONST0_RTX (outermode);
9052 : /* Optimize the highpart SUBREG of a suitable ASHIFT (ZERO_EXTEND). */
9053 754398 : if (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
9054 35231 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == outermode
9055 70104 : && known_eq (GET_MODE_BITSIZE (outermode), val)
9056 69020 : && known_eq (GET_MODE_BITSIZE (innermode), 2 * val)
9057 789629 : && known_eq (subreg_highpart_offset (outermode, innermode), byte))
9058 34510 : return XEXP (XEXP (op, 0), 0);
9059 : }
9060 :
9061 47370514 : auto distribute_subreg = [&](rtx op)
9062 : {
9063 1663141 : return simplify_subreg (outermode, op, innermode, byte);
9064 45707373 : };
9065 :
9066 : /* Try distributing the subreg through logic operations, if that
9067 : leads to all subexpressions being simplified. For example,
9068 : distributing the outer subreg in:
9069 :
9070 : (subreg:SI (not:QI (subreg:QI (reg:SI X) <lowpart>)) 0)
9071 :
9072 : gives:
9073 :
9074 : (not:SI (reg:SI X))
9075 :
9076 : This should be a win if the outermode is word_mode, since logical
9077 : operations on word_mode should (a) be no more expensive than logical
9078 : operations on subword modes and (b) are likely to be cheaper than
9079 : logical operations on multiword modes.
9080 :
9081 : Otherwise, handle the case where the subreg is non-narrowing and does
9082 : not change the number of words. The non-narrowing condition ensures
9083 : that we don't convert word_mode operations to subword operations. */
9084 45707373 : scalar_int_mode int_outermode, int_innermode;
9085 45707373 : if (is_a <scalar_int_mode> (outermode, &int_outermode)
9086 38677944 : && is_a <scalar_int_mode> (innermode, &int_innermode)
9087 82874079 : && (outermode == word_mode
9088 22378244 : || ((GET_MODE_PRECISION (int_outermode)
9089 22378244 : >= GET_MODE_PRECISION (int_innermode))
9090 4453440 : && (CEIL (GET_MODE_SIZE (int_outermode), UNITS_PER_WORD)
9091 4383469 : <= CEIL (GET_MODE_SIZE (int_innermode), UNITS_PER_WORD)))))
9092 19111483 : switch (GET_CODE (op))
9093 : {
9094 35176 : case NOT:
9095 35176 : if (rtx op0 = distribute_subreg (XEXP (op, 0)))
9096 1899 : return simplify_gen_unary (GET_CODE (op), outermode, op0, outermode);
9097 : break;
9098 :
9099 502494 : case AND:
9100 502494 : case IOR:
9101 502494 : case XOR:
9102 502494 : if (rtx op0 = distribute_subreg (XEXP (op, 0)))
9103 209272 : if (rtx op1 = distribute_subreg (XEXP (op, 1)))
9104 204597 : return simplify_gen_binary (GET_CODE (op), outermode, op0, op1);
9105 : break;
9106 :
9107 : default:
9108 : break;
9109 : }
9110 :
9111 45500877 : if (is_a <scalar_int_mode> (outermode)
9112 38471448 : && is_a <scalar_int_mode> (innermode)
9113 83972325 : && known_eq (byte,
9114 : subreg_lowpart_offset (as_a <scalar_int_mode> (outermode),
9115 : as_a <scalar_int_mode> (innermode))))
9116 : {
9117 34643499 : scalar_int_mode int_outermode = as_a <scalar_int_mode> (outermode);
9118 34643499 : scalar_int_mode int_innermode = as_a <scalar_int_mode> (innermode);
9119 :
9120 : /* Handle polynomial integers. The upper bits of a paradoxical
9121 : subreg are undefined, so this is safe regardless of whether
9122 : we're truncating or extending. */
9123 34643499 : if (CONST_POLY_INT_P (op))
9124 : {
9125 : poly_wide_int val
9126 : = poly_wide_int::from (const_poly_int_value (op),
9127 : GET_MODE_PRECISION (int_outermode),
9128 : SIGNED);
9129 : return immed_wide_int_const (val, int_outermode);
9130 : }
9131 :
9132 34643499 : if (GET_MODE_PRECISION (int_outermode)
9133 34643499 : < GET_MODE_PRECISION (int_innermode))
9134 : {
9135 21615169 : rtx tem = simplify_truncation (int_outermode, op, int_innermode);
9136 21615169 : if (tem)
9137 : return tem;
9138 : }
9139 : }
9140 :
9141 : /* If the outer mode is not integral, try taking a subreg with the equivalent
9142 : integer outer mode and then bitcasting the result.
9143 : Other simplifications rely on integer to integer subregs and we'd
9144 : potentially miss out on optimizations otherwise. */
9145 87342968 : if (known_gt (GET_MODE_SIZE (innermode),
9146 : GET_MODE_SIZE (outermode))
9147 23448662 : && SCALAR_INT_MODE_P (innermode)
9148 22179526 : && !SCALAR_INT_MODE_P (outermode)
9149 67274224 : && int_mode_for_size (GET_MODE_BITSIZE (outermode),
9150 77039 : 0).exists (&int_outermode))
9151 : {
9152 77039 : rtx tem = simplify_subreg (int_outermode, op, innermode, byte);
9153 77039 : if (tem)
9154 1973 : return lowpart_subreg (outermode, tem, int_outermode);
9155 : }
9156 :
9157 : /* If OP is a vector comparison and the subreg is not changing the
9158 : number of elements or the size of the elements, change the result
9159 : of the comparison to the new mode. */
9160 43669511 : if (COMPARISON_P (op)
9161 307390 : && VECTOR_MODE_P (outermode)
9162 217635 : && VECTOR_MODE_P (innermode)
9163 652881 : && known_eq (GET_MODE_NUNITS (outermode), GET_MODE_NUNITS (innermode))
9164 44072348 : && known_eq (GET_MODE_UNIT_SIZE (outermode),
9165 : GET_MODE_UNIT_SIZE (innermode)))
9166 : /* INNERMODE is the mode of the comparison result; the comparison is
9167 : still done in the mode of its operands. */
9168 133935 : return simplify_gen_relational (GET_CODE (op), outermode, VOIDmode,
9169 133935 : XEXP (op, 0), XEXP (op, 1));
9170 :
9171 : /* Distribute non-paradoxical subregs through logic ops in cases where
9172 : one term disappears.
9173 :
9174 : (subreg:M1 (and:M2 X C1)) -> (subreg:M1 X)
9175 : (subreg:M1 (ior:M2 X C1)) -> (subreg:M1 C1)
9176 : (subreg:M1 (xor:M2 X C1)) -> (subreg:M1 (not:M2 X))
9177 :
9178 : if M2 is no smaller than M1 and (subreg:M1 C1) is all-ones.
9179 :
9180 : (subreg:M1 (and:M2 X C2)) -> (subreg:M1 C2)
9181 : (subreg:M1 (ior/xor:M2 X C2)) -> (subreg:M1 X)
9182 :
9183 : if M2 is no smaller than M1 and (subreg:M1 C2) is zero. */
9184 43535576 : if (known_ge (innersize, outersize)
9185 29915129 : && GET_MODE_CLASS (outermode) == GET_MODE_CLASS (innermode)
9186 27594148 : && (GET_CODE (op) == AND || GET_CODE (op) == IOR || GET_CODE (op) == XOR)
9187 45300419 : && CONSTANT_P (XEXP (op, 1)))
9188 : {
9189 908919 : rtx op1_subreg = distribute_subreg (XEXP (op, 1));
9190 908919 : if (op1_subreg == CONSTM1_RTX (outermode))
9191 : {
9192 124185 : if (GET_CODE (op) == IOR)
9193 : return op1_subreg;
9194 123951 : rtx op0 = XEXP (op, 0);
9195 123951 : if (GET_CODE (op) == XOR)
9196 889 : op0 = simplify_gen_unary (NOT, innermode, op0, innermode);
9197 123951 : return simplify_gen_subreg (outermode, op0, innermode, byte);
9198 : }
9199 :
9200 784734 : if (op1_subreg == CONST0_RTX (outermode))
9201 12533 : return (GET_CODE (op) == AND
9202 12533 : ? op1_subreg
9203 7280 : : distribute_subreg (XEXP (op, 0)));
9204 : }
9205 :
9206 : return NULL_RTX;
9207 : }
9208 :
9209 : /* Make a SUBREG operation or equivalent if it folds. */
9210 :
9211 : rtx
9212 46843253 : simplify_context::simplify_gen_subreg (machine_mode outermode, rtx op,
9213 : machine_mode innermode,
9214 : poly_uint64 byte)
9215 : {
9216 46843253 : rtx newx;
9217 :
9218 46843253 : newx = simplify_subreg (outermode, op, innermode, byte);
9219 46843253 : if (newx)
9220 : return newx;
9221 :
9222 22279878 : if (GET_CODE (op) == SUBREG
9223 22279878 : || GET_CODE (op) == CONCAT
9224 22246108 : || CONST_SCALAR_INT_P (op)
9225 22246082 : || CONST_DOUBLE_AS_FLOAT_P (op)
9226 22246082 : || CONST_FIXED_P (op)
9227 22246082 : || GET_CODE (op) == CONST_VECTOR)
9228 : return NULL_RTX;
9229 :
9230 22246072 : if (validate_subreg (outermode, innermode, op, byte))
9231 22213992 : return gen_rtx_SUBREG (outermode, op, byte);
9232 :
9233 : return NULL_RTX;
9234 : }
9235 :
9236 : /* Generates a subreg to get the least significant part of EXPR (in mode
9237 : INNER_MODE) to OUTER_MODE. */
9238 :
9239 : rtx
9240 34559976 : simplify_context::lowpart_subreg (machine_mode outer_mode, rtx expr,
9241 : machine_mode inner_mode)
9242 : {
9243 34559976 : return simplify_gen_subreg (outer_mode, expr, inner_mode,
9244 34559976 : subreg_lowpart_offset (outer_mode, inner_mode));
9245 : }
9246 :
9247 : /* Generate RTX to select element at INDEX out of vector OP. */
9248 :
9249 : rtx
9250 676739 : simplify_context::simplify_gen_vec_select (rtx op, unsigned int index)
9251 : {
9252 676739 : gcc_assert (VECTOR_MODE_P (GET_MODE (op)));
9253 :
9254 676739 : scalar_mode imode = GET_MODE_INNER (GET_MODE (op));
9255 :
9256 1353478 : if (known_eq (index * GET_MODE_SIZE (imode),
9257 : subreg_lowpart_offset (imode, GET_MODE (op))))
9258 : {
9259 676589 : rtx res = lowpart_subreg (imode, op, GET_MODE (op));
9260 676589 : if (res)
9261 : return res;
9262 : }
9263 :
9264 671 : rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (index)));
9265 671 : return gen_rtx_VEC_SELECT (imode, op, tmp);
9266 : }
9267 :
9268 :
9269 : /* Simplify X, an rtx expression.
9270 :
9271 : Return the simplified expression or NULL if no simplifications
9272 : were possible.
9273 :
9274 : This is the preferred entry point into the simplification routines;
9275 : however, we still allow passes to call the more specific routines.
9276 :
9277 : Right now GCC has three (yes, three) major bodies of RTL simplification
9278 : code that need to be unified.
9279 :
9280 : 1. fold_rtx in cse.cc. This code uses various CSE specific
9281 : information to aid in RTL simplification.
9282 :
9283 : 2. simplify_rtx in combine.cc. Similar to fold_rtx, except that
9284 : it uses combine specific information to aid in RTL
9285 : simplification.
9286 :
9287 : 3. The routines in this file.
9288 :
9289 :
9290 : Long term we want to only have one body of simplification code; to
9291 : get to that state I recommend the following steps:
9292 :
9293 : 1. Pour over fold_rtx & simplify_rtx and move any simplifications
9294 : which are not pass dependent state into these routines.
9295 :
9296 : 2. As code is moved by #1, change fold_rtx & simplify_rtx to
9297 : use this routine whenever possible.
9298 :
9299 : 3. Allow for pass dependent state to be provided to these
9300 : routines and add simplifications based on the pass dependent
9301 : state. Remove code from cse.cc & combine.cc that becomes
9302 : redundant/dead.
9303 :
9304 : It will take time, but ultimately the compiler will be easier to
9305 : maintain and improve. It's totally silly that when we add a
9306 : simplification that it needs to be added to 4 places (3 for RTL
9307 : simplification and 1 for tree simplification. */
9308 :
9309 : rtx
9310 47527904 : simplify_rtx (const_rtx x)
9311 : {
9312 47527904 : const enum rtx_code code = GET_CODE (x);
9313 47527904 : const machine_mode mode = GET_MODE (x);
9314 :
9315 47527904 : switch (GET_RTX_CLASS (code))
9316 : {
9317 789356 : case RTX_UNARY:
9318 1578712 : return simplify_unary_operation (code, mode,
9319 789356 : XEXP (x, 0), GET_MODE (XEXP (x, 0)));
9320 27321646 : case RTX_COMM_ARITH:
9321 27321646 : if (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
9322 438503 : return simplify_gen_binary (code, mode, XEXP (x, 1), XEXP (x, 0));
9323 :
9324 : /* Fall through. */
9325 :
9326 33397304 : case RTX_BIN_ARITH:
9327 33397304 : return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
9328 :
9329 82347 : case RTX_TERNARY:
9330 82347 : case RTX_BITFIELD_OPS:
9331 82347 : return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
9332 82347 : XEXP (x, 0), XEXP (x, 1),
9333 82347 : XEXP (x, 2));
9334 :
9335 205073 : case RTX_COMPARE:
9336 205073 : case RTX_COMM_COMPARE:
9337 205073 : return simplify_relational_operation (code, mode,
9338 205073 : ((GET_MODE (XEXP (x, 0))
9339 : != VOIDmode)
9340 : ? GET_MODE (XEXP (x, 0))
9341 355 : : GET_MODE (XEXP (x, 1))),
9342 205073 : XEXP (x, 0),
9343 410146 : XEXP (x, 1));
9344 :
9345 227783 : case RTX_EXTRA:
9346 227783 : if (code == SUBREG)
9347 2785 : return simplify_subreg (mode, SUBREG_REG (x),
9348 2785 : GET_MODE (SUBREG_REG (x)),
9349 2785 : SUBREG_BYTE (x));
9350 : break;
9351 :
9352 6582929 : case RTX_OBJ:
9353 6582929 : if (code == LO_SUM)
9354 : {
9355 : /* Convert (lo_sum (high FOO) FOO) to FOO. */
9356 0 : if (GET_CODE (XEXP (x, 0)) == HIGH
9357 0 : && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
9358 0 : return XEXP (x, 1);
9359 : }
9360 : break;
9361 :
9362 : default:
9363 : break;
9364 : }
9365 : return NULL;
9366 : }
9367 :
9368 : #if CHECKING_P
9369 :
9370 : namespace selftest {
9371 :
9372 : /* Make a unique pseudo REG of mode MODE for use by selftests. */
9373 :
9374 : static rtx
9375 2948 : make_test_reg (machine_mode mode)
9376 : {
9377 2948 : static int test_reg_num = LAST_VIRTUAL_REGISTER + 1;
9378 :
9379 2948 : return gen_rtx_REG (mode, test_reg_num++);
9380 : }
9381 :
9382 : /* Test reversing rotations with a masked negative count. */
9383 :
9384 : static void
9385 40 : test_masked_negative_rotate (machine_mode mode)
9386 : {
9387 40 : rtx value = make_test_reg (mode);
9388 40 : unsigned int precision = GET_MODE_PRECISION (mode).to_constant ();
9389 40 : int count_bits = exact_log2 (precision);
9390 40 : static const machine_mode count_modes[] = { QImode, SImode, DImode };
9391 40 : static const rtx_code codes[] = { ROTATE, ROTATERT };
9392 :
9393 160 : for (auto count_mode : count_modes)
9394 : {
9395 120 : rtx reg = make_test_reg (count_mode);
9396 120 : rtx neg = gen_rtx_NEG (count_mode, reg);
9397 120 : rtx mask = gen_int_mode (precision - 1, count_mode);
9398 120 : rtx count = gen_rtx_AND (count_mode, neg, mask);
9399 120 : rtx positive = simplify_gen_binary (AND, count_mode, reg, mask);
9400 :
9401 360 : for (auto code : codes)
9402 : {
9403 240 : rtx_code reverse = code == ROTATE ? ROTATERT : ROTATE;
9404 240 : rtx expected = NULL_RTX;
9405 240 : if (count_bits >= 0
9406 240 : && GET_MODE_PRECISION (count_mode).to_constant () > count_bits)
9407 200 : expected = simplify_gen_binary (reverse, mode, value, positive);
9408 240 : ASSERT_RTX_EQ (expected,
9409 : simplify_binary_operation (code, mode, value, count));
9410 :
9411 : /* A bare negative count or a narrower mask must not be folded. */
9412 240 : ASSERT_FALSE (simplify_binary_operation (code, mode, value, neg));
9413 240 : rtx narrow = gen_rtx_AND (count_mode, neg,
9414 : gen_int_mode (precision / 2 - 1, count_mode));
9415 240 : ASSERT_FALSE (simplify_binary_operation (code, mode, value, narrow));
9416 :
9417 240 : if (count_mode == QImode)
9418 80 : continue;
9419 160 : expected = NULL_RTX;
9420 160 : if (count_bits >= 0 && GET_MODE_PRECISION (QImode) > count_bits)
9421 112 : expected = simplify_gen_binary
9422 112 : (reverse, mode, value,
9423 : simplify_gen_unary (TRUNCATE, QImode, positive, count_mode));
9424 160 : rtx low = gen_rtx_SUBREG (QImode, count,
9425 : subreg_lowpart_offset (QImode, count_mode));
9426 160 : ASSERT_RTX_EQ (expected,
9427 : simplify_binary_operation (code, mode, value, low));
9428 160 : rtx trunc = gen_rtx_TRUNCATE (QImode, count);
9429 160 : ASSERT_RTX_EQ (expected,
9430 : simplify_binary_operation (code, mode, value, trunc));
9431 160 : poly_uint64 offset = subreg_highpart_offset (QImode, count_mode);
9432 160 : if (validate_subreg (QImode, count_mode, count, offset))
9433 : {
9434 0 : rtx high = gen_rtx_SUBREG (QImode, count, offset);
9435 0 : ASSERT_FALSE (simplify_binary_operation (code, mode, value, high));
9436 : }
9437 : }
9438 : }
9439 40 : }
9440 :
9441 : static void
9442 40 : test_scalar_int_ops (machine_mode mode)
9443 : {
9444 40 : rtx op0 = make_test_reg (mode);
9445 40 : rtx op1 = make_test_reg (mode);
9446 40 : rtx six = GEN_INT (6);
9447 :
9448 40 : rtx neg_op0 = simplify_gen_unary (NEG, mode, op0, mode);
9449 40 : rtx not_op0 = simplify_gen_unary (NOT, mode, op0, mode);
9450 40 : rtx bswap_op0 = simplify_gen_unary (BSWAP, mode, op0, mode);
9451 :
9452 40 : rtx and_op0_op1 = simplify_gen_binary (AND, mode, op0, op1);
9453 40 : rtx ior_op0_op1 = simplify_gen_binary (IOR, mode, op0, op1);
9454 40 : rtx xor_op0_op1 = simplify_gen_binary (XOR, mode, op0, op1);
9455 :
9456 40 : rtx and_op0_6 = simplify_gen_binary (AND, mode, op0, six);
9457 40 : rtx and_op1_6 = simplify_gen_binary (AND, mode, op1, six);
9458 :
9459 : /* Test some binary identities. */
9460 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, op0, const0_rtx));
9461 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, const0_rtx, op0));
9462 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MINUS, mode, op0, const0_rtx));
9463 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, op0, const1_rtx));
9464 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, const1_rtx, op0));
9465 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (DIV, mode, op0, const1_rtx));
9466 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, constm1_rtx));
9467 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, constm1_rtx, op0));
9468 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, const0_rtx));
9469 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, const0_rtx, op0));
9470 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, op0, const0_rtx));
9471 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, const0_rtx, op0));
9472 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFT, mode, op0, const0_rtx));
9473 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATE, mode, op0, const0_rtx));
9474 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFTRT, mode, op0, const0_rtx));
9475 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (LSHIFTRT, mode, op0, const0_rtx));
9476 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATERT, mode, op0, const0_rtx));
9477 :
9478 : /* Test some self-inverse operations. */
9479 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NEG, mode, neg_op0, mode));
9480 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NOT, mode, not_op0, mode));
9481 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (BSWAP, mode, bswap_op0, mode));
9482 :
9483 : /* Test some reflexive operations. */
9484 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, op0));
9485 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, op0));
9486 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMIN, mode, op0, op0));
9487 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMAX, mode, op0, op0));
9488 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMIN, mode, op0, op0));
9489 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMAX, mode, op0, op0));
9490 :
9491 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (MINUS, mode, op0, op0));
9492 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (XOR, mode, op0, op0));
9493 :
9494 : /* Test simplify_distributive_operation. */
9495 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, xor_op0_op1, six),
9496 : simplify_gen_binary (XOR, mode, and_op0_6, and_op1_6));
9497 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, ior_op0_op1, six),
9498 : simplify_gen_binary (IOR, mode, and_op0_6, and_op1_6));
9499 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, and_op0_op1, six),
9500 : simplify_gen_binary (AND, mode, and_op0_6, and_op1_6));
9501 :
9502 : /* Test useless extensions are eliminated. */
9503 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (TRUNCATE, mode, op0, mode));
9504 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (ZERO_EXTEND, mode, op0, mode));
9505 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (SIGN_EXTEND, mode, op0, mode));
9506 40 : ASSERT_RTX_EQ (op0, lowpart_subreg (mode, op0, mode));
9507 :
9508 40 : test_masked_negative_rotate (mode);
9509 40 : }
9510 :
9511 : /* Verify some simplifications of integer extension/truncation.
9512 : Machine mode BMODE is the guaranteed wider than SMODE. */
9513 :
9514 : static void
9515 24 : test_scalar_int_ext_ops (machine_mode bmode, machine_mode smode)
9516 : {
9517 24 : rtx sreg = make_test_reg (smode);
9518 :
9519 : /* Check truncation of extension. */
9520 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9521 : simplify_gen_unary (ZERO_EXTEND, bmode,
9522 : sreg, smode),
9523 : bmode),
9524 : sreg);
9525 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9526 : simplify_gen_unary (SIGN_EXTEND, bmode,
9527 : sreg, smode),
9528 : bmode),
9529 : sreg);
9530 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9531 : lowpart_subreg (bmode, sreg, smode),
9532 : bmode),
9533 : sreg);
9534 :
9535 : /* Test extensions, followed by logic ops, followed by truncations. */
9536 24 : rtx bsubreg = lowpart_subreg (bmode, sreg, smode);
9537 24 : rtx smask = gen_int_mode (GET_MODE_MASK (smode), bmode);
9538 24 : rtx inv_smask = gen_int_mode (~GET_MODE_MASK (smode), bmode);
9539 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9540 : simplify_gen_binary (AND, bmode,
9541 : bsubreg, smask),
9542 : bmode),
9543 : sreg);
9544 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9545 : simplify_gen_binary (AND, bmode,
9546 : bsubreg, inv_smask),
9547 : bmode),
9548 : const0_rtx);
9549 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9550 : simplify_gen_binary (IOR, bmode,
9551 : bsubreg, smask),
9552 : bmode),
9553 : constm1_rtx);
9554 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9555 : simplify_gen_binary (IOR, bmode,
9556 : bsubreg, inv_smask),
9557 : bmode),
9558 : sreg);
9559 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9560 : simplify_gen_binary (XOR, bmode,
9561 : bsubreg, smask),
9562 : bmode),
9563 : lowpart_subreg (smode,
9564 : gen_rtx_NOT (bmode, bsubreg),
9565 : bmode));
9566 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9567 : simplify_gen_binary (XOR, bmode,
9568 : bsubreg, inv_smask),
9569 : bmode),
9570 : sreg);
9571 :
9572 24 : if (known_le (GET_MODE_PRECISION (bmode), BITS_PER_WORD))
9573 : {
9574 24 : rtx breg1 = make_test_reg (bmode);
9575 24 : rtx breg2 = make_test_reg (bmode);
9576 24 : rtx ssubreg1 = lowpart_subreg (smode, breg1, bmode);
9577 24 : rtx ssubreg2 = lowpart_subreg (smode, breg2, bmode);
9578 24 : rtx not_1 = simplify_gen_unary (NOT, smode, ssubreg1, smode);
9579 24 : rtx and_12 = simplify_gen_binary (AND, smode, ssubreg1, ssubreg2);
9580 24 : rtx ior_12 = simplify_gen_binary (IOR, smode, ssubreg1, ssubreg2);
9581 24 : rtx xor_12 = simplify_gen_binary (XOR, smode, ssubreg1, ssubreg2);
9582 24 : rtx and_n12 = simplify_gen_binary (AND, smode, not_1, ssubreg2);
9583 24 : rtx ior_n12 = simplify_gen_binary (IOR, smode, not_1, ssubreg2);
9584 24 : rtx xor_12_c = simplify_gen_binary (XOR, smode, xor_12, const1_rtx);
9585 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, not_1, smode),
9586 : gen_rtx_NOT (bmode, breg1));
9587 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, and_12, smode),
9588 : gen_rtx_AND (bmode, breg1, breg2));
9589 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, ior_12, smode),
9590 : gen_rtx_IOR (bmode, breg1, breg2));
9591 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, xor_12, smode),
9592 : gen_rtx_XOR (bmode, breg1, breg2));
9593 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, and_n12, smode),
9594 : gen_rtx_AND (bmode, gen_rtx_NOT (bmode, breg1), breg2));
9595 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, ior_n12, smode),
9596 : gen_rtx_IOR (bmode, gen_rtx_NOT (bmode, breg1), breg2));
9597 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, xor_12_c, smode),
9598 : gen_rtx_XOR (bmode,
9599 : gen_rtx_XOR (bmode, breg1, breg2),
9600 : const1_rtx));
9601 : }
9602 24 : }
9603 :
9604 : /* Verify more simplifications of integer extension/truncation.
9605 : BMODE is wider than MMODE which is wider than SMODE. */
9606 :
9607 : static void
9608 16 : test_scalar_int_ext_ops2 (machine_mode bmode, machine_mode mmode,
9609 : machine_mode smode)
9610 : {
9611 16 : rtx breg = make_test_reg (bmode);
9612 16 : rtx mreg = make_test_reg (mmode);
9613 16 : rtx sreg = make_test_reg (smode);
9614 :
9615 : /* Check truncate of truncate. */
9616 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9617 : simplify_gen_unary (TRUNCATE, mmode,
9618 : breg, bmode),
9619 : mmode),
9620 : simplify_gen_unary (TRUNCATE, smode, breg, bmode));
9621 :
9622 : /* Check extension of extension. */
9623 16 : ASSERT_RTX_EQ (simplify_gen_unary (ZERO_EXTEND, bmode,
9624 : simplify_gen_unary (ZERO_EXTEND, mmode,
9625 : sreg, smode),
9626 : mmode),
9627 : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
9628 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
9629 : simplify_gen_unary (SIGN_EXTEND, mmode,
9630 : sreg, smode),
9631 : mmode),
9632 : simplify_gen_unary (SIGN_EXTEND, bmode, sreg, smode));
9633 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
9634 : simplify_gen_unary (ZERO_EXTEND, mmode,
9635 : sreg, smode),
9636 : mmode),
9637 : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
9638 :
9639 : /* Check truncation of extension. */
9640 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9641 : simplify_gen_unary (ZERO_EXTEND, bmode,
9642 : mreg, mmode),
9643 : bmode),
9644 : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
9645 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9646 : simplify_gen_unary (SIGN_EXTEND, bmode,
9647 : mreg, mmode),
9648 : bmode),
9649 : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
9650 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9651 : lowpart_subreg (bmode, mreg, mmode),
9652 : bmode),
9653 : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
9654 16 : }
9655 :
9656 : /* Test comparisons of comparisons, with the inner comparisons being
9657 : between values of mode MODE2 and producing results of mode MODE1,
9658 : and with the outer comparisons producing results of mode MODE0. */
9659 :
9660 : static void
9661 4 : test_comparisons (machine_mode mode0, machine_mode mode1, machine_mode mode2)
9662 : {
9663 4 : rtx reg0 = make_test_reg (mode2);
9664 4 : rtx reg1 = make_test_reg (mode2);
9665 :
9666 4 : static const rtx_code codes[] = {
9667 : EQ, NE, LT, LTU, LE, LEU, GE, GEU, GT, GTU
9668 : };
9669 4 : constexpr auto num_codes = ARRAY_SIZE (codes);
9670 4 : rtx cmps[num_codes];
9671 4 : rtx vals[] = { constm1_rtx, const0_rtx, const1_rtx };
9672 :
9673 44 : for (unsigned int i = 0; i < num_codes; ++i)
9674 40 : cmps[i] = gen_rtx_fmt_ee (codes[i], mode1, reg0, reg1);
9675 :
9676 44 : for (auto code : codes)
9677 440 : for (unsigned int i0 = 0; i0 < num_codes; ++i0)
9678 4400 : for (unsigned int i1 = 0; i1 < num_codes; ++i1)
9679 : {
9680 4000 : rtx cmp_res = simplify_relational_operation (code, mode0, mode1,
9681 : cmps[i0], cmps[i1]);
9682 4000 : if (i0 >= 2 && i1 >= 2 && (i0 ^ i1) & 1)
9683 1280 : ASSERT_TRUE (cmp_res == NULL_RTX);
9684 : else
9685 : {
9686 2720 : ASSERT_TRUE (cmp_res != NULL_RTX
9687 : && (CONSTANT_P (cmp_res)
9688 : || (COMPARISON_P (cmp_res)
9689 : && GET_MODE (cmp_res) == mode0
9690 : && REG_P (XEXP (cmp_res, 0))
9691 : && REG_P (XEXP (cmp_res, 1)))));
9692 10880 : for (rtx reg0_val : vals)
9693 32640 : for (rtx reg1_val : vals)
9694 : {
9695 24480 : rtx val0 = simplify_const_relational_operation
9696 24480 : (codes[i0], mode1, reg0_val, reg1_val);
9697 24480 : rtx val1 = simplify_const_relational_operation
9698 24480 : (codes[i1], mode1, reg0_val, reg1_val);
9699 24480 : rtx val = simplify_const_relational_operation
9700 24480 : (code, mode0, val0, val1);
9701 24480 : rtx folded = cmp_res;
9702 24480 : if (COMPARISON_P (cmp_res))
9703 16704 : folded = simplify_const_relational_operation
9704 16704 : (GET_CODE (cmp_res), mode0,
9705 16704 : XEXP (cmp_res, 0) == reg0 ? reg0_val : reg1_val,
9706 16704 : XEXP (cmp_res, 1) == reg0 ? reg0_val : reg1_val);
9707 24480 : ASSERT_RTX_EQ (val, folded);
9708 : }
9709 : }
9710 : }
9711 4 : }
9712 :
9713 : /* Return true if comparison CODE holds when its operands compare as
9714 : OUTCOME, one of CMP_BIT_LT, CMP_BIT_GT, CMP_BIT_EQ and CMP_BIT_UN. */
9715 :
9716 : static bool
9717 363536 : comparison_holds_p (rtx_code code, int outcome)
9718 : {
9719 363536 : int mask = unsigned_comparison_to_mask (code);
9720 363536 : if (mask == 0 || code == NE)
9721 331944 : mask = comparison_to_mask (code);
9722 363536 : return mask & outcome;
9723 : }
9724 :
9725 : /* Return true if X is a comparison that raises an invalid-operation
9726 : exception for a quiet NaN operand. */
9727 :
9728 : static bool
9729 6664 : comparison_signals_p (rtx x)
9730 : {
9731 6664 : return (COMPARISON_P (x)
9732 7724 : && comparison_mask_signals_p (comparison_to_mask (GET_CODE (x))));
9733 : }
9734 :
9735 : /* Check RES, a simplification of (CODE OP0 OP1) where OP0 and OP1 are
9736 : comparisons between REG0 and REG1, against the value of (CODE OP0 OP1)
9737 : for every possible outcome of comparing REG0 and REG1. CODE is IOR or
9738 : a comparison of the results of OP0 and OP1. NANS_P says whether REG0
9739 : and REG1 can be unordered, TRAPS_P whether the result must signal
9740 : exactly when OP0 or OP1 does. */
9741 :
9742 : static void
9743 49392 : check_logical_comparison (rtx res, rtx_code code, machine_mode mode,
9744 : rtx op0, rtx op1, rtx reg0, rtx reg1,
9745 : bool nans_p, bool traps_p)
9746 : {
9747 49392 : if (res == NULL_RTX)
9748 : return;
9749 :
9750 42480 : ASSERT_TRUE (CONSTANT_P (res)
9751 : || (COMPARISON_P (res)
9752 : && GET_MODE (res) == mode
9753 : && ((XEXP (res, 0) == reg0 && XEXP (res, 1) == reg1)
9754 : || (XEXP (res, 0) == reg1 && XEXP (res, 1) == reg0))));
9755 :
9756 : /* Generic folds of (CODE X X) are allowed to drop a trap. */
9757 42480 : if (traps_p && GET_CODE (op0) != GET_CODE (op1))
9758 2392 : ASSERT_EQ (comparison_signals_p (op0) || comparison_signals_p (op1),
9759 : comparison_signals_p (res));
9760 :
9761 42480 : static const int outcomes[] = {
9762 : CMP_BIT_LT, CMP_BIT_GT, CMP_BIT_EQ, CMP_BIT_UN
9763 : };
9764 212400 : for (int outcome : outcomes)
9765 : {
9766 169920 : if (outcome == CMP_BIT_UN && !nans_p)
9767 32928 : continue;
9768 :
9769 136992 : rtx val0 = (comparison_holds_p (GET_CODE (op0), outcome)
9770 136992 : ? const_true_rtx : const0_rtx);
9771 136992 : rtx val1 = (comparison_holds_p (GET_CODE (op1), outcome)
9772 136992 : ? const_true_rtx : const0_rtx);
9773 136992 : rtx expected;
9774 136992 : if (code == IOR)
9775 19936 : expected = simplify_const_binary_operation (code, mode, val0, val1);
9776 : else
9777 117056 : expected = simplify_const_relational_operation (code, mode,
9778 : val0, val1);
9779 :
9780 136992 : rtx folded = res;
9781 136992 : if (COMPARISON_P (res))
9782 : {
9783 : /* Swapping the operands exchanges < and >. */
9784 89552 : int swapped = outcome;
9785 89552 : if (XEXP (res, 0) == reg1 && outcome == CMP_BIT_LT)
9786 : swapped = CMP_BIT_GT;
9787 89552 : else if (XEXP (res, 0) == reg1 && outcome == CMP_BIT_GT)
9788 89552 : swapped = CMP_BIT_LT;
9789 179104 : folded = (comparison_holds_p (GET_CODE (res), swapped)
9790 89552 : ? const_true_rtx : const0_rtx);
9791 : }
9792 169920 : ASSERT_RTX_EQ (expected, folded);
9793 : }
9794 : }
9795 :
9796 : /* Simplify (CODE (CODE0 REG0 REG1) (CODE1 REG0 REG1)), with all results
9797 : in SImode. CODE is IOR or a comparison; the latter combines the inner
9798 : comparisons through AND, IOR or XOR, inverting the first one for all
9799 : but NE (for example, (ltu X Y) is (and (not X) Y)). */
9800 :
9801 : static rtx
9802 49468 : simplify_logical_comparison (rtx_code code, rtx_code code0, rtx_code code1,
9803 : rtx reg0, rtx reg1)
9804 : {
9805 49468 : rtx op0 = gen_rtx_fmt_ee (code0, SImode, reg0, reg1);
9806 49468 : rtx op1 = gen_rtx_fmt_ee (code1, SImode, reg0, reg1);
9807 49468 : if (code == IOR)
9808 7092 : return simplify_binary_operation (code, SImode, op0, op1);
9809 42376 : return simplify_relational_operation (code, SImode, SImode, op0, op1);
9810 : }
9811 :
9812 : /* Test the simplification of IOR and comparisons of two comparisons
9813 : between operands of mode OP_MODE against an evaluation of them, for
9814 : all pairs of comparison codes. */
9815 :
9816 : static void
9817 36 : test_logical_comparisons_in_mode (machine_mode op_mode)
9818 : {
9819 36 : rtx reg0 = make_test_reg (op_mode);
9820 36 : rtx reg1 = (GET_MODE_CLASS (op_mode) == MODE_CC
9821 36 : ? const0_rtx : make_test_reg (op_mode));
9822 :
9823 36 : static const rtx_code codes[] = {
9824 : EQ, NE, LT, LE, GT, GE, LTGT, ORDERED,
9825 : UNORDERED, UNEQ, UNLT, UNLE, UNGT, UNGE
9826 : };
9827 36 : static const rtx_code outer_codes[] = { IOR, EQ, NE, LTU, LEU, GTU, GEU };
9828 :
9829 36 : bool nans_p = (GET_MODE_CLASS (op_mode) == MODE_CC
9830 36 : || HONOR_NANS (op_mode));
9831 36 : bool traps_p = HONOR_NANS (op_mode) && flag_trapping_math;
9832 :
9833 540 : for (auto code0 : codes)
9834 7560 : for (auto code1 : codes)
9835 56448 : for (auto code : outer_codes)
9836 : {
9837 49392 : rtx res = simplify_logical_comparison (code, code0, code1,
9838 : reg0, reg1);
9839 49392 : check_logical_comparison (res, code, SImode,
9840 : gen_rtx_fmt_ee (code0, SImode, reg0, reg1),
9841 : gen_rtx_fmt_ee (code1, SImode, reg0, reg1),
9842 : reg0, reg1, nans_p, traps_p);
9843 : /* Only a constant result can be expressed for a CC mode, other
9844 : than by the generic (ior X X) -> X. */
9845 49392 : if (GET_MODE_CLASS (op_mode) == MODE_CC
9846 5488 : && code == IOR
9847 5488 : && code0 != code1)
9848 728 : ASSERT_FALSE (res && COMPARISON_P (res));
9849 : }
9850 36 : }
9851 :
9852 : /* Return the code of simplify_logical_comparison's result, or UNKNOWN
9853 : if there is none. A constant result has code CONST_INT. */
9854 :
9855 : static rtx_code
9856 76 : logical_comparison_code (rtx_code code, rtx_code code0, rtx_code code1,
9857 : rtx reg0, rtx reg1)
9858 : {
9859 0 : rtx res = simplify_logical_comparison (code, code0, code1, reg0, reg1);
9860 76 : return res ? GET_CODE (res) : UNKNOWN;
9861 : }
9862 :
9863 : /* Test combinations of CC-mode, integer and floating-point comparisons,
9864 : the latter under the various floating-point options. */
9865 :
9866 : static void
9867 4 : test_logical_comparisons ()
9868 : {
9869 4 : int saved_trapping_math = flag_trapping_math;
9870 4 : int saved_finite_math_only = flag_finite_math_only;
9871 :
9872 4 : test_logical_comparisons_in_mode (CCmode);
9873 :
9874 16 : for (int finite = 0; finite <= 1; ++finite)
9875 24 : for (int trapping = 0; trapping <= 1; ++trapping)
9876 : {
9877 16 : flag_finite_math_only = finite;
9878 16 : flag_trapping_math = trapping;
9879 16 : test_logical_comparisons_in_mode (SImode);
9880 16 : test_logical_comparisons_in_mode (DFmode);
9881 : }
9882 :
9883 4 : rtx a = make_test_reg (DFmode);
9884 4 : rtx b = make_test_reg (DFmode);
9885 4 : rtx cc = make_test_reg (CCmode);
9886 :
9887 : /* All outcomes are possible for a CC mode: x < 0 || x >= 0 is not
9888 : always true. */
9889 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, GE, cc, const0_rtx),
9890 : UNKNOWN);
9891 8 : ASSERT_EQ (logical_comparison_code (IOR, EQ, NE, cc, const0_rtx),
9892 : CONST_INT);
9893 :
9894 4 : if (MODE_HAS_NANS (DFmode))
9895 : {
9896 4 : flag_finite_math_only = 0;
9897 4 : flag_trapping_math = 1;
9898 : /* These would add a trap: ORDERED & NE -> LTGT, ORDERED & UNLT -> LT. */
9899 8 : ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, NE, a, b), UNKNOWN);
9900 8 : ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, UNLT, a, b),
9901 : UNKNOWN);
9902 : /* These would drop one: UNLT | GT -> NE, LT ^ LE -> EQ,
9903 : !UNGE & GT -> false. */
9904 8 : ASSERT_EQ (logical_comparison_code (IOR, UNLT, GT, a, b), UNKNOWN);
9905 8 : ASSERT_EQ (logical_comparison_code (NE, LT, LE, a, b), UNKNOWN);
9906 8 : ASSERT_EQ (logical_comparison_code (LTU, UNGE, GT, a, b), UNKNOWN);
9907 : /* These do not. */
9908 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, GT, a, b), LTGT);
9909 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, EQ, a, b), LE);
9910 8 : ASSERT_EQ (logical_comparison_code (IOR, UNORDERED, EQ, a, b), UNEQ);
9911 8 : ASSERT_EQ (logical_comparison_code (LTU, UNLT, UNLE, a, b), EQ);
9912 8 : ASSERT_EQ (logical_comparison_code (LTU, LT, GE, a, b), GE);
9913 :
9914 4 : flag_trapping_math = 0;
9915 8 : ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, NE, a, b), LTGT);
9916 8 : ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, UNLT, a, b), LT);
9917 8 : ASSERT_EQ (logical_comparison_code (IOR, UNLT, GT, a, b), NE);
9918 8 : ASSERT_EQ (logical_comparison_code (NE, LT, LE, a, b), EQ);
9919 8 : ASSERT_EQ (logical_comparison_code (LTU, UNGE, GT, a, b), CONST_INT);
9920 : }
9921 :
9922 4 : flag_finite_math_only = 1;
9923 4 : flag_trapping_math = 1;
9924 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, GT, a, b), NE);
9925 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, GE, a, b), CONST_INT);
9926 :
9927 4 : flag_trapping_math = saved_trapping_math;
9928 4 : flag_finite_math_only = saved_finite_math_only;
9929 4 : }
9930 :
9931 : /* Verify some simplifications involving scalar expressions. */
9932 :
9933 : static void
9934 4 : test_scalar_ops ()
9935 : {
9936 500 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
9937 : {
9938 496 : machine_mode mode = (machine_mode) i;
9939 496 : if (SCALAR_INT_MODE_P (mode) && mode != BImode)
9940 40 : test_scalar_int_ops (mode);
9941 : }
9942 :
9943 4 : test_scalar_int_ext_ops (HImode, QImode);
9944 4 : test_scalar_int_ext_ops (SImode, QImode);
9945 4 : test_scalar_int_ext_ops (SImode, HImode);
9946 4 : test_scalar_int_ext_ops (DImode, QImode);
9947 4 : test_scalar_int_ext_ops (DImode, HImode);
9948 4 : test_scalar_int_ext_ops (DImode, SImode);
9949 :
9950 4 : test_scalar_int_ext_ops2 (SImode, HImode, QImode);
9951 4 : test_scalar_int_ext_ops2 (DImode, HImode, QImode);
9952 4 : test_scalar_int_ext_ops2 (DImode, SImode, QImode);
9953 4 : test_scalar_int_ext_ops2 (DImode, SImode, HImode);
9954 :
9955 4 : test_comparisons (QImode, HImode, SImode);
9956 4 : test_logical_comparisons ();
9957 4 : }
9958 :
9959 : /* Test vector simplifications involving VEC_DUPLICATE in which the
9960 : operands and result have vector mode MODE. SCALAR_REG is a pseudo
9961 : register that holds one element of MODE. */
9962 :
9963 : static void
9964 224 : test_vector_ops_duplicate (machine_mode mode, rtx scalar_reg)
9965 : {
9966 224 : scalar_mode inner_mode = GET_MODE_INNER (mode);
9967 224 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
9968 448 : poly_uint64 nunits = GET_MODE_NUNITS (mode);
9969 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9970 : {
9971 : /* Test some simple unary cases with VEC_DUPLICATE arguments. */
9972 124 : rtx not_scalar_reg = gen_rtx_NOT (inner_mode, scalar_reg);
9973 124 : rtx duplicate_not = gen_rtx_VEC_DUPLICATE (mode, not_scalar_reg);
9974 124 : ASSERT_RTX_EQ (duplicate,
9975 : simplify_unary_operation (NOT, mode,
9976 : duplicate_not, mode));
9977 :
9978 124 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
9979 124 : rtx duplicate_neg = gen_rtx_VEC_DUPLICATE (mode, neg_scalar_reg);
9980 124 : ASSERT_RTX_EQ (duplicate,
9981 : simplify_unary_operation (NEG, mode,
9982 : duplicate_neg, mode));
9983 :
9984 : /* Test some simple binary cases with VEC_DUPLICATE arguments. */
9985 124 : ASSERT_RTX_EQ (duplicate,
9986 : simplify_binary_operation (PLUS, mode, duplicate,
9987 : CONST0_RTX (mode)));
9988 :
9989 124 : ASSERT_RTX_EQ (duplicate,
9990 : simplify_binary_operation (MINUS, mode, duplicate,
9991 : CONST0_RTX (mode)));
9992 :
9993 124 : ASSERT_RTX_PTR_EQ (CONST0_RTX (mode),
9994 : simplify_binary_operation (MINUS, mode, duplicate,
9995 : duplicate));
9996 : }
9997 :
9998 : /* Test a scalar VEC_SELECT of a VEC_DUPLICATE. */
9999 224 : rtx zero_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
10000 224 : ASSERT_RTX_PTR_EQ (scalar_reg,
10001 : simplify_binary_operation (VEC_SELECT, inner_mode,
10002 : duplicate, zero_par));
10003 :
10004 224 : unsigned HOST_WIDE_INT const_nunits;
10005 224 : if (nunits.is_constant (&const_nunits))
10006 : {
10007 : /* And again with the final element. */
10008 224 : rtx last_index = gen_int_mode (const_nunits - 1, word_mode);
10009 224 : rtx last_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, last_index));
10010 224 : ASSERT_RTX_PTR_EQ (scalar_reg,
10011 : simplify_binary_operation (VEC_SELECT, inner_mode,
10012 : duplicate, last_par));
10013 :
10014 : /* Test a scalar subreg of a VEC_MERGE of a VEC_DUPLICATE. */
10015 : /* Skip this test for vectors of booleans, because offset is in bytes,
10016 : while vec_merge indices are in elements (usually bits). */
10017 224 : if (GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL)
10018 : {
10019 224 : rtx vector_reg = make_test_reg (mode);
10020 3732 : for (unsigned HOST_WIDE_INT i = 0; i < const_nunits; i++)
10021 : {
10022 3288 : if (i >= HOST_BITS_PER_WIDE_INT)
10023 : break;
10024 3284 : rtx mask = GEN_INT ((HOST_WIDE_INT_1U << i) | (i + 1));
10025 3284 : rtx vm = gen_rtx_VEC_MERGE (mode, duplicate, vector_reg, mask);
10026 6568 : poly_uint64 offset = i * GET_MODE_SIZE (inner_mode);
10027 :
10028 3284 : ASSERT_RTX_EQ (scalar_reg,
10029 : simplify_gen_subreg (inner_mode, vm,
10030 : mode, offset));
10031 : }
10032 : }
10033 : }
10034 :
10035 : /* Test a scalar subreg of a VEC_DUPLICATE. */
10036 224 : poly_uint64 offset = subreg_lowpart_offset (inner_mode, mode);
10037 224 : ASSERT_RTX_EQ (scalar_reg,
10038 : simplify_gen_subreg (inner_mode, duplicate,
10039 : mode, offset));
10040 :
10041 224 : machine_mode narrower_mode;
10042 224 : if (maybe_ne (nunits, 2U)
10043 184 : && multiple_p (nunits, 2)
10044 396 : && mode_for_vector (inner_mode, 2).exists (&narrower_mode)
10045 396 : && VECTOR_MODE_P (narrower_mode))
10046 : {
10047 : /* Test VEC_DUPLICATE of a vector. */
10048 172 : rtx_vector_builder nbuilder (narrower_mode, 2, 1);
10049 172 : nbuilder.quick_push (const0_rtx);
10050 172 : nbuilder.quick_push (const1_rtx);
10051 172 : rtx_vector_builder builder (mode, 2, 1);
10052 172 : builder.quick_push (const0_rtx);
10053 172 : builder.quick_push (const1_rtx);
10054 172 : ASSERT_RTX_EQ (builder.build (),
10055 : simplify_unary_operation (VEC_DUPLICATE, mode,
10056 : nbuilder.build (),
10057 : narrower_mode));
10058 :
10059 : /* Test VEC_SELECT of a vector. */
10060 172 : rtx vec_par
10061 172 : = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, const1_rtx, const0_rtx));
10062 172 : rtx narrower_duplicate
10063 172 : = gen_rtx_VEC_DUPLICATE (narrower_mode, scalar_reg);
10064 172 : ASSERT_RTX_EQ (narrower_duplicate,
10065 : simplify_binary_operation (VEC_SELECT, narrower_mode,
10066 : duplicate, vec_par));
10067 :
10068 : /* Test a vector subreg of a VEC_DUPLICATE. */
10069 172 : poly_uint64 offset = subreg_lowpart_offset (narrower_mode, mode);
10070 172 : ASSERT_RTX_EQ (narrower_duplicate,
10071 : simplify_gen_subreg (narrower_mode, duplicate,
10072 : mode, offset));
10073 172 : }
10074 224 : }
10075 :
10076 : /* Test vector simplifications involving VEC_SERIES in which the
10077 : operands and result have vector mode MODE. SCALAR_REG is a pseudo
10078 : register that holds one element of MODE. */
10079 :
10080 : static void
10081 92 : test_vector_ops_series (machine_mode mode, rtx scalar_reg)
10082 : {
10083 : /* Test unary cases with VEC_SERIES arguments. */
10084 92 : scalar_mode inner_mode = GET_MODE_INNER (mode);
10085 92 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
10086 92 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
10087 92 : rtx series_0_r = gen_rtx_VEC_SERIES (mode, const0_rtx, scalar_reg);
10088 92 : rtx series_0_nr = gen_rtx_VEC_SERIES (mode, const0_rtx, neg_scalar_reg);
10089 92 : rtx series_nr_1 = gen_rtx_VEC_SERIES (mode, neg_scalar_reg, const1_rtx);
10090 92 : rtx series_r_m1 = gen_rtx_VEC_SERIES (mode, scalar_reg, constm1_rtx);
10091 92 : rtx series_r_r = gen_rtx_VEC_SERIES (mode, scalar_reg, scalar_reg);
10092 92 : rtx series_nr_nr = gen_rtx_VEC_SERIES (mode, neg_scalar_reg,
10093 : neg_scalar_reg);
10094 92 : ASSERT_RTX_EQ (series_0_r,
10095 : simplify_unary_operation (NEG, mode, series_0_nr, mode));
10096 92 : ASSERT_RTX_EQ (series_r_m1,
10097 : simplify_unary_operation (NEG, mode, series_nr_1, mode));
10098 92 : ASSERT_RTX_EQ (series_r_r,
10099 : simplify_unary_operation (NEG, mode, series_nr_nr, mode));
10100 :
10101 : /* Test that a VEC_SERIES with a zero step is simplified away. */
10102 92 : ASSERT_RTX_EQ (duplicate,
10103 : simplify_binary_operation (VEC_SERIES, mode,
10104 : scalar_reg, const0_rtx));
10105 :
10106 : /* Test PLUS and MINUS with VEC_SERIES. */
10107 92 : rtx series_0_1 = gen_const_vec_series (mode, const0_rtx, const1_rtx);
10108 92 : rtx series_0_m1 = gen_const_vec_series (mode, const0_rtx, constm1_rtx);
10109 92 : rtx series_r_1 = gen_rtx_VEC_SERIES (mode, scalar_reg, const1_rtx);
10110 92 : ASSERT_RTX_EQ (series_r_r,
10111 : simplify_binary_operation (PLUS, mode, series_0_r,
10112 : duplicate));
10113 92 : ASSERT_RTX_EQ (series_r_1,
10114 : simplify_binary_operation (PLUS, mode, duplicate,
10115 : series_0_1));
10116 92 : ASSERT_RTX_EQ (series_r_m1,
10117 : simplify_binary_operation (PLUS, mode, duplicate,
10118 : series_0_m1));
10119 92 : ASSERT_RTX_EQ (series_0_r,
10120 : simplify_binary_operation (MINUS, mode, series_r_r,
10121 : duplicate));
10122 92 : ASSERT_RTX_EQ (series_r_m1,
10123 : simplify_binary_operation (MINUS, mode, duplicate,
10124 : series_0_1));
10125 92 : ASSERT_RTX_EQ (series_r_1,
10126 : simplify_binary_operation (MINUS, mode, duplicate,
10127 : series_0_m1));
10128 92 : ASSERT_RTX_EQ (series_0_m1,
10129 : simplify_binary_operation (VEC_SERIES, mode, const0_rtx,
10130 : constm1_rtx));
10131 :
10132 : /* Test NEG on constant vector series. */
10133 92 : ASSERT_RTX_EQ (series_0_m1,
10134 : simplify_unary_operation (NEG, mode, series_0_1, mode));
10135 92 : ASSERT_RTX_EQ (series_0_1,
10136 : simplify_unary_operation (NEG, mode, series_0_m1, mode));
10137 :
10138 : /* Test PLUS and MINUS on constant vector series. */
10139 92 : rtx scalar2 = gen_int_mode (2, inner_mode);
10140 92 : rtx scalar3 = gen_int_mode (3, inner_mode);
10141 92 : rtx series_1_1 = gen_const_vec_series (mode, const1_rtx, const1_rtx);
10142 92 : rtx series_0_2 = gen_const_vec_series (mode, const0_rtx, scalar2);
10143 92 : rtx series_1_3 = gen_const_vec_series (mode, const1_rtx, scalar3);
10144 92 : ASSERT_RTX_EQ (series_1_1,
10145 : simplify_binary_operation (PLUS, mode, series_0_1,
10146 : CONST1_RTX (mode)));
10147 92 : ASSERT_RTX_EQ (series_0_m1,
10148 : simplify_binary_operation (PLUS, mode, CONST0_RTX (mode),
10149 : series_0_m1));
10150 92 : ASSERT_RTX_EQ (series_1_3,
10151 : simplify_binary_operation (PLUS, mode, series_1_1,
10152 : series_0_2));
10153 92 : ASSERT_RTX_EQ (series_0_1,
10154 : simplify_binary_operation (MINUS, mode, series_1_1,
10155 : CONST1_RTX (mode)));
10156 92 : ASSERT_RTX_EQ (series_1_1,
10157 : simplify_binary_operation (MINUS, mode, CONST1_RTX (mode),
10158 : series_0_m1));
10159 92 : ASSERT_RTX_EQ (series_1_1,
10160 : simplify_binary_operation (MINUS, mode, series_1_3,
10161 : series_0_2));
10162 :
10163 : /* Test MULT between constant vectors. */
10164 92 : rtx vec2 = gen_const_vec_duplicate (mode, scalar2);
10165 92 : rtx vec3 = gen_const_vec_duplicate (mode, scalar3);
10166 92 : rtx scalar9 = gen_int_mode (9, inner_mode);
10167 92 : rtx series_3_9 = gen_const_vec_series (mode, scalar3, scalar9);
10168 92 : ASSERT_RTX_EQ (series_0_2,
10169 : simplify_binary_operation (MULT, mode, series_0_1, vec2));
10170 92 : ASSERT_RTX_EQ (series_3_9,
10171 : simplify_binary_operation (MULT, mode, vec3, series_1_3));
10172 92 : if (!GET_MODE_NUNITS (mode).is_constant ())
10173 : ASSERT_FALSE (simplify_binary_operation (MULT, mode, series_0_1,
10174 : series_0_1));
10175 :
10176 : /* Test ASHIFT between constant vectors. */
10177 92 : ASSERT_RTX_EQ (series_0_2,
10178 : simplify_binary_operation (ASHIFT, mode, series_0_1,
10179 : CONST1_RTX (mode)));
10180 92 : if (!GET_MODE_NUNITS (mode).is_constant ())
10181 : ASSERT_FALSE (simplify_binary_operation (ASHIFT, mode, CONST1_RTX (mode),
10182 : series_0_1));
10183 92 : }
10184 :
10185 : static rtx
10186 3136 : simplify_merge_mask (rtx x, rtx mask, int op)
10187 : {
10188 0 : return simplify_context ().simplify_merge_mask (x, mask, op);
10189 : }
10190 :
10191 : /* Verify simplify_merge_mask works correctly. */
10192 :
10193 : static void
10194 224 : test_vec_merge (machine_mode mode)
10195 : {
10196 224 : rtx op0 = make_test_reg (mode);
10197 224 : rtx op1 = make_test_reg (mode);
10198 224 : rtx op2 = make_test_reg (mode);
10199 224 : rtx op3 = make_test_reg (mode);
10200 224 : rtx op4 = make_test_reg (mode);
10201 224 : rtx op5 = make_test_reg (mode);
10202 224 : rtx mask1 = make_test_reg (SImode);
10203 224 : rtx mask2 = make_test_reg (SImode);
10204 224 : rtx vm1 = gen_rtx_VEC_MERGE (mode, op0, op1, mask1);
10205 224 : rtx vm2 = gen_rtx_VEC_MERGE (mode, op2, op3, mask1);
10206 224 : rtx vm3 = gen_rtx_VEC_MERGE (mode, op4, op5, mask1);
10207 :
10208 : /* Simple vec_merge. */
10209 224 : ASSERT_EQ (op0, simplify_merge_mask (vm1, mask1, 0));
10210 224 : ASSERT_EQ (op1, simplify_merge_mask (vm1, mask1, 1));
10211 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 0));
10212 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 1));
10213 :
10214 : /* Nested vec_merge.
10215 : It's tempting to make this simplify right down to opN, but we don't
10216 : because all the simplify_* functions assume that the operands have
10217 : already been simplified. */
10218 224 : rtx nvm = gen_rtx_VEC_MERGE (mode, vm1, vm2, mask1);
10219 224 : ASSERT_EQ (vm1, simplify_merge_mask (nvm, mask1, 0));
10220 224 : ASSERT_EQ (vm2, simplify_merge_mask (nvm, mask1, 1));
10221 :
10222 : /* Intermediate unary op. */
10223 224 : rtx unop = gen_rtx_NOT (mode, vm1);
10224 224 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op0),
10225 : simplify_merge_mask (unop, mask1, 0));
10226 224 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op1),
10227 : simplify_merge_mask (unop, mask1, 1));
10228 :
10229 : /* Intermediate binary op. */
10230 224 : rtx binop = gen_rtx_PLUS (mode, vm1, vm2);
10231 224 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op0, op2),
10232 : simplify_merge_mask (binop, mask1, 0));
10233 224 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op1, op3),
10234 : simplify_merge_mask (binop, mask1, 1));
10235 :
10236 : /* Intermediate ternary op. */
10237 224 : rtx tenop = gen_rtx_FMA (mode, vm1, vm2, vm3);
10238 224 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op0, op2, op4),
10239 : simplify_merge_mask (tenop, mask1, 0));
10240 224 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op1, op3, op5),
10241 : simplify_merge_mask (tenop, mask1, 1));
10242 :
10243 : /* Side effects. */
10244 224 : rtx badop0 = gen_rtx_PRE_INC (mode, op0);
10245 224 : rtx badvm = gen_rtx_VEC_MERGE (mode, badop0, op1, mask1);
10246 224 : ASSERT_EQ (badop0, simplify_merge_mask (badvm, mask1, 0));
10247 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (badvm, mask1, 1));
10248 :
10249 : /* Called indirectly. */
10250 224 : ASSERT_RTX_EQ (gen_rtx_VEC_MERGE (mode, op0, op3, mask1),
10251 : simplify_rtx (nvm));
10252 224 : }
10253 :
10254 : /* Test that vector rotate formation works at RTL level. Try various
10255 : combinations of (REG << C) [|,^,+] (REG >> (<bitwidth> - C)). */
10256 :
10257 : static void
10258 92 : test_vector_rotate (rtx reg)
10259 : {
10260 92 : machine_mode mode = GET_MODE (reg);
10261 92 : unsigned bitwidth = GET_MODE_UNIT_SIZE (mode) * BITS_PER_UNIT;
10262 92 : rtx plus_rtx = gen_rtx_PLUS (mode, reg, reg);
10263 92 : rtx lshftrt_amnt = GEN_INT (bitwidth - 1);
10264 92 : lshftrt_amnt = gen_const_vec_duplicate (mode, lshftrt_amnt);
10265 92 : rtx lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
10266 92 : rtx rotate_rtx = gen_rtx_ROTATE (mode, reg, CONST1_RTX (mode));
10267 : /* Test explicitly the case where ASHIFT (x, 1) is a PLUS (x, x). */
10268 92 : ASSERT_RTX_EQ (rotate_rtx,
10269 : simplify_rtx (gen_rtx_IOR (mode, plus_rtx, lshiftrt_rtx)));
10270 92 : ASSERT_RTX_EQ (rotate_rtx,
10271 : simplify_rtx (gen_rtx_XOR (mode, plus_rtx, lshiftrt_rtx)));
10272 92 : ASSERT_RTX_EQ (rotate_rtx,
10273 : simplify_rtx (gen_rtx_PLUS (mode, plus_rtx, lshiftrt_rtx)));
10274 :
10275 : /* Don't go through every possible rotate amount to save execution time.
10276 : Multiple of BITS_PER_UNIT amounts could conceivably be simplified to
10277 : other bswap operations sometimes. Go through just the odd amounts. */
10278 1380 : for (unsigned i = 3; i < bitwidth - 2; i += 2)
10279 : {
10280 1288 : rtx rot_amnt = gen_const_vec_duplicate (mode, GEN_INT (i));
10281 1288 : rtx ashift_rtx = gen_rtx_ASHIFT (mode, reg, rot_amnt);
10282 1288 : lshftrt_amnt = gen_const_vec_duplicate (mode, GEN_INT (bitwidth - i));
10283 1288 : lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
10284 1288 : rotate_rtx = gen_rtx_ROTATE (mode, reg, rot_amnt);
10285 1288 : ASSERT_RTX_EQ (rotate_rtx,
10286 : simplify_rtx (gen_rtx_IOR (mode, ashift_rtx, lshiftrt_rtx)));
10287 1288 : ASSERT_RTX_EQ (rotate_rtx,
10288 : simplify_rtx (gen_rtx_XOR (mode, ashift_rtx, lshiftrt_rtx)));
10289 1288 : ASSERT_RTX_EQ (rotate_rtx,
10290 : simplify_rtx (gen_rtx_PLUS (mode, ashift_rtx, lshiftrt_rtx)));
10291 : }
10292 92 : }
10293 :
10294 : /* Test subregs of integer vector constant X, trying elements in
10295 : the range [ELT_BIAS, ELT_BIAS + constant_lower_bound (NELTS)),
10296 : where NELTS is the number of elements in X. Subregs involving
10297 : elements [ELT_BIAS, ELT_BIAS + FIRST_VALID) are expected to fail. */
10298 :
10299 : static void
10300 276 : test_vector_subregs_modes (rtx x, poly_uint64 elt_bias = 0,
10301 : unsigned int first_valid = 0)
10302 : {
10303 276 : machine_mode inner_mode = GET_MODE (x);
10304 276 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
10305 :
10306 34500 : for (unsigned int modei = 0; modei < NUM_MACHINE_MODES; ++modei)
10307 : {
10308 34224 : machine_mode outer_mode = (machine_mode) modei;
10309 34224 : if (!VECTOR_MODE_P (outer_mode))
10310 18768 : continue;
10311 :
10312 15456 : unsigned int outer_nunits;
10313 15456 : if (GET_MODE_INNER (outer_mode) == int_mode
10314 1932 : && GET_MODE_NUNITS (outer_mode).is_constant (&outer_nunits)
10315 20412 : && multiple_p (GET_MODE_NUNITS (inner_mode), outer_nunits))
10316 : {
10317 : /* Test subregs in which the outer mode is a smaller,
10318 : constant-sized vector of the same element type. */
10319 1092 : unsigned int limit
10320 1092 : = constant_lower_bound (GET_MODE_NUNITS (inner_mode));
10321 8028 : for (unsigned int elt = 0; elt < limit; elt += outer_nunits)
10322 : {
10323 6936 : rtx expected = NULL_RTX;
10324 6936 : if (elt >= first_valid)
10325 : {
10326 6936 : rtx_vector_builder builder (outer_mode, outer_nunits, 1);
10327 46704 : for (unsigned int i = 0; i < outer_nunits; ++i)
10328 32832 : builder.quick_push (CONST_VECTOR_ELT (x, elt + i));
10329 6936 : expected = builder.build ();
10330 6936 : }
10331 13872 : poly_uint64 byte = (elt_bias + elt) * GET_MODE_SIZE (int_mode);
10332 6936 : ASSERT_RTX_EQ (expected,
10333 : simplify_subreg (outer_mode, x,
10334 : inner_mode, byte));
10335 : }
10336 : }
10337 28728 : else if (known_eq (GET_MODE_SIZE (outer_mode),
10338 : GET_MODE_SIZE (inner_mode))
10339 2040 : && known_eq (elt_bias, 0U)
10340 2040 : && (GET_MODE_CLASS (outer_mode) != MODE_VECTOR_BOOL
10341 0 : || known_eq (GET_MODE_BITSIZE (outer_mode),
10342 : GET_MODE_NUNITS (outer_mode)))
10343 2040 : && (!FLOAT_MODE_P (outer_mode)
10344 15876 : || (FLOAT_MODE_FORMAT (outer_mode)->ieee_bits
10345 1104 : == GET_MODE_UNIT_PRECISION (outer_mode)))
10346 14364 : && (GET_MODE_SIZE (inner_mode).is_constant ()
10347 : || !CONST_VECTOR_STEPPED_P (x)))
10348 : {
10349 : /* Try converting to OUTER_MODE and back. */
10350 1800 : rtx outer_x = simplify_subreg (outer_mode, x, inner_mode, 0);
10351 1800 : ASSERT_TRUE (outer_x != NULL_RTX);
10352 1800 : ASSERT_RTX_EQ (x, simplify_subreg (inner_mode, outer_x,
10353 : outer_mode, 0));
10354 : }
10355 : }
10356 :
10357 276 : if (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
10358 : {
10359 : /* Test each byte in the element range. */
10360 276 : unsigned int limit
10361 276 : = constant_lower_bound (GET_MODE_SIZE (inner_mode));
10362 14604 : for (unsigned int i = 0; i < limit; ++i)
10363 : {
10364 14328 : unsigned int elt = i / GET_MODE_SIZE (int_mode);
10365 14328 : rtx expected = NULL_RTX;
10366 14328 : if (elt >= first_valid)
10367 : {
10368 14328 : unsigned int byte_shift = i % GET_MODE_SIZE (int_mode);
10369 14328 : if (BYTES_BIG_ENDIAN)
10370 : byte_shift = GET_MODE_SIZE (int_mode) - byte_shift - 1;
10371 14328 : rtx_mode_t vec_elt (CONST_VECTOR_ELT (x, elt), int_mode);
10372 14328 : wide_int shifted_elt
10373 14328 : = wi::lrshift (vec_elt, byte_shift * BITS_PER_UNIT);
10374 14328 : expected = immed_wide_int_const (shifted_elt, QImode);
10375 14328 : }
10376 28656 : poly_uint64 byte = elt_bias * GET_MODE_SIZE (int_mode) + i;
10377 14328 : ASSERT_RTX_EQ (expected,
10378 : simplify_subreg (QImode, x, inner_mode, byte));
10379 : }
10380 : }
10381 276 : }
10382 :
10383 : /* Test constant subregs of integer vector mode INNER_MODE, using 1
10384 : element per pattern. */
10385 :
10386 : static void
10387 92 : test_vector_subregs_repeating (machine_mode inner_mode)
10388 : {
10389 184 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
10390 92 : unsigned int min_nunits = constant_lower_bound (nunits);
10391 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
10392 92 : unsigned int count = gcd (min_nunits, 8);
10393 :
10394 92 : rtx_vector_builder builder (inner_mode, count, 1);
10395 776 : for (unsigned int i = 0; i < count; ++i)
10396 592 : builder.quick_push (gen_int_mode (8 - i, int_mode));
10397 92 : rtx x = builder.build ();
10398 :
10399 92 : test_vector_subregs_modes (x);
10400 92 : if (!nunits.is_constant ())
10401 : test_vector_subregs_modes (x, nunits - min_nunits);
10402 92 : }
10403 :
10404 : /* Test constant subregs of integer vector mode INNER_MODE, using 2
10405 : elements per pattern. */
10406 :
10407 : static void
10408 92 : test_vector_subregs_fore_back (machine_mode inner_mode)
10409 : {
10410 184 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
10411 92 : unsigned int min_nunits = constant_lower_bound (nunits);
10412 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
10413 92 : unsigned int count = gcd (min_nunits, 4);
10414 :
10415 92 : rtx_vector_builder builder (inner_mode, count, 2);
10416 536 : for (unsigned int i = 0; i < count; ++i)
10417 352 : builder.quick_push (gen_int_mode (i, int_mode));
10418 444 : for (unsigned int i = 0; i < count; ++i)
10419 352 : builder.quick_push (gen_int_mode (-1 - (int) i, int_mode));
10420 92 : rtx x = builder.build ();
10421 :
10422 92 : test_vector_subregs_modes (x);
10423 92 : if (!nunits.is_constant ())
10424 : test_vector_subregs_modes (x, nunits - min_nunits, count);
10425 92 : }
10426 :
10427 : /* Test constant subregs of integer vector mode INNER_MODE, using 3
10428 : elements per pattern. */
10429 :
10430 : static void
10431 92 : test_vector_subregs_stepped (machine_mode inner_mode)
10432 : {
10433 : /* Build { 0, 1, 2, 3, ... }. */
10434 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
10435 92 : rtx_vector_builder builder (inner_mode, 1, 3);
10436 460 : for (unsigned int i = 0; i < 3; ++i)
10437 276 : builder.quick_push (gen_int_mode (i, int_mode));
10438 92 : rtx x = builder.build ();
10439 :
10440 92 : test_vector_subregs_modes (x);
10441 92 : }
10442 :
10443 : /* Test constant subregs of integer vector mode INNER_MODE. */
10444 :
10445 : static void
10446 92 : test_vector_subregs (machine_mode inner_mode)
10447 : {
10448 92 : test_vector_subregs_repeating (inner_mode);
10449 92 : test_vector_subregs_fore_back (inner_mode);
10450 92 : test_vector_subregs_stepped (inner_mode);
10451 92 : }
10452 :
10453 : /* A subreg of a vector comparison is simplified to a comparison in the
10454 : subreg's mode. Check that the comparison is still done in the mode of
10455 : its operands, not of its integer result: with floating-point operands
10456 : that may be NaNs, x unord x and x ord x must not fold to constants. */
10457 :
10458 : static void
10459 100 : test_vector_subreg_of_fp_comparison (machine_mode mode)
10460 : {
10461 100 : machine_mode int_mode;
10462 100 : if (!HONOR_NANS (mode)
10463 100 : || !related_int_vector_mode (mode).exists (&int_mode))
10464 100 : return;
10465 :
10466 36 : rtx reg = make_test_reg (mode);
10467 36 : rtx unord = gen_rtx_UNORDERED (int_mode, reg, reg);
10468 36 : rtx ord = gen_rtx_ORDERED (int_mode, reg, reg);
10469 36 : rtx x = simplify_subreg (mode, unord, int_mode, 0);
10470 36 : ASSERT_TRUE (x == NULL_RTX || !CONSTANT_P (x));
10471 36 : x = simplify_subreg (mode, ord, int_mode, 0);
10472 36 : ASSERT_TRUE (x == NULL_RTX || !CONSTANT_P (x));
10473 : }
10474 :
10475 : /* Verify some simplifications involving vectors. */
10476 :
10477 : static void
10478 4 : test_vector_ops ()
10479 : {
10480 500 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
10481 : {
10482 496 : machine_mode mode = (machine_mode) i;
10483 496 : if (VECTOR_MODE_P (mode))
10484 : {
10485 448 : rtx scalar_reg = make_test_reg (GET_MODE_INNER (mode));
10486 224 : test_vector_ops_duplicate (mode, scalar_reg);
10487 224 : rtx vector_reg = make_test_reg (mode);
10488 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
10489 348 : && maybe_gt (GET_MODE_NUNITS (mode), 2))
10490 : {
10491 92 : test_vector_ops_series (mode, scalar_reg);
10492 92 : test_vector_subregs (mode);
10493 92 : test_vector_rotate (vector_reg);
10494 : }
10495 224 : test_vec_merge (mode);
10496 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
10497 100 : test_vector_subreg_of_fp_comparison (mode);
10498 : }
10499 : }
10500 4 : }
10501 :
10502 : template<unsigned int N>
10503 : struct simplify_const_poly_int_tests
10504 : {
10505 : static void run ();
10506 : };
10507 :
10508 : template<>
10509 : struct simplify_const_poly_int_tests<1>
10510 : {
10511 : static void run () {}
10512 : };
10513 :
10514 : /* Test various CONST_POLY_INT properties. */
10515 :
10516 : template<unsigned int N>
10517 : void
10518 : simplify_const_poly_int_tests<N>::run ()
10519 : {
10520 : using poly_int64 = poly_int<N, HOST_WIDE_INT>;
10521 : rtx x1 = gen_int_mode (poly_int64 (1, 1), QImode);
10522 : rtx x2 = gen_int_mode (poly_int64 (-80, 127), QImode);
10523 : rtx x3 = gen_int_mode (poly_int64 (-79, -128), QImode);
10524 : rtx x4 = gen_int_mode (poly_int64 (5, 4), QImode);
10525 : rtx x5 = gen_int_mode (poly_int64 (30, 24), QImode);
10526 : rtx x6 = gen_int_mode (poly_int64 (20, 16), QImode);
10527 : rtx x7 = gen_int_mode (poly_int64 (7, 4), QImode);
10528 : rtx x8 = gen_int_mode (poly_int64 (30, 24), HImode);
10529 : rtx x9 = gen_int_mode (poly_int64 (-30, -24), HImode);
10530 : rtx x10 = gen_int_mode (poly_int64 (-31, -24), HImode);
10531 : rtx two = GEN_INT (2);
10532 : rtx six = GEN_INT (6);
10533 : poly_uint64 offset = subreg_lowpart_offset (QImode, HImode);
10534 :
10535 : /* These tests only try limited operation combinations. Fuller arithmetic
10536 : testing is done directly on poly_ints. */
10537 : ASSERT_EQ (simplify_unary_operation (NEG, HImode, x8, HImode), x9);
10538 : ASSERT_EQ (simplify_unary_operation (NOT, HImode, x8, HImode), x10);
10539 : ASSERT_EQ (simplify_unary_operation (TRUNCATE, QImode, x8, HImode), x5);
10540 : ASSERT_EQ (simplify_binary_operation (PLUS, QImode, x1, x2), x3);
10541 : ASSERT_EQ (simplify_binary_operation (MINUS, QImode, x3, x1), x2);
10542 : ASSERT_EQ (simplify_binary_operation (MULT, QImode, x4, six), x5);
10543 : ASSERT_EQ (simplify_binary_operation (MULT, QImode, six, x4), x5);
10544 : ASSERT_EQ (simplify_binary_operation (ASHIFT, QImode, x4, two), x6);
10545 : ASSERT_EQ (simplify_binary_operation (IOR, QImode, x4, two), x7);
10546 : ASSERT_EQ (simplify_subreg (HImode, x5, QImode, 0), x8);
10547 : ASSERT_EQ (simplify_subreg (QImode, x8, HImode, offset), x5);
10548 : }
10549 :
10550 : /* Run all of the selftests within this file. */
10551 :
10552 : void
10553 4 : simplify_rtx_cc_tests ()
10554 : {
10555 4 : test_scalar_ops ();
10556 4 : test_vector_ops ();
10557 4 : simplify_const_poly_int_tests<NUM_POLY_INT_COEFFS>::run ();
10558 4 : }
10559 :
10560 : } // namespace selftest
10561 :
10562 : #endif /* CHECKING_P */
|