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 : 9142761 : neg_poly_int_rtx (machine_mode mode, const_rtx i)
56 : : {
57 : 9142761 : 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 : 6391910 : mode_signbit_p (machine_mode mode, const_rtx x)
65 : : {
66 : 6391910 : unsigned HOST_WIDE_INT val;
67 : 6391910 : unsigned int width;
68 : 6391910 : scalar_int_mode int_mode;
69 : :
70 : 6391910 : if (!is_int_mode (mode, &int_mode))
71 : : return false;
72 : :
73 : 6391902 : width = GET_MODE_PRECISION (int_mode);
74 : 6391902 : if (width == 0)
75 : : return false;
76 : :
77 : 6391902 : if (width <= HOST_BITS_PER_WIDE_INT
78 : 6390358 : && CONST_INT_P (x))
79 : 6249766 : val = INTVAL (x);
80 : : #if TARGET_SUPPORTS_WIDE_INT
81 : 142136 : 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 : 6249766 : if (width < HOST_BITS_PER_WIDE_INT)
109 : 5641398 : val &= (HOST_WIDE_INT_1U << width) - 1;
110 : 6250180 : 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 : 3758441 : val_signbit_p (machine_mode mode, unsigned HOST_WIDE_INT val)
119 : : {
120 : 3758441 : unsigned int width;
121 : 3758441 : scalar_int_mode int_mode;
122 : :
123 : 3758441 : if (!is_int_mode (mode, &int_mode))
124 : : return false;
125 : :
126 : 3758405 : width = GET_MODE_PRECISION (int_mode);
127 : 3758405 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
128 : : return false;
129 : :
130 : 3753751 : val &= GET_MODE_MASK (int_mode);
131 : 3753751 : 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 : 2734644 : val_signbit_known_set_p (machine_mode mode, unsigned HOST_WIDE_INT val)
138 : : {
139 : 2734644 : unsigned int width;
140 : :
141 : 2734644 : scalar_int_mode int_mode;
142 : 2734644 : if (!is_int_mode (mode, &int_mode))
143 : : return false;
144 : :
145 : 2701371 : width = GET_MODE_PRECISION (int_mode);
146 : 2701371 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
147 : : return false;
148 : :
149 : 2701371 : val &= HOST_WIDE_INT_1U << (width - 1);
150 : 2701371 : 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 : 7668230 : val_signbit_known_clear_p (machine_mode mode, unsigned HOST_WIDE_INT val)
157 : : {
158 : 7668230 : unsigned int width;
159 : :
160 : 7668230 : scalar_int_mode int_mode;
161 : 7668230 : if (!is_int_mode (mode, &int_mode))
162 : : return false;
163 : :
164 : 7342539 : width = GET_MODE_PRECISION (int_mode);
165 : 7342539 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
166 : : return false;
167 : :
168 : 7230918 : val &= HOST_WIDE_INT_1U << (width - 1);
169 : 7230918 : 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 : 117089236 : simplify_context::simplify_gen_binary (rtx_code code, machine_mode mode,
177 : : rtx op0, rtx op1)
178 : : {
179 : 117089236 : rtx tem;
180 : :
181 : : /* If this simplifies, do it. */
182 : 117089236 : tem = simplify_binary_operation (code, mode, op0, op1);
183 : 117089236 : if (tem)
184 : : return tem;
185 : :
186 : : /* Put complex operands first and constants second if commutative. */
187 : 73715525 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
188 : 73715525 : && swap_commutative_operands_p (op0, op1))
189 : : std::swap (op0, op1);
190 : :
191 : 73715525 : 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 : 2789719311 : avoid_constant_pool_reference (rtx x)
198 : : {
199 : 2789719311 : rtx c, tmp, addr;
200 : 2789719311 : machine_mode cmode;
201 : 2789719311 : poly_int64 offset = 0;
202 : :
203 : 2789719311 : switch (GET_CODE (x))
204 : : {
205 : 259228311 : case MEM:
206 : 259228311 : break;
207 : :
208 : 910179 : case FLOAT_EXTEND:
209 : : /* Handle float extensions of constant pool references. */
210 : 910179 : tmp = XEXP (x, 0);
211 : 910179 : c = avoid_constant_pool_reference (tmp);
212 : 910179 : if (c != tmp && CONST_DOUBLE_AS_FLOAT_P (c))
213 : 123051 : return const_double_from_real_value (*CONST_DOUBLE_REAL_VALUE (c),
214 : 123051 : GET_MODE (x));
215 : : return x;
216 : :
217 : : default:
218 : : return x;
219 : : }
220 : :
221 : 259228311 : if (GET_MODE (x) == BLKmode)
222 : : return x;
223 : :
224 : 257054108 : addr = XEXP (x, 0);
225 : :
226 : : /* Call target hook to avoid the effects of -fpic etc.... */
227 : 257054108 : addr = targetm.delegitimize_address (addr);
228 : :
229 : : /* Split the address into a base and integer offset. */
230 : 257054108 : addr = strip_offset (addr, &offset);
231 : :
232 : 257054108 : 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 : 257054108 : if (GET_CODE (addr) == SYMBOL_REF
238 : 257054108 : && CONSTANT_POOL_ADDRESS_P (addr))
239 : : {
240 : 5422560 : c = get_pool_constant (addr);
241 : 5422560 : 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 : 5422560 : if (known_eq (offset, 0) && cmode == GET_MODE (x))
247 : : return c;
248 : 19908 : else if (known_in_range_p (offset, 0, GET_MODE_SIZE (cmode)))
249 : : {
250 : 9954 : rtx tem = simplify_subreg (GET_MODE (x), c, cmode, offset);
251 : 9954 : 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 : 3517462043 : 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 : 3517462043 : if (MEM_P (x)
269 : 62059401 : && MEM_EXPR (x)
270 : 3555552404 : && MEM_OFFSET_KNOWN_P (x))
271 : : {
272 : 35165141 : tree decl = MEM_EXPR (x);
273 : 35165141 : machine_mode mode = GET_MODE (x);
274 : 35165141 : poly_int64 offset = 0;
275 : :
276 : 35165141 : switch (TREE_CODE (decl))
277 : : {
278 : : default:
279 : : decl = NULL;
280 : : break;
281 : :
282 : : case VAR_DECL:
283 : : break;
284 : :
285 : 10104226 : case ARRAY_REF:
286 : 10104226 : case ARRAY_RANGE_REF:
287 : 10104226 : case COMPONENT_REF:
288 : 10104226 : case BIT_FIELD_REF:
289 : 10104226 : case REALPART_EXPR:
290 : 10104226 : case IMAGPART_EXPR:
291 : 10104226 : case VIEW_CONVERT_EXPR:
292 : 10104226 : {
293 : 10104226 : poly_int64 bitsize, bitpos, bytepos, toffset_val = 0;
294 : 10104226 : tree toffset;
295 : 10104226 : int unsignedp, reversep, volatilep = 0;
296 : :
297 : 10104226 : decl
298 : 10104226 : = get_inner_reference (decl, &bitsize, &bitpos, &toffset, &mode,
299 : : &unsignedp, &reversep, &volatilep);
300 : 20208452 : if (maybe_ne (bitsize, GET_MODE_BITSIZE (mode))
301 : 10387440 : || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
302 : 19752717 : || (toffset && !poly_int_tree_p (toffset, &toffset_val)))
303 : : decl = NULL;
304 : : else
305 : 9365277 : offset += bytepos + toffset_val;
306 : 10104226 : break;
307 : : }
308 : : }
309 : :
310 : 738949 : if (decl
311 : 20637687 : && mode == GET_MODE (x)
312 : 20355383 : && VAR_P (decl)
313 : 13211856 : && (TREE_STATIC (decl)
314 : 11997356 : || DECL_THREAD_LOCAL_P (decl))
315 : 1250018 : && DECL_RTL_SET_P (decl)
316 : 10614789 : && MEM_P (DECL_RTL (decl)))
317 : : {
318 : 1249512 : rtx newx;
319 : :
320 : 1249512 : offset += MEM_OFFSET (x);
321 : :
322 : 1249512 : newx = DECL_RTL (decl);
323 : :
324 : 1249512 : if (MEM_P (newx))
325 : : {
326 : 1249512 : rtx n = XEXP (newx, 0), o = XEXP (x, 0);
327 : 1249512 : 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 : 1249512 : n = strip_offset (n, &n_offset);
336 : 1249512 : o = strip_offset (o, &o_offset);
337 : 2471850 : if (!(known_eq (o_offset, n_offset + offset)
338 : 1222338 : && rtx_equal_p (o, n)))
339 : 209076 : 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 : 3517462043 : 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 : 5333003 : simplify_context::simplify_gen_unary (rtx_code code, machine_mode mode, rtx op,
355 : : machine_mode op_mode)
356 : : {
357 : 5333003 : rtx tem;
358 : :
359 : : /* If this simplifies, use it. */
360 : 5333003 : if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
361 : : return tem;
362 : :
363 : 2044406 : return gen_rtx_fmt_e (code, mode, op);
364 : : }
365 : :
366 : : /* Likewise for ternary operations. */
367 : :
368 : : rtx
369 : 2210867 : 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 : 2210867 : rtx tem;
374 : :
375 : : /* If this simplifies, use it. */
376 : 2210867 : if ((tem = simplify_ternary_operation (code, mode, op0_mode,
377 : : op0, op1, op2)) != 0)
378 : : return tem;
379 : :
380 : 1960965 : 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 : 21425844 : simplify_context::simplify_gen_relational (rtx_code code, machine_mode mode,
388 : : machine_mode cmp_mode,
389 : : rtx op0, rtx op1)
390 : : {
391 : 21425844 : rtx tem;
392 : :
393 : 21425844 : if ((tem = simplify_relational_operation (code, mode, cmp_mode,
394 : : op0, op1)) != 0)
395 : : return tem;
396 : :
397 : 19162287 : 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 : 488862436 : simplify_replace_fn_rtx (rtx x, const_rtx old_rtx,
407 : : rtx (*fn) (rtx, const_rtx, void *), void *data)
408 : : {
409 : 488862436 : enum rtx_code code = GET_CODE (x);
410 : 488862436 : machine_mode mode = GET_MODE (x);
411 : 488862436 : machine_mode op_mode;
412 : 488862436 : const char *fmt;
413 : 488862436 : rtx op0, op1, op2, newx, op;
414 : 488862436 : rtvec vec, newvec;
415 : 488862436 : int i, j;
416 : :
417 : 488862436 : if (UNLIKELY (fn != NULL))
418 : : {
419 : 426324245 : newx = fn (x, old_rtx, data);
420 : 426324245 : if (newx)
421 : : return newx;
422 : : }
423 : 62538191 : else if (rtx_equal_p (x, old_rtx))
424 : 5281226 : return copy_rtx ((rtx) data);
425 : :
426 : 382959755 : switch (GET_RTX_CLASS (code))
427 : : {
428 : 2150546 : case RTX_UNARY:
429 : 2150546 : op0 = XEXP (x, 0);
430 : 2150546 : op_mode = GET_MODE (op0);
431 : 2150546 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
432 : 2150546 : if (op0 == XEXP (x, 0))
433 : : return x;
434 : 679548 : return simplify_gen_unary (code, mode, op0, op_mode);
435 : :
436 : 76221410 : case RTX_BIN_ARITH:
437 : 76221410 : case RTX_COMM_ARITH:
438 : 76221410 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
439 : 76221410 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
440 : 76221410 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
441 : : return x;
442 : 23438204 : return simplify_gen_binary (code, mode, op0, op1);
443 : :
444 : 9819224 : case RTX_COMPARE:
445 : 9819224 : case RTX_COMM_COMPARE:
446 : 9819224 : op0 = XEXP (x, 0);
447 : 9819224 : op1 = XEXP (x, 1);
448 : 9819224 : op_mode = GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
449 : 9819224 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
450 : 9819224 : op1 = simplify_replace_fn_rtx (op1, old_rtx, fn, data);
451 : 9819224 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
452 : : return x;
453 : 2329640 : return simplify_gen_relational (code, mode, op_mode, op0, op1);
454 : :
455 : 5821580 : case RTX_TERNARY:
456 : 5821580 : case RTX_BITFIELD_OPS:
457 : 5821580 : op0 = XEXP (x, 0);
458 : 5821580 : op_mode = GET_MODE (op0);
459 : 5821580 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
460 : 5821580 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
461 : 5821580 : op2 = simplify_replace_fn_rtx (XEXP (x, 2), old_rtx, fn, data);
462 : 5821580 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1) && op2 == XEXP (x, 2))
463 : : return x;
464 : 1655178 : if (op_mode == VOIDmode)
465 : 1634907 : op_mode = GET_MODE (op0);
466 : 1655178 : return simplify_gen_ternary (code, mode, op_mode, op0, op1, op2);
467 : :
468 : 83369353 : case RTX_EXTRA:
469 : 83369353 : if (code == SUBREG)
470 : : {
471 : 708727 : op0 = simplify_replace_fn_rtx (SUBREG_REG (x), old_rtx, fn, data);
472 : 708727 : if (op0 == SUBREG_REG (x))
473 : : return x;
474 : 139168 : op0 = simplify_gen_subreg (GET_MODE (x), op0,
475 : 69584 : GET_MODE (SUBREG_REG (x)),
476 : 69584 : SUBREG_BYTE (x));
477 : 69584 : return op0 ? op0 : x;
478 : : }
479 : : break;
480 : :
481 : 61035836 : case RTX_OBJ:
482 : 61035836 : if (code == MEM)
483 : : {
484 : 10961480 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
485 : 10961480 : if (op0 == XEXP (x, 0))
486 : : return x;
487 : 165520 : return replace_equiv_address_nv (x, op0);
488 : : }
489 : 50074356 : 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 : 277276788 : newx = x;
515 : 277276788 : fmt = GET_RTX_FORMAT (code);
516 : 598385846 : for (i = 0; fmt[i]; i++)
517 : 321109058 : switch (fmt[i])
518 : : {
519 : 3048911 : case 'E':
520 : 3048911 : vec = XVEC (x, i);
521 : 3048911 : newvec = XVEC (newx, i);
522 : 12653633 : for (j = 0; j < GET_NUM_ELEM (vec); j++)
523 : : {
524 : 9604722 : op = simplify_replace_fn_rtx (RTVEC_ELT (vec, j),
525 : : old_rtx, fn, data);
526 : 9604722 : if (op != RTVEC_ELT (vec, j))
527 : : {
528 : 347308 : if (newvec == vec)
529 : : {
530 : 338346 : newvec = shallow_copy_rtvec (vec);
531 : 338346 : if (x == newx)
532 : 338346 : newx = shallow_copy_rtx (x);
533 : 338346 : XVEC (newx, i) = newvec;
534 : : }
535 : 347308 : RTVEC_ELT (newvec, j) = op;
536 : : }
537 : : }
538 : : break;
539 : :
540 : 75212601 : case 'e':
541 : 75212601 : if (XEXP (x, i))
542 : : {
543 : 75212601 : op = simplify_replace_fn_rtx (XEXP (x, i), old_rtx, fn, data);
544 : 75212601 : if (op != XEXP (x, i))
545 : : {
546 : 4379484 : if (x == newx)
547 : 4376194 : newx = shallow_copy_rtx (x);
548 : 4379484 : 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 : 12865290 : simplify_replace_rtx (rtx x, const_rtx old_rtx, rtx new_rtx)
561 : : {
562 : 12865290 : 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 : 18491958 : simplify_context::simplify_truncation (machine_mode mode, rtx op,
614 : : machine_mode op_mode)
615 : : {
616 : 18491958 : unsigned int precision = GET_MODE_UNIT_PRECISION (mode);
617 : 18491958 : unsigned int op_precision = GET_MODE_UNIT_PRECISION (op_mode);
618 : 18491958 : scalar_int_mode int_mode, int_op_mode, subreg_mode;
619 : :
620 : 18491958 : gcc_assert (precision <= op_precision);
621 : :
622 : : /* Optimize truncations of zero and sign extended values. */
623 : 18491958 : if (GET_CODE (op) == ZERO_EXTEND
624 : 18491958 : || 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 : 287581 : machine_mode origmode = GET_MODE (XEXP (op, 0));
633 : 287581 : if (mode == origmode)
634 : : return XEXP (op, 0);
635 : 16888 : else if (precision <= GET_MODE_UNIT_PRECISION (origmode))
636 : 4770 : return simplify_gen_unary (TRUNCATE, mode,
637 : 4770 : XEXP (op, 0), origmode);
638 : : else
639 : 3674 : return simplify_gen_unary (GET_CODE (op), mode,
640 : 3674 : 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 : 18204377 : if (1
647 : : && (!WORD_REGISTER_OPERATIONS || precision >= BITS_PER_WORD)
648 : : && (GET_CODE (op) == PLUS
649 : : || GET_CODE (op) == MINUS
650 : 18204377 : || GET_CODE (op) == MULT))
651 : : {
652 : 781891 : rtx op0 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0), op_mode);
653 : 781891 : if (op0)
654 : : {
655 : 781891 : rtx op1 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 1), op_mode);
656 : 781891 : if (op1)
657 : 781891 : 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 : 17422486 : if ((GET_CODE (op) == LSHIFTRT
665 : 17422486 : || 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 : 1705706 : && 2 * precision <= op_precision
671 : 1705706 : && CONST_INT_P (XEXP (op, 1))
672 : 1606161 : && GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
673 : 25 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
674 : 22 : && UINTVAL (XEXP (op, 1)) < precision)
675 : 18 : return simplify_gen_binary (ASHIFTRT, mode,
676 : 18 : 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 : 17422468 : if ((GET_CODE (op) == LSHIFTRT
682 : : || GET_CODE (op) == ASHIFTRT)
683 : 1705688 : && CONST_INT_P (XEXP (op, 1))
684 : 1606143 : && GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
685 : 680 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
686 : 680 : && UINTVAL (XEXP (op, 1)) < precision)
687 : 670 : return simplify_gen_binary (LSHIFTRT, mode,
688 : 670 : 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 : 17421798 : if (GET_CODE (op) == ASHIFT
694 : 467947 : && CONST_INT_P (XEXP (op, 1))
695 : 408964 : && (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
696 : 408964 : || GET_CODE (XEXP (op, 0)) == SIGN_EXTEND)
697 : 547 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
698 : 534 : && UINTVAL (XEXP (op, 1)) < precision)
699 : 526 : return simplify_gen_binary (ASHIFT, mode,
700 : 526 : 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 : 17421272 : if (GET_CODE (op) == AND
706 : 625710 : && (GET_CODE (XEXP (op, 0)) == LSHIFTRT
707 : 625710 : || GET_CODE (XEXP (op, 0)) == ASHIFTRT)
708 : 31231 : && CONST_INT_P (XEXP (XEXP (op, 0), 1))
709 : 31121 : && CONST_INT_P (XEXP (op, 1)))
710 : : {
711 : 31121 : rtx op0 = (XEXP (XEXP (op, 0), 0));
712 : 31121 : rtx shift_op = XEXP (XEXP (op, 0), 1);
713 : 31121 : rtx mask_op = XEXP (op, 1);
714 : 31121 : unsigned HOST_WIDE_INT shift = UINTVAL (shift_op);
715 : 31121 : unsigned HOST_WIDE_INT mask = UINTVAL (mask_op);
716 : :
717 : 31121 : if (shift < precision
718 : : /* If doing this transform works for an X with all bits set,
719 : : it works for any X. */
720 : 17937 : && ((GET_MODE_MASK (mode) >> shift) & mask)
721 : 17937 : == ((GET_MODE_MASK (op_mode) >> shift) & mask)
722 : 3201 : && (op0 = simplify_gen_unary (TRUNCATE, mode, op0, op_mode))
723 : 34322 : && (op0 = simplify_gen_binary (LSHIFTRT, mode, op0, shift_op)))
724 : : {
725 : 3201 : mask_op = GEN_INT (trunc_int_for_mode (mask, mode));
726 : 3201 : 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 : 17418071 : if ((GET_CODE (op) == ZERO_EXTRACT || GET_CODE (op) == SIGN_EXTRACT)
734 : 415250 : && REG_P (XEXP (op, 0))
735 : 275840 : && GET_MODE (XEXP (op, 0)) == GET_MODE (op)
736 : 275048 : && CONST_INT_P (XEXP (op, 1))
737 : 275048 : && CONST_INT_P (XEXP (op, 2)))
738 : : {
739 : 238455 : rtx op0 = XEXP (op, 0);
740 : 238455 : unsigned HOST_WIDE_INT len = UINTVAL (XEXP (op, 1));
741 : 238455 : unsigned HOST_WIDE_INT pos = UINTVAL (XEXP (op, 2));
742 : 238455 : 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 : 238455 : else if (!BITS_BIG_ENDIAN && precision >= len + pos)
753 : : {
754 : 8134 : op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
755 : 8134 : if (op0)
756 : 8134 : return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
757 : 8134 : XEXP (op, 1), XEXP (op, 2));
758 : : }
759 : : }
760 : :
761 : : /* Recognize a word extraction from a multi-word subreg. */
762 : 17409937 : if ((GET_CODE (op) == LSHIFTRT
763 : 17409937 : || GET_CODE (op) == ASHIFTRT)
764 : 1705018 : && SCALAR_INT_MODE_P (mode)
765 : 1702076 : && SCALAR_INT_MODE_P (op_mode)
766 : 1834413 : && precision >= BITS_PER_WORD
767 : 64780 : && 2 * precision <= op_precision
768 : 64780 : && CONST_INT_P (XEXP (op, 1))
769 : 56690 : && (INTVAL (XEXP (op, 1)) & (precision - 1)) == 0
770 : 1865 : && UINTVAL (XEXP (op, 1)) < op_precision)
771 : : {
772 : 1865 : poly_int64 byte = subreg_lowpart_offset (mode, op_mode);
773 : 1865 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
774 : 1865 : return simplify_gen_subreg (mode, XEXP (op, 0), op_mode,
775 : : (WORDS_BIG_ENDIAN
776 : 1865 : ? byte - shifted_bytes
777 : 1865 : : 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 : 17408072 : if ((GET_CODE (op) == LSHIFTRT
784 : : || GET_CODE (op) == ASHIFTRT)
785 : 1700211 : && is_a <scalar_int_mode> (mode, &int_mode)
786 : 19107050 : && is_a <scalar_int_mode> (op_mode, &int_op_mode)
787 : 1700211 : && MEM_P (XEXP (op, 0))
788 : 11131 : && CONST_INT_P (XEXP (op, 1))
789 : 20770 : && INTVAL (XEXP (op, 1)) % GET_MODE_BITSIZE (int_mode) == 0
790 : 1272 : && INTVAL (XEXP (op, 1)) > 0
791 : 2544 : && INTVAL (XEXP (op, 1)) < GET_MODE_BITSIZE (int_op_mode)
792 : 1272 : && ! mode_dependent_address_p (XEXP (XEXP (op, 0), 0),
793 : 1272 : MEM_ADDR_SPACE (XEXP (op, 0)))
794 : 1272 : && ! MEM_VOLATILE_P (XEXP (op, 0))
795 : 17408072 : && (GET_MODE_SIZE (int_mode) >= UNITS_PER_WORD
796 : : || WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN))
797 : : {
798 : 1233 : poly_int64 byte = subreg_lowpart_offset (int_mode, int_op_mode);
799 : 1233 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
800 : 1233 : 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 : 17406839 : if ((GET_CODE (op) == ABS
809 : 17406839 : || GET_CODE (op) == NEG)
810 : 27312 : && (GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
811 : 27312 : || 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 : 17406837 : if (GET_CODE (op) == SUBREG
818 : 17406935 : && is_a <scalar_int_mode> (mode, &int_mode)
819 : 35058 : && SCALAR_INT_MODE_P (op_mode)
820 : 35058 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &subreg_mode)
821 : 17441833 : && subreg_lowpart_p (op))
822 : : {
823 : : /* (truncate:A (subreg:B (truncate:C X) 0)) is (truncate:A X). */
824 : 34993 : 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 : 34993 : if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
841 : : {
842 : 34993 : unsigned int int_op_prec = GET_MODE_PRECISION (int_op_mode);
843 : 34993 : unsigned int subreg_prec = GET_MODE_PRECISION (subreg_mode);
844 : 34993 : if (int_op_prec > subreg_prec)
845 : : {
846 : 1352 : 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 : 33641 : else if (int_op_prec < subreg_prec
855 : 33641 : && GET_MODE_PRECISION (int_mode) < int_op_prec)
856 : 33641 : return simplify_gen_unary (TRUNCATE, int_mode,
857 : 33641 : SUBREG_REG (op), subreg_mode);
858 : : }
859 : : }
860 : :
861 : : /* (truncate:A (truncate:B X)) is (truncate:A X). */
862 : 17371877 : 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 : 17371877 : if (GET_CODE (op) == IOR
869 : 33876 : && SCALAR_INT_MODE_P (mode)
870 : 33876 : && SCALAR_INT_MODE_P (op_mode)
871 : 33876 : && CONST_INT_P (XEXP (op, 1))
872 : 17380041 : && trunc_int_for_mode (INTVAL (XEXP (op, 1)), mode) == -1)
873 : 42 : return constm1_rtx;
874 : :
875 : : return NULL_RTX;
876 : : }
877 : :
878 : : /* Try to simplify a unary operation CODE whose output mode is to be
879 : : MODE with input operand OP whose mode was originally OP_MODE.
880 : : Return zero if no simplification can be made. */
881 : : rtx
882 : 27967882 : simplify_context::simplify_unary_operation (rtx_code code, machine_mode mode,
883 : : rtx op, machine_mode op_mode)
884 : : {
885 : 27967882 : rtx trueop, tem;
886 : :
887 : 27967882 : trueop = avoid_constant_pool_reference (op);
888 : :
889 : 27967882 : tem = simplify_const_unary_operation (code, mode, trueop, op_mode);
890 : 27967882 : if (tem)
891 : : return tem;
892 : :
893 : 22939708 : 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 : 2692 : exact_int_to_float_conversion_p (const_rtx op)
901 : : {
902 : 2692 : 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 : 2692 : if (op0_mode == VOIDmode)
906 : : return false;
907 : 5382 : int out_bits = significand_size (GET_MODE_INNER (GET_MODE (op)));
908 : 2691 : int in_prec = GET_MODE_UNIT_PRECISION (op0_mode);
909 : 2691 : int in_bits = in_prec;
910 : 2691 : if (HWI_COMPUTABLE_MODE_P (op0_mode))
911 : : {
912 : 2601 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (XEXP (op, 0), op0_mode);
913 : 2601 : if (GET_CODE (op) == FLOAT)
914 : 2477 : in_bits -= num_sign_bit_copies (XEXP (op, 0), op0_mode);
915 : 124 : else if (GET_CODE (op) == UNSIGNED_FLOAT)
916 : 124 : in_bits = wi::min_precision (wi::uhwi (nonzero, in_prec), UNSIGNED);
917 : : else
918 : 0 : gcc_unreachable ();
919 : 2601 : in_bits -= wi::ctz (wi::uhwi (nonzero, in_prec));
920 : : }
921 : 2691 : 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 : 22939708 : simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode,
928 : : rtx op)
929 : : {
930 : 22939708 : enum rtx_code reversed;
931 : 22939708 : rtx temp, elt, base, step;
932 : 22939708 : scalar_int_mode inner, int_mode, op_mode, op0_mode;
933 : :
934 : 22939708 : switch (code)
935 : : {
936 : 1839267 : case NOT:
937 : : /* (not (not X)) == X. */
938 : 1839267 : if (GET_CODE (op) == NOT)
939 : 3811 : 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 : 1835456 : if (COMPARISON_P (op)
944 : 11132 : && (mode == BImode || STORE_FLAG_VALUE == -1)
945 : 1835456 : && ((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 : 1835456 : if (GET_CODE (op) == PLUS
951 : 290892 : && XEXP (op, 1) == constm1_rtx)
952 : 7277 : 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 : 1828179 : if (GET_CODE (op) == NEG && CONSTM1_RTX (mode))
958 : 71619 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
959 : 71619 : CONSTM1_RTX (mode));
960 : :
961 : : /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
962 : 1756560 : if (GET_CODE (op) == XOR
963 : 15709 : && CONST_INT_P (XEXP (op, 1))
964 : 1760616 : && (temp = simplify_unary_operation (NOT, mode,
965 : : XEXP (op, 1), mode)) != 0)
966 : 4056 : 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 : 1752504 : if (GET_CODE (op) == PLUS
970 : 283615 : && CONST_INT_P (XEXP (op, 1))
971 : 166266 : && mode_signbit_p (mode, XEXP (op, 1))
972 : 1757900 : && (temp = simplify_unary_operation (NOT, mode,
973 : : XEXP (op, 1), mode)) != 0)
974 : 5396 : 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 : 1747108 : if (GET_CODE (op) == ASHIFT
983 : 48053 : && XEXP (op, 0) == const1_rtx)
984 : : {
985 : 1104 : temp = simplify_gen_unary (NOT, mode, const1_rtx, mode);
986 : 1104 : 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 : 1746004 : 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 : 1746004 : if (partial_subreg_p (op)
1002 : 71313 : && subreg_lowpart_p (op)
1003 : 70999 : && GET_CODE (SUBREG_REG (op)) == ASHIFT
1004 : 97400 : && XEXP (SUBREG_REG (op), 0) == const1_rtx)
1005 : : {
1006 : 163 : machine_mode inner_mode = GET_MODE (SUBREG_REG (op));
1007 : 163 : rtx x;
1008 : :
1009 : 163 : 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 : 163 : temp = rtl_hooks.gen_lowpart_no_emit (mode, x);
1014 : 163 : 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 : 1745841 : if (GET_CODE (op) == IOR || GET_CODE (op) == AND)
1023 : : {
1024 : 11717 : rtx in1 = XEXP (op, 0), in2 = XEXP (op, 1);
1025 : 11717 : machine_mode op_mode;
1026 : :
1027 : 11717 : op_mode = GET_MODE (in1);
1028 : 11717 : in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
1029 : :
1030 : 11717 : op_mode = GET_MODE (in2);
1031 : 11717 : if (op_mode == VOIDmode)
1032 : 5795 : op_mode = mode;
1033 : 11717 : in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
1034 : :
1035 : 11717 : if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
1036 : : std::swap (in1, in2);
1037 : :
1038 : 23434 : 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 : 1734124 : 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 : 1650031 : case NEG:
1051 : : /* (neg (neg X)) == X. */
1052 : 1650031 : if (GET_CODE (op) == NEG)
1053 : 6592 : 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 : 1643439 : if (GET_CODE (op) == IF_THEN_ELSE)
1059 : : {
1060 : 2414 : rtx cond = XEXP (op, 0);
1061 : 2414 : rtx true_rtx = XEXP (op, 1);
1062 : 2414 : rtx false_rtx = XEXP (op, 2);
1063 : :
1064 : 2414 : if ((GET_CODE (true_rtx) == NEG
1065 : 0 : && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
1066 : 2414 : || (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 : 1643439 : if (GET_CODE (op) == PLUS
1083 : 135493 : && XEXP (op, 1) == const1_rtx)
1084 : 53327 : return simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1085 : :
1086 : : /* Similarly, (neg (not X)) is (plus X 1). */
1087 : 1590112 : if (GET_CODE (op) == NOT)
1088 : 542 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
1089 : 542 : 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 : 1589570 : if (GET_CODE (op) == MINUS
1097 : 24379 : && !HONOR_SIGNED_ZEROS (mode)
1098 : 1612541 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1099 : 22971 : return simplify_gen_binary (MINUS, mode, XEXP (op, 1), XEXP (op, 0));
1100 : :
1101 : 1566599 : if (GET_CODE (op) == PLUS
1102 : 82166 : && !HONOR_SIGNED_ZEROS (mode)
1103 : 1648405 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1104 : : {
1105 : : /* (neg (plus A C)) is simplified to (minus -C A). */
1106 : 81806 : if (CONST_SCALAR_INT_P (XEXP (op, 1))
1107 : 5025 : || CONST_DOUBLE_AS_FLOAT_P (XEXP (op, 1)))
1108 : : {
1109 : 76781 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 1), mode);
1110 : 76781 : if (temp)
1111 : 76781 : 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 : 5025 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
1116 : 5025 : 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 : 1484793 : if (GET_CODE (op) == MULT
1122 : 1484793 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1123 : : {
1124 : 20199 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 1), mode);
1125 : 20199 : 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 : 1464594 : if (GET_CODE (op) == ASHIFT)
1132 : : {
1133 : 52713 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 0), mode);
1134 : 52713 : if (temp)
1135 : 12880 : 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 : 1451714 : if (GET_CODE (op) == ASHIFTRT
1141 : 27353 : && CONST_INT_P (XEXP (op, 1))
1142 : 1506324 : && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1143 : 420 : return simplify_gen_binary (LSHIFTRT, mode,
1144 : 420 : 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 : 1451294 : if (GET_CODE (op) == LSHIFTRT
1149 : 7387 : && CONST_INT_P (XEXP (op, 1))
1150 : 1465912 : && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1151 : 2424 : return simplify_gen_binary (ASHIFTRT, mode,
1152 : 2424 : 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 : 1448870 : if (GET_CODE (op) == XOR
1156 : 23947 : && XEXP (op, 1) == const1_rtx
1157 : 1449008 : && 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 : 1448830 : if (GET_CODE (op) == LT
1163 : 2556 : && XEXP (op, 1) == const0_rtx
1164 : 1450430 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op, 0)), &inner))
1165 : : {
1166 : 336 : int_mode = as_a <scalar_int_mode> (mode);
1167 : 336 : int isize = GET_MODE_PRECISION (inner);
1168 : 336 : if (STORE_FLAG_VALUE == 1)
1169 : : {
1170 : 336 : temp = simplify_gen_binary (ASHIFTRT, inner, XEXP (op, 0),
1171 : : gen_int_shift_amount (inner,
1172 : 336 : isize - 1));
1173 : 336 : if (int_mode == inner)
1174 : : return temp;
1175 : 169 : if (GET_MODE_PRECISION (int_mode) > isize)
1176 : 104 : 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 : 1448494 : 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 : 276 : scalar_mode inner_mode = GET_MODE_INNER (mode);
1198 : 276 : base = simplify_unary_operation (NEG, inner_mode, base, inner_mode);
1199 : 276 : if (base)
1200 : : {
1201 : 276 : step = simplify_unary_operation (NEG, inner_mode,
1202 : : step, inner_mode);
1203 : 276 : if (step)
1204 : 276 : return gen_vec_series (mode, base, step);
1205 : : }
1206 : : }
1207 : : break;
1208 : :
1209 : 1297126 : case TRUNCATE:
1210 : : /* Don't optimize (lshiftrt (mult ...)) as it would interfere
1211 : : with the umulXi3_highpart patterns. */
1212 : 1297126 : if (GET_CODE (op) == LSHIFTRT
1213 : 18290 : && GET_CODE (XEXP (op, 0)) == MULT)
1214 : : break;
1215 : :
1216 : 1289896 : 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 : 1289884 : if (GET_MODE (op) != VOIDmode)
1231 : : {
1232 : 1289884 : temp = simplify_truncation (mode, op, GET_MODE (op));
1233 : 1289884 : 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 : 1157915 : if (known_eq (GET_MODE_NUNITS (mode), 1)
1240 : 1157915 : && (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op))
1241 : 0 : || truncated_to_mode (mode, op)))
1242 : : {
1243 : 1146836 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1244 : 1146836 : 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 : 11307 : if (HWI_COMPUTABLE_MODE_P (mode)
1253 : 228 : && COMPARISON_P (op)
1254 : 0 : && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
1255 : 11307 : && 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 : 11307 : if (GET_CODE (op) == MEM
1265 : 339 : && !VECTOR_MODE_P (mode)
1266 : 226 : && !MEM_VOLATILE_P (op)
1267 : 11533 : && !mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op)))
1268 : : {
1269 : 226 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1270 : 226 : if (temp)
1271 : : return temp;
1272 : : }
1273 : :
1274 : : /* Check for useless truncation. */
1275 : 11307 : if (GET_MODE (op) == mode)
1276 : : return op;
1277 : : break;
1278 : :
1279 : 173793 : case FLOAT_TRUNCATE:
1280 : : /* Check for useless truncation. */
1281 : 173793 : if (GET_MODE (op) == mode)
1282 : : return op;
1283 : :
1284 : 173793 : if (DECIMAL_FLOAT_MODE_P (mode))
1285 : : break;
1286 : :
1287 : : /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
1288 : 173639 : 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 : 173639 : if ((GET_CODE (op) == FLOAT_TRUNCATE
1302 : 145 : && flag_unsafe_math_optimizations)
1303 : 173635 : || 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 : 173633 : if ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1312 : 173633 : && (flag_unsafe_math_optimizations
1313 : 1419 : || exact_int_to_float_conversion_p (op)))
1314 : 1418 : return simplify_gen_unary (GET_CODE (op), mode,
1315 : : XEXP (op, 0),
1316 : 1418 : 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 : 172215 : if ((GET_CODE (op) == ABS
1321 : 172215 : || 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 : 172187 : if (GET_CODE (op) == SUBREG
1330 : 308 : && subreg_lowpart_p (op)
1331 : 172492 : && GET_CODE (SUBREG_REG (op)) == FLOAT_TRUNCATE)
1332 : : return SUBREG_REG (op);
1333 : : break;
1334 : :
1335 : 593514 : case FLOAT_EXTEND:
1336 : : /* Check for useless extension. */
1337 : 593514 : if (GET_MODE (op) == mode)
1338 : : return op;
1339 : :
1340 : 593514 : 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 : 593411 : if (GET_CODE (op) == FLOAT_EXTEND
1349 : 593411 : || ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1350 : 1273 : && exact_int_to_float_conversion_p (op)))
1351 : 562 : return simplify_gen_unary (GET_CODE (op), mode,
1352 : : XEXP (op, 0),
1353 : 562 : GET_MODE (XEXP (op, 0)));
1354 : :
1355 : : break;
1356 : :
1357 : 288841 : case ABS:
1358 : : /* (abs (neg <foo>)) -> (abs <foo>) */
1359 : 288841 : if (GET_CODE (op) == NEG)
1360 : 30 : return simplify_gen_unary (ABS, mode, XEXP (op, 0),
1361 : 30 : 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 : 288811 : if (GET_MODE (op) == VOIDmode)
1366 : : break;
1367 : :
1368 : : /* If operand is something known to be positive, ignore the ABS. */
1369 : 288811 : 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 : 288582 : 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 : 288582 : && 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 : 288582 : if (is_a <scalar_int_mode> (mode, &int_mode)
1400 : 40262 : && (num_sign_bit_copies (op, int_mode)
1401 : 40262 : == GET_MODE_PRECISION (int_mode)))
1402 : 49 : 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 : 3419 : case POPCOUNT:
1419 : 3419 : 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 : 36 : case ZERO_EXTEND:
1428 : : /* (popcount (zero_extend <X>)) = (zero_extend (popcount <X>)). */
1429 : 72 : temp = simplify_gen_unary (POPCOUNT, GET_MODE (XEXP (op, 0)),
1430 : 36 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1431 : 36 : return simplify_gen_unary (ZERO_EXTEND, mode, temp,
1432 : 36 : 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 : 30399 : case BSWAP:
1481 : : /* (bswap (bswap x)) -> x. */
1482 : 30399 : 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 : 930986 : case FLOAT:
1493 : : /* (float (sign_extend <X>)) = (float <X>). */
1494 : 930986 : if (GET_CODE (op) == SIGN_EXTEND)
1495 : 10918 : return simplify_gen_unary (FLOAT, mode, XEXP (op, 0),
1496 : 10918 : GET_MODE (XEXP (op, 0)));
1497 : : break;
1498 : :
1499 : 3221821 : case SIGN_EXTEND:
1500 : : /* Check for useless extension. */
1501 : 3221821 : 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 : 3221781 : 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 : 3221781 : if (GET_CODE (op) == MULT)
1518 : : {
1519 : 68226 : rtx lhs = XEXP (op, 0);
1520 : 68226 : rtx rhs = XEXP (op, 1);
1521 : 68226 : enum rtx_code lcode = GET_CODE (lhs);
1522 : 68226 : 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 : 68226 : if ((lcode == SIGN_EXTEND
1527 : 68077 : || (lcode == ASHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1528 : 877 : && (rcode == SIGN_EXTEND
1529 : 833 : || (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 : 3221673 : if (GET_CODE (op) == SUBREG
1563 : 164576 : && SUBREG_PROMOTED_VAR_P (op)
1564 : 3226615 : && 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 : 3221673 : if (GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND)
1591 : : {
1592 : 20202 : gcc_assert (GET_MODE_UNIT_PRECISION (mode)
1593 : : > GET_MODE_UNIT_PRECISION (GET_MODE (op)));
1594 : 6734 : return simplify_gen_unary (GET_CODE (op), mode, XEXP (op, 0),
1595 : 6734 : 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 : 3214939 : if ((GET_CODE (op) == ASHIFTRT || GET_CODE (op) == LSHIFTRT)
1604 : 87242 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1605 : 3217435 : && is_a <scalar_int_mode> (mode, &int_mode)
1606 : 5229 : && CONST_INT_P (XEXP (op, 1))
1607 : 5229 : && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1608 : 3220042 : && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1609 : 5103 : GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1610 : : {
1611 : 5103 : scalar_int_mode tmode;
1612 : 5103 : gcc_assert (GET_MODE_PRECISION (int_mode)
1613 : : > GET_MODE_PRECISION (op_mode));
1614 : 5103 : if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1615 : 7473 : - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1616 : : {
1617 : 2733 : rtx inner =
1618 : 2733 : rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1619 : 2733 : if (inner)
1620 : 2733 : return simplify_gen_unary (GET_CODE (op) == ASHIFTRT
1621 : : ? SIGN_EXTEND : ZERO_EXTEND,
1622 : 2733 : 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 : 3212206 : if (GET_CODE (op) == LSHIFTRT
1629 : 201 : && CONST_INT_P (XEXP (op, 1))
1630 : 201 : && XEXP (op, 1) != const0_rtx)
1631 : 201 : 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 : 3212005 : 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 : 3212005 : if (val_signbit_known_clear_p (GET_MODE (op),
1668 : 3212005 : nonzero_bits (op, GET_MODE (op))))
1669 : 44207 : return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1670 : :
1671 : : /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1672 : 3167798 : if (GET_CODE (op) == SUBREG
1673 : 164023 : && subreg_lowpart_p (op)
1674 : 163892 : && GET_MODE (SUBREG_REG (op)) == mode
1675 : 3304075 : && is_a <scalar_int_mode> (mode, &int_mode)
1676 : 143055 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1677 : 143055 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1678 : 141248 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1679 : 3309046 : && (nonzero_bits (SUBREG_REG (op), mode)
1680 : 141248 : & ~(GET_MODE_MASK (op_mode) >> 1)) == 0)
1681 : 6778 : 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 : 3161020 : 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 : 11991161 : case ZERO_EXTEND:
1708 : : /* Check for useless extension. */
1709 : 11991161 : 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 : 11991121 : if (GET_CODE (op) == AND
1715 : 132462 : && CONST_INT_P (XEXP (op, 1))
1716 : 105581 : && INTVAL (XEXP (op, 1)) > 0)
1717 : : {
1718 : 98499 : rtx tem = rtl_hooks.gen_lowpart_no_emit (mode, XEXP (op, 0));
1719 : 98499 : if (tem)
1720 : 81623 : 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 : 11909498 : if (GET_CODE (op) == SUBREG
1727 : 1497295 : && SUBREG_PROMOTED_VAR_P (op)
1728 : 11909947 : && SUBREG_PROMOTED_UNSIGNED_P (op))
1729 : : {
1730 : 449 : rtx subreg = SUBREG_REG (op);
1731 : 449 : machine_mode subreg_mode = GET_MODE (subreg);
1732 : 449 : if (!paradoxical_subreg_p (mode, subreg_mode))
1733 : : {
1734 : 293 : temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1735 : 293 : if (temp)
1736 : : {
1737 : : /* Preserve SUBREG_PROMOTED_VAR_P. */
1738 : 293 : if (partial_subreg_p (temp))
1739 : : {
1740 : 129 : SUBREG_PROMOTED_VAR_P (temp) = 1;
1741 : 129 : SUBREG_PROMOTED_SET (temp, SRP_UNSIGNED);
1742 : : }
1743 : 293 : return temp;
1744 : : }
1745 : : }
1746 : : else
1747 : : /* Zero-extending a zero-extended subreg. */
1748 : 156 : return simplify_gen_unary (ZERO_EXTEND, mode,
1749 : 156 : subreg, subreg_mode);
1750 : : }
1751 : :
1752 : : /* Extending a widening multiplication should be canonicalized to
1753 : : a wider widening multiplication. */
1754 : 11909049 : if (GET_CODE (op) == MULT)
1755 : : {
1756 : 178441 : rtx lhs = XEXP (op, 0);
1757 : 178441 : rtx rhs = XEXP (op, 1);
1758 : 178441 : enum rtx_code lcode = GET_CODE (lhs);
1759 : 178441 : 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 : 178441 : if ((lcode == ZERO_EXTEND
1764 : 177705 : || (lcode == LSHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1765 : 784 : && (rcode == ZERO_EXTEND
1766 : 672 : || (rcode == LSHIFTRT && CONST_INT_P (XEXP (rhs, 1)))))
1767 : : {
1768 : 112 : machine_mode lmode = GET_MODE (lhs);
1769 : 112 : machine_mode rmode = GET_MODE (rhs);
1770 : 112 : int bits;
1771 : :
1772 : 112 : 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 : 224 : bits = GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (lhs, 0)));
1779 : :
1780 : 112 : if (rcode == LSHIFTRT)
1781 : 0 : bits += (GET_MODE_UNIT_PRECISION (rmode)
1782 : 0 : - INTVAL (XEXP (rhs, 1)));
1783 : : else /* rcode == ZERO_EXTEND */
1784 : 224 : 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 : 224 : if (bits <= GET_MODE_UNIT_PRECISION (GET_MODE (op)))
1789 : 112 : return simplify_gen_binary
1790 : 112 : (MULT, mode,
1791 : : simplify_gen_unary (ZERO_EXTEND, mode, lhs, lmode),
1792 : 112 : 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 : 11908937 : if (GET_CODE (op) == ZERO_EXTEND)
1798 : 23930 : return simplify_gen_unary (ZERO_EXTEND, mode, XEXP (op, 0),
1799 : 23930 : 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 : 11885007 : if (GET_CODE (op) == LSHIFTRT
1805 : 81951 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1806 : 11885030 : && is_a <scalar_int_mode> (mode, &int_mode)
1807 : 23 : && CONST_INT_P (XEXP (op, 1))
1808 : 18 : && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1809 : 11885007 : && (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 : 11885007 : if (partial_subreg_p (op)
1830 : 13337000 : && is_a <scalar_int_mode> (mode, &int_mode)
1831 : 1478329 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &op0_mode)
1832 : 1477761 : && GET_MODE_PRECISION (op0_mode) <= HOST_BITS_PER_WIDE_INT
1833 : 1026219 : && GET_MODE_PRECISION (int_mode) >= GET_MODE_PRECISION (op0_mode)
1834 : 1003091 : && subreg_lowpart_p (op)
1835 : 2185901 : && (nonzero_bits (SUBREG_REG (op), op0_mode)
1836 : 689055 : & ~GET_MODE_MASK (GET_MODE (op))) == 0)
1837 : : {
1838 : 26336 : if (GET_MODE_PRECISION (int_mode) == GET_MODE_PRECISION (op0_mode))
1839 : 16174 : return SUBREG_REG (op);
1840 : 10162 : return simplify_gen_unary (ZERO_EXTEND, int_mode, SUBREG_REG (op),
1841 : 10162 : op0_mode);
1842 : : }
1843 : :
1844 : : /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1845 : 11858671 : if (GET_CODE (op) == SUBREG
1846 : 1470510 : && subreg_lowpart_p (op)
1847 : 774640 : && GET_MODE (SUBREG_REG (op)) == mode
1848 : 12551806 : && is_a <scalar_int_mode> (mode, &int_mode)
1849 : 693135 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1850 : 693135 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1851 : 624310 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1852 : 12482981 : && (nonzero_bits (SUBREG_REG (op), mode)
1853 : 624310 : & ~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 : 11858671 : if (partial_subreg_p (op)
1865 : 1451993 : && GET_CODE (XEXP (op, 0)) == NOT
1866 : 1868 : && GET_MODE (XEXP (op, 0)) == mode
1867 : 1865 : && subreg_lowpart_p (op)
1868 : 11858961 : && HWI_COMPUTABLE_MODE_P (mode)
1869 : 297 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1870 : 1470807 : && (nonzero_bits (XEXP (XEXP (op, 0), 0), mode)
1871 : 297 : & ~GET_MODE_MASK (op_mode)) == 0)
1872 : : {
1873 : 7 : unsigned HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
1874 : 14 : return simplify_gen_binary (XOR, mode,
1875 : 7 : XEXP (XEXP (op, 0), 0),
1876 : 7 : 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 : 11858664 : if (target_default_pointer_address_modes_p ()
1884 : : && POINTERS_EXTEND_UNSIGNED > 0
1885 : 13165156 : && 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 : 11858685 : && !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 : 19683101 : if (VECTOR_MODE_P (mode)
1908 : 1464339 : && vec_duplicate_p (op, &elt)
1909 : 21153137 : && code != VEC_DUPLICATE)
1910 : : {
1911 : 5693 : 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 : 4630 : temp = simplify_gen_unary (code, GET_MODE_INNER (mode),
1916 : 9260 : 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 : 1063 : temp = simplify_unary_operation (code, GET_MODE_INNER (mode),
1939 : 2126 : elt, GET_MODE_INNER (GET_MODE (op)));
1940 : 5693 : if (temp)
1941 : 5204 : 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 : 27968759 : simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
1952 : : rtx op, machine_mode op_mode)
1953 : : {
1954 : 27968759 : scalar_int_mode result_mode;
1955 : :
1956 : 27968759 : if (code == VEC_DUPLICATE)
1957 : : {
1958 : 1597585 : gcc_assert (VECTOR_MODE_P (mode));
1959 : 1597585 : if (GET_MODE (op) != VOIDmode)
1960 : : {
1961 : 529855 : if (!VECTOR_MODE_P (GET_MODE (op)))
1962 : 1046414 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE (op));
1963 : : else
1964 : 19944 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE_INNER
1965 : : (GET_MODE (op)));
1966 : : }
1967 : 1597585 : if (CONST_SCALAR_INT_P (op) || CONST_DOUBLE_AS_FLOAT_P (op))
1968 : 1105849 : return gen_const_vec_duplicate (mode, op);
1969 : 491736 : if (GET_CODE (op) == CONST_VECTOR
1970 : 491736 : && (CONST_VECTOR_DUPLICATE_P (op)
1971 : : || CONST_VECTOR_NUNITS (op).is_constant ()))
1972 : : {
1973 : 755 : unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op)
1974 : 755 : ? CONST_VECTOR_NPATTERNS (op)
1975 : 1509 : : CONST_VECTOR_NUNITS (op).to_constant ());
1976 : 2265 : gcc_assert (multiple_p (GET_MODE_NUNITS (mode), npatterns));
1977 : 755 : rtx_vector_builder builder (mode, npatterns, 1);
1978 : 3130 : for (unsigned i = 0; i < npatterns; i++)
1979 : 2375 : builder.quick_push (CONST_VECTOR_ELT (op, i));
1980 : 755 : return builder.build ();
1981 : 755 : }
1982 : : }
1983 : :
1984 : 25677804 : if (VECTOR_MODE_P (mode)
1985 : 1502481 : && GET_CODE (op) == CONST_VECTOR
1986 : 26959658 : && known_eq (GET_MODE_NUNITS (mode), CONST_VECTOR_NUNITS (op)))
1987 : : {
1988 : 32501 : gcc_assert (GET_MODE (op) == op_mode);
1989 : :
1990 : 32501 : rtx_vector_builder builder;
1991 : 32501 : if (!builder.new_unary_operation (mode, op, false))
1992 : : return 0;
1993 : :
1994 : 32501 : unsigned int count = builder.encoded_nelts ();
1995 : 143465 : for (unsigned int i = 0; i < count; i++)
1996 : : {
1997 : 222966 : rtx x = simplify_unary_operation (code, GET_MODE_INNER (mode),
1998 : : CONST_VECTOR_ELT (op, i),
1999 : 222966 : GET_MODE_INNER (op_mode));
2000 : 111483 : if (!x || !valid_for_const_vector_p (mode, x))
2001 : 519 : return 0;
2002 : 110964 : builder.quick_push (x);
2003 : : }
2004 : 31982 : return builder.build ();
2005 : 32501 : }
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 : 26829654 : if (code == FLOAT && CONST_SCALAR_INT_P (op))
2012 : : {
2013 : 7292 : REAL_VALUE_TYPE d;
2014 : :
2015 : 7292 : 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 : 7292 : 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 : 7292 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2030 : : return 0;
2031 : :
2032 : 7292 : 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 : 7292 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
2037 : : {
2038 : 1003 : bool fail = false;
2039 : 1003 : wide_int w = real_to_integer (&d, &fail,
2040 : : GET_MODE_PRECISION
2041 : 1003 : (as_a <scalar_int_mode> (op_mode)));
2042 : 2006 : if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
2043 : 897 : return 0;
2044 : 1003 : }
2045 : :
2046 : 6395 : return const_double_from_real_value (d, mode);
2047 : : }
2048 : 26822362 : else if (code == UNSIGNED_FLOAT && CONST_SCALAR_INT_P (op))
2049 : : {
2050 : 2139 : REAL_VALUE_TYPE d;
2051 : :
2052 : 2139 : 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 : 2139 : 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 : 2139 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2067 : : return 0;
2068 : :
2069 : 2139 : 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 : 2139 : 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 : 2123 : return const_double_from_real_value (d, mode);
2084 : : }
2085 : :
2086 : 26820223 : if (CONST_SCALAR_INT_P (op) && is_a <scalar_int_mode> (mode, &result_mode))
2087 : : {
2088 : 3459889 : unsigned int width = GET_MODE_PRECISION (result_mode);
2089 : 3459889 : if (width > MAX_BITSIZE_MODE_ANY_INT)
2090 : : return 0;
2091 : :
2092 : 3459889 : wide_int result;
2093 : 3459889 : scalar_int_mode imode = (op_mode == VOIDmode
2094 : 3459889 : ? result_mode
2095 : 3459672 : : as_a <scalar_int_mode> (op_mode));
2096 : 3459889 : rtx_mode_t op0 = rtx_mode_t (op, imode);
2097 : 3459889 : 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 : 3459889 : switch (code)
2110 : : {
2111 : 182658 : case NOT:
2112 : 182658 : result = wi::bit_not (op0);
2113 : 182658 : break;
2114 : :
2115 : 1910560 : case NEG:
2116 : 1910560 : result = wi::neg (op0);
2117 : 1910560 : break;
2118 : :
2119 : 7294 : case ABS:
2120 : 7294 : result = wi::abs (op0);
2121 : 7294 : 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 : 160 : case POPCOUNT:
2148 : 160 : result = wi::shwi (wi::popcount (op0), result_mode);
2149 : 160 : break;
2150 : :
2151 : 0 : case PARITY:
2152 : 0 : result = wi::shwi (wi::parity (op0), result_mode);
2153 : 0 : break;
2154 : :
2155 : 2013 : case BSWAP:
2156 : 2013 : result = wi::bswap (op0);
2157 : 2013 : break;
2158 : :
2159 : 0 : case BITREVERSE:
2160 : 0 : result = wi::bitreverse (op0);
2161 : 0 : break;
2162 : :
2163 : 1191979 : case TRUNCATE:
2164 : 1191979 : case ZERO_EXTEND:
2165 : 1191979 : result = wide_int::from (op0, width, UNSIGNED);
2166 : 1191979 : 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 : 150760 : case SIGN_EXTEND:
2183 : 150760 : result = wide_int::from (op0, width, SIGNED);
2184 : 150760 : 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 : 3459698 : return immed_wide_int_const (result, result_mode);
2206 : 3459889 : }
2207 : :
2208 : 23360334 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2209 : 424981 : && SCALAR_FLOAT_MODE_P (mode)
2210 : 422983 : && SCALAR_FLOAT_MODE_P (GET_MODE (op)))
2211 : : {
2212 : 422983 : REAL_VALUE_TYPE d = *CONST_DOUBLE_REAL_VALUE (op);
2213 : 422983 : switch (code)
2214 : : {
2215 : : case SQRT:
2216 : : return 0;
2217 : 350 : case ABS:
2218 : 350 : d = real_value_abs (&d);
2219 : 350 : break;
2220 : 15656 : case NEG:
2221 : 15656 : d = real_value_negate (&d);
2222 : 15656 : break;
2223 : 2276 : case FLOAT_TRUNCATE:
2224 : : /* Don't perform the operation if flag_signaling_nans is on
2225 : : and the operand is a signaling NaN. */
2226 : 2276 : 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 : 2276 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2231 : 2276 : && !exact_real_truncate (mode, &d))
2232 : 231 : return NULL_RTX;
2233 : 2045 : d = real_value_truncate (mode, d);
2234 : 2045 : break;
2235 : 398114 : case FLOAT_EXTEND:
2236 : : /* Don't perform the operation if flag_signaling_nans is on
2237 : : and the operand is a signaling NaN. */
2238 : 398114 : 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 : 398112 : 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 : 5985 : case NOT:
2253 : 5985 : {
2254 : 5985 : long tmp[4];
2255 : 5985 : int i;
2256 : :
2257 : 5985 : real_to_target (tmp, &d, GET_MODE (op));
2258 : 29925 : for (i = 0; i < 4; i++)
2259 : 23940 : tmp[i] = ~tmp[i];
2260 : 5985 : real_from_target (&d, tmp, mode);
2261 : 5985 : break;
2262 : : }
2263 : 0 : default:
2264 : 0 : gcc_unreachable ();
2265 : : }
2266 : 422148 : return const_double_from_real_value (d, mode);
2267 : : }
2268 : 1998 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2269 : 1998 : && SCALAR_FLOAT_MODE_P (GET_MODE (op))
2270 : 22939349 : && is_int_mode (mode, &result_mode))
2271 : : {
2272 : 1998 : unsigned int width = GET_MODE_PRECISION (result_mode);
2273 : 1998 : 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 : 1998 : REAL_VALUE_TYPE t;
2284 : 1998 : const REAL_VALUE_TYPE *x = CONST_DOUBLE_REAL_VALUE (op);
2285 : 1998 : 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 : 1998 : bool fail;
2289 : :
2290 : 1998 : switch (code)
2291 : : {
2292 : 1990 : 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 : 1990 : if (REAL_VALUE_ISNAN (*x))
2301 : 941 : return flag_trapping_math ? NULL_RTX : const0_rtx;
2302 : :
2303 : 1049 : if (REAL_VALUE_ISINF (*x) && flag_trapping_math)
2304 : : return NULL_RTX;
2305 : :
2306 : : /* Test against the signed upper bound. */
2307 : 103 : wmax = wi::max_value (width, SIGNED);
2308 : 103 : real_from_integer (&t, VOIDmode, wmax, SIGNED);
2309 : 103 : 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 : 100 : wmin = wi::min_value (width, SIGNED);
2315 : 100 : real_from_integer (&t, VOIDmode, wmin, SIGNED);
2316 : 100 : if (real_less (x, &t))
2317 : 8 : return immed_wide_int_const (wmin, mode);
2318 : :
2319 : 92 : 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 : 1998 : }
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 : 40467583 : simplify_context::simplify_byte_swapping_operation (rtx_code code,
2374 : : machine_mode mode,
2375 : : rtx op0, rtx op1)
2376 : : {
2377 : 40467583 : rtx tem;
2378 : :
2379 : : /* (op (bswap x) C1)) -> (bswap (op x C2)) with C2 swapped. */
2380 : 40467583 : if (GET_CODE (op0) == BSWAP && CONST_SCALAR_INT_P (op1))
2381 : : {
2382 : 503 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0),
2383 : : simplify_gen_unary (BSWAP, mode, op1, mode));
2384 : 503 : return simplify_gen_unary (BSWAP, mode, tem, mode);
2385 : : }
2386 : :
2387 : : /* (op (bswap x) (bswap y)) -> (bswap (op x y)). */
2388 : 40467080 : 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 : 50157984 : simplify_context::simplify_associative_operation (rtx_code code,
2405 : : machine_mode mode,
2406 : : rtx op0, rtx op1)
2407 : : {
2408 : 50157984 : 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 : 50157984 : if (++assoc_count >= max_assoc_count)
2418 : : return NULL_RTX;
2419 : :
2420 : : /* Linearize the operator to the left. */
2421 : 50150683 : if (GET_CODE (op1) == code)
2422 : : {
2423 : : /* "(a op b) op (c op d)" becomes "((a op b) op c) op d)". */
2424 : 24661 : if (GET_CODE (op0) == code)
2425 : : {
2426 : 5277 : tem = simplify_gen_binary (code, mode, op0, XEXP (op1, 0));
2427 : 5277 : 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 : 19384 : if (! swap_commutative_operands_p (op1, op0))
2432 : 19384 : return simplify_gen_binary (code, mode, op1, op0);
2433 : :
2434 : : std::swap (op0, op1);
2435 : : }
2436 : :
2437 : 50126022 : if (GET_CODE (op0) == code)
2438 : : {
2439 : : /* Canonicalize "(x op c) op y" as "(x op y) op c". */
2440 : 1626073 : if (swap_commutative_operands_p (XEXP (op0, 1), op1))
2441 : : {
2442 : 351062 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0), op1);
2443 : 351062 : 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 : 1275011 : tem = simplify_binary_operation (code, mode, XEXP (op0, 1), op1);
2448 : 1275011 : if (tem != 0)
2449 : 95971 : 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 : 1179040 : tem = simplify_binary_operation (code, mode, XEXP (op0, 0), op1);
2453 : 1179040 : if (tem != 0)
2454 : 58384 : 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 : 18624 : 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 : 6475 : mask_to_unsigned_comparison (int mask)
2492 : : {
2493 : 6475 : switch (mask)
2494 : : {
2495 : : case 8:
2496 : : return LTU;
2497 : 160 : case 4:
2498 : 160 : return GTU;
2499 : 2467 : case 2:
2500 : 2467 : return EQ;
2501 : :
2502 : 160 : case 10:
2503 : 160 : return LEU;
2504 : 160 : case 6:
2505 : 160 : return GEU;
2506 : :
2507 : 3368 : case 12:
2508 : 3368 : 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 : 861689 : relational_result (machine_mode mode, machine_mode cmp_mode, rtx res)
2605 : : {
2606 : 861689 : if (SCALAR_FLOAT_MODE_P (mode))
2607 : : {
2608 : 204 : if (res == const0_rtx)
2609 : 200 : 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 : 861485 : if (VECTOR_MODE_P (mode))
2618 : : {
2619 : 350 : if (res == const0_rtx)
2620 : 51 : return CONST0_RTX (mode);
2621 : : #ifdef VECTOR_STORE_FLAG_VALUE
2622 : 299 : rtx val = VECTOR_STORE_FLAG_VALUE (mode);
2623 : 299 : if (val == NULL_RTX)
2624 : : return NULL_RTX;
2625 : 299 : if (val == const1_rtx)
2626 : 0 : return CONST1_RTX (mode);
2627 : :
2628 : 299 : 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 : 861135 : 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 : 15422693 : simplify_context::simplify_logical_relational_operation (rtx_code code,
2653 : : machine_mode mode,
2654 : : rtx op0, rtx op1,
2655 : : bool invert0_p)
2656 : : {
2657 : 15422693 : if (!(COMPARISON_P (op0) && COMPARISON_P (op1)))
2658 : : return 0;
2659 : :
2660 : 21237 : if (!(rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2661 : 9739 : && rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))))
2662 : 2186 : return 0;
2663 : :
2664 : 9312 : if (side_effects_p (op0))
2665 : : return 0;
2666 : :
2667 : 9312 : enum rtx_code code0 = GET_CODE (op0);
2668 : 9312 : enum rtx_code code1 = GET_CODE (op1);
2669 : 9312 : machine_mode cmp_mode = GET_MODE (XEXP (op0, 0));
2670 : 9312 : 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 : 9312 : int all = 14;
2676 : 9312 : int mask0 = unsigned_comparison_to_mask (code0);
2677 : 9312 : int mask1 = unsigned_comparison_to_mask (code1);
2678 : 18624 : bool unsigned_p = (IN_RANGE (mask0 & 12, 4, 8)
2679 : 9312 : || 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 : 7979 : 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 : 8032 : if (invert0_p)
2699 : 4535 : mask0 = mask0 ^ all;
2700 : :
2701 : 8032 : int mask;
2702 : 8032 : if (code == AND)
2703 : 960 : mask = mask0 & mask1;
2704 : 7072 : else if (code == IOR)
2705 : 948 : mask = mask0 | mask1;
2706 : 6124 : else if (code == XOR)
2707 : 6124 : mask = mask0 ^ mask1;
2708 : : else
2709 : : return 0;
2710 : :
2711 : 8032 : if (mask == all)
2712 : 232 : return relational_result (mode, GET_MODE (op0), const_true_rtx);
2713 : :
2714 : 7800 : if (mask == 0)
2715 : 232 : return relational_result (mode, GET_MODE (op0), const0_rtx);
2716 : :
2717 : 7568 : if (unsigned_p)
2718 : 6475 : 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 : 7489 : op0 = XEXP (op1, 0);
2732 : 7489 : op1 = XEXP (op1, 1);
2733 : :
2734 : 7489 : 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 : 479821858 : simplify_context::simplify_binary_operation (rtx_code code, machine_mode mode,
2744 : : rtx op0, rtx op1)
2745 : : {
2746 : 479821858 : rtx trueop0, trueop1;
2747 : 479821858 : 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 : 479821858 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMPARE);
2754 : 479821858 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMM_COMPARE);
2755 : :
2756 : : /* Make sure the constant is second. */
2757 : 479821858 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
2758 : 479821858 : && swap_commutative_operands_p (op0, op1))
2759 : : std::swap (op0, op1);
2760 : :
2761 : 479821858 : trueop0 = avoid_constant_pool_reference (op0);
2762 : 479821858 : trueop1 = avoid_constant_pool_reference (op1);
2763 : :
2764 : 479821858 : tem = simplify_const_binary_operation (code, mode, trueop0, trueop1);
2765 : 479821858 : if (tem)
2766 : : return tem;
2767 : 449386882 : tem = simplify_binary_operation_1 (code, mode, op0, op1, trueop0, trueop1);
2768 : :
2769 : 449386882 : 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 : 386279899 : if (trueop0 != op0 || trueop1 != op1)
2775 : 578302 : 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 : 2295370 : simplify_context::simplify_binary_operation_series (rtx_code code,
2791 : : machine_mode mode,
2792 : : rtx op0, rtx op1)
2793 : : {
2794 : 2295370 : rtx base0, step0;
2795 : 2295370 : if (vec_duplicate_p (op0, &base0))
2796 : 63179 : step0 = const0_rtx;
2797 : 2232191 : else if (!vec_series_p (op0, &base0, &step0))
2798 : : return NULL_RTX;
2799 : :
2800 : 63671 : rtx base1, step1;
2801 : 63671 : if (vec_duplicate_p (op1, &base1))
2802 : 405 : step1 = const0_rtx;
2803 : 63266 : 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 : 2940 : scalar_mode inner_mode = GET_MODE_INNER (mode);
2810 : 2940 : rtx new_base = simplify_binary_operation (code, inner_mode, base0, base1);
2811 : 2940 : if (!new_base)
2812 : : return NULL_RTX;
2813 : :
2814 : 2671 : rtx new_step = simplify_binary_operation (code, inner_mode, step0, step1);
2815 : 2671 : if (!new_step)
2816 : : return NULL_RTX;
2817 : :
2818 : 2671 : 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 : 1526657 : simplify_context::simplify_distributive_operation (rtx_code code,
2828 : : machine_mode mode,
2829 : : rtx op0, rtx op1)
2830 : : {
2831 : 1526657 : enum rtx_code op = GET_CODE (op0);
2832 : 1526657 : gcc_assert (GET_CODE (op1) == op);
2833 : :
2834 : 1526657 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))
2835 : 1526657 : && ! side_effects_p (XEXP (op0, 1)))
2836 : 316724 : return simplify_gen_binary (op, mode,
2837 : : simplify_gen_binary (code, mode,
2838 : : XEXP (op0, 0),
2839 : : XEXP (op1, 0)),
2840 : 316724 : XEXP (op0, 1));
2841 : :
2842 : 1209933 : if (GET_RTX_CLASS (op) == RTX_COMM_ARITH)
2843 : : {
2844 : 1182596 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2845 : 1182596 : && ! side_effects_p (XEXP (op0, 0)))
2846 : 533048 : return simplify_gen_binary (op, mode,
2847 : : simplify_gen_binary (code, mode,
2848 : : XEXP (op0, 1),
2849 : : XEXP (op1, 1)),
2850 : 533048 : XEXP (op0, 0));
2851 : 649548 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 1))
2852 : 649548 : && ! side_effects_p (XEXP (op0, 0)))
2853 : 52 : return simplify_gen_binary (op, mode,
2854 : : simplify_gen_binary (code, mode,
2855 : : XEXP (op0, 1),
2856 : : XEXP (op1, 0)),
2857 : 52 : XEXP (op0, 0));
2858 : 649496 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 0))
2859 : 649496 : && ! side_effects_p (XEXP (op0, 1)))
2860 : 343548 : return simplify_gen_binary (op, mode,
2861 : : simplify_gen_binary (code, mode,
2862 : : XEXP (op0, 0),
2863 : : XEXP (op1, 1)),
2864 : 343548 : 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 : 143433 : reverse_rotate_by_imm_p (machine_mode mode, unsigned int left, rtx op1)
2879 : : {
2880 : 143433 : 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 : 111690 : optab binoptab = left ? rotl_optab : rotr_optab;
2887 : 46715 : optab re_binoptab = left ? rotr_optab : rotl_optab;
2888 : 111690 : enum insn_code icode = optab_handler (binoptab, mode);
2889 : 111690 : 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 : 111690 : 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 : 109190 : if ((icode == CODE_FOR_nothing)
2900 : 109190 : || (!insn_operand_matches (icode, 2, op1))
2901 : 545950 : || (IN_RANGE (INTVAL (op1),
2902 : : GET_MODE_UNIT_PRECISION (mode) / 2 + left,
2903 : : GET_MODE_UNIT_PRECISION (mode) - 1)))
2904 : 14393 : 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 : 530344298 : extract_ashift_operands_p (rtx x, rtx *shift_opnd, rtx *shift_amnt)
2916 : : {
2917 : 530344298 : if (GET_CODE (x) == ASHIFT)
2918 : : {
2919 : 13805073 : *shift_opnd = XEXP (x, 0);
2920 : 13805073 : *shift_amnt = XEXP (x, 1);
2921 : 13805073 : return true;
2922 : : }
2923 : 516539225 : if (GET_CODE (x) == PLUS && rtx_equal_p (XEXP (x, 0), XEXP (x, 1)))
2924 : : {
2925 : 12561 : *shift_opnd = XEXP (x, 0);
2926 : 12561 : *shift_amnt = CONST1_RTX (GET_MODE (x));
2927 : 12561 : return true;
2928 : : }
2929 : 516526664 : *shift_opnd = NULL_RTX;
2930 : 516526664 : *shift_amnt = NULL_RTX;
2931 : 516526664 : 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 : 267361650 : 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 : 267361650 : rtx opleft = op0;
2948 : 267361650 : rtx opright = op1;
2949 : 267361650 : 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 : 267361650 : bool ashift_op_p
2953 : 267361650 : = extract_ashift_operands_p (op1, &ashift_opnd, &ashift_amnt);
2954 : :
2955 : 267361650 : if (ashift_op_p
2956 : 265616007 : || GET_CODE (op1) == SUBREG)
2957 : : {
2958 : : opleft = op1;
2959 : : opright = op0;
2960 : : }
2961 : : else
2962 : : {
2963 : 262982648 : opright = op1;
2964 : 262982648 : opleft = op0;
2965 : 262982648 : ashift_op_p
2966 : 262982648 : = extract_ashift_operands_p (opleft, &ashift_opnd, &ashift_amnt);
2967 : : }
2968 : :
2969 : 13817634 : if (ashift_op_p && GET_CODE (opright) == LSHIFTRT
2970 : 265658823 : && rtx_equal_p (ashift_opnd, XEXP (opright, 0)))
2971 : : {
2972 : 9783 : rtx leftcst = unwrap_const_vec_duplicate (ashift_amnt);
2973 : 9783 : rtx rightcst = unwrap_const_vec_duplicate (XEXP (opright, 1));
2974 : :
2975 : 5965 : if (CONST_INT_P (leftcst) && CONST_INT_P (rightcst)
2976 : 15748 : && (INTVAL (leftcst) + INTVAL (rightcst)
2977 : 5965 : == GET_MODE_UNIT_PRECISION (mode)))
2978 : 5360 : 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 : 267356290 : scalar_int_mode int_mode, inner_mode;
2984 : :
2985 : 267356290 : if (GET_CODE (opleft) == SUBREG
2986 : 271129862 : && is_a <scalar_int_mode> (mode, &int_mode)
2987 : 3768212 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (opleft)),
2988 : : &inner_mode)
2989 : 3737934 : && GET_CODE (SUBREG_REG (opleft)) == ASHIFT
2990 : 108109 : && GET_CODE (opright) == LSHIFTRT
2991 : 1098 : && GET_CODE (XEXP (opright, 0)) == SUBREG
2992 : 251 : && known_eq (SUBREG_BYTE (opleft), SUBREG_BYTE (XEXP (opright, 0)))
2993 : 498 : && GET_MODE_SIZE (int_mode) < GET_MODE_SIZE (inner_mode)
2994 : 235 : && rtx_equal_p (XEXP (SUBREG_REG (opleft), 0),
2995 : 235 : SUBREG_REG (XEXP (opright, 0)))
2996 : 19 : && CONST_INT_P (XEXP (SUBREG_REG (opleft), 1))
2997 : 19 : && CONST_INT_P (XEXP (opright, 1))
2998 : 267356290 : && (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 : 42725915 : match_plus_neg_pattern (rtx op0, rtx op1, machine_mode mode)
3011 : : {
3012 : : /* Remove SUBREG from OP0 and OP1, if needed. */
3013 : 42725915 : if (GET_CODE (op0) == SUBREG
3014 : 7305784 : && GET_CODE (op1) == SUBREG
3015 : 283326 : && subreg_lowpart_p (op0)
3016 : 43007847 : && subreg_lowpart_p (op1))
3017 : : {
3018 : 281923 : op0 = XEXP (op0, 0);
3019 : 281923 : op1 = XEXP (op1, 0);
3020 : : }
3021 : :
3022 : : /* Check for the pattern (OP (plus (A - 1)) (neg A)). */
3023 : 42725915 : if (((GET_CODE (op1) == NEG
3024 : 3612 : && GET_CODE (op0) == PLUS
3025 : 2182 : && XEXP (op0, 1) == CONSTM1_RTX (mode))
3026 : 42725253 : || (GET_CODE (op0) == NEG
3027 : 77863 : && GET_CODE (op1) == PLUS
3028 : 0 : && XEXP (op1, 1) == CONSTM1_RTX (mode)))
3029 : 662 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3030 : 42725917 : && !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 : 80937527 : non_paradoxical_subreg_not_p (rtx op)
3040 : : {
3041 : 80937527 : return GET_CODE (op) == SUBREG
3042 : 8810597 : && !paradoxical_subreg_p (op)
3043 : 83880947 : && 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 : 40469996 : simplify_with_subreg_not (rtx_code binop, machine_mode mode, rtx op0, rtx op1)
3052 : : {
3053 : 40469996 : rtx opn = NULL_RTX;
3054 : 40469996 : if (non_paradoxical_subreg_not_p (op0))
3055 : : opn = op0;
3056 : 40467531 : else if (non_paradoxical_subreg_not_p (op1))
3057 : : opn = op1;
3058 : :
3059 : 2468 : if (opn == NULL_RTX)
3060 : : return NULL_RTX;
3061 : :
3062 : 4936 : rtx new_subreg = simplify_gen_subreg (mode,
3063 : : XEXP (SUBREG_REG (opn), 0),
3064 : 2468 : GET_MODE (SUBREG_REG (opn)),
3065 : 2468 : SUBREG_BYTE (opn));
3066 : :
3067 : 2468 : if (!new_subreg)
3068 : : return NULL_RTX;
3069 : :
3070 : 2413 : rtx new_not = simplify_gen_unary (NOT, mode, new_subreg, mode);
3071 : 2413 : if (opn == op0)
3072 : 2410 : 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 : 449386882 : 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 : 449386882 : rtx tem, reversed, elt0, elt1;
3089 : 449386882 : HOST_WIDE_INT val;
3090 : 449386882 : scalar_int_mode int_mode, inner_mode;
3091 : 449386882 : poly_int64 offset;
3092 : :
3093 : : /* Even if we can't compute a constant result,
3094 : : there are some cases worth simplifying. */
3095 : :
3096 : 449386882 : switch (code)
3097 : : {
3098 : 254396035 : 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 : 504872513 : if (!HONOR_SIGNED_ZEROS (mode) && !HONOR_SNANS (mode)
3104 : 504872501 : && 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 : 253080526 : if (GET_CODE (op0) == NEG)
3110 : 26173 : return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
3111 : 253054353 : else if (GET_CODE (op1) == NEG)
3112 : 6987 : return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
3113 : :
3114 : : /* (~a) + 1 -> -a */
3115 : 253047366 : if (INTEGRAL_MODE_P (mode)
3116 : 248223799 : && GET_CODE (op0) == NOT
3117 : 654211 : && trueop1 == const1_rtx)
3118 : 3519 : 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 : 253043847 : if ((GET_CODE (op0) == CONST
3127 : 253043847 : || GET_CODE (op0) == SYMBOL_REF
3128 : 250576952 : || GET_CODE (op0) == LABEL_REF)
3129 : 253043847 : && poly_int_rtx_p (op1, &offset))
3130 : 2465919 : return plus_constant (mode, op0, offset);
3131 : 250577928 : else if ((GET_CODE (op1) == CONST
3132 : 250577928 : || GET_CODE (op1) == SYMBOL_REF
3133 : 246912814 : || GET_CODE (op1) == LABEL_REF)
3134 : 250577928 : && 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 : 250577928 : if (is_a <scalar_int_mode> (mode, &int_mode))
3144 : : {
3145 : 243695891 : rtx lhs = op0, rhs = op1;
3146 : :
3147 : 243695891 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3148 : 243695891 : wide_int coeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3149 : :
3150 : 243695891 : 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 : 243695891 : else if (GET_CODE (lhs) == MULT
3156 : 6738191 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3157 : : {
3158 : 5616501 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3159 : 5616501 : lhs = XEXP (lhs, 0);
3160 : : }
3161 : 238079390 : else if (GET_CODE (lhs) == ASHIFT
3162 : 10674914 : && CONST_INT_P (XEXP (lhs, 1))
3163 : 10603607 : && INTVAL (XEXP (lhs, 1)) >= 0
3164 : 248682985 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3165 : : {
3166 : 10603595 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3167 : 21207190 : GET_MODE_PRECISION (int_mode));
3168 : 10603595 : lhs = XEXP (lhs, 0);
3169 : : }
3170 : :
3171 : 243695891 : 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 : 243695891 : else if (GET_CODE (rhs) == MULT
3177 : 276551 : && CONST_INT_P (XEXP (rhs, 1)))
3178 : : {
3179 : 172941 : coeff1 = rtx_mode_t (XEXP (rhs, 1), int_mode);
3180 : 172941 : rhs = XEXP (rhs, 0);
3181 : : }
3182 : 243522950 : else if (GET_CODE (rhs) == ASHIFT
3183 : 513292 : && CONST_INT_P (XEXP (rhs, 1))
3184 : 503557 : && INTVAL (XEXP (rhs, 1)) >= 0
3185 : 244026507 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3186 : : {
3187 : 503557 : coeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3188 : 1007114 : GET_MODE_PRECISION (int_mode));
3189 : 503557 : rhs = XEXP (rhs, 0);
3190 : : }
3191 : :
3192 : 243695891 : if (rtx_equal_p (lhs, rhs))
3193 : : {
3194 : 727514 : rtx orig = gen_rtx_PLUS (int_mode, op0, op1);
3195 : 727514 : rtx coeff;
3196 : 727514 : bool speed = optimize_function_for_speed_p (cfun);
3197 : :
3198 : 727514 : coeff = immed_wide_int_const (coeff0 + coeff1, int_mode);
3199 : :
3200 : 727514 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3201 : 727514 : return (set_src_cost (tem, int_mode, speed)
3202 : 727514 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3203 : : }
3204 : :
3205 : : /* Optimize (X - 1) * Y + Y to X * Y. */
3206 : 242968377 : lhs = op0;
3207 : 242968377 : rhs = op1;
3208 : 242968377 : if (GET_CODE (op0) == MULT)
3209 : : {
3210 : 6716337 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3211 : 273713 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx)
3212 : 6676293 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3213 : 37353 : && XEXP (XEXP (op0, 0), 1) == const1_rtx))
3214 : 6756381 : && rtx_equal_p (XEXP (op0, 1), op1))
3215 : 118 : lhs = XEXP (XEXP (op0, 0), 0);
3216 : 6716219 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3217 : 1600 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx)
3218 : 6716184 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3219 : 263 : && XEXP (XEXP (op0, 1), 1) == const1_rtx))
3220 : 6716254 : && rtx_equal_p (XEXP (op0, 0), op1))
3221 : 0 : lhs = XEXP (XEXP (op0, 1), 0);
3222 : : }
3223 : 236252040 : else if (GET_CODE (op1) == MULT)
3224 : : {
3225 : 128299 : if (((GET_CODE (XEXP (op1, 0)) == PLUS
3226 : 84 : && XEXP (XEXP (op1, 0), 1) == constm1_rtx)
3227 : 128297 : || (GET_CODE (XEXP (op1, 0)) == MINUS
3228 : 27 : && XEXP (XEXP (op1, 0), 1) == const1_rtx))
3229 : 128301 : && rtx_equal_p (XEXP (op1, 1), op0))
3230 : 0 : rhs = XEXP (XEXP (op1, 0), 0);
3231 : 128299 : else if (((GET_CODE (XEXP (op1, 1)) == PLUS
3232 : 45 : && XEXP (XEXP (op1, 1), 1) == constm1_rtx)
3233 : 128299 : || (GET_CODE (XEXP (op1, 1)) == MINUS
3234 : 0 : && XEXP (XEXP (op1, 1), 1) == const1_rtx))
3235 : 128299 : && rtx_equal_p (XEXP (op1, 0), op0))
3236 : 0 : rhs = XEXP (XEXP (op1, 1), 0);
3237 : : }
3238 : 242968377 : if (lhs != op0 || rhs != op1)
3239 : 118 : return simplify_gen_binary (MULT, int_mode, lhs, rhs);
3240 : 243695891 : }
3241 : :
3242 : : /* (plus (xor X C1) C2) is (xor X (C1^C2)) if C2 is signbit. */
3243 : 249850296 : if (CONST_SCALAR_INT_P (op1)
3244 : 193658497 : && GET_CODE (op0) == XOR
3245 : 22827 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3246 : 249864641 : && 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 : 249850175 : if (CONST_SCALAR_INT_P (op1)
3254 : 193658376 : && GET_CODE (op0) == XOR
3255 : 22706 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3256 : 14224 : && nonzero_bits (XEXP (op0, 0), mode) == 1
3257 : 191 : && 2 * (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) == 0
3258 : 249850178 : && 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 : 249850172 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3265 : 249847694 : && GET_CODE (op0) == MULT
3266 : 256931467 : && GET_CODE (XEXP (op0, 0)) == NEG)
3267 : : {
3268 : 5447 : rtx in1, in2;
3269 : :
3270 : 5447 : in1 = XEXP (XEXP (op0, 0), 0);
3271 : 5447 : in2 = XEXP (op0, 1);
3272 : 5447 : return simplify_gen_binary (MINUS, mode, op1,
3273 : : simplify_gen_binary (MULT, mode,
3274 : 5447 : 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 : 249844725 : if (COMPARISON_P (op0)
3281 : 1513538 : && ((STORE_FLAG_VALUE == -1 && trueop1 == const1_rtx)
3282 : 1513538 : || (STORE_FLAG_VALUE == 1 && trueop1 == constm1_rtx))
3283 : 249897048 : && (reversed = reversed_comparison (op0, mode)))
3284 : 52019 : return
3285 : 52019 : 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 : 249792706 : 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 : 249791236 : if (INTEGRAL_MODE_P (mode)
3299 : 244967718 : && (plus_minus_operand_p (op0)
3300 : 211259486 : || plus_minus_operand_p (op1))
3301 : 34701093 : && (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 : 215795012 : if (FLOAT_MODE_P (mode)
3307 : 4823518 : && flag_associative_math)
3308 : : {
3309 : 903939 : tem = simplify_associative_operation (code, mode, op0, op1);
3310 : 903939 : if (tem)
3311 : : return tem;
3312 : : }
3313 : :
3314 : : /* Handle vector series. */
3315 : 215781430 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3316 : : {
3317 : 1896509 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3318 : 1896509 : if (tem)
3319 : : return tem;
3320 : : }
3321 : : break;
3322 : :
3323 : : case COMPARE:
3324 : : break;
3325 : :
3326 : 42338612 : 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 : 42338612 : if (rtx_equal_p (trueop0, trueop1)
3331 : 222567 : && ! side_effects_p (op0)
3332 : 42559957 : && (!FLOAT_MODE_P (mode) || !HONOR_NANS (mode)))
3333 : 218961 : 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 : 42119651 : if (!HONOR_SIGNED_ZEROS (mode) && trueop0 == CONST0_RTX (mode))
3340 : 292757 : 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 : 41826894 : if (trueop0 == CONSTM1_RTX (mode)
3346 : 41826894 : && !contains_symbolic_reference_p (op1))
3347 : 379334 : 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 : 42146300 : if (!(HONOR_SIGNED_ZEROS (mode)
3353 : 698740 : && HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3354 : 41446382 : && !HONOR_SNANS (mode)
3355 : 82893906 : && 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 : 40479778 : if (is_a <scalar_int_mode> (mode, &int_mode))
3365 : : {
3366 : 39241931 : rtx lhs = op0, rhs = op1;
3367 : :
3368 : 39241931 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3369 : 39241931 : wide_int negcoeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3370 : :
3371 : 39241931 : if (GET_CODE (lhs) == NEG)
3372 : : {
3373 : 118900 : coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3374 : 118900 : lhs = XEXP (lhs, 0);
3375 : : }
3376 : 39123031 : else if (GET_CODE (lhs) == MULT
3377 : 188940 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3378 : : {
3379 : 78849 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3380 : 78849 : lhs = XEXP (lhs, 0);
3381 : : }
3382 : 39044182 : else if (GET_CODE (lhs) == ASHIFT
3383 : 323372 : && CONST_INT_P (XEXP (lhs, 1))
3384 : 320307 : && INTVAL (XEXP (lhs, 1)) >= 0
3385 : 39364468 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3386 : : {
3387 : 320286 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3388 : 640572 : GET_MODE_PRECISION (int_mode));
3389 : 320286 : lhs = XEXP (lhs, 0);
3390 : : }
3391 : :
3392 : 39241931 : if (GET_CODE (rhs) == NEG)
3393 : : {
3394 : 7288 : negcoeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3395 : 7288 : rhs = XEXP (rhs, 0);
3396 : : }
3397 : 39234643 : else if (GET_CODE (rhs) == MULT
3398 : 165981 : && CONST_INT_P (XEXP (rhs, 1)))
3399 : : {
3400 : 131628 : negcoeff1 = wi::neg (rtx_mode_t (XEXP (rhs, 1), int_mode));
3401 : 131628 : rhs = XEXP (rhs, 0);
3402 : : }
3403 : 39103015 : else if (GET_CODE (rhs) == ASHIFT
3404 : 390330 : && CONST_INT_P (XEXP (rhs, 1))
3405 : 389896 : && INTVAL (XEXP (rhs, 1)) >= 0
3406 : 39492911 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3407 : : {
3408 : 389896 : negcoeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3409 : 779792 : GET_MODE_PRECISION (int_mode));
3410 : 389896 : negcoeff1 = -negcoeff1;
3411 : 389896 : rhs = XEXP (rhs, 0);
3412 : : }
3413 : :
3414 : 39241931 : if (rtx_equal_p (lhs, rhs))
3415 : : {
3416 : 103039 : rtx orig = gen_rtx_MINUS (int_mode, op0, op1);
3417 : 103039 : rtx coeff;
3418 : 103039 : bool speed = optimize_function_for_speed_p (cfun);
3419 : :
3420 : 103039 : coeff = immed_wide_int_const (coeff0 + negcoeff1, int_mode);
3421 : :
3422 : 103039 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3423 : 103039 : return (set_src_cost (tem, int_mode, speed)
3424 : 103039 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3425 : : }
3426 : :
3427 : : /* Optimize (X + 1) * Y - Y to X * Y. */
3428 : 39138892 : lhs = op0;
3429 : 39138892 : if (GET_CODE (op0) == MULT)
3430 : : {
3431 : 188835 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3432 : 4496 : && XEXP (XEXP (op0, 0), 1) == const1_rtx)
3433 : 187464 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3434 : 2228 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx))
3435 : 190206 : && rtx_equal_p (XEXP (op0, 1), op1))
3436 : 2 : lhs = XEXP (XEXP (op0, 0), 0);
3437 : 188833 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3438 : 16 : && XEXP (XEXP (op0, 1), 1) == const1_rtx)
3439 : 188829 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3440 : 45 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx))
3441 : 188837 : && rtx_equal_p (XEXP (op0, 0), op1))
3442 : 0 : lhs = XEXP (XEXP (op0, 1), 0);
3443 : : }
3444 : 39138892 : if (lhs != op0)
3445 : 2 : return simplify_gen_binary (MULT, int_mode, lhs, op1);
3446 : 39241931 : }
3447 : :
3448 : : /* (a - (-b)) -> (a + b). True even for IEEE. */
3449 : 40376737 : if (GET_CODE (op1) == NEG)
3450 : 7250 : return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
3451 : :
3452 : : /* (-x - c) may be simplified as (-c - x). */
3453 : 40369487 : if (GET_CODE (op0) == NEG
3454 : 123066 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1)))
3455 : : {
3456 : 612 : tem = simplify_unary_operation (NEG, mode, op1, mode);
3457 : 612 : if (tem)
3458 : 612 : return simplify_gen_binary (MINUS, mode, tem, XEXP (op0, 0));
3459 : : }
3460 : :
3461 : 40368875 : if ((GET_CODE (op0) == CONST
3462 : 40368875 : || GET_CODE (op0) == SYMBOL_REF
3463 : 34981688 : || GET_CODE (op0) == LABEL_REF)
3464 : 40368875 : && poly_int_rtx_p (op1, &offset))
3465 : 50900 : return plus_constant (mode, op0, trunc_int_for_mode (-offset, mode));
3466 : :
3467 : : /* Don't let a relocatable value get a negative coeff. */
3468 : 40317975 : if (is_a <scalar_int_mode> (mode)
3469 : 39080159 : && poly_int_rtx_p (op1)
3470 : 47584572 : && GET_MODE (op0) != VOIDmode)
3471 : 7266597 : return simplify_gen_binary (PLUS, mode,
3472 : : op0,
3473 : 7266597 : neg_poly_int_rtx (mode, op1));
3474 : :
3475 : : /* (x - (x & y)) -> (x & ~y) */
3476 : 33051378 : if (INTEGRAL_MODE_P (mode) && GET_CODE (op1) == AND)
3477 : : {
3478 : 310517 : if (rtx_equal_p (op0, XEXP (op1, 0)))
3479 : : {
3480 : 498 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 1),
3481 : 249 : GET_MODE (XEXP (op1, 1)));
3482 : 249 : return simplify_gen_binary (AND, mode, op0, tem);
3483 : : }
3484 : 310268 : if (rtx_equal_p (op0, XEXP (op1, 1)))
3485 : : {
3486 : 2004 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 0),
3487 : 1002 : GET_MODE (XEXP (op1, 0)));
3488 : 1002 : return simplify_gen_binary (AND, mode, op0, tem);
3489 : : }
3490 : : }
3491 : :
3492 : : /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3493 : : by reversing the comparison code if valid. */
3494 : 33050127 : if (STORE_FLAG_VALUE == 1
3495 : 33050127 : && trueop0 == const1_rtx
3496 : 1055403 : && COMPARISON_P (op1)
3497 : 33130911 : && (reversed = reversed_comparison (op1, mode)))
3498 : : return reversed;
3499 : :
3500 : : /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A). */
3501 : 32969366 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3502 : 32967987 : && GET_CODE (op1) == MULT
3503 : 33234740 : && GET_CODE (XEXP (op1, 0)) == NEG)
3504 : : {
3505 : 165 : rtx in1, in2;
3506 : :
3507 : 165 : in1 = XEXP (XEXP (op1, 0), 0);
3508 : 165 : in2 = XEXP (op1, 1);
3509 : 165 : return simplify_gen_binary (PLUS, mode,
3510 : : simplify_gen_binary (MULT, mode,
3511 : : in1, in2),
3512 : 165 : op0);
3513 : : }
3514 : :
3515 : : /* Canonicalize (minus (neg A) (mult B C)) to
3516 : : (minus (mult (neg B) C) A). */
3517 : 32969201 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3518 : 32967822 : && GET_CODE (op1) == MULT
3519 : 33234410 : && GET_CODE (op0) == NEG)
3520 : : {
3521 : 655 : rtx in1, in2;
3522 : :
3523 : 655 : in1 = simplify_gen_unary (NEG, mode, XEXP (op1, 0), mode);
3524 : 655 : in2 = XEXP (op1, 1);
3525 : 655 : return simplify_gen_binary (MINUS, mode,
3526 : : simplify_gen_binary (MULT, mode,
3527 : : in1, in2),
3528 : 655 : XEXP (op0, 0));
3529 : : }
3530 : :
3531 : : /* If one of the operands is a PLUS or a MINUS, see if we can
3532 : : simplify this by the associative law. This will, for example,
3533 : : canonicalize (minus A (plus B C)) to (minus (minus A B) C).
3534 : : Don't use the associative law for floating point.
3535 : : The inaccuracy makes it nonassociative,
3536 : : and subtle programs can break if operations are associated. */
3537 : :
3538 : 32968546 : if (INTEGRAL_MODE_P (mode)
3539 : 32150228 : && (plus_minus_operand_p (op0)
3540 : 29740240 : || plus_minus_operand_p (op1))
3541 : 3646671 : && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3542 : : return tem;
3543 : :
3544 : : /* Handle vector series. */
3545 : 29453880 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3546 : : {
3547 : 398861 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3548 : 398861 : if (tem)
3549 : : return tem;
3550 : : }
3551 : : break;
3552 : :
3553 : 12189037 : case MULT:
3554 : 12189037 : if (trueop1 == constm1_rtx)
3555 : 32020 : return simplify_gen_unary (NEG, mode, op0, mode);
3556 : :
3557 : 12157017 : if (GET_CODE (op0) == NEG)
3558 : : {
3559 : 32593 : rtx temp = simplify_unary_operation (NEG, mode, op1, mode);
3560 : : /* If op1 is a MULT as well and simplify_unary_operation
3561 : : just moved the NEG to the second operand, simplify_gen_binary
3562 : : below could through simplify_associative_operation move
3563 : : the NEG around again and recurse endlessly. */
3564 : 32593 : if (temp
3565 : 1442 : && GET_CODE (op1) == MULT
3566 : 0 : && GET_CODE (temp) == MULT
3567 : 0 : && XEXP (op1, 0) == XEXP (temp, 0)
3568 : 0 : && GET_CODE (XEXP (temp, 1)) == NEG
3569 : 0 : && XEXP (op1, 1) == XEXP (XEXP (temp, 1), 0))
3570 : : temp = NULL_RTX;
3571 : : if (temp)
3572 : 1442 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), temp);
3573 : : }
3574 : 12155575 : if (GET_CODE (op1) == NEG)
3575 : : {
3576 : 953 : rtx temp = simplify_unary_operation (NEG, mode, op0, mode);
3577 : : /* If op0 is a MULT as well and simplify_unary_operation
3578 : : just moved the NEG to the second operand, simplify_gen_binary
3579 : : below could through simplify_associative_operation move
3580 : : the NEG around again and recurse endlessly. */
3581 : 953 : if (temp
3582 : 384 : && GET_CODE (op0) == MULT
3583 : 300 : && GET_CODE (temp) == MULT
3584 : 300 : && XEXP (op0, 0) == XEXP (temp, 0)
3585 : 6 : && GET_CODE (XEXP (temp, 1)) == NEG
3586 : 5 : && XEXP (op0, 1) == XEXP (XEXP (temp, 1), 0))
3587 : : temp = NULL_RTX;
3588 : : if (temp)
3589 : 379 : return simplify_gen_binary (MULT, mode, temp, XEXP (op1, 0));
3590 : : }
3591 : :
3592 : : /* Maybe simplify x * 0 to 0. The reduction is not valid if
3593 : : x is NaN, since x * 0 is then also NaN. Nor is it valid
3594 : : when the mode has signed zeros, since multiplying a negative
3595 : : number by 0 will give -0, not 0. */
3596 : 12155196 : if (!HONOR_NANS (mode)
3597 : 11196763 : && !HONOR_SIGNED_ZEROS (mode)
3598 : 11196367 : && trueop1 == CONST0_RTX (mode)
3599 : 12219897 : && ! side_effects_p (op0))
3600 : : return op1;
3601 : :
3602 : : /* In IEEE floating point, x*1 is not equivalent to x for
3603 : : signalling NaNs. */
3604 : 12091718 : if (!HONOR_SNANS (mode)
3605 : 12091718 : && trueop1 == CONST1_RTX (mode))
3606 : : return op0;
3607 : :
3608 : : /* Convert multiply by constant power of two into shift. */
3609 : 11538184 : if (mem_depth == 0 && CONST_SCALAR_INT_P (trueop1))
3610 : : {
3611 : 6253556 : val = wi::exact_log2 (rtx_mode_t (trueop1, mode));
3612 : 6253556 : if (val >= 0)
3613 : 2944296 : return simplify_gen_binary (ASHIFT, mode, op0,
3614 : 2944296 : gen_int_shift_amount (mode, val));
3615 : : }
3616 : :
3617 : : /* x*2 is x+x and x*(-1) is -x */
3618 : 8593888 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
3619 : 168744 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop1))
3620 : 168744 : && !DECIMAL_FLOAT_MODE_P (GET_MODE (trueop1))
3621 : 168460 : && GET_MODE (op0) == mode)
3622 : : {
3623 : 168460 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
3624 : :
3625 : 168460 : if (real_equal (d1, &dconst2))
3626 : 614 : return simplify_gen_binary (PLUS, mode, op0, copy_rtx (op0));
3627 : :
3628 : 167846 : if (!HONOR_SNANS (mode)
3629 : 167846 : && real_equal (d1, &dconstm1))
3630 : 24 : return simplify_gen_unary (NEG, mode, op0, mode);
3631 : : }
3632 : :
3633 : : /* Optimize -x * -x as x * x. */
3634 : 8593250 : if (FLOAT_MODE_P (mode)
3635 : 1375393 : && GET_CODE (op0) == NEG
3636 : 7848 : && GET_CODE (op1) == NEG
3637 : 0 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3638 : 0 : && !side_effects_p (XEXP (op0, 0)))
3639 : 0 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3640 : :
3641 : : /* Likewise, optimize abs(x) * abs(x) as x * x. */
3642 : 8593250 : if (SCALAR_FLOAT_MODE_P (mode)
3643 : 1092064 : && GET_CODE (op0) == ABS
3644 : 1340 : && GET_CODE (op1) == ABS
3645 : 0 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3646 : 8593250 : && !side_effects_p (XEXP (op0, 0)))
3647 : 0 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), XEXP (op1, 0));
3648 : :
3649 : : /* Reassociate multiplication, but for floating point MULTs
3650 : : only when the user specifies unsafe math optimizations. */
3651 : 8593250 : if (! FLOAT_MODE_P (mode)
3652 : 1375393 : || flag_unsafe_math_optimizations)
3653 : : {
3654 : 7635977 : tem = simplify_associative_operation (code, mode, op0, op1);
3655 : 7635977 : if (tem)
3656 : : return tem;
3657 : : }
3658 : : break;
3659 : :
3660 : 17415864 : case IOR:
3661 : 17415864 : if (trueop1 == CONST0_RTX (mode))
3662 : : return op0;
3663 : 16493802 : if (INTEGRAL_MODE_P (mode)
3664 : 16218843 : && trueop1 == CONSTM1_RTX (mode)
3665 : 9470 : && !side_effects_p (op0))
3666 : : return op1;
3667 : 16484332 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3668 : : return op0;
3669 : : /* A | (~A) -> -1 */
3670 : 73761 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
3671 : 16464955 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
3672 : 10 : && ! side_effects_p (op0)
3673 : 16464975 : && GET_MODE_CLASS (mode) != MODE_CC)
3674 : 10 : return CONSTM1_RTX (mode);
3675 : :
3676 : : /* IOR of two single bit bitfields extracted from the same object.
3677 : : Bitfields are represented as an AND based extraction */
3678 : 16464955 : if (GET_CODE (op0) == AND
3679 : 4702993 : && GET_CODE (op1) == AND
3680 : : /* Verify both AND operands are logical right shifts. */
3681 : 335920 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
3682 : 4936 : && GET_CODE (XEXP (op1, 0)) == LSHIFTRT
3683 : : /* Verify both bitfields are extracted from the same object. */
3684 : 54 : && XEXP (XEXP (op0, 0), 0) == XEXP (XEXP (op1, 0), 0)
3685 : : /* Verify both fields are a single bit (could be generalized). */
3686 : 54 : && XEXP (op0, 1) == CONST1_RTX (mode)
3687 : 0 : && XEXP (op1, 1) == CONST1_RTX (mode)
3688 : : /* Verify bit positions (for cases with variable bit position). */
3689 : 0 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
3690 : 0 : && CONST_INT_P (XEXP (XEXP (op1, 0), 1)))
3691 : : {
3692 : 0 : unsigned HOST_WIDE_INT bitpos1 = INTVAL (XEXP (XEXP (op0, 0), 1));
3693 : 0 : unsigned HOST_WIDE_INT bitpos2 = INTVAL (XEXP (XEXP (op1, 0), 1));
3694 : 0 : unsigned HOST_WIDE_INT mask
3695 : 0 : = (HOST_WIDE_INT_1U << bitpos1) | (HOST_WIDE_INT_1U << bitpos2);
3696 : :
3697 : 0 : rtx m = GEN_INT (mask);
3698 : 0 : rtx t = gen_rtx_AND (mode, XEXP (XEXP (op0, 0), 0), m);
3699 : 0 : t = gen_rtx_NE (mode, t, CONST0_RTX (mode));
3700 : 0 : return t;
3701 : : }
3702 : :
3703 : : /* IOR of multiple single bit bitfields extracted from the same object
3704 : : (building on previous case).
3705 : : First bitfield is represented as an AND based extraction, as done
3706 : : above. Second represented as NE based extraction, from
3707 : : output above. */
3708 : 16464955 : if (GET_CODE (op0) == AND
3709 : 4702993 : && GET_CODE (op1) == NE
3710 : : /* Verify AND operand is logical right shift. */
3711 : 4467 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
3712 : : /* Verify NE operand is an AND (based on output above). */
3713 : 86 : && GET_CODE (XEXP (op1, 0)) == AND
3714 : : /* Verify both bitfields are extracted from the same object. */
3715 : 0 : && XEXP (XEXP (op0, 0), 0) == XEXP (XEXP (op1, 0), 0)
3716 : : /* Verify masking is with a single bit and that we have a NE 0
3717 : : comparison for the other operand. */
3718 : 0 : && XEXP (op0, 1) == CONST1_RTX (mode)
3719 : 0 : && XEXP (op1, 1) == CONST0_RTX (mode)
3720 : : /* Verify bit position. */
3721 : 0 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1)))
3722 : : {
3723 : 0 : unsigned HOST_WIDE_INT bitpos1 = INTVAL (XEXP (XEXP (op0, 0), 1));
3724 : 0 : unsigned HOST_WIDE_INT mask
3725 : 0 : = (HOST_WIDE_INT_1U << bitpos1) | INTVAL (XEXP (XEXP (op1, 0), 1));
3726 : :
3727 : 0 : rtx m = GEN_INT (mask);
3728 : 0 : rtx t = gen_rtx_AND (mode, XEXP (XEXP (op0, 0), 0), m);
3729 : 0 : t = gen_rtx_NE (mode, t, CONST0_RTX (mode));
3730 : 0 : return t;
3731 : : }
3732 : :
3733 : : /* Convert (ior (plus (A - 1)) (neg A)) to -1. */
3734 : 16464955 : if (match_plus_neg_pattern (op0, op1, mode))
3735 : 0 : return CONSTM1_RTX (mode);
3736 : :
3737 : : /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
3738 : 16464955 : if (CONST_INT_P (op1)
3739 : 4495516 : && HWI_COMPUTABLE_MODE_P (mode)
3740 : 4434049 : && (nonzero_bits (op0, mode) & ~UINTVAL (op1)) == 0
3741 : 16830301 : && !side_effects_p (op0))
3742 : : return op1;
3743 : :
3744 : : /* Canonicalize (X & C1) | C2. */
3745 : 16099609 : if (GET_CODE (op0) == AND
3746 : 4693467 : && CONST_INT_P (trueop1)
3747 : 820746 : && CONST_INT_P (XEXP (op0, 1)))
3748 : : {
3749 : 665586 : HOST_WIDE_INT mask = GET_MODE_MASK (mode);
3750 : 665586 : HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
3751 : 665586 : HOST_WIDE_INT c2 = INTVAL (trueop1);
3752 : :
3753 : : /* If (C1&C2) == C1, then (X&C1)|C2 becomes C2. */
3754 : 665586 : if ((c1 & c2) == c1
3755 : 665586 : && !side_effects_p (XEXP (op0, 0)))
3756 : : return trueop1;
3757 : :
3758 : : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
3759 : 665566 : if (((c1|c2) & mask) == mask)
3760 : 89350 : return simplify_gen_binary (IOR, mode, XEXP (op0, 0), op1);
3761 : :
3762 : : /* If (C1|C2) has a single bit clear, then adjust C1 so that
3763 : : when split it'll match a single bit clear style insn.
3764 : :
3765 : : This could have been done with a target dependent splitter, but
3766 : : then every target with single bit manipulation insns would need
3767 : : to implement such splitters. */
3768 : 576216 : if (exact_log2 (~(c1 | c2)) >= 0)
3769 : : {
3770 : 102932 : rtx temp = gen_rtx_AND (mode, XEXP (op0, 0), GEN_INT (c1 | c2));
3771 : 102932 : temp = gen_rtx_IOR (mode, temp, trueop1);
3772 : 102932 : return temp;
3773 : : }
3774 : : }
3775 : :
3776 : : /* Convert (A & B) | A to A. */
3777 : 15907307 : if (GET_CODE (op0) == AND
3778 : 4501165 : && (rtx_equal_p (XEXP (op0, 0), op1)
3779 : 4501058 : || rtx_equal_p (XEXP (op0, 1), op1))
3780 : 3768 : && ! side_effects_p (XEXP (op0, 0))
3781 : 15911075 : && ! side_effects_p (XEXP (op0, 1)))
3782 : : return op1;
3783 : :
3784 : : /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3785 : : mode size to (rotate A CX). */
3786 : 15903539 : tem = simplify_rotate_op (op0, op1, mode);
3787 : 15903539 : if (tem)
3788 : : return tem;
3789 : :
3790 : : /* If OP0 is (ashiftrt (plus ...) C), it might actually be
3791 : : a (sign_extend (plus ...)). Then check if OP1 is a CONST_INT and
3792 : : the PLUS does not affect any of the bits in OP1: then we can do
3793 : : the IOR as a PLUS and we can associate. This is valid if OP1
3794 : : can be safely shifted left C bits. */
3795 : 15901014 : if (CONST_INT_P (trueop1) && GET_CODE (op0) == ASHIFTRT
3796 : 6728 : && GET_CODE (XEXP (op0, 0)) == PLUS
3797 : 141 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
3798 : 87 : && CONST_INT_P (XEXP (op0, 1))
3799 : 87 : && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
3800 : : {
3801 : 87 : int count = INTVAL (XEXP (op0, 1));
3802 : 87 : HOST_WIDE_INT mask = UINTVAL (trueop1) << count;
3803 : :
3804 : 87 : if (mask >> count == INTVAL (trueop1)
3805 : 80 : && trunc_int_for_mode (mask, mode) == mask
3806 : 154 : && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
3807 : 0 : return simplify_gen_binary (ASHIFTRT, mode,
3808 : : plus_constant (mode, XEXP (op0, 0),
3809 : 0 : mask),
3810 : : XEXP (op0, 1));
3811 : : }
3812 : :
3813 : : /* The following happens with bitfield merging.
3814 : : (X & C) | ((X | Y) & ~C) -> X | (Y & ~C) */
3815 : 15901014 : if (GET_CODE (op0) == AND
3816 : 4497397 : && GET_CODE (op1) == AND
3817 : 335920 : && CONST_INT_P (XEXP (op0, 1))
3818 : 167292 : && CONST_INT_P (XEXP (op1, 1))
3819 : 161648 : && (INTVAL (XEXP (op0, 1))
3820 : 161648 : == ~INTVAL (XEXP (op1, 1))))
3821 : : {
3822 : : /* The IOR may be on both sides. */
3823 : 39410 : rtx top0 = NULL_RTX, top1 = NULL_RTX;
3824 : 39410 : if (GET_CODE (XEXP (op1, 0)) == IOR)
3825 : : top0 = op0, top1 = op1;
3826 : 39356 : else if (GET_CODE (XEXP (op0, 0)) == IOR)
3827 : 3 : top0 = op1, top1 = op0;
3828 : 39410 : if (top0 && top1)
3829 : : {
3830 : : /* X may be on either side of the inner IOR. */
3831 : 57 : rtx tem = NULL_RTX;
3832 : 57 : if (rtx_equal_p (XEXP (top0, 0),
3833 : 57 : XEXP (XEXP (top1, 0), 0)))
3834 : 43 : tem = XEXP (XEXP (top1, 0), 1);
3835 : 14 : else if (rtx_equal_p (XEXP (top0, 0),
3836 : 14 : XEXP (XEXP (top1, 0), 1)))
3837 : 5 : tem = XEXP (XEXP (top1, 0), 0);
3838 : 48 : if (tem)
3839 : 48 : return simplify_gen_binary (IOR, mode, XEXP (top0, 0),
3840 : : simplify_gen_binary
3841 : 48 : (AND, mode, tem, XEXP (top1, 1)));
3842 : : }
3843 : : }
3844 : :
3845 : : /* Convert (ior (and A C) (and B C)) into (and (ior A B) C). */
3846 : 15900966 : if (GET_CODE (op0) == GET_CODE (op1)
3847 : 3840407 : && (GET_CODE (op0) == AND
3848 : : || GET_CODE (op0) == IOR
3849 : 3840407 : || GET_CODE (op0) == LSHIFTRT
3850 : 3503021 : || GET_CODE (op0) == ASHIFTRT
3851 : 3502927 : || GET_CODE (op0) == ASHIFT
3852 : 3477262 : || GET_CODE (op0) == ROTATE
3853 : 3477262 : || GET_CODE (op0) == ROTATERT))
3854 : : {
3855 : 363145 : tem = simplify_distributive_operation (code, mode, op0, op1);
3856 : 363145 : if (tem)
3857 : : return tem;
3858 : : }
3859 : :
3860 : : /* Convert (ior (and (not A) B) A) into A | B. */
3861 : 15817293 : if (GET_CODE (op0) == AND
3862 : 4413816 : && GET_CODE (XEXP (op0, 0)) == NOT
3863 : 15967887 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1))
3864 : 3367 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
3865 : :
3866 : 15813926 : tem = simplify_with_subreg_not (code, mode, op0, op1);
3867 : 15813926 : if (tem)
3868 : : return tem;
3869 : :
3870 : 15813921 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
3871 : 15813921 : if (tem)
3872 : : return tem;
3873 : :
3874 : 15813891 : tem = simplify_associative_operation (code, mode, op0, op1);
3875 : 15813891 : if (tem)
3876 : : return tem;
3877 : :
3878 : 15412840 : tem = simplify_logical_relational_operation (code, mode, op0, op1);
3879 : 15412840 : if (tem)
3880 : : return tem;
3881 : : break;
3882 : :
3883 : 1956281 : case XOR:
3884 : 1956281 : if (trueop1 == CONST0_RTX (mode))
3885 : : return op0;
3886 : 1884000 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
3887 : 23784 : return simplify_gen_unary (NOT, mode, op0, mode);
3888 : 1860216 : if (rtx_equal_p (trueop0, trueop1)
3889 : 2476 : && ! side_effects_p (op0)
3890 : 1862692 : && GET_MODE_CLASS (mode) != MODE_CC)
3891 : 2476 : return CONST0_RTX (mode);
3892 : :
3893 : : /* Canonicalize XOR of the most significant bit to PLUS. */
3894 : 1857740 : if (CONST_SCALAR_INT_P (op1)
3895 : 1857740 : && mode_signbit_p (mode, op1))
3896 : 41406 : return simplify_gen_binary (PLUS, mode, op0, op1);
3897 : : /* (xor (plus X C1) C2) is (xor X (C1^C2)) if C1 is signbit. */
3898 : 1816334 : if (CONST_SCALAR_INT_P (op1)
3899 : 688621 : && GET_CODE (op0) == PLUS
3900 : 2493 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3901 : 1817920 : && mode_signbit_p (mode, XEXP (op0, 1)))
3902 : 189 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3903 : : simplify_gen_binary (XOR, mode, op1,
3904 : 189 : XEXP (op0, 1)));
3905 : :
3906 : : /* If we are XORing two things that have no bits in common,
3907 : : convert them into an IOR. This helps to detect rotation encoded
3908 : : using those methods and possibly other simplifications. */
3909 : :
3910 : 1816145 : if (HWI_COMPUTABLE_MODE_P (mode)
3911 : 1532863 : && (nonzero_bits (op0, mode)
3912 : 1532863 : & nonzero_bits (op1, mode)) == 0)
3913 : 12351 : return (simplify_gen_binary (IOR, mode, op0, op1));
3914 : :
3915 : : /* Convert (xor (plus (A - 1)) (neg A)) to -1. */
3916 : 1803794 : if (match_plus_neg_pattern (op0, op1, mode))
3917 : 0 : return CONSTM1_RTX (mode);
3918 : :
3919 : : /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
3920 : : Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
3921 : : (NOT y). */
3922 : 1803794 : {
3923 : 1803794 : int num_negated = 0;
3924 : :
3925 : 1803794 : if (GET_CODE (op0) == NOT)
3926 : 1027 : num_negated++, op0 = XEXP (op0, 0);
3927 : 1803794 : if (GET_CODE (op1) == NOT)
3928 : 0 : num_negated++, op1 = XEXP (op1, 0);
3929 : :
3930 : 0 : if (num_negated == 2)
3931 : 0 : return simplify_gen_binary (XOR, mode, op0, op1);
3932 : 1803794 : else if (num_negated == 1)
3933 : 1027 : return simplify_gen_unary (NOT, mode,
3934 : : simplify_gen_binary (XOR, mode, op0, op1),
3935 : 1027 : mode);
3936 : : }
3937 : :
3938 : : /* Convert (xor (and A B) B) to (and (not A) B). The latter may
3939 : : correspond to a machine insn or result in further simplifications
3940 : : if B is a constant. */
3941 : :
3942 : 1802767 : if (GET_CODE (op0) == AND
3943 : 176218 : && rtx_equal_p (XEXP (op0, 1), op1)
3944 : 1837154 : && ! side_effects_p (op1))
3945 : 34387 : return simplify_gen_binary (AND, mode,
3946 : : simplify_gen_unary (NOT, mode,
3947 : : XEXP (op0, 0), mode),
3948 : 34387 : op1);
3949 : :
3950 : 1768380 : else if (GET_CODE (op0) == AND
3951 : 141831 : && rtx_equal_p (XEXP (op0, 0), op1)
3952 : 1769722 : && ! side_effects_p (op1))
3953 : 1342 : return simplify_gen_binary (AND, mode,
3954 : : simplify_gen_unary (NOT, mode,
3955 : : XEXP (op0, 1), mode),
3956 : 1342 : op1);
3957 : :
3958 : : /* Given (xor (ior (xor A B) C) D), where B, C and D are
3959 : : constants, simplify to (xor (ior A C) (B&~C)^D), canceling
3960 : : out bits inverted twice and not set by C. Similarly, given
3961 : : (xor (and (xor A B) C) D), simplify without inverting C in
3962 : : the xor operand: (xor (and A C) (B&C)^D).
3963 : : */
3964 : 1767038 : else if ((GET_CODE (op0) == IOR || GET_CODE (op0) == AND)
3965 : 162804 : && GET_CODE (XEXP (op0, 0)) == XOR
3966 : 5925 : && CONST_INT_P (op1)
3967 : 83 : && CONST_INT_P (XEXP (op0, 1))
3968 : 38 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1)))
3969 : : {
3970 : 38 : enum rtx_code op = GET_CODE (op0);
3971 : 38 : rtx a = XEXP (XEXP (op0, 0), 0);
3972 : 38 : rtx b = XEXP (XEXP (op0, 0), 1);
3973 : 38 : rtx c = XEXP (op0, 1);
3974 : 38 : rtx d = op1;
3975 : 38 : HOST_WIDE_INT bval = INTVAL (b);
3976 : 38 : HOST_WIDE_INT cval = INTVAL (c);
3977 : 38 : HOST_WIDE_INT dval = INTVAL (d);
3978 : 38 : HOST_WIDE_INT xcval;
3979 : :
3980 : 38 : if (op == IOR)
3981 : 8 : xcval = ~cval;
3982 : : else
3983 : : xcval = cval;
3984 : :
3985 : 38 : return simplify_gen_binary (XOR, mode,
3986 : : simplify_gen_binary (op, mode, a, c),
3987 : 38 : gen_int_mode ((bval & xcval) ^ dval,
3988 : : mode));
3989 : : }
3990 : :
3991 : : /* Given (xor (and A B) C), using P^Q == (~P&Q) | (~Q&P),
3992 : : we can transform like this:
3993 : : (A&B)^C == ~(A&B)&C | ~C&(A&B)
3994 : : == (~A|~B)&C | ~C&(A&B) * DeMorgan's Law
3995 : : == ~A&C | ~B&C | A&(~C&B) * Distribute and re-order
3996 : : Attempt a few simplifications when B and C are both constants. */
3997 : 1767000 : if (GET_CODE (op0) == AND
3998 : 140459 : && CONST_INT_P (op1)
3999 : 12920 : && CONST_INT_P (XEXP (op0, 1)))
4000 : : {
4001 : 11015 : rtx a = XEXP (op0, 0);
4002 : 11015 : rtx b = XEXP (op0, 1);
4003 : 11015 : rtx c = op1;
4004 : 11015 : HOST_WIDE_INT bval = INTVAL (b);
4005 : 11015 : HOST_WIDE_INT cval = INTVAL (c);
4006 : :
4007 : : /* Instead of computing ~A&C, we compute its negated value,
4008 : : ~(A|~C). If it yields -1, ~A&C is zero, so we can
4009 : : optimize for sure. If it does not simplify, we still try
4010 : : to compute ~A&C below, but since that always allocates
4011 : : RTL, we don't try that before committing to returning a
4012 : : simplified expression. */
4013 : 11015 : rtx n_na_c = simplify_binary_operation (IOR, mode, a,
4014 : : GEN_INT (~cval));
4015 : :
4016 : 11015 : if ((~cval & bval) == 0)
4017 : : {
4018 : 437 : rtx na_c = NULL_RTX;
4019 : 437 : if (n_na_c)
4020 : 0 : na_c = simplify_gen_unary (NOT, mode, n_na_c, mode);
4021 : : else
4022 : : {
4023 : : /* If ~A does not simplify, don't bother: we don't
4024 : : want to simplify 2 operations into 3, and if na_c
4025 : : were to simplify with na, n_na_c would have
4026 : : simplified as well. */
4027 : 437 : rtx na = simplify_unary_operation (NOT, mode, a, mode);
4028 : 437 : if (na)
4029 : 0 : na_c = simplify_gen_binary (AND, mode, na, c);
4030 : : }
4031 : :
4032 : : /* Try to simplify ~A&C | ~B&C. */
4033 : 0 : if (na_c != NULL_RTX)
4034 : 0 : return simplify_gen_binary (IOR, mode, na_c,
4035 : 0 : gen_int_mode (~bval & cval, mode));
4036 : : }
4037 : : else
4038 : : {
4039 : : /* If ~A&C is zero, simplify A&(~C&B) | ~B&C. */
4040 : 10578 : if (n_na_c == CONSTM1_RTX (mode))
4041 : : {
4042 : 0 : rtx a_nc_b = simplify_gen_binary (AND, mode, a,
4043 : 0 : gen_int_mode (~cval & bval,
4044 : : mode));
4045 : 0 : return simplify_gen_binary (IOR, mode, a_nc_b,
4046 : 0 : gen_int_mode (~bval & cval,
4047 : : mode));
4048 : : }
4049 : : }
4050 : : }
4051 : :
4052 : : /* If we have (xor (and (xor A B) C) A) with C a constant we can instead
4053 : : do (ior (and A ~C) (and B C)) which is a machine instruction on some
4054 : : machines, and also has shorter instruction path length. */
4055 : 1767000 : if (GET_CODE (op0) == AND
4056 : 140459 : && GET_CODE (XEXP (op0, 0)) == XOR
4057 : 5424 : && CONST_INT_P (XEXP (op0, 1))
4058 : 1769154 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), trueop1))
4059 : : {
4060 : 7 : rtx a = trueop1;
4061 : 7 : rtx b = XEXP (XEXP (op0, 0), 1);
4062 : 7 : rtx c = XEXP (op0, 1);
4063 : 7 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
4064 : 7 : rtx a_nc = simplify_gen_binary (AND, mode, a, nc);
4065 : 7 : rtx bc = simplify_gen_binary (AND, mode, b, c);
4066 : 7 : return simplify_gen_binary (IOR, mode, a_nc, bc);
4067 : : }
4068 : : /* Similarly, (xor (and (xor A B) C) B) as (ior (and A C) (and B ~C)) */
4069 : 1766993 : else if (GET_CODE (op0) == AND
4070 : 140452 : && GET_CODE (XEXP (op0, 0)) == XOR
4071 : 5417 : && CONST_INT_P (XEXP (op0, 1))
4072 : 1769140 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), trueop1))
4073 : : {
4074 : 8 : rtx a = XEXP (XEXP (op0, 0), 0);
4075 : 8 : rtx b = trueop1;
4076 : 8 : rtx c = XEXP (op0, 1);
4077 : 8 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
4078 : 8 : rtx b_nc = simplify_gen_binary (AND, mode, b, nc);
4079 : 8 : rtx ac = simplify_gen_binary (AND, mode, a, c);
4080 : 8 : return simplify_gen_binary (IOR, mode, ac, b_nc);
4081 : : }
4082 : :
4083 : : /* (xor (comparison foo bar) (const_int 1)) can become the reversed
4084 : : comparison if STORE_FLAG_VALUE is 1. */
4085 : 1766985 : if (STORE_FLAG_VALUE == 1
4086 : 1766985 : && trueop1 == const1_rtx
4087 : 255222 : && COMPARISON_P (op0)
4088 : 1774997 : && (reversed = reversed_comparison (op0, mode)))
4089 : : return reversed;
4090 : :
4091 : : /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4092 : : is (lt foo (const_int 0)), so we can perform the above
4093 : : simplification if STORE_FLAG_VALUE is 1. */
4094 : :
4095 : 1758981 : if (is_a <scalar_int_mode> (mode, &int_mode)
4096 : : && STORE_FLAG_VALUE == 1
4097 : 1479890 : && trueop1 == const1_rtx
4098 : 247218 : && GET_CODE (op0) == LSHIFTRT
4099 : 35714 : && CONST_INT_P (XEXP (op0, 1))
4100 : 35714 : && INTVAL (XEXP (op0, 1)) == GET_MODE_PRECISION (int_mode) - 1)
4101 : 34816 : return gen_rtx_GE (int_mode, XEXP (op0, 0), const0_rtx);
4102 : :
4103 : : /* (xor (comparison foo bar) (const_int sign-bit))
4104 : : when STORE_FLAG_VALUE is the sign bit. */
4105 : 1724165 : if (is_a <scalar_int_mode> (mode, &int_mode)
4106 : 1445074 : && val_signbit_p (int_mode, STORE_FLAG_VALUE)
4107 : 0 : && trueop1 == const_true_rtx
4108 : 0 : && COMPARISON_P (op0)
4109 : 0 : && (reversed = reversed_comparison (op0, int_mode)))
4110 : : return reversed;
4111 : :
4112 : : /* Convert (xor (and A C) (and B C)) into (and (xor A B) C). */
4113 : 1724165 : if (GET_CODE (op0) == GET_CODE (op1)
4114 : 528897 : && (GET_CODE (op0) == AND
4115 : 528897 : || GET_CODE (op0) == LSHIFTRT
4116 : 466429 : || GET_CODE (op0) == ASHIFTRT
4117 : 466378 : || GET_CODE (op0) == ASHIFT
4118 : 466262 : || GET_CODE (op0) == ROTATE
4119 : 466154 : || GET_CODE (op0) == ROTATERT))
4120 : : {
4121 : 63277 : tem = simplify_distributive_operation (code, mode, op0, op1);
4122 : 63277 : if (tem)
4123 : : return tem;
4124 : : }
4125 : :
4126 : : /* Convert (xor (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4127 : : mode size to (rotate A CX). */
4128 : 1665405 : tem = simplify_rotate_op (op0, op1, mode);
4129 : 1665405 : if (tem)
4130 : : return tem;
4131 : :
4132 : : /* Convert (xor (and (not A) B) A) into A | B. */
4133 : 1664025 : if (GET_CODE (op0) == AND
4134 : 81975 : && GET_CODE (XEXP (op0, 0)) == NOT
4135 : 1668663 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1))
4136 : 1 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4137 : :
4138 : : /* Convert (xor (and (rotate (~1) A) B) (ashift 1 A))
4139 : : into B | (1 << A). */
4140 : 1664024 : if (SHIFT_COUNT_TRUNCATED
4141 : : && GET_CODE (op0) == AND
4142 : : && GET_CODE (XEXP (op0, 0)) == ROTATE
4143 : : && CONST_INT_P (XEXP (XEXP (op0, 0), 0))
4144 : : && INTVAL (XEXP (XEXP (op0, 0), 0)) == -2
4145 : : && GET_CODE (op1) == ASHIFT
4146 : : && CONST_INT_P (XEXP (op1, 0))
4147 : : && INTVAL (XEXP (op1, 0)) == 1
4148 : : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), XEXP (op1, 1))
4149 : : && !side_effects_p (XEXP (op1, 1)))
4150 : : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4151 : :
4152 : 1664024 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4153 : 1664024 : if (tem)
4154 : : return tem;
4155 : :
4156 : 1664024 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4157 : 1664024 : if (tem)
4158 : : return tem;
4159 : :
4160 : 1664024 : tem = simplify_associative_operation (code, mode, op0, op1);
4161 : 1664024 : if (tem)
4162 : : return tem;
4163 : : break;
4164 : :
4165 : 25611052 : case AND:
4166 : 25611052 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
4167 : : return trueop1;
4168 : 25327506 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
4169 : : return op0;
4170 : 24937478 : if (HWI_COMPUTABLE_MODE_P (mode))
4171 : : {
4172 : : /* When WORD_REGISTER_OPERATIONS is true, we need to know the
4173 : : nonzero bits in WORD_MODE rather than MODE. */
4174 : 21941620 : scalar_int_mode tmode = as_a <scalar_int_mode> (mode);
4175 : 21941620 : if (WORD_REGISTER_OPERATIONS
4176 : : && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
4177 : : tmode = word_mode;
4178 : 21941620 : HOST_WIDE_INT nzop0 = nonzero_bits (trueop0, tmode);
4179 : 21941620 : HOST_WIDE_INT nzop1;
4180 : 21941620 : if (CONST_INT_P (trueop1))
4181 : : {
4182 : 18722465 : HOST_WIDE_INT val1 = INTVAL (trueop1);
4183 : : /* If we are turning off bits already known off in OP0, we need
4184 : : not do an AND. */
4185 : 18722465 : if ((nzop0 & ~val1) == 0)
4186 : 472990 : return op0;
4187 : :
4188 : : /* Canonicalize (and (subreg (lshiftrt X shift)) mask) into
4189 : : (and (lshiftrt (subreg X) shift) mask).
4190 : :
4191 : : Keeps shift and AND in the same mode, improving recognition.
4192 : : Only applied when subreg is a lowpart, shift is valid,
4193 : : and no precision is lost. */
4194 : 6332655 : if (SUBREG_P (op0) && subreg_lowpart_p (op0)
4195 : 6316453 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
4196 : : /* simplify_subreg asserts the object being accessed is not
4197 : : VOIDmode or BLKmode. We may have a REG_EQUAL note which
4198 : : is not simplified and the source operand is a constant,
4199 : : and thus VOIDmode. Guard against that. */
4200 : 151298 : && GET_MODE (XEXP (XEXP (op0, 0), 0)) != VOIDmode
4201 : 151205 : && GET_MODE (XEXP (XEXP (op0, 0), 0)) != BLKmode
4202 : 151205 : && !CONST_INT_P (XEXP (XEXP (op0, 0), 0))
4203 : 151205 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
4204 : 122620 : && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
4205 : 122620 : && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT
4206 : 18472368 : && ((INTVAL (XEXP (XEXP (op0, 0), 1))
4207 : 122619 : + floor_log2 (val1))
4208 : 18349749 : < GET_MODE_PRECISION (as_a <scalar_int_mode> (mode))))
4209 : : {
4210 : 36513 : tem = XEXP (XEXP (op0, 0), 0);
4211 : 36513 : if (SUBREG_P (tem))
4212 : : {
4213 : 600 : if (subreg_lowpart_p (tem))
4214 : 600 : tem = SUBREG_REG (tem);
4215 : : else
4216 : : tem = NULL_RTX;
4217 : : }
4218 : 600 : if (tem != NULL_RTX)
4219 : : {
4220 : 36513 : offset = subreg_lowpart_offset (mode, GET_MODE (tem));
4221 : 36513 : tem = simplify_gen_subreg (mode, tem, GET_MODE (tem),
4222 : 36513 : offset);
4223 : 36513 : if (tem)
4224 : : {
4225 : 36513 : unsigned shiftamt = INTVAL (XEXP (XEXP (op0, 0), 1));
4226 : 36513 : rtx shiftamtrtx = gen_int_shift_amount (mode,
4227 : 36513 : shiftamt);
4228 : 36513 : op0 = simplify_gen_binary (LSHIFTRT, mode, tem,
4229 : : shiftamtrtx);
4230 : 36513 : return simplify_gen_binary (AND, mode, op0, op1);
4231 : : }
4232 : : }
4233 : : }
4234 : : }
4235 : 21532391 : nzop1 = nonzero_bits (trueop1, mode);
4236 : : /* If we are clearing all the nonzero bits, the result is zero. */
4237 : 21532391 : if ((nzop1 & nzop0) == 0
4238 : 21532391 : && !side_effects_p (op0) && !side_effects_p (op1))
4239 : 63761 : return CONST0_RTX (mode);
4240 : : }
4241 : 24467810 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0)
4242 : 24467810 : && GET_MODE_CLASS (mode) != MODE_CC)
4243 : : return op0;
4244 : : /* A & (~A) -> 0 */
4245 : 664997 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
4246 : 24457212 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
4247 : 4000 : && ! side_effects_p (op0)
4248 : 24465166 : && GET_MODE_CLASS (mode) != MODE_CC)
4249 : 4000 : return CONST0_RTX (mode);
4250 : :
4251 : : /* Convert (and (plus (A - 1)) (neg A)) to 0. */
4252 : 24457166 : if (match_plus_neg_pattern (op0, op1, mode))
4253 : 2 : return CONST0_RTX (mode);
4254 : :
4255 : : /* Transform (and (extend X) C) into (zero_extend (and X C)) if
4256 : : there are no nonzero bits of C outside of X's mode. */
4257 : 48914328 : if ((GET_CODE (op0) == SIGN_EXTEND
4258 : 24457164 : || GET_CODE (op0) == ZERO_EXTEND)
4259 : 100910 : && CONST_SCALAR_INT_P (trueop1)
4260 : 86323 : && is_a <scalar_int_mode> (mode, &int_mode)
4261 : 86323 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
4262 : 24543487 : && (wi::mask (GET_MODE_PRECISION (inner_mode), true,
4263 : 86323 : GET_MODE_PRECISION (int_mode))
4264 : 24543487 : & rtx_mode_t (trueop1, mode)) == 0)
4265 : : {
4266 : 84009 : machine_mode imode = GET_MODE (XEXP (op0, 0));
4267 : 84009 : tem = immed_wide_int_const (rtx_mode_t (trueop1, mode), imode);
4268 : 84009 : tem = simplify_gen_binary (AND, imode, XEXP (op0, 0), tem);
4269 : 84009 : return simplify_gen_unary (ZERO_EXTEND, mode, tem, imode);
4270 : : }
4271 : :
4272 : : /* Transform (and (truncate X) C) into (truncate (and X C)). This way
4273 : : we might be able to further simplify the AND with X and potentially
4274 : : remove the truncation altogether. */
4275 : 24373155 : if (GET_CODE (op0) == TRUNCATE && CONST_INT_P (trueop1))
4276 : : {
4277 : 6 : rtx x = XEXP (op0, 0);
4278 : 6 : machine_mode xmode = GET_MODE (x);
4279 : 6 : tem = simplify_gen_binary (AND, xmode, x,
4280 : 6 : gen_int_mode (INTVAL (trueop1), xmode));
4281 : 6 : return simplify_gen_unary (TRUNCATE, mode, tem, xmode);
4282 : : }
4283 : :
4284 : : /* Canonicalize (A | C1) & C2 as (A & C2) | (C1 & C2). */
4285 : 24373149 : if (GET_CODE (op0) == IOR
4286 : 1589765 : && CONST_INT_P (trueop1)
4287 : 255011 : && CONST_INT_P (XEXP (op0, 1)))
4288 : : {
4289 : 155696 : HOST_WIDE_INT tmp = INTVAL (trueop1) & INTVAL (XEXP (op0, 1));
4290 : 155696 : return simplify_gen_binary (IOR, mode,
4291 : : simplify_gen_binary (AND, mode,
4292 : : XEXP (op0, 0), op1),
4293 : 155696 : gen_int_mode (tmp, mode));
4294 : : }
4295 : :
4296 : : /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
4297 : : insn (and may simplify more). */
4298 : 24217453 : if (GET_CODE (op0) == XOR
4299 : 127851 : && rtx_equal_p (XEXP (op0, 0), op1)
4300 : 24218851 : && ! side_effects_p (op1))
4301 : 1398 : return simplify_gen_binary (AND, mode,
4302 : : simplify_gen_unary (NOT, mode,
4303 : : XEXP (op0, 1), mode),
4304 : 1398 : op1);
4305 : :
4306 : 24216055 : if (GET_CODE (op0) == XOR
4307 : 126453 : && rtx_equal_p (XEXP (op0, 1), op1)
4308 : 24219681 : && ! side_effects_p (op1))
4309 : 3626 : return simplify_gen_binary (AND, mode,
4310 : : simplify_gen_unary (NOT, mode,
4311 : : XEXP (op0, 0), mode),
4312 : 3626 : op1);
4313 : :
4314 : : /* Similarly for (~(A ^ B)) & A. */
4315 : 24212429 : if (GET_CODE (op0) == NOT
4316 : 661043 : && GET_CODE (XEXP (op0, 0)) == XOR
4317 : 3460 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4318 : 24212483 : && ! side_effects_p (op1))
4319 : 54 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4320 : :
4321 : 24212375 : if (GET_CODE (op0) == NOT
4322 : 660989 : && GET_CODE (XEXP (op0, 0)) == XOR
4323 : 3406 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4324 : 24212412 : && ! side_effects_p (op1))
4325 : 37 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4326 : :
4327 : : /* Convert (A | B) & A to A. */
4328 : 24212338 : if (GET_CODE (op0) == IOR
4329 : 1434069 : && (rtx_equal_p (XEXP (op0, 0), op1)
4330 : 1433521 : || rtx_equal_p (XEXP (op0, 1), op1))
4331 : 743 : && ! side_effects_p (XEXP (op0, 0))
4332 : 24213081 : && ! side_effects_p (XEXP (op0, 1)))
4333 : : return op1;
4334 : :
4335 : : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
4336 : : ((A & N) + B) & M -> (A + B) & M
4337 : : Similarly if (N & M) == 0,
4338 : : ((A | N) + B) & M -> (A + B) & M
4339 : : and for - instead of + and/or ^ instead of |.
4340 : : Also, if (N & M) == 0, then
4341 : : (A +- N) & M -> A & M. */
4342 : 24211595 : if (CONST_INT_P (trueop1)
4343 : 18122953 : && HWI_COMPUTABLE_MODE_P (mode)
4344 : 18086480 : && ~UINTVAL (trueop1)
4345 : 18086480 : && (UINTVAL (trueop1) & (UINTVAL (trueop1) + 1)) == 0
4346 : 35544102 : && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS))
4347 : : {
4348 : 1014710 : rtx pmop[2];
4349 : 1014710 : int which;
4350 : :
4351 : 1014710 : pmop[0] = XEXP (op0, 0);
4352 : 1014710 : pmop[1] = XEXP (op0, 1);
4353 : :
4354 : 1014710 : if (CONST_INT_P (pmop[1])
4355 : 551523 : && (UINTVAL (pmop[1]) & UINTVAL (trueop1)) == 0)
4356 : 168529 : return simplify_gen_binary (AND, mode, pmop[0], op1);
4357 : :
4358 : 2563185 : for (which = 0; which < 2; which++)
4359 : : {
4360 : 1708790 : tem = pmop[which];
4361 : 1708790 : switch (GET_CODE (tem))
4362 : : {
4363 : 12471 : case AND:
4364 : 12471 : if (CONST_INT_P (XEXP (tem, 1))
4365 : 10977 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1))
4366 : : == UINTVAL (trueop1))
4367 : 8075 : pmop[which] = XEXP (tem, 0);
4368 : : break;
4369 : 1751 : case IOR:
4370 : 1751 : case XOR:
4371 : 1751 : if (CONST_INT_P (XEXP (tem, 1))
4372 : 741 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1)) == 0)
4373 : 139 : pmop[which] = XEXP (tem, 0);
4374 : : break;
4375 : : default:
4376 : : break;
4377 : : }
4378 : : }
4379 : :
4380 : 854395 : if (pmop[0] != XEXP (op0, 0) || pmop[1] != XEXP (op0, 1))
4381 : : {
4382 : 8214 : tem = simplify_gen_binary (GET_CODE (op0), mode,
4383 : : pmop[0], pmop[1]);
4384 : 8214 : return simplify_gen_binary (code, mode, tem, op1);
4385 : : }
4386 : : }
4387 : :
4388 : : /* (and X (ior (not X) Y) -> (and X Y) */
4389 : 24043066 : if (GET_CODE (op1) == IOR
4390 : 1100664 : && GET_CODE (XEXP (op1, 0)) == NOT
4391 : 24048481 : && rtx_equal_p (op0, XEXP (XEXP (op1, 0), 0)))
4392 : 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 1));
4393 : :
4394 : : /* (and (ior (not X) Y) X) -> (and X Y) */
4395 : 24043066 : if (GET_CODE (op0) == IOR
4396 : 1433326 : && GET_CODE (XEXP (op0, 0)) == NOT
4397 : 24093202 : && rtx_equal_p (op1, XEXP (XEXP (op0, 0), 0)))
4398 : 6 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 1));
4399 : :
4400 : : /* (and X (ior Y (not X)) -> (and X Y) */
4401 : 24043060 : if (GET_CODE (op1) == IOR
4402 : 1100664 : && GET_CODE (XEXP (op1, 1)) == NOT
4403 : 24043325 : && rtx_equal_p (op0, XEXP (XEXP (op1, 1), 0)))
4404 : 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 0));
4405 : :
4406 : : /* (and (ior Y (not X)) X) -> (and X Y) */
4407 : 24043060 : if (GET_CODE (op0) == IOR
4408 : 1433320 : && GET_CODE (XEXP (op0, 1)) == NOT
4409 : 24051519 : && rtx_equal_p (op1, XEXP (XEXP (op0, 1), 0)))
4410 : 6 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 0));
4411 : :
4412 : : /* (and (ior/xor (X Y) (not Y)) -> X & ~Y */
4413 : 24043054 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4414 : 1556141 : && GET_CODE (op1) == NOT
4415 : 24154939 : && rtx_equal_p (XEXP (op1, 0), XEXP (op0, 1)))
4416 : 23 : return simplify_gen_binary (AND, mode, XEXP (op0, 0),
4417 : : simplify_gen_unary (NOT, mode,
4418 : : XEXP (op1, 0),
4419 : 23 : mode));
4420 : : /* (and (ior/xor (Y X) (not Y)) -> X & ~Y */
4421 : 24043031 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4422 : 1556118 : && GET_CODE (op1) == NOT
4423 : 24154893 : && rtx_equal_p (XEXP (op1, 0), XEXP (op0, 0)))
4424 : 4 : return simplify_gen_binary (AND, mode, XEXP (op0, 1),
4425 : : simplify_gen_unary (NOT, mode,
4426 : : XEXP (op1, 0),
4427 : 4 : mode));
4428 : :
4429 : : /* Convert (and (ior A C) (ior B C)) into (ior (and A B) C). */
4430 : 24043027 : if (GET_CODE (op0) == GET_CODE (op1)
4431 : 2295510 : && (GET_CODE (op0) == AND
4432 : : || GET_CODE (op0) == IOR
4433 : 2295510 : || GET_CODE (op0) == LSHIFTRT
4434 : 1195540 : || GET_CODE (op0) == ASHIFTRT
4435 : 1195426 : || GET_CODE (op0) == ASHIFT
4436 : 1195275 : || GET_CODE (op0) == ROTATE
4437 : 1195275 : || GET_CODE (op0) == ROTATERT))
4438 : : {
4439 : 1100235 : tem = simplify_distributive_operation (code, mode, op0, op1);
4440 : 1100235 : if (tem)
4441 : : return tem;
4442 : : }
4443 : :
4444 : : /* (and:v4si
4445 : : (ashiftrt:v4si A 16)
4446 : : (const_vector: 0xffff x4))
4447 : : is just (lshiftrt:v4si A 16). */
4448 : 22992088 : if (VECTOR_MODE_P (mode) && GET_CODE (op0) == ASHIFTRT
4449 : 4216 : && (CONST_INT_P (XEXP (op0, 1))
4450 : 1850 : || (GET_CODE (XEXP (op0, 1)) == CONST_VECTOR
4451 : 92 : && const_vec_duplicate_p (XEXP (op0, 1))
4452 : 0 : && CONST_INT_P (XVECEXP (XEXP (op0, 1), 0, 0))))
4453 : 2366 : && GET_CODE (op1) == CONST_VECTOR
4454 : 22992104 : && const_vec_duplicate_p (op1)
4455 : 22992146 : && CONST_INT_P (XVECEXP (op1, 0, 0)))
4456 : : {
4457 : 112 : unsigned HOST_WIDE_INT shift_count
4458 : : = (CONST_INT_P (XEXP (op0, 1))
4459 : 56 : ? UINTVAL (XEXP (op0, 1))
4460 : 0 : : UINTVAL (XVECEXP (XEXP (op0, 1), 0, 0)));
4461 : 56 : unsigned HOST_WIDE_INT inner_prec
4462 : 112 : = GET_MODE_PRECISION (GET_MODE_INNER (mode));
4463 : :
4464 : : /* Avoid UD shift count. */
4465 : 56 : if (shift_count < inner_prec
4466 : 56 : && (UINTVAL (XVECEXP (op1, 0, 0))
4467 : 56 : == (HOST_WIDE_INT_1U << (inner_prec - shift_count)) - 1))
4468 : 42 : return simplify_gen_binary (LSHIFTRT, mode, XEXP (op0, 0), XEXP (op0, 1));
4469 : : }
4470 : :
4471 : 22992046 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4472 : 22992046 : if (tem)
4473 : : return tem;
4474 : :
4475 : 22989638 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4476 : 22989638 : if (tem)
4477 : : return tem;
4478 : :
4479 : 22989165 : tem = simplify_associative_operation (code, mode, op0, op1);
4480 : 22989165 : if (tem)
4481 : : return tem;
4482 : : break;
4483 : :
4484 : 955434 : case UDIV:
4485 : : /* 0/x is 0 (or x&0 if x has side-effects). */
4486 : 955434 : if (trueop0 == CONST0_RTX (mode)
4487 : 339 : && !cfun->can_throw_non_call_exceptions)
4488 : : {
4489 : 339 : if (side_effects_p (op1))
4490 : 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4491 : : return trueop0;
4492 : : }
4493 : : /* x/1 is x. */
4494 : 955095 : if (trueop1 == CONST1_RTX (mode))
4495 : : {
4496 : 241673 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4497 : 241673 : if (tem)
4498 : : return tem;
4499 : : }
4500 : : /* Convert divide by power of two into shift. */
4501 : 713422 : if (CONST_INT_P (trueop1)
4502 : 1055094 : && (val = exact_log2 (UINTVAL (trueop1))) > 0)
4503 : 341672 : return simplify_gen_binary (LSHIFTRT, mode, op0,
4504 : 341672 : gen_int_shift_amount (mode, val));
4505 : : break;
4506 : :
4507 : 1166997 : case DIV:
4508 : : /* Handle floating point and integers separately. */
4509 : 1166997 : if (SCALAR_FLOAT_MODE_P (mode))
4510 : : {
4511 : : /* Maybe change 0.0 / x to 0.0. This transformation isn't
4512 : : safe for modes with NaNs, since 0.0 / 0.0 will then be
4513 : : NaN rather than 0.0. Nor is it safe for modes with signed
4514 : : zeros, since dividing 0 by a negative number gives -0.0 */
4515 : 330654 : if (trueop0 == CONST0_RTX (mode)
4516 : 2914 : && !HONOR_NANS (mode)
4517 : 14 : && !HONOR_SIGNED_ZEROS (mode)
4518 : 330668 : && ! side_effects_p (op1))
4519 : : return op0;
4520 : : /* x/1.0 is x. */
4521 : 330640 : if (trueop1 == CONST1_RTX (mode)
4522 : 330640 : && !HONOR_SNANS (mode))
4523 : : return op0;
4524 : :
4525 : 330635 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
4526 : 27470 : && trueop1 != CONST0_RTX (mode))
4527 : : {
4528 : 21283 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
4529 : :
4530 : : /* x/-1.0 is -x. */
4531 : 21283 : if (real_equal (d1, &dconstm1)
4532 : 21283 : && !HONOR_SNANS (mode))
4533 : 0 : return simplify_gen_unary (NEG, mode, op0, mode);
4534 : :
4535 : : /* Change FP division by a constant into multiplication.
4536 : : Only do this with -freciprocal-math. */
4537 : 21283 : if (flag_reciprocal_math
4538 : 21283 : && !real_equal (d1, &dconst0))
4539 : : {
4540 : 7 : REAL_VALUE_TYPE d;
4541 : 7 : real_arithmetic (&d, RDIV_EXPR, &dconst1, d1);
4542 : 7 : tem = const_double_from_real_value (d, mode);
4543 : 7 : return simplify_gen_binary (MULT, mode, op0, tem);
4544 : : }
4545 : : }
4546 : : }
4547 : 836343 : else if (SCALAR_INT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
4548 : : {
4549 : : /* 0/x is 0 (or x&0 if x has side-effects). */
4550 : 815051 : if (trueop0 == CONST0_RTX (mode)
4551 : 644 : && !cfun->can_throw_non_call_exceptions)
4552 : : {
4553 : 567 : if (side_effects_p (op1))
4554 : 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4555 : : return trueop0;
4556 : : }
4557 : : /* x/1 is x. */
4558 : 814484 : if (trueop1 == CONST1_RTX (mode))
4559 : : {
4560 : 299 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4561 : 299 : if (tem)
4562 : : return tem;
4563 : : }
4564 : : /* x/-1 is -x. */
4565 : 814185 : if (trueop1 == CONSTM1_RTX (mode))
4566 : : {
4567 : 215 : rtx x = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4568 : 215 : if (x)
4569 : 215 : return simplify_gen_unary (NEG, mode, x, mode);
4570 : : }
4571 : : }
4572 : : break;
4573 : :
4574 : 936801 : case UMOD:
4575 : : /* 0%x is 0 (or x&0 if x has side-effects). */
4576 : 936801 : if (trueop0 == CONST0_RTX (mode))
4577 : : {
4578 : 817 : if (side_effects_p (op1))
4579 : 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4580 : : return trueop0;
4581 : : }
4582 : : /* x%1 is 0 (of x&0 if x has side-effects). */
4583 : 935984 : if (trueop1 == CONST1_RTX (mode))
4584 : : {
4585 : 277118 : if (side_effects_p (op0))
4586 : 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4587 : 277118 : return CONST0_RTX (mode);
4588 : : }
4589 : : /* Implement modulus by power of two as AND. */
4590 : 658866 : if (CONST_INT_P (trueop1)
4591 : 975540 : && exact_log2 (UINTVAL (trueop1)) > 0)
4592 : 316674 : return simplify_gen_binary (AND, mode, op0,
4593 : 316674 : gen_int_mode (UINTVAL (trueop1) - 1,
4594 : : mode));
4595 : : break;
4596 : :
4597 : 340939 : case MOD:
4598 : : /* 0%x is 0 (or x&0 if x has side-effects). */
4599 : 340939 : if (trueop0 == CONST0_RTX (mode))
4600 : : {
4601 : 645 : if (side_effects_p (op1))
4602 : 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4603 : : return trueop0;
4604 : : }
4605 : : /* x%1 and x%-1 is 0 (or x&0 if x has side-effects). */
4606 : 340294 : if (trueop1 == CONST1_RTX (mode) || trueop1 == constm1_rtx)
4607 : : {
4608 : 412 : if (side_effects_p (op0))
4609 : 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4610 : 412 : return CONST0_RTX (mode);
4611 : : }
4612 : : break;
4613 : :
4614 : 135620 : case ROTATERT:
4615 : 135620 : case ROTATE:
4616 : 135620 : if (trueop1 == CONST0_RTX (mode))
4617 : : return op0;
4618 : : /* Canonicalize rotates by constant amount. If the condition of
4619 : : reversing direction is met, then reverse the direction. */
4620 : : #if defined(HAVE_rotate) && defined(HAVE_rotatert)
4621 : 135530 : if (reverse_rotate_by_imm_p (mode, (code == ROTATE), trueop1))
4622 : : {
4623 : 11414 : int new_amount = GET_MODE_UNIT_PRECISION (mode) - INTVAL (trueop1);
4624 : 11414 : rtx new_amount_rtx = gen_int_shift_amount (mode, new_amount);
4625 : 11958 : return simplify_gen_binary (code == ROTATE ? ROTATERT : ROTATE,
4626 : : mode, op0, new_amount_rtx);
4627 : : }
4628 : : #endif
4629 : : /* ROTATE/ROTATERT:HI (X:HI, 8) is BSWAP:HI (X). Other combinations
4630 : : such as SImode with a count of 16 do not correspond to RTL BSWAP
4631 : : semantics. */
4632 : 124116 : tem = unwrap_const_vec_duplicate (trueop1);
4633 : 124116 : if (GET_MODE_UNIT_BITSIZE (mode) == (2 * BITS_PER_UNIT)
4634 : 124116 : && CONST_INT_P (tem) && INTVAL (tem) == BITS_PER_UNIT)
4635 : 599 : return simplify_gen_unary (BSWAP, mode, op0, mode);
4636 : :
4637 : : /* FALLTHRU */
4638 : 5099922 : case ASHIFTRT:
4639 : 5099922 : if (trueop1 == CONST0_RTX (mode))
4640 : : return op0;
4641 : 5098116 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4642 : : return op0;
4643 : : /* Rotating ~0 always results in ~0. */
4644 : 5097958 : if (CONST_INT_P (trueop0)
4645 : 14833 : && HWI_COMPUTABLE_MODE_P (mode)
4646 : 14789 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
4647 : 5097958 : && ! side_effects_p (op1))
4648 : : return op0;
4649 : :
4650 : 31245856 : canonicalize_shift:
4651 : : /* Given:
4652 : : scalar modes M1, M2
4653 : : scalar constants c1, c2
4654 : : size (M2) > size (M1)
4655 : : c1 == size (M2) - size (M1)
4656 : : optimize:
4657 : : ([a|l]shiftrt:M1 (subreg:M1 (lshiftrt:M2 (reg:M2) (const_int <c1>))
4658 : : <low_part>)
4659 : : (const_int <c2>))
4660 : : to:
4661 : : (subreg:M1 ([a|l]shiftrt:M2 (reg:M2) (const_int <c1 + c2>))
4662 : : <low_part>). */
4663 : 31245856 : if ((code == ASHIFTRT || code == LSHIFTRT)
4664 : 11837921 : && is_a <scalar_int_mode> (mode, &int_mode)
4665 : 11058260 : && SUBREG_P (op0)
4666 : 1023693 : && CONST_INT_P (op1)
4667 : 1022669 : && GET_CODE (SUBREG_REG (op0)) == LSHIFTRT
4668 : 23279 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
4669 : : &inner_mode)
4670 : 23279 : && CONST_INT_P (XEXP (SUBREG_REG (op0), 1))
4671 : 46252 : && GET_MODE_BITSIZE (inner_mode) > GET_MODE_BITSIZE (int_mode)
4672 : 23126 : && (INTVAL (XEXP (SUBREG_REG (op0), 1))
4673 : 46252 : == GET_MODE_BITSIZE (inner_mode) - GET_MODE_BITSIZE (int_mode))
4674 : 31268699 : && subreg_lowpart_p (op0))
4675 : : {
4676 : 22843 : rtx tmp = gen_int_shift_amount
4677 : 22843 : (inner_mode, INTVAL (XEXP (SUBREG_REG (op0), 1)) + INTVAL (op1));
4678 : :
4679 : : /* Combine would usually zero out the value when combining two
4680 : : local shifts and the range becomes larger or equal to the mode.
4681 : : However since we fold away one of the shifts here combine won't
4682 : : see it so we should immediately zero the result if it's out of
4683 : : range. */
4684 : 22843 : if (code == LSHIFTRT
4685 : 42118 : && INTVAL (tmp) >= GET_MODE_BITSIZE (inner_mode))
4686 : 0 : tmp = const0_rtx;
4687 : : else
4688 : 22843 : tmp = simplify_gen_binary (code,
4689 : : inner_mode,
4690 : 22843 : XEXP (SUBREG_REG (op0), 0),
4691 : : tmp);
4692 : :
4693 : 22843 : return lowpart_subreg (int_mode, tmp, inner_mode);
4694 : : }
4695 : :
4696 : 31223013 : if (SHIFT_COUNT_TRUNCATED && CONST_INT_P (op1))
4697 : : {
4698 : : val = INTVAL (op1) & (GET_MODE_UNIT_PRECISION (mode) - 1);
4699 : : if (val != INTVAL (op1))
4700 : : return simplify_gen_binary (code, mode, op0,
4701 : : gen_int_shift_amount (mode, val));
4702 : : }
4703 : :
4704 : : /* Simplify:
4705 : :
4706 : : (code:M1
4707 : : (subreg:M1
4708 : : ([al]shiftrt:M2
4709 : : (subreg:M2
4710 : : (ashift:M1 X C1))
4711 : : C2))
4712 : : C3)
4713 : :
4714 : : to:
4715 : :
4716 : : (code:M1
4717 : : ([al]shiftrt:M1
4718 : : (ashift:M1 X C1+N)
4719 : : C2+N)
4720 : : C3)
4721 : :
4722 : : where M1 is N bits wider than M2. Optimizing the (subreg:M1 ...)
4723 : : directly would be arithmetically correct, but restricting the
4724 : : simplification to shifts by constants is more conservative,
4725 : : since it is more likely to lead to further simplifications. */
4726 : 31223013 : if (is_a<scalar_int_mode> (mode, &int_mode)
4727 : 5428153 : && paradoxical_subreg_p (op0)
4728 : 4968703 : && is_a<scalar_int_mode> (GET_MODE (SUBREG_REG (op0)), &inner_mode)
4729 : 4968617 : && (GET_CODE (SUBREG_REG (op0)) == ASHIFTRT
4730 : 4968617 : || GET_CODE (SUBREG_REG (op0)) == LSHIFTRT)
4731 : 142167 : && CONST_INT_P (op1))
4732 : : {
4733 : 142167 : auto xcode = GET_CODE (SUBREG_REG (op0));
4734 : 142167 : rtx xop0 = XEXP (SUBREG_REG (op0), 0);
4735 : 142167 : rtx xop1 = XEXP (SUBREG_REG (op0), 1);
4736 : 142167 : if (SUBREG_P (xop0)
4737 : 8976 : && GET_MODE (SUBREG_REG (xop0)) == mode
4738 : 8948 : && GET_CODE (SUBREG_REG (xop0)) == ASHIFT
4739 : 605 : && CONST_INT_P (xop1)
4740 : 142772 : && UINTVAL (xop1) < GET_MODE_PRECISION (inner_mode))
4741 : : {
4742 : 605 : rtx yop0 = XEXP (SUBREG_REG (xop0), 0);
4743 : 605 : rtx yop1 = XEXP (SUBREG_REG (xop0), 1);
4744 : 605 : if (CONST_INT_P (yop1)
4745 : 605 : && UINTVAL (yop1) < GET_MODE_PRECISION (inner_mode))
4746 : : {
4747 : 1210 : auto bias = (GET_MODE_BITSIZE (int_mode)
4748 : 605 : - GET_MODE_BITSIZE (inner_mode));
4749 : 605 : tem = simplify_gen_binary (ASHIFT, mode, yop0,
4750 : 605 : GEN_INT (INTVAL (yop1) + bias));
4751 : 605 : tem = simplify_gen_binary (xcode, mode, tem,
4752 : 605 : GEN_INT (INTVAL (xop1) + bias));
4753 : 605 : return simplify_gen_binary (code, mode, tem, op1);
4754 : : }
4755 : : }
4756 : : }
4757 : : break;
4758 : :
4759 : 0 : case SS_ASHIFT:
4760 : 0 : if (CONST_INT_P (trueop0)
4761 : 0 : && HWI_COMPUTABLE_MODE_P (mode)
4762 : 0 : && (UINTVAL (trueop0) == (GET_MODE_MASK (mode) >> 1)
4763 : 0 : || mode_signbit_p (mode, trueop0))
4764 : 0 : && ! side_effects_p (op1))
4765 : : return op0;
4766 : 0 : goto simplify_ashift;
4767 : :
4768 : 0 : case US_ASHIFT:
4769 : 0 : if (CONST_INT_P (trueop0)
4770 : 0 : && HWI_COMPUTABLE_MODE_P (mode)
4771 : 0 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
4772 : 0 : && ! side_effects_p (op1))
4773 : : return op0;
4774 : : /* FALLTHRU */
4775 : :
4776 : 19728116 : case ASHIFT:
4777 : 19728116 : simplify_ashift:
4778 : 19728116 : if (trueop1 == CONST0_RTX (mode))
4779 : : return op0;
4780 : 19561245 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4781 : : return op0;
4782 : 19531601 : if (mem_depth
4783 : 247168 : && code == ASHIFT
4784 : 247168 : && CONST_INT_P (trueop1)
4785 : 247160 : && is_a <scalar_int_mode> (mode, &int_mode)
4786 : 19778749 : && IN_RANGE (UINTVAL (trueop1),
4787 : : 1, GET_MODE_PRECISION (int_mode) - 1))
4788 : : {
4789 : 247148 : auto c = (wi::one (GET_MODE_PRECISION (int_mode))
4790 : 247148 : << UINTVAL (trueop1));
4791 : 247148 : rtx new_op1 = immed_wide_int_const (c, int_mode);
4792 : 247148 : return simplify_gen_binary (MULT, int_mode, op0, new_op1);
4793 : 247148 : }
4794 : 19284453 : goto canonicalize_shift;
4795 : :
4796 : 8685576 : case LSHIFTRT:
4797 : 8685576 : if (trueop1 == CONST0_RTX (mode))
4798 : : return op0;
4799 : 6864845 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4800 : : return op0;
4801 : : /* Optimize (lshiftrt (clz X) C) as (eq X 0). */
4802 : 6863445 : if (GET_CODE (op0) == CLZ
4803 : 0 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
4804 : 0 : && CONST_INT_P (trueop1)
4805 : : && STORE_FLAG_VALUE == 1
4806 : 6863445 : && INTVAL (trueop1) < GET_MODE_UNIT_PRECISION (mode))
4807 : : {
4808 : 0 : unsigned HOST_WIDE_INT zero_val = 0;
4809 : :
4810 : 0 : if (CLZ_DEFINED_VALUE_AT_ZERO (inner_mode, zero_val)
4811 : 0 : && zero_val == GET_MODE_PRECISION (inner_mode)
4812 : 0 : && INTVAL (trueop1) == exact_log2 (zero_val))
4813 : 0 : return simplify_gen_relational (EQ, mode, inner_mode,
4814 : 0 : XEXP (op0, 0), const0_rtx);
4815 : : }
4816 : 6863445 : goto canonicalize_shift;
4817 : :
4818 : 225860 : case SMIN:
4819 : 225860 : if (HWI_COMPUTABLE_MODE_P (mode)
4820 : 205625 : && mode_signbit_p (mode, trueop1)
4821 : 0 : && ! side_effects_p (op0))
4822 : : return op1;
4823 : 225860 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4824 : : return op0;
4825 : 225719 : tem = simplify_associative_operation (code, mode, op0, op1);
4826 : 225719 : if (tem)
4827 : : return tem;
4828 : : break;
4829 : :
4830 : 463024 : case SMAX:
4831 : 463024 : if (HWI_COMPUTABLE_MODE_P (mode)
4832 : 436172 : && CONST_INT_P (trueop1)
4833 : 406500 : && (UINTVAL (trueop1) == GET_MODE_MASK (mode) >> 1)
4834 : 0 : && ! side_effects_p (op0))
4835 : : return op1;
4836 : 463024 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4837 : : return op0;
4838 : 462919 : tem = simplify_associative_operation (code, mode, op0, op1);
4839 : 462919 : if (tem)
4840 : : return tem;
4841 : : break;
4842 : :
4843 : 233723 : case UMIN:
4844 : 233723 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
4845 : : return op1;
4846 : 233708 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4847 : : return op0;
4848 : 233607 : tem = simplify_associative_operation (code, mode, op0, op1);
4849 : 233607 : if (tem)
4850 : : return tem;
4851 : : break;
4852 : :
4853 : 228833 : case UMAX:
4854 : 228833 : if (trueop1 == constm1_rtx && ! side_effects_p (op0))
4855 : : return op1;
4856 : 228833 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4857 : : return op0;
4858 : 228743 : tem = simplify_associative_operation (code, mode, op0, op1);
4859 : 228743 : if (tem)
4860 : : return tem;
4861 : : break;
4862 : :
4863 : 11656 : case SS_PLUS:
4864 : 11656 : case US_PLUS:
4865 : 11656 : case SS_MINUS:
4866 : 11656 : case US_MINUS:
4867 : : /* Simplify x +/- 0 to x, if possible. */
4868 : 11656 : if (trueop1 == CONST0_RTX (mode))
4869 : : return op0;
4870 : : return 0;
4871 : :
4872 : 0 : case SS_MULT:
4873 : 0 : case US_MULT:
4874 : : /* Simplify x * 0 to 0, if possible. */
4875 : 0 : if (trueop1 == CONST0_RTX (mode)
4876 : 0 : && !side_effects_p (op0))
4877 : : return op1;
4878 : :
4879 : : /* Simplify x * 1 to x, if possible. */
4880 : 0 : if (trueop1 == CONST1_RTX (mode))
4881 : : return op0;
4882 : : return 0;
4883 : :
4884 : 528211 : case SMUL_HIGHPART:
4885 : 528211 : case UMUL_HIGHPART:
4886 : : /* Simplify x * 0 to 0, if possible. */
4887 : 528211 : if (trueop1 == CONST0_RTX (mode)
4888 : 528211 : && !side_effects_p (op0))
4889 : : return op1;
4890 : : return 0;
4891 : :
4892 : 0 : case SS_DIV:
4893 : 0 : case US_DIV:
4894 : : /* Simplify x / 1 to x, if possible. */
4895 : 0 : if (trueop1 == CONST1_RTX (mode))
4896 : : return op0;
4897 : : return 0;
4898 : :
4899 : 0 : case COPYSIGN:
4900 : 0 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4901 : : return op0;
4902 : 0 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1))
4903 : : {
4904 : 0 : REAL_VALUE_TYPE f1;
4905 : 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
4906 : 0 : rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
4907 : 0 : if (REAL_VALUE_NEGATIVE (f1))
4908 : 0 : tmp = simplify_unary_operation (NEG, mode, tmp, mode);
4909 : 0 : return tmp;
4910 : : }
4911 : 0 : if (GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
4912 : 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
4913 : 0 : if (GET_CODE (op1) == ABS
4914 : 0 : && ! side_effects_p (op1))
4915 : 0 : return simplify_gen_unary (ABS, mode, op0, mode);
4916 : 0 : if (GET_CODE (op0) == COPYSIGN
4917 : 0 : && ! side_effects_p (XEXP (op0, 1)))
4918 : 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
4919 : 0 : if (GET_CODE (op1) == COPYSIGN
4920 : 0 : && ! side_effects_p (XEXP (op1, 0)))
4921 : 0 : return simplify_gen_binary (COPYSIGN, mode, op0, XEXP (op1, 1));
4922 : : return 0;
4923 : :
4924 : 1050 : case VEC_SERIES:
4925 : 2100 : if (op1 == CONST0_RTX (GET_MODE_INNER (mode)))
4926 : 92 : return gen_vec_duplicate (mode, op0);
4927 : 958 : if (valid_for_const_vector_p (mode, op0)
4928 : 958 : && valid_for_const_vector_p (mode, op1))
4929 : 93 : return gen_const_vec_series (mode, op0, op1);
4930 : : return 0;
4931 : :
4932 : 3343207 : case VEC_SELECT:
4933 : 3343207 : if (!VECTOR_MODE_P (mode))
4934 : : {
4935 : 894111 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
4936 : 1788222 : gcc_assert (mode == GET_MODE_INNER (GET_MODE (trueop0)));
4937 : 894111 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
4938 : 894111 : gcc_assert (XVECLEN (trueop1, 0) == 1);
4939 : :
4940 : : /* We can't reason about selections made at runtime. */
4941 : 894111 : if (!CONST_INT_P (XVECEXP (trueop1, 0, 0)))
4942 : 449386882 : return 0;
4943 : :
4944 : 894111 : if (vec_duplicate_p (trueop0, &elt0))
4945 : 2149 : return elt0;
4946 : :
4947 : 891962 : if (GET_CODE (trueop0) == CONST_VECTOR)
4948 : 7293 : return CONST_VECTOR_ELT (trueop0, INTVAL (XVECEXP
4949 : : (trueop1, 0, 0)));
4950 : :
4951 : : /* Extract a scalar element from a nested VEC_SELECT expression
4952 : : (with optional nested VEC_CONCAT expression). Some targets
4953 : : (i386) extract scalar element from a vector using chain of
4954 : : nested VEC_SELECT expressions. When input operand is a memory
4955 : : operand, this operation can be simplified to a simple scalar
4956 : : load from an offseted memory address. */
4957 : 884669 : int n_elts;
4958 : 884669 : if (GET_CODE (trueop0) == VEC_SELECT
4959 : 951191 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
4960 : 66522 : .is_constant (&n_elts)))
4961 : : {
4962 : 66522 : rtx op0 = XEXP (trueop0, 0);
4963 : 66522 : rtx op1 = XEXP (trueop0, 1);
4964 : :
4965 : 66522 : int i = INTVAL (XVECEXP (trueop1, 0, 0));
4966 : 66522 : int elem;
4967 : :
4968 : 66522 : rtvec vec;
4969 : 66522 : rtx tmp_op, tmp;
4970 : :
4971 : 66522 : gcc_assert (GET_CODE (op1) == PARALLEL);
4972 : 66522 : gcc_assert (i < n_elts);
4973 : :
4974 : : /* Select element, pointed by nested selector. */
4975 : 66522 : elem = INTVAL (XVECEXP (op1, 0, i));
4976 : :
4977 : : /* Handle the case when nested VEC_SELECT wraps VEC_CONCAT. */
4978 : 66522 : if (GET_CODE (op0) == VEC_CONCAT)
4979 : : {
4980 : 27549 : rtx op00 = XEXP (op0, 0);
4981 : 27549 : rtx op01 = XEXP (op0, 1);
4982 : :
4983 : 27549 : machine_mode mode00, mode01;
4984 : 27549 : int n_elts00, n_elts01;
4985 : :
4986 : 27549 : mode00 = GET_MODE (op00);
4987 : 27549 : mode01 = GET_MODE (op01);
4988 : :
4989 : : /* Find out the number of elements of each operand.
4990 : : Since the concatenated result has a constant number
4991 : : of elements, the operands must too. */
4992 : 27549 : n_elts00 = GET_MODE_NUNITS (mode00).to_constant ();
4993 : 27549 : n_elts01 = GET_MODE_NUNITS (mode01).to_constant ();
4994 : :
4995 : 27549 : gcc_assert (n_elts == n_elts00 + n_elts01);
4996 : :
4997 : : /* Select correct operand of VEC_CONCAT
4998 : : and adjust selector. */
4999 : 27549 : if (elem < n_elts01)
5000 : : tmp_op = op00;
5001 : : else
5002 : : {
5003 : 41 : tmp_op = op01;
5004 : 41 : elem -= n_elts00;
5005 : : }
5006 : : }
5007 : : else
5008 : : tmp_op = op0;
5009 : :
5010 : 66522 : vec = rtvec_alloc (1);
5011 : 66522 : RTVEC_ELT (vec, 0) = GEN_INT (elem);
5012 : :
5013 : 66522 : tmp = gen_rtx_fmt_ee (code, mode,
5014 : : tmp_op, gen_rtx_PARALLEL (VOIDmode, vec));
5015 : 66522 : return tmp;
5016 : : }
5017 : : }
5018 : : else
5019 : : {
5020 : 2449096 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
5021 : 7347288 : gcc_assert (GET_MODE_INNER (mode)
5022 : : == GET_MODE_INNER (GET_MODE (trueop0)));
5023 : 2449096 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
5024 : :
5025 : 2449096 : if (vec_duplicate_p (trueop0, &elt0))
5026 : : /* It doesn't matter which elements are selected by trueop1,
5027 : : because they are all the same. */
5028 : 15275 : return gen_vec_duplicate (mode, elt0);
5029 : :
5030 : 2433821 : if (GET_CODE (trueop0) == CONST_VECTOR)
5031 : : {
5032 : 17222 : unsigned n_elts = XVECLEN (trueop1, 0);
5033 : 17222 : rtvec v = rtvec_alloc (n_elts);
5034 : 17222 : unsigned int i;
5035 : :
5036 : 34444 : gcc_assert (known_eq (n_elts, GET_MODE_NUNITS (mode)));
5037 : 88246 : for (i = 0; i < n_elts; i++)
5038 : : {
5039 : 71024 : rtx x = XVECEXP (trueop1, 0, i);
5040 : :
5041 : 71024 : if (!CONST_INT_P (x))
5042 : : return 0;
5043 : :
5044 : 71024 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0,
5045 : : INTVAL (x));
5046 : : }
5047 : :
5048 : 17222 : return gen_rtx_CONST_VECTOR (mode, v);
5049 : : }
5050 : :
5051 : : /* Recognize the identity. */
5052 : 2416599 : if (GET_MODE (trueop0) == mode)
5053 : : {
5054 : 564304 : bool maybe_ident = true;
5055 : 564304 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
5056 : : {
5057 : 563925 : rtx j = XVECEXP (trueop1, 0, i);
5058 : 563925 : if (!CONST_INT_P (j) || INTVAL (j) != i)
5059 : : {
5060 : : maybe_ident = false;
5061 : : break;
5062 : : }
5063 : : }
5064 : 341526 : if (maybe_ident)
5065 : : return trueop0;
5066 : : }
5067 : :
5068 : : /* If we select a low-part subreg, return that. */
5069 : 2416220 : if (vec_series_lowpart_p (mode, GET_MODE (trueop0), trueop1))
5070 : : {
5071 : 0 : rtx new_rtx = lowpart_subreg (mode, trueop0,
5072 : 0 : GET_MODE (trueop0));
5073 : 0 : if (new_rtx != NULL_RTX)
5074 : : return new_rtx;
5075 : : }
5076 : :
5077 : : /* If we build {a,b} then permute it, build the result directly. */
5078 : 2416220 : if (XVECLEN (trueop1, 0) == 2
5079 : 577834 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5080 : 577834 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
5081 : 577834 : && GET_CODE (trueop0) == VEC_CONCAT
5082 : 171774 : && GET_CODE (XEXP (trueop0, 0)) == VEC_CONCAT
5083 : 79 : && GET_MODE (XEXP (trueop0, 0)) == mode
5084 : 79 : && GET_CODE (XEXP (trueop0, 1)) == VEC_CONCAT
5085 : 56 : && GET_MODE (XEXP (trueop0, 1)) == mode)
5086 : : {
5087 : 56 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5088 : 56 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
5089 : 56 : rtx subop0, subop1;
5090 : :
5091 : 56 : gcc_assert (i0 < 4 && i1 < 4);
5092 : 56 : subop0 = XEXP (XEXP (trueop0, i0 / 2), i0 % 2);
5093 : 56 : subop1 = XEXP (XEXP (trueop0, i1 / 2), i1 % 2);
5094 : :
5095 : 56 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
5096 : : }
5097 : :
5098 : 2416164 : if (XVECLEN (trueop1, 0) == 2
5099 : 577778 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5100 : 577778 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
5101 : 577778 : && GET_CODE (trueop0) == VEC_CONCAT
5102 : 171718 : && GET_MODE (trueop0) == mode)
5103 : : {
5104 : 2 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5105 : 2 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
5106 : 2 : rtx subop0, subop1;
5107 : :
5108 : 2 : gcc_assert (i0 < 2 && i1 < 2);
5109 : 2 : subop0 = XEXP (trueop0, i0);
5110 : 2 : subop1 = XEXP (trueop0, i1);
5111 : :
5112 : 2 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
5113 : : }
5114 : :
5115 : : /* If we select one half of a vec_concat, return that. */
5116 : 2416162 : int l0, l1;
5117 : 2416162 : if (GET_CODE (trueop0) == VEC_CONCAT
5118 : 2997592 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
5119 : 1498796 : .is_constant (&l0))
5120 : 2997592 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 1)))
5121 : 1498796 : .is_constant (&l1))
5122 : 3914958 : && CONST_INT_P (XVECEXP (trueop1, 0, 0)))
5123 : : {
5124 : 1498796 : rtx subop0 = XEXP (trueop0, 0);
5125 : 1498796 : rtx subop1 = XEXP (trueop0, 1);
5126 : 1498796 : machine_mode mode0 = GET_MODE (subop0);
5127 : 1498796 : machine_mode mode1 = GET_MODE (subop1);
5128 : 1498796 : int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5129 : 1498796 : if (i0 == 0 && !side_effects_p (op1) && mode == mode0)
5130 : : {
5131 : 958533 : bool success = true;
5132 : 958533 : for (int i = 1; i < l0; ++i)
5133 : : {
5134 : 958206 : rtx j = XVECEXP (trueop1, 0, i);
5135 : 958206 : if (!CONST_INT_P (j) || INTVAL (j) != i)
5136 : : {
5137 : : success = false;
5138 : : break;
5139 : : }
5140 : : }
5141 : 875508 : if (success)
5142 : : return subop0;
5143 : : }
5144 : 1498469 : if (i0 == l0 && !side_effects_p (op0) && mode == mode1)
5145 : : {
5146 : 590 : bool success = true;
5147 : 590 : for (int i = 1; i < l1; ++i)
5148 : : {
5149 : 543 : rtx j = XVECEXP (trueop1, 0, i);
5150 : 543 : if (!CONST_INT_P (j) || INTVAL (j) != i0 + i)
5151 : : {
5152 : : success = false;
5153 : : break;
5154 : : }
5155 : : }
5156 : 76 : if (success)
5157 : : return subop1;
5158 : : }
5159 : : }
5160 : :
5161 : : /* Simplify vec_select of a subreg of X to just a vec_select of X
5162 : : when X has same component mode as vec_select. */
5163 : 2415788 : unsigned HOST_WIDE_INT subreg_offset = 0;
5164 : 2415788 : if (GET_CODE (trueop0) == SUBREG
5165 : 355541 : && GET_MODE_INNER (mode)
5166 : 711082 : == GET_MODE_INNER (GET_MODE (SUBREG_REG (trueop0)))
5167 : 28562 : && GET_MODE_NUNITS (mode).is_constant (&l1)
5168 : 2771329 : && constant_multiple_p (subreg_memory_offset (trueop0),
5169 : 28562 : GET_MODE_UNIT_BITSIZE (mode),
5170 : : &subreg_offset))
5171 : : {
5172 : 14281 : poly_uint64 nunits
5173 : 28562 : = GET_MODE_NUNITS (GET_MODE (SUBREG_REG (trueop0)));
5174 : 14281 : bool success = true;
5175 : 81207 : for (int i = 0; i != l1; i++)
5176 : : {
5177 : 77275 : rtx idx = XVECEXP (trueop1, 0, i);
5178 : 77275 : if (!CONST_INT_P (idx)
5179 : 77275 : || maybe_ge (UINTVAL (idx) + subreg_offset, nunits))
5180 : : {
5181 : : success = false;
5182 : : break;
5183 : : }
5184 : : }
5185 : :
5186 : 14281 : if (success)
5187 : : {
5188 : 3932 : rtx par = trueop1;
5189 : 3932 : if (subreg_offset)
5190 : : {
5191 : 0 : rtvec vec = rtvec_alloc (l1);
5192 : 0 : for (int i = 0; i < l1; i++)
5193 : 0 : RTVEC_ELT (vec, i)
5194 : 0 : = GEN_INT (INTVAL (XVECEXP (trueop1, 0, i))
5195 : : + subreg_offset);
5196 : 0 : par = gen_rtx_PARALLEL (VOIDmode, vec);
5197 : : }
5198 : 3932 : return gen_rtx_VEC_SELECT (mode, SUBREG_REG (trueop0), par);
5199 : : }
5200 : : }
5201 : : }
5202 : :
5203 : 3230003 : if (XVECLEN (trueop1, 0) == 1
5204 : 818231 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5205 : 818231 : && GET_CODE (trueop0) == VEC_CONCAT)
5206 : : {
5207 : 1259 : rtx vec = trueop0;
5208 : 2518 : offset = INTVAL (XVECEXP (trueop1, 0, 0)) * GET_MODE_SIZE (mode);
5209 : :
5210 : : /* Try to find the element in the VEC_CONCAT. */
5211 : 1259 : while (GET_MODE (vec) != mode
5212 : 2518 : && GET_CODE (vec) == VEC_CONCAT)
5213 : : {
5214 : 1259 : poly_int64 vec_size;
5215 : :
5216 : 1259 : if (CONST_INT_P (XEXP (vec, 0)))
5217 : : {
5218 : : /* vec_concat of two const_ints doesn't make sense with
5219 : : respect to modes. */
5220 : 3 : if (CONST_INT_P (XEXP (vec, 1)))
5221 : 385640115 : return 0;
5222 : :
5223 : 3 : vec_size = GET_MODE_SIZE (GET_MODE (trueop0))
5224 : 9 : - GET_MODE_SIZE (GET_MODE (XEXP (vec, 1)));
5225 : : }
5226 : : else
5227 : 2512 : vec_size = GET_MODE_SIZE (GET_MODE (XEXP (vec, 0)));
5228 : :
5229 : 1259 : if (known_lt (offset, vec_size))
5230 : : vec = XEXP (vec, 0);
5231 : 313 : else if (known_ge (offset, vec_size))
5232 : : {
5233 : 313 : offset -= vec_size;
5234 : 313 : vec = XEXP (vec, 1);
5235 : : }
5236 : : else
5237 : : break;
5238 : 1259 : vec = avoid_constant_pool_reference (vec);
5239 : : }
5240 : :
5241 : 1259 : if (GET_MODE (vec) == mode)
5242 : : return vec;
5243 : : }
5244 : :
5245 : : /* If we select elements in a vec_merge that all come from the same
5246 : : operand, select from that operand directly. */
5247 : 3228919 : if (GET_CODE (op0) == VEC_MERGE)
5248 : : {
5249 : 9649 : rtx trueop02 = avoid_constant_pool_reference (XEXP (op0, 2));
5250 : 9649 : if (CONST_INT_P (trueop02))
5251 : : {
5252 : 3198 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop02);
5253 : 3198 : bool all_operand0 = true;
5254 : 3198 : bool all_operand1 = true;
5255 : 10510 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
5256 : : {
5257 : 7312 : rtx j = XVECEXP (trueop1, 0, i);
5258 : 7312 : if (sel & (HOST_WIDE_INT_1U << UINTVAL (j)))
5259 : : all_operand1 = false;
5260 : : else
5261 : 3223 : all_operand0 = false;
5262 : : }
5263 : 3198 : if (all_operand0 && !side_effects_p (XEXP (op0, 1)))
5264 : 1437 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 0), op1);
5265 : 1761 : if (all_operand1 && !side_effects_p (XEXP (op0, 0)))
5266 : 47 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 1), op1);
5267 : : }
5268 : : }
5269 : :
5270 : : /* If we have two nested selects that are inverses of each
5271 : : other, replace them with the source operand. */
5272 : 3227435 : if (GET_CODE (trueop0) == VEC_SELECT
5273 : 69804 : && GET_MODE (XEXP (trueop0, 0)) == mode)
5274 : : {
5275 : 1275 : rtx op0_subop1 = XEXP (trueop0, 1);
5276 : 1275 : gcc_assert (GET_CODE (op0_subop1) == PARALLEL);
5277 : 2550 : gcc_assert (known_eq (XVECLEN (trueop1, 0), GET_MODE_NUNITS (mode)));
5278 : :
5279 : : /* Apply the outer ordering vector to the inner one. (The inner
5280 : : ordering vector is expressly permitted to be of a different
5281 : : length than the outer one.) If the result is { 0, 1, ..., n-1 }
5282 : : then the two VEC_SELECTs cancel. */
5283 : 1581 : for (int i = 0; i < XVECLEN (trueop1, 0); ++i)
5284 : : {
5285 : 1581 : rtx x = XVECEXP (trueop1, 0, i);
5286 : 1581 : if (!CONST_INT_P (x))
5287 : : return 0;
5288 : 1581 : rtx y = XVECEXP (op0_subop1, 0, INTVAL (x));
5289 : 1581 : if (!CONST_INT_P (y) || i != INTVAL (y))
5290 : : return 0;
5291 : : }
5292 : : return XEXP (trueop0, 0);
5293 : : }
5294 : :
5295 : : return 0;
5296 : 4171678 : case VEC_CONCAT:
5297 : 4171678 : {
5298 : 4171678 : machine_mode op0_mode = (GET_MODE (trueop0) != VOIDmode
5299 : 4171678 : ? GET_MODE (trueop0)
5300 : 4171678 : : GET_MODE_INNER (mode));
5301 : 4171678 : machine_mode op1_mode = (GET_MODE (trueop1) != VOIDmode
5302 : 4171678 : ? GET_MODE (trueop1)
5303 : 4171678 : : GET_MODE_INNER (mode));
5304 : :
5305 : 4171678 : gcc_assert (VECTOR_MODE_P (mode));
5306 : 16686712 : gcc_assert (known_eq (GET_MODE_SIZE (op0_mode)
5307 : : + GET_MODE_SIZE (op1_mode),
5308 : : GET_MODE_SIZE (mode)));
5309 : :
5310 : 4171678 : if (VECTOR_MODE_P (op0_mode))
5311 : 5645160 : gcc_assert (GET_MODE_INNER (mode)
5312 : : == GET_MODE_INNER (op0_mode));
5313 : : else
5314 : 4579916 : gcc_assert (GET_MODE_INNER (mode) == op0_mode);
5315 : :
5316 : 4171678 : if (VECTOR_MODE_P (op1_mode))
5317 : 5645160 : gcc_assert (GET_MODE_INNER (mode)
5318 : : == GET_MODE_INNER (op1_mode));
5319 : : else
5320 : 4579916 : gcc_assert (GET_MODE_INNER (mode) == op1_mode);
5321 : :
5322 : 4171678 : unsigned int n_elts, in_n_elts;
5323 : 4171678 : if ((GET_CODE (trueop0) == CONST_VECTOR
5324 : 4171678 : || CONST_SCALAR_INT_P (trueop0)
5325 : 4007739 : || CONST_DOUBLE_AS_FLOAT_P (trueop0))
5326 : 165382 : && (GET_CODE (trueop1) == CONST_VECTOR
5327 : 165382 : || CONST_SCALAR_INT_P (trueop1)
5328 : 165382 : || CONST_DOUBLE_AS_FLOAT_P (trueop1))
5329 : 0 : && GET_MODE_NUNITS (mode).is_constant (&n_elts)
5330 : 4171678 : && GET_MODE_NUNITS (op0_mode).is_constant (&in_n_elts))
5331 : : {
5332 : 0 : rtvec v = rtvec_alloc (n_elts);
5333 : 0 : unsigned int i;
5334 : 0 : for (i = 0; i < n_elts; i++)
5335 : : {
5336 : 0 : if (i < in_n_elts)
5337 : : {
5338 : 0 : if (!VECTOR_MODE_P (op0_mode))
5339 : 0 : RTVEC_ELT (v, i) = trueop0;
5340 : : else
5341 : 0 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0, i);
5342 : : }
5343 : : else
5344 : : {
5345 : 0 : if (!VECTOR_MODE_P (op1_mode))
5346 : 0 : RTVEC_ELT (v, i) = trueop1;
5347 : : else
5348 : 0 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop1,
5349 : : i - in_n_elts);
5350 : : }
5351 : : }
5352 : :
5353 : 0 : return gen_rtx_CONST_VECTOR (mode, v);
5354 : : }
5355 : :
5356 : : /* Try to merge two VEC_SELECTs from the same vector into a single one.
5357 : : Restrict the transformation to avoid generating a VEC_SELECT with a
5358 : : mode unrelated to its operand. */
5359 : 4171678 : if (GET_CODE (trueop0) == VEC_SELECT
5360 : 129871 : && GET_CODE (trueop1) == VEC_SELECT
5361 : 28528 : && rtx_equal_p (XEXP (trueop0, 0), XEXP (trueop1, 0))
5362 : 4187778 : && GET_MODE_INNER (GET_MODE (XEXP (trueop0, 0)))
5363 : 32200 : == GET_MODE_INNER(mode))
5364 : : {
5365 : 16100 : rtx par0 = XEXP (trueop0, 1);
5366 : 16100 : rtx par1 = XEXP (trueop1, 1);
5367 : 16100 : int len0 = XVECLEN (par0, 0);
5368 : 16100 : int len1 = XVECLEN (par1, 0);
5369 : 16100 : rtvec vec = rtvec_alloc (len0 + len1);
5370 : 99138 : for (int i = 0; i < len0; i++)
5371 : 83038 : RTVEC_ELT (vec, i) = XVECEXP (par0, 0, i);
5372 : 99138 : for (int i = 0; i < len1; i++)
5373 : 83038 : RTVEC_ELT (vec, len0 + i) = XVECEXP (par1, 0, i);
5374 : 16100 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (trueop0, 0),
5375 : 16100 : gen_rtx_PARALLEL (VOIDmode, vec));
5376 : : }
5377 : : /* (vec_concat:
5378 : : (subreg_lowpart:N OP)
5379 : : (vec_select:N OP P)) --> OP when P selects the high half
5380 : : of the OP. */
5381 : 4155578 : if (GET_CODE (trueop0) == SUBREG
5382 : 475229 : && subreg_lowpart_p (trueop0)
5383 : 475001 : && GET_CODE (trueop1) == VEC_SELECT
5384 : 49 : && SUBREG_REG (trueop0) == XEXP (trueop1, 0)
5385 : 0 : && !side_effects_p (XEXP (trueop1, 0))
5386 : 4155578 : && vec_series_highpart_p (op1_mode, mode, XEXP (trueop1, 1)))
5387 : 0 : return XEXP (trueop1, 0);
5388 : : }
5389 : : return 0;
5390 : :
5391 : 0 : default:
5392 : 0 : gcc_unreachable ();
5393 : : }
5394 : :
5395 : 377716434 : if (mode == GET_MODE (op0)
5396 : 322732750 : && mode == GET_MODE (op1)
5397 : 98168793 : && vec_duplicate_p (op0, &elt0)
5398 : 377830479 : && vec_duplicate_p (op1, &elt1))
5399 : : {
5400 : : /* Try applying the operator to ELT and see if that simplifies.
5401 : : We can duplicate the result if so.
5402 : :
5403 : : The reason we don't use simplify_gen_binary is that it isn't
5404 : : necessarily a win to convert things like:
5405 : :
5406 : : (plus:V (vec_duplicate:V (reg:S R1))
5407 : : (vec_duplicate:V (reg:S R2)))
5408 : :
5409 : : to:
5410 : :
5411 : : (vec_duplicate:V (plus:S (reg:S R1) (reg:S R2)))
5412 : :
5413 : : The first might be done entirely in vector registers while the
5414 : : second might need a move between register files. */
5415 : 118 : tem = simplify_binary_operation (code, GET_MODE_INNER (mode),
5416 : : elt0, elt1);
5417 : 59 : if (tem)
5418 : 2 : return gen_vec_duplicate (mode, tem);
5419 : : }
5420 : :
5421 : : return 0;
5422 : : }
5423 : :
5424 : : /* Return true if binary operation OP distributes over addition in operand
5425 : : OPNO, with the other operand being held constant. OPNO counts from 1. */
5426 : :
5427 : : static bool
5428 : 7105 : distributes_over_addition_p (rtx_code op, int opno)
5429 : : {
5430 : 0 : switch (op)
5431 : : {
5432 : : case PLUS:
5433 : : case MINUS:
5434 : : case MULT:
5435 : : return true;
5436 : :
5437 : 0 : case ASHIFT:
5438 : 0 : return opno == 1;
5439 : :
5440 : 0 : default:
5441 : 0 : return false;
5442 : : }
5443 : : }
5444 : :
5445 : : rtx
5446 : 483515828 : simplify_const_binary_operation (enum rtx_code code, machine_mode mode,
5447 : : rtx op0, rtx op1)
5448 : : {
5449 : 483515828 : if (VECTOR_MODE_P (mode)
5450 : 14693344 : && code != VEC_CONCAT
5451 : 10511376 : && GET_CODE (op0) == CONST_VECTOR
5452 : 185030 : && GET_CODE (op1) == CONST_VECTOR)
5453 : : {
5454 : 7822 : bool step_ok_p;
5455 : 7822 : if (CONST_VECTOR_STEPPED_P (op0)
5456 : 7822 : && CONST_VECTOR_STEPPED_P (op1))
5457 : : /* We can operate directly on the encoding if:
5458 : :
5459 : : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
5460 : : implies
5461 : : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
5462 : :
5463 : : Addition and subtraction are the supported operators
5464 : : for which this is true. */
5465 : 717 : step_ok_p = (code == PLUS || code == MINUS);
5466 : 7105 : else if (CONST_VECTOR_STEPPED_P (op0))
5467 : : /* We can operate directly on stepped encodings if:
5468 : :
5469 : : a3 - a2 == a2 - a1
5470 : : implies:
5471 : : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
5472 : :
5473 : : which is true if (x -> x op c) distributes over addition. */
5474 : 1028 : step_ok_p = distributes_over_addition_p (code, 1);
5475 : : else
5476 : : /* Similarly in reverse. */
5477 : 6077 : step_ok_p = distributes_over_addition_p (code, 2);
5478 : 7822 : rtx_vector_builder builder;
5479 : 7822 : if (!builder.new_binary_operation (mode, op0, op1, step_ok_p))
5480 : : return 0;
5481 : :
5482 : 7822 : unsigned int count = builder.encoded_nelts ();
5483 : 51019 : for (unsigned int i = 0; i < count; i++)
5484 : : {
5485 : 86660 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
5486 : : CONST_VECTOR_ELT (op0, i),
5487 : 43330 : CONST_VECTOR_ELT (op1, i));
5488 : 43330 : if (!x || !valid_for_const_vector_p (mode, x))
5489 : 133 : return 0;
5490 : 43197 : builder.quick_push (x);
5491 : : }
5492 : 7689 : return builder.build ();
5493 : 7822 : }
5494 : :
5495 : 483508006 : if (VECTOR_MODE_P (mode)
5496 : 14685522 : && code == VEC_CONCAT
5497 : 4181968 : && (CONST_SCALAR_INT_P (op0)
5498 : 4028926 : || CONST_FIXED_P (op0)
5499 : 4028926 : || CONST_DOUBLE_AS_FLOAT_P (op0)
5500 : 4026119 : || CONST_VECTOR_P (op0))
5501 : 175672 : && (CONST_SCALAR_INT_P (op1)
5502 : 173835 : || CONST_DOUBLE_AS_FLOAT_P (op1)
5503 : 172471 : || CONST_FIXED_P (op1)
5504 : 172471 : || CONST_VECTOR_P (op1)))
5505 : : {
5506 : : /* Both inputs have a constant number of elements, so the result
5507 : : must too. */
5508 : 10290 : unsigned n_elts = GET_MODE_NUNITS (mode).to_constant ();
5509 : 10290 : rtvec v = rtvec_alloc (n_elts);
5510 : :
5511 : 10290 : gcc_assert (n_elts >= 2);
5512 : 10290 : if (n_elts == 2)
5513 : : {
5514 : 3201 : gcc_assert (GET_CODE (op0) != CONST_VECTOR);
5515 : 3201 : gcc_assert (GET_CODE (op1) != CONST_VECTOR);
5516 : :
5517 : 3201 : RTVEC_ELT (v, 0) = op0;
5518 : 3201 : RTVEC_ELT (v, 1) = op1;
5519 : : }
5520 : : else
5521 : : {
5522 : 7089 : unsigned op0_n_elts = GET_MODE_NUNITS (GET_MODE (op0)).to_constant ();
5523 : 7089 : unsigned op1_n_elts = GET_MODE_NUNITS (GET_MODE (op1)).to_constant ();
5524 : 7089 : unsigned i;
5525 : :
5526 : 7089 : gcc_assert (GET_CODE (op0) == CONST_VECTOR);
5527 : 7089 : gcc_assert (GET_CODE (op1) == CONST_VECTOR);
5528 : 7089 : gcc_assert (op0_n_elts + op1_n_elts == n_elts);
5529 : :
5530 : 60749 : for (i = 0; i < op0_n_elts; ++i)
5531 : 53660 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (op0, i);
5532 : 60941 : for (i = 0; i < op1_n_elts; ++i)
5533 : 53852 : RTVEC_ELT (v, op0_n_elts+i) = CONST_VECTOR_ELT (op1, i);
5534 : : }
5535 : :
5536 : 10290 : return gen_rtx_CONST_VECTOR (mode, v);
5537 : : }
5538 : :
5539 : 472059872 : if (VECTOR_MODE_P (mode)
5540 : 14675232 : && GET_CODE (op0) == CONST_VECTOR
5541 : 189942 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1))
5542 : 483497716 : && (CONST_VECTOR_DUPLICATE_P (op0)
5543 : : || CONST_VECTOR_NUNITS (op0).is_constant ()))
5544 : : {
5545 : 135578 : switch (code)
5546 : : {
5547 : 135578 : case PLUS:
5548 : 135578 : case MINUS:
5549 : 135578 : case MULT:
5550 : 135578 : case DIV:
5551 : 135578 : case MOD:
5552 : 135578 : case UDIV:
5553 : 135578 : case UMOD:
5554 : 135578 : case AND:
5555 : 135578 : case IOR:
5556 : 135578 : case XOR:
5557 : 135578 : case SMIN:
5558 : 135578 : case SMAX:
5559 : 135578 : case UMIN:
5560 : 135578 : case UMAX:
5561 : 135578 : case LSHIFTRT:
5562 : 135578 : case ASHIFTRT:
5563 : 135578 : case ASHIFT:
5564 : 135578 : case ROTATE:
5565 : 135578 : case ROTATERT:
5566 : 135578 : case SS_PLUS:
5567 : 135578 : case US_PLUS:
5568 : 135578 : case SS_MINUS:
5569 : 135578 : case US_MINUS:
5570 : 135578 : case SS_ASHIFT:
5571 : 135578 : case US_ASHIFT:
5572 : 135578 : case COPYSIGN:
5573 : 135578 : break;
5574 : : default:
5575 : : return NULL_RTX;
5576 : : }
5577 : :
5578 : 135578 : unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op0)
5579 : 135578 : ? CONST_VECTOR_NPATTERNS (op0)
5580 : 143295 : : CONST_VECTOR_NUNITS (op0).to_constant ());
5581 : 135578 : rtx_vector_builder builder (mode, npatterns, 1);
5582 : 284183 : for (unsigned i = 0; i < npatterns; i++)
5583 : : {
5584 : 297210 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
5585 : 148605 : CONST_VECTOR_ELT (op0, i), op1);
5586 : 148605 : if (!x || !valid_for_const_vector_p (mode, x))
5587 : 0 : return 0;
5588 : 148605 : builder.quick_push (x);
5589 : : }
5590 : 135578 : return builder.build ();
5591 : : }
5592 : :
5593 : 483362138 : if (SCALAR_FLOAT_MODE_P (mode)
5594 : 6408491 : && CONST_DOUBLE_AS_FLOAT_P (op0)
5595 : 76599 : && CONST_DOUBLE_AS_FLOAT_P (op1)
5596 : 11642 : && mode == GET_MODE (op0) && mode == GET_MODE (op1))
5597 : : {
5598 : 11642 : if (code == AND
5599 : : || code == IOR
5600 : 11642 : || code == XOR)
5601 : : {
5602 : 2518 : long tmp0[4];
5603 : 2518 : long tmp1[4];
5604 : 2518 : REAL_VALUE_TYPE r;
5605 : 2518 : int i;
5606 : :
5607 : 2518 : real_to_target (tmp0, CONST_DOUBLE_REAL_VALUE (op0),
5608 : 2518 : GET_MODE (op0));
5609 : 2518 : real_to_target (tmp1, CONST_DOUBLE_REAL_VALUE (op1),
5610 : 2518 : GET_MODE (op1));
5611 : 12590 : for (i = 0; i < 4; i++)
5612 : : {
5613 : 10072 : switch (code)
5614 : : {
5615 : 5280 : case AND:
5616 : 5280 : tmp0[i] &= tmp1[i];
5617 : 5280 : break;
5618 : 2512 : case IOR:
5619 : 2512 : tmp0[i] |= tmp1[i];
5620 : 2512 : break;
5621 : 2280 : case XOR:
5622 : 2280 : tmp0[i] ^= tmp1[i];
5623 : 2280 : break;
5624 : : default:
5625 : : gcc_unreachable ();
5626 : : }
5627 : : }
5628 : 2518 : real_from_target (&r, tmp0, mode);
5629 : 2518 : return const_double_from_real_value (r, mode);
5630 : : }
5631 : 9124 : else if (code == COPYSIGN)
5632 : : {
5633 : 0 : REAL_VALUE_TYPE f0, f1;
5634 : 0 : real_convert (&f0, mode, CONST_DOUBLE_REAL_VALUE (op0));
5635 : 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (op1));
5636 : 0 : real_copysign (&f0, &f1);
5637 : 0 : return const_double_from_real_value (f0, mode);
5638 : : }
5639 : : else
5640 : : {
5641 : 9124 : REAL_VALUE_TYPE f0, f1, value, result;
5642 : 9124 : const REAL_VALUE_TYPE *opr0, *opr1;
5643 : 9124 : bool inexact;
5644 : :
5645 : 9124 : opr0 = CONST_DOUBLE_REAL_VALUE (op0);
5646 : 9124 : opr1 = CONST_DOUBLE_REAL_VALUE (op1);
5647 : :
5648 : 9124 : if (HONOR_SNANS (mode)
5649 : 9124 : && (REAL_VALUE_ISSIGNALING_NAN (*opr0)
5650 : 803 : || REAL_VALUE_ISSIGNALING_NAN (*opr1)))
5651 : 10 : return 0;
5652 : :
5653 : 9114 : real_convert (&f0, mode, opr0);
5654 : 9114 : real_convert (&f1, mode, opr1);
5655 : :
5656 : 9114 : if (code == DIV
5657 : 4185 : && real_equal (&f1, &dconst0)
5658 : 12831 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
5659 : 3713 : return 0;
5660 : :
5661 : 26904 : if (MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
5662 : 5309 : && flag_trapping_math
5663 : 5233 : && REAL_VALUE_ISINF (f0) && REAL_VALUE_ISINF (f1))
5664 : : {
5665 : 9 : int s0 = REAL_VALUE_NEGATIVE (f0);
5666 : 9 : int s1 = REAL_VALUE_NEGATIVE (f1);
5667 : :
5668 : 9 : switch (code)
5669 : : {
5670 : 0 : case PLUS:
5671 : : /* Inf + -Inf = NaN plus exception. */
5672 : 0 : if (s0 != s1)
5673 : : return 0;
5674 : : break;
5675 : 0 : case MINUS:
5676 : : /* Inf - Inf = NaN plus exception. */
5677 : 0 : if (s0 == s1)
5678 : : return 0;
5679 : : break;
5680 : : case DIV:
5681 : : /* Inf / Inf = NaN plus exception. */
5682 : : return 0;
5683 : : default:
5684 : : break;
5685 : : }
5686 : : }
5687 : :
5688 : 7992 : if (code == MULT && MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
5689 : 1964 : && flag_trapping_math
5690 : 7310 : && ((REAL_VALUE_ISINF (f0) && real_equal (&f1, &dconst0))
5691 : 1910 : || (REAL_VALUE_ISINF (f1)
5692 : 16 : && real_equal (&f0, &dconst0))))
5693 : : /* Inf * 0 = NaN plus exception. */
5694 : 24 : return 0;
5695 : :
5696 : 5368 : inexact = real_arithmetic (&value, rtx_to_tree_code (code),
5697 : : &f0, &f1);
5698 : 5368 : real_convert (&result, mode, &value);
5699 : :
5700 : : /* Don't constant fold this floating point operation if
5701 : : the result has overflowed and flag_trapping_math. */
5702 : :
5703 : 5368 : if (flag_trapping_math
5704 : 20800 : && MODE_HAS_INFINITIES (mode)
5705 : 5200 : && REAL_VALUE_ISINF (result)
5706 : 1104 : && !REAL_VALUE_ISINF (f0)
5707 : 6458 : && !REAL_VALUE_ISINF (f1))
5708 : : /* Overflow plus exception. */
5709 : 1090 : return 0;
5710 : :
5711 : : /* Don't constant fold this floating point operation if the
5712 : : result may dependent upon the run-time rounding mode and
5713 : : flag_rounding_math is set, or if GCC's software emulation
5714 : : is unable to accurately represent the result. */
5715 : :
5716 : 4278 : if ((flag_rounding_math
5717 : 27265 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
5718 : 4278 : && (inexact || !real_identical (&result, &value)))
5719 : 378 : return NULL_RTX;
5720 : :
5721 : 3900 : return const_double_from_real_value (result, mode);
5722 : : }
5723 : : }
5724 : :
5725 : : /* We can fold some multi-word operations. */
5726 : 483350496 : scalar_int_mode int_mode;
5727 : 483350496 : if (is_a <scalar_int_mode> (mode, &int_mode)
5728 : 413067108 : && CONST_SCALAR_INT_P (op0)
5729 : 40547656 : && CONST_SCALAR_INT_P (op1)
5730 : 33969715 : && GET_MODE_PRECISION (int_mode) <= MAX_BITSIZE_MODE_ANY_INT)
5731 : : {
5732 : 33969715 : wide_int result;
5733 : 33969715 : wi::overflow_type overflow;
5734 : 33969715 : rtx_mode_t pop0 = rtx_mode_t (op0, int_mode);
5735 : 33969715 : rtx_mode_t pop1 = rtx_mode_t (op1, int_mode);
5736 : :
5737 : : #if TARGET_SUPPORTS_WIDE_INT == 0
5738 : : /* This assert keeps the simplification from producing a result
5739 : : that cannot be represented in a CONST_DOUBLE but a lot of
5740 : : upstream callers expect that this function never fails to
5741 : : simplify something and so you if you added this to the test
5742 : : above the code would die later anyway. If this assert
5743 : : happens, you just need to make the port support wide int. */
5744 : : gcc_assert (GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_DOUBLE_INT);
5745 : : #endif
5746 : 33969715 : switch (code)
5747 : : {
5748 : 1125146 : case MINUS:
5749 : 1125146 : result = wi::sub (pop0, pop1);
5750 : 1125146 : break;
5751 : :
5752 : 26658175 : case PLUS:
5753 : 26658175 : result = wi::add (pop0, pop1);
5754 : 26658175 : break;
5755 : :
5756 : 310695 : case MULT:
5757 : 310695 : result = wi::mul (pop0, pop1);
5758 : 310695 : break;
5759 : :
5760 : 5591 : case DIV:
5761 : 5591 : result = wi::div_trunc (pop0, pop1, SIGNED, &overflow);
5762 : 5591 : if (overflow)
5763 : : return NULL_RTX;
5764 : : break;
5765 : :
5766 : 223 : case MOD:
5767 : 223 : result = wi::mod_trunc (pop0, pop1, SIGNED, &overflow);
5768 : 223 : if (overflow)
5769 : : return NULL_RTX;
5770 : : break;
5771 : :
5772 : 6817 : case UDIV:
5773 : 6817 : result = wi::div_trunc (pop0, pop1, UNSIGNED, &overflow);
5774 : 6817 : if (overflow)
5775 : : return NULL_RTX;
5776 : : break;
5777 : :
5778 : 16586 : case UMOD:
5779 : 16586 : result = wi::mod_trunc (pop0, pop1, UNSIGNED, &overflow);
5780 : 16586 : if (overflow)
5781 : : return NULL_RTX;
5782 : : break;
5783 : :
5784 : 606695 : case AND:
5785 : 606695 : result = wi::bit_and (pop0, pop1);
5786 : 606695 : break;
5787 : :
5788 : 307137 : case IOR:
5789 : 307137 : result = wi::bit_or (pop0, pop1);
5790 : 307137 : break;
5791 : :
5792 : 90289 : case XOR:
5793 : 90289 : result = wi::bit_xor (pop0, pop1);
5794 : 90289 : break;
5795 : :
5796 : 1773 : case SMIN:
5797 : 1773 : result = wi::smin (pop0, pop1);
5798 : 1773 : break;
5799 : :
5800 : 1833 : case SMAX:
5801 : 1833 : result = wi::smax (pop0, pop1);
5802 : 1833 : break;
5803 : :
5804 : 3212 : case UMIN:
5805 : 3212 : result = wi::umin (pop0, pop1);
5806 : 3212 : break;
5807 : :
5808 : 2805 : case UMAX:
5809 : 2805 : result = wi::umax (pop0, pop1);
5810 : 2805 : break;
5811 : :
5812 : 4789955 : case LSHIFTRT:
5813 : 4789955 : case ASHIFTRT:
5814 : 4789955 : case ASHIFT:
5815 : 4789955 : case SS_ASHIFT:
5816 : 4789955 : case US_ASHIFT:
5817 : 4789955 : {
5818 : : /* The shift count might be in SImode while int_mode might
5819 : : be narrower. On IA-64 it is even DImode. If the shift
5820 : : count is too large and doesn't fit into int_mode, we'd
5821 : : ICE. So, if int_mode is narrower than word, use
5822 : : word_mode for the shift count. */
5823 : 4789955 : if (GET_MODE (op1) == VOIDmode
5824 : 5142298 : && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD)
5825 : 1549210 : pop1 = rtx_mode_t (op1, word_mode);
5826 : :
5827 : 4789955 : wide_int wop1 = pop1;
5828 : 4789955 : if (SHIFT_COUNT_TRUNCATED)
5829 : : wop1 = wi::umod_trunc (wop1, GET_MODE_PRECISION (int_mode));
5830 : 4789955 : else if (wi::geu_p (wop1, GET_MODE_PRECISION (int_mode)))
5831 : 129 : return NULL_RTX;
5832 : :
5833 : 4789826 : switch (code)
5834 : : {
5835 : 2707063 : case LSHIFTRT:
5836 : 2707063 : result = wi::lrshift (pop0, wop1);
5837 : 2707063 : break;
5838 : :
5839 : 57632 : case ASHIFTRT:
5840 : 57632 : result = wi::arshift (pop0, wop1);
5841 : 57632 : break;
5842 : :
5843 : 2025131 : case ASHIFT:
5844 : 2025131 : result = wi::lshift (pop0, wop1);
5845 : 2025131 : break;
5846 : :
5847 : 0 : case SS_ASHIFT:
5848 : 0 : if (wi::leu_p (wop1, wi::clrsb (pop0)))
5849 : 0 : result = wi::lshift (pop0, wop1);
5850 : 0 : else if (wi::neg_p (pop0))
5851 : 0 : result = wi::min_value (int_mode, SIGNED);
5852 : : else
5853 : 0 : result = wi::max_value (int_mode, SIGNED);
5854 : : break;
5855 : :
5856 : 0 : case US_ASHIFT:
5857 : 0 : if (wi::eq_p (pop0, 0))
5858 : 0 : result = pop0;
5859 : 0 : else if (wi::leu_p (wop1, wi::clz (pop0)))
5860 : 0 : result = wi::lshift (pop0, wop1);
5861 : : else
5862 : 0 : result = wi::max_value (int_mode, UNSIGNED);
5863 : : break;
5864 : :
5865 : 0 : default:
5866 : 0 : gcc_unreachable ();
5867 : : }
5868 : 4789826 : break;
5869 : 4789955 : }
5870 : 34219 : case ROTATE:
5871 : 34219 : case ROTATERT:
5872 : 34219 : {
5873 : : /* The rotate count might be in SImode while int_mode might
5874 : : be narrower. On IA-64 it is even DImode. If the shift
5875 : : count is too large and doesn't fit into int_mode, we'd
5876 : : ICE. So, if int_mode is narrower than word, use
5877 : : word_mode for the shift count. */
5878 : 34219 : if (GET_MODE (op1) == VOIDmode
5879 : 42994 : && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD)
5880 : 20295 : pop1 = rtx_mode_t (op1, word_mode);
5881 : :
5882 : 34219 : if (wi::neg_p (pop1))
5883 : : return NULL_RTX;
5884 : :
5885 : 34149 : switch (code)
5886 : : {
5887 : 11470 : case ROTATE:
5888 : 11470 : result = wi::lrotate (pop0, pop1);
5889 : 11470 : break;
5890 : :
5891 : 22679 : case ROTATERT:
5892 : 22679 : result = wi::rrotate (pop0, pop1);
5893 : 22679 : break;
5894 : :
5895 : 0 : default:
5896 : 0 : gcc_unreachable ();
5897 : : }
5898 : : break;
5899 : : }
5900 : :
5901 : 2270 : case SS_PLUS:
5902 : 2270 : result = wi::add (pop0, pop1, SIGNED, &overflow);
5903 : 4484 : clamp_signed_saturation:
5904 : 4484 : if (overflow == wi::OVF_OVERFLOW)
5905 : 314 : result = wi::max_value (GET_MODE_PRECISION (int_mode), SIGNED);
5906 : 4170 : else if (overflow == wi::OVF_UNDERFLOW)
5907 : 278 : result = wi::min_value (GET_MODE_PRECISION (int_mode), SIGNED);
5908 : 3892 : else if (overflow != wi::OVF_NONE)
5909 : : return NULL_RTX;
5910 : : break;
5911 : :
5912 : 2220 : case US_PLUS:
5913 : 2220 : result = wi::add (pop0, pop1, UNSIGNED, &overflow);
5914 : 2220 : clamp_unsigned_saturation:
5915 : 2220 : if (overflow != wi::OVF_NONE)
5916 : 461 : result = wi::max_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
5917 : : break;
5918 : :
5919 : 2214 : case SS_MINUS:
5920 : 2214 : result = wi::sub (pop0, pop1, SIGNED, &overflow);
5921 : 2214 : goto clamp_signed_saturation;
5922 : :
5923 : 1852 : case US_MINUS:
5924 : 1852 : result = wi::sub (pop0, pop1, UNSIGNED, &overflow);
5925 : 1852 : if (overflow != wi::OVF_NONE)
5926 : 1203 : result = wi::min_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
5927 : : break;
5928 : :
5929 : 0 : case SS_MULT:
5930 : 0 : result = wi::mul (pop0, pop1, SIGNED, &overflow);
5931 : 0 : goto clamp_signed_saturation;
5932 : :
5933 : 0 : case US_MULT:
5934 : 0 : result = wi::mul (pop0, pop1, UNSIGNED, &overflow);
5935 : 0 : goto clamp_unsigned_saturation;
5936 : :
5937 : 8 : case SMUL_HIGHPART:
5938 : 8 : result = wi::mul_high (pop0, pop1, SIGNED);
5939 : 8 : break;
5940 : :
5941 : 0 : case UMUL_HIGHPART:
5942 : 0 : result = wi::mul_high (pop0, pop1, UNSIGNED);
5943 : 0 : break;
5944 : :
5945 : : default:
5946 : : return NULL_RTX;
5947 : : }
5948 : 33968971 : return immed_wide_int_const (result, int_mode);
5949 : 33969715 : }
5950 : :
5951 : : /* Handle polynomial integers. */
5952 : : if (NUM_POLY_INT_COEFFS > 1
5953 : : && is_a <scalar_int_mode> (mode, &int_mode)
5954 : : && poly_int_rtx_p (op0)
5955 : : && poly_int_rtx_p (op1))
5956 : : {
5957 : : poly_wide_int result;
5958 : : switch (code)
5959 : : {
5960 : : case PLUS:
5961 : : result = wi::to_poly_wide (op0, mode) + wi::to_poly_wide (op1, mode);
5962 : : break;
5963 : :
5964 : : case MINUS:
5965 : : result = wi::to_poly_wide (op0, mode) - wi::to_poly_wide (op1, mode);
5966 : : break;
5967 : :
5968 : : case MULT:
5969 : : if (CONST_SCALAR_INT_P (op1))
5970 : : result = wi::to_poly_wide (op0, mode) * rtx_mode_t (op1, mode);
5971 : : else
5972 : : return NULL_RTX;
5973 : : break;
5974 : :
5975 : : case ASHIFT:
5976 : : if (CONST_SCALAR_INT_P (op1))
5977 : : {
5978 : : wide_int shift
5979 : : = rtx_mode_t (op1,
5980 : : GET_MODE (op1) == VOIDmode
5981 : : && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD
5982 : : ? word_mode : mode);
5983 : : if (SHIFT_COUNT_TRUNCATED)
5984 : : shift = wi::umod_trunc (shift, GET_MODE_PRECISION (int_mode));
5985 : : else if (wi::geu_p (shift, GET_MODE_PRECISION (int_mode)))
5986 : : return NULL_RTX;
5987 : : result = wi::to_poly_wide (op0, mode) << shift;
5988 : : }
5989 : : else
5990 : : return NULL_RTX;
5991 : : break;
5992 : :
5993 : : case IOR:
5994 : : if (!CONST_SCALAR_INT_P (op1)
5995 : : || !can_ior_p (wi::to_poly_wide (op0, mode),
5996 : : rtx_mode_t (op1, mode), &result))
5997 : : return NULL_RTX;
5998 : : break;
5999 : :
6000 : : default:
6001 : : return NULL_RTX;
6002 : : }
6003 : : return immed_wide_int_const (result, int_mode);
6004 : : }
6005 : :
6006 : : return NULL_RTX;
6007 : : }
6008 : :
6009 : :
6010 : :
6011 : : /* Return a positive integer if X should sort after Y. The value
6012 : : returned is 1 if and only if X and Y are both regs. */
6013 : :
6014 : : static int
6015 : 114288641 : simplify_plus_minus_op_data_cmp (rtx x, rtx y)
6016 : : {
6017 : 114288641 : int result;
6018 : :
6019 : 114288641 : result = (commutative_operand_precedence (y)
6020 : 114288641 : - commutative_operand_precedence (x));
6021 : 114288641 : if (result)
6022 : 79967936 : return result + result;
6023 : :
6024 : : /* Group together equal REGs to do more simplification. */
6025 : 34320705 : if (REG_P (x) && REG_P (y))
6026 : 8438775 : return REGNO (x) > REGNO (y);
6027 : :
6028 : : return 0;
6029 : : }
6030 : :
6031 : : /* Simplify and canonicalize a PLUS or MINUS, at least one of whose
6032 : : operands may be another PLUS or MINUS.
6033 : :
6034 : : Rather than test for specific case, we do this by a brute-force method
6035 : : and do all possible simplifications until no more changes occur. Then
6036 : : we rebuild the operation.
6037 : :
6038 : : May return NULL_RTX when no changes were made. */
6039 : :
6040 : : rtx
6041 : 38347764 : simplify_context::simplify_plus_minus (rtx_code code, machine_mode mode,
6042 : : rtx op0, rtx op1)
6043 : : {
6044 : 38347764 : struct simplify_plus_minus_op_data
6045 : : {
6046 : : rtx op;
6047 : : short neg;
6048 : : } ops[16];
6049 : 38347764 : rtx result, tem;
6050 : 38347764 : int n_ops = 2;
6051 : 38347764 : int changed, n_constants, canonicalized = 0;
6052 : 38347764 : int i, j;
6053 : :
6054 : 38347764 : memset (ops, 0, sizeof ops);
6055 : :
6056 : : /* Set up the two operands and then expand them until nothing has been
6057 : : changed. If we run out of room in our array, give up; this should
6058 : : almost never happen. */
6059 : :
6060 : 38347764 : ops[0].op = op0;
6061 : 38347764 : ops[0].neg = 0;
6062 : 38347764 : ops[1].op = op1;
6063 : 38347764 : ops[1].neg = (code == MINUS);
6064 : :
6065 : 78029980 : do
6066 : : {
6067 : 78029980 : changed = 0;
6068 : 78029980 : n_constants = 0;
6069 : :
6070 : 315944694 : for (i = 0; i < n_ops; i++)
6071 : : {
6072 : 237914728 : rtx this_op = ops[i].op;
6073 : 237914728 : int this_neg = ops[i].neg;
6074 : 237914728 : enum rtx_code this_code = GET_CODE (this_op);
6075 : :
6076 : 237914728 : switch (this_code)
6077 : : {
6078 : 38832427 : case PLUS:
6079 : 38832427 : case MINUS:
6080 : 38832427 : if (n_ops == ARRAY_SIZE (ops))
6081 : : return NULL_RTX;
6082 : :
6083 : 38832413 : ops[n_ops].op = XEXP (this_op, 1);
6084 : 38832413 : ops[n_ops].neg = (this_code == MINUS) ^ this_neg;
6085 : 38832413 : n_ops++;
6086 : :
6087 : 38832413 : ops[i].op = XEXP (this_op, 0);
6088 : 38832413 : changed = 1;
6089 : : /* If this operand was negated then we will potentially
6090 : : canonicalize the expression. Similarly if we don't
6091 : : place the operands adjacent we're re-ordering the
6092 : : expression and thus might be performing a
6093 : : canonicalization. Ignore register re-ordering.
6094 : : ??? It might be better to shuffle the ops array here,
6095 : : but then (plus (plus (A, B), plus (C, D))) wouldn't
6096 : : be seen as non-canonical. */
6097 : 38832413 : if (this_neg
6098 : 38095484 : || (i != n_ops - 2
6099 : 37409142 : && !(REG_P (ops[i].op) && REG_P (ops[n_ops - 1].op))))
6100 : 237914714 : canonicalized = 1;
6101 : : break;
6102 : :
6103 : 1679 : case NEG:
6104 : 1679 : ops[i].op = XEXP (this_op, 0);
6105 : 1679 : ops[i].neg = ! this_neg;
6106 : 1679 : changed = 1;
6107 : 1679 : canonicalized = 1;
6108 : 1679 : break;
6109 : :
6110 : 1475423 : case CONST:
6111 : 1475423 : if (n_ops != ARRAY_SIZE (ops)
6112 : 1475423 : && GET_CODE (XEXP (this_op, 0)) == PLUS
6113 : 1354700 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 0))
6114 : 1331592 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 1)))
6115 : : {
6116 : 1331592 : ops[i].op = XEXP (XEXP (this_op, 0), 0);
6117 : 1331592 : ops[n_ops].op = XEXP (XEXP (this_op, 0), 1);
6118 : 1331592 : ops[n_ops].neg = this_neg;
6119 : 1331592 : n_ops++;
6120 : 1331592 : changed = 1;
6121 : 1331592 : canonicalized = 1;
6122 : : }
6123 : : break;
6124 : :
6125 : 41143 : case NOT:
6126 : : /* ~a -> (-a - 1) */
6127 : 41143 : if (n_ops != ARRAY_SIZE (ops))
6128 : : {
6129 : 41143 : ops[n_ops].op = CONSTM1_RTX (mode);
6130 : 41143 : ops[n_ops++].neg = this_neg;
6131 : 41143 : ops[i].op = XEXP (this_op, 0);
6132 : 41143 : ops[i].neg = !this_neg;
6133 : 41143 : changed = 1;
6134 : 41143 : canonicalized = 1;
6135 : : }
6136 : : break;
6137 : :
6138 : 120461097 : CASE_CONST_SCALAR_INT:
6139 : 120461097 : case CONST_POLY_INT:
6140 : 120461097 : n_constants++;
6141 : 120461097 : if (this_neg)
6142 : : {
6143 : 1193311 : ops[i].op = neg_poly_int_rtx (mode, this_op);
6144 : 1193311 : ops[i].neg = 0;
6145 : 1193311 : changed = 1;
6146 : 1193311 : canonicalized = 1;
6147 : : }
6148 : : break;
6149 : :
6150 : : default:
6151 : : break;
6152 : : }
6153 : : }
6154 : : }
6155 : 78029966 : while (changed);
6156 : :
6157 : 38347750 : if (n_constants > 1)
6158 : 24177037 : canonicalized = 1;
6159 : :
6160 : 38347750 : gcc_assert (n_ops >= 2);
6161 : :
6162 : : /* If we only have two operands, we can avoid the loops. */
6163 : 38347750 : if (n_ops == 2)
6164 : : {
6165 : 0 : enum rtx_code code = ops[0].neg || ops[1].neg ? MINUS : PLUS;
6166 : 0 : rtx lhs, rhs;
6167 : :
6168 : : /* Get the two operands. Be careful with the order, especially for
6169 : : the cases where code == MINUS. */
6170 : 0 : if (ops[0].neg && ops[1].neg)
6171 : : {
6172 : 0 : lhs = gen_rtx_NEG (mode, ops[0].op);
6173 : 0 : rhs = ops[1].op;
6174 : : }
6175 : 0 : else if (ops[0].neg)
6176 : : {
6177 : 0 : lhs = ops[1].op;
6178 : 0 : rhs = ops[0].op;
6179 : : }
6180 : : else
6181 : : {
6182 : 0 : lhs = ops[0].op;
6183 : 0 : rhs = ops[1].op;
6184 : : }
6185 : :
6186 : 0 : return simplify_const_binary_operation (code, mode, lhs, rhs);
6187 : : }
6188 : :
6189 : : /* Now simplify each pair of operands until nothing changes. */
6190 : 63331671 : while (1)
6191 : : {
6192 : : /* Insertion sort is good enough for a small array. */
6193 : 167322392 : for (i = 1; i < n_ops; i++)
6194 : : {
6195 : 103990721 : struct simplify_plus_minus_op_data save;
6196 : 103990721 : int cmp;
6197 : :
6198 : 103990721 : j = i - 1;
6199 : 103990721 : cmp = simplify_plus_minus_op_data_cmp (ops[j].op, ops[i].op);
6200 : 103990721 : if (cmp <= 0)
6201 : 91907321 : continue;
6202 : : /* Just swapping registers doesn't count as canonicalization. */
6203 : 12083400 : if (cmp != 1)
6204 : 9196385 : canonicalized = 1;
6205 : :
6206 : 12083400 : save = ops[i];
6207 : 14489346 : do
6208 : 14489346 : ops[j + 1] = ops[j];
6209 : 14489346 : while (j--
6210 : 26572746 : && simplify_plus_minus_op_data_cmp (ops[j].op, save.op) > 0);
6211 : 12083400 : ops[j + 1] = save;
6212 : : }
6213 : :
6214 : 63331671 : changed = 0;
6215 : 167322392 : for (i = n_ops - 1; i > 0; i--)
6216 : 249567314 : for (j = i - 1; j >= 0; j--)
6217 : : {
6218 : 146378983 : rtx lhs = ops[j].op, rhs = ops[i].op;
6219 : 146378983 : int lneg = ops[j].neg, rneg = ops[i].neg;
6220 : :
6221 : 146378983 : if (lhs != 0 && rhs != 0)
6222 : : {
6223 : 120581502 : enum rtx_code ncode = PLUS;
6224 : :
6225 : 120581502 : if (lneg != rneg)
6226 : : {
6227 : 10298753 : ncode = MINUS;
6228 : 10298753 : if (lneg)
6229 : 6664179 : std::swap (lhs, rhs);
6230 : : }
6231 : 110282749 : else if (swap_commutative_operands_p (lhs, rhs))
6232 : 222858 : std::swap (lhs, rhs);
6233 : :
6234 : 120581502 : if ((GET_CODE (lhs) == CONST || CONST_INT_P (lhs))
6235 : 28580916 : && (GET_CODE (rhs) == CONST || CONST_INT_P (rhs)))
6236 : : {
6237 : 24328480 : rtx tem_lhs, tem_rhs;
6238 : :
6239 : 24328480 : tem_lhs = GET_CODE (lhs) == CONST ? XEXP (lhs, 0) : lhs;
6240 : 24328480 : tem_rhs = GET_CODE (rhs) == CONST ? XEXP (rhs, 0) : rhs;
6241 : 24328480 : tem = simplify_binary_operation (ncode, mode, tem_lhs,
6242 : : tem_rhs);
6243 : :
6244 : 24328480 : if (tem && !CONSTANT_P (tem))
6245 : 1743 : tem = gen_rtx_CONST (GET_MODE (tem), tem);
6246 : : }
6247 : : else
6248 : 96253022 : tem = simplify_binary_operation (ncode, mode, lhs, rhs);
6249 : :
6250 : 96254765 : if (tem)
6251 : : {
6252 : : /* Reject "simplifications" that just wrap the two
6253 : : arguments in a CONST. Failure to do so can result
6254 : : in infinite recursion with simplify_binary_operation
6255 : : when it calls us to simplify CONST operations.
6256 : : Also, if we find such a simplification, don't try
6257 : : any more combinations with this rhs: We must have
6258 : : something like symbol+offset, ie. one of the
6259 : : trivial CONST expressions we handle later. */
6260 : 26276562 : if (GET_CODE (tem) == CONST
6261 : 804133 : && GET_CODE (XEXP (tem, 0)) == ncode
6262 : 803587 : && XEXP (XEXP (tem, 0), 0) == lhs
6263 : 802390 : && XEXP (XEXP (tem, 0), 1) == rhs)
6264 : : break;
6265 : 25474172 : lneg &= rneg;
6266 : 25474172 : if (GET_CODE (tem) == NEG)
6267 : 45559 : tem = XEXP (tem, 0), lneg = !lneg;
6268 : 25474172 : if (poly_int_rtx_p (tem) && lneg)
6269 : 0 : tem = neg_poly_int_rtx (mode, tem), lneg = 0;
6270 : :
6271 : 25474172 : ops[i].op = tem;
6272 : 25474172 : ops[i].neg = lneg;
6273 : 25474172 : ops[j].op = NULL_RTX;
6274 : 25474172 : changed = 1;
6275 : 25474172 : canonicalized = 1;
6276 : : }
6277 : : }
6278 : : }
6279 : :
6280 : 63331671 : if (!changed)
6281 : : break;
6282 : :
6283 : : /* Pack all the operands to the lower-numbered entries. */
6284 : 100880033 : for (i = 0, j = 0; j < n_ops; j++)
6285 : 75896112 : if (ops[j].op)
6286 : : {
6287 : 50421940 : ops[i] = ops[j];
6288 : 50421940 : i++;
6289 : : }
6290 : : n_ops = i;
6291 : : }
6292 : :
6293 : : /* If nothing changed, check that rematerialization of rtl instructions
6294 : : is still required. */
6295 : 38347750 : if (!canonicalized)
6296 : : {
6297 : : /* Perform rematerialization if only all operands are registers and
6298 : : all operations are PLUS. */
6299 : : /* ??? Also disallow (non-global, non-frame) fixed registers to work
6300 : : around rs6000 and how it uses the CA register. See PR67145. */
6301 : 5063489 : for (i = 0; i < n_ops; i++)
6302 : 4079372 : if (ops[i].neg
6303 : 3818669 : || !REG_P (ops[i].op)
6304 : 7321884 : || (REGNO (ops[i].op) < FIRST_PSEUDO_REGISTER
6305 : 318958 : && fixed_regs[REGNO (ops[i].op)]
6306 : 539 : && !global_regs[REGNO (ops[i].op)]
6307 : 539 : && ops[i].op != frame_pointer_rtx
6308 : 108 : && ops[i].op != arg_pointer_rtx
6309 : 94 : && ops[i].op != stack_pointer_rtx))
6310 : : return NULL_RTX;
6311 : 984117 : goto gen_result;
6312 : : }
6313 : :
6314 : : /* Create (minus -C X) instead of (neg (const (plus X C))). */
6315 : 36526773 : if (n_ops == 2
6316 : 23275021 : && CONST_INT_P (ops[1].op)
6317 : 22805030 : && CONSTANT_P (ops[0].op)
6318 : 211 : && ops[0].neg)
6319 : 105 : return gen_rtx_fmt_ee (MINUS, mode, ops[1].op, ops[0].op);
6320 : :
6321 : : /* We suppressed creation of trivial CONST expressions in the
6322 : : combination loop to avoid recursion. Create one manually now.
6323 : : The combination loop should have ensured that there is exactly
6324 : : one CONST_INT, and the sort will have ensured that it is last
6325 : : in the array and that any other constant will be next-to-last. */
6326 : :
6327 : 36526668 : if (n_ops > 1
6328 : 35968951 : && poly_int_rtx_p (ops[n_ops - 1].op)
6329 : 70281068 : && CONSTANT_P (ops[n_ops - 2].op))
6330 : : {
6331 : 1399477 : rtx value = ops[n_ops - 1].op;
6332 : 1399477 : if (ops[n_ops - 1].neg ^ ops[n_ops - 2].neg)
6333 : 682853 : value = neg_poly_int_rtx (mode, value);
6334 : 1399477 : if (CONST_INT_P (value))
6335 : : {
6336 : 2798954 : ops[n_ops - 2].op = plus_constant (mode, ops[n_ops - 2].op,
6337 : 1399477 : INTVAL (value));
6338 : 1399477 : n_ops--;
6339 : : }
6340 : : }
6341 : :
6342 : : /* Put a non-negated operand first, if possible. */
6343 : :
6344 : 38207590 : for (i = 0; i < n_ops && ops[i].neg; i++)
6345 : 1680922 : continue;
6346 : 36526668 : if (i == n_ops)
6347 : 9804 : ops[0].op = gen_rtx_NEG (mode, ops[0].op);
6348 : 36516864 : else if (i != 0)
6349 : : {
6350 : 1580859 : tem = ops[0].op;
6351 : 1580859 : ops[0] = ops[i];
6352 : 1580859 : ops[i].op = tem;
6353 : 1580859 : ops[i].neg = 1;
6354 : : }
6355 : :
6356 : : /* Now make the result by performing the requested operations. */
6357 : 34936005 : gen_result:
6358 : 37510785 : result = ops[0].op;
6359 : 87514156 : for (i = 1; i < n_ops; i++)
6360 : 100006742 : result = gen_rtx_fmt_ee (ops[i].neg ? MINUS : PLUS,
6361 : : mode, result, ops[i].op);
6362 : :
6363 : : return result;
6364 : 1680922 : }
6365 : :
6366 : : /* Check whether an operand is suitable for calling simplify_plus_minus. */
6367 : : static bool
6368 : 518117672 : plus_minus_operand_p (const_rtx x)
6369 : : {
6370 : 518117672 : return GET_CODE (x) == PLUS
6371 : 518117672 : || GET_CODE (x) == MINUS
6372 : 518117672 : || (GET_CODE (x) == CONST
6373 : 1843983 : && GET_CODE (XEXP (x, 0)) == PLUS
6374 : 1238659 : && CONSTANT_P (XEXP (XEXP (x, 0), 0))
6375 : 1162055 : && CONSTANT_P (XEXP (XEXP (x, 0), 1)));
6376 : : }
6377 : :
6378 : : /* Like simplify_binary_operation except used for relational operators.
6379 : : MODE is the mode of the result. If MODE is VOIDmode, both operands must
6380 : : not also be VOIDmode.
6381 : :
6382 : : CMP_MODE specifies in which mode the comparison is done in, so it is
6383 : : the mode of the operands. If CMP_MODE is VOIDmode, it is taken from
6384 : : the operands or, if both are VOIDmode, the operands are compared in
6385 : : "infinite precision". */
6386 : : rtx
6387 : 131471491 : simplify_context::simplify_relational_operation (rtx_code code,
6388 : : machine_mode mode,
6389 : : machine_mode cmp_mode,
6390 : : rtx op0, rtx op1)
6391 : : {
6392 : 131471491 : rtx tem, trueop0, trueop1;
6393 : :
6394 : 131471491 : if (cmp_mode == VOIDmode)
6395 : 28548999 : cmp_mode = GET_MODE (op0);
6396 : 28548999 : if (cmp_mode == VOIDmode)
6397 : 461196 : cmp_mode = GET_MODE (op1);
6398 : :
6399 : 131471491 : tem = simplify_const_relational_operation (code, cmp_mode, op0, op1);
6400 : 131471491 : if (tem)
6401 : 861225 : return relational_result (mode, cmp_mode, tem);
6402 : :
6403 : : /* For the following tests, ensure const0_rtx is op1. */
6404 : 130610266 : if (swap_commutative_operands_p (op0, op1)
6405 : 130610266 : || (op0 == const0_rtx && op1 != const0_rtx))
6406 : 2661691 : std::swap (op0, op1), code = swap_condition (code);
6407 : :
6408 : : /* If op0 is a compare, extract the comparison arguments from it. */
6409 : 130610266 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6410 : 13903748 : return simplify_gen_relational (code, mode, VOIDmode,
6411 : 13903748 : XEXP (op0, 0), XEXP (op0, 1));
6412 : :
6413 : 116706518 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC)
6414 : : return NULL_RTX;
6415 : :
6416 : 85669228 : trueop0 = avoid_constant_pool_reference (op0);
6417 : 85669228 : trueop1 = avoid_constant_pool_reference (op1);
6418 : 85669228 : return simplify_relational_operation_1 (code, mode, cmp_mode,
6419 : 85669228 : trueop0, trueop1);
6420 : : }
6421 : :
6422 : : /* This part of simplify_relational_operation is only used when CMP_MODE
6423 : : is not in class MODE_CC (i.e. it is a real comparison).
6424 : :
6425 : : MODE is the mode of the result, while CMP_MODE specifies in which
6426 : : mode the comparison is done in, so it is the mode of the operands. */
6427 : :
6428 : : rtx
6429 : 85669228 : simplify_context::simplify_relational_operation_1 (rtx_code code,
6430 : : machine_mode mode,
6431 : : machine_mode cmp_mode,
6432 : : rtx op0, rtx op1)
6433 : : {
6434 : 85669228 : enum rtx_code op0code = GET_CODE (op0);
6435 : :
6436 : 85669228 : if (op1 == const0_rtx && COMPARISON_P (op0))
6437 : : {
6438 : : /* If op0 is a comparison, extract the comparison arguments
6439 : : from it. */
6440 : 468589 : if (code == NE)
6441 : : {
6442 : 215161 : if (GET_MODE (op0) == mode)
6443 : 164 : return simplify_rtx (op0);
6444 : : else
6445 : 214997 : return simplify_gen_relational (GET_CODE (op0), mode, VOIDmode,
6446 : 214997 : XEXP (op0, 0), XEXP (op0, 1));
6447 : : }
6448 : 253428 : else if (code == EQ)
6449 : : {
6450 : 127911 : enum rtx_code new_code = reversed_comparison_code (op0, NULL);
6451 : 127911 : if (new_code != UNKNOWN)
6452 : 127607 : return simplify_gen_relational (new_code, mode, VOIDmode,
6453 : 127607 : XEXP (op0, 0), XEXP (op0, 1));
6454 : : }
6455 : : }
6456 : :
6457 : : /* (LTU/GEU (PLUS a C) C), where C is constant, can be simplified to
6458 : : (GEU/LTU a -C). Likewise for (LTU/GEU (PLUS a C) a). */
6459 : 85326460 : if ((code == LTU || code == GEU)
6460 : 5085500 : && GET_CODE (op0) == PLUS
6461 : 679793 : && CONST_INT_P (XEXP (op0, 1))
6462 : 452301 : && (rtx_equal_p (op1, XEXP (op0, 0))
6463 : 333736 : || rtx_equal_p (op1, XEXP (op0, 1)))
6464 : : /* (LTU/GEU (PLUS a 0) 0) is not the same as (GEU/LTU a 0). */
6465 : 85512481 : && XEXP (op0, 1) != const0_rtx)
6466 : : {
6467 : 186021 : rtx new_cmp
6468 : 186021 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6469 : 188290 : return simplify_gen_relational ((code == LTU ? GEU : LTU), mode,
6470 : 186021 : cmp_mode, XEXP (op0, 0), new_cmp);
6471 : : }
6472 : :
6473 : : /* (GTU (PLUS a C) (C - 1)) where C is a non-zero constant can be
6474 : : transformed into (LTU a -C). */
6475 : 85140439 : if (code == GTU && GET_CODE (op0) == PLUS && CONST_INT_P (op1)
6476 : 374396 : && CONST_INT_P (XEXP (op0, 1))
6477 : 300970 : && (UINTVAL (op1) == UINTVAL (XEXP (op0, 1)) - 1)
6478 : 28889 : && XEXP (op0, 1) != const0_rtx)
6479 : : {
6480 : 28889 : rtx new_cmp
6481 : 28889 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6482 : 28889 : return simplify_gen_relational (LTU, mode, cmp_mode,
6483 : 28889 : XEXP (op0, 0), new_cmp);
6484 : : }
6485 : :
6486 : : /* Canonicalize (LTU/GEU (PLUS a b) b) as (LTU/GEU (PLUS a b) a). */
6487 : 85111550 : if ((code == LTU || code == GEU)
6488 : 4899479 : && GET_CODE (op0) == PLUS
6489 : 493772 : && rtx_equal_p (op1, XEXP (op0, 1))
6490 : : /* Don't recurse "infinitely" for (LTU/GEU (PLUS b b) b). */
6491 : 85116906 : && !rtx_equal_p (op1, XEXP (op0, 0)))
6492 : 5356 : return simplify_gen_relational (code, mode, cmp_mode, op0,
6493 : 5356 : copy_rtx (XEXP (op0, 0)));
6494 : :
6495 : 85106194 : if (op1 == const0_rtx)
6496 : : {
6497 : : /* Canonicalize (GTU x 0) as (NE x 0). */
6498 : 37796413 : if (code == GTU)
6499 : 176469 : return simplify_gen_relational (NE, mode, cmp_mode, op0, op1);
6500 : : /* Canonicalize (LEU x 0) as (EQ x 0). */
6501 : 37619944 : if (code == LEU)
6502 : 51108 : return simplify_gen_relational (EQ, mode, cmp_mode, op0, op1);
6503 : :
6504 : 37568836 : if ((code == NE || code == EQ)
6505 : : /* Verify op0 is IOR */
6506 : 33964044 : && GET_CODE (op0) == IOR
6507 : : /* only enters if op1 is 0 */
6508 : : /* Verify IOR operand is NE */
6509 : 537287 : && GET_CODE (XEXP (op0, 0)) == NE
6510 : : /* Verify second NE operand is 0 */
6511 : 6207 : && XEXP (XEXP (op0, 0), 1) == CONST0_RTX (mode))
6512 : : {
6513 : 6207 : rtx t = gen_rtx_IOR (cmp_mode, XEXP (XEXP (op0, 0), 0), XEXP (op0, 1));
6514 : 6207 : t = gen_rtx_fmt_ee (code, mode, t, CONST0_RTX (mode));
6515 : 6207 : return t;
6516 : : }
6517 : :
6518 : : }
6519 : 47309781 : else if (op1 == const1_rtx)
6520 : : {
6521 : 3261669 : switch (code)
6522 : : {
6523 : 9066 : case GE:
6524 : : /* Canonicalize (GE x 1) as (GT x 0). */
6525 : 9066 : return simplify_gen_relational (GT, mode, cmp_mode,
6526 : 9066 : op0, const0_rtx);
6527 : 177650 : case GEU:
6528 : : /* Canonicalize (GEU x 1) as (NE x 0). */
6529 : 177650 : return simplify_gen_relational (NE, mode, cmp_mode,
6530 : 177650 : op0, const0_rtx);
6531 : 10522 : case LT:
6532 : : /* Canonicalize (LT x 1) as (LE x 0). */
6533 : 10522 : return simplify_gen_relational (LE, mode, cmp_mode,
6534 : 10522 : op0, const0_rtx);
6535 : 55905 : case LTU:
6536 : : /* Canonicalize (LTU x 1) as (EQ x 0). */
6537 : 55905 : return simplify_gen_relational (EQ, mode, cmp_mode,
6538 : 55905 : op0, const0_rtx);
6539 : : default:
6540 : : break;
6541 : : }
6542 : : }
6543 : 44048112 : else if (op1 == constm1_rtx)
6544 : : {
6545 : : /* Canonicalize (LE x -1) as (LT x 0). */
6546 : 1186180 : if (code == LE)
6547 : 1662 : return simplify_gen_relational (LT, mode, cmp_mode, op0, const0_rtx);
6548 : : /* Canonicalize (GT x -1) as (GE x 0). */
6549 : 1184518 : if (code == GT)
6550 : 5039 : return simplify_gen_relational (GE, mode, cmp_mode, op0, const0_rtx);
6551 : : }
6552 : :
6553 : : /* (eq/ne (plus x cst1) cst2) simplifies to (eq/ne x (cst2 - cst1)) */
6554 : 81007774 : if ((code == EQ || code == NE)
6555 : 63654671 : && (op0code == PLUS || op0code == MINUS)
6556 : 2372087 : && CONSTANT_P (op1)
6557 : 981130 : && CONSTANT_P (XEXP (op0, 1))
6558 : 542742 : && (INTEGRAL_MODE_P (cmp_mode) || flag_unsafe_math_optimizations))
6559 : : {
6560 : 542710 : rtx x = XEXP (op0, 0);
6561 : 542710 : rtx c = XEXP (op0, 1);
6562 : 542710 : enum rtx_code invcode = op0code == PLUS ? MINUS : PLUS;
6563 : 542710 : rtx tem = simplify_gen_binary (invcode, cmp_mode, op1, c);
6564 : :
6565 : : /* Detect an infinite recursive condition, where we oscillate at this
6566 : : simplification case between:
6567 : : A + B == C <---> C - B == A,
6568 : : where A, B, and C are all constants with non-simplifiable expressions,
6569 : : usually SYMBOL_REFs. */
6570 : 542710 : if (GET_CODE (tem) == invcode
6571 : 46 : && CONSTANT_P (x)
6572 : 542728 : && rtx_equal_p (c, XEXP (tem, 1)))
6573 : : return NULL_RTX;
6574 : :
6575 : 542692 : return simplify_gen_relational (code, mode, cmp_mode, x, tem);
6576 : : }
6577 : :
6578 : : /* (ne:SI (zero_extract:SI FOO (const_int 1) BAR) (const_int 0))) is
6579 : : the same as (zero_extract:SI FOO (const_int 1) BAR). */
6580 : 63111961 : scalar_int_mode int_mode, int_cmp_mode;
6581 : 63111961 : if (code == NE
6582 : 33280271 : && op1 == const0_rtx
6583 : 2206078 : && is_int_mode (mode, &int_mode)
6584 : 84015400 : && is_a <scalar_int_mode> (cmp_mode, &int_cmp_mode)
6585 : : /* ??? Work-around BImode bugs in the ia64 backend. */
6586 : 2206078 : && int_mode != BImode
6587 : 2206054 : && int_cmp_mode != BImode
6588 : 2206054 : && nonzero_bits (op0, int_cmp_mode) == 1
6589 : 63111961 : && STORE_FLAG_VALUE == 1)
6590 : 108912 : return GET_MODE_SIZE (int_mode) > GET_MODE_SIZE (int_cmp_mode)
6591 : 54456 : ? simplify_gen_unary (ZERO_EXTEND, int_mode, op0, int_cmp_mode)
6592 : 22913 : : lowpart_subreg (int_mode, op0, int_cmp_mode);
6593 : :
6594 : : /* (eq/ne (xor x y) 0) simplifies to (eq/ne x y). */
6595 : 84015400 : if ((code == EQ || code == NE)
6596 : 63057505 : && op1 == const0_rtx
6597 : 33825923 : && op0code == XOR)
6598 : 14088 : return simplify_gen_relational (code, mode, cmp_mode,
6599 : 14088 : XEXP (op0, 0), XEXP (op0, 1));
6600 : :
6601 : : /* (eq/ne (xor x y) x) simplifies to (eq/ne y 0). */
6602 : 63043417 : if ((code == EQ || code == NE)
6603 : 63043417 : && op0code == XOR
6604 : 4598 : && rtx_equal_p (XEXP (op0, 0), op1)
6605 : 0 : && !side_effects_p (XEXP (op0, 0)))
6606 : 0 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 1),
6607 : 0 : CONST0_RTX (mode));
6608 : :
6609 : : /* Likewise (eq/ne (xor x y) y) simplifies to (eq/ne x 0). */
6610 : 84001312 : if ((code == EQ || code == NE)
6611 : 63043417 : && op0code == XOR
6612 : 4598 : && rtx_equal_p (XEXP (op0, 1), op1)
6613 : 84001446 : && !side_effects_p (XEXP (op0, 1)))
6614 : 134 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6615 : 134 : CONST0_RTX (mode));
6616 : :
6617 : : /* (eq/ne (xor x C1) C2) simplifies to (eq/ne x (C1^C2)). */
6618 : 84001178 : if ((code == EQ || code == NE)
6619 : 63043283 : && op0code == XOR
6620 : 4464 : && CONST_SCALAR_INT_P (op1)
6621 : 1142 : && CONST_SCALAR_INT_P (XEXP (op0, 1)))
6622 : 602 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6623 : : simplify_gen_binary (XOR, cmp_mode,
6624 : 602 : XEXP (op0, 1), op1));
6625 : :
6626 : : /* Simplify eq/ne (and/ior x y) x/y) for targets with a BICS instruction or
6627 : : constant folding if x/y is a constant. */
6628 : 63042681 : if ((code == EQ || code == NE)
6629 : 63042681 : && (op0code == AND || op0code == IOR)
6630 : 4047583 : && !side_effects_p (op1)
6631 : 4047539 : && op1 != CONST0_RTX (cmp_mode))
6632 : : {
6633 : : /* Both (eq/ne (and x y) x) and (eq/ne (ior x y) y) simplify to
6634 : : (eq/ne (and (not y) x) 0). */
6635 : 543423 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 0), op1))
6636 : 1083794 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 1), op1)))
6637 : : {
6638 : 25298 : rtx not_y = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 1),
6639 : : cmp_mode);
6640 : 25298 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_y, XEXP (op0, 0));
6641 : :
6642 : 25298 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
6643 : 25298 : CONST0_RTX (cmp_mode));
6644 : : }
6645 : :
6646 : : /* Both (eq/ne (and x y) y) and (eq/ne (ior x y) x) simplify to
6647 : : (eq/ne (and (not x) y) 0). */
6648 : 518193 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 1), op1))
6649 : 1013352 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 0), op1)))
6650 : : {
6651 : 45164 : rtx not_x = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 0),
6652 : : cmp_mode);
6653 : 45164 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_x, XEXP (op0, 1));
6654 : :
6655 : 45164 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
6656 : 45164 : CONST0_RTX (cmp_mode));
6657 : : }
6658 : : }
6659 : :
6660 : : /* (eq/ne (bswap x) C1) simplifies to (eq/ne x C2) with C2 swapped. */
6661 : 83930114 : if ((code == EQ || code == NE)
6662 : 62972219 : && GET_CODE (op0) == BSWAP
6663 : 324 : && CONST_SCALAR_INT_P (op1))
6664 : 93 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6665 : : simplify_gen_unary (BSWAP, cmp_mode,
6666 : 93 : op1, cmp_mode));
6667 : :
6668 : : /* (eq/ne (bswap x) (bswap y)) simplifies to (eq/ne x y). */
6669 : 62972126 : if ((code == EQ || code == NE)
6670 : 62972126 : && GET_CODE (op0) == BSWAP
6671 : 231 : && GET_CODE (op1) == BSWAP)
6672 : 18 : return simplify_gen_relational (code, mode, cmp_mode,
6673 : 18 : XEXP (op0, 0), XEXP (op1, 0));
6674 : :
6675 : 83930003 : if (op0code == POPCOUNT && op1 == const0_rtx)
6676 : 0 : switch (code)
6677 : : {
6678 : 0 : case EQ:
6679 : 0 : case LE:
6680 : 0 : case LEU:
6681 : : /* (eq (popcount x) (const_int 0)) -> (eq x (const_int 0)). */
6682 : 0 : return simplify_gen_relational (EQ, mode, GET_MODE (XEXP (op0, 0)),
6683 : : XEXP (op0, 0),
6684 : 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
6685 : :
6686 : 0 : case NE:
6687 : 0 : case GT:
6688 : 0 : case GTU:
6689 : : /* (ne (popcount x) (const_int 0)) -> (ne x (const_int 0)). */
6690 : 0 : return simplify_gen_relational (NE, mode, GET_MODE (XEXP (op0, 0)),
6691 : : XEXP (op0, 0),
6692 : 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
6693 : :
6694 : : default:
6695 : : break;
6696 : : }
6697 : :
6698 : : /* (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) -> (and:SI x 1). */
6699 : 83930003 : if (code == NE
6700 : 33179470 : && op1 == const0_rtx
6701 : 16537874 : && (op0code == TRUNCATE
6702 : 148758 : || (partial_subreg_p (op0)
6703 : 148094 : && subreg_lowpart_p (op0)))
6704 : 125610 : && SCALAR_INT_MODE_P (mode)
6705 : 83930003 : && STORE_FLAG_VALUE == 1)
6706 : : {
6707 : 30561 : rtx tmp = XEXP (op0, 0);
6708 : 30561 : if (GET_CODE (tmp) == ASHIFT
6709 : 1884 : && GET_MODE (tmp) == mode
6710 : 188 : && CONST_INT_P (XEXP (tmp, 1))
6711 : 188 : && is_int_mode (GET_MODE (op0), &int_mode)
6712 : 30749 : && INTVAL (XEXP (tmp, 1)) == GET_MODE_PRECISION (int_mode) - 1)
6713 : 188 : return simplify_gen_binary (AND, mode, XEXP (tmp, 0), const1_rtx);
6714 : : }
6715 : :
6716 : : /* For two unsigned booleans A and B:
6717 : :
6718 : : A > B == ~B & A
6719 : : A >= B == ~B | A
6720 : : A < B == ~A & B
6721 : : A <= B == ~A | B
6722 : : A == B == ~A ^ B (== ~B ^ A)
6723 : : A != B == A ^ B
6724 : :
6725 : : For signed comparisons, we have to take STORE_FLAG_VALUE into account,
6726 : : with the rules above applying for positive STORE_FLAG_VALUE and with
6727 : : the relations reversed for negative STORE_FLAG_VALUE. */
6728 : 83929815 : if (is_a<scalar_int_mode> (cmp_mode)
6729 : 81511148 : && COMPARISON_P (op0)
6730 : 83995272 : && COMPARISON_P (op1))
6731 : : {
6732 : 9853 : rtx t = NULL_RTX;
6733 : 9853 : if (code == GTU || code == (STORE_FLAG_VALUE > 0 ? GT : LT))
6734 : 755 : t = simplify_logical_relational_operation (AND, mode, op1, op0, true);
6735 : : else if (code == GEU || code == (STORE_FLAG_VALUE > 0 ? GE : LE))
6736 : 720 : t = simplify_logical_relational_operation (IOR, mode, op1, op0, true);
6737 : : else if (code == LTU || code == (STORE_FLAG_VALUE > 0 ? LT : GT))
6738 : 720 : t = simplify_logical_relational_operation (AND, mode, op0, op1, true);
6739 : : else if (code == LEU || code == (STORE_FLAG_VALUE > 0 ? LE : GE))
6740 : 720 : t = simplify_logical_relational_operation (IOR, mode, op0, op1, true);
6741 : : else if (code == EQ)
6742 : 3126 : t = simplify_logical_relational_operation (XOR, mode, op0, op1, true);
6743 : : else if (code == NE)
6744 : 3812 : t = simplify_logical_relational_operation (XOR, mode, op0, op1);
6745 : 9853 : if (t)
6746 : : return t;
6747 : : }
6748 : :
6749 : : return NULL_RTX;
6750 : : }
6751 : :
6752 : : enum
6753 : : {
6754 : : CMP_EQ = 1,
6755 : : CMP_LT = 2,
6756 : : CMP_GT = 4,
6757 : : CMP_LTU = 8,
6758 : : CMP_GTU = 16
6759 : : };
6760 : :
6761 : :
6762 : : /* Convert the known results for EQ, LT, GT, LTU, GTU contained in
6763 : : KNOWN_RESULT to a CONST_INT, based on the requested comparison CODE
6764 : : For KNOWN_RESULT to make sense it should be either CMP_EQ, or the
6765 : : logical OR of one of (CMP_LT, CMP_GT) and one of (CMP_LTU, CMP_GTU).
6766 : : For floating-point comparisons, assume that the operands were ordered. */
6767 : :
6768 : : static rtx
6769 : 694090 : comparison_result (enum rtx_code code, int known_results)
6770 : : {
6771 : 694090 : switch (code)
6772 : : {
6773 : 131262 : case EQ:
6774 : 131262 : case UNEQ:
6775 : 131262 : return (known_results & CMP_EQ) ? const_true_rtx : const0_rtx;
6776 : 448367 : case NE:
6777 : 448367 : case LTGT:
6778 : 448367 : return (known_results & CMP_EQ) ? const0_rtx : const_true_rtx;
6779 : :
6780 : 9456 : case LT:
6781 : 9456 : case UNLT:
6782 : 9456 : return (known_results & CMP_LT) ? const_true_rtx : const0_rtx;
6783 : 8554 : case GE:
6784 : 8554 : case UNGE:
6785 : 8554 : return (known_results & CMP_LT) ? const0_rtx : const_true_rtx;
6786 : :
6787 : 13038 : case GT:
6788 : 13038 : case UNGT:
6789 : 13038 : return (known_results & CMP_GT) ? const_true_rtx : const0_rtx;
6790 : 14751 : case LE:
6791 : 14751 : case UNLE:
6792 : 14751 : return (known_results & CMP_GT) ? const0_rtx : const_true_rtx;
6793 : :
6794 : 12719 : case LTU:
6795 : 12719 : return (known_results & CMP_LTU) ? const_true_rtx : const0_rtx;
6796 : 8919 : case GEU:
6797 : 8919 : return (known_results & CMP_LTU) ? const0_rtx : const_true_rtx;
6798 : :
6799 : 35757 : case GTU:
6800 : 35757 : return (known_results & CMP_GTU) ? const_true_rtx : const0_rtx;
6801 : 11259 : case LEU:
6802 : 11259 : return (known_results & CMP_GTU) ? const0_rtx : const_true_rtx;
6803 : :
6804 : 0 : case ORDERED:
6805 : 0 : return const_true_rtx;
6806 : 8 : case UNORDERED:
6807 : 8 : return const0_rtx;
6808 : 0 : default:
6809 : 0 : gcc_unreachable ();
6810 : : }
6811 : : }
6812 : :
6813 : : /* Check if the given comparison (done in the given MODE) is actually
6814 : : a tautology or a contradiction. If the mode is VOIDmode, the
6815 : : comparison is done in "infinite precision". If no simplification
6816 : : is possible, this function returns zero. Otherwise, it returns
6817 : : either const_true_rtx or const0_rtx. */
6818 : :
6819 : : rtx
6820 : 131561635 : simplify_const_relational_operation (enum rtx_code code,
6821 : : machine_mode mode,
6822 : : rtx op0, rtx op1)
6823 : : {
6824 : 138381312 : rtx tem;
6825 : 138381312 : rtx trueop0;
6826 : 138381312 : rtx trueop1;
6827 : :
6828 : 138381312 : gcc_assert (mode != VOIDmode
6829 : : || (GET_MODE (op0) == VOIDmode
6830 : : && GET_MODE (op1) == VOIDmode));
6831 : :
6832 : : /* We only handle MODE_CC comparisons that are COMPARE against zero. */
6833 : 138381312 : if (GET_MODE_CLASS (mode) == MODE_CC
6834 : 44945940 : && (op1 != const0_rtx
6835 : 44945940 : || GET_CODE (op0) != COMPARE))
6836 : : return NULL_RTX;
6837 : :
6838 : : /* If op0 is a compare, extract the comparison arguments from it. */
6839 : 107344022 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6840 : : {
6841 : 13908650 : op1 = XEXP (op0, 1);
6842 : 13908650 : op0 = XEXP (op0, 0);
6843 : :
6844 : 13908650 : if (GET_MODE (op0) != VOIDmode)
6845 : 13791334 : mode = GET_MODE (op0);
6846 : 117316 : else if (GET_MODE (op1) != VOIDmode)
6847 : 107736 : mode = GET_MODE (op1);
6848 : : else
6849 : : return 0;
6850 : : }
6851 : :
6852 : : /* We can't simplify MODE_CC values since we don't know what the
6853 : : actual comparison is. */
6854 : 107334442 : if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6855 : : return 0;
6856 : :
6857 : : /* Make sure the constant is second. */
6858 : 107334442 : if (swap_commutative_operands_p (op0, op1))
6859 : : {
6860 : 3154047 : std::swap (op0, op1);
6861 : 3154047 : code = swap_condition (code);
6862 : : }
6863 : :
6864 : 107334442 : trueop0 = avoid_constant_pool_reference (op0);
6865 : 107334442 : trueop1 = avoid_constant_pool_reference (op1);
6866 : :
6867 : : /* For integer comparisons of A and B maybe we can simplify A - B and can
6868 : : then simplify a comparison of that with zero. If A and B are both either
6869 : : a register or a CONST_INT, this can't help; testing for these cases will
6870 : : prevent infinite recursion here and speed things up.
6871 : :
6872 : : We can only do this for EQ and NE comparisons as otherwise we may
6873 : : lose or introduce overflow which we cannot disregard as undefined as
6874 : : we do not know the signedness of the operation on either the left or
6875 : : the right hand side of the comparison. */
6876 : :
6877 : 107334442 : if (INTEGRAL_MODE_P (mode)
6878 : 105022165 : && trueop1 != CONST0_RTX (mode)
6879 : 53770747 : && (code == EQ || code == NE)
6880 : 34340813 : && ! ((REG_P (op0)
6881 : 9635485 : || CONST_SCALAR_INT_P (trueop0)
6882 : 9606647 : || CONST_VECTOR_P (trueop0))
6883 : 24734186 : && (REG_P (op1)
6884 : 14997949 : || CONST_SCALAR_INT_P (trueop1)
6885 : 3322796 : || CONST_VECTOR_P (trueop1)))
6886 : 12927650 : && (tem = simplify_binary_operation (MINUS, mode, op0, op1)) != 0
6887 : : /* We cannot do this if tem is a nonzero address. */
6888 : 6819679 : && ! nonzero_address_p (tem))
6889 : 6819677 : return simplify_const_relational_operation (signed_condition (code),
6890 : 6819677 : mode, tem, CONST0_RTX (mode));
6891 : :
6892 : 100514765 : if (! HONOR_NANS (mode) && code == ORDERED)
6893 : 0 : return const_true_rtx;
6894 : :
6895 : 100514765 : if (! HONOR_NANS (mode) && code == UNORDERED)
6896 : 8 : return const0_rtx;
6897 : :
6898 : : /* For modes without NaNs, if the two operands are equal, we know the
6899 : : result except if they have side-effects. Even with NaNs we know
6900 : : the result of unordered comparisons and, if signaling NaNs are
6901 : : irrelevant, also the result of LT/GT/LTGT. */
6902 : 100514757 : if ((! HONOR_NANS (trueop0)
6903 : 2097486 : || code == UNEQ || code == UNLE || code == UNGE
6904 : : || ((code == LT || code == GT || code == LTGT)
6905 : 835698 : && ! HONOR_SNANS (trueop0)))
6906 : 99357189 : && rtx_equal_p (trueop0, trueop1)
6907 : 101000906 : && ! side_effects_p (trueop0))
6908 : 486141 : return comparison_result (code, CMP_EQ);
6909 : :
6910 : : /* If the operands are floating-point constants, see if we can fold
6911 : : the result. */
6912 : 100028616 : if (CONST_DOUBLE_AS_FLOAT_P (trueop0)
6913 : 1300 : && CONST_DOUBLE_AS_FLOAT_P (trueop1)
6914 : 1300 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop0)))
6915 : : {
6916 : 1300 : const REAL_VALUE_TYPE *d0 = CONST_DOUBLE_REAL_VALUE (trueop0);
6917 : 1300 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
6918 : :
6919 : : /* Comparisons are unordered iff at least one of the values is NaN. */
6920 : 1300 : if (REAL_VALUE_ISNAN (*d0) || REAL_VALUE_ISNAN (*d1))
6921 : 182 : switch (code)
6922 : : {
6923 : 0 : case UNEQ:
6924 : 0 : case UNLT:
6925 : 0 : case UNGT:
6926 : 0 : case UNLE:
6927 : 0 : case UNGE:
6928 : 0 : case NE:
6929 : 0 : case UNORDERED:
6930 : 0 : return const_true_rtx;
6931 : 182 : case EQ:
6932 : 182 : case LT:
6933 : 182 : case GT:
6934 : 182 : case LE:
6935 : 182 : case GE:
6936 : 182 : case LTGT:
6937 : 182 : case ORDERED:
6938 : 182 : return const0_rtx;
6939 : : default:
6940 : : return 0;
6941 : : }
6942 : :
6943 : 1207 : return comparison_result (code,
6944 : 1207 : (real_equal (d0, d1) ? CMP_EQ :
6945 : 1207 : real_less (d0, d1) ? CMP_LT : CMP_GT));
6946 : : }
6947 : :
6948 : : /* Otherwise, see if the operands are both integers. */
6949 : 100027316 : if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
6950 : 97310578 : && CONST_SCALAR_INT_P (trueop0) && CONST_SCALAR_INT_P (trueop1))
6951 : : {
6952 : : /* It would be nice if we really had a mode here. However, the
6953 : : largest int representable on the target is as good as
6954 : : infinite. */
6955 : 206831 : machine_mode cmode = (mode == VOIDmode) ? MAX_MODE_INT : mode;
6956 : 206831 : rtx_mode_t ptrueop0 = rtx_mode_t (trueop0, cmode);
6957 : 206831 : rtx_mode_t ptrueop1 = rtx_mode_t (trueop1, cmode);
6958 : :
6959 : 206831 : if (wi::eq_p (ptrueop0, ptrueop1))
6960 : 0 : return comparison_result (code, CMP_EQ);
6961 : : else
6962 : : {
6963 : 206831 : int cr = wi::lts_p (ptrueop0, ptrueop1) ? CMP_LT : CMP_GT;
6964 : 206831 : cr |= wi::ltu_p (ptrueop0, ptrueop1) ? CMP_LTU : CMP_GTU;
6965 : 206831 : return comparison_result (code, cr);
6966 : : }
6967 : : }
6968 : :
6969 : : /* Optimize comparisons with upper and lower bounds. */
6970 : 99820485 : scalar_int_mode int_mode;
6971 : 99820485 : if (CONST_INT_P (trueop1)
6972 : 70688980 : && is_a <scalar_int_mode> (mode, &int_mode)
6973 : 70688980 : && HWI_COMPUTABLE_MODE_P (int_mode)
6974 : 169972573 : && !side_effects_p (trueop0))
6975 : : {
6976 : 70064342 : int sign;
6977 : 70064342 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (trueop0, int_mode);
6978 : 70064342 : HOST_WIDE_INT val = INTVAL (trueop1);
6979 : 70064342 : HOST_WIDE_INT mmin, mmax;
6980 : :
6981 : 70064342 : if (code == GEU
6982 : 70064342 : || code == LEU
6983 : 66885890 : || code == GTU
6984 : 66885890 : || code == LTU)
6985 : : sign = 0;
6986 : : else
6987 : 70064342 : sign = 1;
6988 : :
6989 : : /* Get a reduced range if the sign bit is zero. */
6990 : 70064342 : if (nonzero <= (GET_MODE_MASK (int_mode) >> 1))
6991 : : {
6992 : 6921168 : mmin = 0;
6993 : 6921168 : mmax = nonzero;
6994 : : }
6995 : : else
6996 : : {
6997 : 63143174 : rtx mmin_rtx, mmax_rtx;
6998 : 63143174 : get_mode_bounds (int_mode, sign, int_mode, &mmin_rtx, &mmax_rtx);
6999 : :
7000 : 63143174 : mmin = INTVAL (mmin_rtx);
7001 : 63143174 : mmax = INTVAL (mmax_rtx);
7002 : 63143174 : if (sign)
7003 : : {
7004 : 56959245 : unsigned int sign_copies
7005 : 56959245 : = num_sign_bit_copies (trueop0, int_mode);
7006 : :
7007 : 56959245 : mmin >>= (sign_copies - 1);
7008 : 56959245 : mmax >>= (sign_copies - 1);
7009 : : }
7010 : : }
7011 : :
7012 : 70064342 : switch (code)
7013 : : {
7014 : : /* x >= y is always true for y <= mmin, always false for y > mmax. */
7015 : 563527 : case GEU:
7016 : 563527 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
7017 : 11292 : return const_true_rtx;
7018 : 552235 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
7019 : 49 : return const0_rtx;
7020 : : break;
7021 : 926931 : case GE:
7022 : 926931 : if (val <= mmin)
7023 : 2181 : return const_true_rtx;
7024 : 924750 : if (val > mmax)
7025 : 0 : return const0_rtx;
7026 : : break;
7027 : :
7028 : : /* x <= y is always true for y >= mmax, always false for y < mmin. */
7029 : 2614925 : case LEU:
7030 : 2614925 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
7031 : 16918 : return const_true_rtx;
7032 : 2598007 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
7033 : 0 : return const0_rtx;
7034 : : break;
7035 : 2312556 : case LE:
7036 : 2312556 : if (val >= mmax)
7037 : 412 : return const_true_rtx;
7038 : 2312144 : if (val < mmin)
7039 : 0 : return const0_rtx;
7040 : : break;
7041 : :
7042 : 26435299 : case EQ:
7043 : : /* x == y is always false for y out of range. */
7044 : 26435299 : if (val < mmin || val > mmax)
7045 : 430 : return const0_rtx;
7046 : : break;
7047 : :
7048 : : /* x > y is always false for y >= mmax, always true for y < mmin. */
7049 : 2736153 : case GTU:
7050 : 2736153 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
7051 : 127840 : return const0_rtx;
7052 : 2608313 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
7053 : 0 : return const_true_rtx;
7054 : : break;
7055 : 1783543 : case GT:
7056 : 1783543 : if (val >= mmax)
7057 : 336 : return const0_rtx;
7058 : 1783207 : if (val < mmin)
7059 : 5 : return const_true_rtx;
7060 : : break;
7061 : :
7062 : : /* x < y is always false for y <= mmin, always true for y > mmax. */
7063 : 856103 : case LTU:
7064 : 856103 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
7065 : 2771 : return const0_rtx;
7066 : 853332 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
7067 : 88108 : return const_true_rtx;
7068 : : break;
7069 : 1118569 : case LT:
7070 : 1118569 : if (val <= mmin)
7071 : 2344 : return const0_rtx;
7072 : 1116225 : if (val > mmax)
7073 : 3502 : return const_true_rtx;
7074 : : break;
7075 : :
7076 : 30716736 : case NE:
7077 : : /* x != y is always true for y out of range. */
7078 : 30716736 : if (val < mmin || val > mmax)
7079 : 138 : return const_true_rtx;
7080 : : break;
7081 : :
7082 : : default:
7083 : : break;
7084 : : }
7085 : : }
7086 : :
7087 : : /* Optimize integer comparisons with zero. */
7088 : 99564159 : if (is_a <scalar_int_mode> (mode, &int_mode)
7089 : 96890502 : && trueop1 == const0_rtx
7090 : 50523089 : && !side_effects_p (trueop0))
7091 : : {
7092 : : /* Some addresses are known to be nonzero. We don't know
7093 : : their sign, but equality comparisons are known. */
7094 : 50433852 : if (nonzero_address_p (trueop0))
7095 : : {
7096 : 559 : if (code == EQ || code == LEU)
7097 : 297 : return const0_rtx;
7098 : 262 : if (code == NE || code == GTU)
7099 : 262 : return const_true_rtx;
7100 : : }
7101 : :
7102 : : /* See if the first operand is an IOR with a constant. If so, we
7103 : : may be able to determine the result of this comparison. */
7104 : 50433293 : if (GET_CODE (op0) == IOR)
7105 : : {
7106 : 616468 : rtx inner_const = avoid_constant_pool_reference (XEXP (op0, 1));
7107 : 616468 : if (CONST_INT_P (inner_const) && inner_const != const0_rtx)
7108 : : {
7109 : 290 : int sign_bitnum = GET_MODE_PRECISION (int_mode) - 1;
7110 : 580 : int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum
7111 : 290 : && (UINTVAL (inner_const)
7112 : 290 : & (HOST_WIDE_INT_1U
7113 : : << sign_bitnum)));
7114 : :
7115 : 290 : switch (code)
7116 : : {
7117 : : case EQ:
7118 : : case LEU:
7119 : : return const0_rtx;
7120 : 4 : case NE:
7121 : 4 : case GTU:
7122 : 4 : return const_true_rtx;
7123 : 70 : case LT:
7124 : 70 : case LE:
7125 : 70 : if (has_sign)
7126 : 2 : return const_true_rtx;
7127 : : break;
7128 : 210 : case GT:
7129 : 210 : case GE:
7130 : 210 : if (has_sign)
7131 : : return const0_rtx;
7132 : : break;
7133 : : default:
7134 : : break;
7135 : : }
7136 : : }
7137 : : }
7138 : : }
7139 : :
7140 : : /* Optimize comparison of ABS with zero. */
7141 : 50850356 : if (trueop1 == CONST0_RTX (mode) && !side_effects_p (trueop0)
7142 : 150324286 : && (GET_CODE (trueop0) == ABS
7143 : 50760355 : || (GET_CODE (trueop0) == FLOAT_EXTEND
7144 : 34 : && GET_CODE (XEXP (trueop0, 0)) == ABS)))
7145 : : {
7146 : 535 : switch (code)
7147 : : {
7148 : 60 : case LT:
7149 : : /* Optimize abs(x) < 0.0. */
7150 : 60 : if (!INTEGRAL_MODE_P (mode) && !HONOR_SNANS (mode))
7151 : 0 : return const0_rtx;
7152 : : break;
7153 : :
7154 : 42 : case GE:
7155 : : /* Optimize abs(x) >= 0.0. */
7156 : 42 : if (!INTEGRAL_MODE_P (mode) && !HONOR_NANS (mode))
7157 : 0 : return const_true_rtx;
7158 : : break;
7159 : :
7160 : 0 : case UNGE:
7161 : : /* Optimize ! (abs(x) < 0.0). */
7162 : 0 : return const_true_rtx;
7163 : :
7164 : : default:
7165 : : break;
7166 : : }
7167 : : }
7168 : :
7169 : : return 0;
7170 : : }
7171 : :
7172 : : /* Recognize expressions of the form (X CMP 0) ? VAL : OP (X)
7173 : : where OP is CLZ or CTZ and VAL is the value from CLZ_DEFINED_VALUE_AT_ZERO
7174 : : or CTZ_DEFINED_VALUE_AT_ZERO respectively and return OP (X) if the expression
7175 : : can be simplified to that or NULL_RTX if not.
7176 : : Assume X is compared against zero with CMP_CODE and the true
7177 : : arm is TRUE_VAL and the false arm is FALSE_VAL. */
7178 : :
7179 : : rtx
7180 : 31576005 : simplify_context::simplify_cond_clz_ctz (rtx x, rtx_code cmp_code,
7181 : : rtx true_val, rtx false_val)
7182 : : {
7183 : 31576005 : if (cmp_code != EQ && cmp_code != NE)
7184 : : return NULL_RTX;
7185 : :
7186 : : /* Result on X == 0 and X !=0 respectively. */
7187 : 23009263 : rtx on_zero, on_nonzero;
7188 : 23009263 : if (cmp_code == EQ)
7189 : : {
7190 : : on_zero = true_val;
7191 : : on_nonzero = false_val;
7192 : : }
7193 : : else
7194 : : {
7195 : 12107856 : on_zero = false_val;
7196 : 12107856 : on_nonzero = true_val;
7197 : : }
7198 : :
7199 : 23009263 : rtx_code op_code = GET_CODE (on_nonzero);
7200 : 23009263 : if ((op_code != CLZ && op_code != CTZ)
7201 : 1856 : || !rtx_equal_p (XEXP (on_nonzero, 0), x)
7202 : 23010200 : || !CONST_INT_P (on_zero))
7203 : 23008968 : return NULL_RTX;
7204 : :
7205 : 295 : HOST_WIDE_INT op_val;
7206 : 295 : scalar_int_mode mode ATTRIBUTE_UNUSED
7207 : 295 : = as_a <scalar_int_mode> (GET_MODE (XEXP (on_nonzero, 0)));
7208 : 0 : if (((op_code == CLZ && CLZ_DEFINED_VALUE_AT_ZERO (mode, op_val))
7209 : 590 : || (op_code == CTZ && CTZ_DEFINED_VALUE_AT_ZERO (mode, op_val)))
7210 : 319 : && op_val == INTVAL (on_zero))
7211 : : return on_nonzero;
7212 : :
7213 : : return NULL_RTX;
7214 : : }
7215 : :
7216 : : /* Try to simplify X given that it appears within operand OP of a
7217 : : VEC_MERGE operation whose mask is MASK. X need not use the same
7218 : : vector mode as the VEC_MERGE, but it must have the same number of
7219 : : elements.
7220 : :
7221 : : Return the simplified X on success, otherwise return NULL_RTX. */
7222 : :
7223 : : rtx
7224 : 1612279 : simplify_context::simplify_merge_mask (rtx x, rtx mask, int op)
7225 : : {
7226 : 1612279 : gcc_assert (VECTOR_MODE_P (GET_MODE (x)));
7227 : 3224558 : poly_uint64 nunits = GET_MODE_NUNITS (GET_MODE (x));
7228 : 1612279 : if (GET_CODE (x) == VEC_MERGE && rtx_equal_p (XEXP (x, 2), mask))
7229 : : {
7230 : 5484 : if (side_effects_p (XEXP (x, 1 - op)))
7231 : : return NULL_RTX;
7232 : :
7233 : 5260 : return XEXP (x, op);
7234 : : }
7235 : 1606795 : if (UNARY_P (x)
7236 : 182300 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7237 : 1664527 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits))
7238 : : {
7239 : 24266 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7240 : 24266 : if (top0)
7241 : 448 : return simplify_gen_unary (GET_CODE (x), GET_MODE (x), top0,
7242 : 448 : GET_MODE (XEXP (x, 0)));
7243 : : }
7244 : 1606347 : if (BINARY_P (x)
7245 : 204603 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7246 : 409088 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7247 : 179256 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7248 : 1891269 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits))
7249 : : {
7250 : 142461 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7251 : 142461 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7252 : 142461 : if (top0 || top1)
7253 : : {
7254 : 952 : if (COMPARISON_P (x))
7255 : 0 : return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7256 : 0 : GET_MODE (XEXP (x, 0)) != VOIDmode
7257 : : ? GET_MODE (XEXP (x, 0))
7258 : 0 : : GET_MODE (XEXP (x, 1)),
7259 : : top0 ? top0 : XEXP (x, 0),
7260 : 0 : top1 ? top1 : XEXP (x, 1));
7261 : : else
7262 : 952 : return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7263 : : top0 ? top0 : XEXP (x, 0),
7264 : 952 : top1 ? top1 : XEXP (x, 1));
7265 : : }
7266 : : }
7267 : 1605395 : if (GET_RTX_CLASS (GET_CODE (x)) == RTX_TERNARY
7268 : 35705 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7269 : 71410 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7270 : 35705 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7271 : 71410 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits)
7272 : 35705 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 2)))
7273 : 1623575 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 2))), nunits))
7274 : : {
7275 : 9090 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7276 : 9090 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7277 : 9090 : rtx top2 = simplify_merge_mask (XEXP (x, 2), mask, op);
7278 : 9090 : if (top0 || top1 || top2)
7279 : 448 : return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7280 : 448 : GET_MODE (XEXP (x, 0)),
7281 : : top0 ? top0 : XEXP (x, 0),
7282 : : top1 ? top1 : XEXP (x, 1),
7283 : 448 : top2 ? top2 : XEXP (x, 2));
7284 : : }
7285 : : return NULL_RTX;
7286 : : }
7287 : :
7288 : :
7289 : : /* Simplify CODE, an operation with result mode MODE and three operands,
7290 : : OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
7291 : : a constant. Return 0 if no simplifications is possible. */
7292 : :
7293 : : rtx
7294 : 43016067 : simplify_context::simplify_ternary_operation (rtx_code code, machine_mode mode,
7295 : : machine_mode op0_mode,
7296 : : rtx op0, rtx op1, rtx op2)
7297 : : {
7298 : 43016067 : bool any_change = false;
7299 : 43016067 : rtx tem, trueop2;
7300 : 43016067 : scalar_int_mode int_mode, int_op0_mode;
7301 : 43016067 : unsigned int n_elts;
7302 : :
7303 : 43016067 : switch (code)
7304 : : {
7305 : 336782 : case FMA:
7306 : : /* Simplify negations around the multiplication. */
7307 : : /* -a * -b + c => a * b + c. */
7308 : 336782 : if (GET_CODE (op0) == NEG)
7309 : : {
7310 : 80941 : tem = simplify_unary_operation (NEG, mode, op1, mode);
7311 : 80941 : if (tem)
7312 : 231 : op1 = tem, op0 = XEXP (op0, 0), any_change = true;
7313 : : }
7314 : 255841 : else if (GET_CODE (op1) == NEG)
7315 : : {
7316 : 1076 : tem = simplify_unary_operation (NEG, mode, op0, mode);
7317 : 1076 : if (tem)
7318 : 0 : op0 = tem, op1 = XEXP (op1, 0), any_change = true;
7319 : : }
7320 : :
7321 : : /* Canonicalize the two multiplication operands. */
7322 : : /* a * -b + c => -b * a + c. */
7323 : 336782 : if (swap_commutative_operands_p (op0, op1))
7324 : : std::swap (op0, op1), any_change = true;
7325 : :
7326 : 308486 : if (any_change)
7327 : 28518 : return gen_rtx_FMA (mode, op0, op1, op2);
7328 : : return NULL_RTX;
7329 : :
7330 : 651416 : case SIGN_EXTRACT:
7331 : 651416 : case ZERO_EXTRACT:
7332 : 651416 : if (CONST_INT_P (op0)
7333 : 20279 : && CONST_INT_P (op1)
7334 : 20279 : && CONST_INT_P (op2)
7335 : 43016099 : && is_a <scalar_int_mode> (mode, &int_mode)
7336 : 32 : && INTVAL (op1) + INTVAL (op2) <= GET_MODE_PRECISION (int_mode)
7337 : 651448 : && HWI_COMPUTABLE_MODE_P (int_mode))
7338 : : {
7339 : : /* Extracting a bit-field from a constant */
7340 : 32 : unsigned HOST_WIDE_INT val = UINTVAL (op0);
7341 : 32 : HOST_WIDE_INT op1val = INTVAL (op1);
7342 : 32 : HOST_WIDE_INT op2val = INTVAL (op2);
7343 : 32 : if (!BITS_BIG_ENDIAN)
7344 : 32 : val >>= op2val;
7345 : : else if (is_a <scalar_int_mode> (op0_mode, &int_op0_mode))
7346 : : val >>= GET_MODE_PRECISION (int_op0_mode) - op2val - op1val;
7347 : : else
7348 : : /* Not enough information to calculate the bit position. */
7349 : : break;
7350 : :
7351 : 32 : if (HOST_BITS_PER_WIDE_INT != op1val)
7352 : : {
7353 : : /* First zero-extend. */
7354 : 29 : val &= (HOST_WIDE_INT_1U << op1val) - 1;
7355 : : /* If desired, propagate sign bit. */
7356 : 29 : if (code == SIGN_EXTRACT
7357 : 5 : && (val & (HOST_WIDE_INT_1U << (op1val - 1)))
7358 : 5 : != 0)
7359 : 2 : val |= ~ ((HOST_WIDE_INT_1U << op1val) - 1);
7360 : : }
7361 : :
7362 : 32 : return gen_int_mode (val, int_mode);
7363 : : }
7364 : : break;
7365 : :
7366 : 41257576 : case IF_THEN_ELSE:
7367 : 41257576 : if (CONST_INT_P (op0))
7368 : 288259 : return op0 != const0_rtx ? op1 : op2;
7369 : :
7370 : : /* Convert c ? a : a into "a". */
7371 : 41063148 : if (rtx_equal_p (op1, op2) && ! side_effects_p (op0))
7372 : : return op1;
7373 : :
7374 : : /* Convert a != b ? a : b into "a". */
7375 : 41059503 : if (GET_CODE (op0) == NE
7376 : 15940250 : && ! side_effects_p (op0)
7377 : 15902417 : && ! HONOR_NANS (mode)
7378 : 15897652 : && ! HONOR_SIGNED_ZEROS (mode)
7379 : 56957155 : && ((rtx_equal_p (XEXP (op0, 0), op1)
7380 : 76653 : && rtx_equal_p (XEXP (op0, 1), op2))
7381 : 15897595 : || (rtx_equal_p (XEXP (op0, 0), op2)
7382 : 6416 : && rtx_equal_p (XEXP (op0, 1), op1))))
7383 : 152 : return op1;
7384 : :
7385 : : /* Convert a == b ? a : b into "b". */
7386 : 41059351 : if (GET_CODE (op0) == EQ
7387 : 13284716 : && ! side_effects_p (op0)
7388 : 13274231 : && ! HONOR_NANS (mode)
7389 : 13175406 : && ! HONOR_SIGNED_ZEROS (mode)
7390 : 54234757 : && ((rtx_equal_p (XEXP (op0, 0), op1)
7391 : 14471 : && rtx_equal_p (XEXP (op0, 1), op2))
7392 : 13175396 : || (rtx_equal_p (XEXP (op0, 0), op2)
7393 : 7254 : && rtx_equal_p (XEXP (op0, 1), op1))))
7394 : 21 : return op2;
7395 : :
7396 : : /* Convert a != 0 ? -a : 0 into "-a". */
7397 : 41059330 : if (GET_CODE (op0) == NE
7398 : 15940098 : && ! side_effects_p (op0)
7399 : 15902265 : && ! HONOR_NANS (mode)
7400 : 15897500 : && ! HONOR_SIGNED_ZEROS (mode)
7401 : 15897500 : && XEXP (op0, 1) == CONST0_RTX (mode)
7402 : 12102970 : && op2 == CONST0_RTX (mode)
7403 : 100724 : && GET_CODE (op1) == NEG
7404 : 41059393 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0)))
7405 : : return op1;
7406 : :
7407 : : /* Convert a == 0 ? 0 : -a into "-a". */
7408 : 41059325 : if (GET_CODE (op0) == EQ
7409 : 13284695 : && ! side_effects_p (op0)
7410 : 13274210 : && ! HONOR_NANS (mode)
7411 : 13175385 : && ! HONOR_SIGNED_ZEROS (mode)
7412 : 13175385 : && op1 == CONST0_RTX (mode)
7413 : 33362 : && XEXP (op0, 1) == CONST0_RTX (mode)
7414 : 14955 : && GET_CODE (op2) == NEG
7415 : 41059329 : && rtx_equal_p (XEXP (op0, 0), XEXP (op2, 0)))
7416 : : return op2;
7417 : :
7418 : : /* Convert (!c) != {0,...,0} ? a : b into
7419 : : c != {0,...,0} ? b : a for vector modes. */
7420 : 41059321 : if (VECTOR_MODE_P (GET_MODE (op1))
7421 : 11606 : && GET_CODE (op0) == NE
7422 : 460 : && GET_CODE (XEXP (op0, 0)) == NOT
7423 : 0 : && GET_CODE (XEXP (op0, 1)) == CONST_VECTOR)
7424 : : {
7425 : 0 : rtx cv = XEXP (op0, 1);
7426 : 0 : int nunits;
7427 : 0 : bool ok = true;
7428 : 0 : if (!CONST_VECTOR_NUNITS (cv).is_constant (&nunits))
7429 : : ok = false;
7430 : : else
7431 : 0 : for (int i = 0; i < nunits; ++i)
7432 : 0 : if (CONST_VECTOR_ELT (cv, i) != const0_rtx)
7433 : : {
7434 : : ok = false;
7435 : : break;
7436 : : }
7437 : 0 : if (ok)
7438 : : {
7439 : 0 : rtx new_op0 = gen_rtx_NE (GET_MODE (op0),
7440 : : XEXP (XEXP (op0, 0), 0),
7441 : : XEXP (op0, 1));
7442 : 0 : rtx retval = gen_rtx_IF_THEN_ELSE (mode, new_op0, op2, op1);
7443 : 0 : return retval;
7444 : : }
7445 : : }
7446 : :
7447 : : /* Convert x == 0 ? N : clz (x) into clz (x) when
7448 : : CLZ_DEFINED_VALUE_AT_ZERO is defined to N for the mode of x.
7449 : : Similarly for ctz (x). */
7450 : 41058360 : if (COMPARISON_P (op0) && !side_effects_p (op0)
7451 : 82032522 : && XEXP (op0, 1) == const0_rtx)
7452 : : {
7453 : 31576005 : rtx simplified
7454 : 31576005 : = simplify_cond_clz_ctz (XEXP (op0, 0), GET_CODE (op0),
7455 : : op1, op2);
7456 : 31576005 : if (simplified)
7457 : : return simplified;
7458 : : }
7459 : :
7460 : 41059321 : if (COMPARISON_P (op0) && ! side_effects_p (op0))
7461 : : {
7462 : 82020947 : machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
7463 : 40973201 : ? GET_MODE (XEXP (op0, 1))
7464 : : : GET_MODE (XEXP (op0, 0)));
7465 : 40973201 : rtx temp;
7466 : :
7467 : : /* Look for happy constants in op1 and op2. */
7468 : 40973201 : if (CONST_INT_P (op1) && CONST_INT_P (op2))
7469 : : {
7470 : 222831 : HOST_WIDE_INT t = INTVAL (op1);
7471 : 222831 : HOST_WIDE_INT f = INTVAL (op2);
7472 : :
7473 : 222831 : if (t == STORE_FLAG_VALUE && f == 0)
7474 : 53167 : code = GET_CODE (op0);
7475 : 169664 : else if (t == 0 && f == STORE_FLAG_VALUE)
7476 : : {
7477 : 31873 : enum rtx_code tmp;
7478 : 31873 : tmp = reversed_comparison_code (op0, NULL);
7479 : 31873 : if (tmp == UNKNOWN)
7480 : : break;
7481 : : code = tmp;
7482 : : }
7483 : : else
7484 : : break;
7485 : :
7486 : 79654 : return simplify_gen_relational (code, mode, cmp_mode,
7487 : 79654 : XEXP (op0, 0), XEXP (op0, 1));
7488 : : }
7489 : :
7490 : 40750370 : temp = simplify_relational_operation (GET_CODE (op0), op0_mode,
7491 : : cmp_mode, XEXP (op0, 0),
7492 : : XEXP (op0, 1));
7493 : :
7494 : : /* See if any simplifications were possible. */
7495 : 40750370 : if (temp)
7496 : : {
7497 : 7103 : if (CONST_INT_P (temp))
7498 : 864 : return temp == const0_rtx ? op2 : op1;
7499 : 6280 : else if (temp)
7500 : 6280 : return gen_rtx_IF_THEN_ELSE (mode, temp, op1, op2);
7501 : : }
7502 : : }
7503 : : break;
7504 : :
7505 : 770293 : case VEC_MERGE:
7506 : 770293 : gcc_assert (GET_MODE (op0) == mode);
7507 : 770293 : gcc_assert (GET_MODE (op1) == mode);
7508 : 770293 : gcc_assert (VECTOR_MODE_P (mode));
7509 : 770293 : trueop2 = avoid_constant_pool_reference (op2);
7510 : 770293 : if (CONST_INT_P (trueop2)
7511 : 1220587 : && GET_MODE_NUNITS (mode).is_constant (&n_elts))
7512 : : {
7513 : 450294 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop2);
7514 : 450294 : unsigned HOST_WIDE_INT mask;
7515 : 450294 : if (n_elts == HOST_BITS_PER_WIDE_INT)
7516 : : mask = -1;
7517 : : else
7518 : 447847 : mask = (HOST_WIDE_INT_1U << n_elts) - 1;
7519 : :
7520 : 450294 : if (!(sel & mask) && !side_effects_p (op0))
7521 : : return op1;
7522 : 449857 : if ((sel & mask) == mask && !side_effects_p (op1))
7523 : : return op0;
7524 : :
7525 : 439133 : rtx trueop0 = avoid_constant_pool_reference (op0);
7526 : 439133 : rtx trueop1 = avoid_constant_pool_reference (op1);
7527 : 439133 : if (GET_CODE (trueop0) == CONST_VECTOR
7528 : 9003 : && GET_CODE (trueop1) == CONST_VECTOR)
7529 : : {
7530 : 4749 : rtvec v = rtvec_alloc (n_elts);
7531 : 4749 : unsigned int i;
7532 : :
7533 : 53796 : for (i = 0; i < n_elts; i++)
7534 : 44298 : RTVEC_ELT (v, i) = ((sel & (HOST_WIDE_INT_1U << i))
7535 : 44298 : ? CONST_VECTOR_ELT (trueop0, i)
7536 : 24768 : : CONST_VECTOR_ELT (trueop1, i));
7537 : 4749 : return gen_rtx_CONST_VECTOR (mode, v);
7538 : : }
7539 : :
7540 : 434384 : if (swap_commutative_operands_p (op0, op1)
7541 : : /* Two operands have same precedence, then first bit of mask
7542 : : select first operand. */
7543 : 434384 : || (!swap_commutative_operands_p (op1, op0) && !(sel & 1)))
7544 : 30469 : return simplify_gen_ternary (code, mode, mode, op1, op0,
7545 : 60938 : GEN_INT (~sel & mask));
7546 : :
7547 : : /* Replace (vec_merge (vec_merge a b m) c n) with (vec_merge b c n)
7548 : : if no element from a appears in the result. */
7549 : 403915 : if (GET_CODE (op0) == VEC_MERGE)
7550 : : {
7551 : 17233 : tem = avoid_constant_pool_reference (XEXP (op0, 2));
7552 : 17233 : if (CONST_INT_P (tem))
7553 : : {
7554 : 1372 : unsigned HOST_WIDE_INT sel0 = UINTVAL (tem);
7555 : 1372 : if (!(sel & sel0 & mask) && !side_effects_p (XEXP (op0, 0)))
7556 : 99 : return simplify_gen_ternary (code, mode, mode,
7557 : 99 : XEXP (op0, 1), op1, op2);
7558 : 1273 : if (!(sel & ~sel0 & mask) && !side_effects_p (XEXP (op0, 1)))
7559 : 834 : return simplify_gen_ternary (code, mode, mode,
7560 : 834 : XEXP (op0, 0), op1, op2);
7561 : : }
7562 : : }
7563 : 402982 : if (GET_CODE (op1) == VEC_MERGE)
7564 : : {
7565 : 585 : tem = avoid_constant_pool_reference (XEXP (op1, 2));
7566 : 585 : if (CONST_INT_P (tem))
7567 : : {
7568 : 554 : unsigned HOST_WIDE_INT sel1 = UINTVAL (tem);
7569 : 554 : if (!(~sel & sel1 & mask) && !side_effects_p (XEXP (op1, 0)))
7570 : 523 : return simplify_gen_ternary (code, mode, mode,
7571 : 523 : op0, XEXP (op1, 1), op2);
7572 : 31 : if (!(~sel & ~sel1 & mask) && !side_effects_p (XEXP (op1, 1)))
7573 : 4 : return simplify_gen_ternary (code, mode, mode,
7574 : 4 : op0, XEXP (op1, 0), op2);
7575 : : }
7576 : : }
7577 : :
7578 : : /* Replace (vec_merge (vec_duplicate (vec_select a parallel (i))) a 1 << i)
7579 : : with a. */
7580 : 402455 : if (GET_CODE (op0) == VEC_DUPLICATE
7581 : 133541 : && GET_CODE (XEXP (op0, 0)) == VEC_SELECT
7582 : 632 : && GET_CODE (XEXP (XEXP (op0, 0), 1)) == PARALLEL
7583 : 403719 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (op0, 0))), 1))
7584 : : {
7585 : 564 : tem = XVECEXP ((XEXP (XEXP (op0, 0), 1)), 0, 0);
7586 : 564 : if (CONST_INT_P (tem) && CONST_INT_P (op2))
7587 : : {
7588 : 564 : if (XEXP (XEXP (op0, 0), 0) == op1
7589 : 2 : && UINTVAL (op2) == HOST_WIDE_INT_1U << UINTVAL (tem))
7590 : : return op1;
7591 : : }
7592 : : }
7593 : : /* Replace (vec_merge (vec_duplicate (X)) (const_vector [A, B])
7594 : : (const_int N))
7595 : : with (vec_concat (X) (B)) if N == 1 or
7596 : : (vec_concat (A) (X)) if N == 2. */
7597 : 402453 : if (GET_CODE (op0) == VEC_DUPLICATE
7598 : 133539 : && GET_CODE (op1) == CONST_VECTOR
7599 : 141756 : && known_eq (CONST_VECTOR_NUNITS (op1), 2)
7600 : 2042 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7601 : 403474 : && IN_RANGE (sel, 1, 2))
7602 : : {
7603 : 1019 : rtx newop0 = XEXP (op0, 0);
7604 : 1019 : rtx newop1 = CONST_VECTOR_ELT (op1, 2 - sel);
7605 : 1019 : if (sel == 2)
7606 : 123 : std::swap (newop0, newop1);
7607 : 1019 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7608 : : }
7609 : : /* Replace (vec_merge (vec_duplicate x) (vec_concat (y) (z)) (const_int N))
7610 : : with (vec_concat x z) if N == 1, or (vec_concat y x) if N == 2.
7611 : : Only applies for vectors of two elements. */
7612 : 401434 : if (GET_CODE (op0) == VEC_DUPLICATE
7613 : 132520 : && GET_CODE (op1) == VEC_CONCAT
7614 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7615 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7616 : 401434 : && IN_RANGE (sel, 1, 2))
7617 : : {
7618 : 0 : rtx newop0 = XEXP (op0, 0);
7619 : 0 : rtx newop1 = XEXP (op1, 2 - sel);
7620 : 0 : rtx otherop = XEXP (op1, sel - 1);
7621 : 0 : if (sel == 2)
7622 : 0 : std::swap (newop0, newop1);
7623 : : /* Don't want to throw away the other part of the vec_concat if
7624 : : it has side-effects. */
7625 : 0 : if (!side_effects_p (otherop))
7626 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7627 : : }
7628 : :
7629 : : /* Replace:
7630 : :
7631 : : (vec_merge:outer (vec_duplicate:outer x:inner)
7632 : : (subreg:outer y:inner 0)
7633 : : (const_int N))
7634 : :
7635 : : with (vec_concat:outer x:inner y:inner) if N == 1,
7636 : : or (vec_concat:outer y:inner x:inner) if N == 2.
7637 : :
7638 : : Implicitly, this means we have a paradoxical subreg, but such
7639 : : a check is cheap, so make it anyway.
7640 : :
7641 : : Only applies for vectors of two elements. */
7642 : 401434 : if (GET_CODE (op0) == VEC_DUPLICATE
7643 : 132520 : && GET_CODE (op1) == SUBREG
7644 : 40758 : && GET_MODE (op1) == GET_MODE (op0)
7645 : 40758 : && GET_MODE (SUBREG_REG (op1)) == GET_MODE (XEXP (op0, 0))
7646 : 0 : && paradoxical_subreg_p (op1)
7647 : 0 : && subreg_lowpart_p (op1)
7648 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7649 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7650 : 401434 : && IN_RANGE (sel, 1, 2))
7651 : : {
7652 : 0 : rtx newop0 = XEXP (op0, 0);
7653 : 0 : rtx newop1 = SUBREG_REG (op1);
7654 : 0 : if (sel == 2)
7655 : 0 : std::swap (newop0, newop1);
7656 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7657 : : }
7658 : :
7659 : : /* Same as above but with switched operands:
7660 : : Replace (vec_merge:outer (subreg:outer x:inner 0)
7661 : : (vec_duplicate:outer y:inner)
7662 : : (const_int N))
7663 : :
7664 : : with (vec_concat:outer x:inner y:inner) if N == 1,
7665 : : or (vec_concat:outer y:inner x:inner) if N == 2. */
7666 : 401434 : if (GET_CODE (op1) == VEC_DUPLICATE
7667 : 27126 : && GET_CODE (op0) == SUBREG
7668 : 24258 : && GET_MODE (op0) == GET_MODE (op1)
7669 : 24258 : && GET_MODE (SUBREG_REG (op0)) == GET_MODE (XEXP (op1, 0))
7670 : 0 : && paradoxical_subreg_p (op0)
7671 : 0 : && subreg_lowpart_p (op0)
7672 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7673 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7674 : 401434 : && IN_RANGE (sel, 1, 2))
7675 : : {
7676 : 0 : rtx newop0 = SUBREG_REG (op0);
7677 : 0 : rtx newop1 = XEXP (op1, 0);
7678 : 0 : if (sel == 2)
7679 : 0 : std::swap (newop0, newop1);
7680 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7681 : : }
7682 : :
7683 : : /* Replace (vec_merge (vec_duplicate x) (vec_duplicate y)
7684 : : (const_int n))
7685 : : with (vec_concat x y) or (vec_concat y x) depending on value
7686 : : of N. */
7687 : 401434 : if (GET_CODE (op0) == VEC_DUPLICATE
7688 : 132520 : && GET_CODE (op1) == VEC_DUPLICATE
7689 : 198 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7690 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7691 : 401434 : && IN_RANGE (sel, 1, 2))
7692 : : {
7693 : 0 : rtx newop0 = XEXP (op0, 0);
7694 : 0 : rtx newop1 = XEXP (op1, 0);
7695 : 0 : if (sel == 2)
7696 : 0 : std::swap (newop0, newop1);
7697 : :
7698 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7699 : : }
7700 : : }
7701 : :
7702 : 721433 : if (rtx_equal_p (op0, op1)
7703 : 721433 : && !side_effects_p (op2) && !side_effects_p (op1))
7704 : : return op0;
7705 : :
7706 : 721146 : if (!side_effects_p (op2))
7707 : : {
7708 : 718057 : rtx top0
7709 : 718057 : = may_trap_p (op0) ? NULL_RTX : simplify_merge_mask (op0, op2, 0);
7710 : 718057 : rtx top1
7711 : 718057 : = may_trap_p (op1) ? NULL_RTX : simplify_merge_mask (op1, op2, 1);
7712 : 718057 : if (top0 || top1)
7713 : 984 : return simplify_gen_ternary (code, mode, mode,
7714 : : top0 ? top0 : op0,
7715 : 812 : top1 ? top1 : op1, op2);
7716 : : }
7717 : :
7718 : : break;
7719 : :
7720 : 0 : default:
7721 : 0 : gcc_unreachable ();
7722 : : }
7723 : :
7724 : : return 0;
7725 : : }
7726 : :
7727 : : /* Try to calculate NUM_BYTES bytes of the target memory image of X,
7728 : : starting at byte FIRST_BYTE. Return true on success and add the
7729 : : bytes to BYTES, such that each byte has BITS_PER_UNIT bits and such
7730 : : that the bytes follow target memory order. Leave BYTES unmodified
7731 : : on failure.
7732 : :
7733 : : MODE is the mode of X. The caller must reserve NUM_BYTES bytes in
7734 : : BYTES before calling this function. */
7735 : :
7736 : : bool
7737 : 14156114 : native_encode_rtx (machine_mode mode, rtx x, vec<target_unit> &bytes,
7738 : : unsigned int first_byte, unsigned int num_bytes)
7739 : : {
7740 : : /* Check the mode is sensible. */
7741 : 14156114 : gcc_assert (GET_MODE (x) == VOIDmode
7742 : : ? is_a <scalar_int_mode> (mode)
7743 : : : mode == GET_MODE (x));
7744 : :
7745 : 14156114 : if (GET_CODE (x) == CONST_VECTOR)
7746 : : {
7747 : : /* CONST_VECTOR_ELT follows target memory order, so no shuffling
7748 : : is necessary. The only complication is that MODE_VECTOR_BOOL
7749 : : vectors can have several elements per byte. */
7750 : 986780 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
7751 : : GET_MODE_NUNITS (mode));
7752 : 493390 : unsigned int elt = first_byte * BITS_PER_UNIT / elt_bits;
7753 : 493390 : if (elt_bits < BITS_PER_UNIT)
7754 : : {
7755 : : /* This is the only case in which elements can be smaller than
7756 : : a byte. */
7757 : 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
7758 : 0 : auto mask = GET_MODE_MASK (GET_MODE_INNER (mode));
7759 : 0 : for (unsigned int i = 0; i < num_bytes; ++i)
7760 : : {
7761 : 0 : target_unit value = 0;
7762 : 0 : for (unsigned int j = 0; j < BITS_PER_UNIT; j += elt_bits)
7763 : : {
7764 : 0 : if (INTVAL (CONST_VECTOR_ELT (x, elt)))
7765 : 0 : value |= mask << j;
7766 : 0 : elt += 1;
7767 : : }
7768 : 0 : bytes.quick_push (value);
7769 : : }
7770 : : return true;
7771 : : }
7772 : :
7773 : 493390 : unsigned int start = bytes.length ();
7774 : 493390 : unsigned int elt_bytes = GET_MODE_UNIT_SIZE (mode);
7775 : : /* Make FIRST_BYTE relative to ELT. */
7776 : 493390 : first_byte %= elt_bytes;
7777 : 2636361 : while (num_bytes > 0)
7778 : : {
7779 : : /* Work out how many bytes we want from element ELT. */
7780 : 2142971 : unsigned int chunk_bytes = MIN (num_bytes, elt_bytes - first_byte);
7781 : 4285942 : if (!native_encode_rtx (GET_MODE_INNER (mode),
7782 : : CONST_VECTOR_ELT (x, elt), bytes,
7783 : : first_byte, chunk_bytes))
7784 : : {
7785 : 0 : bytes.truncate (start);
7786 : 0 : return false;
7787 : : }
7788 : 2142971 : elt += 1;
7789 : 2142971 : first_byte = 0;
7790 : 2142971 : num_bytes -= chunk_bytes;
7791 : : }
7792 : : return true;
7793 : : }
7794 : :
7795 : : /* All subsequent cases are limited to scalars. */
7796 : 13662724 : scalar_mode smode;
7797 : 13694424 : if (!is_a <scalar_mode> (mode, &smode))
7798 : : return false;
7799 : :
7800 : : /* Make sure that the region is in range. */
7801 : 13662724 : unsigned int end_byte = first_byte + num_bytes;
7802 : 13662724 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
7803 : 13662724 : gcc_assert (end_byte <= mode_bytes);
7804 : :
7805 : 13662724 : if (CONST_SCALAR_INT_P (x))
7806 : : {
7807 : : /* The target memory layout is affected by both BYTES_BIG_ENDIAN
7808 : : and WORDS_BIG_ENDIAN. Use the subreg machinery to get the lsb
7809 : : position of each byte. */
7810 : 12991886 : rtx_mode_t value (x, smode);
7811 : 12991886 : wide_int_ref value_wi (value);
7812 : 54898386 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7813 : : {
7814 : : /* Always constant because the inputs are. */
7815 : 41906500 : unsigned int lsb
7816 : 41906500 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7817 : : /* Operate directly on the encoding rather than using
7818 : : wi::extract_uhwi, so that we preserve the sign or zero
7819 : : extension for modes that are not a whole number of bits in
7820 : : size. (Zero extension is only used for the combination of
7821 : : innermode == BImode && STORE_FLAG_VALUE == 1). */
7822 : 41906500 : unsigned int elt = lsb / HOST_BITS_PER_WIDE_INT;
7823 : 41906500 : unsigned int shift = lsb % HOST_BITS_PER_WIDE_INT;
7824 : 41906500 : unsigned HOST_WIDE_INT uhwi = value_wi.elt (elt);
7825 : 41906500 : bytes.quick_push (uhwi >> shift);
7826 : : }
7827 : 12991886 : return true;
7828 : : }
7829 : :
7830 : 670838 : if (CONST_DOUBLE_P (x))
7831 : : {
7832 : : /* real_to_target produces an array of integers in target memory order.
7833 : : All integers before the last one have 32 bits; the last one may
7834 : : have 32 bits or fewer, depending on whether the mode bitsize
7835 : : is divisible by 32. Each of these integers is then laid out
7836 : : in target memory as any other integer would be. */
7837 : 639138 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
7838 : 639138 : real_to_target (el32, CONST_DOUBLE_REAL_VALUE (x), smode);
7839 : :
7840 : : /* The (maximum) number of target bytes per element of el32. */
7841 : 639138 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
7842 : 639138 : gcc_assert (bytes_per_el32 != 0);
7843 : :
7844 : : /* Build up the integers in a similar way to the CONST_SCALAR_INT_P
7845 : : handling above. */
7846 : 4379041 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7847 : : {
7848 : 3739903 : unsigned int index = byte / bytes_per_el32;
7849 : 3739903 : unsigned int subbyte = byte % bytes_per_el32;
7850 : 3739903 : unsigned int int_bytes = MIN (bytes_per_el32,
7851 : : mode_bytes - index * bytes_per_el32);
7852 : : /* Always constant because the inputs are. */
7853 : 3739903 : unsigned int lsb
7854 : 3739903 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
7855 : 3739903 : bytes.quick_push ((unsigned long) el32[index] >> lsb);
7856 : : }
7857 : 639138 : return true;
7858 : : }
7859 : :
7860 : 31700 : if (GET_CODE (x) == CONST_FIXED)
7861 : : {
7862 : 0 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7863 : : {
7864 : : /* Always constant because the inputs are. */
7865 : 0 : unsigned int lsb
7866 : 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7867 : 0 : unsigned HOST_WIDE_INT piece = CONST_FIXED_VALUE_LOW (x);
7868 : 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
7869 : : {
7870 : 0 : lsb -= HOST_BITS_PER_WIDE_INT;
7871 : 0 : piece = CONST_FIXED_VALUE_HIGH (x);
7872 : : }
7873 : 0 : bytes.quick_push (piece >> lsb);
7874 : : }
7875 : : return true;
7876 : : }
7877 : :
7878 : : return false;
7879 : : }
7880 : :
7881 : : /* Read a vector of mode MODE from the target memory image given by BYTES,
7882 : : starting at byte FIRST_BYTE. The vector is known to be encodable using
7883 : : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each,
7884 : : and BYTES is known to have enough bytes to supply NPATTERNS *
7885 : : NELTS_PER_PATTERN vector elements. Each element of BYTES contains
7886 : : BITS_PER_UNIT bits and the bytes are in target memory order.
7887 : :
7888 : : Return the vector on success, otherwise return NULL_RTX. */
7889 : :
7890 : : rtx
7891 : 225742 : native_decode_vector_rtx (machine_mode mode, const vec<target_unit> &bytes,
7892 : : unsigned int first_byte, unsigned int npatterns,
7893 : : unsigned int nelts_per_pattern)
7894 : : {
7895 : 225742 : rtx_vector_builder builder (mode, npatterns, nelts_per_pattern);
7896 : :
7897 : 451484 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
7898 : : GET_MODE_NUNITS (mode));
7899 : 225742 : if (elt_bits < BITS_PER_UNIT)
7900 : : {
7901 : : /* This is the only case in which elements can be smaller than a byte.
7902 : : Element 0 is always in the lsb of the containing byte. */
7903 : 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
7904 : 0 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
7905 : : {
7906 : 0 : unsigned int bit_index = first_byte * BITS_PER_UNIT + i * elt_bits;
7907 : 0 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
7908 : 0 : unsigned int lsb = bit_index % BITS_PER_UNIT;
7909 : 0 : unsigned int value = bytes[byte_index] >> lsb;
7910 : 0 : builder.quick_push (gen_int_mode (value, GET_MODE_INNER (mode)));
7911 : : }
7912 : : }
7913 : : else
7914 : : {
7915 : 901029 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
7916 : : {
7917 : 1350574 : rtx x = native_decode_rtx (GET_MODE_INNER (mode), bytes, first_byte);
7918 : 675287 : if (!x)
7919 : 0 : return NULL_RTX;
7920 : 675287 : builder.quick_push (x);
7921 : 675287 : first_byte += elt_bits / BITS_PER_UNIT;
7922 : : }
7923 : : }
7924 : 225742 : return builder.build ();
7925 : 225742 : }
7926 : :
7927 : : /* Extract a PRECISION-bit integer from bytes [FIRST_BYTE, FIRST_BYTE + SIZE)
7928 : : of target memory image BYTES. */
7929 : :
7930 : : wide_int
7931 : 11983569 : native_decode_int (const vec<target_unit> &bytes, unsigned int first_byte,
7932 : : unsigned int size, unsigned int precision)
7933 : : {
7934 : : /* Pull the bytes msb first, so that we can use simple
7935 : : shift-and-insert wide_int operations. */
7936 : 11983569 : wide_int result (wi::zero (precision));
7937 : 54247835 : for (unsigned int i = 0; i < size; ++i)
7938 : : {
7939 : 42264266 : unsigned int lsb = (size - i - 1) * BITS_PER_UNIT;
7940 : : /* Always constant because the inputs are. */
7941 : 42264266 : unsigned int subbyte
7942 : 42264266 : = subreg_size_offset_from_lsb (1, size, lsb).to_constant ();
7943 : 42264266 : result <<= BITS_PER_UNIT;
7944 : 42264266 : result |= bytes[first_byte + subbyte];
7945 : : }
7946 : 11983569 : return result;
7947 : : }
7948 : :
7949 : : /* Read an rtx of mode MODE from the target memory image given by BYTES,
7950 : : starting at byte FIRST_BYTE. Each element of BYTES contains BITS_PER_UNIT
7951 : : bits and the bytes are in target memory order. The image has enough
7952 : : values to specify all bytes of MODE.
7953 : :
7954 : : Return the rtx on success, otherwise return NULL_RTX. */
7955 : :
7956 : : rtx
7957 : 12282709 : native_decode_rtx (machine_mode mode, const vec<target_unit> &bytes,
7958 : : unsigned int first_byte)
7959 : : {
7960 : 12282709 : if (VECTOR_MODE_P (mode))
7961 : : {
7962 : : /* If we know at compile time how many elements there are,
7963 : : pull each element directly from BYTES. */
7964 : 59028 : unsigned int nelts;
7965 : 118056 : if (GET_MODE_NUNITS (mode).is_constant (&nelts))
7966 : 59028 : return native_decode_vector_rtx (mode, bytes, first_byte, nelts, 1);
7967 : : return NULL_RTX;
7968 : : }
7969 : :
7970 : 12223681 : scalar_int_mode imode;
7971 : 12223681 : if (is_a <scalar_int_mode> (mode, &imode)
7972 : 11983569 : && GET_MODE_PRECISION (imode) <= MAX_BITSIZE_MODE_ANY_INT)
7973 : : {
7974 : 11983569 : auto result = native_decode_int (bytes, first_byte,
7975 : 11983569 : GET_MODE_SIZE (imode),
7976 : 23967138 : GET_MODE_PRECISION (imode));
7977 : 11983569 : return immed_wide_int_const (result, imode);
7978 : 11983569 : }
7979 : :
7980 : 240112 : scalar_float_mode fmode;
7981 : 240112 : if (is_a <scalar_float_mode> (mode, &fmode))
7982 : : {
7983 : : /* We need to build an array of integers in target memory order.
7984 : : All integers before the last one have 32 bits; the last one may
7985 : : have 32 bits or fewer, depending on whether the mode bitsize
7986 : : is divisible by 32. */
7987 : 240082 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
7988 : 240082 : unsigned int num_el32 = CEIL (GET_MODE_BITSIZE (fmode), 32);
7989 : 240082 : memset (el32, 0, num_el32 * sizeof (long));
7990 : :
7991 : : /* The (maximum) number of target bytes per element of el32. */
7992 : 240082 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
7993 : 240082 : gcc_assert (bytes_per_el32 != 0);
7994 : :
7995 : 240082 : unsigned int mode_bytes = GET_MODE_SIZE (fmode);
7996 : 1671104 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
7997 : : {
7998 : 1431022 : unsigned int index = byte / bytes_per_el32;
7999 : 1431022 : unsigned int subbyte = byte % bytes_per_el32;
8000 : 1431022 : unsigned int int_bytes = MIN (bytes_per_el32,
8001 : : mode_bytes - index * bytes_per_el32);
8002 : : /* Always constant because the inputs are. */
8003 : 1431022 : unsigned int lsb
8004 : 1431022 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
8005 : 1431022 : el32[index] |= (unsigned long) bytes[first_byte + byte] << lsb;
8006 : : }
8007 : 240082 : REAL_VALUE_TYPE r;
8008 : 240082 : real_from_target (&r, el32, fmode);
8009 : 240082 : return const_double_from_real_value (r, fmode);
8010 : : }
8011 : :
8012 : 30 : if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
8013 : : {
8014 : 0 : scalar_mode smode = as_a <scalar_mode> (mode);
8015 : 0 : FIXED_VALUE_TYPE f;
8016 : 0 : f.data.low = 0;
8017 : 0 : f.data.high = 0;
8018 : 0 : f.mode = smode;
8019 : :
8020 : 0 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
8021 : 0 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
8022 : : {
8023 : : /* Always constant because the inputs are. */
8024 : 0 : unsigned int lsb
8025 : 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
8026 : 0 : unsigned HOST_WIDE_INT unit = bytes[first_byte + byte];
8027 : 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
8028 : 0 : f.data.high |= unit << (lsb - HOST_BITS_PER_WIDE_INT);
8029 : : else
8030 : 0 : f.data.low |= unit << lsb;
8031 : : }
8032 : 0 : return CONST_FIXED_FROM_FIXED_VALUE (f, mode);
8033 : : }
8034 : :
8035 : : return NULL_RTX;
8036 : : }
8037 : :
8038 : : /* Simplify a byte offset BYTE into CONST_VECTOR X. The main purpose
8039 : : is to convert a runtime BYTE value into a constant one. */
8040 : :
8041 : : static poly_uint64
8042 : 283751 : simplify_const_vector_byte_offset (rtx x, poly_uint64 byte)
8043 : : {
8044 : : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
8045 : 283751 : machine_mode mode = GET_MODE (x);
8046 : 567502 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
8047 : : GET_MODE_NUNITS (mode));
8048 : : /* The number of bits needed to encode one element from each pattern. */
8049 : 283751 : unsigned int sequence_bits = CONST_VECTOR_NPATTERNS (x) * elt_bits;
8050 : :
8051 : : /* Identify the start point in terms of a sequence number and a byte offset
8052 : : within that sequence. */
8053 : 283751 : poly_uint64 first_sequence;
8054 : 283751 : unsigned HOST_WIDE_INT subbit;
8055 : 283751 : if (can_div_trunc_p (byte * BITS_PER_UNIT, sequence_bits,
8056 : : &first_sequence, &subbit))
8057 : : {
8058 : 283751 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
8059 : 283751 : if (nelts_per_pattern == 1)
8060 : : /* This is a duplicated vector, so the value of FIRST_SEQUENCE
8061 : : doesn't matter. */
8062 : 233383 : byte = subbit / BITS_PER_UNIT;
8063 : 50368 : else if (nelts_per_pattern == 2 && known_gt (first_sequence, 0U))
8064 : : {
8065 : : /* The subreg drops the first element from each pattern and
8066 : : only uses the second element. Find the first sequence
8067 : : that starts on a byte boundary. */
8068 : 5568 : subbit += least_common_multiple (sequence_bits, BITS_PER_UNIT);
8069 : 5568 : byte = subbit / BITS_PER_UNIT;
8070 : : }
8071 : : }
8072 : 283751 : return byte;
8073 : : }
8074 : :
8075 : : /* Subroutine of simplify_subreg in which:
8076 : :
8077 : : - X is known to be a CONST_VECTOR
8078 : : - OUTERMODE is known to be a vector mode
8079 : :
8080 : : Try to handle the subreg by operating on the CONST_VECTOR encoding
8081 : : rather than on each individual element of the CONST_VECTOR.
8082 : :
8083 : : Return the simplified subreg on success, otherwise return NULL_RTX. */
8084 : :
8085 : : static rtx
8086 : 174385 : simplify_const_vector_subreg (machine_mode outermode, rtx x,
8087 : : machine_mode innermode, unsigned int first_byte)
8088 : : {
8089 : : /* Paradoxical subregs of vectors have dubious semantics. */
8090 : 174385 : if (paradoxical_subreg_p (outermode, innermode))
8091 : : return NULL_RTX;
8092 : :
8093 : : /* We can only preserve the semantics of a stepped pattern if the new
8094 : : vector element is the same as the original one. */
8095 : 174319 : if (CONST_VECTOR_STEPPED_P (x)
8096 : 194753 : && GET_MODE_INNER (outermode) != GET_MODE_INNER (innermode))
8097 : : return NULL_RTX;
8098 : :
8099 : : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
8100 : 166714 : unsigned int x_elt_bits
8101 : 166714 : = vector_element_size (GET_MODE_PRECISION (innermode),
8102 : : GET_MODE_NUNITS (innermode));
8103 : 166714 : unsigned int out_elt_bits
8104 : 166714 : = vector_element_size (GET_MODE_PRECISION (outermode),
8105 : : GET_MODE_NUNITS (outermode));
8106 : :
8107 : : /* The number of bits needed to encode one element from every pattern
8108 : : of the original vector. */
8109 : 166714 : unsigned int x_sequence_bits = CONST_VECTOR_NPATTERNS (x) * x_elt_bits;
8110 : :
8111 : : /* The number of bits needed to encode one element from every pattern
8112 : : of the result. */
8113 : 166714 : unsigned int out_sequence_bits
8114 : 166714 : = least_common_multiple (x_sequence_bits, out_elt_bits);
8115 : :
8116 : : /* Work out the number of interleaved patterns in the output vector
8117 : : and the number of encoded elements per pattern. */
8118 : 166714 : unsigned int out_npatterns = out_sequence_bits / out_elt_bits;
8119 : 166714 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
8120 : :
8121 : : /* The encoding scheme requires the number of elements to be a multiple
8122 : : of the number of patterns, so that each pattern appears at least once
8123 : : and so that the same number of elements appear from each pattern. */
8124 : 333428 : bool ok_p = multiple_p (GET_MODE_NUNITS (outermode), out_npatterns);
8125 : 166714 : unsigned int const_nunits;
8126 : 333428 : if (GET_MODE_NUNITS (outermode).is_constant (&const_nunits)
8127 : 166714 : && (!ok_p || out_npatterns * nelts_per_pattern > const_nunits))
8128 : : {
8129 : : /* Either the encoding is invalid, or applying it would give us
8130 : : more elements than we need. Just encode each element directly. */
8131 : : out_npatterns = const_nunits;
8132 : : nelts_per_pattern = 1;
8133 : : }
8134 : : else if (!ok_p)
8135 : : return NULL_RTX;
8136 : :
8137 : : /* Get enough bytes of X to form the new encoding. */
8138 : 166714 : unsigned int buffer_bits = out_npatterns * nelts_per_pattern * out_elt_bits;
8139 : 166714 : unsigned int buffer_bytes = CEIL (buffer_bits, BITS_PER_UNIT);
8140 : 166714 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8141 : 166714 : if (!native_encode_rtx (innermode, x, buffer, first_byte, buffer_bytes))
8142 : : return NULL_RTX;
8143 : :
8144 : : /* Reencode the bytes as OUTERMODE. */
8145 : 166714 : return native_decode_vector_rtx (outermode, buffer, 0, out_npatterns,
8146 : 166714 : nelts_per_pattern);
8147 : 166714 : }
8148 : :
8149 : : /* Try to simplify a subreg of a constant by encoding the subreg region
8150 : : as a sequence of target bytes and reading them back in the new mode.
8151 : : Return the new value on success, otherwise return null.
8152 : :
8153 : : The subreg has outer mode OUTERMODE, inner mode INNERMODE, inner value X
8154 : : and byte offset FIRST_BYTE. */
8155 : :
8156 : : static rtx
8157 : 11314824 : simplify_immed_subreg (fixed_size_mode outermode, rtx x,
8158 : : machine_mode innermode, unsigned int first_byte)
8159 : : {
8160 : 11314824 : unsigned int buffer_bytes = GET_MODE_SIZE (outermode);
8161 : 11314824 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8162 : :
8163 : : /* Some ports misuse CCmode. */
8164 : 11314824 : if (GET_MODE_CLASS (outermode) == MODE_CC && CONST_INT_P (x))
8165 : : return x;
8166 : :
8167 : : /* Paradoxical subregs read undefined values for bytes outside of the
8168 : : inner value. However, we have traditionally always sign-extended
8169 : : integer constants and zero-extended others. */
8170 : 11314208 : unsigned int inner_bytes = buffer_bytes;
8171 : 11314208 : if (paradoxical_subreg_p (outermode, innermode))
8172 : : {
8173 : 705632 : if (!GET_MODE_SIZE (innermode).is_constant (&inner_bytes))
8174 : 0 : return NULL_RTX;
8175 : :
8176 : 352816 : target_unit filler = 0;
8177 : 352816 : if (CONST_SCALAR_INT_P (x) && wi::neg_p (rtx_mode_t (x, innermode)))
8178 : 28561 : filler = -1;
8179 : :
8180 : : /* Add any leading bytes due to big-endian layout. The number of
8181 : : bytes must be constant because both modes have constant size. */
8182 : 352816 : unsigned int leading_bytes
8183 : 352816 : = -byte_lowpart_offset (outermode, innermode).to_constant ();
8184 : 352816 : for (unsigned int i = 0; i < leading_bytes; ++i)
8185 : 0 : buffer.quick_push (filler);
8186 : :
8187 : 352816 : if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8188 : 0 : return NULL_RTX;
8189 : :
8190 : : /* Add any trailing bytes due to little-endian layout. */
8191 : 4314916 : while (buffer.length () < buffer_bytes)
8192 : 1804642 : buffer.quick_push (filler);
8193 : : }
8194 : 10961392 : else if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8195 : : return NULL_RTX;
8196 : 11314208 : rtx ret = native_decode_rtx (outermode, buffer, 0);
8197 : 11314208 : if (ret && FLOAT_MODE_P (outermode))
8198 : : {
8199 : 126353 : auto_vec<target_unit, 128> buffer2 (buffer_bytes);
8200 : 126353 : if (!native_encode_rtx (outermode, ret, buffer2, 0, buffer_bytes))
8201 : : return NULL_RTX;
8202 : 1394902 : for (unsigned int i = 0; i < buffer_bytes; ++i)
8203 : 1268584 : if (buffer[i] != buffer2[i])
8204 : : return NULL_RTX;
8205 : 126353 : }
8206 : : return ret;
8207 : 11314824 : }
8208 : :
8209 : : /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
8210 : : Return 0 if no simplifications are possible. */
8211 : : rtx
8212 : 70234824 : simplify_context::simplify_subreg (machine_mode outermode, rtx op,
8213 : : machine_mode innermode, poly_uint64 byte)
8214 : : {
8215 : : /* Little bit of sanity checking. */
8216 : 70234824 : gcc_assert (innermode != VOIDmode);
8217 : 70234824 : gcc_assert (outermode != VOIDmode);
8218 : 70234824 : gcc_assert (innermode != BLKmode);
8219 : 70234824 : gcc_assert (outermode != BLKmode);
8220 : :
8221 : 70234824 : gcc_assert (GET_MODE (op) == innermode
8222 : : || GET_MODE (op) == VOIDmode);
8223 : :
8224 : 140469648 : poly_uint64 outersize = GET_MODE_SIZE (outermode);
8225 : 70234824 : if (!multiple_p (byte, outersize))
8226 : : return NULL_RTX;
8227 : :
8228 : 140469608 : poly_uint64 innersize = GET_MODE_SIZE (innermode);
8229 : 70234804 : if (maybe_ge (byte, innersize))
8230 : : return NULL_RTX;
8231 : :
8232 : 70234804 : if (outermode == innermode && known_eq (byte, 0U))
8233 : 4700702 : return op;
8234 : :
8235 : 65534102 : if (GET_CODE (op) == CONST_VECTOR)
8236 : 283751 : byte = simplify_const_vector_byte_offset (op, byte);
8237 : :
8238 : 131068204 : if (multiple_p (byte, GET_MODE_UNIT_SIZE (innermode)))
8239 : : {
8240 : 59582711 : rtx elt;
8241 : :
8242 : 51154738 : if (VECTOR_MODE_P (outermode)
8243 : 25283919 : && GET_MODE_INNER (outermode) == GET_MODE_INNER (innermode)
8244 : 61276985 : && vec_duplicate_p (op, &elt))
8245 : 13947 : return gen_vec_duplicate (outermode, elt);
8246 : :
8247 : 59578942 : if (outermode == GET_MODE_INNER (innermode)
8248 : 59578942 : && vec_duplicate_p (op, &elt))
8249 : 10178 : return elt;
8250 : : }
8251 : :
8252 : 65520155 : if (CONST_SCALAR_INT_P (op)
8253 : 54370582 : || CONST_DOUBLE_AS_FLOAT_P (op)
8254 : 54313629 : || CONST_FIXED_P (op)
8255 : 54313629 : || GET_CODE (op) == CONST_VECTOR)
8256 : : {
8257 : 11481538 : unsigned HOST_WIDE_INT cbyte;
8258 : 11481538 : if (byte.is_constant (&cbyte))
8259 : : {
8260 : 11481538 : if (GET_CODE (op) == CONST_VECTOR && VECTOR_MODE_P (outermode))
8261 : : {
8262 : 174385 : rtx tmp = simplify_const_vector_subreg (outermode, op,
8263 : : innermode, cbyte);
8264 : 174385 : if (tmp)
8265 : 11481538 : return tmp;
8266 : : }
8267 : :
8268 : 11314824 : fixed_size_mode fs_outermode;
8269 : 11314824 : if (is_a <fixed_size_mode> (outermode, &fs_outermode))
8270 : 11314824 : return simplify_immed_subreg (fs_outermode, op, innermode, cbyte);
8271 : : }
8272 : : }
8273 : :
8274 : : /* Changing mode twice with SUBREG => just change it once,
8275 : : or not at all if changing back op starting mode. */
8276 : 54038617 : if (GET_CODE (op) == SUBREG)
8277 : : {
8278 : 1277214 : machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
8279 : 2554428 : poly_uint64 innermostsize = GET_MODE_SIZE (innermostmode);
8280 : 1277214 : rtx newx;
8281 : :
8282 : : /* Make sure that the relationship between the two subregs is
8283 : : known at compile time. */
8284 : 1277214 : if (!ordered_p (outersize, innermostsize))
8285 : : return NULL_RTX;
8286 : :
8287 : 1277214 : if (outermode == innermostmode
8288 : 691789 : && known_eq (byte, subreg_lowpart_offset (outermode, innermode))
8289 : 1969002 : && known_eq (SUBREG_BYTE (op),
8290 : : subreg_lowpart_offset (innermode, innermostmode)))
8291 : 691788 : return SUBREG_REG (op);
8292 : :
8293 : : /* Work out the memory offset of the final OUTERMODE value relative
8294 : : to the inner value of OP. */
8295 : 585426 : poly_int64 mem_offset = subreg_memory_offset (outermode,
8296 : : innermode, byte);
8297 : 585426 : poly_int64 op_mem_offset = subreg_memory_offset (op);
8298 : 585426 : poly_int64 final_offset = mem_offset + op_mem_offset;
8299 : :
8300 : : /* See whether resulting subreg will be paradoxical. */
8301 : 585426 : if (!paradoxical_subreg_p (outermode, innermostmode))
8302 : : {
8303 : : /* Bail out in case resulting subreg would be incorrect. */
8304 : 984530 : if (maybe_lt (final_offset, 0)
8305 : 984525 : || maybe_ge (poly_uint64 (final_offset), innermostsize)
8306 : 984529 : || !multiple_p (final_offset, outersize))
8307 : 5 : return NULL_RTX;
8308 : : }
8309 : : else
8310 : : {
8311 : 93161 : poly_int64 required_offset = subreg_memory_offset (outermode,
8312 : : innermostmode, 0);
8313 : 93161 : if (maybe_ne (final_offset, required_offset))
8314 : 0 : return NULL_RTX;
8315 : : /* Paradoxical subregs always have byte offset 0. */
8316 : 93161 : final_offset = 0;
8317 : : }
8318 : :
8319 : : /* Recurse for further possible simplifications. */
8320 : 585421 : newx = simplify_subreg (outermode, SUBREG_REG (op), innermostmode,
8321 : 585421 : final_offset);
8322 : 585421 : if (newx)
8323 : : return newx;
8324 : 585044 : if (validate_subreg (outermode, innermostmode,
8325 : 585044 : SUBREG_REG (op), final_offset))
8326 : : {
8327 : 531422 : newx = gen_rtx_SUBREG (outermode, SUBREG_REG (op), final_offset);
8328 : 531422 : if (SUBREG_PROMOTED_VAR_P (op)
8329 : 321 : && SUBREG_PROMOTED_SIGN (op) >= 0
8330 : 321 : && GET_MODE_CLASS (outermode) == MODE_INT
8331 : 317 : && known_ge (outersize, innersize)
8332 : 292 : && known_le (outersize, innermostsize)
8333 : 531426 : && subreg_lowpart_p (newx))
8334 : : {
8335 : 4 : SUBREG_PROMOTED_VAR_P (newx) = 1;
8336 : 4 : SUBREG_PROMOTED_SET (newx, SUBREG_PROMOTED_GET (op));
8337 : : }
8338 : 531422 : return newx;
8339 : : }
8340 : : return NULL_RTX;
8341 : : }
8342 : :
8343 : : /* SUBREG of a hard register => just change the register number
8344 : : and/or mode. If the hard register is not valid in that mode,
8345 : : suppress this simplification. If the hard register is the stack,
8346 : : frame, or argument pointer, leave this as a SUBREG. */
8347 : :
8348 : 52761403 : if (REG_P (op) && HARD_REGISTER_P (op))
8349 : : {
8350 : 10903503 : unsigned int regno, final_regno;
8351 : :
8352 : 10903503 : regno = REGNO (op);
8353 : 10903503 : final_regno = simplify_subreg_regno (regno, innermode, byte, outermode);
8354 : 10903503 : if (HARD_REGISTER_NUM_P (final_regno))
8355 : : {
8356 : 10879490 : rtx x = gen_rtx_REG_offset (op, outermode, final_regno,
8357 : : subreg_memory_offset (outermode,
8358 : : innermode, byte));
8359 : :
8360 : : /* Propagate original regno. We don't have any way to specify
8361 : : the offset inside original regno, so do so only for lowpart.
8362 : : The information is used only by alias analysis that cannot
8363 : : grog partial register anyway. */
8364 : :
8365 : 10879490 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte))
8366 : 8172566 : ORIGINAL_REGNO (x) = ORIGINAL_REGNO (op);
8367 : 10879490 : return x;
8368 : : }
8369 : : }
8370 : :
8371 : : /* If we have a SUBREG of a register that we are replacing and we are
8372 : : replacing it with a MEM, make a new MEM and try replacing the
8373 : : SUBREG with it. Don't do this if the MEM has a mode-dependent address
8374 : : or if we would be widening it. */
8375 : :
8376 : 41881913 : if (MEM_P (op)
8377 : 1731884 : && ! mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op))
8378 : : /* Allow splitting of volatile memory references in case we don't
8379 : : have instruction to move the whole thing. */
8380 : 1731881 : && (! MEM_VOLATILE_P (op)
8381 : 44167 : || ! have_insn_for (SET, innermode))
8382 : : && !(STRICT_ALIGNMENT && MEM_ALIGN (op) < GET_MODE_ALIGNMENT (outermode))
8383 : 43569627 : && known_le (outersize, innersize))
8384 : 825840 : return adjust_address_nv (op, outermode, byte);
8385 : :
8386 : : /* Handle complex or vector values represented as CONCAT or VEC_CONCAT
8387 : : of two parts. */
8388 : 41056073 : if (GET_CODE (op) == CONCAT
8389 : 41056073 : || GET_CODE (op) == VEC_CONCAT)
8390 : : {
8391 : 192269 : poly_uint64 final_offset;
8392 : 192269 : rtx part, res;
8393 : :
8394 : 192269 : machine_mode part_mode = GET_MODE (XEXP (op, 0));
8395 : 192269 : if (part_mode == VOIDmode)
8396 : 11 : part_mode = GET_MODE_INNER (GET_MODE (op));
8397 : 384538 : poly_uint64 part_size = GET_MODE_SIZE (part_mode);
8398 : 192269 : if (known_lt (byte, part_size))
8399 : : {
8400 : 190506 : part = XEXP (op, 0);
8401 : 190506 : final_offset = byte;
8402 : : }
8403 : 1763 : else if (known_ge (byte, part_size))
8404 : : {
8405 : 1763 : part = XEXP (op, 1);
8406 : 1763 : final_offset = byte - part_size;
8407 : : }
8408 : : else
8409 : : return NULL_RTX;
8410 : :
8411 : 192269 : if (maybe_gt (final_offset + outersize, part_size))
8412 : : return NULL_RTX;
8413 : :
8414 : 129804 : part_mode = GET_MODE (part);
8415 : 129804 : if (part_mode == VOIDmode)
8416 : 0 : part_mode = GET_MODE_INNER (GET_MODE (op));
8417 : 129804 : res = simplify_subreg (outermode, part, part_mode, final_offset);
8418 : 129804 : if (res)
8419 : : return res;
8420 : 295 : if (validate_subreg (outermode, part_mode, part, final_offset))
8421 : 295 : return gen_rtx_SUBREG (outermode, part, final_offset);
8422 : : return NULL_RTX;
8423 : : }
8424 : :
8425 : : /* Simplify
8426 : : (subreg (vec_merge (X)
8427 : : (vector)
8428 : : (const_int ((1 << N) | M)))
8429 : : (N * sizeof (outermode)))
8430 : : to
8431 : : (subreg (X) (N * sizeof (outermode)))
8432 : : */
8433 : 40863804 : unsigned int idx;
8434 : 81727608 : if (constant_multiple_p (byte, GET_MODE_SIZE (outermode), &idx)
8435 : 40863804 : && idx < HOST_BITS_PER_WIDE_INT
8436 : 40863804 : && GET_CODE (op) == VEC_MERGE
8437 : 611560 : && GET_MODE_INNER (innermode) == outermode
8438 : 4840 : && CONST_INT_P (XEXP (op, 2))
8439 : 40868062 : && (UINTVAL (XEXP (op, 2)) & (HOST_WIDE_INT_1U << idx)) != 0)
8440 : 4249 : return simplify_gen_subreg (outermode, XEXP (op, 0), innermode, byte);
8441 : :
8442 : : /* A SUBREG resulting from a zero extension may fold to zero if
8443 : : it extracts higher bits that the ZERO_EXTEND's source bits. */
8444 : 40859555 : if (GET_CODE (op) == ZERO_EXTEND && SCALAR_INT_MODE_P (innermode))
8445 : : {
8446 : 223538 : poly_uint64 bitpos = subreg_lsb_1 (outermode, innermode, byte);
8447 : 223538 : if (known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (XEXP (op, 0)))))
8448 : 56064 : return CONST0_RTX (outermode);
8449 : : }
8450 : :
8451 : : /* Optimize SUBREGS of scalar integral ASHIFT by a valid constant. */
8452 : 40803491 : if (GET_CODE (op) == ASHIFT
8453 : 740305 : && SCALAR_INT_MODE_P (innermode)
8454 : 713324 : && CONST_INT_P (XEXP (op, 1))
8455 : 638007 : && INTVAL (XEXP (op, 1)) > 0
8456 : 42181753 : && known_gt (GET_MODE_BITSIZE (innermode), INTVAL (XEXP (op, 1))))
8457 : : {
8458 : 637957 : HOST_WIDE_INT val = INTVAL (XEXP (op, 1));
8459 : : /* A lowpart SUBREG of a ASHIFT by a constant may fold to zero. */
8460 : 637957 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte)
8461 : 1237610 : && known_le (GET_MODE_BITSIZE (outermode), val))
8462 : 208463 : return CONST0_RTX (outermode);
8463 : : /* Optimize the highpart SUBREG of a suitable ASHIFT (ZERO_EXTEND). */
8464 : 465675 : if (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
8465 : 36800 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == outermode
8466 : 73384 : && known_eq (GET_MODE_BITSIZE (outermode), val)
8467 : 72362 : && known_eq (GET_MODE_BITSIZE (innermode), 2 * val)
8468 : 502475 : && known_eq (subreg_highpart_offset (outermode, innermode), byte))
8469 : 36181 : return XEXP (XEXP (op, 0), 0);
8470 : : }
8471 : :
8472 : 42111315 : auto distribute_subreg = [&](rtx op)
8473 : : {
8474 : 1516287 : return simplify_subreg (outermode, op, innermode, byte);
8475 : 40595028 : };
8476 : :
8477 : : /* Try distributing the subreg through logic operations, if that
8478 : : leads to all subexpressions being simplified. For example,
8479 : : distributing the outer subreg in:
8480 : :
8481 : : (subreg:SI (not:QI (subreg:QI (reg:SI X) <lowpart>)) 0)
8482 : :
8483 : : gives:
8484 : :
8485 : : (not:SI (reg:SI X))
8486 : :
8487 : : This should be a win if the outermode is word_mode, since logical
8488 : : operations on word_mode should (a) be no more expensive than logical
8489 : : operations on subword modes and (b) are likely to be cheaper than
8490 : : logical operations on multiword modes.
8491 : :
8492 : : Otherwise, handle the case where the subreg is non-narrowing and does
8493 : : not change the number of words. The non-narrowing condition ensures
8494 : : that we don't convert word_mode operations to subword operations. */
8495 : 40595028 : scalar_int_mode int_outermode, int_innermode;
8496 : 40595028 : if (is_a <scalar_int_mode> (outermode, &int_outermode)
8497 : 33924154 : && is_a <scalar_int_mode> (innermode, &int_innermode)
8498 : 73412536 : && (outermode == word_mode
8499 : 18338127 : || ((GET_MODE_PRECISION (int_outermode)
8500 : 18338127 : >= GET_MODE_PRECISION (int_innermode))
8501 : 4653456 : && (CEIL (GET_MODE_SIZE (int_outermode), UNITS_PER_WORD)
8502 : 4573176 : <= CEIL (GET_MODE_SIZE (int_innermode), UNITS_PER_WORD)))))
8503 : 18991235 : switch (GET_CODE (op))
8504 : : {
8505 : 33963 : case NOT:
8506 : 33963 : if (rtx op0 = distribute_subreg (XEXP (op, 0)))
8507 : 1382 : return simplify_gen_unary (GET_CODE (op), outermode, op0, outermode);
8508 : : break;
8509 : :
8510 : 478559 : case AND:
8511 : 478559 : case IOR:
8512 : 478559 : case XOR:
8513 : 478559 : if (rtx op0 = distribute_subreg (XEXP (op, 0)))
8514 : 218691 : if (rtx op1 = distribute_subreg (XEXP (op, 1)))
8515 : 214604 : return simplify_gen_binary (GET_CODE (op), outermode, op0, op1);
8516 : : break;
8517 : :
8518 : : default:
8519 : : break;
8520 : : }
8521 : :
8522 : 40379042 : if (is_a <scalar_int_mode> (outermode, &int_outermode)
8523 : 33708168 : && is_a <scalar_int_mode> (innermode, &int_innermode)
8524 : 74087210 : && known_eq (byte, subreg_lowpart_offset (int_outermode, int_innermode)))
8525 : : {
8526 : : /* Handle polynomial integers. The upper bits of a paradoxical
8527 : : subreg are undefined, so this is safe regardless of whether
8528 : : we're truncating or extending. */
8529 : 30343691 : if (CONST_POLY_INT_P (op))
8530 : : {
8531 : : poly_wide_int val
8532 : : = poly_wide_int::from (const_poly_int_value (op),
8533 : : GET_MODE_PRECISION (int_outermode),
8534 : : SIGNED);
8535 : : return immed_wide_int_const (val, int_outermode);
8536 : : }
8537 : :
8538 : 30343691 : if (GET_MODE_PRECISION (int_outermode)
8539 : 30343691 : < GET_MODE_PRECISION (int_innermode))
8540 : : {
8541 : 17202074 : rtx tem = simplify_truncation (int_outermode, op, int_innermode);
8542 : 17202074 : if (tem)
8543 : : return tem;
8544 : : }
8545 : : }
8546 : :
8547 : : /* If the outer mode is not integral, try taking a subreg with the equivalent
8548 : : integer outer mode and then bitcasting the result.
8549 : : Other simplifications rely on integer to integer subregs and we'd
8550 : : potentially miss out on optimizations otherwise. */
8551 : 78781776 : if (known_gt (GET_MODE_SIZE (innermode),
8552 : : GET_MODE_SIZE (outermode))
8553 : 19627964 : && SCALAR_INT_MODE_P (innermode)
8554 : 18558796 : && !SCALAR_INT_MODE_P (outermode)
8555 : 59192942 : && int_mode_for_size (GET_MODE_BITSIZE (outermode),
8556 : 87045 : 0).exists (&int_outermode))
8557 : : {
8558 : 87045 : rtx tem = simplify_subreg (int_outermode, op, innermode, byte);
8559 : 87045 : if (tem)
8560 : 1958 : return lowpart_subreg (outermode, tem, int_outermode);
8561 : : }
8562 : :
8563 : : /* If OP is a vector comparison and the subreg is not changing the
8564 : : number of elements or the size of the elements, change the result
8565 : : of the comparison to the new mode. */
8566 : 39388930 : if (COMPARISON_P (op)
8567 : 255457 : && VECTOR_MODE_P (outermode)
8568 : 195879 : && VECTOR_MODE_P (innermode)
8569 : 587613 : && known_eq (GET_MODE_NUNITS (outermode), GET_MODE_NUNITS (innermode))
8570 : 39747676 : && known_eq (GET_MODE_UNIT_SIZE (outermode),
8571 : : GET_MODE_UNIT_SIZE (innermode)))
8572 : 119238 : return simplify_gen_relational (GET_CODE (op), outermode, innermode,
8573 : 119238 : XEXP (op, 0), XEXP (op, 1));
8574 : :
8575 : : /* Distribute non-paradoxical subregs through logic ops in cases where
8576 : : one term disappears.
8577 : :
8578 : : (subreg:M1 (and:M2 X C1)) -> (subreg:M1 X)
8579 : : (subreg:M1 (ior:M2 X C1)) -> (subreg:M1 C1)
8580 : : (subreg:M1 (xor:M2 X C1)) -> (subreg:M1 (not:M2 X))
8581 : :
8582 : : if M2 is no smaller than M1 and (subreg:M1 C1) is all-ones.
8583 : :
8584 : : (subreg:M1 (and:M2 X C2)) -> (subreg:M1 C2)
8585 : : (subreg:M1 (ior/xor:M2 X C2)) -> (subreg:M1 X)
8586 : :
8587 : : if M2 is no smaller than M1 and (subreg:M1 C2) is zero. */
8588 : 39269692 : if (known_ge (innersize, outersize)
8589 : 25662684 : && GET_MODE_CLASS (outermode) == GET_MODE_CLASS (innermode)
8590 : 23717590 : && (GET_CODE (op) == AND || GET_CODE (op) == IOR || GET_CODE (op) == XOR)
8591 : 40831893 : && CONSTANT_P (XEXP (op, 1)))
8592 : : {
8593 : 778310 : rtx op1_subreg = distribute_subreg (XEXP (op, 1));
8594 : 778310 : if (op1_subreg == CONSTM1_RTX (outermode))
8595 : : {
8596 : 122819 : if (GET_CODE (op) == IOR)
8597 : : return op1_subreg;
8598 : 122585 : rtx op0 = XEXP (op, 0);
8599 : 122585 : if (GET_CODE (op) == XOR)
8600 : 789 : op0 = simplify_gen_unary (NOT, innermode, op0, innermode);
8601 : 122585 : return simplify_gen_subreg (outermode, op0, innermode, byte);
8602 : : }
8603 : :
8604 : 655491 : if (op1_subreg == CONST0_RTX (outermode))
8605 : 12658 : return (GET_CODE (op) == AND
8606 : 12658 : ? op1_subreg
8607 : 6764 : : distribute_subreg (XEXP (op, 0)));
8608 : : }
8609 : :
8610 : : return NULL_RTX;
8611 : : }
8612 : :
8613 : : /* Make a SUBREG operation or equivalent if it folds. */
8614 : :
8615 : : rtx
8616 : 44705575 : simplify_context::simplify_gen_subreg (machine_mode outermode, rtx op,
8617 : : machine_mode innermode,
8618 : : poly_uint64 byte)
8619 : : {
8620 : 44705575 : rtx newx;
8621 : :
8622 : 44705575 : newx = simplify_subreg (outermode, op, innermode, byte);
8623 : 44705575 : if (newx)
8624 : : return newx;
8625 : :
8626 : 20813995 : if (GET_CODE (op) == SUBREG
8627 : 20813995 : || GET_CODE (op) == CONCAT
8628 : 20784146 : || CONST_SCALAR_INT_P (op)
8629 : 20784120 : || CONST_DOUBLE_AS_FLOAT_P (op)
8630 : 20784120 : || CONST_FIXED_P (op)
8631 : 20784120 : || GET_CODE (op) == CONST_VECTOR)
8632 : : return NULL_RTX;
8633 : :
8634 : 20784110 : if (validate_subreg (outermode, innermode, op, byte))
8635 : 20751887 : return gen_rtx_SUBREG (outermode, op, byte);
8636 : :
8637 : : return NULL_RTX;
8638 : : }
8639 : :
8640 : : /* Generates a subreg to get the least significant part of EXPR (in mode
8641 : : INNER_MODE) to OUTER_MODE. */
8642 : :
8643 : : rtx
8644 : 33503512 : simplify_context::lowpart_subreg (machine_mode outer_mode, rtx expr,
8645 : : machine_mode inner_mode)
8646 : : {
8647 : 33503512 : return simplify_gen_subreg (outer_mode, expr, inner_mode,
8648 : 33503512 : subreg_lowpart_offset (outer_mode, inner_mode));
8649 : : }
8650 : :
8651 : : /* Generate RTX to select element at INDEX out of vector OP. */
8652 : :
8653 : : rtx
8654 : 677498 : simplify_context::simplify_gen_vec_select (rtx op, unsigned int index)
8655 : : {
8656 : 677498 : gcc_assert (VECTOR_MODE_P (GET_MODE (op)));
8657 : :
8658 : 677498 : scalar_mode imode = GET_MODE_INNER (GET_MODE (op));
8659 : :
8660 : 1354996 : if (known_eq (index * GET_MODE_SIZE (imode),
8661 : : subreg_lowpart_offset (imode, GET_MODE (op))))
8662 : : {
8663 : 677348 : rtx res = lowpart_subreg (imode, op, GET_MODE (op));
8664 : 677348 : if (res)
8665 : : return res;
8666 : : }
8667 : :
8668 : 215 : rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (index)));
8669 : 215 : return gen_rtx_VEC_SELECT (imode, op, tmp);
8670 : : }
8671 : :
8672 : :
8673 : : /* Simplify X, an rtx expression.
8674 : :
8675 : : Return the simplified expression or NULL if no simplifications
8676 : : were possible.
8677 : :
8678 : : This is the preferred entry point into the simplification routines;
8679 : : however, we still allow passes to call the more specific routines.
8680 : :
8681 : : Right now GCC has three (yes, three) major bodies of RTL simplification
8682 : : code that need to be unified.
8683 : :
8684 : : 1. fold_rtx in cse.cc. This code uses various CSE specific
8685 : : information to aid in RTL simplification.
8686 : :
8687 : : 2. simplify_rtx in combine.cc. Similar to fold_rtx, except that
8688 : : it uses combine specific information to aid in RTL
8689 : : simplification.
8690 : :
8691 : : 3. The routines in this file.
8692 : :
8693 : :
8694 : : Long term we want to only have one body of simplification code; to
8695 : : get to that state I recommend the following steps:
8696 : :
8697 : : 1. Pour over fold_rtx & simplify_rtx and move any simplifications
8698 : : which are not pass dependent state into these routines.
8699 : :
8700 : : 2. As code is moved by #1, change fold_rtx & simplify_rtx to
8701 : : use this routine whenever possible.
8702 : :
8703 : : 3. Allow for pass dependent state to be provided to these
8704 : : routines and add simplifications based on the pass dependent
8705 : : state. Remove code from cse.cc & combine.cc that becomes
8706 : : redundant/dead.
8707 : :
8708 : : It will take time, but ultimately the compiler will be easier to
8709 : : maintain and improve. It's totally silly that when we add a
8710 : : simplification that it needs to be added to 4 places (3 for RTL
8711 : : simplification and 1 for tree simplification. */
8712 : :
8713 : : rtx
8714 : 47387500 : simplify_rtx (const_rtx x)
8715 : : {
8716 : 47387500 : const enum rtx_code code = GET_CODE (x);
8717 : 47387500 : const machine_mode mode = GET_MODE (x);
8718 : :
8719 : 47387500 : switch (GET_RTX_CLASS (code))
8720 : : {
8721 : 903971 : case RTX_UNARY:
8722 : 1807942 : return simplify_unary_operation (code, mode,
8723 : 903971 : XEXP (x, 0), GET_MODE (XEXP (x, 0)));
8724 : 27172506 : case RTX_COMM_ARITH:
8725 : 27172506 : if (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8726 : 528045 : return simplify_gen_binary (code, mode, XEXP (x, 1), XEXP (x, 0));
8727 : :
8728 : : /* Fall through. */
8729 : :
8730 : 33052163 : case RTX_BIN_ARITH:
8731 : 33052163 : return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
8732 : :
8733 : 64535 : case RTX_TERNARY:
8734 : 64535 : case RTX_BITFIELD_OPS:
8735 : 64535 : return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
8736 : 64535 : XEXP (x, 0), XEXP (x, 1),
8737 : 64535 : XEXP (x, 2));
8738 : :
8739 : 177537 : case RTX_COMPARE:
8740 : 177537 : case RTX_COMM_COMPARE:
8741 : 177537 : return simplify_relational_operation (code, mode,
8742 : 177537 : ((GET_MODE (XEXP (x, 0))
8743 : : != VOIDmode)
8744 : : ? GET_MODE (XEXP (x, 0))
8745 : 485 : : GET_MODE (XEXP (x, 1))),
8746 : 177537 : XEXP (x, 0),
8747 : 355074 : XEXP (x, 1));
8748 : :
8749 : 230256 : case RTX_EXTRA:
8750 : 230256 : if (code == SUBREG)
8751 : 2453 : return simplify_subreg (mode, SUBREG_REG (x),
8752 : 2453 : GET_MODE (SUBREG_REG (x)),
8753 : 2453 : SUBREG_BYTE (x));
8754 : : break;
8755 : :
8756 : 6700943 : case RTX_OBJ:
8757 : 6700943 : if (code == LO_SUM)
8758 : : {
8759 : : /* Convert (lo_sum (high FOO) FOO) to FOO. */
8760 : 0 : if (GET_CODE (XEXP (x, 0)) == HIGH
8761 : 0 : && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
8762 : 0 : return XEXP (x, 1);
8763 : : }
8764 : : break;
8765 : :
8766 : : default:
8767 : : break;
8768 : : }
8769 : : return NULL;
8770 : : }
8771 : :
8772 : : #if CHECKING_P
8773 : :
8774 : : namespace selftest {
8775 : :
8776 : : /* Make a unique pseudo REG of mode MODE for use by selftests. */
8777 : :
8778 : : static rtx
8779 : 2672 : make_test_reg (machine_mode mode)
8780 : : {
8781 : 2672 : static int test_reg_num = LAST_VIRTUAL_REGISTER + 1;
8782 : :
8783 : 2672 : return gen_rtx_REG (mode, test_reg_num++);
8784 : : }
8785 : :
8786 : : static void
8787 : 40 : test_scalar_int_ops (machine_mode mode)
8788 : : {
8789 : 40 : rtx op0 = make_test_reg (mode);
8790 : 40 : rtx op1 = make_test_reg (mode);
8791 : 40 : rtx six = GEN_INT (6);
8792 : :
8793 : 40 : rtx neg_op0 = simplify_gen_unary (NEG, mode, op0, mode);
8794 : 40 : rtx not_op0 = simplify_gen_unary (NOT, mode, op0, mode);
8795 : 40 : rtx bswap_op0 = simplify_gen_unary (BSWAP, mode, op0, mode);
8796 : :
8797 : 40 : rtx and_op0_op1 = simplify_gen_binary (AND, mode, op0, op1);
8798 : 40 : rtx ior_op0_op1 = simplify_gen_binary (IOR, mode, op0, op1);
8799 : 40 : rtx xor_op0_op1 = simplify_gen_binary (XOR, mode, op0, op1);
8800 : :
8801 : 40 : rtx and_op0_6 = simplify_gen_binary (AND, mode, op0, six);
8802 : 40 : rtx and_op1_6 = simplify_gen_binary (AND, mode, op1, six);
8803 : :
8804 : : /* Test some binary identities. */
8805 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, op0, const0_rtx));
8806 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, const0_rtx, op0));
8807 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MINUS, mode, op0, const0_rtx));
8808 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, op0, const1_rtx));
8809 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, const1_rtx, op0));
8810 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (DIV, mode, op0, const1_rtx));
8811 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, constm1_rtx));
8812 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, constm1_rtx, op0));
8813 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, const0_rtx));
8814 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, const0_rtx, op0));
8815 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, op0, const0_rtx));
8816 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, const0_rtx, op0));
8817 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFT, mode, op0, const0_rtx));
8818 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATE, mode, op0, const0_rtx));
8819 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFTRT, mode, op0, const0_rtx));
8820 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (LSHIFTRT, mode, op0, const0_rtx));
8821 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATERT, mode, op0, const0_rtx));
8822 : :
8823 : : /* Test some self-inverse operations. */
8824 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NEG, mode, neg_op0, mode));
8825 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NOT, mode, not_op0, mode));
8826 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (BSWAP, mode, bswap_op0, mode));
8827 : :
8828 : : /* Test some reflexive operations. */
8829 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, op0));
8830 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, op0));
8831 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMIN, mode, op0, op0));
8832 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMAX, mode, op0, op0));
8833 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMIN, mode, op0, op0));
8834 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMAX, mode, op0, op0));
8835 : :
8836 : 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (MINUS, mode, op0, op0));
8837 : 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (XOR, mode, op0, op0));
8838 : :
8839 : : /* Test simplify_distributive_operation. */
8840 : 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, xor_op0_op1, six),
8841 : : simplify_gen_binary (XOR, mode, and_op0_6, and_op1_6));
8842 : 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, ior_op0_op1, six),
8843 : : simplify_gen_binary (IOR, mode, and_op0_6, and_op1_6));
8844 : 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, and_op0_op1, six),
8845 : : simplify_gen_binary (AND, mode, and_op0_6, and_op1_6));
8846 : :
8847 : : /* Test useless extensions are eliminated. */
8848 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (TRUNCATE, mode, op0, mode));
8849 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (ZERO_EXTEND, mode, op0, mode));
8850 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (SIGN_EXTEND, mode, op0, mode));
8851 : 40 : ASSERT_RTX_EQ (op0, lowpart_subreg (mode, op0, mode));
8852 : 40 : }
8853 : :
8854 : : /* Verify some simplifications of integer extension/truncation.
8855 : : Machine mode BMODE is the guaranteed wider than SMODE. */
8856 : :
8857 : : static void
8858 : 24 : test_scalar_int_ext_ops (machine_mode bmode, machine_mode smode)
8859 : : {
8860 : 24 : rtx sreg = make_test_reg (smode);
8861 : :
8862 : : /* Check truncation of extension. */
8863 : 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8864 : : simplify_gen_unary (ZERO_EXTEND, bmode,
8865 : : sreg, smode),
8866 : : bmode),
8867 : : sreg);
8868 : 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8869 : : simplify_gen_unary (SIGN_EXTEND, bmode,
8870 : : sreg, smode),
8871 : : bmode),
8872 : : sreg);
8873 : 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8874 : : lowpart_subreg (bmode, sreg, smode),
8875 : : bmode),
8876 : : sreg);
8877 : :
8878 : : /* Test extensions, followed by logic ops, followed by truncations. */
8879 : 24 : rtx bsubreg = lowpart_subreg (bmode, sreg, smode);
8880 : 24 : rtx smask = gen_int_mode (GET_MODE_MASK (smode), bmode);
8881 : 24 : rtx inv_smask = gen_int_mode (~GET_MODE_MASK (smode), bmode);
8882 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8883 : : simplify_gen_binary (AND, bmode,
8884 : : bsubreg, smask),
8885 : : bmode),
8886 : : sreg);
8887 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8888 : : simplify_gen_binary (AND, bmode,
8889 : : bsubreg, inv_smask),
8890 : : bmode),
8891 : : const0_rtx);
8892 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8893 : : simplify_gen_binary (IOR, bmode,
8894 : : bsubreg, smask),
8895 : : bmode),
8896 : : constm1_rtx);
8897 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8898 : : simplify_gen_binary (IOR, bmode,
8899 : : bsubreg, inv_smask),
8900 : : bmode),
8901 : : sreg);
8902 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8903 : : simplify_gen_binary (XOR, bmode,
8904 : : bsubreg, smask),
8905 : : bmode),
8906 : : lowpart_subreg (smode,
8907 : : gen_rtx_NOT (bmode, bsubreg),
8908 : : bmode));
8909 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8910 : : simplify_gen_binary (XOR, bmode,
8911 : : bsubreg, inv_smask),
8912 : : bmode),
8913 : : sreg);
8914 : :
8915 : 24 : if (known_le (GET_MODE_PRECISION (bmode), BITS_PER_WORD))
8916 : : {
8917 : 24 : rtx breg1 = make_test_reg (bmode);
8918 : 24 : rtx breg2 = make_test_reg (bmode);
8919 : 24 : rtx ssubreg1 = lowpart_subreg (smode, breg1, bmode);
8920 : 24 : rtx ssubreg2 = lowpart_subreg (smode, breg2, bmode);
8921 : 24 : rtx not_1 = simplify_gen_unary (NOT, smode, ssubreg1, smode);
8922 : 24 : rtx and_12 = simplify_gen_binary (AND, smode, ssubreg1, ssubreg2);
8923 : 24 : rtx ior_12 = simplify_gen_binary (IOR, smode, ssubreg1, ssubreg2);
8924 : 24 : rtx xor_12 = simplify_gen_binary (XOR, smode, ssubreg1, ssubreg2);
8925 : 24 : rtx and_n12 = simplify_gen_binary (AND, smode, not_1, ssubreg2);
8926 : 24 : rtx ior_n12 = simplify_gen_binary (IOR, smode, not_1, ssubreg2);
8927 : 24 : rtx xor_12_c = simplify_gen_binary (XOR, smode, xor_12, const1_rtx);
8928 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, not_1, smode),
8929 : : gen_rtx_NOT (bmode, breg1));
8930 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, and_12, smode),
8931 : : gen_rtx_AND (bmode, breg1, breg2));
8932 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, ior_12, smode),
8933 : : gen_rtx_IOR (bmode, breg1, breg2));
8934 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, xor_12, smode),
8935 : : gen_rtx_XOR (bmode, breg1, breg2));
8936 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, and_n12, smode),
8937 : : gen_rtx_AND (bmode, gen_rtx_NOT (bmode, breg1), breg2));
8938 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, ior_n12, smode),
8939 : : gen_rtx_IOR (bmode, gen_rtx_NOT (bmode, breg1), breg2));
8940 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, xor_12_c, smode),
8941 : : gen_rtx_XOR (bmode,
8942 : : gen_rtx_XOR (bmode, breg1, breg2),
8943 : : const1_rtx));
8944 : : }
8945 : 24 : }
8946 : :
8947 : : /* Verify more simplifications of integer extension/truncation.
8948 : : BMODE is wider than MMODE which is wider than SMODE. */
8949 : :
8950 : : static void
8951 : 16 : test_scalar_int_ext_ops2 (machine_mode bmode, machine_mode mmode,
8952 : : machine_mode smode)
8953 : : {
8954 : 16 : rtx breg = make_test_reg (bmode);
8955 : 16 : rtx mreg = make_test_reg (mmode);
8956 : 16 : rtx sreg = make_test_reg (smode);
8957 : :
8958 : : /* Check truncate of truncate. */
8959 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8960 : : simplify_gen_unary (TRUNCATE, mmode,
8961 : : breg, bmode),
8962 : : mmode),
8963 : : simplify_gen_unary (TRUNCATE, smode, breg, bmode));
8964 : :
8965 : : /* Check extension of extension. */
8966 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (ZERO_EXTEND, bmode,
8967 : : simplify_gen_unary (ZERO_EXTEND, mmode,
8968 : : sreg, smode),
8969 : : mmode),
8970 : : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
8971 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
8972 : : simplify_gen_unary (SIGN_EXTEND, mmode,
8973 : : sreg, smode),
8974 : : mmode),
8975 : : simplify_gen_unary (SIGN_EXTEND, bmode, sreg, smode));
8976 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
8977 : : simplify_gen_unary (ZERO_EXTEND, mmode,
8978 : : sreg, smode),
8979 : : mmode),
8980 : : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
8981 : :
8982 : : /* Check truncation of extension. */
8983 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8984 : : simplify_gen_unary (ZERO_EXTEND, bmode,
8985 : : mreg, mmode),
8986 : : bmode),
8987 : : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8988 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8989 : : simplify_gen_unary (SIGN_EXTEND, bmode,
8990 : : mreg, mmode),
8991 : : bmode),
8992 : : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8993 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8994 : : lowpart_subreg (bmode, mreg, mmode),
8995 : : bmode),
8996 : : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8997 : 16 : }
8998 : :
8999 : : /* Test comparisons of comparisons, with the inner comparisons being
9000 : : between values of mode MODE2 and producing results of mode MODE1,
9001 : : and with the outer comparisons producing results of mode MODE0. */
9002 : :
9003 : : static void
9004 : 4 : test_comparisons (machine_mode mode0, machine_mode mode1, machine_mode mode2)
9005 : : {
9006 : 4 : rtx reg0 = make_test_reg (mode2);
9007 : 4 : rtx reg1 = make_test_reg (mode2);
9008 : :
9009 : 4 : static const rtx_code codes[] = {
9010 : : EQ, NE, LT, LTU, LE, LEU, GE, GEU, GT, GTU
9011 : : };
9012 : 4 : constexpr auto num_codes = ARRAY_SIZE (codes);
9013 : 4 : rtx cmps[num_codes];
9014 : 4 : rtx vals[] = { constm1_rtx, const0_rtx, const1_rtx };
9015 : :
9016 : 44 : for (unsigned int i = 0; i < num_codes; ++i)
9017 : 40 : cmps[i] = gen_rtx_fmt_ee (codes[i], mode1, reg0, reg1);
9018 : :
9019 : 44 : for (auto code : codes)
9020 : 440 : for (unsigned int i0 = 0; i0 < num_codes; ++i0)
9021 : 4400 : for (unsigned int i1 = 0; i1 < num_codes; ++i1)
9022 : : {
9023 : 4000 : rtx cmp_res = simplify_relational_operation (code, mode0, mode1,
9024 : : cmps[i0], cmps[i1]);
9025 : 4000 : if (i0 >= 2 && i1 >= 2 && (i0 ^ i1) & 1)
9026 : 1280 : ASSERT_TRUE (cmp_res == NULL_RTX);
9027 : : else
9028 : : {
9029 : 2720 : ASSERT_TRUE (cmp_res != NULL_RTX
9030 : : && (CONSTANT_P (cmp_res)
9031 : : || (COMPARISON_P (cmp_res)
9032 : : && GET_MODE (cmp_res) == mode0
9033 : : && REG_P (XEXP (cmp_res, 0))
9034 : : && REG_P (XEXP (cmp_res, 1)))));
9035 : 10880 : for (rtx reg0_val : vals)
9036 : 32640 : for (rtx reg1_val : vals)
9037 : : {
9038 : 24480 : rtx val0 = simplify_const_relational_operation
9039 : 24480 : (codes[i0], mode1, reg0_val, reg1_val);
9040 : 24480 : rtx val1 = simplify_const_relational_operation
9041 : 24480 : (codes[i1], mode1, reg0_val, reg1_val);
9042 : 24480 : rtx val = simplify_const_relational_operation
9043 : 24480 : (code, mode0, val0, val1);
9044 : 24480 : rtx folded = cmp_res;
9045 : 24480 : if (COMPARISON_P (cmp_res))
9046 : 16704 : folded = simplify_const_relational_operation
9047 : 16704 : (GET_CODE (cmp_res), mode0,
9048 : 16704 : XEXP (cmp_res, 0) == reg0 ? reg0_val : reg1_val,
9049 : 16704 : XEXP (cmp_res, 1) == reg0 ? reg0_val : reg1_val);
9050 : 24480 : ASSERT_RTX_EQ (val, folded);
9051 : : }
9052 : : }
9053 : : }
9054 : 4 : }
9055 : :
9056 : :
9057 : : /* Verify some simplifications involving scalar expressions. */
9058 : :
9059 : : static void
9060 : 4 : test_scalar_ops ()
9061 : : {
9062 : 500 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
9063 : : {
9064 : 496 : machine_mode mode = (machine_mode) i;
9065 : 496 : if (SCALAR_INT_MODE_P (mode) && mode != BImode)
9066 : 40 : test_scalar_int_ops (mode);
9067 : : }
9068 : :
9069 : 4 : test_scalar_int_ext_ops (HImode, QImode);
9070 : 4 : test_scalar_int_ext_ops (SImode, QImode);
9071 : 4 : test_scalar_int_ext_ops (SImode, HImode);
9072 : 4 : test_scalar_int_ext_ops (DImode, QImode);
9073 : 4 : test_scalar_int_ext_ops (DImode, HImode);
9074 : 4 : test_scalar_int_ext_ops (DImode, SImode);
9075 : :
9076 : 4 : test_scalar_int_ext_ops2 (SImode, HImode, QImode);
9077 : 4 : test_scalar_int_ext_ops2 (DImode, HImode, QImode);
9078 : 4 : test_scalar_int_ext_ops2 (DImode, SImode, QImode);
9079 : 4 : test_scalar_int_ext_ops2 (DImode, SImode, HImode);
9080 : :
9081 : 4 : test_comparisons (QImode, HImode, SImode);
9082 : 4 : }
9083 : :
9084 : : /* Test vector simplifications involving VEC_DUPLICATE in which the
9085 : : operands and result have vector mode MODE. SCALAR_REG is a pseudo
9086 : : register that holds one element of MODE. */
9087 : :
9088 : : static void
9089 : 224 : test_vector_ops_duplicate (machine_mode mode, rtx scalar_reg)
9090 : : {
9091 : 224 : scalar_mode inner_mode = GET_MODE_INNER (mode);
9092 : 224 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
9093 : 448 : poly_uint64 nunits = GET_MODE_NUNITS (mode);
9094 : 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9095 : : {
9096 : : /* Test some simple unary cases with VEC_DUPLICATE arguments. */
9097 : 124 : rtx not_scalar_reg = gen_rtx_NOT (inner_mode, scalar_reg);
9098 : 124 : rtx duplicate_not = gen_rtx_VEC_DUPLICATE (mode, not_scalar_reg);
9099 : 124 : ASSERT_RTX_EQ (duplicate,
9100 : : simplify_unary_operation (NOT, mode,
9101 : : duplicate_not, mode));
9102 : :
9103 : 124 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
9104 : 124 : rtx duplicate_neg = gen_rtx_VEC_DUPLICATE (mode, neg_scalar_reg);
9105 : 124 : ASSERT_RTX_EQ (duplicate,
9106 : : simplify_unary_operation (NEG, mode,
9107 : : duplicate_neg, mode));
9108 : :
9109 : : /* Test some simple binary cases with VEC_DUPLICATE arguments. */
9110 : 124 : ASSERT_RTX_EQ (duplicate,
9111 : : simplify_binary_operation (PLUS, mode, duplicate,
9112 : : CONST0_RTX (mode)));
9113 : :
9114 : 124 : ASSERT_RTX_EQ (duplicate,
9115 : : simplify_binary_operation (MINUS, mode, duplicate,
9116 : : CONST0_RTX (mode)));
9117 : :
9118 : 124 : ASSERT_RTX_PTR_EQ (CONST0_RTX (mode),
9119 : : simplify_binary_operation (MINUS, mode, duplicate,
9120 : : duplicate));
9121 : : }
9122 : :
9123 : : /* Test a scalar VEC_SELECT of a VEC_DUPLICATE. */
9124 : 224 : rtx zero_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
9125 : 224 : ASSERT_RTX_PTR_EQ (scalar_reg,
9126 : : simplify_binary_operation (VEC_SELECT, inner_mode,
9127 : : duplicate, zero_par));
9128 : :
9129 : 224 : unsigned HOST_WIDE_INT const_nunits;
9130 : 224 : if (nunits.is_constant (&const_nunits))
9131 : : {
9132 : : /* And again with the final element. */
9133 : 224 : rtx last_index = gen_int_mode (const_nunits - 1, word_mode);
9134 : 224 : rtx last_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, last_index));
9135 : 224 : ASSERT_RTX_PTR_EQ (scalar_reg,
9136 : : simplify_binary_operation (VEC_SELECT, inner_mode,
9137 : : duplicate, last_par));
9138 : :
9139 : : /* Test a scalar subreg of a VEC_MERGE of a VEC_DUPLICATE. */
9140 : : /* Skip this test for vectors of booleans, because offset is in bytes,
9141 : : while vec_merge indices are in elements (usually bits). */
9142 : 224 : if (GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL)
9143 : : {
9144 : 224 : rtx vector_reg = make_test_reg (mode);
9145 : 3508 : for (unsigned HOST_WIDE_INT i = 0; i < const_nunits; i++)
9146 : : {
9147 : 3288 : if (i >= HOST_BITS_PER_WIDE_INT)
9148 : : break;
9149 : 3284 : rtx mask = GEN_INT ((HOST_WIDE_INT_1U << i) | (i + 1));
9150 : 3284 : rtx vm = gen_rtx_VEC_MERGE (mode, duplicate, vector_reg, mask);
9151 : 6568 : poly_uint64 offset = i * GET_MODE_SIZE (inner_mode);
9152 : :
9153 : 3284 : ASSERT_RTX_EQ (scalar_reg,
9154 : : simplify_gen_subreg (inner_mode, vm,
9155 : : mode, offset));
9156 : : }
9157 : : }
9158 : : }
9159 : :
9160 : : /* Test a scalar subreg of a VEC_DUPLICATE. */
9161 : 224 : poly_uint64 offset = subreg_lowpart_offset (inner_mode, mode);
9162 : 224 : ASSERT_RTX_EQ (scalar_reg,
9163 : : simplify_gen_subreg (inner_mode, duplicate,
9164 : : mode, offset));
9165 : :
9166 : 224 : machine_mode narrower_mode;
9167 : 224 : if (maybe_ne (nunits, 2U)
9168 : 184 : && multiple_p (nunits, 2)
9169 : 396 : && mode_for_vector (inner_mode, 2).exists (&narrower_mode)
9170 : 396 : && VECTOR_MODE_P (narrower_mode))
9171 : : {
9172 : : /* Test VEC_DUPLICATE of a vector. */
9173 : 172 : rtx_vector_builder nbuilder (narrower_mode, 2, 1);
9174 : 172 : nbuilder.quick_push (const0_rtx);
9175 : 172 : nbuilder.quick_push (const1_rtx);
9176 : 172 : rtx_vector_builder builder (mode, 2, 1);
9177 : 172 : builder.quick_push (const0_rtx);
9178 : 172 : builder.quick_push (const1_rtx);
9179 : 172 : ASSERT_RTX_EQ (builder.build (),
9180 : : simplify_unary_operation (VEC_DUPLICATE, mode,
9181 : : nbuilder.build (),
9182 : : narrower_mode));
9183 : :
9184 : : /* Test VEC_SELECT of a vector. */
9185 : 172 : rtx vec_par
9186 : 172 : = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, const1_rtx, const0_rtx));
9187 : 172 : rtx narrower_duplicate
9188 : 172 : = gen_rtx_VEC_DUPLICATE (narrower_mode, scalar_reg);
9189 : 172 : ASSERT_RTX_EQ (narrower_duplicate,
9190 : : simplify_binary_operation (VEC_SELECT, narrower_mode,
9191 : : duplicate, vec_par));
9192 : :
9193 : : /* Test a vector subreg of a VEC_DUPLICATE. */
9194 : 172 : poly_uint64 offset = subreg_lowpart_offset (narrower_mode, mode);
9195 : 172 : ASSERT_RTX_EQ (narrower_duplicate,
9196 : : simplify_gen_subreg (narrower_mode, duplicate,
9197 : : mode, offset));
9198 : 172 : }
9199 : 224 : }
9200 : :
9201 : : /* Test vector simplifications involving VEC_SERIES in which the
9202 : : operands and result have vector mode MODE. SCALAR_REG is a pseudo
9203 : : register that holds one element of MODE. */
9204 : :
9205 : : static void
9206 : 92 : test_vector_ops_series (machine_mode mode, rtx scalar_reg)
9207 : : {
9208 : : /* Test unary cases with VEC_SERIES arguments. */
9209 : 92 : scalar_mode inner_mode = GET_MODE_INNER (mode);
9210 : 92 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
9211 : 92 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
9212 : 92 : rtx series_0_r = gen_rtx_VEC_SERIES (mode, const0_rtx, scalar_reg);
9213 : 92 : rtx series_0_nr = gen_rtx_VEC_SERIES (mode, const0_rtx, neg_scalar_reg);
9214 : 92 : rtx series_nr_1 = gen_rtx_VEC_SERIES (mode, neg_scalar_reg, const1_rtx);
9215 : 92 : rtx series_r_m1 = gen_rtx_VEC_SERIES (mode, scalar_reg, constm1_rtx);
9216 : 92 : rtx series_r_r = gen_rtx_VEC_SERIES (mode, scalar_reg, scalar_reg);
9217 : 92 : rtx series_nr_nr = gen_rtx_VEC_SERIES (mode, neg_scalar_reg,
9218 : : neg_scalar_reg);
9219 : 92 : ASSERT_RTX_EQ (series_0_r,
9220 : : simplify_unary_operation (NEG, mode, series_0_nr, mode));
9221 : 92 : ASSERT_RTX_EQ (series_r_m1,
9222 : : simplify_unary_operation (NEG, mode, series_nr_1, mode));
9223 : 92 : ASSERT_RTX_EQ (series_r_r,
9224 : : simplify_unary_operation (NEG, mode, series_nr_nr, mode));
9225 : :
9226 : : /* Test that a VEC_SERIES with a zero step is simplified away. */
9227 : 92 : ASSERT_RTX_EQ (duplicate,
9228 : : simplify_binary_operation (VEC_SERIES, mode,
9229 : : scalar_reg, const0_rtx));
9230 : :
9231 : : /* Test PLUS and MINUS with VEC_SERIES. */
9232 : 92 : rtx series_0_1 = gen_const_vec_series (mode, const0_rtx, const1_rtx);
9233 : 92 : rtx series_0_m1 = gen_const_vec_series (mode, const0_rtx, constm1_rtx);
9234 : 92 : rtx series_r_1 = gen_rtx_VEC_SERIES (mode, scalar_reg, const1_rtx);
9235 : 92 : ASSERT_RTX_EQ (series_r_r,
9236 : : simplify_binary_operation (PLUS, mode, series_0_r,
9237 : : duplicate));
9238 : 92 : ASSERT_RTX_EQ (series_r_1,
9239 : : simplify_binary_operation (PLUS, mode, duplicate,
9240 : : series_0_1));
9241 : 92 : ASSERT_RTX_EQ (series_r_m1,
9242 : : simplify_binary_operation (PLUS, mode, duplicate,
9243 : : series_0_m1));
9244 : 92 : ASSERT_RTX_EQ (series_0_r,
9245 : : simplify_binary_operation (MINUS, mode, series_r_r,
9246 : : duplicate));
9247 : 92 : ASSERT_RTX_EQ (series_r_m1,
9248 : : simplify_binary_operation (MINUS, mode, duplicate,
9249 : : series_0_1));
9250 : 92 : ASSERT_RTX_EQ (series_r_1,
9251 : : simplify_binary_operation (MINUS, mode, duplicate,
9252 : : series_0_m1));
9253 : 92 : ASSERT_RTX_EQ (series_0_m1,
9254 : : simplify_binary_operation (VEC_SERIES, mode, const0_rtx,
9255 : : constm1_rtx));
9256 : :
9257 : : /* Test NEG on constant vector series. */
9258 : 92 : ASSERT_RTX_EQ (series_0_m1,
9259 : : simplify_unary_operation (NEG, mode, series_0_1, mode));
9260 : 92 : ASSERT_RTX_EQ (series_0_1,
9261 : : simplify_unary_operation (NEG, mode, series_0_m1, mode));
9262 : :
9263 : : /* Test PLUS and MINUS on constant vector series. */
9264 : 92 : rtx scalar2 = gen_int_mode (2, inner_mode);
9265 : 92 : rtx scalar3 = gen_int_mode (3, inner_mode);
9266 : 92 : rtx series_1_1 = gen_const_vec_series (mode, const1_rtx, const1_rtx);
9267 : 92 : rtx series_0_2 = gen_const_vec_series (mode, const0_rtx, scalar2);
9268 : 92 : rtx series_1_3 = gen_const_vec_series (mode, const1_rtx, scalar3);
9269 : 92 : ASSERT_RTX_EQ (series_1_1,
9270 : : simplify_binary_operation (PLUS, mode, series_0_1,
9271 : : CONST1_RTX (mode)));
9272 : 92 : ASSERT_RTX_EQ (series_0_m1,
9273 : : simplify_binary_operation (PLUS, mode, CONST0_RTX (mode),
9274 : : series_0_m1));
9275 : 92 : ASSERT_RTX_EQ (series_1_3,
9276 : : simplify_binary_operation (PLUS, mode, series_1_1,
9277 : : series_0_2));
9278 : 92 : ASSERT_RTX_EQ (series_0_1,
9279 : : simplify_binary_operation (MINUS, mode, series_1_1,
9280 : : CONST1_RTX (mode)));
9281 : 92 : ASSERT_RTX_EQ (series_1_1,
9282 : : simplify_binary_operation (MINUS, mode, CONST1_RTX (mode),
9283 : : series_0_m1));
9284 : 92 : ASSERT_RTX_EQ (series_1_1,
9285 : : simplify_binary_operation (MINUS, mode, series_1_3,
9286 : : series_0_2));
9287 : :
9288 : : /* Test MULT between constant vectors. */
9289 : 92 : rtx vec2 = gen_const_vec_duplicate (mode, scalar2);
9290 : 92 : rtx vec3 = gen_const_vec_duplicate (mode, scalar3);
9291 : 92 : rtx scalar9 = gen_int_mode (9, inner_mode);
9292 : 92 : rtx series_3_9 = gen_const_vec_series (mode, scalar3, scalar9);
9293 : 92 : ASSERT_RTX_EQ (series_0_2,
9294 : : simplify_binary_operation (MULT, mode, series_0_1, vec2));
9295 : 92 : ASSERT_RTX_EQ (series_3_9,
9296 : : simplify_binary_operation (MULT, mode, vec3, series_1_3));
9297 : 92 : if (!GET_MODE_NUNITS (mode).is_constant ())
9298 : : ASSERT_FALSE (simplify_binary_operation (MULT, mode, series_0_1,
9299 : : series_0_1));
9300 : :
9301 : : /* Test ASHIFT between constant vectors. */
9302 : 92 : ASSERT_RTX_EQ (series_0_2,
9303 : : simplify_binary_operation (ASHIFT, mode, series_0_1,
9304 : : CONST1_RTX (mode)));
9305 : 92 : if (!GET_MODE_NUNITS (mode).is_constant ())
9306 : : ASSERT_FALSE (simplify_binary_operation (ASHIFT, mode, CONST1_RTX (mode),
9307 : : series_0_1));
9308 : 92 : }
9309 : :
9310 : : static rtx
9311 : 3136 : simplify_merge_mask (rtx x, rtx mask, int op)
9312 : : {
9313 : 0 : return simplify_context ().simplify_merge_mask (x, mask, op);
9314 : : }
9315 : :
9316 : : /* Verify simplify_merge_mask works correctly. */
9317 : :
9318 : : static void
9319 : 224 : test_vec_merge (machine_mode mode)
9320 : : {
9321 : 224 : rtx op0 = make_test_reg (mode);
9322 : 224 : rtx op1 = make_test_reg (mode);
9323 : 224 : rtx op2 = make_test_reg (mode);
9324 : 224 : rtx op3 = make_test_reg (mode);
9325 : 224 : rtx op4 = make_test_reg (mode);
9326 : 224 : rtx op5 = make_test_reg (mode);
9327 : 224 : rtx mask1 = make_test_reg (SImode);
9328 : 224 : rtx mask2 = make_test_reg (SImode);
9329 : 224 : rtx vm1 = gen_rtx_VEC_MERGE (mode, op0, op1, mask1);
9330 : 224 : rtx vm2 = gen_rtx_VEC_MERGE (mode, op2, op3, mask1);
9331 : 224 : rtx vm3 = gen_rtx_VEC_MERGE (mode, op4, op5, mask1);
9332 : :
9333 : : /* Simple vec_merge. */
9334 : 224 : ASSERT_EQ (op0, simplify_merge_mask (vm1, mask1, 0));
9335 : 224 : ASSERT_EQ (op1, simplify_merge_mask (vm1, mask1, 1));
9336 : 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 0));
9337 : 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 1));
9338 : :
9339 : : /* Nested vec_merge.
9340 : : It's tempting to make this simplify right down to opN, but we don't
9341 : : because all the simplify_* functions assume that the operands have
9342 : : already been simplified. */
9343 : 224 : rtx nvm = gen_rtx_VEC_MERGE (mode, vm1, vm2, mask1);
9344 : 224 : ASSERT_EQ (vm1, simplify_merge_mask (nvm, mask1, 0));
9345 : 224 : ASSERT_EQ (vm2, simplify_merge_mask (nvm, mask1, 1));
9346 : :
9347 : : /* Intermediate unary op. */
9348 : 224 : rtx unop = gen_rtx_NOT (mode, vm1);
9349 : 224 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op0),
9350 : : simplify_merge_mask (unop, mask1, 0));
9351 : 224 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op1),
9352 : : simplify_merge_mask (unop, mask1, 1));
9353 : :
9354 : : /* Intermediate binary op. */
9355 : 224 : rtx binop = gen_rtx_PLUS (mode, vm1, vm2);
9356 : 224 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op0, op2),
9357 : : simplify_merge_mask (binop, mask1, 0));
9358 : 224 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op1, op3),
9359 : : simplify_merge_mask (binop, mask1, 1));
9360 : :
9361 : : /* Intermediate ternary op. */
9362 : 224 : rtx tenop = gen_rtx_FMA (mode, vm1, vm2, vm3);
9363 : 224 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op0, op2, op4),
9364 : : simplify_merge_mask (tenop, mask1, 0));
9365 : 224 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op1, op3, op5),
9366 : : simplify_merge_mask (tenop, mask1, 1));
9367 : :
9368 : : /* Side effects. */
9369 : 224 : rtx badop0 = gen_rtx_PRE_INC (mode, op0);
9370 : 224 : rtx badvm = gen_rtx_VEC_MERGE (mode, badop0, op1, mask1);
9371 : 224 : ASSERT_EQ (badop0, simplify_merge_mask (badvm, mask1, 0));
9372 : 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (badvm, mask1, 1));
9373 : :
9374 : : /* Called indirectly. */
9375 : 224 : ASSERT_RTX_EQ (gen_rtx_VEC_MERGE (mode, op0, op3, mask1),
9376 : : simplify_rtx (nvm));
9377 : 224 : }
9378 : :
9379 : : /* Test that vector rotate formation works at RTL level. Try various
9380 : : combinations of (REG << C) [|,^,+] (REG >> (<bitwidth> - C)). */
9381 : :
9382 : : static void
9383 : 92 : test_vector_rotate (rtx reg)
9384 : : {
9385 : 92 : machine_mode mode = GET_MODE (reg);
9386 : 92 : unsigned bitwidth = GET_MODE_UNIT_SIZE (mode) * BITS_PER_UNIT;
9387 : 92 : rtx plus_rtx = gen_rtx_PLUS (mode, reg, reg);
9388 : 92 : rtx lshftrt_amnt = GEN_INT (bitwidth - 1);
9389 : 92 : lshftrt_amnt = gen_const_vec_duplicate (mode, lshftrt_amnt);
9390 : 92 : rtx lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
9391 : 92 : rtx rotate_rtx = gen_rtx_ROTATE (mode, reg, CONST1_RTX (mode));
9392 : : /* Test explicitly the case where ASHIFT (x, 1) is a PLUS (x, x). */
9393 : 92 : ASSERT_RTX_EQ (rotate_rtx,
9394 : : simplify_rtx (gen_rtx_IOR (mode, plus_rtx, lshiftrt_rtx)));
9395 : 92 : ASSERT_RTX_EQ (rotate_rtx,
9396 : : simplify_rtx (gen_rtx_XOR (mode, plus_rtx, lshiftrt_rtx)));
9397 : 92 : ASSERT_RTX_EQ (rotate_rtx,
9398 : : simplify_rtx (gen_rtx_PLUS (mode, plus_rtx, lshiftrt_rtx)));
9399 : :
9400 : : /* Don't go through every possible rotate amount to save execution time.
9401 : : Multiple of BITS_PER_UNIT amounts could conceivably be simplified to
9402 : : other bswap operations sometimes. Go through just the odd amounts. */
9403 : 1380 : for (unsigned i = 3; i < bitwidth - 2; i += 2)
9404 : : {
9405 : 1288 : rtx rot_amnt = gen_const_vec_duplicate (mode, GEN_INT (i));
9406 : 1288 : rtx ashift_rtx = gen_rtx_ASHIFT (mode, reg, rot_amnt);
9407 : 1288 : lshftrt_amnt = gen_const_vec_duplicate (mode, GEN_INT (bitwidth - i));
9408 : 1288 : lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
9409 : 1288 : rotate_rtx = gen_rtx_ROTATE (mode, reg, rot_amnt);
9410 : 1288 : ASSERT_RTX_EQ (rotate_rtx,
9411 : : simplify_rtx (gen_rtx_IOR (mode, ashift_rtx, lshiftrt_rtx)));
9412 : 1288 : ASSERT_RTX_EQ (rotate_rtx,
9413 : : simplify_rtx (gen_rtx_XOR (mode, ashift_rtx, lshiftrt_rtx)));
9414 : 1288 : ASSERT_RTX_EQ (rotate_rtx,
9415 : : simplify_rtx (gen_rtx_PLUS (mode, ashift_rtx, lshiftrt_rtx)));
9416 : : }
9417 : 92 : }
9418 : :
9419 : : /* Test subregs of integer vector constant X, trying elements in
9420 : : the range [ELT_BIAS, ELT_BIAS + constant_lower_bound (NELTS)),
9421 : : where NELTS is the number of elements in X. Subregs involving
9422 : : elements [ELT_BIAS, ELT_BIAS + FIRST_VALID) are expected to fail. */
9423 : :
9424 : : static void
9425 : 276 : test_vector_subregs_modes (rtx x, poly_uint64 elt_bias = 0,
9426 : : unsigned int first_valid = 0)
9427 : : {
9428 : 276 : machine_mode inner_mode = GET_MODE (x);
9429 : 276 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9430 : :
9431 : 34500 : for (unsigned int modei = 0; modei < NUM_MACHINE_MODES; ++modei)
9432 : : {
9433 : 34224 : machine_mode outer_mode = (machine_mode) modei;
9434 : 34224 : if (!VECTOR_MODE_P (outer_mode))
9435 : 18768 : continue;
9436 : :
9437 : 15456 : unsigned int outer_nunits;
9438 : 15456 : if (GET_MODE_INNER (outer_mode) == int_mode
9439 : 1932 : && GET_MODE_NUNITS (outer_mode).is_constant (&outer_nunits)
9440 : 20412 : && multiple_p (GET_MODE_NUNITS (inner_mode), outer_nunits))
9441 : : {
9442 : : /* Test subregs in which the outer mode is a smaller,
9443 : : constant-sized vector of the same element type. */
9444 : 1092 : unsigned int limit
9445 : 1092 : = constant_lower_bound (GET_MODE_NUNITS (inner_mode));
9446 : 8028 : for (unsigned int elt = 0; elt < limit; elt += outer_nunits)
9447 : : {
9448 : 6936 : rtx expected = NULL_RTX;
9449 : 6936 : if (elt >= first_valid)
9450 : : {
9451 : 6936 : rtx_vector_builder builder (outer_mode, outer_nunits, 1);
9452 : 39768 : for (unsigned int i = 0; i < outer_nunits; ++i)
9453 : 32832 : builder.quick_push (CONST_VECTOR_ELT (x, elt + i));
9454 : 6936 : expected = builder.build ();
9455 : 6936 : }
9456 : 13872 : poly_uint64 byte = (elt_bias + elt) * GET_MODE_SIZE (int_mode);
9457 : 6936 : ASSERT_RTX_EQ (expected,
9458 : : simplify_subreg (outer_mode, x,
9459 : : inner_mode, byte));
9460 : : }
9461 : : }
9462 : 28728 : else if (known_eq (GET_MODE_SIZE (outer_mode),
9463 : : GET_MODE_SIZE (inner_mode))
9464 : 2040 : && known_eq (elt_bias, 0U)
9465 : 2040 : && (GET_MODE_CLASS (outer_mode) != MODE_VECTOR_BOOL
9466 : 0 : || known_eq (GET_MODE_BITSIZE (outer_mode),
9467 : : GET_MODE_NUNITS (outer_mode)))
9468 : 2040 : && (!FLOAT_MODE_P (outer_mode)
9469 : 15876 : || (FLOAT_MODE_FORMAT (outer_mode)->ieee_bits
9470 : 1104 : == GET_MODE_UNIT_PRECISION (outer_mode)))
9471 : 14364 : && (GET_MODE_SIZE (inner_mode).is_constant ()
9472 : : || !CONST_VECTOR_STEPPED_P (x)))
9473 : : {
9474 : : /* Try converting to OUTER_MODE and back. */
9475 : 1800 : rtx outer_x = simplify_subreg (outer_mode, x, inner_mode, 0);
9476 : 1800 : ASSERT_TRUE (outer_x != NULL_RTX);
9477 : 1800 : ASSERT_RTX_EQ (x, simplify_subreg (inner_mode, outer_x,
9478 : : outer_mode, 0));
9479 : : }
9480 : : }
9481 : :
9482 : 276 : if (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
9483 : : {
9484 : : /* Test each byte in the element range. */
9485 : 276 : unsigned int limit
9486 : 276 : = constant_lower_bound (GET_MODE_SIZE (inner_mode));
9487 : 14604 : for (unsigned int i = 0; i < limit; ++i)
9488 : : {
9489 : 14328 : unsigned int elt = i / GET_MODE_SIZE (int_mode);
9490 : 14328 : rtx expected = NULL_RTX;
9491 : 14328 : if (elt >= first_valid)
9492 : : {
9493 : 14328 : unsigned int byte_shift = i % GET_MODE_SIZE (int_mode);
9494 : 14328 : if (BYTES_BIG_ENDIAN)
9495 : : byte_shift = GET_MODE_SIZE (int_mode) - byte_shift - 1;
9496 : 14328 : rtx_mode_t vec_elt (CONST_VECTOR_ELT (x, elt), int_mode);
9497 : 14328 : wide_int shifted_elt
9498 : 14328 : = wi::lrshift (vec_elt, byte_shift * BITS_PER_UNIT);
9499 : 14328 : expected = immed_wide_int_const (shifted_elt, QImode);
9500 : 14328 : }
9501 : 28656 : poly_uint64 byte = elt_bias * GET_MODE_SIZE (int_mode) + i;
9502 : 14328 : ASSERT_RTX_EQ (expected,
9503 : : simplify_subreg (QImode, x, inner_mode, byte));
9504 : : }
9505 : : }
9506 : 276 : }
9507 : :
9508 : : /* Test constant subregs of integer vector mode INNER_MODE, using 1
9509 : : element per pattern. */
9510 : :
9511 : : static void
9512 : 92 : test_vector_subregs_repeating (machine_mode inner_mode)
9513 : : {
9514 : 184 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
9515 : 92 : unsigned int min_nunits = constant_lower_bound (nunits);
9516 : 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9517 : 92 : unsigned int count = gcd (min_nunits, 8);
9518 : :
9519 : 92 : rtx_vector_builder builder (inner_mode, count, 1);
9520 : 684 : for (unsigned int i = 0; i < count; ++i)
9521 : 592 : builder.quick_push (gen_int_mode (8 - i, int_mode));
9522 : 92 : rtx x = builder.build ();
9523 : :
9524 : 92 : test_vector_subregs_modes (x);
9525 : 92 : if (!nunits.is_constant ())
9526 : : test_vector_subregs_modes (x, nunits - min_nunits);
9527 : 92 : }
9528 : :
9529 : : /* Test constant subregs of integer vector mode INNER_MODE, using 2
9530 : : elements per pattern. */
9531 : :
9532 : : static void
9533 : 92 : test_vector_subregs_fore_back (machine_mode inner_mode)
9534 : : {
9535 : 184 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
9536 : 92 : unsigned int min_nunits = constant_lower_bound (nunits);
9537 : 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9538 : 92 : unsigned int count = gcd (min_nunits, 4);
9539 : :
9540 : 92 : rtx_vector_builder builder (inner_mode, count, 2);
9541 : 444 : for (unsigned int i = 0; i < count; ++i)
9542 : 352 : builder.quick_push (gen_int_mode (i, int_mode));
9543 : 444 : for (unsigned int i = 0; i < count; ++i)
9544 : 352 : builder.quick_push (gen_int_mode (-1 - (int) i, int_mode));
9545 : 92 : rtx x = builder.build ();
9546 : :
9547 : 92 : test_vector_subregs_modes (x);
9548 : 92 : if (!nunits.is_constant ())
9549 : : test_vector_subregs_modes (x, nunits - min_nunits, count);
9550 : 92 : }
9551 : :
9552 : : /* Test constant subregs of integer vector mode INNER_MODE, using 3
9553 : : elements per pattern. */
9554 : :
9555 : : static void
9556 : 92 : test_vector_subregs_stepped (machine_mode inner_mode)
9557 : : {
9558 : : /* Build { 0, 1, 2, 3, ... }. */
9559 : 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9560 : 92 : rtx_vector_builder builder (inner_mode, 1, 3);
9561 : 368 : for (unsigned int i = 0; i < 3; ++i)
9562 : 276 : builder.quick_push (gen_int_mode (i, int_mode));
9563 : 92 : rtx x = builder.build ();
9564 : :
9565 : 92 : test_vector_subregs_modes (x);
9566 : 92 : }
9567 : :
9568 : : /* Test constant subregs of integer vector mode INNER_MODE. */
9569 : :
9570 : : static void
9571 : 92 : test_vector_subregs (machine_mode inner_mode)
9572 : : {
9573 : 92 : test_vector_subregs_repeating (inner_mode);
9574 : 92 : test_vector_subregs_fore_back (inner_mode);
9575 : 92 : test_vector_subregs_stepped (inner_mode);
9576 : 92 : }
9577 : :
9578 : : /* Verify some simplifications involving vectors. */
9579 : :
9580 : : static void
9581 : 4 : test_vector_ops ()
9582 : : {
9583 : 500 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
9584 : : {
9585 : 496 : machine_mode mode = (machine_mode) i;
9586 : 496 : if (VECTOR_MODE_P (mode))
9587 : : {
9588 : 448 : rtx scalar_reg = make_test_reg (GET_MODE_INNER (mode));
9589 : 224 : test_vector_ops_duplicate (mode, scalar_reg);
9590 : 224 : rtx vector_reg = make_test_reg (mode);
9591 : 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9592 : 348 : && maybe_gt (GET_MODE_NUNITS (mode), 2))
9593 : : {
9594 : 92 : test_vector_ops_series (mode, scalar_reg);
9595 : 92 : test_vector_subregs (mode);
9596 : 92 : test_vector_rotate (vector_reg);
9597 : : }
9598 : 224 : test_vec_merge (mode);
9599 : : }
9600 : : }
9601 : 4 : }
9602 : :
9603 : : template<unsigned int N>
9604 : : struct simplify_const_poly_int_tests
9605 : : {
9606 : : static void run ();
9607 : : };
9608 : :
9609 : : template<>
9610 : : struct simplify_const_poly_int_tests<1>
9611 : : {
9612 : : static void run () {}
9613 : : };
9614 : :
9615 : : /* Test various CONST_POLY_INT properties. */
9616 : :
9617 : : template<unsigned int N>
9618 : : void
9619 : : simplify_const_poly_int_tests<N>::run ()
9620 : : {
9621 : : using poly_int64 = poly_int<N, HOST_WIDE_INT>;
9622 : : rtx x1 = gen_int_mode (poly_int64 (1, 1), QImode);
9623 : : rtx x2 = gen_int_mode (poly_int64 (-80, 127), QImode);
9624 : : rtx x3 = gen_int_mode (poly_int64 (-79, -128), QImode);
9625 : : rtx x4 = gen_int_mode (poly_int64 (5, 4), QImode);
9626 : : rtx x5 = gen_int_mode (poly_int64 (30, 24), QImode);
9627 : : rtx x6 = gen_int_mode (poly_int64 (20, 16), QImode);
9628 : : rtx x7 = gen_int_mode (poly_int64 (7, 4), QImode);
9629 : : rtx x8 = gen_int_mode (poly_int64 (30, 24), HImode);
9630 : : rtx x9 = gen_int_mode (poly_int64 (-30, -24), HImode);
9631 : : rtx x10 = gen_int_mode (poly_int64 (-31, -24), HImode);
9632 : : rtx two = GEN_INT (2);
9633 : : rtx six = GEN_INT (6);
9634 : : poly_uint64 offset = subreg_lowpart_offset (QImode, HImode);
9635 : :
9636 : : /* These tests only try limited operation combinations. Fuller arithmetic
9637 : : testing is done directly on poly_ints. */
9638 : : ASSERT_EQ (simplify_unary_operation (NEG, HImode, x8, HImode), x9);
9639 : : ASSERT_EQ (simplify_unary_operation (NOT, HImode, x8, HImode), x10);
9640 : : ASSERT_EQ (simplify_unary_operation (TRUNCATE, QImode, x8, HImode), x5);
9641 : : ASSERT_EQ (simplify_binary_operation (PLUS, QImode, x1, x2), x3);
9642 : : ASSERT_EQ (simplify_binary_operation (MINUS, QImode, x3, x1), x2);
9643 : : ASSERT_EQ (simplify_binary_operation (MULT, QImode, x4, six), x5);
9644 : : ASSERT_EQ (simplify_binary_operation (MULT, QImode, six, x4), x5);
9645 : : ASSERT_EQ (simplify_binary_operation (ASHIFT, QImode, x4, two), x6);
9646 : : ASSERT_EQ (simplify_binary_operation (IOR, QImode, x4, two), x7);
9647 : : ASSERT_EQ (simplify_subreg (HImode, x5, QImode, 0), x8);
9648 : : ASSERT_EQ (simplify_subreg (QImode, x8, HImode, offset), x5);
9649 : : }
9650 : :
9651 : : /* Run all of the selftests within this file. */
9652 : :
9653 : : void
9654 : 4 : simplify_rtx_cc_tests ()
9655 : : {
9656 : 4 : test_scalar_ops ();
9657 : 4 : test_vector_ops ();
9658 : 4 : simplify_const_poly_int_tests<NUM_POLY_INT_COEFFS>::run ();
9659 : 4 : }
9660 : :
9661 : : } // namespace selftest
9662 : :
9663 : : #endif /* CHECKING_P */
|