Branch data Line data Source code
1 : : /* RTL simplification functions for GNU compiler.
2 : : Copyright (C) 1987-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "backend.h"
25 : : #include "target.h"
26 : : #include "rtl.h"
27 : : #include "tree.h"
28 : : #include "predict.h"
29 : : #include "memmodel.h"
30 : : #include "optabs.h"
31 : : #include "emit-rtl.h"
32 : : #include "recog.h"
33 : : #include "diagnostic-core.h"
34 : : #include "varasm.h"
35 : : #include "flags.h"
36 : : #include "selftest.h"
37 : : #include "selftest-rtl.h"
38 : : #include "rtx-vector-builder.h"
39 : : #include "rtlanal.h"
40 : :
41 : : /* Simplification and canonicalization of RTL. */
42 : :
43 : : /* Much code operates on (low, high) pairs; the low value is an
44 : : unsigned wide int, the high value a signed wide int. We
45 : : occasionally need to sign extend from low to high as if low were a
46 : : signed wide int. */
47 : : #define HWI_SIGN_EXTEND(low) \
48 : : ((((HOST_WIDE_INT) low) < 0) ? HOST_WIDE_INT_M1 : HOST_WIDE_INT_0)
49 : :
50 : : static bool plus_minus_operand_p (const_rtx);
51 : :
52 : : /* Negate I, which satisfies poly_int_rtx_p. MODE is the mode of I. */
53 : :
54 : : static rtx
55 : 8937090 : neg_poly_int_rtx (machine_mode mode, const_rtx i)
56 : : {
57 : 8937090 : 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 : 6127447 : mode_signbit_p (machine_mode mode, const_rtx x)
65 : : {
66 : 6127447 : unsigned HOST_WIDE_INT val;
67 : 6127447 : unsigned int width;
68 : 6127447 : scalar_int_mode int_mode;
69 : :
70 : 6127447 : if (!is_int_mode (mode, &int_mode))
71 : : return false;
72 : :
73 : 6127439 : width = GET_MODE_PRECISION (int_mode);
74 : 6127439 : if (width == 0)
75 : : return false;
76 : :
77 : 6127439 : if (width <= HOST_BITS_PER_WIDE_INT
78 : 6125921 : && CONST_INT_P (x))
79 : 6009297 : val = INTVAL (x);
80 : : #if TARGET_SUPPORTS_WIDE_INT
81 : 118142 : else if (CONST_WIDE_INT_P (x))
82 : : {
83 : 474 : unsigned int i;
84 : 474 : unsigned int elts = CONST_WIDE_INT_NUNITS (x);
85 : 474 : if (elts != (width + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
86 : : return false;
87 : 888 : for (i = 0; i < elts - 1; i++)
88 : 474 : if (CONST_WIDE_INT_ELT (x, i) != 0)
89 : : return false;
90 : 414 : val = CONST_WIDE_INT_ELT (x, elts - 1);
91 : 414 : width %= HOST_BITS_PER_WIDE_INT;
92 : 414 : 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 : 6009297 : if (width < HOST_BITS_PER_WIDE_INT)
109 : 5421681 : val &= (HOST_WIDE_INT_1U << width) - 1;
110 : 6009711 : 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 : 3672693 : val_signbit_p (machine_mode mode, unsigned HOST_WIDE_INT val)
119 : : {
120 : 3672693 : unsigned int width;
121 : 3672693 : scalar_int_mode int_mode;
122 : :
123 : 3672693 : if (!is_int_mode (mode, &int_mode))
124 : : return false;
125 : :
126 : 3672657 : width = GET_MODE_PRECISION (int_mode);
127 : 3672657 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
128 : : return false;
129 : :
130 : 3668067 : val &= GET_MODE_MASK (int_mode);
131 : 3668067 : 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 : 2727622 : val_signbit_known_set_p (machine_mode mode, unsigned HOST_WIDE_INT val)
138 : : {
139 : 2727622 : unsigned int width;
140 : :
141 : 2727622 : scalar_int_mode int_mode;
142 : 2727622 : if (!is_int_mode (mode, &int_mode))
143 : : return false;
144 : :
145 : 2696331 : width = GET_MODE_PRECISION (int_mode);
146 : 2696331 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
147 : : return false;
148 : :
149 : 2696331 : val &= HOST_WIDE_INT_1U << (width - 1);
150 : 2696331 : 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 : 7908712 : val_signbit_known_clear_p (machine_mode mode, unsigned HOST_WIDE_INT val)
157 : : {
158 : 7908712 : unsigned int width;
159 : :
160 : 7908712 : scalar_int_mode int_mode;
161 : 7908712 : if (!is_int_mode (mode, &int_mode))
162 : : return false;
163 : :
164 : 7595020 : width = GET_MODE_PRECISION (int_mode);
165 : 7595020 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
166 : : return false;
167 : :
168 : 7484722 : val &= HOST_WIDE_INT_1U << (width - 1);
169 : 7484722 : 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 : 115715343 : simplify_context::simplify_gen_binary (rtx_code code, machine_mode mode,
177 : : rtx op0, rtx op1)
178 : : {
179 : 115715343 : rtx tem;
180 : :
181 : : /* If this simplifies, do it. */
182 : 115715343 : tem = simplify_binary_operation (code, mode, op0, op1);
183 : 115715343 : if (tem)
184 : : return tem;
185 : :
186 : : /* Put complex operands first and constants second if commutative. */
187 : 73959572 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
188 : 73959572 : && swap_commutative_operands_p (op0, op1))
189 : : std::swap (op0, op1);
190 : :
191 : 73959572 : 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 : 2671805101 : avoid_constant_pool_reference (rtx x)
198 : : {
199 : 2671805101 : rtx c, tmp, addr;
200 : 2671805101 : machine_mode cmode;
201 : 2671805101 : poly_int64 offset = 0;
202 : :
203 : 2671805101 : switch (GET_CODE (x))
204 : : {
205 : 245234219 : case MEM:
206 : 245234219 : break;
207 : :
208 : 909768 : case FLOAT_EXTEND:
209 : : /* Handle float extensions of constant pool references. */
210 : 909768 : tmp = XEXP (x, 0);
211 : 909768 : c = avoid_constant_pool_reference (tmp);
212 : 909768 : if (c != tmp && CONST_DOUBLE_AS_FLOAT_P (c))
213 : 121784 : return const_double_from_real_value (*CONST_DOUBLE_REAL_VALUE (c),
214 : 121784 : GET_MODE (x));
215 : : return x;
216 : :
217 : : default:
218 : : return x;
219 : : }
220 : :
221 : 245234219 : if (GET_MODE (x) == BLKmode)
222 : : return x;
223 : :
224 : 242936938 : addr = XEXP (x, 0);
225 : :
226 : : /* Call target hook to avoid the effects of -fpic etc.... */
227 : 242936938 : addr = targetm.delegitimize_address (addr);
228 : :
229 : : /* Split the address into a base and integer offset. */
230 : 242936938 : addr = strip_offset (addr, &offset);
231 : :
232 : 242936938 : 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 : 242936938 : if (GET_CODE (addr) == SYMBOL_REF
238 : 242936938 : && CONSTANT_POOL_ADDRESS_P (addr))
239 : : {
240 : 5357430 : c = get_pool_constant (addr);
241 : 5357430 : 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 : 5357430 : if (known_eq (offset, 0) && cmode == GET_MODE (x))
247 : : return c;
248 : 15418 : else if (known_in_range_p (offset, 0, GET_MODE_SIZE (cmode)))
249 : : {
250 : 7709 : rtx tem = simplify_subreg (GET_MODE (x), c, cmode, offset);
251 : 7709 : if (tem && CONSTANT_P (tem))
252 : : return tem;
253 : : }
254 : : }
255 : :
256 : : return x;
257 : : }
258 : :
259 : : /* Simplify a MEM based on its attributes. This is the default
260 : : delegitimize_address target hook, and it's recommended that every
261 : : overrider call it. */
262 : :
263 : : rtx
264 : 3511129660 : 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 : 3511129660 : if (MEM_P (x)
269 : 60221341 : && MEM_EXPR (x)
270 : 3548968878 : && MEM_OFFSET_KNOWN_P (x))
271 : : {
272 : 34940736 : tree decl = MEM_EXPR (x);
273 : 34940736 : machine_mode mode = GET_MODE (x);
274 : 34940736 : poly_int64 offset = 0;
275 : :
276 : 34940736 : switch (TREE_CODE (decl))
277 : : {
278 : : default:
279 : : decl = NULL;
280 : : break;
281 : :
282 : : case VAR_DECL:
283 : : break;
284 : :
285 : 10004351 : case ARRAY_REF:
286 : 10004351 : case ARRAY_RANGE_REF:
287 : 10004351 : case COMPONENT_REF:
288 : 10004351 : case BIT_FIELD_REF:
289 : 10004351 : case REALPART_EXPR:
290 : 10004351 : case IMAGPART_EXPR:
291 : 10004351 : case VIEW_CONVERT_EXPR:
292 : 10004351 : {
293 : 10004351 : poly_int64 bitsize, bitpos, bytepos, toffset_val = 0;
294 : 10004351 : tree toffset;
295 : 10004351 : int unsignedp, reversep, volatilep = 0;
296 : :
297 : 10004351 : decl
298 : 10004351 : = get_inner_reference (decl, &bitsize, &bitpos, &toffset, &mode,
299 : : &unsignedp, &reversep, &volatilep);
300 : 20008702 : if (maybe_ne (bitsize, GET_MODE_BITSIZE (mode))
301 : 10278730 : || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
302 : 19505523 : || (toffset && !poly_int_tree_p (toffset, &toffset_val)))
303 : : decl = NULL;
304 : : else
305 : 9226793 : offset += bytepos + toffset_val;
306 : 10004351 : break;
307 : : }
308 : : }
309 : :
310 : 777558 : if (decl
311 : 20520519 : && mode == GET_MODE (x)
312 : 20265239 : && VAR_P (decl)
313 : 13139051 : && (TREE_STATIC (decl)
314 : 11932303 : || DECL_THREAD_LOCAL_P (decl))
315 : 1240890 : && DECL_RTL_SET_P (decl)
316 : 10467180 : && MEM_P (DECL_RTL (decl)))
317 : : {
318 : 1240387 : rtx newx;
319 : :
320 : 1240387 : offset += MEM_OFFSET (x);
321 : :
322 : 1240387 : newx = DECL_RTL (decl);
323 : :
324 : 1240387 : if (MEM_P (newx))
325 : : {
326 : 1240387 : rtx n = XEXP (newx, 0), o = XEXP (x, 0);
327 : 1240387 : 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 : 1240387 : n = strip_offset (n, &n_offset);
336 : 1240387 : o = strip_offset (o, &o_offset);
337 : 2453700 : if (!(known_eq (o_offset, n_offset + offset)
338 : 1213313 : && rtx_equal_p (o, n)))
339 : 206430 : 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 : 3511129660 : 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 : 5749260 : simplify_context::simplify_gen_unary (rtx_code code, machine_mode mode, rtx op,
355 : : machine_mode op_mode)
356 : : {
357 : 5749260 : rtx tem;
358 : :
359 : : /* If this simplifies, use it. */
360 : 5749260 : if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
361 : : return tem;
362 : :
363 : 2054461 : return gen_rtx_fmt_e (code, mode, op);
364 : : }
365 : :
366 : : /* Likewise for ternary operations. */
367 : :
368 : : rtx
369 : 2103637 : 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 : 2103637 : rtx tem;
374 : :
375 : : /* If this simplifies, use it. */
376 : 2103637 : if ((tem = simplify_ternary_operation (code, mode, op0_mode,
377 : : op0, op1, op2)) != 0)
378 : : return tem;
379 : :
380 : 1880401 : 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 : 20720065 : simplify_context::simplify_gen_relational (rtx_code code, machine_mode mode,
388 : : machine_mode cmp_mode,
389 : : rtx op0, rtx op1)
390 : : {
391 : 20720065 : rtx tem;
392 : :
393 : 20720065 : if ((tem = simplify_relational_operation (code, mode, cmp_mode,
394 : : op0, op1)) != 0)
395 : : return tem;
396 : :
397 : 18547747 : 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 : 473699835 : simplify_replace_fn_rtx (rtx x, const_rtx old_rtx,
407 : : rtx (*fn) (rtx, const_rtx, void *), void *data)
408 : : {
409 : 473699835 : enum rtx_code code = GET_CODE (x);
410 : 473699835 : machine_mode mode = GET_MODE (x);
411 : 473699835 : machine_mode op_mode;
412 : 473699835 : const char *fmt;
413 : 473699835 : rtx op0, op1, op2, newx, op;
414 : 473699835 : rtvec vec, newvec;
415 : 473699835 : int i, j;
416 : :
417 : 473699835 : if (UNLIKELY (fn != NULL))
418 : : {
419 : 412870582 : newx = fn (x, old_rtx, data);
420 : 412870582 : if (newx)
421 : : return newx;
422 : : }
423 : 60829253 : else if (rtx_equal_p (x, old_rtx))
424 : 5134220 : return copy_rtx ((rtx) data);
425 : :
426 : 370402428 : switch (GET_RTX_CLASS (code))
427 : : {
428 : 2142115 : case RTX_UNARY:
429 : 2142115 : op0 = XEXP (x, 0);
430 : 2142115 : op_mode = GET_MODE (op0);
431 : 2142115 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
432 : 2142115 : if (op0 == XEXP (x, 0))
433 : : return x;
434 : 646546 : return simplify_gen_unary (code, mode, op0, op_mode);
435 : :
436 : 74228094 : case RTX_BIN_ARITH:
437 : 74228094 : case RTX_COMM_ARITH:
438 : 74228094 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
439 : 74228094 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
440 : 74228094 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
441 : : return x;
442 : 23141704 : return simplify_gen_binary (code, mode, op0, op1);
443 : :
444 : 9514102 : case RTX_COMPARE:
445 : 9514102 : case RTX_COMM_COMPARE:
446 : 9514102 : op0 = XEXP (x, 0);
447 : 9514102 : op1 = XEXP (x, 1);
448 : 9514102 : op_mode = GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
449 : 9514102 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
450 : 9514102 : op1 = simplify_replace_fn_rtx (op1, old_rtx, fn, data);
451 : 9514102 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
452 : : return x;
453 : 2287733 : return simplify_gen_relational (code, mode, op_mode, op0, op1);
454 : :
455 : 5621128 : case RTX_TERNARY:
456 : 5621128 : case RTX_BITFIELD_OPS:
457 : 5621128 : op0 = XEXP (x, 0);
458 : 5621128 : op_mode = GET_MODE (op0);
459 : 5621128 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
460 : 5621128 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
461 : 5621128 : op2 = simplify_replace_fn_rtx (XEXP (x, 2), old_rtx, fn, data);
462 : 5621128 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1) && op2 == XEXP (x, 2))
463 : : return x;
464 : 1596553 : if (op_mode == VOIDmode)
465 : 1576606 : op_mode = GET_MODE (op0);
466 : 1596553 : return simplify_gen_ternary (code, mode, op_mode, op0, op1, op2);
467 : :
468 : 79851737 : case RTX_EXTRA:
469 : 79851737 : if (code == SUBREG)
470 : : {
471 : 710263 : op0 = simplify_replace_fn_rtx (SUBREG_REG (x), old_rtx, fn, data);
472 : 710263 : if (op0 == SUBREG_REG (x))
473 : : return x;
474 : 100994 : op0 = simplify_gen_subreg (GET_MODE (x), op0,
475 : 50497 : GET_MODE (SUBREG_REG (x)),
476 : 50497 : SUBREG_BYTE (x));
477 : 50497 : return op0 ? op0 : x;
478 : : }
479 : : break;
480 : :
481 : 59543414 : case RTX_OBJ:
482 : 59543414 : if (code == MEM)
483 : : {
484 : 10510133 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
485 : 10510133 : if (op0 == XEXP (x, 0))
486 : : return x;
487 : 168755 : return replace_equiv_address_nv (x, op0);
488 : : }
489 : 49033281 : 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 : 267676593 : newx = x;
515 : 267676593 : fmt = GET_RTX_FORMAT (code);
516 : 577177709 : for (i = 0; fmt[i]; i++)
517 : 309501116 : switch (fmt[i])
518 : : {
519 : 2967159 : case 'E':
520 : 2967159 : vec = XVEC (x, i);
521 : 2967159 : newvec = XVEC (newx, i);
522 : 12025622 : for (j = 0; j < GET_NUM_ELEM (vec); j++)
523 : : {
524 : 9058463 : op = simplify_replace_fn_rtx (RTVEC_ELT (vec, j),
525 : : old_rtx, fn, data);
526 : 9058463 : if (op != RTVEC_ELT (vec, j))
527 : : {
528 : 350686 : if (newvec == vec)
529 : : {
530 : 334617 : newvec = shallow_copy_rtvec (vec);
531 : 334617 : if (x == newx)
532 : 334617 : newx = shallow_copy_rtx (x);
533 : 334617 : XVEC (newx, i) = newvec;
534 : : }
535 : 350686 : RTVEC_ELT (newvec, j) = op;
536 : : }
537 : : }
538 : : break;
539 : :
540 : 71376188 : case 'e':
541 : 71376188 : if (XEXP (x, i))
542 : : {
543 : 71376188 : op = simplify_replace_fn_rtx (XEXP (x, i), old_rtx, fn, data);
544 : 71376188 : if (op != XEXP (x, i))
545 : : {
546 : 4181214 : if (x == newx)
547 : 4177715 : newx = shallow_copy_rtx (x);
548 : 4181214 : 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 : 12535614 : simplify_replace_rtx (rtx x, const_rtx old_rtx, rtx new_rtx)
561 : : {
562 : 12535614 : 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 : 20915612 : simplify_context::simplify_truncation (machine_mode mode, rtx op,
614 : : machine_mode op_mode)
615 : : {
616 : 20915612 : unsigned int precision = GET_MODE_UNIT_PRECISION (mode);
617 : 20915612 : unsigned int op_precision = GET_MODE_UNIT_PRECISION (op_mode);
618 : 20915612 : scalar_int_mode int_mode, int_op_mode, subreg_mode;
619 : :
620 : 20915612 : gcc_assert (precision <= op_precision);
621 : :
622 : : /* Optimize truncations of zero and sign extended values. */
623 : 20915612 : if (GET_CODE (op) == ZERO_EXTEND
624 : 20915612 : || 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 : 313177 : machine_mode origmode = GET_MODE (XEXP (op, 0));
633 : 313177 : if (mode == origmode)
634 : : return XEXP (op, 0);
635 : 31500 : else if (precision <= GET_MODE_UNIT_PRECISION (origmode))
636 : 7330 : return simplify_gen_unary (TRUNCATE, mode,
637 : 7330 : XEXP (op, 0), origmode);
638 : : else
639 : 8420 : return simplify_gen_unary (GET_CODE (op), mode,
640 : 8420 : 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 : 20602435 : if (1
647 : : && (!WORD_REGISTER_OPERATIONS || precision >= BITS_PER_WORD)
648 : : && (GET_CODE (op) == PLUS
649 : : || GET_CODE (op) == MINUS
650 : 20602435 : || GET_CODE (op) == MULT))
651 : : {
652 : 1013887 : rtx op0 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0), op_mode);
653 : 1013887 : if (op0)
654 : : {
655 : 1013887 : rtx op1 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 1), op_mode);
656 : 1013887 : if (op1)
657 : 1013887 : 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 : 19588548 : if ((GET_CODE (op) == LSHIFTRT
665 : 19588548 : || 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 : 2038925 : && 2 * precision <= op_precision
671 : 2038925 : && CONST_INT_P (XEXP (op, 1))
672 : 1941547 : && GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
673 : 23 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
674 : 20 : && UINTVAL (XEXP (op, 1)) < precision)
675 : 16 : return simplify_gen_binary (ASHIFTRT, mode,
676 : 16 : 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 : 19588532 : if ((GET_CODE (op) == LSHIFTRT
682 : : || GET_CODE (op) == ASHIFTRT)
683 : 2038909 : && CONST_INT_P (XEXP (op, 1))
684 : 1941531 : && GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
685 : 648 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
686 : 648 : && UINTVAL (XEXP (op, 1)) < precision)
687 : 638 : return simplify_gen_binary (LSHIFTRT, mode,
688 : 638 : 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 : 19587894 : if (GET_CODE (op) == ASHIFT
694 : 719226 : && CONST_INT_P (XEXP (op, 1))
695 : 665938 : && (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
696 : 665938 : || GET_CODE (XEXP (op, 0)) == SIGN_EXTEND)
697 : 510 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
698 : 493 : && UINTVAL (XEXP (op, 1)) < precision)
699 : 486 : return simplify_gen_binary (ASHIFT, mode,
700 : 486 : 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 : 19587408 : if (GET_CODE (op) == AND
706 : 631325 : && (GET_CODE (XEXP (op, 0)) == LSHIFTRT
707 : 631325 : || GET_CODE (XEXP (op, 0)) == ASHIFTRT)
708 : 48264 : && CONST_INT_P (XEXP (XEXP (op, 0), 1))
709 : 48174 : && CONST_INT_P (XEXP (op, 1)))
710 : : {
711 : 48174 : rtx op0 = (XEXP (XEXP (op, 0), 0));
712 : 48174 : rtx shift_op = XEXP (XEXP (op, 0), 1);
713 : 48174 : rtx mask_op = XEXP (op, 1);
714 : 48174 : unsigned HOST_WIDE_INT shift = UINTVAL (shift_op);
715 : 48174 : unsigned HOST_WIDE_INT mask = UINTVAL (mask_op);
716 : :
717 : 48174 : if (shift < precision
718 : : /* If doing this transform works for an X with all bits set,
719 : : it works for any X. */
720 : 34129 : && ((GET_MODE_MASK (mode) >> shift) & mask)
721 : 34129 : == ((GET_MODE_MASK (op_mode) >> shift) & mask)
722 : 2981 : && (op0 = simplify_gen_unary (TRUNCATE, mode, op0, op_mode))
723 : 51155 : && (op0 = simplify_gen_binary (LSHIFTRT, mode, op0, shift_op)))
724 : : {
725 : 2981 : mask_op = GEN_INT (trunc_int_for_mode (mask, mode));
726 : 2981 : return simplify_gen_binary (AND, mode, op0, mask_op);
727 : : }
728 : : }
729 : :
730 : : /* Turn (truncate:M1 (*_extract:M2 (reg:M2) (len) (pos))) into
731 : : (*_extract:M1 (truncate:M1 (reg:M2)) (len) (pos')) if possible without
732 : : changing len. */
733 : 19584427 : if ((GET_CODE (op) == ZERO_EXTRACT || GET_CODE (op) == SIGN_EXTRACT)
734 : 505996 : && REG_P (XEXP (op, 0))
735 : 345943 : && GET_MODE (XEXP (op, 0)) == GET_MODE (op)
736 : 344358 : && CONST_INT_P (XEXP (op, 1))
737 : 344358 : && CONST_INT_P (XEXP (op, 2)))
738 : : {
739 : 307572 : rtx op0 = XEXP (op, 0);
740 : 307572 : unsigned HOST_WIDE_INT len = UINTVAL (XEXP (op, 1));
741 : 307572 : unsigned HOST_WIDE_INT pos = UINTVAL (XEXP (op, 2));
742 : 307572 : if (BITS_BIG_ENDIAN && pos >= op_precision - precision)
743 : : {
744 : : op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
745 : : if (op0)
746 : : {
747 : : pos -= op_precision - precision;
748 : : return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
749 : : XEXP (op, 1), GEN_INT (pos));
750 : : }
751 : : }
752 : 307572 : else if (!BITS_BIG_ENDIAN && precision >= len + pos)
753 : : {
754 : 7127 : op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
755 : 7127 : if (op0)
756 : 7127 : return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
757 : 7127 : XEXP (op, 1), XEXP (op, 2));
758 : : }
759 : : }
760 : :
761 : : /* Recognize a word extraction from a multi-word subreg. */
762 : 19577300 : if ((GET_CODE (op) == LSHIFTRT
763 : 19577300 : || GET_CODE (op) == ASHIFTRT)
764 : 2038271 : && SCALAR_INT_MODE_P (mode)
765 : 2035329 : && SCALAR_INT_MODE_P (op_mode)
766 : 2167108 : && precision >= BITS_PER_WORD
767 : 62588 : && 2 * precision <= op_precision
768 : 62588 : && CONST_INT_P (XEXP (op, 1))
769 : 54958 : && (INTVAL (XEXP (op, 1)) & (precision - 1)) == 0
770 : 1901 : && UINTVAL (XEXP (op, 1)) < op_precision)
771 : : {
772 : 1901 : poly_int64 byte = subreg_lowpart_offset (mode, op_mode);
773 : 1901 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
774 : 1901 : return simplify_gen_subreg (mode, XEXP (op, 0), op_mode,
775 : : (WORDS_BIG_ENDIAN
776 : 1901 : ? byte - shifted_bytes
777 : 1901 : : byte + shifted_bytes));
778 : : }
779 : :
780 : : /* If we have a TRUNCATE of a right shift of MEM, make a new MEM
781 : : and try replacing the TRUNCATE and shift with it. Don't do this
782 : : if the MEM has a mode-dependent address. */
783 : 19575399 : if ((GET_CODE (op) == LSHIFTRT
784 : : || GET_CODE (op) == ASHIFTRT)
785 : 2033428 : && is_a <scalar_int_mode> (mode, &int_mode)
786 : 21607806 : && is_a <scalar_int_mode> (op_mode, &int_op_mode)
787 : 2033428 : && MEM_P (XEXP (op, 0))
788 : 12029 : && CONST_INT_P (XEXP (op, 1))
789 : 22418 : && INTVAL (XEXP (op, 1)) % GET_MODE_BITSIZE (int_mode) == 0
790 : 1060 : && INTVAL (XEXP (op, 1)) > 0
791 : 2120 : && INTVAL (XEXP (op, 1)) < GET_MODE_BITSIZE (int_op_mode)
792 : 1060 : && ! mode_dependent_address_p (XEXP (XEXP (op, 0), 0),
793 : 1060 : MEM_ADDR_SPACE (XEXP (op, 0)))
794 : 1060 : && ! MEM_VOLATILE_P (XEXP (op, 0))
795 : 19575399 : && (GET_MODE_SIZE (int_mode) >= UNITS_PER_WORD
796 : : || WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN))
797 : : {
798 : 1021 : poly_int64 byte = subreg_lowpart_offset (int_mode, int_op_mode);
799 : 1021 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
800 : 1021 : return adjust_address_nv (XEXP (op, 0), int_mode,
801 : : (WORDS_BIG_ENDIAN
802 : : ? byte - shifted_bytes
803 : : : byte + shifted_bytes));
804 : : }
805 : :
806 : : /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
807 : : (OP:SI foo:SI) if OP is NEG or ABS. */
808 : 19574378 : if ((GET_CODE (op) == ABS
809 : 19574378 : || GET_CODE (op) == NEG)
810 : 23634 : && (GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
811 : 23634 : || GET_CODE (XEXP (op, 0)) == ZERO_EXTEND)
812 : 18 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
813 : 2 : return simplify_gen_unary (GET_CODE (op), mode,
814 : 2 : XEXP (XEXP (op, 0), 0), mode);
815 : :
816 : : /* Simplifications of (truncate:A (subreg:B X 0)). */
817 : 19574376 : if (GET_CODE (op) == SUBREG
818 : 19574475 : && is_a <scalar_int_mode> (mode, &int_mode)
819 : 50148 : && SCALAR_INT_MODE_P (op_mode)
820 : 50148 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &subreg_mode)
821 : 19624462 : && subreg_lowpart_p (op))
822 : : {
823 : : /* (truncate:A (subreg:B (truncate:C X) 0)) is (truncate:A X). */
824 : 50082 : if (GET_CODE (SUBREG_REG (op)) == TRUNCATE)
825 : : {
826 : 0 : rtx inner = XEXP (SUBREG_REG (op), 0);
827 : 0 : if (GET_MODE_PRECISION (int_mode)
828 : 0 : <= GET_MODE_PRECISION (subreg_mode))
829 : 0 : return simplify_gen_unary (TRUNCATE, int_mode, inner,
830 : 0 : GET_MODE (inner));
831 : : else
832 : : /* If subreg above is paradoxical and C is narrower
833 : : than A, return (subreg:A (truncate:C X) 0). */
834 : 0 : return simplify_gen_subreg (int_mode, SUBREG_REG (op),
835 : : subreg_mode, 0);
836 : : }
837 : :
838 : : /* Simplifications of (truncate:A (subreg:B X:C 0)) with
839 : : paradoxical subregs (B is wider than C). */
840 : 50082 : if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
841 : : {
842 : 50082 : unsigned int int_op_prec = GET_MODE_PRECISION (int_op_mode);
843 : 50082 : unsigned int subreg_prec = GET_MODE_PRECISION (subreg_mode);
844 : 50082 : if (int_op_prec > subreg_prec)
845 : : {
846 : 1309 : if (int_mode == subreg_mode)
847 : : return SUBREG_REG (op);
848 : 60 : if (GET_MODE_PRECISION (int_mode) < subreg_prec)
849 : 27 : return simplify_gen_unary (TRUNCATE, int_mode,
850 : 27 : SUBREG_REG (op), subreg_mode);
851 : : }
852 : : /* Simplification of (truncate:A (subreg:B X:C 0)) where
853 : : A is narrower than B and B is narrower than C. */
854 : 48773 : else if (int_op_prec < subreg_prec
855 : 48773 : && GET_MODE_PRECISION (int_mode) < int_op_prec)
856 : 48773 : return simplify_gen_unary (TRUNCATE, int_mode,
857 : 48773 : SUBREG_REG (op), subreg_mode);
858 : : }
859 : : }
860 : :
861 : : /* (truncate:A (truncate:B X)) is (truncate:A X). */
862 : 19524327 : if (GET_CODE (op) == TRUNCATE)
863 : 0 : return simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0),
864 : 0 : GET_MODE (XEXP (op, 0)));
865 : :
866 : : /* (truncate:A (ior X C)) is (const_int -1) if C is equal to that already,
867 : : in mode A. */
868 : 19524327 : if (GET_CODE (op) == IOR
869 : 99957 : && SCALAR_INT_MODE_P (mode)
870 : 99957 : && SCALAR_INT_MODE_P (op_mode)
871 : 99957 : && CONST_INT_P (XEXP (op, 1))
872 : 19533003 : && trunc_int_for_mode (INTVAL (XEXP (op, 1)), mode) == -1)
873 : 18 : return constm1_rtx;
874 : :
875 : : return NULL_RTX;
876 : : }
877 : :
878 : : /* Try to simplify a unary operation CODE whose output mode is to be
879 : : MODE with input operand OP whose mode was originally OP_MODE.
880 : : Return zero if no simplification can be made. */
881 : : rtx
882 : 28243839 : simplify_context::simplify_unary_operation (rtx_code code, machine_mode mode,
883 : : rtx op, machine_mode op_mode)
884 : : {
885 : 28243839 : rtx trueop, tem;
886 : :
887 : 28243839 : trueop = avoid_constant_pool_reference (op);
888 : :
889 : 28243839 : tem = simplify_const_unary_operation (code, mode, trueop, op_mode);
890 : 28243839 : if (tem)
891 : : return tem;
892 : :
893 : 23183277 : return simplify_unary_operation_1 (code, mode, op);
894 : : }
895 : :
896 : : /* Return true if FLOAT or UNSIGNED_FLOAT operation OP is known
897 : : to be exact. */
898 : :
899 : : static bool
900 : 2724 : exact_int_to_float_conversion_p (const_rtx op)
901 : : {
902 : 2724 : machine_mode op0_mode = GET_MODE (XEXP (op, 0));
903 : : /* Constants can reach here with -frounding-math, if they do then
904 : : the conversion isn't exact. */
905 : 2724 : if (op0_mode == VOIDmode)
906 : : return false;
907 : 5446 : int out_bits = significand_size (GET_MODE_INNER (GET_MODE (op)));
908 : 2723 : int in_prec = GET_MODE_UNIT_PRECISION (op0_mode);
909 : 2723 : int in_bits = in_prec;
910 : 2723 : if (HWI_COMPUTABLE_MODE_P (op0_mode))
911 : : {
912 : 2633 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (XEXP (op, 0), op0_mode);
913 : 2633 : if (GET_CODE (op) == FLOAT)
914 : 2499 : in_bits -= num_sign_bit_copies (XEXP (op, 0), op0_mode);
915 : 134 : else if (GET_CODE (op) == UNSIGNED_FLOAT)
916 : 134 : in_bits = wi::min_precision (wi::uhwi (nonzero, in_prec), UNSIGNED);
917 : : else
918 : 0 : gcc_unreachable ();
919 : 2633 : in_bits -= wi::ctz (wi::uhwi (nonzero, in_prec));
920 : : }
921 : 2723 : return in_bits <= out_bits;
922 : : }
923 : :
924 : : /* Perform some simplifications we can do even if the operands
925 : : aren't constant. */
926 : : rtx
927 : 23183277 : simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode,
928 : : rtx op)
929 : : {
930 : 23183277 : enum rtx_code reversed;
931 : 23183277 : rtx temp, elt, base, step;
932 : 23183277 : scalar_int_mode inner, int_mode, op_mode, op0_mode;
933 : :
934 : 23183277 : switch (code)
935 : : {
936 : 1769891 : case NOT:
937 : : /* (not (not X)) == X. */
938 : 1769891 : if (GET_CODE (op) == NOT)
939 : 2548 : return XEXP (op, 0);
940 : :
941 : : /* (not (eq X Y)) == (ne X Y), etc. if BImode or the result of the
942 : : comparison is all ones. */
943 : 1767343 : if (COMPARISON_P (op)
944 : 10852 : && (mode == BImode || STORE_FLAG_VALUE == -1)
945 : 1767343 : && ((reversed = reversed_comparison_code (op, NULL)) != UNKNOWN))
946 : 0 : return simplify_gen_relational (reversed, mode, VOIDmode,
947 : 0 : XEXP (op, 0), XEXP (op, 1));
948 : :
949 : : /* (not (plus X -1)) can become (neg X). */
950 : 1767343 : if (GET_CODE (op) == PLUS
951 : 289295 : && XEXP (op, 1) == constm1_rtx)
952 : 6944 : return simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
953 : :
954 : : /* Similarly, (not (neg X)) is (plus X -1). Only do this for
955 : : modes that have CONSTM1_RTX, i.e. MODE_INT, MODE_PARTIAL_INT
956 : : and MODE_VECTOR_INT. */
957 : 1760399 : if (GET_CODE (op) == NEG && CONSTM1_RTX (mode))
958 : 68879 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
959 : 68879 : CONSTM1_RTX (mode));
960 : :
961 : : /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
962 : 1691520 : if (GET_CODE (op) == XOR
963 : 13522 : && CONST_INT_P (XEXP (op, 1))
964 : 1694932 : && (temp = simplify_unary_operation (NOT, mode,
965 : : XEXP (op, 1), mode)) != 0)
966 : 3412 : return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
967 : :
968 : : /* (not (plus X C)) for signbit C is (xor X D) with D = ~C. */
969 : 1688108 : if (GET_CODE (op) == PLUS
970 : 282351 : && CONST_INT_P (XEXP (op, 1))
971 : 165195 : && mode_signbit_p (mode, XEXP (op, 1))
972 : 1693312 : && (temp = simplify_unary_operation (NOT, mode,
973 : : XEXP (op, 1), mode)) != 0)
974 : 5204 : return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
975 : :
976 : :
977 : : /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for
978 : : operands other than 1, but that is not valid. We could do a
979 : : similar simplification for (not (lshiftrt C X)) where C is
980 : : just the sign bit, but this doesn't seem common enough to
981 : : bother with. */
982 : 1682904 : if (GET_CODE (op) == ASHIFT
983 : 45673 : && XEXP (op, 0) == const1_rtx)
984 : : {
985 : 992 : temp = simplify_gen_unary (NOT, mode, const1_rtx, mode);
986 : 992 : return simplify_gen_binary (ROTATE, mode, temp, XEXP (op, 1));
987 : : }
988 : :
989 : : /* (not (ashiftrt foo C)) where C is the number of bits in FOO
990 : : minus 1 is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1,
991 : : so we can perform the above simplification. */
992 : 1681912 : if (STORE_FLAG_VALUE == -1
993 : : && is_a <scalar_int_mode> (mode, &int_mode)
994 : : && GET_CODE (op) == ASHIFTRT
995 : : && CONST_INT_P (XEXP (op, 1))
996 : : && INTVAL (XEXP (op, 1)) == GET_MODE_PRECISION (int_mode) - 1)
997 : : return simplify_gen_relational (GE, int_mode, VOIDmode,
998 : : XEXP (op, 0), const0_rtx);
999 : :
1000 : :
1001 : 1681912 : if (partial_subreg_p (op)
1002 : 74313 : && subreg_lowpart_p (op)
1003 : 74048 : && GET_CODE (SUBREG_REG (op)) == ASHIFT
1004 : 96339 : && XEXP (SUBREG_REG (op), 0) == const1_rtx)
1005 : : {
1006 : 34 : machine_mode inner_mode = GET_MODE (SUBREG_REG (op));
1007 : 34 : rtx x;
1008 : :
1009 : 34 : x = gen_rtx_ROTATE (inner_mode,
1010 : : simplify_gen_unary (NOT, inner_mode, const1_rtx,
1011 : : inner_mode),
1012 : : XEXP (SUBREG_REG (op), 1));
1013 : 34 : temp = rtl_hooks.gen_lowpart_no_emit (mode, x);
1014 : 34 : if (temp)
1015 : : return temp;
1016 : : }
1017 : :
1018 : : /* Apply De Morgan's laws to reduce number of patterns for machines
1019 : : with negating logical insns (and-not, nand, etc.). If result has
1020 : : only one NOT, put it first, since that is how the patterns are
1021 : : coded. */
1022 : 1681878 : if (GET_CODE (op) == IOR || GET_CODE (op) == AND)
1023 : : {
1024 : 10873 : rtx in1 = XEXP (op, 0), in2 = XEXP (op, 1);
1025 : 10873 : machine_mode op_mode;
1026 : :
1027 : 10873 : op_mode = GET_MODE (in1);
1028 : 10873 : in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
1029 : :
1030 : 10873 : op_mode = GET_MODE (in2);
1031 : 10873 : if (op_mode == VOIDmode)
1032 : 5640 : op_mode = mode;
1033 : 10873 : in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
1034 : :
1035 : 10873 : if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
1036 : : std::swap (in1, in2);
1037 : :
1038 : 21746 : return gen_rtx_fmt_ee (GET_CODE (op) == IOR ? AND : IOR,
1039 : : mode, in1, in2);
1040 : : }
1041 : :
1042 : : /* (not (bswap x)) -> (bswap (not x)). */
1043 : 1671005 : if (GET_CODE (op) == BSWAP || GET_CODE (op) == BITREVERSE)
1044 : : {
1045 : 0 : rtx x = simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1046 : 0 : return simplify_gen_unary (GET_CODE (op), mode, x, mode);
1047 : : }
1048 : : break;
1049 : :
1050 : 1633692 : case NEG:
1051 : : /* (neg (neg X)) == X. */
1052 : 1633692 : if (GET_CODE (op) == NEG)
1053 : 6479 : return XEXP (op, 0);
1054 : :
1055 : : /* (neg (x ? (neg y) : y)) == !x ? (neg y) : y.
1056 : : If comparison is not reversible use
1057 : : x ? y : (neg y). */
1058 : 1627213 : if (GET_CODE (op) == IF_THEN_ELSE)
1059 : : {
1060 : 2142 : rtx cond = XEXP (op, 0);
1061 : 2142 : rtx true_rtx = XEXP (op, 1);
1062 : 2142 : rtx false_rtx = XEXP (op, 2);
1063 : :
1064 : 2142 : if ((GET_CODE (true_rtx) == NEG
1065 : 0 : && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
1066 : 2142 : || (GET_CODE (false_rtx) == NEG
1067 : 0 : && rtx_equal_p (XEXP (false_rtx, 0), true_rtx)))
1068 : : {
1069 : 0 : if (reversed_comparison_code (cond, NULL) != UNKNOWN)
1070 : 0 : temp = reversed_comparison (cond, mode);
1071 : : else
1072 : : {
1073 : : temp = cond;
1074 : : std::swap (true_rtx, false_rtx);
1075 : : }
1076 : 0 : return simplify_gen_ternary (IF_THEN_ELSE, mode,
1077 : 0 : mode, temp, true_rtx, false_rtx);
1078 : : }
1079 : : }
1080 : :
1081 : : /* (neg (plus X 1)) can become (not X). */
1082 : 1627213 : if (GET_CODE (op) == PLUS
1083 : 130587 : && XEXP (op, 1) == const1_rtx)
1084 : 51164 : return simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1085 : :
1086 : : /* Similarly, (neg (not X)) is (plus X 1). */
1087 : 1576049 : if (GET_CODE (op) == NOT)
1088 : 555 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
1089 : 555 : CONST1_RTX (mode));
1090 : :
1091 : : /* (neg (minus X Y)) can become (minus Y X). This transformation
1092 : : isn't safe for modes with signed zeros, since if X and Y are
1093 : : both +0, (minus Y X) is the same as (minus X Y). If the
1094 : : rounding mode is towards +infinity (or -infinity) then the two
1095 : : expressions will be rounded differently. */
1096 : 1575494 : if (GET_CODE (op) == MINUS
1097 : 23120 : && !HONOR_SIGNED_ZEROS (mode)
1098 : 1597204 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1099 : 21710 : return simplify_gen_binary (MINUS, mode, XEXP (op, 1), XEXP (op, 0));
1100 : :
1101 : 1553784 : if (GET_CODE (op) == PLUS
1102 : 79423 : && !HONOR_SIGNED_ZEROS (mode)
1103 : 1632871 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1104 : : {
1105 : : /* (neg (plus A C)) is simplified to (minus -C A). */
1106 : 79087 : if (CONST_SCALAR_INT_P (XEXP (op, 1))
1107 : 4596 : || CONST_DOUBLE_AS_FLOAT_P (XEXP (op, 1)))
1108 : : {
1109 : 74491 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 1), mode);
1110 : 74491 : if (temp)
1111 : 74491 : return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 0));
1112 : : }
1113 : :
1114 : : /* (neg (plus A B)) is canonicalized to (minus (neg A) B). */
1115 : 4596 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
1116 : 4596 : return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 1));
1117 : : }
1118 : :
1119 : : /* (neg (mult A B)) becomes (mult A (neg B)).
1120 : : This works even for floating-point values. */
1121 : 1474697 : if (GET_CODE (op) == MULT
1122 : 1474697 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1123 : : {
1124 : 20478 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 1), mode);
1125 : 20478 : return simplify_gen_binary (MULT, mode, XEXP (op, 0), temp);
1126 : : }
1127 : :
1128 : : /* NEG commutes with ASHIFT since it is multiplication. Only do
1129 : : this if we can then eliminate the NEG (e.g., if the operand
1130 : : is a constant). */
1131 : 1454219 : if (GET_CODE (op) == ASHIFT)
1132 : : {
1133 : 51626 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 0), mode);
1134 : 51626 : if (temp)
1135 : 12627 : return simplify_gen_binary (ASHIFT, mode, temp, XEXP (op, 1));
1136 : : }
1137 : :
1138 : : /* (neg (ashiftrt X C)) can be replaced by (lshiftrt X C) when
1139 : : C is equal to the width of MODE minus 1. */
1140 : 1441592 : if (GET_CODE (op) == ASHIFTRT
1141 : 25951 : && CONST_INT_P (XEXP (op, 1))
1142 : 1493414 : && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1143 : 416 : return simplify_gen_binary (LSHIFTRT, mode,
1144 : 416 : XEXP (op, 0), XEXP (op, 1));
1145 : :
1146 : : /* (neg (lshiftrt X C)) can be replaced by (ashiftrt X C) when
1147 : : C is equal to the width of MODE minus 1. */
1148 : 1441176 : if (GET_CODE (op) == LSHIFTRT
1149 : 6675 : && CONST_INT_P (XEXP (op, 1))
1150 : 1454366 : && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1151 : 2418 : return simplify_gen_binary (ASHIFTRT, mode,
1152 : 2418 : XEXP (op, 0), XEXP (op, 1));
1153 : :
1154 : : /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
1155 : 1438758 : if (GET_CODE (op) == XOR
1156 : 23098 : && XEXP (op, 1) == const1_rtx
1157 : 1438896 : && nonzero_bits (XEXP (op, 0), mode) == 1)
1158 : 40 : return plus_constant (mode, XEXP (op, 0), -1);
1159 : :
1160 : : /* (neg (lt x 0)) is (ashiftrt X C) if STORE_FLAG_VALUE is 1. */
1161 : : /* (neg (lt x 0)) is (lshiftrt X C) if STORE_FLAG_VALUE is -1. */
1162 : 1438718 : if (GET_CODE (op) == LT
1163 : 1644 : && XEXP (op, 1) == const0_rtx
1164 : 1439825 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op, 0)), &inner))
1165 : : {
1166 : 238 : int_mode = as_a <scalar_int_mode> (mode);
1167 : 238 : int isize = GET_MODE_PRECISION (inner);
1168 : 238 : if (STORE_FLAG_VALUE == 1)
1169 : : {
1170 : 238 : temp = simplify_gen_binary (ASHIFTRT, inner, XEXP (op, 0),
1171 : : gen_int_shift_amount (inner,
1172 : 238 : isize - 1));
1173 : 238 : if (int_mode == inner)
1174 : : return temp;
1175 : 160 : if (GET_MODE_PRECISION (int_mode) > isize)
1176 : 95 : return simplify_gen_unary (SIGN_EXTEND, int_mode, temp, inner);
1177 : 65 : return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1178 : : }
1179 : : else if (STORE_FLAG_VALUE == -1)
1180 : : {
1181 : : temp = simplify_gen_binary (LSHIFTRT, inner, XEXP (op, 0),
1182 : : gen_int_shift_amount (inner,
1183 : : isize - 1));
1184 : : if (int_mode == inner)
1185 : : return temp;
1186 : : if (GET_MODE_PRECISION (int_mode) > isize)
1187 : : return simplify_gen_unary (ZERO_EXTEND, int_mode, temp, inner);
1188 : : return simplify_gen_unary (TRUNCATE, int_mode, temp, inner);
1189 : : }
1190 : : }
1191 : :
1192 : 1438480 : if (vec_series_p (op, &base, &step))
1193 : : {
1194 : : /* Only create a new series if we can simplify both parts. In other
1195 : : cases this isn't really a simplification, and it's not necessarily
1196 : : a win to replace a vector operation with a scalar operation. */
1197 : 288 : scalar_mode inner_mode = GET_MODE_INNER (mode);
1198 : 288 : base = simplify_unary_operation (NEG, inner_mode, base, inner_mode);
1199 : 288 : if (base)
1200 : : {
1201 : 288 : step = simplify_unary_operation (NEG, inner_mode,
1202 : : step, inner_mode);
1203 : 288 : if (step)
1204 : 288 : return gen_vec_series (mode, base, step);
1205 : : }
1206 : : }
1207 : : break;
1208 : :
1209 : 1751002 : case TRUNCATE:
1210 : : /* Don't optimize (lshiftrt (mult ...)) as it would interfere
1211 : : with the umulXi3_highpart patterns. */
1212 : 1751002 : if (GET_CODE (op) == LSHIFTRT
1213 : 22673 : && GET_CODE (XEXP (op, 0)) == MULT)
1214 : : break;
1215 : :
1216 : 1742856 : if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
1217 : : {
1218 : 12 : if (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op)))
1219 : : {
1220 : 12 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1221 : 12 : if (temp)
1222 : : return temp;
1223 : : }
1224 : : /* We can't handle truncation to a partial integer mode here
1225 : : because we don't know the real bitsize of the partial
1226 : : integer mode. */
1227 : : break;
1228 : : }
1229 : :
1230 : 1742844 : if (GET_MODE (op) != VOIDmode)
1231 : : {
1232 : 1742844 : temp = simplify_truncation (mode, op, GET_MODE (op));
1233 : 1742844 : if (temp)
1234 : : return temp;
1235 : : }
1236 : :
1237 : : /* If we know that the value is already truncated, we can
1238 : : replace the TRUNCATE with a SUBREG. */
1239 : 1570899 : if (known_eq (GET_MODE_NUNITS (mode), 1)
1240 : 1570899 : && (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op))
1241 : 0 : || truncated_to_mode (mode, op)))
1242 : : {
1243 : 1559740 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1244 : 1559740 : if (temp)
1245 : : return temp;
1246 : : }
1247 : :
1248 : : /* A truncate of a comparison can be replaced with a subreg if
1249 : : STORE_FLAG_VALUE permits. This is like the previous test,
1250 : : but it works even if the comparison is done in a mode larger
1251 : : than HOST_BITS_PER_WIDE_INT. */
1252 : 11598 : if (HWI_COMPUTABLE_MODE_P (mode)
1253 : 439 : && COMPARISON_P (op)
1254 : 0 : && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
1255 : 11598 : && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op)))
1256 : : {
1257 : 0 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1258 : 0 : if (temp)
1259 : : return temp;
1260 : : }
1261 : :
1262 : : /* A truncate of a memory is just loading the low part of the memory
1263 : : if we are not changing the meaning of the address. */
1264 : 11598 : if (GET_CODE (op) == MEM
1265 : 551 : && !VECTOR_MODE_P (mode)
1266 : 437 : && !MEM_VOLATILE_P (op)
1267 : 12035 : && !mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op)))
1268 : : {
1269 : 437 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1270 : 437 : if (temp)
1271 : : return temp;
1272 : : }
1273 : :
1274 : : /* Check for useless truncation. */
1275 : 11598 : if (GET_MODE (op) == mode)
1276 : : return op;
1277 : : break;
1278 : :
1279 : 172419 : case FLOAT_TRUNCATE:
1280 : : /* Check for useless truncation. */
1281 : 172419 : if (GET_MODE (op) == mode)
1282 : : return op;
1283 : :
1284 : 172419 : if (DECIMAL_FLOAT_MODE_P (mode))
1285 : : break;
1286 : :
1287 : : /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
1288 : 172265 : if (GET_CODE (op) == FLOAT_EXTEND
1289 : 2 : && GET_MODE (XEXP (op, 0)) == mode)
1290 : : return XEXP (op, 0);
1291 : :
1292 : : /* (float_truncate:SF (float_truncate:DF foo:XF))
1293 : : = (float_truncate:SF foo:XF).
1294 : : This may eliminate double rounding, so it is unsafe.
1295 : :
1296 : : (float_truncate:SF (float_extend:XF foo:DF))
1297 : : = (float_truncate:SF foo:DF).
1298 : :
1299 : : (float_truncate:DF (float_extend:XF foo:SF))
1300 : : = (float_extend:DF foo:SF). */
1301 : 172265 : if ((GET_CODE (op) == FLOAT_TRUNCATE
1302 : 133 : && flag_unsafe_math_optimizations)
1303 : 172261 : || GET_CODE (op) == FLOAT_EXTEND)
1304 : 12 : return simplify_gen_unary (GET_MODE_UNIT_SIZE (GET_MODE (XEXP (op, 0)))
1305 : 6 : > GET_MODE_UNIT_SIZE (mode)
1306 : : ? FLOAT_TRUNCATE : FLOAT_EXTEND,
1307 : : mode,
1308 : 12 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1309 : :
1310 : : /* (float_truncate (float x)) is (float x) */
1311 : 172259 : if ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1312 : 172259 : && (flag_unsafe_math_optimizations
1313 : 1431 : || exact_int_to_float_conversion_p (op)))
1314 : 1430 : return simplify_gen_unary (GET_CODE (op), mode,
1315 : : XEXP (op, 0),
1316 : 1430 : GET_MODE (XEXP (op, 0)));
1317 : :
1318 : : /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
1319 : : (OP:SF foo:SF) if OP is NEG or ABS. */
1320 : 170829 : if ((GET_CODE (op) == ABS
1321 : 170829 : || GET_CODE (op) == NEG)
1322 : 209 : && GET_CODE (XEXP (op, 0)) == FLOAT_EXTEND
1323 : 28 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode)
1324 : 28 : return simplify_gen_unary (GET_CODE (op), mode,
1325 : 28 : XEXP (XEXP (op, 0), 0), mode);
1326 : :
1327 : : /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
1328 : : is (float_truncate:SF x). */
1329 : 170801 : if (GET_CODE (op) == SUBREG
1330 : 308 : && subreg_lowpart_p (op)
1331 : 171106 : && GET_CODE (SUBREG_REG (op)) == FLOAT_TRUNCATE)
1332 : : return SUBREG_REG (op);
1333 : : break;
1334 : :
1335 : 593704 : case FLOAT_EXTEND:
1336 : : /* Check for useless extension. */
1337 : 593704 : if (GET_MODE (op) == mode)
1338 : : return op;
1339 : :
1340 : 593704 : if (DECIMAL_FLOAT_MODE_P (mode))
1341 : : break;
1342 : :
1343 : : /* (float_extend (float_extend x)) is (float_extend x)
1344 : :
1345 : : (float_extend (float x)) is (float x) assuming that double
1346 : : rounding can't happen.
1347 : : */
1348 : 593601 : if (GET_CODE (op) == FLOAT_EXTEND
1349 : 593601 : || ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1350 : 1293 : && exact_int_to_float_conversion_p (op)))
1351 : 560 : return simplify_gen_unary (GET_CODE (op), mode,
1352 : : XEXP (op, 0),
1353 : 560 : GET_MODE (XEXP (op, 0)));
1354 : :
1355 : : break;
1356 : :
1357 : 309536 : case ABS:
1358 : : /* (abs (neg <foo>)) -> (abs <foo>) */
1359 : 309536 : if (GET_CODE (op) == NEG)
1360 : 18 : return simplify_gen_unary (ABS, mode, XEXP (op, 0),
1361 : 18 : GET_MODE (XEXP (op, 0)));
1362 : :
1363 : : /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
1364 : : do nothing. */
1365 : 309518 : if (GET_MODE (op) == VOIDmode)
1366 : : break;
1367 : :
1368 : : /* If operand is something known to be positive, ignore the ABS. */
1369 : 309518 : if (val_signbit_known_clear_p (GET_MODE (op),
1370 : : nonzero_bits (op, GET_MODE (op))))
1371 : : return op;
1372 : :
1373 : : /* Using nonzero_bits doesn't (currently) work for modes wider than
1374 : : HOST_WIDE_INT, so the following transformations help simplify
1375 : : ABS for TImode and wider. */
1376 : 309334 : switch (GET_CODE (op))
1377 : : {
1378 : : case ABS:
1379 : : case CLRSB:
1380 : : case FFS:
1381 : : case PARITY:
1382 : : case POPCOUNT:
1383 : : case SS_ABS:
1384 : : return op;
1385 : :
1386 : 0 : case LSHIFTRT:
1387 : 0 : if (CONST_INT_P (XEXP (op, 1))
1388 : 0 : && INTVAL (XEXP (op, 1)) > 0
1389 : 309334 : && is_a <scalar_int_mode> (mode, &int_mode)
1390 : 0 : && INTVAL (XEXP (op, 1)) < GET_MODE_PRECISION (int_mode))
1391 : : return op;
1392 : : break;
1393 : :
1394 : : default:
1395 : : break;
1396 : : }
1397 : :
1398 : : /* If operand is known to be only -1 or 0, convert ABS to NEG. */
1399 : 309334 : if (is_a <scalar_int_mode> (mode, &int_mode)
1400 : 65579 : && (num_sign_bit_copies (op, int_mode)
1401 : 65579 : == GET_MODE_PRECISION (int_mode)))
1402 : 4 : return gen_rtx_NEG (int_mode, op);
1403 : :
1404 : : break;
1405 : :
1406 : 0 : case FFS:
1407 : : /* (ffs (*_extend <X>)) = (*_extend (ffs <X>)). */
1408 : 0 : if (GET_CODE (op) == SIGN_EXTEND
1409 : 0 : || GET_CODE (op) == ZERO_EXTEND)
1410 : : {
1411 : 0 : temp = simplify_gen_unary (FFS, GET_MODE (XEXP (op, 0)),
1412 : 0 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1413 : 0 : return simplify_gen_unary (GET_CODE (op), mode, temp,
1414 : 0 : GET_MODE (temp));
1415 : : }
1416 : : break;
1417 : :
1418 : 3249 : case POPCOUNT:
1419 : 3249 : switch (GET_CODE (op))
1420 : : {
1421 : 0 : case BSWAP:
1422 : 0 : case BITREVERSE:
1423 : : /* (popcount (bswap <X>)) = (popcount <X>). */
1424 : 0 : return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1425 : 0 : GET_MODE (XEXP (op, 0)));
1426 : :
1427 : 34 : case ZERO_EXTEND:
1428 : : /* (popcount (zero_extend <X>)) = (zero_extend (popcount <X>)). */
1429 : 68 : temp = simplify_gen_unary (POPCOUNT, GET_MODE (XEXP (op, 0)),
1430 : 34 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1431 : 34 : return simplify_gen_unary (ZERO_EXTEND, mode, temp,
1432 : 34 : GET_MODE (temp));
1433 : :
1434 : 0 : case ROTATE:
1435 : 0 : case ROTATERT:
1436 : : /* Rotations don't affect popcount. */
1437 : 0 : if (!side_effects_p (XEXP (op, 1)))
1438 : 0 : return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1439 : 0 : GET_MODE (XEXP (op, 0)));
1440 : : break;
1441 : :
1442 : : default:
1443 : : break;
1444 : : }
1445 : : break;
1446 : :
1447 : 0 : case PARITY:
1448 : 0 : switch (GET_CODE (op))
1449 : : {
1450 : 0 : case NOT:
1451 : 0 : case BSWAP:
1452 : 0 : case BITREVERSE:
1453 : 0 : return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1454 : 0 : GET_MODE (XEXP (op, 0)));
1455 : :
1456 : 0 : case ZERO_EXTEND:
1457 : 0 : case SIGN_EXTEND:
1458 : 0 : temp = simplify_gen_unary (PARITY, GET_MODE (XEXP (op, 0)),
1459 : 0 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1460 : 0 : return simplify_gen_unary (GET_CODE (op), mode, temp,
1461 : 0 : GET_MODE (temp));
1462 : :
1463 : 0 : case ROTATE:
1464 : 0 : case ROTATERT:
1465 : : /* Rotations don't affect parity. */
1466 : 0 : if (!side_effects_p (XEXP (op, 1)))
1467 : 0 : return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1468 : 0 : GET_MODE (XEXP (op, 0)));
1469 : : break;
1470 : :
1471 : : case PARITY:
1472 : : /* (parity (parity x)) -> parity (x). */
1473 : : return op;
1474 : :
1475 : : default:
1476 : : break;
1477 : : }
1478 : : break;
1479 : :
1480 : 28871 : case BSWAP:
1481 : : /* (bswap (bswap x)) -> x. */
1482 : 28871 : if (GET_CODE (op) == BSWAP)
1483 : 128 : return XEXP (op, 0);
1484 : : break;
1485 : :
1486 : 0 : case BITREVERSE:
1487 : : /* (bitreverse (bitreverse x)) -> x. */
1488 : 0 : if (GET_CODE (op) == BITREVERSE)
1489 : 0 : return XEXP (op, 0);
1490 : : break;
1491 : :
1492 : 930889 : case FLOAT:
1493 : : /* (float (sign_extend <X>)) = (float <X>). */
1494 : 930889 : if (GET_CODE (op) == SIGN_EXTEND)
1495 : 10924 : return simplify_gen_unary (FLOAT, mode, XEXP (op, 0),
1496 : 10924 : GET_MODE (XEXP (op, 0)));
1497 : : break;
1498 : :
1499 : 3247849 : case SIGN_EXTEND:
1500 : : /* Check for useless extension. */
1501 : 3247849 : if (GET_MODE (op) == mode)
1502 : : return op;
1503 : :
1504 : : /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
1505 : : becomes just the MINUS if its mode is MODE. This allows
1506 : : folding switch statements on machines using casesi (such as
1507 : : the VAX). */
1508 : 3247809 : if (GET_CODE (op) == TRUNCATE
1509 : 62 : && GET_MODE (XEXP (op, 0)) == mode
1510 : 62 : && GET_CODE (XEXP (op, 0)) == MINUS
1511 : 0 : && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
1512 : 0 : && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
1513 : : return XEXP (op, 0);
1514 : :
1515 : : /* Extending a widening multiplication should be canonicalized to
1516 : : a wider widening multiplication. */
1517 : 3247809 : if (GET_CODE (op) == MULT)
1518 : : {
1519 : 67511 : rtx lhs = XEXP (op, 0);
1520 : 67511 : rtx rhs = XEXP (op, 1);
1521 : 67511 : enum rtx_code lcode = GET_CODE (lhs);
1522 : 67511 : enum rtx_code rcode = GET_CODE (rhs);
1523 : :
1524 : : /* Widening multiplies usually extend both operands, but sometimes
1525 : : they use a shift to extract a portion of a register. */
1526 : 67511 : if ((lcode == SIGN_EXTEND
1527 : 67362 : || (lcode == ASHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1528 : 875 : && (rcode == SIGN_EXTEND
1529 : 831 : || (rcode == ASHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1530 : : {
1531 : 165 : machine_mode lmode = GET_MODE (lhs);
1532 : 165 : machine_mode rmode = GET_MODE (rhs);
1533 : 165 : int bits;
1534 : :
1535 : 165 : if (lcode == ASHIFTRT)
1536 : : /* Number of bits not shifted off the end. */
1537 : 125 : bits = (GET_MODE_UNIT_PRECISION (lmode)
1538 : 125 : - INTVAL (XEXP (lhs, 1)));
1539 : : else /* lcode == SIGN_EXTEND */
1540 : : /* Size of inner mode. */
1541 : 80 : bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1542 : :
1543 : 165 : if (rcode == ASHIFTRT)
1544 : 121 : bits += (GET_MODE_UNIT_PRECISION (rmode)
1545 : 121 : - INTVAL (XEXP (rhs, 1)));
1546 : : else /* rcode == SIGN_EXTEND */
1547 : 88 : bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1548 : :
1549 : : /* We can only widen multiplies if the result is mathematiclly
1550 : : equivalent. I.e. if overflow was impossible. */
1551 : 330 : if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1552 : 108 : return simplify_gen_binary
1553 : 108 : (MULT, mode,
1554 : : simplify_gen_unary (SIGN_EXTEND, mode, lhs, lmode),
1555 : 108 : simplify_gen_unary (SIGN_EXTEND, mode, rhs, rmode));
1556 : : }
1557 : : }
1558 : :
1559 : : /* Check for a sign extension of a subreg of a promoted
1560 : : variable, where the promotion is sign-extended, and the
1561 : : target mode is the same as the variable's promotion. */
1562 : 3247701 : if (GET_CODE (op) == SUBREG
1563 : 193596 : && SUBREG_PROMOTED_VAR_P (op)
1564 : 3253477 : && SUBREG_PROMOTED_SIGNED_P (op))
1565 : : {
1566 : 0 : rtx subreg = SUBREG_REG (op);
1567 : 0 : machine_mode subreg_mode = GET_MODE (subreg);
1568 : 0 : if (!paradoxical_subreg_p (mode, subreg_mode))
1569 : : {
1570 : 0 : temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1571 : 0 : if (temp)
1572 : : {
1573 : : /* Preserve SUBREG_PROMOTED_VAR_P. */
1574 : 0 : if (partial_subreg_p (temp))
1575 : : {
1576 : 0 : SUBREG_PROMOTED_VAR_P (temp) = 1;
1577 : 0 : SUBREG_PROMOTED_SET (temp, SRP_SIGNED);
1578 : : }
1579 : 0 : return temp;
1580 : : }
1581 : : }
1582 : : else
1583 : : /* Sign-extending a sign-extended subreg. */
1584 : 0 : return simplify_gen_unary (SIGN_EXTEND, mode,
1585 : 0 : subreg, subreg_mode);
1586 : : }
1587 : :
1588 : : /* (sign_extend:M (sign_extend:N <X>)) is (sign_extend:M <X>).
1589 : : (sign_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1590 : 3247701 : if (GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND)
1591 : : {
1592 : 19128 : gcc_assert (GET_MODE_UNIT_PRECISION (mode)
1593 : : > GET_MODE_UNIT_PRECISION (GET_MODE (op)));
1594 : 6376 : return simplify_gen_unary (GET_CODE (op), mode, XEXP (op, 0),
1595 : 6376 : GET_MODE (XEXP (op, 0)));
1596 : : }
1597 : :
1598 : : /* (sign_extend:M (ashiftrt:N (ashift <X> (const_int I)) (const_int I)))
1599 : : is (sign_extend:M (subreg:O <X>)) if there is mode with
1600 : : GET_MODE_BITSIZE (N) - I bits.
1601 : : (sign_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1602 : : is similarly (zero_extend:M (subreg:O <X>)). */
1603 : 3241325 : if ((GET_CODE (op) == ASHIFTRT || GET_CODE (op) == LSHIFTRT)
1604 : 84316 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1605 : 3243818 : && is_a <scalar_int_mode> (mode, &int_mode)
1606 : 4905 : && CONST_INT_P (XEXP (op, 1))
1607 : 4905 : && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1608 : 3246106 : && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1609 : 4781 : GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1610 : : {
1611 : 4781 : scalar_int_mode tmode;
1612 : 4781 : gcc_assert (GET_MODE_PRECISION (int_mode)
1613 : : > GET_MODE_PRECISION (op_mode));
1614 : 4781 : if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1615 : 7150 : - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1616 : : {
1617 : 2412 : rtx inner =
1618 : 2412 : rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1619 : 2412 : if (inner)
1620 : 2412 : return simplify_gen_unary (GET_CODE (op) == ASHIFTRT
1621 : : ? SIGN_EXTEND : ZERO_EXTEND,
1622 : 2412 : int_mode, inner, tmode);
1623 : : }
1624 : : }
1625 : :
1626 : : /* (sign_extend:M (lshiftrt:N <X> (const_int I))) is better as
1627 : : (zero_extend:M (lshiftrt:N <X> (const_int I))) if I is not 0. */
1628 : 3238913 : if (GET_CODE (op) == LSHIFTRT
1629 : 192 : && CONST_INT_P (XEXP (op, 1))
1630 : 192 : && XEXP (op, 1) != const0_rtx)
1631 : 192 : return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1632 : :
1633 : : /* (sign_extend:M (truncate:N (lshiftrt:O <X> (const_int I)))) where
1634 : : I is GET_MODE_PRECISION(O) - GET_MODE_PRECISION(N), simplifies to
1635 : : (ashiftrt:M <X> (const_int I)) if modes M and O are the same, and
1636 : : (truncate:M (ashiftrt:O <X> (const_int I))) if M is narrower than
1637 : : O, and (sign_extend:M (ashiftrt:O <X> (const_int I))) if M is
1638 : : wider than O. */
1639 : 3238721 : if (GET_CODE (op) == TRUNCATE
1640 : 62 : && GET_CODE (XEXP (op, 0)) == LSHIFTRT
1641 : 0 : && CONST_INT_P (XEXP (XEXP (op, 0), 1)))
1642 : : {
1643 : 0 : scalar_int_mode m_mode, n_mode, o_mode;
1644 : 0 : rtx old_shift = XEXP (op, 0);
1645 : 0 : if (is_a <scalar_int_mode> (mode, &m_mode)
1646 : 0 : && is_a <scalar_int_mode> (GET_MODE (op), &n_mode)
1647 : 0 : && is_a <scalar_int_mode> (GET_MODE (old_shift), &o_mode)
1648 : 0 : && GET_MODE_PRECISION (o_mode) - GET_MODE_PRECISION (n_mode)
1649 : 0 : == INTVAL (XEXP (old_shift, 1)))
1650 : : {
1651 : 0 : rtx new_shift = simplify_gen_binary (ASHIFTRT,
1652 : : GET_MODE (old_shift),
1653 : : XEXP (old_shift, 0),
1654 : : XEXP (old_shift, 1));
1655 : 0 : if (GET_MODE_PRECISION (m_mode) > GET_MODE_PRECISION (o_mode))
1656 : 0 : return simplify_gen_unary (SIGN_EXTEND, mode, new_shift,
1657 : 0 : GET_MODE (new_shift));
1658 : 0 : if (mode != GET_MODE (new_shift))
1659 : 0 : return simplify_gen_unary (TRUNCATE, mode, new_shift,
1660 : 0 : GET_MODE (new_shift));
1661 : : return new_shift;
1662 : : }
1663 : : }
1664 : :
1665 : : /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when
1666 : : we know the sign bit of OP must be clear. */
1667 : 3238721 : if (val_signbit_known_clear_p (GET_MODE (op),
1668 : 3238721 : nonzero_bits (op, GET_MODE (op))))
1669 : 45941 : return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1670 : :
1671 : : /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1672 : 3192780 : if (GET_CODE (op) == SUBREG
1673 : 193116 : && subreg_lowpart_p (op)
1674 : 192985 : && GET_MODE (SUBREG_REG (op)) == mode
1675 : 3356057 : && is_a <scalar_int_mode> (mode, &int_mode)
1676 : 170019 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1677 : 170019 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1678 : 168191 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1679 : 3360971 : && (nonzero_bits (SUBREG_REG (op), mode)
1680 : 168191 : & ~(GET_MODE_MASK (op_mode) >> 1)) == 0)
1681 : 6742 : return SUBREG_REG (op);
1682 : :
1683 : : #if defined(POINTERS_EXTEND_UNSIGNED)
1684 : : /* As we do not know which address space the pointer is referring to,
1685 : : we can do this only if the target does not support different pointer
1686 : : or address modes depending on the address space. */
1687 : 3186038 : if (target_default_pointer_address_modes_p ()
1688 : : && ! POINTERS_EXTEND_UNSIGNED
1689 : : && mode == Pmode && GET_MODE (op) == ptr_mode
1690 : : && (CONSTANT_P (op)
1691 : : || (GET_CODE (op) == SUBREG
1692 : : && REG_P (SUBREG_REG (op))
1693 : : && REG_POINTER (SUBREG_REG (op))
1694 : : && GET_MODE (SUBREG_REG (op)) == Pmode))
1695 : : && !targetm.have_ptr_extend ())
1696 : : {
1697 : : temp
1698 : : = convert_memory_address_addr_space_1 (Pmode, op,
1699 : : ADDR_SPACE_GENERIC, false,
1700 : : true);
1701 : : if (temp)
1702 : : return temp;
1703 : : }
1704 : : #endif
1705 : : break;
1706 : :
1707 : 11823245 : case ZERO_EXTEND:
1708 : : /* Check for useless extension. */
1709 : 11823245 : if (GET_MODE (op) == mode)
1710 : : return op;
1711 : :
1712 : : /* (zero_extend:SI (and:QI X (const))) -> (and:SI (lowpart:SI X) const)
1713 : : where const does not sign bit set. */
1714 : 11823205 : if (GET_CODE (op) == AND
1715 : 127598 : && CONST_INT_P (XEXP (op, 1))
1716 : 107570 : && INTVAL (XEXP (op, 1)) > 0)
1717 : : {
1718 : 99862 : rtx tem = rtl_hooks.gen_lowpart_no_emit (mode, XEXP (op, 0));
1719 : 99862 : if (tem)
1720 : 85259 : return simplify_gen_binary (AND, mode, tem, XEXP (op, 1));
1721 : : }
1722 : :
1723 : : /* Check for a zero extension of a subreg of a promoted
1724 : : variable, where the promotion is zero-extended, and the
1725 : : target mode is the same as the variable's promotion. */
1726 : 11737946 : if (GET_CODE (op) == SUBREG
1727 : 1395052 : && SUBREG_PROMOTED_VAR_P (op)
1728 : 11738420 : && SUBREG_PROMOTED_UNSIGNED_P (op))
1729 : : {
1730 : 474 : rtx subreg = SUBREG_REG (op);
1731 : 474 : machine_mode subreg_mode = GET_MODE (subreg);
1732 : 474 : if (!paradoxical_subreg_p (mode, subreg_mode))
1733 : : {
1734 : 322 : temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1735 : 322 : if (temp)
1736 : : {
1737 : : /* Preserve SUBREG_PROMOTED_VAR_P. */
1738 : 322 : if (partial_subreg_p (temp))
1739 : : {
1740 : 129 : SUBREG_PROMOTED_VAR_P (temp) = 1;
1741 : 129 : SUBREG_PROMOTED_SET (temp, SRP_UNSIGNED);
1742 : : }
1743 : 322 : return temp;
1744 : : }
1745 : : }
1746 : : else
1747 : : /* Zero-extending a zero-extended subreg. */
1748 : 152 : return simplify_gen_unary (ZERO_EXTEND, mode,
1749 : 152 : subreg, subreg_mode);
1750 : : }
1751 : :
1752 : : /* Extending a widening multiplication should be canonicalized to
1753 : : a wider widening multiplication. */
1754 : 11737472 : if (GET_CODE (op) == MULT)
1755 : : {
1756 : 199450 : rtx lhs = XEXP (op, 0);
1757 : 199450 : rtx rhs = XEXP (op, 1);
1758 : 199450 : enum rtx_code lcode = GET_CODE (lhs);
1759 : 199450 : enum rtx_code rcode = GET_CODE (rhs);
1760 : :
1761 : : /* Widening multiplies usually extend both operands, but sometimes
1762 : : they use a shift to extract a portion of a register. */
1763 : 199450 : if ((lcode == ZERO_EXTEND
1764 : 198713 : || (lcode == LSHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1765 : 785 : && (rcode == ZERO_EXTEND
1766 : 713 : || (rcode == LSHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1767 : : {
1768 : 72 : machine_mode lmode = GET_MODE (lhs);
1769 : 72 : machine_mode rmode = GET_MODE (rhs);
1770 : 72 : int bits;
1771 : :
1772 : 72 : if (lcode == LSHIFTRT)
1773 : : /* Number of bits not shifted off the end. */
1774 : 0 : bits = (GET_MODE_UNIT_PRECISION (lmode)
1775 : 0 : - INTVAL (XEXP (lhs, 1)));
1776 : : else /* lcode == ZERO_EXTEND */
1777 : : /* Size of inner mode. */
1778 : 144 : bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1779 : :
1780 : 72 : if (rcode == LSHIFTRT)
1781 : 0 : bits += (GET_MODE_UNIT_PRECISION (rmode)
1782 : 0 : - INTVAL (XEXP (rhs, 1)));
1783 : : else /* rcode == ZERO_EXTEND */
1784 : 144 : bits += GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (rhs, 0)));
1785 : :
1786 : : /* We can only widen multiplies if the result is mathematiclly
1787 : : equivalent. I.e. if overflow was impossible. */
1788 : 144 : if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1789 : 72 : return simplify_gen_binary
1790 : 72 : (MULT, mode,
1791 : : simplify_gen_unary (ZERO_EXTEND, mode, lhs, lmode),
1792 : 72 : simplify_gen_unary (ZERO_EXTEND, mode, rhs, rmode));
1793 : : }
1794 : : }
1795 : :
1796 : : /* (zero_extend:M (zero_extend:N <X>)) is (zero_extend:M <X>). */
1797 : 11737400 : if (GET_CODE (op) == ZERO_EXTEND)
1798 : 26493 : return simplify_gen_unary (ZERO_EXTEND, mode, XEXP (op, 0),
1799 : 26493 : GET_MODE (XEXP (op, 0)));
1800 : :
1801 : : /* (zero_extend:M (lshiftrt:N (ashift <X> (const_int I)) (const_int I)))
1802 : : is (zero_extend:M (subreg:O <X>)) if there is mode with
1803 : : GET_MODE_PRECISION (N) - I bits. */
1804 : 11710907 : if (GET_CODE (op) == LSHIFTRT
1805 : 86735 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1806 : 11710960 : && is_a <scalar_int_mode> (mode, &int_mode)
1807 : 53 : && CONST_INT_P (XEXP (op, 1))
1808 : 48 : && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1809 : 11710907 : && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1810 : 0 : GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1811 : : {
1812 : 0 : scalar_int_mode tmode;
1813 : 0 : if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1814 : 0 : - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1815 : : {
1816 : 0 : rtx inner =
1817 : 0 : rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1818 : 0 : if (inner)
1819 : 0 : return simplify_gen_unary (ZERO_EXTEND, int_mode,
1820 : 0 : inner, tmode);
1821 : : }
1822 : : }
1823 : :
1824 : : /* (zero_extend:M (subreg:N <X:O>)) is <X:O> (for M == O) or
1825 : : (zero_extend:M <X:O>), if X doesn't have any non-zero bits outside
1826 : : of mode N. E.g.
1827 : : (zero_extend:SI (subreg:QI (and:SI (reg:SI) (const_int 63)) 0)) is
1828 : : (and:SI (reg:SI) (const_int 63)). */
1829 : 11710907 : if (partial_subreg_p (op)
1830 : 13056140 : && is_a <scalar_int_mode> (mode, &int_mode)
1831 : 1376177 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &op0_mode)
1832 : 1375656 : && GET_MODE_PRECISION (op0_mode) <= HOST_BITS_PER_WIDE_INT
1833 : 968353 : && GET_MODE_PRECISION (int_mode) >= GET_MODE_PRECISION (op0_mode)
1834 : 943503 : && subreg_lowpart_p (op)
1835 : 2031082 : && (nonzero_bits (SUBREG_REG (op), op0_mode)
1836 : 636504 : & ~GET_MODE_MASK (GET_MODE (op))) == 0)
1837 : : {
1838 : 30944 : if (GET_MODE_PRECISION (int_mode) == GET_MODE_PRECISION (op0_mode))
1839 : 19663 : return SUBREG_REG (op);
1840 : 11281 : return simplify_gen_unary (ZERO_EXTEND, int_mode, SUBREG_REG (op),
1841 : 11281 : op0_mode);
1842 : : }
1843 : :
1844 : : /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1845 : 11679963 : if (GET_CODE (op) == SUBREG
1846 : 1363634 : && subreg_lowpart_p (op)
1847 : 720625 : && GET_MODE (SUBREG_REG (op)) == mode
1848 : 12305412 : && is_a <scalar_int_mode> (mode, &int_mode)
1849 : 625449 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1850 : 625449 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1851 : 554975 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1852 : 12234938 : && (nonzero_bits (SUBREG_REG (op), mode)
1853 : 554975 : & ~GET_MODE_MASK (op_mode)) == 0)
1854 : 0 : return SUBREG_REG (op);
1855 : :
1856 : : /* Trying to optimize:
1857 : : (zero_extend:M (subreg:N (not:M (X:M)))) ->
1858 : : (xor:M (zero_extend:M (subreg:N (X:M)), mask))
1859 : : where the mask is GET_MODE_MASK (N).
1860 : : For the cases when X:M doesn't have any non-zero bits
1861 : : outside of mode N, (zero_extend:M (subreg:N (X:M))
1862 : : will be simplified to just (X:M)
1863 : : and whole optimization will be -> (xor:M (X:M, mask)). */
1864 : 11679963 : if (partial_subreg_p (op)
1865 : 1345233 : && GET_CODE (XEXP (op, 0)) == NOT
1866 : 698 : && GET_MODE (XEXP (op, 0)) == mode
1867 : 695 : && subreg_lowpart_p (op)
1868 : 11680240 : && HWI_COMPUTABLE_MODE_P (mode)
1869 : 278 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1870 : 1363912 : && (nonzero_bits (XEXP (XEXP (op, 0), 0), mode)
1871 : 278 : & ~GET_MODE_MASK (op_mode)) == 0)
1872 : : {
1873 : 1 : unsigned HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
1874 : 2 : return simplify_gen_binary (XOR, mode,
1875 : 1 : XEXP (XEXP (op, 0), 0),
1876 : 1 : gen_int_mode (mask, mode));
1877 : : }
1878 : :
1879 : : #if defined(POINTERS_EXTEND_UNSIGNED)
1880 : : /* As we do not know which address space the pointer is referring to,
1881 : : we can do this only if the target does not support different pointer
1882 : : or address modes depending on the address space. */
1883 : 11679962 : if (target_default_pointer_address_modes_p ()
1884 : : && POINTERS_EXTEND_UNSIGNED > 0
1885 : 12978383 : && mode == Pmode && GET_MODE (op) == ptr_mode
1886 : 677 : && (CONSTANT_P (op)
1887 : 656 : || (GET_CODE (op) == SUBREG
1888 : 0 : && REG_P (SUBREG_REG (op))
1889 : 0 : && REG_POINTER (SUBREG_REG (op))
1890 : 0 : && GET_MODE (SUBREG_REG (op)) == Pmode))
1891 : 11679983 : && !targetm.have_ptr_extend ())
1892 : : {
1893 : 21 : temp
1894 : 21 : = convert_memory_address_addr_space_1 (Pmode, op,
1895 : : ADDR_SPACE_GENERIC, false,
1896 : : true);
1897 : 21 : if (temp)
1898 : : return temp;
1899 : : }
1900 : : #endif
1901 : : break;
1902 : :
1903 : : default:
1904 : : break;
1905 : : }
1906 : :
1907 : 19499703 : if (VECTOR_MODE_P (mode)
1908 : 1439499 : && vec_duplicate_p (op, &elt)
1909 : 20944640 : && code != VEC_DUPLICATE)
1910 : : {
1911 : 5434 : if (code == SIGN_EXTEND || code == ZERO_EXTEND)
1912 : : /* Enforce a canonical order of VEC_DUPLICATE wrt other unary
1913 : : operations by promoting VEC_DUPLICATE to the root of the expression
1914 : : (as far as possible). */
1915 : 4366 : temp = simplify_gen_unary (code, GET_MODE_INNER (mode),
1916 : 8732 : elt, GET_MODE_INNER (GET_MODE (op)));
1917 : : else
1918 : : /* Try applying the operator to ELT and see if that simplifies.
1919 : : We can duplicate the result if so.
1920 : :
1921 : : The reason we traditionally haven't used simplify_gen_unary
1922 : : for these codes is that it didn't necessarily seem to be a
1923 : : win to convert things like:
1924 : :
1925 : : (neg:V (vec_duplicate:V (reg:S R)))
1926 : :
1927 : : to:
1928 : :
1929 : : (vec_duplicate:V (neg:S (reg:S R)))
1930 : :
1931 : : The first might be done entirely in vector registers while the
1932 : : second might need a move between register files.
1933 : :
1934 : : However, there also cases where promoting the vec_duplicate is
1935 : : more efficient, and there is definite value in having a canonical
1936 : : form when matching instruction patterns. We should consider
1937 : : extending the simplify_gen_unary code above to more cases. */
1938 : 1068 : temp = simplify_unary_operation (code, GET_MODE_INNER (mode),
1939 : 2136 : elt, GET_MODE_INNER (GET_MODE (op)));
1940 : 5434 : if (temp)
1941 : 4950 : return gen_vec_duplicate (mode, temp);
1942 : : }
1943 : :
1944 : : return 0;
1945 : : }
1946 : :
1947 : : /* Try to compute the value of a unary operation CODE whose output mode is to
1948 : : be MODE with input operand OP whose mode was originally OP_MODE.
1949 : : Return zero if the value cannot be computed. */
1950 : : rtx
1951 : 28244726 : simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
1952 : : rtx op, machine_mode op_mode)
1953 : : {
1954 : 28244726 : scalar_int_mode result_mode;
1955 : :
1956 : 28244726 : if (code == VEC_DUPLICATE)
1957 : : {
1958 : 1620578 : gcc_assert (VECTOR_MODE_P (mode));
1959 : 1620578 : if (GET_MODE (op) != VOIDmode)
1960 : : {
1961 : 527811 : if (!VECTOR_MODE_P (GET_MODE (op)))
1962 : 1042418 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE (op));
1963 : : else
1964 : 19806 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE_INNER
1965 : : (GET_MODE (op)));
1966 : : }
1967 : 1620578 : if (CONST_SCALAR_INT_P (op) || CONST_DOUBLE_AS_FLOAT_P (op))
1968 : 1127607 : return gen_const_vec_duplicate (mode, op);
1969 : 492971 : if (GET_CODE (op) == CONST_VECTOR
1970 : 492971 : && (CONST_VECTOR_DUPLICATE_P (op)
1971 : : || CONST_VECTOR_NUNITS (op).is_constant ()))
1972 : : {
1973 : 779 : unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op)
1974 : 779 : ? CONST_VECTOR_NPATTERNS (op)
1975 : 1557 : : CONST_VECTOR_NUNITS (op).to_constant ());
1976 : 2337 : gcc_assert (multiple_p (GET_MODE_NUNITS (mode), npatterns));
1977 : 779 : rtx_vector_builder builder (mode, npatterns, 1);
1978 : 3202 : for (unsigned i = 0; i < npatterns; i++)
1979 : 2423 : builder.quick_push (CONST_VECTOR_ELT (op, i));
1980 : 779 : return builder.build ();
1981 : 779 : }
1982 : : }
1983 : :
1984 : 25952962 : if (VECTOR_MODE_P (mode)
1985 : 1496811 : && GET_CODE (op) == CONST_VECTOR
1986 : 27271251 : && known_eq (GET_MODE_NUNITS (mode), CONST_VECTOR_NUNITS (op)))
1987 : : {
1988 : 51637 : gcc_assert (GET_MODE (op) == op_mode);
1989 : :
1990 : 51637 : rtx_vector_builder builder;
1991 : 51637 : if (!builder.new_unary_operation (mode, op, false))
1992 : : return 0;
1993 : :
1994 : 51637 : unsigned int count = builder.encoded_nelts ();
1995 : 201682 : for (unsigned int i = 0; i < count; i++)
1996 : : {
1997 : 301128 : rtx x = simplify_unary_operation (code, GET_MODE_INNER (mode),
1998 : : CONST_VECTOR_ELT (op, i),
1999 : 301128 : GET_MODE_INNER (op_mode));
2000 : 150564 : if (!x || !valid_for_const_vector_p (mode, x))
2001 : 519 : return 0;
2002 : 150045 : builder.quick_push (x);
2003 : : }
2004 : 51118 : return builder.build ();
2005 : 51637 : }
2006 : :
2007 : : /* The order of these tests is critical so that, for example, we don't
2008 : : check the wrong mode (input vs. output) for a conversion operation,
2009 : : such as FIX. At some point, this should be simplified. */
2010 : :
2011 : 27064703 : if (code == FLOAT && CONST_SCALAR_INT_P (op))
2012 : : {
2013 : 7181 : REAL_VALUE_TYPE d;
2014 : :
2015 : 7181 : if (op_mode == VOIDmode)
2016 : : {
2017 : : /* CONST_INT have VOIDmode as the mode. We assume that all
2018 : : the bits of the constant are significant, though, this is
2019 : : a dangerous assumption as many times CONST_INTs are
2020 : : created and used with garbage in the bits outside of the
2021 : : precision of the implied mode of the const_int. */
2022 : 63 : op_mode = MAX_MODE_INT;
2023 : : }
2024 : :
2025 : 7181 : real_from_integer (&d, mode, rtx_mode_t (op, op_mode), SIGNED);
2026 : :
2027 : : /* Avoid the folding if flag_signaling_nans is on and
2028 : : operand is a signaling NaN. */
2029 : 7181 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2030 : : return 0;
2031 : :
2032 : 7181 : d = real_value_truncate (mode, d);
2033 : :
2034 : : /* Avoid the folding if flag_rounding_math is on and the
2035 : : conversion is not exact. */
2036 : 7181 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2037 : : {
2038 : 1004 : bool fail = false;
2039 : 1004 : wide_int w = real_to_integer (&d, &fail,
2040 : : GET_MODE_PRECISION
2041 : 1004 : (as_a <scalar_int_mode> (op_mode)));
2042 : 2008 : if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
2043 : 898 : return 0;
2044 : 1004 : }
2045 : :
2046 : 6283 : return const_double_from_real_value (d, mode);
2047 : : }
2048 : 27057522 : else if (code == UNSIGNED_FLOAT && CONST_SCALAR_INT_P (op))
2049 : : {
2050 : 2149 : REAL_VALUE_TYPE d;
2051 : :
2052 : 2149 : 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 : 8 : op_mode = MAX_MODE_INT;
2060 : : }
2061 : :
2062 : 2149 : real_from_integer (&d, mode, rtx_mode_t (op, op_mode), UNSIGNED);
2063 : :
2064 : : /* Avoid the folding if flag_signaling_nans is on and
2065 : : operand is a signaling NaN. */
2066 : 2149 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2067 : : return 0;
2068 : :
2069 : 2149 : 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 : 2149 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2074 : : {
2075 : 16 : bool fail = false;
2076 : 16 : wide_int w = real_to_integer (&d, &fail,
2077 : : GET_MODE_PRECISION
2078 : 16 : (as_a <scalar_int_mode> (op_mode)));
2079 : 28 : if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
2080 : 16 : return 0;
2081 : 16 : }
2082 : :
2083 : 2133 : return const_double_from_real_value (d, mode);
2084 : : }
2085 : :
2086 : 27055373 : if (CONST_SCALAR_INT_P (op) && is_a <scalar_int_mode> (mode, &result_mode))
2087 : : {
2088 : 3390421 : unsigned int width = GET_MODE_PRECISION (result_mode);
2089 : 3390421 : if (width > MAX_BITSIZE_MODE_ANY_INT)
2090 : : return 0;
2091 : :
2092 : 3390421 : wide_int result;
2093 : 3390421 : scalar_int_mode imode = (op_mode == VOIDmode
2094 : 3390421 : ? result_mode
2095 : 3388927 : : as_a <scalar_int_mode> (op_mode));
2096 : 3390421 : rtx_mode_t op0 = rtx_mode_t (op, imode);
2097 : 3390421 : int int_value;
2098 : :
2099 : : #if TARGET_SUPPORTS_WIDE_INT == 0
2100 : : /* This assert keeps the simplification from producing a result
2101 : : that cannot be represented in a CONST_DOUBLE but a lot of
2102 : : upstream callers expect that this function never fails to
2103 : : simplify something and so you if you added this to the test
2104 : : above the code would die later anyway. If this assert
2105 : : happens, you just need to make the port support wide int. */
2106 : : gcc_assert (width <= HOST_BITS_PER_DOUBLE_INT);
2107 : : #endif
2108 : :
2109 : 3390421 : switch (code)
2110 : : {
2111 : 174439 : case NOT:
2112 : 174439 : result = wi::bit_not (op0);
2113 : 174439 : break;
2114 : :
2115 : 1883063 : case NEG:
2116 : 1883063 : result = wi::neg (op0);
2117 : 1883063 : break;
2118 : :
2119 : 6783 : case ABS:
2120 : 6783 : result = wi::abs (op0);
2121 : 6783 : break;
2122 : :
2123 : 0 : case FFS:
2124 : 0 : result = wi::shwi (wi::ffs (op0), result_mode);
2125 : 0 : break;
2126 : :
2127 : 211 : case CLZ:
2128 : 211 : if (wi::ne_p (op0, 0))
2129 : 20 : int_value = wi::clz (op0);
2130 : 382 : else if (! CLZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
2131 : : return NULL_RTX;
2132 : 20 : result = wi::shwi (int_value, result_mode);
2133 : 20 : break;
2134 : :
2135 : 0 : case CLRSB:
2136 : 0 : result = wi::shwi (wi::clrsb (op0), result_mode);
2137 : 0 : break;
2138 : :
2139 : 0 : case CTZ:
2140 : 0 : if (wi::ne_p (op0, 0))
2141 : 0 : int_value = wi::ctz (op0);
2142 : 0 : else if (! CTZ_DEFINED_VALUE_AT_ZERO (imode, int_value))
2143 : : return NULL_RTX;
2144 : 0 : result = wi::shwi (int_value, result_mode);
2145 : 0 : break;
2146 : :
2147 : 171 : case POPCOUNT:
2148 : 171 : result = wi::shwi (wi::popcount (op0), result_mode);
2149 : 171 : break;
2150 : :
2151 : 0 : case PARITY:
2152 : 0 : result = wi::shwi (wi::parity (op0), result_mode);
2153 : 0 : break;
2154 : :
2155 : 2020 : case BSWAP:
2156 : 2020 : result = wi::bswap (op0);
2157 : 2020 : break;
2158 : :
2159 : 0 : case BITREVERSE:
2160 : 0 : result = wi::bitreverse (op0);
2161 : 0 : break;
2162 : :
2163 : 1159369 : case TRUNCATE:
2164 : 1159369 : case ZERO_EXTEND:
2165 : 1159369 : result = wide_int::from (op0, width, UNSIGNED);
2166 : 1159369 : break;
2167 : :
2168 : 14254 : case US_TRUNCATE:
2169 : 14254 : case SS_TRUNCATE:
2170 : 14254 : {
2171 : 14254 : signop sgn = code == US_TRUNCATE ? UNSIGNED : SIGNED;
2172 : 14254 : wide_int nmax
2173 : 14254 : = wide_int::from (wi::max_value (width, sgn),
2174 : 28508 : GET_MODE_PRECISION (imode), sgn);
2175 : 14254 : wide_int nmin
2176 : 14254 : = wide_int::from (wi::min_value (width, sgn),
2177 : 28508 : GET_MODE_PRECISION (imode), sgn);
2178 : 14254 : result = wi::min (wi::max (op0, nmin, sgn), nmax, sgn);
2179 : 14254 : result = wide_int::from (result, width, sgn);
2180 : 14254 : break;
2181 : 14254 : }
2182 : 150111 : case SIGN_EXTEND:
2183 : 150111 : result = wide_int::from (op0, width, SIGNED);
2184 : 150111 : break;
2185 : :
2186 : 0 : case SS_NEG:
2187 : 0 : if (wi::only_sign_bit_p (op0))
2188 : 0 : result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED);
2189 : : else
2190 : 0 : result = wi::neg (op0);
2191 : : break;
2192 : :
2193 : 0 : case SS_ABS:
2194 : 0 : if (wi::only_sign_bit_p (op0))
2195 : 0 : result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED);
2196 : : else
2197 : 0 : result = wi::abs (op0);
2198 : : break;
2199 : :
2200 : : case SQRT:
2201 : : default:
2202 : : return 0;
2203 : : }
2204 : :
2205 : 3390230 : return immed_wide_int_const (result, result_mode);
2206 : 3390421 : }
2207 : :
2208 : 23664952 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2209 : 486031 : && SCALAR_FLOAT_MODE_P (mode)
2210 : 484035 : && SCALAR_FLOAT_MODE_P (GET_MODE (op)))
2211 : : {
2212 : 484035 : REAL_VALUE_TYPE d = *CONST_DOUBLE_REAL_VALUE (op);
2213 : 484035 : switch (code)
2214 : : {
2215 : : case SQRT:
2216 : : return 0;
2217 : 23068 : case ABS:
2218 : 23068 : d = real_value_abs (&d);
2219 : 23068 : break;
2220 : 15701 : case NEG:
2221 : 15701 : d = real_value_negate (&d);
2222 : 15701 : break;
2223 : 925 : case FLOAT_TRUNCATE:
2224 : : /* Don't perform the operation if flag_signaling_nans is on
2225 : : and the operand is a signaling NaN. */
2226 : 925 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2227 : : return NULL_RTX;
2228 : : /* Or if flag_rounding_math is on and the truncation is not
2229 : : exact. */
2230 : 925 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2231 : 925 : && !exact_real_truncate (mode, &d))
2232 : 231 : return NULL_RTX;
2233 : 694 : d = real_value_truncate (mode, d);
2234 : 694 : break;
2235 : 395660 : case FLOAT_EXTEND:
2236 : : /* Don't perform the operation if flag_signaling_nans is on
2237 : : and the operand is a signaling NaN. */
2238 : 395660 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2239 : : return NULL_RTX;
2240 : : /* All this does is change the mode, unless changing
2241 : : mode class. */
2242 : 395658 : if (GET_MODE_CLASS (mode) != GET_MODE_CLASS (GET_MODE (op)))
2243 : 0 : real_convert (&d, mode, &d);
2244 : : break;
2245 : 0 : case FIX:
2246 : : /* Don't perform the operation if flag_signaling_nans is on
2247 : : and the operand is a signaling NaN. */
2248 : 0 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2249 : : return NULL_RTX;
2250 : 0 : real_arithmetic (&d, FIX_TRUNC_EXPR, &d, NULL);
2251 : 0 : break;
2252 : 48079 : case NOT:
2253 : 48079 : {
2254 : 48079 : long tmp[4];
2255 : 48079 : int i;
2256 : :
2257 : 48079 : real_to_target (tmp, &d, GET_MODE (op));
2258 : 240395 : for (i = 0; i < 4; i++)
2259 : 192316 : tmp[i] = ~tmp[i];
2260 : 48079 : real_from_target (&d, tmp, mode);
2261 : 48079 : break;
2262 : : }
2263 : 0 : default:
2264 : 0 : gcc_unreachable ();
2265 : : }
2266 : 483200 : return const_double_from_real_value (d, mode);
2267 : : }
2268 : 1996 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2269 : 1996 : && SCALAR_FLOAT_MODE_P (GET_MODE (op))
2270 : 23182913 : && is_int_mode (mode, &result_mode))
2271 : : {
2272 : 1996 : unsigned int width = GET_MODE_PRECISION (result_mode);
2273 : 1996 : if (width > MAX_BITSIZE_MODE_ANY_INT)
2274 : : return 0;
2275 : :
2276 : : /* Although the overflow semantics of RTL's FIX and UNSIGNED_FIX
2277 : : operators are intentionally left unspecified (to ease implementation
2278 : : by target backends), for consistency, this routine implements the
2279 : : same semantics for constant folding as used by the middle-end. */
2280 : :
2281 : : /* This was formerly used only for non-IEEE float.
2282 : : eggert@twinsun.com says it is safe for IEEE also. */
2283 : 1996 : REAL_VALUE_TYPE t;
2284 : 1996 : const REAL_VALUE_TYPE *x = CONST_DOUBLE_REAL_VALUE (op);
2285 : 1996 : wide_int wmax, wmin;
2286 : : /* This is part of the abi to real_to_integer, but we check
2287 : : things before making this call. */
2288 : 1996 : bool fail;
2289 : :
2290 : 1996 : switch (code)
2291 : : {
2292 : 1988 : case FIX:
2293 : : /* According to IEEE standard, for conversions from floating point to
2294 : : integer. When a NaN or infinite operand cannot be represented in
2295 : : the destination format and this cannot otherwise be indicated, the
2296 : : invalid operation exception shall be signaled. When a numeric
2297 : : operand would convert to an integer outside the range of the
2298 : : destination format, the invalid operation exception shall be
2299 : : signaled if this situation cannot otherwise be indicated. */
2300 : 1988 : if (REAL_VALUE_ISNAN (*x))
2301 : 941 : return flag_trapping_math ? NULL_RTX : const0_rtx;
2302 : :
2303 : 1047 : if (REAL_VALUE_ISINF (*x) && flag_trapping_math)
2304 : : return NULL_RTX;
2305 : :
2306 : : /* Test against the signed upper bound. */
2307 : 101 : wmax = wi::max_value (width, SIGNED);
2308 : 101 : real_from_integer (&t, VOIDmode, wmax, SIGNED);
2309 : 101 : if (real_less (&t, x))
2310 : 3 : return (flag_trapping_math
2311 : 3 : ? NULL_RTX : immed_wide_int_const (wmax, mode));
2312 : :
2313 : : /* Test against the signed lower bound. */
2314 : 98 : wmin = wi::min_value (width, SIGNED);
2315 : 98 : real_from_integer (&t, VOIDmode, wmin, SIGNED);
2316 : 98 : if (real_less (x, &t))
2317 : 8 : return immed_wide_int_const (wmin, mode);
2318 : :
2319 : 90 : return immed_wide_int_const (real_to_integer (x, &fail, width),
2320 : : mode);
2321 : :
2322 : 8 : case UNSIGNED_FIX:
2323 : 8 : if (REAL_VALUE_ISNAN (*x) || REAL_VALUE_NEGATIVE (*x))
2324 : 6 : return flag_trapping_math ? NULL_RTX : const0_rtx;
2325 : :
2326 : 2 : if (REAL_VALUE_ISINF (*x) && flag_trapping_math)
2327 : : return NULL_RTX;
2328 : :
2329 : : /* Test against the unsigned upper bound. */
2330 : 0 : wmax = wi::max_value (width, UNSIGNED);
2331 : 0 : real_from_integer (&t, VOIDmode, wmax, UNSIGNED);
2332 : 0 : if (real_less (&t, x))
2333 : 0 : return (flag_trapping_math
2334 : 0 : ? NULL_RTX : immed_wide_int_const (wmax, mode));
2335 : :
2336 : 0 : return immed_wide_int_const (real_to_integer (x, &fail, width),
2337 : : mode);
2338 : :
2339 : 0 : default:
2340 : 0 : gcc_unreachable ();
2341 : : }
2342 : 1996 : }
2343 : :
2344 : : /* Handle polynomial integers. */
2345 : : else if (CONST_POLY_INT_P (op))
2346 : : {
2347 : : poly_wide_int result;
2348 : : switch (code)
2349 : : {
2350 : : case NEG:
2351 : : result = -const_poly_int_value (op);
2352 : : break;
2353 : :
2354 : : case NOT:
2355 : : result = ~const_poly_int_value (op);
2356 : : break;
2357 : :
2358 : : default:
2359 : : return NULL_RTX;
2360 : : }
2361 : : return immed_wide_int_const (result, mode);
2362 : : }
2363 : :
2364 : : return NULL_RTX;
2365 : : }
2366 : :
2367 : : /* Subroutine of simplify_binary_operation to simplify a binary operation
2368 : : CODE that can commute with byte swapping, with result mode MODE and
2369 : : operating on OP0 and OP1. CODE is currently one of AND, IOR or XOR.
2370 : : Return zero if no simplification or canonicalization is possible. */
2371 : :
2372 : : rtx
2373 : 39718944 : simplify_context::simplify_byte_swapping_operation (rtx_code code,
2374 : : machine_mode mode,
2375 : : rtx op0, rtx op1)
2376 : : {
2377 : 39718944 : rtx tem;
2378 : :
2379 : : /* (op (bswap x) C1)) -> (bswap (op x C2)) with C2 swapped. */
2380 : 39718944 : if (GET_CODE (op0) == BSWAP && CONST_SCALAR_INT_P (op1))
2381 : : {
2382 : 509 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0),
2383 : : simplify_gen_unary (BSWAP, mode, op1, mode));
2384 : 509 : return simplify_gen_unary (BSWAP, mode, tem, mode);
2385 : : }
2386 : :
2387 : : /* (op (bswap x) (bswap y)) -> (bswap (op x y)). */
2388 : 39718435 : if (GET_CODE (op0) == BSWAP && GET_CODE (op1) == BSWAP)
2389 : : {
2390 : 0 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0), XEXP (op1, 0));
2391 : 0 : return simplify_gen_unary (BSWAP, mode, tem, mode);
2392 : : }
2393 : :
2394 : : return NULL_RTX;
2395 : : }
2396 : :
2397 : : /* Subroutine of simplify_binary_operation to simplify a commutative,
2398 : : associative binary operation CODE with result mode MODE, operating
2399 : : on OP0 and OP1. CODE is currently one of PLUS, MULT, AND, IOR, XOR,
2400 : : SMIN, SMAX, UMIN or UMAX. Return zero if no simplification or
2401 : : canonicalization is possible. */
2402 : :
2403 : : rtx
2404 : 50168563 : simplify_context::simplify_associative_operation (rtx_code code,
2405 : : machine_mode mode,
2406 : : rtx op0, rtx op1)
2407 : : {
2408 : 50168563 : rtx tem;
2409 : :
2410 : : /* Normally expressions simplified by simplify-rtx.cc are combined
2411 : : at most from a few machine instructions and therefore the
2412 : : expressions should be fairly small. During var-tracking
2413 : : we can see arbitrarily large expressions though and reassociating
2414 : : those can be quadratic, so punt after encountering max_assoc_count
2415 : : simplify_associative_operation calls during outermost simplify_*
2416 : : call. */
2417 : 50168563 : if (++assoc_count >= max_assoc_count)
2418 : : return NULL_RTX;
2419 : :
2420 : : /* Linearize the operator to the left. */
2421 : 50167969 : if (GET_CODE (op1) == code)
2422 : : {
2423 : : /* "(a op b) op (c op d)" becomes "((a op b) op c) op d)". */
2424 : 26363 : if (GET_CODE (op0) == code)
2425 : : {
2426 : 5675 : tem = simplify_gen_binary (code, mode, op0, XEXP (op1, 0));
2427 : 5675 : return simplify_gen_binary (code, mode, tem, XEXP (op1, 1));
2428 : : }
2429 : :
2430 : : /* "a op (b op c)" becomes "(b op c) op a". */
2431 : 20688 : if (! swap_commutative_operands_p (op1, op0))
2432 : 20688 : return simplify_gen_binary (code, mode, op1, op0);
2433 : :
2434 : : std::swap (op0, op1);
2435 : : }
2436 : :
2437 : 50141606 : if (GET_CODE (op0) == code)
2438 : : {
2439 : : /* Canonicalize "(x op c) op y" as "(x op y) op c". */
2440 : 1512162 : if (swap_commutative_operands_p (XEXP (op0, 1), op1))
2441 : : {
2442 : 290026 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0), op1);
2443 : 290026 : return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2444 : : }
2445 : :
2446 : : /* Attempt to simplify "(a op b) op c" as "a op (b op c)". */
2447 : 1222136 : tem = simplify_binary_operation (code, mode, XEXP (op0, 1), op1);
2448 : 1222136 : if (tem != 0)
2449 : 83347 : return simplify_gen_binary (code, mode, XEXP (op0, 0), tem);
2450 : :
2451 : : /* Attempt to simplify "(a op b) op c" as "(a op c) op b". */
2452 : 1138789 : tem = simplify_binary_operation (code, mode, XEXP (op0, 0), op1);
2453 : 1138789 : if (tem != 0)
2454 : 2629 : return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
2455 : : }
2456 : :
2457 : : return 0;
2458 : : }
2459 : :
2460 : : /* If COMPARISON can be treated as an unsigned comparison, return a mask
2461 : : that represents it (8 if it includes <, 4 if it includes > and 2
2462 : : if it includes ==). Return 0 otherwise. */
2463 : : static int
2464 : 18784 : unsigned_comparison_to_mask (rtx_code comparison)
2465 : : {
2466 : 0 : switch (comparison)
2467 : : {
2468 : : case LTU:
2469 : : return 8;
2470 : : case GTU:
2471 : : return 4;
2472 : : case EQ:
2473 : : return 2;
2474 : :
2475 : : case LEU:
2476 : : return 10;
2477 : : case GEU:
2478 : : return 6;
2479 : :
2480 : : case NE:
2481 : : return 12;
2482 : :
2483 : : default:
2484 : : return 0;
2485 : : }
2486 : : }
2487 : :
2488 : : /* Reverse the mapping in unsigned_comparison_to_mask, going from masks
2489 : : to comparisons. */
2490 : : static rtx_code
2491 : 6555 : mask_to_unsigned_comparison (int mask)
2492 : : {
2493 : 6555 : switch (mask)
2494 : : {
2495 : : case 8:
2496 : : return LTU;
2497 : 160 : case 4:
2498 : 160 : return GTU;
2499 : 2522 : case 2:
2500 : 2522 : return EQ;
2501 : :
2502 : 160 : case 10:
2503 : 160 : return LEU;
2504 : 160 : case 6:
2505 : 160 : return GEU;
2506 : :
2507 : 3393 : case 12:
2508 : 3393 : return NE;
2509 : :
2510 : 0 : default:
2511 : 0 : gcc_unreachable ();
2512 : : }
2513 : : }
2514 : :
2515 : : /* Return a mask describing the COMPARISON. */
2516 : : static int
2517 : 2666 : comparison_to_mask (enum rtx_code comparison)
2518 : : {
2519 : 2666 : switch (comparison)
2520 : : {
2521 : : case LT:
2522 : : return 8;
2523 : 472 : case GT:
2524 : 472 : return 4;
2525 : 419 : case EQ:
2526 : 419 : return 2;
2527 : 19 : case UNORDERED:
2528 : 19 : return 1;
2529 : :
2530 : 0 : case LTGT:
2531 : 0 : return 12;
2532 : 441 : case LE:
2533 : 441 : return 10;
2534 : 441 : case GE:
2535 : 441 : return 6;
2536 : 0 : case UNLT:
2537 : 0 : return 9;
2538 : 0 : case UNGT:
2539 : 0 : return 5;
2540 : 0 : case UNEQ:
2541 : 0 : return 3;
2542 : :
2543 : 0 : case ORDERED:
2544 : 0 : return 14;
2545 : 400 : case NE:
2546 : 400 : return 13;
2547 : 0 : case UNLE:
2548 : 0 : return 11;
2549 : 0 : case UNGE:
2550 : 0 : return 7;
2551 : :
2552 : 0 : default:
2553 : 0 : gcc_unreachable ();
2554 : : }
2555 : : }
2556 : :
2557 : : /* Return a comparison corresponding to the MASK. */
2558 : : static enum rtx_code
2559 : 1014 : mask_to_comparison (int mask)
2560 : : {
2561 : 1014 : switch (mask)
2562 : : {
2563 : : case 8:
2564 : : return LT;
2565 : : case 4:
2566 : : return GT;
2567 : : case 2:
2568 : : return EQ;
2569 : : case 1:
2570 : : return UNORDERED;
2571 : :
2572 : : case 12:
2573 : : return LTGT;
2574 : : case 10:
2575 : : return LE;
2576 : : case 6:
2577 : : return GE;
2578 : : case 9:
2579 : : return UNLT;
2580 : : case 5:
2581 : : return UNGT;
2582 : : case 3:
2583 : : return UNEQ;
2584 : :
2585 : : case 14:
2586 : : return ORDERED;
2587 : : case 13:
2588 : : return NE;
2589 : : case 11:
2590 : : return UNLE;
2591 : : case 7:
2592 : : return UNGE;
2593 : :
2594 : 0 : default:
2595 : 0 : gcc_unreachable ();
2596 : : }
2597 : : }
2598 : :
2599 : : /* Canonicalize RES, a scalar const0_rtx/const_true_rtx to the right
2600 : : false/true value of comparison with MODE where comparison operands
2601 : : have CMP_MODE. */
2602 : :
2603 : : static rtx
2604 : 818857 : relational_result (machine_mode mode, machine_mode cmp_mode, rtx res)
2605 : : {
2606 : 818857 : if (SCALAR_FLOAT_MODE_P (mode))
2607 : : {
2608 : 202 : if (res == const0_rtx)
2609 : 198 : return CONST0_RTX (mode);
2610 : : #ifdef FLOAT_STORE_FLAG_VALUE
2611 : : REAL_VALUE_TYPE val = FLOAT_STORE_FLAG_VALUE (mode);
2612 : : return const_double_from_real_value (val, mode);
2613 : : #else
2614 : : return NULL_RTX;
2615 : : #endif
2616 : : }
2617 : 818655 : if (VECTOR_MODE_P (mode))
2618 : : {
2619 : 347 : if (res == const0_rtx)
2620 : 50 : return CONST0_RTX (mode);
2621 : : #ifdef VECTOR_STORE_FLAG_VALUE
2622 : 297 : rtx val = VECTOR_STORE_FLAG_VALUE (mode);
2623 : 297 : if (val == NULL_RTX)
2624 : : return NULL_RTX;
2625 : 297 : if (val == const1_rtx)
2626 : 0 : return CONST1_RTX (mode);
2627 : :
2628 : 297 : return gen_const_vec_duplicate (mode, val);
2629 : : #else
2630 : : return NULL_RTX;
2631 : : #endif
2632 : : }
2633 : : /* For vector comparison with scalar int result, it is unknown
2634 : : if the target means here a comparison into an integral bitmask,
2635 : : or comparison where all comparisons true mean const_true_rtx
2636 : : whole result, or where any comparisons true mean const_true_rtx
2637 : : whole result. For const0_rtx all the cases are the same. */
2638 : 818308 : if (VECTOR_MODE_P (cmp_mode)
2639 : 0 : && SCALAR_INT_MODE_P (mode)
2640 : 0 : && res == const_true_rtx)
2641 : 0 : return NULL_RTX;
2642 : :
2643 : : return res;
2644 : : }
2645 : :
2646 : : /* Simplify a logical operation CODE with result mode MODE, operating on OP0
2647 : : and OP1, in the case where both are relational operations. Assume that
2648 : : OP0 is inverted if INVERT0_P is true.
2649 : :
2650 : : Return 0 if no such simplification is possible. */
2651 : : rtx
2652 : 15267563 : simplify_context::simplify_logical_relational_operation (rtx_code code,
2653 : : machine_mode mode,
2654 : : rtx op0, rtx op1,
2655 : : bool invert0_p)
2656 : : {
2657 : 15267563 : if (!(COMPARISON_P (op0) && COMPARISON_P (op1)))
2658 : : return 0;
2659 : :
2660 : 21866 : if (!(rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2661 : 9887 : && rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))))
2662 : 2587 : return 0;
2663 : :
2664 : 9392 : if (side_effects_p (op0))
2665 : : return 0;
2666 : :
2667 : 9392 : enum rtx_code code0 = GET_CODE (op0);
2668 : 9392 : enum rtx_code code1 = GET_CODE (op1);
2669 : 9392 : machine_mode cmp_mode = GET_MODE (XEXP (op0, 0));
2670 : 9392 : if (cmp_mode == VOIDmode)
2671 : 0 : cmp_mode = GET_MODE (XEXP (op0, 1));
2672 : :
2673 : : /* Assume at first that the comparisons are on integers, and that the
2674 : : operands are therefore ordered. */
2675 : 9392 : int all = 14;
2676 : 9392 : int mask0 = unsigned_comparison_to_mask (code0);
2677 : 9392 : int mask1 = unsigned_comparison_to_mask (code1);
2678 : 18784 : bool unsigned_p = (IN_RANGE (mask0 & 12, 4, 8)
2679 : 9392 : || IN_RANGE (mask1 & 12, 4, 8));
2680 : 1333 : if (unsigned_p)
2681 : : {
2682 : : /* We only reach here when comparing integers. Reject mixtures of signed
2683 : : and unsigned comparisons. */
2684 : 8059 : if (mask0 == 0 || mask1 == 0)
2685 : : return 0;
2686 : : }
2687 : : else
2688 : : {
2689 : : /* See whether the operands might be unordered. Assume that all
2690 : : results are possible for CC modes, and punt later if we don't get an
2691 : : always-true or always-false answer. */
2692 : 1333 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC || HONOR_NANS (cmp_mode))
2693 : : all = 15;
2694 : 1333 : mask0 = comparison_to_mask (code0) & all;
2695 : 1333 : mask1 = comparison_to_mask (code1) & all;
2696 : : }
2697 : :
2698 : 8112 : if (invert0_p)
2699 : 4590 : mask0 = mask0 ^ all;
2700 : :
2701 : 8112 : int mask;
2702 : 8112 : if (code == AND)
2703 : 960 : mask = mask0 & mask1;
2704 : 7152 : else if (code == IOR)
2705 : 948 : mask = mask0 | mask1;
2706 : 6204 : else if (code == XOR)
2707 : 6204 : mask = mask0 ^ mask1;
2708 : : else
2709 : : return 0;
2710 : :
2711 : 8112 : if (mask == all)
2712 : 232 : return relational_result (mode, GET_MODE (op0), const_true_rtx);
2713 : :
2714 : 7880 : if (mask == 0)
2715 : 232 : return relational_result (mode, GET_MODE (op0), const0_rtx);
2716 : :
2717 : 7648 : if (unsigned_p)
2718 : 6555 : code = mask_to_unsigned_comparison (mask);
2719 : : else
2720 : : {
2721 : 1093 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC)
2722 : : return 0;
2723 : :
2724 : 1014 : code = mask_to_comparison (mask);
2725 : : /* LTGT and NE are arithmetically equivalent for ordered operands,
2726 : : with NE being the canonical choice. */
2727 : 1014 : if (code == LTGT && all == 14)
2728 : 184 : code = NE;
2729 : : }
2730 : :
2731 : 7569 : op0 = XEXP (op1, 0);
2732 : 7569 : op1 = XEXP (op1, 1);
2733 : :
2734 : 7569 : return simplify_gen_relational (code, mode, VOIDmode, op0, op1);
2735 : : }
2736 : :
2737 : : /* Simplify a binary operation CODE with result mode MODE, operating on OP0
2738 : : and OP1. Return 0 if no simplification is possible.
2739 : :
2740 : : Don't use this for relational operations such as EQ or LT.
2741 : : Use simplify_relational_operation instead. */
2742 : : rtx
2743 : 460666852 : simplify_context::simplify_binary_operation (rtx_code code, machine_mode mode,
2744 : : rtx op0, rtx op1)
2745 : : {
2746 : 460666852 : rtx trueop0, trueop1;
2747 : 460666852 : rtx tem;
2748 : :
2749 : : /* Relational operations don't work here. We must know the mode
2750 : : of the operands in order to do the comparison correctly.
2751 : : Assuming a full word can give incorrect results.
2752 : : Consider comparing 128 with -128 in QImode. */
2753 : 460666852 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMPARE);
2754 : 460666852 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMM_COMPARE);
2755 : :
2756 : : /* Make sure the constant is second. */
2757 : 460666852 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
2758 : 460666852 : && swap_commutative_operands_p (op0, op1))
2759 : : std::swap (op0, op1);
2760 : :
2761 : 460666852 : trueop0 = avoid_constant_pool_reference (op0);
2762 : 460666852 : trueop1 = avoid_constant_pool_reference (op1);
2763 : :
2764 : 460666852 : tem = simplify_const_binary_operation (code, mode, trueop0, trueop1);
2765 : 460666852 : if (tem)
2766 : : return tem;
2767 : 431194689 : tem = simplify_binary_operation_1 (code, mode, op0, op1, trueop0, trueop1);
2768 : :
2769 : 431194689 : if (tem)
2770 : : return tem;
2771 : :
2772 : : /* If the above steps did not result in a simplification and op0 or op1
2773 : : were constant pool references, use the referenced constants directly. */
2774 : 370944868 : if (trueop0 != op0 || trueop1 != op1)
2775 : 574193 : return simplify_gen_binary (code, mode, trueop0, trueop1);
2776 : :
2777 : : return NULL_RTX;
2778 : : }
2779 : :
2780 : : /* Subroutine of simplify_binary_operation_1 that looks for cases in
2781 : : which OP0 and OP1 are both vector series or vector duplicates
2782 : : (which are really just series with a step of 0). If so, try to
2783 : : form a new series by applying CODE to the bases and to the steps.
2784 : : Return null if no simplification is possible.
2785 : :
2786 : : MODE is the mode of the operation and is known to be a vector
2787 : : integer mode. */
2788 : :
2789 : : rtx
2790 : 2181823 : simplify_context::simplify_binary_operation_series (rtx_code code,
2791 : : machine_mode mode,
2792 : : rtx op0, rtx op1)
2793 : : {
2794 : 2181823 : rtx base0, step0;
2795 : 2181823 : if (vec_duplicate_p (op0, &base0))
2796 : 66082 : step0 = const0_rtx;
2797 : 2115741 : else if (!vec_series_p (op0, &base0, &step0))
2798 : : return NULL_RTX;
2799 : :
2800 : 66342 : rtx base1, step1;
2801 : 66342 : if (vec_duplicate_p (op1, &base1))
2802 : 358 : step1 = const0_rtx;
2803 : 65984 : else if (!vec_series_p (op1, &base1, &step1))
2804 : : return NULL_RTX;
2805 : :
2806 : : /* Only create a new series if we can simplify both parts. In other
2807 : : cases this isn't really a simplification, and it's not necessarily
2808 : : a win to replace a vector operation with a scalar operation. */
2809 : 3202 : scalar_mode inner_mode = GET_MODE_INNER (mode);
2810 : 3202 : rtx new_base = simplify_binary_operation (code, inner_mode, base0, base1);
2811 : 3202 : if (!new_base)
2812 : : return NULL_RTX;
2813 : :
2814 : 2973 : rtx new_step = simplify_binary_operation (code, inner_mode, step0, step1);
2815 : 2973 : if (!new_step)
2816 : : return NULL_RTX;
2817 : :
2818 : 2973 : return gen_vec_series (mode, new_base, new_step);
2819 : : }
2820 : :
2821 : : /* Subroutine of simplify_binary_operation_1. Un-distribute a binary
2822 : : operation CODE with result mode MODE, operating on OP0 and OP1.
2823 : : e.g. simplify (xor (and A C) (and (B C)) to (and (xor (A B) C).
2824 : : Returns NULL_RTX if no simplification is possible. */
2825 : :
2826 : : rtx
2827 : 1521225 : simplify_context::simplify_distributive_operation (rtx_code code,
2828 : : machine_mode mode,
2829 : : rtx op0, rtx op1)
2830 : : {
2831 : 1521225 : enum rtx_code op = GET_CODE (op0);
2832 : 1521225 : gcc_assert (GET_CODE (op1) == op);
2833 : :
2834 : 1521225 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))
2835 : 1521225 : && ! side_effects_p (XEXP (op0, 1)))
2836 : 302592 : return simplify_gen_binary (op, mode,
2837 : : simplify_gen_binary (code, mode,
2838 : : XEXP (op0, 0),
2839 : : XEXP (op1, 0)),
2840 : 302592 : XEXP (op0, 1));
2841 : :
2842 : 1218633 : if (GET_RTX_CLASS (op) == RTX_COMM_ARITH)
2843 : : {
2844 : 1185982 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2845 : 1185982 : && ! side_effects_p (XEXP (op0, 0)))
2846 : 548525 : return simplify_gen_binary (op, mode,
2847 : : simplify_gen_binary (code, mode,
2848 : : XEXP (op0, 1),
2849 : : XEXP (op1, 1)),
2850 : 548525 : XEXP (op0, 0));
2851 : 637457 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 1))
2852 : 637457 : && ! side_effects_p (XEXP (op0, 0)))
2853 : 50 : return simplify_gen_binary (op, mode,
2854 : : simplify_gen_binary (code, mode,
2855 : : XEXP (op0, 1),
2856 : : XEXP (op1, 0)),
2857 : 50 : XEXP (op0, 0));
2858 : 637407 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 0))
2859 : 637407 : && ! side_effects_p (XEXP (op0, 1)))
2860 : 319817 : return simplify_gen_binary (op, mode,
2861 : : simplify_gen_binary (code, mode,
2862 : : XEXP (op0, 0),
2863 : : XEXP (op1, 1)),
2864 : 319817 : XEXP (op0, 1));
2865 : : }
2866 : :
2867 : : return NULL_RTX;
2868 : : }
2869 : :
2870 : : /* Return TRUE if a rotate in mode MODE with a constant count in OP1
2871 : : should be reversed.
2872 : :
2873 : : If the rotate should not be reversed, return FALSE.
2874 : :
2875 : : LEFT indicates if this is a rotate left or a rotate right. */
2876 : :
2877 : : bool
2878 : 137333 : reverse_rotate_by_imm_p (machine_mode mode, unsigned int left, rtx op1)
2879 : : {
2880 : 137333 : if (!CONST_INT_P (op1))
2881 : : return false;
2882 : :
2883 : : /* Some targets may only be able to rotate by a constant
2884 : : in one direction. So we need to query the optab interface
2885 : : to see what is possible. */
2886 : 106228 : optab binoptab = left ? rotl_optab : rotr_optab;
2887 : 44517 : optab re_binoptab = left ? rotr_optab : rotl_optab;
2888 : 106228 : enum insn_code icode = optab_handler (binoptab, mode);
2889 : 106228 : enum insn_code re_icode = optab_handler (re_binoptab, mode);
2890 : :
2891 : : /* If the target can not support the reversed optab, then there
2892 : : is nothing to do. */
2893 : 106228 : if (re_icode == CODE_FOR_nothing)
2894 : : return false;
2895 : :
2896 : : /* If the target does not support the requested rotate-by-immediate,
2897 : : then we want to try reversing the rotate. We also want to try
2898 : : reversing to minimize the count. */
2899 : 103728 : if ((icode == CODE_FOR_nothing)
2900 : 103728 : || (!insn_operand_matches (icode, 2, op1))
2901 : 518640 : || (IN_RANGE (INTVAL (op1),
2902 : : GET_MODE_UNIT_PRECISION (mode) / 2 + left,
2903 : : GET_MODE_UNIT_PRECISION (mode) - 1)))
2904 : 13647 : return (insn_operand_matches (re_icode, 2, op1));
2905 : : return false;
2906 : : }
2907 : :
2908 : : /* Analyse argument X to see if it represents an (ASHIFT X Y) operation
2909 : : and return the expression to be shifted in SHIFT_OPND and the shift amount
2910 : : in SHIFT_AMNT. This is primarily used to group handling of ASHIFT (X, CST)
2911 : : and (PLUS (X, X)) in one place. If the expression is not equivalent to an
2912 : : ASHIFT then return FALSE and set SHIFT_OPND and SHIFT_AMNT to NULL. */
2913 : :
2914 : : static bool
2915 : 503872930 : extract_ashift_operands_p (rtx x, rtx *shift_opnd, rtx *shift_amnt)
2916 : : {
2917 : 503872930 : if (GET_CODE (x) == ASHIFT)
2918 : : {
2919 : 14181881 : *shift_opnd = XEXP (x, 0);
2920 : 14181881 : *shift_amnt = XEXP (x, 1);
2921 : 14181881 : return true;
2922 : : }
2923 : 489691049 : if (GET_CODE (x) == PLUS && rtx_equal_p (XEXP (x, 0), XEXP (x, 1)))
2924 : : {
2925 : 10394 : *shift_opnd = XEXP (x, 0);
2926 : 10394 : *shift_amnt = CONST1_RTX (GET_MODE (x));
2927 : 10394 : return true;
2928 : : }
2929 : 489680655 : *shift_opnd = NULL_RTX;
2930 : 489680655 : *shift_amnt = NULL_RTX;
2931 : 489680655 : return false;
2932 : : }
2933 : :
2934 : : /* OP0 and OP1 are combined under an operation of mode MODE that can
2935 : : potentially result in a ROTATE expression. Analyze the OP0 and OP1
2936 : : and return the resulting ROTATE expression if so. Return NULL otherwise.
2937 : : This is used in detecting the patterns (X << C1) [+,|,^] (X >> C2) where
2938 : : C1 + C2 == GET_MODE_UNIT_PRECISION (mode).
2939 : : (X << C1) and (C >> C2) would be OP0 and OP1. */
2940 : :
2941 : : static rtx
2942 : 254344998 : simplify_rotate_op (rtx op0, rtx op1, machine_mode mode)
2943 : : {
2944 : : /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
2945 : : mode size to (rotate A CX). */
2946 : :
2947 : 254344998 : rtx opleft = op0;
2948 : 254344998 : rtx opright = op1;
2949 : 254344998 : rtx ashift_opnd, ashift_amnt;
2950 : : /* In some cases the ASHIFT is not a direct ASHIFT. Look deeper and extract
2951 : : the relevant operands here. */
2952 : 254344998 : bool ashift_op_p
2953 : 254344998 : = extract_ashift_operands_p (op1, &ashift_opnd, &ashift_amnt);
2954 : :
2955 : 254344998 : if (ashift_op_p
2956 : 252546068 : || GET_CODE (op1) == SUBREG)
2957 : : {
2958 : : opleft = op1;
2959 : : opright = op0;
2960 : : }
2961 : : else
2962 : : {
2963 : 249527932 : opright = op1;
2964 : 249527932 : opleft = op0;
2965 : 249527932 : ashift_op_p
2966 : 249527932 : = extract_ashift_operands_p (opleft, &ashift_opnd, &ashift_amnt);
2967 : : }
2968 : :
2969 : 14192275 : if (ashift_op_p && GET_CODE (opright) == LSHIFTRT
2970 : 252588210 : && rtx_equal_p (ashift_opnd, XEXP (opright, 0)))
2971 : : {
2972 : 9963 : rtx leftcst = unwrap_const_vec_duplicate (ashift_amnt);
2973 : 9963 : rtx rightcst = unwrap_const_vec_duplicate (XEXP (opright, 1));
2974 : :
2975 : 6133 : if (CONST_INT_P (leftcst) && CONST_INT_P (rightcst)
2976 : 16096 : && (INTVAL (leftcst) + INTVAL (rightcst)
2977 : 6133 : == GET_MODE_UNIT_PRECISION (mode)))
2978 : 5528 : return gen_rtx_ROTATE (mode, XEXP (opright, 0), ashift_amnt);
2979 : : }
2980 : :
2981 : : /* Same, but for ashift that has been "simplified" to a wider mode
2982 : : by simplify_shift_const. */
2983 : 254339470 : scalar_int_mode int_mode, inner_mode;
2984 : :
2985 : 254339470 : if (GET_CODE (opleft) == SUBREG
2986 : 258970925 : && is_a <scalar_int_mode> (mode, &int_mode)
2987 : 4625927 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (opleft)),
2988 : : &inner_mode)
2989 : 4599249 : && GET_CODE (SUBREG_REG (opleft)) == ASHIFT
2990 : 251252 : && GET_CODE (opright) == LSHIFTRT
2991 : 1178 : && GET_CODE (XEXP (opright, 0)) == SUBREG
2992 : 141 : && known_eq (SUBREG_BYTE (opleft), SUBREG_BYTE (XEXP (opright, 0)))
2993 : 278 : && GET_MODE_SIZE (int_mode) < GET_MODE_SIZE (inner_mode)
2994 : 125 : && rtx_equal_p (XEXP (SUBREG_REG (opleft), 0),
2995 : 125 : SUBREG_REG (XEXP (opright, 0)))
2996 : 19 : && CONST_INT_P (XEXP (SUBREG_REG (opleft), 1))
2997 : 19 : && CONST_INT_P (XEXP (opright, 1))
2998 : 254339470 : && (INTVAL (XEXP (SUBREG_REG (opleft), 1))
2999 : 19 : + INTVAL (XEXP (opright, 1))
3000 : 19 : == GET_MODE_PRECISION (int_mode)))
3001 : 15 : return gen_rtx_ROTATE (int_mode, XEXP (opright, 0),
3002 : : XEXP (SUBREG_REG (opleft), 1));
3003 : : return NULL_RTX;
3004 : : }
3005 : :
3006 : : /* Returns true if OP0 and OP1 match the pattern (OP (plus (A - 1)) (neg A)),
3007 : : and the pattern can be simplified (there are no side effects). */
3008 : :
3009 : : static bool
3010 : 41807224 : match_plus_neg_pattern (rtx op0, rtx op1, machine_mode mode)
3011 : : {
3012 : : /* Remove SUBREG from OP0 and OP1, if needed. */
3013 : 41807224 : if (GET_CODE (op0) == SUBREG
3014 : 7508181 : && GET_CODE (op1) == SUBREG
3015 : 274489 : && subreg_lowpart_p (op0)
3016 : 42080212 : && subreg_lowpart_p (op1))
3017 : : {
3018 : 272980 : op0 = XEXP (op0, 0);
3019 : 272980 : op1 = XEXP (op1, 0);
3020 : : }
3021 : :
3022 : : /* Check for the pattern (OP (plus (A - 1)) (neg A)). */
3023 : 41807224 : if (((GET_CODE (op1) == NEG
3024 : 3573 : && GET_CODE (op0) == PLUS
3025 : 2178 : && XEXP (op0, 1) == CONSTM1_RTX (mode))
3026 : 41806571 : || (GET_CODE (op0) == NEG
3027 : 74322 : && GET_CODE (op1) == PLUS
3028 : 0 : && XEXP (op1, 1) == CONSTM1_RTX (mode)))
3029 : 653 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3030 : 41807226 : && !side_effects_p (XEXP (op0, 0)))
3031 : : return true;
3032 : : return false;
3033 : : }
3034 : :
3035 : : /* Check if OP matches the pattern of (subreg (not X)) and the subreg is
3036 : : non-paradoxical. */
3037 : :
3038 : : static bool
3039 : 79440505 : non_paradoxical_subreg_not_p (rtx op)
3040 : : {
3041 : 79440505 : return GET_CODE (op) == SUBREG
3042 : 8901664 : && !paradoxical_subreg_p (op)
3043 : 82225995 : && GET_CODE (SUBREG_REG (op)) == NOT;
3044 : : }
3045 : :
3046 : : /* Convert (binop (subreg (not X)) Y) into (binop (not (subreg X)) Y), or
3047 : : (binop X (subreg (not Y))) into (binop X (not (subreg Y))) to expose
3048 : : opportunities to combine another binary logical operation with NOT. */
3049 : :
3050 : : static rtx
3051 : 39721589 : simplify_with_subreg_not (rtx_code binop, machine_mode mode, rtx op0, rtx op1)
3052 : : {
3053 : 39721589 : rtx opn = NULL_RTX;
3054 : 39721589 : if (non_paradoxical_subreg_not_p (op0))
3055 : : opn = op0;
3056 : 39718916 : else if (non_paradoxical_subreg_not_p (op1))
3057 : : opn = op1;
3058 : :
3059 : 2676 : if (opn == NULL_RTX)
3060 : : return NULL_RTX;
3061 : :
3062 : 5352 : rtx new_subreg = simplify_gen_subreg (mode,
3063 : : XEXP (SUBREG_REG (opn), 0),
3064 : 2676 : GET_MODE (SUBREG_REG (opn)),
3065 : 2676 : SUBREG_BYTE (opn));
3066 : :
3067 : 2676 : if (!new_subreg)
3068 : : return NULL_RTX;
3069 : :
3070 : 2645 : rtx new_not = simplify_gen_unary (NOT, mode, new_subreg, mode);
3071 : 2645 : if (opn == op0)
3072 : 2642 : return simplify_gen_binary (binop, mode, new_not, op1);
3073 : : else
3074 : 3 : return simplify_gen_binary (binop, mode, op0, new_not);
3075 : : }
3076 : :
3077 : : /* Subroutine of simplify_binary_operation. Simplify a binary operation
3078 : : CODE with result mode MODE, operating on OP0 and OP1. If OP0 and/or
3079 : : OP1 are constant pool references, TRUEOP0 and TRUEOP1 represent the
3080 : : actual constants. */
3081 : :
3082 : : rtx
3083 : 431194689 : simplify_context::simplify_binary_operation_1 (rtx_code code,
3084 : : machine_mode mode,
3085 : : rtx op0, rtx op1,
3086 : : rtx trueop0, rtx trueop1)
3087 : : {
3088 : 431194689 : rtx tem, reversed, elt0, elt1;
3089 : 431194689 : HOST_WIDE_INT val;
3090 : 431194689 : scalar_int_mode int_mode, inner_mode;
3091 : 431194689 : poly_int64 offset;
3092 : :
3093 : : /* Even if we can't compute a constant result,
3094 : : there are some cases worth simplifying. */
3095 : :
3096 : 431194689 : switch (code)
3097 : : {
3098 : 241811647 : case PLUS:
3099 : : /* Maybe simplify x + 0 to x. The two expressions are equivalent
3100 : : when x is NaN, infinite, or finite and nonzero. They aren't
3101 : : when x is -0 and the rounding mode is not towards -infinity,
3102 : : since (-0) + 0 is then 0. */
3103 : 479715961 : if (!HONOR_SIGNED_ZEROS (mode) && !HONOR_SNANS (mode)
3104 : 479715949 : && trueop1 == CONST0_RTX (mode))
3105 : : return op0;
3106 : :
3107 : : /* ((-a) + b) -> (b - a) and similarly for (a + (-b)). These
3108 : : transformations are safe even for IEEE. */
3109 : 240472692 : if (GET_CODE (op0) == NEG)
3110 : 29544 : return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
3111 : 240443148 : else if (GET_CODE (op1) == NEG)
3112 : 7634 : return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
3113 : :
3114 : : /* (~a) + 1 -> -a */
3115 : 240435514 : if (INTEGRAL_MODE_P (mode)
3116 : 235635574 : && GET_CODE (op0) == NOT
3117 : 622181 : && trueop1 == const1_rtx)
3118 : 3431 : return simplify_gen_unary (NEG, mode, XEXP (op0, 0), mode);
3119 : :
3120 : : /* Handle both-operands-constant cases. We can only add
3121 : : CONST_INTs to constants since the sum of relocatable symbols
3122 : : can't be handled by most assemblers. Don't add CONST_INT
3123 : : to CONST_INT since overflow won't be computed properly if wider
3124 : : than HOST_BITS_PER_WIDE_INT. */
3125 : :
3126 : 240432083 : if ((GET_CODE (op0) == CONST
3127 : 240432083 : || GET_CODE (op0) == SYMBOL_REF
3128 : 238135655 : || GET_CODE (op0) == LABEL_REF)
3129 : 240432083 : && poly_int_rtx_p (op1, &offset))
3130 : 2295439 : return plus_constant (mode, op0, offset);
3131 : 238136644 : else if ((GET_CODE (op1) == CONST
3132 : 238136644 : || GET_CODE (op1) == SYMBOL_REF
3133 : 234598071 : || GET_CODE (op1) == LABEL_REF)
3134 : 238136644 : && poly_int_rtx_p (op0, &offset))
3135 : 0 : return plus_constant (mode, op1, offset);
3136 : :
3137 : : /* See if this is something like X * C - X or vice versa or
3138 : : if the multiplication is written as a shift. If so, we can
3139 : : distribute and make a new multiply, shift, or maybe just
3140 : : have X (if C is 2 in the example above). But don't make
3141 : : something more expensive than we had before. */
3142 : :
3143 : 238136644 : if (is_a <scalar_int_mode> (mode, &int_mode))
3144 : : {
3145 : 231361771 : rtx lhs = op0, rhs = op1;
3146 : :
3147 : 231361771 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3148 : 231361771 : wide_int coeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3149 : :
3150 : 231361771 : if (GET_CODE (lhs) == NEG)
3151 : : {
3152 : 0 : coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3153 : 0 : lhs = XEXP (lhs, 0);
3154 : : }
3155 : 231361771 : else if (GET_CODE (lhs) == MULT
3156 : 6877648 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3157 : : {
3158 : 5784366 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3159 : 5784366 : lhs = XEXP (lhs, 0);
3160 : : }
3161 : 225577405 : else if (GET_CODE (lhs) == ASHIFT
3162 : 10959421 : && CONST_INT_P (XEXP (lhs, 1))
3163 : 10888769 : && INTVAL (XEXP (lhs, 1)) >= 0
3164 : 236466162 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3165 : : {
3166 : 10888757 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3167 : 21777514 : GET_MODE_PRECISION (int_mode));
3168 : 10888757 : lhs = XEXP (lhs, 0);
3169 : : }
3170 : :
3171 : 231361771 : if (GET_CODE (rhs) == NEG)
3172 : : {
3173 : 0 : coeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3174 : 0 : rhs = XEXP (rhs, 0);
3175 : : }
3176 : 231361771 : else if (GET_CODE (rhs) == MULT
3177 : 274034 : && CONST_INT_P (XEXP (rhs, 1)))
3178 : : {
3179 : 174472 : coeff1 = rtx_mode_t (XEXP (rhs, 1), int_mode);
3180 : 174472 : rhs = XEXP (rhs, 0);
3181 : : }
3182 : 231187299 : else if (GET_CODE (rhs) == ASHIFT
3183 : 574532 : && CONST_INT_P (XEXP (rhs, 1))
3184 : 564760 : && INTVAL (XEXP (rhs, 1)) >= 0
3185 : 231752059 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3186 : : {
3187 : 564760 : coeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3188 : 1129520 : GET_MODE_PRECISION (int_mode));
3189 : 564760 : rhs = XEXP (rhs, 0);
3190 : : }
3191 : :
3192 : 231361771 : if (rtx_equal_p (lhs, rhs))
3193 : : {
3194 : 746252 : rtx orig = gen_rtx_PLUS (int_mode, op0, op1);
3195 : 746252 : rtx coeff;
3196 : 746252 : bool speed = optimize_function_for_speed_p (cfun);
3197 : :
3198 : 746252 : coeff = immed_wide_int_const (coeff0 + coeff1, int_mode);
3199 : :
3200 : 746252 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3201 : 746252 : return (set_src_cost (tem, int_mode, speed)
3202 : 746252 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3203 : : }
3204 : :
3205 : : /* Optimize (X - 1) * Y + Y to X * Y. */
3206 : 230615519 : lhs = op0;
3207 : 230615519 : rhs = op1;
3208 : 230615519 : if (GET_CODE (op0) == MULT)
3209 : : {
3210 : 6854082 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3211 : 324896 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx)
3212 : 6812546 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3213 : 36721 : && XEXP (XEXP (op0, 0), 1) == const1_rtx))
3214 : 6895618 : && rtx_equal_p (XEXP (op0, 1), op1))
3215 : 42 : lhs = XEXP (XEXP (op0, 0), 0);
3216 : 6854040 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3217 : 1581 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx)
3218 : 6854005 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3219 : 266 : && XEXP (XEXP (op0, 1), 1) == const1_rtx))
3220 : 6854075 : && rtx_equal_p (XEXP (op0, 0), op1))
3221 : 0 : lhs = XEXP (XEXP (op0, 1), 0);
3222 : : }
3223 : 223761437 : else if (GET_CODE (op1) == MULT)
3224 : : {
3225 : 127480 : if (((GET_CODE (XEXP (op1, 0)) == PLUS
3226 : 80 : && XEXP (XEXP (op1, 0), 1) == constm1_rtx)
3227 : 127478 : || (GET_CODE (XEXP (op1, 0)) == MINUS
3228 : 29 : && XEXP (XEXP (op1, 0), 1) == const1_rtx))
3229 : 127482 : && rtx_equal_p (XEXP (op1, 1), op0))
3230 : 0 : rhs = XEXP (XEXP (op1, 0), 0);
3231 : 127480 : else if (((GET_CODE (XEXP (op1, 1)) == PLUS
3232 : 45 : && XEXP (XEXP (op1, 1), 1) == constm1_rtx)
3233 : 127480 : || (GET_CODE (XEXP (op1, 1)) == MINUS
3234 : 0 : && XEXP (XEXP (op1, 1), 1) == const1_rtx))
3235 : 127480 : && rtx_equal_p (XEXP (op1, 0), op0))
3236 : 0 : rhs = XEXP (XEXP (op1, 1), 0);
3237 : : }
3238 : 230615519 : if (lhs != op0 || rhs != op1)
3239 : 42 : return simplify_gen_binary (MULT, int_mode, lhs, rhs);
3240 : 231361771 : }
3241 : :
3242 : : /* (plus (xor X C1) C2) is (xor X (C1^C2)) if C2 is signbit. */
3243 : 237390350 : if (CONST_SCALAR_INT_P (op1)
3244 : 184324935 : && GET_CODE (op0) == XOR
3245 : 24503 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3246 : 237406380 : && mode_signbit_p (mode, op1))
3247 : 121 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3248 : : simplify_gen_binary (XOR, mode, op1,
3249 : 121 : XEXP (op0, 1)));
3250 : :
3251 : : /* (plus (xor X C1) C2) is (xor X (C1^C2)) if X is either 0 or 1 and
3252 : : 2 * ((X ^ C1) & C2) == 0; based on A + B == A ^ B + 2 * (A & B). */
3253 : 237390229 : if (CONST_SCALAR_INT_P (op1)
3254 : 184324814 : && GET_CODE (op0) == XOR
3255 : 24382 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3256 : 15909 : && nonzero_bits (XEXP (op0, 0), mode) == 1
3257 : 189 : && 2 * (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) == 0
3258 : 237390232 : && 2 * ((1 ^ INTVAL (XEXP (op0, 1))) & INTVAL (op1)) == 0)
3259 : 3 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3260 : : simplify_gen_binary (XOR, mode, op1,
3261 : 3 : XEXP (op0, 1)));
3262 : :
3263 : : /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)). */
3264 : 237390226 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3265 : 237387743 : && GET_CODE (op0) == MULT
3266 : 244600365 : && GET_CODE (XEXP (op0, 0)) == NEG)
3267 : : {
3268 : 5504 : rtx in1, in2;
3269 : :
3270 : 5504 : in1 = XEXP (XEXP (op0, 0), 0);
3271 : 5504 : in2 = XEXP (op0, 1);
3272 : 5504 : return simplify_gen_binary (MINUS, mode, op1,
3273 : : simplify_gen_binary (MULT, mode,
3274 : 5504 : in1, in2));
3275 : : }
3276 : :
3277 : : /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3278 : : C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3279 : : is 1. */
3280 : 237384722 : if (COMPARISON_P (op0)
3281 : 1751356 : && ((STORE_FLAG_VALUE == -1 && trueop1 == const1_rtx)
3282 : 1751356 : || (STORE_FLAG_VALUE == 1 && trueop1 == constm1_rtx))
3283 : 237432838 : && (reversed = reversed_comparison (op0, mode)))
3284 : 47800 : return
3285 : 47800 : simplify_gen_unary (NEG, mode, reversed, mode);
3286 : :
3287 : : /* Convert (plus (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3288 : : mode size to (rotate A CX). */
3289 : 237336922 : if ((tem = simplify_rotate_op (op0, op1, mode)))
3290 : : return tem;
3291 : :
3292 : : /* If one of the operands is a PLUS or a MINUS, see if we can
3293 : : simplify this by the associative law.
3294 : : Don't use the associative law for floating point.
3295 : : The inaccuracy makes it nonassociative,
3296 : : and subtle programs can break if operations are associated. */
3297 : :
3298 : 237335392 : if (INTEGRAL_MODE_P (mode)
3299 : 232535501 : && (plus_minus_operand_p (op0)
3300 : 200928395 : || plus_minus_operand_p (op1))
3301 : 32547609 : && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3302 : : return tem;
3303 : :
3304 : : /* Reassociate floating point addition only when the user
3305 : : specifies associative math operations. */
3306 : 205504415 : if (FLOAT_MODE_P (mode)
3307 : 4799891 : && flag_associative_math)
3308 : : {
3309 : 892536 : tem = simplify_associative_operation (code, mode, op0, op1);
3310 : 892536 : if (tem)
3311 : : return tem;
3312 : : }
3313 : :
3314 : : /* Handle vector series. */
3315 : 205491409 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3316 : : {
3317 : 1821242 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3318 : 1821242 : if (tem)
3319 : : return tem;
3320 : : }
3321 : : break;
3322 : :
3323 : : case COMPARE:
3324 : : break;
3325 : :
3326 : 42418319 : case MINUS:
3327 : : /* We can't assume x-x is 0 even with non-IEEE floating point,
3328 : : but since it is zero except in very strange circumstances, we
3329 : : will treat it as zero with -ffinite-math-only. */
3330 : 42418319 : if (rtx_equal_p (trueop0, trueop1)
3331 : 221270 : && ! side_effects_p (op0)
3332 : 42638532 : && (!FLOAT_MODE_P (mode) || !HONOR_NANS (mode)))
3333 : 217829 : return CONST0_RTX (mode);
3334 : :
3335 : : /* Change subtraction from zero into negation. (0 - x) is the
3336 : : same as -x when x is NaN, infinite, or finite and nonzero.
3337 : : But if the mode has signed zeros, and does not round towards
3338 : : -infinity, then 0 - 0 is 0, not -0. */
3339 : 42200490 : if (!HONOR_SIGNED_ZEROS (mode) && trueop0 == CONST0_RTX (mode))
3340 : 290474 : return simplify_gen_unary (NEG, mode, op1, mode);
3341 : :
3342 : : /* (-1 - a) is ~a, unless the expression contains symbolic
3343 : : constants, in which case not retaining additions and
3344 : : subtractions could cause invalid assembly to be produced. */
3345 : 41910016 : if (trueop0 == CONSTM1_RTX (mode)
3346 : 41910016 : && !contains_symbolic_reference_p (op1))
3347 : 360684 : return simplify_gen_unary (NOT, mode, op1, mode);
3348 : :
3349 : : /* Subtracting 0 has no effect unless the mode has signalling NaNs,
3350 : : or has signed zeros and supports rounding towards -infinity.
3351 : : In such a case, 0 - 0 is -0. */
3352 : 42244201 : if (!(HONOR_SIGNED_ZEROS (mode)
3353 : 694869 : && HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3354 : 41548154 : && !HONOR_SNANS (mode)
3355 : 83097450 : && trueop1 == CONST0_RTX (mode))
3356 : : return op0;
3357 : :
3358 : : /* See if this is something like X * C - X or vice versa or
3359 : : if the multiplication is written as a shift. If so, we can
3360 : : distribute and make a new multiply, shift, or maybe just
3361 : : have X (if C is 2 in the example above). But don't make
3362 : : something more expensive than we had before. */
3363 : :
3364 : 40457747 : if (is_a <scalar_int_mode> (mode, &int_mode))
3365 : : {
3366 : 39274231 : rtx lhs = op0, rhs = op1;
3367 : :
3368 : 39274231 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3369 : 39274231 : wide_int negcoeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3370 : :
3371 : 39274231 : if (GET_CODE (lhs) == NEG)
3372 : : {
3373 : 114726 : coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3374 : 114726 : lhs = XEXP (lhs, 0);
3375 : : }
3376 : 39159505 : else if (GET_CODE (lhs) == MULT
3377 : 202524 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3378 : : {
3379 : 85769 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3380 : 85769 : lhs = XEXP (lhs, 0);
3381 : : }
3382 : 39073736 : else if (GET_CODE (lhs) == ASHIFT
3383 : 305036 : && CONST_INT_P (XEXP (lhs, 1))
3384 : 301955 : && INTVAL (XEXP (lhs, 1)) >= 0
3385 : 39375670 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3386 : : {
3387 : 301934 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3388 : 603868 : GET_MODE_PRECISION (int_mode));
3389 : 301934 : lhs = XEXP (lhs, 0);
3390 : : }
3391 : :
3392 : 39274231 : if (GET_CODE (rhs) == NEG)
3393 : : {
3394 : 7152 : negcoeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3395 : 7152 : rhs = XEXP (rhs, 0);
3396 : : }
3397 : 39267079 : else if (GET_CODE (rhs) == MULT
3398 : 273169 : && CONST_INT_P (XEXP (rhs, 1)))
3399 : : {
3400 : 240233 : negcoeff1 = wi::neg (rtx_mode_t (XEXP (rhs, 1), int_mode));
3401 : 240233 : rhs = XEXP (rhs, 0);
3402 : : }
3403 : 39026846 : else if (GET_CODE (rhs) == ASHIFT
3404 : 403202 : && CONST_INT_P (XEXP (rhs, 1))
3405 : 402764 : && INTVAL (XEXP (rhs, 1)) >= 0
3406 : 39429610 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3407 : : {
3408 : 402764 : negcoeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3409 : 805528 : GET_MODE_PRECISION (int_mode));
3410 : 402764 : negcoeff1 = -negcoeff1;
3411 : 402764 : rhs = XEXP (rhs, 0);
3412 : : }
3413 : :
3414 : 39274231 : if (rtx_equal_p (lhs, rhs))
3415 : : {
3416 : 129997 : rtx orig = gen_rtx_MINUS (int_mode, op0, op1);
3417 : 129997 : rtx coeff;
3418 : 129997 : bool speed = optimize_function_for_speed_p (cfun);
3419 : :
3420 : 129997 : coeff = immed_wide_int_const (coeff0 + negcoeff1, int_mode);
3421 : :
3422 : 129997 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3423 : 129997 : return (set_src_cost (tem, int_mode, speed)
3424 : 129997 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3425 : : }
3426 : :
3427 : : /* Optimize (X + 1) * Y - Y to X * Y. */
3428 : 39144234 : lhs = op0;
3429 : 39144234 : if (GET_CODE (op0) == MULT)
3430 : : {
3431 : 202399 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3432 : 4589 : && XEXP (XEXP (op0, 0), 1) == const1_rtx)
3433 : 200940 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3434 : 5704 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx))
3435 : 203858 : && rtx_equal_p (XEXP (op0, 1), op1))
3436 : 86 : lhs = XEXP (XEXP (op0, 0), 0);
3437 : 202313 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3438 : 12 : && XEXP (XEXP (op0, 1), 1) == const1_rtx)
3439 : 202309 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3440 : 46 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx))
3441 : 202317 : && rtx_equal_p (XEXP (op0, 0), op1))
3442 : 0 : lhs = XEXP (XEXP (op0, 1), 0);
3443 : : }
3444 : 39144234 : if (lhs != op0)
3445 : 86 : return simplify_gen_binary (MULT, int_mode, lhs, op1);
3446 : 39274231 : }
3447 : :
3448 : : /* (a - (-b)) -> (a + b). True even for IEEE. */
3449 : 40327664 : if (GET_CODE (op1) == NEG)
3450 : 7118 : return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
3451 : :
3452 : : /* (-x - c) may be simplified as (-c - x). */
3453 : 40320546 : if (GET_CODE (op0) == NEG
3454 : 118916 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1)))
3455 : : {
3456 : 599 : tem = simplify_unary_operation (NEG, mode, op1, mode);
3457 : 599 : if (tem)
3458 : 599 : return simplify_gen_binary (MINUS, mode, tem, XEXP (op0, 0));
3459 : : }
3460 : :
3461 : 40319947 : if ((GET_CODE (op0) == CONST
3462 : 40319947 : || GET_CODE (op0) == SYMBOL_REF
3463 : 34762408 : || GET_CODE (op0) == LABEL_REF)
3464 : 40319947 : && poly_int_rtx_p (op1, &offset))
3465 : 45139 : return plus_constant (mode, op0, trunc_int_for_mode (-offset, mode));
3466 : :
3467 : : /* Don't let a relocatable value get a negative coeff. */
3468 : 40274808 : if (poly_int_rtx_p (op1) && GET_MODE (op0) != VOIDmode)
3469 : 7114751 : return simplify_gen_binary (PLUS, mode,
3470 : : op0,
3471 : 7114751 : neg_poly_int_rtx (mode, op1));
3472 : :
3473 : : /* (x - (x & y)) -> (x & ~y) */
3474 : 33160057 : if (INTEGRAL_MODE_P (mode) && GET_CODE (op1) == AND)
3475 : : {
3476 : 462514 : if (rtx_equal_p (op0, XEXP (op1, 0)))
3477 : : {
3478 : 3036 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 1),
3479 : 1518 : GET_MODE (XEXP (op1, 1)));
3480 : 1518 : return simplify_gen_binary (AND, mode, op0, tem);
3481 : : }
3482 : 460996 : if (rtx_equal_p (op0, XEXP (op1, 1)))
3483 : : {
3484 : 2324 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 0),
3485 : 1162 : GET_MODE (XEXP (op1, 0)));
3486 : 1162 : return simplify_gen_binary (AND, mode, op0, tem);
3487 : : }
3488 : : }
3489 : :
3490 : : /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3491 : : by reversing the comparison code if valid. */
3492 : 33157377 : if (STORE_FLAG_VALUE == 1
3493 : 33157377 : && trueop0 == const1_rtx
3494 : 1036583 : && COMPARISON_P (op1)
3495 : 33237870 : && (reversed = reversed_comparison (op1, mode)))
3496 : : return reversed;
3497 : :
3498 : : /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A). */
3499 : 33076907 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3500 : 33075528 : && GET_CODE (op1) == MULT
3501 : 33444426 : && GET_CODE (XEXP (op1, 0)) == NEG)
3502 : : {
3503 : 165 : rtx in1, in2;
3504 : :
3505 : 165 : in1 = XEXP (XEXP (op1, 0), 0);
3506 : 165 : in2 = XEXP (op1, 1);
3507 : 165 : return simplify_gen_binary (PLUS, mode,
3508 : : simplify_gen_binary (MULT, mode,
3509 : : in1, in2),
3510 : 165 : op0);
3511 : : }
3512 : :
3513 : : /* Canonicalize (minus (neg A) (mult B C)) to
3514 : : (minus (mult (neg B) C) A). */
3515 : 33076742 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3516 : 33075363 : && GET_CODE (op1) == MULT
3517 : 33444096 : && GET_CODE (op0) == NEG)
3518 : : {
3519 : 673 : rtx in1, in2;
3520 : :
3521 : 673 : in1 = simplify_gen_unary (NEG, mode, XEXP (op1, 0), mode);
3522 : 673 : in2 = XEXP (op1, 1);
3523 : 673 : return simplify_gen_binary (MINUS, mode,
3524 : : simplify_gen_binary (MULT, mode,
3525 : : in1, in2),
3526 : 673 : XEXP (op0, 0));
3527 : : }
3528 : :
3529 : : /* If one of the operands is a PLUS or a MINUS, see if we can
3530 : : simplify this by the associative law. This will, for example,
3531 : : canonicalize (minus A (plus B C)) to (minus (minus A B) C).
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 : 33076069 : if (INTEGRAL_MODE_P (mode)
3537 : 32273507 : && (plus_minus_operand_p (op0)
3538 : 29887711 : || plus_minus_operand_p (op1))
3539 : 3625019 : && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3540 : : return tem;
3541 : :
3542 : : /* Handle vector series. */
3543 : 29581214 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3544 : : {
3545 : 360581 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3546 : 360581 : if (tem)
3547 : : return tem;
3548 : : }
3549 : : break;
3550 : :
3551 : 12944613 : case MULT:
3552 : 12944613 : if (trueop1 == constm1_rtx)
3553 : 29120 : return simplify_gen_unary (NEG, mode, op0, mode);
3554 : :
3555 : 12915493 : if (GET_CODE (op0) == NEG)
3556 : : {
3557 : 33361 : rtx temp = simplify_unary_operation (NEG, mode, op1, mode);
3558 : : /* If op1 is a MULT as well and simplify_unary_operation
3559 : : just moved the NEG to the second operand, simplify_gen_binary
3560 : : below could through simplify_associative_operation move
3561 : : the NEG around again and recurse endlessly. */
3562 : 33361 : if (temp
3563 : 1447 : && GET_CODE (op1) == MULT
3564 : 0 : && GET_CODE (temp) == MULT
3565 : 0 : && XEXP (op1, 0) == XEXP (temp, 0)
3566 : 0 : && GET_CODE (XEXP (temp, 1)) == NEG
3567 : 0 : && XEXP (op1, 1) == XEXP (XEXP (temp, 1), 0))
3568 : : temp = NULL_RTX;
3569 : : if (temp)
3570 : 1447 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), temp);
3571 : : }
3572 : 12914046 : if (GET_CODE (op1) == NEG)
3573 : : {
3574 : 968 : rtx temp = simplify_unary_operation (NEG, mode, op0, mode);
3575 : : /* If op0 is a MULT as well and simplify_unary_operation
3576 : : just moved the NEG to the second operand, simplify_gen_binary
3577 : : below could through simplify_associative_operation move
3578 : : the NEG around again and recurse endlessly. */
3579 : 968 : if (temp
3580 : 379 : && GET_CODE (op0) == MULT
3581 : 299 : && GET_CODE (temp) == MULT
3582 : 299 : && XEXP (op0, 0) == XEXP (temp, 0)
3583 : 6 : && GET_CODE (XEXP (temp, 1)) == NEG
3584 : 5 : && XEXP (op0, 1) == XEXP (XEXP (temp, 1), 0))
3585 : : temp = NULL_RTX;
3586 : : if (temp)
3587 : 374 : return simplify_gen_binary (MULT, mode, temp, XEXP (op1, 0));
3588 : : }
3589 : :
3590 : : /* Maybe simplify x * 0 to 0. The reduction is not valid if
3591 : : x is NaN, since x * 0 is then also NaN. Nor is it valid
3592 : : when the mode has signed zeros, since multiplying a negative
3593 : : number by 0 will give -0, not 0. */
3594 : 12913672 : if (!HONOR_NANS (mode)
3595 : 11958673 : && !HONOR_SIGNED_ZEROS (mode)
3596 : 11958618 : && trueop1 == CONST0_RTX (mode)
3597 : 12973814 : && ! side_effects_p (op0))
3598 : : return op1;
3599 : :
3600 : : /* In IEEE floating point, x*1 is not equivalent to x for
3601 : : signalling NaNs. */
3602 : 12854588 : if (!HONOR_SNANS (mode)
3603 : 12854588 : && trueop1 == CONST1_RTX (mode))
3604 : : return op0;
3605 : :
3606 : : /* Convert multiply by constant power of two into shift. */
3607 : 12316778 : if (mem_depth == 0 && CONST_SCALAR_INT_P (trueop1))
3608 : : {
3609 : 7100984 : val = wi::exact_log2 (rtx_mode_t (trueop1, mode));
3610 : 7100984 : if (val >= 0)
3611 : 2886198 : return simplify_gen_binary (ASHIFT, mode, op0,
3612 : 2886198 : gen_int_shift_amount (mode, val));
3613 : : }
3614 : :
3615 : : /* x*2 is x+x and x*(-1) is -x */
3616 : 9430580 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
3617 : 169082 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop1))
3618 : 169082 : && !DECIMAL_FLOAT_MODE_P (GET_MODE (trueop1))
3619 : 168804 : && GET_MODE (op0) == mode)
3620 : : {
3621 : 168804 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
3622 : :
3623 : 168804 : if (real_equal (d1, &dconst2))
3624 : 683 : return simplify_gen_binary (PLUS, mode, op0, copy_rtx (op0));
3625 : :
3626 : 168121 : if (!HONOR_SNANS (mode)
3627 : 168121 : && real_equal (d1, &dconstm1))
3628 : 24 : return simplify_gen_unary (NEG, mode, op0, mode);
3629 : : }
3630 : :
3631 : : /* Optimize -x * -x as x * x. */
3632 : 9429873 : if (FLOAT_MODE_P (mode)
3633 : 1355867 : && GET_CODE (op0) == NEG
3634 : 7946 : && GET_CODE (op1) == NEG
3635 : 0 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3636 : 0 : && !side_effects_p (XEXP (op0, 0)))
3637 : 0 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3638 : :
3639 : : /* Likewise, optimize abs(x) * abs(x) as x * x. */
3640 : 9429873 : if (SCALAR_FLOAT_MODE_P (mode)
3641 : 1104470 : && GET_CODE (op0) == ABS
3642 : 1381 : && GET_CODE (op1) == ABS
3643 : 0 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3644 : 9429873 : && !side_effects_p (XEXP (op0, 0)))
3645 : 0 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3646 : :
3647 : : /* Reassociate multiplication, but for floating point MULTs
3648 : : only when the user specifies unsafe math optimizations. */
3649 : 9429873 : if (! FLOAT_MODE_P (mode)
3650 : 1355867 : || flag_unsafe_math_optimizations)
3651 : : {
3652 : 8476104 : tem = simplify_associative_operation (code, mode, op0, op1);
3653 : 8476104 : if (tem)
3654 : : return tem;
3655 : : }
3656 : : break;
3657 : :
3658 : 16959886 : case IOR:
3659 : 16959886 : if (trueop1 == CONST0_RTX (mode))
3660 : : return op0;
3661 : 16096951 : if (INTEGRAL_MODE_P (mode)
3662 : 15861410 : && trueop1 == CONSTM1_RTX (mode)
3663 : 5040 : && !side_effects_p (op0))
3664 : : return op1;
3665 : 16091911 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3666 : : return op0;
3667 : : /* A | (~A) -> -1 */
3668 : 70598 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
3669 : 16072258 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
3670 : 10 : && ! side_effects_p (op0)
3671 : 16072278 : && GET_MODE_CLASS (mode) != MODE_CC)
3672 : 10 : return CONSTM1_RTX (mode);
3673 : :
3674 : : /* Convert (ior (plus (A - 1)) (neg A)) to -1. */
3675 : 16072258 : if (match_plus_neg_pattern (op0, op1, mode))
3676 : 0 : return CONSTM1_RTX (mode);
3677 : :
3678 : : /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
3679 : 16072258 : if (CONST_INT_P (op1)
3680 : 4225731 : && HWI_COMPUTABLE_MODE_P (mode)
3681 : 4177752 : && (nonzero_bits (op0, mode) & ~UINTVAL (op1)) == 0
3682 : 16428285 : && !side_effects_p (op0))
3683 : : return op1;
3684 : :
3685 : : /* Canonicalize (X & C1) | C2. */
3686 : 15716231 : if (GET_CODE (op0) == AND
3687 : 4461597 : && CONST_INT_P (trueop1)
3688 : 739848 : && CONST_INT_P (XEXP (op0, 1)))
3689 : : {
3690 : 610232 : HOST_WIDE_INT mask = GET_MODE_MASK (mode);
3691 : 610232 : HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
3692 : 610232 : HOST_WIDE_INT c2 = INTVAL (trueop1);
3693 : :
3694 : : /* If (C1&C2) == C1, then (X&C1)|C2 becomes C2. */
3695 : 610232 : if ((c1 & c2) == c1
3696 : 610232 : && !side_effects_p (XEXP (op0, 0)))
3697 : : return trueop1;
3698 : :
3699 : : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
3700 : 610232 : if (((c1|c2) & mask) == mask)
3701 : 72828 : return simplify_gen_binary (IOR, mode, XEXP (op0, 0), op1);
3702 : : }
3703 : :
3704 : : /* Convert (A & B) | A to A. */
3705 : 15643403 : if (GET_CODE (op0) == AND
3706 : 4388769 : && (rtx_equal_p (XEXP (op0, 0), op1)
3707 : 4388664 : || rtx_equal_p (XEXP (op0, 1), op1))
3708 : 3630 : && ! side_effects_p (XEXP (op0, 0))
3709 : 15647033 : && ! side_effects_p (XEXP (op0, 1)))
3710 : : return op1;
3711 : :
3712 : : /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3713 : : mode size to (rotate A CX). */
3714 : 15639773 : tem = simplify_rotate_op (op0, op1, mode);
3715 : 15639773 : if (tem)
3716 : : return tem;
3717 : :
3718 : : /* If OP0 is (ashiftrt (plus ...) C), it might actually be
3719 : : a (sign_extend (plus ...)). Then check if OP1 is a CONST_INT and
3720 : : the PLUS does not affect any of the bits in OP1: then we can do
3721 : : the IOR as a PLUS and we can associate. This is valid if OP1
3722 : : can be safely shifted left C bits. */
3723 : 15637200 : if (CONST_INT_P (trueop1) && GET_CODE (op0) == ASHIFTRT
3724 : 7194 : && GET_CODE (XEXP (op0, 0)) == PLUS
3725 : 141 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
3726 : 87 : && CONST_INT_P (XEXP (op0, 1))
3727 : 87 : && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
3728 : : {
3729 : 87 : int count = INTVAL (XEXP (op0, 1));
3730 : 87 : HOST_WIDE_INT mask = UINTVAL (trueop1) << count;
3731 : :
3732 : 87 : if (mask >> count == INTVAL (trueop1)
3733 : 80 : && trunc_int_for_mode (mask, mode) == mask
3734 : 154 : && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
3735 : 0 : return simplify_gen_binary (ASHIFTRT, mode,
3736 : : plus_constant (mode, XEXP (op0, 0),
3737 : 0 : mask),
3738 : : XEXP (op0, 1));
3739 : : }
3740 : :
3741 : : /* The following happens with bitfield merging.
3742 : : (X & C) | ((X | Y) & ~C) -> X | (Y & ~C) */
3743 : 15637200 : if (GET_CODE (op0) == AND
3744 : 4385139 : && GET_CODE (op1) == AND
3745 : 353791 : && CONST_INT_P (XEXP (op0, 1))
3746 : 197646 : && CONST_INT_P (XEXP (op1, 1))
3747 : 192089 : && (INTVAL (XEXP (op0, 1))
3748 : 192089 : == ~INTVAL (XEXP (op1, 1))))
3749 : : {
3750 : : /* The IOR may be on both sides. */
3751 : 38181 : rtx top0 = NULL_RTX, top1 = NULL_RTX;
3752 : 38181 : if (GET_CODE (XEXP (op1, 0)) == IOR)
3753 : : top0 = op0, top1 = op1;
3754 : 38129 : else if (GET_CODE (XEXP (op0, 0)) == IOR)
3755 : 3 : top0 = op1, top1 = op0;
3756 : 38181 : if (top0 && top1)
3757 : : {
3758 : : /* X may be on either side of the inner IOR. */
3759 : 55 : rtx tem = NULL_RTX;
3760 : 55 : if (rtx_equal_p (XEXP (top0, 0),
3761 : 55 : XEXP (XEXP (top1, 0), 0)))
3762 : 43 : tem = XEXP (XEXP (top1, 0), 1);
3763 : 12 : else if (rtx_equal_p (XEXP (top0, 0),
3764 : 12 : XEXP (XEXP (top1, 0), 1)))
3765 : 3 : tem = XEXP (XEXP (top1, 0), 0);
3766 : 46 : if (tem)
3767 : 46 : return simplify_gen_binary (IOR, mode, XEXP (top0, 0),
3768 : : simplify_gen_binary
3769 : 46 : (AND, mode, tem, XEXP (top1, 1)));
3770 : : }
3771 : : }
3772 : :
3773 : : /* Convert (ior (and A C) (and B C)) into (and (ior A B) C). */
3774 : 15637154 : if (GET_CODE (op0) == GET_CODE (op1)
3775 : 3843588 : && (GET_CODE (op0) == AND
3776 : : || GET_CODE (op0) == IOR
3777 : 3843588 : || GET_CODE (op0) == LSHIFTRT
3778 : 3487942 : || GET_CODE (op0) == ASHIFTRT
3779 : 3487849 : || GET_CODE (op0) == ASHIFT
3780 : 3456824 : || GET_CODE (op0) == ROTATE
3781 : 3456824 : || GET_CODE (op0) == ROTATERT))
3782 : : {
3783 : 386764 : tem = simplify_distributive_operation (code, mode, op0, op1);
3784 : 386764 : if (tem)
3785 : : return tem;
3786 : : }
3787 : :
3788 : : /* Convert (ior (and (not A) B) A) into A | B. */
3789 : 15551747 : if (GET_CODE (op0) == AND
3790 : 4299826 : && GET_CODE (XEXP (op0, 0)) == NOT
3791 : 15697650 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1))
3792 : 3025 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
3793 : :
3794 : 15548722 : tem = simplify_with_subreg_not (code, mode, op0, op1);
3795 : 15548722 : if (tem)
3796 : : return tem;
3797 : :
3798 : 15548717 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
3799 : 15548717 : if (tem)
3800 : : return tem;
3801 : :
3802 : 15548688 : tem = simplify_associative_operation (code, mode, op0, op1);
3803 : 15548688 : if (tem)
3804 : : return tem;
3805 : :
3806 : 15257630 : tem = simplify_logical_relational_operation (code, mode, op0, op1);
3807 : 15257630 : if (tem)
3808 : : return tem;
3809 : : break;
3810 : :
3811 : 1622453 : case XOR:
3812 : 1622453 : if (trueop1 == CONST0_RTX (mode))
3813 : : return op0;
3814 : 1575209 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
3815 : 22892 : return simplify_gen_unary (NOT, mode, op0, mode);
3816 : 1552317 : if (rtx_equal_p (trueop0, trueop1)
3817 : 2501 : && ! side_effects_p (op0)
3818 : 1554818 : && GET_MODE_CLASS (mode) != MODE_CC)
3819 : 2501 : return CONST0_RTX (mode);
3820 : :
3821 : : /* Canonicalize XOR of the most significant bit to PLUS. */
3822 : 1549816 : if (CONST_SCALAR_INT_P (op1)
3823 : 1549816 : && mode_signbit_p (mode, op1))
3824 : 41549 : return simplify_gen_binary (PLUS, mode, op0, op1);
3825 : : /* (xor (plus X C1) C2) is (xor X (C1^C2)) if C1 is signbit. */
3826 : 1508267 : if (CONST_SCALAR_INT_P (op1)
3827 : 623734 : && GET_CODE (op0) == PLUS
3828 : 2493 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3829 : 1509865 : && mode_signbit_p (mode, XEXP (op0, 1)))
3830 : 189 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3831 : : simplify_gen_binary (XOR, mode, op1,
3832 : 189 : XEXP (op0, 1)));
3833 : :
3834 : : /* If we are XORing two things that have no bits in common,
3835 : : convert them into an IOR. This helps to detect rotation encoded
3836 : : using those methods and possibly other simplifications. */
3837 : :
3838 : 1508078 : if (HWI_COMPUTABLE_MODE_P (mode)
3839 : 1347448 : && (nonzero_bits (op0, mode)
3840 : 1347448 : & nonzero_bits (op1, mode)) == 0)
3841 : 11754 : return (simplify_gen_binary (IOR, mode, op0, op1));
3842 : :
3843 : : /* Convert (xor (plus (A - 1)) (neg A)) to -1. */
3844 : 1496324 : if (match_plus_neg_pattern (op0, op1, mode))
3845 : 0 : return CONSTM1_RTX (mode);
3846 : :
3847 : : /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
3848 : : Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
3849 : : (NOT y). */
3850 : 1496324 : {
3851 : 1496324 : int num_negated = 0;
3852 : :
3853 : 1496324 : if (GET_CODE (op0) == NOT)
3854 : 902 : num_negated++, op0 = XEXP (op0, 0);
3855 : 1496324 : if (GET_CODE (op1) == NOT)
3856 : 0 : num_negated++, op1 = XEXP (op1, 0);
3857 : :
3858 : 0 : if (num_negated == 2)
3859 : 0 : return simplify_gen_binary (XOR, mode, op0, op1);
3860 : 1496324 : else if (num_negated == 1)
3861 : 902 : return simplify_gen_unary (NOT, mode,
3862 : : simplify_gen_binary (XOR, mode, op0, op1),
3863 : 902 : mode);
3864 : : }
3865 : :
3866 : : /* Convert (xor (and A B) B) to (and (not A) B). The latter may
3867 : : correspond to a machine insn or result in further simplifications
3868 : : if B is a constant. */
3869 : :
3870 : 1495422 : if (GET_CODE (op0) == AND
3871 : 157328 : && rtx_equal_p (XEXP (op0, 1), op1)
3872 : 1528108 : && ! side_effects_p (op1))
3873 : 32686 : return simplify_gen_binary (AND, mode,
3874 : : simplify_gen_unary (NOT, mode,
3875 : : XEXP (op0, 0), mode),
3876 : 32686 : op1);
3877 : :
3878 : 1462736 : else if (GET_CODE (op0) == AND
3879 : 124642 : && rtx_equal_p (XEXP (op0, 0), op1)
3880 : 1464008 : && ! side_effects_p (op1))
3881 : 1272 : return simplify_gen_binary (AND, mode,
3882 : : simplify_gen_unary (NOT, mode,
3883 : : XEXP (op0, 1), mode),
3884 : 1272 : op1);
3885 : :
3886 : : /* Given (xor (ior (xor A B) C) D), where B, C and D are
3887 : : constants, simplify to (xor (ior A C) (B&~C)^D), canceling
3888 : : out bits inverted twice and not set by C. Similarly, given
3889 : : (xor (and (xor A B) C) D), simplify without inverting C in
3890 : : the xor operand: (xor (and A C) (B&C)^D).
3891 : : */
3892 : 1461464 : else if ((GET_CODE (op0) == IOR || GET_CODE (op0) == AND)
3893 : 143764 : && GET_CODE (XEXP (op0, 0)) == XOR
3894 : 5691 : && CONST_INT_P (op1)
3895 : 135 : && CONST_INT_P (XEXP (op0, 1))
3896 : 90 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1)))
3897 : : {
3898 : 90 : enum rtx_code op = GET_CODE (op0);
3899 : 90 : rtx a = XEXP (XEXP (op0, 0), 0);
3900 : 90 : rtx b = XEXP (XEXP (op0, 0), 1);
3901 : 90 : rtx c = XEXP (op0, 1);
3902 : 90 : rtx d = op1;
3903 : 90 : HOST_WIDE_INT bval = INTVAL (b);
3904 : 90 : HOST_WIDE_INT cval = INTVAL (c);
3905 : 90 : HOST_WIDE_INT dval = INTVAL (d);
3906 : 90 : HOST_WIDE_INT xcval;
3907 : :
3908 : 90 : if (op == IOR)
3909 : 8 : xcval = ~cval;
3910 : : else
3911 : : xcval = cval;
3912 : :
3913 : 90 : return simplify_gen_binary (XOR, mode,
3914 : : simplify_gen_binary (op, mode, a, c),
3915 : 90 : gen_int_mode ((bval & xcval) ^ dval,
3916 : : mode));
3917 : : }
3918 : :
3919 : : /* Given (xor (and A B) C), using P^Q == (~P&Q) | (~Q&P),
3920 : : we can transform like this:
3921 : : (A&B)^C == ~(A&B)&C | ~C&(A&B)
3922 : : == (~A|~B)&C | ~C&(A&B) * DeMorgan's Law
3923 : : == ~A&C | ~B&C | A&(~C&B) * Distribute and re-order
3924 : : Attempt a few simplifications when B and C are both constants. */
3925 : 1461374 : if (GET_CODE (op0) == AND
3926 : 123288 : && CONST_INT_P (op1)
3927 : 17734 : && CONST_INT_P (XEXP (op0, 1)))
3928 : : {
3929 : 11033 : rtx a = XEXP (op0, 0);
3930 : 11033 : rtx b = XEXP (op0, 1);
3931 : 11033 : rtx c = op1;
3932 : 11033 : HOST_WIDE_INT bval = INTVAL (b);
3933 : 11033 : HOST_WIDE_INT cval = INTVAL (c);
3934 : :
3935 : : /* Instead of computing ~A&C, we compute its negated value,
3936 : : ~(A|~C). If it yields -1, ~A&C is zero, so we can
3937 : : optimize for sure. If it does not simplify, we still try
3938 : : to compute ~A&C below, but since that always allocates
3939 : : RTL, we don't try that before committing to returning a
3940 : : simplified expression. */
3941 : 11033 : rtx n_na_c = simplify_binary_operation (IOR, mode, a,
3942 : : GEN_INT (~cval));
3943 : :
3944 : 11033 : if ((~cval & bval) == 0)
3945 : : {
3946 : 429 : rtx na_c = NULL_RTX;
3947 : 429 : if (n_na_c)
3948 : 0 : na_c = simplify_gen_unary (NOT, mode, n_na_c, mode);
3949 : : else
3950 : : {
3951 : : /* If ~A does not simplify, don't bother: we don't
3952 : : want to simplify 2 operations into 3, and if na_c
3953 : : were to simplify with na, n_na_c would have
3954 : : simplified as well. */
3955 : 429 : rtx na = simplify_unary_operation (NOT, mode, a, mode);
3956 : 429 : if (na)
3957 : 0 : na_c = simplify_gen_binary (AND, mode, na, c);
3958 : : }
3959 : :
3960 : : /* Try to simplify ~A&C | ~B&C. */
3961 : 0 : if (na_c != NULL_RTX)
3962 : 0 : return simplify_gen_binary (IOR, mode, na_c,
3963 : 0 : gen_int_mode (~bval & cval, mode));
3964 : : }
3965 : : else
3966 : : {
3967 : : /* If ~A&C is zero, simplify A&(~C&B) | ~B&C. */
3968 : 10604 : if (n_na_c == CONSTM1_RTX (mode))
3969 : : {
3970 : 0 : rtx a_nc_b = simplify_gen_binary (AND, mode, a,
3971 : 0 : gen_int_mode (~cval & bval,
3972 : : mode));
3973 : 0 : return simplify_gen_binary (IOR, mode, a_nc_b,
3974 : 0 : gen_int_mode (~bval & cval,
3975 : : mode));
3976 : : }
3977 : : }
3978 : : }
3979 : :
3980 : : /* If we have (xor (and (xor A B) C) A) with C a constant we can instead
3981 : : do (ior (and A ~C) (and B C)) which is a machine instruction on some
3982 : : machines, and also has shorter instruction path length. */
3983 : 1461374 : if (GET_CODE (op0) == AND
3984 : 123288 : && GET_CODE (XEXP (op0, 0)) == XOR
3985 : 5175 : && CONST_INT_P (XEXP (op0, 1))
3986 : 1463340 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), trueop1))
3987 : : {
3988 : 7 : rtx a = trueop1;
3989 : 7 : rtx b = XEXP (XEXP (op0, 0), 1);
3990 : 7 : rtx c = XEXP (op0, 1);
3991 : 7 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
3992 : 7 : rtx a_nc = simplify_gen_binary (AND, mode, a, nc);
3993 : 7 : rtx bc = simplify_gen_binary (AND, mode, b, c);
3994 : 7 : return simplify_gen_binary (IOR, mode, a_nc, bc);
3995 : : }
3996 : : /* Similarly, (xor (and (xor A B) C) B) as (ior (and A C) (and B ~C)) */
3997 : 1461367 : else if (GET_CODE (op0) == AND
3998 : 123281 : && GET_CODE (XEXP (op0, 0)) == XOR
3999 : 5168 : && CONST_INT_P (XEXP (op0, 1))
4000 : 1463326 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), trueop1))
4001 : : {
4002 : 8 : rtx a = XEXP (XEXP (op0, 0), 0);
4003 : 8 : rtx b = trueop1;
4004 : 8 : rtx c = XEXP (op0, 1);
4005 : 8 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
4006 : 8 : rtx b_nc = simplify_gen_binary (AND, mode, b, nc);
4007 : 8 : rtx ac = simplify_gen_binary (AND, mode, a, c);
4008 : 8 : return simplify_gen_binary (IOR, mode, ac, b_nc);
4009 : : }
4010 : :
4011 : : /* (xor (comparison foo bar) (const_int 1)) can become the reversed
4012 : : comparison if STORE_FLAG_VALUE is 1. */
4013 : 1461359 : if (STORE_FLAG_VALUE == 1
4014 : 1461359 : && trueop1 == const1_rtx
4015 : 198766 : && COMPARISON_P (op0)
4016 : 1466330 : && (reversed = reversed_comparison (op0, mode)))
4017 : : return reversed;
4018 : :
4019 : : /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4020 : : is (lt foo (const_int 0)), so we can perform the above
4021 : : simplification if STORE_FLAG_VALUE is 1. */
4022 : :
4023 : 1456476 : if (is_a <scalar_int_mode> (mode, &int_mode)
4024 : : && STORE_FLAG_VALUE == 1
4025 : 1300006 : && trueop1 == const1_rtx
4026 : 193883 : && GET_CODE (op0) == LSHIFTRT
4027 : 37589 : && CONST_INT_P (XEXP (op0, 1))
4028 : 37589 : && INTVAL (XEXP (op0, 1)) == GET_MODE_PRECISION (int_mode) - 1)
4029 : 36613 : return gen_rtx_GE (int_mode, XEXP (op0, 0), const0_rtx);
4030 : :
4031 : : /* (xor (comparison foo bar) (const_int sign-bit))
4032 : : when STORE_FLAG_VALUE is the sign bit. */
4033 : 1419863 : if (is_a <scalar_int_mode> (mode, &int_mode)
4034 : 1263393 : && val_signbit_p (int_mode, STORE_FLAG_VALUE)
4035 : 0 : && trueop1 == const_true_rtx
4036 : 0 : && COMPARISON_P (op0)
4037 : 0 : && (reversed = reversed_comparison (op0, int_mode)))
4038 : : return reversed;
4039 : :
4040 : : /* Convert (xor (and A C) (and B C)) into (and (xor A B) C). */
4041 : 1419863 : if (GET_CODE (op0) == GET_CODE (op1)
4042 : 382777 : && (GET_CODE (op0) == AND
4043 : 382777 : || GET_CODE (op0) == LSHIFTRT
4044 : 327865 : || GET_CODE (op0) == ASHIFTRT
4045 : 327815 : || GET_CODE (op0) == ASHIFT
4046 : 327655 : || GET_CODE (op0) == ROTATE
4047 : 327558 : || GET_CODE (op0) == ROTATERT))
4048 : : {
4049 : 55755 : tem = simplify_distributive_operation (code, mode, op0, op1);
4050 : 55755 : if (tem)
4051 : : return tem;
4052 : : }
4053 : :
4054 : : /* Convert (xor (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4055 : : mode size to (rotate A CX). */
4056 : 1368303 : tem = simplify_rotate_op (op0, op1, mode);
4057 : 1368303 : if (tem)
4058 : : return tem;
4059 : :
4060 : : /* Convert (xor (and (not A) B) A) into A | B. */
4061 : 1366863 : if (GET_CODE (op0) == AND
4062 : 71980 : && GET_CODE (XEXP (op0, 0)) == NOT
4063 : 1369010 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1))
4064 : 1 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4065 : :
4066 : : /* Convert (xor (and (rotate (~1) A) B) (ashift 1 A))
4067 : : into B | (1 << A). */
4068 : 1366862 : if (SHIFT_COUNT_TRUNCATED
4069 : : && GET_CODE (op0) == AND
4070 : : && GET_CODE (XEXP (op0, 0)) == ROTATE
4071 : : && CONST_INT_P (XEXP (XEXP (op0, 0), 0))
4072 : : && INTVAL (XEXP (XEXP (op0, 0), 0)) == -2
4073 : : && GET_CODE (op1) == ASHIFT
4074 : : && CONST_INT_P (XEXP (op1, 0))
4075 : : && INTVAL (XEXP (op1, 0)) == 1
4076 : : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), XEXP (op1, 1))
4077 : : && !side_effects_p (XEXP (op1, 1)))
4078 : : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4079 : :
4080 : 1366862 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4081 : 1366862 : if (tem)
4082 : : return tem;
4083 : :
4084 : 1366862 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4085 : 1366862 : if (tem)
4086 : : return tem;
4087 : :
4088 : 1366862 : tem = simplify_associative_operation (code, mode, op0, op1);
4089 : 1366862 : if (tem)
4090 : : return tem;
4091 : : break;
4092 : :
4093 : 25348314 : case AND:
4094 : 25348314 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
4095 : : return trueop1;
4096 : 25103950 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
4097 : : return op0;
4098 : 24693188 : if (HWI_COMPUTABLE_MODE_P (mode))
4099 : : {
4100 : : /* When WORD_REGISTER_OPERATIONS is true, we need to know the
4101 : : nonzero bits in WORD_MODE rather than MODE. */
4102 : 21879562 : scalar_int_mode tmode = as_a <scalar_int_mode> (mode);
4103 : 21879562 : if (WORD_REGISTER_OPERATIONS
4104 : : && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
4105 : : tmode = word_mode;
4106 : 21879562 : HOST_WIDE_INT nzop0 = nonzero_bits (trueop0, tmode);
4107 : 21879562 : HOST_WIDE_INT nzop1;
4108 : 21879562 : if (CONST_INT_P (trueop1))
4109 : : {
4110 : 18913846 : HOST_WIDE_INT val1 = INTVAL (trueop1);
4111 : : /* If we are turning off bits already known off in OP0, we need
4112 : : not do an AND. */
4113 : 18913846 : if ((nzop0 & ~val1) == 0)
4114 : 447374 : return op0;
4115 : : }
4116 : 21495666 : nzop1 = nonzero_bits (trueop1, mode);
4117 : : /* If we are clearing all the nonzero bits, the result is zero. */
4118 : 21495666 : if ((nzop1 & nzop0) == 0
4119 : 21495666 : && !side_effects_p (op0) && !side_effects_p (op1))
4120 : 63478 : return CONST0_RTX (mode);
4121 : : }
4122 : 24249014 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0)
4123 : 24249014 : && GET_MODE_CLASS (mode) != MODE_CC)
4124 : : return op0;
4125 : : /* A & (~A) -> 0 */
4126 : 627394 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
4127 : 24238688 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
4128 : 3972 : && ! side_effects_p (op0)
4129 : 24246586 : && GET_MODE_CLASS (mode) != MODE_CC)
4130 : 3972 : return CONST0_RTX (mode);
4131 : :
4132 : : /* Convert (and (plus (A - 1)) (neg A)) to 0. */
4133 : 24238642 : if (match_plus_neg_pattern (op0, op1, mode))
4134 : 2 : return CONST0_RTX (mode);
4135 : :
4136 : : /* Transform (and (extend X) C) into (zero_extend (and X C)) if
4137 : : there are no nonzero bits of C outside of X's mode. */
4138 : 48477280 : if ((GET_CODE (op0) == SIGN_EXTEND
4139 : 24238640 : || GET_CODE (op0) == ZERO_EXTEND)
4140 : 97986 : && CONST_SCALAR_INT_P (trueop1)
4141 : 85994 : && is_a <scalar_int_mode> (mode, &int_mode)
4142 : 85994 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
4143 : 24324634 : && (wi::mask (GET_MODE_PRECISION (inner_mode), true,
4144 : 85994 : GET_MODE_PRECISION (int_mode))
4145 : 24324634 : & rtx_mode_t (trueop1, mode)) == 0)
4146 : : {
4147 : 83220 : machine_mode imode = GET_MODE (XEXP (op0, 0));
4148 : 83220 : tem = immed_wide_int_const (rtx_mode_t (trueop1, mode), imode);
4149 : 83220 : tem = simplify_gen_binary (AND, imode, XEXP (op0, 0), tem);
4150 : 83220 : return simplify_gen_unary (ZERO_EXTEND, mode, tem, imode);
4151 : : }
4152 : :
4153 : : /* Transform (and (truncate X) C) into (truncate (and X C)). This way
4154 : : we might be able to further simplify the AND with X and potentially
4155 : : remove the truncation altogether. */
4156 : 24155420 : if (GET_CODE (op0) == TRUNCATE && CONST_INT_P (trueop1))
4157 : : {
4158 : 6 : rtx x = XEXP (op0, 0);
4159 : 6 : machine_mode xmode = GET_MODE (x);
4160 : 6 : tem = simplify_gen_binary (AND, xmode, x,
4161 : 6 : gen_int_mode (INTVAL (trueop1), xmode));
4162 : 6 : return simplify_gen_unary (TRUNCATE, mode, tem, xmode);
4163 : : }
4164 : :
4165 : : /* Canonicalize (A | C1) & C2 as (A & C2) | (C1 & C2). */
4166 : 24155414 : if (GET_CODE (op0) == IOR
4167 : 1550496 : && CONST_INT_P (trueop1)
4168 : 249203 : && CONST_INT_P (XEXP (op0, 1)))
4169 : : {
4170 : 152957 : HOST_WIDE_INT tmp = INTVAL (trueop1) & INTVAL (XEXP (op0, 1));
4171 : 152957 : return simplify_gen_binary (IOR, mode,
4172 : : simplify_gen_binary (AND, mode,
4173 : : XEXP (op0, 0), op1),
4174 : 152957 : gen_int_mode (tmp, mode));
4175 : : }
4176 : :
4177 : : /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
4178 : : insn (and may simplify more). */
4179 : 24002457 : if (GET_CODE (op0) == XOR
4180 : 113300 : && rtx_equal_p (XEXP (op0, 0), op1)
4181 : 24003591 : && ! side_effects_p (op1))
4182 : 1134 : return simplify_gen_binary (AND, mode,
4183 : : simplify_gen_unary (NOT, mode,
4184 : : XEXP (op0, 1), mode),
4185 : 1134 : op1);
4186 : :
4187 : 24001323 : if (GET_CODE (op0) == XOR
4188 : 112166 : && rtx_equal_p (XEXP (op0, 1), op1)
4189 : 24004308 : && ! side_effects_p (op1))
4190 : 2985 : return simplify_gen_binary (AND, mode,
4191 : : simplify_gen_unary (NOT, mode,
4192 : : XEXP (op0, 0), mode),
4193 : 2985 : op1);
4194 : :
4195 : : /* Similarly for (~(A ^ B)) & A. */
4196 : 23998338 : if (GET_CODE (op0) == NOT
4197 : 623468 : && GET_CODE (XEXP (op0, 0)) == XOR
4198 : 2838 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4199 : 23998392 : && ! side_effects_p (op1))
4200 : 54 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4201 : :
4202 : 23998284 : if (GET_CODE (op0) == NOT
4203 : 623414 : && GET_CODE (XEXP (op0, 0)) == XOR
4204 : 2784 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4205 : 23998321 : && ! side_effects_p (op1))
4206 : 37 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4207 : :
4208 : : /* Convert (A | B) & A to A. */
4209 : 23998247 : if (GET_CODE (op0) == IOR
4210 : 1397539 : && (rtx_equal_p (XEXP (op0, 0), op1)
4211 : 1396991 : || rtx_equal_p (XEXP (op0, 1), op1))
4212 : 695 : && ! side_effects_p (XEXP (op0, 0))
4213 : 23998942 : && ! side_effects_p (XEXP (op0, 1)))
4214 : : return op1;
4215 : :
4216 : : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
4217 : : ((A & N) + B) & M -> (A + B) & M
4218 : : Similarly if (N & M) == 0,
4219 : : ((A | N) + B) & M -> (A + B) & M
4220 : : and for - instead of + and/or ^ instead of |.
4221 : : Also, if (N & M) == 0, then
4222 : : (A +- N) & M -> A & M. */
4223 : 23997552 : if (CONST_INT_P (trueop1)
4224 : 18341073 : && HWI_COMPUTABLE_MODE_P (mode)
4225 : 18307202 : && ~UINTVAL (trueop1)
4226 : 18307202 : && (UINTVAL (trueop1) & (UINTVAL (trueop1) + 1)) == 0
4227 : 35477819 : && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS))
4228 : : {
4229 : 965215 : rtx pmop[2];
4230 : 965215 : int which;
4231 : :
4232 : 965215 : pmop[0] = XEXP (op0, 0);
4233 : 965215 : pmop[1] = XEXP (op0, 1);
4234 : :
4235 : 965215 : if (CONST_INT_P (pmop[1])
4236 : 519639 : && (UINTVAL (pmop[1]) & UINTVAL (trueop1)) == 0)
4237 : 157460 : return simplify_gen_binary (AND, mode, pmop[0], op1);
4238 : :
4239 : 2446176 : for (which = 0; which < 2; which++)
4240 : : {
4241 : 1630784 : tem = pmop[which];
4242 : 1630784 : switch (GET_CODE (tem))
4243 : : {
4244 : 11877 : case AND:
4245 : 11877 : if (CONST_INT_P (XEXP (tem, 1))
4246 : 10391 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1))
4247 : : == UINTVAL (trueop1))
4248 : 7498 : pmop[which] = XEXP (tem, 0);
4249 : : break;
4250 : 1683 : case IOR:
4251 : 1683 : case XOR:
4252 : 1683 : if (CONST_INT_P (XEXP (tem, 1))
4253 : 671 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1)) == 0)
4254 : 139 : pmop[which] = XEXP (tem, 0);
4255 : : break;
4256 : : default:
4257 : : break;
4258 : : }
4259 : : }
4260 : :
4261 : 815392 : if (pmop[0] != XEXP (op0, 0) || pmop[1] != XEXP (op0, 1))
4262 : : {
4263 : 7637 : tem = simplify_gen_binary (GET_CODE (op0), mode,
4264 : : pmop[0], pmop[1]);
4265 : 7637 : return simplify_gen_binary (code, mode, tem, op1);
4266 : : }
4267 : : }
4268 : :
4269 : : /* (and X (ior (not X) Y) -> (and X Y) */
4270 : 23840092 : if (GET_CODE (op1) == IOR
4271 : 1079087 : && GET_CODE (XEXP (op1, 0)) == NOT
4272 : 23845268 : && rtx_equal_p (op0, XEXP (XEXP (op1, 0), 0)))
4273 : 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 1));
4274 : :
4275 : : /* (and (ior (not X) Y) X) -> (and X Y) */
4276 : 23840092 : if (GET_CODE (op0) == IOR
4277 : 1396844 : && GET_CODE (XEXP (op0, 0)) == NOT
4278 : 23887912 : && rtx_equal_p (op1, XEXP (XEXP (op0, 0), 0)))
4279 : 6 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 1));
4280 : :
4281 : : /* (and X (ior Y (not X)) -> (and X Y) */
4282 : 23840086 : if (GET_CODE (op1) == IOR
4283 : 1079087 : && GET_CODE (XEXP (op1, 1)) == NOT
4284 : 23840351 : && rtx_equal_p (op0, XEXP (XEXP (op1, 1), 0)))
4285 : 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 0));
4286 : :
4287 : : /* (and (ior Y (not X)) X) -> (and X Y) */
4288 : 23840086 : if (GET_CODE (op0) == IOR
4289 : 1396838 : && GET_CODE (XEXP (op0, 1)) == NOT
4290 : 23848035 : && rtx_equal_p (op1, XEXP (XEXP (op0, 1), 0)))
4291 : 6 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 0));
4292 : :
4293 : : /* (and (ior/xor (X Y) (not Y)) -> X & ~Y */
4294 : 23840080 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4295 : 1506013 : && GET_CODE (op1) == NOT
4296 : 23950150 : && rtx_equal_p (XEXP (op1, 0), XEXP (op0, 1)))
4297 : 12 : return simplify_gen_binary (AND, mode, XEXP (op0, 0),
4298 : : simplify_gen_unary (NOT, mode,
4299 : : XEXP (op1, 0),
4300 : 12 : mode));
4301 : : /* (and (ior/xor (Y X) (not Y)) -> X & ~Y */
4302 : 23840068 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4303 : 1506001 : && GET_CODE (op1) == NOT
4304 : 23950126 : && rtx_equal_p (XEXP (op1, 0), XEXP (op0, 0)))
4305 : 4 : return simplify_gen_binary (AND, mode, XEXP (op0, 1),
4306 : : simplify_gen_unary (NOT, mode,
4307 : : XEXP (op1, 0),
4308 : 4 : mode));
4309 : :
4310 : : /* Convert (and (ior A C) (ior B C)) into (ior (and A B) C). */
4311 : 23840064 : if (GET_CODE (op0) == GET_CODE (op1)
4312 : 2182973 : && (GET_CODE (op0) == AND
4313 : : || GET_CODE (op0) == IOR
4314 : 2182973 : || GET_CODE (op0) == LSHIFTRT
4315 : 1104493 : || GET_CODE (op0) == ASHIFTRT
4316 : 1104396 : || GET_CODE (op0) == ASHIFT
4317 : 1104267 : || GET_CODE (op0) == ROTATE
4318 : 1104267 : || GET_CODE (op0) == ROTATERT))
4319 : : {
4320 : 1078706 : tem = simplify_distributive_operation (code, mode, op0, op1);
4321 : 1078706 : if (tem)
4322 : : return tem;
4323 : : }
4324 : :
4325 : : /* (and:v4si
4326 : : (ashiftrt:v4si A 16)
4327 : : (const_vector: 0xffff x4))
4328 : : is just (lshiftrt:v4si A 16). */
4329 : 22806047 : if (VECTOR_MODE_P (mode) && GET_CODE (op0) == ASHIFTRT
4330 : 3913 : && (CONST_INT_P (XEXP (op0, 1))
4331 : 1850 : || (GET_CODE (XEXP (op0, 1)) == CONST_VECTOR
4332 : 92 : && const_vec_duplicate_p (XEXP (op0, 1))
4333 : 0 : && CONST_INT_P (XVECEXP (XEXP (op0, 1), 0, 0))))
4334 : 2063 : && GET_CODE (op1) == CONST_VECTOR
4335 : 22806065 : && const_vec_duplicate_p (op1)
4336 : 22806107 : && CONST_INT_P (XVECEXP (op1, 0, 0)))
4337 : : {
4338 : 116 : unsigned HOST_WIDE_INT shift_count
4339 : : = (CONST_INT_P (XEXP (op0, 1))
4340 : 58 : ? UINTVAL (XEXP (op0, 1))
4341 : 0 : : UINTVAL (XVECEXP (XEXP (op0, 1), 0, 0)));
4342 : 58 : unsigned HOST_WIDE_INT inner_prec
4343 : 116 : = GET_MODE_PRECISION (GET_MODE_INNER (mode));
4344 : :
4345 : : /* Avoid UD shift count. */
4346 : 58 : if (shift_count < inner_prec
4347 : 58 : && (UINTVAL (XVECEXP (op1, 0, 0))
4348 : 58 : == (HOST_WIDE_INT_1U << (inner_prec - shift_count)) - 1))
4349 : 42 : return simplify_gen_binary (LSHIFTRT, mode, XEXP (op0, 0), XEXP (op0, 1));
4350 : : }
4351 : :
4352 : 22806005 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4353 : 22806005 : if (tem)
4354 : : return tem;
4355 : :
4356 : 22803365 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4357 : 22803365 : if (tem)
4358 : : return tem;
4359 : :
4360 : 22802885 : tem = simplify_associative_operation (code, mode, op0, op1);
4361 : 22802885 : if (tem)
4362 : : return tem;
4363 : : break;
4364 : :
4365 : 972380 : case UDIV:
4366 : : /* 0/x is 0 (or x&0 if x has side-effects). */
4367 : 972380 : if (trueop0 == CONST0_RTX (mode)
4368 : 288 : && !cfun->can_throw_non_call_exceptions)
4369 : : {
4370 : 288 : if (side_effects_p (op1))
4371 : 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4372 : : return trueop0;
4373 : : }
4374 : : /* x/1 is x. */
4375 : 972092 : if (trueop1 == CONST1_RTX (mode))
4376 : : {
4377 : 235884 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4378 : 235884 : if (tem)
4379 : : return tem;
4380 : : }
4381 : : /* Convert divide by power of two into shift. */
4382 : 736208 : if (CONST_INT_P (trueop1)
4383 : 1050830 : && (val = exact_log2 (UINTVAL (trueop1))) > 0)
4384 : 314622 : return simplify_gen_binary (LSHIFTRT, mode, op0,
4385 : 314622 : gen_int_shift_amount (mode, val));
4386 : : break;
4387 : :
4388 : 1159098 : case DIV:
4389 : : /* Handle floating point and integers separately. */
4390 : 1159098 : if (SCALAR_FLOAT_MODE_P (mode))
4391 : : {
4392 : : /* Maybe change 0.0 / x to 0.0. This transformation isn't
4393 : : safe for modes with NaNs, since 0.0 / 0.0 will then be
4394 : : NaN rather than 0.0. Nor is it safe for modes with signed
4395 : : zeros, since dividing 0 by a negative number gives -0.0 */
4396 : 337338 : if (trueop0 == CONST0_RTX (mode)
4397 : 2898 : && !HONOR_NANS (mode)
4398 : 14 : && !HONOR_SIGNED_ZEROS (mode)
4399 : 337352 : && ! side_effects_p (op1))
4400 : : return op0;
4401 : : /* x/1.0 is x. */
4402 : 337324 : if (trueop1 == CONST1_RTX (mode)
4403 : 337324 : && !HONOR_SNANS (mode))
4404 : : return op0;
4405 : :
4406 : 337295 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
4407 : 32254 : && trueop1 != CONST0_RTX (mode))
4408 : : {
4409 : 26131 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
4410 : :
4411 : : /* x/-1.0 is -x. */
4412 : 26131 : if (real_equal (d1, &dconstm1)
4413 : 26131 : && !HONOR_SNANS (mode))
4414 : 0 : return simplify_gen_unary (NEG, mode, op0, mode);
4415 : :
4416 : : /* Change FP division by a constant into multiplication.
4417 : : Only do this with -freciprocal-math. */
4418 : 26131 : if (flag_reciprocal_math
4419 : 26131 : && !real_equal (d1, &dconst0))
4420 : : {
4421 : 7 : REAL_VALUE_TYPE d;
4422 : 7 : real_arithmetic (&d, RDIV_EXPR, &dconst1, d1);
4423 : 7 : tem = const_double_from_real_value (d, mode);
4424 : 7 : return simplify_gen_binary (MULT, mode, op0, tem);
4425 : : }
4426 : : }
4427 : : }
4428 : 821760 : else if (SCALAR_INT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
4429 : : {
4430 : : /* 0/x is 0 (or x&0 if x has side-effects). */
4431 : 800578 : if (trueop0 == CONST0_RTX (mode)
4432 : 629 : && !cfun->can_throw_non_call_exceptions)
4433 : : {
4434 : 552 : if (side_effects_p (op1))
4435 : 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4436 : : return trueop0;
4437 : : }
4438 : : /* x/1 is x. */
4439 : 800026 : if (trueop1 == CONST1_RTX (mode))
4440 : : {
4441 : 304 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4442 : 304 : if (tem)
4443 : : return tem;
4444 : : }
4445 : : /* x/-1 is -x. */
4446 : 799722 : if (trueop1 == CONSTM1_RTX (mode))
4447 : : {
4448 : 230 : rtx x = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4449 : 230 : if (x)
4450 : 230 : return simplify_gen_unary (NEG, mode, x, mode);
4451 : : }
4452 : : }
4453 : : break;
4454 : :
4455 : 906662 : case UMOD:
4456 : : /* 0%x is 0 (or x&0 if x has side-effects). */
4457 : 906662 : if (trueop0 == CONST0_RTX (mode))
4458 : : {
4459 : 780 : if (side_effects_p (op1))
4460 : 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4461 : : return trueop0;
4462 : : }
4463 : : /* x%1 is 0 (of x&0 if x has side-effects). */
4464 : 905882 : if (trueop1 == CONST1_RTX (mode))
4465 : : {
4466 : 267855 : if (side_effects_p (op0))
4467 : 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4468 : 267855 : return CONST0_RTX (mode);
4469 : : }
4470 : : /* Implement modulus by power of two as AND. */
4471 : 638027 : if (CONST_INT_P (trueop1)
4472 : 927884 : && exact_log2 (UINTVAL (trueop1)) > 0)
4473 : 289857 : return simplify_gen_binary (AND, mode, op0,
4474 : 289857 : gen_int_mode (UINTVAL (trueop1) - 1,
4475 : : mode));
4476 : : break;
4477 : :
4478 : 342985 : case MOD:
4479 : : /* 0%x is 0 (or x&0 if x has side-effects). */
4480 : 342985 : if (trueop0 == CONST0_RTX (mode))
4481 : : {
4482 : 629 : if (side_effects_p (op1))
4483 : 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4484 : : return trueop0;
4485 : : }
4486 : : /* x%1 and x%-1 is 0 (or x&0 if x has side-effects). */
4487 : 342356 : if (trueop1 == CONST1_RTX (mode) || trueop1 == constm1_rtx)
4488 : : {
4489 : 429 : if (side_effects_p (op0))
4490 : 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4491 : 429 : return CONST0_RTX (mode);
4492 : : }
4493 : : break;
4494 : :
4495 : 129959 : case ROTATERT:
4496 : 129959 : case ROTATE:
4497 : 129959 : if (trueop1 == CONST0_RTX (mode))
4498 : : return op0;
4499 : : /* Canonicalize rotates by constant amount. If the condition of
4500 : : reversing direction is met, then reverse the direction. */
4501 : : #if defined(HAVE_rotate) && defined(HAVE_rotatert)
4502 : 129863 : if (reverse_rotate_by_imm_p (mode, (code == ROTATE), trueop1))
4503 : : {
4504 : 10958 : int new_amount = GET_MODE_UNIT_PRECISION (mode) - INTVAL (trueop1);
4505 : 10958 : rtx new_amount_rtx = gen_int_shift_amount (mode, new_amount);
4506 : 11284 : return simplify_gen_binary (code == ROTATE ? ROTATERT : ROTATE,
4507 : : mode, op0, new_amount_rtx);
4508 : : }
4509 : : #endif
4510 : : /* ROTATE/ROTATERT:HI (X:HI, 8) is BSWAP:HI (X). Other combinations
4511 : : such as SImode with a count of 16 do not correspond to RTL BSWAP
4512 : : semantics. */
4513 : 118905 : tem = unwrap_const_vec_duplicate (trueop1);
4514 : 118905 : if (GET_MODE_UNIT_BITSIZE (mode) == (2 * BITS_PER_UNIT)
4515 : 118905 : && CONST_INT_P (tem) && INTVAL (tem) == BITS_PER_UNIT)
4516 : 381 : return simplify_gen_unary (BSWAP, mode, op0, mode);
4517 : :
4518 : : /* FALLTHRU */
4519 : 5333111 : case ASHIFTRT:
4520 : 5333111 : if (trueop1 == CONST0_RTX (mode))
4521 : : return op0;
4522 : 5331399 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4523 : : return op0;
4524 : : /* Rotating ~0 always results in ~0. */
4525 : 5331244 : if (CONST_INT_P (trueop0)
4526 : 14754 : && HWI_COMPUTABLE_MODE_P (mode)
4527 : 14706 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
4528 : 5331244 : && ! side_effects_p (op1))
4529 : : return op0;
4530 : :
4531 : 32292091 : canonicalize_shift:
4532 : : /* Given:
4533 : : scalar modes M1, M2
4534 : : scalar constants c1, c2
4535 : : size (M2) > size (M1)
4536 : : c1 == size (M2) - size (M1)
4537 : : optimize:
4538 : : ([a|l]shiftrt:M1 (subreg:M1 (lshiftrt:M2 (reg:M2) (const_int <c1>))
4539 : : <low_part>)
4540 : : (const_int <c2>))
4541 : : to:
4542 : : (subreg:M1 ([a|l]shiftrt:M2 (reg:M2) (const_int <c1 + c2>))
4543 : : <low_part>). */
4544 : 32292091 : if ((code == ASHIFTRT || code == LSHIFTRT)
4545 : 12381823 : && is_a <scalar_int_mode> (mode, &int_mode)
4546 : 11638129 : && SUBREG_P (op0)
4547 : 1030209 : && CONST_INT_P (op1)
4548 : 1029217 : && GET_CODE (SUBREG_REG (op0)) == LSHIFTRT
4549 : 33854 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
4550 : : &inner_mode)
4551 : 33854 : && CONST_INT_P (XEXP (SUBREG_REG (op0), 1))
4552 : 67402 : && GET_MODE_BITSIZE (inner_mode) > GET_MODE_BITSIZE (int_mode)
4553 : 33701 : && (INTVAL (XEXP (SUBREG_REG (op0), 1))
4554 : 67402 : == GET_MODE_BITSIZE (inner_mode) - GET_MODE_BITSIZE (int_mode))
4555 : 32325170 : && subreg_lowpart_p (op0))
4556 : : {
4557 : 33079 : rtx tmp = gen_int_shift_amount
4558 : 33079 : (inner_mode, INTVAL (XEXP (SUBREG_REG (op0), 1)) + INTVAL (op1));
4559 : :
4560 : : /* Combine would usually zero out the value when combining two
4561 : : local shifts and the range becomes larger or equal to the mode.
4562 : : However since we fold away one of the shifts here combine won't
4563 : : see it so we should immediately zero the result if it's out of
4564 : : range. */
4565 : 33079 : if (code == LSHIFTRT
4566 : 62030 : && INTVAL (tmp) >= GET_MODE_BITSIZE (inner_mode))
4567 : 0 : tmp = const0_rtx;
4568 : : else
4569 : 33079 : tmp = simplify_gen_binary (code,
4570 : : inner_mode,
4571 : 33079 : XEXP (SUBREG_REG (op0), 0),
4572 : : tmp);
4573 : :
4574 : 33079 : return lowpart_subreg (int_mode, tmp, inner_mode);
4575 : : }
4576 : :
4577 : 32259012 : if (SHIFT_COUNT_TRUNCATED && CONST_INT_P (op1))
4578 : : {
4579 : : val = INTVAL (op1) & (GET_MODE_UNIT_PRECISION (mode) - 1);
4580 : : if (val != INTVAL (op1))
4581 : : return simplify_gen_binary (code, mode, op0,
4582 : : gen_int_shift_amount (mode, val));
4583 : : }
4584 : :
4585 : : /* Simplify:
4586 : :
4587 : : (code:M1
4588 : : (subreg:M1
4589 : : ([al]shiftrt:M2
4590 : : (subreg:M2
4591 : : (ashift:M1 X C1))
4592 : : C2))
4593 : : C3)
4594 : :
4595 : : to:
4596 : :
4597 : : (code:M1
4598 : : ([al]shiftrt:M1
4599 : : (ashift:M1 X C1+N)
4600 : : C2+N)
4601 : : C3)
4602 : :
4603 : : where M1 is N bits wider than M2. Optimizing the (subreg:M1 ...)
4604 : : directly would be arithmetically correct, but restricting the
4605 : : simplification to shifts by constants is more conservative,
4606 : : since it is more likely to lead to further simplifications. */
4607 : 32259012 : if (is_a<scalar_int_mode> (mode, &int_mode)
4608 : 5663653 : && paradoxical_subreg_p (op0)
4609 : 5094255 : && is_a<scalar_int_mode> (GET_MODE (SUBREG_REG (op0)), &inner_mode)
4610 : 5094119 : && (GET_CODE (SUBREG_REG (op0)) == ASHIFTRT
4611 : 5094119 : || GET_CODE (SUBREG_REG (op0)) == LSHIFTRT)
4612 : 139211 : && CONST_INT_P (op1))
4613 : : {
4614 : 139211 : auto xcode = GET_CODE (SUBREG_REG (op0));
4615 : 139211 : rtx xop0 = XEXP (SUBREG_REG (op0), 0);
4616 : 139211 : rtx xop1 = XEXP (SUBREG_REG (op0), 1);
4617 : 139211 : if (SUBREG_P (xop0)
4618 : 7324 : && GET_MODE (SUBREG_REG (xop0)) == mode
4619 : 7296 : && GET_CODE (SUBREG_REG (xop0)) == ASHIFT
4620 : 605 : && CONST_INT_P (xop1)
4621 : 139816 : && UINTVAL (xop1) < GET_MODE_PRECISION (inner_mode))
4622 : : {
4623 : 605 : rtx yop0 = XEXP (SUBREG_REG (xop0), 0);
4624 : 605 : rtx yop1 = XEXP (SUBREG_REG (xop0), 1);
4625 : 605 : if (CONST_INT_P (yop1)
4626 : 605 : && UINTVAL (yop1) < GET_MODE_PRECISION (inner_mode))
4627 : : {
4628 : 1210 : auto bias = (GET_MODE_BITSIZE (int_mode)
4629 : 605 : - GET_MODE_BITSIZE (inner_mode));
4630 : 605 : tem = simplify_gen_binary (ASHIFT, mode, yop0,
4631 : 605 : GEN_INT (INTVAL (yop1) + bias));
4632 : 605 : tem = simplify_gen_binary (xcode, mode, tem,
4633 : 605 : GEN_INT (INTVAL (xop1) + bias));
4634 : 605 : return simplify_gen_binary (code, mode, tem, op1);
4635 : : }
4636 : : }
4637 : : }
4638 : : break;
4639 : :
4640 : 0 : case SS_ASHIFT:
4641 : 0 : if (CONST_INT_P (trueop0)
4642 : 0 : && HWI_COMPUTABLE_MODE_P (mode)
4643 : 0 : && (UINTVAL (trueop0) == (GET_MODE_MASK (mode) >> 1)
4644 : 0 : || mode_signbit_p (mode, trueop0))
4645 : 0 : && ! side_effects_p (op1))
4646 : : return op0;
4647 : 0 : goto simplify_ashift;
4648 : :
4649 : 0 : case US_ASHIFT:
4650 : 0 : if (CONST_INT_P (trueop0)
4651 : 0 : && HWI_COMPUTABLE_MODE_P (mode)
4652 : 0 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
4653 : 0 : && ! side_effects_p (op1))
4654 : : return op0;
4655 : : /* FALLTHRU */
4656 : :
4657 : 20254503 : case ASHIFT:
4658 : 20254503 : simplify_ashift:
4659 : 20254503 : if (trueop1 == CONST0_RTX (mode))
4660 : : return op0;
4661 : 20066232 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4662 : : return op0;
4663 : 20035289 : if (mem_depth
4664 : 243530 : && code == ASHIFT
4665 : 243530 : && CONST_INT_P (trueop1)
4666 : 243522 : && is_a <scalar_int_mode> (mode, &int_mode)
4667 : 20278799 : && IN_RANGE (UINTVAL (trueop1),
4668 : : 1, GET_MODE_PRECISION (int_mode) - 1))
4669 : : {
4670 : 243510 : auto c = (wi::one (GET_MODE_PRECISION (int_mode))
4671 : 243510 : << UINTVAL (trueop1));
4672 : 243510 : rtx new_op1 = immed_wide_int_const (c, int_mode);
4673 : 243510 : return simplify_gen_binary (MULT, int_mode, op0, new_op1);
4674 : 243510 : }
4675 : 19791779 : goto canonicalize_shift;
4676 : :
4677 : 9061626 : case LSHIFTRT:
4678 : 9061626 : if (trueop1 == CONST0_RTX (mode))
4679 : : return op0;
4680 : 7170378 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4681 : : return op0;
4682 : : /* Optimize (lshiftrt (clz X) C) as (eq X 0). */
4683 : 7169068 : if (GET_CODE (op0) == CLZ
4684 : 0 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
4685 : 0 : && CONST_INT_P (trueop1)
4686 : : && STORE_FLAG_VALUE == 1
4687 : 7169068 : && INTVAL (trueop1) < GET_MODE_UNIT_PRECISION (mode))
4688 : : {
4689 : 0 : unsigned HOST_WIDE_INT zero_val = 0;
4690 : :
4691 : 0 : if (CLZ_DEFINED_VALUE_AT_ZERO (inner_mode, zero_val)
4692 : 0 : && zero_val == GET_MODE_PRECISION (inner_mode)
4693 : 0 : && INTVAL (trueop1) == exact_log2 (zero_val))
4694 : 0 : return simplify_gen_relational (EQ, mode, inner_mode,
4695 : 0 : XEXP (op0, 0), const0_rtx);
4696 : : }
4697 : 7169068 : goto canonicalize_shift;
4698 : :
4699 : 195086 : case SMIN:
4700 : 195086 : if (HWI_COMPUTABLE_MODE_P (mode)
4701 : 175671 : && mode_signbit_p (mode, trueop1)
4702 : 0 : && ! side_effects_p (op0))
4703 : : return op1;
4704 : 195086 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4705 : : return op0;
4706 : 194949 : tem = simplify_associative_operation (code, mode, op0, op1);
4707 : 194949 : if (tem)
4708 : : return tem;
4709 : : break;
4710 : :
4711 : 447745 : case SMAX:
4712 : 447745 : if (HWI_COMPUTABLE_MODE_P (mode)
4713 : 421343 : && CONST_INT_P (trueop1)
4714 : 395411 : && (UINTVAL (trueop1) == GET_MODE_MASK (mode) >> 1)
4715 : 0 : && ! side_effects_p (op0))
4716 : : return op1;
4717 : 447745 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4718 : : return op0;
4719 : 447640 : tem = simplify_associative_operation (code, mode, op0, op1);
4720 : 447640 : if (tem)
4721 : : return tem;
4722 : : break;
4723 : :
4724 : 222749 : case UMIN:
4725 : 222749 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
4726 : : return op1;
4727 : 222730 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4728 : : return op0;
4729 : 222630 : tem = simplify_associative_operation (code, mode, op0, op1);
4730 : 222630 : if (tem)
4731 : : return tem;
4732 : : break;
4733 : :
4734 : 216359 : case UMAX:
4735 : 216359 : if (trueop1 == constm1_rtx && ! side_effects_p (op0))
4736 : : return op1;
4737 : 216359 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4738 : : return op0;
4739 : 216269 : tem = simplify_associative_operation (code, mode, op0, op1);
4740 : 216269 : if (tem)
4741 : : return tem;
4742 : : break;
4743 : :
4744 : 11167 : case SS_PLUS:
4745 : 11167 : case US_PLUS:
4746 : 11167 : case SS_MINUS:
4747 : 11167 : case US_MINUS:
4748 : : /* Simplify x +/- 0 to x, if possible. */
4749 : 11167 : if (trueop1 == CONST0_RTX (mode))
4750 : : return op0;
4751 : : return 0;
4752 : :
4753 : 0 : case SS_MULT:
4754 : 0 : case US_MULT:
4755 : : /* Simplify x * 0 to 0, if possible. */
4756 : 0 : if (trueop1 == CONST0_RTX (mode)
4757 : 0 : && !side_effects_p (op0))
4758 : : return op1;
4759 : :
4760 : : /* Simplify x * 1 to x, if possible. */
4761 : 0 : if (trueop1 == CONST1_RTX (mode))
4762 : : return op0;
4763 : : return 0;
4764 : :
4765 : 784735 : case SMUL_HIGHPART:
4766 : 784735 : case UMUL_HIGHPART:
4767 : : /* Simplify x * 0 to 0, if possible. */
4768 : 784735 : if (trueop1 == CONST0_RTX (mode)
4769 : 784735 : && !side_effects_p (op0))
4770 : : return op1;
4771 : : return 0;
4772 : :
4773 : 0 : case SS_DIV:
4774 : 0 : case US_DIV:
4775 : : /* Simplify x / 1 to x, if possible. */
4776 : 0 : if (trueop1 == CONST1_RTX (mode))
4777 : : return op0;
4778 : : return 0;
4779 : :
4780 : 0 : case COPYSIGN:
4781 : 0 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4782 : : return op0;
4783 : 0 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1))
4784 : : {
4785 : 0 : REAL_VALUE_TYPE f1;
4786 : 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
4787 : 0 : rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
4788 : 0 : if (REAL_VALUE_NEGATIVE (f1))
4789 : 0 : tmp = simplify_unary_operation (NEG, mode, tmp, mode);
4790 : 0 : return tmp;
4791 : : }
4792 : 0 : if (GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
4793 : 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
4794 : 0 : if (GET_CODE (op1) == ABS
4795 : 0 : && ! side_effects_p (op1))
4796 : 0 : return simplify_gen_unary (ABS, mode, op0, mode);
4797 : 0 : if (GET_CODE (op0) == COPYSIGN
4798 : 0 : && ! side_effects_p (XEXP (op0, 1)))
4799 : 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
4800 : 0 : if (GET_CODE (op1) == COPYSIGN
4801 : 0 : && ! side_effects_p (XEXP (op1, 0)))
4802 : 0 : return simplify_gen_binary (COPYSIGN, mode, op0, XEXP (op1, 1));
4803 : : return 0;
4804 : :
4805 : 1251 : case VEC_SERIES:
4806 : 2502 : if (op1 == CONST0_RTX (GET_MODE_INNER (mode)))
4807 : 96 : return gen_vec_duplicate (mode, op0);
4808 : 1155 : if (valid_for_const_vector_p (mode, op0)
4809 : 1155 : && valid_for_const_vector_p (mode, op1))
4810 : 96 : return gen_const_vec_series (mode, op0, op1);
4811 : : return 0;
4812 : :
4813 : 3227363 : case VEC_SELECT:
4814 : 3227363 : if (!VECTOR_MODE_P (mode))
4815 : : {
4816 : 865791 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
4817 : 1731582 : gcc_assert (mode == GET_MODE_INNER (GET_MODE (trueop0)));
4818 : 865791 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
4819 : 865791 : gcc_assert (XVECLEN (trueop1, 0) == 1);
4820 : :
4821 : : /* We can't reason about selections made at runtime. */
4822 : 865791 : if (!CONST_INT_P (XVECEXP (trueop1, 0, 0)))
4823 : 431194689 : return 0;
4824 : :
4825 : 865791 : if (vec_duplicate_p (trueop0, &elt0))
4826 : 1798 : return elt0;
4827 : :
4828 : 863993 : if (GET_CODE (trueop0) == CONST_VECTOR)
4829 : 7133 : return CONST_VECTOR_ELT (trueop0, INTVAL (XVECEXP
4830 : : (trueop1, 0, 0)));
4831 : :
4832 : : /* Extract a scalar element from a nested VEC_SELECT expression
4833 : : (with optional nested VEC_CONCAT expression). Some targets
4834 : : (i386) extract scalar element from a vector using chain of
4835 : : nested VEC_SELECT expressions. When input operand is a memory
4836 : : operand, this operation can be simplified to a simple scalar
4837 : : load from an offseted memory address. */
4838 : 856860 : int n_elts;
4839 : 856860 : if (GET_CODE (trueop0) == VEC_SELECT
4840 : 924585 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
4841 : 67725 : .is_constant (&n_elts)))
4842 : : {
4843 : 67725 : rtx op0 = XEXP (trueop0, 0);
4844 : 67725 : rtx op1 = XEXP (trueop0, 1);
4845 : :
4846 : 67725 : int i = INTVAL (XVECEXP (trueop1, 0, 0));
4847 : 67725 : int elem;
4848 : :
4849 : 67725 : rtvec vec;
4850 : 67725 : rtx tmp_op, tmp;
4851 : :
4852 : 67725 : gcc_assert (GET_CODE (op1) == PARALLEL);
4853 : 67725 : gcc_assert (i < n_elts);
4854 : :
4855 : : /* Select element, pointed by nested selector. */
4856 : 67725 : elem = INTVAL (XVECEXP (op1, 0, i));
4857 : :
4858 : : /* Handle the case when nested VEC_SELECT wraps VEC_CONCAT. */
4859 : 67725 : if (GET_CODE (op0) == VEC_CONCAT)
4860 : : {
4861 : 27175 : rtx op00 = XEXP (op0, 0);
4862 : 27175 : rtx op01 = XEXP (op0, 1);
4863 : :
4864 : 27175 : machine_mode mode00, mode01;
4865 : 27175 : int n_elts00, n_elts01;
4866 : :
4867 : 27175 : mode00 = GET_MODE (op00);
4868 : 27175 : mode01 = GET_MODE (op01);
4869 : :
4870 : : /* Find out the number of elements of each operand.
4871 : : Since the concatenated result has a constant number
4872 : : of elements, the operands must too. */
4873 : 27175 : n_elts00 = GET_MODE_NUNITS (mode00).to_constant ();
4874 : 27175 : n_elts01 = GET_MODE_NUNITS (mode01).to_constant ();
4875 : :
4876 : 27175 : gcc_assert (n_elts == n_elts00 + n_elts01);
4877 : :
4878 : : /* Select correct operand of VEC_CONCAT
4879 : : and adjust selector. */
4880 : 27175 : if (elem < n_elts01)
4881 : : tmp_op = op00;
4882 : : else
4883 : : {
4884 : 42 : tmp_op = op01;
4885 : 42 : elem -= n_elts00;
4886 : : }
4887 : : }
4888 : : else
4889 : : tmp_op = op0;
4890 : :
4891 : 67725 : vec = rtvec_alloc (1);
4892 : 67725 : RTVEC_ELT (vec, 0) = GEN_INT (elem);
4893 : :
4894 : 67725 : tmp = gen_rtx_fmt_ee (code, mode,
4895 : : tmp_op, gen_rtx_PARALLEL (VOIDmode, vec));
4896 : 67725 : return tmp;
4897 : : }
4898 : : }
4899 : : else
4900 : : {
4901 : 2361572 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
4902 : 7084716 : gcc_assert (GET_MODE_INNER (mode)
4903 : : == GET_MODE_INNER (GET_MODE (trueop0)));
4904 : 2361572 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
4905 : :
4906 : 2361572 : if (vec_duplicate_p (trueop0, &elt0))
4907 : : /* It doesn't matter which elements are selected by trueop1,
4908 : : because they are all the same. */
4909 : 14088 : return gen_vec_duplicate (mode, elt0);
4910 : :
4911 : 2347484 : if (GET_CODE (trueop0) == CONST_VECTOR)
4912 : : {
4913 : 16788 : unsigned n_elts = XVECLEN (trueop1, 0);
4914 : 16788 : rtvec v = rtvec_alloc (n_elts);
4915 : 16788 : unsigned int i;
4916 : :
4917 : 33576 : gcc_assert (known_eq (n_elts, GET_MODE_NUNITS (mode)));
4918 : 86888 : for (i = 0; i < n_elts; i++)
4919 : : {
4920 : 70100 : rtx x = XVECEXP (trueop1, 0, i);
4921 : :
4922 : 70100 : if (!CONST_INT_P (x))
4923 : : return 0;
4924 : :
4925 : 70100 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0,
4926 : : INTVAL (x));
4927 : : }
4928 : :
4929 : 16788 : return gen_rtx_CONST_VECTOR (mode, v);
4930 : : }
4931 : :
4932 : : /* Recognize the identity. */
4933 : 2330696 : if (GET_MODE (trueop0) == mode)
4934 : : {
4935 : 532704 : bool maybe_ident = true;
4936 : 532704 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
4937 : : {
4938 : 532342 : rtx j = XVECEXP (trueop1, 0, i);
4939 : 532342 : if (!CONST_INT_P (j) || INTVAL (j) != i)
4940 : : {
4941 : : maybe_ident = false;
4942 : : break;
4943 : : }
4944 : : }
4945 : 317332 : if (maybe_ident)
4946 : : return trueop0;
4947 : : }
4948 : :
4949 : : /* If we select a low-part subreg, return that. */
4950 : 2330334 : if (vec_series_lowpart_p (mode, GET_MODE (trueop0), trueop1))
4951 : : {
4952 : 0 : rtx new_rtx = lowpart_subreg (mode, trueop0,
4953 : 0 : GET_MODE (trueop0));
4954 : 0 : if (new_rtx != NULL_RTX)
4955 : : return new_rtx;
4956 : : }
4957 : :
4958 : : /* If we build {a,b} then permute it, build the result directly. */
4959 : 2330334 : if (XVECLEN (trueop1, 0) == 2
4960 : 542889 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
4961 : 542889 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
4962 : 542889 : && GET_CODE (trueop0) == VEC_CONCAT
4963 : 167824 : && GET_CODE (XEXP (trueop0, 0)) == VEC_CONCAT
4964 : 79 : && GET_MODE (XEXP (trueop0, 0)) == mode
4965 : 79 : && GET_CODE (XEXP (trueop0, 1)) == VEC_CONCAT
4966 : 56 : && GET_MODE (XEXP (trueop0, 1)) == mode)
4967 : : {
4968 : 56 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
4969 : 56 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
4970 : 56 : rtx subop0, subop1;
4971 : :
4972 : 56 : gcc_assert (i0 < 4 && i1 < 4);
4973 : 56 : subop0 = XEXP (XEXP (trueop0, i0 / 2), i0 % 2);
4974 : 56 : subop1 = XEXP (XEXP (trueop0, i1 / 2), i1 % 2);
4975 : :
4976 : 56 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
4977 : : }
4978 : :
4979 : 2330278 : if (XVECLEN (trueop1, 0) == 2
4980 : 542833 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
4981 : 542833 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
4982 : 542833 : && GET_CODE (trueop0) == VEC_CONCAT
4983 : 167768 : && GET_MODE (trueop0) == mode)
4984 : : {
4985 : 2 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
4986 : 2 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
4987 : 2 : rtx subop0, subop1;
4988 : :
4989 : 2 : gcc_assert (i0 < 2 && i1 < 2);
4990 : 2 : subop0 = XEXP (trueop0, i0);
4991 : 2 : subop1 = XEXP (trueop0, i1);
4992 : :
4993 : 2 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
4994 : : }
4995 : :
4996 : : /* If we select one half of a vec_concat, return that. */
4997 : 2330276 : int l0, l1;
4998 : 2330276 : if (GET_CODE (trueop0) == VEC_CONCAT
4999 : 2915036 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
5000 : 1457518 : .is_constant (&l0))
5001 : 2915036 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 1)))
5002 : 1457518 : .is_constant (&l1))
5003 : 3787794 : && CONST_INT_P (XVECEXP (trueop1, 0, 0)))
5004 : : {
5005 : 1457518 : rtx subop0 = XEXP (trueop0, 0);
5006 : 1457518 : rtx subop1 = XEXP (trueop0, 1);
5007 : 1457518 : machine_mode mode0 = GET_MODE (subop0);
5008 : 1457518 : machine_mode mode1 = GET_MODE (subop1);
5009 : 1457518 : int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5010 : 1457518 : if (i0 == 0 && !side_effects_p (op1) && mode == mode0)
5011 : : {
5012 : 933155 : bool success = true;
5013 : 933155 : for (int i = 1; i < l0; ++i)
5014 : : {
5015 : 932881 : rtx j = XVECEXP (trueop1, 0, i);
5016 : 932881 : if (!CONST_INT_P (j) || INTVAL (j) != i)
5017 : : {
5018 : : success = false;
5019 : : break;
5020 : : }
5021 : : }
5022 : 849863 : if (success)
5023 : : return subop0;
5024 : : }
5025 : 1457244 : if (i0 == l0 && !side_effects_p (op0) && mode == mode1)
5026 : : {
5027 : 532 : bool success = true;
5028 : 532 : for (int i = 1; i < l1; ++i)
5029 : : {
5030 : 485 : rtx j = XVECEXP (trueop1, 0, i);
5031 : 485 : if (!CONST_INT_P (j) || INTVAL (j) != i0 + i)
5032 : : {
5033 : : success = false;
5034 : : break;
5035 : : }
5036 : : }
5037 : 76 : if (success)
5038 : : return subop1;
5039 : : }
5040 : : }
5041 : :
5042 : : /* Simplify vec_select of a subreg of X to just a vec_select of X
5043 : : when X has same component mode as vec_select. */
5044 : 2329955 : unsigned HOST_WIDE_INT subreg_offset = 0;
5045 : 2329955 : if (GET_CODE (trueop0) == SUBREG
5046 : 346601 : && GET_MODE_INNER (mode)
5047 : 693202 : == GET_MODE_INNER (GET_MODE (SUBREG_REG (trueop0)))
5048 : 27168 : && GET_MODE_NUNITS (mode).is_constant (&l1)
5049 : 2676556 : && constant_multiple_p (subreg_memory_offset (trueop0),
5050 : 27168 : GET_MODE_UNIT_BITSIZE (mode),
5051 : : &subreg_offset))
5052 : : {
5053 : 13584 : poly_uint64 nunits
5054 : 27168 : = GET_MODE_NUNITS (GET_MODE (SUBREG_REG (trueop0)));
5055 : 13584 : bool success = true;
5056 : 78192 : for (int i = 0; i != l1; i++)
5057 : : {
5058 : 74645 : rtx idx = XVECEXP (trueop1, 0, i);
5059 : 74645 : if (!CONST_INT_P (idx)
5060 : 74645 : || maybe_ge (UINTVAL (idx) + subreg_offset, nunits))
5061 : : {
5062 : : success = false;
5063 : : break;
5064 : : }
5065 : : }
5066 : :
5067 : 13584 : if (success)
5068 : : {
5069 : 3547 : rtx par = trueop1;
5070 : 3547 : if (subreg_offset)
5071 : : {
5072 : 0 : rtvec vec = rtvec_alloc (l1);
5073 : 0 : for (int i = 0; i < l1; i++)
5074 : 0 : RTVEC_ELT (vec, i)
5075 : 0 : = GEN_INT (INTVAL (XVECEXP (trueop1, 0, i))
5076 : : + subreg_offset);
5077 : 0 : par = gen_rtx_PARALLEL (VOIDmode, vec);
5078 : : }
5079 : 3547 : return gen_rtx_VEC_SELECT (mode, SUBREG_REG (trueop0), par);
5080 : : }
5081 : : }
5082 : : }
5083 : :
5084 : 3115543 : if (XVECLEN (trueop1, 0) == 1
5085 : 789219 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5086 : 789219 : && GET_CODE (trueop0) == VEC_CONCAT)
5087 : : {
5088 : 1182 : rtx vec = trueop0;
5089 : 2364 : offset = INTVAL (XVECEXP (trueop1, 0, 0)) * GET_MODE_SIZE (mode);
5090 : :
5091 : : /* Try to find the element in the VEC_CONCAT. */
5092 : 1182 : while (GET_MODE (vec) != mode
5093 : 2364 : && GET_CODE (vec) == VEC_CONCAT)
5094 : : {
5095 : 1182 : poly_int64 vec_size;
5096 : :
5097 : 1182 : if (CONST_INT_P (XEXP (vec, 0)))
5098 : : {
5099 : : /* vec_concat of two const_ints doesn't make sense with
5100 : : respect to modes. */
5101 : 1 : if (CONST_INT_P (XEXP (vec, 1)))
5102 : 370266230 : return 0;
5103 : :
5104 : 1 : vec_size = GET_MODE_SIZE (GET_MODE (trueop0))
5105 : 3 : - GET_MODE_SIZE (GET_MODE (XEXP (vec, 1)));
5106 : : }
5107 : : else
5108 : 2362 : vec_size = GET_MODE_SIZE (GET_MODE (XEXP (vec, 0)));
5109 : :
5110 : 1182 : if (known_lt (offset, vec_size))
5111 : : vec = XEXP (vec, 0);
5112 : 264 : else if (known_ge (offset, vec_size))
5113 : : {
5114 : 264 : offset -= vec_size;
5115 : 264 : vec = XEXP (vec, 1);
5116 : : }
5117 : : else
5118 : : break;
5119 : 1182 : vec = avoid_constant_pool_reference (vec);
5120 : : }
5121 : :
5122 : 1182 : if (GET_MODE (vec) == mode)
5123 : : return vec;
5124 : : }
5125 : :
5126 : : /* If we select elements in a vec_merge that all come from the same
5127 : : operand, select from that operand directly. */
5128 : 3114534 : if (GET_CODE (op0) == VEC_MERGE)
5129 : : {
5130 : 7487 : rtx trueop02 = avoid_constant_pool_reference (XEXP (op0, 2));
5131 : 7487 : if (CONST_INT_P (trueop02))
5132 : : {
5133 : 2459 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop02);
5134 : 2459 : bool all_operand0 = true;
5135 : 2459 : bool all_operand1 = true;
5136 : 8996 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
5137 : : {
5138 : 6537 : rtx j = XVECEXP (trueop1, 0, i);
5139 : 6537 : if (sel & (HOST_WIDE_INT_1U << UINTVAL (j)))
5140 : : all_operand1 = false;
5141 : : else
5142 : 3199 : all_operand0 = false;
5143 : : }
5144 : 2459 : if (all_operand0 && !side_effects_p (XEXP (op0, 1)))
5145 : 710 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 0), op1);
5146 : 1749 : if (all_operand1 && !side_effects_p (XEXP (op0, 0)))
5147 : 47 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 1), op1);
5148 : : }
5149 : : }
5150 : :
5151 : : /* If we have two nested selects that are inverses of each
5152 : : other, replace them with the source operand. */
5153 : 3113777 : if (GET_CODE (trueop0) == VEC_SELECT
5154 : 69044 : && GET_MODE (XEXP (trueop0, 0)) == mode)
5155 : : {
5156 : 1275 : rtx op0_subop1 = XEXP (trueop0, 1);
5157 : 1275 : gcc_assert (GET_CODE (op0_subop1) == PARALLEL);
5158 : 2550 : gcc_assert (known_eq (XVECLEN (trueop1, 0), GET_MODE_NUNITS (mode)));
5159 : :
5160 : : /* Apply the outer ordering vector to the inner one. (The inner
5161 : : ordering vector is expressly permitted to be of a different
5162 : : length than the outer one.) If the result is { 0, 1, ..., n-1 }
5163 : : then the two VEC_SELECTs cancel. */
5164 : 1581 : for (int i = 0; i < XVECLEN (trueop1, 0); ++i)
5165 : : {
5166 : 1581 : rtx x = XVECEXP (trueop1, 0, i);
5167 : 1581 : if (!CONST_INT_P (x))
5168 : : return 0;
5169 : 1581 : rtx y = XVECEXP (op0_subop1, 0, INTVAL (x));
5170 : 1581 : if (!CONST_INT_P (y) || i != INTVAL (y))
5171 : : return 0;
5172 : : }
5173 : : return XEXP (trueop0, 0);
5174 : : }
5175 : :
5176 : : return 0;
5177 : 4096554 : case VEC_CONCAT:
5178 : 4096554 : {
5179 : 4096554 : machine_mode op0_mode = (GET_MODE (trueop0) != VOIDmode
5180 : 4096554 : ? GET_MODE (trueop0)
5181 : 4096554 : : GET_MODE_INNER (mode));
5182 : 4096554 : machine_mode op1_mode = (GET_MODE (trueop1) != VOIDmode
5183 : 4096554 : ? GET_MODE (trueop1)
5184 : 4096554 : : GET_MODE_INNER (mode));
5185 : :
5186 : 4096554 : gcc_assert (VECTOR_MODE_P (mode));
5187 : 16386216 : gcc_assert (known_eq (GET_MODE_SIZE (op0_mode)
5188 : : + GET_MODE_SIZE (op1_mode),
5189 : : GET_MODE_SIZE (mode)));
5190 : :
5191 : 4096554 : if (VECTOR_MODE_P (op0_mode))
5192 : 5465520 : gcc_assert (GET_MODE_INNER (mode)
5193 : : == GET_MODE_INNER (op0_mode));
5194 : : else
5195 : 4549428 : gcc_assert (GET_MODE_INNER (mode) == op0_mode);
5196 : :
5197 : 4096554 : if (VECTOR_MODE_P (op1_mode))
5198 : 5465520 : gcc_assert (GET_MODE_INNER (mode)
5199 : : == GET_MODE_INNER (op1_mode));
5200 : : else
5201 : 4549428 : gcc_assert (GET_MODE_INNER (mode) == op1_mode);
5202 : :
5203 : 4096554 : unsigned int n_elts, in_n_elts;
5204 : 4096554 : if ((GET_CODE (trueop0) == CONST_VECTOR
5205 : 4096554 : || CONST_SCALAR_INT_P (trueop0)
5206 : 3921487 : || CONST_DOUBLE_AS_FLOAT_P (trueop0))
5207 : 176517 : && (GET_CODE (trueop1) == CONST_VECTOR
5208 : 176517 : || CONST_SCALAR_INT_P (trueop1)
5209 : 169368 : || CONST_DOUBLE_AS_FLOAT_P (trueop1))
5210 : 14298 : && GET_MODE_NUNITS (mode).is_constant (&n_elts)
5211 : 4103703 : && GET_MODE_NUNITS (op0_mode).is_constant (&in_n_elts))
5212 : : {
5213 : 7149 : rtvec v = rtvec_alloc (n_elts);
5214 : 7149 : unsigned int i;
5215 : 121710 : for (i = 0; i < n_elts; i++)
5216 : : {
5217 : 107412 : if (i < in_n_elts)
5218 : : {
5219 : 53610 : if (!VECTOR_MODE_P (op0_mode))
5220 : 0 : RTVEC_ELT (v, i) = trueop0;
5221 : : else
5222 : 53610 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0, i);
5223 : : }
5224 : : else
5225 : : {
5226 : 53802 : if (!VECTOR_MODE_P (op1_mode))
5227 : 0 : RTVEC_ELT (v, i) = trueop1;
5228 : : else
5229 : 53802 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop1,
5230 : : i - in_n_elts);
5231 : : }
5232 : : }
5233 : :
5234 : 7149 : return gen_rtx_CONST_VECTOR (mode, v);
5235 : : }
5236 : :
5237 : : /* Try to merge two VEC_SELECTs from the same vector into a single one.
5238 : : Restrict the transformation to avoid generating a VEC_SELECT with a
5239 : : mode unrelated to its operand. */
5240 : 4089405 : if (GET_CODE (trueop0) == VEC_SELECT
5241 : 115682 : && GET_CODE (trueop1) == VEC_SELECT
5242 : 23877 : && rtx_equal_p (XEXP (trueop0, 0), XEXP (trueop1, 0))
5243 : 4106021 : && GET_MODE_INNER (GET_MODE (XEXP (trueop0, 0)))
5244 : 33232 : == GET_MODE_INNER(mode))
5245 : : {
5246 : 16616 : rtx par0 = XEXP (trueop0, 1);
5247 : 16616 : rtx par1 = XEXP (trueop1, 1);
5248 : 16616 : int len0 = XVECLEN (par0, 0);
5249 : 16616 : int len1 = XVECLEN (par1, 0);
5250 : 16616 : rtvec vec = rtvec_alloc (len0 + len1);
5251 : 99933 : for (int i = 0; i < len0; i++)
5252 : 83317 : RTVEC_ELT (vec, i) = XVECEXP (par0, 0, i);
5253 : 99933 : for (int i = 0; i < len1; i++)
5254 : 83317 : RTVEC_ELT (vec, len0 + i) = XVECEXP (par1, 0, i);
5255 : 16616 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (trueop0, 0),
5256 : 16616 : gen_rtx_PARALLEL (VOIDmode, vec));
5257 : : }
5258 : : /* (vec_concat:
5259 : : (subreg_lowpart:N OP)
5260 : : (vec_select:N OP P)) --> OP when P selects the high half
5261 : : of the OP. */
5262 : 4072789 : if (GET_CODE (trueop0) == SUBREG
5263 : 456770 : && subreg_lowpart_p (trueop0)
5264 : 456525 : && GET_CODE (trueop1) == VEC_SELECT
5265 : 11 : && SUBREG_REG (trueop0) == XEXP (trueop1, 0)
5266 : 0 : && !side_effects_p (XEXP (trueop1, 0))
5267 : 4072789 : && vec_series_highpart_p (op1_mode, mode, XEXP (trueop1, 1)))
5268 : 0 : return XEXP (trueop1, 0);
5269 : : }
5270 : : return 0;
5271 : :
5272 : 0 : default:
5273 : 0 : gcc_unreachable ();
5274 : : }
5275 : :
5276 : 362282767 : if (mode == GET_MODE (op0)
5277 : 313911544 : && mode == GET_MODE (op1)
5278 : 94789035 : && vec_duplicate_p (op0, &elt0)
5279 : 362392551 : && vec_duplicate_p (op1, &elt1))
5280 : : {
5281 : : /* Try applying the operator to ELT and see if that simplifies.
5282 : : We can duplicate the result if so.
5283 : :
5284 : : The reason we don't use simplify_gen_binary is that it isn't
5285 : : necessarily a win to convert things like:
5286 : :
5287 : : (plus:V (vec_duplicate:V (reg:S R1))
5288 : : (vec_duplicate:V (reg:S R2)))
5289 : :
5290 : : to:
5291 : :
5292 : : (vec_duplicate:V (plus:S (reg:S R1) (reg:S R2)))
5293 : :
5294 : : The first might be done entirely in vector registers while the
5295 : : second might need a move between register files. */
5296 : 82 : tem = simplify_binary_operation (code, GET_MODE_INNER (mode),
5297 : : elt0, elt1);
5298 : 41 : if (tem)
5299 : 2 : return gen_vec_duplicate (mode, tem);
5300 : : }
5301 : :
5302 : : return 0;
5303 : : }
5304 : :
5305 : : /* Return true if binary operation OP distributes over addition in operand
5306 : : OPNO, with the other operand being held constant. OPNO counts from 1. */
5307 : :
5308 : : static bool
5309 : 24741 : distributes_over_addition_p (rtx_code op, int opno)
5310 : : {
5311 : 0 : switch (op)
5312 : : {
5313 : : case PLUS:
5314 : : case MINUS:
5315 : : case MULT:
5316 : : return true;
5317 : :
5318 : 0 : case ASHIFT:
5319 : 0 : return opno == 1;
5320 : :
5321 : 0 : default:
5322 : 0 : return false;
5323 : : }
5324 : : }
5325 : :
5326 : : rtx
5327 : 464451638 : simplify_const_binary_operation (enum rtx_code code, machine_mode mode,
5328 : : rtx op0, rtx op1)
5329 : : {
5330 : 464451638 : if (VECTOR_MODE_P (mode)
5331 : 13875197 : && code != VEC_CONCAT
5332 : 9776718 : && GET_CODE (op0) == CONST_VECTOR
5333 : 199657 : && GET_CODE (op1) == CONST_VECTOR)
5334 : : {
5335 : 25464 : bool step_ok_p;
5336 : 25464 : if (CONST_VECTOR_STEPPED_P (op0)
5337 : 25464 : && CONST_VECTOR_STEPPED_P (op1))
5338 : : /* We can operate directly on the encoding if:
5339 : :
5340 : : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
5341 : : implies
5342 : : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
5343 : :
5344 : : Addition and subtraction are the supported operators
5345 : : for which this is true. */
5346 : 723 : step_ok_p = (code == PLUS || code == MINUS);
5347 : 24741 : else if (CONST_VECTOR_STEPPED_P (op0))
5348 : : /* We can operate directly on stepped encodings if:
5349 : :
5350 : : a3 - a2 == a2 - a1
5351 : : implies:
5352 : : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
5353 : :
5354 : : which is true if (x -> x op c) distributes over addition. */
5355 : 1067 : step_ok_p = distributes_over_addition_p (code, 1);
5356 : : else
5357 : : /* Similarly in reverse. */
5358 : 23674 : step_ok_p = distributes_over_addition_p (code, 2);
5359 : 25464 : rtx_vector_builder builder;
5360 : 25464 : if (!builder.new_binary_operation (mode, op0, op1, step_ok_p))
5361 : : return 0;
5362 : :
5363 : 25464 : unsigned int count = builder.encoded_nelts ();
5364 : 104052 : for (unsigned int i = 0; i < count; i++)
5365 : : {
5366 : 157444 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
5367 : : CONST_VECTOR_ELT (op0, i),
5368 : 78722 : CONST_VECTOR_ELT (op1, i));
5369 : 78722 : if (!x || !valid_for_const_vector_p (mode, x))
5370 : 134 : return 0;
5371 : 78588 : builder.quick_push (x);
5372 : : }
5373 : 25330 : return builder.build ();
5374 : 25464 : }
5375 : :
5376 : 464426174 : if (VECTOR_MODE_P (mode)
5377 : 13849733 : && code == VEC_CONCAT
5378 : 4098479 : && (CONST_SCALAR_INT_P (op0)
5379 : 3942623 : || CONST_FIXED_P (op0)
5380 : 3942623 : || CONST_DOUBLE_AS_FLOAT_P (op0))
5381 : 158568 : && (CONST_SCALAR_INT_P (op1)
5382 : 157905 : || CONST_DOUBLE_AS_FLOAT_P (op1)
5383 : 156643 : || CONST_FIXED_P (op1)))
5384 : : {
5385 : : /* Both inputs have a constant number of elements, so the result
5386 : : must too. */
5387 : 1925 : unsigned n_elts = GET_MODE_NUNITS (mode).to_constant ();
5388 : 1925 : rtvec v = rtvec_alloc (n_elts);
5389 : :
5390 : 1925 : gcc_assert (n_elts >= 2);
5391 : 1925 : if (n_elts == 2)
5392 : : {
5393 : 1925 : gcc_assert (GET_CODE (op0) != CONST_VECTOR);
5394 : 1925 : gcc_assert (GET_CODE (op1) != CONST_VECTOR);
5395 : :
5396 : 1925 : RTVEC_ELT (v, 0) = op0;
5397 : 1925 : RTVEC_ELT (v, 1) = op1;
5398 : : }
5399 : : else
5400 : : {
5401 : 0 : unsigned op0_n_elts = GET_MODE_NUNITS (GET_MODE (op0)).to_constant ();
5402 : 0 : unsigned op1_n_elts = GET_MODE_NUNITS (GET_MODE (op1)).to_constant ();
5403 : 0 : unsigned i;
5404 : :
5405 : 0 : gcc_assert (GET_CODE (op0) == CONST_VECTOR);
5406 : 0 : gcc_assert (GET_CODE (op1) == CONST_VECTOR);
5407 : 0 : gcc_assert (op0_n_elts + op1_n_elts == n_elts);
5408 : :
5409 : 0 : for (i = 0; i < op0_n_elts; ++i)
5410 : 0 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (op0, i);
5411 : 0 : for (i = 0; i < op1_n_elts; ++i)
5412 : 0 : RTVEC_ELT (v, op0_n_elts+i) = CONST_VECTOR_ELT (op1, i);
5413 : : }
5414 : :
5415 : 1925 : return gen_rtx_CONST_VECTOR (mode, v);
5416 : : }
5417 : :
5418 : 453475324 : if (VECTOR_MODE_P (mode)
5419 : 13847808 : && GET_CODE (op0) == CONST_VECTOR
5420 : 194067 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1))
5421 : 464424249 : && (CONST_VECTOR_DUPLICATE_P (op0)
5422 : : || CONST_VECTOR_NUNITS (op0).is_constant ()))
5423 : : {
5424 : 134945 : switch (code)
5425 : : {
5426 : 134945 : case PLUS:
5427 : 134945 : case MINUS:
5428 : 134945 : case MULT:
5429 : 134945 : case DIV:
5430 : 134945 : case MOD:
5431 : 134945 : case UDIV:
5432 : 134945 : case UMOD:
5433 : 134945 : case AND:
5434 : 134945 : case IOR:
5435 : 134945 : case XOR:
5436 : 134945 : case SMIN:
5437 : 134945 : case SMAX:
5438 : 134945 : case UMIN:
5439 : 134945 : case UMAX:
5440 : 134945 : case LSHIFTRT:
5441 : 134945 : case ASHIFTRT:
5442 : 134945 : case ASHIFT:
5443 : 134945 : case ROTATE:
5444 : 134945 : case ROTATERT:
5445 : 134945 : case SS_PLUS:
5446 : 134945 : case US_PLUS:
5447 : 134945 : case SS_MINUS:
5448 : 134945 : case US_MINUS:
5449 : 134945 : case SS_ASHIFT:
5450 : 134945 : case US_ASHIFT:
5451 : 134945 : case COPYSIGN:
5452 : 134945 : break;
5453 : : default:
5454 : : return NULL_RTX;
5455 : : }
5456 : :
5457 : 134945 : unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op0)
5458 : 134945 : ? CONST_VECTOR_NPATTERNS (op0)
5459 : 142501 : : CONST_VECTOR_NUNITS (op0).to_constant ());
5460 : 134945 : rtx_vector_builder builder (mode, npatterns, 1);
5461 : 282742 : for (unsigned i = 0; i < npatterns; i++)
5462 : : {
5463 : 295594 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
5464 : 147797 : CONST_VECTOR_ELT (op0, i), op1);
5465 : 147797 : if (!x || !valid_for_const_vector_p (mode, x))
5466 : 0 : return 0;
5467 : 147797 : builder.quick_push (x);
5468 : : }
5469 : 134945 : return builder.build ();
5470 : : }
5471 : :
5472 : 464289304 : if (SCALAR_FLOAT_MODE_P (mode)
5473 : 6470363 : && CONST_DOUBLE_AS_FLOAT_P (op0)
5474 : 112699 : && CONST_DOUBLE_AS_FLOAT_P (op1)
5475 : 47893 : && mode == GET_MODE (op0) && mode == GET_MODE (op1))
5476 : : {
5477 : 47893 : if (code == AND
5478 : : || code == IOR
5479 : 47893 : || code == XOR)
5480 : : {
5481 : 38795 : long tmp0[4];
5482 : 38795 : long tmp1[4];
5483 : 38795 : REAL_VALUE_TYPE r;
5484 : 38795 : int i;
5485 : :
5486 : 38795 : real_to_target (tmp0, CONST_DOUBLE_REAL_VALUE (op0),
5487 : 38795 : GET_MODE (op0));
5488 : 38795 : real_to_target (tmp1, CONST_DOUBLE_REAL_VALUE (op1),
5489 : 38795 : GET_MODE (op1));
5490 : 193975 : for (i = 0; i < 4; i++)
5491 : : {
5492 : 155180 : switch (code)
5493 : : {
5494 : 150116 : case AND:
5495 : 150116 : tmp0[i] &= tmp1[i];
5496 : 150116 : break;
5497 : 2784 : case IOR:
5498 : 2784 : tmp0[i] |= tmp1[i];
5499 : 2784 : break;
5500 : 2280 : case XOR:
5501 : 2280 : tmp0[i] ^= tmp1[i];
5502 : 2280 : break;
5503 : : default:
5504 : : gcc_unreachable ();
5505 : : }
5506 : : }
5507 : 38795 : real_from_target (&r, tmp0, mode);
5508 : 38795 : return const_double_from_real_value (r, mode);
5509 : : }
5510 : 9098 : else if (code == COPYSIGN)
5511 : : {
5512 : 0 : REAL_VALUE_TYPE f0, f1;
5513 : 0 : real_convert (&f0, mode, CONST_DOUBLE_REAL_VALUE (op0));
5514 : 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (op1));
5515 : 0 : real_copysign (&f0, &f1);
5516 : 0 : return const_double_from_real_value (f0, mode);
5517 : : }
5518 : : else
5519 : : {
5520 : 9098 : REAL_VALUE_TYPE f0, f1, value, result;
5521 : 9098 : const REAL_VALUE_TYPE *opr0, *opr1;
5522 : 9098 : bool inexact;
5523 : :
5524 : 9098 : opr0 = CONST_DOUBLE_REAL_VALUE (op0);
5525 : 9098 : opr1 = CONST_DOUBLE_REAL_VALUE (op1);
5526 : :
5527 : 9098 : if (HONOR_SNANS (mode)
5528 : 9098 : && (REAL_VALUE_ISSIGNALING_NAN (*opr0)
5529 : 803 : || REAL_VALUE_ISSIGNALING_NAN (*opr1)))
5530 : 10 : return 0;
5531 : :
5532 : 9088 : real_convert (&f0, mode, opr0);
5533 : 9088 : real_convert (&f1, mode, opr1);
5534 : :
5535 : 9088 : if (code == DIV
5536 : 4140 : && real_equal (&f1, &dconst0)
5537 : 12760 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
5538 : 3668 : return 0;
5539 : :
5540 : 27000 : if (MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
5541 : 5329 : && flag_trapping_math
5542 : 5253 : && REAL_VALUE_ISINF (f0) && REAL_VALUE_ISINF (f1))
5543 : : {
5544 : 9 : int s0 = REAL_VALUE_NEGATIVE (f0);
5545 : 9 : int s1 = REAL_VALUE_NEGATIVE (f1);
5546 : :
5547 : 9 : switch (code)
5548 : : {
5549 : 0 : case PLUS:
5550 : : /* Inf + -Inf = NaN plus exception. */
5551 : 0 : if (s0 != s1)
5552 : : return 0;
5553 : : break;
5554 : 0 : case MINUS:
5555 : : /* Inf - Inf = NaN plus exception. */
5556 : 0 : if (s0 == s1)
5557 : : return 0;
5558 : : break;
5559 : : case DIV:
5560 : : /* Inf / Inf = NaN plus exception. */
5561 : : return 0;
5562 : : default:
5563 : : break;
5564 : : }
5565 : : }
5566 : :
5567 : 8084 : if (code == MULT && MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
5568 : 1988 : && flag_trapping_math
5569 : 7353 : && ((REAL_VALUE_ISINF (f0) && real_equal (&f1, &dconst0))
5570 : 1934 : || (REAL_VALUE_ISINF (f1)
5571 : 16 : && real_equal (&f0, &dconst0))))
5572 : : /* Inf * 0 = NaN plus exception. */
5573 : 24 : return 0;
5574 : :
5575 : 5387 : inexact = real_arithmetic (&value, rtx_to_tree_code (code),
5576 : : &f0, &f1);
5577 : 5387 : real_convert (&result, mode, &value);
5578 : :
5579 : : /* Don't constant fold this floating point operation if
5580 : : the result has overflowed and flag_trapping_math. */
5581 : :
5582 : 5387 : if (flag_trapping_math
5583 : 20880 : && MODE_HAS_INFINITIES (mode)
5584 : 5220 : && REAL_VALUE_ISINF (result)
5585 : 1104 : && !REAL_VALUE_ISINF (f0)
5586 : 6477 : && !REAL_VALUE_ISINF (f1))
5587 : : /* Overflow plus exception. */
5588 : 1090 : return 0;
5589 : :
5590 : : /* Don't constant fold this floating point operation if the
5591 : : result may dependent upon the run-time rounding mode and
5592 : : flag_rounding_math is set, or if GCC's software emulation
5593 : : is unable to accurately represent the result. */
5594 : :
5595 : 4297 : if ((flag_rounding_math
5596 : 27398 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
5597 : 4297 : && (inexact || !real_identical (&result, &value)))
5598 : 378 : return NULL_RTX;
5599 : :
5600 : 3919 : return const_double_from_real_value (result, mode);
5601 : : }
5602 : : }
5603 : :
5604 : : /* We can fold some multi-word operations. */
5605 : 464241411 : scalar_int_mode int_mode;
5606 : 464241411 : if (is_a <scalar_int_mode> (mode, &int_mode)
5607 : 401261416 : && CONST_SCALAR_INT_P (op0)
5608 : 39522686 : && CONST_SCALAR_INT_P (op1)
5609 : 33052764 : && GET_MODE_PRECISION (int_mode) <= MAX_BITSIZE_MODE_ANY_INT)
5610 : : {
5611 : 33052764 : wide_int result;
5612 : 33052764 : wi::overflow_type overflow;
5613 : 33052764 : rtx_mode_t pop0 = rtx_mode_t (op0, int_mode);
5614 : 33052764 : rtx_mode_t pop1 = rtx_mode_t (op1, int_mode);
5615 : :
5616 : : #if TARGET_SUPPORTS_WIDE_INT == 0
5617 : : /* This assert keeps the simplification from producing a result
5618 : : that cannot be represented in a CONST_DOUBLE but a lot of
5619 : : upstream callers expect that this function never fails to
5620 : : simplify something and so you if you added this to the test
5621 : : above the code would die later anyway. If this assert
5622 : : happens, you just need to make the port support wide int. */
5623 : : gcc_assert (GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_DOUBLE_INT);
5624 : : #endif
5625 : 33052764 : switch (code)
5626 : : {
5627 : 1094148 : case MINUS:
5628 : 1094148 : result = wi::sub (pop0, pop1);
5629 : 1094148 : break;
5630 : :
5631 : 25515592 : case PLUS:
5632 : 25515592 : result = wi::add (pop0, pop1);
5633 : 25515592 : break;
5634 : :
5635 : 358114 : case MULT:
5636 : 358114 : result = wi::mul (pop0, pop1);
5637 : 358114 : break;
5638 : :
5639 : 5197 : case DIV:
5640 : 5197 : result = wi::div_trunc (pop0, pop1, SIGNED, &overflow);
5641 : 5197 : if (overflow)
5642 : : return NULL_RTX;
5643 : : break;
5644 : :
5645 : 217 : case MOD:
5646 : 217 : result = wi::mod_trunc (pop0, pop1, SIGNED, &overflow);
5647 : 217 : if (overflow)
5648 : : return NULL_RTX;
5649 : : break;
5650 : :
5651 : 6707 : case UDIV:
5652 : 6707 : result = wi::div_trunc (pop0, pop1, UNSIGNED, &overflow);
5653 : 6707 : if (overflow)
5654 : : return NULL_RTX;
5655 : : break;
5656 : :
5657 : 7801 : case UMOD:
5658 : 7801 : result = wi::mod_trunc (pop0, pop1, UNSIGNED, &overflow);
5659 : 7801 : if (overflow)
5660 : : return NULL_RTX;
5661 : : break;
5662 : :
5663 : 537991 : case AND:
5664 : 537991 : result = wi::bit_and (pop0, pop1);
5665 : 537991 : break;
5666 : :
5667 : 225985 : case IOR:
5668 : 225985 : result = wi::bit_or (pop0, pop1);
5669 : 225985 : break;
5670 : :
5671 : 83512 : case XOR:
5672 : 83512 : result = wi::bit_xor (pop0, pop1);
5673 : 83512 : break;
5674 : :
5675 : 1757 : case SMIN:
5676 : 1757 : result = wi::smin (pop0, pop1);
5677 : 1757 : break;
5678 : :
5679 : 1813 : case SMAX:
5680 : 1813 : result = wi::smax (pop0, pop1);
5681 : 1813 : break;
5682 : :
5683 : 3160 : case UMIN:
5684 : 3160 : result = wi::umin (pop0, pop1);
5685 : 3160 : break;
5686 : :
5687 : 2706 : case UMAX:
5688 : 2706 : result = wi::umax (pop0, pop1);
5689 : 2706 : break;
5690 : :
5691 : 5166586 : case LSHIFTRT:
5692 : 5166586 : case ASHIFTRT:
5693 : 5166586 : case ASHIFT:
5694 : 5166586 : case SS_ASHIFT:
5695 : 5166586 : case US_ASHIFT:
5696 : 5166586 : {
5697 : : /* The shift count might be in SImode while int_mode might
5698 : : be narrower. On IA-64 it is even DImode. If the shift
5699 : : count is too large and doesn't fit into int_mode, we'd
5700 : : ICE. So, if int_mode is narrower than word, use
5701 : : word_mode for the shift count. */
5702 : 5166586 : if (GET_MODE (op1) == VOIDmode
5703 : 5608400 : && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD)
5704 : 1575121 : pop1 = rtx_mode_t (op1, word_mode);
5705 : :
5706 : 5166586 : wide_int wop1 = pop1;
5707 : 5166586 : if (SHIFT_COUNT_TRUNCATED)
5708 : : wop1 = wi::umod_trunc (wop1, GET_MODE_PRECISION (int_mode));
5709 : 5166586 : else if (wi::geu_p (wop1, GET_MODE_PRECISION (int_mode)))
5710 : 124 : return NULL_RTX;
5711 : :
5712 : 5166462 : switch (code)
5713 : : {
5714 : 2736319 : case LSHIFTRT:
5715 : 2736319 : result = wi::lrshift (pop0, wop1);
5716 : 2736319 : break;
5717 : :
5718 : 58831 : case ASHIFTRT:
5719 : 58831 : result = wi::arshift (pop0, wop1);
5720 : 58831 : break;
5721 : :
5722 : 2371312 : case ASHIFT:
5723 : 2371312 : result = wi::lshift (pop0, wop1);
5724 : 2371312 : break;
5725 : :
5726 : 0 : case SS_ASHIFT:
5727 : 0 : if (wi::leu_p (wop1, wi::clrsb (pop0)))
5728 : 0 : result = wi::lshift (pop0, wop1);
5729 : 0 : else if (wi::neg_p (pop0))
5730 : 0 : result = wi::min_value (int_mode, SIGNED);
5731 : : else
5732 : 0 : result = wi::max_value (int_mode, SIGNED);
5733 : : break;
5734 : :
5735 : 0 : case US_ASHIFT:
5736 : 0 : if (wi::eq_p (pop0, 0))
5737 : 0 : result = pop0;
5738 : 0 : else if (wi::leu_p (wop1, wi::clz (pop0)))
5739 : 0 : result = wi::lshift (pop0, wop1);
5740 : : else
5741 : 0 : result = wi::max_value (int_mode, UNSIGNED);
5742 : : break;
5743 : :
5744 : 0 : default:
5745 : 0 : gcc_unreachable ();
5746 : : }
5747 : 5166462 : break;
5748 : 5166586 : }
5749 : 32914 : case ROTATE:
5750 : 32914 : case ROTATERT:
5751 : 32914 : {
5752 : : /* The rotate count might be in SImode while int_mode might
5753 : : be narrower. On IA-64 it is even DImode. If the shift
5754 : : count is too large and doesn't fit into int_mode, we'd
5755 : : ICE. So, if int_mode is narrower than word, use
5756 : : word_mode for the shift count. */
5757 : 32914 : if (GET_MODE (op1) == VOIDmode
5758 : 41689 : && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD)
5759 : 19439 : pop1 = rtx_mode_t (op1, word_mode);
5760 : :
5761 : 32914 : if (wi::neg_p (pop1))
5762 : : return NULL_RTX;
5763 : :
5764 : 32844 : switch (code)
5765 : : {
5766 : 11183 : case ROTATE:
5767 : 11183 : result = wi::lrotate (pop0, pop1);
5768 : 11183 : break;
5769 : :
5770 : 21661 : case ROTATERT:
5771 : 21661 : result = wi::rrotate (pop0, pop1);
5772 : 21661 : break;
5773 : :
5774 : 0 : default:
5775 : 0 : gcc_unreachable ();
5776 : : }
5777 : : break;
5778 : : }
5779 : :
5780 : 2270 : case SS_PLUS:
5781 : 2270 : result = wi::add (pop0, pop1, SIGNED, &overflow);
5782 : 4484 : clamp_signed_saturation:
5783 : 4484 : if (overflow == wi::OVF_OVERFLOW)
5784 : 314 : result = wi::max_value (GET_MODE_PRECISION (int_mode), SIGNED);
5785 : 4170 : else if (overflow == wi::OVF_UNDERFLOW)
5786 : 278 : result = wi::min_value (GET_MODE_PRECISION (int_mode), SIGNED);
5787 : 3892 : else if (overflow != wi::OVF_NONE)
5788 : : return NULL_RTX;
5789 : : break;
5790 : :
5791 : 2220 : case US_PLUS:
5792 : 2220 : result = wi::add (pop0, pop1, UNSIGNED, &overflow);
5793 : 2220 : clamp_unsigned_saturation:
5794 : 2220 : if (overflow != wi::OVF_NONE)
5795 : 461 : result = wi::max_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
5796 : : break;
5797 : :
5798 : 2214 : case SS_MINUS:
5799 : 2214 : result = wi::sub (pop0, pop1, SIGNED, &overflow);
5800 : 2214 : goto clamp_signed_saturation;
5801 : :
5802 : 1852 : case US_MINUS:
5803 : 1852 : result = wi::sub (pop0, pop1, UNSIGNED, &overflow);
5804 : 1852 : if (overflow != wi::OVF_NONE)
5805 : 1203 : result = wi::min_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
5806 : : break;
5807 : :
5808 : 0 : case SS_MULT:
5809 : 0 : result = wi::mul (pop0, pop1, SIGNED, &overflow);
5810 : 0 : goto clamp_signed_saturation;
5811 : :
5812 : 0 : case US_MULT:
5813 : 0 : result = wi::mul (pop0, pop1, UNSIGNED, &overflow);
5814 : 0 : goto clamp_unsigned_saturation;
5815 : :
5816 : 8 : case SMUL_HIGHPART:
5817 : 8 : result = wi::mul_high (pop0, pop1, SIGNED);
5818 : 8 : break;
5819 : :
5820 : 0 : case UMUL_HIGHPART:
5821 : 0 : result = wi::mul_high (pop0, pop1, UNSIGNED);
5822 : 0 : break;
5823 : :
5824 : : default:
5825 : : return NULL_RTX;
5826 : : }
5827 : 33052035 : return immed_wide_int_const (result, int_mode);
5828 : 33052764 : }
5829 : :
5830 : : /* Handle polynomial integers. */
5831 : : if (NUM_POLY_INT_COEFFS > 1
5832 : : && is_a <scalar_int_mode> (mode, &int_mode)
5833 : : && poly_int_rtx_p (op0)
5834 : : && poly_int_rtx_p (op1))
5835 : : {
5836 : : poly_wide_int result;
5837 : : switch (code)
5838 : : {
5839 : : case PLUS:
5840 : : result = wi::to_poly_wide (op0, mode) + wi::to_poly_wide (op1, mode);
5841 : : break;
5842 : :
5843 : : case MINUS:
5844 : : result = wi::to_poly_wide (op0, mode) - wi::to_poly_wide (op1, mode);
5845 : : break;
5846 : :
5847 : : case MULT:
5848 : : if (CONST_SCALAR_INT_P (op1))
5849 : : result = wi::to_poly_wide (op0, mode) * rtx_mode_t (op1, mode);
5850 : : else
5851 : : return NULL_RTX;
5852 : : break;
5853 : :
5854 : : case ASHIFT:
5855 : : if (CONST_SCALAR_INT_P (op1))
5856 : : {
5857 : : wide_int shift
5858 : : = rtx_mode_t (op1,
5859 : : GET_MODE (op1) == VOIDmode
5860 : : && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD
5861 : : ? word_mode : mode);
5862 : : if (SHIFT_COUNT_TRUNCATED)
5863 : : shift = wi::umod_trunc (shift, GET_MODE_PRECISION (int_mode));
5864 : : else if (wi::geu_p (shift, GET_MODE_PRECISION (int_mode)))
5865 : : return NULL_RTX;
5866 : : result = wi::to_poly_wide (op0, mode) << shift;
5867 : : }
5868 : : else
5869 : : return NULL_RTX;
5870 : : break;
5871 : :
5872 : : case IOR:
5873 : : if (!CONST_SCALAR_INT_P (op1)
5874 : : || !can_ior_p (wi::to_poly_wide (op0, mode),
5875 : : rtx_mode_t (op1, mode), &result))
5876 : : return NULL_RTX;
5877 : : break;
5878 : :
5879 : : default:
5880 : : return NULL_RTX;
5881 : : }
5882 : : return immed_wide_int_const (result, int_mode);
5883 : : }
5884 : :
5885 : : return NULL_RTX;
5886 : : }
5887 : :
5888 : :
5889 : :
5890 : : /* Return a positive integer if X should sort after Y. The value
5891 : : returned is 1 if and only if X and Y are both regs. */
5892 : :
5893 : : static int
5894 : 106705053 : simplify_plus_minus_op_data_cmp (rtx x, rtx y)
5895 : : {
5896 : 106705053 : int result;
5897 : :
5898 : 106705053 : result = (commutative_operand_precedence (y)
5899 : 106705053 : - commutative_operand_precedence (x));
5900 : 106705053 : if (result)
5901 : 73308544 : return result + result;
5902 : :
5903 : : /* Group together equal REGs to do more simplification. */
5904 : 33396509 : if (REG_P (x) && REG_P (y))
5905 : 8429064 : return REGNO (x) > REGNO (y);
5906 : :
5907 : : return 0;
5908 : : }
5909 : :
5910 : : /* Simplify and canonicalize a PLUS or MINUS, at least one of whose
5911 : : operands may be another PLUS or MINUS.
5912 : :
5913 : : Rather than test for specific case, we do this by a brute-force method
5914 : : and do all possible simplifications until no more changes occur. Then
5915 : : we rebuild the operation.
5916 : :
5917 : : May return NULL_RTX when no changes were made. */
5918 : :
5919 : : rtx
5920 : 36172628 : simplify_context::simplify_plus_minus (rtx_code code, machine_mode mode,
5921 : : rtx op0, rtx op1)
5922 : : {
5923 : 36172628 : struct simplify_plus_minus_op_data
5924 : : {
5925 : : rtx op;
5926 : : short neg;
5927 : : } ops[16];
5928 : 36172628 : rtx result, tem;
5929 : 36172628 : int n_ops = 2;
5930 : 36172628 : int changed, n_constants, canonicalized = 0;
5931 : 36172628 : int i, j;
5932 : :
5933 : 36172628 : memset (ops, 0, sizeof ops);
5934 : :
5935 : : /* Set up the two operands and then expand them until nothing has been
5936 : : changed. If we run out of room in our array, give up; this should
5937 : : almost never happen. */
5938 : :
5939 : 36172628 : ops[0].op = op0;
5940 : 36172628 : ops[0].neg = 0;
5941 : 36172628 : ops[1].op = op1;
5942 : 36172628 : ops[1].neg = (code == MINUS);
5943 : :
5944 : 73335802 : do
5945 : : {
5946 : 73335802 : changed = 0;
5947 : 73335802 : n_constants = 0;
5948 : :
5949 : 296455940 : for (i = 0; i < n_ops; i++)
5950 : : {
5951 : 223120152 : rtx this_op = ops[i].op;
5952 : 223120152 : int this_neg = ops[i].neg;
5953 : 223120152 : enum rtx_code this_code = GET_CODE (this_op);
5954 : :
5955 : 223120152 : switch (this_code)
5956 : : {
5957 : 36367632 : case PLUS:
5958 : 36367632 : case MINUS:
5959 : 36367632 : if (n_ops == ARRAY_SIZE (ops))
5960 : : return NULL_RTX;
5961 : :
5962 : 36367618 : ops[n_ops].op = XEXP (this_op, 1);
5963 : 36367618 : ops[n_ops].neg = (this_code == MINUS) ^ this_neg;
5964 : 36367618 : n_ops++;
5965 : :
5966 : 36367618 : ops[i].op = XEXP (this_op, 0);
5967 : 36367618 : changed = 1;
5968 : : /* If this operand was negated then we will potentially
5969 : : canonicalize the expression. Similarly if we don't
5970 : : place the operands adjacent we're re-ordering the
5971 : : expression and thus might be performing a
5972 : : canonicalization. Ignore register re-ordering.
5973 : : ??? It might be better to shuffle the ops array here,
5974 : : but then (plus (plus (A, B), plus (C, D))) wouldn't
5975 : : be seen as non-canonical. */
5976 : 36367618 : if (this_neg
5977 : 35624703 : || (i != n_ops - 2
5978 : 34938985 : && !(REG_P (ops[i].op) && REG_P (ops[n_ops - 1].op))))
5979 : 223120138 : canonicalized = 1;
5980 : : break;
5981 : :
5982 : 1720 : case NEG:
5983 : 1720 : ops[i].op = XEXP (this_op, 0);
5984 : 1720 : ops[i].neg = ! this_neg;
5985 : 1720 : changed = 1;
5986 : 1720 : canonicalized = 1;
5987 : 1720 : break;
5988 : :
5989 : 1400861 : case CONST:
5990 : 1400861 : if (n_ops != ARRAY_SIZE (ops)
5991 : 1400861 : && GET_CODE (XEXP (this_op, 0)) == PLUS
5992 : 1283777 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 0))
5993 : 1260639 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 1)))
5994 : : {
5995 : 1260639 : ops[i].op = XEXP (XEXP (this_op, 0), 0);
5996 : 1260639 : ops[n_ops].op = XEXP (XEXP (this_op, 0), 1);
5997 : 1260639 : ops[n_ops].neg = this_neg;
5998 : 1260639 : n_ops++;
5999 : 1260639 : changed = 1;
6000 : 1260639 : canonicalized = 1;
6001 : : }
6002 : : break;
6003 : :
6004 : 39946 : case NOT:
6005 : : /* ~a -> (-a - 1) */
6006 : 39946 : if (n_ops != ARRAY_SIZE (ops))
6007 : : {
6008 : 39946 : ops[n_ops].op = CONSTM1_RTX (mode);
6009 : 39946 : ops[n_ops++].neg = this_neg;
6010 : 39946 : ops[i].op = XEXP (this_op, 0);
6011 : 39946 : ops[i].neg = !this_neg;
6012 : 39946 : changed = 1;
6013 : 39946 : canonicalized = 1;
6014 : : }
6015 : : break;
6016 : :
6017 : 112829778 : CASE_CONST_SCALAR_INT:
6018 : 112829778 : case CONST_POLY_INT:
6019 : 112829778 : n_constants++;
6020 : 112829778 : if (this_neg)
6021 : : {
6022 : 1151553 : ops[i].op = neg_poly_int_rtx (mode, this_op);
6023 : 1151553 : ops[i].neg = 0;
6024 : 1151553 : changed = 1;
6025 : 1151553 : canonicalized = 1;
6026 : : }
6027 : : break;
6028 : :
6029 : : default:
6030 : : break;
6031 : : }
6032 : : }
6033 : : }
6034 : 73335788 : while (changed);
6035 : :
6036 : 36172614 : if (n_constants > 1)
6037 : 23079374 : canonicalized = 1;
6038 : :
6039 : 36172614 : gcc_assert (n_ops >= 2);
6040 : :
6041 : : /* If we only have two operands, we can avoid the loops. */
6042 : 36172614 : if (n_ops == 2)
6043 : : {
6044 : 0 : enum rtx_code code = ops[0].neg || ops[1].neg ? MINUS : PLUS;
6045 : 0 : rtx lhs, rhs;
6046 : :
6047 : : /* Get the two operands. Be careful with the order, especially for
6048 : : the cases where code == MINUS. */
6049 : 0 : if (ops[0].neg && ops[1].neg)
6050 : : {
6051 : 0 : lhs = gen_rtx_NEG (mode, ops[0].op);
6052 : 0 : rhs = ops[1].op;
6053 : : }
6054 : 0 : else if (ops[0].neg)
6055 : : {
6056 : 0 : lhs = ops[1].op;
6057 : 0 : rhs = ops[0].op;
6058 : : }
6059 : : else
6060 : : {
6061 : 0 : lhs = ops[0].op;
6062 : 0 : rhs = ops[1].op;
6063 : : }
6064 : :
6065 : 0 : return simplify_const_binary_operation (code, mode, lhs, rhs);
6066 : : }
6067 : :
6068 : : /* Now simplify each pair of operands until nothing changes. */
6069 : 60040627 : while (1)
6070 : : {
6071 : : /* Insertion sort is good enough for a small array. */
6072 : 157823563 : for (i = 1; i < n_ops; i++)
6073 : : {
6074 : 97782936 : struct simplify_plus_minus_op_data save;
6075 : 97782936 : int cmp;
6076 : :
6077 : 97782936 : j = i - 1;
6078 : 97782936 : cmp = simplify_plus_minus_op_data_cmp (ops[j].op, ops[i].op);
6079 : 97782936 : if (cmp <= 0)
6080 : 86746397 : continue;
6081 : : /* Just swapping registers doesn't count as canonicalization. */
6082 : 11036539 : if (cmp != 1)
6083 : 8111753 : canonicalized = 1;
6084 : :
6085 : 11036539 : save = ops[i];
6086 : 13119351 : do
6087 : 13119351 : ops[j + 1] = ops[j];
6088 : 13119351 : while (j--
6089 : 24155890 : && simplify_plus_minus_op_data_cmp (ops[j].op, save.op) > 0);
6090 : 11036539 : ops[j + 1] = save;
6091 : : }
6092 : :
6093 : 60040627 : changed = 0;
6094 : 157823563 : for (i = n_ops - 1; i > 0; i--)
6095 : 234048537 : for (j = i - 1; j >= 0; j--)
6096 : : {
6097 : 136984155 : rtx lhs = ops[j].op, rhs = ops[i].op;
6098 : 136984155 : int lneg = ops[j].neg, rneg = ops[i].neg;
6099 : :
6100 : 136984155 : if (lhs != 0 && rhs != 0)
6101 : : {
6102 : 112711140 : enum rtx_code ncode = PLUS;
6103 : :
6104 : 112711140 : if (lneg != rneg)
6105 : : {
6106 : 10471641 : ncode = MINUS;
6107 : 10471641 : if (lneg)
6108 : 6716708 : std::swap (lhs, rhs);
6109 : : }
6110 : 102239499 : else if (swap_commutative_operands_p (lhs, rhs))
6111 : 213092 : std::swap (lhs, rhs);
6112 : :
6113 : 112711140 : if ((GET_CODE (lhs) == CONST || CONST_INT_P (lhs))
6114 : 27409853 : && (GET_CODE (rhs) == CONST || CONST_INT_P (rhs)))
6115 : : {
6116 : 23230374 : rtx tem_lhs, tem_rhs;
6117 : :
6118 : 23230374 : tem_lhs = GET_CODE (lhs) == CONST ? XEXP (lhs, 0) : lhs;
6119 : 23230374 : tem_rhs = GET_CODE (rhs) == CONST ? XEXP (rhs, 0) : rhs;
6120 : 23230374 : tem = simplify_binary_operation (ncode, mode, tem_lhs,
6121 : : tem_rhs);
6122 : :
6123 : 23230374 : if (tem && !CONSTANT_P (tem))
6124 : 1745 : tem = gen_rtx_CONST (GET_MODE (tem), tem);
6125 : : }
6126 : : else
6127 : 89480766 : tem = simplify_binary_operation (ncode, mode, lhs, rhs);
6128 : :
6129 : 89482511 : if (tem)
6130 : : {
6131 : : /* Reject "simplifications" that just wrap the two
6132 : : arguments in a CONST. Failure to do so can result
6133 : : in infinite recursion with simplify_binary_operation
6134 : : when it calls us to simplify CONST operations.
6135 : : Also, if we find such a simplification, don't try
6136 : : any more combinations with this rhs: We must have
6137 : : something like symbol+offset, ie. one of the
6138 : : trivial CONST expressions we handle later. */
6139 : 25059103 : if (GET_CODE (tem) == CONST
6140 : 720299 : && GET_CODE (XEXP (tem, 0)) == ncode
6141 : 719751 : && XEXP (XEXP (tem, 0), 0) == lhs
6142 : 718554 : && XEXP (XEXP (tem, 0), 1) == rhs)
6143 : : break;
6144 : 24340549 : lneg &= rneg;
6145 : 24340549 : if (GET_CODE (tem) == NEG)
6146 : 44549 : tem = XEXP (tem, 0), lneg = !lneg;
6147 : 24340549 : if (poly_int_rtx_p (tem) && lneg)
6148 : 0 : tem = neg_poly_int_rtx (mode, tem), lneg = 0;
6149 : :
6150 : 24340549 : ops[i].op = tem;
6151 : 24340549 : ops[i].neg = lneg;
6152 : 24340549 : ops[j].op = NULL_RTX;
6153 : 24340549 : changed = 1;
6154 : 24340549 : canonicalized = 1;
6155 : : }
6156 : : }
6157 : : }
6158 : :
6159 : 60040627 : if (!changed)
6160 : : break;
6161 : :
6162 : : /* Pack all the operands to the lower-numbered entries. */
6163 : 96018890 : for (i = 0, j = 0; j < n_ops; j++)
6164 : 72150877 : if (ops[j].op)
6165 : : {
6166 : 47810328 : ops[i] = ops[j];
6167 : 47810328 : i++;
6168 : : }
6169 : : n_ops = i;
6170 : : }
6171 : :
6172 : : /* If nothing changed, check that rematerialization of rtl instructions
6173 : : is still required. */
6174 : 36172614 : if (!canonicalized)
6175 : : {
6176 : : /* Perform rematerialization if only all operands are registers and
6177 : : all operations are PLUS. */
6178 : : /* ??? Also disallow (non-global, non-frame) fixed registers to work
6179 : : around rs6000 and how it uses the CA register. See PR67145. */
6180 : 4976927 : for (i = 0; i < n_ops; i++)
6181 : 4016011 : if (ops[i].neg
6182 : 3742167 : || !REG_P (ops[i].op)
6183 : 7185240 : || (REGNO (ops[i].op) < FIRST_PSEUDO_REGISTER
6184 : 324565 : && fixed_regs[REGNO (ops[i].op)]
6185 : 244 : && !global_regs[REGNO (ops[i].op)]
6186 : 244 : && ops[i].op != frame_pointer_rtx
6187 : 108 : && ops[i].op != arg_pointer_rtx
6188 : 100 : && ops[i].op != stack_pointer_rtx))
6189 : : return NULL_RTX;
6190 : 960916 : goto gen_result;
6191 : : }
6192 : :
6193 : : /* Create (minus -C X) instead of (neg (const (plus X C))). */
6194 : 34364916 : if (n_ops == 2
6195 : 22559505 : && CONST_INT_P (ops[1].op)
6196 : 22114197 : && CONSTANT_P (ops[0].op)
6197 : 157 : && ops[0].neg)
6198 : 120 : return gen_rtx_fmt_ee (MINUS, mode, ops[1].op, ops[0].op);
6199 : :
6200 : : /* We suppressed creation of trivial CONST expressions in the
6201 : : combination loop to avoid recursion. Create one manually now.
6202 : : The combination loop should have ensured that there is exactly
6203 : : one CONST_INT, and the sort will have ensured that it is last
6204 : : in the array and that any other constant will be next-to-last. */
6205 : :
6206 : 34364796 : if (n_ops > 1
6207 : 33815717 : && poly_int_rtx_p (ops[n_ops - 1].op)
6208 : 65814089 : && CONSTANT_P (ops[n_ops - 2].op))
6209 : : {
6210 : 1311562 : rtx value = ops[n_ops - 1].op;
6211 : 1311562 : if (ops[n_ops - 1].neg ^ ops[n_ops - 2].neg)
6212 : 670786 : value = neg_poly_int_rtx (mode, value);
6213 : 1311562 : if (CONST_INT_P (value))
6214 : : {
6215 : 2623124 : ops[n_ops - 2].op = plus_constant (mode, ops[n_ops - 2].op,
6216 : 1311562 : INTVAL (value));
6217 : 1311562 : n_ops--;
6218 : : }
6219 : : }
6220 : :
6221 : : /* Put a non-negated operand first, if possible. */
6222 : :
6223 : 36058883 : for (i = 0; i < n_ops && ops[i].neg; i++)
6224 : 1694087 : continue;
6225 : 34364796 : if (i == n_ops)
6226 : 9314 : ops[0].op = gen_rtx_NEG (mode, ops[0].op);
6227 : 34355482 : else if (i != 0)
6228 : : {
6229 : 1591236 : tem = ops[0].op;
6230 : 1591236 : ops[0] = ops[i];
6231 : 1591236 : ops[i].op = tem;
6232 : 1591236 : ops[i].neg = 1;
6233 : : }
6234 : :
6235 : : /* Now make the result by performing the requested operations. */
6236 : 32764246 : gen_result:
6237 : 35325712 : result = ops[0].op;
6238 : 81818703 : for (i = 1; i < n_ops; i++)
6239 : 92985982 : result = gen_rtx_fmt_ee (ops[i].neg ? MINUS : PLUS,
6240 : : mode, result, ops[i].op);
6241 : :
6242 : : return result;
6243 : 1694087 : }
6244 : :
6245 : : /* Check whether an operand is suitable for calling simplify_plus_minus. */
6246 : : static bool
6247 : 495625114 : plus_minus_operand_p (const_rtx x)
6248 : : {
6249 : 495625114 : return GET_CODE (x) == PLUS
6250 : 495625114 : || GET_CODE (x) == MINUS
6251 : 495625114 : || (GET_CODE (x) == CONST
6252 : 1759554 : && GET_CODE (XEXP (x, 0)) == PLUS
6253 : 1181568 : && CONSTANT_P (XEXP (XEXP (x, 0), 0))
6254 : 1106856 : && CONSTANT_P (XEXP (XEXP (x, 0), 1)));
6255 : : }
6256 : :
6257 : : /* Like simplify_binary_operation except used for relational operators.
6258 : : MODE is the mode of the result. If MODE is VOIDmode, both operands must
6259 : : not also be VOIDmode.
6260 : :
6261 : : CMP_MODE specifies in which mode the comparison is done in, so it is
6262 : : the mode of the operands. If CMP_MODE is VOIDmode, it is taken from
6263 : : the operands or, if both are VOIDmode, the operands are compared in
6264 : : "infinite precision". */
6265 : : rtx
6266 : 125172239 : simplify_context::simplify_relational_operation (rtx_code code,
6267 : : machine_mode mode,
6268 : : machine_mode cmp_mode,
6269 : : rtx op0, rtx op1)
6270 : : {
6271 : 125172239 : rtx tem, trueop0, trueop1;
6272 : :
6273 : 125172239 : if (cmp_mode == VOIDmode)
6274 : 27502396 : cmp_mode = GET_MODE (op0);
6275 : 27502396 : if (cmp_mode == VOIDmode)
6276 : 453913 : cmp_mode = GET_MODE (op1);
6277 : :
6278 : 125172239 : tem = simplify_const_relational_operation (code, cmp_mode, op0, op1);
6279 : 125172239 : if (tem)
6280 : 818393 : return relational_result (mode, cmp_mode, tem);
6281 : :
6282 : : /* For the following tests, ensure const0_rtx is op1. */
6283 : 124353846 : if (swap_commutative_operands_p (op0, op1)
6284 : 124353846 : || (op0 == const0_rtx && op1 != const0_rtx))
6285 : 2563070 : std::swap (op0, op1), code = swap_condition (code);
6286 : :
6287 : : /* If op0 is a compare, extract the comparison arguments from it. */
6288 : 124353846 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6289 : 13484060 : return simplify_gen_relational (code, mode, VOIDmode,
6290 : 13484060 : XEXP (op0, 0), XEXP (op0, 1));
6291 : :
6292 : 110869786 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC)
6293 : : return NULL_RTX;
6294 : :
6295 : 81673993 : trueop0 = avoid_constant_pool_reference (op0);
6296 : 81673993 : trueop1 = avoid_constant_pool_reference (op1);
6297 : 81673993 : return simplify_relational_operation_1 (code, mode, cmp_mode,
6298 : 81673993 : trueop0, trueop1);
6299 : : }
6300 : :
6301 : : /* This part of simplify_relational_operation is only used when CMP_MODE
6302 : : is not in class MODE_CC (i.e. it is a real comparison).
6303 : :
6304 : : MODE is the mode of the result, while CMP_MODE specifies in which
6305 : : mode the comparison is done in, so it is the mode of the operands. */
6306 : :
6307 : : rtx
6308 : 81673993 : simplify_context::simplify_relational_operation_1 (rtx_code code,
6309 : : machine_mode mode,
6310 : : machine_mode cmp_mode,
6311 : : rtx op0, rtx op1)
6312 : : {
6313 : 81673993 : enum rtx_code op0code = GET_CODE (op0);
6314 : :
6315 : 81673993 : if (op1 == const0_rtx && COMPARISON_P (op0))
6316 : : {
6317 : : /* If op0 is a comparison, extract the comparison arguments
6318 : : from it. */
6319 : 445749 : if (code == NE)
6320 : : {
6321 : 217467 : if (GET_MODE (op0) == mode)
6322 : 161 : return simplify_rtx (op0);
6323 : : else
6324 : 217306 : return simplify_gen_relational (GET_CODE (op0), mode, VOIDmode,
6325 : 217306 : XEXP (op0, 0), XEXP (op0, 1));
6326 : : }
6327 : 228282 : else if (code == EQ)
6328 : : {
6329 : 103100 : enum rtx_code new_code = reversed_comparison_code (op0, NULL);
6330 : 103100 : if (new_code != UNKNOWN)
6331 : 102784 : return simplify_gen_relational (new_code, mode, VOIDmode,
6332 : 102784 : XEXP (op0, 0), XEXP (op0, 1));
6333 : : }
6334 : : }
6335 : :
6336 : : /* (LTU/GEU (PLUS a C) C), where C is constant, can be simplified to
6337 : : (GEU/LTU a -C). Likewise for (LTU/GEU (PLUS a C) a). */
6338 : 81353742 : if ((code == LTU || code == GEU)
6339 : 4508921 : && GET_CODE (op0) == PLUS
6340 : 625920 : && CONST_INT_P (XEXP (op0, 1))
6341 : 408359 : && (rtx_equal_p (op1, XEXP (op0, 0))
6342 : 292211 : || rtx_equal_p (op1, XEXP (op0, 1)))
6343 : : /* (LTU/GEU (PLUS a 0) 0) is not the same as (GEU/LTU a 0). */
6344 : 81533003 : && XEXP (op0, 1) != const0_rtx)
6345 : : {
6346 : 179261 : rtx new_cmp
6347 : 179261 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6348 : 180947 : return simplify_gen_relational ((code == LTU ? GEU : LTU), mode,
6349 : 179261 : cmp_mode, XEXP (op0, 0), new_cmp);
6350 : : }
6351 : :
6352 : : /* (GTU (PLUS a C) (C - 1)) where C is a non-zero constant can be
6353 : : transformed into (LTU a -C). */
6354 : 81174481 : if (code == GTU && GET_CODE (op0) == PLUS && CONST_INT_P (op1)
6355 : 348234 : && CONST_INT_P (XEXP (op0, 1))
6356 : 280848 : && (UINTVAL (op1) == UINTVAL (XEXP (op0, 1)) - 1)
6357 : 27041 : && XEXP (op0, 1) != const0_rtx)
6358 : : {
6359 : 27041 : rtx new_cmp
6360 : 27041 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6361 : 27041 : return simplify_gen_relational (LTU, mode, cmp_mode,
6362 : 27041 : XEXP (op0, 0), new_cmp);
6363 : : }
6364 : :
6365 : : /* Canonicalize (LTU/GEU (PLUS a b) b) as (LTU/GEU (PLUS a b) a). */
6366 : 81147440 : if ((code == LTU || code == GEU)
6367 : 4329660 : && GET_CODE (op0) == PLUS
6368 : 446659 : && rtx_equal_p (op1, XEXP (op0, 1))
6369 : : /* Don't recurse "infinitely" for (LTU/GEU (PLUS b b) b). */
6370 : 81152617 : && !rtx_equal_p (op1, XEXP (op0, 0)))
6371 : 5177 : return simplify_gen_relational (code, mode, cmp_mode, op0,
6372 : 5177 : copy_rtx (XEXP (op0, 0)));
6373 : :
6374 : 81142263 : if (op1 == const0_rtx)
6375 : : {
6376 : : /* Canonicalize (GTU x 0) as (NE x 0). */
6377 : 36544001 : if (code == GTU)
6378 : 174676 : return simplify_gen_relational (NE, mode, cmp_mode, op0, op1);
6379 : : /* Canonicalize (LEU x 0) as (EQ x 0). */
6380 : 36369325 : if (code == LEU)
6381 : 49982 : return simplify_gen_relational (EQ, mode, cmp_mode, op0, op1);
6382 : : }
6383 : 44598262 : else if (op1 == const1_rtx)
6384 : : {
6385 : 3062575 : switch (code)
6386 : : {
6387 : 8706 : case GE:
6388 : : /* Canonicalize (GE x 1) as (GT x 0). */
6389 : 8706 : return simplify_gen_relational (GT, mode, cmp_mode,
6390 : 8706 : op0, const0_rtx);
6391 : 169518 : case GEU:
6392 : : /* Canonicalize (GEU x 1) as (NE x 0). */
6393 : 169518 : return simplify_gen_relational (NE, mode, cmp_mode,
6394 : 169518 : op0, const0_rtx);
6395 : 10795 : case LT:
6396 : : /* Canonicalize (LT x 1) as (LE x 0). */
6397 : 10795 : return simplify_gen_relational (LE, mode, cmp_mode,
6398 : 10795 : op0, const0_rtx);
6399 : 35806 : case LTU:
6400 : : /* Canonicalize (LTU x 1) as (EQ x 0). */
6401 : 35806 : return simplify_gen_relational (EQ, mode, cmp_mode,
6402 : 35806 : op0, const0_rtx);
6403 : : default:
6404 : : break;
6405 : : }
6406 : : }
6407 : 41535687 : else if (op1 == constm1_rtx)
6408 : : {
6409 : : /* Canonicalize (LE x -1) as (LT x 0). */
6410 : 1120460 : if (code == LE)
6411 : 1555 : return simplify_gen_relational (LT, mode, cmp_mode, op0, const0_rtx);
6412 : : /* Canonicalize (GT x -1) as (GE x 0). */
6413 : 1118905 : if (code == GT)
6414 : 5144 : return simplify_gen_relational (GE, mode, cmp_mode, op0, const0_rtx);
6415 : : }
6416 : :
6417 : : /* (eq/ne (plus x cst1) cst2) simplifies to (eq/ne x (cst2 - cst1)) */
6418 : 80686081 : if ((code == EQ || code == NE)
6419 : 60724876 : && (op0code == PLUS || op0code == MINUS)
6420 : 2214815 : && CONSTANT_P (op1)
6421 : 915244 : && CONSTANT_P (XEXP (op0, 1))
6422 : 523418 : && (INTEGRAL_MODE_P (cmp_mode) || flag_unsafe_math_optimizations))
6423 : : {
6424 : 523386 : rtx x = XEXP (op0, 0);
6425 : 523386 : rtx c = XEXP (op0, 1);
6426 : 523386 : enum rtx_code invcode = op0code == PLUS ? MINUS : PLUS;
6427 : 523386 : rtx tem = simplify_gen_binary (invcode, cmp_mode, op1, c);
6428 : :
6429 : : /* Detect an infinite recursive condition, where we oscillate at this
6430 : : simplification case between:
6431 : : A + B == C <---> C - B == A,
6432 : : where A, B, and C are all constants with non-simplifiable expressions,
6433 : : usually SYMBOL_REFs. */
6434 : 523386 : if (GET_CODE (tem) == invcode
6435 : 56 : && CONSTANT_P (x)
6436 : 523404 : && rtx_equal_p (c, XEXP (tem, 1)))
6437 : : return NULL_RTX;
6438 : :
6439 : 523368 : return simplify_gen_relational (code, mode, cmp_mode, x, tem);
6440 : : }
6441 : :
6442 : : /* (ne:SI (zero_extract:SI FOO (const_int 1) BAR) (const_int 0))) is
6443 : : the same as (zero_extract:SI FOO (const_int 1) BAR). */
6444 : 60201490 : scalar_int_mode int_mode, int_cmp_mode;
6445 : 60201490 : if (code == NE
6446 : 32250683 : && op1 == const0_rtx
6447 : 2123991 : && is_int_mode (mode, &int_mode)
6448 : 80108815 : && is_a <scalar_int_mode> (cmp_mode, &int_cmp_mode)
6449 : : /* ??? Work-around BImode bugs in the ia64 backend. */
6450 : 2123991 : && int_mode != BImode
6451 : 2123967 : && int_cmp_mode != BImode
6452 : 2123967 : && nonzero_bits (op0, int_cmp_mode) == 1
6453 : 60201490 : && STORE_FLAG_VALUE == 1)
6454 : 107760 : return GET_MODE_SIZE (int_mode) > GET_MODE_SIZE (int_cmp_mode)
6455 : 53880 : ? simplify_gen_unary (ZERO_EXTEND, int_mode, op0, int_cmp_mode)
6456 : 21856 : : lowpart_subreg (int_mode, op0, int_cmp_mode);
6457 : :
6458 : : /* (eq/ne (xor x y) 0) simplifies to (eq/ne x y). */
6459 : 80108815 : if ((code == EQ || code == NE)
6460 : 60147610 : && op1 == const0_rtx
6461 : 32625411 : && op0code == XOR)
6462 : 16410 : return simplify_gen_relational (code, mode, cmp_mode,
6463 : 16410 : XEXP (op0, 0), XEXP (op0, 1));
6464 : :
6465 : : /* (eq/ne (xor x y) x) simplifies to (eq/ne y 0). */
6466 : 60131200 : if ((code == EQ || code == NE)
6467 : 60131200 : && op0code == XOR
6468 : 6561 : && rtx_equal_p (XEXP (op0, 0), op1)
6469 : 0 : && !side_effects_p (XEXP (op0, 0)))
6470 : 0 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 1),
6471 : 0 : CONST0_RTX (mode));
6472 : :
6473 : : /* Likewise (eq/ne (xor x y) y) simplifies to (eq/ne x 0). */
6474 : 80092405 : if ((code == EQ || code == NE)
6475 : 60131200 : && op0code == XOR
6476 : 6561 : && rtx_equal_p (XEXP (op0, 1), op1)
6477 : 80092539 : && !side_effects_p (XEXP (op0, 1)))
6478 : 134 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6479 : 134 : CONST0_RTX (mode));
6480 : :
6481 : : /* (eq/ne (xor x C1) C2) simplifies to (eq/ne x (C1^C2)). */
6482 : 80092271 : if ((code == EQ || code == NE)
6483 : 60131066 : && op0code == XOR
6484 : 6427 : && CONST_SCALAR_INT_P (op1)
6485 : 3188 : && CONST_SCALAR_INT_P (XEXP (op0, 1)))
6486 : 2658 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6487 : : simplify_gen_binary (XOR, cmp_mode,
6488 : 2658 : XEXP (op0, 1), op1));
6489 : :
6490 : : /* Simplify eq/ne (and/ior x y) x/y) for targets with a BICS instruction or
6491 : : constant folding if x/y is a constant. */
6492 : 60128408 : if ((code == EQ || code == NE)
6493 : 60128408 : && (op0code == AND || op0code == IOR)
6494 : 3743032 : && !side_effects_p (op1)
6495 : 3742988 : && op1 != CONST0_RTX (cmp_mode))
6496 : : {
6497 : : /* Both (eq/ne (and x y) x) and (eq/ne (ior x y) y) simplify to
6498 : : (eq/ne (and (not y) x) 0). */
6499 : 511606 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 0), op1))
6500 : 1022693 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 1), op1)))
6501 : : {
6502 : 22365 : rtx not_y = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 1),
6503 : : cmp_mode);
6504 : 22365 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_y, XEXP (op0, 0));
6505 : :
6506 : 22365 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
6507 : 22365 : CONST0_RTX (cmp_mode));
6508 : : }
6509 : :
6510 : : /* Both (eq/ne (and x y) y) and (eq/ne (ior x y) x) simplify to
6511 : : (eq/ne (and (not x) y) 0). */
6512 : 489305 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 1), op1))
6513 : 959810 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 0), op1)))
6514 : : {
6515 : 40540 : rtx not_x = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 0),
6516 : : cmp_mode);
6517 : 40540 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_x, XEXP (op0, 1));
6518 : :
6519 : 40540 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
6520 : 40540 : CONST0_RTX (cmp_mode));
6521 : : }
6522 : : }
6523 : :
6524 : : /* (eq/ne (bswap x) C1) simplifies to (eq/ne x C2) with C2 swapped. */
6525 : 80026708 : if ((code == EQ || code == NE)
6526 : 60065503 : && GET_CODE (op0) == BSWAP
6527 : 324 : && CONST_SCALAR_INT_P (op1))
6528 : 93 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6529 : : simplify_gen_unary (BSWAP, cmp_mode,
6530 : 93 : op1, cmp_mode));
6531 : :
6532 : : /* (eq/ne (bswap x) (bswap y)) simplifies to (eq/ne x y). */
6533 : 60065410 : if ((code == EQ || code == NE)
6534 : 60065410 : && GET_CODE (op0) == BSWAP
6535 : 231 : && GET_CODE (op1) == BSWAP)
6536 : 18 : return simplify_gen_relational (code, mode, cmp_mode,
6537 : 18 : XEXP (op0, 0), XEXP (op1, 0));
6538 : :
6539 : 80026597 : if (op0code == POPCOUNT && op1 == const0_rtx)
6540 : 0 : switch (code)
6541 : : {
6542 : 0 : case EQ:
6543 : 0 : case LE:
6544 : 0 : case LEU:
6545 : : /* (eq (popcount x) (const_int 0)) -> (eq x (const_int 0)). */
6546 : 0 : return simplify_gen_relational (EQ, mode, GET_MODE (XEXP (op0, 0)),
6547 : : XEXP (op0, 0),
6548 : 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
6549 : :
6550 : 0 : case NE:
6551 : 0 : case GT:
6552 : 0 : case GTU:
6553 : : /* (ne (popcount x) (const_int 0)) -> (ne x (const_int 0)). */
6554 : 0 : return simplify_gen_relational (NE, mode, GET_MODE (XEXP (op0, 0)),
6555 : : XEXP (op0, 0),
6556 : 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
6557 : :
6558 : : default:
6559 : : break;
6560 : : }
6561 : :
6562 : : /* (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) -> (and:SI x 1). */
6563 : 80026597 : if (code == NE
6564 : 32152471 : && op1 == const0_rtx
6565 : 16461592 : && (op0code == TRUNCATE
6566 : 154140 : || (partial_subreg_p (op0)
6567 : 153469 : && subreg_lowpart_p (op0)))
6568 : 129852 : && SCALAR_INT_MODE_P (mode)
6569 : 80026597 : && STORE_FLAG_VALUE == 1)
6570 : : {
6571 : 29672 : rtx tmp = XEXP (op0, 0);
6572 : 29672 : if (GET_CODE (tmp) == ASHIFT
6573 : 1807 : && GET_MODE (tmp) == mode
6574 : 188 : && CONST_INT_P (XEXP (tmp, 1))
6575 : 188 : && is_int_mode (GET_MODE (op0), &int_mode)
6576 : 29860 : && INTVAL (XEXP (tmp, 1)) == GET_MODE_PRECISION (int_mode) - 1)
6577 : 188 : return simplify_gen_binary (AND, mode, XEXP (tmp, 0), const1_rtx);
6578 : : }
6579 : :
6580 : : /* For two unsigned booleans A and B:
6581 : :
6582 : : A > B == ~B & A
6583 : : A >= B == ~B | A
6584 : : A < B == ~A & B
6585 : : A <= B == ~A | B
6586 : : A == B == ~A ^ B (== ~B ^ A)
6587 : : A != B == A ^ B
6588 : :
6589 : : For signed comparisons, we have to take STORE_FLAG_VALUE into account,
6590 : : with the rules above applying for positive STORE_FLAG_VALUE and with
6591 : : the relations reversed for negative STORE_FLAG_VALUE. */
6592 : 80026409 : if (is_a<scalar_int_mode> (cmp_mode)
6593 : 77668989 : && COMPARISON_P (op0)
6594 : 80091084 : && COMPARISON_P (op1))
6595 : : {
6596 : 9933 : rtx t = NULL_RTX;
6597 : 9933 : if (code == GTU || code == (STORE_FLAG_VALUE > 0 ? GT : LT))
6598 : 755 : t = simplify_logical_relational_operation (AND, mode, op1, op0, true);
6599 : : else if (code == GEU || code == (STORE_FLAG_VALUE > 0 ? GE : LE))
6600 : 720 : t = simplify_logical_relational_operation (IOR, mode, op1, op0, true);
6601 : : else if (code == LTU || code == (STORE_FLAG_VALUE > 0 ? LT : GT))
6602 : 720 : t = simplify_logical_relational_operation (AND, mode, op0, op1, true);
6603 : : else if (code == LEU || code == (STORE_FLAG_VALUE > 0 ? LE : GE))
6604 : 720 : t = simplify_logical_relational_operation (IOR, mode, op0, op1, true);
6605 : : else if (code == EQ)
6606 : 3181 : t = simplify_logical_relational_operation (XOR, mode, op0, op1, true);
6607 : : else if (code == NE)
6608 : 3837 : t = simplify_logical_relational_operation (XOR, mode, op0, op1);
6609 : 9933 : if (t)
6610 : : return t;
6611 : : }
6612 : :
6613 : : return NULL_RTX;
6614 : : }
6615 : :
6616 : : enum
6617 : : {
6618 : : CMP_EQ = 1,
6619 : : CMP_LT = 2,
6620 : : CMP_GT = 4,
6621 : : CMP_LTU = 8,
6622 : : CMP_GTU = 16
6623 : : };
6624 : :
6625 : :
6626 : : /* Convert the known results for EQ, LT, GT, LTU, GTU contained in
6627 : : KNOWN_RESULT to a CONST_INT, based on the requested comparison CODE
6628 : : For KNOWN_RESULT to make sense it should be either CMP_EQ, or the
6629 : : logical OR of one of (CMP_LT, CMP_GT) and one of (CMP_LTU, CMP_GTU).
6630 : : For floating-point comparisons, assume that the operands were ordered. */
6631 : :
6632 : : static rtx
6633 : 660927 : comparison_result (enum rtx_code code, int known_results)
6634 : : {
6635 : 660927 : switch (code)
6636 : : {
6637 : 123253 : case EQ:
6638 : 123253 : case UNEQ:
6639 : 123253 : return (known_results & CMP_EQ) ? const_true_rtx : const0_rtx;
6640 : 439178 : case NE:
6641 : 439178 : case LTGT:
6642 : 439178 : return (known_results & CMP_EQ) ? const0_rtx : const_true_rtx;
6643 : :
6644 : 9538 : case LT:
6645 : 9538 : case UNLT:
6646 : 9538 : return (known_results & CMP_LT) ? const_true_rtx : const0_rtx;
6647 : 8475 : case GE:
6648 : 8475 : case UNGE:
6649 : 8475 : return (known_results & CMP_LT) ? const0_rtx : const_true_rtx;
6650 : :
6651 : 13374 : case GT:
6652 : 13374 : case UNGT:
6653 : 13374 : return (known_results & CMP_GT) ? const_true_rtx : const0_rtx;
6654 : 14327 : case LE:
6655 : 14327 : case UNLE:
6656 : 14327 : return (known_results & CMP_GT) ? const0_rtx : const_true_rtx;
6657 : :
6658 : 10294 : case LTU:
6659 : 10294 : return (known_results & CMP_LTU) ? const_true_rtx : const0_rtx;
6660 : 8886 : case GEU:
6661 : 8886 : return (known_results & CMP_LTU) ? const0_rtx : const_true_rtx;
6662 : :
6663 : 22725 : case GTU:
6664 : 22725 : return (known_results & CMP_GTU) ? const_true_rtx : const0_rtx;
6665 : 10869 : case LEU:
6666 : 10869 : return (known_results & CMP_GTU) ? const0_rtx : const_true_rtx;
6667 : :
6668 : 0 : case ORDERED:
6669 : 0 : return const_true_rtx;
6670 : 8 : case UNORDERED:
6671 : 8 : return const0_rtx;
6672 : 0 : default:
6673 : 0 : gcc_unreachable ();
6674 : : }
6675 : : }
6676 : :
6677 : : /* Check if the given comparison (done in the given MODE) is actually
6678 : : a tautology or a contradiction. If the mode is VOIDmode, the
6679 : : comparison is done in "infinite precision". If no simplification
6680 : : is possible, this function returns zero. Otherwise, it returns
6681 : : either const_true_rtx or const0_rtx. */
6682 : :
6683 : : rtx
6684 : 125262383 : simplify_const_relational_operation (enum rtx_code code,
6685 : : machine_mode mode,
6686 : : rtx op0, rtx op1)
6687 : : {
6688 : 131806281 : rtx tem;
6689 : 131806281 : rtx trueop0;
6690 : 131806281 : rtx trueop1;
6691 : :
6692 : 131806281 : gcc_assert (mode != VOIDmode
6693 : : || (GET_MODE (op0) == VOIDmode
6694 : : && GET_MODE (op1) == VOIDmode));
6695 : :
6696 : : /* We only handle MODE_CC comparisons that are COMPARE against zero. */
6697 : 131806281 : if (GET_MODE_CLASS (mode) == MODE_CC
6698 : 42684657 : && (op1 != const0_rtx
6699 : 42684657 : || GET_CODE (op0) != COMPARE))
6700 : : return NULL_RTX;
6701 : :
6702 : : /* If op0 is a compare, extract the comparison arguments from it. */
6703 : 102610488 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6704 : : {
6705 : 13488864 : op1 = XEXP (op0, 1);
6706 : 13488864 : op0 = XEXP (op0, 0);
6707 : :
6708 : 13488864 : if (GET_MODE (op0) != VOIDmode)
6709 : 13374984 : mode = GET_MODE (op0);
6710 : 113880 : else if (GET_MODE (op1) != VOIDmode)
6711 : 104330 : mode = GET_MODE (op1);
6712 : : else
6713 : : return 0;
6714 : : }
6715 : :
6716 : : /* We can't simplify MODE_CC values since we don't know what the
6717 : : actual comparison is. */
6718 : 102600938 : if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6719 : : return 0;
6720 : :
6721 : : /* Make sure the constant is second. */
6722 : 102600938 : if (swap_commutative_operands_p (op0, op1))
6723 : : {
6724 : 3055444 : std::swap (op0, op1);
6725 : 3055444 : code = swap_condition (code);
6726 : : }
6727 : :
6728 : 102600938 : trueop0 = avoid_constant_pool_reference (op0);
6729 : 102600938 : trueop1 = avoid_constant_pool_reference (op1);
6730 : :
6731 : : /* For integer comparisons of A and B maybe we can simplify A - B and can
6732 : : then simplify a comparison of that with zero. If A and B are both either
6733 : : a register or a CONST_INT, this can't help; testing for these cases will
6734 : : prevent infinite recursion here and speed things up.
6735 : :
6736 : : We can only do this for EQ and NE comparisons as otherwise we may
6737 : : lose or introduce overflow which we cannot disregard as undefined as
6738 : : we do not know the signedness of the operation on either the left or
6739 : : the right hand side of the comparison. */
6740 : :
6741 : 102600938 : if (INTEGRAL_MODE_P (mode)
6742 : 100296758 : && trueop1 != CONST0_RTX (mode)
6743 : 50717760 : && (code == EQ || code == NE)
6744 : 32423578 : && ! ((REG_P (op0)
6745 : 8871996 : || CONST_SCALAR_INT_P (trueop0)
6746 : 8845241 : || CONST_VECTOR_P (trueop0))
6747 : 23578357 : && (REG_P (op1)
6748 : 14211173 : || CONST_SCALAR_INT_P (trueop1)
6749 : 2846330 : || CONST_VECTOR_P (trueop1)))
6750 : 11689932 : && (tem = simplify_binary_operation (MINUS, mode, op0, op1)) != 0
6751 : : /* We cannot do this if tem is a nonzero address. */
6752 : 6543900 : && ! nonzero_address_p (tem))
6753 : 6543898 : return simplify_const_relational_operation (signed_condition (code),
6754 : 6543898 : mode, tem, CONST0_RTX (mode));
6755 : :
6756 : 96057040 : if (! HONOR_NANS (mode) && code == ORDERED)
6757 : 0 : return const_true_rtx;
6758 : :
6759 : 96057040 : if (! HONOR_NANS (mode) && code == UNORDERED)
6760 : 8 : return const0_rtx;
6761 : :
6762 : : /* For modes without NaNs, if the two operands are equal, we know the
6763 : : result except if they have side-effects. Even with NaNs we know
6764 : : the result of unordered comparisons and, if signaling NaNs are
6765 : : irrelevant, also the result of LT/GT/LTGT. */
6766 : 96057032 : if ((! HONOR_NANS (trueop0)
6767 : 2088609 : || code == UNEQ || code == UNLE || code == UNGE
6768 : : || ((code == LT || code == GT || code == LTGT)
6769 : 823128 : && ! HONOR_SNANS (trueop0)))
6770 : 94907163 : && rtx_equal_p (trueop0, trueop1)
6771 : 96536461 : && ! side_effects_p (trueop0))
6772 : 479421 : return comparison_result (code, CMP_EQ);
6773 : :
6774 : : /* If the operands are floating-point constants, see if we can fold
6775 : : the result. */
6776 : 95577611 : if (CONST_DOUBLE_AS_FLOAT_P (trueop0)
6777 : 1298 : && CONST_DOUBLE_AS_FLOAT_P (trueop1)
6778 : 1298 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop0)))
6779 : : {
6780 : 1298 : const REAL_VALUE_TYPE *d0 = CONST_DOUBLE_REAL_VALUE (trueop0);
6781 : 1298 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
6782 : :
6783 : : /* Comparisons are unordered iff at least one of the values is NaN. */
6784 : 1298 : if (REAL_VALUE_ISNAN (*d0) || REAL_VALUE_ISNAN (*d1))
6785 : 180 : switch (code)
6786 : : {
6787 : 0 : case UNEQ:
6788 : 0 : case UNLT:
6789 : 0 : case UNGT:
6790 : 0 : case UNLE:
6791 : 0 : case UNGE:
6792 : 0 : case NE:
6793 : 0 : case UNORDERED:
6794 : 0 : return const_true_rtx;
6795 : 180 : case EQ:
6796 : 180 : case LT:
6797 : 180 : case GT:
6798 : 180 : case LE:
6799 : 180 : case GE:
6800 : 180 : case LTGT:
6801 : 180 : case ORDERED:
6802 : 180 : return const0_rtx;
6803 : : default:
6804 : : return 0;
6805 : : }
6806 : :
6807 : 1207 : return comparison_result (code,
6808 : 1207 : (real_equal (d0, d1) ? CMP_EQ :
6809 : 1207 : real_less (d0, d1) ? CMP_LT : CMP_GT));
6810 : : }
6811 : :
6812 : : /* Otherwise, see if the operands are both integers. */
6813 : 95576313 : if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
6814 : 92923659 : && CONST_SCALAR_INT_P (trueop0) && CONST_SCALAR_INT_P (trueop1))
6815 : : {
6816 : : /* It would be nice if we really had a mode here. However, the
6817 : : largest int representable on the target is as good as
6818 : : infinite. */
6819 : 180388 : machine_mode cmode = (mode == VOIDmode) ? MAX_MODE_INT : mode;
6820 : 180388 : rtx_mode_t ptrueop0 = rtx_mode_t (trueop0, cmode);
6821 : 180388 : rtx_mode_t ptrueop1 = rtx_mode_t (trueop1, cmode);
6822 : :
6823 : 180388 : if (wi::eq_p (ptrueop0, ptrueop1))
6824 : 0 : return comparison_result (code, CMP_EQ);
6825 : : else
6826 : : {
6827 : 180388 : int cr = wi::lts_p (ptrueop0, ptrueop1) ? CMP_LT : CMP_GT;
6828 : 180388 : cr |= wi::ltu_p (ptrueop0, ptrueop1) ? CMP_LTU : CMP_GTU;
6829 : 180388 : return comparison_result (code, cr);
6830 : : }
6831 : : }
6832 : :
6833 : : /* Optimize comparisons with upper and lower bounds. */
6834 : 95395925 : scalar_int_mode int_mode;
6835 : 95395925 : if (CONST_INT_P (trueop1)
6836 : 67978693 : && is_a <scalar_int_mode> (mode, &int_mode)
6837 : 67978693 : && HWI_COMPUTABLE_MODE_P (int_mode)
6838 : 162863079 : && !side_effects_p (trueop0))
6839 : : {
6840 : 67380105 : int sign;
6841 : 67380105 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (trueop0, int_mode);
6842 : 67380105 : HOST_WIDE_INT val = INTVAL (trueop1);
6843 : 67380105 : HOST_WIDE_INT mmin, mmax;
6844 : :
6845 : 67380105 : if (code == GEU
6846 : 67380105 : || code == LEU
6847 : 64487201 : || code == GTU
6848 : 64487201 : || code == LTU)
6849 : : sign = 0;
6850 : : else
6851 : 67380105 : sign = 1;
6852 : :
6853 : : /* Get a reduced range if the sign bit is zero. */
6854 : 67380105 : if (nonzero <= (GET_MODE_MASK (int_mode) >> 1))
6855 : : {
6856 : 6578471 : mmin = 0;
6857 : 6578471 : mmax = nonzero;
6858 : : }
6859 : : else
6860 : : {
6861 : 60801634 : rtx mmin_rtx, mmax_rtx;
6862 : 60801634 : get_mode_bounds (int_mode, sign, int_mode, &mmin_rtx, &mmax_rtx);
6863 : :
6864 : 60801634 : mmin = INTVAL (mmin_rtx);
6865 : 60801634 : mmax = INTVAL (mmax_rtx);
6866 : 60801634 : if (sign)
6867 : : {
6868 : 55384305 : unsigned int sign_copies
6869 : 55384305 : = num_sign_bit_copies (trueop0, int_mode);
6870 : :
6871 : 55384305 : mmin >>= (sign_copies - 1);
6872 : 55384305 : mmax >>= (sign_copies - 1);
6873 : : }
6874 : : }
6875 : :
6876 : 67380105 : switch (code)
6877 : : {
6878 : : /* x >= y is always true for y <= mmin, always false for y > mmax. */
6879 : 414448 : case GEU:
6880 : 414448 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
6881 : 12168 : return const_true_rtx;
6882 : 402280 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
6883 : 38 : return const0_rtx;
6884 : : break;
6885 : 962627 : case GE:
6886 : 962627 : if (val <= mmin)
6887 : 2224 : return const_true_rtx;
6888 : 960403 : if (val > mmax)
6889 : 0 : return const0_rtx;
6890 : : break;
6891 : :
6892 : : /* x <= y is always true for y >= mmax, always false for y < mmin. */
6893 : 2478456 : case LEU:
6894 : 2478456 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
6895 : 11050 : return const_true_rtx;
6896 : 2467406 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
6897 : 0 : return const0_rtx;
6898 : : break;
6899 : 2028084 : case LE:
6900 : 2028084 : if (val >= mmax)
6901 : 435 : return const_true_rtx;
6902 : 2027649 : if (val < mmin)
6903 : 0 : return const0_rtx;
6904 : : break;
6905 : :
6906 : 25044419 : case EQ:
6907 : : /* x == y is always false for y out of range. */
6908 : 25044419 : if (val < mmin || val > mmax)
6909 : 404 : return const0_rtx;
6910 : : break;
6911 : :
6912 : : /* x > y is always false for y >= mmax, always true for y < mmin. */
6913 : 2444979 : case GTU:
6914 : 2444979 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
6915 : 127605 : return const0_rtx;
6916 : 2317374 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
6917 : 0 : return const_true_rtx;
6918 : : break;
6919 : 2042336 : case GT:
6920 : 2042336 : if (val >= mmax)
6921 : 271 : return const0_rtx;
6922 : 2042065 : if (val < mmin)
6923 : 5 : return const_true_rtx;
6924 : : break;
6925 : :
6926 : : /* x < y is always false for y <= mmin, always true for y > mmax. */
6927 : 636617 : case LTU:
6928 : 636617 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
6929 : 2741 : return const0_rtx;
6930 : 633876 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
6931 : 83851 : return const_true_rtx;
6932 : : break;
6933 : 1115446 : case LT:
6934 : 1115446 : if (val <= mmin)
6935 : 2248 : return const0_rtx;
6936 : 1113198 : if (val > mmax)
6937 : 3507 : return const_true_rtx;
6938 : : break;
6939 : :
6940 : 30212693 : case NE:
6941 : : /* x != y is always true for y out of range. */
6942 : 30212693 : if (val < mmin || val > mmax)
6943 : 123 : return const_true_rtx;
6944 : : break;
6945 : :
6946 : : default:
6947 : : break;
6948 : : }
6949 : : }
6950 : :
6951 : : /* Optimize integer comparisons with zero. */
6952 : 95149255 : if (is_a <scalar_int_mode> (mode, &int_mode)
6953 : 92539126 : && trueop1 == const0_rtx
6954 : 48881008 : && !side_effects_p (trueop0))
6955 : : {
6956 : : /* Some addresses are known to be nonzero. We don't know
6957 : : their sign, but equality comparisons are known. */
6958 : 48792457 : if (nonzero_address_p (trueop0))
6959 : : {
6960 : 552 : if (code == EQ || code == LEU)
6961 : 297 : return const0_rtx;
6962 : 255 : if (code == NE || code == GTU)
6963 : 255 : return const_true_rtx;
6964 : : }
6965 : :
6966 : : /* See if the first operand is an IOR with a constant. If so, we
6967 : : may be able to determine the result of this comparison. */
6968 : 48791905 : if (GET_CODE (op0) == IOR)
6969 : : {
6970 : 595901 : rtx inner_const = avoid_constant_pool_reference (XEXP (op0, 1));
6971 : 595901 : if (CONST_INT_P (inner_const) && inner_const != const0_rtx)
6972 : : {
6973 : 286 : int sign_bitnum = GET_MODE_PRECISION (int_mode) - 1;
6974 : 572 : int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum
6975 : 286 : && (UINTVAL (inner_const)
6976 : 286 : & (HOST_WIDE_INT_1U
6977 : : << sign_bitnum)));
6978 : :
6979 : 286 : switch (code)
6980 : : {
6981 : : case EQ:
6982 : : case LEU:
6983 : : return const0_rtx;
6984 : 4 : case NE:
6985 : 4 : case GTU:
6986 : 4 : return const_true_rtx;
6987 : 68 : case LT:
6988 : 68 : case LE:
6989 : 68 : if (has_sign)
6990 : 0 : return const_true_rtx;
6991 : : break;
6992 : 210 : case GT:
6993 : 210 : case GE:
6994 : 210 : if (has_sign)
6995 : : return const0_rtx;
6996 : : break;
6997 : : default:
6998 : : break;
6999 : : }
7000 : : }
7001 : : }
7002 : : }
7003 : :
7004 : : /* Optimize comparison of ABS with zero. */
7005 : 49187816 : if (trueop1 == CONST0_RTX (mode) && !side_effects_p (trueop0)
7006 : 144247539 : && (GET_CODE (trueop0) == ABS
7007 : 49098517 : || (GET_CODE (trueop0) == FLOAT_EXTEND
7008 : 34 : && GET_CODE (XEXP (trueop0, 0)) == ABS)))
7009 : : {
7010 : 519 : switch (code)
7011 : : {
7012 : 44 : case LT:
7013 : : /* Optimize abs(x) < 0.0. */
7014 : 44 : if (!INTEGRAL_MODE_P (mode) && !HONOR_SNANS (mode))
7015 : 0 : return const0_rtx;
7016 : : break;
7017 : :
7018 : 42 : case GE:
7019 : : /* Optimize abs(x) >= 0.0. */
7020 : 42 : if (!INTEGRAL_MODE_P (mode) && !HONOR_NANS (mode))
7021 : 0 : return const_true_rtx;
7022 : : break;
7023 : :
7024 : 0 : case UNGE:
7025 : : /* Optimize ! (abs(x) < 0.0). */
7026 : 0 : return const_true_rtx;
7027 : :
7028 : : default:
7029 : : break;
7030 : : }
7031 : : }
7032 : :
7033 : : return 0;
7034 : : }
7035 : :
7036 : : /* Recognize expressions of the form (X CMP 0) ? VAL : OP (X)
7037 : : where OP is CLZ or CTZ and VAL is the value from CLZ_DEFINED_VALUE_AT_ZERO
7038 : : or CTZ_DEFINED_VALUE_AT_ZERO respectively and return OP (X) if the expression
7039 : : can be simplified to that or NULL_RTX if not.
7040 : : Assume X is compared against zero with CMP_CODE and the true
7041 : : arm is TRUE_VAL and the false arm is FALSE_VAL. */
7042 : :
7043 : : rtx
7044 : 29999746 : simplify_context::simplify_cond_clz_ctz (rtx x, rtx_code cmp_code,
7045 : : rtx true_val, rtx false_val)
7046 : : {
7047 : 29999746 : if (cmp_code != EQ && cmp_code != NE)
7048 : : return NULL_RTX;
7049 : :
7050 : : /* Result on X == 0 and X !=0 respectively. */
7051 : 21866915 : rtx on_zero, on_nonzero;
7052 : 21866915 : if (cmp_code == EQ)
7053 : : {
7054 : : on_zero = true_val;
7055 : : on_nonzero = false_val;
7056 : : }
7057 : : else
7058 : : {
7059 : 11762379 : on_zero = false_val;
7060 : 11762379 : on_nonzero = true_val;
7061 : : }
7062 : :
7063 : 21866915 : rtx_code op_code = GET_CODE (on_nonzero);
7064 : 21866915 : if ((op_code != CLZ && op_code != CTZ)
7065 : 1811 : || !rtx_equal_p (XEXP (on_nonzero, 0), x)
7066 : 21867840 : || !CONST_INT_P (on_zero))
7067 : 21866622 : return NULL_RTX;
7068 : :
7069 : 293 : HOST_WIDE_INT op_val;
7070 : 293 : scalar_int_mode mode ATTRIBUTE_UNUSED
7071 : 293 : = as_a <scalar_int_mode> (GET_MODE (XEXP (on_nonzero, 0)));
7072 : 0 : if (((op_code == CLZ && CLZ_DEFINED_VALUE_AT_ZERO (mode, op_val))
7073 : 586 : || (op_code == CTZ && CTZ_DEFINED_VALUE_AT_ZERO (mode, op_val)))
7074 : 317 : && op_val == INTVAL (on_zero))
7075 : : return on_nonzero;
7076 : :
7077 : : return NULL_RTX;
7078 : : }
7079 : :
7080 : : /* Try to simplify X given that it appears within operand OP of a
7081 : : VEC_MERGE operation whose mask is MASK. X need not use the same
7082 : : vector mode as the VEC_MERGE, but it must have the same number of
7083 : : elements.
7084 : :
7085 : : Return the simplified X on success, otherwise return NULL_RTX. */
7086 : :
7087 : : rtx
7088 : 1356972 : simplify_context::simplify_merge_mask (rtx x, rtx mask, int op)
7089 : : {
7090 : 1356972 : gcc_assert (VECTOR_MODE_P (GET_MODE (x)));
7091 : 2713944 : poly_uint64 nunits = GET_MODE_NUNITS (GET_MODE (x));
7092 : 1356972 : if (GET_CODE (x) == VEC_MERGE && rtx_equal_p (XEXP (x, 2), mask))
7093 : : {
7094 : 5624 : if (side_effects_p (XEXP (x, 1 - op)))
7095 : : return NULL_RTX;
7096 : :
7097 : 5376 : return XEXP (x, op);
7098 : : }
7099 : 1351348 : if (UNARY_P (x)
7100 : 175973 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7101 : 1408326 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits))
7102 : : {
7103 : 23920 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7104 : 23920 : if (top0)
7105 : 496 : return simplify_gen_unary (GET_CODE (x), GET_MODE (x), top0,
7106 : 496 : GET_MODE (XEXP (x, 0)));
7107 : : }
7108 : 1350852 : if (BINARY_P (x)
7109 : 146656 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7110 : 289902 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7111 : 120371 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7112 : 1528178 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits))
7113 : : {
7114 : 88663 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7115 : 88663 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7116 : 88663 : if (top0 || top1)
7117 : : {
7118 : 838 : if (COMPARISON_P (x))
7119 : 0 : return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7120 : 0 : GET_MODE (XEXP (x, 0)) != VOIDmode
7121 : : ? GET_MODE (XEXP (x, 0))
7122 : 0 : : GET_MODE (XEXP (x, 1)),
7123 : : top0 ? top0 : XEXP (x, 0),
7124 : 0 : top1 ? top1 : XEXP (x, 1));
7125 : : else
7126 : 838 : return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7127 : : top0 ? top0 : XEXP (x, 0),
7128 : 838 : top1 ? top1 : XEXP (x, 1));
7129 : : }
7130 : : }
7131 : 1350014 : if (GET_RTX_CLASS (GET_CODE (x)) == RTX_TERNARY
7132 : 23472 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7133 : 46944 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7134 : 23472 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7135 : 46944 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits)
7136 : 23472 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 2)))
7137 : 1358816 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 2))), nunits))
7138 : : {
7139 : 4401 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7140 : 4401 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7141 : 4401 : rtx top2 = simplify_merge_mask (XEXP (x, 2), mask, op);
7142 : 4401 : if (top0 || top1 || top2)
7143 : 496 : return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7144 : 496 : GET_MODE (XEXP (x, 0)),
7145 : : top0 ? top0 : XEXP (x, 0),
7146 : : top1 ? top1 : XEXP (x, 1),
7147 : 496 : top2 ? top2 : XEXP (x, 2));
7148 : : }
7149 : : return NULL_RTX;
7150 : : }
7151 : :
7152 : :
7153 : : /* Simplify CODE, an operation with result mode MODE and three operands,
7154 : : OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
7155 : : a constant. Return 0 if no simplifications is possible. */
7156 : :
7157 : : rtx
7158 : 40876953 : simplify_context::simplify_ternary_operation (rtx_code code, machine_mode mode,
7159 : : machine_mode op0_mode,
7160 : : rtx op0, rtx op1, rtx op2)
7161 : : {
7162 : 40876953 : bool any_change = false;
7163 : 40876953 : rtx tem, trueop2;
7164 : 40876953 : scalar_int_mode int_mode, int_op0_mode;
7165 : 40876953 : unsigned int n_elts;
7166 : :
7167 : 40876953 : switch (code)
7168 : : {
7169 : 329661 : case FMA:
7170 : : /* Simplify negations around the multiplication. */
7171 : : /* -a * -b + c => a * b + c. */
7172 : 329661 : if (GET_CODE (op0) == NEG)
7173 : : {
7174 : 86695 : tem = simplify_unary_operation (NEG, mode, op1, mode);
7175 : 86695 : if (tem)
7176 : 222 : op1 = tem, op0 = XEXP (op0, 0), any_change = true;
7177 : : }
7178 : 242966 : else if (GET_CODE (op1) == NEG)
7179 : : {
7180 : 1084 : tem = simplify_unary_operation (NEG, mode, op0, mode);
7181 : 1084 : if (tem)
7182 : 0 : op0 = tem, op1 = XEXP (op1, 0), any_change = true;
7183 : : }
7184 : :
7185 : : /* Canonicalize the two multiplication operands. */
7186 : : /* a * -b + c => -b * a + c. */
7187 : 329661 : if (swap_commutative_operands_p (op0, op1))
7188 : : std::swap (op0, op1), any_change = true;
7189 : :
7190 : 304186 : if (any_change)
7191 : 25687 : return gen_rtx_FMA (mode, op0, op1, op2);
7192 : : return NULL_RTX;
7193 : :
7194 : 626436 : case SIGN_EXTRACT:
7195 : 626436 : case ZERO_EXTRACT:
7196 : 626436 : if (CONST_INT_P (op0)
7197 : 19747 : && CONST_INT_P (op1)
7198 : 19747 : && CONST_INT_P (op2)
7199 : 40876982 : && is_a <scalar_int_mode> (mode, &int_mode)
7200 : 29 : && INTVAL (op1) + INTVAL (op2) <= GET_MODE_PRECISION (int_mode)
7201 : 626465 : && HWI_COMPUTABLE_MODE_P (int_mode))
7202 : : {
7203 : : /* Extracting a bit-field from a constant */
7204 : 29 : unsigned HOST_WIDE_INT val = UINTVAL (op0);
7205 : 29 : HOST_WIDE_INT op1val = INTVAL (op1);
7206 : 29 : HOST_WIDE_INT op2val = INTVAL (op2);
7207 : 29 : if (!BITS_BIG_ENDIAN)
7208 : 29 : val >>= op2val;
7209 : : else if (is_a <scalar_int_mode> (op0_mode, &int_op0_mode))
7210 : : val >>= GET_MODE_PRECISION (int_op0_mode) - op2val - op1val;
7211 : : else
7212 : : /* Not enough information to calculate the bit position. */
7213 : : break;
7214 : :
7215 : 29 : if (HOST_BITS_PER_WIDE_INT != op1val)
7216 : : {
7217 : : /* First zero-extend. */
7218 : 26 : val &= (HOST_WIDE_INT_1U << op1val) - 1;
7219 : : /* If desired, propagate sign bit. */
7220 : 26 : if (code == SIGN_EXTRACT
7221 : 4 : && (val & (HOST_WIDE_INT_1U << (op1val - 1)))
7222 : 4 : != 0)
7223 : 1 : val |= ~ ((HOST_WIDE_INT_1U << op1val) - 1);
7224 : : }
7225 : :
7226 : 29 : return gen_int_mode (val, int_mode);
7227 : : }
7228 : : break;
7229 : :
7230 : 39238945 : case IF_THEN_ELSE:
7231 : 39238945 : if (CONST_INT_P (op0))
7232 : 288373 : return op0 != const0_rtx ? op1 : op2;
7233 : :
7234 : : /* Convert c ? a : a into "a". */
7235 : 39044832 : if (rtx_equal_p (op1, op2) && ! side_effects_p (op0))
7236 : : return op1;
7237 : :
7238 : : /* Convert a != b ? a : b into "a". */
7239 : 39041608 : if (GET_CODE (op0) == NE
7240 : 15456889 : && ! side_effects_p (op0)
7241 : 15419044 : && ! HONOR_NANS (mode)
7242 : 15414923 : && ! HONOR_SIGNED_ZEROS (mode)
7243 : 54456531 : && ((rtx_equal_p (XEXP (op0, 0), op1)
7244 : 77465 : && rtx_equal_p (XEXP (op0, 1), op2))
7245 : 15414893 : || (rtx_equal_p (XEXP (op0, 0), op2)
7246 : 4225 : && rtx_equal_p (XEXP (op0, 1), op1))))
7247 : 171 : return op1;
7248 : :
7249 : : /* Convert a == b ? a : b into "b". */
7250 : 39041437 : if (GET_CODE (op0) == EQ
7251 : 12352121 : && ! side_effects_p (op0)
7252 : 12341927 : && ! HONOR_NANS (mode)
7253 : 12241133 : && ! HONOR_SIGNED_ZEROS (mode)
7254 : 51282570 : && ((rtx_equal_p (XEXP (op0, 0), op1)
7255 : 14424 : && rtx_equal_p (XEXP (op0, 1), op2))
7256 : 12241123 : || (rtx_equal_p (XEXP (op0, 0), op2)
7257 : 7357 : && rtx_equal_p (XEXP (op0, 1), op1))))
7258 : 23 : return op2;
7259 : :
7260 : : /* Convert a != 0 ? -a : 0 into "-a". */
7261 : 39041414 : if (GET_CODE (op0) == NE
7262 : 15456718 : && ! side_effects_p (op0)
7263 : 15418873 : && ! HONOR_NANS (mode)
7264 : 15414752 : && ! HONOR_SIGNED_ZEROS (mode)
7265 : 15414752 : && XEXP (op0, 1) == CONST0_RTX (mode)
7266 : 11758316 : && op2 == CONST0_RTX (mode)
7267 : 77610 : && GET_CODE (op1) == NEG
7268 : 39041453 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0)))
7269 : : return op1;
7270 : :
7271 : : /* Convert a == 0 ? 0 : -a into "-a". */
7272 : 39041409 : if (GET_CODE (op0) == EQ
7273 : 12352098 : && ! side_effects_p (op0)
7274 : 12341904 : && ! HONOR_NANS (mode)
7275 : 12241110 : && ! HONOR_SIGNED_ZEROS (mode)
7276 : 12241110 : && op1 == CONST0_RTX (mode)
7277 : 41693 : && XEXP (op0, 1) == CONST0_RTX (mode)
7278 : 19794 : && GET_CODE (op2) == NEG
7279 : 39041413 : && rtx_equal_p (XEXP (op0, 0), XEXP (op2, 0)))
7280 : : return op2;
7281 : :
7282 : : /* Convert (!c) != {0,...,0} ? a : b into
7283 : : c != {0,...,0} ? b : a for vector modes. */
7284 : 39041405 : if (VECTOR_MODE_P (GET_MODE (op1))
7285 : 11838 : && GET_CODE (op0) == NE
7286 : 456 : && GET_CODE (XEXP (op0, 0)) == NOT
7287 : 0 : && GET_CODE (XEXP (op0, 1)) == CONST_VECTOR)
7288 : : {
7289 : 0 : rtx cv = XEXP (op0, 1);
7290 : 0 : int nunits;
7291 : 0 : bool ok = true;
7292 : 0 : if (!CONST_VECTOR_NUNITS (cv).is_constant (&nunits))
7293 : : ok = false;
7294 : : else
7295 : 0 : for (int i = 0; i < nunits; ++i)
7296 : 0 : if (CONST_VECTOR_ELT (cv, i) != const0_rtx)
7297 : : {
7298 : : ok = false;
7299 : : break;
7300 : : }
7301 : 0 : if (ok)
7302 : : {
7303 : 0 : rtx new_op0 = gen_rtx_NE (GET_MODE (op0),
7304 : : XEXP (XEXP (op0, 0), 0),
7305 : : XEXP (op0, 1));
7306 : 0 : rtx retval = gen_rtx_IF_THEN_ELSE (mode, new_op0, op2, op1);
7307 : 0 : return retval;
7308 : : }
7309 : : }
7310 : :
7311 : : /* Convert x == 0 ? N : clz (x) into clz (x) when
7312 : : CLZ_DEFINED_VALUE_AT_ZERO is defined to N for the mode of x.
7313 : : Similarly for ctz (x). */
7314 : 39040245 : if (COMPARISON_P (op0) && !side_effects_p (op0)
7315 : 77996710 : && XEXP (op0, 1) == const0_rtx)
7316 : : {
7317 : 29999746 : rtx simplified
7318 : 29999746 : = simplify_cond_clz_ctz (XEXP (op0, 0), GET_CODE (op0),
7319 : : op1, op2);
7320 : 29999746 : if (simplified)
7321 : : return simplified;
7322 : : }
7323 : :
7324 : 39041405 : if (COMPARISON_P (op0) && ! side_effects_p (op0))
7325 : : {
7326 : 77981584 : machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
7327 : 38955305 : ? GET_MODE (XEXP (op0, 1))
7328 : : : GET_MODE (XEXP (op0, 0)));
7329 : 38955305 : rtx temp;
7330 : :
7331 : : /* Look for happy constants in op1 and op2. */
7332 : 38955305 : if (CONST_INT_P (op1) && CONST_INT_P (op2))
7333 : : {
7334 : 183590 : HOST_WIDE_INT t = INTVAL (op1);
7335 : 183590 : HOST_WIDE_INT f = INTVAL (op2);
7336 : :
7337 : 183590 : if (t == STORE_FLAG_VALUE && f == 0)
7338 : 30524 : code = GET_CODE (op0);
7339 : 153066 : else if (t == 0 && f == STORE_FLAG_VALUE)
7340 : : {
7341 : 31619 : enum rtx_code tmp;
7342 : 31619 : tmp = reversed_comparison_code (op0, NULL);
7343 : 31619 : if (tmp == UNKNOWN)
7344 : : break;
7345 : : code = tmp;
7346 : : }
7347 : : else
7348 : : break;
7349 : :
7350 : 56779 : return simplify_gen_relational (code, mode, cmp_mode,
7351 : 56779 : XEXP (op0, 0), XEXP (op0, 1));
7352 : : }
7353 : :
7354 : 38771715 : temp = simplify_relational_operation (GET_CODE (op0), op0_mode,
7355 : : cmp_mode, XEXP (op0, 0),
7356 : : XEXP (op0, 1));
7357 : :
7358 : : /* See if any simplifications were possible. */
7359 : 38771715 : if (temp)
7360 : : {
7361 : 5886 : if (CONST_INT_P (temp))
7362 : 108 : return temp == const0_rtx ? op2 : op1;
7363 : 5828 : else if (temp)
7364 : 5828 : return gen_rtx_IF_THEN_ELSE (mode, temp, op1, op2);
7365 : : }
7366 : : }
7367 : : break;
7368 : :
7369 : 681911 : case VEC_MERGE:
7370 : 681911 : gcc_assert (GET_MODE (op0) == mode);
7371 : 681911 : gcc_assert (GET_MODE (op1) == mode);
7372 : 681911 : gcc_assert (VECTOR_MODE_P (mode));
7373 : 681911 : trueop2 = avoid_constant_pool_reference (op2);
7374 : 681911 : if (CONST_INT_P (trueop2)
7375 : 1079445 : && GET_MODE_NUNITS (mode).is_constant (&n_elts))
7376 : : {
7377 : 397534 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop2);
7378 : 397534 : unsigned HOST_WIDE_INT mask;
7379 : 397534 : if (n_elts == HOST_BITS_PER_WIDE_INT)
7380 : : mask = -1;
7381 : : else
7382 : 396410 : mask = (HOST_WIDE_INT_1U << n_elts) - 1;
7383 : :
7384 : 397534 : if (!(sel & mask) && !side_effects_p (op0))
7385 : : return op1;
7386 : 397097 : if ((sel & mask) == mask && !side_effects_p (op1))
7387 : : return op0;
7388 : :
7389 : 386950 : rtx trueop0 = avoid_constant_pool_reference (op0);
7390 : 386950 : rtx trueop1 = avoid_constant_pool_reference (op1);
7391 : 386950 : if (GET_CODE (trueop0) == CONST_VECTOR
7392 : 8712 : && GET_CODE (trueop1) == CONST_VECTOR)
7393 : : {
7394 : 4629 : rtvec v = rtvec_alloc (n_elts);
7395 : 4629 : unsigned int i;
7396 : :
7397 : 52652 : for (i = 0; i < n_elts; i++)
7398 : 43394 : RTVEC_ELT (v, i) = ((sel & (HOST_WIDE_INT_1U << i))
7399 : 43394 : ? CONST_VECTOR_ELT (trueop0, i)
7400 : 24233 : : CONST_VECTOR_ELT (trueop1, i));
7401 : 4629 : return gen_rtx_CONST_VECTOR (mode, v);
7402 : : }
7403 : :
7404 : 382321 : if (swap_commutative_operands_p (op0, op1)
7405 : : /* Two operands have same precedence, then first bit of mask
7406 : : select first operand. */
7407 : 382321 : || (!swap_commutative_operands_p (op1, op0) && !(sel & 1)))
7408 : 23126 : return simplify_gen_ternary (code, mode, mode, op1, op0,
7409 : 46252 : GEN_INT (~sel & mask));
7410 : :
7411 : : /* Replace (vec_merge (vec_merge a b m) c n) with (vec_merge b c n)
7412 : : if no element from a appears in the result. */
7413 : 359195 : if (GET_CODE (op0) == VEC_MERGE)
7414 : : {
7415 : 17056 : tem = avoid_constant_pool_reference (XEXP (op0, 2));
7416 : 17056 : if (CONST_INT_P (tem))
7417 : : {
7418 : 1302 : unsigned HOST_WIDE_INT sel0 = UINTVAL (tem);
7419 : 1302 : if (!(sel & sel0 & mask) && !side_effects_p (XEXP (op0, 0)))
7420 : 99 : return simplify_gen_ternary (code, mode, mode,
7421 : 99 : XEXP (op0, 1), op1, op2);
7422 : 1203 : if (!(sel & ~sel0 & mask) && !side_effects_p (XEXP (op0, 1)))
7423 : 757 : return simplify_gen_ternary (code, mode, mode,
7424 : 757 : XEXP (op0, 0), op1, op2);
7425 : : }
7426 : : }
7427 : 358339 : if (GET_CODE (op1) == VEC_MERGE)
7428 : : {
7429 : 157 : tem = avoid_constant_pool_reference (XEXP (op1, 2));
7430 : 157 : if (CONST_INT_P (tem))
7431 : : {
7432 : 142 : unsigned HOST_WIDE_INT sel1 = UINTVAL (tem);
7433 : 142 : if (!(~sel & sel1 & mask) && !side_effects_p (XEXP (op1, 0)))
7434 : 113 : return simplify_gen_ternary (code, mode, mode,
7435 : 113 : op0, XEXP (op1, 1), op2);
7436 : 29 : if (!(~sel & ~sel1 & mask) && !side_effects_p (XEXP (op1, 1)))
7437 : 2 : return simplify_gen_ternary (code, mode, mode,
7438 : 2 : op0, XEXP (op1, 0), op2);
7439 : : }
7440 : : }
7441 : :
7442 : : /* Replace (vec_merge (vec_duplicate (vec_select a parallel (i))) a 1 << i)
7443 : : with a. */
7444 : 358224 : if (GET_CODE (op0) == VEC_DUPLICATE
7445 : 129574 : && GET_CODE (XEXP (op0, 0)) == VEC_SELECT
7446 : 601 : && GET_CODE (XEXP (XEXP (op0, 0), 1)) == PARALLEL
7447 : 359426 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (op0, 0))), 1))
7448 : : {
7449 : 533 : tem = XVECEXP ((XEXP (XEXP (op0, 0), 1)), 0, 0);
7450 : 533 : if (CONST_INT_P (tem) && CONST_INT_P (op2))
7451 : : {
7452 : 533 : if (XEXP (XEXP (op0, 0), 0) == op1
7453 : 2 : && UINTVAL (op2) == HOST_WIDE_INT_1U << UINTVAL (tem))
7454 : : return op1;
7455 : : }
7456 : : }
7457 : : /* Replace (vec_merge (vec_duplicate (X)) (const_vector [A, B])
7458 : : (const_int N))
7459 : : with (vec_concat (X) (B)) if N == 1 or
7460 : : (vec_concat (A) (X)) if N == 2. */
7461 : 358222 : if (GET_CODE (op0) == VEC_DUPLICATE
7462 : 129572 : && GET_CODE (op1) == CONST_VECTOR
7463 : 137202 : && known_eq (CONST_VECTOR_NUNITS (op1), 2)
7464 : 1510 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7465 : 358977 : && IN_RANGE (sel, 1, 2))
7466 : : {
7467 : 753 : rtx newop0 = XEXP (op0, 0);
7468 : 753 : rtx newop1 = CONST_VECTOR_ELT (op1, 2 - sel);
7469 : 753 : if (sel == 2)
7470 : 123 : std::swap (newop0, newop1);
7471 : 753 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7472 : : }
7473 : : /* Replace (vec_merge (vec_duplicate x) (vec_concat (y) (z)) (const_int N))
7474 : : with (vec_concat x z) if N == 1, or (vec_concat y x) if N == 2.
7475 : : Only applies for vectors of two elements. */
7476 : 357469 : if (GET_CODE (op0) == VEC_DUPLICATE
7477 : 128819 : && GET_CODE (op1) == VEC_CONCAT
7478 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7479 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7480 : 357469 : && IN_RANGE (sel, 1, 2))
7481 : : {
7482 : 0 : rtx newop0 = XEXP (op0, 0);
7483 : 0 : rtx newop1 = XEXP (op1, 2 - sel);
7484 : 0 : rtx otherop = XEXP (op1, sel - 1);
7485 : 0 : if (sel == 2)
7486 : 0 : std::swap (newop0, newop1);
7487 : : /* Don't want to throw away the other part of the vec_concat if
7488 : : it has side-effects. */
7489 : 0 : if (!side_effects_p (otherop))
7490 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7491 : : }
7492 : :
7493 : : /* Replace:
7494 : :
7495 : : (vec_merge:outer (vec_duplicate:outer x:inner)
7496 : : (subreg:outer y:inner 0)
7497 : : (const_int N))
7498 : :
7499 : : with (vec_concat:outer x:inner y:inner) if N == 1,
7500 : : or (vec_concat:outer y:inner x:inner) if N == 2.
7501 : :
7502 : : Implicitly, this means we have a paradoxical subreg, but such
7503 : : a check is cheap, so make it anyway.
7504 : :
7505 : : Only applies for vectors of two elements. */
7506 : 357469 : if (GET_CODE (op0) == VEC_DUPLICATE
7507 : 128819 : && GET_CODE (op1) == SUBREG
7508 : 40588 : && GET_MODE (op1) == GET_MODE (op0)
7509 : 40588 : && GET_MODE (SUBREG_REG (op1)) == GET_MODE (XEXP (op0, 0))
7510 : 0 : && paradoxical_subreg_p (op1)
7511 : 0 : && subreg_lowpart_p (op1)
7512 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7513 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7514 : 357469 : && IN_RANGE (sel, 1, 2))
7515 : : {
7516 : 0 : rtx newop0 = XEXP (op0, 0);
7517 : 0 : rtx newop1 = SUBREG_REG (op1);
7518 : 0 : if (sel == 2)
7519 : 0 : std::swap (newop0, newop1);
7520 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7521 : : }
7522 : :
7523 : : /* Same as above but with switched operands:
7524 : : Replace (vec_merge:outer (subreg:outer x:inner 0)
7525 : : (vec_duplicate:outer y:inner)
7526 : : (const_int N))
7527 : :
7528 : : with (vec_concat:outer x:inner y:inner) if N == 1,
7529 : : or (vec_concat:outer y:inner x:inner) if N == 2. */
7530 : 357469 : if (GET_CODE (op1) == VEC_DUPLICATE
7531 : 26552 : && GET_CODE (op0) == SUBREG
7532 : 24013 : && GET_MODE (op0) == GET_MODE (op1)
7533 : 24013 : && GET_MODE (SUBREG_REG (op0)) == GET_MODE (XEXP (op1, 0))
7534 : 0 : && paradoxical_subreg_p (op0)
7535 : 0 : && subreg_lowpart_p (op0)
7536 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7537 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7538 : 357469 : && IN_RANGE (sel, 1, 2))
7539 : : {
7540 : 0 : rtx newop0 = SUBREG_REG (op0);
7541 : 0 : rtx newop1 = XEXP (op1, 0);
7542 : 0 : if (sel == 2)
7543 : 0 : std::swap (newop0, newop1);
7544 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7545 : : }
7546 : :
7547 : : /* Replace (vec_merge (vec_duplicate x) (vec_duplicate y)
7548 : : (const_int n))
7549 : : with (vec_concat x y) or (vec_concat y x) depending on value
7550 : : of N. */
7551 : 357469 : if (GET_CODE (op0) == VEC_DUPLICATE
7552 : 128819 : && GET_CODE (op1) == VEC_DUPLICATE
7553 : 202 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7554 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7555 : 357469 : && IN_RANGE (sel, 1, 2))
7556 : : {
7557 : 0 : rtx newop0 = XEXP (op0, 0);
7558 : 0 : rtx newop1 = XEXP (op1, 0);
7559 : 0 : if (sel == 2)
7560 : 0 : std::swap (newop0, newop1);
7561 : :
7562 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7563 : : }
7564 : : }
7565 : :
7566 : 641846 : if (rtx_equal_p (op0, op1)
7567 : 641846 : && !side_effects_p (op2) && !side_effects_p (op1))
7568 : : return op0;
7569 : :
7570 : 641594 : if (!side_effects_p (op2))
7571 : : {
7572 : 638505 : rtx top0
7573 : 638505 : = may_trap_p (op0) ? NULL_RTX : simplify_merge_mask (op0, op2, 0);
7574 : 638505 : rtx top1
7575 : 638505 : = may_trap_p (op1) ? NULL_RTX : simplify_merge_mask (op1, op2, 1);
7576 : 638505 : if (top0 || top1)
7577 : 811 : return simplify_gen_ternary (code, mode, mode,
7578 : : top0 ? top0 : op0,
7579 : 657 : top1 ? top1 : op1, op2);
7580 : : }
7581 : :
7582 : : break;
7583 : :
7584 : 0 : default:
7585 : 0 : gcc_unreachable ();
7586 : : }
7587 : :
7588 : : return 0;
7589 : : }
7590 : :
7591 : : /* Try to calculate NUM_BYTES bytes of the target memory image of X,
7592 : : starting at byte FIRST_BYTE. Return true on success and add the
7593 : : bytes to BYTES, such that each byte has BITS_PER_UNIT bits and such
7594 : : that the bytes follow target memory order. Leave BYTES unmodified
7595 : : on failure.
7596 : :
7597 : : MODE is the mode of X. The caller must reserve NUM_BYTES bytes in
7598 : : BYTES before calling this function. */
7599 : :
7600 : : bool
7601 : 13054848 : native_encode_rtx (machine_mode mode, rtx x, vec<target_unit> &bytes,
7602 : : unsigned int first_byte, unsigned int num_bytes)
7603 : : {
7604 : : /* Check the mode is sensible. */
7605 : 13054848 : gcc_assert (GET_MODE (x) == VOIDmode
7606 : : ? is_a <scalar_int_mode> (mode)
7607 : : : mode == GET_MODE (x));
7608 : :
7609 : 13054848 : if (GET_CODE (x) == CONST_VECTOR)
7610 : : {
7611 : : /* CONST_VECTOR_ELT follows target memory order, so no shuffling
7612 : : is necessary. The only complication is that MODE_VECTOR_BOOL
7613 : : vectors can have several elements per byte. */
7614 : 879762 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
7615 : : GET_MODE_NUNITS (mode));
7616 : 439881 : unsigned int elt = first_byte * BITS_PER_UNIT / elt_bits;
7617 : 439881 : if (elt_bits < BITS_PER_UNIT)
7618 : : {
7619 : : /* This is the only case in which elements can be smaller than
7620 : : a byte. */
7621 : 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
7622 : 0 : auto mask = GET_MODE_MASK (GET_MODE_INNER (mode));
7623 : 0 : for (unsigned int i = 0; i < num_bytes; ++i)
7624 : : {
7625 : 0 : target_unit value = 0;
7626 : 0 : for (unsigned int j = 0; j < BITS_PER_UNIT; j += elt_bits)
7627 : : {
7628 : 0 : if (INTVAL (CONST_VECTOR_ELT (x, elt)))
7629 : 0 : value |= mask << j;
7630 : 0 : elt += 1;
7631 : : }
7632 : 0 : bytes.quick_push (value);
7633 : : }
7634 : : return true;
7635 : : }
7636 : :
7637 : 439881 : unsigned int start = bytes.length ();
7638 : 439881 : unsigned int elt_bytes = GET_MODE_UNIT_SIZE (mode);
7639 : : /* Make FIRST_BYTE relative to ELT. */
7640 : 439881 : first_byte %= elt_bytes;
7641 : 2256638 : while (num_bytes > 0)
7642 : : {
7643 : : /* Work out how many bytes we want from element ELT. */
7644 : 1816757 : unsigned int chunk_bytes = MIN (num_bytes, elt_bytes - first_byte);
7645 : 3633514 : if (!native_encode_rtx (GET_MODE_INNER (mode),
7646 : : CONST_VECTOR_ELT (x, elt), bytes,
7647 : : first_byte, chunk_bytes))
7648 : : {
7649 : 0 : bytes.truncate (start);
7650 : 0 : return false;
7651 : : }
7652 : 1816757 : elt += 1;
7653 : 1816757 : first_byte = 0;
7654 : 1816757 : num_bytes -= chunk_bytes;
7655 : : }
7656 : : return true;
7657 : : }
7658 : :
7659 : : /* All subsequent cases are limited to scalars. */
7660 : 12614967 : scalar_mode smode;
7661 : 12646537 : if (!is_a <scalar_mode> (mode, &smode))
7662 : : return false;
7663 : :
7664 : : /* Make sure that the region is in range. */
7665 : 12614967 : unsigned int end_byte = first_byte + num_bytes;
7666 : 12614967 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
7667 : 12614967 : gcc_assert (end_byte <= mode_bytes);
7668 : :
7669 : 12614967 : if (CONST_SCALAR_INT_P (x))
7670 : : {
7671 : : /* The target memory layout is affected by both BYTES_BIG_ENDIAN
7672 : : and WORDS_BIG_ENDIAN. Use the subreg machinery to get the lsb
7673 : : position of each byte. */
7674 : 12089690 : rtx_mode_t value (x, smode);
7675 : 12089690 : wide_int_ref value_wi (value);
7676 : 53142647 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7677 : : {
7678 : : /* Always constant because the inputs are. */
7679 : 41052957 : unsigned int lsb
7680 : 41052957 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7681 : : /* Operate directly on the encoding rather than using
7682 : : wi::extract_uhwi, so that we preserve the sign or zero
7683 : : extension for modes that are not a whole number of bits in
7684 : : size. (Zero extension is only used for the combination of
7685 : : innermode == BImode && STORE_FLAG_VALUE == 1). */
7686 : 41052957 : unsigned int elt = lsb / HOST_BITS_PER_WIDE_INT;
7687 : 41052957 : unsigned int shift = lsb % HOST_BITS_PER_WIDE_INT;
7688 : 41052957 : unsigned HOST_WIDE_INT uhwi = value_wi.elt (elt);
7689 : 41052957 : bytes.quick_push (uhwi >> shift);
7690 : : }
7691 : 12089690 : return true;
7692 : : }
7693 : :
7694 : 525277 : if (CONST_DOUBLE_P (x))
7695 : : {
7696 : : /* real_to_target produces an array of integers in target memory order.
7697 : : All integers before the last one have 32 bits; the last one may
7698 : : have 32 bits or fewer, depending on whether the mode bitsize
7699 : : is divisible by 32. Each of these integers is then laid out
7700 : : in target memory as any other integer would be. */
7701 : 493707 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
7702 : 493707 : real_to_target (el32, CONST_DOUBLE_REAL_VALUE (x), smode);
7703 : :
7704 : : /* The (maximum) number of target bytes per element of el32. */
7705 : 493707 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
7706 : 493707 : gcc_assert (bytes_per_el32 != 0);
7707 : :
7708 : : /* Build up the integers in a similar way to the CONST_SCALAR_INT_P
7709 : : handling above. */
7710 : 3407170 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7711 : : {
7712 : 2913463 : unsigned int index = byte / bytes_per_el32;
7713 : 2913463 : unsigned int subbyte = byte % bytes_per_el32;
7714 : 2913463 : unsigned int int_bytes = MIN (bytes_per_el32,
7715 : : mode_bytes - index * bytes_per_el32);
7716 : : /* Always constant because the inputs are. */
7717 : 2913463 : unsigned int lsb
7718 : 2913463 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
7719 : 2913463 : bytes.quick_push ((unsigned long) el32[index] >> lsb);
7720 : : }
7721 : 493707 : return true;
7722 : : }
7723 : :
7724 : 31570 : if (GET_CODE (x) == CONST_FIXED)
7725 : : {
7726 : 0 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7727 : : {
7728 : : /* Always constant because the inputs are. */
7729 : 0 : unsigned int lsb
7730 : 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7731 : 0 : unsigned HOST_WIDE_INT piece = CONST_FIXED_VALUE_LOW (x);
7732 : 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
7733 : : {
7734 : 0 : lsb -= HOST_BITS_PER_WIDE_INT;
7735 : 0 : piece = CONST_FIXED_VALUE_HIGH (x);
7736 : : }
7737 : 0 : bytes.quick_push (piece >> lsb);
7738 : : }
7739 : : return true;
7740 : : }
7741 : :
7742 : : return false;
7743 : : }
7744 : :
7745 : : /* Read a vector of mode MODE from the target memory image given by BYTES,
7746 : : starting at byte FIRST_BYTE. The vector is known to be encodable using
7747 : : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each,
7748 : : and BYTES is known to have enough bytes to supply NPATTERNS *
7749 : : NELTS_PER_PATTERN vector elements. Each element of BYTES contains
7750 : : BITS_PER_UNIT bits and the bytes are in target memory order.
7751 : :
7752 : : Return the vector on success, otherwise return NULL_RTX. */
7753 : :
7754 : : rtx
7755 : 158513 : native_decode_vector_rtx (machine_mode mode, const vec<target_unit> &bytes,
7756 : : unsigned int first_byte, unsigned int npatterns,
7757 : : unsigned int nelts_per_pattern)
7758 : : {
7759 : 158513 : rtx_vector_builder builder (mode, npatterns, nelts_per_pattern);
7760 : :
7761 : 317026 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
7762 : : GET_MODE_NUNITS (mode));
7763 : 158513 : if (elt_bits < BITS_PER_UNIT)
7764 : : {
7765 : : /* This is the only case in which elements can be smaller than a byte.
7766 : : Element 0 is always in the lsb of the containing byte. */
7767 : 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
7768 : 0 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
7769 : : {
7770 : 0 : unsigned int bit_index = first_byte * BITS_PER_UNIT + i * elt_bits;
7771 : 0 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
7772 : 0 : unsigned int lsb = bit_index % BITS_PER_UNIT;
7773 : 0 : unsigned int value = bytes[byte_index] >> lsb;
7774 : 0 : builder.quick_push (gen_int_mode (value, GET_MODE_INNER (mode)));
7775 : : }
7776 : : }
7777 : : else
7778 : : {
7779 : 578214 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
7780 : : {
7781 : 839402 : rtx x = native_decode_rtx (GET_MODE_INNER (mode), bytes, first_byte);
7782 : 419701 : if (!x)
7783 : 0 : return NULL_RTX;
7784 : 419701 : builder.quick_push (x);
7785 : 419701 : first_byte += elt_bits / BITS_PER_UNIT;
7786 : : }
7787 : : }
7788 : 158513 : return builder.build ();
7789 : 158513 : }
7790 : :
7791 : : /* Extract a PRECISION-bit integer from bytes [FIRST_BYTE, FIRST_BYTE + SIZE)
7792 : : of target memory image BYTES. */
7793 : :
7794 : : wide_int
7795 : 11174512 : native_decode_int (const vec<target_unit> &bytes, unsigned int first_byte,
7796 : : unsigned int size, unsigned int precision)
7797 : : {
7798 : : /* Pull the bytes msb first, so that we can use simple
7799 : : shift-and-insert wide_int operations. */
7800 : 11174512 : wide_int result (wi::zero (precision));
7801 : 53564529 : for (unsigned int i = 0; i < size; ++i)
7802 : : {
7803 : 42390017 : unsigned int lsb = (size - i - 1) * BITS_PER_UNIT;
7804 : : /* Always constant because the inputs are. */
7805 : 42390017 : unsigned int subbyte
7806 : 42390017 : = subreg_size_offset_from_lsb (1, size, lsb).to_constant ();
7807 : 42390017 : result <<= BITS_PER_UNIT;
7808 : 42390017 : result |= bytes[first_byte + subbyte];
7809 : : }
7810 : 11174512 : return result;
7811 : : }
7812 : :
7813 : : /* Read an rtx of mode MODE from the target memory image given by BYTES,
7814 : : starting at byte FIRST_BYTE. Each element of BYTES contains BITS_PER_UNIT
7815 : : bits and the bytes are in target memory order. The image has enough
7816 : : values to specify all bytes of MODE.
7817 : :
7818 : : Return the rtx on success, otherwise return NULL_RTX. */
7819 : :
7820 : : rtx
7821 : 11307741 : native_decode_rtx (machine_mode mode, const vec<target_unit> &bytes,
7822 : : unsigned int first_byte)
7823 : : {
7824 : 11307741 : if (VECTOR_MODE_P (mode))
7825 : : {
7826 : : /* If we know at compile time how many elements there are,
7827 : : pull each element directly from BYTES. */
7828 : 26159 : unsigned int nelts;
7829 : 52318 : if (GET_MODE_NUNITS (mode).is_constant (&nelts))
7830 : 26159 : return native_decode_vector_rtx (mode, bytes, first_byte, nelts, 1);
7831 : : return NULL_RTX;
7832 : : }
7833 : :
7834 : 11281582 : scalar_int_mode imode;
7835 : 11281582 : if (is_a <scalar_int_mode> (mode, &imode)
7836 : 11174512 : && GET_MODE_PRECISION (imode) <= MAX_BITSIZE_MODE_ANY_INT)
7837 : : {
7838 : 11174512 : auto result = native_decode_int (bytes, first_byte,
7839 : 11174512 : GET_MODE_SIZE (imode),
7840 : 22349024 : GET_MODE_PRECISION (imode));
7841 : 11174512 : return immed_wide_int_const (result, imode);
7842 : 11174512 : }
7843 : :
7844 : 107070 : scalar_float_mode fmode;
7845 : 107070 : if (is_a <scalar_float_mode> (mode, &fmode))
7846 : : {
7847 : : /* We need to build an array of integers in target memory order.
7848 : : All integers before the last one have 32 bits; the last one may
7849 : : have 32 bits or fewer, depending on whether the mode bitsize
7850 : : is divisible by 32. */
7851 : 107048 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
7852 : 107048 : unsigned int num_el32 = CEIL (GET_MODE_BITSIZE (fmode), 32);
7853 : 107048 : memset (el32, 0, num_el32 * sizeof (long));
7854 : :
7855 : : /* The (maximum) number of target bytes per element of el32. */
7856 : 107048 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
7857 : 107048 : gcc_assert (bytes_per_el32 != 0);
7858 : :
7859 : 107048 : unsigned int mode_bytes = GET_MODE_SIZE (fmode);
7860 : 781632 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
7861 : : {
7862 : 674584 : unsigned int index = byte / bytes_per_el32;
7863 : 674584 : unsigned int subbyte = byte % bytes_per_el32;
7864 : 674584 : unsigned int int_bytes = MIN (bytes_per_el32,
7865 : : mode_bytes - index * bytes_per_el32);
7866 : : /* Always constant because the inputs are. */
7867 : 674584 : unsigned int lsb
7868 : 674584 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
7869 : 674584 : el32[index] |= (unsigned long) bytes[first_byte + byte] << lsb;
7870 : : }
7871 : 107048 : REAL_VALUE_TYPE r;
7872 : 107048 : real_from_target (&r, el32, fmode);
7873 : 107048 : return const_double_from_real_value (r, fmode);
7874 : : }
7875 : :
7876 : 22 : if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
7877 : : {
7878 : 0 : scalar_mode smode = as_a <scalar_mode> (mode);
7879 : 0 : FIXED_VALUE_TYPE f;
7880 : 0 : f.data.low = 0;
7881 : 0 : f.data.high = 0;
7882 : 0 : f.mode = smode;
7883 : :
7884 : 0 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
7885 : 0 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
7886 : : {
7887 : : /* Always constant because the inputs are. */
7888 : 0 : unsigned int lsb
7889 : 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7890 : 0 : unsigned HOST_WIDE_INT unit = bytes[first_byte + byte];
7891 : 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
7892 : 0 : f.data.high |= unit << (lsb - HOST_BITS_PER_WIDE_INT);
7893 : : else
7894 : 0 : f.data.low |= unit << lsb;
7895 : : }
7896 : 0 : return CONST_FIXED_FROM_FIXED_VALUE (f, mode);
7897 : : }
7898 : :
7899 : : return NULL_RTX;
7900 : : }
7901 : :
7902 : : /* Simplify a byte offset BYTE into CONST_VECTOR X. The main purpose
7903 : : is to convert a runtime BYTE value into a constant one. */
7904 : :
7905 : : static poly_uint64
7906 : 230671 : simplify_const_vector_byte_offset (rtx x, poly_uint64 byte)
7907 : : {
7908 : : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
7909 : 230671 : machine_mode mode = GET_MODE (x);
7910 : 461342 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
7911 : : GET_MODE_NUNITS (mode));
7912 : : /* The number of bits needed to encode one element from each pattern. */
7913 : 230671 : unsigned int sequence_bits = CONST_VECTOR_NPATTERNS (x) * elt_bits;
7914 : :
7915 : : /* Identify the start point in terms of a sequence number and a byte offset
7916 : : within that sequence. */
7917 : 230671 : poly_uint64 first_sequence;
7918 : 230671 : unsigned HOST_WIDE_INT subbit;
7919 : 230671 : if (can_div_trunc_p (byte * BITS_PER_UNIT, sequence_bits,
7920 : : &first_sequence, &subbit))
7921 : : {
7922 : 230671 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
7923 : 230671 : if (nelts_per_pattern == 1)
7924 : : /* This is a duplicated vector, so the value of FIRST_SEQUENCE
7925 : : doesn't matter. */
7926 : 178581 : byte = subbit / BITS_PER_UNIT;
7927 : 52090 : else if (nelts_per_pattern == 2 && known_gt (first_sequence, 0U))
7928 : : {
7929 : : /* The subreg drops the first element from each pattern and
7930 : : only uses the second element. Find the first sequence
7931 : : that starts on a byte boundary. */
7932 : 6984 : subbit += least_common_multiple (sequence_bits, BITS_PER_UNIT);
7933 : 6984 : byte = subbit / BITS_PER_UNIT;
7934 : : }
7935 : : }
7936 : 230671 : return byte;
7937 : : }
7938 : :
7939 : : /* Subroutine of simplify_subreg in which:
7940 : :
7941 : : - X is known to be a CONST_VECTOR
7942 : : - OUTERMODE is known to be a vector mode
7943 : :
7944 : : Try to handle the subreg by operating on the CONST_VECTOR encoding
7945 : : rather than on each individual element of the CONST_VECTOR.
7946 : :
7947 : : Return the simplified subreg on success, otherwise return NULL_RTX. */
7948 : :
7949 : : static rtx
7950 : 139897 : simplify_const_vector_subreg (machine_mode outermode, rtx x,
7951 : : machine_mode innermode, unsigned int first_byte)
7952 : : {
7953 : : /* Paradoxical subregs of vectors have dubious semantics. */
7954 : 139897 : if (paradoxical_subreg_p (outermode, innermode))
7955 : : return NULL_RTX;
7956 : :
7957 : : /* We can only preserve the semantics of a stepped pattern if the new
7958 : : vector element is the same as the original one. */
7959 : 139825 : if (CONST_VECTOR_STEPPED_P (x)
7960 : 160999 : && GET_MODE_INNER (outermode) != GET_MODE_INNER (innermode))
7961 : : return NULL_RTX;
7962 : :
7963 : : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
7964 : 132354 : unsigned int x_elt_bits
7965 : 132354 : = vector_element_size (GET_MODE_PRECISION (innermode),
7966 : : GET_MODE_NUNITS (innermode));
7967 : 132354 : unsigned int out_elt_bits
7968 : 132354 : = vector_element_size (GET_MODE_PRECISION (outermode),
7969 : : GET_MODE_NUNITS (outermode));
7970 : :
7971 : : /* The number of bits needed to encode one element from every pattern
7972 : : of the original vector. */
7973 : 132354 : unsigned int x_sequence_bits = CONST_VECTOR_NPATTERNS (x) * x_elt_bits;
7974 : :
7975 : : /* The number of bits needed to encode one element from every pattern
7976 : : of the result. */
7977 : 132354 : unsigned int out_sequence_bits
7978 : 132354 : = least_common_multiple (x_sequence_bits, out_elt_bits);
7979 : :
7980 : : /* Work out the number of interleaved patterns in the output vector
7981 : : and the number of encoded elements per pattern. */
7982 : 132354 : unsigned int out_npatterns = out_sequence_bits / out_elt_bits;
7983 : 132354 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
7984 : :
7985 : : /* The encoding scheme requires the number of elements to be a multiple
7986 : : of the number of patterns, so that each pattern appears at least once
7987 : : and so that the same number of elements appear from each pattern. */
7988 : 264708 : bool ok_p = multiple_p (GET_MODE_NUNITS (outermode), out_npatterns);
7989 : 132354 : unsigned int const_nunits;
7990 : 264708 : if (GET_MODE_NUNITS (outermode).is_constant (&const_nunits)
7991 : 132354 : && (!ok_p || out_npatterns * nelts_per_pattern > const_nunits))
7992 : : {
7993 : : /* Either the encoding is invalid, or applying it would give us
7994 : : more elements than we need. Just encode each element directly. */
7995 : : out_npatterns = const_nunits;
7996 : : nelts_per_pattern = 1;
7997 : : }
7998 : : else if (!ok_p)
7999 : : return NULL_RTX;
8000 : :
8001 : : /* Get enough bytes of X to form the new encoding. */
8002 : 132354 : unsigned int buffer_bits = out_npatterns * nelts_per_pattern * out_elt_bits;
8003 : 132354 : unsigned int buffer_bytes = CEIL (buffer_bits, BITS_PER_UNIT);
8004 : 132354 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8005 : 132354 : if (!native_encode_rtx (innermode, x, buffer, first_byte, buffer_bytes))
8006 : : return NULL_RTX;
8007 : :
8008 : : /* Reencode the bytes as OUTERMODE. */
8009 : 132354 : return native_decode_vector_rtx (outermode, buffer, 0, out_npatterns,
8010 : 132354 : nelts_per_pattern);
8011 : 132354 : }
8012 : :
8013 : : /* Try to simplify a subreg of a constant by encoding the subreg region
8014 : : as a sequence of target bytes and reading them back in the new mode.
8015 : : Return the new value on success, otherwise return null.
8016 : :
8017 : : The subreg has outer mode OUTERMODE, inner mode INNERMODE, inner value X
8018 : : and byte offset FIRST_BYTE. */
8019 : :
8020 : : static rtx
8021 : 10604884 : simplify_immed_subreg (fixed_size_mode outermode, rtx x,
8022 : : machine_mode innermode, unsigned int first_byte)
8023 : : {
8024 : 10604884 : unsigned int buffer_bytes = GET_MODE_SIZE (outermode);
8025 : 10604884 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8026 : :
8027 : : /* Some ports misuse CCmode. */
8028 : 10604884 : if (GET_MODE_CLASS (outermode) == MODE_CC && CONST_INT_P (x))
8029 : : return x;
8030 : :
8031 : : /* Paradoxical subregs read undefined values for bytes outside of the
8032 : : inner value. However, we have traditionally always sign-extended
8033 : : integer constants and zero-extended others. */
8034 : 10604262 : unsigned int inner_bytes = buffer_bytes;
8035 : 10604262 : if (paradoxical_subreg_p (outermode, innermode))
8036 : : {
8037 : 1075686 : if (!GET_MODE_SIZE (innermode).is_constant (&inner_bytes))
8038 : 0 : return NULL_RTX;
8039 : :
8040 : 537843 : target_unit filler = 0;
8041 : 537843 : if (CONST_SCALAR_INT_P (x) && wi::neg_p (rtx_mode_t (x, innermode)))
8042 : 32969 : filler = -1;
8043 : :
8044 : : /* Add any leading bytes due to big-endian layout. The number of
8045 : : bytes must be constant because both modes have constant size. */
8046 : 537843 : unsigned int leading_bytes
8047 : 537843 : = -byte_lowpart_offset (outermode, innermode).to_constant ();
8048 : 537843 : for (unsigned int i = 0; i < leading_bytes; ++i)
8049 : 0 : buffer.quick_push (filler);
8050 : :
8051 : 537843 : if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8052 : 0 : return NULL_RTX;
8053 : :
8054 : : /* Add any trailing bytes due to little-endian layout. */
8055 : 5772716 : while (buffer.length () < buffer_bytes)
8056 : 2348515 : buffer.quick_push (filler);
8057 : : }
8058 : 10066419 : else if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8059 : : return NULL_RTX;
8060 : 10604262 : rtx ret = native_decode_rtx (outermode, buffer, 0);
8061 : 10604262 : if (ret && FLOAT_MODE_P (outermode))
8062 : : {
8063 : 69463 : auto_vec<target_unit, 128> buffer2 (buffer_bytes);
8064 : 69463 : if (!native_encode_rtx (outermode, ret, buffer2, 0, buffer_bytes))
8065 : : return NULL_RTX;
8066 : 669644 : for (unsigned int i = 0; i < buffer_bytes; ++i)
8067 : 600221 : if (buffer[i] != buffer2[i])
8068 : : return NULL_RTX;
8069 : 69463 : }
8070 : : return ret;
8071 : 10604884 : }
8072 : :
8073 : : /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
8074 : : Return 0 if no simplifications are possible. */
8075 : : rtx
8076 : 70809071 : simplify_context::simplify_subreg (machine_mode outermode, rtx op,
8077 : : machine_mode innermode, poly_uint64 byte)
8078 : : {
8079 : : /* Little bit of sanity checking. */
8080 : 70809071 : gcc_assert (innermode != VOIDmode);
8081 : 70809071 : gcc_assert (outermode != VOIDmode);
8082 : 70809071 : gcc_assert (innermode != BLKmode);
8083 : 70809071 : gcc_assert (outermode != BLKmode);
8084 : :
8085 : 70809071 : gcc_assert (GET_MODE (op) == innermode
8086 : : || GET_MODE (op) == VOIDmode);
8087 : :
8088 : 141618142 : poly_uint64 outersize = GET_MODE_SIZE (outermode);
8089 : 70809071 : if (!multiple_p (byte, outersize))
8090 : : return NULL_RTX;
8091 : :
8092 : 141618134 : poly_uint64 innersize = GET_MODE_SIZE (innermode);
8093 : 70809067 : if (maybe_ge (byte, innersize))
8094 : : return NULL_RTX;
8095 : :
8096 : 70809067 : if (outermode == innermode && known_eq (byte, 0U))
8097 : 4556734 : return op;
8098 : :
8099 : 66252333 : if (GET_CODE (op) == CONST_VECTOR)
8100 : 230671 : byte = simplify_const_vector_byte_offset (op, byte);
8101 : :
8102 : 132504666 : if (multiple_p (byte, GET_MODE_UNIT_SIZE (innermode)))
8103 : : {
8104 : 60321665 : rtx elt;
8105 : :
8106 : 52643795 : if (VECTOR_MODE_P (outermode)
8107 : 15355740 : && GET_MODE_INNER (outermode) == GET_MODE_INNER (innermode)
8108 : 61844781 : && vec_duplicate_p (op, &elt))
8109 : 9752 : return gen_vec_duplicate (outermode, elt);
8110 : :
8111 : 60319084 : if (outermode == GET_MODE_INNER (innermode)
8112 : 60319084 : && vec_duplicate_p (op, &elt))
8113 : 7171 : return elt;
8114 : : }
8115 : :
8116 : 66242581 : if (CONST_SCALAR_INT_P (op)
8117 : 55756096 : || CONST_DOUBLE_AS_FLOAT_P (op)
8118 : 55732268 : || CONST_FIXED_P (op)
8119 : 55732268 : || GET_CODE (op) == CONST_VECTOR)
8120 : : {
8121 : 10737238 : unsigned HOST_WIDE_INT cbyte;
8122 : 10737238 : if (byte.is_constant (&cbyte))
8123 : : {
8124 : 10737238 : if (GET_CODE (op) == CONST_VECTOR && VECTOR_MODE_P (outermode))
8125 : : {
8126 : 139897 : rtx tmp = simplify_const_vector_subreg (outermode, op,
8127 : : innermode, cbyte);
8128 : 139897 : if (tmp)
8129 : 10737238 : return tmp;
8130 : : }
8131 : :
8132 : 10604884 : fixed_size_mode fs_outermode;
8133 : 10604884 : if (is_a <fixed_size_mode> (outermode, &fs_outermode))
8134 : 10604884 : return simplify_immed_subreg (fs_outermode, op, innermode, cbyte);
8135 : : }
8136 : : }
8137 : :
8138 : : /* Changing mode twice with SUBREG => just change it once,
8139 : : or not at all if changing back op starting mode. */
8140 : 55505343 : if (GET_CODE (op) == SUBREG)
8141 : : {
8142 : 1220795 : machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
8143 : 2441590 : poly_uint64 innermostsize = GET_MODE_SIZE (innermostmode);
8144 : 1220795 : rtx newx;
8145 : :
8146 : : /* Make sure that the relationship between the two subregs is
8147 : : known at compile time. */
8148 : 1220795 : if (!ordered_p (outersize, innermostsize))
8149 : : return NULL_RTX;
8150 : :
8151 : 1220795 : if (outermode == innermostmode
8152 : 614719 : && known_eq (byte, subreg_lowpart_offset (outermode, innermode))
8153 : 1835512 : && known_eq (SUBREG_BYTE (op),
8154 : : subreg_lowpart_offset (innermode, innermostmode)))
8155 : 614717 : return SUBREG_REG (op);
8156 : :
8157 : : /* Work out the memory offset of the final OUTERMODE value relative
8158 : : to the inner value of OP. */
8159 : 606078 : poly_int64 mem_offset = subreg_memory_offset (outermode,
8160 : : innermode, byte);
8161 : 606078 : poly_int64 op_mem_offset = subreg_memory_offset (op);
8162 : 606078 : poly_int64 final_offset = mem_offset + op_mem_offset;
8163 : :
8164 : : /* See whether resulting subreg will be paradoxical. */
8165 : 606078 : if (!paradoxical_subreg_p (outermode, innermostmode))
8166 : : {
8167 : : /* Bail out in case resulting subreg would be incorrect. */
8168 : 995686 : if (maybe_lt (final_offset, 0)
8169 : 995680 : || maybe_ge (poly_uint64 (final_offset), innermostsize)
8170 : 995684 : || !multiple_p (final_offset, outersize))
8171 : 6 : return NULL_RTX;
8172 : : }
8173 : : else
8174 : : {
8175 : 108235 : poly_int64 required_offset = subreg_memory_offset (outermode,
8176 : : innermostmode, 0);
8177 : 108235 : if (maybe_ne (final_offset, required_offset))
8178 : 0 : return NULL_RTX;
8179 : : /* Paradoxical subregs always have byte offset 0. */
8180 : 108235 : final_offset = 0;
8181 : : }
8182 : :
8183 : : /* Recurse for further possible simplifications. */
8184 : 606072 : newx = simplify_subreg (outermode, SUBREG_REG (op), innermostmode,
8185 : 606072 : final_offset);
8186 : 606072 : if (newx)
8187 : : return newx;
8188 : 605687 : if (validate_subreg (outermode, innermostmode,
8189 : 605687 : SUBREG_REG (op), final_offset))
8190 : : {
8191 : 556205 : newx = gen_rtx_SUBREG (outermode, SUBREG_REG (op), final_offset);
8192 : 556205 : if (SUBREG_PROMOTED_VAR_P (op)
8193 : 298 : && SUBREG_PROMOTED_SIGN (op) >= 0
8194 : 298 : && GET_MODE_CLASS (outermode) == MODE_INT
8195 : 293 : && known_ge (outersize, innersize)
8196 : 292 : && known_le (outersize, innermostsize)
8197 : 556209 : && subreg_lowpart_p (newx))
8198 : : {
8199 : 4 : SUBREG_PROMOTED_VAR_P (newx) = 1;
8200 : 4 : SUBREG_PROMOTED_SET (newx, SUBREG_PROMOTED_GET (op));
8201 : : }
8202 : 556205 : return newx;
8203 : : }
8204 : : return NULL_RTX;
8205 : : }
8206 : :
8207 : : /* SUBREG of a hard register => just change the register number
8208 : : and/or mode. If the hard register is not valid in that mode,
8209 : : suppress this simplification. If the hard register is the stack,
8210 : : frame, or argument pointer, leave this as a SUBREG. */
8211 : :
8212 : 54284548 : if (REG_P (op) && HARD_REGISTER_P (op))
8213 : : {
8214 : 10702579 : unsigned int regno, final_regno;
8215 : :
8216 : 10702579 : regno = REGNO (op);
8217 : 10702579 : final_regno = simplify_subreg_regno (regno, innermode, byte, outermode);
8218 : 10702579 : if (HARD_REGISTER_NUM_P (final_regno))
8219 : : {
8220 : 10678092 : rtx x = gen_rtx_REG_offset (op, outermode, final_regno,
8221 : : subreg_memory_offset (outermode,
8222 : : innermode, byte));
8223 : :
8224 : : /* Propagate original regno. We don't have any way to specify
8225 : : the offset inside original regno, so do so only for lowpart.
8226 : : The information is used only by alias analysis that cannot
8227 : : grog partial register anyway. */
8228 : :
8229 : 10678092 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte))
8230 : 7994759 : ORIGINAL_REGNO (x) = ORIGINAL_REGNO (op);
8231 : 10678092 : return x;
8232 : : }
8233 : : }
8234 : :
8235 : : /* If we have a SUBREG of a register that we are replacing and we are
8236 : : replacing it with a MEM, make a new MEM and try replacing the
8237 : : SUBREG with it. Don't do this if the MEM has a mode-dependent address
8238 : : or if we would be widening it. */
8239 : :
8240 : 43606456 : if (MEM_P (op)
8241 : 1701550 : && ! mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op))
8242 : : /* Allow splitting of volatile memory references in case we don't
8243 : : have instruction to move the whole thing. */
8244 : 1701547 : && (! MEM_VOLATILE_P (op)
8245 : 44101 : || ! have_insn_for (SET, innermode))
8246 : : && !(STRICT_ALIGNMENT && MEM_ALIGN (op) < GET_MODE_ALIGNMENT (outermode))
8247 : 45263902 : && known_le (outersize, innersize))
8248 : 811495 : return adjust_address_nv (op, outermode, byte);
8249 : :
8250 : : /* Handle complex or vector values represented as CONCAT or VEC_CONCAT
8251 : : of two parts. */
8252 : 42794961 : if (GET_CODE (op) == CONCAT
8253 : 42794961 : || GET_CODE (op) == VEC_CONCAT)
8254 : : {
8255 : 181090 : poly_uint64 final_offset;
8256 : 181090 : rtx part, res;
8257 : :
8258 : 181090 : machine_mode part_mode = GET_MODE (XEXP (op, 0));
8259 : 181090 : if (part_mode == VOIDmode)
8260 : 1 : part_mode = GET_MODE_INNER (GET_MODE (op));
8261 : 362180 : poly_uint64 part_size = GET_MODE_SIZE (part_mode);
8262 : 181090 : if (known_lt (byte, part_size))
8263 : : {
8264 : 179640 : part = XEXP (op, 0);
8265 : 179640 : final_offset = byte;
8266 : : }
8267 : 1450 : else if (known_ge (byte, part_size))
8268 : : {
8269 : 1450 : part = XEXP (op, 1);
8270 : 1450 : final_offset = byte - part_size;
8271 : : }
8272 : : else
8273 : : return NULL_RTX;
8274 : :
8275 : 181090 : if (maybe_gt (final_offset + outersize, part_size))
8276 : : return NULL_RTX;
8277 : :
8278 : 127151 : part_mode = GET_MODE (part);
8279 : 127151 : if (part_mode == VOIDmode)
8280 : 0 : part_mode = GET_MODE_INNER (GET_MODE (op));
8281 : 127151 : res = simplify_subreg (outermode, part, part_mode, final_offset);
8282 : 127151 : if (res)
8283 : : return res;
8284 : 231 : if (validate_subreg (outermode, part_mode, part, final_offset))
8285 : 231 : return gen_rtx_SUBREG (outermode, part, final_offset);
8286 : : return NULL_RTX;
8287 : : }
8288 : :
8289 : : /* Simplify
8290 : : (subreg (vec_merge (X)
8291 : : (vector)
8292 : : (const_int ((1 << N) | M)))
8293 : : (N * sizeof (outermode)))
8294 : : to
8295 : : (subreg (X) (N * sizeof (outermode)))
8296 : : */
8297 : 42613871 : unsigned int idx;
8298 : 85227742 : if (constant_multiple_p (byte, GET_MODE_SIZE (outermode), &idx)
8299 : 42613871 : && idx < HOST_BITS_PER_WIDE_INT
8300 : 42613871 : && GET_CODE (op) == VEC_MERGE
8301 : 300053 : && GET_MODE_INNER (innermode) == outermode
8302 : 5912 : && CONST_INT_P (XEXP (op, 2))
8303 : 42619310 : && (UINTVAL (XEXP (op, 2)) & (HOST_WIDE_INT_1U << idx)) != 0)
8304 : 5430 : return simplify_gen_subreg (outermode, XEXP (op, 0), innermode, byte);
8305 : :
8306 : : /* A SUBREG resulting from a zero extension may fold to zero if
8307 : : it extracts higher bits that the ZERO_EXTEND's source bits. */
8308 : 42608441 : if (GET_CODE (op) == ZERO_EXTEND && SCALAR_INT_MODE_P (innermode))
8309 : : {
8310 : 242602 : poly_uint64 bitpos = subreg_lsb_1 (outermode, innermode, byte);
8311 : 242602 : if (known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (XEXP (op, 0)))))
8312 : 56102 : return CONST0_RTX (outermode);
8313 : : }
8314 : :
8315 : : /* Optimize SUBREGS of scalar integral ASHIFT by a valid constant. */
8316 : 42552339 : if (GET_CODE (op) == ASHIFT
8317 : 1061111 : && SCALAR_INT_MODE_P (innermode)
8318 : 1034507 : && CONST_INT_P (XEXP (op, 1))
8319 : 966159 : && INTVAL (XEXP (op, 1)) > 0
8320 : 44579560 : && known_gt (GET_MODE_BITSIZE (innermode), INTVAL (XEXP (op, 1))))
8321 : : {
8322 : 966110 : HOST_WIDE_INT val = INTVAL (XEXP (op, 1));
8323 : : /* A lowpart SUBREG of a ASHIFT by a constant may fold to zero. */
8324 : 966110 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte)
8325 : 1885491 : && known_le (GET_MODE_BITSIZE (outermode), val))
8326 : 279326 : return CONST0_RTX (outermode);
8327 : : /* Optimize the highpart SUBREG of a suitable ASHIFT (ZERO_EXTEND). */
8328 : 731603 : if (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
8329 : 45486 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == outermode
8330 : 45300 : && known_eq (GET_MODE_BITSIZE (outermode), val)
8331 : 89638 : && known_eq (GET_MODE_BITSIZE (innermode), 2 * val)
8332 : 777089 : && known_eq (subreg_highpart_offset (outermode, innermode), byte))
8333 : 44819 : return XEXP (XEXP (op, 0), 0);
8334 : : }
8335 : :
8336 : : /* Attempt to simplify WORD_MODE SUBREGs of bitwise expressions. */
8337 : 42273013 : if (outermode == word_mode
8338 : 15482165 : && (GET_CODE (op) == IOR || GET_CODE (op) == XOR || GET_CODE (op) == AND)
8339 : 42923051 : && SCALAR_INT_MODE_P (innermode))
8340 : : {
8341 : 648782 : rtx op0 = simplify_subreg (outermode, XEXP (op, 0), innermode, byte);
8342 : 648782 : rtx op1 = simplify_subreg (outermode, XEXP (op, 1), innermode, byte);
8343 : 648782 : if (op0 && op1)
8344 : 140059 : return simplify_gen_binary (GET_CODE (op), outermode, op0, op1);
8345 : : }
8346 : :
8347 : 42132954 : scalar_int_mode int_outermode, int_innermode;
8348 : 42132954 : if (is_a <scalar_int_mode> (outermode, &int_outermode)
8349 : 36084546 : && is_a <scalar_int_mode> (innermode, &int_innermode)
8350 : 78217500 : && known_eq (byte, subreg_lowpart_offset (int_outermode, int_innermode)))
8351 : : {
8352 : : /* Handle polynomial integers. The upper bits of a paradoxical
8353 : : subreg are undefined, so this is safe regardless of whether
8354 : : we're truncating or extending. */
8355 : 32844879 : if (CONST_POLY_INT_P (op))
8356 : : {
8357 : : poly_wide_int val
8358 : : = poly_wide_int::from (const_poly_int_value (op),
8359 : : GET_MODE_PRECISION (int_outermode),
8360 : : SIGNED);
8361 : : return immed_wide_int_const (val, int_outermode);
8362 : : }
8363 : :
8364 : 32844879 : if (GET_MODE_PRECISION (int_outermode)
8365 : 32844879 : < GET_MODE_PRECISION (int_innermode))
8366 : : {
8367 : 19172768 : rtx tem = simplify_truncation (int_outermode, op, int_innermode);
8368 : 19172768 : if (tem)
8369 : : return tem;
8370 : : }
8371 : : }
8372 : :
8373 : : /* If the outer mode is not integral, try taking a subreg with the equivalent
8374 : : integer outer mode and then bitcasting the result.
8375 : : Other simplifications rely on integer to integer subregs and we'd
8376 : : potentially miss out on optimizations otherwise. */
8377 : 81827192 : if (known_gt (GET_MODE_SIZE (innermode),
8378 : : GET_MODE_SIZE (outermode))
8379 : 21212579 : && SCALAR_INT_MODE_P (innermode)
8380 : 20286403 : && !SCALAR_INT_MODE_P (outermode)
8381 : 62206902 : && int_mode_for_size (GET_MODE_BITSIZE (outermode),
8382 : 80727 : 0).exists (&int_outermode))
8383 : : {
8384 : 80727 : rtx tem = simplify_subreg (int_outermode, op, innermode, byte);
8385 : 80727 : if (tem)
8386 : 2004 : return lowpart_subreg (outermode, tem, int_outermode);
8387 : : }
8388 : :
8389 : : /* If OP is a vector comparison and the subreg is not changing the
8390 : : number of elements or the size of the elements, change the result
8391 : : of the comparison to the new mode. */
8392 : 40911592 : if (COMPARISON_P (op)
8393 : 237092 : && VECTOR_MODE_P (outermode)
8394 : 167732 : && VECTOR_MODE_P (innermode)
8395 : 335448 : && known_eq (GET_MODE_NUNITS (outermode), GET_MODE_NUNITS (innermode))
8396 : 41265819 : && known_eq (GET_MODE_UNIT_SIZE (outermode),
8397 : : GET_MODE_UNIT_SIZE (innermode)))
8398 : 116791 : return simplify_gen_relational (GET_CODE (op), outermode, innermode,
8399 : 116791 : XEXP (op, 0), XEXP (op, 1));
8400 : : return NULL_RTX;
8401 : : }
8402 : :
8403 : : /* Make a SUBREG operation or equivalent if it folds. */
8404 : :
8405 : : rtx
8406 : 45440163 : simplify_context::simplify_gen_subreg (machine_mode outermode, rtx op,
8407 : : machine_mode innermode,
8408 : : poly_uint64 byte)
8409 : : {
8410 : 45440163 : rtx newx;
8411 : :
8412 : 45440163 : newx = simplify_subreg (outermode, op, innermode, byte);
8413 : 45440163 : if (newx)
8414 : : return newx;
8415 : :
8416 : 21864630 : if (GET_CODE (op) == SUBREG
8417 : 21864630 : || GET_CODE (op) == CONCAT
8418 : 21837406 : || GET_MODE (op) == VOIDmode)
8419 : : return NULL_RTX;
8420 : :
8421 : 23873562 : if (MODE_COMPOSITE_P (outermode)
8422 : 0 : && (CONST_SCALAR_INT_P (op)
8423 : 0 : || CONST_DOUBLE_AS_FLOAT_P (op)
8424 : 0 : || CONST_FIXED_P (op)
8425 : 0 : || GET_CODE (op) == CONST_VECTOR))
8426 : 0 : return NULL_RTX;
8427 : :
8428 : 21837388 : if (validate_subreg (outermode, innermode, op, byte))
8429 : 21806293 : return gen_rtx_SUBREG (outermode, op, byte);
8430 : :
8431 : : return NULL_RTX;
8432 : : }
8433 : :
8434 : : /* Generates a subreg to get the least significant part of EXPR (in mode
8435 : : INNER_MODE) to OUTER_MODE. */
8436 : :
8437 : : rtx
8438 : 34322600 : simplify_context::lowpart_subreg (machine_mode outer_mode, rtx expr,
8439 : : machine_mode inner_mode)
8440 : : {
8441 : 34322600 : return simplify_gen_subreg (outer_mode, expr, inner_mode,
8442 : 34322600 : subreg_lowpart_offset (outer_mode, inner_mode));
8443 : : }
8444 : :
8445 : : /* Generate RTX to select element at INDEX out of vector OP. */
8446 : :
8447 : : rtx
8448 : 561513 : simplify_context::simplify_gen_vec_select (rtx op, unsigned int index)
8449 : : {
8450 : 561513 : gcc_assert (VECTOR_MODE_P (GET_MODE (op)));
8451 : :
8452 : 561513 : scalar_mode imode = GET_MODE_INNER (GET_MODE (op));
8453 : :
8454 : 1123026 : if (known_eq (index * GET_MODE_SIZE (imode),
8455 : : subreg_lowpart_offset (imode, GET_MODE (op))))
8456 : : {
8457 : 561363 : rtx res = lowpart_subreg (imode, op, GET_MODE (op));
8458 : 561363 : if (res)
8459 : : return res;
8460 : : }
8461 : :
8462 : 215 : rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (index)));
8463 : 215 : return gen_rtx_VEC_SELECT (imode, op, tmp);
8464 : : }
8465 : :
8466 : :
8467 : : /* Simplify X, an rtx expression.
8468 : :
8469 : : Return the simplified expression or NULL if no simplifications
8470 : : were possible.
8471 : :
8472 : : This is the preferred entry point into the simplification routines;
8473 : : however, we still allow passes to call the more specific routines.
8474 : :
8475 : : Right now GCC has three (yes, three) major bodies of RTL simplification
8476 : : code that need to be unified.
8477 : :
8478 : : 1. fold_rtx in cse.cc. This code uses various CSE specific
8479 : : information to aid in RTL simplification.
8480 : :
8481 : : 2. simplify_rtx in combine.cc. Similar to fold_rtx, except that
8482 : : it uses combine specific information to aid in RTL
8483 : : simplification.
8484 : :
8485 : : 3. The routines in this file.
8486 : :
8487 : :
8488 : : Long term we want to only have one body of simplification code; to
8489 : : get to that state I recommend the following steps:
8490 : :
8491 : : 1. Pour over fold_rtx & simplify_rtx and move any simplifications
8492 : : which are not pass dependent state into these routines.
8493 : :
8494 : : 2. As code is moved by #1, change fold_rtx & simplify_rtx to
8495 : : use this routine whenever possible.
8496 : :
8497 : : 3. Allow for pass dependent state to be provided to these
8498 : : routines and add simplifications based on the pass dependent
8499 : : state. Remove code from cse.cc & combine.cc that becomes
8500 : : redundant/dead.
8501 : :
8502 : : It will take time, but ultimately the compiler will be easier to
8503 : : maintain and improve. It's totally silly that when we add a
8504 : : simplification that it needs to be added to 4 places (3 for RTL
8505 : : simplification and 1 for tree simplification. */
8506 : :
8507 : : rtx
8508 : 46129225 : simplify_rtx (const_rtx x)
8509 : : {
8510 : 46129225 : const enum rtx_code code = GET_CODE (x);
8511 : 46129225 : const machine_mode mode = GET_MODE (x);
8512 : :
8513 : 46129225 : switch (GET_RTX_CLASS (code))
8514 : : {
8515 : 810066 : case RTX_UNARY:
8516 : 1620132 : return simplify_unary_operation (code, mode,
8517 : 810066 : XEXP (x, 0), GET_MODE (XEXP (x, 0)));
8518 : 26003604 : case RTX_COMM_ARITH:
8519 : 26003604 : if (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8520 : 424472 : return simplify_gen_binary (code, mode, XEXP (x, 1), XEXP (x, 0));
8521 : :
8522 : : /* Fall through. */
8523 : :
8524 : 32224094 : case RTX_BIN_ARITH:
8525 : 32224094 : return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
8526 : :
8527 : 63215 : case RTX_TERNARY:
8528 : 63215 : case RTX_BITFIELD_OPS:
8529 : 63215 : return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
8530 : 63215 : XEXP (x, 0), XEXP (x, 1),
8531 : 63215 : XEXP (x, 2));
8532 : :
8533 : 196085 : case RTX_COMPARE:
8534 : 196085 : case RTX_COMM_COMPARE:
8535 : 196085 : return simplify_relational_operation (code, mode,
8536 : 196085 : ((GET_MODE (XEXP (x, 0))
8537 : : != VOIDmode)
8538 : : ? GET_MODE (XEXP (x, 0))
8539 : 354 : : GET_MODE (XEXP (x, 1))),
8540 : 196085 : XEXP (x, 0),
8541 : 392170 : XEXP (x, 1));
8542 : :
8543 : 225616 : case RTX_EXTRA:
8544 : 225616 : if (code == SUBREG)
8545 : 2351 : return simplify_subreg (mode, SUBREG_REG (x),
8546 : 2351 : GET_MODE (SUBREG_REG (x)),
8547 : 2351 : SUBREG_BYTE (x));
8548 : : break;
8549 : :
8550 : 6515303 : case RTX_OBJ:
8551 : 6515303 : if (code == LO_SUM)
8552 : : {
8553 : : /* Convert (lo_sum (high FOO) FOO) to FOO. */
8554 : 0 : if (GET_CODE (XEXP (x, 0)) == HIGH
8555 : 0 : && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
8556 : 0 : return XEXP (x, 1);
8557 : : }
8558 : : break;
8559 : :
8560 : : default:
8561 : : break;
8562 : : }
8563 : : return NULL;
8564 : : }
8565 : :
8566 : : #if CHECKING_P
8567 : :
8568 : : namespace selftest {
8569 : :
8570 : : /* Make a unique pseudo REG of mode MODE for use by selftests. */
8571 : :
8572 : : static rtx
8573 : 2888 : make_test_reg (machine_mode mode)
8574 : : {
8575 : 2888 : static int test_reg_num = LAST_VIRTUAL_REGISTER + 1;
8576 : :
8577 : 2888 : return gen_rtx_REG (mode, test_reg_num++);
8578 : : }
8579 : :
8580 : : static void
8581 : 40 : test_scalar_int_ops (machine_mode mode)
8582 : : {
8583 : 40 : rtx op0 = make_test_reg (mode);
8584 : 40 : rtx op1 = make_test_reg (mode);
8585 : 40 : rtx six = GEN_INT (6);
8586 : :
8587 : 40 : rtx neg_op0 = simplify_gen_unary (NEG, mode, op0, mode);
8588 : 40 : rtx not_op0 = simplify_gen_unary (NOT, mode, op0, mode);
8589 : 40 : rtx bswap_op0 = simplify_gen_unary (BSWAP, mode, op0, mode);
8590 : :
8591 : 40 : rtx and_op0_op1 = simplify_gen_binary (AND, mode, op0, op1);
8592 : 40 : rtx ior_op0_op1 = simplify_gen_binary (IOR, mode, op0, op1);
8593 : 40 : rtx xor_op0_op1 = simplify_gen_binary (XOR, mode, op0, op1);
8594 : :
8595 : 40 : rtx and_op0_6 = simplify_gen_binary (AND, mode, op0, six);
8596 : 40 : rtx and_op1_6 = simplify_gen_binary (AND, mode, op1, six);
8597 : :
8598 : : /* Test some binary identities. */
8599 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, op0, const0_rtx));
8600 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, const0_rtx, op0));
8601 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MINUS, mode, op0, const0_rtx));
8602 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, op0, const1_rtx));
8603 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, const1_rtx, op0));
8604 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (DIV, mode, op0, const1_rtx));
8605 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, constm1_rtx));
8606 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, constm1_rtx, op0));
8607 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, const0_rtx));
8608 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, const0_rtx, op0));
8609 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, op0, const0_rtx));
8610 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, const0_rtx, op0));
8611 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFT, mode, op0, const0_rtx));
8612 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATE, mode, op0, const0_rtx));
8613 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFTRT, mode, op0, const0_rtx));
8614 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (LSHIFTRT, mode, op0, const0_rtx));
8615 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATERT, mode, op0, const0_rtx));
8616 : :
8617 : : /* Test some self-inverse operations. */
8618 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NEG, mode, neg_op0, mode));
8619 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NOT, mode, not_op0, mode));
8620 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (BSWAP, mode, bswap_op0, mode));
8621 : :
8622 : : /* Test some reflexive operations. */
8623 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, op0));
8624 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, op0));
8625 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMIN, mode, op0, op0));
8626 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMAX, mode, op0, op0));
8627 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMIN, mode, op0, op0));
8628 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMAX, mode, op0, op0));
8629 : :
8630 : 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (MINUS, mode, op0, op0));
8631 : 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (XOR, mode, op0, op0));
8632 : :
8633 : : /* Test simplify_distributive_operation. */
8634 : 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, xor_op0_op1, six),
8635 : : simplify_gen_binary (XOR, mode, and_op0_6, and_op1_6));
8636 : 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, ior_op0_op1, six),
8637 : : simplify_gen_binary (IOR, mode, and_op0_6, and_op1_6));
8638 : 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, and_op0_op1, six),
8639 : : simplify_gen_binary (AND, mode, and_op0_6, and_op1_6));
8640 : :
8641 : : /* Test useless extensions are eliminated. */
8642 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (TRUNCATE, mode, op0, mode));
8643 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (ZERO_EXTEND, mode, op0, mode));
8644 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (SIGN_EXTEND, mode, op0, mode));
8645 : 40 : ASSERT_RTX_EQ (op0, lowpart_subreg (mode, op0, mode));
8646 : 40 : }
8647 : :
8648 : : /* Verify some simplifications of integer extension/truncation.
8649 : : Machine mode BMODE is the guaranteed wider than SMODE. */
8650 : :
8651 : : static void
8652 : 24 : test_scalar_int_ext_ops (machine_mode bmode, machine_mode smode)
8653 : : {
8654 : 24 : rtx sreg = make_test_reg (smode);
8655 : :
8656 : : /* Check truncation of extension. */
8657 : 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8658 : : simplify_gen_unary (ZERO_EXTEND, bmode,
8659 : : sreg, smode),
8660 : : bmode),
8661 : : sreg);
8662 : 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8663 : : simplify_gen_unary (SIGN_EXTEND, bmode,
8664 : : sreg, smode),
8665 : : bmode),
8666 : : sreg);
8667 : 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8668 : : lowpart_subreg (bmode, sreg, smode),
8669 : : bmode),
8670 : : sreg);
8671 : 24 : }
8672 : :
8673 : : /* Verify more simplifications of integer extension/truncation.
8674 : : BMODE is wider than MMODE which is wider than SMODE. */
8675 : :
8676 : : static void
8677 : 16 : test_scalar_int_ext_ops2 (machine_mode bmode, machine_mode mmode,
8678 : : machine_mode smode)
8679 : : {
8680 : 16 : rtx breg = make_test_reg (bmode);
8681 : 16 : rtx mreg = make_test_reg (mmode);
8682 : 16 : rtx sreg = make_test_reg (smode);
8683 : :
8684 : : /* Check truncate of truncate. */
8685 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8686 : : simplify_gen_unary (TRUNCATE, mmode,
8687 : : breg, bmode),
8688 : : mmode),
8689 : : simplify_gen_unary (TRUNCATE, smode, breg, bmode));
8690 : :
8691 : : /* Check extension of extension. */
8692 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (ZERO_EXTEND, bmode,
8693 : : simplify_gen_unary (ZERO_EXTEND, mmode,
8694 : : sreg, smode),
8695 : : mmode),
8696 : : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
8697 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
8698 : : simplify_gen_unary (SIGN_EXTEND, mmode,
8699 : : sreg, smode),
8700 : : mmode),
8701 : : simplify_gen_unary (SIGN_EXTEND, bmode, sreg, smode));
8702 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
8703 : : simplify_gen_unary (ZERO_EXTEND, mmode,
8704 : : sreg, smode),
8705 : : mmode),
8706 : : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
8707 : :
8708 : : /* Check truncation of extension. */
8709 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8710 : : simplify_gen_unary (ZERO_EXTEND, bmode,
8711 : : mreg, mmode),
8712 : : bmode),
8713 : : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8714 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8715 : : simplify_gen_unary (SIGN_EXTEND, bmode,
8716 : : mreg, mmode),
8717 : : bmode),
8718 : : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8719 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8720 : : lowpart_subreg (bmode, mreg, mmode),
8721 : : bmode),
8722 : : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8723 : 16 : }
8724 : :
8725 : : /* Test comparisons of comparisons, with the inner comparisons being
8726 : : between values of mode MODE2 and producing results of mode MODE1,
8727 : : and with the outer comparisons producing results of mode MODE0. */
8728 : :
8729 : : static void
8730 : 4 : test_comparisons (machine_mode mode0, machine_mode mode1, machine_mode mode2)
8731 : : {
8732 : 4 : rtx reg0 = make_test_reg (mode2);
8733 : 4 : rtx reg1 = make_test_reg (mode2);
8734 : :
8735 : 4 : static const rtx_code codes[] = {
8736 : : EQ, NE, LT, LTU, LE, LEU, GE, GEU, GT, GTU
8737 : : };
8738 : 4 : constexpr auto num_codes = ARRAY_SIZE (codes);
8739 : 4 : rtx cmps[num_codes];
8740 : 4 : rtx vals[] = { constm1_rtx, const0_rtx, const1_rtx };
8741 : :
8742 : 44 : for (unsigned int i = 0; i < num_codes; ++i)
8743 : 40 : cmps[i] = gen_rtx_fmt_ee (codes[i], mode1, reg0, reg1);
8744 : :
8745 : 44 : for (auto code : codes)
8746 : 440 : for (unsigned int i0 = 0; i0 < num_codes; ++i0)
8747 : 4400 : for (unsigned int i1 = 0; i1 < num_codes; ++i1)
8748 : : {
8749 : 4000 : rtx cmp_res = simplify_relational_operation (code, mode0, mode1,
8750 : : cmps[i0], cmps[i1]);
8751 : 4000 : if (i0 >= 2 && i1 >= 2 && (i0 ^ i1) & 1)
8752 : 1280 : ASSERT_TRUE (cmp_res == NULL_RTX);
8753 : : else
8754 : : {
8755 : 2720 : ASSERT_TRUE (cmp_res != NULL_RTX
8756 : : && (CONSTANT_P (cmp_res)
8757 : : || (COMPARISON_P (cmp_res)
8758 : : && GET_MODE (cmp_res) == mode0
8759 : : && REG_P (XEXP (cmp_res, 0))
8760 : : && REG_P (XEXP (cmp_res, 1)))));
8761 : 10880 : for (rtx reg0_val : vals)
8762 : 32640 : for (rtx reg1_val : vals)
8763 : : {
8764 : 24480 : rtx val0 = simplify_const_relational_operation
8765 : 24480 : (codes[i0], mode1, reg0_val, reg1_val);
8766 : 24480 : rtx val1 = simplify_const_relational_operation
8767 : 24480 : (codes[i1], mode1, reg0_val, reg1_val);
8768 : 24480 : rtx val = simplify_const_relational_operation
8769 : 24480 : (code, mode0, val0, val1);
8770 : 24480 : rtx folded = cmp_res;
8771 : 24480 : if (COMPARISON_P (cmp_res))
8772 : 16704 : folded = simplify_const_relational_operation
8773 : 16704 : (GET_CODE (cmp_res), mode0,
8774 : 16704 : XEXP (cmp_res, 0) == reg0 ? reg0_val : reg1_val,
8775 : 16704 : XEXP (cmp_res, 1) == reg0 ? reg0_val : reg1_val);
8776 : 24480 : ASSERT_RTX_EQ (val, folded);
8777 : : }
8778 : : }
8779 : : }
8780 : 4 : }
8781 : :
8782 : :
8783 : : /* Verify some simplifications involving scalar expressions. */
8784 : :
8785 : : static void
8786 : 4 : test_scalar_ops ()
8787 : : {
8788 : 524 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
8789 : : {
8790 : 520 : machine_mode mode = (machine_mode) i;
8791 : 520 : if (SCALAR_INT_MODE_P (mode) && mode != BImode)
8792 : 40 : test_scalar_int_ops (mode);
8793 : : }
8794 : :
8795 : 4 : test_scalar_int_ext_ops (HImode, QImode);
8796 : 4 : test_scalar_int_ext_ops (SImode, QImode);
8797 : 4 : test_scalar_int_ext_ops (SImode, HImode);
8798 : 4 : test_scalar_int_ext_ops (DImode, QImode);
8799 : 4 : test_scalar_int_ext_ops (DImode, HImode);
8800 : 4 : test_scalar_int_ext_ops (DImode, SImode);
8801 : :
8802 : 4 : test_scalar_int_ext_ops2 (SImode, HImode, QImode);
8803 : 4 : test_scalar_int_ext_ops2 (DImode, HImode, QImode);
8804 : 4 : test_scalar_int_ext_ops2 (DImode, SImode, QImode);
8805 : 4 : test_scalar_int_ext_ops2 (DImode, SImode, HImode);
8806 : :
8807 : 4 : test_comparisons (QImode, HImode, SImode);
8808 : 4 : }
8809 : :
8810 : : /* Test vector simplifications involving VEC_DUPLICATE in which the
8811 : : operands and result have vector mode MODE. SCALAR_REG is a pseudo
8812 : : register that holds one element of MODE. */
8813 : :
8814 : : static void
8815 : 248 : test_vector_ops_duplicate (machine_mode mode, rtx scalar_reg)
8816 : : {
8817 : 248 : scalar_mode inner_mode = GET_MODE_INNER (mode);
8818 : 248 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
8819 : 496 : poly_uint64 nunits = GET_MODE_NUNITS (mode);
8820 : 248 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
8821 : : {
8822 : : /* Test some simple unary cases with VEC_DUPLICATE arguments. */
8823 : 128 : rtx not_scalar_reg = gen_rtx_NOT (inner_mode, scalar_reg);
8824 : 128 : rtx duplicate_not = gen_rtx_VEC_DUPLICATE (mode, not_scalar_reg);
8825 : 128 : ASSERT_RTX_EQ (duplicate,
8826 : : simplify_unary_operation (NOT, mode,
8827 : : duplicate_not, mode));
8828 : :
8829 : 128 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
8830 : 128 : rtx duplicate_neg = gen_rtx_VEC_DUPLICATE (mode, neg_scalar_reg);
8831 : 128 : ASSERT_RTX_EQ (duplicate,
8832 : : simplify_unary_operation (NEG, mode,
8833 : : duplicate_neg, mode));
8834 : :
8835 : : /* Test some simple binary cases with VEC_DUPLICATE arguments. */
8836 : 128 : ASSERT_RTX_EQ (duplicate,
8837 : : simplify_binary_operation (PLUS, mode, duplicate,
8838 : : CONST0_RTX (mode)));
8839 : :
8840 : 128 : ASSERT_RTX_EQ (duplicate,
8841 : : simplify_binary_operation (MINUS, mode, duplicate,
8842 : : CONST0_RTX (mode)));
8843 : :
8844 : 128 : ASSERT_RTX_PTR_EQ (CONST0_RTX (mode),
8845 : : simplify_binary_operation (MINUS, mode, duplicate,
8846 : : duplicate));
8847 : : }
8848 : :
8849 : : /* Test a scalar VEC_SELECT of a VEC_DUPLICATE. */
8850 : 248 : rtx zero_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
8851 : 248 : ASSERT_RTX_PTR_EQ (scalar_reg,
8852 : : simplify_binary_operation (VEC_SELECT, inner_mode,
8853 : : duplicate, zero_par));
8854 : :
8855 : 248 : unsigned HOST_WIDE_INT const_nunits;
8856 : 248 : if (nunits.is_constant (&const_nunits))
8857 : : {
8858 : : /* And again with the final element. */
8859 : 248 : rtx last_index = gen_int_mode (const_nunits - 1, word_mode);
8860 : 248 : rtx last_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, last_index));
8861 : 248 : ASSERT_RTX_PTR_EQ (scalar_reg,
8862 : : simplify_binary_operation (VEC_SELECT, inner_mode,
8863 : : duplicate, last_par));
8864 : :
8865 : : /* Test a scalar subreg of a VEC_MERGE of a VEC_DUPLICATE. */
8866 : : /* Skip this test for vectors of booleans, because offset is in bytes,
8867 : : while vec_merge indices are in elements (usually bits). */
8868 : 248 : if (GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL)
8869 : : {
8870 : 248 : rtx vector_reg = make_test_reg (mode);
8871 : 4748 : for (unsigned HOST_WIDE_INT i = 0; i < const_nunits; i++)
8872 : : {
8873 : 4512 : if (i >= HOST_BITS_PER_WIDE_INT)
8874 : : break;
8875 : 4500 : rtx mask = GEN_INT ((HOST_WIDE_INT_1U << i) | (i + 1));
8876 : 4500 : rtx vm = gen_rtx_VEC_MERGE (mode, duplicate, vector_reg, mask);
8877 : 9000 : poly_uint64 offset = i * GET_MODE_SIZE (inner_mode);
8878 : :
8879 : 4500 : ASSERT_RTX_EQ (scalar_reg,
8880 : : simplify_gen_subreg (inner_mode, vm,
8881 : : mode, offset));
8882 : : }
8883 : : }
8884 : : }
8885 : :
8886 : : /* Test a scalar subreg of a VEC_DUPLICATE. */
8887 : 248 : poly_uint64 offset = subreg_lowpart_offset (inner_mode, mode);
8888 : 248 : ASSERT_RTX_EQ (scalar_reg,
8889 : : simplify_gen_subreg (inner_mode, duplicate,
8890 : : mode, offset));
8891 : :
8892 : 248 : machine_mode narrower_mode;
8893 : 248 : if (maybe_ne (nunits, 2U)
8894 : 208 : && multiple_p (nunits, 2)
8895 : 444 : && mode_for_vector (inner_mode, 2).exists (&narrower_mode)
8896 : 444 : && VECTOR_MODE_P (narrower_mode))
8897 : : {
8898 : : /* Test VEC_DUPLICATE of a vector. */
8899 : 196 : rtx_vector_builder nbuilder (narrower_mode, 2, 1);
8900 : 196 : nbuilder.quick_push (const0_rtx);
8901 : 196 : nbuilder.quick_push (const1_rtx);
8902 : 196 : rtx_vector_builder builder (mode, 2, 1);
8903 : 196 : builder.quick_push (const0_rtx);
8904 : 196 : builder.quick_push (const1_rtx);
8905 : 196 : ASSERT_RTX_EQ (builder.build (),
8906 : : simplify_unary_operation (VEC_DUPLICATE, mode,
8907 : : nbuilder.build (),
8908 : : narrower_mode));
8909 : :
8910 : : /* Test VEC_SELECT of a vector. */
8911 : 196 : rtx vec_par
8912 : 196 : = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, const1_rtx, const0_rtx));
8913 : 196 : rtx narrower_duplicate
8914 : 196 : = gen_rtx_VEC_DUPLICATE (narrower_mode, scalar_reg);
8915 : 196 : ASSERT_RTX_EQ (narrower_duplicate,
8916 : : simplify_binary_operation (VEC_SELECT, narrower_mode,
8917 : : duplicate, vec_par));
8918 : :
8919 : : /* Test a vector subreg of a VEC_DUPLICATE. */
8920 : 196 : poly_uint64 offset = subreg_lowpart_offset (narrower_mode, mode);
8921 : 196 : ASSERT_RTX_EQ (narrower_duplicate,
8922 : : simplify_gen_subreg (narrower_mode, duplicate,
8923 : : mode, offset));
8924 : 196 : }
8925 : 248 : }
8926 : :
8927 : : /* Test vector simplifications involving VEC_SERIES in which the
8928 : : operands and result have vector mode MODE. SCALAR_REG is a pseudo
8929 : : register that holds one element of MODE. */
8930 : :
8931 : : static void
8932 : 96 : test_vector_ops_series (machine_mode mode, rtx scalar_reg)
8933 : : {
8934 : : /* Test unary cases with VEC_SERIES arguments. */
8935 : 96 : scalar_mode inner_mode = GET_MODE_INNER (mode);
8936 : 96 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
8937 : 96 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
8938 : 96 : rtx series_0_r = gen_rtx_VEC_SERIES (mode, const0_rtx, scalar_reg);
8939 : 96 : rtx series_0_nr = gen_rtx_VEC_SERIES (mode, const0_rtx, neg_scalar_reg);
8940 : 96 : rtx series_nr_1 = gen_rtx_VEC_SERIES (mode, neg_scalar_reg, const1_rtx);
8941 : 96 : rtx series_r_m1 = gen_rtx_VEC_SERIES (mode, scalar_reg, constm1_rtx);
8942 : 96 : rtx series_r_r = gen_rtx_VEC_SERIES (mode, scalar_reg, scalar_reg);
8943 : 96 : rtx series_nr_nr = gen_rtx_VEC_SERIES (mode, neg_scalar_reg,
8944 : : neg_scalar_reg);
8945 : 96 : ASSERT_RTX_EQ (series_0_r,
8946 : : simplify_unary_operation (NEG, mode, series_0_nr, mode));
8947 : 96 : ASSERT_RTX_EQ (series_r_m1,
8948 : : simplify_unary_operation (NEG, mode, series_nr_1, mode));
8949 : 96 : ASSERT_RTX_EQ (series_r_r,
8950 : : simplify_unary_operation (NEG, mode, series_nr_nr, mode));
8951 : :
8952 : : /* Test that a VEC_SERIES with a zero step is simplified away. */
8953 : 96 : ASSERT_RTX_EQ (duplicate,
8954 : : simplify_binary_operation (VEC_SERIES, mode,
8955 : : scalar_reg, const0_rtx));
8956 : :
8957 : : /* Test PLUS and MINUS with VEC_SERIES. */
8958 : 96 : rtx series_0_1 = gen_const_vec_series (mode, const0_rtx, const1_rtx);
8959 : 96 : rtx series_0_m1 = gen_const_vec_series (mode, const0_rtx, constm1_rtx);
8960 : 96 : rtx series_r_1 = gen_rtx_VEC_SERIES (mode, scalar_reg, const1_rtx);
8961 : 96 : ASSERT_RTX_EQ (series_r_r,
8962 : : simplify_binary_operation (PLUS, mode, series_0_r,
8963 : : duplicate));
8964 : 96 : ASSERT_RTX_EQ (series_r_1,
8965 : : simplify_binary_operation (PLUS, mode, duplicate,
8966 : : series_0_1));
8967 : 96 : ASSERT_RTX_EQ (series_r_m1,
8968 : : simplify_binary_operation (PLUS, mode, duplicate,
8969 : : series_0_m1));
8970 : 96 : ASSERT_RTX_EQ (series_0_r,
8971 : : simplify_binary_operation (MINUS, mode, series_r_r,
8972 : : duplicate));
8973 : 96 : ASSERT_RTX_EQ (series_r_m1,
8974 : : simplify_binary_operation (MINUS, mode, duplicate,
8975 : : series_0_1));
8976 : 96 : ASSERT_RTX_EQ (series_r_1,
8977 : : simplify_binary_operation (MINUS, mode, duplicate,
8978 : : series_0_m1));
8979 : 96 : ASSERT_RTX_EQ (series_0_m1,
8980 : : simplify_binary_operation (VEC_SERIES, mode, const0_rtx,
8981 : : constm1_rtx));
8982 : :
8983 : : /* Test NEG on constant vector series. */
8984 : 96 : ASSERT_RTX_EQ (series_0_m1,
8985 : : simplify_unary_operation (NEG, mode, series_0_1, mode));
8986 : 96 : ASSERT_RTX_EQ (series_0_1,
8987 : : simplify_unary_operation (NEG, mode, series_0_m1, mode));
8988 : :
8989 : : /* Test PLUS and MINUS on constant vector series. */
8990 : 96 : rtx scalar2 = gen_int_mode (2, inner_mode);
8991 : 96 : rtx scalar3 = gen_int_mode (3, inner_mode);
8992 : 96 : rtx series_1_1 = gen_const_vec_series (mode, const1_rtx, const1_rtx);
8993 : 96 : rtx series_0_2 = gen_const_vec_series (mode, const0_rtx, scalar2);
8994 : 96 : rtx series_1_3 = gen_const_vec_series (mode, const1_rtx, scalar3);
8995 : 96 : ASSERT_RTX_EQ (series_1_1,
8996 : : simplify_binary_operation (PLUS, mode, series_0_1,
8997 : : CONST1_RTX (mode)));
8998 : 96 : ASSERT_RTX_EQ (series_0_m1,
8999 : : simplify_binary_operation (PLUS, mode, CONST0_RTX (mode),
9000 : : series_0_m1));
9001 : 96 : ASSERT_RTX_EQ (series_1_3,
9002 : : simplify_binary_operation (PLUS, mode, series_1_1,
9003 : : series_0_2));
9004 : 96 : ASSERT_RTX_EQ (series_0_1,
9005 : : simplify_binary_operation (MINUS, mode, series_1_1,
9006 : : CONST1_RTX (mode)));
9007 : 96 : ASSERT_RTX_EQ (series_1_1,
9008 : : simplify_binary_operation (MINUS, mode, CONST1_RTX (mode),
9009 : : series_0_m1));
9010 : 96 : ASSERT_RTX_EQ (series_1_1,
9011 : : simplify_binary_operation (MINUS, mode, series_1_3,
9012 : : series_0_2));
9013 : :
9014 : : /* Test MULT between constant vectors. */
9015 : 96 : rtx vec2 = gen_const_vec_duplicate (mode, scalar2);
9016 : 96 : rtx vec3 = gen_const_vec_duplicate (mode, scalar3);
9017 : 96 : rtx scalar9 = gen_int_mode (9, inner_mode);
9018 : 96 : rtx series_3_9 = gen_const_vec_series (mode, scalar3, scalar9);
9019 : 96 : ASSERT_RTX_EQ (series_0_2,
9020 : : simplify_binary_operation (MULT, mode, series_0_1, vec2));
9021 : 96 : ASSERT_RTX_EQ (series_3_9,
9022 : : simplify_binary_operation (MULT, mode, vec3, series_1_3));
9023 : 96 : if (!GET_MODE_NUNITS (mode).is_constant ())
9024 : : ASSERT_FALSE (simplify_binary_operation (MULT, mode, series_0_1,
9025 : : series_0_1));
9026 : :
9027 : : /* Test ASHIFT between constant vectors. */
9028 : 96 : ASSERT_RTX_EQ (series_0_2,
9029 : : simplify_binary_operation (ASHIFT, mode, series_0_1,
9030 : : CONST1_RTX (mode)));
9031 : 96 : if (!GET_MODE_NUNITS (mode).is_constant ())
9032 : : ASSERT_FALSE (simplify_binary_operation (ASHIFT, mode, CONST1_RTX (mode),
9033 : : series_0_1));
9034 : 96 : }
9035 : :
9036 : : static rtx
9037 : 3472 : simplify_merge_mask (rtx x, rtx mask, int op)
9038 : : {
9039 : 0 : return simplify_context ().simplify_merge_mask (x, mask, op);
9040 : : }
9041 : :
9042 : : /* Verify simplify_merge_mask works correctly. */
9043 : :
9044 : : static void
9045 : 248 : test_vec_merge (machine_mode mode)
9046 : : {
9047 : 248 : rtx op0 = make_test_reg (mode);
9048 : 248 : rtx op1 = make_test_reg (mode);
9049 : 248 : rtx op2 = make_test_reg (mode);
9050 : 248 : rtx op3 = make_test_reg (mode);
9051 : 248 : rtx op4 = make_test_reg (mode);
9052 : 248 : rtx op5 = make_test_reg (mode);
9053 : 248 : rtx mask1 = make_test_reg (SImode);
9054 : 248 : rtx mask2 = make_test_reg (SImode);
9055 : 248 : rtx vm1 = gen_rtx_VEC_MERGE (mode, op0, op1, mask1);
9056 : 248 : rtx vm2 = gen_rtx_VEC_MERGE (mode, op2, op3, mask1);
9057 : 248 : rtx vm3 = gen_rtx_VEC_MERGE (mode, op4, op5, mask1);
9058 : :
9059 : : /* Simple vec_merge. */
9060 : 248 : ASSERT_EQ (op0, simplify_merge_mask (vm1, mask1, 0));
9061 : 248 : ASSERT_EQ (op1, simplify_merge_mask (vm1, mask1, 1));
9062 : 248 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 0));
9063 : 248 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 1));
9064 : :
9065 : : /* Nested vec_merge.
9066 : : It's tempting to make this simplify right down to opN, but we don't
9067 : : because all the simplify_* functions assume that the operands have
9068 : : already been simplified. */
9069 : 248 : rtx nvm = gen_rtx_VEC_MERGE (mode, vm1, vm2, mask1);
9070 : 248 : ASSERT_EQ (vm1, simplify_merge_mask (nvm, mask1, 0));
9071 : 248 : ASSERT_EQ (vm2, simplify_merge_mask (nvm, mask1, 1));
9072 : :
9073 : : /* Intermediate unary op. */
9074 : 248 : rtx unop = gen_rtx_NOT (mode, vm1);
9075 : 248 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op0),
9076 : : simplify_merge_mask (unop, mask1, 0));
9077 : 248 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op1),
9078 : : simplify_merge_mask (unop, mask1, 1));
9079 : :
9080 : : /* Intermediate binary op. */
9081 : 248 : rtx binop = gen_rtx_PLUS (mode, vm1, vm2);
9082 : 248 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op0, op2),
9083 : : simplify_merge_mask (binop, mask1, 0));
9084 : 248 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op1, op3),
9085 : : simplify_merge_mask (binop, mask1, 1));
9086 : :
9087 : : /* Intermediate ternary op. */
9088 : 248 : rtx tenop = gen_rtx_FMA (mode, vm1, vm2, vm3);
9089 : 248 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op0, op2, op4),
9090 : : simplify_merge_mask (tenop, mask1, 0));
9091 : 248 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op1, op3, op5),
9092 : : simplify_merge_mask (tenop, mask1, 1));
9093 : :
9094 : : /* Side effects. */
9095 : 248 : rtx badop0 = gen_rtx_PRE_INC (mode, op0);
9096 : 248 : rtx badvm = gen_rtx_VEC_MERGE (mode, badop0, op1, mask1);
9097 : 248 : ASSERT_EQ (badop0, simplify_merge_mask (badvm, mask1, 0));
9098 : 248 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (badvm, mask1, 1));
9099 : :
9100 : : /* Called indirectly. */
9101 : 248 : ASSERT_RTX_EQ (gen_rtx_VEC_MERGE (mode, op0, op3, mask1),
9102 : : simplify_rtx (nvm));
9103 : 248 : }
9104 : :
9105 : : /* Test that vector rotate formation works at RTL level. Try various
9106 : : combinations of (REG << C) [|,^,+] (REG >> (<bitwidth> - C)). */
9107 : :
9108 : : static void
9109 : 96 : test_vector_rotate (rtx reg)
9110 : : {
9111 : 96 : machine_mode mode = GET_MODE (reg);
9112 : 96 : unsigned bitwidth = GET_MODE_UNIT_SIZE (mode) * BITS_PER_UNIT;
9113 : 96 : rtx plus_rtx = gen_rtx_PLUS (mode, reg, reg);
9114 : 96 : rtx lshftrt_amnt = GEN_INT (bitwidth - 1);
9115 : 96 : lshftrt_amnt = gen_const_vec_duplicate (mode, lshftrt_amnt);
9116 : 96 : rtx lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
9117 : 96 : rtx rotate_rtx = gen_rtx_ROTATE (mode, reg, CONST1_RTX (mode));
9118 : : /* Test explicitly the case where ASHIFT (x, 1) is a PLUS (x, x). */
9119 : 96 : ASSERT_RTX_EQ (rotate_rtx,
9120 : : simplify_rtx (gen_rtx_IOR (mode, plus_rtx, lshiftrt_rtx)));
9121 : 96 : ASSERT_RTX_EQ (rotate_rtx,
9122 : : simplify_rtx (gen_rtx_XOR (mode, plus_rtx, lshiftrt_rtx)));
9123 : 96 : ASSERT_RTX_EQ (rotate_rtx,
9124 : : simplify_rtx (gen_rtx_PLUS (mode, plus_rtx, lshiftrt_rtx)));
9125 : :
9126 : : /* Don't go through every possible rotate amount to save execution time.
9127 : : Multiple of BITS_PER_UNIT amounts could conceivably be simplified to
9128 : : other bswap operations sometimes. Go through just the odd amounts. */
9129 : 1440 : for (unsigned i = 3; i < bitwidth - 2; i += 2)
9130 : : {
9131 : 1344 : rtx rot_amnt = gen_const_vec_duplicate (mode, GEN_INT (i));
9132 : 1344 : rtx ashift_rtx = gen_rtx_ASHIFT (mode, reg, rot_amnt);
9133 : 1344 : lshftrt_amnt = gen_const_vec_duplicate (mode, GEN_INT (bitwidth - i));
9134 : 1344 : lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
9135 : 1344 : rotate_rtx = gen_rtx_ROTATE (mode, reg, rot_amnt);
9136 : 1344 : ASSERT_RTX_EQ (rotate_rtx,
9137 : : simplify_rtx (gen_rtx_IOR (mode, ashift_rtx, lshiftrt_rtx)));
9138 : 1344 : ASSERT_RTX_EQ (rotate_rtx,
9139 : : simplify_rtx (gen_rtx_XOR (mode, ashift_rtx, lshiftrt_rtx)));
9140 : 1344 : ASSERT_RTX_EQ (rotate_rtx,
9141 : : simplify_rtx (gen_rtx_PLUS (mode, ashift_rtx, lshiftrt_rtx)));
9142 : : }
9143 : 96 : }
9144 : :
9145 : : /* Test subregs of integer vector constant X, trying elements in
9146 : : the range [ELT_BIAS, ELT_BIAS + constant_lower_bound (NELTS)),
9147 : : where NELTS is the number of elements in X. Subregs involving
9148 : : elements [ELT_BIAS, ELT_BIAS + FIRST_VALID) are expected to fail. */
9149 : :
9150 : : static void
9151 : 288 : test_vector_subregs_modes (rtx x, poly_uint64 elt_bias = 0,
9152 : : unsigned int first_valid = 0)
9153 : : {
9154 : 288 : machine_mode inner_mode = GET_MODE (x);
9155 : 288 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9156 : :
9157 : 37728 : for (unsigned int modei = 0; modei < NUM_MACHINE_MODES; ++modei)
9158 : : {
9159 : 37440 : machine_mode outer_mode = (machine_mode) modei;
9160 : 37440 : if (!VECTOR_MODE_P (outer_mode))
9161 : 19584 : continue;
9162 : :
9163 : 17856 : unsigned int outer_nunits;
9164 : 17856 : if (GET_MODE_INNER (outer_mode) == int_mode
9165 : 2064 : && GET_MODE_NUNITS (outer_mode).is_constant (&outer_nunits)
9166 : 23160 : && multiple_p (GET_MODE_NUNITS (inner_mode), outer_nunits))
9167 : : {
9168 : : /* Test subregs in which the outer mode is a smaller,
9169 : : constant-sized vector of the same element type. */
9170 : 1176 : unsigned int limit
9171 : 1176 : = constant_lower_bound (GET_MODE_NUNITS (inner_mode));
9172 : 9636 : for (unsigned int elt = 0; elt < limit; elt += outer_nunits)
9173 : : {
9174 : 8460 : rtx expected = NULL_RTX;
9175 : 8460 : if (elt >= first_valid)
9176 : : {
9177 : 8460 : rtx_vector_builder builder (outer_mode, outer_nunits, 1);
9178 : 46668 : for (unsigned int i = 0; i < outer_nunits; ++i)
9179 : 38208 : builder.quick_push (CONST_VECTOR_ELT (x, elt + i));
9180 : 8460 : expected = builder.build ();
9181 : 8460 : }
9182 : 16920 : poly_uint64 byte = (elt_bias + elt) * GET_MODE_SIZE (int_mode);
9183 : 8460 : ASSERT_RTX_EQ (expected,
9184 : : simplify_subreg (outer_mode, x,
9185 : : inner_mode, byte));
9186 : : }
9187 : : }
9188 : 33360 : else if (known_eq (GET_MODE_SIZE (outer_mode),
9189 : : GET_MODE_SIZE (inner_mode))
9190 : 2100 : && known_eq (elt_bias, 0U)
9191 : 2100 : && (GET_MODE_CLASS (outer_mode) != MODE_VECTOR_BOOL
9192 : 0 : || known_eq (GET_MODE_BITSIZE (outer_mode),
9193 : : GET_MODE_NUNITS (outer_mode)))
9194 : 2100 : && (!FLOAT_MODE_P (outer_mode)
9195 : 18324 : || (FLOAT_MODE_FORMAT (outer_mode)->ieee_bits
9196 : 1164 : == GET_MODE_UNIT_PRECISION (outer_mode)))
9197 : 16680 : && (GET_MODE_SIZE (inner_mode).is_constant ()
9198 : : || !CONST_VECTOR_STEPPED_P (x)))
9199 : : {
9200 : : /* Try converting to OUTER_MODE and back. */
9201 : 1848 : rtx outer_x = simplify_subreg (outer_mode, x, inner_mode, 0);
9202 : 1848 : ASSERT_TRUE (outer_x != NULL_RTX);
9203 : 1848 : ASSERT_RTX_EQ (x, simplify_subreg (inner_mode, outer_x,
9204 : : outer_mode, 0));
9205 : : }
9206 : : }
9207 : :
9208 : 288 : if (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
9209 : : {
9210 : : /* Test each byte in the element range. */
9211 : 288 : unsigned int limit
9212 : 288 : = constant_lower_bound (GET_MODE_SIZE (inner_mode));
9213 : 17688 : for (unsigned int i = 0; i < limit; ++i)
9214 : : {
9215 : 17400 : unsigned int elt = i / GET_MODE_SIZE (int_mode);
9216 : 17400 : rtx expected = NULL_RTX;
9217 : 17400 : if (elt >= first_valid)
9218 : : {
9219 : 17400 : unsigned int byte_shift = i % GET_MODE_SIZE (int_mode);
9220 : 17400 : if (BYTES_BIG_ENDIAN)
9221 : : byte_shift = GET_MODE_SIZE (int_mode) - byte_shift - 1;
9222 : 17400 : rtx_mode_t vec_elt (CONST_VECTOR_ELT (x, elt), int_mode);
9223 : 17400 : wide_int shifted_elt
9224 : 17400 : = wi::lrshift (vec_elt, byte_shift * BITS_PER_UNIT);
9225 : 17400 : expected = immed_wide_int_const (shifted_elt, QImode);
9226 : 17400 : }
9227 : 34800 : poly_uint64 byte = elt_bias * GET_MODE_SIZE (int_mode) + i;
9228 : 17400 : ASSERT_RTX_EQ (expected,
9229 : : simplify_subreg (QImode, x, inner_mode, byte));
9230 : : }
9231 : : }
9232 : 288 : }
9233 : :
9234 : : /* Test constant subregs of integer vector mode INNER_MODE, using 1
9235 : : element per pattern. */
9236 : :
9237 : : static void
9238 : 96 : test_vector_subregs_repeating (machine_mode inner_mode)
9239 : : {
9240 : 192 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
9241 : 96 : unsigned int min_nunits = constant_lower_bound (nunits);
9242 : 96 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9243 : 96 : unsigned int count = gcd (min_nunits, 8);
9244 : :
9245 : 96 : rtx_vector_builder builder (inner_mode, count, 1);
9246 : 720 : for (unsigned int i = 0; i < count; ++i)
9247 : 624 : builder.quick_push (gen_int_mode (8 - i, int_mode));
9248 : 96 : rtx x = builder.build ();
9249 : :
9250 : 96 : test_vector_subregs_modes (x);
9251 : 96 : if (!nunits.is_constant ())
9252 : : test_vector_subregs_modes (x, nunits - min_nunits);
9253 : 96 : }
9254 : :
9255 : : /* Test constant subregs of integer vector mode INNER_MODE, using 2
9256 : : elements per pattern. */
9257 : :
9258 : : static void
9259 : 96 : test_vector_subregs_fore_back (machine_mode inner_mode)
9260 : : {
9261 : 192 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
9262 : 96 : unsigned int min_nunits = constant_lower_bound (nunits);
9263 : 96 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9264 : 96 : unsigned int count = gcd (min_nunits, 4);
9265 : :
9266 : 96 : rtx_vector_builder builder (inner_mode, count, 2);
9267 : 464 : for (unsigned int i = 0; i < count; ++i)
9268 : 368 : builder.quick_push (gen_int_mode (i, int_mode));
9269 : 464 : for (unsigned int i = 0; i < count; ++i)
9270 : 368 : builder.quick_push (gen_int_mode (-1 - (int) i, int_mode));
9271 : 96 : rtx x = builder.build ();
9272 : :
9273 : 96 : test_vector_subregs_modes (x);
9274 : 96 : if (!nunits.is_constant ())
9275 : : test_vector_subregs_modes (x, nunits - min_nunits, count);
9276 : 96 : }
9277 : :
9278 : : /* Test constant subregs of integer vector mode INNER_MODE, using 3
9279 : : elements per pattern. */
9280 : :
9281 : : static void
9282 : 96 : test_vector_subregs_stepped (machine_mode inner_mode)
9283 : : {
9284 : : /* Build { 0, 1, 2, 3, ... }. */
9285 : 96 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9286 : 96 : rtx_vector_builder builder (inner_mode, 1, 3);
9287 : 384 : for (unsigned int i = 0; i < 3; ++i)
9288 : 288 : builder.quick_push (gen_int_mode (i, int_mode));
9289 : 96 : rtx x = builder.build ();
9290 : :
9291 : 96 : test_vector_subregs_modes (x);
9292 : 96 : }
9293 : :
9294 : : /* Test constant subregs of integer vector mode INNER_MODE. */
9295 : :
9296 : : static void
9297 : 96 : test_vector_subregs (machine_mode inner_mode)
9298 : : {
9299 : 96 : test_vector_subregs_repeating (inner_mode);
9300 : 96 : test_vector_subregs_fore_back (inner_mode);
9301 : 96 : test_vector_subregs_stepped (inner_mode);
9302 : 96 : }
9303 : :
9304 : : /* Verify some simplifications involving vectors. */
9305 : :
9306 : : static void
9307 : 4 : test_vector_ops ()
9308 : : {
9309 : 524 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
9310 : : {
9311 : 520 : machine_mode mode = (machine_mode) i;
9312 : 520 : if (VECTOR_MODE_P (mode))
9313 : : {
9314 : 496 : rtx scalar_reg = make_test_reg (GET_MODE_INNER (mode));
9315 : 248 : test_vector_ops_duplicate (mode, scalar_reg);
9316 : 248 : rtx vector_reg = make_test_reg (mode);
9317 : 248 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9318 : 376 : && maybe_gt (GET_MODE_NUNITS (mode), 2))
9319 : : {
9320 : 96 : test_vector_ops_series (mode, scalar_reg);
9321 : 96 : test_vector_subregs (mode);
9322 : 96 : test_vector_rotate (vector_reg);
9323 : : }
9324 : 248 : test_vec_merge (mode);
9325 : : }
9326 : : }
9327 : 4 : }
9328 : :
9329 : : template<unsigned int N>
9330 : : struct simplify_const_poly_int_tests
9331 : : {
9332 : : static void run ();
9333 : : };
9334 : :
9335 : : template<>
9336 : : struct simplify_const_poly_int_tests<1>
9337 : : {
9338 : : static void run () {}
9339 : : };
9340 : :
9341 : : /* Test various CONST_POLY_INT properties. */
9342 : :
9343 : : template<unsigned int N>
9344 : : void
9345 : : simplify_const_poly_int_tests<N>::run ()
9346 : : {
9347 : : using poly_int64 = poly_int<N, HOST_WIDE_INT>;
9348 : : rtx x1 = gen_int_mode (poly_int64 (1, 1), QImode);
9349 : : rtx x2 = gen_int_mode (poly_int64 (-80, 127), QImode);
9350 : : rtx x3 = gen_int_mode (poly_int64 (-79, -128), QImode);
9351 : : rtx x4 = gen_int_mode (poly_int64 (5, 4), QImode);
9352 : : rtx x5 = gen_int_mode (poly_int64 (30, 24), QImode);
9353 : : rtx x6 = gen_int_mode (poly_int64 (20, 16), QImode);
9354 : : rtx x7 = gen_int_mode (poly_int64 (7, 4), QImode);
9355 : : rtx x8 = gen_int_mode (poly_int64 (30, 24), HImode);
9356 : : rtx x9 = gen_int_mode (poly_int64 (-30, -24), HImode);
9357 : : rtx x10 = gen_int_mode (poly_int64 (-31, -24), HImode);
9358 : : rtx two = GEN_INT (2);
9359 : : rtx six = GEN_INT (6);
9360 : : poly_uint64 offset = subreg_lowpart_offset (QImode, HImode);
9361 : :
9362 : : /* These tests only try limited operation combinations. Fuller arithmetic
9363 : : testing is done directly on poly_ints. */
9364 : : ASSERT_EQ (simplify_unary_operation (NEG, HImode, x8, HImode), x9);
9365 : : ASSERT_EQ (simplify_unary_operation (NOT, HImode, x8, HImode), x10);
9366 : : ASSERT_EQ (simplify_unary_operation (TRUNCATE, QImode, x8, HImode), x5);
9367 : : ASSERT_EQ (simplify_binary_operation (PLUS, QImode, x1, x2), x3);
9368 : : ASSERT_EQ (simplify_binary_operation (MINUS, QImode, x3, x1), x2);
9369 : : ASSERT_EQ (simplify_binary_operation (MULT, QImode, x4, six), x5);
9370 : : ASSERT_EQ (simplify_binary_operation (MULT, QImode, six, x4), x5);
9371 : : ASSERT_EQ (simplify_binary_operation (ASHIFT, QImode, x4, two), x6);
9372 : : ASSERT_EQ (simplify_binary_operation (IOR, QImode, x4, two), x7);
9373 : : ASSERT_EQ (simplify_subreg (HImode, x5, QImode, 0), x8);
9374 : : ASSERT_EQ (simplify_subreg (QImode, x8, HImode, offset), x5);
9375 : : }
9376 : :
9377 : : /* Run all of the selftests within this file. */
9378 : :
9379 : : void
9380 : 4 : simplify_rtx_cc_tests ()
9381 : : {
9382 : 4 : test_scalar_ops ();
9383 : 4 : test_vector_ops ();
9384 : 4 : simplify_const_poly_int_tests<NUM_POLY_INT_COEFFS>::run ();
9385 : 4 : }
9386 : :
9387 : : } // namespace selftest
9388 : :
9389 : : #endif /* CHECKING_P */
|