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 : 8998582 : neg_poly_int_rtx (machine_mode mode, const_rtx i)
56 : : {
57 : 8998582 : 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 : 6336465 : mode_signbit_p (machine_mode mode, const_rtx x)
65 : : {
66 : 6336465 : unsigned HOST_WIDE_INT val;
67 : 6336465 : unsigned int width;
68 : 6336465 : scalar_int_mode int_mode;
69 : :
70 : 6336465 : if (!is_int_mode (mode, &int_mode))
71 : : return false;
72 : :
73 : 6336457 : width = GET_MODE_PRECISION (int_mode);
74 : 6336457 : if (width == 0)
75 : : return false;
76 : :
77 : 6336457 : if (width <= HOST_BITS_PER_WIDE_INT
78 : 6334921 : && CONST_INT_P (x))
79 : 6216231 : val = INTVAL (x);
80 : : #if TARGET_SUPPORTS_WIDE_INT
81 : 120226 : 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 : 6216231 : if (width < HOST_BITS_PER_WIDE_INT)
109 : 5616162 : val &= (HOST_WIDE_INT_1U << width) - 1;
110 : 6216645 : 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 : 3776336 : val_signbit_p (machine_mode mode, unsigned HOST_WIDE_INT val)
119 : : {
120 : 3776336 : unsigned int width;
121 : 3776336 : scalar_int_mode int_mode;
122 : :
123 : 3776336 : if (!is_int_mode (mode, &int_mode))
124 : : return false;
125 : :
126 : 3776300 : width = GET_MODE_PRECISION (int_mode);
127 : 3776300 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
128 : : return false;
129 : :
130 : 3771685 : val &= GET_MODE_MASK (int_mode);
131 : 3771685 : 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 : 2707456 : val_signbit_known_set_p (machine_mode mode, unsigned HOST_WIDE_INT val)
138 : : {
139 : 2707456 : unsigned int width;
140 : :
141 : 2707456 : scalar_int_mode int_mode;
142 : 2707456 : if (!is_int_mode (mode, &int_mode))
143 : : return false;
144 : :
145 : 2674706 : width = GET_MODE_PRECISION (int_mode);
146 : 2674706 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
147 : : return false;
148 : :
149 : 2674706 : val &= HOST_WIDE_INT_1U << (width - 1);
150 : 2674706 : 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 : 7597081 : val_signbit_known_clear_p (machine_mode mode, unsigned HOST_WIDE_INT val)
157 : : {
158 : 7597081 : unsigned int width;
159 : :
160 : 7597081 : scalar_int_mode int_mode;
161 : 7597081 : if (!is_int_mode (mode, &int_mode))
162 : : return false;
163 : :
164 : 7276906 : width = GET_MODE_PRECISION (int_mode);
165 : 7276906 : if (width == 0 || width > HOST_BITS_PER_WIDE_INT)
166 : : return false;
167 : :
168 : 7165316 : val &= HOST_WIDE_INT_1U << (width - 1);
169 : 7165316 : 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 : 115401851 : simplify_context::simplify_gen_binary (rtx_code code, machine_mode mode,
177 : : rtx op0, rtx op1)
178 : : {
179 : 115401851 : rtx tem;
180 : :
181 : : /* If this simplifies, do it. */
182 : 115401851 : tem = simplify_binary_operation (code, mode, op0, op1);
183 : 115401851 : if (tem)
184 : : return tem;
185 : :
186 : : /* Put complex operands first and constants second if commutative. */
187 : 72694009 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
188 : 72694009 : && swap_commutative_operands_p (op0, op1))
189 : : std::swap (op0, op1);
190 : :
191 : 72694009 : 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 : 2736475778 : avoid_constant_pool_reference (rtx x)
198 : : {
199 : 2736475778 : rtx c, tmp, addr;
200 : 2736475778 : machine_mode cmode;
201 : 2736475778 : poly_int64 offset = 0;
202 : :
203 : 2736475778 : switch (GET_CODE (x))
204 : : {
205 : 255016823 : case MEM:
206 : 255016823 : break;
207 : :
208 : 909152 : case FLOAT_EXTEND:
209 : : /* Handle float extensions of constant pool references. */
210 : 909152 : tmp = XEXP (x, 0);
211 : 909152 : c = avoid_constant_pool_reference (tmp);
212 : 909152 : if (c != tmp && CONST_DOUBLE_AS_FLOAT_P (c))
213 : 122650 : return const_double_from_real_value (*CONST_DOUBLE_REAL_VALUE (c),
214 : 122650 : GET_MODE (x));
215 : : return x;
216 : :
217 : : default:
218 : : return x;
219 : : }
220 : :
221 : 255016823 : if (GET_MODE (x) == BLKmode)
222 : : return x;
223 : :
224 : 252855250 : addr = XEXP (x, 0);
225 : :
226 : : /* Call target hook to avoid the effects of -fpic etc.... */
227 : 252855250 : addr = targetm.delegitimize_address (addr);
228 : :
229 : : /* Split the address into a base and integer offset. */
230 : 252855250 : addr = strip_offset (addr, &offset);
231 : :
232 : 252855250 : 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 : 252855250 : if (GET_CODE (addr) == SYMBOL_REF
238 : 252855250 : && CONSTANT_POOL_ADDRESS_P (addr))
239 : : {
240 : 5418607 : c = get_pool_constant (addr);
241 : 5418607 : 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 : 5418607 : if (known_eq (offset, 0) && cmode == GET_MODE (x))
247 : : return c;
248 : 19288 : else if (known_in_range_p (offset, 0, GET_MODE_SIZE (cmode)))
249 : : {
250 : 9644 : rtx tem = simplify_subreg (GET_MODE (x), c, cmode, offset);
251 : 9644 : 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 : 3512757555 : 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 : 3512757555 : if (MEM_P (x)
269 : 61821602 : && MEM_EXPR (x)
270 : 3550653883 : && MEM_OFFSET_KNOWN_P (x))
271 : : {
272 : 34995912 : tree decl = MEM_EXPR (x);
273 : 34995912 : machine_mode mode = GET_MODE (x);
274 : 34995912 : poly_int64 offset = 0;
275 : :
276 : 34995912 : switch (TREE_CODE (decl))
277 : : {
278 : : default:
279 : : decl = NULL;
280 : : break;
281 : :
282 : : case VAR_DECL:
283 : : break;
284 : :
285 : 9999015 : case ARRAY_REF:
286 : 9999015 : case ARRAY_RANGE_REF:
287 : 9999015 : case COMPONENT_REF:
288 : 9999015 : case BIT_FIELD_REF:
289 : 9999015 : case REALPART_EXPR:
290 : 9999015 : case IMAGPART_EXPR:
291 : 9999015 : case VIEW_CONVERT_EXPR:
292 : 9999015 : {
293 : 9999015 : poly_int64 bitsize, bitpos, bytepos, toffset_val = 0;
294 : 9999015 : tree toffset;
295 : 9999015 : int unsignedp, reversep, volatilep = 0;
296 : :
297 : 9999015 : decl
298 : 9999015 : = get_inner_reference (decl, &bitsize, &bitpos, &toffset, &mode,
299 : : &unsignedp, &reversep, &volatilep);
300 : 19998030 : if (maybe_ne (bitsize, GET_MODE_BITSIZE (mode))
301 : 10277884 : || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
302 : 19544311 : || (toffset && !poly_int_tree_p (toffset, &toffset_val)))
303 : : decl = NULL;
304 : : else
305 : 9266427 : offset += bytepos + toffset_val;
306 : 9999015 : break;
307 : : }
308 : : }
309 : :
310 : 732588 : if (decl
311 : 20536124 : && mode == GET_MODE (x)
312 : 20253790 : && VAR_P (decl)
313 : 13201716 : && (TREE_STATIC (decl)
314 : 11990680 : || DECL_THREAD_LOCAL_P (decl))
315 : 1245468 : && DECL_RTL_SET_P (decl)
316 : 10511387 : && MEM_P (DECL_RTL (decl)))
317 : : {
318 : 1244960 : rtx newx;
319 : :
320 : 1244960 : offset += MEM_OFFSET (x);
321 : :
322 : 1244960 : newx = DECL_RTL (decl);
323 : :
324 : 1244960 : if (MEM_P (newx))
325 : : {
326 : 1244960 : rtx n = XEXP (newx, 0), o = XEXP (x, 0);
327 : 1244960 : 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 : 1244960 : n = strip_offset (n, &n_offset);
336 : 1244960 : o = strip_offset (o, &o_offset);
337 : 2462851 : if (!(known_eq (o_offset, n_offset + offset)
338 : 1217891 : && rtx_equal_p (o, n)))
339 : 207892 : 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 : 3512757555 : 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 : 5281723 : simplify_context::simplify_gen_unary (rtx_code code, machine_mode mode, rtx op,
355 : : machine_mode op_mode)
356 : : {
357 : 5281723 : rtx tem;
358 : :
359 : : /* If this simplifies, use it. */
360 : 5281723 : if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
361 : : return tem;
362 : :
363 : 2015664 : return gen_rtx_fmt_e (code, mode, op);
364 : : }
365 : :
366 : : /* Likewise for ternary operations. */
367 : :
368 : : rtx
369 : 2174271 : 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 : 2174271 : rtx tem;
374 : :
375 : : /* If this simplifies, use it. */
376 : 2174271 : if ((tem = simplify_ternary_operation (code, mode, op0_mode,
377 : : op0, op1, op2)) != 0)
378 : : return tem;
379 : :
380 : 1927156 : 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 : 21037796 : simplify_context::simplify_gen_relational (rtx_code code, machine_mode mode,
388 : : machine_mode cmp_mode,
389 : : rtx op0, rtx op1)
390 : : {
391 : 21037796 : rtx tem;
392 : :
393 : 21037796 : if ((tem = simplify_relational_operation (code, mode, cmp_mode,
394 : : op0, op1)) != 0)
395 : : return tem;
396 : :
397 : 18814300 : 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 : 480999288 : simplify_replace_fn_rtx (rtx x, const_rtx old_rtx,
407 : : rtx (*fn) (rtx, const_rtx, void *), void *data)
408 : : {
409 : 480999288 : enum rtx_code code = GET_CODE (x);
410 : 480999288 : machine_mode mode = GET_MODE (x);
411 : 480999288 : machine_mode op_mode;
412 : 480999288 : const char *fmt;
413 : 480999288 : rtx op0, op1, op2, newx, op;
414 : 480999288 : rtvec vec, newvec;
415 : 480999288 : int i, j;
416 : :
417 : 480999288 : if (UNLIKELY (fn != NULL))
418 : : {
419 : 419293827 : newx = fn (x, old_rtx, data);
420 : 419293827 : if (newx)
421 : : return newx;
422 : : }
423 : 61705461 : else if (rtx_equal_p (x, old_rtx))
424 : 5198108 : return copy_rtx ((rtx) data);
425 : :
426 : 376808768 : switch (GET_RTX_CLASS (code))
427 : : {
428 : 2126892 : case RTX_UNARY:
429 : 2126892 : op0 = XEXP (x, 0);
430 : 2126892 : op_mode = GET_MODE (op0);
431 : 2126892 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
432 : 2126892 : if (op0 == XEXP (x, 0))
433 : : return x;
434 : 675786 : return simplify_gen_unary (code, mode, op0, op_mode);
435 : :
436 : 75397614 : case RTX_BIN_ARITH:
437 : 75397614 : case RTX_COMM_ARITH:
438 : 75397614 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
439 : 75397614 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
440 : 75397614 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
441 : : return x;
442 : 23231617 : return simplify_gen_binary (code, mode, op0, op1);
443 : :
444 : 9634644 : case RTX_COMPARE:
445 : 9634644 : case RTX_COMM_COMPARE:
446 : 9634644 : op0 = XEXP (x, 0);
447 : 9634644 : op1 = XEXP (x, 1);
448 : 9634644 : op_mode = GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
449 : 9634644 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
450 : 9634644 : op1 = simplify_replace_fn_rtx (op1, old_rtx, fn, data);
451 : 9634644 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
452 : : return x;
453 : 2292457 : return simplify_gen_relational (code, mode, op_mode, op0, op1);
454 : :
455 : 5706416 : case RTX_TERNARY:
456 : 5706416 : case RTX_BITFIELD_OPS:
457 : 5706416 : op0 = XEXP (x, 0);
458 : 5706416 : op_mode = GET_MODE (op0);
459 : 5706416 : op0 = simplify_replace_fn_rtx (op0, old_rtx, fn, data);
460 : 5706416 : op1 = simplify_replace_fn_rtx (XEXP (x, 1), old_rtx, fn, data);
461 : 5706416 : op2 = simplify_replace_fn_rtx (XEXP (x, 2), old_rtx, fn, data);
462 : 5706416 : if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1) && op2 == XEXP (x, 2))
463 : : return x;
464 : 1630600 : if (op_mode == VOIDmode)
465 : 1610811 : op_mode = GET_MODE (op0);
466 : 1630600 : return simplify_gen_ternary (code, mode, op_mode, op0, op1, op2);
467 : :
468 : 80930636 : case RTX_EXTRA:
469 : 80930636 : if (code == SUBREG)
470 : : {
471 : 691992 : op0 = simplify_replace_fn_rtx (SUBREG_REG (x), old_rtx, fn, data);
472 : 691992 : if (op0 == SUBREG_REG (x))
473 : : return x;
474 : 108358 : op0 = simplify_gen_subreg (GET_MODE (x), op0,
475 : 54179 : GET_MODE (SUBREG_REG (x)),
476 : 54179 : SUBREG_BYTE (x));
477 : 54179 : return op0 ? op0 : x;
478 : : }
479 : : break;
480 : :
481 : 60869418 : case RTX_OBJ:
482 : 60869418 : if (code == MEM)
483 : : {
484 : 10917364 : op0 = simplify_replace_fn_rtx (XEXP (x, 0), old_rtx, fn, data);
485 : 10917364 : if (op0 == XEXP (x, 0))
486 : : return x;
487 : 164914 : return replace_equiv_address_nv (x, op0);
488 : : }
489 : 49952054 : 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 : 272333846 : newx = x;
515 : 272333846 : fmt = GET_RTX_FORMAT (code);
516 : 587546785 : for (i = 0; fmt[i]; i++)
517 : 315212939 : switch (fmt[i])
518 : : {
519 : 3034686 : case 'E':
520 : 3034686 : vec = XVEC (x, i);
521 : 3034686 : newvec = XVEC (newx, i);
522 : 12538660 : for (j = 0; j < GET_NUM_ELEM (vec); j++)
523 : : {
524 : 9503974 : op = simplify_replace_fn_rtx (RTVEC_ELT (vec, j),
525 : : old_rtx, fn, data);
526 : 9503974 : if (op != RTVEC_ELT (vec, j))
527 : : {
528 : 339482 : if (newvec == vec)
529 : : {
530 : 330709 : newvec = shallow_copy_rtvec (vec);
531 : 330709 : if (x == newx)
532 : 330709 : newx = shallow_copy_rtx (x);
533 : 330709 : XVEC (newx, i) = newvec;
534 : : }
535 : 339482 : RTVEC_ELT (newvec, j) = op;
536 : : }
537 : : }
538 : : break;
539 : :
540 : 72900901 : case 'e':
541 : 72900901 : if (XEXP (x, i))
542 : : {
543 : 72900901 : op = simplify_replace_fn_rtx (XEXP (x, i), old_rtx, fn, data);
544 : 72900901 : if (op != XEXP (x, i))
545 : : {
546 : 4307758 : if (x == newx)
547 : 4304619 : newx = shallow_copy_rtx (x);
548 : 4307758 : 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 : 12669842 : simplify_replace_rtx (rtx x, const_rtx old_rtx, rtx new_rtx)
561 : : {
562 : 12669842 : 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 : 18255885 : simplify_context::simplify_truncation (machine_mode mode, rtx op,
614 : : machine_mode op_mode)
615 : : {
616 : 18255885 : unsigned int precision = GET_MODE_UNIT_PRECISION (mode);
617 : 18255885 : unsigned int op_precision = GET_MODE_UNIT_PRECISION (op_mode);
618 : 18255885 : scalar_int_mode int_mode, int_op_mode, subreg_mode;
619 : :
620 : 18255885 : gcc_assert (precision <= op_precision);
621 : :
622 : : /* Optimize truncations of zero and sign extended values. */
623 : 18255885 : if (GET_CODE (op) == ZERO_EXTEND
624 : 18255885 : || 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 : 285673 : machine_mode origmode = GET_MODE (XEXP (op, 0));
633 : 285673 : if (mode == origmode)
634 : : return XEXP (op, 0);
635 : 16128 : else if (precision <= GET_MODE_UNIT_PRECISION (origmode))
636 : 4599 : return simplify_gen_unary (TRUNCATE, mode,
637 : 4599 : XEXP (op, 0), origmode);
638 : : else
639 : 3465 : return simplify_gen_unary (GET_CODE (op), mode,
640 : 3465 : 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 : 17970212 : if (1
647 : : && (!WORD_REGISTER_OPERATIONS || precision >= BITS_PER_WORD)
648 : : && (GET_CODE (op) == PLUS
649 : : || GET_CODE (op) == MINUS
650 : 17970212 : || GET_CODE (op) == MULT))
651 : : {
652 : 778339 : rtx op0 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 0), op_mode);
653 : 778339 : if (op0)
654 : : {
655 : 778339 : rtx op1 = simplify_gen_unary (TRUNCATE, mode, XEXP (op, 1), op_mode);
656 : 778339 : if (op1)
657 : 778339 : 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 : 17191873 : if ((GET_CODE (op) == LSHIFTRT
665 : 17191873 : || 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 : 1711463 : && 2 * precision <= op_precision
671 : 1711463 : && CONST_INT_P (XEXP (op, 1))
672 : 1611641 : && GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
673 : 23 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
674 : 20 : && UINTVAL (XEXP (op, 1)) < precision)
675 : 16 : return simplify_gen_binary (ASHIFTRT, mode,
676 : 16 : XEXP (XEXP (op, 0), 0), XEXP (op, 1));
677 : :
678 : : /* Likewise (truncate:QI (lshiftrt:SI (zero_extend:SI (x:QI)) C)) into
679 : : to (lshiftrt:QI (x:QI) C), where C is a suitable small constant and
680 : : the outer subreg is effectively a truncation to the original mode. */
681 : 17191857 : if ((GET_CODE (op) == LSHIFTRT
682 : : || GET_CODE (op) == ASHIFTRT)
683 : 1711447 : && CONST_INT_P (XEXP (op, 1))
684 : 1611625 : && GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
685 : 679 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
686 : 679 : && UINTVAL (XEXP (op, 1)) < precision)
687 : 669 : return simplify_gen_binary (LSHIFTRT, mode,
688 : 669 : 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 : 17191188 : if (GET_CODE (op) == ASHIFT
694 : 469689 : && CONST_INT_P (XEXP (op, 1))
695 : 411027 : && (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
696 : 411027 : || GET_CODE (XEXP (op, 0)) == SIGN_EXTEND)
697 : 597 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == mode
698 : 586 : && UINTVAL (XEXP (op, 1)) < precision)
699 : 578 : return simplify_gen_binary (ASHIFT, mode,
700 : 578 : 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 : 17190610 : if (GET_CODE (op) == AND
706 : 594233 : && (GET_CODE (XEXP (op, 0)) == LSHIFTRT
707 : 594233 : || GET_CODE (XEXP (op, 0)) == ASHIFTRT)
708 : 30623 : && CONST_INT_P (XEXP (XEXP (op, 0), 1))
709 : 30517 : && CONST_INT_P (XEXP (op, 1)))
710 : : {
711 : 30517 : rtx op0 = (XEXP (XEXP (op, 0), 0));
712 : 30517 : rtx shift_op = XEXP (XEXP (op, 0), 1);
713 : 30517 : rtx mask_op = XEXP (op, 1);
714 : 30517 : unsigned HOST_WIDE_INT shift = UINTVAL (shift_op);
715 : 30517 : unsigned HOST_WIDE_INT mask = UINTVAL (mask_op);
716 : :
717 : 30517 : if (shift < precision
718 : : /* If doing this transform works for an X with all bits set,
719 : : it works for any X. */
720 : 17548 : && ((GET_MODE_MASK (mode) >> shift) & mask)
721 : 17548 : == ((GET_MODE_MASK (op_mode) >> shift) & mask)
722 : 2949 : && (op0 = simplify_gen_unary (TRUNCATE, mode, op0, op_mode))
723 : 33466 : && (op0 = simplify_gen_binary (LSHIFTRT, mode, op0, shift_op)))
724 : : {
725 : 2949 : mask_op = GEN_INT (trunc_int_for_mode (mask, mode));
726 : 2949 : 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 : 17187661 : if ((GET_CODE (op) == ZERO_EXTRACT || GET_CODE (op) == SIGN_EXTRACT)
734 : 411128 : && REG_P (XEXP (op, 0))
735 : 274772 : && GET_MODE (XEXP (op, 0)) == GET_MODE (op)
736 : 273965 : && CONST_INT_P (XEXP (op, 1))
737 : 273965 : && CONST_INT_P (XEXP (op, 2)))
738 : : {
739 : 237372 : rtx op0 = XEXP (op, 0);
740 : 237372 : unsigned HOST_WIDE_INT len = UINTVAL (XEXP (op, 1));
741 : 237372 : unsigned HOST_WIDE_INT pos = UINTVAL (XEXP (op, 2));
742 : 237372 : 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 : 237372 : else if (!BITS_BIG_ENDIAN && precision >= len + pos)
753 : : {
754 : 7625 : op0 = simplify_gen_unary (TRUNCATE, mode, op0, GET_MODE (op0));
755 : 7625 : if (op0)
756 : 7625 : return simplify_gen_ternary (GET_CODE (op), mode, mode, op0,
757 : 7625 : XEXP (op, 1), XEXP (op, 2));
758 : : }
759 : : }
760 : :
761 : : /* Recognize a word extraction from a multi-word subreg. */
762 : 17180036 : if ((GET_CODE (op) == LSHIFTRT
763 : 17180036 : || GET_CODE (op) == ASHIFTRT)
764 : 1710778 : && SCALAR_INT_MODE_P (mode)
765 : 1707836 : && SCALAR_INT_MODE_P (op_mode)
766 : 1842199 : && precision >= BITS_PER_WORD
767 : 65191 : && 2 * precision <= op_precision
768 : 65191 : && CONST_INT_P (XEXP (op, 1))
769 : 57108 : && (INTVAL (XEXP (op, 1)) & (precision - 1)) == 0
770 : 1899 : && UINTVAL (XEXP (op, 1)) < op_precision)
771 : : {
772 : 1899 : poly_int64 byte = subreg_lowpart_offset (mode, op_mode);
773 : 1899 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
774 : 1899 : return simplify_gen_subreg (mode, XEXP (op, 0), op_mode,
775 : : (WORDS_BIG_ENDIAN
776 : 1899 : ? byte - shifted_bytes
777 : 1899 : : 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 : 17178137 : if ((GET_CODE (op) == LSHIFTRT
784 : : || GET_CODE (op) == ASHIFTRT)
785 : 1705937 : && is_a <scalar_int_mode> (mode, &int_mode)
786 : 18882859 : && is_a <scalar_int_mode> (op_mode, &int_op_mode)
787 : 1705937 : && MEM_P (XEXP (op, 0))
788 : 11065 : && CONST_INT_P (XEXP (op, 1))
789 : 20594 : && INTVAL (XEXP (op, 1)) % GET_MODE_BITSIZE (int_mode) == 0
790 : 1254 : && INTVAL (XEXP (op, 1)) > 0
791 : 2508 : && INTVAL (XEXP (op, 1)) < GET_MODE_BITSIZE (int_op_mode)
792 : 1254 : && ! mode_dependent_address_p (XEXP (XEXP (op, 0), 0),
793 : 1254 : MEM_ADDR_SPACE (XEXP (op, 0)))
794 : 1254 : && ! MEM_VOLATILE_P (XEXP (op, 0))
795 : 17178137 : && (GET_MODE_SIZE (int_mode) >= UNITS_PER_WORD
796 : : || WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN))
797 : : {
798 : 1215 : poly_int64 byte = subreg_lowpart_offset (int_mode, int_op_mode);
799 : 1215 : int shifted_bytes = INTVAL (XEXP (op, 1)) / BITS_PER_UNIT;
800 : 1215 : 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 : 17176922 : if ((GET_CODE (op) == ABS
809 : 17176922 : || GET_CODE (op) == NEG)
810 : 27077 : && (GET_CODE (XEXP (op, 0)) == SIGN_EXTEND
811 : 27077 : || 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 : 17176920 : if (GET_CODE (op) == SUBREG
818 : 17177018 : && is_a <scalar_int_mode> (mode, &int_mode)
819 : 34655 : && SCALAR_INT_MODE_P (op_mode)
820 : 34655 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &subreg_mode)
821 : 17211513 : && subreg_lowpart_p (op))
822 : : {
823 : : /* (truncate:A (subreg:B (truncate:C X) 0)) is (truncate:A X). */
824 : 34590 : 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 : 34590 : if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
841 : : {
842 : 34590 : unsigned int int_op_prec = GET_MODE_PRECISION (int_op_mode);
843 : 34590 : unsigned int subreg_prec = GET_MODE_PRECISION (subreg_mode);
844 : 34590 : if (int_op_prec > subreg_prec)
845 : : {
846 : 1358 : 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 : 33232 : else if (int_op_prec < subreg_prec
855 : 33232 : && GET_MODE_PRECISION (int_mode) < int_op_prec)
856 : 33232 : return simplify_gen_unary (TRUNCATE, int_mode,
857 : 33232 : SUBREG_REG (op), subreg_mode);
858 : : }
859 : : }
860 : :
861 : : /* (truncate:A (truncate:B X)) is (truncate:A X). */
862 : 17142363 : 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 : 17142363 : if (GET_CODE (op) == IOR
869 : 33913 : && SCALAR_INT_MODE_P (mode)
870 : 33913 : && SCALAR_INT_MODE_P (op_mode)
871 : 33913 : && CONST_INT_P (XEXP (op, 1))
872 : 17150492 : && 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 : 27815765 : simplify_context::simplify_unary_operation (rtx_code code, machine_mode mode,
883 : : rtx op, machine_mode op_mode)
884 : : {
885 : 27815765 : rtx trueop, tem;
886 : :
887 : 27815765 : trueop = avoid_constant_pool_reference (op);
888 : :
889 : 27815765 : tem = simplify_const_unary_operation (code, mode, trueop, op_mode);
890 : 27815765 : if (tem)
891 : : return tem;
892 : :
893 : 22736928 : 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 : 2690 : exact_int_to_float_conversion_p (const_rtx op)
901 : : {
902 : 2690 : 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 : 2690 : if (op0_mode == VOIDmode)
906 : : return false;
907 : 5378 : int out_bits = significand_size (GET_MODE_INNER (GET_MODE (op)));
908 : 2689 : int in_prec = GET_MODE_UNIT_PRECISION (op0_mode);
909 : 2689 : int in_bits = in_prec;
910 : 2689 : if (HWI_COMPUTABLE_MODE_P (op0_mode))
911 : : {
912 : 2599 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (XEXP (op, 0), op0_mode);
913 : 2599 : if (GET_CODE (op) == FLOAT)
914 : 2475 : 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 : 2599 : in_bits -= wi::ctz (wi::uhwi (nonzero, in_prec));
920 : : }
921 : 2689 : 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 : 22736928 : simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode,
928 : : rtx op)
929 : : {
930 : 22736928 : enum rtx_code reversed;
931 : 22736928 : rtx temp, elt, base, step;
932 : 22736928 : scalar_int_mode inner, int_mode, op_mode, op0_mode;
933 : :
934 : 22736928 : switch (code)
935 : : {
936 : 1807421 : case NOT:
937 : : /* (not (not X)) == X. */
938 : 1807421 : if (GET_CODE (op) == NOT)
939 : 3791 : 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 : 1803630 : if (COMPARISON_P (op)
944 : 11017 : && (mode == BImode || STORE_FLAG_VALUE == -1)
945 : 1803630 : && ((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 : 1803630 : if (GET_CODE (op) == PLUS
951 : 290748 : && XEXP (op, 1) == constm1_rtx)
952 : 7488 : 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 : 1796142 : if (GET_CODE (op) == NEG && CONSTM1_RTX (mode))
958 : 71501 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
959 : 71501 : CONSTM1_RTX (mode));
960 : :
961 : : /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
962 : 1724641 : if (GET_CODE (op) == XOR
963 : 15119 : && CONST_INT_P (XEXP (op, 1))
964 : 1728495 : && (temp = simplify_unary_operation (NOT, mode,
965 : : XEXP (op, 1), mode)) != 0)
966 : 3854 : 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 : 1720787 : if (GET_CODE (op) == PLUS
970 : 283260 : && CONST_INT_P (XEXP (op, 1))
971 : 166147 : && mode_signbit_p (mode, XEXP (op, 1))
972 : 1726225 : && (temp = simplify_unary_operation (NOT, mode,
973 : : XEXP (op, 1), mode)) != 0)
974 : 5438 : 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 : 1715349 : if (GET_CODE (op) == ASHIFT
983 : 47939 : && XEXP (op, 0) == const1_rtx)
984 : : {
985 : 1071 : temp = simplify_gen_unary (NOT, mode, const1_rtx, mode);
986 : 1071 : 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 : 1714278 : 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 : 1714278 : if (partial_subreg_p (op)
1002 : 70496 : && subreg_lowpart_p (op)
1003 : 70182 : && GET_CODE (SUBREG_REG (op)) == ASHIFT
1004 : 95993 : && XEXP (SUBREG_REG (op), 0) == const1_rtx)
1005 : : {
1006 : 164 : machine_mode inner_mode = GET_MODE (SUBREG_REG (op));
1007 : 164 : rtx x;
1008 : :
1009 : 164 : 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 : 164 : temp = rtl_hooks.gen_lowpart_no_emit (mode, x);
1014 : 164 : 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 : 1714114 : if (GET_CODE (op) == IOR || GET_CODE (op) == AND)
1023 : : {
1024 : 11650 : rtx in1 = XEXP (op, 0), in2 = XEXP (op, 1);
1025 : 11650 : machine_mode op_mode;
1026 : :
1027 : 11650 : op_mode = GET_MODE (in1);
1028 : 11650 : in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
1029 : :
1030 : 11650 : op_mode = GET_MODE (in2);
1031 : 11650 : if (op_mode == VOIDmode)
1032 : 5776 : op_mode = mode;
1033 : 11650 : in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
1034 : :
1035 : 11650 : if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
1036 : : std::swap (in1, in2);
1037 : :
1038 : 23300 : 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 : 1702464 : 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 : 1641879 : case NEG:
1051 : : /* (neg (neg X)) == X. */
1052 : 1641879 : if (GET_CODE (op) == NEG)
1053 : 6796 : 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 : 1635083 : if (GET_CODE (op) == IF_THEN_ELSE)
1059 : : {
1060 : 2486 : rtx cond = XEXP (op, 0);
1061 : 2486 : rtx true_rtx = XEXP (op, 1);
1062 : 2486 : rtx false_rtx = XEXP (op, 2);
1063 : :
1064 : 2486 : if ((GET_CODE (true_rtx) == NEG
1065 : 0 : && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
1066 : 2486 : || (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 : 1635083 : if (GET_CODE (op) == PLUS
1083 : 133742 : && XEXP (op, 1) == const1_rtx)
1084 : 53148 : return simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1085 : :
1086 : : /* Similarly, (neg (not X)) is (plus X 1). */
1087 : 1581935 : if (GET_CODE (op) == NOT)
1088 : 787 : return simplify_gen_binary (PLUS, mode, XEXP (op, 0),
1089 : 787 : 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 : 1581148 : if (GET_CODE (op) == MINUS
1097 : 23825 : && !HONOR_SIGNED_ZEROS (mode)
1098 : 1603565 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1099 : 22417 : return simplify_gen_binary (MINUS, mode, XEXP (op, 1), XEXP (op, 0));
1100 : :
1101 : 1558731 : if (GET_CODE (op) == PLUS
1102 : 80594 : && !HONOR_SIGNED_ZEROS (mode)
1103 : 1638965 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1104 : : {
1105 : : /* (neg (plus A C)) is simplified to (minus -C A). */
1106 : 80234 : if (CONST_SCALAR_INT_P (XEXP (op, 1))
1107 : 5023 : || CONST_DOUBLE_AS_FLOAT_P (XEXP (op, 1)))
1108 : : {
1109 : 75211 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 1), mode);
1110 : 75211 : if (temp)
1111 : 75211 : 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 : 5023 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
1116 : 5023 : 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 : 1478497 : if (GET_CODE (op) == MULT
1122 : 1478497 : && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1123 : : {
1124 : 20146 : temp = simplify_gen_unary (NEG, mode, XEXP (op, 1), mode);
1125 : 20146 : 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 : 1458351 : if (GET_CODE (op) == ASHIFT)
1132 : : {
1133 : 52753 : temp = simplify_unary_operation (NEG, mode, XEXP (op, 0), mode);
1134 : 52753 : if (temp)
1135 : 12672 : 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 : 1445679 : if (GET_CODE (op) == ASHIFTRT
1141 : 25787 : && CONST_INT_P (XEXP (op, 1))
1142 : 1497157 : && 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 : 1445259 : if (GET_CODE (op) == LSHIFTRT
1149 : 7479 : && CONST_INT_P (XEXP (op, 1))
1150 : 1460057 : && INTVAL (XEXP (op, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
1151 : 2432 : return simplify_gen_binary (ASHIFTRT, mode,
1152 : 2432 : 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 : 1442827 : if (GET_CODE (op) == XOR
1156 : 23945 : && XEXP (op, 1) == const1_rtx
1157 : 1442965 : && 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 : 1442787 : if (GET_CODE (op) == LT
1163 : 2539 : && XEXP (op, 1) == const0_rtx
1164 : 1444373 : && 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 : 1442451 : 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 : 1293198 : case TRUNCATE:
1210 : : /* Don't optimize (lshiftrt (mult ...)) as it would interfere
1211 : : with the umulXi3_highpart patterns. */
1212 : 1293198 : if (GET_CODE (op) == LSHIFTRT
1213 : 17772 : && GET_CODE (XEXP (op, 0)) == MULT)
1214 : : break;
1215 : :
1216 : 1285970 : 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 : 1285958 : if (GET_MODE (op) != VOIDmode)
1231 : : {
1232 : 1285958 : temp = simplify_truncation (mode, op, GET_MODE (op));
1233 : 1285958 : 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 : 1154595 : if (known_eq (GET_MODE_NUNITS (mode), 1)
1240 : 1154595 : && (TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op))
1241 : 0 : || truncated_to_mode (mode, op)))
1242 : : {
1243 : 1143549 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1244 : 1143549 : 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 : 11277 : if (HWI_COMPUTABLE_MODE_P (mode)
1253 : 231 : && COMPARISON_P (op)
1254 : 0 : && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
1255 : 11277 : && 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 : 11277 : if (GET_CODE (op) == MEM
1265 : 342 : && !VECTOR_MODE_P (mode)
1266 : 229 : && !MEM_VOLATILE_P (op)
1267 : 11506 : && !mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op)))
1268 : : {
1269 : 229 : temp = rtl_hooks.gen_lowpart_no_emit (mode, op);
1270 : 229 : if (temp)
1271 : : return temp;
1272 : : }
1273 : :
1274 : : /* Check for useless truncation. */
1275 : 11277 : if (GET_MODE (op) == mode)
1276 : : return op;
1277 : : break;
1278 : :
1279 : 171563 : case FLOAT_TRUNCATE:
1280 : : /* Check for useless truncation. */
1281 : 171563 : if (GET_MODE (op) == mode)
1282 : : return op;
1283 : :
1284 : 171563 : if (DECIMAL_FLOAT_MODE_P (mode))
1285 : : break;
1286 : :
1287 : : /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
1288 : 171409 : 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 : 171409 : if ((GET_CODE (op) == FLOAT_TRUNCATE
1302 : 133 : && flag_unsafe_math_optimizations)
1303 : 171405 : || 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 : 171403 : if ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1312 : 171403 : && (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 : 169985 : if ((GET_CODE (op) == ABS
1321 : 169985 : || 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 : 169957 : if (GET_CODE (op) == SUBREG
1330 : 308 : && subreg_lowpart_p (op)
1331 : 170262 : && GET_CODE (SUBREG_REG (op)) == FLOAT_TRUNCATE)
1332 : : return SUBREG_REG (op);
1333 : : break;
1334 : :
1335 : 593188 : case FLOAT_EXTEND:
1336 : : /* Check for useless extension. */
1337 : 593188 : if (GET_MODE (op) == mode)
1338 : : return op;
1339 : :
1340 : 593188 : 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 : 593085 : if (GET_CODE (op) == FLOAT_EXTEND
1349 : 593085 : || ((GET_CODE (op) == FLOAT || GET_CODE (op) == UNSIGNED_FLOAT)
1350 : 1271 : && exact_int_to_float_conversion_p (op)))
1351 : 560 : return simplify_gen_unary (GET_CODE (op), mode,
1352 : : XEXP (op, 0),
1353 : 560 : GET_MODE (XEXP (op, 0)));
1354 : :
1355 : : break;
1356 : :
1357 : 284873 : case ABS:
1358 : : /* (abs (neg <foo>)) -> (abs <foo>) */
1359 : 284873 : 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 : 284843 : if (GET_MODE (op) == VOIDmode)
1366 : : break;
1367 : :
1368 : : /* If operand is something known to be positive, ignore the ABS. */
1369 : 284843 : 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 : 284614 : 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 : 284614 : && 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 : 284614 : if (is_a <scalar_int_mode> (mode, &int_mode)
1400 : 39561 : && (num_sign_bit_copies (op, int_mode)
1401 : 39561 : == 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 : 3284 : case POPCOUNT:
1419 : 3284 : switch (GET_CODE (op))
1420 : : {
1421 : 0 : case BSWAP:
1422 : 0 : case BITREVERSE:
1423 : : /* (popcount (bswap <X>)) = (popcount <X>). */
1424 : 0 : return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1425 : 0 : GET_MODE (XEXP (op, 0)));
1426 : :
1427 : 34 : case ZERO_EXTEND:
1428 : : /* (popcount (zero_extend <X>)) = (zero_extend (popcount <X>)). */
1429 : 68 : temp = simplify_gen_unary (POPCOUNT, GET_MODE (XEXP (op, 0)),
1430 : 34 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1431 : 34 : return simplify_gen_unary (ZERO_EXTEND, mode, temp,
1432 : 34 : GET_MODE (temp));
1433 : :
1434 : 0 : case ROTATE:
1435 : 0 : case ROTATERT:
1436 : : /* Rotations don't affect popcount. */
1437 : 0 : if (!side_effects_p (XEXP (op, 1)))
1438 : 0 : return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0),
1439 : 0 : GET_MODE (XEXP (op, 0)));
1440 : : break;
1441 : :
1442 : : default:
1443 : : break;
1444 : : }
1445 : : break;
1446 : :
1447 : 0 : case PARITY:
1448 : 0 : switch (GET_CODE (op))
1449 : : {
1450 : 0 : case NOT:
1451 : 0 : case BSWAP:
1452 : 0 : case BITREVERSE:
1453 : 0 : return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1454 : 0 : GET_MODE (XEXP (op, 0)));
1455 : :
1456 : 0 : case ZERO_EXTEND:
1457 : 0 : case SIGN_EXTEND:
1458 : 0 : temp = simplify_gen_unary (PARITY, GET_MODE (XEXP (op, 0)),
1459 : 0 : XEXP (op, 0), GET_MODE (XEXP (op, 0)));
1460 : 0 : return simplify_gen_unary (GET_CODE (op), mode, temp,
1461 : 0 : GET_MODE (temp));
1462 : :
1463 : 0 : case ROTATE:
1464 : 0 : case ROTATERT:
1465 : : /* Rotations don't affect parity. */
1466 : 0 : if (!side_effects_p (XEXP (op, 1)))
1467 : 0 : return simplify_gen_unary (PARITY, mode, XEXP (op, 0),
1468 : 0 : GET_MODE (XEXP (op, 0)));
1469 : : break;
1470 : :
1471 : : case PARITY:
1472 : : /* (parity (parity x)) -> parity (x). */
1473 : : return op;
1474 : :
1475 : : default:
1476 : : break;
1477 : : }
1478 : : break;
1479 : :
1480 : 28948 : case BSWAP:
1481 : : /* (bswap (bswap x)) -> x. */
1482 : 28948 : 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 : 928254 : case FLOAT:
1493 : : /* (float (sign_extend <X>)) = (float <X>). */
1494 : 928254 : 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 : 3199422 : case SIGN_EXTEND:
1500 : : /* Check for useless extension. */
1501 : 3199422 : 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 : 3199382 : 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 : 3199382 : if (GET_CODE (op) == MULT)
1518 : : {
1519 : 67346 : rtx lhs = XEXP (op, 0);
1520 : 67346 : rtx rhs = XEXP (op, 1);
1521 : 67346 : enum rtx_code lcode = GET_CODE (lhs);
1522 : 67346 : 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 : 67346 : if ((lcode == SIGN_EXTEND
1527 : 67197 : || (lcode == ASHIFTRT && CONST_INT_P (XEXP (lhs, 1))))
1528 : 876 : && (rcode == SIGN_EXTEND
1529 : 832 : || (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 : 3199274 : if (GET_CODE (op) == SUBREG
1563 : 170676 : && SUBREG_PROMOTED_VAR_P (op)
1564 : 3204216 : && 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 : 3199274 : if (GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND)
1591 : : {
1592 : 18033 : gcc_assert (GET_MODE_UNIT_PRECISION (mode)
1593 : : > GET_MODE_UNIT_PRECISION (GET_MODE (op)));
1594 : 6011 : return simplify_gen_unary (GET_CODE (op), mode, XEXP (op, 0),
1595 : 6011 : 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 : 3193263 : if ((GET_CODE (op) == ASHIFTRT || GET_CODE (op) == LSHIFTRT)
1604 : 84444 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1605 : 3195759 : && is_a <scalar_int_mode> (mode, &int_mode)
1606 : 4828 : && CONST_INT_P (XEXP (op, 1))
1607 : 4828 : && XEXP (XEXP (op, 0), 1) == XEXP (op, 1)
1608 : 3197965 : && (op_mode = as_a <scalar_int_mode> (GET_MODE (op)),
1609 : 4702 : GET_MODE_PRECISION (op_mode) > INTVAL (XEXP (op, 1))))
1610 : : {
1611 : 4702 : scalar_int_mode tmode;
1612 : 4702 : gcc_assert (GET_MODE_PRECISION (int_mode)
1613 : : > GET_MODE_PRECISION (op_mode));
1614 : 4702 : if (int_mode_for_size (GET_MODE_PRECISION (op_mode)
1615 : 7072 : - INTVAL (XEXP (op, 1)), 1).exists (&tmode))
1616 : : {
1617 : 2332 : rtx inner =
1618 : 2332 : rtl_hooks.gen_lowpart_no_emit (tmode, XEXP (XEXP (op, 0), 0));
1619 : 2332 : if (inner)
1620 : 2332 : return simplify_gen_unary (GET_CODE (op) == ASHIFTRT
1621 : : ? SIGN_EXTEND : ZERO_EXTEND,
1622 : 2332 : 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 : 3190931 : if (GET_CODE (op) == LSHIFTRT
1629 : 192 : && CONST_INT_P (XEXP (op, 1))
1630 : 192 : && XEXP (op, 1) != const0_rtx)
1631 : 192 : return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1632 : :
1633 : : /* (sign_extend:M (truncate:N (lshiftrt:O <X> (const_int I)))) where
1634 : : I is GET_MODE_PRECISION(O) - GET_MODE_PRECISION(N), simplifies to
1635 : : (ashiftrt:M <X> (const_int I)) if modes M and O are the same, and
1636 : : (truncate:M (ashiftrt:O <X> (const_int I))) if M is narrower than
1637 : : O, and (sign_extend:M (ashiftrt:O <X> (const_int I))) if M is
1638 : : wider than O. */
1639 : 3190739 : 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 : 3190739 : if (val_signbit_known_clear_p (GET_MODE (op),
1668 : 3190739 : nonzero_bits (op, GET_MODE (op))))
1669 : 44000 : return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op));
1670 : :
1671 : : /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1672 : 3146739 : if (GET_CODE (op) == SUBREG
1673 : 170124 : && subreg_lowpart_p (op)
1674 : 169993 : && GET_MODE (SUBREG_REG (op)) == mode
1675 : 3289123 : && is_a <scalar_int_mode> (mode, &int_mode)
1676 : 149162 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1677 : 149162 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1678 : 147355 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1679 : 3294094 : && (nonzero_bits (SUBREG_REG (op), mode)
1680 : 147355 : & ~(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 : 3139961 : 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 : 11872123 : case ZERO_EXTEND:
1708 : : /* Check for useless extension. */
1709 : 11872123 : 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 : 11872083 : if (GET_CODE (op) == AND
1715 : 132002 : && CONST_INT_P (XEXP (op, 1))
1716 : 105902 : && INTVAL (XEXP (op, 1)) > 0)
1717 : : {
1718 : 98916 : rtx tem = rtl_hooks.gen_lowpart_no_emit (mode, XEXP (op, 0));
1719 : 98916 : if (tem)
1720 : 81948 : 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 : 11790135 : if (GET_CODE (op) == SUBREG
1727 : 1466290 : && SUBREG_PROMOTED_VAR_P (op)
1728 : 11790609 : && SUBREG_PROMOTED_UNSIGNED_P (op))
1729 : : {
1730 : 474 : rtx subreg = SUBREG_REG (op);
1731 : 474 : machine_mode subreg_mode = GET_MODE (subreg);
1732 : 474 : if (!paradoxical_subreg_p (mode, subreg_mode))
1733 : : {
1734 : 322 : temp = rtl_hooks.gen_lowpart_no_emit (mode, subreg);
1735 : 322 : if (temp)
1736 : : {
1737 : : /* Preserve SUBREG_PROMOTED_VAR_P. */
1738 : 322 : if (partial_subreg_p (temp))
1739 : : {
1740 : 129 : SUBREG_PROMOTED_VAR_P (temp) = 1;
1741 : 129 : SUBREG_PROMOTED_SET (temp, SRP_UNSIGNED);
1742 : : }
1743 : 322 : return temp;
1744 : : }
1745 : : }
1746 : : else
1747 : : /* Zero-extending a zero-extended subreg. */
1748 : 152 : return simplify_gen_unary (ZERO_EXTEND, mode,
1749 : 152 : subreg, subreg_mode);
1750 : : }
1751 : :
1752 : : /* Extending a widening multiplication should be canonicalized to
1753 : : a wider widening multiplication. */
1754 : 11789661 : if (GET_CODE (op) == MULT)
1755 : : {
1756 : 177794 : rtx lhs = XEXP (op, 0);
1757 : 177794 : rtx rhs = XEXP (op, 1);
1758 : 177794 : enum rtx_code lcode = GET_CODE (lhs);
1759 : 177794 : 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 : 177794 : if ((lcode == ZERO_EXTEND
1764 : 177058 : || (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 : 11789549 : if (GET_CODE (op) == ZERO_EXTEND)
1798 : 23819 : return simplify_gen_unary (ZERO_EXTEND, mode, XEXP (op, 0),
1799 : 23819 : 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 : 11765730 : if (GET_CODE (op) == LSHIFTRT
1805 : 81499 : && GET_CODE (XEXP (op, 0)) == ASHIFT
1806 : 11765753 : && 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 : 11765730 : && (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 : 11765730 : if (partial_subreg_p (op)
1830 : 13187122 : && is_a <scalar_int_mode> (mode, &int_mode)
1831 : 1447453 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &op0_mode)
1832 : 1446883 : && GET_MODE_PRECISION (op0_mode) <= HOST_BITS_PER_WIDE_INT
1833 : 994584 : && GET_MODE_PRECISION (int_mode) >= GET_MODE_PRECISION (op0_mode)
1834 : 971439 : && subreg_lowpart_p (op)
1835 : 2126167 : && (nonzero_bits (SUBREG_REG (op), op0_mode)
1836 : 660351 : & ~GET_MODE_MASK (GET_MODE (op))) == 0)
1837 : : {
1838 : 26061 : if (GET_MODE_PRECISION (int_mode) == GET_MODE_PRECISION (op0_mode))
1839 : 15944 : return SUBREG_REG (op);
1840 : 10117 : return simplify_gen_unary (ZERO_EXTEND, int_mode, SUBREG_REG (op),
1841 : 10117 : op0_mode);
1842 : : }
1843 : :
1844 : : /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */
1845 : 11739669 : if (GET_CODE (op) == SUBREG
1846 : 1439755 : && subreg_lowpart_p (op)
1847 : 745980 : && GET_MODE (SUBREG_REG (op)) == mode
1848 : 12403949 : && is_a <scalar_int_mode> (mode, &int_mode)
1849 : 664280 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1850 : 664280 : && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT
1851 : 595561 : && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode)
1852 : 12335230 : && (nonzero_bits (SUBREG_REG (op), mode)
1853 : 595561 : & ~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 : 11739669 : if (partial_subreg_p (op)
1865 : 1421392 : && GET_CODE (XEXP (op, 0)) == NOT
1866 : 1863 : && GET_MODE (XEXP (op, 0)) == mode
1867 : 1860 : && subreg_lowpart_p (op)
1868 : 11739952 : && HWI_COMPUTABLE_MODE_P (mode)
1869 : 284 : && is_a <scalar_int_mode> (GET_MODE (op), &op_mode)
1870 : 1440039 : && (nonzero_bits (XEXP (XEXP (op, 0), 0), mode)
1871 : 284 : & ~GET_MODE_MASK (op_mode)) == 0)
1872 : : {
1873 : 1 : unsigned HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
1874 : 2 : return simplify_gen_binary (XOR, mode,
1875 : 1 : XEXP (XEXP (op, 0), 0),
1876 : 1 : gen_int_mode (mask, mode));
1877 : : }
1878 : :
1879 : : #if defined(POINTERS_EXTEND_UNSIGNED)
1880 : : /* As we do not know which address space the pointer is referring to,
1881 : : we can do this only if the target does not support different pointer
1882 : : or address modes depending on the address space. */
1883 : 11739668 : if (target_default_pointer_address_modes_p ()
1884 : : && POINTERS_EXTEND_UNSIGNED > 0
1885 : 13039202 : && 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 : 11739689 : && !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 : 19499966 : if (VECTOR_MODE_P (mode)
1908 : 1452271 : && vec_duplicate_p (op, &elt)
1909 : 20957901 : && code != VEC_DUPLICATE)
1910 : : {
1911 : 5660 : 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 : 4605 : temp = simplify_gen_unary (code, GET_MODE_INNER (mode),
1916 : 9210 : 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 : 1055 : temp = simplify_unary_operation (code, GET_MODE_INNER (mode),
1939 : 2110 : elt, GET_MODE_INNER (GET_MODE (op)));
1940 : 5660 : if (temp)
1941 : 5179 : 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 : 27816650 : simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
1952 : : rtx op, machine_mode op_mode)
1953 : : {
1954 : 27816650 : scalar_int_mode result_mode;
1955 : :
1956 : 27816650 : if (code == VEC_DUPLICATE)
1957 : : {
1958 : 1587954 : gcc_assert (VECTOR_MODE_P (mode));
1959 : 1587954 : if (GET_MODE (op) != VOIDmode)
1960 : : {
1961 : 521631 : if (!VECTOR_MODE_P (GET_MODE (op)))
1962 : 1030074 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE (op));
1963 : : else
1964 : 19782 : gcc_assert (GET_MODE_INNER (mode) == GET_MODE_INNER
1965 : : (GET_MODE (op)));
1966 : : }
1967 : 1587954 : if (CONST_SCALAR_INT_P (op) || CONST_DOUBLE_AS_FLOAT_P (op))
1968 : 1102692 : return gen_const_vec_duplicate (mode, op);
1969 : 485262 : if (GET_CODE (op) == CONST_VECTOR
1970 : 485262 : && (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 : 25535741 : if (VECTOR_MODE_P (mode)
1985 : 1510931 : && GET_CODE (op) == CONST_VECTOR
1986 : 26872221 : && known_eq (GET_MODE_NUNITS (mode), CONST_VECTOR_NUNITS (op)))
1987 : : {
1988 : 53006 : gcc_assert (GET_MODE (op) == op_mode);
1989 : :
1990 : 53006 : rtx_vector_builder builder;
1991 : 53006 : if (!builder.new_unary_operation (mode, op, false))
1992 : : return 0;
1993 : :
1994 : 53006 : unsigned int count = builder.encoded_nelts ();
1995 : 204077 : for (unsigned int i = 0; i < count; i++)
1996 : : {
1997 : 303180 : rtx x = simplify_unary_operation (code, GET_MODE_INNER (mode),
1998 : : CONST_VECTOR_ELT (op, i),
1999 : 303180 : GET_MODE_INNER (op_mode));
2000 : 151590 : if (!x || !valid_for_const_vector_p (mode, x))
2001 : 519 : return 0;
2002 : 151071 : builder.quick_push (x);
2003 : : }
2004 : 52487 : return builder.build ();
2005 : 53006 : }
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 : 26660197 : if (code == FLOAT && CONST_SCALAR_INT_P (op))
2012 : : {
2013 : 7136 : REAL_VALUE_TYPE d;
2014 : :
2015 : 7136 : 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 : 7136 : 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 : 7136 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2030 : : return 0;
2031 : :
2032 : 7136 : 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 : 7136 : 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 : 6239 : return const_double_from_real_value (d, mode);
2047 : : }
2048 : 26653061 : else if (code == UNSIGNED_FLOAT && CONST_SCALAR_INT_P (op))
2049 : : {
2050 : 2147 : REAL_VALUE_TYPE d;
2051 : :
2052 : 2147 : 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 : 2147 : 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 : 2147 : if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
2067 : : return 0;
2068 : :
2069 : 2147 : 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 : 2147 : 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 : 2131 : return const_double_from_real_value (d, mode);
2084 : : }
2085 : :
2086 : 26650914 : if (CONST_SCALAR_INT_P (op) && is_a <scalar_int_mode> (mode, &result_mode))
2087 : : {
2088 : 3432532 : unsigned int width = GET_MODE_PRECISION (result_mode);
2089 : 3432532 : if (width > MAX_BITSIZE_MODE_ANY_INT)
2090 : : return 0;
2091 : :
2092 : 3432532 : wide_int result;
2093 : 3432532 : scalar_int_mode imode = (op_mode == VOIDmode
2094 : 3432532 : ? result_mode
2095 : 3432295 : : as_a <scalar_int_mode> (op_mode));
2096 : 3432532 : rtx_mode_t op0 = rtx_mode_t (op, imode);
2097 : 3432532 : 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 : 3432532 : switch (code)
2110 : : {
2111 : 179672 : case NOT:
2112 : 179672 : result = wi::bit_not (op0);
2113 : 179672 : break;
2114 : :
2115 : 1893455 : case NEG:
2116 : 1893455 : result = wi::neg (op0);
2117 : 1893455 : break;
2118 : :
2119 : 7297 : case ABS:
2120 : 7297 : result = wi::abs (op0);
2121 : 7297 : 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 : 1184878 : case TRUNCATE:
2164 : 1184878 : case ZERO_EXTEND:
2165 : 1184878 : result = wide_int::from (op0, width, UNSIGNED);
2166 : 1184878 : 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 : 150592 : case SIGN_EXTEND:
2183 : 150592 : result = wide_int::from (op0, width, SIGNED);
2184 : 150592 : 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 : 3432341 : return immed_wide_int_const (result, result_mode);
2206 : 3432532 : }
2207 : :
2208 : 23218382 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2209 : 485809 : && SCALAR_FLOAT_MODE_P (mode)
2210 : 483812 : && SCALAR_FLOAT_MODE_P (GET_MODE (op)))
2211 : : {
2212 : 483812 : REAL_VALUE_TYPE d = *CONST_DOUBLE_REAL_VALUE (op);
2213 : 483812 : switch (code)
2214 : : {
2215 : : case SQRT:
2216 : : return 0;
2217 : 23068 : case ABS:
2218 : 23068 : d = real_value_abs (&d);
2219 : 23068 : break;
2220 : 15671 : case NEG:
2221 : 15671 : d = real_value_negate (&d);
2222 : 15671 : break;
2223 : 921 : case FLOAT_TRUNCATE:
2224 : : /* Don't perform the operation if flag_signaling_nans is on
2225 : : and the operand is a signaling NaN. */
2226 : 921 : 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 : 921 : if (HONOR_SIGN_DEPENDENT_ROUNDING (mode)
2231 : 921 : && !exact_real_truncate (mode, &d))
2232 : 231 : return NULL_RTX;
2233 : 690 : d = real_value_truncate (mode, d);
2234 : 690 : break;
2235 : 395475 : case FLOAT_EXTEND:
2236 : : /* Don't perform the operation if flag_signaling_nans is on
2237 : : and the operand is a signaling NaN. */
2238 : 395475 : 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 : 395473 : 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 : 48075 : case NOT:
2253 : 48075 : {
2254 : 48075 : long tmp[4];
2255 : 48075 : int i;
2256 : :
2257 : 48075 : real_to_target (tmp, &d, GET_MODE (op));
2258 : 240375 : for (i = 0; i < 4; i++)
2259 : 192300 : tmp[i] = ~tmp[i];
2260 : 48075 : real_from_target (&d, tmp, mode);
2261 : 48075 : break;
2262 : : }
2263 : 0 : default:
2264 : 0 : gcc_unreachable ();
2265 : : }
2266 : 482977 : return const_double_from_real_value (d, mode);
2267 : : }
2268 : 1997 : else if (CONST_DOUBLE_AS_FLOAT_P (op)
2269 : 1997 : && SCALAR_FLOAT_MODE_P (GET_MODE (op))
2270 : 22736567 : && is_int_mode (mode, &result_mode))
2271 : : {
2272 : 1997 : unsigned int width = GET_MODE_PRECISION (result_mode);
2273 : 1997 : 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 : 1997 : REAL_VALUE_TYPE t;
2284 : 1997 : const REAL_VALUE_TYPE *x = CONST_DOUBLE_REAL_VALUE (op);
2285 : 1997 : 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 : 1997 : bool fail;
2289 : :
2290 : 1997 : switch (code)
2291 : : {
2292 : 1989 : 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 : 1989 : if (REAL_VALUE_ISNAN (*x))
2301 : 941 : return flag_trapping_math ? NULL_RTX : const0_rtx;
2302 : :
2303 : 1048 : if (REAL_VALUE_ISINF (*x) && flag_trapping_math)
2304 : : return NULL_RTX;
2305 : :
2306 : : /* Test against the signed upper bound. */
2307 : 102 : wmax = wi::max_value (width, SIGNED);
2308 : 102 : real_from_integer (&t, VOIDmode, wmax, SIGNED);
2309 : 102 : 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 : 99 : wmin = wi::min_value (width, SIGNED);
2315 : 99 : real_from_integer (&t, VOIDmode, wmin, SIGNED);
2316 : 99 : if (real_less (x, &t))
2317 : 8 : return immed_wide_int_const (wmin, mode);
2318 : :
2319 : 91 : 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 : 1997 : }
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 : 39756216 : simplify_context::simplify_byte_swapping_operation (rtx_code code,
2374 : : machine_mode mode,
2375 : : rtx op0, rtx op1)
2376 : : {
2377 : 39756216 : rtx tem;
2378 : :
2379 : : /* (op (bswap x) C1)) -> (bswap (op x C2)) with C2 swapped. */
2380 : 39756216 : 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 : 39755713 : 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 : 49308785 : simplify_context::simplify_associative_operation (rtx_code code,
2405 : : machine_mode mode,
2406 : : rtx op0, rtx op1)
2407 : : {
2408 : 49308785 : 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 : 49308785 : if (++assoc_count >= max_assoc_count)
2418 : : return NULL_RTX;
2419 : :
2420 : : /* Linearize the operator to the left. */
2421 : 49307427 : if (GET_CODE (op1) == code)
2422 : : {
2423 : : /* "(a op b) op (c op d)" becomes "((a op b) op c) op d)". */
2424 : 27270 : if (GET_CODE (op0) == code)
2425 : : {
2426 : 5720 : tem = simplify_gen_binary (code, mode, op0, XEXP (op1, 0));
2427 : 5720 : 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 : 21550 : if (! swap_commutative_operands_p (op1, op0))
2432 : 21550 : return simplify_gen_binary (code, mode, op1, op0);
2433 : :
2434 : : std::swap (op0, op1);
2435 : : }
2436 : :
2437 : 49280157 : if (GET_CODE (op0) == code)
2438 : : {
2439 : : /* Canonicalize "(x op c) op y" as "(x op y) op c". */
2440 : 1565682 : if (swap_commutative_operands_p (XEXP (op0, 1), op1))
2441 : : {
2442 : 312767 : tem = simplify_gen_binary (code, mode, XEXP (op0, 0), op1);
2443 : 312767 : 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 : 1252915 : tem = simplify_binary_operation (code, mode, XEXP (op0, 1), op1);
2448 : 1252915 : if (tem != 0)
2449 : 91192 : 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 : 1161723 : tem = simplify_binary_operation (code, mode, XEXP (op0, 0), op1);
2453 : 1161723 : if (tem != 0)
2454 : 10109 : 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 : 18730 : 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 : 6528 : mask_to_unsigned_comparison (int mask)
2492 : : {
2493 : 6528 : switch (mask)
2494 : : {
2495 : : case 8:
2496 : : return LTU;
2497 : 160 : case 4:
2498 : 160 : return GTU;
2499 : 2535 : case 2:
2500 : 2535 : return EQ;
2501 : :
2502 : 160 : case 10:
2503 : 160 : return LEU;
2504 : 160 : case 6:
2505 : 160 : return GEU;
2506 : :
2507 : 3353 : case 12:
2508 : 3353 : 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 : 845426 : relational_result (machine_mode mode, machine_mode cmp_mode, rtx res)
2605 : : {
2606 : 845426 : if (SCALAR_FLOAT_MODE_P (mode))
2607 : : {
2608 : 202 : if (res == const0_rtx)
2609 : 198 : return CONST0_RTX (mode);
2610 : : #ifdef FLOAT_STORE_FLAG_VALUE
2611 : : REAL_VALUE_TYPE val = FLOAT_STORE_FLAG_VALUE (mode);
2612 : : return const_double_from_real_value (val, mode);
2613 : : #else
2614 : : return NULL_RTX;
2615 : : #endif
2616 : : }
2617 : 845224 : 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 : 844874 : 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 : 15310214 : simplify_context::simplify_logical_relational_operation (rtx_code code,
2653 : : machine_mode mode,
2654 : : rtx op0, rtx op1,
2655 : : bool invert0_p)
2656 : : {
2657 : 15310214 : if (!(COMPARISON_P (op0) && COMPARISON_P (op1)))
2658 : : return 0;
2659 : :
2660 : 21645 : if (!(rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2661 : 9858 : && rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))))
2662 : 2422 : return 0;
2663 : :
2664 : 9365 : if (side_effects_p (op0))
2665 : : return 0;
2666 : :
2667 : 9365 : enum rtx_code code0 = GET_CODE (op0);
2668 : 9365 : enum rtx_code code1 = GET_CODE (op1);
2669 : 9365 : machine_mode cmp_mode = GET_MODE (XEXP (op0, 0));
2670 : 9365 : 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 : 9365 : int all = 14;
2676 : 9365 : int mask0 = unsigned_comparison_to_mask (code0);
2677 : 9365 : int mask1 = unsigned_comparison_to_mask (code1);
2678 : 18730 : bool unsigned_p = (IN_RANGE (mask0 & 12, 4, 8)
2679 : 9365 : || 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 : 8032 : 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 : 8085 : if (invert0_p)
2699 : 4603 : mask0 = mask0 ^ all;
2700 : :
2701 : 8085 : int mask;
2702 : 8085 : if (code == AND)
2703 : 960 : mask = mask0 & mask1;
2704 : 7125 : else if (code == IOR)
2705 : 948 : mask = mask0 | mask1;
2706 : 6177 : else if (code == XOR)
2707 : 6177 : mask = mask0 ^ mask1;
2708 : : else
2709 : : return 0;
2710 : :
2711 : 8085 : if (mask == all)
2712 : 232 : return relational_result (mode, GET_MODE (op0), const_true_rtx);
2713 : :
2714 : 7853 : if (mask == 0)
2715 : 232 : return relational_result (mode, GET_MODE (op0), const0_rtx);
2716 : :
2717 : 7621 : if (unsigned_p)
2718 : 6528 : 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 : 7542 : op0 = XEXP (op1, 0);
2732 : 7542 : op1 = XEXP (op1, 1);
2733 : :
2734 : 7542 : 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 : 471821528 : simplify_context::simplify_binary_operation (rtx_code code, machine_mode mode,
2744 : : rtx op0, rtx op1)
2745 : : {
2746 : 471821528 : rtx trueop0, trueop1;
2747 : 471821528 : 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 : 471821528 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMPARE);
2754 : 471821528 : gcc_assert (GET_RTX_CLASS (code) != RTX_COMM_COMPARE);
2755 : :
2756 : : /* Make sure the constant is second. */
2757 : 471821528 : if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
2758 : 471821528 : && swap_commutative_operands_p (op0, op1))
2759 : : std::swap (op0, op1);
2760 : :
2761 : 471821528 : trueop0 = avoid_constant_pool_reference (op0);
2762 : 471821528 : trueop1 = avoid_constant_pool_reference (op1);
2763 : :
2764 : 471821528 : tem = simplify_const_binary_operation (code, mode, trueop0, trueop1);
2765 : 471821528 : if (tem)
2766 : : return tem;
2767 : 441645540 : tem = simplify_binary_operation_1 (code, mode, op0, op1, trueop0, trueop1);
2768 : :
2769 : 441645540 : 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 : 379739203 : if (trueop0 != op0 || trueop1 != op1)
2775 : 583941 : 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 : 2254975 : simplify_context::simplify_binary_operation_series (rtx_code code,
2791 : : machine_mode mode,
2792 : : rtx op0, rtx op1)
2793 : : {
2794 : 2254975 : rtx base0, step0;
2795 : 2254975 : if (vec_duplicate_p (op0, &base0))
2796 : 65345 : step0 = const0_rtx;
2797 : 2189630 : else if (!vec_series_p (op0, &base0, &step0))
2798 : : return NULL_RTX;
2799 : :
2800 : 65661 : rtx base1, step1;
2801 : 65661 : if (vec_duplicate_p (op1, &base1))
2802 : 382 : step1 = const0_rtx;
2803 : 65279 : 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 : 3403 : scalar_mode inner_mode = GET_MODE_INNER (mode);
2810 : 3403 : rtx new_base = simplify_binary_operation (code, inner_mode, base0, base1);
2811 : 3403 : if (!new_base)
2812 : : return NULL_RTX;
2813 : :
2814 : 3112 : rtx new_step = simplify_binary_operation (code, inner_mode, step0, step1);
2815 : 3112 : if (!new_step)
2816 : : return NULL_RTX;
2817 : :
2818 : 3112 : 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 : 1514781 : simplify_context::simplify_distributive_operation (rtx_code code,
2828 : : machine_mode mode,
2829 : : rtx op0, rtx op1)
2830 : : {
2831 : 1514781 : enum rtx_code op = GET_CODE (op0);
2832 : 1514781 : gcc_assert (GET_CODE (op1) == op);
2833 : :
2834 : 1514781 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1))
2835 : 1514781 : && ! side_effects_p (XEXP (op0, 1)))
2836 : 317964 : return simplify_gen_binary (op, mode,
2837 : : simplify_gen_binary (code, mode,
2838 : : XEXP (op0, 0),
2839 : : XEXP (op1, 0)),
2840 : 317964 : XEXP (op0, 1));
2841 : :
2842 : 1196817 : if (GET_RTX_CLASS (op) == RTX_COMM_ARITH)
2843 : : {
2844 : 1168667 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
2845 : 1168667 : && ! side_effects_p (XEXP (op0, 0)))
2846 : 537307 : return simplify_gen_binary (op, mode,
2847 : : simplify_gen_binary (code, mode,
2848 : : XEXP (op0, 1),
2849 : : XEXP (op1, 1)),
2850 : 537307 : XEXP (op0, 0));
2851 : 631360 : if (rtx_equal_p (XEXP (op0, 0), XEXP (op1, 1))
2852 : 631360 : && ! 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 : 631308 : if (rtx_equal_p (XEXP (op0, 1), XEXP (op1, 0))
2859 : 631308 : && ! side_effects_p (XEXP (op0, 1)))
2860 : 326289 : return simplify_gen_binary (op, mode,
2861 : : simplify_gen_binary (code, mode,
2862 : : XEXP (op0, 0),
2863 : : XEXP (op1, 1)),
2864 : 326289 : 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 : 142158 : reverse_rotate_by_imm_p (machine_mode mode, unsigned int left, rtx op1)
2879 : : {
2880 : 142158 : 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 : 110529 : optab binoptab = left ? rotl_optab : rotr_optab;
2887 : 46219 : optab re_binoptab = left ? rotr_optab : rotl_optab;
2888 : 110529 : enum insn_code icode = optab_handler (binoptab, mode);
2889 : 110529 : 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 : 110529 : 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 : 108029 : if ((icode == CODE_FOR_nothing)
2900 : 108029 : || (!insn_operand_matches (icode, 2, op1))
2901 : 540145 : || (IN_RANGE (INTVAL (op1),
2902 : : GET_MODE_UNIT_PRECISION (mode) / 2 + left,
2903 : : GET_MODE_UNIT_PRECISION (mode) - 1)))
2904 : 14080 : 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 : 521492640 : extract_ashift_operands_p (rtx x, rtx *shift_opnd, rtx *shift_amnt)
2916 : : {
2917 : 521492640 : if (GET_CODE (x) == ASHIFT)
2918 : : {
2919 : 13752471 : *shift_opnd = XEXP (x, 0);
2920 : 13752471 : *shift_amnt = XEXP (x, 1);
2921 : 13752471 : return true;
2922 : : }
2923 : 507740169 : if (GET_CODE (x) == PLUS && rtx_equal_p (XEXP (x, 0), XEXP (x, 1)))
2924 : : {
2925 : 10875 : *shift_opnd = XEXP (x, 0);
2926 : 10875 : *shift_amnt = CONST1_RTX (GET_MODE (x));
2927 : 10875 : return true;
2928 : : }
2929 : 507729294 : *shift_opnd = NULL_RTX;
2930 : 507729294 : *shift_amnt = NULL_RTX;
2931 : 507729294 : 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 : 262893842 : 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 : 262893842 : rtx opleft = op0;
2948 : 262893842 : rtx opright = op1;
2949 : 262893842 : 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 : 262893842 : bool ashift_op_p
2953 : 262893842 : = extract_ashift_operands_p (op1, &ashift_opnd, &ashift_amnt);
2954 : :
2955 : 262893842 : if (ashift_op_p
2956 : 261179576 : || GET_CODE (op1) == SUBREG)
2957 : : {
2958 : : opleft = op1;
2959 : : opright = op0;
2960 : : }
2961 : : else
2962 : : {
2963 : 258598798 : opright = op1;
2964 : 258598798 : opleft = op0;
2965 : 258598798 : ashift_op_p
2966 : 258598798 : = extract_ashift_operands_p (opleft, &ashift_opnd, &ashift_amnt);
2967 : : }
2968 : :
2969 : 13763346 : if (ashift_op_p && GET_CODE (opright) == LSHIFTRT
2970 : 261222297 : && rtx_equal_p (ashift_opnd, XEXP (opright, 0)))
2971 : : {
2972 : 9795 : rtx leftcst = unwrap_const_vec_duplicate (ashift_amnt);
2973 : 9795 : rtx rightcst = unwrap_const_vec_duplicate (XEXP (opright, 1));
2974 : :
2975 : 5965 : if (CONST_INT_P (leftcst) && CONST_INT_P (rightcst)
2976 : 15760 : && (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 : 262888482 : scalar_int_mode int_mode, inner_mode;
2984 : :
2985 : 262888482 : if (GET_CODE (opleft) == SUBREG
2986 : 266620086 : && is_a <scalar_int_mode> (mode, &int_mode)
2987 : 3726244 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (opleft)),
2988 : : &inner_mode)
2989 : 3698001 : && GET_CODE (SUBREG_REG (opleft)) == ASHIFT
2990 : 108579 : && GET_CODE (opright) == LSHIFTRT
2991 : 1039 : && GET_CODE (XEXP (opright, 0)) == SUBREG
2992 : 141 : && known_eq (SUBREG_BYTE (opleft), SUBREG_BYTE (XEXP (opright, 0)))
2993 : 278 : && GET_MODE_SIZE (int_mode) < GET_MODE_SIZE (inner_mode)
2994 : 125 : && rtx_equal_p (XEXP (SUBREG_REG (opleft), 0),
2995 : 125 : SUBREG_REG (XEXP (opright, 0)))
2996 : 19 : && CONST_INT_P (XEXP (SUBREG_REG (opleft), 1))
2997 : 19 : && CONST_INT_P (XEXP (opright, 1))
2998 : 262888482 : && (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 : 41910618 : match_plus_neg_pattern (rtx op0, rtx op1, machine_mode mode)
3011 : : {
3012 : : /* Remove SUBREG from OP0 and OP1, if needed. */
3013 : 41910618 : if (GET_CODE (op0) == SUBREG
3014 : 7267469 : && GET_CODE (op1) == SUBREG
3015 : 280905 : && subreg_lowpart_p (op0)
3016 : 42190129 : && subreg_lowpart_p (op1))
3017 : : {
3018 : 279502 : op0 = XEXP (op0, 0);
3019 : 279502 : op1 = XEXP (op1, 0);
3020 : : }
3021 : :
3022 : : /* Check for the pattern (OP (plus (A - 1)) (neg A)). */
3023 : 41910618 : if (((GET_CODE (op1) == NEG
3024 : 3589 : && GET_CODE (op0) == PLUS
3025 : 2188 : && XEXP (op0, 1) == CONSTM1_RTX (mode))
3026 : 41909958 : || (GET_CODE (op0) == NEG
3027 : 77312 : && GET_CODE (op1) == PLUS
3028 : 0 : && XEXP (op1, 1) == CONSTM1_RTX (mode)))
3029 : 660 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3030 : 41910620 : && !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 : 79514792 : non_paradoxical_subreg_not_p (rtx op)
3040 : : {
3041 : 79514792 : return GET_CODE (op) == SUBREG
3042 : 8699892 : && !paradoxical_subreg_p (op)
3043 : 82427971 : && 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 : 39758603 : simplify_with_subreg_not (rtx_code binop, machine_mode mode, rtx op0, rtx op1)
3052 : : {
3053 : 39758603 : rtx opn = NULL_RTX;
3054 : 39758603 : if (non_paradoxical_subreg_not_p (op0))
3055 : : opn = op0;
3056 : 39756189 : else if (non_paradoxical_subreg_not_p (op1))
3057 : : opn = op1;
3058 : :
3059 : 2417 : if (opn == NULL_RTX)
3060 : : return NULL_RTX;
3061 : :
3062 : 4834 : rtx new_subreg = simplify_gen_subreg (mode,
3063 : : XEXP (SUBREG_REG (opn), 0),
3064 : 2417 : GET_MODE (SUBREG_REG (opn)),
3065 : 2417 : SUBREG_BYTE (opn));
3066 : :
3067 : 2417 : if (!new_subreg)
3068 : : return NULL_RTX;
3069 : :
3070 : 2387 : rtx new_not = simplify_gen_unary (NOT, mode, new_subreg, mode);
3071 : 2387 : if (opn == op0)
3072 : 2384 : 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 : 441645540 : 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 : 441645540 : rtx tem, reversed, elt0, elt1;
3089 : 441645540 : HOST_WIDE_INT val;
3090 : 441645540 : scalar_int_mode int_mode, inner_mode;
3091 : 441645540 : poly_int64 offset;
3092 : :
3093 : : /* Even if we can't compute a constant result,
3094 : : there are some cases worth simplifying. */
3095 : :
3096 : 441645540 : switch (code)
3097 : : {
3098 : 250078075 : 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 : 496249575 : if (!HONOR_SIGNED_ZEROS (mode) && !HONOR_SNANS (mode)
3104 : 496249563 : && 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 : 248773322 : if (GET_CODE (op0) == NEG)
3110 : 26064 : return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
3111 : 248747258 : else if (GET_CODE (op1) == NEG)
3112 : 7144 : return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
3113 : :
3114 : : /* (~a) + 1 -> -a */
3115 : 248740114 : if (INTEGRAL_MODE_P (mode)
3116 : 243964781 : && GET_CODE (op0) == NOT
3117 : 617349 : && trueop1 == const1_rtx)
3118 : 3486 : 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 : 248736628 : if ((GET_CODE (op0) == CONST
3127 : 248736628 : || GET_CODE (op0) == SYMBOL_REF
3128 : 246291749 : || GET_CODE (op0) == LABEL_REF)
3129 : 248736628 : && poly_int_rtx_p (op1, &offset))
3130 : 2443903 : return plus_constant (mode, op0, offset);
3131 : 246292725 : else if ((GET_CODE (op1) == CONST
3132 : 246292725 : || GET_CODE (op1) == SYMBOL_REF
3133 : 242652274 : || GET_CODE (op1) == LABEL_REF)
3134 : 246292725 : && 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 : 246292725 : if (is_a <scalar_int_mode> (mode, &int_mode))
3144 : : {
3145 : 239495522 : rtx lhs = op0, rhs = op1;
3146 : :
3147 : 239495522 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3148 : 239495522 : wide_int coeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3149 : :
3150 : 239495522 : 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 : 239495522 : else if (GET_CODE (lhs) == MULT
3156 : 6628503 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3157 : : {
3158 : 5531711 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3159 : 5531711 : lhs = XEXP (lhs, 0);
3160 : : }
3161 : 233963811 : else if (GET_CODE (lhs) == ASHIFT
3162 : 10633378 : && CONST_INT_P (XEXP (lhs, 1))
3163 : 10562062 : && INTVAL (XEXP (lhs, 1)) >= 0
3164 : 244525861 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3165 : : {
3166 : 10562050 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3167 : 21124100 : GET_MODE_PRECISION (int_mode));
3168 : 10562050 : lhs = XEXP (lhs, 0);
3169 : : }
3170 : :
3171 : 239495522 : 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 : 239495522 : else if (GET_CODE (rhs) == MULT
3177 : 273748 : && CONST_INT_P (XEXP (rhs, 1)))
3178 : : {
3179 : 173089 : coeff1 = rtx_mode_t (XEXP (rhs, 1), int_mode);
3180 : 173089 : rhs = XEXP (rhs, 0);
3181 : : }
3182 : 239322433 : else if (GET_CODE (rhs) == ASHIFT
3183 : 508151 : && CONST_INT_P (XEXP (rhs, 1))
3184 : 498379 : && INTVAL (XEXP (rhs, 1)) >= 0
3185 : 239820812 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3186 : : {
3187 : 498379 : coeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3188 : 996758 : GET_MODE_PRECISION (int_mode));
3189 : 498379 : rhs = XEXP (rhs, 0);
3190 : : }
3191 : :
3192 : 239495522 : if (rtx_equal_p (lhs, rhs))
3193 : : {
3194 : 731368 : rtx orig = gen_rtx_PLUS (int_mode, op0, op1);
3195 : 731368 : rtx coeff;
3196 : 731368 : bool speed = optimize_function_for_speed_p (cfun);
3197 : :
3198 : 731368 : coeff = immed_wide_int_const (coeff0 + coeff1, int_mode);
3199 : :
3200 : 731368 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3201 : 731368 : return (set_src_cost (tem, int_mode, speed)
3202 : 731368 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3203 : : }
3204 : :
3205 : : /* Optimize (X - 1) * Y + Y to X * Y. */
3206 : 238764154 : lhs = op0;
3207 : 238764154 : rhs = op1;
3208 : 238764154 : if (GET_CODE (op0) == MULT)
3209 : : {
3210 : 6606762 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3211 : 275020 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx)
3212 : 6565358 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3213 : 34298 : && XEXP (XEXP (op0, 0), 1) == const1_rtx))
3214 : 6648166 : && rtx_equal_p (XEXP (op0, 1), op1))
3215 : 111 : lhs = XEXP (XEXP (op0, 0), 0);
3216 : 6606651 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3217 : 1627 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx)
3218 : 6606616 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3219 : 267 : && XEXP (XEXP (op0, 1), 1) == const1_rtx))
3220 : 6606686 : && rtx_equal_p (XEXP (op0, 0), op1))
3221 : 0 : lhs = XEXP (XEXP (op0, 1), 0);
3222 : : }
3223 : 232157392 : else if (GET_CODE (op1) == MULT)
3224 : : {
3225 : 125249 : if (((GET_CODE (XEXP (op1, 0)) == PLUS
3226 : 84 : && XEXP (XEXP (op1, 0), 1) == constm1_rtx)
3227 : 125247 : || (GET_CODE (XEXP (op1, 0)) == MINUS
3228 : 27 : && XEXP (XEXP (op1, 0), 1) == const1_rtx))
3229 : 125251 : && rtx_equal_p (XEXP (op1, 1), op0))
3230 : 0 : rhs = XEXP (XEXP (op1, 0), 0);
3231 : 125249 : else if (((GET_CODE (XEXP (op1, 1)) == PLUS
3232 : 45 : && XEXP (XEXP (op1, 1), 1) == constm1_rtx)
3233 : 125249 : || (GET_CODE (XEXP (op1, 1)) == MINUS
3234 : 0 : && XEXP (XEXP (op1, 1), 1) == const1_rtx))
3235 : 125249 : && rtx_equal_p (XEXP (op1, 0), op0))
3236 : 0 : rhs = XEXP (XEXP (op1, 1), 0);
3237 : : }
3238 : 238764154 : if (lhs != op0 || rhs != op1)
3239 : 111 : return simplify_gen_binary (MULT, int_mode, lhs, rhs);
3240 : 239495522 : }
3241 : :
3242 : : /* (plus (xor X C1) C2) is (xor X (C1^C2)) if C2 is signbit. */
3243 : 245561246 : if (CONST_SCALAR_INT_P (op1)
3244 : 190695973 : && GET_CODE (op0) == XOR
3245 : 22769 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3246 : 245575543 : && 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 : 245561125 : if (CONST_SCALAR_INT_P (op1)
3254 : 190695852 : && GET_CODE (op0) == XOR
3255 : 22648 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3256 : 14176 : && nonzero_bits (XEXP (op0, 0), mode) == 1
3257 : 193 : && 2 * (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) == 0
3258 : 245561128 : && 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 : 245561122 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3265 : 245558644 : && GET_CODE (op0) == MULT
3266 : 252529227 : && GET_CODE (XEXP (op0, 0)) == NEG)
3267 : : {
3268 : 5504 : rtx in1, in2;
3269 : :
3270 : 5504 : in1 = XEXP (XEXP (op0, 0), 0);
3271 : 5504 : in2 = XEXP (op0, 1);
3272 : 5504 : return simplify_gen_binary (MINUS, mode, op1,
3273 : : simplify_gen_binary (MULT, mode,
3274 : 5504 : in1, in2));
3275 : : }
3276 : :
3277 : : /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3278 : : C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3279 : : is 1. */
3280 : 245555618 : if (COMPARISON_P (op0)
3281 : 1507700 : && ((STORE_FLAG_VALUE == -1 && trueop1 == const1_rtx)
3282 : 1507700 : || (STORE_FLAG_VALUE == 1 && trueop1 == constm1_rtx))
3283 : 245607908 : && (reversed = reversed_comparison (op0, mode)))
3284 : 51974 : return
3285 : 51974 : 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 : 245503644 : 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 : 245502174 : if (INTEGRAL_MODE_P (mode)
3299 : 240726890 : && (plus_minus_operand_p (op0)
3300 : 207558548 : || plus_minus_operand_p (op1))
3301 : 34146371 : && (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 : 212044208 : if (FLOAT_MODE_P (mode)
3307 : 4775284 : && flag_associative_math)
3308 : : {
3309 : 868687 : tem = simplify_associative_operation (code, mode, op0, op1);
3310 : 868687 : if (tem)
3311 : : return tem;
3312 : : }
3313 : :
3314 : : /* Handle vector series. */
3315 : 212030032 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3316 : : {
3317 : 1864192 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3318 : 1864192 : if (tem)
3319 : : return tem;
3320 : : }
3321 : : break;
3322 : :
3323 : : case COMPARE:
3324 : : break;
3325 : :
3326 : 41700966 : 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 : 41700966 : if (rtx_equal_p (trueop0, trueop1)
3331 : 221797 : && ! side_effects_p (op0)
3332 : 41921541 : && (!FLOAT_MODE_P (mode) || !HONOR_NANS (mode)))
3333 : 218193 : 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 : 41482773 : if (!HONOR_SIGNED_ZEROS (mode) && trueop0 == CONST0_RTX (mode))
3340 : 291794 : 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 : 41190979 : if (trueop0 == CONSTM1_RTX (mode)
3346 : 41190979 : && !contains_symbolic_reference_p (op1))
3347 : 361950 : 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 : 41524576 : if (!(HONOR_SIGNED_ZEROS (mode)
3353 : 695547 : && HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3354 : 40827851 : && !HONOR_SNANS (mode)
3355 : 81656844 : && 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 : 39871226 : if (is_a <scalar_int_mode> (mode, &int_mode))
3365 : : {
3366 : 38644934 : rtx lhs = op0, rhs = op1;
3367 : :
3368 : 38644934 : wide_int coeff0 = wi::one (GET_MODE_PRECISION (int_mode));
3369 : 38644934 : wide_int negcoeff1 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3370 : :
3371 : 38644934 : if (GET_CODE (lhs) == NEG)
3372 : : {
3373 : 118078 : coeff0 = wi::minus_one (GET_MODE_PRECISION (int_mode));
3374 : 118078 : lhs = XEXP (lhs, 0);
3375 : : }
3376 : 38526856 : else if (GET_CODE (lhs) == MULT
3377 : 188640 : && CONST_SCALAR_INT_P (XEXP (lhs, 1)))
3378 : : {
3379 : 78830 : coeff0 = rtx_mode_t (XEXP (lhs, 1), int_mode);
3380 : 78830 : lhs = XEXP (lhs, 0);
3381 : : }
3382 : 38448026 : else if (GET_CODE (lhs) == ASHIFT
3383 : 323601 : && CONST_INT_P (XEXP (lhs, 1))
3384 : 320524 : && INTVAL (XEXP (lhs, 1)) >= 0
3385 : 38768529 : && INTVAL (XEXP (lhs, 1)) < GET_MODE_PRECISION (int_mode))
3386 : : {
3387 : 320503 : coeff0 = wi::set_bit_in_zero (INTVAL (XEXP (lhs, 1)),
3388 : 641006 : GET_MODE_PRECISION (int_mode));
3389 : 320503 : lhs = XEXP (lhs, 0);
3390 : : }
3391 : :
3392 : 38644934 : if (GET_CODE (rhs) == NEG)
3393 : : {
3394 : 7290 : negcoeff1 = wi::one (GET_MODE_PRECISION (int_mode));
3395 : 7290 : rhs = XEXP (rhs, 0);
3396 : : }
3397 : 38637644 : else if (GET_CODE (rhs) == MULT
3398 : 165175 : && CONST_INT_P (XEXP (rhs, 1)))
3399 : : {
3400 : 130961 : negcoeff1 = wi::neg (rtx_mode_t (XEXP (rhs, 1), int_mode));
3401 : 130961 : rhs = XEXP (rhs, 0);
3402 : : }
3403 : 38506683 : else if (GET_CODE (rhs) == ASHIFT
3404 : 388972 : && CONST_INT_P (XEXP (rhs, 1))
3405 : 388547 : && INTVAL (XEXP (rhs, 1)) >= 0
3406 : 38895230 : && INTVAL (XEXP (rhs, 1)) < GET_MODE_PRECISION (int_mode))
3407 : : {
3408 : 388547 : negcoeff1 = wi::set_bit_in_zero (INTVAL (XEXP (rhs, 1)),
3409 : 777094 : GET_MODE_PRECISION (int_mode));
3410 : 388547 : negcoeff1 = -negcoeff1;
3411 : 388547 : rhs = XEXP (rhs, 0);
3412 : : }
3413 : :
3414 : 38644934 : if (rtx_equal_p (lhs, rhs))
3415 : : {
3416 : 103219 : rtx orig = gen_rtx_MINUS (int_mode, op0, op1);
3417 : 103219 : rtx coeff;
3418 : 103219 : bool speed = optimize_function_for_speed_p (cfun);
3419 : :
3420 : 103219 : coeff = immed_wide_int_const (coeff0 + negcoeff1, int_mode);
3421 : :
3422 : 103219 : tem = simplify_gen_binary (MULT, int_mode, lhs, coeff);
3423 : 103219 : return (set_src_cost (tem, int_mode, speed)
3424 : 103219 : <= set_src_cost (orig, int_mode, speed) ? tem : 0);
3425 : : }
3426 : :
3427 : : /* Optimize (X + 1) * Y - Y to X * Y. */
3428 : 38541715 : lhs = op0;
3429 : 38541715 : if (GET_CODE (op0) == MULT)
3430 : : {
3431 : 188539 : if (((GET_CODE (XEXP (op0, 0)) == PLUS
3432 : 4509 : && XEXP (XEXP (op0, 0), 1) == const1_rtx)
3433 : 187168 : || (GET_CODE (XEXP (op0, 0)) == MINUS
3434 : 2261 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx))
3435 : 189910 : && rtx_equal_p (XEXP (op0, 1), op1))
3436 : 2 : lhs = XEXP (XEXP (op0, 0), 0);
3437 : 188537 : else if (((GET_CODE (XEXP (op0, 1)) == PLUS
3438 : 16 : && XEXP (XEXP (op0, 1), 1) == const1_rtx)
3439 : 188533 : || (GET_CODE (XEXP (op0, 1)) == MINUS
3440 : 46 : && XEXP (XEXP (op0, 1), 1) == constm1_rtx))
3441 : 188541 : && rtx_equal_p (XEXP (op0, 0), op1))
3442 : 0 : lhs = XEXP (XEXP (op0, 1), 0);
3443 : : }
3444 : 38541715 : if (lhs != op0)
3445 : 2 : return simplify_gen_binary (MULT, int_mode, lhs, op1);
3446 : 38644934 : }
3447 : :
3448 : : /* (a - (-b)) -> (a + b). True even for IEEE. */
3449 : 39768005 : if (GET_CODE (op1) == NEG)
3450 : 7252 : return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
3451 : :
3452 : : /* (-x - c) may be simplified as (-c - x). */
3453 : 39760753 : if (GET_CODE (op0) == NEG
3454 : 122234 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1)))
3455 : : {
3456 : 616 : tem = simplify_unary_operation (NEG, mode, op1, mode);
3457 : 616 : if (tem)
3458 : 616 : return simplify_gen_binary (MINUS, mode, tem, XEXP (op0, 0));
3459 : : }
3460 : :
3461 : 39760137 : if ((GET_CODE (op0) == CONST
3462 : 39760137 : || GET_CODE (op0) == SYMBOL_REF
3463 : 34300666 : || GET_CODE (op0) == LABEL_REF)
3464 : 39760137 : && poly_int_rtx_p (op1, &offset))
3465 : 50929 : return plus_constant (mode, op0, trunc_int_for_mode (-offset, mode));
3466 : :
3467 : : /* Don't let a relocatable value get a negative coeff. */
3468 : 39709208 : if (is_a <scalar_int_mode> (mode)
3469 : 38482947 : && poly_int_rtx_p (op1)
3470 : 46846791 : && GET_MODE (op0) != VOIDmode)
3471 : 7137583 : return simplify_gen_binary (PLUS, mode,
3472 : : op0,
3473 : 7137583 : neg_poly_int_rtx (mode, op1));
3474 : :
3475 : : /* (x - (x & y)) -> (x & ~y) */
3476 : 32571625 : if (INTEGRAL_MODE_P (mode) && GET_CODE (op1) == AND)
3477 : : {
3478 : 311458 : if (rtx_equal_p (op0, XEXP (op1, 0)))
3479 : : {
3480 : 526 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 1),
3481 : 263 : GET_MODE (XEXP (op1, 1)));
3482 : 263 : return simplify_gen_binary (AND, mode, op0, tem);
3483 : : }
3484 : 311195 : if (rtx_equal_p (op0, XEXP (op1, 1)))
3485 : : {
3486 : 2322 : tem = simplify_gen_unary (NOT, mode, XEXP (op1, 0),
3487 : 1161 : GET_MODE (XEXP (op1, 0)));
3488 : 1161 : 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 : 32570201 : if (STORE_FLAG_VALUE == 1
3495 : 32570201 : && trueop0 == const1_rtx
3496 : 1024722 : && COMPARISON_P (op1)
3497 : 32650767 : && (reversed = reversed_comparison (op1, mode)))
3498 : : return reversed;
3499 : :
3500 : : /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A). */
3501 : 32489658 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3502 : 32488279 : && GET_CODE (op1) == MULT
3503 : 32754390 : && 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 : 32489493 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
3518 : 32488114 : && GET_CODE (op1) == MULT
3519 : 32754060 : && GET_CODE (op0) == NEG)
3520 : : {
3521 : 651 : rtx in1, in2;
3522 : :
3523 : 651 : in1 = simplify_gen_unary (NEG, mode, XEXP (op1, 0), mode);
3524 : 651 : in2 = XEXP (op1, 1);
3525 : 651 : return simplify_gen_binary (MINUS, mode,
3526 : : simplify_gen_binary (MULT, mode,
3527 : : in1, in2),
3528 : 651 : 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 : 32488842 : if (INTEGRAL_MODE_P (mode)
3539 : 31673799 : && (plus_minus_operand_p (op0)
3540 : 29341981 : || plus_minus_operand_p (op1))
3541 : 3553932 : && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3542 : : return tem;
3543 : :
3544 : : /* Handle vector series. */
3545 : 29063414 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3546 : : {
3547 : 390783 : tem = simplify_binary_operation_series (code, mode, op0, op1);
3548 : 390783 : if (tem)
3549 : : return tem;
3550 : : }
3551 : : break;
3552 : :
3553 : 12078376 : case MULT:
3554 : 12078376 : if (trueop1 == constm1_rtx)
3555 : 32189 : return simplify_gen_unary (NEG, mode, op0, mode);
3556 : :
3557 : 12046187 : if (GET_CODE (op0) == NEG)
3558 : : {
3559 : 32620 : 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 : 32620 : if (temp
3565 : 1461 : && 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 : 1461 : return simplify_gen_binary (MULT, mode, XEXP (op0, 0), temp);
3573 : : }
3574 : 12044726 : if (GET_CODE (op1) == NEG)
3575 : : {
3576 : 955 : 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 : 955 : if (temp
3582 : 382 : && GET_CODE (op0) == MULT
3583 : 304 : && GET_CODE (temp) == MULT
3584 : 304 : && 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 : 377 : 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 : 12044349 : if (!HONOR_NANS (mode)
3597 : 11092379 : && !HONOR_SIGNED_ZEROS (mode)
3598 : 11092324 : && trueop1 == CONST0_RTX (mode)
3599 : 12108869 : && ! 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 : 11981052 : if (!HONOR_SNANS (mode)
3605 : 11981052 : && trueop1 == CONST1_RTX (mode))
3606 : : return op0;
3607 : :
3608 : : /* Convert multiply by constant power of two into shift. */
3609 : 11430657 : if (mem_depth == 0 && CONST_SCALAR_INT_P (trueop1))
3610 : : {
3611 : 6202603 : val = wi::exact_log2 (rtx_mode_t (trueop1, mode));
3612 : 6202603 : if (val >= 0)
3613 : 2898275 : return simplify_gen_binary (ASHIFT, mode, op0,
3614 : 2898275 : gen_int_shift_amount (mode, val));
3615 : : }
3616 : :
3617 : : /* x*2 is x+x and x*(-1) is -x */
3618 : 8532382 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
3619 : 167196 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop1))
3620 : 167196 : && !DECIMAL_FLOAT_MODE_P (GET_MODE (trueop1))
3621 : 166912 : && GET_MODE (op0) == mode)
3622 : : {
3623 : 166912 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
3624 : :
3625 : 166912 : if (real_equal (d1, &dconst2))
3626 : 613 : return simplify_gen_binary (PLUS, mode, op0, copy_rtx (op0));
3627 : :
3628 : 166299 : if (!HONOR_SNANS (mode)
3629 : 166299 : && real_equal (d1, &dconstm1))
3630 : 24 : return simplify_gen_unary (NEG, mode, op0, mode);
3631 : : }
3632 : :
3633 : : /* Optimize -x * -x as x * x. */
3634 : 8531745 : if (FLOAT_MODE_P (mode)
3635 : 1369885 : && GET_CODE (op0) == NEG
3636 : 7855 : && 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 : 8531745 : if (SCALAR_FLOAT_MODE_P (mode)
3643 : 1085025 : && GET_CODE (op0) == ABS
3644 : 1340 : && GET_CODE (op1) == ABS
3645 : 0 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
3646 : 8531745 : && !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 : 8531745 : if (! FLOAT_MODE_P (mode)
3652 : 1369885 : || flag_unsafe_math_optimizations)
3653 : : {
3654 : 7580934 : tem = simplify_associative_operation (code, mode, op0, op1);
3655 : 7580934 : if (tem)
3656 : : return tem;
3657 : : }
3658 : : break;
3659 : :
3660 : 17122431 : case IOR:
3661 : 17122431 : if (trueop1 == CONST0_RTX (mode))
3662 : : return op0;
3663 : 16214922 : if (INTEGRAL_MODE_P (mode)
3664 : 15978694 : && trueop1 == CONSTM1_RTX (mode)
3665 : 9465 : && !side_effects_p (op0))
3666 : : return op1;
3667 : 16205457 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
3668 : : return op0;
3669 : : /* A | (~A) -> -1 */
3670 : 73708 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
3671 : 16185475 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
3672 : 10 : && ! side_effects_p (op0)
3673 : 16185495 : && GET_MODE_CLASS (mode) != MODE_CC)
3674 : 10 : return CONSTM1_RTX (mode);
3675 : :
3676 : : /* Convert (ior (plus (A - 1)) (neg A)) to -1. */
3677 : 16185475 : if (match_plus_neg_pattern (op0, op1, mode))
3678 : 0 : return CONSTM1_RTX (mode);
3679 : :
3680 : : /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
3681 : 16185475 : if (CONST_INT_P (op1)
3682 : 4333361 : && HWI_COMPUTABLE_MODE_P (mode)
3683 : 4272124 : && (nonzero_bits (op0, mode) & ~UINTVAL (op1)) == 0
3684 : 16547503 : && !side_effects_p (op0))
3685 : : return op1;
3686 : :
3687 : : /* Canonicalize (X & C1) | C2. */
3688 : 15823447 : if (GET_CODE (op0) == AND
3689 : 4485837 : && CONST_INT_P (trueop1)
3690 : 751725 : && CONST_INT_P (XEXP (op0, 1)))
3691 : : {
3692 : 596785 : HOST_WIDE_INT mask = GET_MODE_MASK (mode);
3693 : 596785 : HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
3694 : 596785 : HOST_WIDE_INT c2 = INTVAL (trueop1);
3695 : :
3696 : : /* If (C1&C2) == C1, then (X&C1)|C2 becomes C2. */
3697 : 596785 : if ((c1 & c2) == c1
3698 : 596785 : && !side_effects_p (XEXP (op0, 0)))
3699 : : return trueop1;
3700 : :
3701 : : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
3702 : 596785 : if (((c1|c2) & mask) == mask)
3703 : 81280 : return simplify_gen_binary (IOR, mode, XEXP (op0, 0), op1);
3704 : :
3705 : : /* If (C1|C2) has a single bit clear, then adjust C1 so that
3706 : : when split it'll match a single bit clear style insn.
3707 : :
3708 : : This could have been done with a target dependent splitter, but
3709 : : then every target with single bit manipulation insns would need
3710 : : to implement such splitters. */
3711 : 515505 : if (exact_log2 (~(c1 | c2)) >= 0)
3712 : : {
3713 : 34191 : rtx temp = gen_rtx_AND (mode, XEXP (op0, 0), GEN_INT (c1 | c2));
3714 : 34191 : temp = gen_rtx_IOR (mode, temp, trueop1);
3715 : 34191 : return temp;
3716 : : }
3717 : : }
3718 : :
3719 : : /* Convert (A & B) | A to A. */
3720 : 15707976 : if (GET_CODE (op0) == AND
3721 : 4370366 : && (rtx_equal_p (XEXP (op0, 0), op1)
3722 : 4370259 : || rtx_equal_p (XEXP (op0, 1), op1))
3723 : 3754 : && ! side_effects_p (XEXP (op0, 0))
3724 : 15711730 : && ! side_effects_p (XEXP (op0, 1)))
3725 : : return op1;
3726 : :
3727 : : /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
3728 : : mode size to (rotate A CX). */
3729 : 15704222 : tem = simplify_rotate_op (op0, op1, mode);
3730 : 15704222 : if (tem)
3731 : : return tem;
3732 : :
3733 : : /* If OP0 is (ashiftrt (plus ...) C), it might actually be
3734 : : a (sign_extend (plus ...)). Then check if OP1 is a CONST_INT and
3735 : : the PLUS does not affect any of the bits in OP1: then we can do
3736 : : the IOR as a PLUS and we can associate. This is valid if OP1
3737 : : can be safely shifted left C bits. */
3738 : 15701697 : if (CONST_INT_P (trueop1) && GET_CODE (op0) == ASHIFTRT
3739 : 6630 : && GET_CODE (XEXP (op0, 0)) == PLUS
3740 : 141 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
3741 : 87 : && CONST_INT_P (XEXP (op0, 1))
3742 : 87 : && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
3743 : : {
3744 : 87 : int count = INTVAL (XEXP (op0, 1));
3745 : 87 : HOST_WIDE_INT mask = UINTVAL (trueop1) << count;
3746 : :
3747 : 87 : if (mask >> count == INTVAL (trueop1)
3748 : 80 : && trunc_int_for_mode (mask, mode) == mask
3749 : 154 : && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
3750 : 0 : return simplify_gen_binary (ASHIFTRT, mode,
3751 : : plus_constant (mode, XEXP (op0, 0),
3752 : 0 : mask),
3753 : : XEXP (op0, 1));
3754 : : }
3755 : :
3756 : : /* The following happens with bitfield merging.
3757 : : (X & C) | ((X | Y) & ~C) -> X | (Y & ~C) */
3758 : 15701697 : if (GET_CODE (op0) == AND
3759 : 4366612 : && GET_CODE (op1) == AND
3760 : 338632 : && CONST_INT_P (XEXP (op0, 1))
3761 : 177926 : && CONST_INT_P (XEXP (op1, 1))
3762 : 172315 : && (INTVAL (XEXP (op0, 1))
3763 : 172315 : == ~INTVAL (XEXP (op1, 1))))
3764 : : {
3765 : : /* The IOR may be on both sides. */
3766 : 38035 : rtx top0 = NULL_RTX, top1 = NULL_RTX;
3767 : 38035 : if (GET_CODE (XEXP (op1, 0)) == IOR)
3768 : : top0 = op0, top1 = op1;
3769 : 37981 : else if (GET_CODE (XEXP (op0, 0)) == IOR)
3770 : 3 : top0 = op1, top1 = op0;
3771 : 38035 : if (top0 && top1)
3772 : : {
3773 : : /* X may be on either side of the inner IOR. */
3774 : 57 : rtx tem = NULL_RTX;
3775 : 57 : if (rtx_equal_p (XEXP (top0, 0),
3776 : 57 : XEXP (XEXP (top1, 0), 0)))
3777 : 43 : tem = XEXP (XEXP (top1, 0), 1);
3778 : 14 : else if (rtx_equal_p (XEXP (top0, 0),
3779 : 14 : XEXP (XEXP (top1, 0), 1)))
3780 : 5 : tem = XEXP (XEXP (top1, 0), 0);
3781 : 48 : if (tem)
3782 : 48 : return simplify_gen_binary (IOR, mode, XEXP (top0, 0),
3783 : : simplify_gen_binary
3784 : 48 : (AND, mode, tem, XEXP (top1, 1)));
3785 : : }
3786 : : }
3787 : :
3788 : : /* Convert (ior (and A C) (and B C)) into (and (ior A B) C). */
3789 : 15701649 : if (GET_CODE (op0) == GET_CODE (op1)
3790 : 3850423 : && (GET_CODE (op0) == AND
3791 : : || GET_CODE (op0) == IOR
3792 : 3850423 : || GET_CODE (op0) == LSHIFTRT
3793 : 3509941 : || GET_CODE (op0) == ASHIFTRT
3794 : 3509848 : || GET_CODE (op0) == ASHIFT
3795 : 3483304 : || GET_CODE (op0) == ROTATE
3796 : 3483304 : || GET_CODE (op0) == ROTATERT))
3797 : : {
3798 : 367119 : tem = simplify_distributive_operation (code, mode, op0, op1);
3799 : 367119 : if (tem)
3800 : : return tem;
3801 : : }
3802 : :
3803 : : /* Convert (ior (and (not A) B) A) into A | B. */
3804 : 15616480 : if (GET_CODE (op0) == AND
3805 : 4281535 : && GET_CODE (XEXP (op0, 0)) == NOT
3806 : 15768202 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1))
3807 : 3366 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
3808 : :
3809 : 15613114 : tem = simplify_with_subreg_not (code, mode, op0, op1);
3810 : 15613114 : if (tem)
3811 : : return tem;
3812 : :
3813 : 15613109 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
3814 : 15613109 : if (tem)
3815 : : return tem;
3816 : :
3817 : 15613079 : tem = simplify_associative_operation (code, mode, op0, op1);
3818 : 15613079 : if (tem)
3819 : : return tem;
3820 : :
3821 : 15300308 : tem = simplify_logical_relational_operation (code, mode, op0, op1);
3822 : 15300308 : if (tem)
3823 : : return tem;
3824 : : break;
3825 : :
3826 : 1978979 : case XOR:
3827 : 1978979 : if (trueop1 == CONST0_RTX (mode))
3828 : : return op0;
3829 : 1903467 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
3830 : 23651 : return simplify_gen_unary (NOT, mode, op0, mode);
3831 : 1879816 : if (rtx_equal_p (trueop0, trueop1)
3832 : 2586 : && ! side_effects_p (op0)
3833 : 1882402 : && GET_MODE_CLASS (mode) != MODE_CC)
3834 : 2586 : return CONST0_RTX (mode);
3835 : :
3836 : : /* Canonicalize XOR of the most significant bit to PLUS. */
3837 : 1877230 : if (CONST_SCALAR_INT_P (op1)
3838 : 1877230 : && mode_signbit_p (mode, op1))
3839 : 41371 : return simplify_gen_binary (PLUS, mode, op0, op1);
3840 : : /* (xor (plus X C1) C2) is (xor X (C1^C2)) if C1 is signbit. */
3841 : 1835859 : if (CONST_SCALAR_INT_P (op1)
3842 : 702305 : && GET_CODE (op0) == PLUS
3843 : 2493 : && CONST_SCALAR_INT_P (XEXP (op0, 1))
3844 : 1837445 : && mode_signbit_p (mode, XEXP (op0, 1)))
3845 : 189 : return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
3846 : : simplify_gen_binary (XOR, mode, op1,
3847 : 189 : XEXP (op0, 1)));
3848 : :
3849 : : /* If we are XORing two things that have no bits in common,
3850 : : convert them into an IOR. This helps to detect rotation encoded
3851 : : using those methods and possibly other simplifications. */
3852 : :
3853 : 1835670 : if (HWI_COMPUTABLE_MODE_P (mode)
3854 : 1564695 : && (nonzero_bits (op0, mode)
3855 : 1564695 : & nonzero_bits (op1, mode)) == 0)
3856 : 12296 : return (simplify_gen_binary (IOR, mode, op0, op1));
3857 : :
3858 : : /* Convert (xor (plus (A - 1)) (neg A)) to -1. */
3859 : 1823374 : if (match_plus_neg_pattern (op0, op1, mode))
3860 : 0 : return CONSTM1_RTX (mode);
3861 : :
3862 : : /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
3863 : : Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
3864 : : (NOT y). */
3865 : 1823374 : {
3866 : 1823374 : int num_negated = 0;
3867 : :
3868 : 1823374 : if (GET_CODE (op0) == NOT)
3869 : 1029 : num_negated++, op0 = XEXP (op0, 0);
3870 : 1823374 : if (GET_CODE (op1) == NOT)
3871 : 0 : num_negated++, op1 = XEXP (op1, 0);
3872 : :
3873 : 0 : if (num_negated == 2)
3874 : 0 : return simplify_gen_binary (XOR, mode, op0, op1);
3875 : 1823374 : else if (num_negated == 1)
3876 : 1029 : return simplify_gen_unary (NOT, mode,
3877 : : simplify_gen_binary (XOR, mode, op0, op1),
3878 : 1029 : mode);
3879 : : }
3880 : :
3881 : : /* Convert (xor (and A B) B) to (and (not A) B). The latter may
3882 : : correspond to a machine insn or result in further simplifications
3883 : : if B is a constant. */
3884 : :
3885 : 1822345 : if (GET_CODE (op0) == AND
3886 : 173014 : && rtx_equal_p (XEXP (op0, 1), op1)
3887 : 1856078 : && ! side_effects_p (op1))
3888 : 33733 : return simplify_gen_binary (AND, mode,
3889 : : simplify_gen_unary (NOT, mode,
3890 : : XEXP (op0, 0), mode),
3891 : 33733 : op1);
3892 : :
3893 : 1788612 : else if (GET_CODE (op0) == AND
3894 : 139281 : && rtx_equal_p (XEXP (op0, 0), op1)
3895 : 1789843 : && ! side_effects_p (op1))
3896 : 1231 : return simplify_gen_binary (AND, mode,
3897 : : simplify_gen_unary (NOT, mode,
3898 : : XEXP (op0, 1), mode),
3899 : 1231 : op1);
3900 : :
3901 : : /* Given (xor (ior (xor A B) C) D), where B, C and D are
3902 : : constants, simplify to (xor (ior A C) (B&~C)^D), canceling
3903 : : out bits inverted twice and not set by C. Similarly, given
3904 : : (xor (and (xor A B) C) D), simplify without inverting C in
3905 : : the xor operand: (xor (and A C) (B&C)^D).
3906 : : */
3907 : 1787381 : else if ((GET_CODE (op0) == IOR || GET_CODE (op0) == AND)
3908 : 159452 : && GET_CODE (XEXP (op0, 0)) == XOR
3909 : 5856 : && CONST_INT_P (op1)
3910 : 83 : && CONST_INT_P (XEXP (op0, 1))
3911 : 38 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1)))
3912 : : {
3913 : 38 : enum rtx_code op = GET_CODE (op0);
3914 : 38 : rtx a = XEXP (XEXP (op0, 0), 0);
3915 : 38 : rtx b = XEXP (XEXP (op0, 0), 1);
3916 : 38 : rtx c = XEXP (op0, 1);
3917 : 38 : rtx d = op1;
3918 : 38 : HOST_WIDE_INT bval = INTVAL (b);
3919 : 38 : HOST_WIDE_INT cval = INTVAL (c);
3920 : 38 : HOST_WIDE_INT dval = INTVAL (d);
3921 : 38 : HOST_WIDE_INT xcval;
3922 : :
3923 : 38 : if (op == IOR)
3924 : 8 : xcval = ~cval;
3925 : : else
3926 : : xcval = cval;
3927 : :
3928 : 38 : return simplify_gen_binary (XOR, mode,
3929 : : simplify_gen_binary (op, mode, a, c),
3930 : 38 : gen_int_mode ((bval & xcval) ^ dval,
3931 : : mode));
3932 : : }
3933 : :
3934 : : /* Given (xor (and A B) C), using P^Q == (~P&Q) | (~Q&P),
3935 : : we can transform like this:
3936 : : (A&B)^C == ~(A&B)&C | ~C&(A&B)
3937 : : == (~A|~B)&C | ~C&(A&B) * DeMorgan's Law
3938 : : == ~A&C | ~B&C | A&(~C&B) * Distribute and re-order
3939 : : Attempt a few simplifications when B and C are both constants. */
3940 : 1787343 : if (GET_CODE (op0) == AND
3941 : 138020 : && CONST_INT_P (op1)
3942 : 12890 : && CONST_INT_P (XEXP (op0, 1)))
3943 : : {
3944 : 11018 : rtx a = XEXP (op0, 0);
3945 : 11018 : rtx b = XEXP (op0, 1);
3946 : 11018 : rtx c = op1;
3947 : 11018 : HOST_WIDE_INT bval = INTVAL (b);
3948 : 11018 : HOST_WIDE_INT cval = INTVAL (c);
3949 : :
3950 : : /* Instead of computing ~A&C, we compute its negated value,
3951 : : ~(A|~C). If it yields -1, ~A&C is zero, so we can
3952 : : optimize for sure. If it does not simplify, we still try
3953 : : to compute ~A&C below, but since that always allocates
3954 : : RTL, we don't try that before committing to returning a
3955 : : simplified expression. */
3956 : 11018 : rtx n_na_c = simplify_binary_operation (IOR, mode, a,
3957 : : GEN_INT (~cval));
3958 : :
3959 : 11018 : if ((~cval & bval) == 0)
3960 : : {
3961 : 437 : rtx na_c = NULL_RTX;
3962 : 437 : if (n_na_c)
3963 : 0 : na_c = simplify_gen_unary (NOT, mode, n_na_c, mode);
3964 : : else
3965 : : {
3966 : : /* If ~A does not simplify, don't bother: we don't
3967 : : want to simplify 2 operations into 3, and if na_c
3968 : : were to simplify with na, n_na_c would have
3969 : : simplified as well. */
3970 : 437 : rtx na = simplify_unary_operation (NOT, mode, a, mode);
3971 : 437 : if (na)
3972 : 0 : na_c = simplify_gen_binary (AND, mode, na, c);
3973 : : }
3974 : :
3975 : : /* Try to simplify ~A&C | ~B&C. */
3976 : 0 : if (na_c != NULL_RTX)
3977 : 0 : return simplify_gen_binary (IOR, mode, na_c,
3978 : 0 : gen_int_mode (~bval & cval, mode));
3979 : : }
3980 : : else
3981 : : {
3982 : : /* If ~A&C is zero, simplify A&(~C&B) | ~B&C. */
3983 : 10581 : if (n_na_c == CONSTM1_RTX (mode))
3984 : : {
3985 : 0 : rtx a_nc_b = simplify_gen_binary (AND, mode, a,
3986 : 0 : gen_int_mode (~cval & bval,
3987 : : mode));
3988 : 0 : return simplify_gen_binary (IOR, mode, a_nc_b,
3989 : 0 : gen_int_mode (~bval & cval,
3990 : : mode));
3991 : : }
3992 : : }
3993 : : }
3994 : :
3995 : : /* If we have (xor (and (xor A B) C) A) with C a constant we can instead
3996 : : do (ior (and A ~C) (and B C)) which is a machine instruction on some
3997 : : machines, and also has shorter instruction path length. */
3998 : 1787343 : if (GET_CODE (op0) == AND
3999 : 138020 : && GET_CODE (XEXP (op0, 0)) == XOR
4000 : 5358 : && CONST_INT_P (XEXP (op0, 1))
4001 : 1789481 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), trueop1))
4002 : : {
4003 : 7 : rtx a = trueop1;
4004 : 7 : rtx b = XEXP (XEXP (op0, 0), 1);
4005 : 7 : rtx c = XEXP (op0, 1);
4006 : 7 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
4007 : 7 : rtx a_nc = simplify_gen_binary (AND, mode, a, nc);
4008 : 7 : rtx bc = simplify_gen_binary (AND, mode, b, c);
4009 : 7 : return simplify_gen_binary (IOR, mode, a_nc, bc);
4010 : : }
4011 : : /* Similarly, (xor (and (xor A B) C) B) as (ior (and A C) (and B ~C)) */
4012 : 1787336 : else if (GET_CODE (op0) == AND
4013 : 138013 : && GET_CODE (XEXP (op0, 0)) == XOR
4014 : 5351 : && CONST_INT_P (XEXP (op0, 1))
4015 : 1789467 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), trueop1))
4016 : : {
4017 : 8 : rtx a = XEXP (XEXP (op0, 0), 0);
4018 : 8 : rtx b = trueop1;
4019 : 8 : rtx c = XEXP (op0, 1);
4020 : 8 : rtx nc = simplify_gen_unary (NOT, mode, c, mode);
4021 : 8 : rtx b_nc = simplify_gen_binary (AND, mode, b, nc);
4022 : 8 : rtx ac = simplify_gen_binary (AND, mode, a, c);
4023 : 8 : return simplify_gen_binary (IOR, mode, ac, b_nc);
4024 : : }
4025 : :
4026 : : /* (xor (comparison foo bar) (const_int 1)) can become the reversed
4027 : : comparison if STORE_FLAG_VALUE is 1. */
4028 : 1787328 : if (STORE_FLAG_VALUE == 1
4029 : 1787328 : && trueop1 == const1_rtx
4030 : 269033 : && COMPARISON_P (op0)
4031 : 1796036 : && (reversed = reversed_comparison (op0, mode)))
4032 : : return reversed;
4033 : :
4034 : : /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4035 : : is (lt foo (const_int 0)), so we can perform the above
4036 : : simplification if STORE_FLAG_VALUE is 1. */
4037 : :
4038 : 1778708 : if (is_a <scalar_int_mode> (mode, &int_mode)
4039 : : && STORE_FLAG_VALUE == 1
4040 : 1511921 : && trueop1 == const1_rtx
4041 : 260413 : && GET_CODE (op0) == LSHIFTRT
4042 : 35466 : && CONST_INT_P (XEXP (op0, 1))
4043 : 35466 : && INTVAL (XEXP (op0, 1)) == GET_MODE_PRECISION (int_mode) - 1)
4044 : 34614 : return gen_rtx_GE (int_mode, XEXP (op0, 0), const0_rtx);
4045 : :
4046 : : /* (xor (comparison foo bar) (const_int sign-bit))
4047 : : when STORE_FLAG_VALUE is the sign bit. */
4048 : 1744094 : if (is_a <scalar_int_mode> (mode, &int_mode)
4049 : 1477307 : && val_signbit_p (int_mode, STORE_FLAG_VALUE)
4050 : 0 : && trueop1 == const_true_rtx
4051 : 0 : && COMPARISON_P (op0)
4052 : 0 : && (reversed = reversed_comparison (op0, int_mode)))
4053 : : return reversed;
4054 : :
4055 : : /* Convert (xor (and A C) (and B C)) into (and (xor A B) C). */
4056 : 1744094 : if (GET_CODE (op0) == GET_CODE (op1)
4057 : 535833 : && (GET_CODE (op0) == AND
4058 : 535833 : || GET_CODE (op0) == LSHIFTRT
4059 : 474186 : || GET_CODE (op0) == ASHIFTRT
4060 : 474135 : || GET_CODE (op0) == ASHIFT
4061 : 474019 : || GET_CODE (op0) == ROTATE
4062 : 473919 : || GET_CODE (op0) == ROTATERT))
4063 : : {
4064 : 62446 : tem = simplify_distributive_operation (code, mode, op0, op1);
4065 : 62446 : if (tem)
4066 : : return tem;
4067 : : }
4068 : :
4069 : : /* Convert (xor (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4070 : : mode size to (rotate A CX). */
4071 : 1685976 : tem = simplify_rotate_op (op0, op1, mode);
4072 : 1685976 : if (tem)
4073 : : return tem;
4074 : :
4075 : : /* Convert (xor (and (not A) B) A) into A | B. */
4076 : 1684596 : if (GET_CODE (op0) == AND
4077 : 80178 : && GET_CODE (XEXP (op0, 0)) == NOT
4078 : 1689183 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1))
4079 : 1 : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4080 : :
4081 : : /* Convert (xor (and (rotate (~1) A) B) (ashift 1 A))
4082 : : into B | (1 << A). */
4083 : 1684595 : if (SHIFT_COUNT_TRUNCATED
4084 : : && GET_CODE (op0) == AND
4085 : : && GET_CODE (XEXP (op0, 0)) == ROTATE
4086 : : && CONST_INT_P (XEXP (XEXP (op0, 0), 0))
4087 : : && INTVAL (XEXP (XEXP (op0, 0), 0)) == -2
4088 : : && GET_CODE (op1) == ASHIFT
4089 : : && CONST_INT_P (XEXP (op1, 0))
4090 : : && INTVAL (XEXP (op1, 0)) == 1
4091 : : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), XEXP (op1, 1))
4092 : : && !side_effects_p (XEXP (op1, 1)))
4093 : : return simplify_gen_binary (IOR, mode, XEXP (op0, 1), op1);
4094 : :
4095 : 1684595 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4096 : 1684595 : if (tem)
4097 : : return tem;
4098 : :
4099 : 1684595 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4100 : 1684595 : if (tem)
4101 : : return tem;
4102 : :
4103 : 1684595 : tem = simplify_associative_operation (code, mode, op0, op1);
4104 : 1684595 : if (tem)
4105 : : return tem;
4106 : : break;
4107 : :
4108 : 24971307 : case AND:
4109 : 24971307 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
4110 : : return trueop1;
4111 : 24711723 : if (INTEGRAL_MODE_P (mode) && trueop1 == CONSTM1_RTX (mode))
4112 : : return op0;
4113 : 24323465 : if (HWI_COMPUTABLE_MODE_P (mode))
4114 : : {
4115 : : /* When WORD_REGISTER_OPERATIONS is true, we need to know the
4116 : : nonzero bits in WORD_MODE rather than MODE. */
4117 : 21468872 : scalar_int_mode tmode = as_a <scalar_int_mode> (mode);
4118 : 21468872 : if (WORD_REGISTER_OPERATIONS
4119 : : && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
4120 : : tmode = word_mode;
4121 : 21468872 : HOST_WIDE_INT nzop0 = nonzero_bits (trueop0, tmode);
4122 : 21468872 : HOST_WIDE_INT nzop1;
4123 : 21468872 : if (CONST_INT_P (trueop1))
4124 : : {
4125 : 18346520 : HOST_WIDE_INT val1 = INTVAL (trueop1);
4126 : : /* If we are turning off bits already known off in OP0, we need
4127 : : not do an AND. */
4128 : 18346520 : if ((nzop0 & ~val1) == 0)
4129 : 414361 : return op0;
4130 : : }
4131 : 21117030 : nzop1 = nonzero_bits (trueop1, mode);
4132 : : /* If we are clearing all the nonzero bits, the result is zero. */
4133 : 21117030 : if ((nzop1 & nzop0) == 0
4134 : 21117030 : && !side_effects_p (op0) && !side_effects_p (op1))
4135 : 62519 : return CONST0_RTX (mode);
4136 : : }
4137 : 23912438 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0)
4138 : 23912438 : && GET_MODE_CLASS (mode) != MODE_CC)
4139 : : return op0;
4140 : : /* A & (~A) -> 0 */
4141 : 658024 : if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
4142 : 23901815 : || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
4143 : 4001 : && ! side_effects_p (op0)
4144 : 23909771 : && GET_MODE_CLASS (mode) != MODE_CC)
4145 : 4001 : return CONST0_RTX (mode);
4146 : :
4147 : : /* Convert (and (plus (A - 1)) (neg A)) to 0. */
4148 : 23901769 : if (match_plus_neg_pattern (op0, op1, mode))
4149 : 2 : return CONST0_RTX (mode);
4150 : :
4151 : : /* Transform (and (extend X) C) into (zero_extend (and X C)) if
4152 : : there are no nonzero bits of C outside of X's mode. */
4153 : 47803534 : if ((GET_CODE (op0) == SIGN_EXTEND
4154 : 23901767 : || GET_CODE (op0) == ZERO_EXTEND)
4155 : 100959 : && CONST_SCALAR_INT_P (trueop1)
4156 : 86405 : && is_a <scalar_int_mode> (mode, &int_mode)
4157 : 86405 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
4158 : 23988172 : && (wi::mask (GET_MODE_PRECISION (inner_mode), true,
4159 : 86405 : GET_MODE_PRECISION (int_mode))
4160 : 23988172 : & rtx_mode_t (trueop1, mode)) == 0)
4161 : : {
4162 : 83847 : machine_mode imode = GET_MODE (XEXP (op0, 0));
4163 : 83847 : tem = immed_wide_int_const (rtx_mode_t (trueop1, mode), imode);
4164 : 83847 : tem = simplify_gen_binary (AND, imode, XEXP (op0, 0), tem);
4165 : 83847 : return simplify_gen_unary (ZERO_EXTEND, mode, tem, imode);
4166 : : }
4167 : :
4168 : : /* Transform (and (truncate X) C) into (truncate (and X C)). This way
4169 : : we might be able to further simplify the AND with X and potentially
4170 : : remove the truncation altogether. */
4171 : 23817920 : if (GET_CODE (op0) == TRUNCATE && CONST_INT_P (trueop1))
4172 : : {
4173 : 6 : rtx x = XEXP (op0, 0);
4174 : 6 : machine_mode xmode = GET_MODE (x);
4175 : 6 : tem = simplify_gen_binary (AND, xmode, x,
4176 : 6 : gen_int_mode (INTVAL (trueop1), xmode));
4177 : 6 : return simplify_gen_unary (TRUNCATE, mode, tem, xmode);
4178 : : }
4179 : :
4180 : : /* Canonicalize (A | C1) & C2 as (A & C2) | (C1 & C2). */
4181 : 23817914 : if (GET_CODE (op0) == IOR
4182 : 1560696 : && CONST_INT_P (trueop1)
4183 : 241298 : && CONST_INT_P (XEXP (op0, 1)))
4184 : : {
4185 : 148724 : HOST_WIDE_INT tmp = INTVAL (trueop1) & INTVAL (XEXP (op0, 1));
4186 : 148724 : return simplify_gen_binary (IOR, mode,
4187 : : simplify_gen_binary (AND, mode,
4188 : : XEXP (op0, 0), op1),
4189 : 148724 : gen_int_mode (tmp, mode));
4190 : : }
4191 : :
4192 : : /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
4193 : : insn (and may simplify more). */
4194 : 23669190 : if (GET_CODE (op0) == XOR
4195 : 125923 : && rtx_equal_p (XEXP (op0, 0), op1)
4196 : 23670588 : && ! side_effects_p (op1))
4197 : 1398 : return simplify_gen_binary (AND, mode,
4198 : : simplify_gen_unary (NOT, mode,
4199 : : XEXP (op0, 1), mode),
4200 : 1398 : op1);
4201 : :
4202 : 23667792 : if (GET_CODE (op0) == XOR
4203 : 124525 : && rtx_equal_p (XEXP (op0, 1), op1)
4204 : 23671350 : && ! side_effects_p (op1))
4205 : 3558 : return simplify_gen_binary (AND, mode,
4206 : : simplify_gen_unary (NOT, mode,
4207 : : XEXP (op0, 0), mode),
4208 : 3558 : op1);
4209 : :
4210 : : /* Similarly for (~(A ^ B)) & A. */
4211 : 23664234 : if (GET_CODE (op0) == NOT
4212 : 654069 : && GET_CODE (XEXP (op0, 0)) == XOR
4213 : 3351 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4214 : 23664288 : && ! side_effects_p (op1))
4215 : 54 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4216 : :
4217 : 23664180 : if (GET_CODE (op0) == NOT
4218 : 654015 : && GET_CODE (XEXP (op0, 0)) == XOR
4219 : 3297 : && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4220 : 23664217 : && ! side_effects_p (op1))
4221 : 37 : return simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4222 : :
4223 : : /* Convert (A | B) & A to A. */
4224 : 23664143 : if (GET_CODE (op0) == IOR
4225 : 1411972 : && (rtx_equal_p (XEXP (op0, 0), op1)
4226 : 1411424 : || rtx_equal_p (XEXP (op0, 1), op1))
4227 : 722 : && ! side_effects_p (XEXP (op0, 0))
4228 : 23664865 : && ! side_effects_p (XEXP (op0, 1)))
4229 : : return op1;
4230 : :
4231 : : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
4232 : : ((A & N) + B) & M -> (A + B) & M
4233 : : Similarly if (N & M) == 0,
4234 : : ((A | N) + B) & M -> (A + B) & M
4235 : : and for - instead of + and/or ^ instead of |.
4236 : : Also, if (N & M) == 0, then
4237 : : (A +- N) & M -> A & M. */
4238 : 23663421 : if (CONST_INT_P (trueop1)
4239 : 17812094 : && HWI_COMPUTABLE_MODE_P (mode)
4240 : 17776125 : && ~UINTVAL (trueop1)
4241 : 17776125 : && (UINTVAL (trueop1) & (UINTVAL (trueop1) + 1)) == 0
4242 : 34840444 : && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS))
4243 : : {
4244 : 994344 : rtx pmop[2];
4245 : 994344 : int which;
4246 : :
4247 : 994344 : pmop[0] = XEXP (op0, 0);
4248 : 994344 : pmop[1] = XEXP (op0, 1);
4249 : :
4250 : 994344 : if (CONST_INT_P (pmop[1])
4251 : 538301 : && (UINTVAL (pmop[1]) & UINTVAL (trueop1)) == 0)
4252 : 164121 : return simplify_gen_binary (AND, mode, pmop[0], op1);
4253 : :
4254 : 2515269 : for (which = 0; which < 2; which++)
4255 : : {
4256 : 1676846 : tem = pmop[which];
4257 : 1676846 : switch (GET_CODE (tem))
4258 : : {
4259 : 12440 : case AND:
4260 : 12440 : if (CONST_INT_P (XEXP (tem, 1))
4261 : 10954 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1))
4262 : : == UINTVAL (trueop1))
4263 : 8061 : pmop[which] = XEXP (tem, 0);
4264 : : break;
4265 : 1736 : case IOR:
4266 : 1736 : case XOR:
4267 : 1736 : if (CONST_INT_P (XEXP (tem, 1))
4268 : 725 : && (UINTVAL (XEXP (tem, 1)) & UINTVAL (trueop1)) == 0)
4269 : 139 : pmop[which] = XEXP (tem, 0);
4270 : : break;
4271 : : default:
4272 : : break;
4273 : : }
4274 : : }
4275 : :
4276 : 838423 : if (pmop[0] != XEXP (op0, 0) || pmop[1] != XEXP (op0, 1))
4277 : : {
4278 : 8200 : tem = simplify_gen_binary (GET_CODE (op0), mode,
4279 : : pmop[0], pmop[1]);
4280 : 8200 : return simplify_gen_binary (code, mode, tem, op1);
4281 : : }
4282 : : }
4283 : :
4284 : : /* (and X (ior (not X) Y) -> (and X Y) */
4285 : 23499300 : if (GET_CODE (op1) == IOR
4286 : 1085691 : && GET_CODE (XEXP (op1, 0)) == NOT
4287 : 23504702 : && rtx_equal_p (op0, XEXP (XEXP (op1, 0), 0)))
4288 : 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 1));
4289 : :
4290 : : /* (and (ior (not X) Y) X) -> (and X Y) */
4291 : 23499300 : if (GET_CODE (op0) == IOR
4292 : 1411250 : && GET_CODE (XEXP (op0, 0)) == NOT
4293 : 23549436 : && rtx_equal_p (op1, XEXP (XEXP (op0, 0), 0)))
4294 : 6 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 1));
4295 : :
4296 : : /* (and X (ior Y (not X)) -> (and X Y) */
4297 : 23499294 : if (GET_CODE (op1) == IOR
4298 : 1085691 : && GET_CODE (XEXP (op1, 1)) == NOT
4299 : 23499559 : && rtx_equal_p (op0, XEXP (XEXP (op1, 1), 0)))
4300 : 0 : return simplify_gen_binary (AND, mode, op0, XEXP (op1, 0));
4301 : :
4302 : : /* (and (ior Y (not X)) X) -> (and X Y) */
4303 : 23499294 : if (GET_CODE (op0) == IOR
4304 : 1411244 : && GET_CODE (XEXP (op0, 1)) == NOT
4305 : 23507727 : && rtx_equal_p (op1, XEXP (XEXP (op0, 1), 0)))
4306 : 6 : return simplify_gen_binary (AND, mode, op1, XEXP (op0, 0));
4307 : :
4308 : : /* (and (ior/xor (X Y) (not Y)) -> X & ~Y */
4309 : 23499288 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4310 : 1532205 : && GET_CODE (op1) == NOT
4311 : 23611185 : && rtx_equal_p (XEXP (op1, 0), XEXP (op0, 1)))
4312 : 23 : return simplify_gen_binary (AND, mode, XEXP (op0, 0),
4313 : : simplify_gen_unary (NOT, mode,
4314 : : XEXP (op1, 0),
4315 : 23 : mode));
4316 : : /* (and (ior/xor (Y X) (not Y)) -> X & ~Y */
4317 : 23499265 : if ((GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4318 : 1532182 : && GET_CODE (op1) == NOT
4319 : 23611139 : && rtx_equal_p (XEXP (op1, 0), XEXP (op0, 0)))
4320 : 4 : return simplify_gen_binary (AND, mode, XEXP (op0, 1),
4321 : : simplify_gen_unary (NOT, mode,
4322 : : XEXP (op1, 0),
4323 : 4 : mode));
4324 : :
4325 : : /* Convert (and (ior A C) (ior B C)) into (ior (and A B) C). */
4326 : 23499261 : if (GET_CODE (op0) == GET_CODE (op1)
4327 : 2251221 : && (GET_CODE (op0) == AND
4328 : : || GET_CODE (op0) == IOR
4329 : 2251221 : || GET_CODE (op0) == LSHIFTRT
4330 : 1166231 : || GET_CODE (op0) == ASHIFTRT
4331 : 1166134 : || GET_CODE (op0) == ASHIFT
4332 : 1166005 : || GET_CODE (op0) == ROTATE
4333 : 1166005 : || GET_CODE (op0) == ROTATERT))
4334 : : {
4335 : 1085216 : tem = simplify_distributive_operation (code, mode, op0, op1);
4336 : 1085216 : if (tem)
4337 : : return tem;
4338 : : }
4339 : :
4340 : : /* (and:v4si
4341 : : (ashiftrt:v4si A 16)
4342 : : (const_vector: 0xffff x4))
4343 : : is just (lshiftrt:v4si A 16). */
4344 : 22460936 : if (VECTOR_MODE_P (mode) && GET_CODE (op0) == ASHIFTRT
4345 : 4588 : && (CONST_INT_P (XEXP (op0, 1))
4346 : 1850 : || (GET_CODE (XEXP (op0, 1)) == CONST_VECTOR
4347 : 92 : && const_vec_duplicate_p (XEXP (op0, 1))
4348 : 0 : && CONST_INT_P (XVECEXP (XEXP (op0, 1), 0, 0))))
4349 : 2738 : && GET_CODE (op1) == CONST_VECTOR
4350 : 22460952 : && const_vec_duplicate_p (op1)
4351 : 22460994 : && CONST_INT_P (XVECEXP (op1, 0, 0)))
4352 : : {
4353 : 112 : unsigned HOST_WIDE_INT shift_count
4354 : : = (CONST_INT_P (XEXP (op0, 1))
4355 : 56 : ? UINTVAL (XEXP (op0, 1))
4356 : 0 : : UINTVAL (XVECEXP (XEXP (op0, 1), 0, 0)));
4357 : 56 : unsigned HOST_WIDE_INT inner_prec
4358 : 112 : = GET_MODE_PRECISION (GET_MODE_INNER (mode));
4359 : :
4360 : : /* Avoid UD shift count. */
4361 : 56 : if (shift_count < inner_prec
4362 : 56 : && (UINTVAL (XVECEXP (op1, 0, 0))
4363 : 56 : == (HOST_WIDE_INT_1U << (inner_prec - shift_count)) - 1))
4364 : 42 : return simplify_gen_binary (LSHIFTRT, mode, XEXP (op0, 0), XEXP (op0, 1));
4365 : : }
4366 : :
4367 : 22460894 : tem = simplify_with_subreg_not (code, mode, op0, op1);
4368 : 22460894 : if (tem)
4369 : : return tem;
4370 : :
4371 : 22458512 : tem = simplify_byte_swapping_operation (code, mode, op0, op1);
4372 : 22458512 : if (tem)
4373 : : return tem;
4374 : :
4375 : 22458039 : tem = simplify_associative_operation (code, mode, op0, op1);
4376 : 22458039 : if (tem)
4377 : : return tem;
4378 : : break;
4379 : :
4380 : 943115 : case UDIV:
4381 : : /* 0/x is 0 (or x&0 if x has side-effects). */
4382 : 943115 : if (trueop0 == CONST0_RTX (mode)
4383 : 340 : && !cfun->can_throw_non_call_exceptions)
4384 : : {
4385 : 340 : if (side_effects_p (op1))
4386 : 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4387 : : return trueop0;
4388 : : }
4389 : : /* x/1 is x. */
4390 : 942775 : if (trueop1 == CONST1_RTX (mode))
4391 : : {
4392 : 240629 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4393 : 240629 : if (tem)
4394 : : return tem;
4395 : : }
4396 : : /* Convert divide by power of two into shift. */
4397 : 702146 : if (CONST_INT_P (trueop1)
4398 : 1038123 : && (val = exact_log2 (UINTVAL (trueop1))) > 0)
4399 : 335977 : return simplify_gen_binary (LSHIFTRT, mode, op0,
4400 : 335977 : gen_int_shift_amount (mode, val));
4401 : : break;
4402 : :
4403 : 1146456 : case DIV:
4404 : : /* Handle floating point and integers separately. */
4405 : 1146456 : if (SCALAR_FLOAT_MODE_P (mode))
4406 : : {
4407 : : /* Maybe change 0.0 / x to 0.0. This transformation isn't
4408 : : safe for modes with NaNs, since 0.0 / 0.0 will then be
4409 : : NaN rather than 0.0. Nor is it safe for modes with signed
4410 : : zeros, since dividing 0 by a negative number gives -0.0 */
4411 : 330343 : if (trueop0 == CONST0_RTX (mode)
4412 : 2901 : && !HONOR_NANS (mode)
4413 : 14 : && !HONOR_SIGNED_ZEROS (mode)
4414 : 330357 : && ! side_effects_p (op1))
4415 : : return op0;
4416 : : /* x/1.0 is x. */
4417 : 330329 : if (trueop1 == CONST1_RTX (mode)
4418 : 330329 : && !HONOR_SNANS (mode))
4419 : : return op0;
4420 : :
4421 : 330324 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1)
4422 : 27793 : && trueop1 != CONST0_RTX (mode))
4423 : : {
4424 : 21645 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
4425 : :
4426 : : /* x/-1.0 is -x. */
4427 : 21645 : if (real_equal (d1, &dconstm1)
4428 : 21645 : && !HONOR_SNANS (mode))
4429 : 0 : return simplify_gen_unary (NEG, mode, op0, mode);
4430 : :
4431 : : /* Change FP division by a constant into multiplication.
4432 : : Only do this with -freciprocal-math. */
4433 : 21645 : if (flag_reciprocal_math
4434 : 21645 : && !real_equal (d1, &dconst0))
4435 : : {
4436 : 7 : REAL_VALUE_TYPE d;
4437 : 7 : real_arithmetic (&d, RDIV_EXPR, &dconst1, d1);
4438 : 7 : tem = const_double_from_real_value (d, mode);
4439 : 7 : return simplify_gen_binary (MULT, mode, op0, tem);
4440 : : }
4441 : : }
4442 : : }
4443 : 816113 : else if (SCALAR_INT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
4444 : : {
4445 : : /* 0/x is 0 (or x&0 if x has side-effects). */
4446 : 795019 : if (trueop0 == CONST0_RTX (mode)
4447 : 634 : && !cfun->can_throw_non_call_exceptions)
4448 : : {
4449 : 557 : if (side_effects_p (op1))
4450 : 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4451 : : return trueop0;
4452 : : }
4453 : : /* x/1 is x. */
4454 : 794462 : if (trueop1 == CONST1_RTX (mode))
4455 : : {
4456 : 287 : tem = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4457 : 287 : if (tem)
4458 : : return tem;
4459 : : }
4460 : : /* x/-1 is -x. */
4461 : 794175 : if (trueop1 == CONSTM1_RTX (mode))
4462 : : {
4463 : 215 : rtx x = rtl_hooks.gen_lowpart_no_emit (mode, op0);
4464 : 215 : if (x)
4465 : 215 : return simplify_gen_unary (NEG, mode, x, mode);
4466 : : }
4467 : : }
4468 : : break;
4469 : :
4470 : 920658 : case UMOD:
4471 : : /* 0%x is 0 (or x&0 if x has side-effects). */
4472 : 920658 : if (trueop0 == CONST0_RTX (mode))
4473 : : {
4474 : 818 : if (side_effects_p (op1))
4475 : 0 : return simplify_gen_binary (AND, mode, op1, trueop0);
4476 : : return trueop0;
4477 : : }
4478 : : /* x%1 is 0 (of x&0 if x has side-effects). */
4479 : 919840 : if (trueop1 == CONST1_RTX (mode))
4480 : : {
4481 : 275760 : if (side_effects_p (op0))
4482 : 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4483 : 275760 : return CONST0_RTX (mode);
4484 : : }
4485 : : /* Implement modulus by power of two as AND. */
4486 : 644080 : if (CONST_INT_P (trueop1)
4487 : 953471 : && exact_log2 (UINTVAL (trueop1)) > 0)
4488 : 309391 : return simplify_gen_binary (AND, mode, op0,
4489 : 309391 : gen_int_mode (UINTVAL (trueop1) - 1,
4490 : : mode));
4491 : : break;
4492 : :
4493 : 333082 : case MOD:
4494 : : /* 0%x is 0 (or x&0 if x has side-effects). */
4495 : 333082 : if (trueop0 == CONST0_RTX (mode))
4496 : : {
4497 : 636 : if (side_effects_p (op1))
4498 : 8 : return simplify_gen_binary (AND, mode, op1, trueop0);
4499 : : return trueop0;
4500 : : }
4501 : : /* x%1 and x%-1 is 0 (or x&0 if x has side-effects). */
4502 : 332446 : if (trueop1 == CONST1_RTX (mode) || trueop1 == constm1_rtx)
4503 : : {
4504 : 400 : if (side_effects_p (op0))
4505 : 0 : return simplify_gen_binary (AND, mode, op0, CONST0_RTX (mode));
4506 : 400 : return CONST0_RTX (mode);
4507 : : }
4508 : : break;
4509 : :
4510 : 134440 : case ROTATERT:
4511 : 134440 : case ROTATE:
4512 : 134440 : if (trueop1 == CONST0_RTX (mode))
4513 : : return op0;
4514 : : /* Canonicalize rotates by constant amount. If the condition of
4515 : : reversing direction is met, then reverse the direction. */
4516 : : #if defined(HAVE_rotate) && defined(HAVE_rotatert)
4517 : 134350 : if (reverse_rotate_by_imm_p (mode, (code == ROTATE), trueop1))
4518 : : {
4519 : 11182 : int new_amount = GET_MODE_UNIT_PRECISION (mode) - INTVAL (trueop1);
4520 : 11182 : rtx new_amount_rtx = gen_int_shift_amount (mode, new_amount);
4521 : 11562 : return simplify_gen_binary (code == ROTATE ? ROTATERT : ROTATE,
4522 : : mode, op0, new_amount_rtx);
4523 : : }
4524 : : #endif
4525 : : /* ROTATE/ROTATERT:HI (X:HI, 8) is BSWAP:HI (X). Other combinations
4526 : : such as SImode with a count of 16 do not correspond to RTL BSWAP
4527 : : semantics. */
4528 : 123168 : tem = unwrap_const_vec_duplicate (trueop1);
4529 : 123168 : if (GET_MODE_UNIT_BITSIZE (mode) == (2 * BITS_PER_UNIT)
4530 : 123168 : && CONST_INT_P (tem) && INTVAL (tem) == BITS_PER_UNIT)
4531 : 435 : return simplify_gen_unary (BSWAP, mode, op0, mode);
4532 : :
4533 : : /* FALLTHRU */
4534 : 5051382 : case ASHIFTRT:
4535 : 5051382 : if (trueop1 == CONST0_RTX (mode))
4536 : : return op0;
4537 : 5049584 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4538 : : return op0;
4539 : : /* Rotating ~0 always results in ~0. */
4540 : 5049427 : if (CONST_INT_P (trueop0)
4541 : 14866 : && HWI_COMPUTABLE_MODE_P (mode)
4542 : 14822 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
4543 : 5049427 : && ! side_effects_p (op1))
4544 : : return op0;
4545 : :
4546 : 30866871 : canonicalize_shift:
4547 : : /* Given:
4548 : : scalar modes M1, M2
4549 : : scalar constants c1, c2
4550 : : size (M2) > size (M1)
4551 : : c1 == size (M2) - size (M1)
4552 : : optimize:
4553 : : ([a|l]shiftrt:M1 (subreg:M1 (lshiftrt:M2 (reg:M2) (const_int <c1>))
4554 : : <low_part>)
4555 : : (const_int <c2>))
4556 : : to:
4557 : : (subreg:M1 ([a|l]shiftrt:M2 (reg:M2) (const_int <c1 + c2>))
4558 : : <low_part>). */
4559 : 30866871 : if ((code == ASHIFTRT || code == LSHIFTRT)
4560 : 11643520 : && is_a <scalar_int_mode> (mode, &int_mode)
4561 : 10888845 : && SUBREG_P (op0)
4562 : 963693 : && CONST_INT_P (op1)
4563 : 962689 : && GET_CODE (SUBREG_REG (op0)) == LSHIFTRT
4564 : 23349 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
4565 : : &inner_mode)
4566 : 23349 : && CONST_INT_P (XEXP (SUBREG_REG (op0), 1))
4567 : 46392 : && GET_MODE_BITSIZE (inner_mode) > GET_MODE_BITSIZE (int_mode)
4568 : 23196 : && (INTVAL (XEXP (SUBREG_REG (op0), 1))
4569 : 46392 : == GET_MODE_BITSIZE (inner_mode) - GET_MODE_BITSIZE (int_mode))
4570 : 30889745 : && subreg_lowpart_p (op0))
4571 : : {
4572 : 22874 : rtx tmp = gen_int_shift_amount
4573 : 22874 : (inner_mode, INTVAL (XEXP (SUBREG_REG (op0), 1)) + INTVAL (op1));
4574 : :
4575 : : /* Combine would usually zero out the value when combining two
4576 : : local shifts and the range becomes larger or equal to the mode.
4577 : : However since we fold away one of the shifts here combine won't
4578 : : see it so we should immediately zero the result if it's out of
4579 : : range. */
4580 : 22874 : if (code == LSHIFTRT
4581 : 42186 : && INTVAL (tmp) >= GET_MODE_BITSIZE (inner_mode))
4582 : 0 : tmp = const0_rtx;
4583 : : else
4584 : 22874 : tmp = simplify_gen_binary (code,
4585 : : inner_mode,
4586 : 22874 : XEXP (SUBREG_REG (op0), 0),
4587 : : tmp);
4588 : :
4589 : 22874 : return lowpart_subreg (int_mode, tmp, inner_mode);
4590 : : }
4591 : :
4592 : 30843997 : if (SHIFT_COUNT_TRUNCATED && CONST_INT_P (op1))
4593 : : {
4594 : : val = INTVAL (op1) & (GET_MODE_UNIT_PRECISION (mode) - 1);
4595 : : if (val != INTVAL (op1))
4596 : : return simplify_gen_binary (code, mode, op0,
4597 : : gen_int_shift_amount (mode, val));
4598 : : }
4599 : :
4600 : : /* Simplify:
4601 : :
4602 : : (code:M1
4603 : : (subreg:M1
4604 : : ([al]shiftrt:M2
4605 : : (subreg:M2
4606 : : (ashift:M1 X C1))
4607 : : C2))
4608 : : C3)
4609 : :
4610 : : to:
4611 : :
4612 : : (code:M1
4613 : : ([al]shiftrt:M1
4614 : : (ashift:M1 X C1+N)
4615 : : C2+N)
4616 : : C3)
4617 : :
4618 : : where M1 is N bits wider than M2. Optimizing the (subreg:M1 ...)
4619 : : directly would be arithmetically correct, but restricting the
4620 : : simplification to shifts by constants is more conservative,
4621 : : since it is more likely to lead to further simplifications. */
4622 : 30843997 : if (is_a<scalar_int_mode> (mode, &int_mode)
4623 : 5325235 : && paradoxical_subreg_p (op0)
4624 : 4899863 : && is_a<scalar_int_mode> (GET_MODE (SUBREG_REG (op0)), &inner_mode)
4625 : 4899777 : && (GET_CODE (SUBREG_REG (op0)) == ASHIFTRT
4626 : 4899777 : || GET_CODE (SUBREG_REG (op0)) == LSHIFTRT)
4627 : 135116 : && CONST_INT_P (op1))
4628 : : {
4629 : 135116 : auto xcode = GET_CODE (SUBREG_REG (op0));
4630 : 135116 : rtx xop0 = XEXP (SUBREG_REG (op0), 0);
4631 : 135116 : rtx xop1 = XEXP (SUBREG_REG (op0), 1);
4632 : 135116 : if (SUBREG_P (xop0)
4633 : 7078 : && GET_MODE (SUBREG_REG (xop0)) == mode
4634 : 7050 : && GET_CODE (SUBREG_REG (xop0)) == ASHIFT
4635 : 605 : && CONST_INT_P (xop1)
4636 : 135721 : && UINTVAL (xop1) < GET_MODE_PRECISION (inner_mode))
4637 : : {
4638 : 605 : rtx yop0 = XEXP (SUBREG_REG (xop0), 0);
4639 : 605 : rtx yop1 = XEXP (SUBREG_REG (xop0), 1);
4640 : 605 : if (CONST_INT_P (yop1)
4641 : 605 : && UINTVAL (yop1) < GET_MODE_PRECISION (inner_mode))
4642 : : {
4643 : 1210 : auto bias = (GET_MODE_BITSIZE (int_mode)
4644 : 605 : - GET_MODE_BITSIZE (inner_mode));
4645 : 605 : tem = simplify_gen_binary (ASHIFT, mode, yop0,
4646 : 605 : GEN_INT (INTVAL (yop1) + bias));
4647 : 605 : tem = simplify_gen_binary (xcode, mode, tem,
4648 : 605 : GEN_INT (INTVAL (xop1) + bias));
4649 : 605 : return simplify_gen_binary (code, mode, tem, op1);
4650 : : }
4651 : : }
4652 : : }
4653 : : break;
4654 : :
4655 : 0 : case SS_ASHIFT:
4656 : 0 : if (CONST_INT_P (trueop0)
4657 : 0 : && HWI_COMPUTABLE_MODE_P (mode)
4658 : 0 : && (UINTVAL (trueop0) == (GET_MODE_MASK (mode) >> 1)
4659 : 0 : || mode_signbit_p (mode, trueop0))
4660 : 0 : && ! side_effects_p (op1))
4661 : : return op0;
4662 : 0 : goto simplify_ashift;
4663 : :
4664 : 0 : case US_ASHIFT:
4665 : 0 : if (CONST_INT_P (trueop0)
4666 : 0 : && HWI_COMPUTABLE_MODE_P (mode)
4667 : 0 : && UINTVAL (trueop0) == GET_MODE_MASK (mode)
4668 : 0 : && ! side_effects_p (op1))
4669 : : return op0;
4670 : : /* FALLTHRU */
4671 : :
4672 : 19535209 : case ASHIFT:
4673 : 19535209 : simplify_ashift:
4674 : 19535209 : if (trueop1 == CONST0_RTX (mode))
4675 : : return op0;
4676 : 19369405 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4677 : : return op0;
4678 : 19343239 : if (mem_depth
4679 : 242606 : && code == ASHIFT
4680 : 242606 : && CONST_INT_P (trueop1)
4681 : 242598 : && is_a <scalar_int_mode> (mode, &int_mode)
4682 : 19585825 : && IN_RANGE (UINTVAL (trueop1),
4683 : : 1, GET_MODE_PRECISION (int_mode) - 1))
4684 : : {
4685 : 242586 : auto c = (wi::one (GET_MODE_PRECISION (int_mode))
4686 : 242586 : << UINTVAL (trueop1));
4687 : 242586 : rtx new_op1 = immed_wide_int_const (c, int_mode);
4688 : 242586 : return simplify_gen_binary (MULT, int_mode, op0, new_op1);
4689 : 242586 : }
4690 : 19100653 : goto canonicalize_shift;
4691 : :
4692 : 8514882 : case LSHIFTRT:
4693 : 8514882 : if (trueop1 == CONST0_RTX (mode))
4694 : : return op0;
4695 : 6718167 : if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1))
4696 : : return op0;
4697 : : /* Optimize (lshiftrt (clz X) C) as (eq X 0). */
4698 : 6716791 : if (GET_CODE (op0) == CLZ
4699 : 0 : && is_a <scalar_int_mode> (GET_MODE (XEXP (op0, 0)), &inner_mode)
4700 : 0 : && CONST_INT_P (trueop1)
4701 : : && STORE_FLAG_VALUE == 1
4702 : 6716791 : && INTVAL (trueop1) < GET_MODE_UNIT_PRECISION (mode))
4703 : : {
4704 : 0 : unsigned HOST_WIDE_INT zero_val = 0;
4705 : :
4706 : 0 : if (CLZ_DEFINED_VALUE_AT_ZERO (inner_mode, zero_val)
4707 : 0 : && zero_val == GET_MODE_PRECISION (inner_mode)
4708 : 0 : && INTVAL (trueop1) == exact_log2 (zero_val))
4709 : 0 : return simplify_gen_relational (EQ, mode, inner_mode,
4710 : 0 : XEXP (op0, 0), const0_rtx);
4711 : : }
4712 : 6716791 : goto canonicalize_shift;
4713 : :
4714 : 199332 : case SMIN:
4715 : 199332 : if (HWI_COMPUTABLE_MODE_P (mode)
4716 : 179354 : && mode_signbit_p (mode, trueop1)
4717 : 0 : && ! side_effects_p (op0))
4718 : : return op1;
4719 : 199332 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4720 : : return op0;
4721 : 199195 : tem = simplify_associative_operation (code, mode, op0, op1);
4722 : 199195 : if (tem)
4723 : : return tem;
4724 : : break;
4725 : :
4726 : 454145 : case SMAX:
4727 : 454145 : if (HWI_COMPUTABLE_MODE_P (mode)
4728 : 427413 : && CONST_INT_P (trueop1)
4729 : 399252 : && (UINTVAL (trueop1) == GET_MODE_MASK (mode) >> 1)
4730 : 0 : && ! side_effects_p (op0))
4731 : : return op1;
4732 : 454145 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4733 : : return op0;
4734 : 454040 : tem = simplify_associative_operation (code, mode, op0, op1);
4735 : 454040 : if (tem)
4736 : : return tem;
4737 : : break;
4738 : :
4739 : 230114 : case UMIN:
4740 : 230114 : if (trueop1 == CONST0_RTX (mode) && ! side_effects_p (op0))
4741 : : return op1;
4742 : 230099 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4743 : : return op0;
4744 : 229998 : tem = simplify_associative_operation (code, mode, op0, op1);
4745 : 229998 : if (tem)
4746 : : return tem;
4747 : : break;
4748 : :
4749 : 220308 : case UMAX:
4750 : 220308 : if (trueop1 == constm1_rtx && ! side_effects_p (op0))
4751 : : return op1;
4752 : 220308 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4753 : : return op0;
4754 : 220218 : tem = simplify_associative_operation (code, mode, op0, op1);
4755 : 220218 : if (tem)
4756 : : return tem;
4757 : : break;
4758 : :
4759 : 11176 : case SS_PLUS:
4760 : 11176 : case US_PLUS:
4761 : 11176 : case SS_MINUS:
4762 : 11176 : case US_MINUS:
4763 : : /* Simplify x +/- 0 to x, if possible. */
4764 : 11176 : if (trueop1 == CONST0_RTX (mode))
4765 : : return op0;
4766 : : return 0;
4767 : :
4768 : 0 : case SS_MULT:
4769 : 0 : case US_MULT:
4770 : : /* Simplify x * 0 to 0, if possible. */
4771 : 0 : if (trueop1 == CONST0_RTX (mode)
4772 : 0 : && !side_effects_p (op0))
4773 : : return op1;
4774 : :
4775 : : /* Simplify x * 1 to x, if possible. */
4776 : 0 : if (trueop1 == CONST1_RTX (mode))
4777 : : return op0;
4778 : : return 0;
4779 : :
4780 : 528397 : case SMUL_HIGHPART:
4781 : 528397 : case UMUL_HIGHPART:
4782 : : /* Simplify x * 0 to 0, if possible. */
4783 : 528397 : if (trueop1 == CONST0_RTX (mode)
4784 : 528397 : && !side_effects_p (op0))
4785 : : return op1;
4786 : : return 0;
4787 : :
4788 : 0 : case SS_DIV:
4789 : 0 : case US_DIV:
4790 : : /* Simplify x / 1 to x, if possible. */
4791 : 0 : if (trueop1 == CONST1_RTX (mode))
4792 : : return op0;
4793 : : return 0;
4794 : :
4795 : 0 : case COPYSIGN:
4796 : 0 : if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
4797 : : return op0;
4798 : 0 : if (CONST_DOUBLE_AS_FLOAT_P (trueop1))
4799 : : {
4800 : 0 : REAL_VALUE_TYPE f1;
4801 : 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
4802 : 0 : rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
4803 : 0 : if (REAL_VALUE_NEGATIVE (f1))
4804 : 0 : tmp = simplify_unary_operation (NEG, mode, tmp, mode);
4805 : 0 : return tmp;
4806 : : }
4807 : 0 : if (GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
4808 : 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
4809 : 0 : if (GET_CODE (op1) == ABS
4810 : 0 : && ! side_effects_p (op1))
4811 : 0 : return simplify_gen_unary (ABS, mode, op0, mode);
4812 : 0 : if (GET_CODE (op0) == COPYSIGN
4813 : 0 : && ! side_effects_p (XEXP (op0, 1)))
4814 : 0 : return simplify_gen_binary (COPYSIGN, mode, XEXP (op0, 0), op1);
4815 : 0 : if (GET_CODE (op1) == COPYSIGN
4816 : 0 : && ! side_effects_p (XEXP (op1, 0)))
4817 : 0 : return simplify_gen_binary (COPYSIGN, mode, op0, XEXP (op1, 1));
4818 : : return 0;
4819 : :
4820 : 1301 : case VEC_SERIES:
4821 : 2602 : if (op1 == CONST0_RTX (GET_MODE_INNER (mode)))
4822 : 92 : return gen_vec_duplicate (mode, op0);
4823 : 1209 : if (valid_for_const_vector_p (mode, op0)
4824 : 1209 : && valid_for_const_vector_p (mode, op1))
4825 : 93 : return gen_const_vec_series (mode, op0, op1);
4826 : : return 0;
4827 : :
4828 : 3323949 : case VEC_SELECT:
4829 : 3323949 : if (!VECTOR_MODE_P (mode))
4830 : : {
4831 : 879563 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
4832 : 1759126 : gcc_assert (mode == GET_MODE_INNER (GET_MODE (trueop0)));
4833 : 879563 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
4834 : 879563 : gcc_assert (XVECLEN (trueop1, 0) == 1);
4835 : :
4836 : : /* We can't reason about selections made at runtime. */
4837 : 879563 : if (!CONST_INT_P (XVECEXP (trueop1, 0, 0)))
4838 : 441645540 : return 0;
4839 : :
4840 : 879563 : if (vec_duplicate_p (trueop0, &elt0))
4841 : 1738 : return elt0;
4842 : :
4843 : 877825 : if (GET_CODE (trueop0) == CONST_VECTOR)
4844 : 7093 : return CONST_VECTOR_ELT (trueop0, INTVAL (XVECEXP
4845 : : (trueop1, 0, 0)));
4846 : :
4847 : : /* Extract a scalar element from a nested VEC_SELECT expression
4848 : : (with optional nested VEC_CONCAT expression). Some targets
4849 : : (i386) extract scalar element from a vector using chain of
4850 : : nested VEC_SELECT expressions. When input operand is a memory
4851 : : operand, this operation can be simplified to a simple scalar
4852 : : load from an offseted memory address. */
4853 : 870732 : int n_elts;
4854 : 870732 : if (GET_CODE (trueop0) == VEC_SELECT
4855 : 938769 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
4856 : 68037 : .is_constant (&n_elts)))
4857 : : {
4858 : 68037 : rtx op0 = XEXP (trueop0, 0);
4859 : 68037 : rtx op1 = XEXP (trueop0, 1);
4860 : :
4861 : 68037 : int i = INTVAL (XVECEXP (trueop1, 0, 0));
4862 : 68037 : int elem;
4863 : :
4864 : 68037 : rtvec vec;
4865 : 68037 : rtx tmp_op, tmp;
4866 : :
4867 : 68037 : gcc_assert (GET_CODE (op1) == PARALLEL);
4868 : 68037 : gcc_assert (i < n_elts);
4869 : :
4870 : : /* Select element, pointed by nested selector. */
4871 : 68037 : elem = INTVAL (XVECEXP (op1, 0, i));
4872 : :
4873 : : /* Handle the case when nested VEC_SELECT wraps VEC_CONCAT. */
4874 : 68037 : if (GET_CODE (op0) == VEC_CONCAT)
4875 : : {
4876 : 27360 : rtx op00 = XEXP (op0, 0);
4877 : 27360 : rtx op01 = XEXP (op0, 1);
4878 : :
4879 : 27360 : machine_mode mode00, mode01;
4880 : 27360 : int n_elts00, n_elts01;
4881 : :
4882 : 27360 : mode00 = GET_MODE (op00);
4883 : 27360 : mode01 = GET_MODE (op01);
4884 : :
4885 : : /* Find out the number of elements of each operand.
4886 : : Since the concatenated result has a constant number
4887 : : of elements, the operands must too. */
4888 : 27360 : n_elts00 = GET_MODE_NUNITS (mode00).to_constant ();
4889 : 27360 : n_elts01 = GET_MODE_NUNITS (mode01).to_constant ();
4890 : :
4891 : 27360 : gcc_assert (n_elts == n_elts00 + n_elts01);
4892 : :
4893 : : /* Select correct operand of VEC_CONCAT
4894 : : and adjust selector. */
4895 : 27360 : if (elem < n_elts01)
4896 : : tmp_op = op00;
4897 : : else
4898 : : {
4899 : 41 : tmp_op = op01;
4900 : 41 : elem -= n_elts00;
4901 : : }
4902 : : }
4903 : : else
4904 : : tmp_op = op0;
4905 : :
4906 : 68037 : vec = rtvec_alloc (1);
4907 : 68037 : RTVEC_ELT (vec, 0) = GEN_INT (elem);
4908 : :
4909 : 68037 : tmp = gen_rtx_fmt_ee (code, mode,
4910 : : tmp_op, gen_rtx_PARALLEL (VOIDmode, vec));
4911 : 68037 : return tmp;
4912 : : }
4913 : : }
4914 : : else
4915 : : {
4916 : 2444386 : gcc_assert (VECTOR_MODE_P (GET_MODE (trueop0)));
4917 : 7333158 : gcc_assert (GET_MODE_INNER (mode)
4918 : : == GET_MODE_INNER (GET_MODE (trueop0)));
4919 : 2444386 : gcc_assert (GET_CODE (trueop1) == PARALLEL);
4920 : :
4921 : 2444386 : if (vec_duplicate_p (trueop0, &elt0))
4922 : : /* It doesn't matter which elements are selected by trueop1,
4923 : : because they are all the same. */
4924 : 15159 : return gen_vec_duplicate (mode, elt0);
4925 : :
4926 : 2429227 : if (GET_CODE (trueop0) == CONST_VECTOR)
4927 : : {
4928 : 17110 : unsigned n_elts = XVECLEN (trueop1, 0);
4929 : 17110 : rtvec v = rtvec_alloc (n_elts);
4930 : 17110 : unsigned int i;
4931 : :
4932 : 34220 : gcc_assert (known_eq (n_elts, GET_MODE_NUNITS (mode)));
4933 : 87770 : for (i = 0; i < n_elts; i++)
4934 : : {
4935 : 70660 : rtx x = XVECEXP (trueop1, 0, i);
4936 : :
4937 : 70660 : if (!CONST_INT_P (x))
4938 : : return 0;
4939 : :
4940 : 70660 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0,
4941 : : INTVAL (x));
4942 : : }
4943 : :
4944 : 17110 : return gen_rtx_CONST_VECTOR (mode, v);
4945 : : }
4946 : :
4947 : : /* Recognize the identity. */
4948 : 2412117 : if (GET_MODE (trueop0) == mode)
4949 : : {
4950 : 577119 : bool maybe_ident = true;
4951 : 577119 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
4952 : : {
4953 : 576756 : rtx j = XVECEXP (trueop1, 0, i);
4954 : 576756 : if (!CONST_INT_P (j) || INTVAL (j) != i)
4955 : : {
4956 : : maybe_ident = false;
4957 : : break;
4958 : : }
4959 : : }
4960 : 355696 : if (maybe_ident)
4961 : : return trueop0;
4962 : : }
4963 : :
4964 : : /* If we select a low-part subreg, return that. */
4965 : 2411754 : if (vec_series_lowpart_p (mode, GET_MODE (trueop0), trueop1))
4966 : : {
4967 : 0 : rtx new_rtx = lowpart_subreg (mode, trueop0,
4968 : 0 : GET_MODE (trueop0));
4969 : 0 : if (new_rtx != NULL_RTX)
4970 : : return new_rtx;
4971 : : }
4972 : :
4973 : : /* If we build {a,b} then permute it, build the result directly. */
4974 : 2411754 : if (XVECLEN (trueop1, 0) == 2
4975 : 580344 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
4976 : 580344 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
4977 : 580344 : && GET_CODE (trueop0) == VEC_CONCAT
4978 : 172820 : && GET_CODE (XEXP (trueop0, 0)) == VEC_CONCAT
4979 : 79 : && GET_MODE (XEXP (trueop0, 0)) == mode
4980 : 79 : && GET_CODE (XEXP (trueop0, 1)) == VEC_CONCAT
4981 : 56 : && GET_MODE (XEXP (trueop0, 1)) == mode)
4982 : : {
4983 : 56 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
4984 : 56 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
4985 : 56 : rtx subop0, subop1;
4986 : :
4987 : 56 : gcc_assert (i0 < 4 && i1 < 4);
4988 : 56 : subop0 = XEXP (XEXP (trueop0, i0 / 2), i0 % 2);
4989 : 56 : subop1 = XEXP (XEXP (trueop0, i1 / 2), i1 % 2);
4990 : :
4991 : 56 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
4992 : : }
4993 : :
4994 : 2411698 : if (XVECLEN (trueop1, 0) == 2
4995 : 580288 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
4996 : 580288 : && CONST_INT_P (XVECEXP (trueop1, 0, 1))
4997 : 580288 : && GET_CODE (trueop0) == VEC_CONCAT
4998 : 172764 : && GET_MODE (trueop0) == mode)
4999 : : {
5000 : 2 : unsigned int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5001 : 2 : unsigned int i1 = INTVAL (XVECEXP (trueop1, 0, 1));
5002 : 2 : rtx subop0, subop1;
5003 : :
5004 : 2 : gcc_assert (i0 < 2 && i1 < 2);
5005 : 2 : subop0 = XEXP (trueop0, i0);
5006 : 2 : subop1 = XEXP (trueop0, i1);
5007 : :
5008 : 2 : return simplify_gen_binary (VEC_CONCAT, mode, subop0, subop1);
5009 : : }
5010 : :
5011 : : /* If we select one half of a vec_concat, return that. */
5012 : 2411696 : int l0, l1;
5013 : 2411696 : if (GET_CODE (trueop0) == VEC_CONCAT
5014 : 2974130 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 0)))
5015 : 1487065 : .is_constant (&l0))
5016 : 2974130 : && (GET_MODE_NUNITS (GET_MODE (XEXP (trueop0, 1)))
5017 : 1487065 : .is_constant (&l1))
5018 : 3898761 : && CONST_INT_P (XVECEXP (trueop1, 0, 0)))
5019 : : {
5020 : 1487065 : rtx subop0 = XEXP (trueop0, 0);
5021 : 1487065 : rtx subop1 = XEXP (trueop0, 1);
5022 : 1487065 : machine_mode mode0 = GET_MODE (subop0);
5023 : 1487065 : machine_mode mode1 = GET_MODE (subop1);
5024 : 1487065 : int i0 = INTVAL (XVECEXP (trueop1, 0, 0));
5025 : 1487065 : if (i0 == 0 && !side_effects_p (op1) && mode == mode0)
5026 : : {
5027 : 947749 : bool success = true;
5028 : 947749 : for (int i = 1; i < l0; ++i)
5029 : : {
5030 : 947422 : rtx j = XVECEXP (trueop1, 0, i);
5031 : 947422 : if (!CONST_INT_P (j) || INTVAL (j) != i)
5032 : : {
5033 : : success = false;
5034 : : break;
5035 : : }
5036 : : }
5037 : 865428 : if (success)
5038 : : return subop0;
5039 : : }
5040 : 1486738 : if (i0 == l0 && !side_effects_p (op0) && mode == mode1)
5041 : : {
5042 : 532 : bool success = true;
5043 : 532 : for (int i = 1; i < l1; ++i)
5044 : : {
5045 : 485 : rtx j = XVECEXP (trueop1, 0, i);
5046 : 485 : if (!CONST_INT_P (j) || INTVAL (j) != i0 + i)
5047 : : {
5048 : : success = false;
5049 : : break;
5050 : : }
5051 : : }
5052 : 76 : if (success)
5053 : : return subop1;
5054 : : }
5055 : : }
5056 : :
5057 : : /* Simplify vec_select of a subreg of X to just a vec_select of X
5058 : : when X has same component mode as vec_select. */
5059 : 2411322 : unsigned HOST_WIDE_INT subreg_offset = 0;
5060 : 2411322 : if (GET_CODE (trueop0) == SUBREG
5061 : 353516 : && GET_MODE_INNER (mode)
5062 : 707032 : == GET_MODE_INNER (GET_MODE (SUBREG_REG (trueop0)))
5063 : 28124 : && GET_MODE_NUNITS (mode).is_constant (&l1)
5064 : 2764838 : && constant_multiple_p (subreg_memory_offset (trueop0),
5065 : 28124 : GET_MODE_UNIT_BITSIZE (mode),
5066 : : &subreg_offset))
5067 : : {
5068 : 14062 : poly_uint64 nunits
5069 : 28124 : = GET_MODE_NUNITS (GET_MODE (SUBREG_REG (trueop0)));
5070 : 14062 : bool success = true;
5071 : 80406 : for (int i = 0; i != l1; i++)
5072 : : {
5073 : 76765 : rtx idx = XVECEXP (trueop1, 0, i);
5074 : 76765 : if (!CONST_INT_P (idx)
5075 : 76765 : || maybe_ge (UINTVAL (idx) + subreg_offset, nunits))
5076 : : {
5077 : : success = false;
5078 : : break;
5079 : : }
5080 : : }
5081 : :
5082 : 14062 : if (success)
5083 : : {
5084 : 3641 : rtx par = trueop1;
5085 : 3641 : if (subreg_offset)
5086 : : {
5087 : 0 : rtvec vec = rtvec_alloc (l1);
5088 : 0 : for (int i = 0; i < l1; i++)
5089 : 0 : RTVEC_ELT (vec, i)
5090 : 0 : = GEN_INT (INTVAL (XVECEXP (trueop1, 0, i))
5091 : : + subreg_offset);
5092 : 0 : par = gen_rtx_PARALLEL (VOIDmode, vec);
5093 : : }
5094 : 3641 : return gen_rtx_VEC_SELECT (mode, SUBREG_REG (trueop0), par);
5095 : : }
5096 : : }
5097 : : }
5098 : :
5099 : 3210376 : if (XVECLEN (trueop1, 0) == 1
5100 : 802779 : && CONST_INT_P (XVECEXP (trueop1, 0, 0))
5101 : 802779 : && GET_CODE (trueop0) == VEC_CONCAT)
5102 : : {
5103 : 1260 : rtx vec = trueop0;
5104 : 2520 : offset = INTVAL (XVECEXP (trueop1, 0, 0)) * GET_MODE_SIZE (mode);
5105 : :
5106 : : /* Try to find the element in the VEC_CONCAT. */
5107 : 1260 : while (GET_MODE (vec) != mode
5108 : 2520 : && GET_CODE (vec) == VEC_CONCAT)
5109 : : {
5110 : 1260 : poly_int64 vec_size;
5111 : :
5112 : 1260 : if (CONST_INT_P (XEXP (vec, 0)))
5113 : : {
5114 : : /* vec_concat of two const_ints doesn't make sense with
5115 : : respect to modes. */
5116 : 1 : if (CONST_INT_P (XEXP (vec, 1)))
5117 : 379092047 : return 0;
5118 : :
5119 : 1 : vec_size = GET_MODE_SIZE (GET_MODE (trueop0))
5120 : 3 : - GET_MODE_SIZE (GET_MODE (XEXP (vec, 1)));
5121 : : }
5122 : : else
5123 : 2518 : vec_size = GET_MODE_SIZE (GET_MODE (XEXP (vec, 0)));
5124 : :
5125 : 1260 : if (known_lt (offset, vec_size))
5126 : : vec = XEXP (vec, 0);
5127 : 306 : else if (known_ge (offset, vec_size))
5128 : : {
5129 : 306 : offset -= vec_size;
5130 : 306 : vec = XEXP (vec, 1);
5131 : : }
5132 : : else
5133 : : break;
5134 : 1260 : vec = avoid_constant_pool_reference (vec);
5135 : : }
5136 : :
5137 : 1260 : if (GET_MODE (vec) == mode)
5138 : : return vec;
5139 : : }
5140 : :
5141 : : /* If we select elements in a vec_merge that all come from the same
5142 : : operand, select from that operand directly. */
5143 : 3209289 : if (GET_CODE (op0) == VEC_MERGE)
5144 : : {
5145 : 7517 : rtx trueop02 = avoid_constant_pool_reference (XEXP (op0, 2));
5146 : 7517 : if (CONST_INT_P (trueop02))
5147 : : {
5148 : 2456 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop02);
5149 : 2456 : bool all_operand0 = true;
5150 : 2456 : bool all_operand1 = true;
5151 : 8981 : for (int i = 0; i < XVECLEN (trueop1, 0); i++)
5152 : : {
5153 : 6525 : rtx j = XVECEXP (trueop1, 0, i);
5154 : 6525 : if (sel & (HOST_WIDE_INT_1U << UINTVAL (j)))
5155 : : all_operand1 = false;
5156 : : else
5157 : 3190 : all_operand0 = false;
5158 : : }
5159 : 2456 : if (all_operand0 && !side_effects_p (XEXP (op0, 1)))
5160 : 710 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 0), op1);
5161 : 1746 : if (all_operand1 && !side_effects_p (XEXP (op0, 0)))
5162 : 47 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (op0, 1), op1);
5163 : : }
5164 : : }
5165 : :
5166 : : /* If we have two nested selects that are inverses of each
5167 : : other, replace them with the source operand. */
5168 : 3208532 : if (GET_CODE (trueop0) == VEC_SELECT
5169 : 69166 : && GET_MODE (XEXP (trueop0, 0)) == mode)
5170 : : {
5171 : 1275 : rtx op0_subop1 = XEXP (trueop0, 1);
5172 : 1275 : gcc_assert (GET_CODE (op0_subop1) == PARALLEL);
5173 : 2550 : gcc_assert (known_eq (XVECLEN (trueop1, 0), GET_MODE_NUNITS (mode)));
5174 : :
5175 : : /* Apply the outer ordering vector to the inner one. (The inner
5176 : : ordering vector is expressly permitted to be of a different
5177 : : length than the outer one.) If the result is { 0, 1, ..., n-1 }
5178 : : then the two VEC_SELECTs cancel. */
5179 : 1581 : for (int i = 0; i < XVECLEN (trueop1, 0); ++i)
5180 : : {
5181 : 1581 : rtx x = XVECEXP (trueop1, 0, i);
5182 : 1581 : if (!CONST_INT_P (x))
5183 : : return 0;
5184 : 1581 : rtx y = XVECEXP (op0_subop1, 0, INTVAL (x));
5185 : 1581 : if (!CONST_INT_P (y) || i != INTVAL (y))
5186 : : return 0;
5187 : : }
5188 : : return XEXP (trueop0, 0);
5189 : : }
5190 : :
5191 : : return 0;
5192 : 4152587 : case VEC_CONCAT:
5193 : 4152587 : {
5194 : 4152587 : machine_mode op0_mode = (GET_MODE (trueop0) != VOIDmode
5195 : 4152587 : ? GET_MODE (trueop0)
5196 : 4152587 : : GET_MODE_INNER (mode));
5197 : 4152587 : machine_mode op1_mode = (GET_MODE (trueop1) != VOIDmode
5198 : 4152587 : ? GET_MODE (trueop1)
5199 : 4152587 : : GET_MODE_INNER (mode));
5200 : :
5201 : 4152587 : gcc_assert (VECTOR_MODE_P (mode));
5202 : 16610348 : gcc_assert (known_eq (GET_MODE_SIZE (op0_mode)
5203 : : + GET_MODE_SIZE (op1_mode),
5204 : : GET_MODE_SIZE (mode)));
5205 : :
5206 : 4152587 : if (VECTOR_MODE_P (op0_mode))
5207 : 5617959 : gcc_assert (GET_MODE_INNER (mode)
5208 : : == GET_MODE_INNER (op0_mode));
5209 : : else
5210 : 4559868 : gcc_assert (GET_MODE_INNER (mode) == op0_mode);
5211 : :
5212 : 4152587 : if (VECTOR_MODE_P (op1_mode))
5213 : 5617959 : gcc_assert (GET_MODE_INNER (mode)
5214 : : == GET_MODE_INNER (op1_mode));
5215 : : else
5216 : 4559868 : gcc_assert (GET_MODE_INNER (mode) == op1_mode);
5217 : :
5218 : 4152587 : unsigned int n_elts, in_n_elts;
5219 : 4152587 : if ((GET_CODE (trueop0) == CONST_VECTOR
5220 : 4152587 : || CONST_SCALAR_INT_P (trueop0)
5221 : 3984646 : || CONST_DOUBLE_AS_FLOAT_P (trueop0))
5222 : 169399 : && (GET_CODE (trueop1) == CONST_VECTOR
5223 : 169399 : || CONST_SCALAR_INT_P (trueop1)
5224 : 162385 : || CONST_DOUBLE_AS_FLOAT_P (trueop1))
5225 : 14028 : && GET_MODE_NUNITS (mode).is_constant (&n_elts)
5226 : 4159601 : && GET_MODE_NUNITS (op0_mode).is_constant (&in_n_elts))
5227 : : {
5228 : 7014 : rtvec v = rtvec_alloc (n_elts);
5229 : 7014 : unsigned int i;
5230 : 120964 : for (i = 0; i < n_elts; i++)
5231 : : {
5232 : 106936 : if (i < in_n_elts)
5233 : : {
5234 : 53372 : if (!VECTOR_MODE_P (op0_mode))
5235 : 0 : RTVEC_ELT (v, i) = trueop0;
5236 : : else
5237 : 53372 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0, i);
5238 : : }
5239 : : else
5240 : : {
5241 : 53564 : if (!VECTOR_MODE_P (op1_mode))
5242 : 0 : RTVEC_ELT (v, i) = trueop1;
5243 : : else
5244 : 53564 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop1,
5245 : : i - in_n_elts);
5246 : : }
5247 : : }
5248 : :
5249 : 7014 : return gen_rtx_CONST_VECTOR (mode, v);
5250 : : }
5251 : :
5252 : : /* Try to merge two VEC_SELECTs from the same vector into a single one.
5253 : : Restrict the transformation to avoid generating a VEC_SELECT with a
5254 : : mode unrelated to its operand. */
5255 : 4145573 : if (GET_CODE (trueop0) == VEC_SELECT
5256 : 129711 : && GET_CODE (trueop1) == VEC_SELECT
5257 : 28393 : && rtx_equal_p (XEXP (trueop0, 0), XEXP (trueop1, 0))
5258 : 4161590 : && GET_MODE_INNER (GET_MODE (XEXP (trueop0, 0)))
5259 : 32034 : == GET_MODE_INNER(mode))
5260 : : {
5261 : 16017 : rtx par0 = XEXP (trueop0, 1);
5262 : 16017 : rtx par1 = XEXP (trueop1, 1);
5263 : 16017 : int len0 = XVECLEN (par0, 0);
5264 : 16017 : int len1 = XVECLEN (par1, 0);
5265 : 16017 : rtvec vec = rtvec_alloc (len0 + len1);
5266 : 98407 : for (int i = 0; i < len0; i++)
5267 : 82390 : RTVEC_ELT (vec, i) = XVECEXP (par0, 0, i);
5268 : 98407 : for (int i = 0; i < len1; i++)
5269 : 82390 : RTVEC_ELT (vec, len0 + i) = XVECEXP (par1, 0, i);
5270 : 16017 : return simplify_gen_binary (VEC_SELECT, mode, XEXP (trueop0, 0),
5271 : 16017 : gen_rtx_PARALLEL (VOIDmode, vec));
5272 : : }
5273 : : /* (vec_concat:
5274 : : (subreg_lowpart:N OP)
5275 : : (vec_select:N OP P)) --> OP when P selects the high half
5276 : : of the OP. */
5277 : 4129556 : if (GET_CODE (trueop0) == SUBREG
5278 : 469811 : && subreg_lowpart_p (trueop0)
5279 : 469121 : && GET_CODE (trueop1) == VEC_SELECT
5280 : 49 : && SUBREG_REG (trueop0) == XEXP (trueop1, 0)
5281 : 0 : && !side_effects_p (XEXP (trueop1, 0))
5282 : 4129556 : && vec_series_highpart_p (op1_mode, mode, XEXP (trueop1, 1)))
5283 : 0 : return XEXP (trueop1, 0);
5284 : : }
5285 : : return 0;
5286 : :
5287 : 0 : default:
5288 : 0 : gcc_unreachable ();
5289 : : }
5290 : :
5291 : 371213334 : if (mode == GET_MODE (op0)
5292 : 317577957 : && mode == GET_MODE (op1)
5293 : 96247618 : && vec_duplicate_p (op0, &elt0)
5294 : 371325979 : && vec_duplicate_p (op1, &elt1))
5295 : : {
5296 : : /* Try applying the operator to ELT and see if that simplifies.
5297 : : We can duplicate the result if so.
5298 : :
5299 : : The reason we don't use simplify_gen_binary is that it isn't
5300 : : necessarily a win to convert things like:
5301 : :
5302 : : (plus:V (vec_duplicate:V (reg:S R1))
5303 : : (vec_duplicate:V (reg:S R2)))
5304 : :
5305 : : to:
5306 : :
5307 : : (vec_duplicate:V (plus:S (reg:S R1) (reg:S R2)))
5308 : :
5309 : : The first might be done entirely in vector registers while the
5310 : : second might need a move between register files. */
5311 : 118 : tem = simplify_binary_operation (code, GET_MODE_INNER (mode),
5312 : : elt0, elt1);
5313 : 59 : if (tem)
5314 : 2 : return gen_vec_duplicate (mode, tem);
5315 : : }
5316 : :
5317 : : return 0;
5318 : : }
5319 : :
5320 : : /* Return true if binary operation OP distributes over addition in operand
5321 : : OPNO, with the other operand being held constant. OPNO counts from 1. */
5322 : :
5323 : : static bool
5324 : 24851 : distributes_over_addition_p (rtx_code op, int opno)
5325 : : {
5326 : 0 : switch (op)
5327 : : {
5328 : : case PLUS:
5329 : : case MINUS:
5330 : : case MULT:
5331 : : return true;
5332 : :
5333 : 0 : case ASHIFT:
5334 : 0 : return opno == 1;
5335 : :
5336 : 0 : default:
5337 : 0 : return false;
5338 : : }
5339 : : }
5340 : :
5341 : : rtx
5342 : 475480845 : simplify_const_binary_operation (enum rtx_code code, machine_mode mode,
5343 : : rtx op0, rtx op1)
5344 : : {
5345 : 475480845 : if (VECTOR_MODE_P (mode)
5346 : 14370075 : && code != VEC_CONCAT
5347 : 10214451 : && GET_CODE (op0) == CONST_VECTOR
5348 : 204812 : && GET_CODE (op1) == CONST_VECTOR)
5349 : : {
5350 : 25568 : bool step_ok_p;
5351 : 25568 : if (CONST_VECTOR_STEPPED_P (op0)
5352 : 25568 : && CONST_VECTOR_STEPPED_P (op1))
5353 : : /* We can operate directly on the encoding if:
5354 : :
5355 : : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
5356 : : implies
5357 : : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
5358 : :
5359 : : Addition and subtraction are the supported operators
5360 : : for which this is true. */
5361 : 717 : step_ok_p = (code == PLUS || code == MINUS);
5362 : 24851 : else if (CONST_VECTOR_STEPPED_P (op0))
5363 : : /* We can operate directly on stepped encodings if:
5364 : :
5365 : : a3 - a2 == a2 - a1
5366 : : implies:
5367 : : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
5368 : :
5369 : : which is true if (x -> x op c) distributes over addition. */
5370 : 1045 : step_ok_p = distributes_over_addition_p (code, 1);
5371 : : else
5372 : : /* Similarly in reverse. */
5373 : 23806 : step_ok_p = distributes_over_addition_p (code, 2);
5374 : 25568 : rtx_vector_builder builder;
5375 : 25568 : if (!builder.new_binary_operation (mode, op0, op1, step_ok_p))
5376 : : return 0;
5377 : :
5378 : 25568 : unsigned int count = builder.encoded_nelts ();
5379 : 104321 : for (unsigned int i = 0; i < count; i++)
5380 : : {
5381 : 157772 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
5382 : : CONST_VECTOR_ELT (op0, i),
5383 : 78886 : CONST_VECTOR_ELT (op1, i));
5384 : 78886 : if (!x || !valid_for_const_vector_p (mode, x))
5385 : 133 : return 0;
5386 : 78753 : builder.quick_push (x);
5387 : : }
5388 : 25435 : return builder.build ();
5389 : 25568 : }
5390 : :
5391 : 475455277 : if (VECTOR_MODE_P (mode)
5392 : 14344507 : && code == VEC_CONCAT
5393 : 4155624 : && (CONST_SCALAR_INT_P (op0)
5394 : 4005884 : || CONST_FIXED_P (op0)
5395 : 4005884 : || CONST_DOUBLE_AS_FLOAT_P (op0))
5396 : 152456 : && (CONST_SCALAR_INT_P (op1)
5397 : 150677 : || CONST_DOUBLE_AS_FLOAT_P (op1)
5398 : 149419 : || CONST_FIXED_P (op1)))
5399 : : {
5400 : : /* Both inputs have a constant number of elements, so the result
5401 : : must too. */
5402 : 3037 : unsigned n_elts = GET_MODE_NUNITS (mode).to_constant ();
5403 : 3037 : rtvec v = rtvec_alloc (n_elts);
5404 : :
5405 : 3037 : gcc_assert (n_elts >= 2);
5406 : 3037 : if (n_elts == 2)
5407 : : {
5408 : 3037 : gcc_assert (GET_CODE (op0) != CONST_VECTOR);
5409 : 3037 : gcc_assert (GET_CODE (op1) != CONST_VECTOR);
5410 : :
5411 : 3037 : RTVEC_ELT (v, 0) = op0;
5412 : 3037 : RTVEC_ELT (v, 1) = op1;
5413 : : }
5414 : : else
5415 : : {
5416 : 0 : unsigned op0_n_elts = GET_MODE_NUNITS (GET_MODE (op0)).to_constant ();
5417 : 0 : unsigned op1_n_elts = GET_MODE_NUNITS (GET_MODE (op1)).to_constant ();
5418 : 0 : unsigned i;
5419 : :
5420 : 0 : gcc_assert (GET_CODE (op0) == CONST_VECTOR);
5421 : 0 : gcc_assert (GET_CODE (op1) == CONST_VECTOR);
5422 : 0 : gcc_assert (op0_n_elts + op1_n_elts == n_elts);
5423 : :
5424 : 0 : for (i = 0; i < op0_n_elts; ++i)
5425 : 0 : RTVEC_ELT (v, i) = CONST_VECTOR_ELT (op0, i);
5426 : 0 : for (i = 0; i < op1_n_elts; ++i)
5427 : 0 : RTVEC_ELT (v, op0_n_elts+i) = CONST_VECTOR_ELT (op1, i);
5428 : : }
5429 : :
5430 : 3037 : return gen_rtx_CONST_VECTOR (mode, v);
5431 : : }
5432 : :
5433 : 464175575 : if (VECTOR_MODE_P (mode)
5434 : 14341470 : && GET_CODE (op0) == CONST_VECTOR
5435 : 199224 : && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1))
5436 : 475452240 : && (CONST_VECTOR_DUPLICATE_P (op0)
5437 : : || CONST_VECTOR_NUNITS (op0).is_constant ()))
5438 : : {
5439 : 138186 : switch (code)
5440 : : {
5441 : 138186 : case PLUS:
5442 : 138186 : case MINUS:
5443 : 138186 : case MULT:
5444 : 138186 : case DIV:
5445 : 138186 : case MOD:
5446 : 138186 : case UDIV:
5447 : 138186 : case UMOD:
5448 : 138186 : case AND:
5449 : 138186 : case IOR:
5450 : 138186 : case XOR:
5451 : 138186 : case SMIN:
5452 : 138186 : case SMAX:
5453 : 138186 : case UMIN:
5454 : 138186 : case UMAX:
5455 : 138186 : case LSHIFTRT:
5456 : 138186 : case ASHIFTRT:
5457 : 138186 : case ASHIFT:
5458 : 138186 : case ROTATE:
5459 : 138186 : case ROTATERT:
5460 : 138186 : case SS_PLUS:
5461 : 138186 : case US_PLUS:
5462 : 138186 : case SS_MINUS:
5463 : 138186 : case US_MINUS:
5464 : 138186 : case SS_ASHIFT:
5465 : 138186 : case US_ASHIFT:
5466 : 138186 : case COPYSIGN:
5467 : 138186 : break;
5468 : : default:
5469 : : return NULL_RTX;
5470 : : }
5471 : :
5472 : 138186 : unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op0)
5473 : 138186 : ? CONST_VECTOR_NPATTERNS (op0)
5474 : 145852 : : CONST_VECTOR_NUNITS (op0).to_constant ());
5475 : 138186 : rtx_vector_builder builder (mode, npatterns, 1);
5476 : 289346 : for (unsigned i = 0; i < npatterns; i++)
5477 : : {
5478 : 302320 : rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
5479 : 151160 : CONST_VECTOR_ELT (op0, i), op1);
5480 : 151160 : if (!x || !valid_for_const_vector_p (mode, x))
5481 : 0 : return 0;
5482 : 151160 : builder.quick_push (x);
5483 : : }
5484 : 138186 : return builder.build ();
5485 : : }
5486 : :
5487 : 475314054 : if (SCALAR_FLOAT_MODE_P (mode)
5488 : 6409172 : && CONST_DOUBLE_AS_FLOAT_P (op0)
5489 : 112594 : && CONST_DOUBLE_AS_FLOAT_P (op1)
5490 : 47932 : && mode == GET_MODE (op0) && mode == GET_MODE (op1))
5491 : : {
5492 : 47932 : if (code == AND
5493 : : || code == IOR
5494 : 47932 : || code == XOR)
5495 : : {
5496 : 38795 : long tmp0[4];
5497 : 38795 : long tmp1[4];
5498 : 38795 : REAL_VALUE_TYPE r;
5499 : 38795 : int i;
5500 : :
5501 : 38795 : real_to_target (tmp0, CONST_DOUBLE_REAL_VALUE (op0),
5502 : 38795 : GET_MODE (op0));
5503 : 38795 : real_to_target (tmp1, CONST_DOUBLE_REAL_VALUE (op1),
5504 : 38795 : GET_MODE (op1));
5505 : 193975 : for (i = 0; i < 4; i++)
5506 : : {
5507 : 155180 : switch (code)
5508 : : {
5509 : 150116 : case AND:
5510 : 150116 : tmp0[i] &= tmp1[i];
5511 : 150116 : break;
5512 : 2784 : case IOR:
5513 : 2784 : tmp0[i] |= tmp1[i];
5514 : 2784 : break;
5515 : 2280 : case XOR:
5516 : 2280 : tmp0[i] ^= tmp1[i];
5517 : 2280 : break;
5518 : : default:
5519 : : gcc_unreachable ();
5520 : : }
5521 : : }
5522 : 38795 : real_from_target (&r, tmp0, mode);
5523 : 38795 : return const_double_from_real_value (r, mode);
5524 : : }
5525 : 9137 : else if (code == COPYSIGN)
5526 : : {
5527 : 0 : REAL_VALUE_TYPE f0, f1;
5528 : 0 : real_convert (&f0, mode, CONST_DOUBLE_REAL_VALUE (op0));
5529 : 0 : real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (op1));
5530 : 0 : real_copysign (&f0, &f1);
5531 : 0 : return const_double_from_real_value (f0, mode);
5532 : : }
5533 : : else
5534 : : {
5535 : 9137 : REAL_VALUE_TYPE f0, f1, value, result;
5536 : 9137 : const REAL_VALUE_TYPE *opr0, *opr1;
5537 : 9137 : bool inexact;
5538 : :
5539 : 9137 : opr0 = CONST_DOUBLE_REAL_VALUE (op0);
5540 : 9137 : opr1 = CONST_DOUBLE_REAL_VALUE (op1);
5541 : :
5542 : 9137 : if (HONOR_SNANS (mode)
5543 : 9137 : && (REAL_VALUE_ISSIGNALING_NAN (*opr0)
5544 : 803 : || REAL_VALUE_ISSIGNALING_NAN (*opr1)))
5545 : 10 : return 0;
5546 : :
5547 : 9127 : real_convert (&f0, mode, opr0);
5548 : 9127 : real_convert (&f1, mode, opr1);
5549 : :
5550 : 9127 : if (code == DIV
5551 : 4171 : && real_equal (&f1, &dconst0)
5552 : 12830 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
5553 : 3699 : return 0;
5554 : :
5555 : 27039 : if (MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
5556 : 5336 : && flag_trapping_math
5557 : 5260 : && REAL_VALUE_ISINF (f0) && REAL_VALUE_ISINF (f1))
5558 : : {
5559 : 9 : int s0 = REAL_VALUE_NEGATIVE (f0);
5560 : 9 : int s1 = REAL_VALUE_NEGATIVE (f1);
5561 : :
5562 : 9 : switch (code)
5563 : : {
5564 : 0 : case PLUS:
5565 : : /* Inf + -Inf = NaN plus exception. */
5566 : 0 : if (s0 != s1)
5567 : : return 0;
5568 : : break;
5569 : 0 : case MINUS:
5570 : : /* Inf - Inf = NaN plus exception. */
5571 : 0 : if (s0 == s1)
5572 : : return 0;
5573 : : break;
5574 : : case DIV:
5575 : : /* Inf / Inf = NaN plus exception. */
5576 : : return 0;
5577 : : default:
5578 : : break;
5579 : : }
5580 : : }
5581 : :
5582 : 8120 : if (code == MULT && MODE_HAS_INFINITIES (mode) && HONOR_NANS (mode)
5583 : 1996 : && flag_trapping_math
5584 : 7369 : && ((REAL_VALUE_ISINF (f0) && real_equal (&f1, &dconst0))
5585 : 1942 : || (REAL_VALUE_ISINF (f1)
5586 : 16 : && real_equal (&f0, &dconst0))))
5587 : : /* Inf * 0 = NaN plus exception. */
5588 : 24 : return 0;
5589 : :
5590 : 5395 : inexact = real_arithmetic (&value, rtx_to_tree_code (code),
5591 : : &f0, &f1);
5592 : 5395 : real_convert (&result, mode, &value);
5593 : :
5594 : : /* Don't constant fold this floating point operation if
5595 : : the result has overflowed and flag_trapping_math. */
5596 : :
5597 : 5395 : if (flag_trapping_math
5598 : 20908 : && MODE_HAS_INFINITIES (mode)
5599 : 5227 : && REAL_VALUE_ISINF (result)
5600 : 1104 : && !REAL_VALUE_ISINF (f0)
5601 : 6485 : && !REAL_VALUE_ISINF (f1))
5602 : : /* Overflow plus exception. */
5603 : 1090 : return 0;
5604 : :
5605 : : /* Don't constant fold this floating point operation if the
5606 : : result may dependent upon the run-time rounding mode and
5607 : : flag_rounding_math is set, or if GCC's software emulation
5608 : : is unable to accurately represent the result. */
5609 : :
5610 : 4305 : if ((flag_rounding_math
5611 : 27454 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
5612 : 4305 : && (inexact || !real_identical (&result, &value)))
5613 : 378 : return NULL_RTX;
5614 : :
5615 : 3927 : return const_double_from_real_value (result, mode);
5616 : : }
5617 : : }
5618 : :
5619 : : /* We can fold some multi-word operations. */
5620 : 475266122 : scalar_int_mode int_mode;
5621 : 475266122 : if (is_a <scalar_int_mode> (mode, &int_mode)
5622 : 406563978 : && CONST_SCALAR_INT_P (op0)
5623 : 40055625 : && CONST_SCALAR_INT_P (op1)
5624 : 33626652 : && GET_MODE_PRECISION (int_mode) <= MAX_BITSIZE_MODE_ANY_INT)
5625 : : {
5626 : 33626652 : wide_int result;
5627 : 33626652 : wi::overflow_type overflow;
5628 : 33626652 : rtx_mode_t pop0 = rtx_mode_t (op0, int_mode);
5629 : 33626652 : rtx_mode_t pop1 = rtx_mode_t (op1, int_mode);
5630 : :
5631 : : #if TARGET_SUPPORTS_WIDE_INT == 0
5632 : : /* This assert keeps the simplification from producing a result
5633 : : that cannot be represented in a CONST_DOUBLE but a lot of
5634 : : upstream callers expect that this function never fails to
5635 : : simplify something and so you if you added this to the test
5636 : : above the code would die later anyway. If this assert
5637 : : happens, you just need to make the port support wide int. */
5638 : : gcc_assert (GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_DOUBLE_INT);
5639 : : #endif
5640 : 33626652 : switch (code)
5641 : : {
5642 : 1108067 : case MINUS:
5643 : 1108067 : result = wi::sub (pop0, pop1);
5644 : 1108067 : break;
5645 : :
5646 : 26399918 : case PLUS:
5647 : 26399918 : result = wi::add (pop0, pop1);
5648 : 26399918 : break;
5649 : :
5650 : 308192 : case MULT:
5651 : 308192 : result = wi::mul (pop0, pop1);
5652 : 308192 : break;
5653 : :
5654 : 5548 : case DIV:
5655 : 5548 : result = wi::div_trunc (pop0, pop1, SIGNED, &overflow);
5656 : 5548 : if (overflow)
5657 : : return NULL_RTX;
5658 : : break;
5659 : :
5660 : 217 : case MOD:
5661 : 217 : result = wi::mod_trunc (pop0, pop1, SIGNED, &overflow);
5662 : 217 : if (overflow)
5663 : : return NULL_RTX;
5664 : : break;
5665 : :
5666 : 6800 : case UDIV:
5667 : 6800 : result = wi::div_trunc (pop0, pop1, UNSIGNED, &overflow);
5668 : 6800 : if (overflow)
5669 : : return NULL_RTX;
5670 : : break;
5671 : :
5672 : 13911 : case UMOD:
5673 : 13911 : result = wi::mod_trunc (pop0, pop1, UNSIGNED, &overflow);
5674 : 13911 : if (overflow)
5675 : : return NULL_RTX;
5676 : : break;
5677 : :
5678 : 599524 : case AND:
5679 : 599524 : result = wi::bit_and (pop0, pop1);
5680 : 599524 : break;
5681 : :
5682 : 295681 : case IOR:
5683 : 295681 : result = wi::bit_or (pop0, pop1);
5684 : 295681 : break;
5685 : :
5686 : 89997 : case XOR:
5687 : 89997 : result = wi::bit_xor (pop0, pop1);
5688 : 89997 : break;
5689 : :
5690 : 1761 : case SMIN:
5691 : 1761 : result = wi::smin (pop0, pop1);
5692 : 1761 : break;
5693 : :
5694 : 1831 : case SMAX:
5695 : 1831 : result = wi::smax (pop0, pop1);
5696 : 1831 : break;
5697 : :
5698 : 3186 : case UMIN:
5699 : 3186 : result = wi::umin (pop0, pop1);
5700 : 3186 : break;
5701 : :
5702 : 2801 : case UMAX:
5703 : 2801 : result = wi::umax (pop0, pop1);
5704 : 2801 : break;
5705 : :
5706 : 4746699 : case LSHIFTRT:
5707 : 4746699 : case ASHIFTRT:
5708 : 4746699 : case ASHIFT:
5709 : 4746699 : case SS_ASHIFT:
5710 : 4746699 : case US_ASHIFT:
5711 : 4746699 : {
5712 : : /* The shift count might be in SImode while int_mode might
5713 : : be narrower. On IA-64 it is even DImode. If the shift
5714 : : count is too large and doesn't fit into int_mode, we'd
5715 : : ICE. So, if int_mode is narrower than word, use
5716 : : word_mode for the shift count. */
5717 : 4746699 : if (GET_MODE (op1) == VOIDmode
5718 : 5099884 : && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD)
5719 : 1555169 : pop1 = rtx_mode_t (op1, word_mode);
5720 : :
5721 : 4746699 : wide_int wop1 = pop1;
5722 : 4746699 : if (SHIFT_COUNT_TRUNCATED)
5723 : : wop1 = wi::umod_trunc (wop1, GET_MODE_PRECISION (int_mode));
5724 : 4746699 : else if (wi::geu_p (wop1, GET_MODE_PRECISION (int_mode)))
5725 : 124 : return NULL_RTX;
5726 : :
5727 : 4746575 : switch (code)
5728 : : {
5729 : 2676629 : case LSHIFTRT:
5730 : 2676629 : result = wi::lrshift (pop0, wop1);
5731 : 2676629 : break;
5732 : :
5733 : 57966 : case ASHIFTRT:
5734 : 57966 : result = wi::arshift (pop0, wop1);
5735 : 57966 : break;
5736 : :
5737 : 2011980 : case ASHIFT:
5738 : 2011980 : result = wi::lshift (pop0, wop1);
5739 : 2011980 : break;
5740 : :
5741 : 0 : case SS_ASHIFT:
5742 : 0 : if (wi::leu_p (wop1, wi::clrsb (pop0)))
5743 : 0 : result = wi::lshift (pop0, wop1);
5744 : 0 : else if (wi::neg_p (pop0))
5745 : 0 : result = wi::min_value (int_mode, SIGNED);
5746 : : else
5747 : 0 : result = wi::max_value (int_mode, SIGNED);
5748 : : break;
5749 : :
5750 : 0 : case US_ASHIFT:
5751 : 0 : if (wi::eq_p (pop0, 0))
5752 : 0 : result = pop0;
5753 : 0 : else if (wi::leu_p (wop1, wi::clz (pop0)))
5754 : 0 : result = wi::lshift (pop0, wop1);
5755 : : else
5756 : 0 : result = wi::max_value (int_mode, UNSIGNED);
5757 : : break;
5758 : :
5759 : 0 : default:
5760 : 0 : gcc_unreachable ();
5761 : : }
5762 : 4746575 : break;
5763 : 4746699 : }
5764 : 33955 : case ROTATE:
5765 : 33955 : case ROTATERT:
5766 : 33955 : {
5767 : : /* The rotate count might be in SImode while int_mode might
5768 : : be narrower. On IA-64 it is even DImode. If the shift
5769 : : count is too large and doesn't fit into int_mode, we'd
5770 : : ICE. So, if int_mode is narrower than word, use
5771 : : word_mode for the shift count. */
5772 : 33955 : if (GET_MODE (op1) == VOIDmode
5773 : 42730 : && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD)
5774 : 20051 : pop1 = rtx_mode_t (op1, word_mode);
5775 : :
5776 : 33955 : if (wi::neg_p (pop1))
5777 : : return NULL_RTX;
5778 : :
5779 : 33885 : switch (code)
5780 : : {
5781 : 11403 : case ROTATE:
5782 : 11403 : result = wi::lrotate (pop0, pop1);
5783 : 11403 : break;
5784 : :
5785 : 22482 : case ROTATERT:
5786 : 22482 : result = wi::rrotate (pop0, pop1);
5787 : 22482 : break;
5788 : :
5789 : 0 : default:
5790 : 0 : gcc_unreachable ();
5791 : : }
5792 : : break;
5793 : : }
5794 : :
5795 : 2270 : case SS_PLUS:
5796 : 2270 : result = wi::add (pop0, pop1, SIGNED, &overflow);
5797 : 4484 : clamp_signed_saturation:
5798 : 4484 : if (overflow == wi::OVF_OVERFLOW)
5799 : 314 : result = wi::max_value (GET_MODE_PRECISION (int_mode), SIGNED);
5800 : 4170 : else if (overflow == wi::OVF_UNDERFLOW)
5801 : 278 : result = wi::min_value (GET_MODE_PRECISION (int_mode), SIGNED);
5802 : 3892 : else if (overflow != wi::OVF_NONE)
5803 : : return NULL_RTX;
5804 : : break;
5805 : :
5806 : 2220 : case US_PLUS:
5807 : 2220 : result = wi::add (pop0, pop1, UNSIGNED, &overflow);
5808 : 2220 : clamp_unsigned_saturation:
5809 : 2220 : if (overflow != wi::OVF_NONE)
5810 : 461 : result = wi::max_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
5811 : : break;
5812 : :
5813 : 2214 : case SS_MINUS:
5814 : 2214 : result = wi::sub (pop0, pop1, SIGNED, &overflow);
5815 : 2214 : goto clamp_signed_saturation;
5816 : :
5817 : 1852 : case US_MINUS:
5818 : 1852 : result = wi::sub (pop0, pop1, UNSIGNED, &overflow);
5819 : 1852 : if (overflow != wi::OVF_NONE)
5820 : 1203 : result = wi::min_value (GET_MODE_PRECISION (int_mode), UNSIGNED);
5821 : : break;
5822 : :
5823 : 0 : case SS_MULT:
5824 : 0 : result = wi::mul (pop0, pop1, SIGNED, &overflow);
5825 : 0 : goto clamp_signed_saturation;
5826 : :
5827 : 0 : case US_MULT:
5828 : 0 : result = wi::mul (pop0, pop1, UNSIGNED, &overflow);
5829 : 0 : goto clamp_unsigned_saturation;
5830 : :
5831 : 8 : case SMUL_HIGHPART:
5832 : 8 : result = wi::mul_high (pop0, pop1, SIGNED);
5833 : 8 : break;
5834 : :
5835 : 0 : case UMUL_HIGHPART:
5836 : 0 : result = wi::mul_high (pop0, pop1, UNSIGNED);
5837 : 0 : break;
5838 : :
5839 : : default:
5840 : : return NULL_RTX;
5841 : : }
5842 : 33625925 : return immed_wide_int_const (result, int_mode);
5843 : 33626652 : }
5844 : :
5845 : : /* Handle polynomial integers. */
5846 : : if (NUM_POLY_INT_COEFFS > 1
5847 : : && is_a <scalar_int_mode> (mode, &int_mode)
5848 : : && poly_int_rtx_p (op0)
5849 : : && poly_int_rtx_p (op1))
5850 : : {
5851 : : poly_wide_int result;
5852 : : switch (code)
5853 : : {
5854 : : case PLUS:
5855 : : result = wi::to_poly_wide (op0, mode) + wi::to_poly_wide (op1, mode);
5856 : : break;
5857 : :
5858 : : case MINUS:
5859 : : result = wi::to_poly_wide (op0, mode) - wi::to_poly_wide (op1, mode);
5860 : : break;
5861 : :
5862 : : case MULT:
5863 : : if (CONST_SCALAR_INT_P (op1))
5864 : : result = wi::to_poly_wide (op0, mode) * rtx_mode_t (op1, mode);
5865 : : else
5866 : : return NULL_RTX;
5867 : : break;
5868 : :
5869 : : case ASHIFT:
5870 : : if (CONST_SCALAR_INT_P (op1))
5871 : : {
5872 : : wide_int shift
5873 : : = rtx_mode_t (op1,
5874 : : GET_MODE (op1) == VOIDmode
5875 : : && GET_MODE_PRECISION (int_mode) < BITS_PER_WORD
5876 : : ? word_mode : mode);
5877 : : if (SHIFT_COUNT_TRUNCATED)
5878 : : shift = wi::umod_trunc (shift, GET_MODE_PRECISION (int_mode));
5879 : : else if (wi::geu_p (shift, GET_MODE_PRECISION (int_mode)))
5880 : : return NULL_RTX;
5881 : : result = wi::to_poly_wide (op0, mode) << shift;
5882 : : }
5883 : : else
5884 : : return NULL_RTX;
5885 : : break;
5886 : :
5887 : : case IOR:
5888 : : if (!CONST_SCALAR_INT_P (op1)
5889 : : || !can_ior_p (wi::to_poly_wide (op0, mode),
5890 : : rtx_mode_t (op1, mode), &result))
5891 : : return NULL_RTX;
5892 : : break;
5893 : :
5894 : : default:
5895 : : return NULL_RTX;
5896 : : }
5897 : : return immed_wide_int_const (result, int_mode);
5898 : : }
5899 : :
5900 : : return NULL_RTX;
5901 : : }
5902 : :
5903 : :
5904 : :
5905 : : /* Return a positive integer if X should sort after Y. The value
5906 : : returned is 1 if and only if X and Y are both regs. */
5907 : :
5908 : : static int
5909 : 112324783 : simplify_plus_minus_op_data_cmp (rtx x, rtx y)
5910 : : {
5911 : 112324783 : int result;
5912 : :
5913 : 112324783 : result = (commutative_operand_precedence (y)
5914 : 112324783 : - commutative_operand_precedence (x));
5915 : 112324783 : if (result)
5916 : 78465376 : return result + result;
5917 : :
5918 : : /* Group together equal REGs to do more simplification. */
5919 : 33859407 : if (REG_P (x) && REG_P (y))
5920 : 8225016 : return REGNO (x) > REGNO (y);
5921 : :
5922 : : return 0;
5923 : : }
5924 : :
5925 : : /* Simplify and canonicalize a PLUS or MINUS, at least one of whose
5926 : : operands may be another PLUS or MINUS.
5927 : :
5928 : : Rather than test for specific case, we do this by a brute-force method
5929 : : and do all possible simplifications until no more changes occur. Then
5930 : : we rebuild the operation.
5931 : :
5932 : : May return NULL_RTX when no changes were made. */
5933 : :
5934 : : rtx
5935 : 37700303 : simplify_context::simplify_plus_minus (rtx_code code, machine_mode mode,
5936 : : rtx op0, rtx op1)
5937 : : {
5938 : 37700303 : struct simplify_plus_minus_op_data
5939 : : {
5940 : : rtx op;
5941 : : short neg;
5942 : : } ops[16];
5943 : 37700303 : rtx result, tem;
5944 : 37700303 : int n_ops = 2;
5945 : 37700303 : int changed, n_constants, canonicalized = 0;
5946 : 37700303 : int i, j;
5947 : :
5948 : 37700303 : memset (ops, 0, sizeof ops);
5949 : :
5950 : : /* Set up the two operands and then expand them until nothing has been
5951 : : changed. If we run out of room in our array, give up; this should
5952 : : almost never happen. */
5953 : :
5954 : 37700303 : ops[0].op = op0;
5955 : 37700303 : ops[0].neg = 0;
5956 : 37700303 : ops[1].op = op1;
5957 : 37700303 : ops[1].neg = (code == MINUS);
5958 : :
5959 : 76697416 : do
5960 : : {
5961 : 76697416 : changed = 0;
5962 : 76697416 : n_constants = 0;
5963 : :
5964 : 310539003 : for (i = 0; i < n_ops; i++)
5965 : : {
5966 : 233841601 : rtx this_op = ops[i].op;
5967 : 233841601 : int this_neg = ops[i].neg;
5968 : 233841601 : enum rtx_code this_code = GET_CODE (this_op);
5969 : :
5970 : 233841601 : switch (this_code)
5971 : : {
5972 : 38155337 : case PLUS:
5973 : 38155337 : case MINUS:
5974 : 38155337 : if (n_ops == ARRAY_SIZE (ops))
5975 : : return NULL_RTX;
5976 : :
5977 : 38155323 : ops[n_ops].op = XEXP (this_op, 1);
5978 : 38155323 : ops[n_ops].neg = (this_code == MINUS) ^ this_neg;
5979 : 38155323 : n_ops++;
5980 : :
5981 : 38155323 : ops[i].op = XEXP (this_op, 0);
5982 : 38155323 : changed = 1;
5983 : : /* If this operand was negated then we will potentially
5984 : : canonicalize the expression. Similarly if we don't
5985 : : place the operands adjacent we're re-ordering the
5986 : : expression and thus might be performing a
5987 : : canonicalization. Ignore register re-ordering.
5988 : : ??? It might be better to shuffle the ops array here,
5989 : : but then (plus (plus (A, B), plus (C, D))) wouldn't
5990 : : be seen as non-canonical. */
5991 : 38155323 : if (this_neg
5992 : 37432898 : || (i != n_ops - 2
5993 : 36757769 : && !(REG_P (ops[i].op) && REG_P (ops[n_ops - 1].op))))
5994 : 233841587 : canonicalized = 1;
5995 : : break;
5996 : :
5997 : 1539 : case NEG:
5998 : 1539 : ops[i].op = XEXP (this_op, 0);
5999 : 1539 : ops[i].neg = ! this_neg;
6000 : 1539 : changed = 1;
6001 : 1539 : canonicalized = 1;
6002 : 1539 : break;
6003 : :
6004 : 1467586 : case CONST:
6005 : 1467586 : if (n_ops != ARRAY_SIZE (ops)
6006 : 1467586 : && GET_CODE (XEXP (this_op, 0)) == PLUS
6007 : 1347721 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 0))
6008 : 1324508 : && CONSTANT_P (XEXP (XEXP (this_op, 0), 1)))
6009 : : {
6010 : 1324508 : ops[i].op = XEXP (XEXP (this_op, 0), 0);
6011 : 1324508 : ops[n_ops].op = XEXP (XEXP (this_op, 0), 1);
6012 : 1324508 : ops[n_ops].neg = this_neg;
6013 : 1324508 : n_ops++;
6014 : 1324508 : changed = 1;
6015 : 1324508 : canonicalized = 1;
6016 : : }
6017 : : break;
6018 : :
6019 : 40687 : case NOT:
6020 : : /* ~a -> (-a - 1) */
6021 : 40687 : if (n_ops != ARRAY_SIZE (ops))
6022 : : {
6023 : 40687 : ops[n_ops].op = CONSTM1_RTX (mode);
6024 : 40687 : ops[n_ops++].neg = this_neg;
6025 : 40687 : ops[i].op = XEXP (this_op, 0);
6026 : 40687 : ops[i].neg = !this_neg;
6027 : 40687 : changed = 1;
6028 : 40687 : canonicalized = 1;
6029 : : }
6030 : : break;
6031 : :
6032 : 118769445 : CASE_CONST_SCALAR_INT:
6033 : 118769445 : case CONST_POLY_INT:
6034 : 118769445 : n_constants++;
6035 : 118769445 : if (this_neg)
6036 : : {
6037 : 1179756 : ops[i].op = neg_poly_int_rtx (mode, this_op);
6038 : 1179756 : ops[i].neg = 0;
6039 : 1179756 : changed = 1;
6040 : 1179756 : canonicalized = 1;
6041 : : }
6042 : : break;
6043 : :
6044 : : default:
6045 : : break;
6046 : : }
6047 : : }
6048 : : }
6049 : 76697402 : while (changed);
6050 : :
6051 : 37700289 : if (n_constants > 1)
6052 : 23949603 : canonicalized = 1;
6053 : :
6054 : 37700289 : gcc_assert (n_ops >= 2);
6055 : :
6056 : : /* If we only have two operands, we can avoid the loops. */
6057 : 37700289 : if (n_ops == 2)
6058 : : {
6059 : 0 : enum rtx_code code = ops[0].neg || ops[1].neg ? MINUS : PLUS;
6060 : 0 : rtx lhs, rhs;
6061 : :
6062 : : /* Get the two operands. Be careful with the order, especially for
6063 : : the cases where code == MINUS. */
6064 : 0 : if (ops[0].neg && ops[1].neg)
6065 : : {
6066 : 0 : lhs = gen_rtx_NEG (mode, ops[0].op);
6067 : 0 : rhs = ops[1].op;
6068 : : }
6069 : 0 : else if (ops[0].neg)
6070 : : {
6071 : 0 : lhs = ops[1].op;
6072 : 0 : rhs = ops[0].op;
6073 : : }
6074 : : else
6075 : : {
6076 : 0 : lhs = ops[0].op;
6077 : 0 : rhs = ops[1].op;
6078 : : }
6079 : :
6080 : 0 : return simplify_const_binary_operation (code, mode, lhs, rhs);
6081 : : }
6082 : :
6083 : : /* Now simplify each pair of operands until nothing changes. */
6084 : 62436486 : while (1)
6085 : : {
6086 : : /* Insertion sort is good enough for a small array. */
6087 : 164829072 : for (i = 1; i < n_ops; i++)
6088 : : {
6089 : 102392586 : struct simplify_plus_minus_op_data save;
6090 : 102392586 : int cmp;
6091 : :
6092 : 102392586 : j = i - 1;
6093 : 102392586 : cmp = simplify_plus_minus_op_data_cmp (ops[j].op, ops[i].op);
6094 : 102392586 : if (cmp <= 0)
6095 : 90711094 : continue;
6096 : : /* Just swapping registers doesn't count as canonicalization. */
6097 : 11681492 : if (cmp != 1)
6098 : 8849223 : canonicalized = 1;
6099 : :
6100 : 11681492 : save = ops[i];
6101 : 14023881 : do
6102 : 14023881 : ops[j + 1] = ops[j];
6103 : 14023881 : while (j--
6104 : 25705373 : && simplify_plus_minus_op_data_cmp (ops[j].op, save.op) > 0);
6105 : 11681492 : ops[j + 1] = save;
6106 : : }
6107 : :
6108 : 62436486 : changed = 0;
6109 : 164829072 : for (i = n_ops - 1; i > 0; i--)
6110 : 245638503 : for (j = i - 1; j >= 0; j--)
6111 : : {
6112 : 144042365 : rtx lhs = ops[j].op, rhs = ops[i].op;
6113 : 144042365 : int lneg = ops[j].neg, rneg = ops[i].neg;
6114 : :
6115 : 144042365 : if (lhs != 0 && rhs != 0)
6116 : : {
6117 : 118514132 : enum rtx_code ncode = PLUS;
6118 : :
6119 : 118514132 : if (lneg != rneg)
6120 : : {
6121 : 10057336 : ncode = MINUS;
6122 : 10057336 : if (lneg)
6123 : 6509641 : std::swap (lhs, rhs);
6124 : : }
6125 : 108456796 : else if (swap_commutative_operands_p (lhs, rhs))
6126 : 209257 : std::swap (lhs, rhs);
6127 : :
6128 : 118514132 : if ((GET_CODE (lhs) == CONST || CONST_INT_P (lhs))
6129 : 28246106 : && (GET_CODE (rhs) == CONST || CONST_INT_P (rhs)))
6130 : : {
6131 : 24100685 : rtx tem_lhs, tem_rhs;
6132 : :
6133 : 24100685 : tem_lhs = GET_CODE (lhs) == CONST ? XEXP (lhs, 0) : lhs;
6134 : 24100685 : tem_rhs = GET_CODE (rhs) == CONST ? XEXP (rhs, 0) : rhs;
6135 : 24100685 : tem = simplify_binary_operation (ncode, mode, tem_lhs,
6136 : : tem_rhs);
6137 : :
6138 : 24100685 : if (tem && !CONSTANT_P (tem))
6139 : 1740 : tem = gen_rtx_CONST (GET_MODE (tem), tem);
6140 : : }
6141 : : else
6142 : 94413447 : tem = simplify_binary_operation (ncode, mode, lhs, rhs);
6143 : :
6144 : 94415187 : if (tem)
6145 : : {
6146 : : /* Reject "simplifications" that just wrap the two
6147 : : arguments in a CONST. Failure to do so can result
6148 : : in infinite recursion with simplify_binary_operation
6149 : : when it calls us to simplify CONST operations.
6150 : : Also, if we find such a simplification, don't try
6151 : : any more combinations with this rhs: We must have
6152 : : something like symbol+offset, ie. one of the
6153 : : trivial CONST expressions we handle later. */
6154 : 26019951 : if (GET_CODE (tem) == CONST
6155 : 798188 : && GET_CODE (XEXP (tem, 0)) == ncode
6156 : 797640 : && XEXP (XEXP (tem, 0), 0) == lhs
6157 : 796448 : && XEXP (XEXP (tem, 0), 1) == rhs)
6158 : : break;
6159 : 25223503 : lneg &= rneg;
6160 : 25223503 : if (GET_CODE (tem) == NEG)
6161 : 44919 : tem = XEXP (tem, 0), lneg = !lneg;
6162 : 25223503 : if (poly_int_rtx_p (tem) && lneg)
6163 : 0 : tem = neg_poly_int_rtx (mode, tem), lneg = 0;
6164 : :
6165 : 25223503 : ops[i].op = tem;
6166 : 25223503 : ops[i].neg = lneg;
6167 : 25223503 : ops[j].op = NULL_RTX;
6168 : 25223503 : changed = 1;
6169 : 25223503 : canonicalized = 1;
6170 : : }
6171 : : }
6172 : : }
6173 : :
6174 : 62436486 : if (!changed)
6175 : : break;
6176 : :
6177 : : /* Pack all the operands to the lower-numbered entries. */
6178 : 99867872 : for (i = 0, j = 0; j < n_ops; j++)
6179 : 75131675 : if (ops[j].op)
6180 : : {
6181 : 49908172 : ops[i] = ops[j];
6182 : 49908172 : i++;
6183 : : }
6184 : : n_ops = i;
6185 : : }
6186 : :
6187 : : /* If nothing changed, check that rematerialization of rtl instructions
6188 : : is still required. */
6189 : 37700289 : if (!canonicalized)
6190 : : {
6191 : : /* Perform rematerialization if only all operands are registers and
6192 : : all operations are PLUS. */
6193 : : /* ??? Also disallow (non-global, non-frame) fixed registers to work
6194 : : around rs6000 and how it uses the CA register. See PR67145. */
6195 : 4959411 : for (i = 0; i < n_ops; i++)
6196 : 3994342 : if (ops[i].neg
6197 : 3741210 : || !REG_P (ops[i].op)
6198 : 7171789 : || (REGNO (ops[i].op) < FIRST_PSEUDO_REGISTER
6199 : 309982 : && fixed_regs[REGNO (ops[i].op)]
6200 : 222 : && !global_regs[REGNO (ops[i].op)]
6201 : 222 : && ops[i].op != frame_pointer_rtx
6202 : 117 : && ops[i].op != arg_pointer_rtx
6203 : 103 : && ops[i].op != stack_pointer_rtx))
6204 : : return NULL_RTX;
6205 : 965069 : goto gen_result;
6206 : : }
6207 : :
6208 : : /* Create (minus -C X) instead of (neg (const (plus X C))). */
6209 : 35918325 : if (n_ops == 2
6210 : 23053555 : && CONST_INT_P (ops[1].op)
6211 : 22602055 : && CONSTANT_P (ops[0].op)
6212 : 210 : && ops[0].neg)
6213 : 104 : return gen_rtx_fmt_ee (MINUS, mode, ops[1].op, ops[0].op);
6214 : :
6215 : : /* We suppressed creation of trivial CONST expressions in the
6216 : : combination loop to avoid recursion. Create one manually now.
6217 : : The combination loop should have ensured that there is exactly
6218 : : one CONST_INT, and the sort will have ensured that it is last
6219 : : in the array and that any other constant will be next-to-last. */
6220 : :
6221 : 35918221 : if (n_ops > 1
6222 : 35364421 : && poly_int_rtx_p (ops[n_ops - 1].op)
6223 : 69113451 : && CONSTANT_P (ops[n_ops - 2].op))
6224 : : {
6225 : 1391590 : rtx value = ops[n_ops - 1].op;
6226 : 1391590 : if (ops[n_ops - 1].neg ^ ops[n_ops - 2].neg)
6227 : 681243 : value = neg_poly_int_rtx (mode, value);
6228 : 1391590 : if (CONST_INT_P (value))
6229 : : {
6230 : 2783180 : ops[n_ops - 2].op = plus_constant (mode, ops[n_ops - 2].op,
6231 : 1391590 : INTVAL (value));
6232 : 1391590 : n_ops--;
6233 : : }
6234 : : }
6235 : :
6236 : : /* Put a non-negated operand first, if possible. */
6237 : :
6238 : 37569930 : for (i = 0; i < n_ops && ops[i].neg; i++)
6239 : 1651709 : continue;
6240 : 35918221 : if (i == n_ops)
6241 : 9239 : ops[0].op = gen_rtx_NEG (mode, ops[0].op);
6242 : 35908982 : else if (i != 0)
6243 : : {
6244 : 1552497 : tem = ops[0].op;
6245 : 1552497 : ops[0] = ops[i];
6246 : 1552497 : ops[i].op = tem;
6247 : 1552497 : ops[i].neg = 1;
6248 : : }
6249 : :
6250 : : /* Now make the result by performing the requested operations. */
6251 : 34356485 : gen_result:
6252 : 36883290 : result = ops[0].op;
6253 : 85853108 : for (i = 1; i < n_ops; i++)
6254 : 97939636 : result = gen_rtx_fmt_ee (ops[i].neg ? MINUS : PLUS,
6255 : : mode, result, ops[i].op);
6256 : :
6257 : : return result;
6258 : 1651709 : }
6259 : :
6260 : : /* Check whether an operand is suitable for calling simplify_plus_minus. */
6261 : : static bool
6262 : 509301218 : plus_minus_operand_p (const_rtx x)
6263 : : {
6264 : 509301218 : return GET_CODE (x) == PLUS
6265 : 509301218 : || GET_CODE (x) == MINUS
6266 : 509301218 : || (GET_CODE (x) == CONST
6267 : 1832088 : && GET_CODE (XEXP (x, 0)) == PLUS
6268 : 1232903 : && CONSTANT_P (XEXP (XEXP (x, 0), 0))
6269 : 1156300 : && CONSTANT_P (XEXP (XEXP (x, 0), 1)));
6270 : : }
6271 : :
6272 : : /* Like simplify_binary_operation except used for relational operators.
6273 : : MODE is the mode of the result. If MODE is VOIDmode, both operands must
6274 : : not also be VOIDmode.
6275 : :
6276 : : CMP_MODE specifies in which mode the comparison is done in, so it is
6277 : : the mode of the operands. If CMP_MODE is VOIDmode, it is taken from
6278 : : the operands or, if both are VOIDmode, the operands are compared in
6279 : : "infinite precision". */
6280 : : rtx
6281 : 128245162 : simplify_context::simplify_relational_operation (rtx_code code,
6282 : : machine_mode mode,
6283 : : machine_mode cmp_mode,
6284 : : rtx op0, rtx op1)
6285 : : {
6286 : 128245162 : rtx tem, trueop0, trueop1;
6287 : :
6288 : 128245162 : if (cmp_mode == VOIDmode)
6289 : 28112015 : cmp_mode = GET_MODE (op0);
6290 : 28112015 : if (cmp_mode == VOIDmode)
6291 : 456018 : cmp_mode = GET_MODE (op1);
6292 : :
6293 : 128245162 : tem = simplify_const_relational_operation (code, cmp_mode, op0, op1);
6294 : 128245162 : if (tem)
6295 : 844962 : return relational_result (mode, cmp_mode, tem);
6296 : :
6297 : : /* For the following tests, ensure const0_rtx is op1. */
6298 : 127400200 : if (swap_commutative_operands_p (op0, op1)
6299 : 127400200 : || (op0 == const0_rtx && op1 != const0_rtx))
6300 : 2585733 : std::swap (op0, op1), code = swap_condition (code);
6301 : :
6302 : : /* If op0 is a compare, extract the comparison arguments from it. */
6303 : 127400200 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6304 : 13649467 : return simplify_gen_relational (code, mode, VOIDmode,
6305 : 13649467 : XEXP (op0, 0), XEXP (op0, 1));
6306 : :
6307 : 113750733 : if (GET_MODE_CLASS (cmp_mode) == MODE_CC)
6308 : : return NULL_RTX;
6309 : :
6310 : 83478861 : trueop0 = avoid_constant_pool_reference (op0);
6311 : 83478861 : trueop1 = avoid_constant_pool_reference (op1);
6312 : 83478861 : return simplify_relational_operation_1 (code, mode, cmp_mode,
6313 : 83478861 : trueop0, trueop1);
6314 : : }
6315 : :
6316 : : /* This part of simplify_relational_operation is only used when CMP_MODE
6317 : : is not in class MODE_CC (i.e. it is a real comparison).
6318 : :
6319 : : MODE is the mode of the result, while CMP_MODE specifies in which
6320 : : mode the comparison is done in, so it is the mode of the operands. */
6321 : :
6322 : : rtx
6323 : 83478861 : simplify_context::simplify_relational_operation_1 (rtx_code code,
6324 : : machine_mode mode,
6325 : : machine_mode cmp_mode,
6326 : : rtx op0, rtx op1)
6327 : : {
6328 : 83478861 : enum rtx_code op0code = GET_CODE (op0);
6329 : :
6330 : 83478861 : if (op1 == const0_rtx && COMPARISON_P (op0))
6331 : : {
6332 : : /* If op0 is a comparison, extract the comparison arguments
6333 : : from it. */
6334 : 469451 : if (code == NE)
6335 : : {
6336 : 216942 : if (GET_MODE (op0) == mode)
6337 : 164 : return simplify_rtx (op0);
6338 : : else
6339 : 216778 : return simplify_gen_relational (GET_CODE (op0), mode, VOIDmode,
6340 : 216778 : XEXP (op0, 0), XEXP (op0, 1));
6341 : : }
6342 : 252509 : else if (code == EQ)
6343 : : {
6344 : 127090 : enum rtx_code new_code = reversed_comparison_code (op0, NULL);
6345 : 127090 : if (new_code != UNKNOWN)
6346 : 126774 : return simplify_gen_relational (new_code, mode, VOIDmode,
6347 : 126774 : XEXP (op0, 0), XEXP (op0, 1));
6348 : : }
6349 : : }
6350 : :
6351 : : /* (LTU/GEU (PLUS a C) C), where C is constant, can be simplified to
6352 : : (GEU/LTU a -C). Likewise for (LTU/GEU (PLUS a C) a). */
6353 : 83135145 : if ((code == LTU || code == GEU)
6354 : 4784758 : && GET_CODE (op0) == PLUS
6355 : 667679 : && CONST_INT_P (XEXP (op0, 1))
6356 : 445214 : && (rtx_equal_p (op1, XEXP (op0, 0))
6357 : 327085 : || rtx_equal_p (op1, XEXP (op0, 1)))
6358 : : /* (LTU/GEU (PLUS a 0) 0) is not the same as (GEU/LTU a 0). */
6359 : 83319763 : && XEXP (op0, 1) != const0_rtx)
6360 : : {
6361 : 184618 : rtx new_cmp
6362 : 184618 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6363 : 186849 : return simplify_gen_relational ((code == LTU ? GEU : LTU), mode,
6364 : 184618 : cmp_mode, XEXP (op0, 0), new_cmp);
6365 : : }
6366 : :
6367 : : /* (GTU (PLUS a C) (C - 1)) where C is a non-zero constant can be
6368 : : transformed into (LTU a -C). */
6369 : 82950527 : if (code == GTU && GET_CODE (op0) == PLUS && CONST_INT_P (op1)
6370 : 354466 : && CONST_INT_P (XEXP (op0, 1))
6371 : 284899 : && (UINTVAL (op1) == UINTVAL (XEXP (op0, 1)) - 1)
6372 : 28468 : && XEXP (op0, 1) != const0_rtx)
6373 : : {
6374 : 28468 : rtx new_cmp
6375 : 28468 : = simplify_gen_unary (NEG, cmp_mode, XEXP (op0, 1), cmp_mode);
6376 : 28468 : return simplify_gen_relational (LTU, mode, cmp_mode,
6377 : 28468 : XEXP (op0, 0), new_cmp);
6378 : : }
6379 : :
6380 : : /* Canonicalize (LTU/GEU (PLUS a b) b) as (LTU/GEU (PLUS a b) a). */
6381 : 82922059 : if ((code == LTU || code == GEU)
6382 : 4600140 : && GET_CODE (op0) == PLUS
6383 : 483061 : && rtx_equal_p (op1, XEXP (op0, 1))
6384 : : /* Don't recurse "infinitely" for (LTU/GEU (PLUS b b) b). */
6385 : 82927280 : && !rtx_equal_p (op1, XEXP (op0, 0)))
6386 : 5221 : return simplify_gen_relational (code, mode, cmp_mode, op0,
6387 : 5221 : copy_rtx (XEXP (op0, 0)));
6388 : :
6389 : 82916838 : if (op1 == const0_rtx)
6390 : : {
6391 : : /* Canonicalize (GTU x 0) as (NE x 0). */
6392 : 37073373 : if (code == GTU)
6393 : 175803 : return simplify_gen_relational (NE, mode, cmp_mode, op0, op1);
6394 : : /* Canonicalize (LEU x 0) as (EQ x 0). */
6395 : 36897570 : if (code == LEU)
6396 : 51401 : return simplify_gen_relational (EQ, mode, cmp_mode, op0, op1);
6397 : : }
6398 : 45843465 : else if (op1 == const1_rtx)
6399 : : {
6400 : 3126268 : switch (code)
6401 : : {
6402 : 8971 : case GE:
6403 : : /* Canonicalize (GE x 1) as (GT x 0). */
6404 : 8971 : return simplify_gen_relational (GT, mode, cmp_mode,
6405 : 8971 : op0, const0_rtx);
6406 : 173876 : case GEU:
6407 : : /* Canonicalize (GEU x 1) as (NE x 0). */
6408 : 173876 : return simplify_gen_relational (NE, mode, cmp_mode,
6409 : 173876 : op0, const0_rtx);
6410 : 10925 : case LT:
6411 : : /* Canonicalize (LT x 1) as (LE x 0). */
6412 : 10925 : return simplify_gen_relational (LE, mode, cmp_mode,
6413 : 10925 : op0, const0_rtx);
6414 : 43350 : case LTU:
6415 : : /* Canonicalize (LTU x 1) as (EQ x 0). */
6416 : 43350 : return simplify_gen_relational (EQ, mode, cmp_mode,
6417 : 43350 : op0, const0_rtx);
6418 : : default:
6419 : : break;
6420 : : }
6421 : : }
6422 : 42717197 : else if (op1 == constm1_rtx)
6423 : : {
6424 : : /* Canonicalize (LE x -1) as (LT x 0). */
6425 : 1151272 : if (code == LE)
6426 : 1576 : return simplify_gen_relational (LT, mode, cmp_mode, op0, const0_rtx);
6427 : : /* Canonicalize (GT x -1) as (GE x 0). */
6428 : 1149696 : if (code == GT)
6429 : 5136 : return simplify_gen_relational (GE, mode, cmp_mode, op0, const0_rtx);
6430 : : }
6431 : :
6432 : : /* (eq/ne (plus x cst1) cst2) simplifies to (eq/ne x (cst2 - cst1)) */
6433 : 82445800 : if ((code == EQ || code == NE)
6434 : 62261777 : && (op0code == PLUS || op0code == MINUS)
6435 : 2272642 : && CONSTANT_P (op1)
6436 : 952695 : && CONSTANT_P (XEXP (op0, 1))
6437 : 532768 : && (INTEGRAL_MODE_P (cmp_mode) || flag_unsafe_math_optimizations))
6438 : : {
6439 : 532736 : rtx x = XEXP (op0, 0);
6440 : 532736 : rtx c = XEXP (op0, 1);
6441 : 532736 : enum rtx_code invcode = op0code == PLUS ? MINUS : PLUS;
6442 : 532736 : rtx tem = simplify_gen_binary (invcode, cmp_mode, op1, c);
6443 : :
6444 : : /* Detect an infinite recursive condition, where we oscillate at this
6445 : : simplification case between:
6446 : : A + B == C <---> C - B == A,
6447 : : where A, B, and C are all constants with non-simplifiable expressions,
6448 : : usually SYMBOL_REFs. */
6449 : 532736 : if (GET_CODE (tem) == invcode
6450 : 46 : && CONSTANT_P (x)
6451 : 532754 : && rtx_equal_p (c, XEXP (tem, 1)))
6452 : : return NULL_RTX;
6453 : :
6454 : 532718 : return simplify_gen_relational (code, mode, cmp_mode, x, tem);
6455 : : }
6456 : :
6457 : : /* (ne:SI (zero_extract:SI FOO (const_int 1) BAR) (const_int 0))) is
6458 : : the same as (zero_extract:SI FOO (const_int 1) BAR). */
6459 : 61729041 : scalar_int_mode int_mode, int_cmp_mode;
6460 : 61729041 : if (code == NE
6461 : 32930259 : && op1 == const0_rtx
6462 : 2185038 : && is_int_mode (mode, &int_mode)
6463 : 81858224 : && is_a <scalar_int_mode> (cmp_mode, &int_cmp_mode)
6464 : : /* ??? Work-around BImode bugs in the ia64 backend. */
6465 : 2185038 : && int_mode != BImode
6466 : 2185018 : && int_cmp_mode != BImode
6467 : 2185018 : && nonzero_bits (op0, int_cmp_mode) == 1
6468 : 61729041 : && STORE_FLAG_VALUE == 1)
6469 : 109680 : return GET_MODE_SIZE (int_mode) > GET_MODE_SIZE (int_cmp_mode)
6470 : 54840 : ? simplify_gen_unary (ZERO_EXTEND, int_mode, op0, int_cmp_mode)
6471 : 22598 : : lowpart_subreg (int_mode, op0, int_cmp_mode);
6472 : :
6473 : : /* (eq/ne (xor x y) 0) simplifies to (eq/ne x y). */
6474 : 81858224 : if ((code == EQ || code == NE)
6475 : 61674201 : && op1 == const0_rtx
6476 : 33157494 : && op0code == XOR)
6477 : 14054 : return simplify_gen_relational (code, mode, cmp_mode,
6478 : 14054 : XEXP (op0, 0), XEXP (op0, 1));
6479 : :
6480 : : /* (eq/ne (xor x y) x) simplifies to (eq/ne y 0). */
6481 : 61660147 : if ((code == EQ || code == NE)
6482 : 61660147 : && op0code == XOR
6483 : 4519 : && rtx_equal_p (XEXP (op0, 0), op1)
6484 : 0 : && !side_effects_p (XEXP (op0, 0)))
6485 : 0 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 1),
6486 : 0 : CONST0_RTX (mode));
6487 : :
6488 : : /* Likewise (eq/ne (xor x y) y) simplifies to (eq/ne x 0). */
6489 : 81844170 : if ((code == EQ || code == NE)
6490 : 61660147 : && op0code == XOR
6491 : 4519 : && rtx_equal_p (XEXP (op0, 1), op1)
6492 : 81844304 : && !side_effects_p (XEXP (op0, 1)))
6493 : 134 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6494 : 134 : CONST0_RTX (mode));
6495 : :
6496 : : /* (eq/ne (xor x C1) C2) simplifies to (eq/ne x (C1^C2)). */
6497 : 81844036 : if ((code == EQ || code == NE)
6498 : 61660013 : && op0code == XOR
6499 : 4385 : && CONST_SCALAR_INT_P (op1)
6500 : 1132 : && CONST_SCALAR_INT_P (XEXP (op0, 1)))
6501 : 602 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6502 : : simplify_gen_binary (XOR, cmp_mode,
6503 : 602 : XEXP (op0, 1), op1));
6504 : :
6505 : : /* Simplify eq/ne (and/ior x y) x/y) for targets with a BICS instruction or
6506 : : constant folding if x/y is a constant. */
6507 : 61659411 : if ((code == EQ || code == NE)
6508 : 61659411 : && (op0code == AND || op0code == IOR)
6509 : 3901252 : && !side_effects_p (op1)
6510 : 3901208 : && op1 != CONST0_RTX (cmp_mode))
6511 : : {
6512 : : /* Both (eq/ne (and x y) x) and (eq/ne (ior x y) y) simplify to
6513 : : (eq/ne (and (not y) x) 0). */
6514 : 522540 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 0), op1))
6515 : 1042086 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 1), op1)))
6516 : : {
6517 : 25240 : rtx not_y = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 1),
6518 : : cmp_mode);
6519 : 25240 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_y, XEXP (op0, 0));
6520 : :
6521 : 25240 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
6522 : 25240 : CONST0_RTX (cmp_mode));
6523 : : }
6524 : :
6525 : : /* Both (eq/ne (and x y) y) and (eq/ne (ior x y) x) simplify to
6526 : : (eq/ne (and (not x) y) 0). */
6527 : 497368 : if ((op0code == AND && rtx_equal_p (XEXP (op0, 1), op1))
6528 : 975880 : || (op0code == IOR && rtx_equal_p (XEXP (op0, 0), op1)))
6529 : : {
6530 : 40986 : rtx not_x = simplify_gen_unary (NOT, cmp_mode, XEXP (op0, 0),
6531 : : cmp_mode);
6532 : 40986 : rtx lhs = simplify_gen_binary (AND, cmp_mode, not_x, XEXP (op0, 1));
6533 : :
6534 : 40986 : return simplify_gen_relational (code, mode, cmp_mode, lhs,
6535 : 40986 : CONST0_RTX (cmp_mode));
6536 : : }
6537 : : }
6538 : :
6539 : : /* (eq/ne (bswap x) C1) simplifies to (eq/ne x C2) with C2 swapped. */
6540 : 81777208 : if ((code == EQ || code == NE)
6541 : 61593185 : && GET_CODE (op0) == BSWAP
6542 : 324 : && CONST_SCALAR_INT_P (op1))
6543 : 93 : return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
6544 : : simplify_gen_unary (BSWAP, cmp_mode,
6545 : 93 : op1, cmp_mode));
6546 : :
6547 : : /* (eq/ne (bswap x) (bswap y)) simplifies to (eq/ne x y). */
6548 : 61593092 : if ((code == EQ || code == NE)
6549 : 61593092 : && GET_CODE (op0) == BSWAP
6550 : 231 : && GET_CODE (op1) == BSWAP)
6551 : 18 : return simplify_gen_relational (code, mode, cmp_mode,
6552 : 18 : XEXP (op0, 0), XEXP (op1, 0));
6553 : :
6554 : 81777097 : if (op0code == POPCOUNT && op1 == const0_rtx)
6555 : 0 : switch (code)
6556 : : {
6557 : 0 : case EQ:
6558 : 0 : case LE:
6559 : 0 : case LEU:
6560 : : /* (eq (popcount x) (const_int 0)) -> (eq x (const_int 0)). */
6561 : 0 : return simplify_gen_relational (EQ, mode, GET_MODE (XEXP (op0, 0)),
6562 : : XEXP (op0, 0),
6563 : 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
6564 : :
6565 : 0 : case NE:
6566 : 0 : case GT:
6567 : 0 : case GTU:
6568 : : /* (ne (popcount x) (const_int 0)) -> (ne x (const_int 0)). */
6569 : 0 : return simplify_gen_relational (NE, mode, GET_MODE (XEXP (op0, 0)),
6570 : : XEXP (op0, 0),
6571 : 0 : CONST0_RTX (GET_MODE (XEXP (op0, 0))));
6572 : :
6573 : : default:
6574 : : break;
6575 : : }
6576 : :
6577 : : /* (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) -> (and:SI x 1). */
6578 : 81777097 : if (code == NE
6579 : 32831758 : && op1 == const0_rtx
6580 : 16524603 : && (op0code == TRUNCATE
6581 : 151240 : || (partial_subreg_p (op0)
6582 : 150576 : && subreg_lowpart_p (op0)))
6583 : 126862 : && SCALAR_INT_MODE_P (mode)
6584 : 81777097 : && STORE_FLAG_VALUE == 1)
6585 : : {
6586 : 29598 : rtx tmp = XEXP (op0, 0);
6587 : 29598 : if (GET_CODE (tmp) == ASHIFT
6588 : 1862 : && GET_MODE (tmp) == mode
6589 : 188 : && CONST_INT_P (XEXP (tmp, 1))
6590 : 188 : && is_int_mode (GET_MODE (op0), &int_mode)
6591 : 29786 : && INTVAL (XEXP (tmp, 1)) == GET_MODE_PRECISION (int_mode) - 1)
6592 : 188 : return simplify_gen_binary (AND, mode, XEXP (tmp, 0), const1_rtx);
6593 : : }
6594 : :
6595 : : /* For two unsigned booleans A and B:
6596 : :
6597 : : A > B == ~B & A
6598 : : A >= B == ~B | A
6599 : : A < B == ~A & B
6600 : : A <= B == ~A | B
6601 : : A == B == ~A ^ B (== ~B ^ A)
6602 : : A != B == A ^ B
6603 : :
6604 : : For signed comparisons, we have to take STORE_FLAG_VALUE into account,
6605 : : with the rules above applying for positive STORE_FLAG_VALUE and with
6606 : : the relations reversed for negative STORE_FLAG_VALUE. */
6607 : 81776909 : if (is_a<scalar_int_mode> (cmp_mode)
6608 : 79386011 : && COMPARISON_P (op0)
6609 : 81842284 : && COMPARISON_P (op1))
6610 : : {
6611 : 9906 : rtx t = NULL_RTX;
6612 : 9906 : if (code == GTU || code == (STORE_FLAG_VALUE > 0 ? GT : LT))
6613 : 755 : t = simplify_logical_relational_operation (AND, mode, op1, op0, true);
6614 : : else if (code == GEU || code == (STORE_FLAG_VALUE > 0 ? GE : LE))
6615 : 720 : t = simplify_logical_relational_operation (IOR, mode, op1, op0, true);
6616 : : else if (code == LTU || code == (STORE_FLAG_VALUE > 0 ? LT : GT))
6617 : 720 : t = simplify_logical_relational_operation (AND, mode, op0, op1, true);
6618 : : else if (code == LEU || code == (STORE_FLAG_VALUE > 0 ? LE : GE))
6619 : 720 : t = simplify_logical_relational_operation (IOR, mode, op0, op1, true);
6620 : : else if (code == EQ)
6621 : 3194 : t = simplify_logical_relational_operation (XOR, mode, op0, op1, true);
6622 : : else if (code == NE)
6623 : 3797 : t = simplify_logical_relational_operation (XOR, mode, op0, op1);
6624 : 9906 : if (t)
6625 : : return t;
6626 : : }
6627 : :
6628 : : return NULL_RTX;
6629 : : }
6630 : :
6631 : : enum
6632 : : {
6633 : : CMP_EQ = 1,
6634 : : CMP_LT = 2,
6635 : : CMP_GT = 4,
6636 : : CMP_LTU = 8,
6637 : : CMP_GTU = 16
6638 : : };
6639 : :
6640 : :
6641 : : /* Convert the known results for EQ, LT, GT, LTU, GTU contained in
6642 : : KNOWN_RESULT to a CONST_INT, based on the requested comparison CODE
6643 : : For KNOWN_RESULT to make sense it should be either CMP_EQ, or the
6644 : : logical OR of one of (CMP_LT, CMP_GT) and one of (CMP_LTU, CMP_GTU).
6645 : : For floating-point comparisons, assume that the operands were ordered. */
6646 : :
6647 : : static rtx
6648 : 682184 : comparison_result (enum rtx_code code, int known_results)
6649 : : {
6650 : 682184 : switch (code)
6651 : : {
6652 : 129076 : case EQ:
6653 : 129076 : case UNEQ:
6654 : 129076 : return (known_results & CMP_EQ) ? const_true_rtx : const0_rtx;
6655 : 444429 : case NE:
6656 : 444429 : case LTGT:
6657 : 444429 : return (known_results & CMP_EQ) ? const0_rtx : const_true_rtx;
6658 : :
6659 : 9520 : case LT:
6660 : 9520 : case UNLT:
6661 : 9520 : return (known_results & CMP_LT) ? const_true_rtx : const0_rtx;
6662 : 8552 : case GE:
6663 : 8552 : case UNGE:
6664 : 8552 : return (known_results & CMP_LT) ? const0_rtx : const_true_rtx;
6665 : :
6666 : 13087 : case GT:
6667 : 13087 : case UNGT:
6668 : 13087 : return (known_results & CMP_GT) ? const_true_rtx : const0_rtx;
6669 : 14708 : case LE:
6670 : 14708 : case UNLE:
6671 : 14708 : return (known_results & CMP_GT) ? const0_rtx : const_true_rtx;
6672 : :
6673 : 10267 : case LTU:
6674 : 10267 : return (known_results & CMP_LTU) ? const_true_rtx : const0_rtx;
6675 : 8925 : case GEU:
6676 : 8925 : return (known_results & CMP_LTU) ? const0_rtx : const_true_rtx;
6677 : :
6678 : 32954 : case GTU:
6679 : 32954 : return (known_results & CMP_GTU) ? const_true_rtx : const0_rtx;
6680 : 10658 : case LEU:
6681 : 10658 : return (known_results & CMP_GTU) ? const0_rtx : const_true_rtx;
6682 : :
6683 : 0 : case ORDERED:
6684 : 0 : return const_true_rtx;
6685 : 8 : case UNORDERED:
6686 : 8 : return const0_rtx;
6687 : 0 : default:
6688 : 0 : gcc_unreachable ();
6689 : : }
6690 : : }
6691 : :
6692 : : /* Check if the given comparison (done in the given MODE) is actually
6693 : : a tautology or a contradiction. If the mode is VOIDmode, the
6694 : : comparison is done in "infinite precision". If no simplification
6695 : : is possible, this function returns zero. Otherwise, it returns
6696 : : either const_true_rtx or const0_rtx. */
6697 : :
6698 : : rtx
6699 : 128335306 : simplify_const_relational_operation (enum rtx_code code,
6700 : : machine_mode mode,
6701 : : rtx op0, rtx op1)
6702 : : {
6703 : 134994127 : rtx tem;
6704 : 134994127 : rtx trueop0;
6705 : 134994127 : rtx trueop1;
6706 : :
6707 : 134994127 : gcc_assert (mode != VOIDmode
6708 : : || (GET_MODE (op0) == VOIDmode
6709 : : && GET_MODE (op1) == VOIDmode));
6710 : :
6711 : : /* We only handle MODE_CC comparisons that are COMPARE against zero. */
6712 : 134994127 : if (GET_MODE_CLASS (mode) == MODE_CC
6713 : 43926089 : && (op1 != const0_rtx
6714 : 43926089 : || GET_CODE (op0) != COMPARE))
6715 : : return NULL_RTX;
6716 : :
6717 : : /* If op0 is a compare, extract the comparison arguments from it. */
6718 : 104722255 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6719 : : {
6720 : 13654217 : op1 = XEXP (op0, 1);
6721 : 13654217 : op0 = XEXP (op0, 0);
6722 : :
6723 : 13654217 : if (GET_MODE (op0) != VOIDmode)
6724 : 13541056 : mode = GET_MODE (op0);
6725 : 113161 : else if (GET_MODE (op1) != VOIDmode)
6726 : 103535 : mode = GET_MODE (op1);
6727 : : else
6728 : : return 0;
6729 : : }
6730 : :
6731 : : /* We can't simplify MODE_CC values since we don't know what the
6732 : : actual comparison is. */
6733 : 104712629 : if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6734 : : return 0;
6735 : :
6736 : : /* Make sure the constant is second. */
6737 : 104712629 : if (swap_commutative_operands_p (op0, op1))
6738 : : {
6739 : 3071968 : std::swap (op0, op1);
6740 : 3071968 : code = swap_condition (code);
6741 : : }
6742 : :
6743 : 104712629 : trueop0 = avoid_constant_pool_reference (op0);
6744 : 104712629 : trueop1 = avoid_constant_pool_reference (op1);
6745 : :
6746 : : /* For integer comparisons of A and B maybe we can simplify A - B and can
6747 : : then simplify a comparison of that with zero. If A and B are both either
6748 : : a register or a CONST_INT, this can't help; testing for these cases will
6749 : : prevent infinite recursion here and speed things up.
6750 : :
6751 : : We can only do this for EQ and NE comparisons as otherwise we may
6752 : : lose or introduce overflow which we cannot disregard as undefined as
6753 : : we do not know the signedness of the operation on either the left or
6754 : : the right hand side of the comparison. */
6755 : :
6756 : 104712629 : if (INTEGRAL_MODE_P (mode)
6757 : 102410252 : && trueop1 != CONST0_RTX (mode)
6758 : 52128361 : && (code == EQ || code == NE)
6759 : 33533146 : && ! ((REG_P (op0)
6760 : 9405009 : || CONST_SCALAR_INT_P (trueop0)
6761 : 9378790 : || CONST_VECTOR_P (trueop0))
6762 : 24154376 : && (REG_P (op1)
6763 : 14688372 : || CONST_SCALAR_INT_P (trueop1)
6764 : 3264925 : || CONST_VECTOR_P (trueop1)))
6765 : 12641895 : && (tem = simplify_binary_operation (MINUS, mode, op0, op1)) != 0
6766 : : /* We cannot do this if tem is a nonzero address. */
6767 : 6658823 : && ! nonzero_address_p (tem))
6768 : 6658821 : return simplify_const_relational_operation (signed_condition (code),
6769 : 6658821 : mode, tem, CONST0_RTX (mode));
6770 : :
6771 : 98053808 : if (! HONOR_NANS (mode) && code == ORDERED)
6772 : 0 : return const_true_rtx;
6773 : :
6774 : 98053808 : if (! HONOR_NANS (mode) && code == UNORDERED)
6775 : 8 : return const0_rtx;
6776 : :
6777 : : /* For modes without NaNs, if the two operands are equal, we know the
6778 : : result except if they have side-effects. Even with NaNs we know
6779 : : the result of unordered comparisons and, if signaling NaNs are
6780 : : irrelevant, also the result of LT/GT/LTGT. */
6781 : 98053800 : if ((! HONOR_NANS (trueop0)
6782 : 2087563 : || code == UNEQ || code == UNLE || code == UNGE
6783 : : || ((code == LT || code == GT || code == LTGT)
6784 : 824604 : && ! HONOR_SNANS (trueop0)))
6785 : 96902454 : && rtx_equal_p (trueop0, trueop1)
6786 : 98537105 : && ! side_effects_p (trueop0))
6787 : 483297 : return comparison_result (code, CMP_EQ);
6788 : :
6789 : : /* If the operands are floating-point constants, see if we can fold
6790 : : the result. */
6791 : 97570503 : if (CONST_DOUBLE_AS_FLOAT_P (trueop0)
6792 : 1298 : && CONST_DOUBLE_AS_FLOAT_P (trueop1)
6793 : 1298 : && SCALAR_FLOAT_MODE_P (GET_MODE (trueop0)))
6794 : : {
6795 : 1298 : const REAL_VALUE_TYPE *d0 = CONST_DOUBLE_REAL_VALUE (trueop0);
6796 : 1298 : const REAL_VALUE_TYPE *d1 = CONST_DOUBLE_REAL_VALUE (trueop1);
6797 : :
6798 : : /* Comparisons are unordered iff at least one of the values is NaN. */
6799 : 1298 : if (REAL_VALUE_ISNAN (*d0) || REAL_VALUE_ISNAN (*d1))
6800 : 180 : switch (code)
6801 : : {
6802 : 0 : case UNEQ:
6803 : 0 : case UNLT:
6804 : 0 : case UNGT:
6805 : 0 : case UNLE:
6806 : 0 : case UNGE:
6807 : 0 : case NE:
6808 : 0 : case UNORDERED:
6809 : 0 : return const_true_rtx;
6810 : 180 : case EQ:
6811 : 180 : case LT:
6812 : 180 : case GT:
6813 : 180 : case LE:
6814 : 180 : case GE:
6815 : 180 : case LTGT:
6816 : 180 : case ORDERED:
6817 : 180 : return const0_rtx;
6818 : : default:
6819 : : return 0;
6820 : : }
6821 : :
6822 : 1207 : return comparison_result (code,
6823 : 1207 : (real_equal (d0, d1) ? CMP_EQ :
6824 : 1207 : real_less (d0, d1) ? CMP_LT : CMP_GT));
6825 : : }
6826 : :
6827 : : /* Otherwise, see if the operands are both integers. */
6828 : 97569205 : if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
6829 : 94881647 : && CONST_SCALAR_INT_P (trueop0) && CONST_SCALAR_INT_P (trueop1))
6830 : : {
6831 : : /* It would be nice if we really had a mode here. However, the
6832 : : largest int representable on the target is as good as
6833 : : infinite. */
6834 : 197769 : machine_mode cmode = (mode == VOIDmode) ? MAX_MODE_INT : mode;
6835 : 197769 : rtx_mode_t ptrueop0 = rtx_mode_t (trueop0, cmode);
6836 : 197769 : rtx_mode_t ptrueop1 = rtx_mode_t (trueop1, cmode);
6837 : :
6838 : 197769 : if (wi::eq_p (ptrueop0, ptrueop1))
6839 : 0 : return comparison_result (code, CMP_EQ);
6840 : : else
6841 : : {
6842 : 197769 : int cr = wi::lts_p (ptrueop0, ptrueop1) ? CMP_LT : CMP_GT;
6843 : 197769 : cr |= wi::ltu_p (ptrueop0, ptrueop1) ? CMP_LTU : CMP_GTU;
6844 : 197769 : return comparison_result (code, cr);
6845 : : }
6846 : : }
6847 : :
6848 : : /* Optimize comparisons with upper and lower bounds. */
6849 : 97371436 : scalar_int_mode int_mode;
6850 : 97371436 : if (CONST_INT_P (trueop1)
6851 : 68831629 : && is_a <scalar_int_mode> (mode, &int_mode)
6852 : 68831629 : && HWI_COMPUTABLE_MODE_P (int_mode)
6853 : 165667360 : && !side_effects_p (trueop0))
6854 : : {
6855 : 68208836 : int sign;
6856 : 68208836 : unsigned HOST_WIDE_INT nonzero = nonzero_bits (trueop0, int_mode);
6857 : 68208836 : HOST_WIDE_INT val = INTVAL (trueop1);
6858 : 68208836 : HOST_WIDE_INT mmin, mmax;
6859 : :
6860 : 68208836 : if (code == GEU
6861 : 68208836 : || code == LEU
6862 : 65212088 : || code == GTU
6863 : 65212088 : || code == LTU)
6864 : : sign = 0;
6865 : : else
6866 : 68208836 : sign = 1;
6867 : :
6868 : : /* Get a reduced range if the sign bit is zero. */
6869 : 68208836 : if (nonzero <= (GET_MODE_MASK (int_mode) >> 1))
6870 : : {
6871 : 6829822 : mmin = 0;
6872 : 6829822 : mmax = nonzero;
6873 : : }
6874 : : else
6875 : : {
6876 : 61379014 : rtx mmin_rtx, mmax_rtx;
6877 : 61379014 : get_mode_bounds (int_mode, sign, int_mode, &mmin_rtx, &mmax_rtx);
6878 : :
6879 : 61379014 : mmin = INTVAL (mmin_rtx);
6880 : 61379014 : mmax = INTVAL (mmax_rtx);
6881 : 61379014 : if (sign)
6882 : : {
6883 : 55810254 : unsigned int sign_copies
6884 : 55810254 : = num_sign_bit_copies (trueop0, int_mode);
6885 : :
6886 : 55810254 : mmin >>= (sign_copies - 1);
6887 : 55810254 : mmax >>= (sign_copies - 1);
6888 : : }
6889 : : }
6890 : :
6891 : 68208836 : switch (code)
6892 : : {
6893 : : /* x >= y is always true for y <= mmin, always false for y > mmax. */
6894 : 472253 : case GEU:
6895 : 472253 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
6896 : 11126 : return const_true_rtx;
6897 : 461127 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
6898 : 48 : return const0_rtx;
6899 : : break;
6900 : 937365 : case GE:
6901 : 937365 : if (val <= mmin)
6902 : 2255 : return const_true_rtx;
6903 : 935110 : if (val > mmax)
6904 : 0 : return const0_rtx;
6905 : : break;
6906 : :
6907 : : /* x <= y is always true for y >= mmax, always false for y < mmin. */
6908 : 2524495 : case LEU:
6909 : 2524495 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
6910 : 16942 : return const_true_rtx;
6911 : 2507553 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
6912 : 0 : return const0_rtx;
6913 : : break;
6914 : 2027543 : case LE:
6915 : 2027543 : if (val >= mmax)
6916 : 433 : return const_true_rtx;
6917 : 2027110 : if (val < mmin)
6918 : 0 : return const0_rtx;
6919 : : break;
6920 : :
6921 : 25573628 : case EQ:
6922 : : /* x == y is always false for y out of range. */
6923 : 25573628 : if (val < mmin || val > mmax)
6924 : 408 : return const0_rtx;
6925 : : break;
6926 : :
6927 : : /* x > y is always false for y >= mmax, always true for y < mmin. */
6928 : 2429310 : case GTU:
6929 : 2429310 : if ((unsigned HOST_WIDE_INT) val >= (unsigned HOST_WIDE_INT) mmax)
6930 : 127796 : return const0_rtx;
6931 : 2301514 : if ((unsigned HOST_WIDE_INT) val < (unsigned HOST_WIDE_INT) mmin)
6932 : 0 : return const_true_rtx;
6933 : : break;
6934 : 2003825 : case GT:
6935 : 2003825 : if (val >= mmax)
6936 : 341 : return const0_rtx;
6937 : 2003484 : if (val < mmin)
6938 : 5 : return const_true_rtx;
6939 : : break;
6940 : :
6941 : : /* x < y is always false for y <= mmin, always true for y > mmax. */
6942 : 720605 : case LTU:
6943 : 720605 : if ((unsigned HOST_WIDE_INT) val <= (unsigned HOST_WIDE_INT) mmin)
6944 : 2745 : return const0_rtx;
6945 : 717860 : if ((unsigned HOST_WIDE_INT) val > (unsigned HOST_WIDE_INT) mmax)
6946 : 83880 : return const_true_rtx;
6947 : : break;
6948 : 1099907 : case LT:
6949 : 1099907 : if (val <= mmin)
6950 : 2278 : return const0_rtx;
6951 : 1097629 : if (val > mmax)
6952 : 3577 : return const_true_rtx;
6953 : : break;
6954 : :
6955 : 30419905 : case NE:
6956 : : /* x != y is always true for y out of range. */
6957 : 30419905 : if (val < mmin || val > mmax)
6958 : 145 : return const_true_rtx;
6959 : : break;
6960 : :
6961 : : default:
6962 : : break;
6963 : : }
6964 : : }
6965 : :
6966 : : /* Optimize integer comparisons with zero. */
6967 : 97119457 : if (is_a <scalar_int_mode> (mode, &int_mode)
6968 : 94474982 : && trueop1 == const0_rtx
6969 : 49564815 : && !side_effects_p (trueop0))
6970 : : {
6971 : : /* Some addresses are known to be nonzero. We don't know
6972 : : their sign, but equality comparisons are known. */
6973 : 49476247 : if (nonzero_address_p (trueop0))
6974 : : {
6975 : 553 : if (code == EQ || code == LEU)
6976 : 298 : return const0_rtx;
6977 : 255 : if (code == NE || code == GTU)
6978 : 255 : return const_true_rtx;
6979 : : }
6980 : :
6981 : : /* See if the first operand is an IOR with a constant. If so, we
6982 : : may be able to determine the result of this comparison. */
6983 : 49475694 : if (GET_CODE (op0) == IOR)
6984 : : {
6985 : 598015 : rtx inner_const = avoid_constant_pool_reference (XEXP (op0, 1));
6986 : 598015 : if (CONST_INT_P (inner_const) && inner_const != const0_rtx)
6987 : : {
6988 : 288 : int sign_bitnum = GET_MODE_PRECISION (int_mode) - 1;
6989 : 576 : int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum
6990 : 288 : && (UINTVAL (inner_const)
6991 : 288 : & (HOST_WIDE_INT_1U
6992 : : << sign_bitnum)));
6993 : :
6994 : 288 : switch (code)
6995 : : {
6996 : : case EQ:
6997 : : case LEU:
6998 : : return const0_rtx;
6999 : 4 : case NE:
7000 : 4 : case GTU:
7001 : 4 : return const_true_rtx;
7002 : 70 : case LT:
7003 : 70 : case LE:
7004 : 70 : if (has_sign)
7005 : 2 : return const_true_rtx;
7006 : : break;
7007 : 210 : case GT:
7008 : 210 : case GE:
7009 : 210 : if (has_sign)
7010 : : return const0_rtx;
7011 : : break;
7012 : : default:
7013 : : break;
7014 : : }
7015 : : }
7016 : : }
7017 : : }
7018 : :
7019 : : /* Optimize comparison of ABS with zero. */
7020 : 49878940 : if (trueop1 == CONST0_RTX (mode) && !side_effects_p (trueop0)
7021 : 146908845 : && (GET_CODE (trueop0) == ABS
7022 : 49789608 : || (GET_CODE (trueop0) == FLOAT_EXTEND
7023 : 34 : && GET_CODE (XEXP (trueop0, 0)) == ABS)))
7024 : : {
7025 : 535 : switch (code)
7026 : : {
7027 : 60 : case LT:
7028 : : /* Optimize abs(x) < 0.0. */
7029 : 60 : if (!INTEGRAL_MODE_P (mode) && !HONOR_SNANS (mode))
7030 : 0 : return const0_rtx;
7031 : : break;
7032 : :
7033 : 42 : case GE:
7034 : : /* Optimize abs(x) >= 0.0. */
7035 : 42 : if (!INTEGRAL_MODE_P (mode) && !HONOR_NANS (mode))
7036 : 0 : return const_true_rtx;
7037 : : break;
7038 : :
7039 : 0 : case UNGE:
7040 : : /* Optimize ! (abs(x) < 0.0). */
7041 : 0 : return const_true_rtx;
7042 : :
7043 : : default:
7044 : : break;
7045 : : }
7046 : : }
7047 : :
7048 : : return 0;
7049 : : }
7050 : :
7051 : : /* Recognize expressions of the form (X CMP 0) ? VAL : OP (X)
7052 : : where OP is CLZ or CTZ and VAL is the value from CLZ_DEFINED_VALUE_AT_ZERO
7053 : : or CTZ_DEFINED_VALUE_AT_ZERO respectively and return OP (X) if the expression
7054 : : can be simplified to that or NULL_RTX if not.
7055 : : Assume X is compared against zero with CMP_CODE and the true
7056 : : arm is TRUE_VAL and the false arm is FALSE_VAL. */
7057 : :
7058 : : rtx
7059 : 30742247 : simplify_context::simplify_cond_clz_ctz (rtx x, rtx_code cmp_code,
7060 : : rtx true_val, rtx false_val)
7061 : : {
7062 : 30742247 : if (cmp_code != EQ && cmp_code != NE)
7063 : : return NULL_RTX;
7064 : :
7065 : : /* Result on X == 0 and X !=0 respectively. */
7066 : 22466913 : rtx on_zero, on_nonzero;
7067 : 22466913 : if (cmp_code == EQ)
7068 : : {
7069 : : on_zero = true_val;
7070 : : on_nonzero = false_val;
7071 : : }
7072 : : else
7073 : : {
7074 : 11975953 : on_zero = false_val;
7075 : 11975953 : on_nonzero = true_val;
7076 : : }
7077 : :
7078 : 22466913 : rtx_code op_code = GET_CODE (on_nonzero);
7079 : 22466913 : if ((op_code != CLZ && op_code != CTZ)
7080 : 1877 : || !rtx_equal_p (XEXP (on_nonzero, 0), x)
7081 : 22467853 : || !CONST_INT_P (on_zero))
7082 : 22466618 : return NULL_RTX;
7083 : :
7084 : 295 : HOST_WIDE_INT op_val;
7085 : 295 : scalar_int_mode mode ATTRIBUTE_UNUSED
7086 : 295 : = as_a <scalar_int_mode> (GET_MODE (XEXP (on_nonzero, 0)));
7087 : 0 : if (((op_code == CLZ && CLZ_DEFINED_VALUE_AT_ZERO (mode, op_val))
7088 : 590 : || (op_code == CTZ && CTZ_DEFINED_VALUE_AT_ZERO (mode, op_val)))
7089 : 319 : && op_val == INTVAL (on_zero))
7090 : : return on_nonzero;
7091 : :
7092 : : return NULL_RTX;
7093 : : }
7094 : :
7095 : : /* Try to simplify X given that it appears within operand OP of a
7096 : : VEC_MERGE operation whose mask is MASK. X need not use the same
7097 : : vector mode as the VEC_MERGE, but it must have the same number of
7098 : : elements.
7099 : :
7100 : : Return the simplified X on success, otherwise return NULL_RTX. */
7101 : :
7102 : : rtx
7103 : 1517619 : simplify_context::simplify_merge_mask (rtx x, rtx mask, int op)
7104 : : {
7105 : 1517619 : gcc_assert (VECTOR_MODE_P (GET_MODE (x)));
7106 : 3035238 : poly_uint64 nunits = GET_MODE_NUNITS (GET_MODE (x));
7107 : 1517619 : if (GET_CODE (x) == VEC_MERGE && rtx_equal_p (XEXP (x, 2), mask))
7108 : : {
7109 : 5164 : if (side_effects_p (XEXP (x, 1 - op)))
7110 : : return NULL_RTX;
7111 : :
7112 : 4940 : return XEXP (x, op);
7113 : : }
7114 : 1512455 : if (UNARY_P (x)
7115 : 179178 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7116 : 1569789 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits))
7117 : : {
7118 : 24098 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7119 : 24098 : if (top0)
7120 : 448 : return simplify_gen_unary (GET_CODE (x), GET_MODE (x), top0,
7121 : 448 : GET_MODE (XEXP (x, 0)));
7122 : : }
7123 : 1512007 : if (BINARY_P (x)
7124 : 188258 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7125 : 376418 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7126 : 163245 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7127 : 1774041 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits))
7128 : : {
7129 : 131017 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7130 : 131017 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7131 : 131017 : if (top0 || top1)
7132 : : {
7133 : 792 : if (COMPARISON_P (x))
7134 : 0 : return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7135 : 0 : GET_MODE (XEXP (x, 0)) != VOIDmode
7136 : : ? GET_MODE (XEXP (x, 0))
7137 : 0 : : GET_MODE (XEXP (x, 1)),
7138 : : top0 ? top0 : XEXP (x, 0),
7139 : 0 : top1 ? top1 : XEXP (x, 1));
7140 : : else
7141 : 792 : return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7142 : : top0 ? top0 : XEXP (x, 0),
7143 : 792 : top1 ? top1 : XEXP (x, 1));
7144 : : }
7145 : : }
7146 : 1511215 : if (GET_RTX_CLASS (GET_CODE (x)) == RTX_TERNARY
7147 : 28737 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
7148 : 57474 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))), nunits)
7149 : 28737 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 1)))
7150 : 57474 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 1))), nunits)
7151 : 28737 : && VECTOR_MODE_P (GET_MODE (XEXP (x, 2)))
7152 : 1529821 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (x, 2))), nunits))
7153 : : {
7154 : 9303 : rtx top0 = simplify_merge_mask (XEXP (x, 0), mask, op);
7155 : 9303 : rtx top1 = simplify_merge_mask (XEXP (x, 1), mask, op);
7156 : 9303 : rtx top2 = simplify_merge_mask (XEXP (x, 2), mask, op);
7157 : 9303 : if (top0 || top1 || top2)
7158 : 448 : return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7159 : 448 : GET_MODE (XEXP (x, 0)),
7160 : : top0 ? top0 : XEXP (x, 0),
7161 : : top1 ? top1 : XEXP (x, 1),
7162 : 448 : top2 ? top2 : XEXP (x, 2));
7163 : : }
7164 : : return NULL_RTX;
7165 : : }
7166 : :
7167 : :
7168 : : /* Simplify CODE, an operation with result mode MODE and three operands,
7169 : : OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
7170 : : a constant. Return 0 if no simplifications is possible. */
7171 : :
7172 : : rtx
7173 : 41909032 : simplify_context::simplify_ternary_operation (rtx_code code, machine_mode mode,
7174 : : machine_mode op0_mode,
7175 : : rtx op0, rtx op1, rtx op2)
7176 : : {
7177 : 41909032 : bool any_change = false;
7178 : 41909032 : rtx tem, trueop2;
7179 : 41909032 : scalar_int_mode int_mode, int_op0_mode;
7180 : 41909032 : unsigned int n_elts;
7181 : :
7182 : 41909032 : switch (code)
7183 : : {
7184 : 326714 : case FMA:
7185 : : /* Simplify negations around the multiplication. */
7186 : : /* -a * -b + c => a * b + c. */
7187 : 326714 : if (GET_CODE (op0) == NEG)
7188 : : {
7189 : 78750 : tem = simplify_unary_operation (NEG, mode, op1, mode);
7190 : 78750 : if (tem)
7191 : 234 : op1 = tem, op0 = XEXP (op0, 0), any_change = true;
7192 : : }
7193 : 247964 : else if (GET_CODE (op1) == NEG)
7194 : : {
7195 : 1130 : tem = simplify_unary_operation (NEG, mode, op0, mode);
7196 : 1130 : if (tem)
7197 : 0 : op0 = tem, op1 = XEXP (op1, 0), any_change = true;
7198 : : }
7199 : :
7200 : : /* Canonicalize the two multiplication operands. */
7201 : : /* a * -b + c => -b * a + c. */
7202 : 326714 : if (swap_commutative_operands_p (op0, op1))
7203 : : std::swap (op0, op1), any_change = true;
7204 : :
7205 : 297888 : if (any_change)
7206 : 29051 : return gen_rtx_FMA (mode, op0, op1, op2);
7207 : : return NULL_RTX;
7208 : :
7209 : 646486 : case SIGN_EXTRACT:
7210 : 646486 : case ZERO_EXTRACT:
7211 : 646486 : if (CONST_INT_P (op0)
7212 : 20320 : && CONST_INT_P (op1)
7213 : 20320 : && CONST_INT_P (op2)
7214 : 41909065 : && is_a <scalar_int_mode> (mode, &int_mode)
7215 : 33 : && INTVAL (op1) + INTVAL (op2) <= GET_MODE_PRECISION (int_mode)
7216 : 646519 : && HWI_COMPUTABLE_MODE_P (int_mode))
7217 : : {
7218 : : /* Extracting a bit-field from a constant */
7219 : 33 : unsigned HOST_WIDE_INT val = UINTVAL (op0);
7220 : 33 : HOST_WIDE_INT op1val = INTVAL (op1);
7221 : 33 : HOST_WIDE_INT op2val = INTVAL (op2);
7222 : 33 : if (!BITS_BIG_ENDIAN)
7223 : 33 : val >>= op2val;
7224 : : else if (is_a <scalar_int_mode> (op0_mode, &int_op0_mode))
7225 : : val >>= GET_MODE_PRECISION (int_op0_mode) - op2val - op1val;
7226 : : else
7227 : : /* Not enough information to calculate the bit position. */
7228 : : break;
7229 : :
7230 : 33 : if (HOST_BITS_PER_WIDE_INT != op1val)
7231 : : {
7232 : : /* First zero-extend. */
7233 : 30 : val &= (HOST_WIDE_INT_1U << op1val) - 1;
7234 : : /* If desired, propagate sign bit. */
7235 : 30 : if (code == SIGN_EXTRACT
7236 : 5 : && (val & (HOST_WIDE_INT_1U << (op1val - 1)))
7237 : 5 : != 0)
7238 : 2 : val |= ~ ((HOST_WIDE_INT_1U << op1val) - 1);
7239 : : }
7240 : :
7241 : 33 : return gen_int_mode (val, int_mode);
7242 : : }
7243 : : break;
7244 : :
7245 : 40207634 : case IF_THEN_ELSE:
7246 : 40207634 : if (CONST_INT_P (op0))
7247 : 285483 : return op0 != const0_rtx ? op1 : op2;
7248 : :
7249 : : /* Convert c ? a : a into "a". */
7250 : 40015379 : if (rtx_equal_p (op1, op2) && ! side_effects_p (op0))
7251 : : return op1;
7252 : :
7253 : : /* Convert a != b ? a : b into "a". */
7254 : 40012064 : if (GET_CODE (op0) == NE
7255 : 15743716 : && ! side_effects_p (op0)
7256 : 15705953 : && ! HONOR_NANS (mode)
7257 : 15701631 : && ! HONOR_SIGNED_ZEROS (mode)
7258 : 55713695 : && ((rtx_equal_p (XEXP (op0, 0), op1)
7259 : 74741 : && rtx_equal_p (XEXP (op0, 1), op2))
7260 : 15701603 : || (rtx_equal_p (XEXP (op0, 0), op2)
7261 : 5937 : && rtx_equal_p (XEXP (op0, 1), op1))))
7262 : 123 : return op1;
7263 : :
7264 : : /* Convert a == b ? a : b into "b". */
7265 : 40011941 : if (GET_CODE (op0) == EQ
7266 : 12824670 : && ! side_effects_p (op0)
7267 : 12814367 : && ! HONOR_NANS (mode)
7268 : 12715062 : && ! HONOR_SIGNED_ZEROS (mode)
7269 : 52727003 : && ((rtx_equal_p (XEXP (op0, 0), op1)
7270 : 14582 : && rtx_equal_p (XEXP (op0, 1), op2))
7271 : 12715054 : || (rtx_equal_p (XEXP (op0, 0), op2)
7272 : 7602 : && rtx_equal_p (XEXP (op0, 1), op1))))
7273 : 21 : return op2;
7274 : :
7275 : : /* Convert a != 0 ? -a : 0 into "-a". */
7276 : 40011920 : if (GET_CODE (op0) == NE
7277 : 15743593 : && ! side_effects_p (op0)
7278 : 15705830 : && ! HONOR_NANS (mode)
7279 : 15701508 : && ! HONOR_SIGNED_ZEROS (mode)
7280 : 15701508 : && XEXP (op0, 1) == CONST0_RTX (mode)
7281 : 11971631 : && op2 == CONST0_RTX (mode)
7282 : 96389 : && GET_CODE (op1) == NEG
7283 : 40011971 : && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0)))
7284 : : return op1;
7285 : :
7286 : : /* Convert a == 0 ? 0 : -a into "-a". */
7287 : 40011915 : if (GET_CODE (op0) == EQ
7288 : 12824649 : && ! side_effects_p (op0)
7289 : 12814346 : && ! HONOR_NANS (mode)
7290 : 12715041 : && ! HONOR_SIGNED_ZEROS (mode)
7291 : 12715041 : && op1 == CONST0_RTX (mode)
7292 : 32770 : && XEXP (op0, 1) == CONST0_RTX (mode)
7293 : 14619 : && GET_CODE (op2) == NEG
7294 : 40011919 : && rtx_equal_p (XEXP (op0, 0), XEXP (op2, 0)))
7295 : : return op2;
7296 : :
7297 : : /* Convert (!c) != {0,...,0} ? a : b into
7298 : : c != {0,...,0} ? b : a for vector modes. */
7299 : 40011911 : if (VECTOR_MODE_P (GET_MODE (op1))
7300 : 11649 : && GET_CODE (op0) == NE
7301 : 505 : && GET_CODE (XEXP (op0, 0)) == NOT
7302 : 0 : && GET_CODE (XEXP (op0, 1)) == CONST_VECTOR)
7303 : : {
7304 : 0 : rtx cv = XEXP (op0, 1);
7305 : 0 : int nunits;
7306 : 0 : bool ok = true;
7307 : 0 : if (!CONST_VECTOR_NUNITS (cv).is_constant (&nunits))
7308 : : ok = false;
7309 : : else
7310 : 0 : for (int i = 0; i < nunits; ++i)
7311 : 0 : if (CONST_VECTOR_ELT (cv, i) != const0_rtx)
7312 : : {
7313 : : ok = false;
7314 : : break;
7315 : : }
7316 : 0 : if (ok)
7317 : : {
7318 : 0 : rtx new_op0 = gen_rtx_NE (GET_MODE (op0),
7319 : : XEXP (XEXP (op0, 0), 0),
7320 : : XEXP (op0, 1));
7321 : 0 : rtx retval = gen_rtx_IF_THEN_ELSE (mode, new_op0, op2, op1);
7322 : 0 : return retval;
7323 : : }
7324 : : }
7325 : :
7326 : : /* Convert x == 0 ? N : clz (x) into clz (x) when
7327 : : CLZ_DEFINED_VALUE_AT_ZERO is defined to N for the mode of x.
7328 : : Similarly for ctz (x). */
7329 : 40010938 : if (COMPARISON_P (op0) && !side_effects_p (op0)
7330 : 79937972 : && XEXP (op0, 1) == const0_rtx)
7331 : : {
7332 : 30742247 : rtx simplified
7333 : 30742247 : = simplify_cond_clz_ctz (XEXP (op0, 0), GET_CODE (op0),
7334 : : op1, op2);
7335 : 30742247 : if (simplified)
7336 : : return simplified;
7337 : : }
7338 : :
7339 : 40011911 : if (COMPARISON_P (op0) && ! side_effects_p (op0))
7340 : : {
7341 : 79922759 : machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
7342 : 39926061 : ? GET_MODE (XEXP (op0, 1))
7343 : : : GET_MODE (XEXP (op0, 0)));
7344 : 39926061 : rtx temp;
7345 : :
7346 : : /* Look for happy constants in op1 and op2. */
7347 : 39926061 : if (CONST_INT_P (op1) && CONST_INT_P (op2))
7348 : : {
7349 : 214002 : HOST_WIDE_INT t = INTVAL (op1);
7350 : 214002 : HOST_WIDE_INT f = INTVAL (op2);
7351 : :
7352 : 214002 : if (t == STORE_FLAG_VALUE && f == 0)
7353 : 53132 : code = GET_CODE (op0);
7354 : 160870 : else if (t == 0 && f == STORE_FLAG_VALUE)
7355 : : {
7356 : 31730 : enum rtx_code tmp;
7357 : 31730 : tmp = reversed_comparison_code (op0, NULL);
7358 : 31730 : if (tmp == UNKNOWN)
7359 : : break;
7360 : : code = tmp;
7361 : : }
7362 : : else
7363 : : break;
7364 : :
7365 : 79474 : return simplify_gen_relational (code, mode, cmp_mode,
7366 : 79474 : XEXP (op0, 0), XEXP (op0, 1));
7367 : : }
7368 : :
7369 : 39712059 : temp = simplify_relational_operation (GET_CODE (op0), op0_mode,
7370 : : cmp_mode, XEXP (op0, 0),
7371 : : XEXP (op0, 1));
7372 : :
7373 : : /* See if any simplifications were possible. */
7374 : 39712059 : if (temp)
7375 : : {
7376 : 6962 : if (CONST_INT_P (temp))
7377 : 864 : return temp == const0_rtx ? op2 : op1;
7378 : 6139 : else if (temp)
7379 : 6139 : return gen_rtx_IF_THEN_ELSE (mode, temp, op1, op2);
7380 : : }
7381 : : }
7382 : : break;
7383 : :
7384 : 728198 : case VEC_MERGE:
7385 : 728198 : gcc_assert (GET_MODE (op0) == mode);
7386 : 728198 : gcc_assert (GET_MODE (op1) == mode);
7387 : 728198 : gcc_assert (VECTOR_MODE_P (mode));
7388 : 728198 : trueop2 = avoid_constant_pool_reference (op2);
7389 : 728198 : if (CONST_INT_P (trueop2)
7390 : 1160101 : && GET_MODE_NUNITS (mode).is_constant (&n_elts))
7391 : : {
7392 : 431903 : unsigned HOST_WIDE_INT sel = UINTVAL (trueop2);
7393 : 431903 : unsigned HOST_WIDE_INT mask;
7394 : 431903 : if (n_elts == HOST_BITS_PER_WIDE_INT)
7395 : : mask = -1;
7396 : : else
7397 : 429588 : mask = (HOST_WIDE_INT_1U << n_elts) - 1;
7398 : :
7399 : 431903 : if (!(sel & mask) && !side_effects_p (op0))
7400 : : return op1;
7401 : 431466 : if ((sel & mask) == mask && !side_effects_p (op1))
7402 : : return op0;
7403 : :
7404 : 421316 : rtx trueop0 = avoid_constant_pool_reference (op0);
7405 : 421316 : rtx trueop1 = avoid_constant_pool_reference (op1);
7406 : 421316 : if (GET_CODE (trueop0) == CONST_VECTOR
7407 : 8748 : && GET_CODE (trueop1) == CONST_VECTOR)
7408 : : {
7409 : 4591 : rtvec v = rtvec_alloc (n_elts);
7410 : 4591 : unsigned int i;
7411 : :
7412 : 52428 : for (i = 0; i < n_elts; i++)
7413 : 43246 : RTVEC_ELT (v, i) = ((sel & (HOST_WIDE_INT_1U << i))
7414 : 43246 : ? CONST_VECTOR_ELT (trueop0, i)
7415 : 24115 : : CONST_VECTOR_ELT (trueop1, i));
7416 : 4591 : return gen_rtx_CONST_VECTOR (mode, v);
7417 : : }
7418 : :
7419 : 416725 : if (swap_commutative_operands_p (op0, op1)
7420 : : /* Two operands have same precedence, then first bit of mask
7421 : : select first operand. */
7422 : 416725 : || (!swap_commutative_operands_p (op1, op0) && !(sel & 1)))
7423 : 29487 : return simplify_gen_ternary (code, mode, mode, op1, op0,
7424 : 58974 : GEN_INT (~sel & mask));
7425 : :
7426 : : /* Replace (vec_merge (vec_merge a b m) c n) with (vec_merge b c n)
7427 : : if no element from a appears in the result. */
7428 : 387238 : if (GET_CODE (op0) == VEC_MERGE)
7429 : : {
7430 : 17167 : tem = avoid_constant_pool_reference (XEXP (op0, 2));
7431 : 17167 : if (CONST_INT_P (tem))
7432 : : {
7433 : 1331 : unsigned HOST_WIDE_INT sel0 = UINTVAL (tem);
7434 : 1331 : if (!(sel & sel0 & mask) && !side_effects_p (XEXP (op0, 0)))
7435 : 99 : return simplify_gen_ternary (code, mode, mode,
7436 : 99 : XEXP (op0, 1), op1, op2);
7437 : 1232 : if (!(sel & ~sel0 & mask) && !side_effects_p (XEXP (op0, 1)))
7438 : 786 : return simplify_gen_ternary (code, mode, mode,
7439 : 786 : XEXP (op0, 0), op1, op2);
7440 : : }
7441 : : }
7442 : 386353 : if (GET_CODE (op1) == VEC_MERGE)
7443 : : {
7444 : 175 : tem = avoid_constant_pool_reference (XEXP (op1, 2));
7445 : 175 : if (CONST_INT_P (tem))
7446 : : {
7447 : 144 : unsigned HOST_WIDE_INT sel1 = UINTVAL (tem);
7448 : 144 : if (!(~sel & sel1 & mask) && !side_effects_p (XEXP (op1, 0)))
7449 : 113 : return simplify_gen_ternary (code, mode, mode,
7450 : 113 : op0, XEXP (op1, 1), op2);
7451 : 31 : if (!(~sel & ~sel1 & mask) && !side_effects_p (XEXP (op1, 1)))
7452 : 4 : return simplify_gen_ternary (code, mode, mode,
7453 : 4 : op0, XEXP (op1, 0), op2);
7454 : : }
7455 : : }
7456 : :
7457 : : /* Replace (vec_merge (vec_duplicate (vec_select a parallel (i))) a 1 << i)
7458 : : with a. */
7459 : 386236 : if (GET_CODE (op0) == VEC_DUPLICATE
7460 : 131268 : && GET_CODE (XEXP (op0, 0)) == VEC_SELECT
7461 : 624 : && GET_CODE (XEXP (XEXP (op0, 0), 1)) == PARALLEL
7462 : 387484 : && known_eq (GET_MODE_NUNITS (GET_MODE (XEXP (op0, 0))), 1))
7463 : : {
7464 : 556 : tem = XVECEXP ((XEXP (XEXP (op0, 0), 1)), 0, 0);
7465 : 556 : if (CONST_INT_P (tem) && CONST_INT_P (op2))
7466 : : {
7467 : 556 : if (XEXP (XEXP (op0, 0), 0) == op1
7468 : 2 : && UINTVAL (op2) == HOST_WIDE_INT_1U << UINTVAL (tem))
7469 : : return op1;
7470 : : }
7471 : : }
7472 : : /* Replace (vec_merge (vec_duplicate (X)) (const_vector [A, B])
7473 : : (const_int N))
7474 : : with (vec_concat (X) (B)) if N == 1 or
7475 : : (vec_concat (A) (X)) if N == 2. */
7476 : 386234 : if (GET_CODE (op0) == VEC_DUPLICATE
7477 : 131266 : && GET_CODE (op1) == CONST_VECTOR
7478 : 138270 : && known_eq (CONST_VECTOR_NUNITS (op1), 2)
7479 : 1714 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7480 : 387091 : && IN_RANGE (sel, 1, 2))
7481 : : {
7482 : 855 : rtx newop0 = XEXP (op0, 0);
7483 : 855 : rtx newop1 = CONST_VECTOR_ELT (op1, 2 - sel);
7484 : 855 : if (sel == 2)
7485 : 123 : std::swap (newop0, newop1);
7486 : 855 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7487 : : }
7488 : : /* Replace (vec_merge (vec_duplicate x) (vec_concat (y) (z)) (const_int N))
7489 : : with (vec_concat x z) if N == 1, or (vec_concat y x) if N == 2.
7490 : : Only applies for vectors of two elements. */
7491 : 385379 : if (GET_CODE (op0) == VEC_DUPLICATE
7492 : 130411 : && GET_CODE (op1) == VEC_CONCAT
7493 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7494 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7495 : 385379 : && IN_RANGE (sel, 1, 2))
7496 : : {
7497 : 0 : rtx newop0 = XEXP (op0, 0);
7498 : 0 : rtx newop1 = XEXP (op1, 2 - sel);
7499 : 0 : rtx otherop = XEXP (op1, sel - 1);
7500 : 0 : if (sel == 2)
7501 : 0 : std::swap (newop0, newop1);
7502 : : /* Don't want to throw away the other part of the vec_concat if
7503 : : it has side-effects. */
7504 : 0 : if (!side_effects_p (otherop))
7505 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7506 : : }
7507 : :
7508 : : /* Replace:
7509 : :
7510 : : (vec_merge:outer (vec_duplicate:outer x:inner)
7511 : : (subreg:outer y:inner 0)
7512 : : (const_int N))
7513 : :
7514 : : with (vec_concat:outer x:inner y:inner) if N == 1,
7515 : : or (vec_concat:outer y:inner x:inner) if N == 2.
7516 : :
7517 : : Implicitly, this means we have a paradoxical subreg, but such
7518 : : a check is cheap, so make it anyway.
7519 : :
7520 : : Only applies for vectors of two elements. */
7521 : 385379 : if (GET_CODE (op0) == VEC_DUPLICATE
7522 : 130411 : && GET_CODE (op1) == SUBREG
7523 : 40829 : && GET_MODE (op1) == GET_MODE (op0)
7524 : 40829 : && GET_MODE (SUBREG_REG (op1)) == GET_MODE (XEXP (op0, 0))
7525 : 0 : && paradoxical_subreg_p (op1)
7526 : 0 : && subreg_lowpart_p (op1)
7527 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7528 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7529 : 385379 : && IN_RANGE (sel, 1, 2))
7530 : : {
7531 : 0 : rtx newop0 = XEXP (op0, 0);
7532 : 0 : rtx newop1 = SUBREG_REG (op1);
7533 : 0 : if (sel == 2)
7534 : 0 : std::swap (newop0, newop1);
7535 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7536 : : }
7537 : :
7538 : : /* Same as above but with switched operands:
7539 : : Replace (vec_merge:outer (subreg:outer x:inner 0)
7540 : : (vec_duplicate:outer y:inner)
7541 : : (const_int N))
7542 : :
7543 : : with (vec_concat:outer x:inner y:inner) if N == 1,
7544 : : or (vec_concat:outer y:inner x:inner) if N == 2. */
7545 : 385379 : if (GET_CODE (op1) == VEC_DUPLICATE
7546 : 26690 : && GET_CODE (op0) == SUBREG
7547 : 24141 : && GET_MODE (op0) == GET_MODE (op1)
7548 : 24141 : && GET_MODE (SUBREG_REG (op0)) == GET_MODE (XEXP (op1, 0))
7549 : 0 : && paradoxical_subreg_p (op0)
7550 : 0 : && subreg_lowpart_p (op0)
7551 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7552 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7553 : 385379 : && IN_RANGE (sel, 1, 2))
7554 : : {
7555 : 0 : rtx newop0 = SUBREG_REG (op0);
7556 : 0 : rtx newop1 = XEXP (op1, 0);
7557 : 0 : if (sel == 2)
7558 : 0 : std::swap (newop0, newop1);
7559 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7560 : : }
7561 : :
7562 : : /* Replace (vec_merge (vec_duplicate x) (vec_duplicate y)
7563 : : (const_int n))
7564 : : with (vec_concat x y) or (vec_concat y x) depending on value
7565 : : of N. */
7566 : 385379 : if (GET_CODE (op0) == VEC_DUPLICATE
7567 : 130411 : && GET_CODE (op1) == VEC_DUPLICATE
7568 : 198 : && known_eq (GET_MODE_NUNITS (GET_MODE (op0)), 2)
7569 : 0 : && known_eq (GET_MODE_NUNITS (GET_MODE (op1)), 2)
7570 : 385379 : && IN_RANGE (sel, 1, 2))
7571 : : {
7572 : 0 : rtx newop0 = XEXP (op0, 0);
7573 : 0 : rtx newop1 = XEXP (op1, 0);
7574 : 0 : if (sel == 2)
7575 : 0 : std::swap (newop0, newop1);
7576 : :
7577 : 0 : return simplify_gen_binary (VEC_CONCAT, mode, newop0, newop1);
7578 : : }
7579 : : }
7580 : :
7581 : 681674 : if (rtx_equal_p (op0, op1)
7582 : 681674 : && !side_effects_p (op2) && !side_effects_p (op1))
7583 : : return op0;
7584 : :
7585 : 681399 : if (!side_effects_p (op2))
7586 : : {
7587 : 678310 : rtx top0
7588 : 678310 : = may_trap_p (op0) ? NULL_RTX : simplify_merge_mask (op0, op2, 0);
7589 : 678310 : rtx top1
7590 : 678310 : = may_trap_p (op1) ? NULL_RTX : simplify_merge_mask (op1, op2, 1);
7591 : 678310 : if (top0 || top1)
7592 : 824 : return simplify_gen_ternary (code, mode, mode,
7593 : : top0 ? top0 : op0,
7594 : 652 : top1 ? top1 : op1, op2);
7595 : : }
7596 : :
7597 : : break;
7598 : :
7599 : 0 : default:
7600 : 0 : gcc_unreachable ();
7601 : : }
7602 : :
7603 : : return 0;
7604 : : }
7605 : :
7606 : : /* Try to calculate NUM_BYTES bytes of the target memory image of X,
7607 : : starting at byte FIRST_BYTE. Return true on success and add the
7608 : : bytes to BYTES, such that each byte has BITS_PER_UNIT bits and such
7609 : : that the bytes follow target memory order. Leave BYTES unmodified
7610 : : on failure.
7611 : :
7612 : : MODE is the mode of X. The caller must reserve NUM_BYTES bytes in
7613 : : BYTES before calling this function. */
7614 : :
7615 : : bool
7616 : 13509245 : native_encode_rtx (machine_mode mode, rtx x, vec<target_unit> &bytes,
7617 : : unsigned int first_byte, unsigned int num_bytes)
7618 : : {
7619 : : /* Check the mode is sensible. */
7620 : 13509245 : gcc_assert (GET_MODE (x) == VOIDmode
7621 : : ? is_a <scalar_int_mode> (mode)
7622 : : : mode == GET_MODE (x));
7623 : :
7624 : 13509245 : if (GET_CODE (x) == CONST_VECTOR)
7625 : : {
7626 : : /* CONST_VECTOR_ELT follows target memory order, so no shuffling
7627 : : is necessary. The only complication is that MODE_VECTOR_BOOL
7628 : : vectors can have several elements per byte. */
7629 : 921186 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
7630 : : GET_MODE_NUNITS (mode));
7631 : 460593 : unsigned int elt = first_byte * BITS_PER_UNIT / elt_bits;
7632 : 460593 : if (elt_bits < BITS_PER_UNIT)
7633 : : {
7634 : : /* This is the only case in which elements can be smaller than
7635 : : a byte. */
7636 : 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
7637 : 0 : auto mask = GET_MODE_MASK (GET_MODE_INNER (mode));
7638 : 0 : for (unsigned int i = 0; i < num_bytes; ++i)
7639 : : {
7640 : 0 : target_unit value = 0;
7641 : 0 : for (unsigned int j = 0; j < BITS_PER_UNIT; j += elt_bits)
7642 : : {
7643 : 0 : if (INTVAL (CONST_VECTOR_ELT (x, elt)))
7644 : 0 : value |= mask << j;
7645 : 0 : elt += 1;
7646 : : }
7647 : 0 : bytes.quick_push (value);
7648 : : }
7649 : : return true;
7650 : : }
7651 : :
7652 : 460593 : unsigned int start = bytes.length ();
7653 : 460593 : unsigned int elt_bytes = GET_MODE_UNIT_SIZE (mode);
7654 : : /* Make FIRST_BYTE relative to ELT. */
7655 : 460593 : first_byte %= elt_bytes;
7656 : 2490194 : while (num_bytes > 0)
7657 : : {
7658 : : /* Work out how many bytes we want from element ELT. */
7659 : 2029601 : unsigned int chunk_bytes = MIN (num_bytes, elt_bytes - first_byte);
7660 : 4059202 : if (!native_encode_rtx (GET_MODE_INNER (mode),
7661 : : CONST_VECTOR_ELT (x, elt), bytes,
7662 : : first_byte, chunk_bytes))
7663 : : {
7664 : 0 : bytes.truncate (start);
7665 : 0 : return false;
7666 : : }
7667 : 2029601 : elt += 1;
7668 : 2029601 : first_byte = 0;
7669 : 2029601 : num_bytes -= chunk_bytes;
7670 : : }
7671 : : return true;
7672 : : }
7673 : :
7674 : : /* All subsequent cases are limited to scalars. */
7675 : 13048652 : scalar_mode smode;
7676 : 13080110 : if (!is_a <scalar_mode> (mode, &smode))
7677 : : return false;
7678 : :
7679 : : /* Make sure that the region is in range. */
7680 : 13048652 : unsigned int end_byte = first_byte + num_bytes;
7681 : 13048652 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
7682 : 13048652 : gcc_assert (end_byte <= mode_bytes);
7683 : :
7684 : 13048652 : if (CONST_SCALAR_INT_P (x))
7685 : : {
7686 : : /* The target memory layout is affected by both BYTES_BIG_ENDIAN
7687 : : and WORDS_BIG_ENDIAN. Use the subreg machinery to get the lsb
7688 : : position of each byte. */
7689 : 12531252 : rtx_mode_t value (x, smode);
7690 : 12531252 : wide_int_ref value_wi (value);
7691 : 53301982 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7692 : : {
7693 : : /* Always constant because the inputs are. */
7694 : 40770730 : unsigned int lsb
7695 : 40770730 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7696 : : /* Operate directly on the encoding rather than using
7697 : : wi::extract_uhwi, so that we preserve the sign or zero
7698 : : extension for modes that are not a whole number of bits in
7699 : : size. (Zero extension is only used for the combination of
7700 : : innermode == BImode && STORE_FLAG_VALUE == 1). */
7701 : 40770730 : unsigned int elt = lsb / HOST_BITS_PER_WIDE_INT;
7702 : 40770730 : unsigned int shift = lsb % HOST_BITS_PER_WIDE_INT;
7703 : 40770730 : unsigned HOST_WIDE_INT uhwi = value_wi.elt (elt);
7704 : 40770730 : bytes.quick_push (uhwi >> shift);
7705 : : }
7706 : 12531252 : return true;
7707 : : }
7708 : :
7709 : 517400 : if (CONST_DOUBLE_P (x))
7710 : : {
7711 : : /* real_to_target produces an array of integers in target memory order.
7712 : : All integers before the last one have 32 bits; the last one may
7713 : : have 32 bits or fewer, depending on whether the mode bitsize
7714 : : is divisible by 32. Each of these integers is then laid out
7715 : : in target memory as any other integer would be. */
7716 : 485942 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
7717 : 485942 : real_to_target (el32, CONST_DOUBLE_REAL_VALUE (x), smode);
7718 : :
7719 : : /* The (maximum) number of target bytes per element of el32. */
7720 : 485942 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
7721 : 485942 : gcc_assert (bytes_per_el32 != 0);
7722 : :
7723 : : /* Build up the integers in a similar way to the CONST_SCALAR_INT_P
7724 : : handling above. */
7725 : 3368061 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7726 : : {
7727 : 2882119 : unsigned int index = byte / bytes_per_el32;
7728 : 2882119 : unsigned int subbyte = byte % bytes_per_el32;
7729 : 2882119 : unsigned int int_bytes = MIN (bytes_per_el32,
7730 : : mode_bytes - index * bytes_per_el32);
7731 : : /* Always constant because the inputs are. */
7732 : 2882119 : unsigned int lsb
7733 : 2882119 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
7734 : 2882119 : bytes.quick_push ((unsigned long) el32[index] >> lsb);
7735 : : }
7736 : 485942 : return true;
7737 : : }
7738 : :
7739 : 31458 : if (GET_CODE (x) == CONST_FIXED)
7740 : : {
7741 : 0 : for (unsigned int byte = first_byte; byte < end_byte; ++byte)
7742 : : {
7743 : : /* Always constant because the inputs are. */
7744 : 0 : unsigned int lsb
7745 : 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7746 : 0 : unsigned HOST_WIDE_INT piece = CONST_FIXED_VALUE_LOW (x);
7747 : 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
7748 : : {
7749 : 0 : lsb -= HOST_BITS_PER_WIDE_INT;
7750 : 0 : piece = CONST_FIXED_VALUE_HIGH (x);
7751 : : }
7752 : 0 : bytes.quick_push (piece >> lsb);
7753 : : }
7754 : : return true;
7755 : : }
7756 : :
7757 : : return false;
7758 : : }
7759 : :
7760 : : /* Read a vector of mode MODE from the target memory image given by BYTES,
7761 : : starting at byte FIRST_BYTE. The vector is known to be encodable using
7762 : : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each,
7763 : : and BYTES is known to have enough bytes to supply NPATTERNS *
7764 : : NELTS_PER_PATTERN vector elements. Each element of BYTES contains
7765 : : BITS_PER_UNIT bits and the bytes are in target memory order.
7766 : :
7767 : : Return the vector on success, otherwise return NULL_RTX. */
7768 : :
7769 : : rtx
7770 : 193328 : native_decode_vector_rtx (machine_mode mode, const vec<target_unit> &bytes,
7771 : : unsigned int first_byte, unsigned int npatterns,
7772 : : unsigned int nelts_per_pattern)
7773 : : {
7774 : 193328 : rtx_vector_builder builder (mode, npatterns, nelts_per_pattern);
7775 : :
7776 : 386656 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
7777 : : GET_MODE_NUNITS (mode));
7778 : 193328 : if (elt_bits < BITS_PER_UNIT)
7779 : : {
7780 : : /* This is the only case in which elements can be smaller than a byte.
7781 : : Element 0 is always in the lsb of the containing byte. */
7782 : 0 : gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL);
7783 : 0 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
7784 : : {
7785 : 0 : unsigned int bit_index = first_byte * BITS_PER_UNIT + i * elt_bits;
7786 : 0 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
7787 : 0 : unsigned int lsb = bit_index % BITS_PER_UNIT;
7788 : 0 : unsigned int value = bytes[byte_index] >> lsb;
7789 : 0 : builder.quick_push (gen_int_mode (value, GET_MODE_INNER (mode)));
7790 : : }
7791 : : }
7792 : : else
7793 : : {
7794 : 766214 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
7795 : : {
7796 : 1145772 : rtx x = native_decode_rtx (GET_MODE_INNER (mode), bytes, first_byte);
7797 : 572886 : if (!x)
7798 : 0 : return NULL_RTX;
7799 : 572886 : builder.quick_push (x);
7800 : 572886 : first_byte += elt_bits / BITS_PER_UNIT;
7801 : : }
7802 : : }
7803 : 193328 : return builder.build ();
7804 : 193328 : }
7805 : :
7806 : : /* Extract a PRECISION-bit integer from bytes [FIRST_BYTE, FIRST_BYTE + SIZE)
7807 : : of target memory image BYTES. */
7808 : :
7809 : : wide_int
7810 : 11564201 : native_decode_int (const vec<target_unit> &bytes, unsigned int first_byte,
7811 : : unsigned int size, unsigned int precision)
7812 : : {
7813 : : /* Pull the bytes msb first, so that we can use simple
7814 : : shift-and-insert wide_int operations. */
7815 : 11564201 : wide_int result (wi::zero (precision));
7816 : 52892469 : for (unsigned int i = 0; i < size; ++i)
7817 : : {
7818 : 41328268 : unsigned int lsb = (size - i - 1) * BITS_PER_UNIT;
7819 : : /* Always constant because the inputs are. */
7820 : 41328268 : unsigned int subbyte
7821 : 41328268 : = subreg_size_offset_from_lsb (1, size, lsb).to_constant ();
7822 : 41328268 : result <<= BITS_PER_UNIT;
7823 : 41328268 : result |= bytes[first_byte + subbyte];
7824 : : }
7825 : 11564201 : return result;
7826 : : }
7827 : :
7828 : : /* Read an rtx of mode MODE from the target memory image given by BYTES,
7829 : : starting at byte FIRST_BYTE. Each element of BYTES contains BITS_PER_UNIT
7830 : : bits and the bytes are in target memory order. The image has enough
7831 : : values to specify all bytes of MODE.
7832 : :
7833 : : Return the rtx on success, otherwise return NULL_RTX. */
7834 : :
7835 : : rtx
7836 : 11704898 : native_decode_rtx (machine_mode mode, const vec<target_unit> &bytes,
7837 : : unsigned int first_byte)
7838 : : {
7839 : 11704898 : if (VECTOR_MODE_P (mode))
7840 : : {
7841 : : /* If we know at compile time how many elements there are,
7842 : : pull each element directly from BYTES. */
7843 : 25605 : unsigned int nelts;
7844 : 51210 : if (GET_MODE_NUNITS (mode).is_constant (&nelts))
7845 : 25605 : return native_decode_vector_rtx (mode, bytes, first_byte, nelts, 1);
7846 : : return NULL_RTX;
7847 : : }
7848 : :
7849 : 11679293 : scalar_int_mode imode;
7850 : 11679293 : if (is_a <scalar_int_mode> (mode, &imode)
7851 : 11564201 : && GET_MODE_PRECISION (imode) <= MAX_BITSIZE_MODE_ANY_INT)
7852 : : {
7853 : 11564201 : auto result = native_decode_int (bytes, first_byte,
7854 : 11564201 : GET_MODE_SIZE (imode),
7855 : 23128402 : GET_MODE_PRECISION (imode));
7856 : 11564201 : return immed_wide_int_const (result, imode);
7857 : 11564201 : }
7858 : :
7859 : 115092 : scalar_float_mode fmode;
7860 : 115092 : if (is_a <scalar_float_mode> (mode, &fmode))
7861 : : {
7862 : : /* We need to build an array of integers in target memory order.
7863 : : All integers before the last one have 32 bits; the last one may
7864 : : have 32 bits or fewer, depending on whether the mode bitsize
7865 : : is divisible by 32. */
7866 : 115062 : long el32[MAX_BITSIZE_MODE_ANY_MODE / 32];
7867 : 115062 : unsigned int num_el32 = CEIL (GET_MODE_BITSIZE (fmode), 32);
7868 : 115062 : memset (el32, 0, num_el32 * sizeof (long));
7869 : :
7870 : : /* The (maximum) number of target bytes per element of el32. */
7871 : 115062 : unsigned int bytes_per_el32 = 32 / BITS_PER_UNIT;
7872 : 115062 : gcc_assert (bytes_per_el32 != 0);
7873 : :
7874 : 115062 : unsigned int mode_bytes = GET_MODE_SIZE (fmode);
7875 : 864906 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
7876 : : {
7877 : 749844 : unsigned int index = byte / bytes_per_el32;
7878 : 749844 : unsigned int subbyte = byte % bytes_per_el32;
7879 : 749844 : unsigned int int_bytes = MIN (bytes_per_el32,
7880 : : mode_bytes - index * bytes_per_el32);
7881 : : /* Always constant because the inputs are. */
7882 : 749844 : unsigned int lsb
7883 : 749844 : = subreg_size_lsb (1, int_bytes, subbyte).to_constant ();
7884 : 749844 : el32[index] |= (unsigned long) bytes[first_byte + byte] << lsb;
7885 : : }
7886 : 115062 : REAL_VALUE_TYPE r;
7887 : 115062 : real_from_target (&r, el32, fmode);
7888 : 115062 : return const_double_from_real_value (r, fmode);
7889 : : }
7890 : :
7891 : 30 : if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
7892 : : {
7893 : 0 : scalar_mode smode = as_a <scalar_mode> (mode);
7894 : 0 : FIXED_VALUE_TYPE f;
7895 : 0 : f.data.low = 0;
7896 : 0 : f.data.high = 0;
7897 : 0 : f.mode = smode;
7898 : :
7899 : 0 : unsigned int mode_bytes = GET_MODE_SIZE (smode);
7900 : 0 : for (unsigned int byte = 0; byte < mode_bytes; ++byte)
7901 : : {
7902 : : /* Always constant because the inputs are. */
7903 : 0 : unsigned int lsb
7904 : 0 : = subreg_size_lsb (1, mode_bytes, byte).to_constant ();
7905 : 0 : unsigned HOST_WIDE_INT unit = bytes[first_byte + byte];
7906 : 0 : if (lsb >= HOST_BITS_PER_WIDE_INT)
7907 : 0 : f.data.high |= unit << (lsb - HOST_BITS_PER_WIDE_INT);
7908 : : else
7909 : 0 : f.data.low |= unit << lsb;
7910 : : }
7911 : 0 : return CONST_FIXED_FROM_FIXED_VALUE (f, mode);
7912 : : }
7913 : :
7914 : : return NULL_RTX;
7915 : : }
7916 : :
7917 : : /* Simplify a byte offset BYTE into CONST_VECTOR X. The main purpose
7918 : : is to convert a runtime BYTE value into a constant one. */
7919 : :
7920 : : static poly_uint64
7921 : 282837 : simplify_const_vector_byte_offset (rtx x, poly_uint64 byte)
7922 : : {
7923 : : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
7924 : 282837 : machine_mode mode = GET_MODE (x);
7925 : 565674 : unsigned int elt_bits = vector_element_size (GET_MODE_PRECISION (mode),
7926 : : GET_MODE_NUNITS (mode));
7927 : : /* The number of bits needed to encode one element from each pattern. */
7928 : 282837 : unsigned int sequence_bits = CONST_VECTOR_NPATTERNS (x) * elt_bits;
7929 : :
7930 : : /* Identify the start point in terms of a sequence number and a byte offset
7931 : : within that sequence. */
7932 : 282837 : poly_uint64 first_sequence;
7933 : 282837 : unsigned HOST_WIDE_INT subbit;
7934 : 282837 : if (can_div_trunc_p (byte * BITS_PER_UNIT, sequence_bits,
7935 : : &first_sequence, &subbit))
7936 : : {
7937 : 282837 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
7938 : 282837 : if (nelts_per_pattern == 1)
7939 : : /* This is a duplicated vector, so the value of FIRST_SEQUENCE
7940 : : doesn't matter. */
7941 : 230591 : byte = subbit / BITS_PER_UNIT;
7942 : 52246 : else if (nelts_per_pattern == 2 && known_gt (first_sequence, 0U))
7943 : : {
7944 : : /* The subreg drops the first element from each pattern and
7945 : : only uses the second element. Find the first sequence
7946 : : that starts on a byte boundary. */
7947 : 5568 : subbit += least_common_multiple (sequence_bits, BITS_PER_UNIT);
7948 : 5568 : byte = subbit / BITS_PER_UNIT;
7949 : : }
7950 : : }
7951 : 282837 : return byte;
7952 : : }
7953 : :
7954 : : /* Subroutine of simplify_subreg in which:
7955 : :
7956 : : - X is known to be a CONST_VECTOR
7957 : : - OUTERMODE is known to be a vector mode
7958 : :
7959 : : Try to handle the subreg by operating on the CONST_VECTOR encoding
7960 : : rather than on each individual element of the CONST_VECTOR.
7961 : :
7962 : : Return the simplified subreg on success, otherwise return NULL_RTX. */
7963 : :
7964 : : static rtx
7965 : 175133 : simplify_const_vector_subreg (machine_mode outermode, rtx x,
7966 : : machine_mode innermode, unsigned int first_byte)
7967 : : {
7968 : : /* Paradoxical subregs of vectors have dubious semantics. */
7969 : 175133 : if (paradoxical_subreg_p (outermode, innermode))
7970 : : return NULL_RTX;
7971 : :
7972 : : /* We can only preserve the semantics of a stepped pattern if the new
7973 : : vector element is the same as the original one. */
7974 : 175067 : if (CONST_VECTOR_STEPPED_P (x)
7975 : 194979 : && GET_MODE_INNER (outermode) != GET_MODE_INNER (innermode))
7976 : : return NULL_RTX;
7977 : :
7978 : : /* Cope with MODE_VECTOR_BOOL by operating on bits rather than bytes. */
7979 : 167723 : unsigned int x_elt_bits
7980 : 167723 : = vector_element_size (GET_MODE_PRECISION (innermode),
7981 : : GET_MODE_NUNITS (innermode));
7982 : 167723 : unsigned int out_elt_bits
7983 : 167723 : = vector_element_size (GET_MODE_PRECISION (outermode),
7984 : : GET_MODE_NUNITS (outermode));
7985 : :
7986 : : /* The number of bits needed to encode one element from every pattern
7987 : : of the original vector. */
7988 : 167723 : unsigned int x_sequence_bits = CONST_VECTOR_NPATTERNS (x) * x_elt_bits;
7989 : :
7990 : : /* The number of bits needed to encode one element from every pattern
7991 : : of the result. */
7992 : 167723 : unsigned int out_sequence_bits
7993 : 167723 : = least_common_multiple (x_sequence_bits, out_elt_bits);
7994 : :
7995 : : /* Work out the number of interleaved patterns in the output vector
7996 : : and the number of encoded elements per pattern. */
7997 : 167723 : unsigned int out_npatterns = out_sequence_bits / out_elt_bits;
7998 : 167723 : unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
7999 : :
8000 : : /* The encoding scheme requires the number of elements to be a multiple
8001 : : of the number of patterns, so that each pattern appears at least once
8002 : : and so that the same number of elements appear from each pattern. */
8003 : 335446 : bool ok_p = multiple_p (GET_MODE_NUNITS (outermode), out_npatterns);
8004 : 167723 : unsigned int const_nunits;
8005 : 335446 : if (GET_MODE_NUNITS (outermode).is_constant (&const_nunits)
8006 : 167723 : && (!ok_p || out_npatterns * nelts_per_pattern > const_nunits))
8007 : : {
8008 : : /* Either the encoding is invalid, or applying it would give us
8009 : : more elements than we need. Just encode each element directly. */
8010 : : out_npatterns = const_nunits;
8011 : : nelts_per_pattern = 1;
8012 : : }
8013 : : else if (!ok_p)
8014 : : return NULL_RTX;
8015 : :
8016 : : /* Get enough bytes of X to form the new encoding. */
8017 : 167723 : unsigned int buffer_bits = out_npatterns * nelts_per_pattern * out_elt_bits;
8018 : 167723 : unsigned int buffer_bytes = CEIL (buffer_bits, BITS_PER_UNIT);
8019 : 167723 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8020 : 167723 : if (!native_encode_rtx (innermode, x, buffer, first_byte, buffer_bytes))
8021 : : return NULL_RTX;
8022 : :
8023 : : /* Reencode the bytes as OUTERMODE. */
8024 : 167723 : return native_decode_vector_rtx (outermode, buffer, 0, out_npatterns,
8025 : 167723 : nelts_per_pattern);
8026 : 167723 : }
8027 : :
8028 : : /* Try to simplify a subreg of a constant by encoding the subreg region
8029 : : as a sequence of target bytes and reading them back in the new mode.
8030 : : Return the new value on success, otherwise return null.
8031 : :
8032 : : The subreg has outer mode OUTERMODE, inner mode INNERMODE, inner value X
8033 : : and byte offset FIRST_BYTE. */
8034 : :
8035 : : static rtx
8036 : 10837757 : simplify_immed_subreg (fixed_size_mode outermode, rtx x,
8037 : : machine_mode innermode, unsigned int first_byte)
8038 : : {
8039 : 10837757 : unsigned int buffer_bytes = GET_MODE_SIZE (outermode);
8040 : 10837757 : auto_vec<target_unit, 128> buffer (buffer_bytes);
8041 : :
8042 : : /* Some ports misuse CCmode. */
8043 : 10837757 : if (GET_MODE_CLASS (outermode) == MODE_CC && CONST_INT_P (x))
8044 : : return x;
8045 : :
8046 : : /* Paradoxical subregs read undefined values for bytes outside of the
8047 : : inner value. However, we have traditionally always sign-extended
8048 : : integer constants and zero-extended others. */
8049 : 10837129 : unsigned int inner_bytes = buffer_bytes;
8050 : 10837129 : if (paradoxical_subreg_p (outermode, innermode))
8051 : : {
8052 : 634570 : if (!GET_MODE_SIZE (innermode).is_constant (&inner_bytes))
8053 : 0 : return NULL_RTX;
8054 : :
8055 : 317285 : target_unit filler = 0;
8056 : 317285 : if (CONST_SCALAR_INT_P (x) && wi::neg_p (rtx_mode_t (x, innermode)))
8057 : 28446 : filler = -1;
8058 : :
8059 : : /* Add any leading bytes due to big-endian layout. The number of
8060 : : bytes must be constant because both modes have constant size. */
8061 : 317285 : unsigned int leading_bytes
8062 : 317285 : = -byte_lowpart_offset (outermode, innermode).to_constant ();
8063 : 317285 : for (unsigned int i = 0; i < leading_bytes; ++i)
8064 : 0 : buffer.quick_push (filler);
8065 : :
8066 : 317285 : if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8067 : 0 : return NULL_RTX;
8068 : :
8069 : : /* Add any trailing bytes due to little-endian layout. */
8070 : 3555938 : while (buffer.length () < buffer_bytes)
8071 : 1460684 : buffer.quick_push (filler);
8072 : : }
8073 : 10519844 : else if (!native_encode_rtx (innermode, x, buffer, first_byte, inner_bytes))
8074 : : return NULL_RTX;
8075 : 10837129 : rtx ret = native_decode_rtx (outermode, buffer, 0);
8076 : 10837129 : if (ret && FLOAT_MODE_P (outermode))
8077 : : {
8078 : 69266 : auto_vec<target_unit, 128> buffer2 (buffer_bytes);
8079 : 69266 : if (!native_encode_rtx (outermode, ret, buffer2, 0, buffer_bytes))
8080 : : return NULL_RTX;
8081 : 652607 : for (unsigned int i = 0; i < buffer_bytes; ++i)
8082 : 583376 : if (buffer[i] != buffer2[i])
8083 : : return NULL_RTX;
8084 : 69266 : }
8085 : : return ret;
8086 : 10837757 : }
8087 : :
8088 : : /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
8089 : : Return 0 if no simplifications are possible. */
8090 : : rtx
8091 : 68881038 : simplify_context::simplify_subreg (machine_mode outermode, rtx op,
8092 : : machine_mode innermode, poly_uint64 byte)
8093 : : {
8094 : : /* Little bit of sanity checking. */
8095 : 68881038 : gcc_assert (innermode != VOIDmode);
8096 : 68881038 : gcc_assert (outermode != VOIDmode);
8097 : 68881038 : gcc_assert (innermode != BLKmode);
8098 : 68881038 : gcc_assert (outermode != BLKmode);
8099 : :
8100 : 68881038 : gcc_assert (GET_MODE (op) == innermode
8101 : : || GET_MODE (op) == VOIDmode);
8102 : :
8103 : 137762076 : poly_uint64 outersize = GET_MODE_SIZE (outermode);
8104 : 68881038 : if (!multiple_p (byte, outersize))
8105 : : return NULL_RTX;
8106 : :
8107 : 137762068 : poly_uint64 innersize = GET_MODE_SIZE (innermode);
8108 : 68881034 : if (maybe_ge (byte, innersize))
8109 : : return NULL_RTX;
8110 : :
8111 : 68881034 : if (outermode == innermode && known_eq (byte, 0U))
8112 : 4648611 : return op;
8113 : :
8114 : 64232423 : if (GET_CODE (op) == CONST_VECTOR)
8115 : 282837 : byte = simplify_const_vector_byte_offset (op, byte);
8116 : :
8117 : 128464846 : if (multiple_p (byte, GET_MODE_UNIT_SIZE (innermode)))
8118 : : {
8119 : 58295479 : rtx elt;
8120 : :
8121 : 50155070 : if (VECTOR_MODE_P (outermode)
8122 : 24421227 : && GET_MODE_INNER (outermode) == GET_MODE_INNER (innermode)
8123 : 59836091 : && vec_duplicate_p (op, &elt))
8124 : 13518 : return gen_vec_duplicate (outermode, elt);
8125 : :
8126 : 58291783 : if (outermode == GET_MODE_INNER (innermode)
8127 : 58291783 : && vec_duplicate_p (op, &elt))
8128 : 9822 : return elt;
8129 : : }
8130 : :
8131 : 64218905 : if (CONST_SCALAR_INT_P (op)
8132 : 53511703 : || CONST_DOUBLE_AS_FLOAT_P (op)
8133 : 53487913 : || CONST_FIXED_P (op)
8134 : 53487913 : || GET_CODE (op) == CONST_VECTOR)
8135 : : {
8136 : 11005480 : unsigned HOST_WIDE_INT cbyte;
8137 : 11005480 : if (byte.is_constant (&cbyte))
8138 : : {
8139 : 11005480 : if (GET_CODE (op) == CONST_VECTOR && VECTOR_MODE_P (outermode))
8140 : : {
8141 : 175133 : rtx tmp = simplify_const_vector_subreg (outermode, op,
8142 : : innermode, cbyte);
8143 : 175133 : if (tmp)
8144 : 11005480 : return tmp;
8145 : : }
8146 : :
8147 : 10837757 : fixed_size_mode fs_outermode;
8148 : 10837757 : if (is_a <fixed_size_mode> (outermode, &fs_outermode))
8149 : 10837757 : return simplify_immed_subreg (fs_outermode, op, innermode, cbyte);
8150 : : }
8151 : : }
8152 : :
8153 : : /* Changing mode twice with SUBREG => just change it once,
8154 : : or not at all if changing back op starting mode. */
8155 : 53213425 : if (GET_CODE (op) == SUBREG)
8156 : : {
8157 : 1271150 : machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
8158 : 2542300 : poly_uint64 innermostsize = GET_MODE_SIZE (innermostmode);
8159 : 1271150 : rtx newx;
8160 : :
8161 : : /* Make sure that the relationship between the two subregs is
8162 : : known at compile time. */
8163 : 1271150 : if (!ordered_p (outersize, innermostsize))
8164 : : return NULL_RTX;
8165 : :
8166 : 1271150 : if (outermode == innermostmode
8167 : 690669 : && known_eq (byte, subreg_lowpart_offset (outermode, innermode))
8168 : 1961818 : && known_eq (SUBREG_BYTE (op),
8169 : : subreg_lowpart_offset (innermode, innermostmode)))
8170 : 690668 : return SUBREG_REG (op);
8171 : :
8172 : : /* Work out the memory offset of the final OUTERMODE value relative
8173 : : to the inner value of OP. */
8174 : 580482 : poly_int64 mem_offset = subreg_memory_offset (outermode,
8175 : : innermode, byte);
8176 : 580482 : poly_int64 op_mem_offset = subreg_memory_offset (op);
8177 : 580482 : poly_int64 final_offset = mem_offset + op_mem_offset;
8178 : :
8179 : : /* See whether resulting subreg will be paradoxical. */
8180 : 580482 : if (!paradoxical_subreg_p (outermode, innermostmode))
8181 : : {
8182 : : /* Bail out in case resulting subreg would be incorrect. */
8183 : 978060 : if (maybe_lt (final_offset, 0)
8184 : 978055 : || maybe_ge (poly_uint64 (final_offset), innermostsize)
8185 : 978059 : || !multiple_p (final_offset, outersize))
8186 : 5 : return NULL_RTX;
8187 : : }
8188 : : else
8189 : : {
8190 : 91452 : poly_int64 required_offset = subreg_memory_offset (outermode,
8191 : : innermostmode, 0);
8192 : 91452 : if (maybe_ne (final_offset, required_offset))
8193 : 0 : return NULL_RTX;
8194 : : /* Paradoxical subregs always have byte offset 0. */
8195 : 91452 : final_offset = 0;
8196 : : }
8197 : :
8198 : : /* Recurse for further possible simplifications. */
8199 : 580477 : newx = simplify_subreg (outermode, SUBREG_REG (op), innermostmode,
8200 : 580477 : final_offset);
8201 : 580477 : if (newx)
8202 : : return newx;
8203 : 580093 : if (validate_subreg (outermode, innermostmode,
8204 : 580093 : SUBREG_REG (op), final_offset))
8205 : : {
8206 : 527121 : newx = gen_rtx_SUBREG (outermode, SUBREG_REG (op), final_offset);
8207 : 527121 : if (SUBREG_PROMOTED_VAR_P (op)
8208 : 322 : && SUBREG_PROMOTED_SIGN (op) >= 0
8209 : 322 : && GET_MODE_CLASS (outermode) == MODE_INT
8210 : 317 : && known_ge (outersize, innersize)
8211 : 316 : && known_le (outersize, innermostsize)
8212 : 527137 : && subreg_lowpart_p (newx))
8213 : : {
8214 : 16 : SUBREG_PROMOTED_VAR_P (newx) = 1;
8215 : 16 : SUBREG_PROMOTED_SET (newx, SUBREG_PROMOTED_GET (op));
8216 : : }
8217 : 527121 : return newx;
8218 : : }
8219 : : return NULL_RTX;
8220 : : }
8221 : :
8222 : : /* SUBREG of a hard register => just change the register number
8223 : : and/or mode. If the hard register is not valid in that mode,
8224 : : suppress this simplification. If the hard register is the stack,
8225 : : frame, or argument pointer, leave this as a SUBREG. */
8226 : :
8227 : 51942275 : if (REG_P (op) && HARD_REGISTER_P (op))
8228 : : {
8229 : 10791466 : unsigned int regno, final_regno;
8230 : :
8231 : 10791466 : regno = REGNO (op);
8232 : 10791466 : final_regno = simplify_subreg_regno (regno, innermode, byte, outermode);
8233 : 10791466 : if (HARD_REGISTER_NUM_P (final_regno))
8234 : : {
8235 : 10767632 : rtx x = gen_rtx_REG_offset (op, outermode, final_regno,
8236 : : subreg_memory_offset (outermode,
8237 : : innermode, byte));
8238 : :
8239 : : /* Propagate original regno. We don't have any way to specify
8240 : : the offset inside original regno, so do so only for lowpart.
8241 : : The information is used only by alias analysis that cannot
8242 : : grog partial register anyway. */
8243 : :
8244 : 10767632 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte))
8245 : 8069756 : ORIGINAL_REGNO (x) = ORIGINAL_REGNO (op);
8246 : 10767632 : return x;
8247 : : }
8248 : : }
8249 : :
8250 : : /* If we have a SUBREG of a register that we are replacing and we are
8251 : : replacing it with a MEM, make a new MEM and try replacing the
8252 : : SUBREG with it. Don't do this if the MEM has a mode-dependent address
8253 : : or if we would be widening it. */
8254 : :
8255 : 41174643 : if (MEM_P (op)
8256 : 1712032 : && ! mode_dependent_address_p (XEXP (op, 0), MEM_ADDR_SPACE (op))
8257 : : /* Allow splitting of volatile memory references in case we don't
8258 : : have instruction to move the whole thing. */
8259 : 1712029 : && (! MEM_VOLATILE_P (op)
8260 : 44157 : || ! have_insn_for (SET, innermode))
8261 : : && !(STRICT_ALIGNMENT && MEM_ALIGN (op) < GET_MODE_ALIGNMENT (outermode))
8262 : 42842515 : && known_le (outersize, innersize))
8263 : 821668 : return adjust_address_nv (op, outermode, byte);
8264 : :
8265 : : /* Handle complex or vector values represented as CONCAT or VEC_CONCAT
8266 : : of two parts. */
8267 : 40352975 : if (GET_CODE (op) == CONCAT
8268 : 40352975 : || GET_CODE (op) == VEC_CONCAT)
8269 : : {
8270 : 190474 : poly_uint64 final_offset;
8271 : 190474 : rtx part, res;
8272 : :
8273 : 190474 : machine_mode part_mode = GET_MODE (XEXP (op, 0));
8274 : 190474 : if (part_mode == VOIDmode)
8275 : 11 : part_mode = GET_MODE_INNER (GET_MODE (op));
8276 : 380948 : poly_uint64 part_size = GET_MODE_SIZE (part_mode);
8277 : 190474 : if (known_lt (byte, part_size))
8278 : : {
8279 : 188746 : part = XEXP (op, 0);
8280 : 188746 : final_offset = byte;
8281 : : }
8282 : 1728 : else if (known_ge (byte, part_size))
8283 : : {
8284 : 1728 : part = XEXP (op, 1);
8285 : 1728 : final_offset = byte - part_size;
8286 : : }
8287 : : else
8288 : : return NULL_RTX;
8289 : :
8290 : 190474 : if (maybe_gt (final_offset + outersize, part_size))
8291 : : return NULL_RTX;
8292 : :
8293 : 129613 : part_mode = GET_MODE (part);
8294 : 129613 : if (part_mode == VOIDmode)
8295 : 0 : part_mode = GET_MODE_INNER (GET_MODE (op));
8296 : 129613 : res = simplify_subreg (outermode, part, part_mode, final_offset);
8297 : 129613 : if (res)
8298 : : return res;
8299 : 231 : if (validate_subreg (outermode, part_mode, part, final_offset))
8300 : 231 : return gen_rtx_SUBREG (outermode, part, final_offset);
8301 : : return NULL_RTX;
8302 : : }
8303 : :
8304 : : /* Simplify
8305 : : (subreg (vec_merge (X)
8306 : : (vector)
8307 : : (const_int ((1 << N) | M)))
8308 : : (N * sizeof (outermode)))
8309 : : to
8310 : : (subreg (X) (N * sizeof (outermode)))
8311 : : */
8312 : 40162501 : unsigned int idx;
8313 : 80325002 : if (constant_multiple_p (byte, GET_MODE_SIZE (outermode), &idx)
8314 : 40162501 : && idx < HOST_BITS_PER_WIDE_INT
8315 : 40162501 : && GET_CODE (op) == VEC_MERGE
8316 : 599964 : && GET_MODE_INNER (innermode) == outermode
8317 : 4705 : && CONST_INT_P (XEXP (op, 2))
8318 : 40166733 : && (UINTVAL (XEXP (op, 2)) & (HOST_WIDE_INT_1U << idx)) != 0)
8319 : 4223 : return simplify_gen_subreg (outermode, XEXP (op, 0), innermode, byte);
8320 : :
8321 : : /* A SUBREG resulting from a zero extension may fold to zero if
8322 : : it extracts higher bits that the ZERO_EXTEND's source bits. */
8323 : 40158278 : if (GET_CODE (op) == ZERO_EXTEND && SCALAR_INT_MODE_P (innermode))
8324 : : {
8325 : 221513 : poly_uint64 bitpos = subreg_lsb_1 (outermode, innermode, byte);
8326 : 221513 : if (known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (XEXP (op, 0)))))
8327 : 55787 : return CONST0_RTX (outermode);
8328 : : }
8329 : :
8330 : : /* Optimize SUBREGS of scalar integral ASHIFT by a valid constant. */
8331 : 40102491 : if (GET_CODE (op) == ASHIFT
8332 : 740917 : && SCALAR_INT_MODE_P (innermode)
8333 : 713984 : && CONST_INT_P (XEXP (op, 1))
8334 : 639219 : && INTVAL (XEXP (op, 1)) > 0
8335 : 41482577 : && known_gt (GET_MODE_BITSIZE (innermode), INTVAL (XEXP (op, 1))))
8336 : : {
8337 : 639169 : HOST_WIDE_INT val = INTVAL (XEXP (op, 1));
8338 : : /* A lowpart SUBREG of a ASHIFT by a constant may fold to zero. */
8339 : 639169 : if (known_eq (subreg_lowpart_offset (outermode, innermode), byte)
8340 : 1240100 : && known_le (GET_MODE_BITSIZE (outermode), val))
8341 : 208270 : return CONST0_RTX (outermode);
8342 : : /* Optimize the highpart SUBREG of a suitable ASHIFT (ZERO_EXTEND). */
8343 : 467041 : if (GET_CODE (XEXP (op, 0)) == ZERO_EXTEND
8344 : 36822 : && GET_MODE (XEXP (XEXP (op, 0), 0)) == outermode
8345 : 73434 : && known_eq (GET_MODE_BITSIZE (outermode), val)
8346 : 72284 : && known_eq (GET_MODE_BITSIZE (innermode), 2 * val)
8347 : 503863 : && known_eq (subreg_highpart_offset (outermode, innermode), byte))
8348 : 36142 : return XEXP (XEXP (op, 0), 0);
8349 : : }
8350 : :
8351 : 41381026 : auto distribute_subreg = [&](rtx op)
8352 : : {
8353 : 1486805 : return simplify_subreg (outermode, op, innermode, byte);
8354 : 39894221 : };
8355 : :
8356 : : /* Try distributing the subreg through logic operations, if that
8357 : : leads to all subexpressions being simplified. For example,
8358 : : distributing the outer subreg in:
8359 : :
8360 : : (subreg:SI (not:QI (subreg:QI (reg:SI X) <lowpart>)) 0)
8361 : :
8362 : : gives:
8363 : :
8364 : : (not:SI (reg:SI X))
8365 : :
8366 : : This should be a win if the outermode is word_mode, since logical
8367 : : operations on word_mode should (a) be no more expensive than logical
8368 : : operations on subword modes and (b) are likely to be cheaper than
8369 : : logical operations on multiword modes.
8370 : :
8371 : : Otherwise, handle the case where the subreg is non-narrowing and does
8372 : : not change the number of words. The non-narrowing condition ensures
8373 : : that we don't convert word_mode operations to subword operations. */
8374 : 39894221 : scalar_int_mode int_outermode, int_innermode;
8375 : 39894221 : if (is_a <scalar_int_mode> (outermode, &int_outermode)
8376 : 33460820 : && is_a <scalar_int_mode> (innermode, &int_innermode)
8377 : 72269530 : && (outermode == word_mode
8378 : 18071911 : || ((GET_MODE_PRECISION (int_outermode)
8379 : 18071911 : >= GET_MODE_PRECISION (int_innermode))
8380 : 4616592 : && (CEIL (GET_MODE_SIZE (int_outermode), UNITS_PER_WORD)
8381 : 4539697 : <= CEIL (GET_MODE_SIZE (int_innermode), UNITS_PER_WORD)))))
8382 : 18782157 : switch (GET_CODE (op))
8383 : : {
8384 : 33480 : case NOT:
8385 : 33480 : if (rtx op0 = distribute_subreg (XEXP (op, 0)))
8386 : 1348 : return simplify_gen_unary (GET_CODE (op), outermode, op0, outermode);
8387 : : break;
8388 : :
8389 : 480248 : case AND:
8390 : 480248 : case IOR:
8391 : 480248 : case XOR:
8392 : 480248 : if (rtx op0 = distribute_subreg (XEXP (op, 0)))
8393 : 218515 : if (rtx op1 = distribute_subreg (XEXP (op, 1)))
8394 : 214638 : return simplify_gen_binary (GET_CODE (op), outermode, op0, op1);
8395 : : break;
8396 : :
8397 : : default:
8398 : : break;
8399 : : }
8400 : :
8401 : 39678235 : if (is_a <scalar_int_mode> (outermode, &int_outermode)
8402 : 33244834 : && is_a <scalar_int_mode> (innermode, &int_innermode)
8403 : 72923069 : && known_eq (byte, subreg_lowpart_offset (int_outermode, int_innermode)))
8404 : : {
8405 : : /* Handle polynomial integers. The upper bits of a paradoxical
8406 : : subreg are undefined, so this is safe regardless of whether
8407 : : we're truncating or extending. */
8408 : 29905739 : if (CONST_POLY_INT_P (op))
8409 : : {
8410 : : poly_wide_int val
8411 : : = poly_wide_int::from (const_poly_int_value (op),
8412 : : GET_MODE_PRECISION (int_outermode),
8413 : : SIGNED);
8414 : : return immed_wide_int_const (val, int_outermode);
8415 : : }
8416 : :
8417 : 29905739 : if (GET_MODE_PRECISION (int_outermode)
8418 : 29905739 : < GET_MODE_PRECISION (int_innermode))
8419 : : {
8420 : 16969927 : rtx tem = simplify_truncation (int_outermode, op, int_innermode);
8421 : 16969927 : if (tem)
8422 : : return tem;
8423 : : }
8424 : : }
8425 : :
8426 : : /* If the outer mode is not integral, try taking a subreg with the equivalent
8427 : : integer outer mode and then bitcasting the result.
8428 : : Other simplifications rely on integer to integer subregs and we'd
8429 : : potentially miss out on optimizations otherwise. */
8430 : 77392068 : if (known_gt (GET_MODE_SIZE (innermode),
8431 : : GET_MODE_SIZE (outermode))
8432 : 19363725 : && SCALAR_INT_MODE_P (innermode)
8433 : 18326820 : && !SCALAR_INT_MODE_P (outermode)
8434 : 58230779 : && int_mode_for_size (GET_MODE_BITSIZE (outermode),
8435 : 85510 : 0).exists (&int_outermode))
8436 : : {
8437 : 85510 : rtx tem = simplify_subreg (int_outermode, op, innermode, byte);
8438 : 85510 : if (tem)
8439 : 1973 : return lowpart_subreg (outermode, tem, int_outermode);
8440 : : }
8441 : :
8442 : : /* If OP is a vector comparison and the subreg is not changing the
8443 : : number of elements or the size of the elements, change the result
8444 : : of the comparison to the new mode. */
8445 : 38694061 : if (COMPARISON_P (op)
8446 : 242086 : && VECTOR_MODE_P (outermode)
8447 : 183314 : && VECTOR_MODE_P (innermode)
8448 : 549918 : && known_eq (GET_MODE_NUNITS (outermode), GET_MODE_NUNITS (innermode))
8449 : 39047665 : && known_eq (GET_MODE_UNIT_SIZE (outermode),
8450 : : GET_MODE_UNIT_SIZE (innermode)))
8451 : 117524 : return simplify_gen_relational (GET_CODE (op), outermode, innermode,
8452 : 117524 : XEXP (op, 0), XEXP (op, 1));
8453 : :
8454 : : /* Distribute non-paradoxical subregs through logic ops in cases where
8455 : : one term disappears.
8456 : :
8457 : : (subreg:M1 (and:M2 X C1)) -> (subreg:M1 X)
8458 : : (subreg:M1 (ior:M2 X C1)) -> (subreg:M1 C1)
8459 : : (subreg:M1 (xor:M2 X C1)) -> (subreg:M1 (not:M2 X))
8460 : :
8461 : : if M2 is no smaller than M1 and (subreg:M1 C1) is all-ones.
8462 : :
8463 : : (subreg:M1 (and:M2 X C2)) -> (subreg:M1 C2)
8464 : : (subreg:M1 (ior/xor:M2 X C2)) -> (subreg:M1 X)
8465 : :
8466 : : if M2 is no smaller than M1 and (subreg:M1 C2) is zero. */
8467 : 38576537 : if (known_ge (innersize, outersize)
8468 : 25272687 : && GET_MODE_CLASS (outermode) == GET_MODE_CLASS (innermode)
8469 : 23380842 : && (GET_CODE (op) == AND || GET_CODE (op) == IOR || GET_CODE (op) == XOR)
8470 : 40095412 : && CONSTANT_P (XEXP (op, 1)))
8471 : : {
8472 : 747829 : rtx op1_subreg = distribute_subreg (XEXP (op, 1));
8473 : 747829 : if (op1_subreg == CONSTM1_RTX (outermode))
8474 : : {
8475 : 122176 : if (GET_CODE (op) == IOR)
8476 : : return op1_subreg;
8477 : 121942 : rtx op0 = XEXP (op, 0);
8478 : 121942 : if (GET_CODE (op) == XOR)
8479 : 777 : op0 = simplify_gen_unary (NOT, innermode, op0, innermode);
8480 : 121942 : return simplify_gen_subreg (outermode, op0, innermode, byte);
8481 : : }
8482 : :
8483 : 625653 : if (op1_subreg == CONST0_RTX (outermode))
8484 : 12599 : return (GET_CODE (op) == AND
8485 : 12599 : ? op1_subreg
8486 : 6733 : : distribute_subreg (XEXP (op, 0)));
8487 : : }
8488 : :
8489 : : return NULL_RTX;
8490 : : }
8491 : :
8492 : : /* Make a SUBREG operation or equivalent if it folds. */
8493 : :
8494 : : rtx
8495 : 43807059 : simplify_context::simplify_gen_subreg (machine_mode outermode, rtx op,
8496 : : machine_mode innermode,
8497 : : poly_uint64 byte)
8498 : : {
8499 : 43807059 : rtx newx;
8500 : :
8501 : 43807059 : newx = simplify_subreg (outermode, op, innermode, byte);
8502 : 43807059 : if (newx)
8503 : : return newx;
8504 : :
8505 : 20469747 : if (GET_CODE (op) == SUBREG
8506 : 20469747 : || GET_CODE (op) == CONCAT
8507 : 20440013 : || CONST_SCALAR_INT_P (op)
8508 : 20439987 : || CONST_DOUBLE_AS_FLOAT_P (op)
8509 : 20439987 : || CONST_FIXED_P (op)
8510 : 20439987 : || GET_CODE (op) == CONST_VECTOR)
8511 : : return NULL_RTX;
8512 : :
8513 : 20439977 : if (validate_subreg (outermode, innermode, op, byte))
8514 : 20407848 : return gen_rtx_SUBREG (outermode, op, byte);
8515 : :
8516 : : return NULL_RTX;
8517 : : }
8518 : :
8519 : : /* Generates a subreg to get the least significant part of EXPR (in mode
8520 : : INNER_MODE) to OUTER_MODE. */
8521 : :
8522 : : rtx
8523 : 32694691 : simplify_context::lowpart_subreg (machine_mode outer_mode, rtx expr,
8524 : : machine_mode inner_mode)
8525 : : {
8526 : 32694691 : return simplify_gen_subreg (outer_mode, expr, inner_mode,
8527 : 32694691 : subreg_lowpart_offset (outer_mode, inner_mode));
8528 : : }
8529 : :
8530 : : /* Generate RTX to select element at INDEX out of vector OP. */
8531 : :
8532 : : rtx
8533 : 667058 : simplify_context::simplify_gen_vec_select (rtx op, unsigned int index)
8534 : : {
8535 : 667058 : gcc_assert (VECTOR_MODE_P (GET_MODE (op)));
8536 : :
8537 : 667058 : scalar_mode imode = GET_MODE_INNER (GET_MODE (op));
8538 : :
8539 : 1334116 : if (known_eq (index * GET_MODE_SIZE (imode),
8540 : : subreg_lowpart_offset (imode, GET_MODE (op))))
8541 : : {
8542 : 666908 : rtx res = lowpart_subreg (imode, op, GET_MODE (op));
8543 : 666908 : if (res)
8544 : : return res;
8545 : : }
8546 : :
8547 : 215 : rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (index)));
8548 : 215 : return gen_rtx_VEC_SELECT (imode, op, tmp);
8549 : : }
8550 : :
8551 : :
8552 : : /* Simplify X, an rtx expression.
8553 : :
8554 : : Return the simplified expression or NULL if no simplifications
8555 : : were possible.
8556 : :
8557 : : This is the preferred entry point into the simplification routines;
8558 : : however, we still allow passes to call the more specific routines.
8559 : :
8560 : : Right now GCC has three (yes, three) major bodies of RTL simplification
8561 : : code that need to be unified.
8562 : :
8563 : : 1. fold_rtx in cse.cc. This code uses various CSE specific
8564 : : information to aid in RTL simplification.
8565 : :
8566 : : 2. simplify_rtx in combine.cc. Similar to fold_rtx, except that
8567 : : it uses combine specific information to aid in RTL
8568 : : simplification.
8569 : :
8570 : : 3. The routines in this file.
8571 : :
8572 : :
8573 : : Long term we want to only have one body of simplification code; to
8574 : : get to that state I recommend the following steps:
8575 : :
8576 : : 1. Pour over fold_rtx & simplify_rtx and move any simplifications
8577 : : which are not pass dependent state into these routines.
8578 : :
8579 : : 2. As code is moved by #1, change fold_rtx & simplify_rtx to
8580 : : use this routine whenever possible.
8581 : :
8582 : : 3. Allow for pass dependent state to be provided to these
8583 : : routines and add simplifications based on the pass dependent
8584 : : state. Remove code from cse.cc & combine.cc that becomes
8585 : : redundant/dead.
8586 : :
8587 : : It will take time, but ultimately the compiler will be easier to
8588 : : maintain and improve. It's totally silly that when we add a
8589 : : simplification that it needs to be added to 4 places (3 for RTL
8590 : : simplification and 1 for tree simplification. */
8591 : :
8592 : : rtx
8593 : 46726875 : simplify_rtx (const_rtx x)
8594 : : {
8595 : 46726875 : const enum rtx_code code = GET_CODE (x);
8596 : 46726875 : const machine_mode mode = GET_MODE (x);
8597 : :
8598 : 46726875 : switch (GET_RTX_CLASS (code))
8599 : : {
8600 : 898740 : case RTX_UNARY:
8601 : 1797480 : return simplify_unary_operation (code, mode,
8602 : 898740 : XEXP (x, 0), GET_MODE (XEXP (x, 0)));
8603 : 26637508 : case RTX_COMM_ARITH:
8604 : 26637508 : if (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8605 : 523172 : return simplify_gen_binary (code, mode, XEXP (x, 1), XEXP (x, 0));
8606 : :
8607 : : /* Fall through. */
8608 : :
8609 : 32577641 : case RTX_BIN_ARITH:
8610 : 32577641 : return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
8611 : :
8612 : 62791 : case RTX_TERNARY:
8613 : 62791 : case RTX_BITFIELD_OPS:
8614 : 62791 : return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
8615 : 62791 : XEXP (x, 0), XEXP (x, 1),
8616 : 62791 : XEXP (x, 2));
8617 : :
8618 : 172603 : case RTX_COMPARE:
8619 : 172603 : case RTX_COMM_COMPARE:
8620 : 172603 : return simplify_relational_operation (code, mode,
8621 : 172603 : ((GET_MODE (XEXP (x, 0))
8622 : : != VOIDmode)
8623 : : ? GET_MODE (XEXP (x, 0))
8624 : 470 : : GET_MODE (XEXP (x, 1))),
8625 : 172603 : XEXP (x, 0),
8626 : 345206 : XEXP (x, 1));
8627 : :
8628 : 227953 : case RTX_EXTRA:
8629 : 227953 : if (code == SUBREG)
8630 : 2434 : return simplify_subreg (mode, SUBREG_REG (x),
8631 : 2434 : GET_MODE (SUBREG_REG (x)),
8632 : 2434 : SUBREG_BYTE (x));
8633 : : break;
8634 : :
8635 : 6559992 : case RTX_OBJ:
8636 : 6559992 : if (code == LO_SUM)
8637 : : {
8638 : : /* Convert (lo_sum (high FOO) FOO) to FOO. */
8639 : 0 : if (GET_CODE (XEXP (x, 0)) == HIGH
8640 : 0 : && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
8641 : 0 : return XEXP (x, 1);
8642 : : }
8643 : : break;
8644 : :
8645 : : default:
8646 : : break;
8647 : : }
8648 : : return NULL;
8649 : : }
8650 : :
8651 : : #if CHECKING_P
8652 : :
8653 : : namespace selftest {
8654 : :
8655 : : /* Make a unique pseudo REG of mode MODE for use by selftests. */
8656 : :
8657 : : static rtx
8658 : 2672 : make_test_reg (machine_mode mode)
8659 : : {
8660 : 2672 : static int test_reg_num = LAST_VIRTUAL_REGISTER + 1;
8661 : :
8662 : 2672 : return gen_rtx_REG (mode, test_reg_num++);
8663 : : }
8664 : :
8665 : : static void
8666 : 40 : test_scalar_int_ops (machine_mode mode)
8667 : : {
8668 : 40 : rtx op0 = make_test_reg (mode);
8669 : 40 : rtx op1 = make_test_reg (mode);
8670 : 40 : rtx six = GEN_INT (6);
8671 : :
8672 : 40 : rtx neg_op0 = simplify_gen_unary (NEG, mode, op0, mode);
8673 : 40 : rtx not_op0 = simplify_gen_unary (NOT, mode, op0, mode);
8674 : 40 : rtx bswap_op0 = simplify_gen_unary (BSWAP, mode, op0, mode);
8675 : :
8676 : 40 : rtx and_op0_op1 = simplify_gen_binary (AND, mode, op0, op1);
8677 : 40 : rtx ior_op0_op1 = simplify_gen_binary (IOR, mode, op0, op1);
8678 : 40 : rtx xor_op0_op1 = simplify_gen_binary (XOR, mode, op0, op1);
8679 : :
8680 : 40 : rtx and_op0_6 = simplify_gen_binary (AND, mode, op0, six);
8681 : 40 : rtx and_op1_6 = simplify_gen_binary (AND, mode, op1, six);
8682 : :
8683 : : /* Test some binary identities. */
8684 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, op0, const0_rtx));
8685 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (PLUS, mode, const0_rtx, op0));
8686 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MINUS, mode, op0, const0_rtx));
8687 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, op0, const1_rtx));
8688 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (MULT, mode, const1_rtx, op0));
8689 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (DIV, mode, op0, const1_rtx));
8690 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, constm1_rtx));
8691 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, constm1_rtx, op0));
8692 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, const0_rtx));
8693 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, const0_rtx, op0));
8694 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, op0, const0_rtx));
8695 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (XOR, mode, const0_rtx, op0));
8696 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFT, mode, op0, const0_rtx));
8697 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATE, mode, op0, const0_rtx));
8698 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ASHIFTRT, mode, op0, const0_rtx));
8699 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (LSHIFTRT, mode, op0, const0_rtx));
8700 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (ROTATERT, mode, op0, const0_rtx));
8701 : :
8702 : : /* Test some self-inverse operations. */
8703 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NEG, mode, neg_op0, mode));
8704 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (NOT, mode, not_op0, mode));
8705 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (BSWAP, mode, bswap_op0, mode));
8706 : :
8707 : : /* Test some reflexive operations. */
8708 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (AND, mode, op0, op0));
8709 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (IOR, mode, op0, op0));
8710 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMIN, mode, op0, op0));
8711 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (SMAX, mode, op0, op0));
8712 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMIN, mode, op0, op0));
8713 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_binary (UMAX, mode, op0, op0));
8714 : :
8715 : 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (MINUS, mode, op0, op0));
8716 : 40 : ASSERT_RTX_EQ (const0_rtx, simplify_gen_binary (XOR, mode, op0, op0));
8717 : :
8718 : : /* Test simplify_distributive_operation. */
8719 : 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, xor_op0_op1, six),
8720 : : simplify_gen_binary (XOR, mode, and_op0_6, and_op1_6));
8721 : 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, ior_op0_op1, six),
8722 : : simplify_gen_binary (IOR, mode, and_op0_6, and_op1_6));
8723 : 40 : ASSERT_RTX_EQ (simplify_gen_binary (AND, mode, and_op0_op1, six),
8724 : : simplify_gen_binary (AND, mode, and_op0_6, and_op1_6));
8725 : :
8726 : : /* Test useless extensions are eliminated. */
8727 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (TRUNCATE, mode, op0, mode));
8728 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (ZERO_EXTEND, mode, op0, mode));
8729 : 40 : ASSERT_RTX_EQ (op0, simplify_gen_unary (SIGN_EXTEND, mode, op0, mode));
8730 : 40 : ASSERT_RTX_EQ (op0, lowpart_subreg (mode, op0, mode));
8731 : 40 : }
8732 : :
8733 : : /* Verify some simplifications of integer extension/truncation.
8734 : : Machine mode BMODE is the guaranteed wider than SMODE. */
8735 : :
8736 : : static void
8737 : 24 : test_scalar_int_ext_ops (machine_mode bmode, machine_mode smode)
8738 : : {
8739 : 24 : rtx sreg = make_test_reg (smode);
8740 : :
8741 : : /* Check truncation of extension. */
8742 : 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8743 : : simplify_gen_unary (ZERO_EXTEND, bmode,
8744 : : sreg, smode),
8745 : : bmode),
8746 : : sreg);
8747 : 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8748 : : simplify_gen_unary (SIGN_EXTEND, bmode,
8749 : : sreg, smode),
8750 : : bmode),
8751 : : sreg);
8752 : 24 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8753 : : lowpart_subreg (bmode, sreg, smode),
8754 : : bmode),
8755 : : sreg);
8756 : :
8757 : : /* Test extensions, followed by logic ops, followed by truncations. */
8758 : 24 : rtx bsubreg = lowpart_subreg (bmode, sreg, smode);
8759 : 24 : rtx smask = gen_int_mode (GET_MODE_MASK (smode), bmode);
8760 : 24 : rtx inv_smask = gen_int_mode (~GET_MODE_MASK (smode), bmode);
8761 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8762 : : simplify_gen_binary (AND, bmode,
8763 : : bsubreg, smask),
8764 : : bmode),
8765 : : sreg);
8766 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8767 : : simplify_gen_binary (AND, bmode,
8768 : : bsubreg, inv_smask),
8769 : : bmode),
8770 : : const0_rtx);
8771 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8772 : : simplify_gen_binary (IOR, bmode,
8773 : : bsubreg, smask),
8774 : : bmode),
8775 : : constm1_rtx);
8776 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8777 : : simplify_gen_binary (IOR, bmode,
8778 : : bsubreg, inv_smask),
8779 : : bmode),
8780 : : sreg);
8781 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8782 : : simplify_gen_binary (XOR, bmode,
8783 : : bsubreg, smask),
8784 : : bmode),
8785 : : lowpart_subreg (smode,
8786 : : gen_rtx_NOT (bmode, bsubreg),
8787 : : bmode));
8788 : 24 : ASSERT_RTX_EQ (lowpart_subreg (smode,
8789 : : simplify_gen_binary (XOR, bmode,
8790 : : bsubreg, inv_smask),
8791 : : bmode),
8792 : : sreg);
8793 : :
8794 : 24 : if (known_le (GET_MODE_PRECISION (bmode), BITS_PER_WORD))
8795 : : {
8796 : 24 : rtx breg1 = make_test_reg (bmode);
8797 : 24 : rtx breg2 = make_test_reg (bmode);
8798 : 24 : rtx ssubreg1 = lowpart_subreg (smode, breg1, bmode);
8799 : 24 : rtx ssubreg2 = lowpart_subreg (smode, breg2, bmode);
8800 : 24 : rtx not_1 = simplify_gen_unary (NOT, smode, ssubreg1, smode);
8801 : 24 : rtx and_12 = simplify_gen_binary (AND, smode, ssubreg1, ssubreg2);
8802 : 24 : rtx ior_12 = simplify_gen_binary (IOR, smode, ssubreg1, ssubreg2);
8803 : 24 : rtx xor_12 = simplify_gen_binary (XOR, smode, ssubreg1, ssubreg2);
8804 : 24 : rtx and_n12 = simplify_gen_binary (AND, smode, not_1, ssubreg2);
8805 : 24 : rtx ior_n12 = simplify_gen_binary (IOR, smode, not_1, ssubreg2);
8806 : 24 : rtx xor_12_c = simplify_gen_binary (XOR, smode, xor_12, const1_rtx);
8807 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, not_1, smode),
8808 : : gen_rtx_NOT (bmode, breg1));
8809 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, and_12, smode),
8810 : : gen_rtx_AND (bmode, breg1, breg2));
8811 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, ior_12, smode),
8812 : : gen_rtx_IOR (bmode, breg1, breg2));
8813 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, xor_12, smode),
8814 : : gen_rtx_XOR (bmode, breg1, breg2));
8815 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, and_n12, smode),
8816 : : gen_rtx_AND (bmode, gen_rtx_NOT (bmode, breg1), breg2));
8817 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, ior_n12, smode),
8818 : : gen_rtx_IOR (bmode, gen_rtx_NOT (bmode, breg1), breg2));
8819 : 24 : ASSERT_RTX_EQ (lowpart_subreg (bmode, xor_12_c, smode),
8820 : : gen_rtx_XOR (bmode,
8821 : : gen_rtx_XOR (bmode, breg1, breg2),
8822 : : const1_rtx));
8823 : : }
8824 : 24 : }
8825 : :
8826 : : /* Verify more simplifications of integer extension/truncation.
8827 : : BMODE is wider than MMODE which is wider than SMODE. */
8828 : :
8829 : : static void
8830 : 16 : test_scalar_int_ext_ops2 (machine_mode bmode, machine_mode mmode,
8831 : : machine_mode smode)
8832 : : {
8833 : 16 : rtx breg = make_test_reg (bmode);
8834 : 16 : rtx mreg = make_test_reg (mmode);
8835 : 16 : rtx sreg = make_test_reg (smode);
8836 : :
8837 : : /* Check truncate of truncate. */
8838 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8839 : : simplify_gen_unary (TRUNCATE, mmode,
8840 : : breg, bmode),
8841 : : mmode),
8842 : : simplify_gen_unary (TRUNCATE, smode, breg, bmode));
8843 : :
8844 : : /* Check extension of extension. */
8845 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (ZERO_EXTEND, bmode,
8846 : : simplify_gen_unary (ZERO_EXTEND, mmode,
8847 : : sreg, smode),
8848 : : mmode),
8849 : : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
8850 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
8851 : : simplify_gen_unary (SIGN_EXTEND, mmode,
8852 : : sreg, smode),
8853 : : mmode),
8854 : : simplify_gen_unary (SIGN_EXTEND, bmode, sreg, smode));
8855 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (SIGN_EXTEND, bmode,
8856 : : simplify_gen_unary (ZERO_EXTEND, mmode,
8857 : : sreg, smode),
8858 : : mmode),
8859 : : simplify_gen_unary (ZERO_EXTEND, bmode, sreg, smode));
8860 : :
8861 : : /* Check truncation of extension. */
8862 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8863 : : simplify_gen_unary (ZERO_EXTEND, bmode,
8864 : : mreg, mmode),
8865 : : bmode),
8866 : : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8867 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8868 : : simplify_gen_unary (SIGN_EXTEND, bmode,
8869 : : mreg, mmode),
8870 : : bmode),
8871 : : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8872 : 16 : ASSERT_RTX_EQ (simplify_gen_unary (TRUNCATE, smode,
8873 : : lowpart_subreg (bmode, mreg, mmode),
8874 : : bmode),
8875 : : simplify_gen_unary (TRUNCATE, smode, mreg, mmode));
8876 : 16 : }
8877 : :
8878 : : /* Test comparisons of comparisons, with the inner comparisons being
8879 : : between values of mode MODE2 and producing results of mode MODE1,
8880 : : and with the outer comparisons producing results of mode MODE0. */
8881 : :
8882 : : static void
8883 : 4 : test_comparisons (machine_mode mode0, machine_mode mode1, machine_mode mode2)
8884 : : {
8885 : 4 : rtx reg0 = make_test_reg (mode2);
8886 : 4 : rtx reg1 = make_test_reg (mode2);
8887 : :
8888 : 4 : static const rtx_code codes[] = {
8889 : : EQ, NE, LT, LTU, LE, LEU, GE, GEU, GT, GTU
8890 : : };
8891 : 4 : constexpr auto num_codes = ARRAY_SIZE (codes);
8892 : 4 : rtx cmps[num_codes];
8893 : 4 : rtx vals[] = { constm1_rtx, const0_rtx, const1_rtx };
8894 : :
8895 : 44 : for (unsigned int i = 0; i < num_codes; ++i)
8896 : 40 : cmps[i] = gen_rtx_fmt_ee (codes[i], mode1, reg0, reg1);
8897 : :
8898 : 44 : for (auto code : codes)
8899 : 440 : for (unsigned int i0 = 0; i0 < num_codes; ++i0)
8900 : 4400 : for (unsigned int i1 = 0; i1 < num_codes; ++i1)
8901 : : {
8902 : 4000 : rtx cmp_res = simplify_relational_operation (code, mode0, mode1,
8903 : : cmps[i0], cmps[i1]);
8904 : 4000 : if (i0 >= 2 && i1 >= 2 && (i0 ^ i1) & 1)
8905 : 1280 : ASSERT_TRUE (cmp_res == NULL_RTX);
8906 : : else
8907 : : {
8908 : 2720 : ASSERT_TRUE (cmp_res != NULL_RTX
8909 : : && (CONSTANT_P (cmp_res)
8910 : : || (COMPARISON_P (cmp_res)
8911 : : && GET_MODE (cmp_res) == mode0
8912 : : && REG_P (XEXP (cmp_res, 0))
8913 : : && REG_P (XEXP (cmp_res, 1)))));
8914 : 10880 : for (rtx reg0_val : vals)
8915 : 32640 : for (rtx reg1_val : vals)
8916 : : {
8917 : 24480 : rtx val0 = simplify_const_relational_operation
8918 : 24480 : (codes[i0], mode1, reg0_val, reg1_val);
8919 : 24480 : rtx val1 = simplify_const_relational_operation
8920 : 24480 : (codes[i1], mode1, reg0_val, reg1_val);
8921 : 24480 : rtx val = simplify_const_relational_operation
8922 : 24480 : (code, mode0, val0, val1);
8923 : 24480 : rtx folded = cmp_res;
8924 : 24480 : if (COMPARISON_P (cmp_res))
8925 : 16704 : folded = simplify_const_relational_operation
8926 : 16704 : (GET_CODE (cmp_res), mode0,
8927 : 16704 : XEXP (cmp_res, 0) == reg0 ? reg0_val : reg1_val,
8928 : 16704 : XEXP (cmp_res, 1) == reg0 ? reg0_val : reg1_val);
8929 : 24480 : ASSERT_RTX_EQ (val, folded);
8930 : : }
8931 : : }
8932 : : }
8933 : 4 : }
8934 : :
8935 : :
8936 : : /* Verify some simplifications involving scalar expressions. */
8937 : :
8938 : : static void
8939 : 4 : test_scalar_ops ()
8940 : : {
8941 : 500 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
8942 : : {
8943 : 496 : machine_mode mode = (machine_mode) i;
8944 : 496 : if (SCALAR_INT_MODE_P (mode) && mode != BImode)
8945 : 40 : test_scalar_int_ops (mode);
8946 : : }
8947 : :
8948 : 4 : test_scalar_int_ext_ops (HImode, QImode);
8949 : 4 : test_scalar_int_ext_ops (SImode, QImode);
8950 : 4 : test_scalar_int_ext_ops (SImode, HImode);
8951 : 4 : test_scalar_int_ext_ops (DImode, QImode);
8952 : 4 : test_scalar_int_ext_ops (DImode, HImode);
8953 : 4 : test_scalar_int_ext_ops (DImode, SImode);
8954 : :
8955 : 4 : test_scalar_int_ext_ops2 (SImode, HImode, QImode);
8956 : 4 : test_scalar_int_ext_ops2 (DImode, HImode, QImode);
8957 : 4 : test_scalar_int_ext_ops2 (DImode, SImode, QImode);
8958 : 4 : test_scalar_int_ext_ops2 (DImode, SImode, HImode);
8959 : :
8960 : 4 : test_comparisons (QImode, HImode, SImode);
8961 : 4 : }
8962 : :
8963 : : /* Test vector simplifications involving VEC_DUPLICATE in which the
8964 : : operands and result have vector mode MODE. SCALAR_REG is a pseudo
8965 : : register that holds one element of MODE. */
8966 : :
8967 : : static void
8968 : 224 : test_vector_ops_duplicate (machine_mode mode, rtx scalar_reg)
8969 : : {
8970 : 224 : scalar_mode inner_mode = GET_MODE_INNER (mode);
8971 : 224 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
8972 : 448 : poly_uint64 nunits = GET_MODE_NUNITS (mode);
8973 : 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
8974 : : {
8975 : : /* Test some simple unary cases with VEC_DUPLICATE arguments. */
8976 : 124 : rtx not_scalar_reg = gen_rtx_NOT (inner_mode, scalar_reg);
8977 : 124 : rtx duplicate_not = gen_rtx_VEC_DUPLICATE (mode, not_scalar_reg);
8978 : 124 : ASSERT_RTX_EQ (duplicate,
8979 : : simplify_unary_operation (NOT, mode,
8980 : : duplicate_not, mode));
8981 : :
8982 : 124 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
8983 : 124 : rtx duplicate_neg = gen_rtx_VEC_DUPLICATE (mode, neg_scalar_reg);
8984 : 124 : ASSERT_RTX_EQ (duplicate,
8985 : : simplify_unary_operation (NEG, mode,
8986 : : duplicate_neg, mode));
8987 : :
8988 : : /* Test some simple binary cases with VEC_DUPLICATE arguments. */
8989 : 124 : ASSERT_RTX_EQ (duplicate,
8990 : : simplify_binary_operation (PLUS, mode, duplicate,
8991 : : CONST0_RTX (mode)));
8992 : :
8993 : 124 : ASSERT_RTX_EQ (duplicate,
8994 : : simplify_binary_operation (MINUS, mode, duplicate,
8995 : : CONST0_RTX (mode)));
8996 : :
8997 : 124 : ASSERT_RTX_PTR_EQ (CONST0_RTX (mode),
8998 : : simplify_binary_operation (MINUS, mode, duplicate,
8999 : : duplicate));
9000 : : }
9001 : :
9002 : : /* Test a scalar VEC_SELECT of a VEC_DUPLICATE. */
9003 : 224 : rtx zero_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
9004 : 224 : ASSERT_RTX_PTR_EQ (scalar_reg,
9005 : : simplify_binary_operation (VEC_SELECT, inner_mode,
9006 : : duplicate, zero_par));
9007 : :
9008 : 224 : unsigned HOST_WIDE_INT const_nunits;
9009 : 224 : if (nunits.is_constant (&const_nunits))
9010 : : {
9011 : : /* And again with the final element. */
9012 : 224 : rtx last_index = gen_int_mode (const_nunits - 1, word_mode);
9013 : 224 : rtx last_par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, last_index));
9014 : 224 : ASSERT_RTX_PTR_EQ (scalar_reg,
9015 : : simplify_binary_operation (VEC_SELECT, inner_mode,
9016 : : duplicate, last_par));
9017 : :
9018 : : /* Test a scalar subreg of a VEC_MERGE of a VEC_DUPLICATE. */
9019 : : /* Skip this test for vectors of booleans, because offset is in bytes,
9020 : : while vec_merge indices are in elements (usually bits). */
9021 : 224 : if (GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL)
9022 : : {
9023 : 224 : rtx vector_reg = make_test_reg (mode);
9024 : 3508 : for (unsigned HOST_WIDE_INT i = 0; i < const_nunits; i++)
9025 : : {
9026 : 3288 : if (i >= HOST_BITS_PER_WIDE_INT)
9027 : : break;
9028 : 3284 : rtx mask = GEN_INT ((HOST_WIDE_INT_1U << i) | (i + 1));
9029 : 3284 : rtx vm = gen_rtx_VEC_MERGE (mode, duplicate, vector_reg, mask);
9030 : 6568 : poly_uint64 offset = i * GET_MODE_SIZE (inner_mode);
9031 : :
9032 : 3284 : ASSERT_RTX_EQ (scalar_reg,
9033 : : simplify_gen_subreg (inner_mode, vm,
9034 : : mode, offset));
9035 : : }
9036 : : }
9037 : : }
9038 : :
9039 : : /* Test a scalar subreg of a VEC_DUPLICATE. */
9040 : 224 : poly_uint64 offset = subreg_lowpart_offset (inner_mode, mode);
9041 : 224 : ASSERT_RTX_EQ (scalar_reg,
9042 : : simplify_gen_subreg (inner_mode, duplicate,
9043 : : mode, offset));
9044 : :
9045 : 224 : machine_mode narrower_mode;
9046 : 224 : if (maybe_ne (nunits, 2U)
9047 : 184 : && multiple_p (nunits, 2)
9048 : 396 : && mode_for_vector (inner_mode, 2).exists (&narrower_mode)
9049 : 396 : && VECTOR_MODE_P (narrower_mode))
9050 : : {
9051 : : /* Test VEC_DUPLICATE of a vector. */
9052 : 172 : rtx_vector_builder nbuilder (narrower_mode, 2, 1);
9053 : 172 : nbuilder.quick_push (const0_rtx);
9054 : 172 : nbuilder.quick_push (const1_rtx);
9055 : 172 : rtx_vector_builder builder (mode, 2, 1);
9056 : 172 : builder.quick_push (const0_rtx);
9057 : 172 : builder.quick_push (const1_rtx);
9058 : 172 : ASSERT_RTX_EQ (builder.build (),
9059 : : simplify_unary_operation (VEC_DUPLICATE, mode,
9060 : : nbuilder.build (),
9061 : : narrower_mode));
9062 : :
9063 : : /* Test VEC_SELECT of a vector. */
9064 : 172 : rtx vec_par
9065 : 172 : = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, const1_rtx, const0_rtx));
9066 : 172 : rtx narrower_duplicate
9067 : 172 : = gen_rtx_VEC_DUPLICATE (narrower_mode, scalar_reg);
9068 : 172 : ASSERT_RTX_EQ (narrower_duplicate,
9069 : : simplify_binary_operation (VEC_SELECT, narrower_mode,
9070 : : duplicate, vec_par));
9071 : :
9072 : : /* Test a vector subreg of a VEC_DUPLICATE. */
9073 : 172 : poly_uint64 offset = subreg_lowpart_offset (narrower_mode, mode);
9074 : 172 : ASSERT_RTX_EQ (narrower_duplicate,
9075 : : simplify_gen_subreg (narrower_mode, duplicate,
9076 : : mode, offset));
9077 : 172 : }
9078 : 224 : }
9079 : :
9080 : : /* Test vector simplifications involving VEC_SERIES in which the
9081 : : operands and result have vector mode MODE. SCALAR_REG is a pseudo
9082 : : register that holds one element of MODE. */
9083 : :
9084 : : static void
9085 : 92 : test_vector_ops_series (machine_mode mode, rtx scalar_reg)
9086 : : {
9087 : : /* Test unary cases with VEC_SERIES arguments. */
9088 : 92 : scalar_mode inner_mode = GET_MODE_INNER (mode);
9089 : 92 : rtx duplicate = gen_rtx_VEC_DUPLICATE (mode, scalar_reg);
9090 : 92 : rtx neg_scalar_reg = gen_rtx_NEG (inner_mode, scalar_reg);
9091 : 92 : rtx series_0_r = gen_rtx_VEC_SERIES (mode, const0_rtx, scalar_reg);
9092 : 92 : rtx series_0_nr = gen_rtx_VEC_SERIES (mode, const0_rtx, neg_scalar_reg);
9093 : 92 : rtx series_nr_1 = gen_rtx_VEC_SERIES (mode, neg_scalar_reg, const1_rtx);
9094 : 92 : rtx series_r_m1 = gen_rtx_VEC_SERIES (mode, scalar_reg, constm1_rtx);
9095 : 92 : rtx series_r_r = gen_rtx_VEC_SERIES (mode, scalar_reg, scalar_reg);
9096 : 92 : rtx series_nr_nr = gen_rtx_VEC_SERIES (mode, neg_scalar_reg,
9097 : : neg_scalar_reg);
9098 : 92 : ASSERT_RTX_EQ (series_0_r,
9099 : : simplify_unary_operation (NEG, mode, series_0_nr, mode));
9100 : 92 : ASSERT_RTX_EQ (series_r_m1,
9101 : : simplify_unary_operation (NEG, mode, series_nr_1, mode));
9102 : 92 : ASSERT_RTX_EQ (series_r_r,
9103 : : simplify_unary_operation (NEG, mode, series_nr_nr, mode));
9104 : :
9105 : : /* Test that a VEC_SERIES with a zero step is simplified away. */
9106 : 92 : ASSERT_RTX_EQ (duplicate,
9107 : : simplify_binary_operation (VEC_SERIES, mode,
9108 : : scalar_reg, const0_rtx));
9109 : :
9110 : : /* Test PLUS and MINUS with VEC_SERIES. */
9111 : 92 : rtx series_0_1 = gen_const_vec_series (mode, const0_rtx, const1_rtx);
9112 : 92 : rtx series_0_m1 = gen_const_vec_series (mode, const0_rtx, constm1_rtx);
9113 : 92 : rtx series_r_1 = gen_rtx_VEC_SERIES (mode, scalar_reg, const1_rtx);
9114 : 92 : ASSERT_RTX_EQ (series_r_r,
9115 : : simplify_binary_operation (PLUS, mode, series_0_r,
9116 : : duplicate));
9117 : 92 : ASSERT_RTX_EQ (series_r_1,
9118 : : simplify_binary_operation (PLUS, mode, duplicate,
9119 : : series_0_1));
9120 : 92 : ASSERT_RTX_EQ (series_r_m1,
9121 : : simplify_binary_operation (PLUS, mode, duplicate,
9122 : : series_0_m1));
9123 : 92 : ASSERT_RTX_EQ (series_0_r,
9124 : : simplify_binary_operation (MINUS, mode, series_r_r,
9125 : : duplicate));
9126 : 92 : ASSERT_RTX_EQ (series_r_m1,
9127 : : simplify_binary_operation (MINUS, mode, duplicate,
9128 : : series_0_1));
9129 : 92 : ASSERT_RTX_EQ (series_r_1,
9130 : : simplify_binary_operation (MINUS, mode, duplicate,
9131 : : series_0_m1));
9132 : 92 : ASSERT_RTX_EQ (series_0_m1,
9133 : : simplify_binary_operation (VEC_SERIES, mode, const0_rtx,
9134 : : constm1_rtx));
9135 : :
9136 : : /* Test NEG on constant vector series. */
9137 : 92 : ASSERT_RTX_EQ (series_0_m1,
9138 : : simplify_unary_operation (NEG, mode, series_0_1, mode));
9139 : 92 : ASSERT_RTX_EQ (series_0_1,
9140 : : simplify_unary_operation (NEG, mode, series_0_m1, mode));
9141 : :
9142 : : /* Test PLUS and MINUS on constant vector series. */
9143 : 92 : rtx scalar2 = gen_int_mode (2, inner_mode);
9144 : 92 : rtx scalar3 = gen_int_mode (3, inner_mode);
9145 : 92 : rtx series_1_1 = gen_const_vec_series (mode, const1_rtx, const1_rtx);
9146 : 92 : rtx series_0_2 = gen_const_vec_series (mode, const0_rtx, scalar2);
9147 : 92 : rtx series_1_3 = gen_const_vec_series (mode, const1_rtx, scalar3);
9148 : 92 : ASSERT_RTX_EQ (series_1_1,
9149 : : simplify_binary_operation (PLUS, mode, series_0_1,
9150 : : CONST1_RTX (mode)));
9151 : 92 : ASSERT_RTX_EQ (series_0_m1,
9152 : : simplify_binary_operation (PLUS, mode, CONST0_RTX (mode),
9153 : : series_0_m1));
9154 : 92 : ASSERT_RTX_EQ (series_1_3,
9155 : : simplify_binary_operation (PLUS, mode, series_1_1,
9156 : : series_0_2));
9157 : 92 : ASSERT_RTX_EQ (series_0_1,
9158 : : simplify_binary_operation (MINUS, mode, series_1_1,
9159 : : CONST1_RTX (mode)));
9160 : 92 : ASSERT_RTX_EQ (series_1_1,
9161 : : simplify_binary_operation (MINUS, mode, CONST1_RTX (mode),
9162 : : series_0_m1));
9163 : 92 : ASSERT_RTX_EQ (series_1_1,
9164 : : simplify_binary_operation (MINUS, mode, series_1_3,
9165 : : series_0_2));
9166 : :
9167 : : /* Test MULT between constant vectors. */
9168 : 92 : rtx vec2 = gen_const_vec_duplicate (mode, scalar2);
9169 : 92 : rtx vec3 = gen_const_vec_duplicate (mode, scalar3);
9170 : 92 : rtx scalar9 = gen_int_mode (9, inner_mode);
9171 : 92 : rtx series_3_9 = gen_const_vec_series (mode, scalar3, scalar9);
9172 : 92 : ASSERT_RTX_EQ (series_0_2,
9173 : : simplify_binary_operation (MULT, mode, series_0_1, vec2));
9174 : 92 : ASSERT_RTX_EQ (series_3_9,
9175 : : simplify_binary_operation (MULT, mode, vec3, series_1_3));
9176 : 92 : if (!GET_MODE_NUNITS (mode).is_constant ())
9177 : : ASSERT_FALSE (simplify_binary_operation (MULT, mode, series_0_1,
9178 : : series_0_1));
9179 : :
9180 : : /* Test ASHIFT between constant vectors. */
9181 : 92 : ASSERT_RTX_EQ (series_0_2,
9182 : : simplify_binary_operation (ASHIFT, mode, series_0_1,
9183 : : CONST1_RTX (mode)));
9184 : 92 : if (!GET_MODE_NUNITS (mode).is_constant ())
9185 : : ASSERT_FALSE (simplify_binary_operation (ASHIFT, mode, CONST1_RTX (mode),
9186 : : series_0_1));
9187 : 92 : }
9188 : :
9189 : : static rtx
9190 : 3136 : simplify_merge_mask (rtx x, rtx mask, int op)
9191 : : {
9192 : 0 : return simplify_context ().simplify_merge_mask (x, mask, op);
9193 : : }
9194 : :
9195 : : /* Verify simplify_merge_mask works correctly. */
9196 : :
9197 : : static void
9198 : 224 : test_vec_merge (machine_mode mode)
9199 : : {
9200 : 224 : rtx op0 = make_test_reg (mode);
9201 : 224 : rtx op1 = make_test_reg (mode);
9202 : 224 : rtx op2 = make_test_reg (mode);
9203 : 224 : rtx op3 = make_test_reg (mode);
9204 : 224 : rtx op4 = make_test_reg (mode);
9205 : 224 : rtx op5 = make_test_reg (mode);
9206 : 224 : rtx mask1 = make_test_reg (SImode);
9207 : 224 : rtx mask2 = make_test_reg (SImode);
9208 : 224 : rtx vm1 = gen_rtx_VEC_MERGE (mode, op0, op1, mask1);
9209 : 224 : rtx vm2 = gen_rtx_VEC_MERGE (mode, op2, op3, mask1);
9210 : 224 : rtx vm3 = gen_rtx_VEC_MERGE (mode, op4, op5, mask1);
9211 : :
9212 : : /* Simple vec_merge. */
9213 : 224 : ASSERT_EQ (op0, simplify_merge_mask (vm1, mask1, 0));
9214 : 224 : ASSERT_EQ (op1, simplify_merge_mask (vm1, mask1, 1));
9215 : 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 0));
9216 : 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (vm1, mask2, 1));
9217 : :
9218 : : /* Nested vec_merge.
9219 : : It's tempting to make this simplify right down to opN, but we don't
9220 : : because all the simplify_* functions assume that the operands have
9221 : : already been simplified. */
9222 : 224 : rtx nvm = gen_rtx_VEC_MERGE (mode, vm1, vm2, mask1);
9223 : 224 : ASSERT_EQ (vm1, simplify_merge_mask (nvm, mask1, 0));
9224 : 224 : ASSERT_EQ (vm2, simplify_merge_mask (nvm, mask1, 1));
9225 : :
9226 : : /* Intermediate unary op. */
9227 : 224 : rtx unop = gen_rtx_NOT (mode, vm1);
9228 : 224 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op0),
9229 : : simplify_merge_mask (unop, mask1, 0));
9230 : 224 : ASSERT_RTX_EQ (gen_rtx_NOT (mode, op1),
9231 : : simplify_merge_mask (unop, mask1, 1));
9232 : :
9233 : : /* Intermediate binary op. */
9234 : 224 : rtx binop = gen_rtx_PLUS (mode, vm1, vm2);
9235 : 224 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op0, op2),
9236 : : simplify_merge_mask (binop, mask1, 0));
9237 : 224 : ASSERT_RTX_EQ (gen_rtx_PLUS (mode, op1, op3),
9238 : : simplify_merge_mask (binop, mask1, 1));
9239 : :
9240 : : /* Intermediate ternary op. */
9241 : 224 : rtx tenop = gen_rtx_FMA (mode, vm1, vm2, vm3);
9242 : 224 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op0, op2, op4),
9243 : : simplify_merge_mask (tenop, mask1, 0));
9244 : 224 : ASSERT_RTX_EQ (gen_rtx_FMA (mode, op1, op3, op5),
9245 : : simplify_merge_mask (tenop, mask1, 1));
9246 : :
9247 : : /* Side effects. */
9248 : 224 : rtx badop0 = gen_rtx_PRE_INC (mode, op0);
9249 : 224 : rtx badvm = gen_rtx_VEC_MERGE (mode, badop0, op1, mask1);
9250 : 224 : ASSERT_EQ (badop0, simplify_merge_mask (badvm, mask1, 0));
9251 : 224 : ASSERT_EQ (NULL_RTX, simplify_merge_mask (badvm, mask1, 1));
9252 : :
9253 : : /* Called indirectly. */
9254 : 224 : ASSERT_RTX_EQ (gen_rtx_VEC_MERGE (mode, op0, op3, mask1),
9255 : : simplify_rtx (nvm));
9256 : 224 : }
9257 : :
9258 : : /* Test that vector rotate formation works at RTL level. Try various
9259 : : combinations of (REG << C) [|,^,+] (REG >> (<bitwidth> - C)). */
9260 : :
9261 : : static void
9262 : 92 : test_vector_rotate (rtx reg)
9263 : : {
9264 : 92 : machine_mode mode = GET_MODE (reg);
9265 : 92 : unsigned bitwidth = GET_MODE_UNIT_SIZE (mode) * BITS_PER_UNIT;
9266 : 92 : rtx plus_rtx = gen_rtx_PLUS (mode, reg, reg);
9267 : 92 : rtx lshftrt_amnt = GEN_INT (bitwidth - 1);
9268 : 92 : lshftrt_amnt = gen_const_vec_duplicate (mode, lshftrt_amnt);
9269 : 92 : rtx lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
9270 : 92 : rtx rotate_rtx = gen_rtx_ROTATE (mode, reg, CONST1_RTX (mode));
9271 : : /* Test explicitly the case where ASHIFT (x, 1) is a PLUS (x, x). */
9272 : 92 : ASSERT_RTX_EQ (rotate_rtx,
9273 : : simplify_rtx (gen_rtx_IOR (mode, plus_rtx, lshiftrt_rtx)));
9274 : 92 : ASSERT_RTX_EQ (rotate_rtx,
9275 : : simplify_rtx (gen_rtx_XOR (mode, plus_rtx, lshiftrt_rtx)));
9276 : 92 : ASSERT_RTX_EQ (rotate_rtx,
9277 : : simplify_rtx (gen_rtx_PLUS (mode, plus_rtx, lshiftrt_rtx)));
9278 : :
9279 : : /* Don't go through every possible rotate amount to save execution time.
9280 : : Multiple of BITS_PER_UNIT amounts could conceivably be simplified to
9281 : : other bswap operations sometimes. Go through just the odd amounts. */
9282 : 1380 : for (unsigned i = 3; i < bitwidth - 2; i += 2)
9283 : : {
9284 : 1288 : rtx rot_amnt = gen_const_vec_duplicate (mode, GEN_INT (i));
9285 : 1288 : rtx ashift_rtx = gen_rtx_ASHIFT (mode, reg, rot_amnt);
9286 : 1288 : lshftrt_amnt = gen_const_vec_duplicate (mode, GEN_INT (bitwidth - i));
9287 : 1288 : lshiftrt_rtx = gen_rtx_LSHIFTRT (mode, reg, lshftrt_amnt);
9288 : 1288 : rotate_rtx = gen_rtx_ROTATE (mode, reg, rot_amnt);
9289 : 1288 : ASSERT_RTX_EQ (rotate_rtx,
9290 : : simplify_rtx (gen_rtx_IOR (mode, ashift_rtx, lshiftrt_rtx)));
9291 : 1288 : ASSERT_RTX_EQ (rotate_rtx,
9292 : : simplify_rtx (gen_rtx_XOR (mode, ashift_rtx, lshiftrt_rtx)));
9293 : 1288 : ASSERT_RTX_EQ (rotate_rtx,
9294 : : simplify_rtx (gen_rtx_PLUS (mode, ashift_rtx, lshiftrt_rtx)));
9295 : : }
9296 : 92 : }
9297 : :
9298 : : /* Test subregs of integer vector constant X, trying elements in
9299 : : the range [ELT_BIAS, ELT_BIAS + constant_lower_bound (NELTS)),
9300 : : where NELTS is the number of elements in X. Subregs involving
9301 : : elements [ELT_BIAS, ELT_BIAS + FIRST_VALID) are expected to fail. */
9302 : :
9303 : : static void
9304 : 276 : test_vector_subregs_modes (rtx x, poly_uint64 elt_bias = 0,
9305 : : unsigned int first_valid = 0)
9306 : : {
9307 : 276 : machine_mode inner_mode = GET_MODE (x);
9308 : 276 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9309 : :
9310 : 34500 : for (unsigned int modei = 0; modei < NUM_MACHINE_MODES; ++modei)
9311 : : {
9312 : 34224 : machine_mode outer_mode = (machine_mode) modei;
9313 : 34224 : if (!VECTOR_MODE_P (outer_mode))
9314 : 18768 : continue;
9315 : :
9316 : 15456 : unsigned int outer_nunits;
9317 : 15456 : if (GET_MODE_INNER (outer_mode) == int_mode
9318 : 1932 : && GET_MODE_NUNITS (outer_mode).is_constant (&outer_nunits)
9319 : 20412 : && multiple_p (GET_MODE_NUNITS (inner_mode), outer_nunits))
9320 : : {
9321 : : /* Test subregs in which the outer mode is a smaller,
9322 : : constant-sized vector of the same element type. */
9323 : 1092 : unsigned int limit
9324 : 1092 : = constant_lower_bound (GET_MODE_NUNITS (inner_mode));
9325 : 8028 : for (unsigned int elt = 0; elt < limit; elt += outer_nunits)
9326 : : {
9327 : 6936 : rtx expected = NULL_RTX;
9328 : 6936 : if (elt >= first_valid)
9329 : : {
9330 : 6936 : rtx_vector_builder builder (outer_mode, outer_nunits, 1);
9331 : 39768 : for (unsigned int i = 0; i < outer_nunits; ++i)
9332 : 32832 : builder.quick_push (CONST_VECTOR_ELT (x, elt + i));
9333 : 6936 : expected = builder.build ();
9334 : 6936 : }
9335 : 13872 : poly_uint64 byte = (elt_bias + elt) * GET_MODE_SIZE (int_mode);
9336 : 6936 : ASSERT_RTX_EQ (expected,
9337 : : simplify_subreg (outer_mode, x,
9338 : : inner_mode, byte));
9339 : : }
9340 : : }
9341 : 28728 : else if (known_eq (GET_MODE_SIZE (outer_mode),
9342 : : GET_MODE_SIZE (inner_mode))
9343 : 2040 : && known_eq (elt_bias, 0U)
9344 : 2040 : && (GET_MODE_CLASS (outer_mode) != MODE_VECTOR_BOOL
9345 : 0 : || known_eq (GET_MODE_BITSIZE (outer_mode),
9346 : : GET_MODE_NUNITS (outer_mode)))
9347 : 2040 : && (!FLOAT_MODE_P (outer_mode)
9348 : 15876 : || (FLOAT_MODE_FORMAT (outer_mode)->ieee_bits
9349 : 1104 : == GET_MODE_UNIT_PRECISION (outer_mode)))
9350 : 14364 : && (GET_MODE_SIZE (inner_mode).is_constant ()
9351 : : || !CONST_VECTOR_STEPPED_P (x)))
9352 : : {
9353 : : /* Try converting to OUTER_MODE and back. */
9354 : 1800 : rtx outer_x = simplify_subreg (outer_mode, x, inner_mode, 0);
9355 : 1800 : ASSERT_TRUE (outer_x != NULL_RTX);
9356 : 1800 : ASSERT_RTX_EQ (x, simplify_subreg (inner_mode, outer_x,
9357 : : outer_mode, 0));
9358 : : }
9359 : : }
9360 : :
9361 : 276 : if (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
9362 : : {
9363 : : /* Test each byte in the element range. */
9364 : 276 : unsigned int limit
9365 : 276 : = constant_lower_bound (GET_MODE_SIZE (inner_mode));
9366 : 14604 : for (unsigned int i = 0; i < limit; ++i)
9367 : : {
9368 : 14328 : unsigned int elt = i / GET_MODE_SIZE (int_mode);
9369 : 14328 : rtx expected = NULL_RTX;
9370 : 14328 : if (elt >= first_valid)
9371 : : {
9372 : 14328 : unsigned int byte_shift = i % GET_MODE_SIZE (int_mode);
9373 : 14328 : if (BYTES_BIG_ENDIAN)
9374 : : byte_shift = GET_MODE_SIZE (int_mode) - byte_shift - 1;
9375 : 14328 : rtx_mode_t vec_elt (CONST_VECTOR_ELT (x, elt), int_mode);
9376 : 14328 : wide_int shifted_elt
9377 : 14328 : = wi::lrshift (vec_elt, byte_shift * BITS_PER_UNIT);
9378 : 14328 : expected = immed_wide_int_const (shifted_elt, QImode);
9379 : 14328 : }
9380 : 28656 : poly_uint64 byte = elt_bias * GET_MODE_SIZE (int_mode) + i;
9381 : 14328 : ASSERT_RTX_EQ (expected,
9382 : : simplify_subreg (QImode, x, inner_mode, byte));
9383 : : }
9384 : : }
9385 : 276 : }
9386 : :
9387 : : /* Test constant subregs of integer vector mode INNER_MODE, using 1
9388 : : element per pattern. */
9389 : :
9390 : : static void
9391 : 92 : test_vector_subregs_repeating (machine_mode inner_mode)
9392 : : {
9393 : 184 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
9394 : 92 : unsigned int min_nunits = constant_lower_bound (nunits);
9395 : 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9396 : 92 : unsigned int count = gcd (min_nunits, 8);
9397 : :
9398 : 92 : rtx_vector_builder builder (inner_mode, count, 1);
9399 : 684 : for (unsigned int i = 0; i < count; ++i)
9400 : 592 : builder.quick_push (gen_int_mode (8 - i, int_mode));
9401 : 92 : rtx x = builder.build ();
9402 : :
9403 : 92 : test_vector_subregs_modes (x);
9404 : 92 : if (!nunits.is_constant ())
9405 : : test_vector_subregs_modes (x, nunits - min_nunits);
9406 : 92 : }
9407 : :
9408 : : /* Test constant subregs of integer vector mode INNER_MODE, using 2
9409 : : elements per pattern. */
9410 : :
9411 : : static void
9412 : 92 : test_vector_subregs_fore_back (machine_mode inner_mode)
9413 : : {
9414 : 184 : poly_uint64 nunits = GET_MODE_NUNITS (inner_mode);
9415 : 92 : unsigned int min_nunits = constant_lower_bound (nunits);
9416 : 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9417 : 92 : unsigned int count = gcd (min_nunits, 4);
9418 : :
9419 : 92 : rtx_vector_builder builder (inner_mode, count, 2);
9420 : 444 : for (unsigned int i = 0; i < count; ++i)
9421 : 352 : builder.quick_push (gen_int_mode (i, int_mode));
9422 : 444 : for (unsigned int i = 0; i < count; ++i)
9423 : 352 : builder.quick_push (gen_int_mode (-1 - (int) i, int_mode));
9424 : 92 : rtx x = builder.build ();
9425 : :
9426 : 92 : test_vector_subregs_modes (x);
9427 : 92 : if (!nunits.is_constant ())
9428 : : test_vector_subregs_modes (x, nunits - min_nunits, count);
9429 : 92 : }
9430 : :
9431 : : /* Test constant subregs of integer vector mode INNER_MODE, using 3
9432 : : elements per pattern. */
9433 : :
9434 : : static void
9435 : 92 : test_vector_subregs_stepped (machine_mode inner_mode)
9436 : : {
9437 : : /* Build { 0, 1, 2, 3, ... }. */
9438 : 92 : scalar_mode int_mode = GET_MODE_INNER (inner_mode);
9439 : 92 : rtx_vector_builder builder (inner_mode, 1, 3);
9440 : 368 : for (unsigned int i = 0; i < 3; ++i)
9441 : 276 : builder.quick_push (gen_int_mode (i, int_mode));
9442 : 92 : rtx x = builder.build ();
9443 : :
9444 : 92 : test_vector_subregs_modes (x);
9445 : 92 : }
9446 : :
9447 : : /* Test constant subregs of integer vector mode INNER_MODE. */
9448 : :
9449 : : static void
9450 : 92 : test_vector_subregs (machine_mode inner_mode)
9451 : : {
9452 : 92 : test_vector_subregs_repeating (inner_mode);
9453 : 92 : test_vector_subregs_fore_back (inner_mode);
9454 : 92 : test_vector_subregs_stepped (inner_mode);
9455 : 92 : }
9456 : :
9457 : : /* Verify some simplifications involving vectors. */
9458 : :
9459 : : static void
9460 : 4 : test_vector_ops ()
9461 : : {
9462 : 500 : for (unsigned int i = 0; i < NUM_MACHINE_MODES; ++i)
9463 : : {
9464 : 496 : machine_mode mode = (machine_mode) i;
9465 : 496 : if (VECTOR_MODE_P (mode))
9466 : : {
9467 : 448 : rtx scalar_reg = make_test_reg (GET_MODE_INNER (mode));
9468 : 224 : test_vector_ops_duplicate (mode, scalar_reg);
9469 : 224 : rtx vector_reg = make_test_reg (mode);
9470 : 224 : if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9471 : 348 : && maybe_gt (GET_MODE_NUNITS (mode), 2))
9472 : : {
9473 : 92 : test_vector_ops_series (mode, scalar_reg);
9474 : 92 : test_vector_subregs (mode);
9475 : 92 : test_vector_rotate (vector_reg);
9476 : : }
9477 : 224 : test_vec_merge (mode);
9478 : : }
9479 : : }
9480 : 4 : }
9481 : :
9482 : : template<unsigned int N>
9483 : : struct simplify_const_poly_int_tests
9484 : : {
9485 : : static void run ();
9486 : : };
9487 : :
9488 : : template<>
9489 : : struct simplify_const_poly_int_tests<1>
9490 : : {
9491 : : static void run () {}
9492 : : };
9493 : :
9494 : : /* Test various CONST_POLY_INT properties. */
9495 : :
9496 : : template<unsigned int N>
9497 : : void
9498 : : simplify_const_poly_int_tests<N>::run ()
9499 : : {
9500 : : using poly_int64 = poly_int<N, HOST_WIDE_INT>;
9501 : : rtx x1 = gen_int_mode (poly_int64 (1, 1), QImode);
9502 : : rtx x2 = gen_int_mode (poly_int64 (-80, 127), QImode);
9503 : : rtx x3 = gen_int_mode (poly_int64 (-79, -128), QImode);
9504 : : rtx x4 = gen_int_mode (poly_int64 (5, 4), QImode);
9505 : : rtx x5 = gen_int_mode (poly_int64 (30, 24), QImode);
9506 : : rtx x6 = gen_int_mode (poly_int64 (20, 16), QImode);
9507 : : rtx x7 = gen_int_mode (poly_int64 (7, 4), QImode);
9508 : : rtx x8 = gen_int_mode (poly_int64 (30, 24), HImode);
9509 : : rtx x9 = gen_int_mode (poly_int64 (-30, -24), HImode);
9510 : : rtx x10 = gen_int_mode (poly_int64 (-31, -24), HImode);
9511 : : rtx two = GEN_INT (2);
9512 : : rtx six = GEN_INT (6);
9513 : : poly_uint64 offset = subreg_lowpart_offset (QImode, HImode);
9514 : :
9515 : : /* These tests only try limited operation combinations. Fuller arithmetic
9516 : : testing is done directly on poly_ints. */
9517 : : ASSERT_EQ (simplify_unary_operation (NEG, HImode, x8, HImode), x9);
9518 : : ASSERT_EQ (simplify_unary_operation (NOT, HImode, x8, HImode), x10);
9519 : : ASSERT_EQ (simplify_unary_operation (TRUNCATE, QImode, x8, HImode), x5);
9520 : : ASSERT_EQ (simplify_binary_operation (PLUS, QImode, x1, x2), x3);
9521 : : ASSERT_EQ (simplify_binary_operation (MINUS, QImode, x3, x1), x2);
9522 : : ASSERT_EQ (simplify_binary_operation (MULT, QImode, x4, six), x5);
9523 : : ASSERT_EQ (simplify_binary_operation (MULT, QImode, six, x4), x5);
9524 : : ASSERT_EQ (simplify_binary_operation (ASHIFT, QImode, x4, two), x6);
9525 : : ASSERT_EQ (simplify_binary_operation (IOR, QImode, x4, two), x7);
9526 : : ASSERT_EQ (simplify_subreg (HImode, x5, QImode, 0), x8);
9527 : : ASSERT_EQ (simplify_subreg (QImode, x8, HImode, offset), x5);
9528 : : }
9529 : :
9530 : : /* Run all of the selftests within this file. */
9531 : :
9532 : : void
9533 : 4 : simplify_rtx_cc_tests ()
9534 : : {
9535 : 4 : test_scalar_ops ();
9536 : 4 : test_vector_ops ();
9537 : 4 : simplify_const_poly_int_tests<NUM_POLY_INT_COEFFS>::run ();
9538 : 4 : }
9539 : :
9540 : : } // namespace selftest
9541 : :
9542 : : #endif /* CHECKING_P */
|