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 9261942 : neg_poly_int_rtx (machine_mode mode, const_rtx i)
56 : {
57 9261942 : 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 6362928 : mode_signbit_p (machine_mode mode, const_rtx x)
65 : {
66 6362928 : unsigned HOST_WIDE_INT val;
67 6362928 : unsigned int width;
68 6362928 : scalar_int_mode int_mode;
69 :
70 6362928 : if (!is_int_mode (mode, &int_mode))
71 : return false;
72 :
73 6362920 : width = GET_MODE_PRECISION (int_mode);
74 6362920 : if (width == 0)
75 : return false;
76 :
77 6362920 : if (width <= HOST_BITS_PER_WIDE_INT
78 6361332 : && CONST_INT_P (x))
79 6215311 : val = INTVAL (x);
80 : #if TARGET_SUPPORTS_WIDE_INT
81 147609 : else if (CONST_WIDE_INT_P (x))
82 : {
83 501 : unsigned int i;
84 501 : unsigned int elts = CONST_WIDE_INT_NUNITS (x);
85 501 : if (elts != (width + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
86 : return false;
87 934 : for (i = 0; i < elts - 1; i++)
88 501 : if (CONST_WIDE_INT_ELT (x, i) != 0)
89 : return false;
90 433 : val = CONST_WIDE_INT_ELT (x, elts - 1);
91 433 : width %= HOST_BITS_PER_WIDE_INT;
92 433 : 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 6215311 : if (width < HOST_BITS_PER_WIDE_INT)
109 5622178 : val &= (HOST_WIDE_INT_1U << width) - 1;
110 6215744 : 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 4067636 : val_signbit_p (machine_mode mode, unsigned HOST_WIDE_INT val)
119 : {
120 4067636 : unsigned int width;
121 4067636 : scalar_int_mode int_mode;
122 :
123 4067636 : if (!is_int_mode (mode, &int_mode))
124 : return false;
125 :
126 4067600 : width = GET_MODE_PRECISION (int_mode);
127 4067600 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
128 : return false;
129 :
130 3989332 : val &= GET_MODE_MASK (int_mode);
131 3989332 : 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 2731697 : val_signbit_known_set_p (machine_mode mode, unsigned HOST_WIDE_INT val)
138 : {
139 2731697 : unsigned int width;
140 :
141 2731697 : scalar_int_mode int_mode;
142 2731697 : if (!is_int_mode (mode, &int_mode))
143 : return false;
144 :
145 2668186 : width = GET_MODE_PRECISION (int_mode);
146 2668186 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
147 : return false;
148 :
149 2668186 : val &= HOST_WIDE_INT_1U << (width - 1);
150 2668186 : 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 8090748 : val_signbit_known_clear_p (machine_mode mode, unsigned HOST_WIDE_INT val)
157 : {
158 8090748 : unsigned int width;
159 :
160 8090748 : scalar_int_mode int_mode;
161 8090748 : if (!is_int_mode (mode, &int_mode))
162 : return false;
163 :
164 7725087 : width = GET_MODE_PRECISION (int_mode);
165 7725087 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
166 : return false;
167 :
168 7614502 : val &= HOST_WIDE_INT_1U << (width - 1);
169 7614502 : 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 125009078 : simplify_context::simplify_gen_binary (rtx_code code, machine_mode mode,
177 : rtx op0, rtx op1)
178 : {
179 125009078 : rtx tem;
180 :
181 : /* If this simplifies, do it. */
182 125009078 : tem = simplify_binary_operation (code, mode, op0, op1);
183 125009078 : if (tem)
184 : return tem;
185 :
186 : /* Put complex operands first and constants second if commutative. */
187 80035490 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
188 80035490 : && swap_commutative_operands_p (op0, op1))
189 : std::swap (op0, op1);
190 :
191 80035490 : 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 2828831808 : avoid_constant_pool_reference (rtx x)
198 : {
199 2828831808 : rtx c, tmp, addr;
200 2828831808 : machine_mode cmode;
201 2828831808 : poly_int64 offset = 0;
202 :
203 2828831808 : switch (GET_CODE (x))
204 : {
205 269387148 : case MEM:
206 269387148 : break;
207 :
208 918327 : case FLOAT_EXTEND:
209 : /* Handle float extensions of constant pool references. */
210 918327 : tmp = XEXP (x, 0);
211 918327 : c = avoid_constant_pool_reference (tmp);
212 918327 : if (c != tmp && CONST_DOUBLE_AS_FLOAT_P (c))
213 125542 : return const_double_from_real_value (*CONST_DOUBLE_REAL_VALUE (c),
214 125542 : GET_MODE (x));
215 : return x;
216 :
217 : default:
218 : return x;
219 : }
220 :
221 269387148 : if (GET_MODE (x) == BLKmode)
222 : return x;
223 :
224 265287095 : addr = XEXP (x, 0);
225 :
226 : /* Call target hook to avoid the effects of -fpic etc.... */
227 265287095 : addr = targetm.delegitimize_address (addr);
228 :
229 : /* Split the address into a base and integer offset. */
230 265287095 : addr = strip_offset (addr, &offset);
231 :
232 265287095 : 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 265287095 : if (GET_CODE (addr) == SYMBOL_REF
238 265287095 : && CONSTANT_POOL_ADDRESS_P (addr))
239 : {
240 5519554 : c = get_pool_constant (addr);
241 5519554 : 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 5519554 : if (known_eq (offset, 0) && cmode == GET_MODE (x))
247 : return c;
248 45633 : else if (known_in_range_p (offset, 0, GET_MODE_SIZE (cmode)))
249 : {
250 15211 : rtx tem = simplify_subreg (GET_MODE (x), c, cmode, offset);
251 15211 : if (tem && CONSTANT_P (tem))
252 15094 : 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 3584830761 : 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 3584830761 : if (MEM_P (x)
269 62710795 : && MEM_EXPR (x)
270 3623671065 : && MEM_OFFSET_KNOWN_P (x))
271 : {
272 35841672 : tree decl = MEM_EXPR (x);
273 35841672 : machine_mode mode = GET_MODE (x);
274 35841672 : poly_int64 offset = 0;
275 :
276 35841672 : switch (TREE_CODE (decl))
277 : {
278 : default:
279 : decl = NULL;
280 : break;
281 :
282 : case VAR_DECL:
283 : break;
284 :
285 10104858 : case ARRAY_REF:
286 10104858 : case ARRAY_RANGE_REF:
287 10104858 : case COMPONENT_REF:
288 10104858 : case BIT_FIELD_REF:
289 10104858 : case REALPART_EXPR:
290 10104858 : case IMAGPART_EXPR:
291 10104858 : case VIEW_CONVERT_EXPR:
292 10104858 : {
293 10104858 : poly_int64 bitsize, bitpos, bytepos, toffset_val = 0;
294 10104858 : tree toffset;
295 10104858 : int unsignedp, reversep, volatilep = 0;
296 :
297 10104858 : decl
298 10104858 : = get_inner_reference (decl, &bitsize, &bitpos, &toffset, &mode,
299 : &unsignedp, &reversep, &volatilep);
300 20209716 : if (maybe_ne (bitsize, GET_MODE_BITSIZE (mode))
301 10387228 : || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
302 19769398 : || (toffset && !poly_int_tree_p (toffset, &toffset_val)))
303 : decl = NULL;
304 : else
305 9382170 : offset += bytepos + toffset_val;
306 10104858 : break;
307 : }
308 : }
309 :
310 722688 : if (decl
311 20967274 : && mode == GET_MODE (x)
312 20691927 : && VAR_P (decl)
313 13494064 : && (TREE_STATIC (decl)
314 12254319 : || DECL_THREAD_LOCAL_P (decl))
315 1277493 : && DECL_RTL_SET_P (decl)
316 10659201 : && MEM_P (DECL_RTL (decl)))
317 : {
318 1277031 : rtx newx;
319 :
320 1277031 : offset += MEM_OFFSET (x);
321 :
322 1277031 : newx = DECL_RTL (decl);
323 :
324 1277031 : if (MEM_P (newx))
325 : {
326 1277031 : rtx n = XEXP (newx, 0), o = XEXP (x, 0);
327 1277031 : 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 1277031 : n = strip_offset (n, &n_offset);
336 1277031 : o = strip_offset (o, &o_offset);
337 2525959 : if (!(known_eq (o_offset, n_offset + offset)
338 1248928 : && rtx_equal_p (o, n)))
339 214437 : 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 3584830761 : 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 7404176 : simplify_context::simplify_gen_unary (rtx_code code, machine_mode mode, rtx op,
355 : machine_mode op_mode)
356 : {
357 7404176 : rtx tem;
358 :
359 : /* If this simplifies, use it. */
360 7404176 : if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
361 : return tem;
362 :
363 2539408 : return gen_rtx_fmt_e (code, mode, op);
364 : }
365 :
366 : /* Likewise for ternary operations. */
367 :
368 : rtx
369 2973183 : 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 2973183 : rtx tem;
374 :
375 : /* If this simplifies, use it. */
376 2973183 : if ((tem = simplify_ternary_operation (code, mode, op0_mode,
377 : op0, op1, op2)) != 0)
378 : return tem;
379 :
380 2720965 : 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 23022055 : simplify_context::simplify_gen_relational (rtx_code code, machine_mode mode,
388 : machine_mode cmp_mode,
389 : rtx op0, rtx op1)
390 : {
391 23022055 : rtx tem;
392 :
393 23022055 : if ((tem = simplify_relational_operation (code, mode, cmp_mode,
394 : op0, op1)) != 0)
395 : return tem;
396 :
397 20275737 : 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 503534187 : simplify_replace_fn_rtx (rtx x, const_rtx old_rtx,
407 : rtx (*fn) (rtx, const_rtx, void *), void *data)
408 : {
409 503534187 : enum rtx_code code = GET_CODE (x);
410 503534187 : machine_mode mode = GET_MODE (x);
411 503534187 : machine_mode op_mode;
412 503534187 : const char *fmt;
413 503534187 : rtx op0, op1, op2, newx, op;
414 503534187 : rtvec vec, newvec;
415 503534187 : int i, j;
416 :
417 503534187 : if (UNLIKELY (fn != NULL))
418 : {
419 437806040 : newx = fn (x, old_rtx, data);
420 437806040 : if (newx)
421 : return newx;
422 : }
423 65728147 : else if (rtx_equal_p (x, old_rtx))
424 5739165 : return copy_rtx ((rtx) data);
425 :
426 396712985 : switch (GET_RTX_CLASS (code))
427 : {
428 2086337 : case RTX_UNARY:
429 2086337 : op0 = XEXP (x, 0);
430 2086337 : op_mode = GET_MODE (op0);
431 2086337 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
432 2086337 : if (op0 == XEXP (x, 0))
433 : return x;
434 662112 : return simplify_gen_unary (code, mode, op0, op_mode);
435 :
436 76306839 : case RTX_BIN_ARITH:
437 76306839 : case RTX_COMM_ARITH:
438 76306839 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
439 76306839 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
440 76306839 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
441 : return x;
442 23469719 : return simplify_gen_binary (code, mode, op0, op1);
443 :
444 10169873 : case RTX_COMPARE:
445 10169873 : case RTX_COMM_COMPARE:
446 10169873 : op0 = XEXP (x, 0);
447 10169873 : op1 = XEXP (x, 1);
448 10169873 : op_mode = GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
449 10169873 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
450 10169873 : op1 = simplify_replace_fn_rtx (op1, old_rtx, fn, data);
451 10169873 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
452 : return x;
453 2803704 : return simplify_gen_relational (code, mode, op_mode, op0, op1);
454 :
455 6371952 : case RTX_TERNARY:
456 6371952 : case RTX_BITFIELD_OPS:
457 6371952 : op0 = XEXP (x, 0);
458 6371952 : op_mode = GET_MODE (op0);
459 6371952 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
460 6371952 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
461 6371952 : op2 = simplify_replace_fn_rtx (XEXP (x, 2), old_rtx, fn, data);
462 6371952 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1) && op2 == XEXP (x, 2))
463 : return x;
464 2198936 : if (op_mode == VOIDmode)
465 2175463 : op_mode = GET_MODE (op0);
466 2198936 : return simplify_gen_ternary (code, mode, op_mode, op0, op1, op2);
467 :
468 88522666 : case RTX_EXTRA:
469 88522666 : if (code == SUBREG)
470 : {
471 626607 : op0 = simplify_replace_fn_rtx (SUBREG_REG (x), old_rtx, fn, data);
472 626607 : if (op0 == SUBREG_REG (x))
473 : return x;
474 131626 : op0 = simplify_gen_subreg (GET_MODE (x), op0,
475 65813 : GET_MODE (SUBREG_REG (x)),
476 65813 : SUBREG_BYTE (x));
477 65813 : return op0 ? op0 : x;
478 : }
479 : break;
480 :
481 63187978 : case RTX_OBJ:
482 63187978 : if (code == MEM)
483 : {
484 10763857 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
485 10763857 : if (op0 == XEXP (x, 0))
486 : return x;
487 161584 : return replace_equiv_address_nv (x, op0);
488 : }
489 52424121 : 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 290387520 : newx = x;
515 290387520 : fmt = GET_RTX_FORMAT (code);
516 627149810 : for (i = 0; fmt[i]; i++)
517 336762290 : switch (fmt[i])
518 : {
519 3326590 : case 'E':
520 3326590 : vec = XVEC (x, i);
521 3326590 : newvec = XVEC (newx, i);
522 13917610 : for (j = 0; j < GET_NUM_ELEM (vec); j++)
523 : {
524 10591020 : op = simplify_replace_fn_rtx (RTVEC_ELT (vec, j),
525 : old_rtx, fn, data);
526 10591020 : if (op != RTVEC_ELT (vec, j))
527 : {
528 359759 : if (newvec == vec)
529 : {
530 345212 : newvec = shallow_copy_rtvec (vec);
531 345212 : if (x == newx)
532 345212 : newx = shallow_copy_rtx (x);
533 345212 : XVEC (newx, i) = newvec;
534 : }
535 359759 : RTVEC_ELT (newvec, j) = op;
536 : }
537 : }
538 : break;
539 :
540 80152626 : case 'e':
541 80152626 : if (XEXP (x, i))
542 : {
543 80152576 : op = simplify_replace_fn_rtx (XEXP (x, i), old_rtx, fn, data);
544 80152576 : if (op != XEXP (x, i))
545 : {
546 4491484 : if (x == newx)
547 4488257 : newx = shallow_copy_rtx (x);
548 4491484 : 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 13369271 : simplify_replace_rtx (rtx x, const_rtx old_rtx, rtx new_rtx)
561 : {
562 13369271 : 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 24311039 : simplify_context::simplify_truncation (machine_mode mode, rtx op,
614 : machine_mode op_mode)
615 : {
616 24311039 : unsigned int precision = GET_MODE_UNIT_PRECISION (mode);
617 24311039 : unsigned int op_precision = GET_MODE_UNIT_PRECISION (op_mode);
618 24311039 : scalar_int_mode int_mode, int_op_mode, subreg_mode;
619 :
620 24311039 : gcc_assert (precision <= op_precision);
621 :
622 : /* Optimize truncations of zero and sign extended values. */
623 24311039 : if (GET_CODE (op) == ZERO_EXTEND
624 24311039 : || 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 387008 : machine_mode origmode = GET_MODE (XEXP (op, 0));
633 387008 : if (mode == origmode)
634 : return XEXP (op, 0);
635 28424 : else if (precision <= GET_MODE_UNIT_PRECISION (origmode))
636 11248 : return simplify_gen_unary (TRUNCATE, mode,
637 11248 : XEXP (op, 0), origmode);
638 : else
639 2964 : return simplify_gen_unary (GET_CODE (op), mode,
640 2964 : 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 23924031 : if (1
647 : && (!WORD_REGISTER_OPERATIONS || precision >= BITS_PER_WORD)
648 : && (GET_CODE (op) == PLUS
649 : || GET_CODE (op) == MINUS
650 23924031 : || GET_CODE (op) == MULT))
651 : {
652 1443791 : rtx op0 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0), op_mode);
653 1443791 : if (op0)
654 : {
655 1443791 : rtx op1 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 1), op_mode);
656 1443791 : if (op1)
657 1443791 : 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 22480240 : if ((GET_CODE (op) == LSHIFTRT
665 22480240 : || 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 1962765 : && 2 * precision <= op_precision
671 1962765 : && CONST_INT_P (XEXP (op, 1))
672 1860279 : && 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 22480216 : if ((GET_CODE (op) == LSHIFTRT
682 : || GET_CODE (op) == ASHIFTRT)
683 1962741 : && CONST_INT_P (XEXP (op, 1))
684 1860255 : && GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
685 948 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
686 948 : && UINTVAL (XEXP (op, 1)) < precision)
687 931 : return simplify_gen_binary (LSHIFTRT, mode,
688 931 : 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 22479285 : if (GET_CODE (op) == ASHIFT
694 850453 : && CONST_INT_P (XEXP (op, 1))
695 789970 : && (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
696 789970 : || GET_CODE (XEXP (op, 0)) == SIGN_EXTEND)
697 663 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
698 653 : && UINTVAL (XEXP (op, 1)) < precision)
699 653 : return simplify_gen_binary (ASHIFT, mode,
700 653 : 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 22478632 : if (GET_CODE (op) == AND
706 884899 : && (GET_CODE (XEXP (op, 0)) == LSHIFTRT
707 884899 : || GET_CODE (XEXP (op, 0)) == ASHIFTRT)
708 47345 : && CONST_INT_P (XEXP (XEXP (op, 0), 1))
709 47180 : && CONST_INT_P (XEXP (op, 1)))
710 : {
711 47123 : rtx op0 = (XEXP (XEXP (op, 0), 0));
712 47123 : rtx shift_op = XEXP (XEXP (op, 0), 1);
713 47123 : rtx mask_op = XEXP (op, 1);
714 47123 : unsigned HOST_WIDE_INT shift = UINTVAL (shift_op);
715 47123 : unsigned HOST_WIDE_INT mask
716 47123 : = UINTVAL (mask_op) & GET_MODE_MASK (op_mode);
717 :
718 47123 : if (shift < precision
719 22503562 : && HWI_COMPUTABLE_MODE_P (mode)
720 : /* Every bit selected by the mask must be produced by the narrowed
721 : shift, not by bits of X above the truncated mode. */
722 31876 : && (mask & ~(GET_MODE_MASK (mode) >> shift)) == 0
723 6946 : && (op0 = simplify_gen_unary (TRUNCATE, mode, op0, op_mode))
724 54069 : && (op0 = simplify_gen_binary (LSHIFTRT, mode, op0, shift_op)))
725 : {
726 6946 : mask_op = GEN_INT (trunc_int_for_mode (mask, mode));
727 6946 : return simplify_gen_binary (AND, mode, op0, mask_op);
728 : }
729 : }
730 :
731 : /* Turn (truncate:M1 (*_extract:M2 (reg:M3) (len) (pos))) into
732 : (*_extract:M1 (truncate:M1 (reg:M3)) (len) (pos')) if possible. */
733 22471686 : if ((GET_CODE (op) == ZERO_EXTRACT || GET_CODE (op) == SIGN_EXTRACT)
734 1169172 : && precision <= GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (op, 0)))
735 582904 : && CONST_INT_P (XEXP (op, 1))
736 23054590 : && CONST_INT_P (XEXP (op, 2)))
737 : {
738 546462 : rtx op0 = XEXP (op, 0);
739 546462 : unsigned HOST_WIDE_INT len = UINTVAL (XEXP (op, 1));
740 546462 : unsigned HOST_WIDE_INT pos = UINTVAL (XEXP (op, 2));
741 546462 : if (BITS_BIG_ENDIAN && pos >= op_precision - precision)
742 : {
743 : if (GET_MODE (op0) != mode)
744 : op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
745 : if (op0)
746 : {
747 : pos -= op_precision - precision;
748 : return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
749 : XEXP (op, 1), GEN_INT (pos));
750 : }
751 : }
752 546462 : else if (!BITS_BIG_ENDIAN && precision >= len + pos)
753 : {
754 184320 : if (GET_MODE (op0) != mode)
755 139849 : op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
756 139849 : if (op0)
757 184320 : return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
758 184320 : XEXP (op, 1), XEXP (op, 2));
759 : }
760 : }
761 :
762 : /* Recognize a word extraction from a multi-word subreg. */
763 22287366 : if ((GET_CODE (op) == LSHIFTRT
764 22287366 : || GET_CODE (op) == ASHIFTRT)
765 1961810 : && SCALAR_INT_MODE_P (mode)
766 1958593 : && SCALAR_INT_MODE_P (op_mode)
767 2096850 : && precision >= BITS_PER_WORD
768 61826 : && 2 * precision <= op_precision
769 61826 : && CONST_INT_P (XEXP (op, 1))
770 52322 : && (INTVAL (XEXP (op, 1)) & (precision - 1)) == 0
771 1805 : && UINTVAL (XEXP (op, 1)) < op_precision)
772 : {
773 1805 : poly_int64 byte = subreg_lowpart_offset (mode, op_mode);
774 1805 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
775 1805 : return simplify_gen_subreg (mode, XEXP (op, 0), op_mode,
776 : (WORDS_BIG_ENDIAN
777 1805 : ? byte - shifted_bytes
778 1805 : : byte + shifted_bytes));
779 : }
780 :
781 : /* If we have a TRUNCATE of a right shift of MEM, make a new MEM
782 : and try replacing the TRUNCATE and shift with it. Don't do this
783 : if the MEM has a mode-dependent address. */
784 22285561 : if ((GET_CODE (op) == LSHIFTRT
785 : || GET_CODE (op) == ASHIFTRT)
786 1956788 : && is_a <scalar_int_mode> (mode, &int_mode)
787 22284989 : && is_a <scalar_int_mode> (op_mode, &int_op_mode)
788 1956788 : && MEM_P (XEXP (op, 0))
789 11245 : && CONST_INT_P (XEXP (op, 1))
790 20630 : && INTVAL (XEXP (op, 1)) % GET_MODE_BITSIZE (int_mode) == 0
791 615 : && INTVAL (XEXP (op, 1)) > 0
792 1230 : && INTVAL (XEXP (op, 1)) < GET_MODE_BITSIZE (int_op_mode)
793 615 : && ! mode_dependent_address_p (XEXP (XEXP (op, 0), 0),
794 615 : MEM_ADDR_SPACE (XEXP (op, 0)))
795 615 : && ! MEM_VOLATILE_P (XEXP (op, 0))
796 22285561 : && (GET_MODE_SIZE (int_mode) >= UNITS_PER_WORD
797 : || WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN))
798 : {
799 572 : poly_int64 byte = subreg_lowpart_offset (int_mode, int_op_mode);
800 572 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
801 572 : return adjust_address_nv (XEXP (op, 0), int_mode,
802 : (WORDS_BIG_ENDIAN
803 : ? byte - shifted_bytes
804 : : byte + shifted_bytes));
805 : }
806 :
807 : /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
808 : (OP:SI foo:SI) if OP is NEG or ABS. */
809 22284989 : if ((GET_CODE (op) == ABS
810 22284989 : || GET_CODE (op) == NEG)
811 18343 : && (GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
812 18343 : || GET_CODE (XEXP (op, 0)) == ZERO_EXTEND)
813 17 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
814 1 : return simplify_gen_unary (GET_CODE (op), mode,
815 1 : XEXP (XEXP (op, 0), 0), mode);
816 :
817 : /* Simplifications of (truncate:A (subreg:B X 0)). */
818 22284988 : if (GET_CODE (op) == SUBREG
819 22285544 : && is_a <scalar_int_mode> (mode, &int_mode)
820 121133 : && SCALAR_INT_MODE_P (op_mode)
821 121133 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &subreg_mode)
822 22405621 : && subreg_lowpart_p (op))
823 : {
824 : /* (truncate:A (subreg:B (truncate:C X) 0)) is (truncate:A X). */
825 120613 : if (GET_CODE (SUBREG_REG (op)) == TRUNCATE)
826 : {
827 0 : rtx inner = XEXP (SUBREG_REG (op), 0);
828 0 : if (GET_MODE_PRECISION (int_mode)
829 0 : <= GET_MODE_PRECISION (subreg_mode))
830 0 : return simplify_gen_unary (TRUNCATE, int_mode, inner,
831 0 : GET_MODE (inner));
832 : else
833 : /* If subreg above is paradoxical and C is narrower
834 : than A, return (subreg:A (truncate:C X) 0). */
835 0 : return simplify_gen_subreg (int_mode, SUBREG_REG (op),
836 : subreg_mode, 0);
837 : }
838 :
839 : /* Simplifications of (truncate:A (subreg:B X:C 0)) with
840 : paradoxical subregs (B is wider than C). */
841 120613 : if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
842 : {
843 120613 : unsigned int int_op_prec = GET_MODE_PRECISION (int_op_mode);
844 120613 : unsigned int subreg_prec = GET_MODE_PRECISION (subreg_mode);
845 120613 : if (int_op_prec > subreg_prec)
846 : {
847 90349 : if (int_mode == subreg_mode)
848 : return SUBREG_REG (op);
849 63 : if (GET_MODE_PRECISION (int_mode) < subreg_prec)
850 27 : return simplify_gen_unary (TRUNCATE, int_mode,
851 27 : SUBREG_REG (op), subreg_mode);
852 : }
853 : /* Simplification of (truncate:A (subreg:B X:C 0)) where
854 : A is narrower than B and B is narrower than C. */
855 30264 : else if (int_op_prec < subreg_prec
856 30264 : && GET_MODE_PRECISION (int_mode) < int_op_prec)
857 30264 : return simplify_gen_unary (TRUNCATE, int_mode,
858 30264 : SUBREG_REG (op), subreg_mode);
859 : }
860 : }
861 :
862 : /* (truncate:A (truncate:B X)) is (truncate:A X). */
863 22164411 : if (GET_CODE (op) == TRUNCATE)
864 0 : return simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0),
865 0 : GET_MODE (XEXP (op, 0)));
866 :
867 : /* (truncate:A (ior X C)) is (const_int -1) if C is equal to that already,
868 : in mode A. */
869 22164411 : if (GET_CODE (op) == IOR
870 36846 : && SCALAR_INT_MODE_P (mode)
871 36846 : && SCALAR_INT_MODE_P (op_mode)
872 36846 : && CONST_INT_P (XEXP (op, 1))
873 22172892 : && trunc_int_for_mode (INTVAL (XEXP (op, 1)), mode) == -1)
874 42 : return constm1_rtx;
875 :
876 : return NULL_RTX;
877 : }
878 :
879 : /* Try to simplify a unary operation CODE whose output mode is to be
880 : MODE with input operand OP whose mode was originally OP_MODE.
881 : Return zero if no simplification can be made. */
882 : rtx
883 30642460 : simplify_context::simplify_unary_operation (rtx_code code, machine_mode mode,
884 : rtx op, machine_mode op_mode)
885 : {
886 30642460 : rtx trueop, tem;
887 :
888 30642460 : trueop = avoid_constant_pool_reference (op);
889 :
890 30642460 : tem = simplify_const_unary_operation (code, mode, trueop, op_mode);
891 30642460 : if (tem)
892 : return tem;
893 :
894 25109507 : return simplify_unary_operation_1 (code, mode, op);
895 : }
896 :
897 : /* Return true if FLOAT or UNSIGNED_FLOAT operation OP is known
898 : to be exact. */
899 :
900 : static bool
901 2795 : exact_int_to_float_conversion_p (const_rtx op)
902 : {
903 2795 : machine_mode op0_mode = GET_MODE (XEXP (op, 0));
904 : /* Constants can reach here with -frounding-math, if they do then
905 : the conversion isn't exact. */
906 2795 : if (op0_mode == VOIDmode)
907 : return false;
908 5588 : int out_bits = significand_size (GET_MODE_INNER (GET_MODE (op)));
909 2794 : int in_prec = GET_MODE_UNIT_PRECISION (op0_mode);
910 2794 : int in_bits = in_prec;
911 2794 : if (HWI_COMPUTABLE_MODE_P (op0_mode))
912 : {
913 2704 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (XEXP (op, 0), op0_mode);
914 2704 : if (GET_CODE (op) == FLOAT)
915 2583 : in_bits -= num_sign_bit_copies (XEXP (op, 0), op0_mode);
916 121 : else if (GET_CODE (op) == UNSIGNED_FLOAT)
917 121 : in_bits = wi::min_precision (wi::uhwi (nonzero, in_prec), UNSIGNED);
918 : else
919 0 : gcc_unreachable ();
920 2704 : in_bits -= wi::ctz (wi::uhwi (nonzero, in_prec));
921 : }
922 2794 : return in_bits <= out_bits;
923 : }
924 :
925 : /* Perform some simplifications we can do even if the operands
926 : aren't constant. */
927 : rtx
928 25109507 : simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode,
929 : rtx op)
930 : {
931 25109507 : enum rtx_code reversed;
932 25109507 : rtx temp, elt, base, step;
933 25109507 : scalar_int_mode inner, int_mode, op_mode, op0_mode;
934 :
935 25109507 : switch (code)
936 : {
937 2415205 : case NOT:
938 : /* (not (not X)) == X. */
939 2415205 : if (GET_CODE (op) == NOT)
940 3163 : return XEXP (op, 0);
941 :
942 : /* (not (eq X Y)) == (ne X Y), etc. if BImode or the result of the
943 : comparison is all ones. */
944 2412042 : if (COMPARISON_P (op)
945 17057 : && ((SCALAR_INT_MODE_P (mode) && STORE_FLAG_VALUE == -1)
946 : #ifdef VECTOR_STORE_FLAG_VALUE
947 17057 : || (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
948 : && VECTOR_STORE_FLAG_VALUE (mode) == constm1_rtx)
949 : #endif
950 11504 : || mode == BImode)
951 2417595 : && ((reversed = reversed_comparison_code (op, NULL)) != UNKNOWN))
952 5388 : return simplify_gen_relational (reversed, mode, VOIDmode,
953 5388 : XEXP (op, 0), XEXP (op, 1));
954 :
955 : /* (not (neg (eq X Y))) is (neg (ne X Y)), etc. if the result of
956 : the comparison is one. */
957 2406654 : if (GET_CODE (op) == NEG
958 77306 : && COMPARISON_P (XEXP (op, 0))
959 6 : && ((SCALAR_INT_MODE_P (mode) && STORE_FLAG_VALUE == 1)
960 : #ifdef VECTOR_STORE_FLAG_VALUE
961 0 : || (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
962 0 : && VECTOR_STORE_FLAG_VALUE (mode) == const1_rtx)
963 : #endif
964 : )
965 2406660 : && ((reversed = reversed_comparison_code (XEXP (op, 0), NULL))
966 : != UNKNOWN))
967 : {
968 12 : temp = simplify_gen_relational (reversed, mode, VOIDmode,
969 : XEXP (XEXP (op, 0), 0),
970 6 : XEXP (XEXP (op, 0), 1));
971 6 : return simplify_gen_unary (NEG, mode, temp, mode);
972 : }
973 :
974 : /* (not (plus X -1)) can become (neg X). */
975 2406648 : if (GET_CODE (op) == PLUS
976 280452 : && XEXP (op, 1) == constm1_rtx)
977 4641 : return simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
978 :
979 : /* Similarly, (not (neg X)) is (plus X -1). Only do this for
980 : modes that have CONSTM1_RTX, i.e. MODE_INT, MODE_PARTIAL_INT
981 : and MODE_VECTOR_INT. */
982 2402007 : if (GET_CODE (op) == NEG && CONSTM1_RTX (mode))
983 77297 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
984 77297 : CONSTM1_RTX (mode));
985 :
986 : /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
987 2324710 : if (GET_CODE (op) == XOR
988 18715 : && CONST_INT_P (XEXP (op, 1))
989 2329247 : && (temp = simplify_unary_operation (NOT, mode,
990 : XEXP (op, 1), mode)) != 0)
991 4537 : return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
992 :
993 : /* (not (plus X C)) for signbit C is (xor X D) with D = ~C. */
994 2320173 : if (GET_CODE (op) == PLUS
995 275811 : && CONST_INT_P (XEXP (op, 1))
996 160052 : && mode_signbit_p (mode, XEXP (op, 1))
997 2324072 : && (temp = simplify_unary_operation (NOT, mode,
998 : XEXP (op, 1), mode)) != 0)
999 3899 : return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
1000 :
1001 :
1002 : /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for
1003 : operands other than 1, but that is not valid. We could do a
1004 : similar simplification for (not (lshiftrt C X)) where C is
1005 : just the sign bit, but this doesn't seem common enough to
1006 : bother with. */
1007 2316274 : if (GET_CODE (op) == ASHIFT
1008 31827 : && XEXP (op, 0) == const1_rtx)
1009 : {
1010 1123 : temp = simplify_gen_unary (NOT, mode, const1_rtx, mode);
1011 1123 : return simplify_gen_binary (ROTATE, mode, temp, XEXP (op, 1));
1012 : }
1013 :
1014 : /* (not (ashiftrt foo C)) where C is the number of bits in FOO
1015 : minus 1 is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1,
1016 : so we can perform the above simplification. */
1017 2315151 : if (STORE_FLAG_VALUE == -1
1018 : && is_a <scalar_int_mode> (mode, &int_mode)
1019 : && GET_CODE (op) == ASHIFTRT
1020 : && CONST_INT_P (XEXP (op, 1))
1021 : && INTVAL (XEXP (op, 1)) == GET_MODE_PRECISION (int_mode) - 1)
1022 : return simplify_gen_relational (GE, int_mode, VOIDmode,
1023 : XEXP (op, 0), const0_rtx);
1024 :
1025 :
1026 2315151 : if (partial_subreg_p (op)
1027 607342 : && subreg_lowpart_p (op)
1028 607030 : && GET_CODE (SUBREG_REG (op)) == ASHIFT
1029 624928 : && XEXP (SUBREG_REG (op), 0) == const1_rtx)
1030 : {
1031 147 : machine_mode inner_mode = GET_MODE (SUBREG_REG (op));
1032 147 : rtx x;
1033 :
1034 147 : x = gen_rtx_ROTATE (inner_mode,
1035 : simplify_gen_unary (NOT, inner_mode, const1_rtx,
1036 : inner_mode),
1037 : XEXP (SUBREG_REG (op), 1));
1038 147 : temp = rtl_hooks.gen_lowpart_no_emit (mode, x);
1039 147 : if (temp)
1040 : return temp;
1041 : }
1042 :
1043 : /* Apply De Morgan's laws to reduce number of patterns for machines
1044 : with negating logical insns (and-not, nand, etc.). If result has
1045 : only one NOT, put it first, since that is how the patterns are
1046 : coded. */
1047 2315004 : if (GET_CODE (op) == IOR || GET_CODE (op) == AND)
1048 : {
1049 15767 : rtx in1 = XEXP (op, 0), in2 = XEXP (op, 1);
1050 15767 : machine_mode op_mode;
1051 :
1052 15767 : op_mode = GET_MODE (in1);
1053 15767 : in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
1054 :
1055 15767 : op_mode = GET_MODE (in2);
1056 15767 : if (op_mode == VOIDmode)
1057 6911 : op_mode = mode;
1058 15767 : in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
1059 :
1060 15767 : if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
1061 : std::swap (in1, in2);
1062 :
1063 31534 : return gen_rtx_fmt_ee (GET_CODE (op) == IOR ? AND : IOR,
1064 : mode, in1, in2);
1065 : }
1066 :
1067 : /* (not (bswap x)) -> (bswap (not x)). */
1068 2299237 : if (GET_CODE (op) == BSWAP || GET_CODE (op) == BITREVERSE)
1069 : {
1070 0 : rtx x = simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1071 0 : return simplify_gen_unary (GET_CODE (op), mode, x, mode);
1072 : }
1073 : break;
1074 :
1075 1878092 : case NEG:
1076 : /* (neg (neg X)) == X. */
1077 1878092 : if (GET_CODE (op) == NEG)
1078 6908 : return XEXP (op, 0);
1079 :
1080 : /* (neg (x ? (neg y) : y)) == !x ? (neg y) : y.
1081 : If comparison is not reversible use
1082 : x ? y : (neg y). */
1083 1871184 : if (GET_CODE (op) == IF_THEN_ELSE)
1084 : {
1085 3537 : rtx cond = XEXP (op, 0);
1086 3537 : rtx true_rtx = XEXP (op, 1);
1087 3537 : rtx false_rtx = XEXP (op, 2);
1088 :
1089 3537 : if ((GET_CODE (true_rtx) == NEG
1090 0 : && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
1091 3537 : || (GET_CODE (false_rtx) == NEG
1092 0 : && rtx_equal_p (XEXP (false_rtx, 0), true_rtx)))
1093 : {
1094 0 : if (reversed_comparison_code (cond, NULL) != UNKNOWN)
1095 0 : temp = reversed_comparison (cond, mode);
1096 : else
1097 : {
1098 : temp = cond;
1099 : std::swap (true_rtx, false_rtx);
1100 : }
1101 0 : return simplify_gen_ternary (IF_THEN_ELSE, mode,
1102 0 : mode, temp, true_rtx, false_rtx);
1103 : }
1104 : }
1105 :
1106 : /* (neg (plus X 1)) can become (not X). */
1107 1871184 : if (GET_CODE (op) == PLUS
1108 144812 : && XEXP (op, 1) == const1_rtx)
1109 58118 : return simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1110 :
1111 : /* Similarly, (neg (not X)) is (plus X 1). */
1112 1813066 : if (GET_CODE (op) == NOT)
1113 320 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
1114 320 : CONST1_RTX (mode));
1115 :
1116 : /* (neg (minus X Y)) can become (minus Y X). This transformation
1117 : isn't safe for modes with signed zeros, since if X and Y are
1118 : both +0, (minus Y X) is the same as (minus X Y). If the
1119 : rounding mode is towards +infinity (or -infinity) then the two
1120 : expressions will be rounded differently. */
1121 1812746 : if (GET_CODE (op) == MINUS
1122 25966 : && !HONOR_SIGNED_ZEROS (mode)
1123 1837299 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1124 24553 : return simplify_gen_binary (MINUS, mode, XEXP (op, 1), XEXP (op, 0));
1125 :
1126 1788193 : if (GET_CODE (op) == PLUS
1127 86694 : && !HONOR_SIGNED_ZEROS (mode)
1128 1874369 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1129 : {
1130 : /* (neg (plus A C)) is simplified to (minus -C A). */
1131 86176 : if (CONST_SCALAR_INT_P (XEXP (op, 1))
1132 5172 : || CONST_DOUBLE_AS_FLOAT_P (XEXP (op, 1)))
1133 : {
1134 81004 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 1), mode);
1135 81004 : if (temp)
1136 81004 : return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 0));
1137 : }
1138 :
1139 : /* (neg (plus A B)) is canonicalized to (minus (neg A) B). */
1140 5172 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
1141 5172 : return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 1));
1142 : }
1143 :
1144 : /* (neg (mult A B)) becomes (mult A (neg B)).
1145 : This works even for floating-point values. */
1146 1702017 : if (GET_CODE (op) == MULT
1147 1702017 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1148 : {
1149 27911 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 1), mode);
1150 27911 : return simplify_gen_binary (MULT, mode, XEXP (op, 0), temp);
1151 : }
1152 :
1153 : /* NEG commutes with ASHIFT since it is multiplication. Only do
1154 : this if we can then eliminate the NEG (e.g., if the operand
1155 : is a constant). */
1156 1674106 : if (GET_CODE (op) == ASHIFT)
1157 : {
1158 53415 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 0), mode);
1159 53415 : if (temp)
1160 11315 : return simplify_gen_binary (ASHIFT, mode, temp, XEXP (op, 1));
1161 : }
1162 :
1163 : /* (neg (ashiftrt X C)) can be replaced by (lshiftrt X C) when
1164 : C is equal to the width of MODE minus 1. */
1165 1662791 : if (GET_CODE (op) == ASHIFTRT
1166 33119 : && CONST_INT_P (XEXP (op, 1))
1167 1728921 : && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1168 919 : return simplify_gen_binary (LSHIFTRT, mode,
1169 919 : XEXP (op, 0), XEXP (op, 1));
1170 :
1171 : /* (neg (lshiftrt X C)) can be replaced by (ashiftrt X C) when
1172 : C is equal to the width of MODE minus 1. */
1173 1661872 : if (GET_CODE (op) == LSHIFTRT
1174 9242 : && CONST_INT_P (XEXP (op, 1))
1175 1680212 : && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1176 3599 : return simplify_gen_binary (ASHIFTRT, mode,
1177 3599 : XEXP (op, 0), XEXP (op, 1));
1178 :
1179 : /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
1180 1658273 : if (GET_CODE (op) == XOR
1181 12015 : && XEXP (op, 1) == const1_rtx
1182 1658360 : && nonzero_bits (XEXP (op, 0), mode) == 1)
1183 34 : return plus_constant (mode, XEXP (op, 0), -1);
1184 :
1185 : /* (neg (lt x 0)) is (ashiftrt X C) if STORE_FLAG_VALUE is 1. */
1186 : /* (neg (lt x 0)) is (lshiftrt X C) if STORE_FLAG_VALUE is -1. */
1187 1658239 : if (GET_CODE (op) == LT
1188 3404 : && XEXP (op, 1) == const0_rtx
1189 1660795 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op, 0)), &inner))
1190 : {
1191 554 : int_mode = as_a <scalar_int_mode> (mode);
1192 554 : int isize = GET_MODE_PRECISION (inner);
1193 554 : if (STORE_FLAG_VALUE == 1)
1194 : {
1195 554 : temp = simplify_gen_binary (ASHIFTRT, inner, XEXP (op, 0),
1196 : gen_int_shift_amount (inner,
1197 554 : isize - 1));
1198 554 : if (int_mode == inner)
1199 : return temp;
1200 326 : if (GET_MODE_PRECISION (int_mode) > isize)
1201 195 : return simplify_gen_unary (SIGN_EXTEND, int_mode, temp, inner);
1202 131 : return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1203 : }
1204 : else if (STORE_FLAG_VALUE == -1)
1205 : {
1206 : temp = simplify_gen_binary (LSHIFTRT, inner, XEXP (op, 0),
1207 : gen_int_shift_amount (inner,
1208 : isize - 1));
1209 : if (int_mode == inner)
1210 : return temp;
1211 : if (GET_MODE_PRECISION (int_mode) > isize)
1212 : return simplify_gen_unary (ZERO_EXTEND, int_mode, temp, inner);
1213 : return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1214 : }
1215 : }
1216 :
1217 1657685 : if (vec_series_p (op, &base, &step))
1218 : {
1219 : /* Only create a new series if we can simplify both parts. In other
1220 : cases this isn't really a simplification, and it's not necessarily
1221 : a win to replace a vector operation with a scalar operation. */
1222 276 : scalar_mode inner_mode = GET_MODE_INNER (mode);
1223 276 : base = simplify_unary_operation (NEG, inner_mode, base, inner_mode);
1224 276 : if (base)
1225 : {
1226 276 : step = simplify_unary_operation (NEG, inner_mode,
1227 : step, inner_mode);
1228 276 : if (step)
1229 276 : return gen_vec_series (mode, base, step);
1230 : }
1231 : }
1232 : break;
1233 :
1234 2308725 : case TRUNCATE:
1235 : /* Don't optimize (lshiftrt (mult ...)) as it would interfere
1236 : with the umulXi3_highpart patterns. */
1237 2308725 : if (GET_CODE (op) == LSHIFTRT
1238 20053 : && GET_CODE (XEXP (op, 0)) == MULT)
1239 : break;
1240 :
1241 2301719 : if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
1242 : {
1243 12 : if (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op)))
1244 : {
1245 12 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1246 12 : if (temp)
1247 : return temp;
1248 : }
1249 : /* We can't handle truncation to a partial integer mode here
1250 : because we don't know the real bitsize of the partial
1251 : integer mode. */
1252 : break;
1253 : }
1254 :
1255 2301707 : if (GET_MODE (op) != VOIDmode)
1256 : {
1257 2301707 : temp = simplify_truncation (mode, op, GET_MODE (op));
1258 2301707 : if (temp)
1259 : return temp;
1260 : }
1261 :
1262 : /* If we know that the value is already truncated, we can
1263 : replace the TRUNCATE with a SUBREG. */
1264 1998173 : if (known_eq (GET_MODE_NUNITS (mode), 1)
1265 1998173 : && (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op))
1266 0 : || truncated_to_mode (mode, op)))
1267 : {
1268 1985520 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1269 1985520 : if (temp)
1270 : return temp;
1271 : }
1272 :
1273 : /* A truncate of a comparison can be replaced with a subreg if
1274 : STORE_FLAG_VALUE permits. This is like the previous test,
1275 : but it works even if the comparison is done in a mode larger
1276 : than HOST_BITS_PER_WIDE_INT. */
1277 12817 : if (HWI_COMPUTABLE_MODE_P (mode)
1278 164 : && COMPARISON_P (op)
1279 0 : && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
1280 12817 : && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op)))
1281 : {
1282 0 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1283 0 : if (temp)
1284 : return temp;
1285 : }
1286 :
1287 : /* A truncate of a memory is just loading the low part of the memory
1288 : if we are not changing the meaning of the address. */
1289 12817 : if (GET_CODE (op) == MEM
1290 276 : && !VECTOR_MODE_P (mode)
1291 160 : && !MEM_VOLATILE_P (op)
1292 12971 : && !mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op)))
1293 : {
1294 154 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1295 154 : if (temp)
1296 : return temp;
1297 : }
1298 :
1299 : /* Check for useless truncation. */
1300 12817 : if (GET_MODE (op) == mode)
1301 : return op;
1302 : break;
1303 :
1304 174576 : case FLOAT_TRUNCATE:
1305 : /* Check for useless truncation. */
1306 174576 : if (GET_MODE (op) == mode)
1307 : return op;
1308 :
1309 174576 : if (DECIMAL_FLOAT_MODE_P (mode))
1310 : break;
1311 :
1312 : /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
1313 174422 : if (GET_CODE (op) == FLOAT_EXTEND
1314 3 : && GET_MODE (XEXP (op, 0)) == mode)
1315 : return XEXP (op, 0);
1316 :
1317 : /* (float_truncate:SF (float_truncate:DF foo:XF))
1318 : = (float_truncate:SF foo:XF).
1319 : This may eliminate double rounding, so it is unsafe.
1320 :
1321 : (float_truncate:SF (float_extend:XF foo:DF))
1322 : = (float_truncate:SF foo:DF).
1323 :
1324 : (float_truncate:DF (float_extend:XF foo:SF))
1325 : = (float_extend:DF foo:SF). */
1326 174422 : if ((GET_CODE (op) == FLOAT_TRUNCATE
1327 145 : && flag_unsafe_math_optimizations)
1328 174418 : || GET_CODE (op) == FLOAT_EXTEND)
1329 14 : return simplify_gen_unary (GET_MODE_UNIT_SIZE (GET_MODE (XEXP (op, 0)))
1330 7 : > GET_MODE_UNIT_SIZE (mode)
1331 : ? FLOAT_TRUNCATE : FLOAT_EXTEND,
1332 : mode,
1333 14 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1334 :
1335 : /* (float_truncate (float x)) is (float x) */
1336 174415 : if ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1337 174415 : && (flag_unsafe_math_optimizations
1338 1414 : || exact_int_to_float_conversion_p (op)))
1339 1413 : return simplify_gen_unary (GET_CODE (op), mode,
1340 : XEXP (op, 0),
1341 1413 : GET_MODE (XEXP (op, 0)));
1342 :
1343 : /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
1344 : (OP:SF foo:SF) if OP is NEG or ABS. */
1345 173002 : if ((GET_CODE (op) == ABS
1346 173002 : || GET_CODE (op) == NEG)
1347 228 : && GET_CODE (XEXP (op, 0)) == FLOAT_EXTEND
1348 18 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
1349 18 : return simplify_gen_unary (GET_CODE (op), mode,
1350 18 : XEXP (XEXP (op, 0), 0), mode);
1351 :
1352 : /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
1353 : is (float_truncate:SF x). */
1354 172984 : if (GET_CODE (op) == SUBREG
1355 307 : && subreg_lowpart_p (op)
1356 173288 : && GET_CODE (SUBREG_REG (op)) == FLOAT_TRUNCATE)
1357 : return SUBREG_REG (op);
1358 : break;
1359 :
1360 602426 : case FLOAT_EXTEND:
1361 : /* Check for useless extension. */
1362 602426 : if (GET_MODE (op) == mode)
1363 : return op;
1364 :
1365 602426 : if (DECIMAL_FLOAT_MODE_P (mode))
1366 : break;
1367 :
1368 : /* (float_extend (float_extend x)) is (float_extend x)
1369 :
1370 : (float_extend (float x)) is (float x) assuming that double
1371 : rounding can't happen.
1372 : */
1373 602323 : if (GET_CODE (op) == FLOAT_EXTEND
1374 602323 : || ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1375 1381 : && exact_int_to_float_conversion_p (op)))
1376 551 : return simplify_gen_unary (GET_CODE (op), mode,
1377 : XEXP (op, 0),
1378 551 : GET_MODE (XEXP (op, 0)));
1379 :
1380 : break;
1381 :
1382 334118 : case ABS:
1383 : /* (abs (neg <foo>)) -> (abs <foo>) */
1384 334118 : if (GET_CODE (op) == NEG)
1385 18 : return simplify_gen_unary (ABS, mode, XEXP (op, 0),
1386 18 : GET_MODE (XEXP (op, 0)));
1387 :
1388 : /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
1389 : do nothing. */
1390 334100 : if (GET_MODE (op) == VOIDmode)
1391 : break;
1392 :
1393 : /* If operand is something known to be positive, ignore the ABS. */
1394 334100 : if (val_signbit_known_clear_p (GET_MODE (op),
1395 : nonzero_bits (op, GET_MODE (op))))
1396 : return op;
1397 :
1398 : /* Using nonzero_bits doesn't (currently) work for modes wider than
1399 : HOST_WIDE_INT, so the following transformations help simplify
1400 : ABS for TImode and wider. */
1401 333954 : switch (GET_CODE (op))
1402 : {
1403 : case ABS:
1404 : case CLRSB:
1405 : case FFS:
1406 : case PARITY:
1407 : case POPCOUNT:
1408 : case SS_ABS:
1409 : return op;
1410 :
1411 0 : case LSHIFTRT:
1412 0 : if (CONST_INT_P (XEXP (op, 1))
1413 0 : && INTVAL (XEXP (op, 1)) > 0
1414 333954 : && is_a <scalar_int_mode> (mode, &int_mode)
1415 0 : && INTVAL (XEXP (op, 1)) < GET_MODE_PRECISION (int_mode))
1416 : return op;
1417 : break;
1418 :
1419 : default:
1420 : break;
1421 : }
1422 :
1423 : /* If operand is known to be only -1 or 0, convert ABS to NEG. */
1424 333954 : if (is_a <scalar_int_mode> (mode, &int_mode)
1425 54656 : && (num_sign_bit_copies (op, int_mode)
1426 54656 : == GET_MODE_PRECISION (int_mode)))
1427 14 : return gen_rtx_NEG (int_mode, op);
1428 :
1429 : break;
1430 :
1431 0 : case FFS:
1432 : /* (ffs (*_extend <X>)) = (*_extend (ffs <X>)). */
1433 0 : if (GET_CODE (op) == SIGN_EXTEND
1434 0 : || GET_CODE (op) == ZERO_EXTEND)
1435 : {
1436 0 : temp = simplify_gen_unary (FFS, GET_MODE (XEXP (op, 0)),
1437 0 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1438 0 : return simplify_gen_unary (GET_CODE (op), mode, temp,
1439 0 : GET_MODE (temp));
1440 : }
1441 : break;
1442 :
1443 3565 : case POPCOUNT:
1444 3565 : switch (GET_CODE (op))
1445 : {
1446 0 : case BSWAP:
1447 0 : case BITREVERSE:
1448 : /* (popcount (bswap <X>)) = (popcount <X>). */
1449 0 : return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1450 0 : GET_MODE (XEXP (op, 0)));
1451 :
1452 44 : case ZERO_EXTEND:
1453 : /* (popcount (zero_extend <X>)) = (zero_extend (popcount <X>)). */
1454 88 : temp = simplify_gen_unary (POPCOUNT, GET_MODE (XEXP (op, 0)),
1455 44 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1456 44 : return simplify_gen_unary (ZERO_EXTEND, mode, temp,
1457 44 : GET_MODE (temp));
1458 :
1459 0 : case ROTATE:
1460 0 : case ROTATERT:
1461 : /* Rotations don't affect popcount. */
1462 0 : if (!side_effects_p (XEXP (op, 1)))
1463 0 : return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1464 0 : GET_MODE (XEXP (op, 0)));
1465 : break;
1466 :
1467 : default:
1468 : break;
1469 : }
1470 : break;
1471 :
1472 0 : case PARITY:
1473 0 : switch (GET_CODE (op))
1474 : {
1475 0 : case NOT:
1476 0 : case BSWAP:
1477 0 : case BITREVERSE:
1478 0 : return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1479 0 : GET_MODE (XEXP (op, 0)));
1480 :
1481 0 : case ZERO_EXTEND:
1482 0 : case SIGN_EXTEND:
1483 0 : temp = simplify_gen_unary (PARITY, GET_MODE (XEXP (op, 0)),
1484 0 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1485 0 : return simplify_gen_unary (GET_CODE (op), mode, temp,
1486 0 : GET_MODE (temp));
1487 :
1488 0 : case ROTATE:
1489 0 : case ROTATERT:
1490 : /* Rotations don't affect parity. */
1491 0 : if (!side_effects_p (XEXP (op, 1)))
1492 0 : return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1493 0 : GET_MODE (XEXP (op, 0)));
1494 : break;
1495 :
1496 : case PARITY:
1497 : /* (parity (parity x)) -> parity (x). */
1498 : return op;
1499 :
1500 : default:
1501 : break;
1502 : }
1503 : break;
1504 :
1505 32422 : case BSWAP:
1506 : /* (bswap (bswap x)) -> x. */
1507 32422 : if (GET_CODE (op) == BSWAP)
1508 184 : return XEXP (op, 0);
1509 : /* Canonicalize (bswap (bitreverse x)) as (bitreverse (bswap x)). */
1510 32238 : if (GET_CODE (op) == BITREVERSE)
1511 0 : return simplify_gen_unary (BITREVERSE, mode,
1512 : simplify_gen_unary (BSWAP, mode,
1513 : XEXP (op, 0), mode),
1514 0 : mode);
1515 : break;
1516 :
1517 0 : case BITREVERSE:
1518 : /* (bitreverse (bitreverse x)) -> x. */
1519 0 : if (GET_CODE (op) == BITREVERSE)
1520 0 : return XEXP (op, 0);
1521 : break;
1522 :
1523 965299 : case FLOAT:
1524 : /* (float (sign_extend <X>)) = (float <X>). */
1525 965299 : if (GET_CODE (op) == SIGN_EXTEND)
1526 9704 : return simplify_gen_unary (FLOAT, mode, XEXP (op, 0),
1527 9704 : GET_MODE (XEXP (op, 0)));
1528 : break;
1529 :
1530 3152434 : case SIGN_EXTEND:
1531 : /* Check for useless extension. */
1532 3152434 : if (GET_MODE (op) == mode)
1533 : return op;
1534 :
1535 : /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
1536 : becomes just the MINUS if its mode is MODE. This allows
1537 : folding switch statements on machines using casesi (such as
1538 : the VAX). */
1539 3152394 : if (GET_CODE (op) == TRUNCATE
1540 62 : && GET_MODE (XEXP (op, 0)) == mode
1541 62 : && GET_CODE (XEXP (op, 0)) == MINUS
1542 0 : && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
1543 0 : && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
1544 : return XEXP (op, 0);
1545 :
1546 : /* Extending a widening multiplication should be canonicalized to
1547 : a wider widening multiplication. */
1548 3152394 : if (GET_CODE (op) == MULT)
1549 : {
1550 67815 : rtx lhs = XEXP (op, 0);
1551 67815 : rtx rhs = XEXP (op, 1);
1552 67815 : enum rtx_code lcode = GET_CODE (lhs);
1553 67815 : enum rtx_code rcode = GET_CODE (rhs);
1554 :
1555 : /* Widening multiplies usually extend both operands, but sometimes
1556 : they use a shift to extract a portion of a register. */
1557 67815 : if ((lcode == SIGN_EXTEND
1558 67686 : || (lcode == ASHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1559 886 : && (rcode == SIGN_EXTEND
1560 866 : || (rcode == ASHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1561 : {
1562 105 : machine_mode lmode = GET_MODE (lhs);
1563 105 : machine_mode rmode = GET_MODE (rhs);
1564 105 : int bits;
1565 :
1566 105 : if (lcode == ASHIFTRT)
1567 : /* Number of bits not shifted off the end. */
1568 89 : bits = (GET_MODE_UNIT_PRECISION (lmode)
1569 89 : - INTVAL (XEXP (lhs, 1)));
1570 : else /* lcode == SIGN_EXTEND */
1571 : /* Size of inner mode. */
1572 32 : bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1573 :
1574 105 : if (rcode == ASHIFTRT)
1575 85 : bits += (GET_MODE_UNIT_PRECISION (rmode)
1576 85 : - INTVAL (XEXP (rhs, 1)));
1577 : else /* rcode == SIGN_EXTEND */
1578 40 : bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1579 :
1580 : /* We can only widen multiplies if the result is mathematiclly
1581 : equivalent. I.e. if overflow was impossible. */
1582 210 : if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1583 48 : return simplify_gen_binary
1584 48 : (MULT, mode,
1585 : simplify_gen_unary (SIGN_EXTEND, mode, lhs, lmode),
1586 48 : simplify_gen_unary (SIGN_EXTEND, mode, rhs, rmode));
1587 : }
1588 : }
1589 :
1590 : /* Check for a sign extension of a subreg of a promoted
1591 : variable, where the promotion is sign-extended, and the
1592 : target mode is the same as the variable's promotion. */
1593 3152346 : if (GET_CODE (op) == SUBREG
1594 215075 : && SUBREG_PROMOTED_VAR_P (op)
1595 3157564 : && SUBREG_PROMOTED_SIGNED_P (op))
1596 : {
1597 0 : rtx subreg = SUBREG_REG (op);
1598 0 : machine_mode subreg_mode = GET_MODE (subreg);
1599 0 : if (!paradoxical_subreg_p (mode, subreg_mode))
1600 : {
1601 0 : temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1602 0 : if (temp)
1603 : {
1604 : /* Preserve SUBREG_PROMOTED_VAR_P. */
1605 0 : if (partial_subreg_p (temp))
1606 : {
1607 0 : SUBREG_PROMOTED_VAR_P (temp) = 1;
1608 0 : SUBREG_PROMOTED_SET (temp, SRP_SIGNED);
1609 : }
1610 : return temp;
1611 : }
1612 : }
1613 : else
1614 : /* Sign-extending a sign-extended subreg. */
1615 0 : return simplify_gen_unary (SIGN_EXTEND, mode,
1616 0 : subreg, subreg_mode);
1617 : }
1618 :
1619 : /* (sign_extend:M (sign_extend:N <X>)) is (sign_extend:M <X>).
1620 : (sign_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1621 3152346 : if (GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND)
1622 : {
1623 20199 : gcc_assert (GET_MODE_UNIT_PRECISION (mode)
1624 : > GET_MODE_UNIT_PRECISION (GET_MODE (op)));
1625 6733 : return simplify_gen_unary (GET_CODE (op), mode, XEXP (op, 0),
1626 6733 : GET_MODE (XEXP (op, 0)));
1627 : }
1628 :
1629 : /* (sign_extend:M (ashiftrt:N (ashift <X> (const_int I)) (const_int I)))
1630 : is (sign_extend:M (subreg:O <X>)) if there is mode with
1631 : GET_MODE_BITSIZE (N) - I bits.
1632 : (sign_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1633 : is similarly (zero_extend:M (subreg:O <X>)). */
1634 3145613 : if ((GET_CODE (op) == ASHIFTRT || GET_CODE (op) == LSHIFTRT)
1635 91913 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1636 3148516 : && is_a <scalar_int_mode> (mode, &int_mode)
1637 5532 : && CONST_INT_P (XEXP (op, 1))
1638 5532 : && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1639 3150884 : && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1640 5271 : GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1641 : {
1642 5271 : scalar_int_mode tmode;
1643 5271 : gcc_assert (GET_MODE_PRECISION (int_mode)
1644 : > GET_MODE_PRECISION (op_mode));
1645 5271 : if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1646 7913 : - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1647 : {
1648 2629 : rtx inner =
1649 2629 : rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1650 2629 : if (inner)
1651 2629 : return simplify_gen_unary (GET_CODE (op) == ASHIFTRT
1652 : ? SIGN_EXTEND : ZERO_EXTEND,
1653 2629 : int_mode, inner, tmode);
1654 : }
1655 : }
1656 :
1657 : /* (sign_extend:M (lshiftrt:N <X> (const_int I))) is better as
1658 : (zero_extend:M (lshiftrt:N <X> (const_int I))) if I is not 0. */
1659 3142984 : if (GET_CODE (op) == LSHIFTRT
1660 167 : && CONST_INT_P (XEXP (op, 1))
1661 167 : && XEXP (op, 1) != const0_rtx)
1662 167 : return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1663 :
1664 : /* (sign_extend:M (truncate:N (lshiftrt:O <X> (const_int I)))) where
1665 : I is GET_MODE_PRECISION(O) - GET_MODE_PRECISION(N), simplifies to
1666 : (ashiftrt:M <X> (const_int I)) if modes M and O are the same, and
1667 : (truncate:M (ashiftrt:O <X> (const_int I))) if M is narrower than
1668 : O, and (sign_extend:M (ashiftrt:O <X> (const_int I))) if M is
1669 : wider than O. */
1670 3142817 : if (GET_CODE (op) == TRUNCATE
1671 62 : && GET_CODE (XEXP (op, 0)) == LSHIFTRT
1672 0 : && CONST_INT_P (XEXP (XEXP (op, 0), 1)))
1673 : {
1674 0 : scalar_int_mode m_mode, n_mode, o_mode;
1675 0 : rtx old_shift = XEXP (op, 0);
1676 0 : if (is_a <scalar_int_mode> (mode, &m_mode)
1677 0 : && is_a <scalar_int_mode> (GET_MODE (op), &n_mode)
1678 0 : && is_a <scalar_int_mode> (GET_MODE (old_shift), &o_mode)
1679 0 : && GET_MODE_PRECISION (o_mode) - GET_MODE_PRECISION (n_mode)
1680 0 : == INTVAL (XEXP (old_shift, 1)))
1681 : {
1682 0 : rtx new_shift = simplify_gen_binary (ASHIFTRT,
1683 : GET_MODE (old_shift),
1684 : XEXP (old_shift, 0),
1685 : XEXP (old_shift, 1));
1686 0 : if (GET_MODE_PRECISION (m_mode) > GET_MODE_PRECISION (o_mode))
1687 0 : return simplify_gen_unary (SIGN_EXTEND, mode, new_shift,
1688 0 : GET_MODE (new_shift));
1689 0 : if (mode != GET_MODE (new_shift))
1690 0 : return simplify_gen_unary (TRUNCATE, mode, new_shift,
1691 0 : GET_MODE (new_shift));
1692 : return new_shift;
1693 : }
1694 : }
1695 :
1696 : /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when
1697 : we know the sign bit of OP must be clear. */
1698 3142817 : if (val_signbit_known_clear_p (GET_MODE (op),
1699 3142817 : nonzero_bits (op, GET_MODE (op))))
1700 39671 : return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1701 :
1702 : /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1703 3103146 : if (GET_CODE (op) == SUBREG
1704 214789 : && subreg_lowpart_p (op)
1705 214595 : && GET_MODE (SUBREG_REG (op)) == mode
1706 3285929 : && is_a <scalar_int_mode> (mode, &int_mode)
1707 189561 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1708 189561 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1709 187768 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1710 3290914 : && (nonzero_bits (SUBREG_REG (op), mode)
1711 187768 : & ~(GET_MODE_MASK (op_mode) >> 1)) == 0)
1712 6778 : return SUBREG_REG (op);
1713 :
1714 : #if defined(POINTERS_EXTEND_UNSIGNED)
1715 : /* As we do not know which address space the pointer is referring to,
1716 : we can do this only if the target does not support different pointer
1717 : or address modes depending on the address space. */
1718 3096368 : if (target_default_pointer_address_modes_p ()
1719 : && ! POINTERS_EXTEND_UNSIGNED
1720 : && mode == Pmode && GET_MODE (op) == ptr_mode
1721 : && (CONSTANT_P (op)
1722 : || (GET_CODE (op) == SUBREG
1723 : && REG_P (SUBREG_REG (op))
1724 : && REG_POINTER (SUBREG_REG (op))
1725 : && GET_MODE (SUBREG_REG (op)) == Pmode))
1726 : && !targetm.have_ptr_extend ())
1727 : {
1728 : temp
1729 : = convert_memory_address_addr_space_1 (Pmode, op,
1730 : ADDR_SPACE_GENERIC, false,
1731 : true);
1732 : if (temp)
1733 : return temp;
1734 : }
1735 : #endif
1736 : break;
1737 :
1738 12048402 : case ZERO_EXTEND:
1739 : /* Check for useless extension. */
1740 12048402 : if (GET_MODE (op) == mode)
1741 : return op;
1742 :
1743 : /* (zero_extend:SI (and:QI X (const))) -> (and:SI (lowpart:SI X) const)
1744 : where const does not sign bit set. */
1745 12048360 : if (GET_CODE (op) == AND
1746 138302 : && CONST_INT_P (XEXP (op, 1))
1747 101179 : && INTVAL (XEXP (op, 1)) > 0)
1748 : {
1749 92206 : rtx tem = rtl_hooks.gen_lowpart_no_emit (mode, XEXP (op, 0));
1750 92206 : if (tem)
1751 73465 : return simplify_gen_binary (AND, mode, tem, XEXP (op, 1));
1752 : }
1753 :
1754 : /* Check for a zero extension of a subreg of a promoted
1755 : variable, where the promotion is zero-extended, and the
1756 : target mode is the same as the variable's promotion. */
1757 11974895 : if (GET_CODE (op) == SUBREG
1758 1723386 : && SUBREG_PROMOTED_VAR_P (op)
1759 11975380 : && SUBREG_PROMOTED_UNSIGNED_P (op))
1760 : {
1761 485 : rtx subreg = SUBREG_REG (op);
1762 485 : machine_mode subreg_mode = GET_MODE (subreg);
1763 485 : if (!paradoxical_subreg_p (mode, subreg_mode))
1764 : {
1765 330 : temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1766 330 : if (temp)
1767 : {
1768 : /* Preserve SUBREG_PROMOTED_VAR_P. */
1769 330 : if (partial_subreg_p (temp))
1770 : {
1771 193 : SUBREG_PROMOTED_VAR_P (temp) = 1;
1772 193 : SUBREG_PROMOTED_SET (temp, SRP_UNSIGNED);
1773 : }
1774 : return temp;
1775 : }
1776 : }
1777 : else
1778 : /* Zero-extending a zero-extended subreg. */
1779 155 : return simplify_gen_unary (ZERO_EXTEND, mode,
1780 155 : subreg, subreg_mode);
1781 : }
1782 :
1783 : /* Extending a widening multiplication should be canonicalized to
1784 : a wider widening multiplication. */
1785 11974410 : if (GET_CODE (op) == MULT)
1786 : {
1787 188052 : rtx lhs = XEXP (op, 0);
1788 188052 : rtx rhs = XEXP (op, 1);
1789 188052 : enum rtx_code lcode = GET_CODE (lhs);
1790 188052 : enum rtx_code rcode = GET_CODE (rhs);
1791 :
1792 : /* Widening multiplies usually extend both operands, but sometimes
1793 : they use a shift to extract a portion of a register. */
1794 188052 : if ((lcode == ZERO_EXTEND
1795 187339 : || (lcode == LSHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1796 932 : && (rcode == ZERO_EXTEND
1797 879 : || (rcode == LSHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1798 : {
1799 65 : machine_mode lmode = GET_MODE (lhs);
1800 65 : machine_mode rmode = GET_MODE (rhs);
1801 65 : int bits;
1802 :
1803 65 : if (lcode == LSHIFTRT)
1804 : /* Number of bits not shifted off the end. */
1805 12 : bits = (GET_MODE_UNIT_PRECISION (lmode)
1806 12 : - INTVAL (XEXP (lhs, 1)));
1807 : else /* lcode == ZERO_EXTEND */
1808 : /* Size of inner mode. */
1809 106 : bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1810 :
1811 65 : if (rcode == LSHIFTRT)
1812 12 : bits += (GET_MODE_UNIT_PRECISION (rmode)
1813 12 : - INTVAL (XEXP (rhs, 1)));
1814 : else /* rcode == ZERO_EXTEND */
1815 106 : bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1816 :
1817 : /* We can only widen multiplies if the result is mathematiclly
1818 : equivalent. I.e. if overflow was impossible. */
1819 130 : if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1820 53 : return simplify_gen_binary
1821 53 : (MULT, mode,
1822 : simplify_gen_unary (ZERO_EXTEND, mode, lhs, lmode),
1823 53 : simplify_gen_unary (ZERO_EXTEND, mode, rhs, rmode));
1824 : }
1825 : }
1826 :
1827 : /* (zero_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1828 11974357 : if (GET_CODE (op) == ZERO_EXTEND)
1829 22746 : return simplify_gen_unary (ZERO_EXTEND, mode, XEXP (op, 0),
1830 22746 : GET_MODE (XEXP (op, 0)));
1831 :
1832 : /* (zero_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1833 : is (zero_extend:M (subreg:O <X>)) if there is mode with
1834 : GET_MODE_PRECISION (N) - I bits. */
1835 11951611 : if (GET_CODE (op) == LSHIFTRT
1836 54563 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1837 11951617 : && is_a <scalar_int_mode> (mode, &int_mode)
1838 6 : && CONST_INT_P (XEXP (op, 1))
1839 1 : && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1840 11951611 : && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1841 0 : GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1842 : {
1843 0 : scalar_int_mode tmode;
1844 0 : if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1845 0 : - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1846 : {
1847 0 : rtx inner =
1848 0 : rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1849 0 : if (inner)
1850 0 : return simplify_gen_unary (ZERO_EXTEND, int_mode,
1851 0 : inner, tmode);
1852 : }
1853 : }
1854 :
1855 : /* (zero_extend:M (subreg:N <X:O>)) is <X:O> (for M == O) or
1856 : (zero_extend:M <X:O>), if X doesn't have any non-zero bits outside
1857 : of mode N. E.g.
1858 : (zero_extend:SI (subreg:QI (and:SI (reg:SI) (const_int 63)) 0)) is
1859 : (and:SI (reg:SI) (const_int 63)). */
1860 11951611 : if (partial_subreg_p (op)
1861 13597973 : && is_a <scalar_int_mode> (mode, &int_mode)
1862 1669641 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &op0_mode)
1863 1668949 : && GET_MODE_PRECISION (op0_mode) <= HOST_BITS_PER_WIDE_INT
1864 1308720 : && GET_MODE_PRECISION (int_mode) >= GET_MODE_PRECISION (op0_mode)
1865 1286852 : && subreg_lowpart_p (op)
1866 2567944 : && (nonzero_bits (SUBREG_REG (op), op0_mode)
1867 845043 : & ~GET_MODE_MASK (GET_MODE (op))) == 0)
1868 : {
1869 23279 : if (GET_MODE_PRECISION (int_mode) == GET_MODE_PRECISION (op0_mode))
1870 15977 : return SUBREG_REG (op);
1871 7302 : return simplify_gen_unary (ZERO_EXTEND, int_mode, SUBREG_REG (op),
1872 7302 : op0_mode);
1873 : }
1874 :
1875 : /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1876 11928332 : if (GET_CODE (op) == SUBREG
1877 1699622 : && subreg_lowpart_p (op)
1878 997398 : && GET_MODE (SUBREG_REG (op)) == mode
1879 12773782 : && is_a <scalar_int_mode> (mode, &int_mode)
1880 845450 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1881 845450 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1882 746706 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1883 12675038 : && (nonzero_bits (SUBREG_REG (op), mode)
1884 746706 : & ~GET_MODE_MASK (op_mode)) == 0)
1885 0 : return SUBREG_REG (op);
1886 :
1887 : /* Trying to optimize:
1888 : (zero_extend:M (subreg:N (not:M (X:M)))) ->
1889 : (xor:M (zero_extend:M (subreg:N (X:M)), mask))
1890 : where the mask is GET_MODE_MASK (N).
1891 : For the cases when X:M doesn't have any non-zero bits
1892 : outside of mode N, (zero_extend:M (subreg:N (X:M))
1893 : will be simplified to just (X:M)
1894 : and whole optimization will be -> (xor:M (X:M, mask)). */
1895 11928332 : if (partial_subreg_p (op)
1896 1646362 : && GET_CODE (XEXP (op, 0)) == NOT
1897 682 : && GET_MODE (XEXP (op, 0)) == mode
1898 664 : && subreg_lowpart_p (op)
1899 11928683 : && HWI_COMPUTABLE_MODE_P (mode)
1900 352 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1901 1699974 : && (nonzero_bits (XEXP (XEXP (op, 0), 0), mode)
1902 352 : & ~GET_MODE_MASK (op_mode)) == 0)
1903 : {
1904 1 : unsigned HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
1905 2 : return simplify_gen_binary (XOR, mode,
1906 1 : XEXP (XEXP (op, 0), 0),
1907 1 : gen_int_mode (mask, mode));
1908 : }
1909 :
1910 : #if defined(POINTERS_EXTEND_UNSIGNED)
1911 : /* As we do not know which address space the pointer is referring to,
1912 : we can do this only if the target does not support different pointer
1913 : or address modes depending on the address space. */
1914 11928331 : if (target_default_pointer_address_modes_p ()
1915 : && POINTERS_EXTEND_UNSIGNED > 0
1916 13552634 : && mode == Pmode && GET_MODE (op) == ptr_mode
1917 977 : && (CONSTANT_P (op)
1918 956 : || (GET_CODE (op) == SUBREG
1919 0 : && REG_P (SUBREG_REG (op))
1920 0 : && REG_POINTER (SUBREG_REG (op))
1921 0 : && GET_MODE (SUBREG_REG (op)) == Pmode))
1922 11928352 : && !targetm.have_ptr_extend ())
1923 : {
1924 21 : temp
1925 21 : = convert_memory_address_addr_space_1 (Pmode, op,
1926 : ADDR_SPACE_GENERIC, false,
1927 : true);
1928 21 : if (temp)
1929 : return temp;
1930 : }
1931 : #endif
1932 : break;
1933 :
1934 803665 : case VEC_DUPLICATE:
1935 803665 : if (GET_CODE (op) == VEC_DUPLICATE)
1936 2 : return simplify_gen_unary (VEC_DUPLICATE, mode, XEXP (op, 0),
1937 2 : GET_MODE (XEXP (op, 0)));
1938 : break;
1939 :
1940 : default:
1941 : break;
1942 : }
1943 :
1944 20397152 : if (VECTOR_MODE_P (mode)
1945 1898543 : && vec_duplicate_p (op, &elt)
1946 22301688 : && code != VEC_DUPLICATE)
1947 : {
1948 5993 : if (code == SIGN_EXTEND || code == ZERO_EXTEND)
1949 : /* Enforce a canonical order of VEC_DUPLICATE wrt other unary
1950 : operations by promoting VEC_DUPLICATE to the root of the expression
1951 : (as far as possible). */
1952 5004 : temp = simplify_gen_unary (code, GET_MODE_INNER (mode),
1953 10008 : elt, GET_MODE_INNER (GET_MODE (op)));
1954 : else
1955 : /* Try applying the operator to ELT and see if that simplifies.
1956 : We can duplicate the result if so.
1957 :
1958 : The reason we traditionally haven't used simplify_gen_unary
1959 : for these codes is that it didn't necessarily seem to be a
1960 : win to convert things like:
1961 :
1962 : (neg:V (vec_duplicate:V (reg:S R)))
1963 :
1964 : to:
1965 :
1966 : (vec_duplicate:V (neg:S (reg:S R)))
1967 :
1968 : The first might be done entirely in vector registers while the
1969 : second might need a move between register files.
1970 :
1971 : However, there also cases where promoting the vec_duplicate is
1972 : more efficient, and there is definite value in having a canonical
1973 : form when matching instruction patterns. We should consider
1974 : extending the simplify_gen_unary code above to more cases. */
1975 989 : temp = simplify_unary_operation (code, GET_MODE_INNER (mode),
1976 1978 : elt, GET_MODE_INNER (GET_MODE (op)));
1977 5993 : if (temp)
1978 5536 : return gen_vec_duplicate (mode, temp);
1979 : }
1980 :
1981 : return 0;
1982 : }
1983 :
1984 : /* Try to compute the value of a unary operation CODE whose output mode is to
1985 : be MODE with input operand OP whose mode was originally OP_MODE.
1986 : Return zero if the value cannot be computed. */
1987 : rtx
1988 30643426 : simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
1989 : rtx op, machine_mode op_mode)
1990 : {
1991 30643426 : scalar_int_mode result_mode;
1992 :
1993 30643426 : if (code == VEC_DUPLICATE)
1994 : {
1995 1964863 : gcc_assert (VECTOR_MODE_P (mode));
1996 1964863 : if (GET_MODE (op) != VOIDmode)
1997 : {
1998 817726 : if (!VECTOR_MODE_P (GET_MODE (op)))
1999 1622074 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE (op));
2000 : else
2001 20067 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE_INNER
2002 : (GET_MODE (op)));
2003 : }
2004 1964863 : if (CONST_SCALAR_INT_P (op) || CONST_DOUBLE_AS_FLOAT_P (op))
2005 1160442 : return gen_const_vec_duplicate (mode, op);
2006 804421 : if (GET_CODE (op) == CONST_VECTOR
2007 804421 : && (CONST_VECTOR_DUPLICATE_P (op)
2008 756 : || CONST_VECTOR_NUNITS (op).is_constant ()))
2009 : {
2010 756 : unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op)
2011 756 : ? CONST_VECTOR_NPATTERNS (op)
2012 1510 : : CONST_VECTOR_NUNITS (op).to_constant ());
2013 2268 : gcc_assert (multiple_p (GET_MODE_NUNITS (mode), npatterns));
2014 756 : rtx_vector_builder builder (mode, npatterns, 1);
2015 3888 : for (unsigned i = 0; i < npatterns; i++)
2016 2376 : builder.quick_push (CONST_VECTOR_ELT (op, i));
2017 756 : return builder.build ();
2018 756 : }
2019 : }
2020 :
2021 27909668 : if (VECTOR_MODE_P (mode)
2022 1948611 : && GET_CODE (op) == CONST_VECTOR
2023 29589943 : && known_eq (GET_MODE_NUNITS (mode), CONST_VECTOR_NUNITS (op)))
2024 : {
2025 35905 : gcc_assert (GET_MODE (op) == op_mode);
2026 :
2027 35905 : rtx_vector_builder builder;
2028 35905 : if (!builder.new_unary_operation (mode, op, false))
2029 : return 0;
2030 :
2031 35905 : unsigned int count = builder.encoded_nelts ();
2032 154579 : for (unsigned int i = 0; i < count; i++)
2033 : {
2034 238400 : rtx x = simplify_unary_operation (code, GET_MODE_INNER (mode),
2035 : CONST_VECTOR_ELT (op, i),
2036 238400 : GET_MODE_INNER (op_mode));
2037 119200 : if (!x || !valid_for_const_vector_p (mode, x))
2038 526 : return 0;
2039 118674 : builder.quick_push (x);
2040 : }
2041 35379 : return builder.build ();
2042 35905 : }
2043 :
2044 : /* The order of these tests is critical so that, for example, we don't
2045 : check the wrong mode (input vs. output) for a conversion operation,
2046 : such as FIX. At some point, this should be simplified. */
2047 :
2048 29446323 : if (code == FLOAT && CONST_SCALAR_INT_P (op))
2049 : {
2050 8392 : REAL_VALUE_TYPE d;
2051 :
2052 8392 : if (op_mode == VOIDmode)
2053 : {
2054 : /* CONST_INT have VOIDmode as the mode. We assume that all
2055 : the bits of the constant are significant, though, this is
2056 : a dangerous assumption as many times CONST_INTs are
2057 : created and used with garbage in the bits outside of the
2058 : precision of the implied mode of the const_int. */
2059 64 : op_mode = MAX_MODE_INT;
2060 : }
2061 :
2062 8392 : real_from_integer (&d, mode, rtx_mode_t (op, op_mode), SIGNED);
2063 :
2064 : /* Avoid the folding if flag_signaling_nans is on and
2065 : operand is a signaling NaN. */
2066 8392 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2067 : return 0;
2068 :
2069 8392 : d = real_value_truncate (mode, d);
2070 :
2071 : /* Avoid the folding if flag_rounding_math is on and the
2072 : conversion is not exact. */
2073 8392 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2074 : {
2075 1011 : bool fail = false;
2076 1011 : wide_int w = real_to_integer (&d, &fail,
2077 : GET_MODE_PRECISION
2078 1011 : (as_a <scalar_int_mode> (op_mode)));
2079 2022 : if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
2080 905 : return 0;
2081 1011 : }
2082 :
2083 7487 : return const_double_from_real_value (d, mode);
2084 : }
2085 29437931 : else if (code == UNSIGNED_FLOAT && CONST_SCALAR_INT_P (op))
2086 : {
2087 2133 : REAL_VALUE_TYPE d;
2088 :
2089 2133 : if (op_mode == VOIDmode)
2090 : {
2091 : /* CONST_INT have VOIDmode as the mode. We assume that all
2092 : the bits of the constant are significant, though, this is
2093 : a dangerous assumption as many times CONST_INTs are
2094 : created and used with garbage in the bits outside of the
2095 : precision of the implied mode of the const_int. */
2096 8 : op_mode = MAX_MODE_INT;
2097 : }
2098 :
2099 2133 : real_from_integer (&d, mode, rtx_mode_t (op, op_mode), UNSIGNED);
2100 :
2101 : /* Avoid the folding if flag_signaling_nans is on and
2102 : operand is a signaling NaN. */
2103 2133 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2104 : return 0;
2105 :
2106 2133 : d = real_value_truncate (mode, d);
2107 :
2108 : /* Avoid the folding if flag_rounding_math is on and the
2109 : conversion is not exact. */
2110 2133 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2111 : {
2112 16 : bool fail = false;
2113 16 : wide_int w = real_to_integer (&d, &fail,
2114 : GET_MODE_PRECISION
2115 16 : (as_a <scalar_int_mode> (op_mode)));
2116 28 : if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
2117 16 : return 0;
2118 16 : }
2119 :
2120 2117 : return const_double_from_real_value (d, mode);
2121 : }
2122 :
2123 29435798 : if (CONST_SCALAR_INT_P (op) && is_a <scalar_int_mode> (mode, &result_mode))
2124 : {
2125 3915650 : unsigned int width = GET_MODE_PRECISION (result_mode);
2126 3915650 : if (width > MAX_BITSIZE_MODE_ANY_INT)
2127 : return 0;
2128 :
2129 3915650 : wide_int result;
2130 3915650 : scalar_int_mode imode = (op_mode == VOIDmode
2131 3915650 : ? result_mode
2132 3915368 : : as_a <scalar_int_mode> (op_mode));
2133 3915650 : rtx_mode_t op0 = rtx_mode_t (op, imode);
2134 3915650 : int int_value;
2135 :
2136 : #if TARGET_SUPPORTS_WIDE_INT == 0
2137 : /* This assert keeps the simplification from producing a result
2138 : that cannot be represented in a CONST_DOUBLE but a lot of
2139 : upstream callers expect that this function never fails to
2140 : simplify something and so you if you added this to the test
2141 : above the code would die later anyway. If this assert
2142 : happens, you just need to make the port support wide int. */
2143 : gcc_assert (width <= HOST_BITS_PER_DOUBLE_INT);
2144 : #endif
2145 :
2146 3915650 : switch (code)
2147 : {
2148 189676 : case NOT:
2149 189676 : result = wi::bit_not (op0);
2150 189676 : break;
2151 :
2152 1902694 : case NEG:
2153 1902694 : result = wi::neg (op0);
2154 1902694 : break;
2155 :
2156 7145 : case ABS:
2157 7145 : result = wi::abs (op0);
2158 7145 : break;
2159 :
2160 0 : case FFS:
2161 0 : result = wi::shwi (wi::ffs (op0), result_mode);
2162 0 : break;
2163 :
2164 175 : case CLZ:
2165 175 : if (wi::ne_p (op0, 0))
2166 45 : int_value = wi::clz (op0);
2167 260 : else if (! CLZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
2168 : return NULL_RTX;
2169 45 : result = wi::shwi (int_value, result_mode);
2170 45 : break;
2171 :
2172 0 : case CLRSB:
2173 0 : result = wi::shwi (wi::clrsb (op0), result_mode);
2174 0 : break;
2175 :
2176 0 : case CTZ:
2177 0 : if (wi::ne_p (op0, 0))
2178 0 : int_value = wi::ctz (op0);
2179 0 : else if (! CTZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
2180 : return NULL_RTX;
2181 0 : result = wi::shwi (int_value, result_mode);
2182 0 : break;
2183 :
2184 160 : case POPCOUNT:
2185 160 : result = wi::shwi (wi::popcount (op0), result_mode);
2186 160 : break;
2187 :
2188 0 : case PARITY:
2189 0 : result = wi::shwi (wi::parity (op0), result_mode);
2190 0 : break;
2191 :
2192 1854 : case BSWAP:
2193 1854 : result = wi::bswap (op0);
2194 1854 : break;
2195 :
2196 0 : case BITREVERSE:
2197 0 : result = wi::bitreverse (op0);
2198 0 : break;
2199 :
2200 1634009 : case TRUNCATE:
2201 1634009 : case ZERO_EXTEND:
2202 1634009 : result = wide_int::from (op0, width, UNSIGNED);
2203 1634009 : break;
2204 :
2205 14450 : case US_TRUNCATE:
2206 14450 : case SS_TRUNCATE:
2207 14450 : {
2208 14450 : signop sgn = code == US_TRUNCATE ? UNSIGNED : SIGNED;
2209 14450 : wide_int nmax
2210 14450 : = wide_int::from (wi::max_value (width, sgn),
2211 28900 : GET_MODE_PRECISION (imode), sgn);
2212 14450 : wide_int nmin
2213 14450 : = wide_int::from (wi::min_value (width, sgn),
2214 28900 : GET_MODE_PRECISION (imode), sgn);
2215 14450 : result = wi::min (wi::max (op0, nmin, sgn), nmax, sgn);
2216 14450 : result = wide_int::from (result, width, sgn);
2217 14450 : break;
2218 14450 : }
2219 165487 : case SIGN_EXTEND:
2220 165487 : result = wide_int::from (op0, width, SIGNED);
2221 165487 : break;
2222 :
2223 0 : case SS_NEG:
2224 0 : if (wi::only_sign_bit_p (op0))
2225 0 : result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED);
2226 : else
2227 0 : result = wi::neg (op0);
2228 : break;
2229 :
2230 0 : case SS_ABS:
2231 0 : if (wi::only_sign_bit_p (op0))
2232 0 : result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED);
2233 : else
2234 0 : result = wi::abs (op0);
2235 : break;
2236 :
2237 : case SQRT:
2238 : default:
2239 : return 0;
2240 : }
2241 :
2242 3915520 : return immed_wide_int_const (result, result_mode);
2243 3915650 : }
2244 :
2245 25520148 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2246 414989 : && SCALAR_FLOAT_MODE_P (mode)
2247 412931 : && SCALAR_FLOAT_MODE_P (GET_MODE (op)))
2248 : {
2249 412931 : REAL_VALUE_TYPE d = *CONST_DOUBLE_REAL_VALUE (op);
2250 412931 : switch (code)
2251 : {
2252 : case SQRT:
2253 : return 0;
2254 768 : case ABS:
2255 768 : d = real_value_abs (&d);
2256 768 : break;
2257 15808 : case NEG:
2258 15808 : d = real_value_negate (&d);
2259 15808 : break;
2260 2283 : case FLOAT_TRUNCATE:
2261 : /* Don't perform the operation if flag_signaling_nans is on
2262 : and the operand is a signaling NaN. */
2263 2283 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2264 : return NULL_RTX;
2265 : /* Or if flag_rounding_math is on and the truncation is not
2266 : exact. */
2267 2283 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2268 2283 : && !exact_real_truncate (mode, &d))
2269 231 : return NULL_RTX;
2270 2052 : d = real_value_truncate (mode, d);
2271 2052 : break;
2272 387397 : case FLOAT_EXTEND:
2273 : /* Don't perform the operation if flag_signaling_nans is on
2274 : and the operand is a signaling NaN. */
2275 387397 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2276 : return NULL_RTX;
2277 : /* All this does is change the mode, unless changing
2278 : mode class. */
2279 387395 : if (GET_MODE_CLASS (mode) != GET_MODE_CLASS (GET_MODE (op)))
2280 0 : real_convert (&d, mode, &d);
2281 : break;
2282 0 : case FIX:
2283 : /* Don't perform the operation if flag_signaling_nans is on
2284 : and the operand is a signaling NaN. */
2285 0 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2286 : return NULL_RTX;
2287 0 : real_arithmetic (&d, FIX_TRUNC_EXPR, &d, NULL);
2288 0 : break;
2289 6062 : case NOT:
2290 6062 : {
2291 6062 : long tmp[4];
2292 6062 : int i;
2293 :
2294 6062 : real_to_target (tmp, &d, GET_MODE (op));
2295 30310 : for (i = 0; i < 4; i++)
2296 24248 : tmp[i] = ~tmp[i];
2297 6062 : real_from_target (&d, tmp, mode);
2298 6062 : break;
2299 : }
2300 0 : default:
2301 0 : gcc_unreachable ();
2302 : }
2303 412085 : return const_double_from_real_value (d, mode);
2304 : }
2305 2058 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2306 2058 : && SCALAR_FLOAT_MODE_P (GET_MODE (op))
2307 25109275 : && is_int_mode (mode, &result_mode))
2308 : {
2309 2058 : unsigned int width = GET_MODE_PRECISION (result_mode);
2310 2058 : if (width > MAX_BITSIZE_MODE_ANY_INT)
2311 : return 0;
2312 :
2313 : /* Although the overflow semantics of RTL's FIX and UNSIGNED_FIX
2314 : operators are intentionally left unspecified (to ease implementation
2315 : by target backends), for consistency, this routine implements the
2316 : same semantics for constant folding as used by the middle-end. */
2317 :
2318 : /* This was formerly used only for non-IEEE float.
2319 : eggert@twinsun.com says it is safe for IEEE also. */
2320 2058 : REAL_VALUE_TYPE t;
2321 2058 : const REAL_VALUE_TYPE *x = CONST_DOUBLE_REAL_VALUE (op);
2322 2058 : wide_int wmax, wmin;
2323 : /* This is part of the abi to real_to_integer, but we check
2324 : things before making this call. */
2325 2058 : bool fail;
2326 :
2327 2058 : switch (code)
2328 : {
2329 2050 : case FIX:
2330 : /* According to IEEE standard, for conversions from floating point to
2331 : integer. When a NaN or infinite operand cannot be represented in
2332 : the destination format and this cannot otherwise be indicated, the
2333 : invalid operation exception shall be signaled. When a numeric
2334 : operand would convert to an integer outside the range of the
2335 : destination format, the invalid operation exception shall be
2336 : signaled if this situation cannot otherwise be indicated. */
2337 2050 : if (REAL_VALUE_ISNAN (*x))
2338 955 : return flag_trapping_math ? NULL_RTX : const0_rtx;
2339 :
2340 1095 : if (REAL_VALUE_ISINF (*x) && flag_trapping_math)
2341 : return NULL_RTX;
2342 :
2343 : /* Test against the signed upper bound. */
2344 135 : wmax = wi::max_value (width, SIGNED);
2345 135 : real_from_integer (&t, VOIDmode, wmax, SIGNED);
2346 135 : if (real_less (&t, x))
2347 3 : return (flag_trapping_math
2348 3 : ? NULL_RTX : immed_wide_int_const (wmax, mode));
2349 :
2350 : /* Test against the signed lower bound. */
2351 132 : wmin = wi::min_value (width, SIGNED);
2352 132 : real_from_integer (&t, VOIDmode, wmin, SIGNED);
2353 132 : if (real_less (x, &t))
2354 8 : return immed_wide_int_const (wmin, mode);
2355 :
2356 124 : return immed_wide_int_const (real_to_integer (x, &fail, width),
2357 : mode);
2358 :
2359 8 : case UNSIGNED_FIX:
2360 8 : if (REAL_VALUE_ISNAN (*x) || REAL_VALUE_NEGATIVE (*x))
2361 6 : return flag_trapping_math ? NULL_RTX : const0_rtx;
2362 :
2363 2 : if (REAL_VALUE_ISINF (*x) && flag_trapping_math)
2364 : return NULL_RTX;
2365 :
2366 : /* Test against the unsigned upper bound. */
2367 0 : wmax = wi::max_value (width, UNSIGNED);
2368 0 : real_from_integer (&t, VOIDmode, wmax, UNSIGNED);
2369 0 : if (real_less (&t, x))
2370 0 : return (flag_trapping_math
2371 0 : ? NULL_RTX : immed_wide_int_const (wmax, mode));
2372 :
2373 0 : return immed_wide_int_const (real_to_integer (x, &fail, width),
2374 : mode);
2375 :
2376 0 : default:
2377 0 : gcc_unreachable ();
2378 : }
2379 2058 : }
2380 :
2381 : /* Handle polynomial integers. */
2382 : else if (CONST_POLY_INT_P (op))
2383 : {
2384 : poly_wide_int result;
2385 : switch (code)
2386 : {
2387 : case NEG:
2388 : result = -const_poly_int_value (op);
2389 : break;
2390 :
2391 : case NOT:
2392 : result = ~const_poly_int_value (op);
2393 : break;
2394 :
2395 : default:
2396 : return NULL_RTX;
2397 : }
2398 : return immed_wide_int_const (result, mode);
2399 : }
2400 :
2401 : return NULL_RTX;
2402 : }
2403 :
2404 : /* Subroutine of simplify_binary_operation to simplify a binary operation
2405 : CODE that can commute with byte swapping, with result mode MODE and
2406 : operating on OP0 and OP1. CODE is currently one of AND, IOR or XOR.
2407 : Return zero if no simplification or canonicalization is possible. */
2408 :
2409 : rtx
2410 38921200 : simplify_context::simplify_byte_swapping_operation (rtx_code code,
2411 : machine_mode mode,
2412 : rtx op0, rtx op1)
2413 : {
2414 38921200 : rtx tem;
2415 :
2416 : /* (op (bswap x) C1)) -> (bswap (op x C2)) with C2 swapped. */
2417 38921200 : if (GET_CODE (op0) == BSWAP && CONST_SCALAR_INT_P (op1))
2418 : {
2419 287 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0),
2420 : simplify_gen_unary (BSWAP, mode, op1, mode));
2421 287 : return simplify_gen_unary (BSWAP, mode, tem, mode);
2422 : }
2423 :
2424 : /* (op (bswap x) (bswap y)) -> (bswap (op x y)). */
2425 38920913 : if (GET_CODE (op0) == BSWAP && GET_CODE (op1) == BSWAP)
2426 : {
2427 0 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0), XEXP (op1, 0));
2428 0 : return simplify_gen_unary (BSWAP, mode, tem, mode);
2429 : }
2430 :
2431 : return NULL_RTX;
2432 : }
2433 :
2434 : /* Subroutine of simplify_binary_operation to simplify a commutative,
2435 : associative binary operation CODE with result mode MODE, operating
2436 : on OP0 and OP1. CODE is currently one of PLUS, MULT, AND, IOR, XOR,
2437 : SMIN, SMAX, UMIN or UMAX. Return zero if no simplification or
2438 : canonicalization is possible. */
2439 :
2440 : rtx
2441 50302717 : simplify_context::simplify_associative_operation (rtx_code code,
2442 : machine_mode mode,
2443 : rtx op0, rtx op1)
2444 : {
2445 50302717 : rtx tem;
2446 :
2447 : /* Normally expressions simplified by simplify-rtx.cc are combined
2448 : at most from a few machine instructions and therefore the
2449 : expressions should be fairly small. During var-tracking
2450 : we can see arbitrarily large expressions though and reassociating
2451 : those can be quadratic, so punt after encountering max_assoc_count
2452 : simplify_associative_operation calls during outermost simplify_*
2453 : call. */
2454 50302717 : if (++assoc_count >= max_assoc_count)
2455 : return NULL_RTX;
2456 :
2457 : /* Linearize the operator to the left. */
2458 50298367 : if (GET_CODE (op1) == code)
2459 : {
2460 : /* "(a op b) op (c op d)" becomes "((a op b) op c) op d)". */
2461 20789 : if (GET_CODE (op0) == code)
2462 : {
2463 6266 : tem = simplify_gen_binary (code, mode, op0, XEXP (op1, 0));
2464 6266 : return simplify_gen_binary (code, mode, tem, XEXP (op1, 1));
2465 : }
2466 :
2467 : /* "a op (b op c)" becomes "(b op c) op a". */
2468 14523 : if (! swap_commutative_operands_p (op1, op0))
2469 14523 : return simplify_gen_binary (code, mode, op1, op0);
2470 :
2471 : std::swap (op0, op1);
2472 : }
2473 :
2474 50277578 : if (GET_CODE (op0) == code)
2475 : {
2476 : /* Canonicalize "(x op c) op y" as "(x op y) op c". */
2477 1271203 : if (swap_commutative_operands_p (XEXP (op0, 1), op1))
2478 : {
2479 262120 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0), op1);
2480 262120 : return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2481 : }
2482 :
2483 : /* Attempt to simplify "(a op b) op c" as "a op (b op c)". */
2484 1009083 : tem = simplify_binary_operation (code, mode, XEXP (op0, 1), op1);
2485 1009083 : if (tem != 0)
2486 70128 : return simplify_gen_binary (code, mode, XEXP (op0, 0), tem);
2487 :
2488 : /* Attempt to simplify "(a op b) op c" as "(a op c) op b". */
2489 938955 : tem = simplify_binary_operation (code, mode, XEXP (op0, 0), op1);
2490 938955 : if (tem != 0)
2491 31672 : return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2492 : }
2493 :
2494 : return 0;
2495 : }
2496 :
2497 : /* A comparison mask has one bit per outcome for which the comparison
2498 : holds. */
2499 : enum comparison_mask
2500 : {
2501 : CMP_BIT_UN = 1,
2502 : CMP_BIT_EQ = 2,
2503 : CMP_BIT_GT = 4,
2504 : CMP_BIT_LT = 8,
2505 : CMP_MASK_ORDERED = CMP_BIT_LT | CMP_BIT_GT | CMP_BIT_EQ,
2506 : CMP_MASK_ALL = CMP_MASK_ORDERED | CMP_BIT_UN
2507 : };
2508 :
2509 : /* If COMPARISON can be treated as an unsigned comparison, return a mask
2510 : that represents it. Return 0 otherwise. */
2511 : static int
2512 473960 : unsigned_comparison_to_mask (rtx_code comparison)
2513 : {
2514 0 : switch (comparison)
2515 : {
2516 : case LTU:
2517 : return CMP_BIT_LT;
2518 : case GTU:
2519 : return CMP_BIT_GT;
2520 : case EQ:
2521 : return CMP_BIT_EQ;
2522 :
2523 : case LEU:
2524 : return CMP_BIT_LT | CMP_BIT_EQ;
2525 : case GEU:
2526 : return CMP_BIT_GT | CMP_BIT_EQ;
2527 :
2528 : case NE:
2529 : return CMP_BIT_LT | CMP_BIT_GT;
2530 :
2531 : default:
2532 : return 0;
2533 : }
2534 : }
2535 :
2536 : /* Reverse the mapping in unsigned_comparison_to_mask, going from masks
2537 : to comparisons. */
2538 : static rtx_code
2539 6541 : mask_to_unsigned_comparison (int mask)
2540 : {
2541 6541 : switch (mask)
2542 : {
2543 : case CMP_BIT_LT:
2544 : return LTU;
2545 160 : case CMP_BIT_GT:
2546 160 : return GTU;
2547 2384 : case CMP_BIT_EQ:
2548 2384 : return EQ;
2549 :
2550 160 : case CMP_BIT_LT | CMP_BIT_EQ:
2551 160 : return LEU;
2552 160 : case CMP_BIT_GT | CMP_BIT_EQ:
2553 160 : return GEU;
2554 :
2555 3517 : case CMP_BIT_LT | CMP_BIT_GT:
2556 3517 : return NE;
2557 :
2558 0 : default:
2559 0 : gcc_unreachable ();
2560 : }
2561 : }
2562 :
2563 : /* Return true if CODE is LTU, GTU, LEU or GEU. */
2564 : static bool
2565 103339 : unsigned_inequality_p (rtx_code code)
2566 : {
2567 0 : switch (code)
2568 : {
2569 : case LTU:
2570 : case GTU:
2571 : case LEU:
2572 : case GEU:
2573 : return true;
2574 :
2575 95294 : default:
2576 0 : return false;
2577 : }
2578 : }
2579 :
2580 : /* Return a mask describing the COMPARISON. */
2581 : static int
2582 441022 : comparison_to_mask (enum rtx_code comparison)
2583 : {
2584 441022 : switch (comparison)
2585 : {
2586 : case LT:
2587 : return CMP_BIT_LT;
2588 38800 : case GT:
2589 38800 : return CMP_BIT_GT;
2590 8155 : case EQ:
2591 8155 : return CMP_BIT_EQ;
2592 29411 : case UNORDERED:
2593 29411 : return CMP_BIT_UN;
2594 :
2595 28008 : case LTGT:
2596 28008 : return CMP_BIT_LT | CMP_BIT_GT;
2597 39800 : case LE:
2598 39800 : return CMP_BIT_LT | CMP_BIT_EQ;
2599 39808 : case GE:
2600 39808 : return CMP_BIT_GT | CMP_BIT_EQ;
2601 28664 : case UNLT:
2602 28664 : return CMP_BIT_UN | CMP_BIT_LT;
2603 28632 : case UNGT:
2604 28632 : return CMP_BIT_UN | CMP_BIT_GT;
2605 29092 : case UNEQ:
2606 29092 : return CMP_BIT_UN | CMP_BIT_EQ;
2607 :
2608 29876 : case ORDERED:
2609 29876 : return CMP_MASK_ORDERED;
2610 41948 : case NE:
2611 41948 : return CMP_BIT_UN | CMP_BIT_LT | CMP_BIT_GT;
2612 30000 : case UNLE:
2613 30000 : return CMP_BIT_UN | CMP_BIT_LT | CMP_BIT_EQ;
2614 30004 : case UNGE:
2615 30004 : return CMP_BIT_UN | CMP_BIT_GT | CMP_BIT_EQ;
2616 :
2617 0 : default:
2618 0 : gcc_unreachable ();
2619 : }
2620 : }
2621 :
2622 : /* Return a comparison corresponding to the MASK. */
2623 : static enum rtx_code
2624 28247 : mask_to_comparison (int mask)
2625 : {
2626 28247 : switch (mask)
2627 : {
2628 : case CMP_BIT_LT:
2629 : return LT;
2630 : case CMP_BIT_GT:
2631 : return GT;
2632 : case CMP_BIT_EQ:
2633 : return EQ;
2634 : case CMP_BIT_UN:
2635 : return UNORDERED;
2636 :
2637 : case CMP_BIT_LT | CMP_BIT_GT:
2638 : return LTGT;
2639 : case CMP_BIT_LT | CMP_BIT_EQ:
2640 : return LE;
2641 : case CMP_BIT_GT | CMP_BIT_EQ:
2642 : return GE;
2643 : case CMP_BIT_UN | CMP_BIT_LT:
2644 : return UNLT;
2645 : case CMP_BIT_UN | CMP_BIT_GT:
2646 : return UNGT;
2647 : case CMP_BIT_UN | CMP_BIT_EQ:
2648 : return UNEQ;
2649 :
2650 : case CMP_MASK_ORDERED:
2651 : return ORDERED;
2652 : case CMP_BIT_UN | CMP_BIT_LT | CMP_BIT_GT:
2653 : return NE;
2654 : case CMP_BIT_UN | CMP_BIT_LT | CMP_BIT_EQ:
2655 : return UNLE;
2656 : case CMP_BIT_UN | CMP_BIT_GT | CMP_BIT_EQ:
2657 : return UNGE;
2658 :
2659 0 : default:
2660 0 : gcc_unreachable ();
2661 : }
2662 : }
2663 :
2664 : /* Return true if the comparison with mask MASK (see comparison_to_mask)
2665 : raises an invalid-operation exception for a quiet NaN operand, as LT,
2666 : GT, LE, GE and LTGT do: they are false for unordered operands and
2667 : depend on their order. EQ and ORDERED do not, and neither do the
2668 : constant results 0 and CMP_MASK_ALL. */
2669 :
2670 : static bool
2671 19904 : comparison_mask_signals_p (int mask)
2672 : {
2673 19904 : return ((mask & CMP_BIT_UN) == 0
2674 9436 : && (mask & (CMP_BIT_LT | CMP_BIT_GT)) != 0
2675 7608 : && mask != CMP_MASK_ORDERED);
2676 : }
2677 :
2678 : /* Canonicalize RES, a scalar const0_rtx/const_true_rtx to the right
2679 : false/true value of comparison with MODE where comparison operands
2680 : have CMP_MODE. */
2681 :
2682 : static rtx
2683 806218 : relational_result (machine_mode mode, machine_mode cmp_mode, rtx res)
2684 : {
2685 806218 : if (SCALAR_FLOAT_MODE_P (mode))
2686 : {
2687 168 : if (res == const0_rtx)
2688 164 : return CONST0_RTX (mode);
2689 : #ifdef FLOAT_STORE_FLAG_VALUE
2690 : REAL_VALUE_TYPE val = FLOAT_STORE_FLAG_VALUE (mode);
2691 : return const_double_from_real_value (val, mode);
2692 : #else
2693 : return NULL_RTX;
2694 : #endif
2695 : }
2696 806050 : if (VECTOR_MODE_P (mode))
2697 : {
2698 446 : if (res == const0_rtx)
2699 81 : return CONST0_RTX (mode);
2700 : #ifdef VECTOR_STORE_FLAG_VALUE
2701 365 : rtx val = VECTOR_STORE_FLAG_VALUE (mode);
2702 358 : if (val == NULL_RTX)
2703 : return NULL_RTX;
2704 358 : if (val == const1_rtx)
2705 0 : return CONST1_RTX (mode);
2706 :
2707 358 : return gen_const_vec_duplicate (mode, val);
2708 : #else
2709 : return NULL_RTX;
2710 : #endif
2711 : }
2712 : /* For vector comparison with scalar int result, it is unknown
2713 : if the target means here a comparison into an integral bitmask,
2714 : or comparison where all comparisons true mean const_true_rtx
2715 : whole result, or where any comparisons true mean const_true_rtx
2716 : whole result. For const0_rtx all the cases are the same. */
2717 805604 : if (VECTOR_MODE_P (cmp_mode)
2718 0 : && SCALAR_INT_MODE_P (mode)
2719 0 : && res == const_true_rtx)
2720 0 : return NULL_RTX;
2721 :
2722 : return res;
2723 : }
2724 :
2725 : /* Simplify a logical operation CODE with result mode MODE, operating on OP0
2726 : and OP1, in the case where both are relational operations. Assume that
2727 : OP0 is inverted if INVERT0_P is true.
2728 :
2729 : Return 0 if no such simplification is possible. */
2730 : rtx
2731 13746731 : simplify_context::simplify_logical_relational_operation (rtx_code code,
2732 : machine_mode mode,
2733 : rtx op0, rtx op1,
2734 : bool invert0_p)
2735 : {
2736 13746731 : if (!(COMPARISON_P (op0) && COMPARISON_P (op1)))
2737 : return 0;
2738 :
2739 113451 : if (!(rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2740 55656 : && rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))))
2741 : return 0;
2742 :
2743 55212 : if (side_effects_p (op0))
2744 : return 0;
2745 :
2746 55212 : enum rtx_code code0 = GET_CODE (op0);
2747 55212 : enum rtx_code code1 = GET_CODE (op1);
2748 55212 : machine_mode cmp_mode = GET_MODE (XEXP (op0, 0));
2749 55212 : if (cmp_mode == VOIDmode)
2750 0 : cmp_mode = GET_MODE (XEXP (op0, 1));
2751 :
2752 : /* Assume at first that the comparisons are on integers, and that the
2753 : operands are therefore ordered. */
2754 55212 : int all = CMP_MASK_ORDERED;
2755 55212 : int mask0 = unsigned_comparison_to_mask (code0);
2756 55212 : int mask1 = unsigned_comparison_to_mask (code1);
2757 55212 : bool unsigned_p = (unsigned_inequality_p (code0)
2758 55212 : || unsigned_inequality_p (code1));
2759 47167 : if (unsigned_p)
2760 : {
2761 : /* We only reach here when comparing integers. Reject mixtures of signed
2762 : and unsigned comparisons. */
2763 8045 : if (mask0 == 0 || mask1 == 0)
2764 : return 0;
2765 : }
2766 : else
2767 : {
2768 : /* See whether the operands might be unordered. Assume that all
2769 : results are possible for CC modes, and punt later if we don't get an
2770 : always-true or always-false answer. */
2771 47167 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC || HONOR_NANS (cmp_mode))
2772 : all = CMP_MASK_ALL;
2773 47167 : mask0 = comparison_to_mask (code0) & all;
2774 47167 : mask1 = comparison_to_mask (code1) & all;
2775 : }
2776 :
2777 53932 : if (invert0_p)
2778 37136 : mask0 = mask0 ^ all;
2779 :
2780 53932 : int mask;
2781 53932 : if (code == AND)
2782 14064 : mask = mask0 & mask1;
2783 39868 : else if (code == IOR)
2784 20647 : mask = mask0 | mask1;
2785 19221 : else if (code == XOR)
2786 19221 : mask = mask0 ^ mask1;
2787 : else
2788 : return 0;
2789 :
2790 : /* With trapping math, the result must signal for a quiet NaN exactly
2791 : when OP0 or OP1 does. Inverting OP0 does not change that. */
2792 53932 : if (HONOR_NANS (cmp_mode)
2793 10279 : && flag_trapping_math
2794 59092 : && ((comparison_mask_signals_p (comparison_to_mask (code0))
2795 6880 : || comparison_mask_signals_p (comparison_to_mask (code1)))
2796 5160 : != comparison_mask_signals_p (mask)))
2797 : return 0;
2798 :
2799 51204 : if (mask == all)
2800 7248 : return relational_result (mode, GET_MODE (op0), const_true_rtx);
2801 :
2802 43956 : if (mask == 0)
2803 4956 : return relational_result (mode, GET_MODE (op0), const0_rtx);
2804 :
2805 39000 : if (unsigned_p)
2806 6541 : code = mask_to_unsigned_comparison (mask);
2807 : else
2808 : {
2809 32459 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC)
2810 : return 0;
2811 :
2812 28247 : code = mask_to_comparison (mask);
2813 : /* LTGT and NE are arithmetically equivalent for ordered operands,
2814 : with NE being the canonical choice. */
2815 28247 : if (code == LTGT && all == CMP_MASK_ORDERED)
2816 3956 : code = NE;
2817 : }
2818 :
2819 34788 : op0 = XEXP (op1, 0);
2820 34788 : op1 = XEXP (op1, 1);
2821 :
2822 34788 : return simplify_gen_relational (code, mode, VOIDmode, op0, op1);
2823 : }
2824 :
2825 : /* We are going to IOR together OP0/OP1. If there is a common term in OP0/OP1
2826 : then we may be able to simplify the expression. We're primarily trying to
2827 : simplify down to IOR/XOR expression right now, but there may be other
2828 : simplifications we can do in the future.
2829 :
2830 : Return the simplified expression or NULL_RTX if no simplification was
2831 : possible. */
2832 : rtx
2833 27893682 : simplify_context::simplify_ior_with_common_term (machine_mode mode, rtx op0, rtx op1)
2834 : {
2835 : /* (ior X (plus/xor X C)) can be simplified into (ior X C) when
2836 : X and C have no bits in common. */
2837 27893682 : if ((GET_CODE (op1) == PLUS || GET_CODE (op1) == XOR)
2838 262327 : && rtx_equal_p (op0, XEXP (op1, 0))
2839 8336 : && ((nonzero_bits (op0, GET_MODE (op0))
2840 8336 : & nonzero_bits (XEXP (op1, 1), GET_MODE (op1))) == 0)
2841 27893682 : && !side_effects_p (op1))
2842 0 : return simplify_gen_binary (IOR, mode, op0, XEXP (op1, 1));
2843 :
2844 : /* (ior (and A C1) (and (not A) C2)) can be converted
2845 : into (and (xor A C2) (C1 + C2)) when there are no bits
2846 : in common between C1 and C2. */
2847 27893682 : if (GET_CODE (op0) == AND
2848 4446457 : && GET_CODE (op1) == AND
2849 428490 : && GET_CODE (XEXP (op1, 0)) == NOT
2850 19823 : && rtx_equal_p (XEXP (op0, 0), XEXP (XEXP (op1, 0), 0))
2851 3381 : && CONST_INT_P (XEXP (op0, 1))
2852 90 : && CONST_INT_P (XEXP (op1, 1))
2853 27893772 : && (INTVAL (XEXP (op0, 1)) & INTVAL (XEXP (op1, 1))) == 0)
2854 : {
2855 90 : rtx c = GEN_INT (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1)));
2856 :
2857 90 : rtx tem = simplify_gen_binary (XOR, mode, XEXP (op0, 0), XEXP (op1, 1));
2858 90 : if (tem)
2859 : {
2860 90 : tem = simplify_gen_binary (AND, mode, tem, c);
2861 :
2862 90 : if (tem)
2863 : return tem;
2864 : }
2865 : }
2866 :
2867 : /* Another variant seen on some target particularly those with
2868 : sub-word operations.
2869 :
2870 : (ior (and A C1) (plus (and A C2) C2)) can be simplified into
2871 : (and (xor (A C2) (C1 + C2).
2872 :
2873 : Where C2 is the sign bit for A's mode. So 0x80 for QI,
2874 : 0x8000 for HI, etc. In this case we know there is no carry
2875 : from the PLUS into relevant bits of the output. */
2876 27893592 : if (GET_CODE (op0) == AND
2877 4446367 : && GET_CODE (op1) == PLUS
2878 7826 : && GET_CODE (XEXP (op1, 0)) == AND
2879 330 : && rtx_equal_p (XEXP (op0, 0), XEXP (XEXP (op1, 0), 0))
2880 144 : && CONST_INT_P (XEXP (op0, 1))
2881 144 : && CONST_INT_P (XEXP (op1, 1))
2882 144 : && CONST_INT_P (XEXP (XEXP (op1, 0), 1))
2883 144 : && INTVAL (XEXP (op1, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
2884 36 : && GET_MODE_BITSIZE (GET_MODE (op1)).is_constant ()
2885 72 : && ((INTVAL (XEXP (op1, 1)) & GET_MODE_MASK (GET_MODE (op1)))
2886 72 : == HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (GET_MODE (op1)).to_constant () - 1))
2887 27893592 : && (INTVAL (XEXP (op0, 1)) & INTVAL (XEXP (op1, 1))) == 0)
2888 : {
2889 0 : rtx c = GEN_INT (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1)));
2890 :
2891 0 : rtx tem = simplify_gen_binary (XOR, mode, XEXP (op0, 0), XEXP (op1, 1));
2892 0 : if (tem)
2893 : {
2894 0 : tem = simplify_gen_binary (AND, mode, tem, c);
2895 0 : if (tem)
2896 : return tem;
2897 : }
2898 : }
2899 : return NULL_RTX;
2900 : }
2901 :
2902 :
2903 : /* Simplify a binary operation CODE with result mode MODE, operating on OP0
2904 : and OP1. Return 0 if no simplification is possible.
2905 :
2906 : Don't use this for relational operations such as EQ or LT.
2907 : Use simplify_relational_operation instead. */
2908 : rtx
2909 496978204 : simplify_context::simplify_binary_operation (rtx_code code, machine_mode mode,
2910 : rtx op0, rtx op1)
2911 : {
2912 496978204 : rtx trueop0, trueop1;
2913 496978204 : rtx tem;
2914 :
2915 : /* Relational operations don't work here. We must know the mode
2916 : of the operands in order to do the comparison correctly.
2917 : Assuming a full word can give incorrect results.
2918 : Consider comparing 128 with -128 in QImode. */
2919 496978204 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMPARE);
2920 496978204 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMM_COMPARE);
2921 :
2922 : /* Make sure the constant is second. */
2923 496978204 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
2924 496978204 : && swap_commutative_operands_p (op0, op1))
2925 : std::swap (op0, op1);
2926 :
2927 496978204 : trueop0 = avoid_constant_pool_reference (op0);
2928 496978204 : trueop1 = avoid_constant_pool_reference (op1);
2929 :
2930 496978204 : tem = simplify_const_binary_operation (code, mode, trueop0, trueop1);
2931 496978204 : if (tem)
2932 : return tem;
2933 466533967 : tem = simplify_binary_operation_1 (code, mode, op0, op1, trueop0, trueop1);
2934 :
2935 466533967 : if (tem)
2936 : return tem;
2937 :
2938 : /* If the above steps did not result in a simplification and op0 or op1
2939 : were constant pool references, use the referenced constants directly. */
2940 401382904 : if (trueop0 != op0 || trueop1 != op1)
2941 586417 : return simplify_gen_binary (code, mode, trueop0, trueop1);
2942 :
2943 : return NULL_RTX;
2944 : }
2945 :
2946 : /* Subroutine of simplify_binary_operation_1 that looks for cases in
2947 : which OP0 and OP1 are both vector series or vector duplicates
2948 : (which are really just series with a step of 0). If so, try to
2949 : form a new series by applying CODE to the bases and to the steps.
2950 : Return null if no simplification is possible.
2951 :
2952 : MODE is the mode of the operation and is known to be a vector
2953 : integer mode. */
2954 :
2955 : rtx
2956 2637539 : simplify_context::simplify_binary_operation_series (rtx_code code,
2957 : machine_mode mode,
2958 : rtx op0, rtx op1)
2959 : {
2960 2637539 : rtx base0, step0;
2961 2637539 : if (vec_duplicate_p (op0, &base0))
2962 87023 : step0 = const0_rtx;
2963 2550516 : else if (!vec_series_p (op0, &base0, &step0))
2964 : return NULL_RTX;
2965 :
2966 87818 : rtx base1, step1;
2967 87818 : if (vec_duplicate_p (op1, &base1))
2968 426 : step1 = const0_rtx;
2969 87392 : else if (!vec_series_p (op1, &base1, &step1))
2970 : return NULL_RTX;
2971 :
2972 : /* Only create a new series if we can simplify both parts. In other
2973 : cases this isn't really a simplification, and it's not necessarily
2974 : a win to replace a vector operation with a scalar operation. */
2975 6454 : scalar_mode inner_mode = GET_MODE_INNER (mode);
2976 6454 : rtx new_base = simplify_binary_operation (code, inner_mode, base0, base1);
2977 6454 : if (!new_base)
2978 : return NULL_RTX;
2979 :
2980 5440 : rtx new_step = simplify_binary_operation (code, inner_mode, step0, step1);
2981 5440 : if (!new_step)
2982 : return NULL_RTX;
2983 :
2984 5440 : return gen_vec_series (mode, new_base, new_step);
2985 : }
2986 :
2987 : /* Subroutine of simplify_binary_operation_1. Un-distribute a binary
2988 : operation CODE with result mode MODE, operating on OP0 and OP1.
2989 : e.g. simplify (xor (and A C) (and (B C)) to (and (xor (A B) C).
2990 : Returns NULL_RTX if no simplification is possible. */
2991 :
2992 : rtx
2993 1455415 : simplify_context::simplify_distributive_operation (rtx_code code,
2994 : machine_mode mode,
2995 : rtx op0, rtx op1)
2996 : {
2997 1455415 : enum rtx_code op = GET_CODE (op0);
2998 1455415 : gcc_assert (GET_CODE (op1) == op);
2999 :
3000 1455415 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))
3001 1455415 : && ! side_effects_p (XEXP (op0, 1)))
3002 365817 : return simplify_gen_binary (op, mode,
3003 : simplify_gen_binary (code, mode,
3004 : XEXP (op0, 0),
3005 : XEXP (op1, 0)),
3006 365817 : XEXP (op0, 1));
3007 :
3008 1089598 : if (GET_RTX_CLASS (op) == RTX_COMM_ARITH)
3009 : {
3010 1070737 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3011 1070737 : && ! side_effects_p (XEXP (op0, 0)))
3012 531262 : return simplify_gen_binary (op, mode,
3013 : simplify_gen_binary (code, mode,
3014 : XEXP (op0, 1),
3015 : XEXP (op1, 1)),
3016 531262 : XEXP (op0, 0));
3017 539475 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 1))
3018 539475 : && ! side_effects_p (XEXP (op0, 0)))
3019 61 : return simplify_gen_binary (op, mode,
3020 : simplify_gen_binary (code, mode,
3021 : XEXP (op0, 1),
3022 : XEXP (op1, 0)),
3023 61 : XEXP (op0, 0));
3024 539414 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 0))
3025 539414 : && ! side_effects_p (XEXP (op0, 1)))
3026 269469 : return simplify_gen_binary (op, mode,
3027 : simplify_gen_binary (code, mode,
3028 : XEXP (op0, 0),
3029 : XEXP (op1, 1)),
3030 269469 : XEXP (op0, 1));
3031 : }
3032 :
3033 : return NULL_RTX;
3034 : }
3035 :
3036 : /* Return TRUE if a rotate in mode MODE with a constant count in OP1
3037 : should be reversed.
3038 :
3039 : If the rotate should not be reversed, return FALSE.
3040 :
3041 : LEFT indicates if this is a rotate left or a rotate right. */
3042 :
3043 : bool
3044 150852 : reverse_rotate_by_imm_p (machine_mode mode, unsigned int left, rtx op1)
3045 : {
3046 150852 : if (!CONST_INT_P (op1))
3047 : return false;
3048 :
3049 : /* Some targets may only be able to rotate by a constant
3050 : in one direction. So we need to query the optab interface
3051 : to see what is possible. */
3052 112371 : optab binoptab = left ? rotl_optab : rotr_optab;
3053 48316 : optab re_binoptab = left ? rotr_optab : rotl_optab;
3054 112371 : enum insn_code icode = optab_handler (binoptab, mode);
3055 112371 : enum insn_code re_icode = optab_handler (re_binoptab, mode);
3056 :
3057 : /* If the target can not support the reversed optab, then there
3058 : is nothing to do. */
3059 112371 : if (re_icode == CODE_FOR_nothing)
3060 : return false;
3061 :
3062 : /* If the target does not support the requested rotate-by-immediate,
3063 : then we want to try reversing the rotate. We also want to try
3064 : reversing to minimize the count. */
3065 109889 : if ((icode == CODE_FOR_nothing)
3066 109889 : || (!insn_operand_matches (icode, 2, op1))
3067 549445 : || (IN_RANGE (INTVAL (op1),
3068 : GET_MODE_UNIT_PRECISION (mode) / 2 + left,
3069 : GET_MODE_UNIT_PRECISION (mode) - 1)))
3070 15465 : return (insn_operand_matches (re_icode, 2, op1));
3071 : return false;
3072 : }
3073 :
3074 : /* Analyse argument X to see if it represents an (ASHIFT X Y) operation
3075 : and return the expression to be shifted in SHIFT_OPND and the shift amount
3076 : in SHIFT_AMNT. This is primarily used to group handling of ASHIFT (X, CST)
3077 : and (PLUS (X, X)) in one place. If the expression is not equivalent to an
3078 : ASHIFT then return FALSE and set SHIFT_OPND and SHIFT_AMNT to NULL. */
3079 :
3080 : static bool
3081 545812749 : extract_ashift_operands_p (rtx x, rtx *shift_opnd, rtx *shift_amnt)
3082 : {
3083 545812749 : if (GET_CODE (x) == ASHIFT)
3084 : {
3085 14237431 : *shift_opnd = XEXP (x, 0);
3086 14237431 : *shift_amnt = XEXP (x, 1);
3087 14237431 : return true;
3088 : }
3089 531575318 : if (GET_CODE (x) == PLUS && rtx_equal_p (XEXP (x, 0), XEXP (x, 1)))
3090 : {
3091 11878 : *shift_opnd = XEXP (x, 0);
3092 11878 : *shift_amnt = CONST1_RTX (GET_MODE (x));
3093 11878 : return true;
3094 : }
3095 531563440 : *shift_opnd = NULL_RTX;
3096 531563440 : *shift_amnt = NULL_RTX;
3097 531563440 : return false;
3098 : }
3099 :
3100 : /* OP0 and OP1 are combined under an operation of mode MODE that can
3101 : potentially result in a ROTATE expression. Analyze the OP0 and OP1
3102 : and return the resulting ROTATE expression if so. Return NULL otherwise.
3103 : This is used in detecting the patterns (X << C1) [+,|,^] (X >> C2) where
3104 : C1 + C2 == GET_MODE_UNIT_PRECISION (mode).
3105 : (X << C1) and (C >> C2) would be OP0 and OP1. */
3106 :
3107 : static rtx
3108 275862558 : simplify_rotate_op (rtx op0, rtx op1, machine_mode mode)
3109 : {
3110 : /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3111 : mode size to (rotate A CX). */
3112 :
3113 275862558 : rtx opleft = op0;
3114 275862558 : rtx opright = op1;
3115 275862558 : rtx ashift_opnd, ashift_amnt;
3116 : /* In some cases the ASHIFT is not a direct ASHIFT. Look deeper and extract
3117 : the relevant operands here. */
3118 275862558 : bool ashift_op_p
3119 275862558 : = extract_ashift_operands_p (op1, &ashift_opnd, &ashift_amnt);
3120 :
3121 275862558 : if (ashift_op_p
3122 274050441 : || GET_CODE (op1) == SUBREG)
3123 : {
3124 : opleft = op1;
3125 : opright = op0;
3126 : }
3127 : else
3128 : {
3129 269950191 : opright = op1;
3130 269950191 : opleft = op0;
3131 269950191 : ashift_op_p
3132 269950191 : = extract_ashift_operands_p (opleft, &ashift_opnd, &ashift_amnt);
3133 : }
3134 :
3135 14249309 : if (ashift_op_p && GET_CODE (opright) == LSHIFTRT
3136 274122475 : && rtx_equal_p (ashift_opnd, XEXP (opright, 0)))
3137 : {
3138 9682 : rtx leftcst = unwrap_const_vec_duplicate (ashift_amnt);
3139 9682 : rtx rightcst = unwrap_const_vec_duplicate (XEXP (opright, 1));
3140 :
3141 5850 : if (CONST_INT_P (leftcst) && CONST_INT_P (rightcst)
3142 15532 : && (INTVAL (leftcst) + INTVAL (rightcst)
3143 5850 : == GET_MODE_UNIT_PRECISION (mode)))
3144 5095 : return gen_rtx_ROTATE (mode, XEXP (opright, 0), ashift_amnt);
3145 : }
3146 :
3147 : /* Same, but for ashift that has been "simplified" to a wider mode
3148 : by simplify_shift_const. */
3149 275857463 : scalar_int_mode int_mode, inner_mode;
3150 :
3151 275857463 : if (GET_CODE (opleft) == SUBREG
3152 281702576 : && is_a <scalar_int_mode> (mode, &int_mode)
3153 5840018 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (opleft)),
3154 : &inner_mode)
3155 5800882 : && GET_CODE (SUBREG_REG (opleft)) == ASHIFT
3156 191237 : && GET_CODE (opright) == LSHIFTRT
3157 956 : && GET_CODE (XEXP (opright, 0)) == SUBREG
3158 234 : && known_eq (SUBREG_BYTE (opleft), SUBREG_BYTE (XEXP (opright, 0)))
3159 464 : && GET_MODE_SIZE (int_mode) < GET_MODE_SIZE (inner_mode)
3160 225 : && rtx_equal_p (XEXP (SUBREG_REG (opleft), 0),
3161 225 : SUBREG_REG (XEXP (opright, 0)))
3162 13 : && CONST_INT_P (XEXP (SUBREG_REG (opleft), 1))
3163 13 : && CONST_INT_P (XEXP (opright, 1))
3164 275857463 : && (INTVAL (XEXP (SUBREG_REG (opleft), 1))
3165 13 : + INTVAL (XEXP (opright, 1))
3166 13 : == GET_MODE_PRECISION (int_mode)))
3167 1 : return gen_rtx_ROTATE (int_mode, XEXP (opright, 0),
3168 : XEXP (SUBREG_REG (opleft), 1));
3169 : return NULL_RTX;
3170 : }
3171 :
3172 : /* Try to combine two unsigned comparisons against the same value, OP0 and
3173 : OP1, joined by CODE (IOR or AND), into one comparison of a UMIN/UMAX:
3174 :
3175 : (A <=u C) | (B <=u C) -> umin (A, B) <=u C
3176 : (A <=u C) & (B <=u C) -> umax (A, B) <=u C
3177 : (A >u C) | (B >u C) -> umax (A, B) >u C
3178 : (A >u C) & (B >u C) -> umin (A, B) >u C
3179 :
3180 : and likewise for LTU and GEU.
3181 :
3182 : Return the combined RTX or NULL_RTX otherwise. */
3183 :
3184 : static rtx
3185 38994164 : simplify_unsigned_cmp_minmax (rtx_code code, machine_mode mode,
3186 : rtx op0, rtx op1)
3187 : {
3188 38994164 : rtx_code cmp = GET_CODE (op0);
3189 38994164 : if (STORE_FLAG_VALUE != 1
3190 38994164 : || GET_CODE (op1) != cmp
3191 5582579 : || (cmp != LEU && cmp != LTU && cmp != GEU && cmp != GTU)
3192 11 : || !SCALAR_INT_MODE_P (mode)
3193 11 : || !rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))
3194 0 : || side_effects_p (op0)
3195 38994164 : || side_effects_p (op1))
3196 : return NULL_RTX;
3197 :
3198 0 : machine_mode cmp_mode = GET_MODE (XEXP (op0, 0));
3199 0 : if (!SCALAR_INT_MODE_P (cmp_mode) || GET_MODE (XEXP (op1, 0)) != cmp_mode)
3200 : return NULL_RTX;
3201 :
3202 0 : bool less = cmp == LEU || cmp == LTU;
3203 0 : rtx_code minmax = (less == (code == IOR)) ? UMIN : UMAX;
3204 0 : rtx m = simplify_gen_binary (minmax, cmp_mode,
3205 : XEXP (op0, 0), XEXP (op1, 0));
3206 0 : return simplify_gen_relational (cmp, mode, cmp_mode, m, XEXP (op0, 1));
3207 : }
3208 :
3209 : /* Returns true if OP0 and OP1 match the pattern (OP (plus (A - 1)) (neg A)),
3210 : and the pattern can be simplified (there are no side effects). */
3211 :
3212 : static bool
3213 40929083 : match_plus_neg_pattern (rtx op0, rtx op1, machine_mode mode)
3214 : {
3215 : /* Remove SUBREG from OP0 and OP1, if needed. */
3216 40929083 : if (GET_CODE (op0) == SUBREG
3217 7532981 : && GET_CODE (op1) == SUBREG
3218 309536 : && subreg_lowpart_p (op0)
3219 41237999 : && subreg_lowpart_p (op1))
3220 : {
3221 308899 : op0 = XEXP (op0, 0);
3222 308899 : op1 = XEXP (op1, 0);
3223 : }
3224 :
3225 : /* Check for the pattern (OP (plus (A - 1)) (neg A)). */
3226 40929083 : if (((GET_CODE (op1) == NEG
3227 3816 : && GET_CODE (op0) == PLUS
3228 2135 : && XEXP (op0, 1) == CONSTM1_RTX (mode))
3229 40928390 : || (GET_CODE (op0) == NEG
3230 78925 : && GET_CODE (op1) == PLUS
3231 0 : && XEXP (op1, 1) == CONSTM1_RTX (mode)))
3232 693 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3233 40929085 : && !side_effects_p (XEXP (op0, 0)))
3234 2 : return true;
3235 : return false;
3236 : }
3237 :
3238 : /* Check if OP matches the pattern of (subreg (not X)) and the subreg is
3239 : non-paradoxical. */
3240 :
3241 : static bool
3242 77844827 : non_paradoxical_subreg_not_p (rtx op)
3243 : {
3244 77844827 : return GET_CODE (op) == SUBREG
3245 9106991 : && !paradoxical_subreg_p (op)
3246 80680008 : && GET_CODE (SUBREG_REG (op)) == NOT;
3247 : }
3248 :
3249 : /* Convert (binop (subreg (not X)) Y) into (binop (not (subreg X)) Y), or
3250 : (binop X (subreg (not Y))) into (binop X (not (subreg Y))) to expose
3251 : opportunities to combine another binary logical operation with NOT. */
3252 :
3253 : static rtx
3254 38923656 : simplify_with_subreg_not (rtx_code binop, machine_mode mode, rtx op0, rtx op1)
3255 : {
3256 38923656 : rtx opn = NULL_RTX;
3257 38923656 : if (non_paradoxical_subreg_not_p (op0))
3258 : opn = op0;
3259 38921171 : else if (non_paradoxical_subreg_not_p (op1))
3260 : opn = op1;
3261 :
3262 2508 : if (opn == NULL_RTX)
3263 : return NULL_RTX;
3264 :
3265 5016 : rtx new_subreg = simplify_gen_subreg (mode,
3266 : XEXP (SUBREG_REG (opn), 0),
3267 2508 : GET_MODE (SUBREG_REG (opn)),
3268 2508 : SUBREG_BYTE (opn));
3269 :
3270 2508 : if (!new_subreg)
3271 : return NULL_RTX;
3272 :
3273 2456 : rtx new_not = simplify_gen_unary (NOT, mode, new_subreg, mode);
3274 2456 : if (opn == op0)
3275 2433 : return simplify_gen_binary (binop, mode, new_not, op1);
3276 : else
3277 23 : return simplify_gen_binary (binop, mode, op0, new_not);
3278 : }
3279 :
3280 : /* Return TRUE iff NOP is a negated form of OP, or vice-versa. */
3281 : static bool
3282 7354123 : negated_ops_p (rtx nop, rtx op)
3283 : {
3284 : /* Explicit negation. */
3285 7354123 : if (GET_CODE (nop) == NOT
3286 7354123 : && rtx_equal_p (XEXP (nop, 0), op))
3287 : return true;
3288 7350715 : if (GET_CODE (op) == NOT
3289 7350715 : && rtx_equal_p (XEXP (op, 0), nop))
3290 : return true;
3291 :
3292 : /* (~C <r A) is a negated form of (C << A) if C == 1. */
3293 7347488 : if (GET_CODE (op) == ASHIFT
3294 1528228 : && GET_CODE (nop) == ROTATE
3295 0 : && XEXP (op, 0) == CONST1_RTX (GET_MODE (op))
3296 0 : && CONST_INT_P (XEXP (nop, 0))
3297 0 : && INTVAL (XEXP (nop, 0)) == -2
3298 7347488 : && rtx_equal_p (XEXP (op, 1), XEXP (nop, 1)))
3299 : return true;
3300 7347488 : if (GET_CODE (nop) == ASHIFT
3301 155727 : && GET_CODE (op) == ROTATE
3302 0 : && XEXP (nop, 0) == CONST1_RTX (GET_MODE (op))
3303 0 : && CONST_INT_P (XEXP (nop, 0))
3304 0 : && INTVAL (XEXP (nop, 0)) == -2
3305 7347488 : && rtx_equal_p (XEXP (op, 1), XEXP (nop, 1)))
3306 : return true;
3307 :
3308 : /* ??? Should we consider rotations of C and ~C by the same amount? */
3309 :
3310 : return false;
3311 : }
3312 :
3313 : /* Subroutine of simplify_binary_operation. Simplify a binary operation
3314 : CODE with result mode MODE, operating on OP0 and OP1. If OP0 and/or
3315 : OP1 are constant pool references, TRUEOP0 and TRUEOP1 represent the
3316 : actual constants. */
3317 :
3318 : rtx
3319 466533967 : simplify_context::simplify_binary_operation_1 (rtx_code code,
3320 : machine_mode mode,
3321 : rtx op0, rtx op1,
3322 : rtx trueop0, rtx trueop1)
3323 : {
3324 466533967 : rtx tem, reversed, elt0, elt1;
3325 466533967 : HOST_WIDE_INT val;
3326 466533967 : scalar_int_mode int_mode, inner_mode;
3327 466533967 : poly_int64 offset;
3328 :
3329 : /* Even if we can't compute a constant result,
3330 : there are some cases worth simplifying. */
3331 :
3332 466533967 : switch (code)
3333 : {
3334 265486989 : case PLUS:
3335 : /* Maybe simplify x + 0 to x. The two expressions are equivalent
3336 : when x is NaN, infinite, or finite and nonzero. They aren't
3337 : when x is -0 and the rounding mode is not towards -infinity,
3338 : since (-0) + 0 is then 0. */
3339 527044161 : if (!HONOR_SIGNED_ZEROS (mode) && !HONOR_SNANS (mode)
3340 527044149 : && trueop1 == CONST0_RTX (mode))
3341 : return op0;
3342 :
3343 : /* ((-a) + b) -> (b - a) and similarly for (a + (-b)). These
3344 : transformations are safe even for IEEE. */
3345 263828030 : if (GET_CODE (op0) == NEG)
3346 70271 : return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
3347 263757759 : else if (GET_CODE (op1) == NEG)
3348 8816 : return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
3349 :
3350 : /* (~a) + 1 -> -a */
3351 263748943 : if (INTEGRAL_MODE_P (mode)
3352 258903633 : && GET_CODE (op0) == NOT
3353 1396473 : && trueop1 == const1_rtx)
3354 3840 : return simplify_gen_unary (NEG, mode, XEXP (op0, 0), mode);
3355 :
3356 : /* Handle both-operands-constant cases. We can only add
3357 : CONST_INTs to constants since the sum of relocatable symbols
3358 : can't be handled by most assemblers. Don't add CONST_INT
3359 : to CONST_INT since overflow won't be computed properly if wider
3360 : than HOST_BITS_PER_WIDE_INT. */
3361 :
3362 263745103 : if ((GET_CODE (op0) == CONST
3363 263745103 : || GET_CODE (op0) == SYMBOL_REF
3364 260962641 : || GET_CODE (op0) == LABEL_REF)
3365 263745103 : && poly_int_rtx_p (op1, &offset))
3366 2781477 : return plus_constant (mode, op0, offset);
3367 260963626 : else if ((GET_CODE (op1) == CONST
3368 260963626 : || GET_CODE (op1) == SYMBOL_REF
3369 256753465 : || GET_CODE (op1) == LABEL_REF)
3370 260963626 : && poly_int_rtx_p (op0, &offset))
3371 0 : return plus_constant (mode, op1, offset);
3372 :
3373 : /* See if this is something like X * C - X or vice versa or
3374 : if the multiplication is written as a shift. If so, we can
3375 : distribute and make a new multiply, shift, or maybe just
3376 : have X (if C is 2 in the example above). But don't make
3377 : something more expensive than we had before. */
3378 :
3379 260963626 : if (is_a <scalar_int_mode> (mode, &int_mode))
3380 : {
3381 253860660 : rtx lhs = op0, rhs = op1;
3382 :
3383 253860660 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3384 253860660 : wide_int coeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3385 :
3386 253860660 : if (GET_CODE (lhs) == NEG)
3387 : {
3388 0 : coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3389 0 : lhs = XEXP (lhs, 0);
3390 : }
3391 253860660 : else if (GET_CODE (lhs) == MULT
3392 9785080 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3393 : {
3394 8359397 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3395 8359397 : lhs = XEXP (lhs, 0);
3396 : }
3397 245501263 : else if (GET_CODE (lhs) == ASHIFT
3398 11268718 : && CONST_INT_P (XEXP (lhs, 1))
3399 11196065 : && INTVAL (XEXP (lhs, 1)) >= 0
3400 256697316 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3401 : {
3402 11196053 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3403 22392106 : GET_MODE_PRECISION (int_mode));
3404 11196053 : lhs = XEXP (lhs, 0);
3405 : }
3406 :
3407 253860660 : if (GET_CODE (rhs) == NEG)
3408 : {
3409 0 : coeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3410 0 : rhs = XEXP (rhs, 0);
3411 : }
3412 253860660 : else if (GET_CODE (rhs) == MULT
3413 375163 : && CONST_INT_P (XEXP (rhs, 1)))
3414 : {
3415 201820 : coeff1 = rtx_mode_t (XEXP (rhs, 1), int_mode);
3416 201820 : rhs = XEXP (rhs, 0);
3417 : }
3418 253658840 : else if (GET_CODE (rhs) == ASHIFT
3419 627169 : && CONST_INT_P (XEXP (rhs, 1))
3420 617374 : && INTVAL (XEXP (rhs, 1)) >= 0
3421 254276214 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3422 : {
3423 617374 : coeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3424 1234748 : GET_MODE_PRECISION (int_mode));
3425 617374 : rhs = XEXP (rhs, 0);
3426 : }
3427 :
3428 : /* Don't merge operands that have side effects. */
3429 253860660 : if (rtx_equal_p (lhs, rhs) && !side_effects_p (lhs))
3430 : {
3431 868724 : rtx orig = gen_rtx_PLUS (int_mode, op0, op1);
3432 868724 : rtx coeff;
3433 868724 : bool speed = optimize_function_for_speed_p (cfun);
3434 :
3435 868724 : coeff = immed_wide_int_const (coeff0 + coeff1, int_mode);
3436 :
3437 868724 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3438 868724 : return (set_src_cost (tem, int_mode, speed)
3439 868724 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3440 : }
3441 :
3442 : /* Optimize (X - 1) * Y + Y to X * Y. */
3443 252991936 : lhs = op0;
3444 252991936 : rhs = op1;
3445 252991936 : if (GET_CODE (op0) == MULT)
3446 : {
3447 9733189 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3448 646269 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx)
3449 9680698 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3450 54792 : && XEXP (XEXP (op0, 0), 1) == const1_rtx))
3451 9785680 : && rtx_equal_p (XEXP (op0, 1), op1))
3452 81 : lhs = XEXP (XEXP (op0, 0), 0);
3453 9733108 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3454 1765 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx)
3455 9733032 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3456 503 : && XEXP (XEXP (op0, 1), 1) == const1_rtx))
3457 9733184 : && rtx_equal_p (XEXP (op0, 0), op1))
3458 0 : lhs = XEXP (XEXP (op0, 1), 0);
3459 : }
3460 243258747 : else if (GET_CODE (op1) == MULT)
3461 : {
3462 131241 : if (((GET_CODE (XEXP (op1, 0)) == PLUS
3463 104 : && XEXP (XEXP (op1, 0), 1) == constm1_rtx)
3464 131233 : || (GET_CODE (XEXP (op1, 0)) == MINUS
3465 17 : && XEXP (XEXP (op1, 0), 1) == const1_rtx))
3466 131249 : && rtx_equal_p (XEXP (op1, 1), op0))
3467 0 : rhs = XEXP (XEXP (op1, 0), 0);
3468 131241 : else if (((GET_CODE (XEXP (op1, 1)) == PLUS
3469 45 : && XEXP (XEXP (op1, 1), 1) == constm1_rtx)
3470 131241 : || (GET_CODE (XEXP (op1, 1)) == MINUS
3471 4 : && XEXP (XEXP (op1, 1), 1) == const1_rtx))
3472 131241 : && rtx_equal_p (XEXP (op1, 0), op0))
3473 0 : rhs = XEXP (XEXP (op1, 1), 0);
3474 : }
3475 81 : if ((lhs != op0 && !side_effects_p (op1))
3476 252991937 : || (rhs != op1 && !side_effects_p (op0)))
3477 80 : return simplify_gen_binary (MULT, int_mode, lhs, rhs);
3478 253860660 : }
3479 :
3480 : /* (plus (xor X C1) C2) is (xor X (C1^C2)) if C2 is signbit. */
3481 260094822 : if (CONST_SCALAR_INT_P (op1)
3482 196928368 : && GET_CODE (op0) == XOR
3483 20151 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3484 260106044 : && mode_signbit_p (mode, op1))
3485 121 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3486 : simplify_gen_binary (XOR, mode, op1,
3487 121 : XEXP (op0, 1)));
3488 :
3489 : /* (plus (xor X C1) C2) is (xor X (C1^C2)) if X is either 0 or 1 and
3490 : 2 * ((X ^ C1) & C2) == 0; based on A + B == A ^ B + 2 * (A & B). */
3491 260094701 : if (CONST_SCALAR_INT_P (op1)
3492 196928247 : && GET_CODE (op0) == XOR
3493 20030 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3494 11101 : && nonzero_bits (XEXP (op0, 0), mode) == 1
3495 341 : && 2 * (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) == 0
3496 260094701 : && 2 * ((1 ^ INTVAL (XEXP (op0, 1))) & INTVAL (op1)) == 0)
3497 0 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3498 : simplify_gen_binary (XOR, mode, op1,
3499 0 : XEXP (op0, 1)));
3500 :
3501 : /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)). */
3502 260094701 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3503 260092233 : && GET_CODE (op0) == MULT
3504 270201708 : && GET_CODE (XEXP (op0, 0)) == NEG)
3505 : {
3506 4514 : rtx in1, in2;
3507 :
3508 4514 : in1 = XEXP (XEXP (op0, 0), 0);
3509 4514 : in2 = XEXP (op0, 1);
3510 4514 : return simplify_gen_binary (MINUS, mode, op1,
3511 : simplify_gen_binary (MULT, mode,
3512 4514 : in1, in2));
3513 : }
3514 :
3515 : /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3516 : C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3517 : is 1. */
3518 260090187 : if (COMPARISON_P (op0)
3519 1382403 : && ((STORE_FLAG_VALUE == -1 && trueop1 == const1_rtx)
3520 1382403 : || (STORE_FLAG_VALUE == 1 && trueop1 == constm1_rtx))
3521 260150526 : && (reversed = reversed_comparison (op0, mode)))
3522 59982 : return
3523 59982 : simplify_gen_unary (NEG, mode, reversed, mode);
3524 :
3525 : /* Convert (plus (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3526 : mode size to (rotate A CX). */
3527 260030205 : if ((tem = simplify_rotate_op (op0, op1, mode)))
3528 : return tem;
3529 :
3530 : /* If one of the operands is a PLUS or a MINUS, see if we can
3531 : simplify this by the associative law.
3532 : Don't use the associative law for floating point.
3533 : The inaccuracy makes it nonassociative,
3534 : and subtle programs can break if operations are associated. */
3535 :
3536 260028715 : if (INTEGRAL_MODE_P (mode)
3537 255183454 : && (plus_minus_operand_p (op0)
3538 220828377 : || plus_minus_operand_p (op1))
3539 35358609 : && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3540 : return tem;
3541 :
3542 : /* Reassociate floating point addition only when the user
3543 : specifies associative math operations. */
3544 225324832 : if (FLOAT_MODE_P (mode)
3545 4845261 : && flag_associative_math)
3546 : {
3547 915358 : tem = simplify_associative_operation (code, mode, op0, op1);
3548 915358 : if (tem)
3549 : return tem;
3550 : }
3551 :
3552 : /* Handle vector series. */
3553 225311090 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3554 : {
3555 2087843 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3556 2087843 : if (tem)
3557 : return tem;
3558 : }
3559 : break;
3560 :
3561 : case COMPARE:
3562 : break;
3563 :
3564 45596555 : case MINUS:
3565 : /* We can't assume x-x is 0 even with non-IEEE floating point.
3566 : This is unsafe if x is NaN or Inf, and with FE_DOWNWARD as the
3567 : rounding mode x - x may be -0.0. c.f. PR middle-end/98420. */
3568 45596555 : if (rtx_equal_p (trueop0, trueop1)
3569 217681 : && ! side_effects_p (op0)
3570 45813704 : && (!FLOAT_MODE_P (mode)
3571 3359 : || (!HONOR_NANS (mode)
3572 19 : && !HONOR_INFINITIES (mode)
3573 19 : && (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3574 6 : || !HONOR_SIGNED_ZEROS (mode)))))
3575 213803 : return CONST0_RTX (mode);
3576 :
3577 : /* Change subtraction from zero into negation. (0 - x) is the
3578 : same as -x when x is NaN, infinite, or finite and nonzero.
3579 : But if the mode has signed zeros, and does not round towards
3580 : -infinity, then 0 - 0 is 0, not -0. */
3581 45382752 : if (!HONOR_SIGNED_ZEROS (mode) && trueop0 == CONST0_RTX (mode))
3582 372332 : return simplify_gen_unary (NEG, mode, op1, mode);
3583 :
3584 : /* (-1 - a) is ~a, unless the expression contains symbolic
3585 : constants, in which case not retaining additions and
3586 : subtractions could cause invalid assembly to be produced. */
3587 45010420 : if (trueop0 == CONSTM1_RTX (mode)
3588 45010420 : && !contains_symbolic_reference_p (op1))
3589 618298 : return simplify_gen_unary (NOT, mode, op1, mode);
3590 :
3591 : /* Subtracting 0 has no effect unless the mode has signalling NaNs,
3592 : or has signed zeros and supports rounding towards -infinity.
3593 : In such a case, 0 - 0 is -0. */
3594 45135451 : if (!(HONOR_SIGNED_ZEROS (mode)
3595 743329 : && HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3596 44390879 : && !HONOR_SNANS (mode)
3597 88782965 : && trueop1 == CONST0_RTX (mode))
3598 : return op0;
3599 :
3600 : /* See if this is something like X * C - X or vice versa or
3601 : if the multiplication is written as a shift. If so, we can
3602 : distribute and make a new multiply, shift, or maybe just
3603 : have X (if C is 2 in the example above). But don't make
3604 : something more expensive than we had before. */
3605 :
3606 43448878 : if (is_a <scalar_int_mode> (mode, &int_mode))
3607 : {
3608 42009374 : rtx lhs = op0, rhs = op1;
3609 :
3610 42009374 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3611 42009374 : wide_int negcoeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3612 :
3613 42009374 : if (GET_CODE (lhs) == NEG)
3614 : {
3615 75506 : coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3616 75506 : lhs = XEXP (lhs, 0);
3617 : }
3618 41933868 : else if (GET_CODE (lhs) == MULT
3619 238794 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3620 : {
3621 83037 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3622 83037 : lhs = XEXP (lhs, 0);
3623 : }
3624 41850831 : else if (GET_CODE (lhs) == ASHIFT
3625 331886 : && CONST_INT_P (XEXP (lhs, 1))
3626 328620 : && INTVAL (XEXP (lhs, 1)) >= 0
3627 42179430 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3628 : {
3629 328599 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3630 657198 : GET_MODE_PRECISION (int_mode));
3631 328599 : lhs = XEXP (lhs, 0);
3632 : }
3633 :
3634 42009374 : if (GET_CODE (rhs) == NEG)
3635 : {
3636 13647 : negcoeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3637 13647 : rhs = XEXP (rhs, 0);
3638 : }
3639 41995727 : else if (GET_CODE (rhs) == MULT
3640 144789 : && CONST_INT_P (XEXP (rhs, 1)))
3641 : {
3642 105579 : negcoeff1 = wi::neg (rtx_mode_t (XEXP (rhs, 1), int_mode));
3643 105579 : rhs = XEXP (rhs, 0);
3644 : }
3645 41890148 : else if (GET_CODE (rhs) == ASHIFT
3646 415815 : && CONST_INT_P (XEXP (rhs, 1))
3647 415267 : && INTVAL (XEXP (rhs, 1)) >= 0
3648 42305415 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3649 : {
3650 415267 : negcoeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3651 830534 : GET_MODE_PRECISION (int_mode));
3652 415267 : negcoeff1 = -negcoeff1;
3653 415267 : rhs = XEXP (rhs, 0);
3654 : }
3655 :
3656 42009374 : if (rtx_equal_p (lhs, rhs) && !side_effects_p (lhs))
3657 : {
3658 98352 : rtx orig = gen_rtx_MINUS (int_mode, op0, op1);
3659 98352 : rtx coeff;
3660 98352 : bool speed = optimize_function_for_speed_p (cfun);
3661 :
3662 98352 : coeff = immed_wide_int_const (coeff0 + negcoeff1, int_mode);
3663 :
3664 98352 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3665 98352 : return (set_src_cost (tem, int_mode, speed)
3666 98352 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3667 : }
3668 :
3669 : /* Optimize (X + 1) * Y - Y to X * Y. */
3670 41911022 : lhs = op0;
3671 41911022 : if (GET_CODE (op0) == MULT)
3672 : {
3673 238338 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3674 5658 : && XEXP (XEXP (op0, 0), 1) == const1_rtx)
3675 236657 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3676 1661 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx))
3677 240019 : && rtx_equal_p (XEXP (op0, 1), op1))
3678 3 : lhs = XEXP (XEXP (op0, 0), 0);
3679 238335 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3680 30 : && XEXP (XEXP (op0, 1), 1) == const1_rtx)
3681 238327 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3682 84 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx))
3683 238343 : && rtx_equal_p (XEXP (op0, 0), op1))
3684 0 : lhs = XEXP (XEXP (op0, 1), 0);
3685 : }
3686 41911022 : if (lhs != op0 && !side_effects_p (op1))
3687 2 : return simplify_gen_binary (MULT, int_mode, lhs, op1);
3688 42009374 : }
3689 :
3690 : /* (a - (-b)) -> (a + b). True even for IEEE. */
3691 43350524 : if (GET_CODE (op1) == NEG)
3692 13593 : return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
3693 :
3694 : /* (-x - c) may be simplified as (-c - x). */
3695 43336931 : if (GET_CODE (op0) == NEG
3696 78898 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1)))
3697 : {
3698 666 : tem = simplify_unary_operation (NEG, mode, op1, mode);
3699 666 : if (tem)
3700 666 : return simplify_gen_binary (MINUS, mode, tem, XEXP (op0, 0));
3701 : }
3702 :
3703 43336265 : if ((GET_CODE (op0) == CONST
3704 43336265 : || GET_CODE (op0) == SYMBOL_REF
3705 37861703 : || GET_CODE (op0) == LABEL_REF)
3706 43336265 : && poly_int_rtx_p (op1, &offset))
3707 54447 : return plus_constant (mode, op0, trunc_int_for_mode (-offset, mode));
3708 :
3709 : /* Don't let a relocatable value get a negative coeff. */
3710 43281818 : if (is_a <scalar_int_mode> (mode)
3711 41842329 : && poly_int_rtx_p (op1)
3712 50608679 : && GET_MODE (op0) != VOIDmode)
3713 7326861 : return simplify_gen_binary (PLUS, mode,
3714 : op0,
3715 7326861 : neg_poly_int_rtx (mode, op1));
3716 :
3717 : /* (x - (x & y)) -> (x & ~y) */
3718 35954957 : if (INTEGRAL_MODE_P (mode) && GET_CODE (op1) == AND)
3719 : {
3720 225571 : if (rtx_equal_p (op0, XEXP (op1, 0)))
3721 : {
3722 592 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 1),
3723 296 : GET_MODE (XEXP (op1, 1)));
3724 296 : return simplify_gen_binary (AND, mode, op0, tem);
3725 : }
3726 225275 : if (rtx_equal_p (op0, XEXP (op1, 1)))
3727 : {
3728 2458 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 0),
3729 1229 : GET_MODE (XEXP (op1, 0)));
3730 1229 : return simplify_gen_binary (AND, mode, op0, tem);
3731 : }
3732 : }
3733 :
3734 : /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3735 : by reversing the comparison code if valid. */
3736 35953432 : if (STORE_FLAG_VALUE == 1
3737 35953432 : && trueop0 == const1_rtx
3738 1224579 : && COMPARISON_P (op1)
3739 36058232 : && (reversed = reversed_comparison (op1, mode)))
3740 : return reversed;
3741 :
3742 : /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A). */
3743 35848637 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3744 35847189 : && GET_CODE (op1) == MULT
3745 36094171 : && GET_CODE (XEXP (op1, 0)) == NEG)
3746 : {
3747 173 : rtx in1, in2;
3748 :
3749 173 : in1 = XEXP (XEXP (op1, 0), 0);
3750 173 : in2 = XEXP (op1, 1);
3751 173 : return simplify_gen_binary (PLUS, mode,
3752 : simplify_gen_binary (MULT, mode,
3753 : in1, in2),
3754 173 : op0);
3755 : }
3756 :
3757 : /* Canonicalize (minus (neg A) (mult B C)) to
3758 : (minus (mult (neg B) C) A). */
3759 35848464 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3760 35847016 : && GET_CODE (op1) == MULT
3761 36093825 : && GET_CODE (op0) == NEG)
3762 : {
3763 671 : rtx in1, in2;
3764 :
3765 671 : in1 = simplify_gen_unary (NEG, mode, XEXP (op1, 0), mode);
3766 671 : in2 = XEXP (op1, 1);
3767 671 : return simplify_gen_binary (MINUS, mode,
3768 : simplify_gen_binary (MULT, mode,
3769 : in1, in2),
3770 671 : XEXP (op0, 0));
3771 : }
3772 :
3773 : /* If one of the operands is a PLUS or a MINUS, see if we can
3774 : simplify this by the associative law. This will, for example,
3775 : canonicalize (minus A (plus B C)) to (minus (minus A B) C).
3776 : Don't use the associative law for floating point.
3777 : The inaccuracy makes it nonassociative,
3778 : and subtle programs can break if operations are associated. */
3779 :
3780 35847793 : if (INTEGRAL_MODE_P (mode)
3781 34982650 : && (plus_minus_operand_p (op0)
3782 32088072 : || plus_minus_operand_p (op1))
3783 4149635 : && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3784 : return tem;
3785 :
3786 : /* Handle vector series. */
3787 31850514 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3788 : {
3789 549696 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3790 549696 : if (tem)
3791 : return tem;
3792 : }
3793 : break;
3794 :
3795 13295240 : case MULT:
3796 13295240 : if (trueop1 == constm1_rtx)
3797 35587 : return simplify_gen_unary (NEG, mode, op0, mode);
3798 :
3799 13259653 : if (GET_CODE (op0) == NEG)
3800 : {
3801 41445 : rtx temp = simplify_unary_operation (NEG, mode, op1, mode);
3802 : /* If op1 is a MULT as well and simplify_unary_operation
3803 : just moved the NEG to the second operand, simplify_gen_binary
3804 : below could through simplify_associative_operation move
3805 : the NEG around again and recurse endlessly. */
3806 41445 : if (temp
3807 1967 : && GET_CODE (op1) == MULT
3808 0 : && GET_CODE (temp) == MULT
3809 0 : && XEXP (op1, 0) == XEXP (temp, 0)
3810 0 : && GET_CODE (XEXP (temp, 1)) == NEG
3811 0 : && XEXP (op1, 1) == XEXP (XEXP (temp, 1), 0))
3812 : temp = NULL_RTX;
3813 : if (temp)
3814 1967 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), temp);
3815 : }
3816 13257686 : if (GET_CODE (op1) == NEG)
3817 : {
3818 889 : rtx temp = simplify_unary_operation (NEG, mode, op0, mode);
3819 : /* If op0 is a MULT as well and simplify_unary_operation
3820 : just moved the NEG to the second operand, simplify_gen_binary
3821 : below could through simplify_associative_operation move
3822 : the NEG around again and recurse endlessly. */
3823 889 : if (temp
3824 409 : && GET_CODE (op0) == MULT
3825 305 : && GET_CODE (temp) == MULT
3826 305 : && XEXP (op0, 0) == XEXP (temp, 0)
3827 6 : && GET_CODE (XEXP (temp, 1)) == NEG
3828 5 : && XEXP (op0, 1) == XEXP (XEXP (temp, 1), 0))
3829 : temp = NULL_RTX;
3830 : if (temp)
3831 404 : return simplify_gen_binary (MULT, mode, temp, XEXP (op1, 0));
3832 : }
3833 :
3834 : /* Maybe simplify x * 0 to 0. The reduction is not valid if
3835 : x is NaN, since x * 0 is then also NaN. Nor is it valid
3836 : when the mode has signed zeros, since multiplying a negative
3837 : number by 0 will give -0, not 0. */
3838 13257282 : if (!HONOR_NANS (mode)
3839 12263388 : && !HONOR_SIGNED_ZEROS (mode)
3840 12262981 : && trueop1 == CONST0_RTX (mode)
3841 13303721 : && ! side_effects_p (op0))
3842 : return op1;
3843 :
3844 : /* In IEEE floating point, x*1 is not equivalent to x for
3845 : signalling NaNs. */
3846 13210843 : if (!HONOR_SNANS (mode)
3847 13210843 : && trueop1 == CONST1_RTX (mode))
3848 : return op0;
3849 :
3850 : /* Convert multiply by constant power of two into shift. */
3851 12653658 : if (mem_depth == 0 && CONST_SCALAR_INT_P (trueop1))
3852 : {
3853 6410261 : val = wi::exact_log2 (rtx_mode_t (trueop1, mode));
3854 6410261 : if (val >= 0)
3855 2986079 : return simplify_gen_binary (ASHIFT, mode, op0,
3856 2986079 : gen_int_shift_amount (mode, val));
3857 : }
3858 :
3859 : /* x*2 is x+x and x*(-1) is -x */
3860 9667579 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
3861 173059 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop1))
3862 173059 : && !DECIMAL_FLOAT_MODE_P (GET_MODE (trueop1))
3863 172775 : && GET_MODE (op0) == mode)
3864 : {
3865 172775 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
3866 :
3867 172775 : if (real_equal (d1, &dconst2))
3868 689 : return simplify_gen_binary (PLUS, mode, op0, copy_rtx (op0));
3869 :
3870 172086 : if (!HONOR_SNANS (mode)
3871 172086 : && real_equal (d1, &dconstm1))
3872 24 : return simplify_gen_unary (NEG, mode, op0, mode);
3873 : }
3874 :
3875 : /* Optimize -x * -x as x * x. */
3876 9666866 : if (FLOAT_MODE_P (mode)
3877 1430341 : && GET_CODE (op0) == NEG
3878 8567 : && GET_CODE (op1) == NEG
3879 0 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3880 0 : && !side_effects_p (XEXP (op0, 0)))
3881 0 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3882 :
3883 : /* Likewise, optimize abs(x) * abs(x) as x * x. */
3884 9666866 : if (SCALAR_FLOAT_MODE_P (mode)
3885 1121715 : && GET_CODE (op0) == ABS
3886 1269 : && GET_CODE (op1) == ABS
3887 1 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3888 9666866 : && !side_effects_p (XEXP (op0, 0)))
3889 0 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3890 :
3891 : /* Reassociate multiplication, but for floating point MULTs
3892 : only when the user specifies unsafe math optimizations. */
3893 9666866 : if (! FLOAT_MODE_P (mode)
3894 1430341 : || flag_unsafe_math_optimizations)
3895 : {
3896 8674100 : tem = simplify_associative_operation (code, mode, op0, op1);
3897 8674100 : if (tem)
3898 : return tem;
3899 : }
3900 : break;
3901 :
3902 15152711 : case IOR:
3903 15152711 : if (trueop1 == CONST0_RTX (mode))
3904 : return op0;
3905 14428826 : if (INTEGRAL_MODE_P (mode)
3906 14161974 : && trueop1 == CONSTM1_RTX (mode)
3907 10196 : && !side_effects_p (op0))
3908 : return op1;
3909 14418630 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3910 : return op0;
3911 : /* A | (~A) -> -1 */
3912 80210 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
3913 14407082 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
3914 0 : && ! side_effects_p (op0)
3915 14407082 : && GET_MODE_CLASS (mode) != MODE_CC)
3916 0 : return CONSTM1_RTX (mode);
3917 :
3918 14407082 : if (rtx tem = simplify_unsigned_cmp_minmax (IOR, mode, op0, op1))
3919 : return tem;
3920 :
3921 14407082 : if (HWI_COMPUTABLE_MODE_P (mode))
3922 : {
3923 : /* When WORD_REGISTER_OPERATIONS is true, we need to know the
3924 : nonzero bits in WORD_MODE rather than MODE. */
3925 10657732 : scalar_int_mode tmode = as_a <scalar_int_mode> (mode);
3926 10657732 : if (WORD_REGISTER_OPERATIONS
3927 : && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3928 : tmode = word_mode;
3929 :
3930 : /* Convert (ior (xor A 1) (xor B 1)) to (xor (and A B) 1) when A and B
3931 : are both known to be 0 or 1. */
3932 10657732 : if (is_a <scalar_int_mode> (mode, &int_mode)
3933 10657732 : && HWI_COMPUTABLE_MODE_P (int_mode)
3934 10657732 : && GET_CODE (op0) == XOR
3935 113325 : && GET_CODE (op1) == XOR
3936 189 : && XEXP (op0, 1) == CONST1_RTX (mode)
3937 7 : && XEXP (op1, 1) == CONST1_RTX (mode)
3938 7 : && (nonzero_bits (XEXP (op0, 0), tmode) & ~HOST_WIDE_INT_1U) == 0
3939 3 : && (nonzero_bits (XEXP (op1, 0), tmode) & ~HOST_WIDE_INT_1U) == 0)
3940 3 : return simplify_gen_binary (XOR, mode,
3941 : simplify_gen_binary (AND, mode,
3942 : XEXP (op0, 0),
3943 : XEXP (op1, 0)),
3944 3 : CONST1_RTX (mode));
3945 : }
3946 :
3947 : /* IOR of two single bit bitfields extracted from the same object.
3948 : Bitfields are represented as an AND based extraction */
3949 14407079 : if (GET_CODE (op0) == AND
3950 4463025 : && GET_CODE (op1) == AND
3951 : /* Verify both AND operands are logical right shifts. */
3952 299053 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
3953 2321 : && GET_CODE (XEXP (op1, 0)) == LSHIFTRT
3954 : /* Verify both bitfields are extracted from the same object. */
3955 260 : && XEXP (XEXP (op0, 0), 0) == XEXP (XEXP (op1, 0), 0)
3956 : /* Verify both fields are a single bit (could be generalized). */
3957 256 : && XEXP (op0, 1) == CONST1_RTX (mode)
3958 0 : && XEXP (op1, 1) == CONST1_RTX (mode)
3959 : /* Verify bit positions (for cases with variable bit position). */
3960 0 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
3961 0 : && CONST_INT_P (XEXP (XEXP (op1, 0), 1)))
3962 : {
3963 0 : unsigned HOST_WIDE_INT bitpos1 = INTVAL (XEXP (XEXP (op0, 0), 1));
3964 0 : unsigned HOST_WIDE_INT bitpos2 = INTVAL (XEXP (XEXP (op1, 0), 1));
3965 0 : unsigned HOST_WIDE_INT mask
3966 0 : = (HOST_WIDE_INT_1U << bitpos1) | (HOST_WIDE_INT_1U << bitpos2);
3967 :
3968 0 : rtx m = GEN_INT (mask);
3969 0 : rtx t = gen_rtx_AND (mode, XEXP (XEXP (op0, 0), 0), m);
3970 0 : t = gen_rtx_NE (mode, t, CONST0_RTX (mode));
3971 0 : return t;
3972 : }
3973 :
3974 : /* IOR of multiple single bit bitfields extracted from the same object
3975 : (building on previous case).
3976 : First bitfield is represented as an AND based extraction, as done
3977 : above. Second represented as NE based extraction, from
3978 : output above. */
3979 14407079 : if (GET_CODE (op0) == AND
3980 4463025 : && GET_CODE (op1) == NE
3981 : /* Verify AND operand is logical right shift. */
3982 3480 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
3983 : /* Verify NE operand is an AND (based on output above). */
3984 102 : && GET_CODE (XEXP (op1, 0)) == AND
3985 : /* Verify both bitfields are extracted from the same object. */
3986 0 : && XEXP (XEXP (op0, 0), 0) == XEXP (XEXP (op1, 0), 0)
3987 : /* Verify masking is with a single bit and that we have a NE 0
3988 : comparison for the other operand. */
3989 0 : && XEXP (op0, 1) == CONST1_RTX (mode)
3990 0 : && XEXP (op1, 1) == CONST0_RTX (mode)
3991 : /* Verify bit position. */
3992 0 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
3993 0 : && CONST_INT_P (XEXP (XEXP (op1, 0), 1)))
3994 : {
3995 0 : unsigned HOST_WIDE_INT bitpos1 = INTVAL (XEXP (XEXP (op0, 0), 1));
3996 0 : unsigned HOST_WIDE_INT mask
3997 0 : = (HOST_WIDE_INT_1U << bitpos1) | INTVAL (XEXP (XEXP (op1, 0), 1));
3998 :
3999 0 : rtx m = GEN_INT (mask);
4000 0 : rtx t = gen_rtx_AND (mode, XEXP (XEXP (op0, 0), 0), m);
4001 0 : t = gen_rtx_NE (mode, t, CONST0_RTX (mode));
4002 0 : return t;
4003 : }
4004 :
4005 : /* Convert (ior (plus (A - 1)) (neg A)) to -1. */
4006 14407079 : if (match_plus_neg_pattern (op0, op1, mode))
4007 0 : return CONSTM1_RTX (mode);
4008 :
4009 : /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
4010 14407079 : if (CONST_INT_P (op1)
4011 3711806 : && HWI_COMPUTABLE_MODE_P (mode)
4012 3654917 : && (nonzero_bits (op0, mode) & ~UINTVAL (op1)) == 0
4013 14631081 : && !side_effects_p (op0))
4014 : return op1;
4015 :
4016 : /* Canonicalize (X & C1) | C2. */
4017 14183077 : if (GET_CODE (op0) == AND
4018 4454391 : && CONST_INT_P (trueop1)
4019 779036 : && CONST_INT_P (XEXP (op0, 1)))
4020 : {
4021 556418 : HOST_WIDE_INT mask = GET_MODE_MASK (mode);
4022 556418 : HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
4023 556418 : HOST_WIDE_INT c2 = INTVAL (trueop1);
4024 :
4025 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes C2. */
4026 556418 : if ((c1 & c2) == c1
4027 556418 : && !side_effects_p (XEXP (op0, 0)))
4028 : return trueop1;
4029 :
4030 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
4031 556410 : if (((c1|c2) & mask) == mask)
4032 78101 : return simplify_gen_binary (IOR, mode, XEXP (op0, 0), op1);
4033 :
4034 : /* If (C1|C2) has a single bit clear, then adjust C1 so that
4035 : when split it'll match a single bit clear style insn.
4036 :
4037 : This could have been done with a target dependent splitter, but
4038 : then every target with single bit manipulation insns would need
4039 : to implement such splitters. */
4040 478309 : if (exact_log2 (~(c1 | c2)) >= 0)
4041 : {
4042 60459 : rtx temp = gen_rtx_AND (mode, XEXP (op0, 0), GEN_INT (c1 | c2));
4043 60459 : temp = gen_rtx_IOR (mode, temp, trueop1);
4044 60459 : return temp;
4045 : }
4046 : }
4047 :
4048 : /* Convert (A & B) | A to A. */
4049 14044509 : if (GET_CODE (op0) == AND
4050 4315823 : && (rtx_equal_p (XEXP (op0, 0), op1)
4051 4315306 : || rtx_equal_p (XEXP (op0, 1), op1))
4052 3840 : && ! side_effects_p (XEXP (op0, 0))
4053 14048349 : && ! side_effects_p (XEXP (op0, 1)))
4054 : return op1;
4055 :
4056 : /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4057 : mode size to (rotate A CX). */
4058 14040669 : tem = simplify_rotate_op (op0, op1, mode);
4059 14040669 : if (tem)
4060 : return tem;
4061 :
4062 : /* If OP0 is (ashiftrt (plus ...) C), it might actually be
4063 : a (sign_extend (plus ...)). Then check if OP1 is a CONST_INT and
4064 : the PLUS does not affect any of the bits in OP1: then we can do
4065 : the IOR as a PLUS and we can associate. This is valid if OP1
4066 : can be safely shifted left C bits. */
4067 14038443 : if (CONST_INT_P (trueop1) && GET_CODE (op0) == ASHIFTRT
4068 6277 : && GET_CODE (XEXP (op0, 0)) == PLUS
4069 74 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
4070 50 : && CONST_INT_P (XEXP (op0, 1))
4071 50 : && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
4072 : {
4073 50 : int count = INTVAL (XEXP (op0, 1));
4074 50 : HOST_WIDE_INT mask = UINTVAL (trueop1) << count;
4075 :
4076 50 : if (mask >> count == INTVAL (trueop1)
4077 43 : && trunc_int_for_mode (mask, mode) == mask
4078 80 : && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
4079 0 : return simplify_gen_binary (ASHIFTRT, mode,
4080 : plus_constant (mode, XEXP (op0, 0),
4081 0 : mask),
4082 : XEXP (op0, 1));
4083 : }
4084 :
4085 : /* The following happens with bitfield merging.
4086 : (X & C) | ((X | Y) & ~C) -> X | (Y & ~C) */
4087 14038443 : if (GET_CODE (op0) == AND
4088 4311983 : && GET_CODE (op1) == AND
4089 299053 : && CONST_INT_P (XEXP (op0, 1))
4090 137621 : && CONST_INT_P (XEXP (op1, 1))
4091 133084 : && (INTVAL (XEXP (op0, 1))
4092 133084 : == ~INTVAL (XEXP (op1, 1))))
4093 : {
4094 : /* The IOR/XOR may be on both sides. */
4095 37609 : rtx top0 = NULL_RTX, top1 = NULL_RTX;
4096 37609 : if (GET_CODE (XEXP (op1, 0)) == IOR
4097 37609 : || GET_CODE (XEXP (op1, 0)) == XOR)
4098 : top0 = op0, top1 = op1;
4099 37490 : else if (GET_CODE (XEXP (op0, 0)) == IOR
4100 37490 : || GET_CODE (XEXP (op0, 0)) == XOR)
4101 29 : top0 = op1, top1 = op0;
4102 37609 : if (top0 && top1)
4103 : {
4104 : /* X may be on either side of the inner IOR/XOR. */
4105 148 : rtx tem = NULL_RTX;
4106 148 : if (rtx_equal_p (XEXP (top0, 0),
4107 148 : XEXP (XEXP (top1, 0), 0)))
4108 76 : tem = XEXP (XEXP (top1, 0), 1);
4109 72 : else if (rtx_equal_p (XEXP (top0, 0),
4110 72 : XEXP (XEXP (top1, 0), 1)))
4111 13 : tem = XEXP (XEXP (top1, 0), 0);
4112 89 : if (tem)
4113 89 : return simplify_gen_binary (GET_CODE (XEXP (top1, 0)),
4114 : mode, XEXP (top0, 0),
4115 : simplify_gen_binary
4116 89 : (AND, mode, tem, XEXP (top1, 1)));
4117 : }
4118 : }
4119 :
4120 : /* Convert (ior (and A C) (and B C)) into (and (ior A B) C). */
4121 14038354 : if (GET_CODE (op0) == GET_CODE (op1)
4122 3129478 : && (GET_CODE (op0) == AND
4123 : || GET_CODE (op0) == IOR
4124 3129478 : || GET_CODE (op0) == LSHIFTRT
4125 2829446 : || GET_CODE (op0) == ASHIFTRT
4126 2829301 : || GET_CODE (op0) == ASHIFT
4127 2812265 : || GET_CODE (op0) == ROTATE
4128 2812265 : || GET_CODE (op0) == ROTATERT))
4129 : {
4130 317213 : tem = simplify_distributive_operation (code, mode, op0, op1);
4131 317213 : if (tem)
4132 : return tem;
4133 : }
4134 :
4135 : /* Convert (ior (and (not A) B) A) into A | B. */
4136 13953480 : if (GET_CODE (op0) == AND
4137 13953480 : && negated_ops_p (XEXP (op0, 0), op1))
4138 6594 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4139 :
4140 : /* op0/op1 may have a common term which in turn may allow simplification
4141 : of the the outer IOR. There are likely other cases we should
4142 : handle for the outer code as well as the form of the operands. */
4143 13946886 : tem = simplify_ior_with_common_term (mode, op0, op1);
4144 13946886 : if (tem)
4145 : return tem;
4146 :
4147 : /* IOR is commutative and we can't rely on canonicalization at this point,
4148 : so try again to simplify with the operands reversed. */
4149 13946796 : tem = simplify_ior_with_common_term (mode, op1, op0);
4150 13946796 : if (tem)
4151 : return tem;
4152 :
4153 13946796 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4154 13946796 : if (tem)
4155 : return tem;
4156 :
4157 13946791 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4158 13946791 : if (tem)
4159 : return tem;
4160 :
4161 13946785 : tem = simplify_associative_operation (code, mode, op0, op1);
4162 13946785 : if (tem)
4163 : return tem;
4164 :
4165 13697528 : tem = simplify_logical_relational_operation (code, mode, op0, op1);
4166 13697528 : if (tem)
4167 : return tem;
4168 : break;
4169 :
4170 2090400 : case XOR:
4171 2090400 : if (trueop1 == CONST0_RTX (mode))
4172 : return op0;
4173 2027469 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
4174 31578 : return simplify_gen_unary (NOT, mode, op0, mode);
4175 1995891 : if (rtx_equal_p (trueop0, trueop1)
4176 3291 : && ! side_effects_p (op0)
4177 1999178 : && GET_MODE_CLASS (mode) != MODE_CC)
4178 3287 : return CONST0_RTX (mode);
4179 :
4180 : /* Canonicalize XOR of the most significant bit to PLUS. */
4181 1992604 : if (CONST_SCALAR_INT_P (op1)
4182 1992604 : && mode_signbit_p (mode, op1))
4183 40848 : return simplify_gen_binary (PLUS, mode, op0, op1);
4184 : /* (xor (plus X C1) C2) is (xor X (C1^C2)) if C1 is signbit. */
4185 1951756 : if (CONST_SCALAR_INT_P (op1)
4186 522405 : && GET_CODE (op0) == PLUS
4187 2499 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
4188 1953246 : && mode_signbit_p (mode, XEXP (op0, 1)))
4189 189 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
4190 : simplify_gen_binary (XOR, mode, op1,
4191 189 : XEXP (op0, 1)));
4192 :
4193 : /* If we are XORing two things that have no bits in common,
4194 : convert them into an IOR. This helps to detect rotation encoded
4195 : using those methods and possibly other simplifications. */
4196 :
4197 1951567 : if (HWI_COMPUTABLE_MODE_P (mode)
4198 1525418 : && (nonzero_bits (op0, mode)
4199 1525418 : & nonzero_bits (op1, mode)) == 0)
4200 10244 : return (simplify_gen_binary (IOR, mode, op0, op1));
4201 :
4202 : /* Convert (xor (plus (A - 1)) (neg A)) to -1. */
4203 1941323 : if (match_plus_neg_pattern (op0, op1, mode))
4204 0 : return CONSTM1_RTX (mode);
4205 :
4206 : /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
4207 : Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
4208 : (NOT y). */
4209 1941323 : {
4210 1941323 : int num_negated = 0;
4211 :
4212 1941323 : if (GET_CODE (op0) == NOT)
4213 693 : num_negated++, op0 = XEXP (op0, 0);
4214 1941323 : if (GET_CODE (op1) == NOT)
4215 78 : num_negated++, op1 = XEXP (op1, 0);
4216 :
4217 78 : if (num_negated == 2)
4218 0 : return simplify_gen_binary (XOR, mode, op0, op1);
4219 1941323 : else if (num_negated == 1)
4220 771 : return simplify_gen_unary (NOT, mode,
4221 : simplify_gen_binary (XOR, mode, op0, op1),
4222 771 : mode);
4223 : }
4224 :
4225 : /* Convert (xor (and A B) B) to (and (not A) B). The latter may
4226 : correspond to a machine insn or result in further simplifications
4227 : if B is a constant. */
4228 :
4229 1940552 : if (GET_CODE (op0) == AND
4230 208940 : && rtx_equal_p (XEXP (op0, 1), op1)
4231 1960856 : && ! side_effects_p (op1))
4232 20304 : return simplify_gen_binary (AND, mode,
4233 : simplify_gen_unary (NOT, mode,
4234 : XEXP (op0, 0), mode),
4235 20304 : op1);
4236 :
4237 1920248 : else if (GET_CODE (op0) == AND
4238 188636 : && rtx_equal_p (XEXP (op0, 0), op1)
4239 1920475 : && ! side_effects_p (op1))
4240 227 : return simplify_gen_binary (AND, mode,
4241 : simplify_gen_unary (NOT, mode,
4242 : XEXP (op0, 1), mode),
4243 227 : op1);
4244 :
4245 : /* Similarly, convert (xor (ior A B) B) to (and (not B) A). */
4246 1920021 : else if (GET_CODE (op0) == IOR
4247 32002 : && rtx_equal_p (XEXP (op0, 1), op1)
4248 1920176 : && ! side_effects_p (op1))
4249 155 : return simplify_gen_binary (AND, mode,
4250 : simplify_gen_unary (NOT, mode,
4251 : op1, mode),
4252 155 : XEXP (op0, 0));
4253 1919866 : else if (GET_CODE (op0) == IOR
4254 31847 : && rtx_equal_p (XEXP (op0, 0), op1)
4255 1919874 : && ! side_effects_p (op1))
4256 8 : return simplify_gen_binary (AND, mode,
4257 : simplify_gen_unary (NOT, mode,
4258 : op1, mode),
4259 8 : XEXP (op0, 1));
4260 :
4261 : /* Given (xor (ior (xor A B) C) D), where B, C and D are
4262 : constants, simplify to (xor (ior A C) (B&~C)^D), canceling
4263 : out bits inverted twice and not set by C. Similarly, given
4264 : (xor (and (xor A B) C) D), simplify without inverting C in
4265 : the xor operand: (xor (and A C) (B&C)^D).
4266 : */
4267 1919858 : else if ((GET_CODE (op0) == IOR || GET_CODE (op0) == AND)
4268 220248 : && GET_CODE (XEXP (op0, 0)) == XOR
4269 9110 : && CONST_INT_P (op1)
4270 213 : && CONST_INT_P (XEXP (op0, 1))
4271 183 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1)))
4272 : {
4273 42 : enum rtx_code op = GET_CODE (op0);
4274 42 : rtx a = XEXP (XEXP (op0, 0), 0);
4275 42 : rtx b = XEXP (XEXP (op0, 0), 1);
4276 42 : rtx c = XEXP (op0, 1);
4277 42 : rtx d = op1;
4278 42 : HOST_WIDE_INT bval = INTVAL (b);
4279 42 : HOST_WIDE_INT cval = INTVAL (c);
4280 42 : HOST_WIDE_INT dval = INTVAL (d);
4281 42 : HOST_WIDE_INT xcval;
4282 :
4283 42 : if (op == IOR)
4284 8 : xcval = ~cval;
4285 : else
4286 : xcval = cval;
4287 :
4288 42 : return simplify_gen_binary (XOR, mode,
4289 : simplify_gen_binary (op, mode, a, c),
4290 42 : gen_int_mode ((bval & xcval) ^ dval,
4291 : mode));
4292 : }
4293 :
4294 : /* Given (xor (and A B) C), using P^Q == (~P&Q) | (~Q&P),
4295 : we can transform like this:
4296 : (A&B)^C == ~(A&B)&C | ~C&(A&B)
4297 : == (~A|~B)&C | ~C&(A&B) * DeMorgan's Law
4298 : == ~A&C | ~B&C | A&(~C&B) * Distribute and re-order
4299 : Attempt a few simplifications when B and C are both constants. */
4300 1919816 : if (GET_CODE (op0) == AND
4301 188375 : && CONST_INT_P (op1)
4302 14656 : && CONST_INT_P (XEXP (op0, 1)))
4303 : {
4304 11592 : rtx a = XEXP (op0, 0);
4305 11592 : rtx b = XEXP (op0, 1);
4306 11592 : rtx c = op1;
4307 11592 : HOST_WIDE_INT bval = INTVAL (b);
4308 11592 : HOST_WIDE_INT cval = INTVAL (c);
4309 :
4310 : /* Instead of computing ~A&C, we compute its negated value,
4311 : ~(A|~C). If it yields -1, ~A&C is zero, so we can
4312 : optimize for sure. If it does not simplify, we still try
4313 : to compute ~A&C below, but since that always allocates
4314 : RTL, we don't try that before committing to returning a
4315 : simplified expression. */
4316 11592 : rtx n_na_c = simplify_binary_operation (IOR, mode, a,
4317 : GEN_INT (~cval));
4318 :
4319 11592 : if ((~cval & bval) == 0)
4320 : {
4321 824 : rtx na_c = NULL_RTX;
4322 824 : if (n_na_c)
4323 0 : na_c = simplify_gen_unary (NOT, mode, n_na_c, mode);
4324 : else
4325 : {
4326 : /* If ~A does not simplify, don't bother: we don't
4327 : want to simplify 2 operations into 3, and if na_c
4328 : were to simplify with na, n_na_c would have
4329 : simplified as well. */
4330 824 : rtx na = simplify_unary_operation (NOT, mode, a, mode);
4331 824 : if (na)
4332 0 : na_c = simplify_gen_binary (AND, mode, na, c);
4333 : }
4334 :
4335 : /* Try to simplify ~A&C | ~B&C. */
4336 0 : if (na_c != NULL_RTX)
4337 0 : return simplify_gen_binary (IOR, mode, na_c,
4338 0 : gen_int_mode (~bval & cval, mode));
4339 : }
4340 : else
4341 : {
4342 : /* If ~A&C is zero, simplify A&(~C&B) | ~B&C. */
4343 10768 : if (n_na_c == CONSTM1_RTX (mode))
4344 : {
4345 0 : rtx a_nc_b = simplify_gen_binary (AND, mode, a,
4346 0 : gen_int_mode (~cval & bval,
4347 : mode));
4348 0 : return simplify_gen_binary (IOR, mode, a_nc_b,
4349 0 : gen_int_mode (~bval & cval,
4350 : mode));
4351 : }
4352 : }
4353 : }
4354 :
4355 : /* If we have (xor (and (xor A B) C) A) with C a constant we can instead
4356 : do (ior (and A ~C) (and B C)) which is a machine instruction on some
4357 : machines, and also has shorter instruction path length. */
4358 1919816 : if (GET_CODE (op0) == AND
4359 188375 : && GET_CODE (XEXP (op0, 0)) == XOR
4360 8616 : && CONST_INT_P (XEXP (op0, 1))
4361 1924369 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), trueop1))
4362 : {
4363 7 : rtx a = trueop1;
4364 7 : rtx b = XEXP (XEXP (op0, 0), 1);
4365 7 : rtx c = XEXP (op0, 1);
4366 7 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
4367 7 : rtx a_nc = simplify_gen_binary (AND, mode, a, nc);
4368 7 : rtx bc = simplify_gen_binary (AND, mode, b, c);
4369 7 : return simplify_gen_binary (IOR, mode, a_nc, bc);
4370 : }
4371 : /* Similarly, (xor (and (xor A B) C) B) as (ior (and A C) (and B ~C)) */
4372 1919809 : else if (GET_CODE (op0) == AND
4373 188368 : && GET_CODE (XEXP (op0, 0)) == XOR
4374 8609 : && CONST_INT_P (XEXP (op0, 1))
4375 1924355 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), trueop1))
4376 : {
4377 8 : rtx a = XEXP (XEXP (op0, 0), 0);
4378 8 : rtx b = trueop1;
4379 8 : rtx c = XEXP (op0, 1);
4380 8 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
4381 8 : rtx b_nc = simplify_gen_binary (AND, mode, b, nc);
4382 8 : rtx ac = simplify_gen_binary (AND, mode, a, c);
4383 8 : return simplify_gen_binary (IOR, mode, ac, b_nc);
4384 : }
4385 :
4386 : /* (xor (comparison foo bar) (const_int 1)) can become the reversed
4387 : comparison if STORE_FLAG_VALUE is 1. */
4388 1919801 : if (STORE_FLAG_VALUE == 1
4389 1919801 : && trueop1 == const1_rtx
4390 213231 : && COMPARISON_P (op0)
4391 1926603 : && (reversed = reversed_comparison (op0, mode)))
4392 : return reversed;
4393 :
4394 : /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4395 : is (lt foo (const_int 0)), so we can perform the above
4396 : simplification if STORE_FLAG_VALUE is 1. */
4397 :
4398 1913023 : if (is_a <scalar_int_mode> (mode, &int_mode)
4399 : && STORE_FLAG_VALUE == 1
4400 1564751 : && trueop1 == const1_rtx
4401 206453 : && GET_CODE (op0) == LSHIFTRT
4402 35390 : && CONST_INT_P (XEXP (op0, 1))
4403 35390 : && INTVAL (XEXP (op0, 1)) == GET_MODE_PRECISION (int_mode) - 1)
4404 34192 : return gen_rtx_GE (int_mode, XEXP (op0, 0), const0_rtx);
4405 :
4406 : /* (xor (comparison foo bar) (const_int sign-bit))
4407 : when STORE_FLAG_VALUE is the sign bit. */
4408 1878831 : if (is_a <scalar_int_mode> (mode, &int_mode)
4409 1530559 : && val_signbit_p (int_mode, STORE_FLAG_VALUE)
4410 0 : && trueop1 == const_true_rtx
4411 0 : && COMPARISON_P (op0)
4412 0 : && (reversed = reversed_comparison (op0, int_mode)))
4413 : return reversed;
4414 :
4415 : /* Convert (xor (and A C) (and B C)) into (and (xor A B) C). */
4416 1878831 : if (GET_CODE (op0) == GET_CODE (op1)
4417 684766 : && (GET_CODE (op0) == AND
4418 684766 : || GET_CODE (op0) == LSHIFTRT
4419 591588 : || GET_CODE (op0) == ASHIFTRT
4420 591474 : || GET_CODE (op0) == ASHIFT
4421 591340 : || GET_CODE (op0) == ROTATE
4422 591198 : || GET_CODE (op0) == ROTATERT))
4423 : {
4424 94102 : tem = simplify_distributive_operation (code, mode, op0, op1);
4425 94102 : if (tem)
4426 : return tem;
4427 : }
4428 :
4429 : /* Convert (xor (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4430 : mode size to (rotate A CX). */
4431 1791684 : tem = simplify_rotate_op (op0, op1, mode);
4432 1791684 : if (tem)
4433 : return tem;
4434 :
4435 : /* Convert (xor (and (not A) B) A) into A | B. */
4436 1790304 : if (GET_CODE (op0) == AND
4437 1790304 : && negated_ops_p (XEXP (op0, 0), op1))
4438 0 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4439 :
4440 : /* Convert (xor (and (rotate (~1) A) B) (ashift 1 A))
4441 : into B | (1 << A). */
4442 1790304 : if (SHIFT_COUNT_TRUNCATED
4443 : && GET_CODE (op0) == AND
4444 : && GET_CODE (XEXP (op0, 0)) == ROTATE
4445 : && CONST_INT_P (XEXP (XEXP (op0, 0), 0))
4446 : && INTVAL (XEXP (XEXP (op0, 0), 0)) == -2
4447 : && GET_CODE (op1) == ASHIFT
4448 : && CONST_INT_P (XEXP (op1, 0))
4449 : && INTVAL (XEXP (op1, 0)) == 1
4450 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), XEXP (op1, 1))
4451 : && !side_effects_p (XEXP (op1, 1)))
4452 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4453 :
4454 1790304 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4455 1790304 : if (tem)
4456 : return tem;
4457 :
4458 1790303 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4459 1790303 : if (tem)
4460 : return tem;
4461 :
4462 1790303 : tem = simplify_associative_operation (code, mode, op0, op1);
4463 1790303 : if (tem)
4464 : return tem;
4465 : break;
4466 :
4467 25796262 : case AND:
4468 25796262 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
4469 : return trueop1;
4470 25464977 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
4471 : return op0;
4472 25046730 : if (HWI_COMPUTABLE_MODE_P (mode))
4473 : {
4474 : /* When WORD_REGISTER_OPERATIONS is true, we need to know the
4475 : nonzero bits in WORD_MODE rather than MODE. */
4476 21951913 : scalar_int_mode tmode = as_a <scalar_int_mode> (mode);
4477 21951913 : if (WORD_REGISTER_OPERATIONS
4478 : && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
4479 : tmode = word_mode;
4480 21951913 : HOST_WIDE_INT nzop0 = nonzero_bits (trueop0, tmode);
4481 21951913 : HOST_WIDE_INT nzop1;
4482 21951913 : if (CONST_INT_P (trueop1))
4483 : {
4484 18587986 : HOST_WIDE_INT val1 = INTVAL (trueop1);
4485 : /* If we are turning off bits already known off in OP0, we need
4486 : not do an AND. */
4487 18587986 : if ((nzop0 & ~val1) == 0)
4488 451134 : return op0;
4489 :
4490 : /* Canonicalize (and (subreg (lshiftrt X shift)) mask) into
4491 : (and (lshiftrt (subreg X) shift) mask).
4492 :
4493 : Keeps shift and AND in the same mode, improving recognition.
4494 : Only applied when subreg is a lowpart, shift is valid,
4495 : and no precision is lost. */
4496 18229699 : if (SUBREG_P (op0)
4497 6556536 : && subreg_lowpart_p (op0)
4498 6539547 : && !paradoxical_subreg_p (op0)
4499 810671 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
4500 : /* simplify_subreg asserts the object being accessed is not
4501 : VOIDmode or BLKmode. We may have a REG_EQUAL note which
4502 : is not simplified and the source operand is a constant,
4503 : and thus VOIDmode. Guard against that. */
4504 117632 : && GET_MODE (XEXP (XEXP (op0, 0), 0)) != VOIDmode
4505 117582 : && GET_MODE (XEXP (XEXP (op0, 0), 0)) != BLKmode
4506 117582 : && !CONST_INT_P (XEXP (XEXP (op0, 0), 0))
4507 117582 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
4508 95926 : && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
4509 95926 : && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT
4510 18325600 : && ((INTVAL (XEXP (XEXP (op0, 0), 1))
4511 95901 : + floor_log2 (val1))
4512 18229699 : < GET_MODE_PRECISION (as_a <scalar_int_mode> (mode))))
4513 : {
4514 13550 : tem = XEXP (XEXP (op0, 0), 0);
4515 13550 : if (SUBREG_P (tem))
4516 : {
4517 296 : if (subreg_lowpart_p (tem))
4518 296 : tem = SUBREG_REG (tem);
4519 : else
4520 : tem = NULL_RTX;
4521 : }
4522 296 : if (tem != NULL_RTX)
4523 : {
4524 13550 : offset = subreg_lowpart_offset (mode, GET_MODE (tem));
4525 13550 : tem = simplify_gen_subreg (mode, tem, GET_MODE (tem),
4526 13550 : offset);
4527 13550 : if (tem)
4528 : {
4529 13550 : unsigned shiftamt = INTVAL (XEXP (XEXP (op0, 0), 1));
4530 13550 : rtx shiftamtrtx = gen_int_shift_amount (mode,
4531 13550 : shiftamt);
4532 13550 : op0 = simplify_gen_binary (LSHIFTRT, mode, tem,
4533 : shiftamtrtx);
4534 13550 : return simplify_gen_binary (AND, mode, op0, op1);
4535 : }
4536 : }
4537 : }
4538 : }
4539 21580076 : nzop1 = nonzero_bits (trueop1, mode);
4540 : /* If we are clearing all the nonzero bits, the result is zero. */
4541 21580076 : if ((nzop1 & nzop0) == 0
4542 21580076 : && !side_effects_p (op0) && !side_effects_p (op1))
4543 79297 : return CONST0_RTX (mode);
4544 : }
4545 24600385 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0)
4546 24600381 : && GET_MODE_CLASS (mode) != MODE_CC)
4547 : return op0;
4548 : /* A & (~A) -> 0 */
4549 634321 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
4550 24587129 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
4551 3730 : && ! side_effects_p (op0)
4552 24594540 : && GET_MODE_CLASS (mode) != MODE_CC)
4553 3729 : return CONST0_RTX (mode);
4554 :
4555 24587082 : if (rtx tem = simplify_unsigned_cmp_minmax (AND, mode, op0, op1))
4556 : return tem;
4557 :
4558 : /* Convert (and (xor A 1) B) to (and (not A) B) when B is known to be
4559 : 0 or 1. Only bit 0 of the XOR matters, and the NOT may be folded
4560 : into an and-not instruction. */
4561 89058199 : if (HWI_COMPUTABLE_MODE_P (mode)
4562 : && is_a <scalar_int_mode> (mode, &int_mode))
4563 64471117 : for (int i = 0; i < 2; i++)
4564 : {
4565 42982887 : rtx x = i ? op1 : op0;
4566 21494631 : rtx y = i ? op0 : op1;
4567 :
4568 : /* When WORD_REGISTER_OPERATIONS is true, we need to know the
4569 : nonzero bits in WORD_MODE rather than MODE. */
4570 42982887 : scalar_int_mode tmode = as_a <scalar_int_mode> (int_mode);
4571 42982887 : if (WORD_REGISTER_OPERATIONS
4572 : && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
4573 : tmode = word_mode;
4574 :
4575 42982887 : if (GET_CODE (x) == XOR
4576 139675 : && XEXP (x, 1) == CONST1_RTX (mode)
4577 42996063 : && (nonzero_bits (y, tmode) & ~HOST_WIDE_INT_1U) == 0)
4578 6401 : return simplify_gen_binary (AND, mode,
4579 : simplify_gen_unary (NOT, mode,
4580 : XEXP (x, 0),
4581 : mode),
4582 6401 : y);
4583 : }
4584 :
4585 : /* Convert (and (plus (A - 1)) (neg A)) to 0. */
4586 24580681 : if (match_plus_neg_pattern (op0, op1, mode))
4587 2 : return CONST0_RTX (mode);
4588 :
4589 : /* Transform (and (extend X) C) into (zero_extend (and X C)) if
4590 : there are no nonzero bits of C outside of X's mode. */
4591 49161358 : if ((GET_CODE (op0) == SIGN_EXTEND
4592 24580679 : || GET_CODE (op0) == ZERO_EXTEND)
4593 95766 : && CONST_SCALAR_INT_P (trueop1)
4594 82494 : && is_a <scalar_int_mode> (mode, &int_mode)
4595 82494 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
4596 24663173 : && (wi::mask (GET_MODE_PRECISION (inner_mode), true,
4597 82494 : GET_MODE_PRECISION (int_mode))
4598 24663173 : & rtx_mode_t (trueop1, mode)) == 0)
4599 : {
4600 80428 : machine_mode imode = GET_MODE (XEXP (op0, 0));
4601 80428 : tem = immed_wide_int_const (rtx_mode_t (trueop1, mode), imode);
4602 80428 : tem = simplify_gen_binary (AND, imode, XEXP (op0, 0), tem);
4603 80428 : return simplify_gen_unary (ZERO_EXTEND, mode, tem, imode);
4604 : }
4605 :
4606 : /* Transform (and (truncate X) C) into (truncate (and X C)). This way
4607 : we might be able to further simplify the AND with X and potentially
4608 : remove the truncation altogether. */
4609 24500251 : if (GET_CODE (op0) == TRUNCATE && CONST_INT_P (trueop1))
4610 : {
4611 6 : rtx x = XEXP (op0, 0);
4612 6 : machine_mode xmode = GET_MODE (x);
4613 6 : tem = simplify_gen_binary (AND, xmode, x,
4614 6 : gen_int_mode (INTVAL (trueop1), xmode));
4615 6 : return simplify_gen_unary (TRUNCATE, mode, tem, xmode);
4616 : }
4617 :
4618 : /* Canonicalize (A | C1) & C2 as (A & C2) | (C1 & C2). */
4619 24500245 : if (GET_CODE (op0) == IOR
4620 1492151 : && CONST_INT_P (trueop1)
4621 227030 : && CONST_INT_P (XEXP (op0, 1)))
4622 : {
4623 142182 : HOST_WIDE_INT tmp = INTVAL (trueop1) & INTVAL (XEXP (op0, 1));
4624 142182 : return simplify_gen_binary (IOR, mode,
4625 : simplify_gen_binary (AND, mode,
4626 : XEXP (op0, 0), op1),
4627 142182 : gen_int_mode (tmp, mode));
4628 : }
4629 :
4630 : /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
4631 : insn (and may simplify more). */
4632 24358063 : if (GET_CODE (op0) == XOR
4633 166546 : && rtx_equal_p (XEXP (op0, 0), op1)
4634 24359710 : && ! side_effects_p (op1))
4635 1647 : return simplify_gen_binary (AND, mode,
4636 : simplify_gen_unary (NOT, mode,
4637 : XEXP (op0, 1), mode),
4638 1647 : op1);
4639 :
4640 24356416 : if (GET_CODE (op0) == XOR
4641 164899 : && rtx_equal_p (XEXP (op0, 1), op1)
4642 24357790 : && ! side_effects_p (op1))
4643 1374 : return simplify_gen_binary (AND, mode,
4644 : simplify_gen_unary (NOT, mode,
4645 : XEXP (op0, 0), mode),
4646 1374 : op1);
4647 :
4648 : /* Similarly for (~(A ^ B)) & A. */
4649 24355042 : if (GET_CODE (op0) == NOT
4650 630640 : && GET_CODE (XEXP (op0, 0)) == XOR
4651 3934 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4652 24355096 : && ! side_effects_p (op1))
4653 54 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4654 :
4655 24354988 : if (GET_CODE (op0) == NOT
4656 630586 : && GET_CODE (XEXP (op0, 0)) == XOR
4657 3880 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4658 24355025 : && ! side_effects_p (op1))
4659 37 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4660 :
4661 : /* Convert (A | B) & A to A. */
4662 24354951 : if (GET_CODE (op0) == IOR
4663 1349969 : && (rtx_equal_p (XEXP (op0, 0), op1)
4664 1349444 : || rtx_equal_p (XEXP (op0, 1), op1))
4665 752 : && ! side_effects_p (XEXP (op0, 0))
4666 24355703 : && ! side_effects_p (XEXP (op0, 1)))
4667 : return op1;
4668 :
4669 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
4670 : ((A & N) + B) & M -> (A + B) & M
4671 : Similarly if (N & M) == 0,
4672 : ((A | N) + B) & M -> (A + B) & M
4673 : and for - instead of + and/or ^ instead of |.
4674 : Also, if (N & M) == 0, then
4675 : (A +- N) & M -> A & M. */
4676 24354199 : if (CONST_INT_P (trueop1)
4677 18012534 : && HWI_COMPUTABLE_MODE_P (mode)
4678 17989959 : && ~UINTVAL (trueop1)
4679 17989959 : && (UINTVAL (trueop1) & (UINTVAL (trueop1) + 1)) == 0
4680 36308838 : && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS))
4681 : {
4682 998105 : rtx pmop[2];
4683 998105 : int which;
4684 :
4685 998105 : pmop[0] = XEXP (op0, 0);
4686 998105 : pmop[1] = XEXP (op0, 1);
4687 :
4688 998105 : if (CONST_INT_P (pmop[1])
4689 491999 : && (UINTVAL (pmop[1]) & UINTVAL (trueop1)) == 0)
4690 172890 : return simplify_gen_binary (AND, mode, pmop[0], op1);
4691 :
4692 2498709 : for (which = 0; which < 2; which++)
4693 : {
4694 1665806 : tem = pmop[which];
4695 1665806 : switch (GET_CODE (tem))
4696 : {
4697 15053 : case AND:
4698 15053 : if (CONST_INT_P (XEXP (tem, 1))
4699 13452 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1))
4700 : == UINTVAL (trueop1))
4701 7679 : pmop[which] = XEXP (tem, 0);
4702 : break;
4703 1310 : case IOR:
4704 1310 : case XOR:
4705 1310 : if (CONST_INT_P (XEXP (tem, 1))
4706 140 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1)) == 0)
4707 9 : pmop[which] = XEXP (tem, 0);
4708 : break;
4709 : default:
4710 : break;
4711 : }
4712 : }
4713 :
4714 832903 : if (pmop[0] != XEXP (op0, 0) || pmop[1] != XEXP (op0, 1))
4715 : {
4716 7688 : tem = simplify_gen_binary (GET_CODE (op0), mode,
4717 : pmop[0], pmop[1]);
4718 7688 : return simplify_gen_binary (code, mode, tem, op1);
4719 : }
4720 : }
4721 :
4722 : /* (and X (ior (not X) Y) -> (and X Y) */
4723 24181309 : if (GET_CODE (op1) == IOR
4724 1044395 : && GET_CODE (XEXP (op1, 0)) == NOT
4725 24186862 : && rtx_equal_p (op0, XEXP (XEXP (op1, 0), 0)))
4726 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 1));
4727 :
4728 : /* (and (ior (not X) Y) X) -> (and X Y) */
4729 24181309 : if (GET_CODE (op0) == IOR
4730 1349217 : && GET_CODE (XEXP (op0, 0)) == NOT
4731 24236573 : && rtx_equal_p (op1, XEXP (XEXP (op0, 0), 0)))
4732 6 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 1));
4733 :
4734 : /* (and X (ior Y (not X)) -> (and X Y) */
4735 24181303 : if (GET_CODE (op1) == IOR
4736 1044395 : && GET_CODE (XEXP (op1, 1)) == NOT
4737 24181867 : && rtx_equal_p (op0, XEXP (XEXP (op1, 1), 0)))
4738 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 0));
4739 :
4740 : /* (and (ior Y (not X)) X) -> (and X Y) */
4741 24181303 : if (GET_CODE (op0) == IOR
4742 1349211 : && GET_CODE (XEXP (op0, 1)) == NOT
4743 24188660 : && rtx_equal_p (op1, XEXP (XEXP (op0, 1), 0)))
4744 76 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 0));
4745 :
4746 : /* (and (ior/xor X Y) (not Y)) -> X & ~Y */
4747 24181227 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4748 24181227 : && negated_ops_p (op1, XEXP (op0, 1)))
4749 40 : return simplify_gen_binary (AND, mode, XEXP (op0, 0),
4750 : simplify_gen_unary (NOT, mode,
4751 : XEXP (op0, 1),
4752 40 : mode));
4753 : /* (and (ior/xor Y X) (not Y)) -> X & ~Y */
4754 24181187 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4755 24181187 : && negated_ops_p (op1, XEXP (op0, 0)))
4756 1 : return simplify_gen_binary (AND, mode, XEXP (op0, 1),
4757 : simplify_gen_unary (NOT, mode,
4758 : XEXP (op0, 0),
4759 1 : mode));
4760 :
4761 : /* Convert (and (ior A C) (ior B C)) into (ior (and A B) C). */
4762 24181186 : if (GET_CODE (op0) == GET_CODE (op1)
4763 2453009 : && (GET_CODE (op0) == AND
4764 : || GET_CODE (op0) == IOR
4765 2453009 : || GET_CODE (op0) == LSHIFTRT
4766 1409227 : || GET_CODE (op0) == ASHIFTRT
4767 1409075 : || GET_CODE (op0) == ASHIFT
4768 1408909 : || GET_CODE (op0) == ROTATE
4769 1408909 : || GET_CODE (op0) == ROTATERT))
4770 : {
4771 1044100 : tem = simplify_distributive_operation (code, mode, op0, op1);
4772 1044100 : if (tem)
4773 : return tem;
4774 : }
4775 :
4776 : /* (and:v4si
4777 : (ashiftrt:v4si A 16)
4778 : (const_vector: 0xffff x4))
4779 : is just (lshiftrt:v4si A 16). */
4780 23186598 : if (VECTOR_MODE_P (mode) && GET_CODE (op0) == ASHIFTRT
4781 4839 : && (CONST_INT_P (XEXP (op0, 1))
4782 2035 : || (GET_CODE (XEXP (op0, 1)) == CONST_VECTOR
4783 94 : && const_vec_duplicate_p (XEXP (op0, 1))
4784 0 : && CONST_INT_P (XVECEXP (XEXP (op0, 1), 0, 0))))
4785 2804 : && GET_CODE (op1) == CONST_VECTOR
4786 23186626 : && const_vec_duplicate_p (op1)
4787 23186668 : && CONST_INT_P (XVECEXP (op1, 0, 0)))
4788 : {
4789 136 : unsigned HOST_WIDE_INT shift_count
4790 : = (CONST_INT_P (XEXP (op0, 1))
4791 68 : ? UINTVAL (XEXP (op0, 1))
4792 0 : : UINTVAL (XVECEXP (XEXP (op0, 1), 0, 0)));
4793 68 : unsigned HOST_WIDE_INT inner_prec
4794 136 : = GET_MODE_PRECISION (GET_MODE_INNER (mode));
4795 :
4796 : /* Avoid UD shift count. */
4797 68 : if (shift_count < inner_prec
4798 56 : && (UINTVAL (XVECEXP (op1, 0, 0))
4799 56 : == (HOST_WIDE_INT_1U << (inner_prec - shift_count)) - 1))
4800 42 : return simplify_gen_binary (LSHIFTRT, mode, XEXP (op0, 0), XEXP (op0, 1));
4801 : }
4802 :
4803 23186556 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4804 23186556 : if (tem)
4805 : return tem;
4806 :
4807 23184106 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4808 23184106 : if (tem)
4809 : return tem;
4810 :
4811 23183825 : tem = simplify_associative_operation (code, mode, op0, op1);
4812 23183825 : if (tem)
4813 : return tem;
4814 : break;
4815 :
4816 924591 : case UDIV:
4817 : /* 0/x is 0 (or x&0 if x has side-effects). */
4818 924591 : if (trueop0 == CONST0_RTX (mode)
4819 395 : && !cfun->can_throw_non_call_exceptions)
4820 : {
4821 395 : if (side_effects_p (op1))
4822 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4823 : return trueop0;
4824 : }
4825 : /* x/1 is x. */
4826 924196 : if (trueop1 == CONST1_RTX (mode))
4827 : {
4828 246430 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4829 246430 : if (tem)
4830 : return tem;
4831 : }
4832 : /* Convert divide by power of two into shift. */
4833 677766 : if (CONST_INT_P (trueop1)
4834 1004621 : && (val = exact_log2 (UINTVAL (trueop1))) > 0)
4835 326855 : return simplify_gen_binary (LSHIFTRT, mode, op0,
4836 326855 : gen_int_shift_amount (mode, val));
4837 : break;
4838 :
4839 1484659 : case DIV:
4840 : /* Handle floating point and integers separately. */
4841 1484659 : if (SCALAR_FLOAT_MODE_P (mode))
4842 : {
4843 : /* Maybe change 0.0 / x to 0.0. This transformation isn't
4844 : safe for modes with NaNs, since 0.0 / 0.0 will then be
4845 : NaN rather than 0.0. Nor is it safe for modes with signed
4846 : zeros, since dividing 0 by a negative number gives -0.0 */
4847 338974 : if (trueop0 == CONST0_RTX (mode)
4848 2825 : && !HONOR_NANS (mode)
4849 14 : && !HONOR_SIGNED_ZEROS (mode)
4850 338988 : && ! side_effects_p (op1))
4851 : return op0;
4852 : /* x/1.0 is x. */
4853 338960 : if (trueop1 == CONST1_RTX (mode)
4854 338960 : && !HONOR_SNANS (mode))
4855 : return op0;
4856 :
4857 338956 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
4858 34509 : && trueop1 != CONST0_RTX (mode))
4859 : {
4860 28376 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
4861 :
4862 : /* x/-1.0 is -x. */
4863 28376 : if (real_equal (d1, &dconstm1)
4864 28376 : && !HONOR_SNANS (mode))
4865 0 : return simplify_gen_unary (NEG, mode, op0, mode);
4866 :
4867 : /* Change FP division by a constant into multiplication.
4868 : Only do this with -freciprocal-math. */
4869 28376 : if (flag_reciprocal_math
4870 28376 : && !real_equal (d1, &dconst0))
4871 : {
4872 7 : REAL_VALUE_TYPE d;
4873 7 : real_arithmetic (&d, RDIV_EXPR, &dconst1, d1);
4874 7 : tem = const_double_from_real_value (d, mode);
4875 7 : return simplify_gen_binary (MULT, mode, op0, tem);
4876 : }
4877 : }
4878 : }
4879 1145685 : else if (SCALAR_INT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
4880 : {
4881 : /* 0/x is 0 (or x&0 if x has side-effects). */
4882 1113541 : if (trueop0 == CONST0_RTX (mode)
4883 1115 : && !cfun->can_throw_non_call_exceptions)
4884 : {
4885 1061 : if (side_effects_p (op1))
4886 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4887 : return trueop0;
4888 : }
4889 : /* x/1 is x. */
4890 1112480 : if (trueop1 == CONST1_RTX (mode))
4891 : {
4892 96 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4893 96 : if (tem)
4894 : return tem;
4895 : }
4896 : /* x/-1 is -x. */
4897 1112384 : if (trueop1 == CONSTM1_RTX (mode))
4898 : {
4899 622 : rtx x = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4900 622 : if (x)
4901 622 : return simplify_gen_unary (NEG, mode, x, mode);
4902 : }
4903 : }
4904 : break;
4905 :
4906 939768 : case UMOD:
4907 : /* 0%x is 0 (or x&0 if x has side-effects). */
4908 939768 : if (trueop0 == CONST0_RTX (mode))
4909 : {
4910 932 : if (side_effects_p (op1))
4911 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4912 : return trueop0;
4913 : }
4914 : /* x%1 is 0 (of x&0 if x has side-effects). */
4915 938836 : if (trueop1 == CONST1_RTX (mode))
4916 : {
4917 274219 : if (side_effects_p (op0))
4918 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4919 274219 : return CONST0_RTX (mode);
4920 : }
4921 : /* Implement modulus by power of two as AND. */
4922 664617 : if (CONST_INT_P (trueop1)
4923 964974 : && exact_log2 (UINTVAL (trueop1)) > 0)
4924 300357 : return simplify_gen_binary (AND, mode, op0,
4925 300357 : gen_int_mode (UINTVAL (trueop1) - 1,
4926 : mode));
4927 : break;
4928 :
4929 663724 : case MOD:
4930 : /* 0%x is 0 (or x&0 if x has side-effects). */
4931 663724 : if (trueop0 == CONST0_RTX (mode))
4932 : {
4933 1370 : if (side_effects_p (op1))
4934 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4935 : return trueop0;
4936 : }
4937 : /* x%1 and x%-1 is 0 (or x&0 if x has side-effects). */
4938 662354 : if (trueop1 == CONST1_RTX (mode) || trueop1 == constm1_rtx)
4939 : {
4940 601 : if (side_effects_p (op0))
4941 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4942 601 : return CONST0_RTX (mode);
4943 : }
4944 : break;
4945 :
4946 142725 : case ROTATERT:
4947 142725 : case ROTATE:
4948 142725 : if (trueop1 == CONST0_RTX (mode))
4949 : return op0;
4950 :
4951 : /* Reversing the direction removes a negated count modulo the
4952 : precision. Keep the mask so that the new count is in range even
4953 : on targets that do not truncate rotate counts. */
4954 142635 : if (is_a <scalar_int_mode> (mode, &int_mode)
4955 134469 : && exact_log2 (GET_MODE_PRECISION (int_mode)) >= 0)
4956 : {
4957 134365 : rtx count = op1;
4958 134365 : if ((SUBREG_P (count)
4959 23286 : && subreg_lowpart_p (count)
4960 23286 : && !paradoxical_subreg_p (count))
4961 134365 : || GET_CODE (count) == TRUNCATE)
4962 23430 : count = XEXP (count, 0);
4963 :
4964 134365 : scalar_int_mode count_mode;
4965 134365 : if (GET_CODE (count) == AND
4966 9072 : && GET_CODE (XEXP (count, 0)) == NEG
4967 736 : && CONST_INT_P (XEXP (count, 1))
4968 736 : && INTVAL (XEXP (count, 1))
4969 736 : == GET_MODE_PRECISION (int_mode) - 1
4970 504 : && is_a <scalar_int_mode> (GET_MODE (count), &inner_mode)
4971 142699 : && is_a <scalar_int_mode> (GET_MODE (op1), &count_mode)
4972 1008 : && GET_MODE_PRECISION (count_mode)
4973 1008 : > exact_log2 (GET_MODE_PRECISION (int_mode))
4974 134365 : && GET_MODE_PRECISION (inner_mode)
4975 440 : >= GET_MODE_PRECISION (count_mode))
4976 : {
4977 440 : tem = simplify_gen_binary (AND, inner_mode,
4978 : XEXP (XEXP (count, 0), 0),
4979 : XEXP (count, 1));
4980 440 : if (count != op1)
4981 240 : tem = simplify_gen_unary (TRUNCATE, count_mode, tem,
4982 : inner_mode);
4983 440 : return simplify_gen_binary (code == ROTATE ? ROTATERT : ROTATE,
4984 440 : mode, op0, tem);
4985 : }
4986 : }
4987 :
4988 : /* Canonicalize rotates by constant amount. If the condition of
4989 : reversing direction is met, then reverse the direction. */
4990 : #if defined(HAVE_rotate) && defined(HAVE_rotatert)
4991 142195 : if (reverse_rotate_by_imm_p (mode, (code == ROTATE), trueop1))
4992 : {
4993 12173 : int new_amount = GET_MODE_UNIT_PRECISION (mode) - INTVAL (trueop1);
4994 12173 : rtx new_amount_rtx = gen_int_shift_amount (mode, new_amount);
4995 12899 : return simplify_gen_binary (code == ROTATE ? ROTATERT : ROTATE,
4996 : mode, op0, new_amount_rtx);
4997 : }
4998 : #endif
4999 : /* ROTATE/ROTATERT:HI (X:HI, 8) is BSWAP:HI (X). Other combinations
5000 : such as SImode with a count of 16 do not correspond to RTL BSWAP
5001 : semantics. */
5002 130022 : tem = unwrap_const_vec_duplicate (trueop1);
5003 130022 : if (GET_MODE_UNIT_BITSIZE (mode) == (2 * BITS_PER_UNIT)
5004 130022 : && CONST_INT_P (tem) && INTVAL (tem) == BITS_PER_UNIT)
5005 666 : return simplify_gen_unary (BSWAP, mode, op0, mode);
5006 :
5007 : /* FALLTHRU */
5008 5667560 : case ASHIFTRT:
5009 5667560 : if (trueop1 == CONST0_RTX (mode))
5010 : return op0;
5011 5665180 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
5012 : return op0;
5013 : /* Rotating ~0 always results in ~0. */
5014 5665035 : if (CONST_INT_P (trueop0)
5015 15415 : && HWI_COMPUTABLE_MODE_P (mode)
5016 15387 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
5017 5665035 : && ! side_effects_p (op1))
5018 : return op0;
5019 :
5020 33193932 : canonicalize_shift:
5021 : /* Given:
5022 : scalar modes M1, M2
5023 : scalar constants c1, c2
5024 : size (M2) > size (M1)
5025 : c1 == size (M2) - size (M1)
5026 : optimize:
5027 : ([a|l]shiftrt:M1 (subreg:M1 (lshiftrt:M2 (reg:M2) (const_int <c1>))
5028 : <low_part>)
5029 : (const_int <c2>))
5030 : to:
5031 : (subreg:M1 ([a|l]shiftrt:M2 (reg:M2) (const_int <c1 + c2>))
5032 : <low_part>). */
5033 33193932 : if ((code == ASHIFTRT || code == LSHIFTRT)
5034 12651959 : && is_a <scalar_int_mode> (mode, &int_mode)
5035 11859833 : && SUBREG_P (op0)
5036 1269690 : && CONST_INT_P (op1)
5037 1266709 : && GET_CODE (SUBREG_REG (op0)) == LSHIFTRT
5038 20587 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
5039 : &inner_mode)
5040 20587 : && CONST_INT_P (XEXP (SUBREG_REG (op0), 1))
5041 40788 : && GET_MODE_BITSIZE (inner_mode) > GET_MODE_BITSIZE (int_mode)
5042 20394 : && (INTVAL (XEXP (SUBREG_REG (op0), 1))
5043 40788 : == GET_MODE_BITSIZE (inner_mode) - GET_MODE_BITSIZE (int_mode))
5044 33214067 : && subreg_lowpart_p (op0))
5045 : {
5046 20135 : rtx tmp = gen_int_shift_amount
5047 20135 : (inner_mode, INTVAL (XEXP (SUBREG_REG (op0), 1)) + INTVAL (op1));
5048 :
5049 : /* Combine would usually zero out the value when combining two
5050 : local shifts and the range becomes larger or equal to the mode.
5051 : However since we fold away one of the shifts here combine won't
5052 : see it so we should immediately zero the result if it's out of
5053 : range. */
5054 20135 : if (code == LSHIFTRT
5055 36693 : && INTVAL (tmp) >= GET_MODE_BITSIZE (inner_mode))
5056 0 : tmp = const0_rtx;
5057 : else
5058 20135 : tmp = simplify_gen_binary (code,
5059 : inner_mode,
5060 20135 : XEXP (SUBREG_REG (op0), 0),
5061 : tmp);
5062 :
5063 20135 : return lowpart_subreg (int_mode, tmp, inner_mode);
5064 : }
5065 :
5066 33173797 : if (SHIFT_COUNT_TRUNCATED && CONST_INT_P (op1))
5067 : {
5068 : val = INTVAL (op1) & (GET_MODE_UNIT_PRECISION (mode) - 1);
5069 : if (val != INTVAL (op1))
5070 : return simplify_gen_binary (code, mode, op0,
5071 : gen_int_shift_amount (mode, val));
5072 : }
5073 :
5074 : /* Simplify:
5075 :
5076 : (code:M1
5077 : (subreg:M1
5078 : ([al]shiftrt:M2
5079 : (subreg:M2
5080 : (ashift:M1 X C1))
5081 : C2))
5082 : C3)
5083 :
5084 : to:
5085 :
5086 : (code:M1
5087 : ([al]shiftrt:M1
5088 : (ashift:M1 X C1+N)
5089 : C2+N)
5090 : C3)
5091 :
5092 : where M1 is N bits wider than M2. Optimizing the (subreg:M1 ...)
5093 : directly would be arithmetically correct, but restricting the
5094 : simplification to shifts by constants is more conservative,
5095 : since it is more likely to lead to further simplifications. */
5096 33173797 : if (is_a<scalar_int_mode> (mode, &int_mode)
5097 6030435 : && paradoxical_subreg_p (op0)
5098 5503200 : && is_a<scalar_int_mode> (GET_MODE (SUBREG_REG (op0)), &inner_mode)
5099 5473941 : && (GET_CODE (SUBREG_REG (op0)) == ASHIFTRT
5100 5473941 : || GET_CODE (SUBREG_REG (op0)) == LSHIFTRT)
5101 142843 : && CONST_INT_P (op1))
5102 : {
5103 142843 : auto xcode = GET_CODE (SUBREG_REG (op0));
5104 142843 : rtx xop0 = XEXP (SUBREG_REG (op0), 0);
5105 142843 : rtx xop1 = XEXP (SUBREG_REG (op0), 1);
5106 142843 : if (SUBREG_P (xop0)
5107 10482 : && GET_MODE (SUBREG_REG (xop0)) == mode
5108 10306 : && GET_CODE (SUBREG_REG (xop0)) == ASHIFT
5109 621 : && CONST_INT_P (xop1)
5110 143464 : && UINTVAL (xop1) < GET_MODE_PRECISION (inner_mode))
5111 : {
5112 621 : rtx yop0 = XEXP (SUBREG_REG (xop0), 0);
5113 621 : rtx yop1 = XEXP (SUBREG_REG (xop0), 1);
5114 621 : if (CONST_INT_P (yop1)
5115 621 : && UINTVAL (yop1) < GET_MODE_PRECISION (inner_mode))
5116 : {
5117 1242 : auto bias = (GET_MODE_BITSIZE (int_mode)
5118 621 : - GET_MODE_BITSIZE (inner_mode));
5119 621 : tem = simplify_gen_binary (ASHIFT, mode, yop0,
5120 621 : GEN_INT (INTVAL (yop1) + bias));
5121 621 : tem = simplify_gen_binary (xcode, mode, tem,
5122 621 : GEN_INT (INTVAL (xop1) + bias));
5123 621 : return simplify_gen_binary (code, mode, tem, op1);
5124 : }
5125 : }
5126 : }
5127 : break;
5128 :
5129 0 : case SS_ASHIFT:
5130 0 : if (CONST_INT_P (trueop0)
5131 0 : && HWI_COMPUTABLE_MODE_P (mode)
5132 0 : && (UINTVAL (trueop0) == (GET_MODE_MASK (mode) >> 1)
5133 0 : || mode_signbit_p (mode, trueop0))
5134 0 : && ! side_effects_p (op1))
5135 : return op0;
5136 0 : goto simplify_ashift;
5137 :
5138 0 : case US_ASHIFT:
5139 0 : if (CONST_INT_P (trueop0)
5140 0 : && HWI_COMPUTABLE_MODE_P (mode)
5141 0 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
5142 0 : && ! side_effects_p (op1))
5143 : return op0;
5144 : /* FALLTHRU */
5145 :
5146 20837869 : case ASHIFT:
5147 20837869 : simplify_ashift:
5148 20837869 : if (trueop1 == CONST0_RTX (mode))
5149 : return op0;
5150 20663252 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
5151 : return op0;
5152 20632741 : if (mem_depth
5153 220093 : && code == ASHIFT
5154 220093 : && CONST_INT_P (trueop1)
5155 220085 : && is_a <scalar_int_mode> (mode, &int_mode)
5156 20852814 : && IN_RANGE (UINTVAL (trueop1),
5157 : 1, GET_MODE_PRECISION (int_mode) - 1))
5158 : {
5159 220073 : auto c = (wi::one (GET_MODE_PRECISION (int_mode))
5160 220073 : << UINTVAL (trueop1));
5161 220073 : rtx new_op1 = immed_wide_int_const (c, int_mode);
5162 220073 : return simplify_gen_binary (MULT, int_mode, op0, new_op1);
5163 220073 : }
5164 :
5165 : /* If we're shifting left a signed bitfield extraction and the
5166 : shift count + bitfield size is a natural integral mode and
5167 : the field starts at offset 0 (counting from the LSB), then
5168 : this can be simplified to a sign extension of a left shift.
5169 :
5170 : Some ISAs (RISC-V 64-bit) have inherent support for such
5171 : instructions and it's better for various optimizations to
5172 : express as a SIGN_EXTEND rather than a shifted SIGN_EXTRACT. */
5173 20412668 : if (GET_CODE (op0) == SIGN_EXTRACT
5174 26 : && REG_P (XEXP (op0, 0))
5175 : /* The size of the bitfield, the location of the bitfield and
5176 : shift count must be CONST_INTs. */
5177 20 : && CONST_INT_P (op1)
5178 20 : && CONST_INT_P (XEXP (op0, 1))
5179 20 : && CONST_INT_P (XEXP (op0, 2)))
5180 : {
5181 20 : int size = INTVAL (op1) + INTVAL (XEXP (op0, 1));
5182 20 : machine_mode smaller_mode;
5183 : /* Now we need to verify the size of the bitfield plus the shift
5184 : count is an integral mode and smaller than MODE. This is
5185 : requirement for using SIGN_EXTEND. We also need to verify the
5186 : field starts at bit location 0 and that the subreg lowpart also
5187 : starts at zero. */
5188 20 : if (int_mode_for_size (size, size).exists (&smaller_mode)
5189 1 : && mode > smaller_mode
5190 20 : && (subreg_lowpart_offset (smaller_mode, mode).to_constant ()
5191 1 : == UINTVAL (XEXP (op0, 2)))
5192 1 : && XEXP (op0, 2) == CONST0_RTX (mode))
5193 : {
5194 : /* Everything passed. So we just need to get the subreg of the
5195 : original input, shift it and sign extend the result. */
5196 1 : rtx op = gen_lowpart (smaller_mode, XEXP (op0, 0));
5197 1 : rtx x = gen_rtx_ASHIFT (smaller_mode, op, op1);
5198 1 : return gen_rtx_SIGN_EXTEND (mode, x);
5199 : }
5200 : }
5201 20412667 : goto canonicalize_shift;
5202 :
5203 9152085 : case LSHIFTRT:
5204 9152085 : if (trueop1 == CONST0_RTX (mode))
5205 : return op0;
5206 7117858 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
5207 : return op0;
5208 : /* Optimize (lshiftrt (clz X) C) as (eq X 0). */
5209 7116230 : if (GET_CODE (op0) == CLZ
5210 0 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
5211 0 : && CONST_INT_P (trueop1)
5212 : && STORE_FLAG_VALUE == 1
5213 7116230 : && INTVAL (trueop1) < GET_MODE_UNIT_PRECISION (mode))
5214 : {
5215 0 : unsigned HOST_WIDE_INT zero_val = 0;
5216 :
5217 0 : if (CLZ_DEFINED_VALUE_AT_ZERO (inner_mode, zero_val)
5218 0 : && zero_val == GET_MODE_PRECISION (inner_mode)
5219 0 : && INTVAL (trueop1) == exact_log2 (zero_val))
5220 0 : return simplify_gen_relational (EQ, mode, inner_mode,
5221 0 : XEXP (op0, 0), const0_rtx);
5222 : }
5223 7116230 : goto canonicalize_shift;
5224 :
5225 230135 : case SMIN:
5226 230135 : if (HWI_COMPUTABLE_MODE_P (mode)
5227 208700 : && mode_signbit_p (mode, trueop1)
5228 0 : && ! side_effects_p (op0))
5229 : return op1;
5230 230135 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5231 : return op0;
5232 229957 : tem = simplify_associative_operation (code, mode, op0, op1);
5233 229957 : if (tem)
5234 : return tem;
5235 : break;
5236 :
5237 765869 : case SMAX:
5238 765869 : if (HWI_COMPUTABLE_MODE_P (mode)
5239 737456 : && CONST_INT_P (trueop1)
5240 702815 : && (UINTVAL (trueop1) == GET_MODE_MASK (mode) >> 1)
5241 0 : && ! side_effects_p (op0))
5242 : return op1;
5243 765869 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5244 : return op0;
5245 765764 : if (INTEGRAL_MODE_P (mode))
5246 : {
5247 445 : if (GET_CODE (op0) == NEG && rtx_equal_p (XEXP (op0, 0), op1)
5248 755629 : && !side_effects_p (op0))
5249 123 : return simplify_gen_unary (ABS, mode, op1, mode);
5250 0 : else if (GET_CODE (op1) == NEG && rtx_equal_p (XEXP (op1, 0), op0)
5251 755379 : && !side_effects_p (op1))
5252 0 : return simplify_gen_unary (ABS, mode, op0, mode);
5253 : }
5254 765641 : tem = simplify_associative_operation (code, mode, op0, op1);
5255 765641 : if (tem)
5256 : return tem;
5257 : break;
5258 :
5259 415115 : case UMIN:
5260 415115 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
5261 : return op1;
5262 415040 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5263 : return op0;
5264 414921 : tem = simplify_associative_operation (code, mode, op0, op1);
5265 414921 : if (tem)
5266 : return tem;
5267 : break;
5268 :
5269 381917 : case UMAX:
5270 381917 : if (trueop1 == constm1_rtx && ! side_effects_p (op0))
5271 : return op1;
5272 381917 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5273 : return op0;
5274 381827 : tem = simplify_associative_operation (code, mode, op0, op1);
5275 381827 : if (tem)
5276 : return tem;
5277 : break;
5278 :
5279 12302 : case SS_PLUS:
5280 12302 : case US_PLUS:
5281 12302 : case SS_MINUS:
5282 12302 : case US_MINUS:
5283 : /* Simplify x +/- 0 to x, if possible. */
5284 12302 : if (trueop1 == CONST0_RTX (mode))
5285 0 : return op0;
5286 : return 0;
5287 :
5288 0 : case SS_MULT:
5289 0 : case US_MULT:
5290 : /* Simplify x * 0 to 0, if possible. */
5291 0 : if (trueop1 == CONST0_RTX (mode)
5292 0 : && !side_effects_p (op0))
5293 : return op1;
5294 :
5295 : /* Simplify x * 1 to x, if possible. */
5296 0 : if (trueop1 == CONST1_RTX (mode))
5297 0 : return op0;
5298 : return 0;
5299 :
5300 202182 : case SMUL_HIGHPART:
5301 : /* Simplify x h* 0 to 0, if possible. */
5302 202182 : if (trueop1 == CONST0_RTX (mode)
5303 202182 : && !side_effects_p (op0))
5304 : return op1;
5305 : /* Simplify x h* 1 to sign(x). */
5306 202182 : if (trueop1 == const1_rtx
5307 202182 : && is_a <scalar_int_mode> (mode, &int_mode))
5308 : {
5309 0 : HOST_WIDE_INT bits = GET_MODE_PRECISION (int_mode) - 1;
5310 0 : return simplify_gen_binary (ASHIFTRT, mode, op0,
5311 0 : gen_int_shift_amount (mode, bits));
5312 : }
5313 : /* Simplify SMUL_HIGHPART by power of two as ASHIFRT. */
5314 202182 : if (CONST_INT_P (trueop1)
5315 90563 : && INTVAL (trueop1) > 1
5316 279435 : && is_a <scalar_int_mode> (mode, &int_mode))
5317 : {
5318 77253 : HOST_WIDE_INT prec = GET_MODE_PRECISION (int_mode);
5319 77253 : val = wi::exact_log2 (INTVAL (trueop1));
5320 77253 : if (val > 0 && val < prec)
5321 0 : return simplify_gen_binary (ASHIFTRT, mode, op0,
5322 : gen_int_shift_amount (mode,
5323 0 : prec - val));
5324 : }
5325 : return 0;
5326 :
5327 329378 : case UMUL_HIGHPART:
5328 : /* Simplify x h* 0 to 0, if possible. */
5329 329378 : if (trueop1 == CONST0_RTX (mode)
5330 329378 : && !side_effects_p (op0))
5331 : return op1;
5332 : /* Simplify x h* 1 to 0. */
5333 329304 : if (trueop1 == CONST1_RTX (mode)
5334 329304 : && !side_effects_p (op0))
5335 0 : return CONST0_RTX (mode);
5336 : /* Simplify UMUL_HIGHPART by power of two as LSHIFRT. */
5337 329304 : if (CONST_INT_P (trueop1)
5338 167600 : && UINTVAL (trueop1) > 1
5339 496898 : && is_a <scalar_int_mode> (mode, &int_mode))
5340 : {
5341 167594 : HOST_WIDE_INT prec = GET_MODE_PRECISION (int_mode);
5342 167594 : val = wi::exact_log2 (UINTVAL (trueop1));
5343 167594 : if (val > 0 && val < prec)
5344 58 : return simplify_gen_binary (LSHIFTRT, mode, op0,
5345 : gen_int_shift_amount (mode,
5346 58 : prec - val));
5347 : }
5348 : return 0;
5349 :
5350 0 : case SS_DIV:
5351 0 : case US_DIV:
5352 : /* Simplify x / 1 to x, if possible. */
5353 0 : if (trueop1 == CONST1_RTX (mode))
5354 0 : return op0;
5355 : return 0;
5356 :
5357 0 : case COPYSIGN:
5358 0 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
5359 : return op0;
5360 0 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1))
5361 : {
5362 0 : REAL_VALUE_TYPE f1;
5363 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
5364 0 : rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
5365 0 : if (REAL_VALUE_NEGATIVE (f1))
5366 0 : tmp = simplify_unary_operation (NEG, mode, tmp, mode);
5367 0 : return tmp;
5368 : }
5369 0 : if (GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
5370 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
5371 0 : if (GET_CODE (op1) == ABS
5372 0 : && ! side_effects_p (op1))
5373 0 : return simplify_gen_unary (ABS, mode, op0, mode);
5374 0 : if (GET_CODE (op0) == COPYSIGN
5375 0 : && ! side_effects_p (XEXP (op0, 1)))
5376 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
5377 0 : if (GET_CODE (op1) == COPYSIGN
5378 0 : && ! side_effects_p (XEXP (op1, 0)))
5379 0 : return simplify_gen_binary (COPYSIGN, mode, op0, XEXP (op1, 1));
5380 : return 0;
5381 :
5382 1176 : case VEC_SERIES:
5383 2352 : if (op1 == CONST0_RTX (GET_MODE_INNER (mode)))
5384 92 : return gen_vec_duplicate (mode, op0);
5385 1084 : if (valid_for_const_vector_p (mode, op0)
5386 1084 : && valid_for_const_vector_p (mode, op1))
5387 93 : return gen_const_vec_series (mode, op0, op1);
5388 : return 0;
5389 :
5390 3767096 : case VEC_SELECT:
5391 3767096 : if (!VECTOR_MODE_P (mode))
5392 : {
5393 864115 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
5394 1728230 : gcc_assert (mode == GET_MODE_INNER (GET_MODE (trueop0)));
5395 864115 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
5396 864115 : gcc_assert (XVECLEN (trueop1, 0) == 1);
5397 :
5398 : /* We can't reason about selections made at runtime. */
5399 864115 : if (!CONST_INT_P (XVECEXP (trueop1, 0, 0)))
5400 466533967 : return 0;
5401 :
5402 864115 : if (vec_duplicate_p (trueop0, &elt0))
5403 2162 : return elt0;
5404 :
5405 861953 : if (GET_CODE (trueop0) == CONST_VECTOR)
5406 7363 : return CONST_VECTOR_ELT (trueop0, INTVAL (XVECEXP
5407 : (trueop1, 0, 0)));
5408 :
5409 : /* Extract a scalar element from a nested VEC_SELECT expression
5410 : (with optional nested VEC_CONCAT expression). Some targets
5411 : (i386) extract scalar element from a vector using chain of
5412 : nested VEC_SELECT expressions. When input operand is a memory
5413 : operand, this operation can be simplified to a simple scalar
5414 : load from an offsetted memory address. */
5415 854590 : int n_elts;
5416 854590 : if (GET_CODE (trueop0) == VEC_SELECT
5417 932153 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
5418 77563 : .is_constant (&n_elts)))
5419 : {
5420 77563 : rtx op0 = XEXP (trueop0, 0);
5421 77563 : rtx op1 = XEXP (trueop0, 1);
5422 :
5423 77563 : int i = INTVAL (XVECEXP (trueop1, 0, 0));
5424 77563 : int elem;
5425 :
5426 77563 : rtvec vec;
5427 77563 : rtx tmp_op, tmp;
5428 :
5429 77563 : gcc_assert (GET_CODE (op1) == PARALLEL);
5430 77563 : gcc_assert (i < XVECLEN (op1, 0));
5431 :
5432 : /* Select element, pointed by nested selector. */
5433 77563 : elem = INTVAL (XVECEXP (op1, 0, i));
5434 :
5435 77563 : gcc_assert (elem < n_elts);
5436 :
5437 : /* Handle the case when nested VEC_SELECT wraps VEC_CONCAT. */
5438 77563 : if (GET_CODE (op0) == VEC_CONCAT)
5439 : {
5440 32239 : rtx op00 = XEXP (op0, 0);
5441 32239 : rtx op01 = XEXP (op0, 1);
5442 :
5443 32239 : machine_mode mode00, mode01;
5444 32239 : int n_elts00, n_elts01;
5445 :
5446 32239 : mode00 = GET_MODE (op00);
5447 32239 : mode01 = GET_MODE (op01);
5448 :
5449 : /* Find out the number of elements of each operand.
5450 : Since the concatenated result has a constant number
5451 : of elements, the operands must too. */
5452 32239 : n_elts00 = GET_MODE_NUNITS (mode00).to_constant ();
5453 32239 : n_elts01 = GET_MODE_NUNITS (mode01).to_constant ();
5454 :
5455 32239 : gcc_assert (n_elts == n_elts00 + n_elts01);
5456 :
5457 : /* Select correct operand of VEC_CONCAT
5458 : and adjust selector. */
5459 32239 : if (elem < n_elts01)
5460 : tmp_op = op00;
5461 : else
5462 : {
5463 38 : tmp_op = op01;
5464 38 : elem -= n_elts00;
5465 : }
5466 : }
5467 : else
5468 : tmp_op = op0;
5469 :
5470 77563 : vec = rtvec_alloc (1);
5471 77563 : RTVEC_ELT (vec, 0) = GEN_INT (elem);
5472 :
5473 77563 : tmp = gen_rtx_fmt_ee (code, mode,
5474 : tmp_op, gen_rtx_PARALLEL (VOIDmode, vec));
5475 77563 : return tmp;
5476 : }
5477 : /* If we select one half of a vec_concat, return that. */
5478 777027 : else if (GET_CODE (trueop0) == VEC_CONCAT)
5479 : {
5480 673 : rtx subop0 = XEXP (trueop0, 0);
5481 673 : rtx subop1 = XEXP (trueop0, 1);
5482 673 : machine_mode mode0 = GET_MODE (subop0);
5483 673 : machine_mode mode1 = GET_MODE (subop1);
5484 673 : int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5485 673 : if (i0 == 0 && mode == mode0 && !side_effects_p (subop1))
5486 466533967 : return subop0;
5487 392 : if (known_eq (i0, GET_MODE_NUNITS (mode0))
5488 196 : && mode == mode1 && !side_effects_p (subop0))
5489 : return subop1;
5490 : }
5491 : }
5492 : else
5493 : {
5494 2902981 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
5495 8708943 : gcc_assert (GET_MODE_INNER (mode)
5496 : == GET_MODE_INNER (GET_MODE (trueop0)));
5497 2902981 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
5498 :
5499 2902981 : if (vec_duplicate_p (trueop0, &elt0))
5500 : /* It doesn't matter which elements are selected by trueop1,
5501 : because they are all the same. */
5502 17849 : return gen_vec_duplicate (mode, elt0);
5503 :
5504 2885132 : if (GET_CODE (trueop0) == CONST_VECTOR)
5505 : {
5506 19059 : unsigned n_elts = XVECLEN (trueop1, 0);
5507 19059 : rtvec v = rtvec_alloc (n_elts);
5508 19059 : unsigned int i;
5509 :
5510 38118 : gcc_assert (known_eq (n_elts, GET_MODE_NUNITS (mode)));
5511 87943 : for (i = 0; i < n_elts; i++)
5512 : {
5513 68884 : rtx x = XVECEXP (trueop1, 0, i);
5514 :
5515 68884 : if (!CONST_INT_P (x))
5516 : return 0;
5517 :
5518 68884 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0,
5519 : INTVAL (x));
5520 : }
5521 :
5522 19059 : return gen_rtx_CONST_VECTOR (mode, v);
5523 : }
5524 :
5525 : /* Recognize the identity. */
5526 2866073 : if (GET_MODE (trueop0) == mode)
5527 : {
5528 625863 : bool maybe_ident = true;
5529 625863 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
5530 : {
5531 625470 : rtx j = XVECEXP (trueop1, 0, i);
5532 625470 : if (!CONST_INT_P (j) || INTVAL (j) != i)
5533 : {
5534 : maybe_ident = false;
5535 : break;
5536 : }
5537 : }
5538 387385 : if (maybe_ident)
5539 : return trueop0;
5540 : }
5541 :
5542 : /* If we select a low-part subreg, return that. */
5543 2865680 : if (vec_series_lowpart_p (mode, GET_MODE (trueop0), trueop1))
5544 : {
5545 0 : rtx new_rtx = lowpart_subreg (mode, trueop0,
5546 0 : GET_MODE (trueop0));
5547 0 : if (new_rtx != NULL_RTX)
5548 : return new_rtx;
5549 : }
5550 :
5551 : /* If we build {a,b} then permute it, build the result directly. */
5552 2865680 : if (XVECLEN (trueop1, 0) == 2
5553 609202 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5554 609202 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
5555 609202 : && GET_CODE (trueop0) == VEC_CONCAT
5556 169133 : && GET_CODE (XEXP (trueop0, 0)) == VEC_CONCAT
5557 71 : && GET_MODE (XEXP (trueop0, 0)) == mode
5558 71 : && GET_CODE (XEXP (trueop0, 1)) == VEC_CONCAT
5559 54 : && GET_MODE (XEXP (trueop0, 1)) == mode)
5560 : {
5561 54 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5562 54 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
5563 54 : rtx subop0, subop1;
5564 :
5565 54 : gcc_assert (i0 < 4 && i1 < 4);
5566 54 : subop0 = XEXP (XEXP (trueop0, i0 / 2), i0 % 2);
5567 54 : subop1 = XEXP (XEXP (trueop0, i1 / 2), i1 % 2);
5568 :
5569 54 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
5570 : }
5571 :
5572 2865626 : if (XVECLEN (trueop1, 0) == 2
5573 609148 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5574 609148 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
5575 609148 : && GET_CODE (trueop0) == VEC_CONCAT
5576 169079 : && GET_MODE (trueop0) == mode)
5577 : {
5578 2 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5579 2 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
5580 2 : rtx subop0, subop1;
5581 :
5582 2 : gcc_assert (i0 < 2 && i1 < 2);
5583 2 : subop0 = XEXP (trueop0, i0);
5584 2 : subop1 = XEXP (trueop0, i1);
5585 :
5586 2 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
5587 : }
5588 :
5589 : /* If we select one half of a vec_concat, return that. */
5590 2865624 : int l0, l1;
5591 2865624 : if (GET_CODE (trueop0) == VEC_CONCAT
5592 3753422 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
5593 1876711 : .is_constant (&l0))
5594 3753422 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 1)))
5595 1876711 : .is_constant (&l1))
5596 4742335 : && CONST_INT_P (XVECEXP (trueop1, 0, 0)))
5597 : {
5598 1876711 : rtx subop0 = XEXP (trueop0, 0);
5599 1876711 : rtx subop1 = XEXP (trueop0, 1);
5600 1876711 : machine_mode mode0 = GET_MODE (subop0);
5601 1876711 : machine_mode mode1 = GET_MODE (subop1);
5602 1876711 : int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5603 1876711 : if (i0 == 0 && !side_effects_p (op1) && mode == mode0)
5604 : {
5605 1423016 : bool success = true;
5606 1423016 : for (int i = 1; i < l0; ++i)
5607 : {
5608 1422690 : rtx j = XVECEXP (trueop1, 0, i);
5609 1422690 : if (!CONST_INT_P (j) || INTVAL (j) != i)
5610 : {
5611 : success = false;
5612 : break;
5613 : }
5614 : }
5615 1210144 : if (success)
5616 : return subop0;
5617 : }
5618 1876385 : if (i0 == l0 && !side_effects_p (op0) && mode == mode1)
5619 : {
5620 1216 : bool success = true;
5621 1216 : for (int i = 1; i < l1; ++i)
5622 : {
5623 1169 : rtx j = XVECEXP (trueop1, 0, i);
5624 1169 : if (!CONST_INT_P (j) || INTVAL (j) != i0 + i)
5625 : {
5626 : success = false;
5627 : break;
5628 : }
5629 : }
5630 542 : if (success)
5631 : return subop1;
5632 : }
5633 : }
5634 :
5635 : /* Simplify vec_select of a subreg of X to just a vec_select of X
5636 : when X has same component mode as vec_select. */
5637 2865251 : unsigned HOST_WIDE_INT subreg_offset = 0;
5638 2865251 : if (GET_CODE (trueop0) == SUBREG
5639 387432 : && GET_MODE_INNER (mode)
5640 774864 : == GET_MODE_INNER (GET_MODE (SUBREG_REG (trueop0)))
5641 29852 : && GET_MODE_NUNITS (mode).is_constant (&l1)
5642 3252683 : && constant_multiple_p (subreg_memory_offset (trueop0),
5643 29852 : GET_MODE_UNIT_BITSIZE (mode),
5644 : &subreg_offset))
5645 : {
5646 14926 : poly_uint64 nunits
5647 29852 : = GET_MODE_NUNITS (GET_MODE (SUBREG_REG (trueop0)));
5648 14926 : bool success = true;
5649 91588 : for (int i = 0; i != l1; i++)
5650 : {
5651 87011 : rtx idx = XVECEXP (trueop1, 0, i);
5652 87011 : if (!CONST_INT_P (idx)
5653 87011 : || maybe_ge (UINTVAL (idx) + subreg_offset, nunits))
5654 : {
5655 : success = false;
5656 : break;
5657 : }
5658 : }
5659 :
5660 14926 : if (success)
5661 : {
5662 4577 : rtx par = trueop1;
5663 4577 : if (subreg_offset)
5664 : {
5665 0 : rtvec vec = rtvec_alloc (l1);
5666 0 : for (int i = 0; i < l1; i++)
5667 0 : RTVEC_ELT (vec, i)
5668 0 : = GEN_INT (INTVAL (XVECEXP (trueop1, 0, i))
5669 : + subreg_offset);
5670 0 : par = gen_rtx_PARALLEL (VOIDmode, vec);
5671 : }
5672 4577 : return gen_rtx_VEC_SELECT (mode, SUBREG_REG (trueop0), par);
5673 : }
5674 : }
5675 : }
5676 :
5677 3637187 : if (XVECLEN (trueop1, 0) == 1
5678 776597 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5679 776597 : && GET_CODE (trueop0) == VEC_CONCAT)
5680 : {
5681 159 : rtx vec = trueop0;
5682 318 : offset = INTVAL (XVECEXP (trueop1, 0, 0)) * GET_MODE_SIZE (mode);
5683 :
5684 : /* Try to find the element in the VEC_CONCAT. */
5685 159 : while (GET_MODE (vec) != mode
5686 318 : && GET_CODE (vec) == VEC_CONCAT)
5687 : {
5688 159 : poly_int64 vec_size;
5689 :
5690 159 : if (CONST_INT_P (XEXP (vec, 0)))
5691 : {
5692 : /* vec_concat of two const_ints doesn't make sense with
5693 : respect to modes. */
5694 0 : if (CONST_INT_P (XEXP (vec, 1)))
5695 466533967 : return 0;
5696 :
5697 0 : vec_size = GET_MODE_SIZE (GET_MODE (trueop0))
5698 0 : - GET_MODE_SIZE (GET_MODE (XEXP (vec, 1)));
5699 : }
5700 : else
5701 318 : vec_size = GET_MODE_SIZE (GET_MODE (XEXP (vec, 0)));
5702 :
5703 159 : if (known_lt (offset, vec_size))
5704 : vec = XEXP (vec, 0);
5705 15 : else if (known_ge (offset, vec_size))
5706 : {
5707 15 : offset -= vec_size;
5708 15 : vec = XEXP (vec, 1);
5709 : }
5710 : else
5711 : break;
5712 159 : vec = avoid_constant_pool_reference (vec);
5713 : }
5714 :
5715 159 : if (GET_MODE (vec) == mode)
5716 : return vec;
5717 : }
5718 :
5719 : /* If we select elements in a vec_merge that all come from the same
5720 : operand, select from that operand directly. */
5721 3637187 : if (GET_CODE (op0) == VEC_MERGE)
5722 : {
5723 8253 : rtx trueop02 = avoid_constant_pool_reference (XEXP (op0, 2));
5724 8253 : if (CONST_INT_P (trueop02))
5725 : {
5726 1838 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop02);
5727 1838 : bool all_operand0 = true;
5728 1838 : bool all_operand1 = true;
5729 5866 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
5730 : {
5731 4028 : rtx j = XVECEXP (trueop1, 0, i);
5732 4028 : if (sel & (HOST_WIDE_INT_1U << UINTVAL (j)))
5733 : all_operand1 = false;
5734 : else
5735 1430 : all_operand0 = false;
5736 : }
5737 1838 : if (all_operand0 && !side_effects_p (XEXP (op0, 1)))
5738 1226 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 0), op1);
5739 612 : if (all_operand1 && !side_effects_p (XEXP (op0, 0)))
5740 55 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 1), op1);
5741 : }
5742 : }
5743 :
5744 : /* If we have two nested selects that are inverses of each
5745 : other, replace them with the source operand. */
5746 3635906 : if (GET_CODE (trueop0) == VEC_SELECT
5747 74043 : && GET_MODE (XEXP (trueop0, 0)) == mode)
5748 : {
5749 1016 : rtx op0_subop1 = XEXP (trueop0, 1);
5750 1016 : gcc_assert (GET_CODE (op0_subop1) == PARALLEL);
5751 2032 : gcc_assert (known_eq (XVECLEN (trueop1, 0), GET_MODE_NUNITS (mode)));
5752 : bool identical_p = true;
5753 :
5754 : /* Apply the outer ordering vector to the inner one. (The inner
5755 : ordering vector is expressly permitted to be of a different
5756 : length than the outer one.) If the result is { 0, 1, ..., n-1 }
5757 : then the two VEC_SELECTs cancel. */
5758 8908 : for (int i = 0; i < XVECLEN (trueop1, 0); ++i)
5759 : {
5760 7892 : rtx x = XVECEXP (trueop1, 0, i);
5761 7892 : if (!CONST_INT_P (x))
5762 : return 0;
5763 7892 : rtx y = XVECEXP (op0_subop1, 0, INTVAL (x));
5764 7892 : if (!CONST_INT_P (y))
5765 : return 0;
5766 7892 : if (i != INTVAL (y))
5767 5822 : identical_p = false;
5768 : }
5769 1016 : if (identical_p)
5770 : return XEXP (trueop0, 0);
5771 :
5772 : /* Otherwise a permutation of a permutation is a permutation. */
5773 1016 : int len = XVECLEN (trueop1, 0);
5774 1016 : rtvec vec = rtvec_alloc (len);
5775 9924 : for (int i = 0; i < len; ++i)
5776 : {
5777 7892 : rtx x = XVECEXP (trueop1, 0, i);
5778 7892 : rtx y = XVECEXP (op0_subop1, 0, INTVAL (x));
5779 7892 : RTVEC_ELT (vec, i) = y;
5780 : }
5781 1016 : return gen_rtx_fmt_ee (code, mode, XEXP (trueop0, 0),
5782 : gen_rtx_PARALLEL (VOIDmode, vec));
5783 : }
5784 :
5785 : return 0;
5786 4095917 : case VEC_CONCAT:
5787 4095917 : {
5788 4095917 : machine_mode op0_mode = (GET_MODE (trueop0) != VOIDmode
5789 4095917 : ? GET_MODE (trueop0)
5790 4095917 : : GET_MODE_INNER (mode));
5791 4095917 : machine_mode op1_mode = (GET_MODE (trueop1) != VOIDmode
5792 4095917 : ? GET_MODE (trueop1)
5793 4095917 : : GET_MODE_INNER (mode));
5794 :
5795 4095917 : gcc_assert (VECTOR_MODE_P (mode));
5796 16383668 : gcc_assert (known_eq (GET_MODE_SIZE (op0_mode)
5797 : + GET_MODE_SIZE (op1_mode),
5798 : GET_MODE_SIZE (mode)));
5799 :
5800 4095917 : if (VECTOR_MODE_P (op0_mode))
5801 6417522 : gcc_assert (GET_MODE_INNER (mode)
5802 : == GET_MODE_INNER (op0_mode));
5803 : else
5804 3913486 : gcc_assert (GET_MODE_INNER (mode) == op0_mode);
5805 :
5806 4095917 : if (VECTOR_MODE_P (op1_mode))
5807 6417522 : gcc_assert (GET_MODE_INNER (mode)
5808 : == GET_MODE_INNER (op1_mode));
5809 : else
5810 3913486 : gcc_assert (GET_MODE_INNER (mode) == op1_mode);
5811 :
5812 4095917 : unsigned int n_elts, in_n_elts;
5813 4095917 : if ((GET_CODE (trueop0) == CONST_VECTOR
5814 4095917 : || CONST_SCALAR_INT_P (trueop0)
5815 4069321 : || CONST_DOUBLE_AS_FLOAT_P (trueop0))
5816 28047 : && (GET_CODE (trueop1) == CONST_VECTOR
5817 28047 : || CONST_SCALAR_INT_P (trueop1)
5818 28047 : || CONST_DOUBLE_AS_FLOAT_P (trueop1))
5819 0 : && GET_MODE_NUNITS (mode).is_constant (&n_elts)
5820 4095917 : && GET_MODE_NUNITS (op0_mode).is_constant (&in_n_elts))
5821 : {
5822 0 : rtvec v = rtvec_alloc (n_elts);
5823 0 : unsigned int i;
5824 0 : for (i = 0; i < n_elts; i++)
5825 : {
5826 0 : if (i < in_n_elts)
5827 : {
5828 0 : if (!VECTOR_MODE_P (op0_mode))
5829 0 : RTVEC_ELT (v, i) = trueop0;
5830 : else
5831 0 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0, i);
5832 : }
5833 : else
5834 : {
5835 0 : if (!VECTOR_MODE_P (op1_mode))
5836 0 : RTVEC_ELT (v, i) = trueop1;
5837 : else
5838 0 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop1,
5839 : i - in_n_elts);
5840 : }
5841 : }
5842 :
5843 0 : return gen_rtx_CONST_VECTOR (mode, v);
5844 : }
5845 :
5846 : /* Try to merge two VEC_SELECTs from the same vector into a single one.
5847 : Restrict the transformation to avoid generating a VEC_SELECT with a
5848 : mode unrelated to its operand. */
5849 4095917 : if (GET_CODE (trueop0) == VEC_SELECT
5850 180134 : && GET_CODE (trueop1) == VEC_SELECT
5851 36189 : && rtx_equal_p (XEXP (trueop0, 0), XEXP (trueop1, 0))
5852 4114625 : && GET_MODE_INNER (GET_MODE (XEXP (trueop0, 0)))
5853 37416 : == GET_MODE_INNER(mode))
5854 : {
5855 18708 : rtx par0 = XEXP (trueop0, 1);
5856 18708 : rtx par1 = XEXP (trueop1, 1);
5857 18708 : int len0 = XVECLEN (par0, 0);
5858 18708 : int len1 = XVECLEN (par1, 0);
5859 18708 : rtvec vec = rtvec_alloc (len0 + len1);
5860 131991 : for (int i = 0; i < len0; i++)
5861 94575 : RTVEC_ELT (vec, i) = XVECEXP (par0, 0, i);
5862 113283 : for (int i = 0; i < len1; i++)
5863 94575 : RTVEC_ELT (vec, len0 + i) = XVECEXP (par1, 0, i);
5864 18708 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (trueop0, 0),
5865 18708 : gen_rtx_PARALLEL (VOIDmode, vec));
5866 : }
5867 : /* (vec_concat:N
5868 : (subreg:N/2 OP 0)
5869 : (subreg:N/2 OP N/2)) --> OP
5870 : i.e. where concatenating the first and second halves of the
5871 : same object OP, if the target allows accessing OP in MODE. */
5872 4077209 : {
5873 4077209 : poly_uint64 offset = 0u;
5874 4077209 : rtx base0 = get_ref_base_and_offset (trueop0, &offset);
5875 4077209 : if (known_eq (offset, 0u)
5876 12229937 : && known_eq (GET_MODE_SIZE (GET_MODE (base0)),
5877 : GET_MODE_SIZE (mode)))
5878 : {
5879 68456 : rtx base1 = get_ref_base_and_offset (trueop1, &offset);
5880 68456 : if (rtx_equal_p (base0, base1)
5881 1238 : && known_eq (offset, GET_MODE_SIZE (op0_mode))
5882 1197 : && (!REG_P (base0)
5883 1196 : || targetm.modes_tieable_p (GET_MODE (base0), mode))
5884 1197 : && !side_effects_p (trueop0)
5885 69653 : && !side_effects_p (trueop1))
5886 : {
5887 1197 : tem = rtl_hooks.gen_lowpart_no_emit (mode, base0);
5888 1197 : if (tem)
5889 1197 : return tem;
5890 : }
5891 : }
5892 : }
5893 : }
5894 4076012 : return 0;
5895 :
5896 0 : default:
5897 0 : gcc_unreachable ();
5898 : }
5899 :
5900 392423668 : if (mode == GET_MODE (op0)
5901 337415422 : && mode == GET_MODE (op1)
5902 108027576 : && vec_duplicate_p (op0, &elt0)
5903 392555525 : && vec_duplicate_p (op1, &elt1))
5904 : {
5905 : /* Try applying the operator to ELT and see if that simplifies.
5906 : We can duplicate the result if so.
5907 :
5908 : The reason we don't use simplify_gen_binary is that it isn't
5909 : necessarily a win to convert things like:
5910 :
5911 : (plus:V (vec_duplicate:V (reg:S R1))
5912 : (vec_duplicate:V (reg:S R2)))
5913 :
5914 : to:
5915 :
5916 : (vec_duplicate:V (plus:S (reg:S R1) (reg:S R2)))
5917 :
5918 : The first might be done entirely in vector registers while the
5919 : second might need a move between register files. */
5920 162 : tem = simplify_binary_operation (code, GET_MODE_INNER (mode),
5921 : elt0, elt1);
5922 81 : if (tem)
5923 2 : return gen_vec_duplicate (mode, tem);
5924 : }
5925 :
5926 : return 0;
5927 : }
5928 :
5929 : /* Return true if binary operation OP distributes over addition in operand
5930 : OPNO, with the other operand being held constant. OPNO counts from 1. */
5931 :
5932 : static bool
5933 8783 : distributes_over_addition_p (rtx_code op, int opno)
5934 : {
5935 0 : switch (op)
5936 : {
5937 : case PLUS:
5938 : case MINUS:
5939 : case MULT:
5940 : return true;
5941 :
5942 0 : case ASHIFT:
5943 0 : return opno == 1;
5944 :
5945 0 : default:
5946 0 : return false;
5947 : }
5948 : }
5949 :
5950 : rtx
5951 501056533 : simplify_const_binary_operation (enum rtx_code code, machine_mode mode,
5952 : rtx op0, rtx op1)
5953 : {
5954 501056533 : if (VECTOR_MODE_P (mode)
5955 15935211 : && code != VEC_CONCAT
5956 11824476 : && GET_CODE (op0) == CONST_VECTOR
5957 203926 : && GET_CODE (op1) == CONST_VECTOR)
5958 : {
5959 9492 : bool step_ok_p;
5960 9492 : if (CONST_VECTOR_STEPPED_P (op0)
5961 9492 : && CONST_VECTOR_STEPPED_P (op1))
5962 : /* We can operate directly on the encoding if:
5963 :
5964 : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
5965 : implies
5966 : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
5967 :
5968 : Addition and subtraction are the supported operators
5969 : for which this is true. */
5970 709 : step_ok_p = (code == PLUS || code == MINUS);
5971 8783 : else if (CONST_VECTOR_STEPPED_P (op0))
5972 : /* We can operate directly on stepped encodings if:
5973 :
5974 : a3 - a2 == a2 - a1
5975 : implies:
5976 : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
5977 :
5978 : which is true if (x -> x op c) distributes over addition. */
5979 1313 : step_ok_p = distributes_over_addition_p (code, 1);
5980 : else
5981 : /* Similarly in reverse. */
5982 7470 : step_ok_p = distributes_over_addition_p (code, 2);
5983 9492 : rtx_vector_builder builder;
5984 9492 : if (!builder.new_binary_operation (mode, op0, op1, step_ok_p))
5985 : return 0;
5986 :
5987 9492 : unsigned int count = builder.encoded_nelts ();
5988 56191 : for (unsigned int i = 0; i < count; i++)
5989 : {
5990 93498 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
5991 : CONST_VECTOR_ELT (op0, i),
5992 46749 : CONST_VECTOR_ELT (op1, i));
5993 46749 : if (!x || !valid_for_const_vector_p (mode, x))
5994 50 : return 0;
5995 46699 : builder.quick_push (x);
5996 : }
5997 9442 : return builder.build ();
5998 9492 : }
5999 :
6000 501047041 : if (VECTOR_MODE_P (mode)
6001 15925719 : && code == VEC_CONCAT
6002 4110735 : && (CONST_SCALAR_INT_P (op0)
6003 4090611 : || CONST_FIXED_P (op0)
6004 4090611 : || CONST_DOUBLE_AS_FLOAT_P (op0)
6005 4086887 : || CONST_VECTOR_P (op0))
6006 42865 : && (CONST_SCALAR_INT_P (op1)
6007 39538 : || CONST_DOUBLE_AS_FLOAT_P (op1)
6008 37265 : || CONST_FIXED_P (op1)
6009 37265 : || CONST_VECTOR_P (op1)))
6010 : {
6011 : /* Both inputs have a constant number of elements, so the result
6012 : must too. */
6013 14818 : unsigned n_elts = GET_MODE_NUNITS (mode).to_constant ();
6014 14818 : rtvec v = rtvec_alloc (n_elts);
6015 :
6016 14818 : gcc_assert (n_elts >= 2);
6017 14818 : if (n_elts == 2)
6018 : {
6019 5600 : gcc_assert (GET_CODE (op0) != CONST_VECTOR);
6020 5600 : gcc_assert (GET_CODE (op1) != CONST_VECTOR);
6021 :
6022 5600 : RTVEC_ELT (v, 0) = op0;
6023 5600 : RTVEC_ELT (v, 1) = op1;
6024 : }
6025 : else
6026 : {
6027 9218 : unsigned op0_n_elts = GET_MODE_NUNITS (GET_MODE (op0)).to_constant ();
6028 9218 : unsigned op1_n_elts = GET_MODE_NUNITS (GET_MODE (op1)).to_constant ();
6029 9218 : unsigned i;
6030 :
6031 9218 : gcc_assert (GET_CODE (op0) == CONST_VECTOR);
6032 9218 : gcc_assert (GET_CODE (op1) == CONST_VECTOR);
6033 9218 : gcc_assert (op0_n_elts + op1_n_elts == n_elts);
6034 :
6035 56722 : for (i = 0; i < op0_n_elts; ++i)
6036 47504 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (op0, i);
6037 56914 : for (i = 0; i < op1_n_elts; ++i)
6038 47696 : RTVEC_ELT (v, op0_n_elts+i) = CONST_VECTOR_ELT (op1, i);
6039 : }
6040 :
6041 14818 : return gen_rtx_CONST_VECTOR (mode, v);
6042 : }
6043 :
6044 488499912 : if (VECTOR_MODE_P (mode)
6045 15910901 : && GET_CODE (op0) == CONST_VECTOR
6046 204233 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1))
6047 501032223 : && (CONST_VECTOR_DUPLICATE_P (op0)
6048 137252 : || CONST_VECTOR_NUNITS (op0).is_constant ()))
6049 : {
6050 137252 : switch (code)
6051 : {
6052 137252 : case PLUS:
6053 137252 : case MINUS:
6054 137252 : case MULT:
6055 137252 : case DIV:
6056 137252 : case MOD:
6057 137252 : case UDIV:
6058 137252 : case UMOD:
6059 137252 : case AND:
6060 137252 : case IOR:
6061 137252 : case XOR:
6062 137252 : case SMIN:
6063 137252 : case SMAX:
6064 137252 : case UMIN:
6065 137252 : case UMAX:
6066 137252 : case LSHIFTRT:
6067 137252 : case ASHIFTRT:
6068 137252 : case ASHIFT:
6069 137252 : case ROTATE:
6070 137252 : case ROTATERT:
6071 137252 : case SS_PLUS:
6072 137252 : case US_PLUS:
6073 137252 : case SS_MINUS:
6074 137252 : case US_MINUS:
6075 137252 : case SS_ASHIFT:
6076 137252 : case US_ASHIFT:
6077 137252 : case COPYSIGN:
6078 137252 : break;
6079 : default:
6080 : return NULL_RTX;
6081 : }
6082 :
6083 137252 : unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op0)
6084 137252 : ? CONST_VECTOR_NPATTERNS (op0)
6085 145553 : : CONST_VECTOR_NUNITS (op0).to_constant ());
6086 137252 : rtx_vector_builder builder (mode, npatterns, 1);
6087 425217 : for (unsigned i = 0; i < npatterns; i++)
6088 : {
6089 301426 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
6090 150713 : CONST_VECTOR_ELT (op0, i), op1);
6091 150713 : if (!x || !valid_for_const_vector_p (mode, x))
6092 0 : return 0;
6093 150713 : builder.quick_push (x);
6094 : }
6095 137252 : return builder.build ();
6096 : }
6097 :
6098 500894971 : if (SCALAR_FLOAT_MODE_P (mode)
6099 6517686 : && CONST_DOUBLE_AS_FLOAT_P (op0)
6100 81309 : && CONST_DOUBLE_AS_FLOAT_P (op1)
6101 11857 : && mode == GET_MODE (op0) && mode == GET_MODE (op1))
6102 : {
6103 11857 : if (code == AND
6104 : || code == IOR
6105 11857 : || code == XOR)
6106 : {
6107 2602 : long tmp0[4];
6108 2602 : long tmp1[4];
6109 2602 : REAL_VALUE_TYPE r;
6110 2602 : int i;
6111 :
6112 2602 : real_to_target (tmp0, CONST_DOUBLE_REAL_VALUE (op0),
6113 2602 : GET_MODE (op0));
6114 2602 : real_to_target (tmp1, CONST_DOUBLE_REAL_VALUE (op1),
6115 2602 : GET_MODE (op1));
6116 13010 : for (i = 0; i < 4; i++)
6117 : {
6118 10408 : switch (code)
6119 : {
6120 5376 : case AND:
6121 5376 : tmp0[i] &= tmp1[i];
6122 5376 : break;
6123 2568 : case IOR:
6124 2568 : tmp0[i] |= tmp1[i];
6125 2568 : break;
6126 2464 : case XOR:
6127 2464 : tmp0[i] ^= tmp1[i];
6128 2464 : break;
6129 : default:
6130 : gcc_unreachable ();
6131 : }
6132 : }
6133 2602 : real_from_target (&r, tmp0, mode);
6134 2602 : return const_double_from_real_value (r, mode);
6135 : }
6136 9255 : else if (code == COPYSIGN)
6137 : {
6138 0 : REAL_VALUE_TYPE f0, f1;
6139 0 : real_convert (&f0, mode, CONST_DOUBLE_REAL_VALUE (op0));
6140 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (op1));
6141 0 : real_copysign (&f0, &f1);
6142 0 : return const_double_from_real_value (f0, mode);
6143 : }
6144 : else
6145 : {
6146 9255 : REAL_VALUE_TYPE f0, f1, value, result;
6147 9255 : const REAL_VALUE_TYPE *opr0, *opr1;
6148 9255 : bool inexact;
6149 :
6150 9255 : opr0 = CONST_DOUBLE_REAL_VALUE (op0);
6151 9255 : opr1 = CONST_DOUBLE_REAL_VALUE (op1);
6152 :
6153 9255 : if (HONOR_SNANS (mode)
6154 9255 : && (REAL_VALUE_ISSIGNALING_NAN (*opr0)
6155 845 : || REAL_VALUE_ISSIGNALING_NAN (*opr1)))
6156 : return 0;
6157 :
6158 9245 : real_convert (&f0, mode, opr0);
6159 9245 : real_convert (&f1, mode, opr1);
6160 :
6161 9245 : if (code == DIV
6162 4167 : && real_equal (&f1, &dconst0)
6163 12893 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
6164 : return 0;
6165 :
6166 27906 : if (MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
6167 5511 : && flag_trapping_math
6168 5433 : && REAL_VALUE_ISINF (f0) && REAL_VALUE_ISINF (f1))
6169 : {
6170 9 : int s0 = REAL_VALUE_NEGATIVE (f0);
6171 9 : int s1 = REAL_VALUE_NEGATIVE (f1);
6172 :
6173 9 : switch (code)
6174 : {
6175 0 : case PLUS:
6176 : /* Inf + -Inf = NaN plus exception. */
6177 0 : if (s0 != s1)
6178 : return 0;
6179 : break;
6180 0 : case MINUS:
6181 : /* Inf - Inf = NaN plus exception. */
6182 0 : if (s0 == s1)
6183 : return 0;
6184 : break;
6185 : case DIV:
6186 : /* Inf / Inf = NaN plus exception. */
6187 : return 0;
6188 : default:
6189 : break;
6190 : }
6191 : }
6192 :
6193 8354 : if (code == MULT && MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
6194 2053 : && flag_trapping_math
6195 7597 : && ((REAL_VALUE_ISINF (f0) && real_equal (&f1, &dconst0))
6196 1997 : || (REAL_VALUE_ISINF (f1)
6197 10 : && real_equal (&f0, &dconst0))))
6198 : /* Inf * 0 = NaN plus exception. */
6199 : return 0;
6200 :
6201 5574 : inexact = real_arithmetic (&value, rtx_to_tree_code (code),
6202 : &f0, &f1);
6203 5574 : real_convert (&result, mode, &value);
6204 :
6205 : /* Don't constant fold this floating point operation if
6206 : the result has overflowed and flag_trapping_math. */
6207 :
6208 5574 : if (flag_trapping_math
6209 21624 : && MODE_HAS_INFINITIES (mode)
6210 5406 : && REAL_VALUE_ISINF (result)
6211 1141 : && !REAL_VALUE_ISINF (f0)
6212 6701 : && !REAL_VALUE_ISINF (f1))
6213 : /* Overflow plus exception. */
6214 1127 : return 0;
6215 :
6216 : /* Don't constant fold this floating point operation if the
6217 : result may dependent upon the run-time rounding mode and
6218 : flag_rounding_math is set, or if GCC's software emulation
6219 : is unable to accurately represent the result. When the
6220 : result is known to be exactly zero, rounding may affect
6221 : the sign for modes that support signed zeros. */
6222 :
6223 4447 : if ((flag_rounding_math
6224 28452 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
6225 4447 : && (inexact
6226 358 : || !real_identical (&result, &value)
6227 72 : || (real_iszero (&value) && HONOR_SIGNED_ZEROS (mode))))
6228 : return NULL_RTX;
6229 :
6230 4016 : return const_double_from_real_value (result, mode);
6231 : }
6232 : }
6233 :
6234 : /* We can fold some multi-word operations. */
6235 500883114 : scalar_int_mode int_mode;
6236 500883114 : if (is_a <scalar_int_mode> (mode, &int_mode)
6237 429372524 : && CONST_SCALAR_INT_P (op0)
6238 41482566 : && CONST_SCALAR_INT_P (op1)
6239 34357441 : && GET_MODE_PRECISION (int_mode) <= MAX_BITSIZE_MODE_ANY_INT)
6240 : {
6241 34357441 : wide_int result;
6242 34357441 : wi::overflow_type overflow;
6243 34357441 : rtx_mode_t pop0 = rtx_mode_t (op0, int_mode);
6244 34357441 : rtx_mode_t pop1 = rtx_mode_t (op1, int_mode);
6245 :
6246 : #if TARGET_SUPPORTS_WIDE_INT == 0
6247 : /* This assert keeps the simplification from producing a result
6248 : that cannot be represented in a CONST_DOUBLE but a lot of
6249 : upstream callers expect that this function never fails to
6250 : simplify something and so you if you added this to the test
6251 : above the code would die later anyway. If this assert
6252 : happens, you just need to make the port support wide int. */
6253 : gcc_assert (GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_DOUBLE_INT);
6254 : #endif
6255 34357441 : switch (code)
6256 : {
6257 1112339 : case MINUS:
6258 1112339 : result = wi::sub (pop0, pop1);
6259 1112339 : break;
6260 :
6261 26471790 : case PLUS:
6262 26471790 : result = wi::add (pop0, pop1);
6263 26471790 : break;
6264 :
6265 327200 : case MULT:
6266 327200 : result = wi::mul (pop0, pop1);
6267 327200 : break;
6268 :
6269 9191 : case DIV:
6270 9191 : result = wi::div_trunc (pop0, pop1, SIGNED, &overflow);
6271 9191 : if (overflow)
6272 : return NULL_RTX;
6273 : break;
6274 :
6275 1605 : case MOD:
6276 1605 : result = wi::mod_trunc (pop0, pop1, SIGNED, &overflow);
6277 1605 : if (overflow)
6278 : return NULL_RTX;
6279 : break;
6280 :
6281 6110 : case UDIV:
6282 6110 : result = wi::div_trunc (pop0, pop1, UNSIGNED, &overflow);
6283 6110 : if (overflow)
6284 : return NULL_RTX;
6285 : break;
6286 :
6287 16295 : case UMOD:
6288 16295 : result = wi::mod_trunc (pop0, pop1, UNSIGNED, &overflow);
6289 16295 : if (overflow)
6290 : return NULL_RTX;
6291 : break;
6292 :
6293 821124 : case AND:
6294 821124 : result = wi::bit_and (pop0, pop1);
6295 821124 : break;
6296 :
6297 286337 : case IOR:
6298 286337 : result = wi::bit_or (pop0, pop1);
6299 286337 : break;
6300 :
6301 34241 : case XOR:
6302 34241 : result = wi::bit_xor (pop0, pop1);
6303 34241 : break;
6304 :
6305 1763 : case SMIN:
6306 1763 : result = wi::smin (pop0, pop1);
6307 1763 : break;
6308 :
6309 2017 : case SMAX:
6310 2017 : result = wi::smax (pop0, pop1);
6311 2017 : break;
6312 :
6313 3243 : case UMIN:
6314 3243 : result = wi::umin (pop0, pop1);
6315 3243 : break;
6316 :
6317 3046 : case UMAX:
6318 3046 : result = wi::umax (pop0, pop1);
6319 3046 : break;
6320 :
6321 5220429 : case LSHIFTRT:
6322 5220429 : case ASHIFTRT:
6323 5220429 : case ASHIFT:
6324 5220429 : case SS_ASHIFT:
6325 5220429 : case US_ASHIFT:
6326 5220429 : {
6327 : /* The shift count might be in SImode while int_mode might
6328 : be narrower. On IA-64 it is even DImode. If the shift
6329 : count is too large and doesn't fit into int_mode, we'd
6330 : ICE. So, if int_mode is narrower than
6331 : HOST_BITS_PER_WIDE_INT, use DImode for the shift count. */
6332 5220429 : if (GET_MODE (op1) == VOIDmode
6333 5220429 : && GET_MODE_PRECISION (int_mode) < HOST_BITS_PER_WIDE_INT)
6334 1930066 : pop1 = rtx_mode_t (op1, DImode);
6335 :
6336 5220429 : wide_int wop1 = pop1;
6337 5220429 : if (SHIFT_COUNT_TRUNCATED)
6338 : wop1 = wi::umod_trunc (wop1, GET_MODE_PRECISION (int_mode));
6339 5220429 : else if (wi::geu_p (wop1, GET_MODE_PRECISION (int_mode)))
6340 64 : return NULL_RTX;
6341 :
6342 5220365 : switch (code)
6343 : {
6344 3040560 : case LSHIFTRT:
6345 3040560 : result = wi::lrshift (pop0, wop1);
6346 3040560 : break;
6347 :
6348 85174 : case ASHIFTRT:
6349 85174 : result = wi::arshift (pop0, wop1);
6350 85174 : break;
6351 :
6352 2094631 : case ASHIFT:
6353 2094631 : result = wi::lshift (pop0, wop1);
6354 2094631 : break;
6355 :
6356 0 : case SS_ASHIFT:
6357 0 : if (wi::leu_p (wop1, wi::clrsb (pop0)))
6358 0 : result = wi::lshift (pop0, wop1);
6359 0 : else if (wi::neg_p (pop0))
6360 0 : result = wi::min_value (int_mode, SIGNED);
6361 : else
6362 0 : result = wi::max_value (int_mode, SIGNED);
6363 : break;
6364 :
6365 0 : case US_ASHIFT:
6366 0 : if (wi::eq_p (pop0, 0))
6367 0 : result = pop0;
6368 0 : else if (wi::leu_p (wop1, wi::clz (pop0)))
6369 0 : result = wi::lshift (pop0, wop1);
6370 : else
6371 0 : result = wi::max_value (int_mode, UNSIGNED);
6372 : break;
6373 :
6374 : default:
6375 : gcc_unreachable ();
6376 : }
6377 5220365 : break;
6378 5220429 : }
6379 31429 : case ROTATE:
6380 31429 : case ROTATERT:
6381 31429 : {
6382 : /* The rotate count might be in SImode while int_mode might
6383 : be narrower. On IA-64 it is even DImode. If the shift
6384 : count is too large and doesn't fit into int_mode, we'd
6385 : ICE. So, if int_mode is narrower than
6386 : HOST_BITS_PER_WIDE_INT, use DImode for the shift count. */
6387 31429 : if (GET_MODE (op1) == VOIDmode
6388 31429 : && GET_MODE_PRECISION (int_mode) < HOST_BITS_PER_WIDE_INT)
6389 23759 : pop1 = rtx_mode_t (op1, DImode);
6390 :
6391 31429 : if (wi::neg_p (pop1))
6392 : return NULL_RTX;
6393 :
6394 31329 : switch (code)
6395 : {
6396 10361 : case ROTATE:
6397 10361 : result = wi::lrotate (pop0, pop1);
6398 10361 : break;
6399 :
6400 20968 : case ROTATERT:
6401 20968 : result = wi::rrotate (pop0, pop1);
6402 20968 : break;
6403 :
6404 : default:
6405 : gcc_unreachable ();
6406 : }
6407 : break;
6408 : }
6409 :
6410 2438 : case SS_PLUS:
6411 2438 : result = wi::add (pop0, pop1, SIGNED, &overflow);
6412 4820 : clamp_signed_saturation:
6413 4820 : if (overflow == wi::OVF_OVERFLOW)
6414 314 : result = wi::max_value (GET_MODE_PRECISION (int_mode), SIGNED);
6415 4506 : else if (overflow == wi::OVF_UNDERFLOW)
6416 278 : result = wi::min_value (GET_MODE_PRECISION (int_mode), SIGNED);
6417 4228 : else if (overflow != wi::OVF_NONE)
6418 : return NULL_RTX;
6419 : break;
6420 :
6421 2388 : case US_PLUS:
6422 2388 : result = wi::add (pop0, pop1, UNSIGNED, &overflow);
6423 2388 : clamp_unsigned_saturation:
6424 2388 : if (overflow != wi::OVF_NONE)
6425 502 : result = wi::max_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
6426 : break;
6427 :
6428 2382 : case SS_MINUS:
6429 2382 : result = wi::sub (pop0, pop1, SIGNED, &overflow);
6430 2382 : goto clamp_signed_saturation;
6431 :
6432 2068 : case US_MINUS:
6433 2068 : result = wi::sub (pop0, pop1, UNSIGNED, &overflow);
6434 2068 : if (overflow != wi::OVF_NONE)
6435 1350 : result = wi::min_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
6436 : break;
6437 :
6438 0 : case SS_MULT:
6439 0 : result = wi::mul (pop0, pop1, SIGNED, &overflow);
6440 0 : goto clamp_signed_saturation;
6441 :
6442 0 : case US_MULT:
6443 0 : result = wi::mul (pop0, pop1, UNSIGNED, &overflow);
6444 0 : goto clamp_unsigned_saturation;
6445 :
6446 6 : case SMUL_HIGHPART:
6447 6 : result = wi::mul_high (pop0, pop1, SIGNED);
6448 6 : break;
6449 :
6450 0 : case UMUL_HIGHPART:
6451 0 : result = wi::mul_high (pop0, pop1, UNSIGNED);
6452 0 : break;
6453 :
6454 : default:
6455 : return NULL_RTX;
6456 : }
6457 34354436 : return immed_wide_int_const (result, int_mode);
6458 34357441 : }
6459 :
6460 : /* Handle polynomial integers. */
6461 : if (NUM_POLY_INT_COEFFS > 1
6462 : && is_a <scalar_int_mode> (mode, &int_mode)
6463 : && poly_int_rtx_p (op0)
6464 : && poly_int_rtx_p (op1))
6465 : {
6466 : poly_wide_int result;
6467 : switch (code)
6468 : {
6469 : case PLUS:
6470 : result = wi::to_poly_wide (op0, mode) + wi::to_poly_wide (op1, mode);
6471 : break;
6472 :
6473 : case MINUS:
6474 : result = wi::to_poly_wide (op0, mode) - wi::to_poly_wide (op1, mode);
6475 : break;
6476 :
6477 : case MULT:
6478 : if (CONST_SCALAR_INT_P (op1))
6479 : result = wi::to_poly_wide (op0, mode) * rtx_mode_t (op1, mode);
6480 : else
6481 : return NULL_RTX;
6482 : break;
6483 :
6484 : case ASHIFT:
6485 : if (CONST_SCALAR_INT_P (op1))
6486 : {
6487 : wide_int shift
6488 : = rtx_mode_t (op1,
6489 : GET_MODE (op1) == VOIDmode
6490 : && (GET_MODE_PRECISION (int_mode)
6491 : < HOST_BITS_PER_WIDE_INT)
6492 : ? DImode : mode);
6493 : if (SHIFT_COUNT_TRUNCATED)
6494 : shift = wi::umod_trunc (shift, GET_MODE_PRECISION (int_mode));
6495 : else if (wi::geu_p (shift, GET_MODE_PRECISION (int_mode)))
6496 : return NULL_RTX;
6497 : result = wi::to_poly_wide (op0, mode) << shift;
6498 : }
6499 : else
6500 : return NULL_RTX;
6501 : break;
6502 :
6503 : case IOR:
6504 : if (!CONST_SCALAR_INT_P (op1)
6505 : || !can_ior_p (wi::to_poly_wide (op0, mode),
6506 : rtx_mode_t (op1, mode), &result))
6507 : return NULL_RTX;
6508 : break;
6509 :
6510 : default:
6511 : return NULL_RTX;
6512 : }
6513 : return immed_wide_int_const (result, int_mode);
6514 : }
6515 :
6516 : return NULL_RTX;
6517 : }
6518 :
6519 :
6520 :
6521 : /* Return a positive integer if X should sort after Y. The value
6522 : returned is 1 if and only if X and Y are both regs. */
6523 :
6524 : static int
6525 117155917 : simplify_plus_minus_op_data_cmp (rtx x, rtx y)
6526 : {
6527 117155917 : int result;
6528 :
6529 117155917 : result = (commutative_operand_precedence (y)
6530 117155917 : - commutative_operand_precedence (x));
6531 117155917 : if (result)
6532 81289239 : return result + result;
6533 :
6534 : /* Group together equal REGs to do more simplification. */
6535 35866678 : if (REG_P (x) && REG_P (y))
6536 9299664 : return REGNO (x) > REGNO (y);
6537 :
6538 : return 0;
6539 : }
6540 :
6541 : /* Simplify and canonicalize a PLUS or MINUS, at least one of whose
6542 : operands may be another PLUS or MINUS.
6543 :
6544 : Rather than test for specific case, we do this by a brute-force method
6545 : and do all possible simplifications until no more changes occur. Then
6546 : we rebuild the operation.
6547 :
6548 : May return NULL_RTX when no changes were made. */
6549 :
6550 : rtx
6551 39508244 : simplify_context::simplify_plus_minus (rtx_code code, machine_mode mode,
6552 : rtx op0, rtx op1)
6553 : {
6554 39508244 : struct simplify_plus_minus_op_data
6555 : {
6556 : rtx op;
6557 : short neg;
6558 : } ops[16];
6559 39508244 : rtx result, tem;
6560 39508244 : int n_ops = 2;
6561 39508244 : int changed, n_constants, canonicalized = 0;
6562 39508244 : int i, j;
6563 :
6564 39508244 : memset (ops, 0, sizeof ops);
6565 :
6566 : /* Set up the two operands and then expand them until nothing has been
6567 : changed. If we run out of room in our array, give up; this should
6568 : almost never happen. */
6569 :
6570 39508244 : ops[0].op = op0;
6571 39508244 : ops[0].neg = 0;
6572 39508244 : ops[1].op = op1;
6573 39508244 : ops[1].neg = (code == MINUS);
6574 :
6575 80316995 : do
6576 : {
6577 80316995 : changed = 0;
6578 80316995 : n_constants = 0;
6579 :
6580 325081177 : for (i = 0; i < n_ops; i++)
6581 : {
6582 244764198 : rtx this_op = ops[i].op;
6583 244764198 : int this_neg = ops[i].neg;
6584 244764198 : enum rtx_code this_code = GET_CODE (this_op);
6585 :
6586 244764198 : switch (this_code)
6587 : {
6588 39823002 : case PLUS:
6589 39823002 : case MINUS:
6590 39823002 : if (n_ops == ARRAY_SIZE (ops))
6591 : return NULL_RTX;
6592 :
6593 39822986 : ops[n_ops].op = XEXP (this_op, 1);
6594 39822986 : ops[n_ops].neg = (this_code == MINUS) ^ this_neg;
6595 39822986 : n_ops++;
6596 :
6597 39822986 : ops[i].op = XEXP (this_op, 0);
6598 39822986 : changed = 1;
6599 : /* If this operand was negated then we will potentially
6600 : canonicalize the expression. Similarly if we don't
6601 : place the operands adjacent we're re-ordering the
6602 : expression and thus might be performing a
6603 : canonicalization. Ignore register re-ordering.
6604 : ??? It might be better to shuffle the ops array here,
6605 : but then (plus (plus (A, B), plus (C, D))) wouldn't
6606 : be seen as non-canonical. */
6607 39822986 : if (this_neg
6608 39113346 : || (i != n_ops - 2
6609 38495454 : && !(REG_P (ops[i].op) && REG_P (ops[n_ops - 1].op))))
6610 244764182 : canonicalized = 1;
6611 : break;
6612 :
6613 2075 : case NEG:
6614 2075 : ops[i].op = XEXP (this_op, 0);
6615 2075 : ops[i].neg = ! this_neg;
6616 2075 : changed = 1;
6617 2075 : canonicalized = 1;
6618 2075 : break;
6619 :
6620 1607862 : case CONST:
6621 1607862 : if (n_ops != ARRAY_SIZE (ops)
6622 1607862 : && GET_CODE (XEXP (this_op, 0)) == PLUS
6623 1474694 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 0))
6624 1454084 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 1)))
6625 : {
6626 1454084 : ops[i].op = XEXP (XEXP (this_op, 0), 0);
6627 1454084 : ops[n_ops].op = XEXP (XEXP (this_op, 0), 1);
6628 1454084 : ops[n_ops].neg = this_neg;
6629 1454084 : n_ops++;
6630 1454084 : changed = 1;
6631 1454084 : canonicalized = 1;
6632 : }
6633 : break;
6634 :
6635 70568 : case NOT:
6636 : /* ~a -> (-a - 1) */
6637 70568 : if (n_ops != ARRAY_SIZE (ops))
6638 : {
6639 70568 : ops[n_ops].op = CONSTM1_RTX (mode);
6640 70568 : ops[n_ops++].neg = this_neg;
6641 70568 : ops[i].op = XEXP (this_op, 0);
6642 70568 : ops[i].neg = !this_neg;
6643 70568 : changed = 1;
6644 70568 : canonicalized = 1;
6645 : }
6646 : break;
6647 :
6648 121084057 : CASE_CONST_SCALAR_INT:
6649 121084057 : case CONST_POLY_INT:
6650 121084057 : n_constants++;
6651 121084057 : if (this_neg)
6652 : {
6653 1217504 : ops[i].op = neg_poly_int_rtx (mode, this_op);
6654 1217504 : ops[i].neg = 0;
6655 1217504 : changed = 1;
6656 1217504 : canonicalized = 1;
6657 : }
6658 : break;
6659 :
6660 : default:
6661 : break;
6662 : }
6663 : }
6664 : }
6665 80316979 : while (changed);
6666 :
6667 39508228 : if (n_constants > 1)
6668 23973765 : canonicalized = 1;
6669 :
6670 39508228 : gcc_assert (n_ops >= 2);
6671 :
6672 : /* If we only have two operands, we can avoid the loops. */
6673 39508228 : if (n_ops == 2)
6674 : {
6675 0 : enum rtx_code code = ops[0].neg || ops[1].neg ? MINUS : PLUS;
6676 0 : rtx lhs, rhs;
6677 :
6678 : /* Get the two operands. Be careful with the order, especially for
6679 : the cases where code == MINUS. */
6680 0 : if (ops[0].neg && ops[1].neg)
6681 : {
6682 0 : lhs = gen_rtx_NEG (mode, ops[0].op);
6683 0 : rhs = ops[1].op;
6684 : }
6685 0 : else if (ops[0].neg)
6686 : {
6687 0 : lhs = ops[1].op;
6688 0 : rhs = ops[0].op;
6689 : }
6690 : else
6691 : {
6692 0 : lhs = ops[0].op;
6693 0 : rhs = ops[1].op;
6694 : }
6695 :
6696 0 : return simplify_const_binary_operation (code, mode, lhs, rhs);
6697 : }
6698 :
6699 : /* Now simplify each pair of operands until nothing changes. */
6700 64495435 : while (1)
6701 : {
6702 : /* Insertion sort is good enough for a small array. */
6703 170752833 : for (i = 1; i < n_ops; i++)
6704 : {
6705 106257398 : struct simplify_plus_minus_op_data save;
6706 106257398 : int cmp;
6707 :
6708 106257398 : j = i - 1;
6709 106257398 : cmp = simplify_plus_minus_op_data_cmp (ops[j].op, ops[i].op);
6710 106257398 : if (cmp <= 0)
6711 93156518 : continue;
6712 : /* Just swapping registers doesn't count as canonicalization. */
6713 13100880 : if (cmp != 1)
6714 9872753 : canonicalized = 1;
6715 :
6716 13100880 : save = ops[i];
6717 15553568 : do
6718 15553568 : ops[j + 1] = ops[j];
6719 15553568 : while (j--
6720 28654448 : && simplify_plus_minus_op_data_cmp (ops[j].op, save.op) > 0);
6721 13100880 : ops[j + 1] = save;
6722 : }
6723 :
6724 64495435 : changed = 0;
6725 170752833 : for (i = n_ops - 1; i > 0; i--)
6726 254990652 : for (j = i - 1; j >= 0; j--)
6727 : {
6728 149622920 : rtx lhs = ops[j].op, rhs = ops[i].op;
6729 149622920 : int lneg = ops[j].neg, rneg = ops[i].neg;
6730 :
6731 149622920 : if (lhs != 0 && rhs != 0)
6732 : {
6733 123928267 : enum rtx_code ncode = PLUS;
6734 :
6735 123928267 : if (lneg != rneg)
6736 : {
6737 11786982 : ncode = MINUS;
6738 11786982 : if (lneg)
6739 7420853 : std::swap (lhs, rhs);
6740 : }
6741 112141285 : else if (swap_commutative_operands_p (lhs, rhs))
6742 429252 : std::swap (lhs, rhs);
6743 :
6744 123928267 : if ((GET_CODE (lhs) == CONST || CONST_INT_P (lhs))
6745 28954487 : && (GET_CODE (rhs) == CONST || CONST_INT_P (rhs)))
6746 : {
6747 24129354 : rtx tem_lhs, tem_rhs;
6748 :
6749 24129354 : tem_lhs = GET_CODE (lhs) == CONST ? XEXP (lhs, 0) : lhs;
6750 24129354 : tem_rhs = GET_CODE (rhs) == CONST ? XEXP (rhs, 0) : rhs;
6751 24129354 : tem = simplify_binary_operation (ncode, mode, tem_lhs,
6752 : tem_rhs);
6753 :
6754 24129354 : if (tem && !CONSTANT_P (tem))
6755 1717 : tem = gen_rtx_CONST (GET_MODE (tem), tem);
6756 : }
6757 : else
6758 99798913 : tem = simplify_binary_operation (ncode, mode, lhs, rhs);
6759 :
6760 99800630 : if (tem)
6761 : {
6762 : /* Reject "simplifications" that just wrap the two
6763 : arguments in a CONST. Failure to do so can result
6764 : in infinite recursion with simplify_binary_operation
6765 : when it calls us to simplify CONST operations.
6766 : Also, if we find such a simplification, don't try
6767 : any more combinations with this rhs: We must have
6768 : something like symbol+offset, ie. one of the
6769 : trivial CONST expressions we handle later. */
6770 26309463 : if (GET_CODE (tem) == CONST
6771 891383 : && GET_CODE (XEXP (tem, 0)) == ncode
6772 890832 : && XEXP (XEXP (tem, 0), 0) == lhs
6773 889666 : && XEXP (XEXP (tem, 0), 1) == rhs)
6774 : break;
6775 25419797 : lneg &= rneg;
6776 25419797 : if (GET_CODE (tem) == NEG)
6777 45743 : tem = XEXP (tem, 0), lneg = !lneg;
6778 25419797 : if (poly_int_rtx_p (tem) && lneg)
6779 0 : tem = neg_poly_int_rtx (mode, tem), lneg = 0;
6780 :
6781 25419797 : ops[i].op = tem;
6782 25419797 : ops[i].neg = lneg;
6783 25419797 : ops[j].op = NULL_RTX;
6784 25419797 : changed = 1;
6785 25419797 : canonicalized = 1;
6786 : }
6787 : }
6788 : }
6789 :
6790 64495435 : if (!changed)
6791 : break;
6792 :
6793 : /* Pack all the operands to the lower-numbered entries. */
6794 100795967 : for (i = 0, j = 0; j < n_ops; j++)
6795 75808760 : if (ops[j].op)
6796 : {
6797 50388963 : ops[i] = ops[j];
6798 50388963 : i++;
6799 : }
6800 : n_ops = i;
6801 : }
6802 :
6803 : /* If nothing changed, check that rematerialization of rtl instructions
6804 : is still required. */
6805 39508228 : if (!canonicalized)
6806 : {
6807 : /* Perform rematerialization if only all operands are registers and
6808 : all operations are PLUS. */
6809 : /* ??? Also disallow (non-global, non-frame) fixed registers to work
6810 : around rs6000 and how it uses the CA register. See PR67145. */
6811 5511098 : for (i = 0; i < n_ops; i++)
6812 4416272 : if (ops[i].neg
6813 4123698 : || !REG_P (ops[i].op)
6814 8025478 : || (REGNO (ops[i].op) < FIRST_PSEUDO_REGISTER
6815 345351 : && fixed_regs[REGNO (ops[i].op)]
6816 266 : && !global_regs[REGNO (ops[i].op)]
6817 266 : && ops[i].op != frame_pointer_rtx
6818 116 : && ops[i].op != arg_pointer_rtx
6819 102 : && ops[i].op != stack_pointer_rtx))
6820 : return NULL_RTX;
6821 1094826 : goto gen_result;
6822 : }
6823 :
6824 : /* Create (minus -C X) instead of (neg (const (plus X C))). */
6825 37606336 : if (n_ops == 2
6826 23433689 : && CONST_INT_P (ops[1].op)
6827 22767835 : && CONSTANT_P (ops[0].op)
6828 163 : && ops[0].neg)
6829 57 : return gen_rtx_fmt_ee (MINUS, mode, ops[1].op, ops[0].op);
6830 :
6831 : /* We suppressed creation of trivial CONST expressions in the
6832 : combination loop to avoid recursion. Create one manually now.
6833 : The combination loop should have ensured that there is exactly
6834 : one CONST_INT, and the sort will have ensured that it is last
6835 : in the array and that any other constant will be next-to-last. */
6836 :
6837 37606279 : if (n_ops > 1
6838 37102594 : && poly_int_rtx_p (ops[n_ops - 1].op)
6839 71872583 : && CONSTANT_P (ops[n_ops - 2].op))
6840 : {
6841 1528292 : rtx value = ops[n_ops - 1].op;
6842 1528292 : if (ops[n_ops - 1].neg ^ ops[n_ops - 2].neg)
6843 717577 : value = neg_poly_int_rtx (mode, value);
6844 1528292 : if (CONST_INT_P (value))
6845 : {
6846 3056584 : ops[n_ops - 2].op = plus_constant (mode, ops[n_ops - 2].op,
6847 1528292 : INTVAL (value));
6848 1528292 : n_ops--;
6849 : }
6850 : }
6851 :
6852 : /* Put a non-negated operand first, if possible. */
6853 :
6854 39355121 : for (i = 0; i < n_ops && ops[i].neg; i++)
6855 1748842 : continue;
6856 37606279 : if (i == n_ops)
6857 9756 : ops[0].op = gen_rtx_NEG (mode, ops[0].op);
6858 37596523 : else if (i != 0)
6859 : {
6860 1640887 : tem = ops[0].op;
6861 1640887 : ops[0] = ops[i];
6862 1640887 : ops[i].op = tem;
6863 1640887 : ops[i].neg = 1;
6864 : }
6865 :
6866 : /* Now make the result by performing the requested operations. */
6867 35955636 : gen_result:
6868 38701105 : result = ops[0].op;
6869 90984370 : for (i = 1; i < n_ops; i++)
6870 104566530 : result = gen_rtx_fmt_ee (ops[i].neg ? MINUS : PLUS,
6871 : mode, result, ops[i].op);
6872 :
6873 : return result;
6874 1748842 : }
6875 :
6876 : /* Check whether an operand is suitable for calling simplify_plus_minus. */
6877 : static bool
6878 543082553 : plus_minus_operand_p (const_rtx x)
6879 : {
6880 543082553 : return GET_CODE (x) == PLUS
6881 543082553 : || GET_CODE (x) == MINUS
6882 543082553 : || (GET_CODE (x) == CONST
6883 1965794 : && GET_CODE (XEXP (x, 0)) == PLUS
6884 1341107 : && CONSTANT_P (XEXP (XEXP (x, 0), 0))
6885 1268382 : && CONSTANT_P (XEXP (XEXP (x, 0), 1)));
6886 : }
6887 :
6888 : /* Like simplify_binary_operation except used for relational operators.
6889 : MODE is the mode of the result. If MODE is VOIDmode, both operands must
6890 : not also be VOIDmode.
6891 :
6892 : CMP_MODE specifies in which mode the comparison is done in, so it is
6893 : the mode of the operands. If CMP_MODE is VOIDmode, it is taken from
6894 : the operands or, if both are VOIDmode, the operands are compared in
6895 : "infinite precision". */
6896 : rtx
6897 134305695 : simplify_context::simplify_relational_operation (rtx_code code,
6898 : machine_mode mode,
6899 : machine_mode cmp_mode,
6900 : rtx op0, rtx op1)
6901 : {
6902 134305695 : rtx tem, trueop0, trueop1;
6903 :
6904 134305695 : if (cmp_mode == VOIDmode)
6905 29985629 : cmp_mode = GET_MODE (op0);
6906 29985629 : if (cmp_mode == VOIDmode)
6907 253738 : cmp_mode = GET_MODE (op1);
6908 :
6909 134305695 : tem = simplify_const_relational_operation (code, cmp_mode, op0, op1);
6910 134305695 : if (tem)
6911 794014 : return relational_result (mode, cmp_mode, tem);
6912 :
6913 : /* For the following tests, ensure const0_rtx is op1. */
6914 133511681 : if (swap_commutative_operands_p (op0, op1)
6915 133511681 : || (op0 == const0_rtx && op1 != const0_rtx))
6916 2641891 : std::swap (op0, op1), code = swap_condition (code);
6917 :
6918 : /* If op0 is a compare, extract the comparison arguments from it. */
6919 133511681 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6920 14865207 : return simplify_gen_relational (code, mode, VOIDmode,
6921 14865207 : XEXP (op0, 0), XEXP (op0, 1));
6922 :
6923 118646474 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC)
6924 : return NULL_RTX;
6925 :
6926 87346459 : trueop0 = avoid_constant_pool_reference (op0);
6927 87346459 : trueop1 = avoid_constant_pool_reference (op1);
6928 87346459 : return simplify_relational_operation_1 (code, mode, cmp_mode,
6929 87346459 : trueop0, trueop1);
6930 : }
6931 :
6932 : /* This part of simplify_relational_operation is only used when CMP_MODE
6933 : is not in class MODE_CC (i.e. it is a real comparison).
6934 :
6935 : MODE is the mode of the result, while CMP_MODE specifies in which
6936 : mode the comparison is done in, so it is the mode of the operands. */
6937 :
6938 : rtx
6939 87346459 : simplify_context::simplify_relational_operation_1 (rtx_code code,
6940 : machine_mode mode,
6941 : machine_mode cmp_mode,
6942 : rtx op0, rtx op1)
6943 : {
6944 87346459 : enum rtx_code op0code = GET_CODE (op0);
6945 :
6946 87346459 : if (op1 == const0_rtx && COMPARISON_P (op0))
6947 : {
6948 : /* If op0 is a comparison, extract the comparison arguments
6949 : from it. */
6950 308919 : if (code == NE)
6951 : {
6952 135453 : if (GET_MODE (op0) == mode)
6953 183 : return simplify_rtx (op0);
6954 : else
6955 135270 : return simplify_gen_relational (GET_CODE (op0), mode, VOIDmode,
6956 135270 : XEXP (op0, 0), XEXP (op0, 1));
6957 : }
6958 173466 : else if (code == EQ)
6959 : {
6960 140875 : enum rtx_code new_code = reversed_comparison_code (op0, NULL);
6961 140875 : if (new_code != UNKNOWN)
6962 140556 : return simplify_gen_relational (new_code, mode, VOIDmode,
6963 140556 : XEXP (op0, 0), XEXP (op0, 1));
6964 : }
6965 : }
6966 :
6967 : /* (LTU/GEU (PLUS a C) C), where C is constant, can be simplified to
6968 : (GEU/LTU a -C). Likewise for (LTU/GEU (PLUS a C) a). */
6969 87070450 : if ((code == LTU || code == GEU)
6970 5297046 : && GET_CODE (op0) == PLUS
6971 662050 : && CONST_INT_P (XEXP (op0, 1))
6972 443411 : && (rtx_equal_p (op1, XEXP (op0, 0))
6973 282433 : || rtx_equal_p (op1, XEXP (op0, 1)))
6974 : /* (LTU/GEU (PLUS a 0) 0) is not the same as (GEU/LTU a 0). */
6975 87299242 : && XEXP (op0, 1) != const0_rtx)
6976 : {
6977 228792 : rtx new_cmp
6978 228792 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6979 230337 : return simplify_gen_relational ((code == LTU ? GEU : LTU), mode,
6980 228792 : cmp_mode, XEXP (op0, 0), new_cmp);
6981 : }
6982 :
6983 : /* (GTU (PLUS a C) (C - 1)) where C is a non-zero constant can be
6984 : transformed into (LTU a -C). */
6985 86841658 : if (code == GTU && GET_CODE (op0) == PLUS && CONST_INT_P (op1)
6986 319973 : && CONST_INT_P (XEXP (op0, 1))
6987 237184 : && (UINTVAL (op1) == UINTVAL (XEXP (op0, 1)) - 1)
6988 19581 : && XEXP (op0, 1) != const0_rtx)
6989 : {
6990 19581 : rtx new_cmp
6991 19581 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6992 19581 : return simplify_gen_relational (LTU, mode, cmp_mode,
6993 19581 : XEXP (op0, 0), new_cmp);
6994 : }
6995 :
6996 : /* Canonicalize (LTU/GEU (PLUS a b) b) as (LTU/GEU (PLUS a b) a). */
6997 86822077 : if ((code == LTU || code == GEU)
6998 5068254 : && GET_CODE (op0) == PLUS
6999 433258 : && rtx_equal_p (op1, XEXP (op0, 1))
7000 : /* Don't recurse "infinitely" for (LTU/GEU (PLUS b b) b). */
7001 86828809 : && !rtx_equal_p (op1, XEXP (op0, 0)))
7002 6732 : return simplify_gen_relational (code, mode, cmp_mode, op0,
7003 6732 : copy_rtx (XEXP (op0, 0)));
7004 :
7005 86815345 : if (op1 == const0_rtx)
7006 : {
7007 : /* Canonicalize (GTU x 0) as (NE x 0). */
7008 38168068 : if (code == GTU)
7009 77555 : return simplify_gen_relational (NE, mode, cmp_mode, op0, op1);
7010 : /* Canonicalize (LEU x 0) as (EQ x 0). */
7011 38090513 : if (code == LEU)
7012 34666 : return simplify_gen_relational (EQ, mode, cmp_mode, op0, op1);
7013 :
7014 38055847 : if ((code == NE || code == EQ)
7015 : /* Verify op0 is IOR */
7016 34087417 : && GET_CODE (op0) == IOR
7017 : /* only enters if op1 is 0 */
7018 : /* Verify IOR operand is NE */
7019 590688 : && GET_CODE (XEXP (op0, 0)) == NE
7020 21171 : && GET_MODE (XEXP (XEXP (op0, 0), 0)) == cmp_mode
7021 : /* Verify second NE operand is 0 */
7022 369 : && XEXP (XEXP (op0, 0), 1) == CONST0_RTX (cmp_mode))
7023 : {
7024 31 : rtx t = gen_rtx_IOR (cmp_mode, XEXP (XEXP (op0, 0), 0), XEXP (op0, 1));
7025 31 : t = gen_rtx_fmt_ee (code, mode, t, CONST0_RTX (mode));
7026 31 : return t;
7027 : }
7028 :
7029 : }
7030 48647277 : else if (op1 == const1_rtx)
7031 : {
7032 3355280 : switch (code)
7033 : {
7034 9574 : case GE:
7035 : /* Canonicalize (GE x 1) as (GT x 0). */
7036 9574 : return simplify_gen_relational (GT, mode, cmp_mode,
7037 9574 : op0, const0_rtx);
7038 221174 : case GEU:
7039 : /* Canonicalize (GEU x 1) as (NE x 0). */
7040 221174 : return simplify_gen_relational (NE, mode, cmp_mode,
7041 221174 : op0, const0_rtx);
7042 6960 : case LT:
7043 : /* Canonicalize (LT x 1) as (LE x 0). */
7044 6960 : return simplify_gen_relational (LE, mode, cmp_mode,
7045 6960 : op0, const0_rtx);
7046 54097 : case LTU:
7047 : /* Canonicalize (LTU x 1) as (EQ x 0). */
7048 54097 : return simplify_gen_relational (EQ, mode, cmp_mode,
7049 54097 : op0, const0_rtx);
7050 : default:
7051 : break;
7052 : }
7053 : }
7054 45291997 : else if (op1 == constm1_rtx)
7055 : {
7056 : /* Canonicalize (LE x -1) as (LT x 0). */
7057 1198398 : if (code == LE)
7058 1581 : return simplify_gen_relational (LT, mode, cmp_mode, op0, const0_rtx);
7059 : /* Canonicalize (GT x -1) as (GE x 0). */
7060 1196817 : if (code == GT)
7061 5231 : return simplify_gen_relational (GE, mode, cmp_mode, op0, const0_rtx);
7062 : }
7063 :
7064 : /* (eq/ne (plus x cst1) cst2) simplifies to (eq/ne x (cst2 - cst1)) */
7065 82436046 : if ((code == EQ || code == NE)
7066 64259695 : && (op0code == PLUS || op0code == MINUS)
7067 2504784 : && CONSTANT_P (op1)
7068 932006 : && CONSTANT_P (XEXP (op0, 1))
7069 519615 : && (INTEGRAL_MODE_P (cmp_mode) || flag_unsafe_math_optimizations))
7070 : {
7071 519581 : rtx x = XEXP (op0, 0);
7072 519581 : rtx c = XEXP (op0, 1);
7073 519581 : enum rtx_code invcode = op0code == PLUS ? MINUS : PLUS;
7074 519581 : rtx tem = simplify_gen_binary (invcode, cmp_mode, op1, c);
7075 :
7076 : /* Detect an infinite recursive condition, where we oscillate at this
7077 : simplification case between:
7078 : A + B == C <---> C - B == A,
7079 : where A, B, and C are all constants with non-simplifiable expressions,
7080 : usually SYMBOL_REFs. */
7081 519581 : if (GET_CODE (tem) == invcode
7082 59 : && CONSTANT_P (x)
7083 519599 : && rtx_equal_p (c, XEXP (tem, 1)))
7084 : return NULL_RTX;
7085 :
7086 519563 : return simplify_gen_relational (code, mode, cmp_mode, x, tem);
7087 : }
7088 :
7089 : /* (eq/ne (plus (x) (y)) y) simplifies to (eq/ne x 0). */
7090 63740114 : if ((code == EQ || code == NE)
7091 63740114 : && op0code == PLUS
7092 1640737 : && rtx_equal_p (XEXP (op0, 1), op1)
7093 250 : && !side_effects_p (op1)
7094 250 : && (INTEGRAL_MODE_P (cmp_mode) || flag_unsafe_math_optimizations))
7095 226 : return simplify_gen_relational (code, mode, cmp_mode,
7096 226 : XEXP (op0, 0), CONST0_RTX (cmp_mode));
7097 :
7098 : /* (ne:SI (zero_extract:SI FOO (const_int 1) BAR) (const_int 0))) is
7099 : the same as (zero_extract:SI FOO (const_int 1) BAR). */
7100 85884669 : scalar_int_mode int_mode, int_cmp_mode;
7101 85884669 : if (code == NE
7102 34351045 : && op1 == const0_rtx
7103 2337758 : && is_int_mode (mode, &int_mode)
7104 88138297 : && is_a <scalar_int_mode> (cmp_mode, &int_cmp_mode)
7105 : /* ??? Work-around BImode bugs in the ia64 backend. */
7106 2337758 : && int_mode != BImode
7107 2337734 : && int_cmp_mode != BImode
7108 2337734 : && nonzero_bits (op0, int_cmp_mode) == 1
7109 85884669 : && STORE_FLAG_VALUE == 1)
7110 168260 : return GET_MODE_SIZE (int_mode) > GET_MODE_SIZE (int_cmp_mode)
7111 84130 : ? simplify_gen_unary (ZERO_EXTEND, int_mode, op0, int_cmp_mode)
7112 18809 : : lowpart_subreg (int_mode, op0, int_cmp_mode);
7113 :
7114 : /* (eq/ne (xor x y) 0) simplifies to (eq/ne x y). */
7115 85800539 : if ((code == EQ || code == NE)
7116 63655758 : && op1 == const0_rtx
7117 33928307 : && op0code == XOR)
7118 14989 : return simplify_gen_relational (code, mode, cmp_mode,
7119 14989 : XEXP (op0, 0), XEXP (op0, 1));
7120 :
7121 : /* (eq/ne (xor x y) x) simplifies to (eq/ne y 0). */
7122 63640769 : if ((code == EQ || code == NE)
7123 63640769 : && op0code == XOR
7124 5923 : && rtx_equal_p (XEXP (op0, 0), op1)
7125 6 : && !side_effects_p (XEXP (op0, 0)))
7126 0 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 1),
7127 0 : CONST0_RTX (mode));
7128 :
7129 : /* Likewise (eq/ne (xor x y) y) simplifies to (eq/ne x 0). */
7130 85785550 : if ((code == EQ || code == NE)
7131 63640769 : && op0code == XOR
7132 5923 : && rtx_equal_p (XEXP (op0, 1), op1)
7133 85785734 : && !side_effects_p (XEXP (op0, 1)))
7134 184 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
7135 184 : CONST0_RTX (mode));
7136 :
7137 : /* (eq/ne (xor x C1) C2) simplifies to (eq/ne x (C1^C2)). */
7138 85785366 : if ((code == EQ || code == NE)
7139 63640585 : && op0code == XOR
7140 5739 : && CONST_SCALAR_INT_P (op1)
7141 1466 : && CONST_SCALAR_INT_P (XEXP (op0, 1)))
7142 832 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
7143 : simplify_gen_binary (XOR, cmp_mode,
7144 832 : XEXP (op0, 1), op1));
7145 :
7146 : /* Simplify eq/ne (and/ior x y) x/y) for targets with a BICS instruction or
7147 : constant folding if x/y is a constant. */
7148 63639753 : if ((code == EQ || code == NE)
7149 63639753 : && (op0code == AND || op0code == IOR)
7150 3664564 : && !side_effects_p (op1)
7151 3664458 : && op1 != CONST0_RTX (cmp_mode))
7152 : {
7153 : /* Both (eq/ne (and x y) x) and (eq/ne (ior x y) y) simplify to
7154 : (eq/ne (and (not y) x) 0). */
7155 481147 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 0), op1))
7156 958806 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 1), op1)))
7157 : {
7158 25060 : rtx not_y = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 1),
7159 : cmp_mode);
7160 25060 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_y, XEXP (op0, 0));
7161 :
7162 25060 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
7163 25060 : CONST0_RTX (cmp_mode));
7164 : }
7165 :
7166 : /* Both (eq/ne (and x y) y) and (eq/ne (ior x y) x) simplify to
7167 : (eq/ne (and (not x) y) 0). */
7168 456183 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 1), op1))
7169 887311 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 0), op1)))
7170 : {
7171 46439 : rtx not_x = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 0),
7172 : cmp_mode);
7173 46439 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_x, XEXP (op0, 1));
7174 :
7175 46439 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
7176 46439 : CONST0_RTX (cmp_mode));
7177 : }
7178 : }
7179 :
7180 : /* Optimize (cmp (and/ior x C1) C2) depending on the CMP and C1 and C2's
7181 : relationship. */
7182 85713035 : if ((op0code == AND || op0code == IOR)
7183 3841569 : && CONST_INT_P (op1)
7184 3662442 : && CONST_INT_P (XEXP (op0, 1)))
7185 : {
7186 2383788 : unsigned HOST_WIDE_INT c1 = UINTVAL (XEXP (op0, 1));
7187 2383788 : unsigned HOST_WIDE_INT c2 = UINTVAL (op1);
7188 :
7189 : /* For AND operations:
7190 : - (x & c1) == c2 when some bits are set in c2 but not in c1 -> false
7191 : - (x & c1) != c2 when some bits are set in c2 but not in c1 -> true
7192 : - (x & c1) >= c2 when c1 is less than c2 -> false
7193 : - (x & c1) < c2 when c1 is less than c2 -> true
7194 : - (x & c1) > c2 when c1 is less than or equal to c2 -> false
7195 : - (x & c1) <= c2 when c1 is less than or equal to c2 -> true
7196 :
7197 : For IOR operations:
7198 : - (x | c1) == c2 when some bits are set in c1 but not in c2 -> false
7199 : - (x | c1) != c2 when some bits are set in c1 but not in c2 -> true
7200 : - (x | c1) <= c2 when c1 is greater than c2 -> false
7201 : - (x | c1) > c2 when c1 is greater than c2 -> true
7202 : - (x | c1) < c2 when c1 is greater than or equal to c2 -> false
7203 : - (x | c1) >= c2 when c1 is greater than or equal to c2 -> true */
7204 2383788 : if ((op0code == AND
7205 2381054 : && ((code == EQ && (c1 & c2) != c2)
7206 2381034 : || (code == GEU && c1 < c2)
7207 2381034 : || (code == GTU && c1 <= c2)))
7208 2383768 : || ((op0code == IOR
7209 2734 : && ((code == EQ && (c1 & c2) != c1)
7210 2734 : || (code == LEU && c1 > c2)
7211 2734 : || (code == LTU && c1 >= c2)))))
7212 20 : return const0_rtx;
7213 :
7214 2383768 : if ((op0code == AND
7215 2381034 : && ((code == NE && (c1 & c2) != c2)
7216 2380897 : || (code == LTU && c1 < c2)
7217 2380897 : || (code == LEU && c1 <= c2)))
7218 2383631 : || ((op0code == IOR
7219 2734 : && ((code == NE && (c1 & c2) != c1)
7220 2674 : || (code == GTU && c1 > c2)
7221 2674 : || (code == GEU && c1 >= c2)))))
7222 197 : return const_true_rtx;
7223 : }
7224 :
7225 : /* (eq/ne (bswap x) C1) simplifies to (eq/ne x C2) with C2 swapped. */
7226 85712818 : if ((code == EQ || code == NE)
7227 63568037 : && GET_CODE (op0) == BSWAP
7228 396 : && CONST_SCALAR_INT_P (op1))
7229 123 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
7230 : simplify_gen_unary (BSWAP, cmp_mode,
7231 123 : op1, cmp_mode));
7232 :
7233 : /* (eq/ne (bswap x) (bswap y)) simplifies to (eq/ne x y). */
7234 63567914 : if ((code == EQ || code == NE)
7235 63567914 : && GET_CODE (op0) == BSWAP
7236 273 : && GET_CODE (op1) == BSWAP)
7237 18 : return simplify_gen_relational (code, mode, cmp_mode,
7238 18 : XEXP (op0, 0), XEXP (op1, 0));
7239 :
7240 85712677 : if (op0code == POPCOUNT && op1 == const0_rtx)
7241 0 : switch (code)
7242 : {
7243 0 : case EQ:
7244 0 : case LE:
7245 0 : case LEU:
7246 : /* (eq (popcount x) (const_int 0)) -> (eq x (const_int 0)). */
7247 0 : return simplify_gen_relational (EQ, mode, GET_MODE (XEXP (op0, 0)),
7248 : XEXP (op0, 0),
7249 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
7250 :
7251 0 : case NE:
7252 0 : case GT:
7253 0 : case GTU:
7254 : /* (ne (popcount x) (const_int 0)) -> (ne x (const_int 0)). */
7255 0 : return simplify_gen_relational (NE, mode, GET_MODE (XEXP (op0, 0)),
7256 : XEXP (op0, 0),
7257 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
7258 :
7259 : default:
7260 : break;
7261 : }
7262 :
7263 : /* (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) -> (and:SI x 1). */
7264 85712677 : if (code == NE
7265 34219950 : && op1 == const0_rtx
7266 17628797 : && (op0code == TRUNCATE
7267 164338 : || (partial_subreg_p (op0)
7268 163611 : && subreg_lowpart_p (op0)))
7269 139984 : && SCALAR_INT_MODE_P (mode)
7270 85712677 : && STORE_FLAG_VALUE == 1)
7271 : {
7272 37326 : rtx tmp = XEXP (op0, 0);
7273 37326 : if (GET_CODE (tmp) == ASHIFT
7274 2685 : && GET_MODE (tmp) == mode
7275 241 : && CONST_INT_P (XEXP (tmp, 1))
7276 241 : && is_int_mode (GET_MODE (op0), &int_mode)
7277 37567 : && INTVAL (XEXP (tmp, 1)) == GET_MODE_PRECISION (int_mode) - 1)
7278 241 : return simplify_gen_binary (AND, mode, XEXP (tmp, 0), const1_rtx);
7279 : }
7280 :
7281 : /* For two unsigned booleans A and B:
7282 :
7283 : A > B == ~B & A
7284 : A >= B == ~B | A
7285 : A < B == ~A & B
7286 : A <= B == ~A | B
7287 : A == B == ~A ^ B (== ~B ^ A)
7288 : A != B == A ^ B
7289 :
7290 : For signed comparisons, we have to take STORE_FLAG_VALUE into account,
7291 : with the rules above applying for positive STORE_FLAG_VALUE and with
7292 : the relations reversed for negative STORE_FLAG_VALUE. */
7293 85712436 : if (is_a<scalar_int_mode> (cmp_mode)
7294 82897741 : && COMPARISON_P (op0)
7295 85868624 : && COMPARISON_P (op1))
7296 : {
7297 49203 : rtx t = NULL_RTX;
7298 49203 : if (code == GTU || code == (STORE_FLAG_VALUE > 0 ? GT : LT))
7299 7275 : t = simplify_logical_relational_operation (AND, mode, op1, op0, true);
7300 : else if (code == GEU || code == (STORE_FLAG_VALUE > 0 ? GE : LE))
7301 7272 : t = simplify_logical_relational_operation (IOR, mode, op1, op0, true);
7302 : else if (code == LTU || code == (STORE_FLAG_VALUE > 0 ? LT : GT))
7303 7304 : t = simplify_logical_relational_operation (AND, mode, op0, op1, true);
7304 : else if (code == LEU || code == (STORE_FLAG_VALUE > 0 ? LE : GE))
7305 7272 : t = simplify_logical_relational_operation (IOR, mode, op0, op1, true);
7306 : else if (code == EQ)
7307 9519 : t = simplify_logical_relational_operation (XOR, mode, op0, op1, true);
7308 : else if (code == NE)
7309 10561 : t = simplify_logical_relational_operation (XOR, mode, op0, op1);
7310 : if (t)
7311 : return t;
7312 : }
7313 :
7314 : return NULL_RTX;
7315 : }
7316 :
7317 : enum
7318 : {
7319 : CMP_EQ = 1,
7320 : CMP_LT = 2,
7321 : CMP_GT = 4,
7322 : CMP_LTU = 8,
7323 : CMP_GTU = 16
7324 : };
7325 :
7326 :
7327 : /* Convert the known results for EQ, LT, GT, LTU, GTU contained in
7328 : KNOWN_RESULT to a CONST_INT, based on the requested comparison CODE
7329 : For KNOWN_RESULT to make sense it should be either CMP_EQ, or the
7330 : logical OR of one of (CMP_LT, CMP_GT) and one of (CMP_LTU, CMP_GTU).
7331 : For floating-point comparisons, assume that the operands were ordered. */
7332 :
7333 : static rtx
7334 846406 : comparison_result (enum rtx_code code, int known_results)
7335 : {
7336 846406 : switch (code)
7337 : {
7338 151588 : case EQ:
7339 151588 : case UNEQ:
7340 151588 : return (known_results & CMP_EQ) ? const_true_rtx : const0_rtx;
7341 477012 : case NE:
7342 477012 : case LTGT:
7343 477012 : return (known_results & CMP_EQ) ? const0_rtx : const_true_rtx;
7344 :
7345 9351 : case LT:
7346 9351 : case UNLT:
7347 9351 : return (known_results & CMP_LT) ? const_true_rtx : const0_rtx;
7348 8692 : case GE:
7349 8692 : case UNGE:
7350 8692 : return (known_results & CMP_LT) ? const0_rtx : const_true_rtx;
7351 :
7352 12485 : case GT:
7353 12485 : case UNGT:
7354 12485 : return (known_results & CMP_GT) ? const_true_rtx : const0_rtx;
7355 13670 : case LE:
7356 13670 : case UNLE:
7357 13670 : return (known_results & CMP_GT) ? const0_rtx : const_true_rtx;
7358 :
7359 44247 : case LTU:
7360 44247 : return (known_results & CMP_LTU) ? const_true_rtx : const0_rtx;
7361 29025 : case GEU:
7362 29025 : return (known_results & CMP_LTU) ? const0_rtx : const_true_rtx;
7363 :
7364 69651 : case GTU:
7365 69651 : return (known_results & CMP_GTU) ? const_true_rtx : const0_rtx;
7366 30619 : case LEU:
7367 30619 : return (known_results & CMP_GTU) ? const0_rtx : const_true_rtx;
7368 :
7369 0 : case ORDERED:
7370 0 : return const_true_rtx;
7371 66 : case UNORDERED:
7372 66 : return const0_rtx;
7373 0 : default:
7374 0 : gcc_unreachable ();
7375 : }
7376 : }
7377 :
7378 : /* Check if the given comparison (done in the given MODE) is actually
7379 : a tautology or a contradiction. If the mode is VOIDmode, the
7380 : comparison is done in "infinite precision". If no simplification
7381 : is possible, this function returns zero. Otherwise, it returns
7382 : either const_true_rtx or const0_rtx. */
7383 :
7384 : rtx
7385 134512895 : simplify_const_relational_operation (enum rtx_code code,
7386 : machine_mode mode,
7387 : rtx op0, rtx op1)
7388 : {
7389 141649487 : rtx tem;
7390 141649487 : rtx trueop0;
7391 141649487 : rtx trueop1;
7392 :
7393 141649487 : gcc_assert (mode != VOIDmode
7394 : || (GET_MODE (op0) == VOIDmode
7395 : && GET_MODE (op1) == VOIDmode));
7396 :
7397 : /* We only handle MODE_CC comparisons that are COMPARE against zero. */
7398 141649487 : if (GET_MODE_CLASS (mode) == MODE_CC
7399 46172072 : && (op1 != const0_rtx
7400 46172072 : || GET_CODE (op0) != COMPARE))
7401 : return NULL_RTX;
7402 :
7403 : /* If op0 is a compare, extract the comparison arguments from it. */
7404 110349472 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
7405 : {
7406 14872057 : op1 = XEXP (op0, 1);
7407 14872057 : op0 = XEXP (op0, 0);
7408 :
7409 14872057 : if (GET_MODE (op0) != VOIDmode)
7410 14742977 : mode = GET_MODE (op0);
7411 129080 : else if (GET_MODE (op1) != VOIDmode)
7412 96651 : mode = GET_MODE (op1);
7413 : else
7414 : return 0;
7415 : }
7416 :
7417 : /* We can't simplify MODE_CC values since we don't know what the
7418 : actual comparison is. */
7419 110317043 : if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
7420 : return 0;
7421 :
7422 : /* Make sure the constant is second. */
7423 110317043 : if (swap_commutative_operands_p (op0, op1))
7424 : {
7425 3034114 : std::swap (op0, op1);
7426 3034114 : code = swap_condition (code);
7427 : }
7428 :
7429 110317043 : trueop0 = avoid_constant_pool_reference (op0);
7430 110317043 : trueop1 = avoid_constant_pool_reference (op1);
7431 :
7432 : /* For integer comparisons of A and B maybe we can simplify A - B and can
7433 : then simplify a comparison of that with zero. If A and B are both either
7434 : a register or a CONST_INT, this can't help; testing for these cases will
7435 : prevent infinite recursion here and speed things up.
7436 :
7437 : We can only do this for EQ and NE comparisons as otherwise we may
7438 : lose or introduce overflow which we cannot disregard as undefined as
7439 : we do not know the signedness of the operation on either the left or
7440 : the right hand side of the comparison. */
7441 :
7442 110317043 : if (INTEGRAL_MODE_P (mode)
7443 107560236 : && trueop1 != CONST0_RTX (mode)
7444 55257372 : && (code == EQ || code == NE)
7445 35087110 : && ! ((REG_P (op0)
7446 10156738 : || CONST_SCALAR_INT_P (trueop0)
7447 10108864 : || CONST_VECTOR_P (trueop0))
7448 24978266 : && (REG_P (op1)
7449 15059355 : || CONST_SCALAR_INT_P (trueop1)
7450 3396428 : || CONST_VECTOR_P (trueop1)))
7451 13502149 : && (tem = simplify_binary_operation (MINUS, mode, op0, op1)) != 0
7452 : /* We cannot do this if tem is a nonzero address. */
7453 7136594 : && ! nonzero_address_p (tem))
7454 7136592 : return simplify_const_relational_operation (signed_condition (code),
7455 7136592 : mode, tem, CONST0_RTX (mode));
7456 :
7457 103180451 : if (! HONOR_NANS (mode) && code == ORDERED)
7458 0 : return const_true_rtx;
7459 :
7460 103180451 : if (! HONOR_NANS (mode) && code == UNORDERED)
7461 8 : return const0_rtx;
7462 :
7463 : /* For modes without NaNs, if the two operands are equal, we know the
7464 : result except if they have side-effects. Even with NaNs we know
7465 : the result of unordered comparisons and, if signaling NaNs are
7466 : irrelevant, also the result of LT/GT/LTGT. */
7467 103180443 : if ((! HONOR_NANS (trueop0)
7468 2255834 : || code == UNEQ || code == UNLE || code == UNGE
7469 : || ((code == LT || code == GT || code == LTGT)
7470 945438 : && ! HONOR_SNANS (trueop0)))
7471 101979829 : && rtx_equal_p (trueop0, trueop1)
7472 103764318 : && ! side_effects_p (trueop0))
7473 583830 : return comparison_result (code, CMP_EQ);
7474 :
7475 : /* If the operands are floating-point constants, see if we can fold
7476 : the result. */
7477 102596613 : if (CONST_DOUBLE_AS_FLOAT_P (trueop0)
7478 1515 : && CONST_DOUBLE_AS_FLOAT_P (trueop1)
7479 1515 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop0)))
7480 : {
7481 1515 : const REAL_VALUE_TYPE *d0 = CONST_DOUBLE_REAL_VALUE (trueop0);
7482 1515 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
7483 :
7484 : /* Comparisons are unordered iff at least one of the values is NaN. */
7485 1515 : if (REAL_VALUE_ISNAN (*d0) || REAL_VALUE_ISNAN (*d1))
7486 174 : switch (code)
7487 : {
7488 0 : case UNEQ:
7489 0 : case UNLT:
7490 0 : case UNGT:
7491 0 : case UNLE:
7492 0 : case UNGE:
7493 0 : case NE:
7494 0 : case UNORDERED:
7495 0 : return const_true_rtx;
7496 174 : case EQ:
7497 174 : case LT:
7498 174 : case GT:
7499 174 : case LE:
7500 174 : case GE:
7501 174 : case LTGT:
7502 174 : case ORDERED:
7503 174 : return const0_rtx;
7504 : default:
7505 : return 0;
7506 : }
7507 :
7508 1510 : return comparison_result (code,
7509 1510 : (real_equal (d0, d1) ? CMP_EQ :
7510 1510 : real_less (d0, d1) ? CMP_LT : CMP_GT));
7511 : }
7512 :
7513 : /* Otherwise, see if the operands are both integers. */
7514 102595098 : if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
7515 99370464 : && CONST_SCALAR_INT_P (trueop0) && CONST_SCALAR_INT_P (trueop1))
7516 : {
7517 : /* It would be nice if we really had a mode here. However, the
7518 : largest int representable on the target is as good as
7519 : infinite. */
7520 261235 : machine_mode cmode = (mode == VOIDmode) ? MAX_MODE_INT : mode;
7521 261235 : rtx_mode_t ptrueop0 = rtx_mode_t (trueop0, cmode);
7522 261235 : rtx_mode_t ptrueop1 = rtx_mode_t (trueop1, cmode);
7523 :
7524 261235 : if (wi::eq_p (ptrueop0, ptrueop1))
7525 0 : return comparison_result (code, CMP_EQ);
7526 : else
7527 : {
7528 261235 : int cr = wi::lts_p (ptrueop0, ptrueop1) ? CMP_LT : CMP_GT;
7529 261235 : cr |= wi::ltu_p (ptrueop0, ptrueop1) ? CMP_LTU : CMP_GTU;
7530 261235 : return comparison_result (code, cr);
7531 : }
7532 : }
7533 :
7534 : /* Optimize comparisons with upper and lower bounds. */
7535 102333863 : scalar_int_mode int_mode;
7536 102333863 : if (CONST_INT_P (trueop1)
7537 71762697 : && is_a <scalar_int_mode> (mode, &int_mode)
7538 71762697 : && HWI_COMPUTABLE_MODE_P (int_mode)
7539 173628716 : && !side_effects_p (trueop0))
7540 : {
7541 71142208 : int sign;
7542 71142208 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (trueop0, int_mode);
7543 71142208 : HOST_WIDE_INT val = INTVAL (trueop1);
7544 71142208 : HOST_WIDE_INT mmin, mmax;
7545 :
7546 71142208 : if (code == GEU
7547 71142208 : || code == LEU
7548 67845564 : || code == GTU
7549 67845564 : || code == LTU)
7550 : sign = 0;
7551 : else
7552 71142208 : sign = 1;
7553 :
7554 : /* Get a reduced range if the sign bit is zero. */
7555 71142208 : if (nonzero <= (GET_MODE_MASK (int_mode) >> 1))
7556 : {
7557 6362952 : mmin = 0;
7558 6362952 : mmax = nonzero;
7559 : }
7560 : else
7561 : {
7562 64779256 : rtx mmin_rtx, mmax_rtx;
7563 64779256 : get_mode_bounds (int_mode, sign, int_mode, &mmin_rtx, &mmax_rtx);
7564 :
7565 64779256 : mmin = INTVAL (mmin_rtx);
7566 64779256 : mmax = INTVAL (mmax_rtx);
7567 64779256 : if (sign)
7568 : {
7569 58632241 : unsigned int sign_copies
7570 58632241 : = num_sign_bit_copies (trueop0, int_mode);
7571 :
7572 58632241 : mmin >>= (sign_copies - 1);
7573 58632241 : mmax >>= (sign_copies - 1);
7574 : }
7575 : }
7576 :
7577 71142208 : switch (code)
7578 : {
7579 : /* x >= y is always true for y <= mmin, always false for y > mmax. */
7580 558909 : case GEU:
7581 558909 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
7582 5622 : return const_true_rtx;
7583 553287 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
7584 62 : return const0_rtx;
7585 : break;
7586 986373 : case GE:
7587 986373 : if (val <= mmin)
7588 1997 : return const_true_rtx;
7589 984376 : if (val > mmax)
7590 0 : return const0_rtx;
7591 : break;
7592 :
7593 : /* x <= y is always true for y >= mmax, always false for y < mmin. */
7594 2737735 : case LEU:
7595 2737735 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
7596 15861 : return const_true_rtx;
7597 2721874 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
7598 0 : return const0_rtx;
7599 : break;
7600 2729120 : case LE:
7601 2729120 : if (val >= mmax)
7602 445 : return const_true_rtx;
7603 2728675 : if (val < mmin)
7604 0 : return const0_rtx;
7605 : break;
7606 :
7607 25963761 : case EQ:
7608 : /* x == y is always false for y out of range. */
7609 25963761 : if (val < mmin || val > mmax)
7610 452 : return const0_rtx;
7611 : break;
7612 :
7613 : /* x > y is always false for y >= mmax, always true for y < mmin. */
7614 2472443 : case GTU:
7615 2472443 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
7616 40656 : return const0_rtx;
7617 2431787 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
7618 0 : return const_true_rtx;
7619 : break;
7620 1950590 : case GT:
7621 1950590 : if (val >= mmax)
7622 244 : return const0_rtx;
7623 1950346 : if (val < mmin)
7624 2 : return const_true_rtx;
7625 : break;
7626 :
7627 : /* x < y is always false for y <= mmin, always true for y > mmax. */
7628 860848 : case LTU:
7629 860848 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
7630 3619 : return const0_rtx;
7631 857229 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
7632 79320 : return const_true_rtx;
7633 : break;
7634 1130946 : case LT:
7635 1130946 : if (val <= mmin)
7636 2284 : return const0_rtx;
7637 1128662 : if (val > mmax)
7638 3158 : return const_true_rtx;
7639 : break;
7640 :
7641 31751483 : case NE:
7642 : /* x != y is always true for y out of range. */
7643 31751483 : if (val < mmin || val > mmax)
7644 156 : return const_true_rtx;
7645 : break;
7646 :
7647 : default:
7648 : break;
7649 : }
7650 : }
7651 :
7652 : /* Optimize integer comparisons with zero. */
7653 102179985 : if (is_a <scalar_int_mode> (mode, &int_mode)
7654 98998432 : && trueop1 == const0_rtx
7655 51496234 : && !side_effects_p (trueop0))
7656 : {
7657 : /* Some addresses are known to be nonzero. We don't know
7658 : their sign, but equality comparisons are known. */
7659 51341565 : if (nonzero_address_p (trueop0))
7660 : {
7661 560 : if (code == EQ || code == LEU)
7662 289 : return const0_rtx;
7663 271 : if (code == NE || code == GTU)
7664 263 : return const_true_rtx;
7665 : }
7666 :
7667 : /* See if the first operand is an IOR with a constant. If so, we
7668 : may be able to determine the result of this comparison. */
7669 51341013 : if (GET_CODE (op0) == IOR)
7670 : {
7671 680688 : rtx inner_const = avoid_constant_pool_reference (XEXP (op0, 1));
7672 680688 : if (CONST_INT_P (inner_const) && inner_const != const0_rtx)
7673 : {
7674 207 : int sign_bitnum = GET_MODE_PRECISION (int_mode) - 1;
7675 414 : int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum
7676 207 : && (UINTVAL (inner_const)
7677 207 : & (HOST_WIDE_INT_1U
7678 : << sign_bitnum)));
7679 :
7680 207 : switch (code)
7681 : {
7682 : case EQ:
7683 : case LEU:
7684 : return const0_rtx;
7685 0 : case NE:
7686 0 : case GTU:
7687 0 : return const_true_rtx;
7688 13 : case LT:
7689 13 : case LE:
7690 13 : if (has_sign)
7691 2 : return const_true_rtx;
7692 : break;
7693 192 : case GT:
7694 192 : case GE:
7695 192 : if (has_sign)
7696 : return const0_rtx;
7697 : break;
7698 : default:
7699 : break;
7700 : }
7701 : }
7702 : }
7703 : }
7704 :
7705 : /* Optimize comparison of ABS with zero. */
7706 51849322 : if (trueop1 == CONST0_RTX (mode) && !side_effects_p (trueop0)
7707 153873577 : && (GET_CODE (trueop0) == ABS
7708 51693727 : || (GET_CODE (trueop0) == FLOAT_EXTEND
7709 86 : && GET_CODE (XEXP (trueop0, 0)) == ABS)))
7710 : {
7711 613 : switch (code)
7712 : {
7713 60 : case LT:
7714 : /* Optimize abs(x) < 0.0. */
7715 60 : if (!INTEGRAL_MODE_P (mode) && !HONOR_SNANS (mode))
7716 0 : return const0_rtx;
7717 : break;
7718 :
7719 42 : case GE:
7720 : /* Optimize abs(x) >= 0.0. */
7721 42 : if (!INTEGRAL_MODE_P (mode) && !HONOR_NANS (mode))
7722 0 : return const_true_rtx;
7723 : break;
7724 :
7725 0 : case UNGE:
7726 : /* Optimize ! (abs(x) < 0.0). */
7727 0 : return const_true_rtx;
7728 :
7729 : default:
7730 : break;
7731 : }
7732 : }
7733 :
7734 : return 0;
7735 : }
7736 :
7737 : /* Recognize expressions of the form (X CMP 0) ? VAL : OP (X)
7738 : where OP is CLZ or CTZ and VAL is the value from CLZ_DEFINED_VALUE_AT_ZERO
7739 : or CTZ_DEFINED_VALUE_AT_ZERO respectively and return OP (X) if the expression
7740 : can be simplified to that or NULL_RTX if not.
7741 : Assume X is compared against zero with CMP_CODE and the true
7742 : arm is TRUE_VAL and the false arm is FALSE_VAL. */
7743 :
7744 : rtx
7745 31968070 : simplify_context::simplify_cond_clz_ctz (rtx x, rtx_code cmp_code,
7746 : rtx true_val, rtx false_val)
7747 : {
7748 31968070 : if (cmp_code != EQ && cmp_code != NE)
7749 : return NULL_RTX;
7750 :
7751 : /* Result on X == 0 and X !=0 respectively. */
7752 22951915 : rtx on_zero, on_nonzero;
7753 22951915 : if (cmp_code == EQ)
7754 : {
7755 : on_zero = true_val;
7756 : on_nonzero = false_val;
7757 : }
7758 : else
7759 : {
7760 12481757 : on_zero = false_val;
7761 12481757 : on_nonzero = true_val;
7762 : }
7763 :
7764 22951915 : rtx_code op_code = GET_CODE (on_nonzero);
7765 22951915 : if ((op_code != CLZ && op_code != CTZ)
7766 2782 : || !rtx_equal_p (XEXP (on_nonzero, 0), x)
7767 22953197 : || !CONST_INT_P (on_zero))
7768 : return NULL_RTX;
7769 :
7770 298 : HOST_WIDE_INT op_val;
7771 298 : scalar_int_mode mode ATTRIBUTE_UNUSED
7772 298 : = as_a <scalar_int_mode> (GET_MODE (XEXP (on_nonzero, 0)));
7773 8 : if (((op_code == CLZ && CLZ_DEFINED_VALUE_AT_ZERO (mode, op_val))
7774 588 : || (op_code == CTZ && CTZ_DEFINED_VALUE_AT_ZERO (mode, op_val)))
7775 330 : && op_val == INTVAL (on_zero))
7776 0 : return on_nonzero;
7777 :
7778 : return NULL_RTX;
7779 : }
7780 :
7781 : /* Try to simplify X given that it appears within operand OP of a
7782 : VEC_MERGE operation whose mask is MASK. X need not use the same
7783 : vector mode as the VEC_MERGE, but it must have the same number of
7784 : elements.
7785 :
7786 : Return the simplified X on success, otherwise return NULL_RTX. */
7787 :
7788 : rtx
7789 2076361 : simplify_context::simplify_merge_mask (rtx x, rtx mask, int op)
7790 : {
7791 2076361 : gcc_assert (VECTOR_MODE_P (GET_MODE (x)));
7792 4152722 : poly_uint64 nunits = GET_MODE_NUNITS (GET_MODE (x));
7793 2076361 : if (GET_CODE (x) == VEC_MERGE && rtx_equal_p (XEXP (x, 2), mask))
7794 : {
7795 5489 : if (side_effects_p (XEXP (x, 1 - op)))
7796 : return NULL_RTX;
7797 :
7798 5265 : return XEXP (x, op);
7799 : }
7800 2070872 : if (UNARY_P (x)
7801 372363 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7802 2128636 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits))
7803 : {
7804 24236 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7805 24236 : if (top0)
7806 448 : return simplify_gen_unary (GET_CODE (x), GET_MODE (x), top0,
7807 448 : GET_MODE (XEXP (x, 0)));
7808 : }
7809 2070424 : if (BINARY_P (x)
7810 207430 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7811 414232 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7812 180003 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7813 2358512 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits))
7814 : {
7815 144044 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7816 144044 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7817 144044 : if (top0 || top1)
7818 : {
7819 952 : if (COMPARISON_P (x))
7820 0 : return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7821 0 : GET_MODE (XEXP (x, 0)) != VOIDmode
7822 : ? GET_MODE (XEXP (x, 0))
7823 0 : : GET_MODE (XEXP (x, 1)),
7824 : top0 ? top0 : XEXP (x, 0),
7825 0 : top1 ? top1 : XEXP (x, 1));
7826 : else
7827 952 : return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7828 : top0 ? top0 : XEXP (x, 0),
7829 952 : top1 ? top1 : XEXP (x, 1));
7830 : }
7831 : }
7832 2069472 : if (GET_RTX_CLASS (GET_CODE (x)) == RTX_TERNARY
7833 55093 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7834 110186 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7835 55093 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7836 110186 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits)
7837 55093 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 2)))
7838 2086280 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 2))), nunits))
7839 : {
7840 8404 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7841 8404 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7842 8404 : rtx top2 = simplify_merge_mask (XEXP (x, 2), mask, op);
7843 8404 : if (top0 || top1 || top2)
7844 448 : return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7845 448 : GET_MODE (XEXP (x, 0)),
7846 : top0 ? top0 : XEXP (x, 0),
7847 : top1 ? top1 : XEXP (x, 1),
7848 448 : top2 ? top2 : XEXP (x, 2));
7849 : }
7850 : return NULL_RTX;
7851 : }
7852 :
7853 :
7854 : /* Simplify CODE, an operation with result mode MODE and three operands,
7855 : OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
7856 : a constant. Return 0 if no simplifications is possible. */
7857 :
7858 : rtx
7859 44619700 : simplify_context::simplify_ternary_operation (rtx_code code, machine_mode mode,
7860 : machine_mode op0_mode,
7861 : rtx op0, rtx op1, rtx op2)
7862 : {
7863 44619700 : bool any_change = false;
7864 44619700 : rtx tem, trueop2;
7865 44619700 : scalar_int_mode int_mode, int_op0_mode;
7866 44619700 : unsigned int n_elts;
7867 :
7868 44619700 : switch (code)
7869 : {
7870 336003 : case FMA:
7871 : /* Simplify negations around the multiplication. */
7872 : /* -a * -b + c => a * b + c. */
7873 336003 : if (GET_CODE (op0) == NEG)
7874 : {
7875 91409 : tem = simplify_unary_operation (NEG, mode, op1, mode);
7876 91409 : if (tem)
7877 276 : op1 = tem, op0 = XEXP (op0, 0), any_change = true;
7878 : }
7879 244594 : else if (GET_CODE (op1) == NEG)
7880 : {
7881 1063 : tem = simplify_unary_operation (NEG, mode, op0, mode);
7882 1063 : if (tem)
7883 0 : op0 = tem, op1 = XEXP (op1, 0), any_change = true;
7884 : }
7885 :
7886 : /* Canonicalize the two multiplication operands. */
7887 : /* a * -b + c => -b * a + c. */
7888 336003 : if (swap_commutative_operands_p (op0, op1))
7889 : std::swap (op0, op1), any_change = true;
7890 :
7891 307688 : if (any_change)
7892 28582 : return gen_rtx_FMA (mode, op0, op1, op2);
7893 : return NULL_RTX;
7894 :
7895 812495 : case SIGN_EXTRACT:
7896 812495 : case ZERO_EXTRACT:
7897 812495 : if (CONST_INT_P (op0)
7898 19339 : && CONST_INT_P (op1)
7899 19339 : && CONST_INT_P (op2)
7900 44619734 : && is_a <scalar_int_mode> (mode, &int_mode)
7901 34 : && INTVAL (op1) + INTVAL (op2) <= GET_MODE_PRECISION (int_mode)
7902 812529 : && HWI_COMPUTABLE_MODE_P (int_mode))
7903 : {
7904 : /* Extracting a bit-field from a constant */
7905 34 : unsigned HOST_WIDE_INT val = UINTVAL (op0);
7906 34 : HOST_WIDE_INT op1val = INTVAL (op1);
7907 34 : HOST_WIDE_INT op2val = INTVAL (op2);
7908 34 : if (!BITS_BIG_ENDIAN)
7909 34 : val >>= op2val;
7910 : else if (is_a <scalar_int_mode> (op0_mode, &int_op0_mode))
7911 : val >>= GET_MODE_PRECISION (int_op0_mode) - op2val - op1val;
7912 : else
7913 : /* Not enough information to calculate the bit position. */
7914 : break;
7915 :
7916 34 : if (HOST_BITS_PER_WIDE_INT != op1val)
7917 : {
7918 : /* First zero-extend. */
7919 31 : val &= (HOST_WIDE_INT_1U << op1val) - 1;
7920 : /* If desired, propagate sign bit. */
7921 31 : if (code == SIGN_EXTRACT
7922 6 : && (val & (HOST_WIDE_INT_1U << (op1val - 1)))
7923 6 : != 0)
7924 2 : val |= ~ ((HOST_WIDE_INT_1U << op1val) - 1);
7925 : }
7926 :
7927 34 : return gen_int_mode (val, int_mode);
7928 : }
7929 : break;
7930 :
7931 42391213 : case IF_THEN_ELSE:
7932 42391213 : if (CONST_INT_P (op0))
7933 201370 : return op0 != const0_rtx ? op1 : op2;
7934 :
7935 : /* Convert c ? a : a into "a". Beware that two rtx_equal_p MEMs can
7936 : still carry different memory attributes, in particular incompatible
7937 : alias sets; returning one of them would narrow the aliasing of the
7938 : result to that operand's, which is unsound (PR125683). When the
7939 : attributes differ, fold to a copy that keeps only what both operands
7940 : guarantee, like merge_memattrs does when cross-jumping commons two
7941 : memory references. */
7942 42189843 : if (rtx_equal_p (op1, op2) && ! side_effects_p (op0))
7943 : {
7944 5513 : if (op1 == op2
7945 2978 : || !MEM_P (op1)
7946 5535 : || (mem_attrs_eq_p (get_mem_attrs (op1), get_mem_attrs (op2))
7947 8 : && MEM_READONLY_P (op1) == MEM_READONLY_P (op2)
7948 8 : && MEM_NOTRAP_P (op1) == MEM_NOTRAP_P (op2)
7949 8 : && MEM_POINTER (op1) == MEM_POINTER (op2)))
7950 : return op1;
7951 :
7952 : /* For BLKmode the size in MEM_ATTRS describes the access itself,
7953 : so it cannot be dropped. Volatility is not merged either: it
7954 : constrains when the access happens rather than describing the
7955 : memory, so unlike the flags below it cannot be weakened to what
7956 : both operands allow. Dropping it would lose a required access;
7957 : merge_memattrs and noce_try_cmove_arith instead set it, which is
7958 : sound but claims more than either operand did. Those two have to
7959 : put something on a reference they are already committed to, while
7960 : this fold is free to do nothing, and if-conversion never reaches
7961 : it with a volatile operand in any case: side_effects_p is true
7962 : for one, so noce_operand_ok rejects it. Decline the fold. */
7963 14 : if (GET_MODE (op1) != BLKmode
7964 14 : && MEM_VOLATILE_P (op1) == MEM_VOLATILE_P (op2))
7965 : {
7966 14 : rtx mem = shallow_copy_rtx (op1);
7967 :
7968 14 : if (MEM_ALIAS_SET (op1) != MEM_ALIAS_SET (op2))
7969 1 : set_mem_alias_set (mem, 0);
7970 :
7971 14 : if (!mem_expr_equal_p (MEM_EXPR (op1), MEM_EXPR (op2)))
7972 : {
7973 14 : set_mem_expr (mem, NULL_TREE);
7974 14 : clear_mem_offset (mem);
7975 : }
7976 0 : else if (MEM_OFFSET_KNOWN_P (op1) != MEM_OFFSET_KNOWN_P (op2)
7977 0 : || (MEM_OFFSET_KNOWN_P (op1)
7978 0 : && maybe_ne (MEM_OFFSET (op1), MEM_OFFSET (op2))))
7979 0 : clear_mem_offset (mem);
7980 :
7981 : /* Unlike merge_memattrs, which fixes up two references that
7982 : both stay in the stream, this returns a single reference
7983 : that stands in for either arm, so keep the size only when
7984 : both agree rather than taking the larger one. */
7985 28 : if (!MEM_SIZE_KNOWN_P (op1) || !MEM_SIZE_KNOWN_P (op2)
7986 28 : || maybe_ne (MEM_SIZE (op1), MEM_SIZE (op2)))
7987 0 : clear_mem_size (mem);
7988 :
7989 14 : set_mem_align (mem, MIN (MEM_ALIGN (op1), MEM_ALIGN (op2)));
7990 :
7991 : /* MEM_READONLY_P, MEM_NOTRAP_P and MEM_POINTER are rtx flag
7992 : bits rather than MEM_ATTRS fields, so shallow_copy_rtx has
7993 : already taken them from OP1 and they need clearing by hand.
7994 : Each asserts something about the reference, so the copy may
7995 : only keep it when both operands do, as merge_memattrs does
7996 : for the first two. */
7997 14 : if (MEM_READONLY_P (op1) != MEM_READONLY_P (op2))
7998 0 : MEM_READONLY_P (mem) = 0;
7999 14 : if (MEM_NOTRAP_P (op1) != MEM_NOTRAP_P (op2))
8000 0 : MEM_NOTRAP_P (mem) = 0;
8001 14 : if (MEM_POINTER (op1) != MEM_POINTER (op2))
8002 0 : MEM_POINTER (mem) = 0;
8003 :
8004 : return mem;
8005 : }
8006 : }
8007 :
8008 : /* Convert a != b ? a : b into "a". */
8009 42184330 : if (GET_CODE (op0) == NE
8010 16452519 : && ! side_effects_p (op0)
8011 16403326 : && ! HONOR_NANS (mode)
8012 16218355 : && ! HONOR_SIGNED_ZEROS (mode)
8013 58402679 : && ((rtx_equal_p (XEXP (op0, 0), op1)
8014 111275 : && rtx_equal_p (XEXP (op0, 1), op2))
8015 16218002 : || (rtx_equal_p (XEXP (op0, 0), op2)
8016 3997 : && rtx_equal_p (XEXP (op0, 1), op1))))
8017 : return op1;
8018 :
8019 : /* Convert a == b ? a : b into "b". */
8020 42183739 : if (GET_CODE (op0) == EQ
8021 13070268 : && ! side_effects_p (op0)
8022 13050741 : && ! HONOR_NANS (mode)
8023 13024318 : && ! HONOR_SIGNED_ZEROS (mode)
8024 55208054 : && ((rtx_equal_p (XEXP (op0, 0), op1)
8025 15582 : && rtx_equal_p (XEXP (op0, 1), op2))
8026 13024309 : || (rtx_equal_p (XEXP (op0, 0), op2)
8027 8547 : && rtx_equal_p (XEXP (op0, 1), op1))))
8028 : return op2;
8029 :
8030 : /* Convert a != 0 ? -a : 0 into "-a". */
8031 42183717 : if (GET_CODE (op0) == NE
8032 16451928 : && ! side_effects_p (op0)
8033 16402735 : && ! HONOR_NANS (mode)
8034 16217764 : && ! HONOR_SIGNED_ZEROS (mode)
8035 16217758 : && XEXP (op0, 1) == CONST0_RTX (mode)
8036 12296648 : && op2 == CONST0_RTX (mode)
8037 190948 : && GET_CODE (op1) == NEG
8038 42183760 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0)))
8039 : return op1;
8040 :
8041 : /* Convert a == 0 ? 0 : -a into "-a". */
8042 42183708 : if (GET_CODE (op0) == EQ
8043 13070246 : && ! side_effects_p (op0)
8044 13050719 : && ! HONOR_NANS (mode)
8045 13024296 : && ! HONOR_SIGNED_ZEROS (mode)
8046 13024293 : && op1 == CONST0_RTX (mode)
8047 43576 : && XEXP (op0, 1) == CONST0_RTX (mode)
8048 22873 : && GET_CODE (op2) == NEG
8049 42183714 : && rtx_equal_p (XEXP (op0, 0), XEXP (op2, 0)))
8050 : return op2;
8051 :
8052 : /* Convert (!c) != {0,...,0} ? a : b into
8053 : c != {0,...,0} ? b : a for vector modes. */
8054 42183702 : if (VECTOR_MODE_P (GET_MODE (op1))
8055 14830 : && GET_CODE (op0) == NE
8056 474 : && GET_CODE (XEXP (op0, 0)) == NOT
8057 0 : && GET_CODE (XEXP (op0, 1)) == CONST_VECTOR)
8058 : {
8059 0 : rtx cv = XEXP (op0, 1);
8060 0 : int nunits;
8061 0 : bool ok = true;
8062 0 : if (!CONST_VECTOR_NUNITS (cv).is_constant (&nunits))
8063 : ok = false;
8064 : else
8065 0 : for (int i = 0; i < nunits; ++i)
8066 0 : if (CONST_VECTOR_ELT (cv, i) != const0_rtx)
8067 : {
8068 : ok = false;
8069 : break;
8070 : }
8071 0 : if (ok)
8072 : {
8073 0 : rtx new_op0 = gen_rtx_NE (GET_MODE (op0),
8074 : XEXP (XEXP (op0, 0), 0),
8075 : XEXP (op0, 1));
8076 0 : rtx retval = gen_rtx_IF_THEN_ELSE (mode, new_op0, op2, op1);
8077 0 : return retval;
8078 : }
8079 : }
8080 :
8081 : /* Convert x == 0 ? N : clz (x) into clz (x) when
8082 : CLZ_DEFINED_VALUE_AT_ZERO is defined to N for the mode of x.
8083 : Similarly for ctz (x). */
8084 42182692 : if (COMPARISON_P (op0) && !side_effects_p (op0)
8085 84264848 : && XEXP (op0, 1) == const0_rtx)
8086 : {
8087 31968070 : rtx simplified
8088 31968070 : = simplify_cond_clz_ctz (XEXP (op0, 0), GET_CODE (op0),
8089 : op1, op2);
8090 31968070 : if (simplified)
8091 : return simplified;
8092 : }
8093 :
8094 42183702 : if (COMPARISON_P (op0) && ! side_effects_p (op0))
8095 : {
8096 84241386 : machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
8097 42081146 : ? GET_MODE (XEXP (op0, 1))
8098 : : GET_MODE (XEXP (op0, 0)));
8099 42081146 : rtx temp;
8100 :
8101 : /* Look for happy constants in op1 and op2. */
8102 42081146 : if (CONST_INT_P (op1) && CONST_INT_P (op2))
8103 : {
8104 235436 : HOST_WIDE_INT t = INTVAL (op1);
8105 235436 : HOST_WIDE_INT f = INTVAL (op2);
8106 :
8107 235436 : if (t == STORE_FLAG_VALUE && f == 0)
8108 55159 : code = GET_CODE (op0);
8109 180277 : else if (t == 0 && f == STORE_FLAG_VALUE)
8110 : {
8111 31337 : enum rtx_code tmp;
8112 31337 : tmp = reversed_comparison_code (op0, NULL);
8113 31337 : if (tmp == UNKNOWN)
8114 : break;
8115 : code = tmp;
8116 : }
8117 : else
8118 : break;
8119 :
8120 81245 : return simplify_gen_relational (code, mode, cmp_mode,
8121 81245 : XEXP (op0, 0), XEXP (op0, 1));
8122 : }
8123 :
8124 41845710 : temp = simplify_relational_operation (GET_CODE (op0), op0_mode,
8125 : cmp_mode, XEXP (op0, 0),
8126 : XEXP (op0, 1));
8127 :
8128 : /* See if any simplifications were possible. */
8129 41845710 : if (temp)
8130 : {
8131 7138 : if (CONST_INT_P (temp))
8132 827 : return temp == const0_rtx ? op2 : op1;
8133 6311 : else if (temp)
8134 6311 : return gen_rtx_IF_THEN_ELSE (mode, temp, op1, op2);
8135 : }
8136 : }
8137 : break;
8138 :
8139 1079989 : case VEC_MERGE:
8140 1079989 : gcc_assert (GET_MODE (op0) == mode);
8141 1079989 : gcc_assert (GET_MODE (op1) == mode);
8142 1079989 : gcc_assert (VECTOR_MODE_P (mode));
8143 1079989 : trueop2 = avoid_constant_pool_reference (op2);
8144 1079989 : if (CONST_INT_P (trueop2)
8145 1831654 : && GET_MODE_NUNITS (mode).is_constant (&n_elts))
8146 : {
8147 751665 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop2);
8148 751665 : unsigned HOST_WIDE_INT mask;
8149 751665 : if (n_elts == HOST_BITS_PER_WIDE_INT)
8150 : mask = -1;
8151 : else
8152 749182 : mask = (HOST_WIDE_INT_1U << n_elts) - 1;
8153 :
8154 751665 : if (!(sel & mask) && !side_effects_p (op0))
8155 : return op1;
8156 751240 : if ((sel & mask) == mask && !side_effects_p (op1))
8157 : return op0;
8158 :
8159 740059 : rtx trueop0 = avoid_constant_pool_reference (op0);
8160 740059 : rtx trueop1 = avoid_constant_pool_reference (op1);
8161 740059 : if (GET_CODE (trueop0) == CONST_VECTOR
8162 10646 : && GET_CODE (trueop1) == CONST_VECTOR)
8163 : {
8164 6721 : rtvec v = rtvec_alloc (n_elts);
8165 6721 : unsigned int i;
8166 :
8167 66012 : for (i = 0; i < n_elts; i++)
8168 52570 : RTVEC_ELT (v, i) = ((sel & (HOST_WIDE_INT_1U << i))
8169 52570 : ? CONST_VECTOR_ELT (trueop0, i)
8170 30802 : : CONST_VECTOR_ELT (trueop1, i));
8171 6721 : return gen_rtx_CONST_VECTOR (mode, v);
8172 : }
8173 :
8174 733338 : if (swap_commutative_operands_p (op0, op1)
8175 : /* Two operands have same precedence, then first bit of mask
8176 : select first operand. */
8177 733338 : || (!swap_commutative_operands_p (op1, op0) && !(sel & 1)))
8178 34070 : return simplify_gen_ternary (code, mode, mode, op1, op0,
8179 68140 : GEN_INT (~sel & mask));
8180 :
8181 : /* Replace (vec_merge (vec_merge a b m) c n) with (vec_merge b c n)
8182 : if no element from a appears in the result. */
8183 699268 : if (GET_CODE (op0) == VEC_MERGE)
8184 : {
8185 52333 : tem = avoid_constant_pool_reference (XEXP (op0, 2));
8186 52333 : if (CONST_INT_P (tem))
8187 : {
8188 36646 : unsigned HOST_WIDE_INT sel0 = UINTVAL (tem);
8189 36646 : if (!(sel & sel0 & mask) && !side_effects_p (XEXP (op0, 0)))
8190 99 : return simplify_gen_ternary (code, mode, mode,
8191 99 : XEXP (op0, 1), op1, op2);
8192 36547 : if (!(sel & ~sel0 & mask) && !side_effects_p (XEXP (op0, 1)))
8193 786 : return simplify_gen_ternary (code, mode, mode,
8194 786 : XEXP (op0, 0), op1, op2);
8195 :
8196 : /* Replace (vec_merge (vec_merge a b m) a n) with
8197 : (vec_merge a b (m|~n)). */
8198 35761 : if (rtx_equal_p (XEXP (op0, 0), op1)
8199 35761 : && ! side_effects_p (op1))
8200 449 : return simplify_gen_ternary (code, mode, mode,
8201 : op1, XEXP (op0, 1),
8202 898 : GEN_INT ((sel0 | ~sel) & mask));
8203 : /* Replace (vec_merge (vec_merge b a m) a n) with
8204 : (vec_merge b a (m&n)). */
8205 35312 : if (rtx_equal_p (XEXP (op0, 1), op1)
8206 35312 : && ! side_effects_p (op1))
8207 57 : return simplify_gen_ternary (code, mode, mode,
8208 : XEXP (op0, 0), op1,
8209 57 : GEN_INT (sel & sel0 & mask));
8210 : }
8211 : }
8212 697877 : if (GET_CODE (op1) == VEC_MERGE)
8213 : {
8214 574 : tem = avoid_constant_pool_reference (XEXP (op1, 2));
8215 574 : if (CONST_INT_P (tem))
8216 : {
8217 547 : unsigned HOST_WIDE_INT sel1 = UINTVAL (tem);
8218 547 : if (!(~sel & sel1 & mask) && !side_effects_p (XEXP (op1, 0)))
8219 517 : return simplify_gen_ternary (code, mode, mode,
8220 517 : op0, XEXP (op1, 1), op2);
8221 30 : if (!(~sel & ~sel1 & mask) && !side_effects_p (XEXP (op1, 1)))
8222 4 : return simplify_gen_ternary (code, mode, mode,
8223 4 : op0, XEXP (op1, 0), op2);
8224 :
8225 : /* Replace (vec_merge a (vec_merge a b m) n) with
8226 : (vec_merge a b (m|n)). */
8227 26 : if (rtx_equal_p (XEXP (op1, 0), op0)
8228 26 : && ! side_effects_p (op0))
8229 0 : return simplify_gen_ternary (code, mode, mode,
8230 : op0, XEXP (op1, 1),
8231 0 : GEN_INT ((sel | sel1) & mask));
8232 :
8233 : /* Replace (vec_merge a (vec_merge b a m) n) with
8234 : (vec_merge a b (~m|n)). */
8235 26 : if (rtx_equal_p (XEXP (op1, 1), op0)
8236 26 : && ! side_effects_p (op0))
8237 0 : return simplify_gen_ternary (code, mode, mode,
8238 : op0, XEXP (op1, 0),
8239 0 : GEN_INT ((sel | ~sel1) & mask));
8240 : }
8241 : }
8242 :
8243 : /* Replace (vec_merge (vec_duplicate (vec_select a parallel (i))) a 1 << i)
8244 : with a. */
8245 697356 : if (GET_CODE (op0) == VEC_DUPLICATE
8246 378466 : && GET_CODE (XEXP (op0, 0)) == VEC_SELECT
8247 1553 : && GET_CODE (XEXP (XEXP (op0, 0), 1)) == PARALLEL
8248 700462 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (op0, 0))), 1))
8249 : {
8250 1485 : tem = XVECEXP ((XEXP (XEXP (op0, 0), 1)), 0, 0);
8251 1485 : if (CONST_INT_P (tem) && CONST_INT_P (op2))
8252 : {
8253 1485 : if (XEXP (XEXP (op0, 0), 0) == op1
8254 2 : && UINTVAL (op2) == HOST_WIDE_INT_1U << UINTVAL (tem))
8255 : return op1;
8256 : }
8257 : }
8258 : /* Replace (vec_merge (vec_duplicate (X)) (const_vector [A, B])
8259 : (const_int N))
8260 : with (vec_concat (X) (B)) if N == 1 or
8261 : (vec_concat (A) (X)) if N == 2. */
8262 697354 : if (GET_CODE (op0) == VEC_DUPLICATE
8263 378464 : && GET_CODE (op1) == CONST_VECTOR
8264 539816 : && known_eq (CONST_VECTOR_NUNITS (op1), 2)
8265 1646 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8266 698177 : && IN_RANGE (sel, 1, 2))
8267 : {
8268 821 : rtx newop0 = XEXP (op0, 0);
8269 821 : rtx newop1 = CONST_VECTOR_ELT (op1, 2 - sel);
8270 821 : if (sel == 2)
8271 111 : std::swap (newop0, newop1);
8272 821 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8273 : }
8274 : /* Replace (vec_merge (vec_duplicate x) (vec_concat (y) (z)) (const_int N))
8275 : with (vec_concat x z) if N == 1, or (vec_concat y x) if N == 2.
8276 : Only applies for vectors of two elements. */
8277 696533 : if (GET_CODE (op0) == VEC_DUPLICATE
8278 377643 : && GET_CODE (op1) == VEC_CONCAT
8279 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8280 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
8281 696533 : && IN_RANGE (sel, 1, 2))
8282 : {
8283 0 : rtx newop0 = XEXP (op0, 0);
8284 0 : rtx newop1 = XEXP (op1, 2 - sel);
8285 0 : rtx otherop = XEXP (op1, sel - 1);
8286 0 : if (sel == 2)
8287 0 : std::swap (newop0, newop1);
8288 : /* Don't want to throw away the other part of the vec_concat if
8289 : it has side-effects. */
8290 0 : if (!side_effects_p (otherop))
8291 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8292 : }
8293 :
8294 : /* Replace:
8295 :
8296 : (vec_merge:outer (vec_duplicate:outer x:inner)
8297 : (subreg:outer y:inner 0)
8298 : (const_int N))
8299 :
8300 : with (vec_concat:outer x:inner y:inner) if N == 1,
8301 : or (vec_concat:outer y:inner x:inner) if N == 2.
8302 :
8303 : Implicitly, this means we have a paradoxical subreg, but such
8304 : a check is cheap, so make it anyway.
8305 :
8306 : Only applies for vectors of two elements. */
8307 696533 : if (GET_CODE (op0) == VEC_DUPLICATE
8308 377643 : && GET_CODE (op1) == SUBREG
8309 35333 : && GET_MODE (op1) == GET_MODE (op0)
8310 35333 : && GET_MODE (SUBREG_REG (op1)) == GET_MODE (XEXP (op0, 0))
8311 0 : && paradoxical_subreg_p (op1)
8312 0 : && subreg_lowpart_p (op1)
8313 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8314 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
8315 696533 : && IN_RANGE (sel, 1, 2))
8316 : {
8317 0 : rtx newop0 = XEXP (op0, 0);
8318 0 : rtx newop1 = SUBREG_REG (op1);
8319 0 : if (sel == 2)
8320 0 : std::swap (newop0, newop1);
8321 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8322 : }
8323 :
8324 : /* Same as above but with switched operands:
8325 : Replace (vec_merge:outer (subreg:outer x:inner 0)
8326 : (vec_duplicate:outer y:inner)
8327 : (const_int N))
8328 :
8329 : with (vec_concat:outer x:inner y:inner) if N == 1,
8330 : or (vec_concat:outer y:inner x:inner) if N == 2. */
8331 696533 : if (GET_CODE (op1) == VEC_DUPLICATE
8332 58176 : && GET_CODE (op0) == SUBREG
8333 20917 : && GET_MODE (op0) == GET_MODE (op1)
8334 20917 : && GET_MODE (SUBREG_REG (op0)) == GET_MODE (XEXP (op1, 0))
8335 0 : && paradoxical_subreg_p (op0)
8336 0 : && subreg_lowpart_p (op0)
8337 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
8338 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8339 696533 : && IN_RANGE (sel, 1, 2))
8340 : {
8341 0 : rtx newop0 = SUBREG_REG (op0);
8342 0 : rtx newop1 = XEXP (op1, 0);
8343 0 : if (sel == 2)
8344 0 : std::swap (newop0, newop1);
8345 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8346 : }
8347 :
8348 : /* Replace (vec_merge (vec_duplicate x) (vec_duplicate y)
8349 : (const_int n))
8350 : with (vec_concat x y) or (vec_concat y x) depending on value
8351 : of N. */
8352 696533 : if (GET_CODE (op0) == VEC_DUPLICATE
8353 377643 : && GET_CODE (op1) == VEC_DUPLICATE
8354 206 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
8355 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
8356 696533 : && IN_RANGE (sel, 1, 2))
8357 : {
8358 0 : rtx newop0 = XEXP (op0, 0);
8359 0 : rtx newop1 = XEXP (op1, 0);
8360 0 : if (sel == 2)
8361 0 : std::swap (newop0, newop1);
8362 :
8363 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
8364 : }
8365 : }
8366 :
8367 1024857 : if (rtx_equal_p (op0, op1)
8368 1024857 : && !side_effects_p (op2) && !side_effects_p (op1))
8369 : return op0;
8370 :
8371 1024561 : if (!side_effects_p (op2))
8372 : {
8373 1020615 : rtx top0
8374 1020615 : = may_trap_p (op0) ? NULL_RTX : simplify_merge_mask (op0, op2, 0);
8375 1020615 : rtx top1
8376 1020615 : = may_trap_p (op1) ? NULL_RTX : simplify_merge_mask (op1, op2, 1);
8377 1020615 : if (top0 || top1)
8378 994 : return simplify_gen_ternary (code, mode, mode,
8379 : top0 ? top0 : op0,
8380 817 : top1 ? top1 : op1, op2);
8381 : }
8382 :
8383 : break;
8384 :
8385 0 : default:
8386 0 : gcc_unreachable ();
8387 : }
8388 :
8389 : return 0;
8390 : }
8391 :
8392 : /* Try to calculate NUM_BYTES bytes of the target memory image of X,
8393 : starting at byte FIRST_BYTE. Return true on success and add the
8394 : bytes to BYTES, such that each byte has BITS_PER_UNIT bits and such
8395 : that the bytes follow target memory order. Leave BYTES unmodified
8396 : on failure.
8397 :
8398 : MODE is the mode of X. The caller must reserve NUM_BYTES bytes in
8399 : BYTES before calling this function. */
8400 :
8401 : bool
8402 14314868 : native_encode_rtx (machine_mode mode, rtx x, vec<target_unit> &bytes,
8403 : unsigned int first_byte, unsigned int num_bytes)
8404 : {
8405 : /* Check the mode is sensible. */
8406 14314868 : gcc_assert (GET_MODE (x) == VOIDmode
8407 : ? is_a <scalar_int_mode> (mode)
8408 : : mode == GET_MODE (x));
8409 :
8410 14314868 : if (GET_CODE (x) == CONST_VECTOR)
8411 : {
8412 : /* CONST_VECTOR_ELT follows target memory order, so no shuffling
8413 : is necessary. The only complication is that MODE_VECTOR_BOOL
8414 : vectors can have several elements per byte. */
8415 1159408 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
8416 : GET_MODE_NUNITS (mode));
8417 579704 : unsigned int elt = first_byte * BITS_PER_UNIT / elt_bits;
8418 579704 : if (elt_bits < BITS_PER_UNIT)
8419 : {
8420 : /* This is the only case in which elements can be smaller than
8421 : a byte. */
8422 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
8423 0 : auto mask = GET_MODE_MASK (GET_MODE_INNER (mode));
8424 0 : for (unsigned int i = 0; i < num_bytes; ++i)
8425 : {
8426 0 : target_unit value = 0;
8427 0 : for (unsigned int j = 0; j < BITS_PER_UNIT; j += elt_bits)
8428 : {
8429 0 : if (INTVAL (CONST_VECTOR_ELT (x, elt)))
8430 0 : value |= mask << j;
8431 0 : elt += 1;
8432 : }
8433 0 : bytes.quick_push (value);
8434 : }
8435 : return true;
8436 : }
8437 :
8438 579704 : unsigned int start = bytes.length ();
8439 579704 : unsigned int elt_bytes = GET_MODE_UNIT_SIZE (mode);
8440 : /* Make FIRST_BYTE relative to ELT. */
8441 579704 : first_byte %= elt_bytes;
8442 2870968 : while (num_bytes > 0)
8443 : {
8444 : /* Work out how many bytes we want from element ELT. */
8445 2291264 : unsigned int chunk_bytes = MIN (num_bytes, elt_bytes - first_byte);
8446 4582528 : if (!native_encode_rtx (GET_MODE_INNER (mode),
8447 : CONST_VECTOR_ELT (x, elt), bytes,
8448 : first_byte, chunk_bytes))
8449 : {
8450 0 : bytes.truncate (start);
8451 0 : return false;
8452 : }
8453 2291264 : elt += 1;
8454 2291264 : first_byte = 0;
8455 2291264 : num_bytes -= chunk_bytes;
8456 : }
8457 : return true;
8458 : }
8459 :
8460 : /* All subsequent cases are limited to scalars. */
8461 13735164 : scalar_mode smode;
8462 13735164 : if (!is_a <scalar_mode> (mode, &smode))
8463 : return false;
8464 :
8465 : /* Make sure that the region is in range. */
8466 13735164 : unsigned int end_byte = first_byte + num_bytes;
8467 13735164 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
8468 13735164 : gcc_assert (end_byte <= mode_bytes);
8469 :
8470 13735164 : if (CONST_SCALAR_INT_P (x))
8471 : {
8472 : /* The target memory layout is affected by both BYTES_BIG_ENDIAN
8473 : and WORDS_BIG_ENDIAN. Use the subreg machinery to get the lsb
8474 : position of each byte. */
8475 13037014 : rtx_mode_t value (x, smode);
8476 13037014 : wide_int_ref value_wi (value);
8477 68660632 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
8478 : {
8479 : /* Always constant because the inputs are. */
8480 42586604 : unsigned int lsb
8481 42586604 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
8482 : /* Operate directly on the encoding rather than using
8483 : wi::extract_uhwi, so that we preserve the sign or zero
8484 : extension for modes that are not a whole number of bits in
8485 : size. (Zero extension is only used for the combination of
8486 : innermode == BImode && STORE_FLAG_VALUE == 1). */
8487 42586604 : unsigned int elt = lsb / HOST_BITS_PER_WIDE_INT;
8488 42586604 : unsigned int shift = lsb % HOST_BITS_PER_WIDE_INT;
8489 42586604 : unsigned HOST_WIDE_INT uhwi = value_wi.elt (elt);
8490 42586604 : bytes.quick_push (uhwi >> shift);
8491 : }
8492 13037014 : return true;
8493 : }
8494 :
8495 698150 : if (CONST_DOUBLE_P (x))
8496 : {
8497 : /* real_to_target produces an array of integers in target memory order.
8498 : All integers before the last one have 32 bits; the last one may
8499 : have 32 bits or fewer, depending on whether the mode bitsize
8500 : is divisible by 32. Each of these integers is then laid out
8501 : in target memory as any other integer would be. */
8502 669907 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
8503 669907 : real_to_target (el32, CONST_DOUBLE_REAL_VALUE (x), smode);
8504 :
8505 : /* The (maximum) number of target bytes per element of el32. */
8506 669907 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
8507 669907 : gcc_assert (bytes_per_el32 != 0);
8508 :
8509 : /* Build up the integers in a similar way to the CONST_SCALAR_INT_P
8510 : handling above. */
8511 4573096 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
8512 : {
8513 3903189 : unsigned int index = byte / bytes_per_el32;
8514 3903189 : unsigned int subbyte = byte % bytes_per_el32;
8515 3903189 : unsigned int int_bytes = MIN (bytes_per_el32,
8516 : mode_bytes - index * bytes_per_el32);
8517 : /* Always constant because the inputs are. */
8518 3903189 : unsigned int lsb
8519 3903189 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
8520 3903189 : bytes.quick_push ((unsigned long) el32[index] >> lsb);
8521 : }
8522 669907 : return true;
8523 : }
8524 :
8525 28243 : if (GET_CODE (x) == CONST_FIXED)
8526 : {
8527 0 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
8528 : {
8529 : /* Always constant because the inputs are. */
8530 0 : unsigned int lsb
8531 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
8532 0 : unsigned HOST_WIDE_INT piece = CONST_FIXED_VALUE_LOW (x);
8533 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
8534 : {
8535 0 : lsb -= HOST_BITS_PER_WIDE_INT;
8536 0 : piece = CONST_FIXED_VALUE_HIGH (x);
8537 : }
8538 0 : bytes.quick_push (piece >> lsb);
8539 : }
8540 : return true;
8541 : }
8542 :
8543 : return false;
8544 : }
8545 :
8546 : /* Read a vector of mode MODE from the target memory image given by BYTES,
8547 : starting at byte FIRST_BYTE. The vector is known to be encodable using
8548 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each,
8549 : and BYTES is known to have enough bytes to supply NPATTERNS *
8550 : NELTS_PER_PATTERN vector elements. Each element of BYTES contains
8551 : BITS_PER_UNIT bits and the bytes are in target memory order.
8552 :
8553 : Return the vector on success, otherwise return NULL_RTX. */
8554 :
8555 : rtx
8556 304119 : native_decode_vector_rtx (machine_mode mode, const vec<target_unit> &bytes,
8557 : unsigned int first_byte, unsigned int npatterns,
8558 : unsigned int nelts_per_pattern)
8559 : {
8560 304119 : rtx_vector_builder builder (mode, npatterns, nelts_per_pattern);
8561 :
8562 608238 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
8563 : GET_MODE_NUNITS (mode));
8564 304119 : if (elt_bits < BITS_PER_UNIT)
8565 : {
8566 : /* This is the only case in which elements can be smaller than a byte.
8567 : Element 0 is always in the lsb of the containing byte. */
8568 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
8569 0 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8570 : {
8571 0 : unsigned int bit_index = first_byte * BITS_PER_UNIT + i * elt_bits;
8572 0 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8573 0 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8574 0 : unsigned int value = bytes[byte_index] >> lsb;
8575 0 : builder.quick_push (gen_int_mode (value, GET_MODE_INNER (mode)));
8576 : }
8577 : }
8578 : else
8579 : {
8580 1225594 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8581 : {
8582 1842950 : rtx x = native_decode_rtx (GET_MODE_INNER (mode), bytes, first_byte);
8583 921475 : if (!x)
8584 0 : return NULL_RTX;
8585 921475 : builder.quick_push (x);
8586 921475 : first_byte += elt_bits / BITS_PER_UNIT;
8587 : }
8588 : }
8589 304119 : return builder.build ();
8590 304119 : }
8591 :
8592 : /* Extract a PRECISION-bit integer from bytes [FIRST_BYTE, FIRST_BYTE + SIZE)
8593 : of target memory image BYTES. */
8594 :
8595 : wide_int
8596 12142980 : native_decode_int (const vec<target_unit> &bytes, unsigned int first_byte,
8597 : unsigned int size, unsigned int precision)
8598 : {
8599 : /* Pull the bytes msb first, so that we can use simple
8600 : shift-and-insert wide_int operations. */
8601 12142980 : wide_int result (wi::zero (precision));
8602 56329558 : for (unsigned int i = 0; i < size; ++i)
8603 : {
8604 44186578 : unsigned int lsb = (size - i - 1) * BITS_PER_UNIT;
8605 : /* Always constant because the inputs are. */
8606 44186578 : unsigned int subbyte
8607 44186578 : = subreg_size_offset_from_lsb (1, size, lsb).to_constant ();
8608 44186578 : result <<= BITS_PER_UNIT;
8609 44186578 : result |= bytes[first_byte + subbyte];
8610 : }
8611 12142980 : return result;
8612 : }
8613 :
8614 : /* Read an rtx of mode MODE from the target memory image given by BYTES,
8615 : starting at byte FIRST_BYTE. Each element of BYTES contains BITS_PER_UNIT
8616 : bits and the bytes are in target memory order. The image has enough
8617 : values to specify all bytes of MODE.
8618 :
8619 : Return the rtx on success, otherwise return NULL_RTX. */
8620 :
8621 : rtx
8622 12485495 : native_decode_rtx (machine_mode mode, const vec<target_unit> &bytes,
8623 : unsigned int first_byte)
8624 : {
8625 12485495 : if (VECTOR_MODE_P (mode))
8626 : {
8627 : /* If we know at compile time how many elements there are,
8628 : pull each element directly from BYTES. */
8629 96277 : unsigned int nelts;
8630 192554 : if (GET_MODE_NUNITS (mode).is_constant (&nelts))
8631 96277 : return native_decode_vector_rtx (mode, bytes, first_byte, nelts, 1);
8632 : return NULL_RTX;
8633 : }
8634 :
8635 12389218 : scalar_int_mode imode;
8636 12389218 : if (is_a <scalar_int_mode> (mode, &imode)
8637 12142980 : && GET_MODE_PRECISION (imode) <= MAX_BITSIZE_MODE_ANY_INT)
8638 : {
8639 12142980 : auto result = native_decode_int (bytes, first_byte,
8640 12142980 : GET_MODE_SIZE (imode),
8641 24285960 : GET_MODE_PRECISION (imode));
8642 12142980 : return immed_wide_int_const (result, imode);
8643 12142980 : }
8644 :
8645 246238 : scalar_float_mode fmode;
8646 246238 : if (is_a <scalar_float_mode> (mode, &fmode))
8647 : {
8648 : /* We need to build an array of integers in target memory order.
8649 : All integers before the last one have 32 bits; the last one may
8650 : have 32 bits or fewer, depending on whether the mode bitsize
8651 : is divisible by 32. */
8652 246204 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
8653 246204 : unsigned int num_el32 = CEIL (GET_MODE_BITSIZE (fmode), 32);
8654 246204 : memset (el32, 0, num_el32 * sizeof (long));
8655 :
8656 : /* The (maximum) number of target bytes per element of el32. */
8657 246204 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
8658 246204 : gcc_assert (bytes_per_el32 != 0);
8659 :
8660 246204 : unsigned int mode_bytes = GET_MODE_SIZE (fmode);
8661 1712340 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
8662 : {
8663 1466136 : unsigned int index = byte / bytes_per_el32;
8664 1466136 : unsigned int subbyte = byte % bytes_per_el32;
8665 1466136 : unsigned int int_bytes = MIN (bytes_per_el32,
8666 : mode_bytes - index * bytes_per_el32);
8667 : /* Always constant because the inputs are. */
8668 1466136 : unsigned int lsb
8669 1466136 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
8670 1466136 : el32[index] |= (unsigned long) bytes[first_byte + byte] << lsb;
8671 : }
8672 246204 : REAL_VALUE_TYPE r;
8673 246204 : real_from_target (&r, el32, fmode);
8674 246204 : return const_double_from_real_value (r, fmode);
8675 : }
8676 :
8677 34 : if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
8678 : {
8679 0 : scalar_mode smode = as_a <scalar_mode> (mode);
8680 0 : FIXED_VALUE_TYPE f;
8681 0 : f.data.low = 0;
8682 0 : f.data.high = 0;
8683 0 : f.mode = smode;
8684 :
8685 0 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
8686 0 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
8687 : {
8688 : /* Always constant because the inputs are. */
8689 0 : unsigned int lsb
8690 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
8691 0 : unsigned HOST_WIDE_INT unit = bytes[first_byte + byte];
8692 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
8693 0 : f.data.high |= unit << (lsb - HOST_BITS_PER_WIDE_INT);
8694 : else
8695 0 : f.data.low |= unit << lsb;
8696 : }
8697 0 : return CONST_FIXED_FROM_FIXED_VALUE (f, mode);
8698 : }
8699 :
8700 : return NULL_RTX;
8701 : }
8702 :
8703 : /* Simplify a byte offset BYTE into CONST_VECTOR X. The main purpose
8704 : is to convert a runtime BYTE value into a constant one. */
8705 :
8706 : static poly_uint64
8707 359626 : simplify_const_vector_byte_offset (rtx x, poly_uint64 byte)
8708 : {
8709 : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
8710 359626 : machine_mode mode = GET_MODE (x);
8711 719252 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
8712 : GET_MODE_NUNITS (mode));
8713 : /* The number of bits needed to encode one element from each pattern. */
8714 359626 : unsigned int sequence_bits = CONST_VECTOR_NPATTERNS (x) * elt_bits;
8715 :
8716 : /* Identify the start point in terms of a sequence number and a byte offset
8717 : within that sequence. */
8718 359626 : poly_uint64 first_sequence;
8719 359626 : unsigned HOST_WIDE_INT subbit;
8720 359626 : if (can_div_trunc_p (byte * BITS_PER_UNIT, sequence_bits,
8721 : &first_sequence, &subbit))
8722 : {
8723 359626 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
8724 359626 : if (nelts_per_pattern == 1)
8725 : /* This is a duplicated vector, so the value of FIRST_SEQUENCE
8726 : doesn't matter. */
8727 274922 : byte = subbit / BITS_PER_UNIT;
8728 84704 : else if (nelts_per_pattern == 2 && known_gt (first_sequence, 0U))
8729 : {
8730 : /* The subreg drops the first element from each pattern and
8731 : only uses the second element. Find the first sequence
8732 : that starts on a byte boundary. */
8733 5568 : subbit += least_common_multiple (sequence_bits, BITS_PER_UNIT);
8734 5568 : byte = subbit / BITS_PER_UNIT;
8735 : }
8736 : }
8737 359626 : return byte;
8738 : }
8739 :
8740 : /* Subroutine of simplify_subreg in which:
8741 :
8742 : - X is known to be a CONST_VECTOR
8743 : - OUTERMODE is known to be a vector mode
8744 :
8745 : Try to handle the subreg by operating on the CONST_VECTOR encoding
8746 : rather than on each individual element of the CONST_VECTOR.
8747 :
8748 : Return the simplified subreg on success, otherwise return NULL_RTX. */
8749 :
8750 : static rtx
8751 216310 : simplify_const_vector_subreg (machine_mode outermode, rtx x,
8752 : machine_mode innermode, unsigned int first_byte)
8753 : {
8754 : /* Paradoxical subregs of vectors have dubious semantics. */
8755 216310 : if (paradoxical_subreg_p (outermode, innermode))
8756 : return NULL_RTX;
8757 :
8758 : /* We can only preserve the semantics of a stepped pattern if the new
8759 : vector element is the same as the original one. */
8760 216127 : if (CONST_VECTOR_STEPPED_P (x)
8761 237925 : && GET_MODE_INNER (outermode) != GET_MODE_INNER (innermode))
8762 : return NULL_RTX;
8763 :
8764 : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
8765 207842 : unsigned int x_elt_bits
8766 207842 : = vector_element_size (GET_MODE_PRECISION (innermode),
8767 : GET_MODE_NUNITS (innermode));
8768 207842 : unsigned int out_elt_bits
8769 207842 : = vector_element_size (GET_MODE_PRECISION (outermode),
8770 : GET_MODE_NUNITS (outermode));
8771 :
8772 : /* The number of bits needed to encode one element from every pattern
8773 : of the original vector. */
8774 207842 : unsigned int x_sequence_bits = CONST_VECTOR_NPATTERNS (x) * x_elt_bits;
8775 :
8776 : /* The number of bits needed to encode one element from every pattern
8777 : of the result. */
8778 207842 : unsigned int out_sequence_bits
8779 207842 : = least_common_multiple (x_sequence_bits, out_elt_bits);
8780 :
8781 : /* Work out the number of interleaved patterns in the output vector
8782 : and the number of encoded elements per pattern. */
8783 207842 : unsigned int out_npatterns = out_sequence_bits / out_elt_bits;
8784 207842 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
8785 :
8786 : /* The encoding scheme requires the number of elements to be a multiple
8787 : of the number of patterns, so that each pattern appears at least once
8788 : and so that the same number of elements appear from each pattern. */
8789 415684 : bool ok_p = multiple_p (GET_MODE_NUNITS (outermode), out_npatterns);
8790 207842 : unsigned int const_nunits;
8791 415684 : if (GET_MODE_NUNITS (outermode).is_constant (&const_nunits)
8792 207842 : && (!ok_p || out_npatterns * nelts_per_pattern > const_nunits))
8793 : {
8794 : /* Either the encoding is invalid, or applying it would give us
8795 : more elements than we need. Just encode each element directly. */
8796 : out_npatterns = const_nunits;
8797 : nelts_per_pattern = 1;
8798 : }
8799 : else if (!ok_p)
8800 : return NULL_RTX;
8801 :
8802 : /* Get enough bytes of X to form the new encoding. */
8803 207842 : unsigned int buffer_bits = out_npatterns * nelts_per_pattern * out_elt_bits;
8804 207842 : unsigned int buffer_bytes = CEIL (buffer_bits, BITS_PER_UNIT);
8805 207842 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8806 207842 : if (!native_encode_rtx (innermode, x, buffer, first_byte, buffer_bytes))
8807 : return NULL_RTX;
8808 :
8809 : /* Re-encode the bytes as OUTERMODE. */
8810 207842 : return native_decode_vector_rtx (outermode, buffer, 0, out_npatterns,
8811 207842 : nelts_per_pattern);
8812 207842 : }
8813 :
8814 : /* Try to simplify a subreg of a constant by encoding the subreg region
8815 : as a sequence of target bytes and reading them back in the new mode.
8816 : Return the new value on success, otherwise return null.
8817 :
8818 : The subreg has outer mode OUTERMODE, inner mode INNERMODE, inner value X
8819 : and byte offset FIRST_BYTE. */
8820 :
8821 : static rtx
8822 11274039 : simplify_immed_subreg (fixed_size_mode outermode, rtx x,
8823 : machine_mode innermode, unsigned int first_byte)
8824 : {
8825 11274039 : unsigned int buffer_bytes = GET_MODE_SIZE (outermode);
8826 11274039 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8827 :
8828 : /* Some ports misuse CCmode. */
8829 11274039 : if (GET_MODE_CLASS (outermode) == MODE_CC && CONST_INT_P (x))
8830 : return x;
8831 :
8832 : /* Paradoxical subregs read undefined values for bytes outside of the
8833 : inner value. However, we have traditionally always sign-extended
8834 : integer constants and zero-extended others. */
8835 11272023 : unsigned int inner_bytes = buffer_bytes;
8836 11272023 : if (paradoxical_subreg_p (outermode, innermode))
8837 : {
8838 1167544 : if (!GET_MODE_SIZE (innermode).is_constant (&inner_bytes))
8839 0 : return NULL_RTX;
8840 :
8841 583772 : target_unit filler = 0;
8842 583772 : if (CONST_SCALAR_INT_P (x) && wi::neg_p (rtx_mode_t (x, innermode)))
8843 68067 : filler = -1;
8844 :
8845 : /* Add any leading bytes due to big-endian layout. The number of
8846 : bytes must be constant because both modes have constant size. */
8847 583772 : unsigned int leading_bytes
8848 583772 : = -byte_lowpart_offset (outermode, innermode).to_constant ();
8849 583772 : for (unsigned int i = 0; i < leading_bytes; ++i)
8850 0 : buffer.quick_push (filler);
8851 :
8852 583772 : if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8853 0 : return NULL_RTX;
8854 :
8855 : /* Add any trailing bytes due to little-endian layout. */
8856 7671108 : while (buffer.length () < buffer_bytes)
8857 3251782 : buffer.quick_push (filler);
8858 : }
8859 10688251 : else if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8860 : return NULL_RTX;
8861 11272023 : rtx ret = native_decode_rtx (outermode, buffer, 0);
8862 11272023 : if (ret && FLOAT_MODE_P (outermode))
8863 : {
8864 130366 : auto_vec<target_unit, 128> buffer2 (buffer_bytes);
8865 130366 : if (!native_encode_rtx (outermode, ret, buffer2, 0, buffer_bytes))
8866 : return NULL_RTX;
8867 1433283 : for (unsigned int i = 0; i < buffer_bytes; ++i)
8868 1302952 : if (buffer[i] != buffer2[i])
8869 : return NULL_RTX;
8870 130366 : }
8871 : return ret;
8872 11274039 : }
8873 :
8874 : /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
8875 : Return 0 if no simplifications are possible. */
8876 : rtx
8877 76711284 : simplify_context::simplify_subreg (machine_mode outermode, rtx op,
8878 : machine_mode innermode, poly_uint64 byte)
8879 : {
8880 : /* Little bit of sanity checking. */
8881 76711284 : gcc_assert (innermode != VOIDmode);
8882 76711284 : gcc_assert (outermode != VOIDmode);
8883 76711284 : gcc_assert (innermode != BLKmode);
8884 76711284 : gcc_assert (outermode != BLKmode);
8885 :
8886 76711284 : gcc_assert (GET_MODE (op) == innermode
8887 : || GET_MODE (op) == VOIDmode);
8888 :
8889 153422568 : poly_uint64 outersize = GET_MODE_SIZE (outermode);
8890 76711284 : if (!multiple_p (byte, outersize))
8891 : return NULL_RTX;
8892 :
8893 153422528 : poly_uint64 innersize = GET_MODE_SIZE (innermode);
8894 76711264 : if (maybe_ge (byte, innersize))
8895 : return NULL_RTX;
8896 :
8897 76711264 : if (outermode == innermode && known_eq (byte, 0U))
8898 4637757 : return op;
8899 :
8900 72073507 : if (GET_CODE (op) == CONST_VECTOR)
8901 359626 : byte = simplify_const_vector_byte_offset (op, byte);
8902 :
8903 144147014 : if (multiple_p (byte, GET_MODE_UNIT_SIZE (innermode)))
8904 : {
8905 65939771 : rtx elt;
8906 :
8907 56609875 : if (VECTOR_MODE_P (outermode)
8908 27989688 : && GET_MODE_INNER (outermode) == GET_MODE_INNER (innermode)
8909 68008135 : && vec_duplicate_p (op, &elt))
8910 14197 : return gen_vec_duplicate (outermode, elt);
8911 :
8912 65933958 : if (outermode == GET_MODE_INNER (innermode)
8913 65933958 : && vec_duplicate_p (op, &elt))
8914 8384 : return elt;
8915 : }
8916 :
8917 72059310 : if (CONST_SCALAR_INT_P (op)
8918 60986155 : || CONST_DOUBLE_AS_FLOAT_P (op)
8919 60928789 : || CONST_FIXED_P (op)
8920 60928789 : || GET_CODE (op) == CONST_VECTOR)
8921 : {
8922 11481881 : unsigned HOST_WIDE_INT cbyte;
8923 11481881 : if (byte.is_constant (&cbyte))
8924 : {
8925 11481881 : if (GET_CODE (op) == CONST_VECTOR && VECTOR_MODE_P (outermode))
8926 : {
8927 216310 : rtx tmp = simplify_const_vector_subreg (outermode, op,
8928 : innermode, cbyte);
8929 216310 : if (tmp)
8930 11481881 : return tmp;
8931 : }
8932 :
8933 11274039 : fixed_size_mode fs_outermode;
8934 11274039 : if (is_a <fixed_size_mode> (outermode, &fs_outermode))
8935 11274039 : return simplify_immed_subreg (fs_outermode, op, innermode, cbyte);
8936 : }
8937 : }
8938 :
8939 : /* Changing mode twice with SUBREG => just change it once,
8940 : or not at all if changing back op starting mode. */
8941 60577429 : if (GET_CODE (op) == SUBREG)
8942 : {
8943 1702122 : machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
8944 3404244 : poly_uint64 innermostsize = GET_MODE_SIZE (innermostmode);
8945 1702122 : rtx newx;
8946 :
8947 : /* Make sure that the relationship between the two subregs is
8948 : known at compile time. */
8949 1702122 : if (!ordered_p (outersize, innermostsize))
8950 : return NULL_RTX;
8951 :
8952 1702122 : if (outermode == innermostmode
8953 1024356 : && known_eq (byte, subreg_lowpart_offset (outermode, innermode))
8954 2726471 : && known_eq (SUBREG_BYTE (op),
8955 : subreg_lowpart_offset (innermode, innermostmode)))
8956 1024349 : return SUBREG_REG (op);
8957 :
8958 : /* Work out the memory offset of the final OUTERMODE value relative
8959 : to the inner value of OP. */
8960 677773 : poly_int64 mem_offset = subreg_memory_offset (outermode,
8961 : innermode, byte);
8962 677773 : poly_int64 op_mem_offset = subreg_memory_offset (op);
8963 677773 : poly_int64 final_offset = mem_offset + op_mem_offset;
8964 :
8965 : /* See whether resulting subreg will be paradoxical. */
8966 677773 : if (!paradoxical_subreg_p (outermode, innermostmode))
8967 : {
8968 : /* Bail out in case resulting subreg would be incorrect. */
8969 1056118 : if (maybe_lt (final_offset, 0)
8970 1056111 : || maybe_ge (poly_uint64 (final_offset), innermostsize)
8971 1056111 : || !multiple_p (final_offset, outersize))
8972 : return NULL_RTX;
8973 : }
8974 : else
8975 : {
8976 149714 : poly_int64 required_offset = subreg_memory_offset (outermode,
8977 : innermostmode, 0);
8978 149714 : if (maybe_ne (final_offset, required_offset))
8979 1 : return NULL_RTX;
8980 : /* Paradoxical subregs always have byte offset 0. */
8981 149713 : final_offset = 0;
8982 : }
8983 :
8984 : /* Recurse for further possible simplifications. */
8985 677757 : newx = simplify_subreg (outermode, SUBREG_REG (op), innermostmode,
8986 677757 : final_offset);
8987 677757 : if (newx)
8988 : return newx;
8989 677338 : if (validate_subreg (outermode, innermostmode,
8990 677338 : SUBREG_REG (op), final_offset))
8991 : {
8992 620320 : newx = gen_rtx_SUBREG (outermode, SUBREG_REG (op), final_offset);
8993 620320 : if (SUBREG_PROMOTED_VAR_P (op)
8994 807 : && SUBREG_PROMOTED_SIGN (op) >= 0
8995 807 : && GET_MODE_CLASS (outermode) == MODE_INT
8996 803 : && known_ge (outersize, innersize)
8997 363 : && known_le (outersize, innermostsize)
8998 620378 : && subreg_lowpart_p (newx))
8999 : {
9000 58 : SUBREG_PROMOTED_VAR_P (newx) = 1;
9001 58 : SUBREG_PROMOTED_SET (newx, SUBREG_PROMOTED_GET (op));
9002 : }
9003 : return newx;
9004 : }
9005 : return NULL_RTX;
9006 : }
9007 :
9008 : /* SUBREG of a hard register => just change the register number
9009 : and/or mode. If the hard register is not valid in that mode,
9010 : suppress this simplification. If the hard register is the stack,
9011 : frame, or argument pointer, leave this as a SUBREG. */
9012 :
9013 58875307 : if (REG_P (op) && HARD_REGISTER_P (op))
9014 : {
9015 11178198 : unsigned int regno, final_regno;
9016 :
9017 11178198 : regno = REGNO (op);
9018 11178198 : final_regno = simplify_subreg_regno (regno, innermode, byte, outermode);
9019 11178198 : if (HARD_REGISTER_NUM_P (final_regno))
9020 : {
9021 11152447 : rtx x = gen_rtx_REG_offset (op, outermode, final_regno,
9022 : subreg_memory_offset (outermode,
9023 : innermode, byte));
9024 :
9025 : /* Propagate original regno. We don't have any way to specify
9026 : the offset inside original regno, so do so only for lowpart.
9027 : The information is used only by alias analysis that cannot
9028 : grog partial register anyway. */
9029 :
9030 11152447 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte))
9031 8338945 : ORIGINAL_REGNO (x) = ORIGINAL_REGNO (op);
9032 : return x;
9033 : }
9034 : }
9035 :
9036 : /* If we have a SUBREG of a register that we are replacing and we are
9037 : replacing it with a MEM, make a new MEM and try replacing the
9038 : SUBREG with it. Don't do this if the MEM has a mode-dependent address
9039 : or if we would be widening it. */
9040 :
9041 47722860 : if (MEM_P (op)
9042 1684652 : && ! mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op))
9043 : /* Allow splitting of volatile memory references in case we don't
9044 : have instruction to move the whole thing. */
9045 1684649 : && (! MEM_VOLATILE_P (op)
9046 45891 : || ! have_insn_for (SET, innermode))
9047 : && !(STRICT_ALIGNMENT && MEM_ALIGN (op) < GET_MODE_ALIGNMENT (outermode))
9048 1638758 : && known_le (outersize, innersize)
9049 : /* You can't get the Nth element of a vector of addresses (i.e.
9050 : gather/scatter) by adding an offset to the base. */
9051 48555252 : && !VECTOR_MODE_P (GET_MODE (XEXP (op, 0))))
9052 832392 : return adjust_address_nv (op, outermode, byte);
9053 :
9054 : /* Simplify (subreg:<m> (mem:<vec_m> (reg:<vec_p> addr)) x)
9055 : to (mem:<m> (subreg:<p> (reg:<vec_p>)) y) */
9056 46890468 : if (MEM_P (op)
9057 852260 : && VECTOR_MODE_P (innermode)
9058 13232 : && VECTOR_MODE_P (GET_MODE (XEXP (op, 0)))
9059 46890468 : && GET_MODE_INNER (innermode) == outermode)
9060 : {
9061 0 : poly_uint64 index;
9062 0 : if (multiple_p (byte, GET_MODE_SIZE (outermode), &index))
9063 : {
9064 0 : rtx addr = XEXP (op, 0);
9065 0 : machine_mode addrmode = GET_MODE (addr);
9066 0 : scalar_mode addr_elt_mode = GET_MODE_INNER (addrmode);
9067 0 : poly_uint64 addr_byte = index * GET_MODE_SIZE (addr_elt_mode);
9068 :
9069 0 : rtx new_addr = simplify_gen_subreg (addr_elt_mode, addr, addrmode,
9070 : addr_byte);
9071 0 : if (new_addr)
9072 0 : return change_address_nv (op, outermode, new_addr);
9073 : }
9074 : }
9075 :
9076 : /* Handle complex or vector values represented as CONCAT or VEC_CONCAT
9077 : of two parts. */
9078 46890468 : if (GET_CODE (op) == CONCAT
9079 46890468 : || GET_CODE (op) == VEC_CONCAT)
9080 : {
9081 257910 : poly_uint64 final_offset;
9082 257910 : rtx part, res;
9083 :
9084 257910 : machine_mode part_mode = GET_MODE (XEXP (op, 0));
9085 257910 : if (part_mode == VOIDmode)
9086 17 : part_mode = GET_MODE_INNER (GET_MODE (op));
9087 515820 : poly_uint64 part_size = GET_MODE_SIZE (part_mode);
9088 257910 : if (known_lt (byte, part_size))
9089 : {
9090 256379 : part = XEXP (op, 0);
9091 256379 : final_offset = byte;
9092 : }
9093 1531 : else if (known_ge (byte, part_size))
9094 : {
9095 1531 : part = XEXP (op, 1);
9096 1531 : final_offset = byte - part_size;
9097 : }
9098 : else
9099 : return NULL_RTX;
9100 :
9101 257910 : if (maybe_gt (final_offset + outersize, part_size))
9102 : return NULL_RTX;
9103 :
9104 129062 : part_mode = GET_MODE (part);
9105 129062 : if (part_mode == VOIDmode)
9106 0 : part_mode = GET_MODE_INNER (GET_MODE (op));
9107 129062 : res = simplify_subreg (outermode, part, part_mode, final_offset);
9108 129062 : if (res)
9109 : return res;
9110 368 : if (GET_MODE (part) != VOIDmode
9111 368 : && validate_subreg (outermode, part_mode, part, final_offset))
9112 320 : return gen_rtx_SUBREG (outermode, part, final_offset);
9113 : return NULL_RTX;
9114 : }
9115 :
9116 : /* Simplify
9117 : (subreg (vec_merge (X)
9118 : (vector)
9119 : (const_int ((1 << N) | M)))
9120 : (N * sizeof (outermode)))
9121 : to
9122 : (subreg (X) (N * sizeof (outermode)))
9123 : */
9124 46632558 : unsigned int idx;
9125 93265116 : if (constant_multiple_p (byte, GET_MODE_SIZE (outermode), &idx)
9126 46632558 : && idx < HOST_BITS_PER_WIDE_INT
9127 46632558 : && GET_CODE (op) == VEC_MERGE
9128 542228 : && GET_MODE_INNER (innermode) == outermode
9129 4882 : && CONST_INT_P (XEXP (op, 2))
9130 46636867 : && (UINTVAL (XEXP (op, 2)) & (HOST_WIDE_INT_1U << idx)) != 0)
9131 4300 : return simplify_gen_subreg (outermode, XEXP (op, 0), innermode, byte);
9132 :
9133 : /* A SUBREG resulting from a zero extension may fold to zero if
9134 : it extracts higher bits that the ZERO_EXTEND's source bits. */
9135 46628258 : if (GET_CODE (op) == ZERO_EXTEND && SCALAR_INT_MODE_P (innermode))
9136 : {
9137 244685 : poly_uint64 bitpos = subreg_lsb_1 (outermode, innermode, byte);
9138 244685 : if (known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (XEXP (op, 0)))))
9139 55898 : return CONST0_RTX (outermode);
9140 : }
9141 :
9142 : /* Optimize SUBREGS of scalar integral ASHIFT by a valid constant. */
9143 46572360 : if (GET_CODE (op) == ASHIFT
9144 1089871 : && SCALAR_INT_MODE_P (innermode)
9145 1006959 : && CONST_INT_P (XEXP (op, 1))
9146 923227 : && INTVAL (XEXP (op, 1)) > 0
9147 48585458 : && known_gt (GET_MODE_BITSIZE (innermode), INTVAL (XEXP (op, 1))))
9148 : {
9149 923227 : HOST_WIDE_INT val = INTVAL (XEXP (op, 1));
9150 : /* A lowpart SUBREG of a ASHIFT by a constant may fold to zero. */
9151 923227 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte)
9152 1808694 : && known_le (GET_MODE_BITSIZE (outermode), val))
9153 196199 : return CONST0_RTX (outermode);
9154 : /* Optimize the highpart SUBREG of a suitable ASHIFT (ZERO_EXTEND). */
9155 761577 : if (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
9156 35281 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == outermode
9157 70204 : && known_eq (GET_MODE_BITSIZE (outermode), val)
9158 69098 : && known_eq (GET_MODE_BITSIZE (innermode), 2 * val)
9159 796858 : && known_eq (subreg_highpart_offset (outermode, innermode), byte))
9160 34549 : return XEXP (XEXP (op, 0), 0);
9161 : }
9162 :
9163 48089415 : auto distribute_subreg = [&](rtx op)
9164 : {
9165 1713254 : return simplify_subreg (outermode, op, innermode, byte);
9166 46376161 : };
9167 :
9168 : /* Try distributing the subreg through logic operations, if that
9169 : leads to all subexpressions being simplified. For example,
9170 : distributing the outer subreg in:
9171 :
9172 : (subreg:SI (not:QI (subreg:QI (reg:SI X) <lowpart>)) 0)
9173 :
9174 : gives:
9175 :
9176 : (not:SI (reg:SI X))
9177 :
9178 : This should be a win if the outermode is word_mode, since logical
9179 : operations on word_mode should (a) be no more expensive than logical
9180 : operations on subword modes and (b) are likely to be cheaper than
9181 : logical operations on multiword modes.
9182 :
9183 : Otherwise, handle the case where the subreg is non-narrowing and does
9184 : not change the number of words. The non-narrowing condition ensures
9185 : that we don't convert word_mode operations to subword operations. */
9186 46376161 : scalar_int_mode int_outermode, int_innermode;
9187 46376161 : if (is_a <scalar_int_mode> (outermode, &int_outermode)
9188 39300439 : && is_a <scalar_int_mode> (innermode, &int_innermode)
9189 84141287 : && (outermode == word_mode
9190 22744105 : || ((GET_MODE_PRECISION (int_outermode)
9191 22744105 : >= GET_MODE_PRECISION (int_innermode))
9192 4470228 : && (CEIL (GET_MODE_SIZE (int_outermode), UNITS_PER_WORD)
9193 4397524 : <= CEIL (GET_MODE_SIZE (int_innermode), UNITS_PER_WORD)))))
9194 19357868 : switch (GET_CODE (op))
9195 : {
9196 30618 : case NOT:
9197 30618 : if (rtx op0 = distribute_subreg (XEXP (op, 0)))
9198 1887 : return simplify_gen_unary (GET_CODE (op), outermode, op0, outermode);
9199 : break;
9200 :
9201 522135 : case AND:
9202 522135 : case IOR:
9203 522135 : case XOR:
9204 522135 : if (rtx op0 = distribute_subreg (XEXP (op, 0)))
9205 209765 : if (rtx op1 = distribute_subreg (XEXP (op, 1)))
9206 205116 : return simplify_gen_binary (GET_CODE (op), outermode, op0, op1);
9207 : break;
9208 :
9209 : default:
9210 : break;
9211 : }
9212 :
9213 46169158 : if (is_a <scalar_int_mode> (outermode)
9214 39093436 : && is_a <scalar_int_mode> (innermode)
9215 85262594 : && known_eq (byte,
9216 : subreg_lowpart_offset (as_a <scalar_int_mode> (outermode),
9217 : as_a <scalar_int_mode> (innermode))))
9218 : {
9219 35226635 : scalar_int_mode int_outermode = as_a <scalar_int_mode> (outermode);
9220 35226635 : scalar_int_mode int_innermode = as_a <scalar_int_mode> (innermode);
9221 :
9222 : /* Handle polynomial integers. The upper bits of a paradoxical
9223 : subreg are undefined, so this is safe regardless of whether
9224 : we're truncating or extending. */
9225 35226635 : if (CONST_POLY_INT_P (op))
9226 : {
9227 : poly_wide_int val
9228 : = poly_wide_int::from (const_poly_int_value (op),
9229 : GET_MODE_PRECISION (int_outermode),
9230 : SIGNED);
9231 : return immed_wide_int_const (val, int_outermode);
9232 : }
9233 :
9234 35226635 : if (GET_MODE_PRECISION (int_outermode)
9235 35226635 : < GET_MODE_PRECISION (int_innermode))
9236 : {
9237 22009332 : rtx tem = simplify_truncation (int_outermode, op, int_innermode);
9238 22009332 : if (tem)
9239 : return tem;
9240 : }
9241 : }
9242 :
9243 : /* If the outer mode is not integral, try taking a subreg with the equivalent
9244 : integer outer mode and then bitcasting the result.
9245 : Other simplifications rely on integer to integer subregs and we'd
9246 : potentially miss out on optimizations otherwise. */
9247 88652044 : if (known_gt (GET_MODE_SIZE (innermode),
9248 : GET_MODE_SIZE (outermode))
9249 23857523 : && SCALAR_INT_MODE_P (innermode)
9250 22575084 : && !SCALAR_INT_MODE_P (outermode)
9251 68338345 : && int_mode_for_size (GET_MODE_BITSIZE (outermode),
9252 77400 : 0).exists (&int_outermode))
9253 : {
9254 77400 : rtx tem = simplify_subreg (int_outermode, op, innermode, byte);
9255 77400 : if (tem)
9256 1926 : return lowpart_subreg (outermode, tem, int_outermode);
9257 : }
9258 :
9259 : /* If OP is a vector comparison and the subreg is not changing the
9260 : number of elements or the size of the elements, change the result
9261 : of the comparison to the new mode. */
9262 44324096 : if (COMPARISON_P (op)
9263 310991 : && VECTOR_MODE_P (outermode)
9264 219565 : && VECTOR_MODE_P (innermode)
9265 658671 : && known_eq (GET_MODE_NUNITS (outermode), GET_MODE_NUNITS (innermode))
9266 44729900 : && known_eq (GET_MODE_UNIT_SIZE (outermode),
9267 : GET_MODE_UNIT_SIZE (innermode)))
9268 : /* INNERMODE is the mode of the comparison result; the comparison is
9269 : still done in the mode of its operands. */
9270 134924 : return simplify_gen_relational (GET_CODE (op), outermode, VOIDmode,
9271 134924 : XEXP (op, 0), XEXP (op, 1));
9272 :
9273 : /* Distribute non-paradoxical subregs through logic ops in cases where
9274 : one term disappears.
9275 :
9276 : (subreg:M1 (and:M2 X C1)) -> (subreg:M1 X)
9277 : (subreg:M1 (ior:M2 X C1)) -> (subreg:M1 C1)
9278 : (subreg:M1 (xor:M2 X C1)) -> (subreg:M1 (not:M2 X))
9279 :
9280 : if M2 is no smaller than M1 and (subreg:M1 C1) is all-ones.
9281 :
9282 : (subreg:M1 (and:M2 X C2)) -> (subreg:M1 C2)
9283 : (subreg:M1 (ior/xor:M2 X C2)) -> (subreg:M1 X)
9284 :
9285 : if M2 is no smaller than M1 and (subreg:M1 C2) is zero. */
9286 44189172 : if (known_ge (innersize, outersize)
9287 30378336 : && GET_MODE_CLASS (outermode) == GET_MODE_CLASS (innermode)
9288 28031121 : && (GET_CODE (op) == AND || GET_CODE (op) == IOR || GET_CODE (op) == XOR)
9289 46009487 : && CONSTANT_P (XEXP (op, 1)))
9290 : {
9291 943456 : rtx op1_subreg = distribute_subreg (XEXP (op, 1));
9292 943456 : if (op1_subreg == CONSTM1_RTX (outermode))
9293 : {
9294 124330 : if (GET_CODE (op) == IOR)
9295 : return op1_subreg;
9296 124096 : rtx op0 = XEXP (op, 0);
9297 124096 : if (GET_CODE (op) == XOR)
9298 893 : op0 = simplify_gen_unary (NOT, innermode, op0, innermode);
9299 124096 : return simplify_gen_subreg (outermode, op0, innermode, byte);
9300 : }
9301 :
9302 819126 : if (op1_subreg == CONST0_RTX (outermode))
9303 12523 : return (GET_CODE (op) == AND
9304 12523 : ? op1_subreg
9305 7280 : : distribute_subreg (XEXP (op, 0)));
9306 : }
9307 :
9308 : return NULL_RTX;
9309 : }
9310 :
9311 : /* Make a SUBREG operation or equivalent if it folds. */
9312 :
9313 : rtx
9314 47371423 : simplify_context::simplify_gen_subreg (machine_mode outermode, rtx op,
9315 : machine_mode innermode,
9316 : poly_uint64 byte)
9317 : {
9318 47371423 : rtx newx;
9319 :
9320 47371423 : newx = simplify_subreg (outermode, op, innermode, byte);
9321 47371423 : if (newx)
9322 : return newx;
9323 :
9324 22572262 : if (GET_CODE (op) == SUBREG
9325 22572262 : || GET_CODE (op) == CONCAT
9326 22537274 : || CONST_SCALAR_INT_P (op)
9327 22537248 : || CONST_DOUBLE_AS_FLOAT_P (op)
9328 22537248 : || CONST_FIXED_P (op)
9329 22537248 : || GET_CODE (op) == CONST_VECTOR)
9330 : return NULL_RTX;
9331 :
9332 22537234 : if (validate_subreg (outermode, innermode, op, byte))
9333 22505105 : return gen_rtx_SUBREG (outermode, op, byte);
9334 :
9335 : return NULL_RTX;
9336 : }
9337 :
9338 : /* Generates a subreg to get the least significant part of EXPR (in mode
9339 : INNER_MODE) to OUTER_MODE. */
9340 :
9341 : rtx
9342 34994731 : simplify_context::lowpart_subreg (machine_mode outer_mode, rtx expr,
9343 : machine_mode inner_mode)
9344 : {
9345 34994731 : return simplify_gen_subreg (outer_mode, expr, inner_mode,
9346 34994731 : subreg_lowpart_offset (outer_mode, inner_mode));
9347 : }
9348 :
9349 : /* Generate RTX to select element at INDEX out of vector OP. */
9350 :
9351 : rtx
9352 683783 : simplify_context::simplify_gen_vec_select (rtx op, unsigned int index)
9353 : {
9354 683783 : gcc_assert (VECTOR_MODE_P (GET_MODE (op)));
9355 :
9356 683783 : scalar_mode imode = GET_MODE_INNER (GET_MODE (op));
9357 :
9358 1367566 : if (known_eq (index * GET_MODE_SIZE (imode),
9359 : subreg_lowpart_offset (imode, GET_MODE (op))))
9360 : {
9361 683633 : rtx res = lowpart_subreg (imode, op, GET_MODE (op));
9362 683633 : if (res)
9363 : return res;
9364 : }
9365 :
9366 671 : rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (index)));
9367 671 : return gen_rtx_VEC_SELECT (imode, op, tmp);
9368 : }
9369 :
9370 :
9371 : /* Simplify X, an rtx expression.
9372 :
9373 : Return the simplified expression or NULL if no simplifications
9374 : were possible.
9375 :
9376 : This is the preferred entry point into the simplification routines;
9377 : however, we still allow passes to call the more specific routines.
9378 :
9379 : Right now GCC has three (yes, three) major bodies of RTL simplification
9380 : code that need to be unified.
9381 :
9382 : 1. fold_rtx in cse.cc. This code uses various CSE specific
9383 : information to aid in RTL simplification.
9384 :
9385 : 2. simplify_rtx in combine.cc. Similar to fold_rtx, except that
9386 : it uses combine specific information to aid in RTL
9387 : simplification.
9388 :
9389 : 3. The routines in this file.
9390 :
9391 :
9392 : Long term we want to only have one body of simplification code; to
9393 : get to that state I recommend the following steps:
9394 :
9395 : 1. Pour over fold_rtx & simplify_rtx and move any simplifications
9396 : which are not pass dependent state into these routines.
9397 :
9398 : 2. As code is moved by #1, change fold_rtx & simplify_rtx to
9399 : use this routine whenever possible.
9400 :
9401 : 3. Allow for pass dependent state to be provided to these
9402 : routines and add simplifications based on the pass dependent
9403 : state. Remove code from cse.cc & combine.cc that becomes
9404 : redundant/dead.
9405 :
9406 : It will take time, but ultimately the compiler will be easier to
9407 : maintain and improve. It's totally silly that when we add a
9408 : simplification that it needs to be added to 4 places (3 for RTL
9409 : simplification and 1 for tree simplification. */
9410 :
9411 : rtx
9412 47649961 : simplify_rtx (const_rtx x)
9413 : {
9414 47649961 : const enum rtx_code code = GET_CODE (x);
9415 47649961 : const machine_mode mode = GET_MODE (x);
9416 :
9417 47649961 : switch (GET_RTX_CLASS (code))
9418 : {
9419 792757 : case RTX_UNARY:
9420 1585514 : return simplify_unary_operation (code, mode,
9421 792757 : XEXP (x, 0), GET_MODE (XEXP (x, 0)));
9422 27435318 : case RTX_COMM_ARITH:
9423 27435318 : if (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
9424 443085 : return simplify_gen_binary (code, mode, XEXP (x, 1), XEXP (x, 0));
9425 :
9426 : /* Fall through. */
9427 :
9428 33506516 : case RTX_BIN_ARITH:
9429 33506516 : return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
9430 :
9431 80058 : case RTX_TERNARY:
9432 80058 : case RTX_BITFIELD_OPS:
9433 80058 : return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
9434 80058 : XEXP (x, 0), XEXP (x, 1),
9435 80058 : XEXP (x, 2));
9436 :
9437 202058 : case RTX_COMPARE:
9438 202058 : case RTX_COMM_COMPARE:
9439 202058 : return simplify_relational_operation (code, mode,
9440 202058 : ((GET_MODE (XEXP (x, 0))
9441 : != VOIDmode)
9442 : ? GET_MODE (XEXP (x, 0))
9443 359 : : GET_MODE (XEXP (x, 1))),
9444 202058 : XEXP (x, 0),
9445 404116 : XEXP (x, 1));
9446 :
9447 228642 : case RTX_EXTRA:
9448 228642 : if (code == SUBREG)
9449 2798 : return simplify_subreg (mode, SUBREG_REG (x),
9450 2798 : GET_MODE (SUBREG_REG (x)),
9451 2798 : SUBREG_BYTE (x));
9452 : break;
9453 :
9454 6560221 : case RTX_OBJ:
9455 6560221 : if (code == LO_SUM)
9456 : {
9457 : /* Convert (lo_sum (high FOO) FOO) to FOO. */
9458 0 : if (GET_CODE (XEXP (x, 0)) == HIGH
9459 0 : && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
9460 0 : return XEXP (x, 1);
9461 : }
9462 : break;
9463 :
9464 : default:
9465 : break;
9466 : }
9467 : return NULL;
9468 : }
9469 :
9470 : #if CHECKING_P
9471 :
9472 : namespace selftest {
9473 :
9474 : /* Make a unique pseudo REG of mode MODE for use by selftests. */
9475 :
9476 : static rtx
9477 2948 : make_test_reg (machine_mode mode)
9478 : {
9479 2948 : static int test_reg_num = LAST_VIRTUAL_REGISTER + 1;
9480 :
9481 2948 : return gen_rtx_REG (mode, test_reg_num++);
9482 : }
9483 :
9484 : /* Test reversing rotations with a masked negative count. */
9485 :
9486 : static void
9487 40 : test_masked_negative_rotate (machine_mode mode)
9488 : {
9489 40 : rtx value = make_test_reg (mode);
9490 40 : unsigned int precision = GET_MODE_PRECISION (mode).to_constant ();
9491 40 : int count_bits = exact_log2 (precision);
9492 40 : static const machine_mode count_modes[] = { QImode, SImode, DImode };
9493 40 : static const rtx_code codes[] = { ROTATE, ROTATERT };
9494 :
9495 160 : for (auto count_mode : count_modes)
9496 : {
9497 120 : rtx reg = make_test_reg (count_mode);
9498 120 : rtx neg = gen_rtx_NEG (count_mode, reg);
9499 120 : rtx mask = gen_int_mode (precision - 1, count_mode);
9500 120 : rtx count = gen_rtx_AND (count_mode, neg, mask);
9501 120 : rtx positive = simplify_gen_binary (AND, count_mode, reg, mask);
9502 :
9503 360 : for (auto code : codes)
9504 : {
9505 240 : rtx_code reverse = code == ROTATE ? ROTATERT : ROTATE;
9506 240 : rtx expected = NULL_RTX;
9507 240 : if (count_bits >= 0
9508 240 : && GET_MODE_PRECISION (count_mode).to_constant () > count_bits)
9509 200 : expected = simplify_gen_binary (reverse, mode, value, positive);
9510 240 : ASSERT_RTX_EQ (expected,
9511 : simplify_binary_operation (code, mode, value, count));
9512 :
9513 : /* A bare negative count or a narrower mask must not be folded. */
9514 240 : ASSERT_FALSE (simplify_binary_operation (code, mode, value, neg));
9515 240 : rtx narrow = gen_rtx_AND (count_mode, neg,
9516 : gen_int_mode (precision / 2 - 1, count_mode));
9517 240 : ASSERT_FALSE (simplify_binary_operation (code, mode, value, narrow));
9518 :
9519 240 : if (count_mode == QImode)
9520 80 : continue;
9521 160 : expected = NULL_RTX;
9522 160 : if (count_bits >= 0 && GET_MODE_PRECISION (QImode) > count_bits)
9523 112 : expected = simplify_gen_binary
9524 112 : (reverse, mode, value,
9525 : simplify_gen_unary (TRUNCATE, QImode, positive, count_mode));
9526 160 : rtx low = gen_rtx_SUBREG (QImode, count,
9527 : subreg_lowpart_offset (QImode, count_mode));
9528 160 : ASSERT_RTX_EQ (expected,
9529 : simplify_binary_operation (code, mode, value, low));
9530 160 : rtx trunc = gen_rtx_TRUNCATE (QImode, count);
9531 160 : ASSERT_RTX_EQ (expected,
9532 : simplify_binary_operation (code, mode, value, trunc));
9533 160 : poly_uint64 offset = subreg_highpart_offset (QImode, count_mode);
9534 160 : if (validate_subreg (QImode, count_mode, count, offset))
9535 : {
9536 0 : rtx high = gen_rtx_SUBREG (QImode, count, offset);
9537 0 : ASSERT_FALSE (simplify_binary_operation (code, mode, value, high));
9538 : }
9539 : }
9540 : }
9541 40 : }
9542 :
9543 : static void
9544 40 : test_scalar_int_ops (machine_mode mode)
9545 : {
9546 40 : rtx op0 = make_test_reg (mode);
9547 40 : rtx op1 = make_test_reg (mode);
9548 40 : rtx six = GEN_INT (6);
9549 :
9550 40 : rtx neg_op0 = simplify_gen_unary (NEG, mode, op0, mode);
9551 40 : rtx not_op0 = simplify_gen_unary (NOT, mode, op0, mode);
9552 40 : rtx bswap_op0 = simplify_gen_unary (BSWAP, mode, op0, mode);
9553 :
9554 40 : rtx and_op0_op1 = simplify_gen_binary (AND, mode, op0, op1);
9555 40 : rtx ior_op0_op1 = simplify_gen_binary (IOR, mode, op0, op1);
9556 40 : rtx xor_op0_op1 = simplify_gen_binary (XOR, mode, op0, op1);
9557 :
9558 40 : rtx and_op0_6 = simplify_gen_binary (AND, mode, op0, six);
9559 40 : rtx and_op1_6 = simplify_gen_binary (AND, mode, op1, six);
9560 :
9561 : /* Test some binary identities. */
9562 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, op0, const0_rtx));
9563 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, const0_rtx, op0));
9564 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MINUS, mode, op0, const0_rtx));
9565 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, op0, const1_rtx));
9566 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, const1_rtx, op0));
9567 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (DIV, mode, op0, const1_rtx));
9568 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, constm1_rtx));
9569 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, constm1_rtx, op0));
9570 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, const0_rtx));
9571 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, const0_rtx, op0));
9572 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, op0, const0_rtx));
9573 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, const0_rtx, op0));
9574 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFT, mode, op0, const0_rtx));
9575 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATE, mode, op0, const0_rtx));
9576 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFTRT, mode, op0, const0_rtx));
9577 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (LSHIFTRT, mode, op0, const0_rtx));
9578 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATERT, mode, op0, const0_rtx));
9579 :
9580 : /* Test some self-inverse operations. */
9581 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NEG, mode, neg_op0, mode));
9582 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NOT, mode, not_op0, mode));
9583 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (BSWAP, mode, bswap_op0, mode));
9584 :
9585 : /* Test some reflexive operations. */
9586 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, op0));
9587 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, op0));
9588 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMIN, mode, op0, op0));
9589 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMAX, mode, op0, op0));
9590 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMIN, mode, op0, op0));
9591 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMAX, mode, op0, op0));
9592 :
9593 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (MINUS, mode, op0, op0));
9594 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (XOR, mode, op0, op0));
9595 :
9596 : /* Test simplify_distributive_operation. */
9597 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, xor_op0_op1, six),
9598 : simplify_gen_binary (XOR, mode, and_op0_6, and_op1_6));
9599 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, ior_op0_op1, six),
9600 : simplify_gen_binary (IOR, mode, and_op0_6, and_op1_6));
9601 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, and_op0_op1, six),
9602 : simplify_gen_binary (AND, mode, and_op0_6, and_op1_6));
9603 :
9604 : /* Test useless extensions are eliminated. */
9605 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (TRUNCATE, mode, op0, mode));
9606 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (ZERO_EXTEND, mode, op0, mode));
9607 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (SIGN_EXTEND, mode, op0, mode));
9608 40 : ASSERT_RTX_EQ (op0, lowpart_subreg (mode, op0, mode));
9609 :
9610 40 : test_masked_negative_rotate (mode);
9611 40 : }
9612 :
9613 : /* Verify some simplifications of integer extension/truncation.
9614 : Machine mode BMODE is the guaranteed wider than SMODE. */
9615 :
9616 : static void
9617 24 : test_scalar_int_ext_ops (machine_mode bmode, machine_mode smode)
9618 : {
9619 24 : rtx sreg = make_test_reg (smode);
9620 :
9621 : /* Check truncation of extension. */
9622 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9623 : simplify_gen_unary (ZERO_EXTEND, bmode,
9624 : sreg, smode),
9625 : bmode),
9626 : sreg);
9627 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9628 : simplify_gen_unary (SIGN_EXTEND, bmode,
9629 : sreg, smode),
9630 : bmode),
9631 : sreg);
9632 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9633 : lowpart_subreg (bmode, sreg, smode),
9634 : bmode),
9635 : sreg);
9636 :
9637 : /* Test extensions, followed by logic ops, followed by truncations. */
9638 24 : rtx bsubreg = lowpart_subreg (bmode, sreg, smode);
9639 24 : rtx smask = gen_int_mode (GET_MODE_MASK (smode), bmode);
9640 24 : rtx inv_smask = gen_int_mode (~GET_MODE_MASK (smode), bmode);
9641 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9642 : simplify_gen_binary (AND, bmode,
9643 : bsubreg, smask),
9644 : bmode),
9645 : sreg);
9646 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9647 : simplify_gen_binary (AND, bmode,
9648 : bsubreg, inv_smask),
9649 : bmode),
9650 : const0_rtx);
9651 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9652 : simplify_gen_binary (IOR, bmode,
9653 : bsubreg, smask),
9654 : bmode),
9655 : constm1_rtx);
9656 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9657 : simplify_gen_binary (IOR, bmode,
9658 : bsubreg, inv_smask),
9659 : bmode),
9660 : sreg);
9661 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9662 : simplify_gen_binary (XOR, bmode,
9663 : bsubreg, smask),
9664 : bmode),
9665 : lowpart_subreg (smode,
9666 : gen_rtx_NOT (bmode, bsubreg),
9667 : bmode));
9668 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
9669 : simplify_gen_binary (XOR, bmode,
9670 : bsubreg, inv_smask),
9671 : bmode),
9672 : sreg);
9673 :
9674 24 : if (known_le (GET_MODE_PRECISION (bmode), BITS_PER_WORD))
9675 : {
9676 24 : rtx breg1 = make_test_reg (bmode);
9677 24 : rtx breg2 = make_test_reg (bmode);
9678 24 : rtx ssubreg1 = lowpart_subreg (smode, breg1, bmode);
9679 24 : rtx ssubreg2 = lowpart_subreg (smode, breg2, bmode);
9680 24 : rtx not_1 = simplify_gen_unary (NOT, smode, ssubreg1, smode);
9681 24 : rtx and_12 = simplify_gen_binary (AND, smode, ssubreg1, ssubreg2);
9682 24 : rtx ior_12 = simplify_gen_binary (IOR, smode, ssubreg1, ssubreg2);
9683 24 : rtx xor_12 = simplify_gen_binary (XOR, smode, ssubreg1, ssubreg2);
9684 24 : rtx and_n12 = simplify_gen_binary (AND, smode, not_1, ssubreg2);
9685 24 : rtx ior_n12 = simplify_gen_binary (IOR, smode, not_1, ssubreg2);
9686 24 : rtx xor_12_c = simplify_gen_binary (XOR, smode, xor_12, const1_rtx);
9687 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, not_1, smode),
9688 : gen_rtx_NOT (bmode, breg1));
9689 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, and_12, smode),
9690 : gen_rtx_AND (bmode, breg1, breg2));
9691 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, ior_12, smode),
9692 : gen_rtx_IOR (bmode, breg1, breg2));
9693 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, xor_12, smode),
9694 : gen_rtx_XOR (bmode, breg1, breg2));
9695 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, and_n12, smode),
9696 : gen_rtx_AND (bmode, gen_rtx_NOT (bmode, breg1), breg2));
9697 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, ior_n12, smode),
9698 : gen_rtx_IOR (bmode, gen_rtx_NOT (bmode, breg1), breg2));
9699 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, xor_12_c, smode),
9700 : gen_rtx_XOR (bmode,
9701 : gen_rtx_XOR (bmode, breg1, breg2),
9702 : const1_rtx));
9703 : }
9704 24 : }
9705 :
9706 : /* Verify more simplifications of integer extension/truncation.
9707 : BMODE is wider than MMODE which is wider than SMODE. */
9708 :
9709 : static void
9710 16 : test_scalar_int_ext_ops2 (machine_mode bmode, machine_mode mmode,
9711 : machine_mode smode)
9712 : {
9713 16 : rtx breg = make_test_reg (bmode);
9714 16 : rtx mreg = make_test_reg (mmode);
9715 16 : rtx sreg = make_test_reg (smode);
9716 :
9717 : /* Check truncate of truncate. */
9718 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9719 : simplify_gen_unary (TRUNCATE, mmode,
9720 : breg, bmode),
9721 : mmode),
9722 : simplify_gen_unary (TRUNCATE, smode, breg, bmode));
9723 :
9724 : /* Check extension of extension. */
9725 16 : ASSERT_RTX_EQ (simplify_gen_unary (ZERO_EXTEND, bmode,
9726 : simplify_gen_unary (ZERO_EXTEND, mmode,
9727 : sreg, smode),
9728 : mmode),
9729 : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
9730 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
9731 : simplify_gen_unary (SIGN_EXTEND, mmode,
9732 : sreg, smode),
9733 : mmode),
9734 : simplify_gen_unary (SIGN_EXTEND, bmode, sreg, smode));
9735 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
9736 : simplify_gen_unary (ZERO_EXTEND, mmode,
9737 : sreg, smode),
9738 : mmode),
9739 : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
9740 :
9741 : /* Check truncation of extension. */
9742 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9743 : simplify_gen_unary (ZERO_EXTEND, bmode,
9744 : mreg, mmode),
9745 : bmode),
9746 : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
9747 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9748 : simplify_gen_unary (SIGN_EXTEND, bmode,
9749 : mreg, mmode),
9750 : bmode),
9751 : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
9752 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
9753 : lowpart_subreg (bmode, mreg, mmode),
9754 : bmode),
9755 : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
9756 16 : }
9757 :
9758 : /* Test comparisons of comparisons, with the inner comparisons being
9759 : between values of mode MODE2 and producing results of mode MODE1,
9760 : and with the outer comparisons producing results of mode MODE0. */
9761 :
9762 : static void
9763 4 : test_comparisons (machine_mode mode0, machine_mode mode1, machine_mode mode2)
9764 : {
9765 4 : rtx reg0 = make_test_reg (mode2);
9766 4 : rtx reg1 = make_test_reg (mode2);
9767 :
9768 4 : static const rtx_code codes[] = {
9769 : EQ, NE, LT, LTU, LE, LEU, GE, GEU, GT, GTU
9770 : };
9771 4 : constexpr auto num_codes = ARRAY_SIZE (codes);
9772 4 : rtx cmps[num_codes];
9773 4 : rtx vals[] = { constm1_rtx, const0_rtx, const1_rtx };
9774 :
9775 44 : for (unsigned int i = 0; i < num_codes; ++i)
9776 40 : cmps[i] = gen_rtx_fmt_ee (codes[i], mode1, reg0, reg1);
9777 :
9778 44 : for (auto code : codes)
9779 440 : for (unsigned int i0 = 0; i0 < num_codes; ++i0)
9780 4400 : for (unsigned int i1 = 0; i1 < num_codes; ++i1)
9781 : {
9782 4000 : rtx cmp_res = simplify_relational_operation (code, mode0, mode1,
9783 : cmps[i0], cmps[i1]);
9784 4000 : if (i0 >= 2 && i1 >= 2 && (i0 ^ i1) & 1)
9785 1280 : ASSERT_TRUE (cmp_res == NULL_RTX);
9786 : else
9787 : {
9788 2720 : ASSERT_TRUE (cmp_res != NULL_RTX
9789 : && (CONSTANT_P (cmp_res)
9790 : || (COMPARISON_P (cmp_res)
9791 : && GET_MODE (cmp_res) == mode0
9792 : && REG_P (XEXP (cmp_res, 0))
9793 : && REG_P (XEXP (cmp_res, 1)))));
9794 10880 : for (rtx reg0_val : vals)
9795 32640 : for (rtx reg1_val : vals)
9796 : {
9797 24480 : rtx val0 = simplify_const_relational_operation
9798 24480 : (codes[i0], mode1, reg0_val, reg1_val);
9799 24480 : rtx val1 = simplify_const_relational_operation
9800 24480 : (codes[i1], mode1, reg0_val, reg1_val);
9801 24480 : rtx val = simplify_const_relational_operation
9802 24480 : (code, mode0, val0, val1);
9803 24480 : rtx folded = cmp_res;
9804 24480 : if (COMPARISON_P (cmp_res))
9805 16704 : folded = simplify_const_relational_operation
9806 16704 : (GET_CODE (cmp_res), mode0,
9807 16704 : XEXP (cmp_res, 0) == reg0 ? reg0_val : reg1_val,
9808 16704 : XEXP (cmp_res, 1) == reg0 ? reg0_val : reg1_val);
9809 24480 : ASSERT_RTX_EQ (val, folded);
9810 : }
9811 : }
9812 : }
9813 4 : }
9814 :
9815 : /* Return true if comparison CODE holds when its operands compare as
9816 : OUTCOME, one of CMP_BIT_LT, CMP_BIT_GT, CMP_BIT_EQ and CMP_BIT_UN. */
9817 :
9818 : static bool
9819 363536 : comparison_holds_p (rtx_code code, int outcome)
9820 : {
9821 363536 : int mask = unsigned_comparison_to_mask (code);
9822 363536 : if (mask == 0 || code == NE)
9823 331944 : mask = comparison_to_mask (code);
9824 363536 : return mask & outcome;
9825 : }
9826 :
9827 : /* Return true if X is a comparison that raises an invalid-operation
9828 : exception for a quiet NaN operand. */
9829 :
9830 : static bool
9831 6664 : comparison_signals_p (rtx x)
9832 : {
9833 6664 : return (COMPARISON_P (x)
9834 7724 : && comparison_mask_signals_p (comparison_to_mask (GET_CODE (x))));
9835 : }
9836 :
9837 : /* Check RES, a simplification of (CODE OP0 OP1) where OP0 and OP1 are
9838 : comparisons between REG0 and REG1, against the value of (CODE OP0 OP1)
9839 : for every possible outcome of comparing REG0 and REG1. CODE is IOR or
9840 : a comparison of the results of OP0 and OP1. NANS_P says whether REG0
9841 : and REG1 can be unordered, TRAPS_P whether the result must signal
9842 : exactly when OP0 or OP1 does. */
9843 :
9844 : static void
9845 49392 : check_logical_comparison (rtx res, rtx_code code, machine_mode mode,
9846 : rtx op0, rtx op1, rtx reg0, rtx reg1,
9847 : bool nans_p, bool traps_p)
9848 : {
9849 49392 : if (res == NULL_RTX)
9850 : return;
9851 :
9852 42480 : ASSERT_TRUE (CONSTANT_P (res)
9853 : || (COMPARISON_P (res)
9854 : && GET_MODE (res) == mode
9855 : && ((XEXP (res, 0) == reg0 && XEXP (res, 1) == reg1)
9856 : || (XEXP (res, 0) == reg1 && XEXP (res, 1) == reg0))));
9857 :
9858 : /* Generic folds of (CODE X X) are allowed to drop a trap. */
9859 42480 : if (traps_p && GET_CODE (op0) != GET_CODE (op1))
9860 2392 : ASSERT_EQ (comparison_signals_p (op0) || comparison_signals_p (op1),
9861 : comparison_signals_p (res));
9862 :
9863 42480 : static const int outcomes[] = {
9864 : CMP_BIT_LT, CMP_BIT_GT, CMP_BIT_EQ, CMP_BIT_UN
9865 : };
9866 212400 : for (int outcome : outcomes)
9867 : {
9868 169920 : if (outcome == CMP_BIT_UN && !nans_p)
9869 32928 : continue;
9870 :
9871 136992 : rtx val0 = (comparison_holds_p (GET_CODE (op0), outcome)
9872 136992 : ? const_true_rtx : const0_rtx);
9873 136992 : rtx val1 = (comparison_holds_p (GET_CODE (op1), outcome)
9874 136992 : ? const_true_rtx : const0_rtx);
9875 136992 : rtx expected;
9876 136992 : if (code == IOR)
9877 19936 : expected = simplify_const_binary_operation (code, mode, val0, val1);
9878 : else
9879 117056 : expected = simplify_const_relational_operation (code, mode,
9880 : val0, val1);
9881 :
9882 136992 : rtx folded = res;
9883 136992 : if (COMPARISON_P (res))
9884 : {
9885 : /* Swapping the operands exchanges < and >. */
9886 89552 : int swapped = outcome;
9887 89552 : if (XEXP (res, 0) == reg1 && outcome == CMP_BIT_LT)
9888 : swapped = CMP_BIT_GT;
9889 89552 : else if (XEXP (res, 0) == reg1 && outcome == CMP_BIT_GT)
9890 89552 : swapped = CMP_BIT_LT;
9891 179104 : folded = (comparison_holds_p (GET_CODE (res), swapped)
9892 89552 : ? const_true_rtx : const0_rtx);
9893 : }
9894 169920 : ASSERT_RTX_EQ (expected, folded);
9895 : }
9896 : }
9897 :
9898 : /* Simplify (CODE (CODE0 REG0 REG1) (CODE1 REG0 REG1)), with all results
9899 : in SImode. CODE is IOR or a comparison; the latter combines the inner
9900 : comparisons through AND, IOR or XOR, inverting the first one for all
9901 : but NE (for example, (ltu X Y) is (and (not X) Y)). */
9902 :
9903 : static rtx
9904 49468 : simplify_logical_comparison (rtx_code code, rtx_code code0, rtx_code code1,
9905 : rtx reg0, rtx reg1)
9906 : {
9907 49468 : rtx op0 = gen_rtx_fmt_ee (code0, SImode, reg0, reg1);
9908 49468 : rtx op1 = gen_rtx_fmt_ee (code1, SImode, reg0, reg1);
9909 49468 : if (code == IOR)
9910 7092 : return simplify_binary_operation (code, SImode, op0, op1);
9911 42376 : return simplify_relational_operation (code, SImode, SImode, op0, op1);
9912 : }
9913 :
9914 : /* Test the simplification of IOR and comparisons of two comparisons
9915 : between operands of mode OP_MODE against an evaluation of them, for
9916 : all pairs of comparison codes. */
9917 :
9918 : static void
9919 36 : test_logical_comparisons_in_mode (machine_mode op_mode)
9920 : {
9921 36 : rtx reg0 = make_test_reg (op_mode);
9922 36 : rtx reg1 = (GET_MODE_CLASS (op_mode) == MODE_CC
9923 36 : ? const0_rtx : make_test_reg (op_mode));
9924 :
9925 36 : static const rtx_code codes[] = {
9926 : EQ, NE, LT, LE, GT, GE, LTGT, ORDERED,
9927 : UNORDERED, UNEQ, UNLT, UNLE, UNGT, UNGE
9928 : };
9929 36 : static const rtx_code outer_codes[] = { IOR, EQ, NE, LTU, LEU, GTU, GEU };
9930 :
9931 36 : bool nans_p = (GET_MODE_CLASS (op_mode) == MODE_CC
9932 36 : || HONOR_NANS (op_mode));
9933 36 : bool traps_p = HONOR_NANS (op_mode) && flag_trapping_math;
9934 :
9935 540 : for (auto code0 : codes)
9936 7560 : for (auto code1 : codes)
9937 56448 : for (auto code : outer_codes)
9938 : {
9939 49392 : rtx res = simplify_logical_comparison (code, code0, code1,
9940 : reg0, reg1);
9941 49392 : check_logical_comparison (res, code, SImode,
9942 : gen_rtx_fmt_ee (code0, SImode, reg0, reg1),
9943 : gen_rtx_fmt_ee (code1, SImode, reg0, reg1),
9944 : reg0, reg1, nans_p, traps_p);
9945 : /* Only a constant result can be expressed for a CC mode, other
9946 : than by the generic (ior X X) -> X. */
9947 49392 : if (GET_MODE_CLASS (op_mode) == MODE_CC
9948 5488 : && code == IOR
9949 5488 : && code0 != code1)
9950 728 : ASSERT_FALSE (res && COMPARISON_P (res));
9951 : }
9952 36 : }
9953 :
9954 : /* Return the code of simplify_logical_comparison's result, or UNKNOWN
9955 : if there is none. A constant result has code CONST_INT. */
9956 :
9957 : static rtx_code
9958 76 : logical_comparison_code (rtx_code code, rtx_code code0, rtx_code code1,
9959 : rtx reg0, rtx reg1)
9960 : {
9961 0 : rtx res = simplify_logical_comparison (code, code0, code1, reg0, reg1);
9962 76 : return res ? GET_CODE (res) : UNKNOWN;
9963 : }
9964 :
9965 : /* Test combinations of CC-mode, integer and floating-point comparisons,
9966 : the latter under the various floating-point options. */
9967 :
9968 : static void
9969 4 : test_logical_comparisons ()
9970 : {
9971 4 : int saved_trapping_math = flag_trapping_math;
9972 4 : int saved_finite_math_only = flag_finite_math_only;
9973 :
9974 4 : test_logical_comparisons_in_mode (CCmode);
9975 :
9976 16 : for (int finite = 0; finite <= 1; ++finite)
9977 24 : for (int trapping = 0; trapping <= 1; ++trapping)
9978 : {
9979 16 : flag_finite_math_only = finite;
9980 16 : flag_trapping_math = trapping;
9981 16 : test_logical_comparisons_in_mode (SImode);
9982 16 : test_logical_comparisons_in_mode (DFmode);
9983 : }
9984 :
9985 4 : rtx a = make_test_reg (DFmode);
9986 4 : rtx b = make_test_reg (DFmode);
9987 4 : rtx cc = make_test_reg (CCmode);
9988 :
9989 : /* All outcomes are possible for a CC mode: x < 0 || x >= 0 is not
9990 : always true. */
9991 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, GE, cc, const0_rtx),
9992 : UNKNOWN);
9993 8 : ASSERT_EQ (logical_comparison_code (IOR, EQ, NE, cc, const0_rtx),
9994 : CONST_INT);
9995 :
9996 4 : if (MODE_HAS_NANS (DFmode))
9997 : {
9998 4 : flag_finite_math_only = 0;
9999 4 : flag_trapping_math = 1;
10000 : /* These would add a trap: ORDERED & NE -> LTGT, ORDERED & UNLT -> LT. */
10001 8 : ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, NE, a, b), UNKNOWN);
10002 8 : ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, UNLT, a, b),
10003 : UNKNOWN);
10004 : /* These would drop one: UNLT | GT -> NE, LT ^ LE -> EQ,
10005 : !UNGE & GT -> false. */
10006 8 : ASSERT_EQ (logical_comparison_code (IOR, UNLT, GT, a, b), UNKNOWN);
10007 8 : ASSERT_EQ (logical_comparison_code (NE, LT, LE, a, b), UNKNOWN);
10008 8 : ASSERT_EQ (logical_comparison_code (LTU, UNGE, GT, a, b), UNKNOWN);
10009 : /* These do not. */
10010 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, GT, a, b), LTGT);
10011 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, EQ, a, b), LE);
10012 8 : ASSERT_EQ (logical_comparison_code (IOR, UNORDERED, EQ, a, b), UNEQ);
10013 8 : ASSERT_EQ (logical_comparison_code (LTU, UNLT, UNLE, a, b), EQ);
10014 8 : ASSERT_EQ (logical_comparison_code (LTU, LT, GE, a, b), GE);
10015 :
10016 4 : flag_trapping_math = 0;
10017 8 : ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, NE, a, b), LTGT);
10018 8 : ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, UNLT, a, b), LT);
10019 8 : ASSERT_EQ (logical_comparison_code (IOR, UNLT, GT, a, b), NE);
10020 8 : ASSERT_EQ (logical_comparison_code (NE, LT, LE, a, b), EQ);
10021 8 : ASSERT_EQ (logical_comparison_code (LTU, UNGE, GT, a, b), CONST_INT);
10022 : }
10023 :
10024 4 : flag_finite_math_only = 1;
10025 4 : flag_trapping_math = 1;
10026 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, GT, a, b), NE);
10027 8 : ASSERT_EQ (logical_comparison_code (IOR, LT, GE, a, b), CONST_INT);
10028 :
10029 4 : flag_trapping_math = saved_trapping_math;
10030 4 : flag_finite_math_only = saved_finite_math_only;
10031 4 : }
10032 :
10033 : /* Verify some simplifications involving scalar expressions. */
10034 :
10035 : static void
10036 4 : test_scalar_ops ()
10037 : {
10038 500 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
10039 : {
10040 496 : machine_mode mode = (machine_mode) i;
10041 496 : if (SCALAR_INT_MODE_P (mode) && mode != BImode)
10042 40 : test_scalar_int_ops (mode);
10043 : }
10044 :
10045 4 : test_scalar_int_ext_ops (HImode, QImode);
10046 4 : test_scalar_int_ext_ops (SImode, QImode);
10047 4 : test_scalar_int_ext_ops (SImode, HImode);
10048 4 : test_scalar_int_ext_ops (DImode, QImode);
10049 4 : test_scalar_int_ext_ops (DImode, HImode);
10050 4 : test_scalar_int_ext_ops (DImode, SImode);
10051 :
10052 4 : test_scalar_int_ext_ops2 (SImode, HImode, QImode);
10053 4 : test_scalar_int_ext_ops2 (DImode, HImode, QImode);
10054 4 : test_scalar_int_ext_ops2 (DImode, SImode, QImode);
10055 4 : test_scalar_int_ext_ops2 (DImode, SImode, HImode);
10056 :
10057 4 : test_comparisons (QImode, HImode, SImode);
10058 4 : test_logical_comparisons ();
10059 4 : }
10060 :
10061 : /* Test vector simplifications involving VEC_DUPLICATE in which the
10062 : operands and result have vector mode MODE. SCALAR_REG is a pseudo
10063 : register that holds one element of MODE. */
10064 :
10065 : static void
10066 224 : test_vector_ops_duplicate (machine_mode mode, rtx scalar_reg)
10067 : {
10068 224 : scalar_mode inner_mode = GET_MODE_INNER (mode);
10069 224 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
10070 448 : poly_uint64 nunits = GET_MODE_NUNITS (mode);
10071 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
10072 : {
10073 : /* Test some simple unary cases with VEC_DUPLICATE arguments. */
10074 124 : rtx not_scalar_reg = gen_rtx_NOT (inner_mode, scalar_reg);
10075 124 : rtx duplicate_not = gen_rtx_VEC_DUPLICATE (mode, not_scalar_reg);
10076 124 : ASSERT_RTX_EQ (duplicate,
10077 : simplify_unary_operation (NOT, mode,
10078 : duplicate_not, mode));
10079 :
10080 124 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
10081 124 : rtx duplicate_neg = gen_rtx_VEC_DUPLICATE (mode, neg_scalar_reg);
10082 124 : ASSERT_RTX_EQ (duplicate,
10083 : simplify_unary_operation (NEG, mode,
10084 : duplicate_neg, mode));
10085 :
10086 : /* Test some simple binary cases with VEC_DUPLICATE arguments. */
10087 124 : ASSERT_RTX_EQ (duplicate,
10088 : simplify_binary_operation (PLUS, mode, duplicate,
10089 : CONST0_RTX (mode)));
10090 :
10091 124 : ASSERT_RTX_EQ (duplicate,
10092 : simplify_binary_operation (MINUS, mode, duplicate,
10093 : CONST0_RTX (mode)));
10094 :
10095 124 : ASSERT_RTX_PTR_EQ (CONST0_RTX (mode),
10096 : simplify_binary_operation (MINUS, mode, duplicate,
10097 : duplicate));
10098 : }
10099 :
10100 : /* Test a scalar VEC_SELECT of a VEC_DUPLICATE. */
10101 224 : rtx zero_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
10102 224 : ASSERT_RTX_PTR_EQ (scalar_reg,
10103 : simplify_binary_operation (VEC_SELECT, inner_mode,
10104 : duplicate, zero_par));
10105 :
10106 224 : unsigned HOST_WIDE_INT const_nunits;
10107 224 : if (nunits.is_constant (&const_nunits))
10108 : {
10109 : /* And again with the final element. */
10110 224 : rtx last_index = gen_int_mode (const_nunits - 1, word_mode);
10111 224 : rtx last_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, last_index));
10112 224 : ASSERT_RTX_PTR_EQ (scalar_reg,
10113 : simplify_binary_operation (VEC_SELECT, inner_mode,
10114 : duplicate, last_par));
10115 :
10116 : /* Test a scalar subreg of a VEC_MERGE of a VEC_DUPLICATE. */
10117 : /* Skip this test for vectors of booleans, because offset is in bytes,
10118 : while vec_merge indices are in elements (usually bits). */
10119 224 : if (GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL)
10120 : {
10121 224 : rtx vector_reg = make_test_reg (mode);
10122 3732 : for (unsigned HOST_WIDE_INT i = 0; i < const_nunits; i++)
10123 : {
10124 3288 : if (i >= HOST_BITS_PER_WIDE_INT)
10125 : break;
10126 3284 : rtx mask = GEN_INT ((HOST_WIDE_INT_1U << i) | (i + 1));
10127 3284 : rtx vm = gen_rtx_VEC_MERGE (mode, duplicate, vector_reg, mask);
10128 6568 : poly_uint64 offset = i * GET_MODE_SIZE (inner_mode);
10129 :
10130 3284 : ASSERT_RTX_EQ (scalar_reg,
10131 : simplify_gen_subreg (inner_mode, vm,
10132 : mode, offset));
10133 : }
10134 : }
10135 : }
10136 :
10137 : /* Test a scalar subreg of a VEC_DUPLICATE. */
10138 224 : poly_uint64 offset = subreg_lowpart_offset (inner_mode, mode);
10139 224 : ASSERT_RTX_EQ (scalar_reg,
10140 : simplify_gen_subreg (inner_mode, duplicate,
10141 : mode, offset));
10142 :
10143 224 : machine_mode narrower_mode;
10144 224 : if (maybe_ne (nunits, 2U)
10145 184 : && multiple_p (nunits, 2)
10146 396 : && mode_for_vector (inner_mode, 2).exists (&narrower_mode)
10147 396 : && VECTOR_MODE_P (narrower_mode))
10148 : {
10149 : /* Test VEC_DUPLICATE of a vector. */
10150 172 : rtx_vector_builder nbuilder (narrower_mode, 2, 1);
10151 172 : nbuilder.quick_push (const0_rtx);
10152 172 : nbuilder.quick_push (const1_rtx);
10153 172 : rtx_vector_builder builder (mode, 2, 1);
10154 172 : builder.quick_push (const0_rtx);
10155 172 : builder.quick_push (const1_rtx);
10156 172 : ASSERT_RTX_EQ (builder.build (),
10157 : simplify_unary_operation (VEC_DUPLICATE, mode,
10158 : nbuilder.build (),
10159 : narrower_mode));
10160 :
10161 : /* Test VEC_SELECT of a vector. */
10162 172 : rtx vec_par
10163 172 : = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, const1_rtx, const0_rtx));
10164 172 : rtx narrower_duplicate
10165 172 : = gen_rtx_VEC_DUPLICATE (narrower_mode, scalar_reg);
10166 172 : ASSERT_RTX_EQ (narrower_duplicate,
10167 : simplify_binary_operation (VEC_SELECT, narrower_mode,
10168 : duplicate, vec_par));
10169 :
10170 : /* Test a vector subreg of a VEC_DUPLICATE. */
10171 172 : poly_uint64 offset = subreg_lowpart_offset (narrower_mode, mode);
10172 172 : ASSERT_RTX_EQ (narrower_duplicate,
10173 : simplify_gen_subreg (narrower_mode, duplicate,
10174 : mode, offset));
10175 172 : }
10176 224 : }
10177 :
10178 : /* Test vector simplifications involving VEC_SERIES in which the
10179 : operands and result have vector mode MODE. SCALAR_REG is a pseudo
10180 : register that holds one element of MODE. */
10181 :
10182 : static void
10183 92 : test_vector_ops_series (machine_mode mode, rtx scalar_reg)
10184 : {
10185 : /* Test unary cases with VEC_SERIES arguments. */
10186 92 : scalar_mode inner_mode = GET_MODE_INNER (mode);
10187 92 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
10188 92 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
10189 92 : rtx series_0_r = gen_rtx_VEC_SERIES (mode, const0_rtx, scalar_reg);
10190 92 : rtx series_0_nr = gen_rtx_VEC_SERIES (mode, const0_rtx, neg_scalar_reg);
10191 92 : rtx series_nr_1 = gen_rtx_VEC_SERIES (mode, neg_scalar_reg, const1_rtx);
10192 92 : rtx series_r_m1 = gen_rtx_VEC_SERIES (mode, scalar_reg, constm1_rtx);
10193 92 : rtx series_r_r = gen_rtx_VEC_SERIES (mode, scalar_reg, scalar_reg);
10194 92 : rtx series_nr_nr = gen_rtx_VEC_SERIES (mode, neg_scalar_reg,
10195 : neg_scalar_reg);
10196 92 : ASSERT_RTX_EQ (series_0_r,
10197 : simplify_unary_operation (NEG, mode, series_0_nr, mode));
10198 92 : ASSERT_RTX_EQ (series_r_m1,
10199 : simplify_unary_operation (NEG, mode, series_nr_1, mode));
10200 92 : ASSERT_RTX_EQ (series_r_r,
10201 : simplify_unary_operation (NEG, mode, series_nr_nr, mode));
10202 :
10203 : /* Test that a VEC_SERIES with a zero step is simplified away. */
10204 92 : ASSERT_RTX_EQ (duplicate,
10205 : simplify_binary_operation (VEC_SERIES, mode,
10206 : scalar_reg, const0_rtx));
10207 :
10208 : /* Test PLUS and MINUS with VEC_SERIES. */
10209 92 : rtx series_0_1 = gen_const_vec_series (mode, const0_rtx, const1_rtx);
10210 92 : rtx series_0_m1 = gen_const_vec_series (mode, const0_rtx, constm1_rtx);
10211 92 : rtx series_r_1 = gen_rtx_VEC_SERIES (mode, scalar_reg, const1_rtx);
10212 92 : ASSERT_RTX_EQ (series_r_r,
10213 : simplify_binary_operation (PLUS, mode, series_0_r,
10214 : duplicate));
10215 92 : ASSERT_RTX_EQ (series_r_1,
10216 : simplify_binary_operation (PLUS, mode, duplicate,
10217 : series_0_1));
10218 92 : ASSERT_RTX_EQ (series_r_m1,
10219 : simplify_binary_operation (PLUS, mode, duplicate,
10220 : series_0_m1));
10221 92 : ASSERT_RTX_EQ (series_0_r,
10222 : simplify_binary_operation (MINUS, mode, series_r_r,
10223 : duplicate));
10224 92 : ASSERT_RTX_EQ (series_r_m1,
10225 : simplify_binary_operation (MINUS, mode, duplicate,
10226 : series_0_1));
10227 92 : ASSERT_RTX_EQ (series_r_1,
10228 : simplify_binary_operation (MINUS, mode, duplicate,
10229 : series_0_m1));
10230 92 : ASSERT_RTX_EQ (series_0_m1,
10231 : simplify_binary_operation (VEC_SERIES, mode, const0_rtx,
10232 : constm1_rtx));
10233 :
10234 : /* Test NEG on constant vector series. */
10235 92 : ASSERT_RTX_EQ (series_0_m1,
10236 : simplify_unary_operation (NEG, mode, series_0_1, mode));
10237 92 : ASSERT_RTX_EQ (series_0_1,
10238 : simplify_unary_operation (NEG, mode, series_0_m1, mode));
10239 :
10240 : /* Test PLUS and MINUS on constant vector series. */
10241 92 : rtx scalar2 = gen_int_mode (2, inner_mode);
10242 92 : rtx scalar3 = gen_int_mode (3, inner_mode);
10243 92 : rtx series_1_1 = gen_const_vec_series (mode, const1_rtx, const1_rtx);
10244 92 : rtx series_0_2 = gen_const_vec_series (mode, const0_rtx, scalar2);
10245 92 : rtx series_1_3 = gen_const_vec_series (mode, const1_rtx, scalar3);
10246 92 : ASSERT_RTX_EQ (series_1_1,
10247 : simplify_binary_operation (PLUS, mode, series_0_1,
10248 : CONST1_RTX (mode)));
10249 92 : ASSERT_RTX_EQ (series_0_m1,
10250 : simplify_binary_operation (PLUS, mode, CONST0_RTX (mode),
10251 : series_0_m1));
10252 92 : ASSERT_RTX_EQ (series_1_3,
10253 : simplify_binary_operation (PLUS, mode, series_1_1,
10254 : series_0_2));
10255 92 : ASSERT_RTX_EQ (series_0_1,
10256 : simplify_binary_operation (MINUS, mode, series_1_1,
10257 : CONST1_RTX (mode)));
10258 92 : ASSERT_RTX_EQ (series_1_1,
10259 : simplify_binary_operation (MINUS, mode, CONST1_RTX (mode),
10260 : series_0_m1));
10261 92 : ASSERT_RTX_EQ (series_1_1,
10262 : simplify_binary_operation (MINUS, mode, series_1_3,
10263 : series_0_2));
10264 :
10265 : /* Test MULT between constant vectors. */
10266 92 : rtx vec2 = gen_const_vec_duplicate (mode, scalar2);
10267 92 : rtx vec3 = gen_const_vec_duplicate (mode, scalar3);
10268 92 : rtx scalar9 = gen_int_mode (9, inner_mode);
10269 92 : rtx series_3_9 = gen_const_vec_series (mode, scalar3, scalar9);
10270 92 : ASSERT_RTX_EQ (series_0_2,
10271 : simplify_binary_operation (MULT, mode, series_0_1, vec2));
10272 92 : ASSERT_RTX_EQ (series_3_9,
10273 : simplify_binary_operation (MULT, mode, vec3, series_1_3));
10274 92 : if (!GET_MODE_NUNITS (mode).is_constant ())
10275 : ASSERT_FALSE (simplify_binary_operation (MULT, mode, series_0_1,
10276 : series_0_1));
10277 :
10278 : /* Test ASHIFT between constant vectors. */
10279 92 : ASSERT_RTX_EQ (series_0_2,
10280 : simplify_binary_operation (ASHIFT, mode, series_0_1,
10281 : CONST1_RTX (mode)));
10282 92 : if (!GET_MODE_NUNITS (mode).is_constant ())
10283 : ASSERT_FALSE (simplify_binary_operation (ASHIFT, mode, CONST1_RTX (mode),
10284 : series_0_1));
10285 92 : }
10286 :
10287 : static rtx
10288 3136 : simplify_merge_mask (rtx x, rtx mask, int op)
10289 : {
10290 0 : return simplify_context ().simplify_merge_mask (x, mask, op);
10291 : }
10292 :
10293 : /* Verify simplify_merge_mask works correctly. */
10294 :
10295 : static void
10296 224 : test_vec_merge (machine_mode mode)
10297 : {
10298 224 : rtx op0 = make_test_reg (mode);
10299 224 : rtx op1 = make_test_reg (mode);
10300 224 : rtx op2 = make_test_reg (mode);
10301 224 : rtx op3 = make_test_reg (mode);
10302 224 : rtx op4 = make_test_reg (mode);
10303 224 : rtx op5 = make_test_reg (mode);
10304 224 : rtx mask1 = make_test_reg (SImode);
10305 224 : rtx mask2 = make_test_reg (SImode);
10306 224 : rtx vm1 = gen_rtx_VEC_MERGE (mode, op0, op1, mask1);
10307 224 : rtx vm2 = gen_rtx_VEC_MERGE (mode, op2, op3, mask1);
10308 224 : rtx vm3 = gen_rtx_VEC_MERGE (mode, op4, op5, mask1);
10309 :
10310 : /* Simple vec_merge. */
10311 224 : ASSERT_EQ (op0, simplify_merge_mask (vm1, mask1, 0));
10312 224 : ASSERT_EQ (op1, simplify_merge_mask (vm1, mask1, 1));
10313 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 0));
10314 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 1));
10315 :
10316 : /* Nested vec_merge.
10317 : It's tempting to make this simplify right down to opN, but we don't
10318 : because all the simplify_* functions assume that the operands have
10319 : already been simplified. */
10320 224 : rtx nvm = gen_rtx_VEC_MERGE (mode, vm1, vm2, mask1);
10321 224 : ASSERT_EQ (vm1, simplify_merge_mask (nvm, mask1, 0));
10322 224 : ASSERT_EQ (vm2, simplify_merge_mask (nvm, mask1, 1));
10323 :
10324 : /* Intermediate unary op. */
10325 224 : rtx unop = gen_rtx_NOT (mode, vm1);
10326 224 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op0),
10327 : simplify_merge_mask (unop, mask1, 0));
10328 224 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op1),
10329 : simplify_merge_mask (unop, mask1, 1));
10330 :
10331 : /* Intermediate binary op. */
10332 224 : rtx binop = gen_rtx_PLUS (mode, vm1, vm2);
10333 224 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op0, op2),
10334 : simplify_merge_mask (binop, mask1, 0));
10335 224 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op1, op3),
10336 : simplify_merge_mask (binop, mask1, 1));
10337 :
10338 : /* Intermediate ternary op. */
10339 224 : rtx tenop = gen_rtx_FMA (mode, vm1, vm2, vm3);
10340 224 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op0, op2, op4),
10341 : simplify_merge_mask (tenop, mask1, 0));
10342 224 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op1, op3, op5),
10343 : simplify_merge_mask (tenop, mask1, 1));
10344 :
10345 : /* Side effects. */
10346 224 : rtx badop0 = gen_rtx_PRE_INC (mode, op0);
10347 224 : rtx badvm = gen_rtx_VEC_MERGE (mode, badop0, op1, mask1);
10348 224 : ASSERT_EQ (badop0, simplify_merge_mask (badvm, mask1, 0));
10349 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (badvm, mask1, 1));
10350 :
10351 : /* Called indirectly. */
10352 224 : ASSERT_RTX_EQ (gen_rtx_VEC_MERGE (mode, op0, op3, mask1),
10353 : simplify_rtx (nvm));
10354 224 : }
10355 :
10356 : /* Test that vector rotate formation works at RTL level. Try various
10357 : combinations of (REG << C) [|,^,+] (REG >> (<bitwidth> - C)). */
10358 :
10359 : static void
10360 92 : test_vector_rotate (rtx reg)
10361 : {
10362 92 : machine_mode mode = GET_MODE (reg);
10363 92 : unsigned bitwidth = GET_MODE_UNIT_SIZE (mode) * BITS_PER_UNIT;
10364 92 : rtx plus_rtx = gen_rtx_PLUS (mode, reg, reg);
10365 92 : rtx lshftrt_amnt = GEN_INT (bitwidth - 1);
10366 92 : lshftrt_amnt = gen_const_vec_duplicate (mode, lshftrt_amnt);
10367 92 : rtx lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
10368 92 : rtx rotate_rtx = gen_rtx_ROTATE (mode, reg, CONST1_RTX (mode));
10369 : /* Test explicitly the case where ASHIFT (x, 1) is a PLUS (x, x). */
10370 92 : ASSERT_RTX_EQ (rotate_rtx,
10371 : simplify_rtx (gen_rtx_IOR (mode, plus_rtx, lshiftrt_rtx)));
10372 92 : ASSERT_RTX_EQ (rotate_rtx,
10373 : simplify_rtx (gen_rtx_XOR (mode, plus_rtx, lshiftrt_rtx)));
10374 92 : ASSERT_RTX_EQ (rotate_rtx,
10375 : simplify_rtx (gen_rtx_PLUS (mode, plus_rtx, lshiftrt_rtx)));
10376 :
10377 : /* Don't go through every possible rotate amount to save execution time.
10378 : Multiple of BITS_PER_UNIT amounts could conceivably be simplified to
10379 : other bswap operations sometimes. Go through just the odd amounts. */
10380 1380 : for (unsigned i = 3; i < bitwidth - 2; i += 2)
10381 : {
10382 1288 : rtx rot_amnt = gen_const_vec_duplicate (mode, GEN_INT (i));
10383 1288 : rtx ashift_rtx = gen_rtx_ASHIFT (mode, reg, rot_amnt);
10384 1288 : lshftrt_amnt = gen_const_vec_duplicate (mode, GEN_INT (bitwidth - i));
10385 1288 : lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
10386 1288 : rotate_rtx = gen_rtx_ROTATE (mode, reg, rot_amnt);
10387 1288 : ASSERT_RTX_EQ (rotate_rtx,
10388 : simplify_rtx (gen_rtx_IOR (mode, ashift_rtx, lshiftrt_rtx)));
10389 1288 : ASSERT_RTX_EQ (rotate_rtx,
10390 : simplify_rtx (gen_rtx_XOR (mode, ashift_rtx, lshiftrt_rtx)));
10391 1288 : ASSERT_RTX_EQ (rotate_rtx,
10392 : simplify_rtx (gen_rtx_PLUS (mode, ashift_rtx, lshiftrt_rtx)));
10393 : }
10394 92 : }
10395 :
10396 : /* Test subregs of integer vector constant X, trying elements in
10397 : the range [ELT_BIAS, ELT_BIAS + constant_lower_bound (NELTS)),
10398 : where NELTS is the number of elements in X. Subregs involving
10399 : elements [ELT_BIAS, ELT_BIAS + FIRST_VALID) are expected to fail. */
10400 :
10401 : static void
10402 276 : test_vector_subregs_modes (rtx x, poly_uint64 elt_bias = 0,
10403 : unsigned int first_valid = 0)
10404 : {
10405 276 : machine_mode inner_mode = GET_MODE (x);
10406 276 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
10407 :
10408 34500 : for (unsigned int modei = 0; modei < NUM_MACHINE_MODES; ++modei)
10409 : {
10410 34224 : machine_mode outer_mode = (machine_mode) modei;
10411 34224 : if (!VECTOR_MODE_P (outer_mode))
10412 18768 : continue;
10413 :
10414 15456 : unsigned int outer_nunits;
10415 15456 : if (GET_MODE_INNER (outer_mode) == int_mode
10416 1932 : && GET_MODE_NUNITS (outer_mode).is_constant (&outer_nunits)
10417 20412 : && multiple_p (GET_MODE_NUNITS (inner_mode), outer_nunits))
10418 : {
10419 : /* Test subregs in which the outer mode is a smaller,
10420 : constant-sized vector of the same element type. */
10421 1092 : unsigned int limit
10422 1092 : = constant_lower_bound (GET_MODE_NUNITS (inner_mode));
10423 8028 : for (unsigned int elt = 0; elt < limit; elt += outer_nunits)
10424 : {
10425 6936 : rtx expected = NULL_RTX;
10426 6936 : if (elt >= first_valid)
10427 : {
10428 6936 : rtx_vector_builder builder (outer_mode, outer_nunits, 1);
10429 46704 : for (unsigned int i = 0; i < outer_nunits; ++i)
10430 32832 : builder.quick_push (CONST_VECTOR_ELT (x, elt + i));
10431 6936 : expected = builder.build ();
10432 6936 : }
10433 13872 : poly_uint64 byte = (elt_bias + elt) * GET_MODE_SIZE (int_mode);
10434 6936 : ASSERT_RTX_EQ (expected,
10435 : simplify_subreg (outer_mode, x,
10436 : inner_mode, byte));
10437 : }
10438 : }
10439 28728 : else if (known_eq (GET_MODE_SIZE (outer_mode),
10440 : GET_MODE_SIZE (inner_mode))
10441 2040 : && known_eq (elt_bias, 0U)
10442 2040 : && (GET_MODE_CLASS (outer_mode) != MODE_VECTOR_BOOL
10443 0 : || known_eq (GET_MODE_BITSIZE (outer_mode),
10444 : GET_MODE_NUNITS (outer_mode)))
10445 2040 : && (!FLOAT_MODE_P (outer_mode)
10446 15876 : || (FLOAT_MODE_FORMAT (outer_mode)->ieee_bits
10447 1104 : == GET_MODE_UNIT_PRECISION (outer_mode)))
10448 14364 : && (GET_MODE_SIZE (inner_mode).is_constant ()
10449 : || !CONST_VECTOR_STEPPED_P (x)))
10450 : {
10451 : /* Try converting to OUTER_MODE and back. */
10452 1800 : rtx outer_x = simplify_subreg (outer_mode, x, inner_mode, 0);
10453 1800 : ASSERT_TRUE (outer_x != NULL_RTX);
10454 1800 : ASSERT_RTX_EQ (x, simplify_subreg (inner_mode, outer_x,
10455 : outer_mode, 0));
10456 : }
10457 : }
10458 :
10459 276 : if (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
10460 : {
10461 : /* Test each byte in the element range. */
10462 276 : unsigned int limit
10463 276 : = constant_lower_bound (GET_MODE_SIZE (inner_mode));
10464 14604 : for (unsigned int i = 0; i < limit; ++i)
10465 : {
10466 14328 : unsigned int elt = i / GET_MODE_SIZE (int_mode);
10467 14328 : rtx expected = NULL_RTX;
10468 14328 : if (elt >= first_valid)
10469 : {
10470 14328 : unsigned int byte_shift = i % GET_MODE_SIZE (int_mode);
10471 14328 : if (BYTES_BIG_ENDIAN)
10472 : byte_shift = GET_MODE_SIZE (int_mode) - byte_shift - 1;
10473 14328 : rtx_mode_t vec_elt (CONST_VECTOR_ELT (x, elt), int_mode);
10474 14328 : wide_int shifted_elt
10475 14328 : = wi::lrshift (vec_elt, byte_shift * BITS_PER_UNIT);
10476 14328 : expected = immed_wide_int_const (shifted_elt, QImode);
10477 14328 : }
10478 28656 : poly_uint64 byte = elt_bias * GET_MODE_SIZE (int_mode) + i;
10479 14328 : ASSERT_RTX_EQ (expected,
10480 : simplify_subreg (QImode, x, inner_mode, byte));
10481 : }
10482 : }
10483 276 : }
10484 :
10485 : /* Test constant subregs of integer vector mode INNER_MODE, using 1
10486 : element per pattern. */
10487 :
10488 : static void
10489 92 : test_vector_subregs_repeating (machine_mode inner_mode)
10490 : {
10491 184 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
10492 92 : unsigned int min_nunits = constant_lower_bound (nunits);
10493 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
10494 92 : unsigned int count = gcd (min_nunits, 8);
10495 :
10496 92 : rtx_vector_builder builder (inner_mode, count, 1);
10497 776 : for (unsigned int i = 0; i < count; ++i)
10498 592 : builder.quick_push (gen_int_mode (8 - i, int_mode));
10499 92 : rtx x = builder.build ();
10500 :
10501 92 : test_vector_subregs_modes (x);
10502 92 : if (!nunits.is_constant ())
10503 : test_vector_subregs_modes (x, nunits - min_nunits);
10504 92 : }
10505 :
10506 : /* Test constant subregs of integer vector mode INNER_MODE, using 2
10507 : elements per pattern. */
10508 :
10509 : static void
10510 92 : test_vector_subregs_fore_back (machine_mode inner_mode)
10511 : {
10512 184 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
10513 92 : unsigned int min_nunits = constant_lower_bound (nunits);
10514 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
10515 92 : unsigned int count = gcd (min_nunits, 4);
10516 :
10517 92 : rtx_vector_builder builder (inner_mode, count, 2);
10518 536 : for (unsigned int i = 0; i < count; ++i)
10519 352 : builder.quick_push (gen_int_mode (i, int_mode));
10520 444 : for (unsigned int i = 0; i < count; ++i)
10521 352 : builder.quick_push (gen_int_mode (-1 - (int) i, int_mode));
10522 92 : rtx x = builder.build ();
10523 :
10524 92 : test_vector_subregs_modes (x);
10525 92 : if (!nunits.is_constant ())
10526 : test_vector_subregs_modes (x, nunits - min_nunits, count);
10527 92 : }
10528 :
10529 : /* Test constant subregs of integer vector mode INNER_MODE, using 3
10530 : elements per pattern. */
10531 :
10532 : static void
10533 92 : test_vector_subregs_stepped (machine_mode inner_mode)
10534 : {
10535 : /* Build { 0, 1, 2, 3, ... }. */
10536 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
10537 92 : rtx_vector_builder builder (inner_mode, 1, 3);
10538 460 : for (unsigned int i = 0; i < 3; ++i)
10539 276 : builder.quick_push (gen_int_mode (i, int_mode));
10540 92 : rtx x = builder.build ();
10541 :
10542 92 : test_vector_subregs_modes (x);
10543 92 : }
10544 :
10545 : /* Test constant subregs of integer vector mode INNER_MODE. */
10546 :
10547 : static void
10548 92 : test_vector_subregs (machine_mode inner_mode)
10549 : {
10550 92 : test_vector_subregs_repeating (inner_mode);
10551 92 : test_vector_subregs_fore_back (inner_mode);
10552 92 : test_vector_subregs_stepped (inner_mode);
10553 92 : }
10554 :
10555 : /* A subreg of a vector comparison is simplified to a comparison in the
10556 : subreg's mode. Check that the comparison is still done in the mode of
10557 : its operands, not of its integer result: with floating-point operands
10558 : that may be NaNs, x unord x and x ord x must not fold to constants. */
10559 :
10560 : static void
10561 100 : test_vector_subreg_of_fp_comparison (machine_mode mode)
10562 : {
10563 100 : machine_mode int_mode;
10564 100 : if (!HONOR_NANS (mode)
10565 100 : || !related_int_vector_mode (mode).exists (&int_mode))
10566 100 : return;
10567 :
10568 36 : rtx reg = make_test_reg (mode);
10569 36 : rtx unord = gen_rtx_UNORDERED (int_mode, reg, reg);
10570 36 : rtx ord = gen_rtx_ORDERED (int_mode, reg, reg);
10571 36 : rtx x = simplify_subreg (mode, unord, int_mode, 0);
10572 36 : ASSERT_TRUE (x == NULL_RTX || !CONSTANT_P (x));
10573 36 : x = simplify_subreg (mode, ord, int_mode, 0);
10574 36 : ASSERT_TRUE (x == NULL_RTX || !CONSTANT_P (x));
10575 : }
10576 :
10577 : /* Verify some simplifications involving vectors. */
10578 :
10579 : static void
10580 4 : test_vector_ops ()
10581 : {
10582 500 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
10583 : {
10584 496 : machine_mode mode = (machine_mode) i;
10585 496 : if (VECTOR_MODE_P (mode))
10586 : {
10587 448 : rtx scalar_reg = make_test_reg (GET_MODE_INNER (mode));
10588 224 : test_vector_ops_duplicate (mode, scalar_reg);
10589 224 : rtx vector_reg = make_test_reg (mode);
10590 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
10591 348 : && maybe_gt (GET_MODE_NUNITS (mode), 2))
10592 : {
10593 92 : test_vector_ops_series (mode, scalar_reg);
10594 92 : test_vector_subregs (mode);
10595 92 : test_vector_rotate (vector_reg);
10596 : }
10597 224 : test_vec_merge (mode);
10598 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
10599 100 : test_vector_subreg_of_fp_comparison (mode);
10600 : }
10601 : }
10602 4 : }
10603 :
10604 : template<unsigned int N>
10605 : struct simplify_const_poly_int_tests
10606 : {
10607 : static void run ();
10608 : };
10609 :
10610 : template<>
10611 : struct simplify_const_poly_int_tests<1>
10612 : {
10613 : static void run () {}
10614 : };
10615 :
10616 : /* Test various CONST_POLY_INT properties. */
10617 :
10618 : template<unsigned int N>
10619 : void
10620 : simplify_const_poly_int_tests<N>::run ()
10621 : {
10622 : using poly_int64 = poly_int<N, HOST_WIDE_INT>;
10623 : rtx x1 = gen_int_mode (poly_int64 (1, 1), QImode);
10624 : rtx x2 = gen_int_mode (poly_int64 (-80, 127), QImode);
10625 : rtx x3 = gen_int_mode (poly_int64 (-79, -128), QImode);
10626 : rtx x4 = gen_int_mode (poly_int64 (5, 4), QImode);
10627 : rtx x5 = gen_int_mode (poly_int64 (30, 24), QImode);
10628 : rtx x6 = gen_int_mode (poly_int64 (20, 16), QImode);
10629 : rtx x7 = gen_int_mode (poly_int64 (7, 4), QImode);
10630 : rtx x8 = gen_int_mode (poly_int64 (30, 24), HImode);
10631 : rtx x9 = gen_int_mode (poly_int64 (-30, -24), HImode);
10632 : rtx x10 = gen_int_mode (poly_int64 (-31, -24), HImode);
10633 : rtx two = GEN_INT (2);
10634 : rtx six = GEN_INT (6);
10635 : poly_uint64 offset = subreg_lowpart_offset (QImode, HImode);
10636 :
10637 : /* These tests only try limited operation combinations. Fuller arithmetic
10638 : testing is done directly on poly_ints. */
10639 : ASSERT_EQ (simplify_unary_operation (NEG, HImode, x8, HImode), x9);
10640 : ASSERT_EQ (simplify_unary_operation (NOT, HImode, x8, HImode), x10);
10641 : ASSERT_EQ (simplify_unary_operation (TRUNCATE, QImode, x8, HImode), x5);
10642 : ASSERT_EQ (simplify_binary_operation (PLUS, QImode, x1, x2), x3);
10643 : ASSERT_EQ (simplify_binary_operation (MINUS, QImode, x3, x1), x2);
10644 : ASSERT_EQ (simplify_binary_operation (MULT, QImode, x4, six), x5);
10645 : ASSERT_EQ (simplify_binary_operation (MULT, QImode, six, x4), x5);
10646 : ASSERT_EQ (simplify_binary_operation (ASHIFT, QImode, x4, two), x6);
10647 : ASSERT_EQ (simplify_binary_operation (IOR, QImode, x4, two), x7);
10648 : ASSERT_EQ (simplify_subreg (HImode, x5, QImode, 0), x8);
10649 : ASSERT_EQ (simplify_subreg (QImode, x8, HImode, offset), x5);
10650 : }
10651 :
10652 : /* Run all of the selftests within this file. */
10653 :
10654 : void
10655 4 : simplify_rtx_cc_tests ()
10656 : {
10657 4 : test_scalar_ops ();
10658 4 : test_vector_ops ();
10659 4 : simplify_const_poly_int_tests<NUM_POLY_INT_COEFFS>::run ();
10660 4 : }
10661 :
10662 : } // namespace selftest
10663 :
10664 : #endif /* CHECKING_P */
|