]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/match.pd
p5600.md (define_automaton, [...]): Replace p5600_agen_pipe and p5600_alu_pipe with...
[thirdparty/gcc.git] / gcc / match.pd
CommitLineData
3d2cf79f
RB
1/* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
4
5 Copyright (C) 2014 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8
9This file is part of GCC.
10
11GCC is free software; you can redistribute it and/or modify it under
12the terms of the GNU General Public License as published by the Free
13Software Foundation; either version 3, or (at your option) any later
14version.
15
16GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17WARRANTY; without even the implied warranty of MERCHANTABILITY or
18FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19for more details.
20
21You should have received a copy of the GNU General Public License
22along with GCC; see the file COPYING3. If not see
23<http://www.gnu.org/licenses/>. */
24
25
26/* Generic tree predicates we inherit. */
27(define_predicates
cc7b5acf 28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
09240451 29 integer_each_onep integer_truep
cc7b5acf 30 real_zerop real_onep real_minus_onep
f3582e54
RB
31 CONSTANT_CLASS_P
32 tree_expr_nonnegative_p)
e0ee10ed 33
f84e7fd6
RB
34/* Operator lists. */
35(define_operator_list tcc_comparison
36 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
37(define_operator_list inverted_tcc_comparison
38 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
39(define_operator_list inverted_tcc_comparison_with_nans
40 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
41
e0ee10ed
RB
42
43/* Simplifications of operations with one constant operand and
36a60e48 44 simplifications to constants or single values. */
e0ee10ed
RB
45
46(for op (plus pointer_plus minus bit_ior bit_xor)
47 (simplify
48 (op @0 integer_zerop)
49 (non_lvalue @0)))
50
a499aac5
RB
51/* 0 +p index -> (type)index */
52(simplify
53 (pointer_plus integer_zerop @1)
54 (non_lvalue (convert @1)))
55
a7f24614
RB
56/* See if ARG1 is zero and X + ARG1 reduces to X.
57 Likewise if the operands are reversed. */
58(simplify
59 (plus:c @0 real_zerop@1)
60 (if (fold_real_zero_addition_p (type, @1, 0))
61 (non_lvalue @0)))
62
63/* See if ARG1 is zero and X - ARG1 reduces to X. */
64(simplify
65 (minus @0 real_zerop@1)
66 (if (fold_real_zero_addition_p (type, @1, 1))
67 (non_lvalue @0)))
68
e0ee10ed
RB
69/* Simplify x - x.
70 This is unsafe for certain floats even in non-IEEE formats.
71 In IEEE, it is unsafe because it does wrong for NaNs.
72 Also note that operand_equal_p is always false if an operand
73 is volatile. */
74(simplify
a7f24614 75 (minus @0 @0)
09240451 76 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (element_mode (type)))
a7f24614 77 { build_zero_cst (type); }))
e0ee10ed
RB
78
79(simplify
a7f24614
RB
80 (mult @0 integer_zerop@1)
81 @1)
82
83/* Maybe fold x * 0 to 0. The expressions aren't the same
84 when x is NaN, since x * 0 is also NaN. Nor are they the
85 same in modes with signed zeros, since multiplying a
86 negative value by 0 gives -0, not +0. */
87(simplify
88 (mult @0 real_zerop@1)
09240451
MG
89 (if (!HONOR_NANS (element_mode (type))
90 && !HONOR_SIGNED_ZEROS (element_mode (type)))
a7f24614
RB
91 @1))
92
93/* In IEEE floating point, x*1 is not equivalent to x for snans.
94 Likewise for complex arithmetic with signed zeros. */
95(simplify
96 (mult @0 real_onep)
09240451
MG
97 (if (!HONOR_SNANS (element_mode (type))
98 && (!HONOR_SIGNED_ZEROS (element_mode (type))
a7f24614
RB
99 || !COMPLEX_FLOAT_TYPE_P (type)))
100 (non_lvalue @0)))
101
102/* Transform x * -1.0 into -x. */
103(simplify
104 (mult @0 real_minus_onep)
09240451
MG
105 (if (!HONOR_SNANS (element_mode (type))
106 && (!HONOR_SIGNED_ZEROS (element_mode (type))
a7f24614
RB
107 || !COMPLEX_FLOAT_TYPE_P (type)))
108 (negate @0)))
e0ee10ed
RB
109
110/* Make sure to preserve divisions by zero. This is the reason why
111 we don't simplify x / x to 1 or 0 / x to 0. */
112(for op (mult trunc_div ceil_div floor_div round_div exact_div)
113 (simplify
114 (op @0 integer_onep)
115 (non_lvalue @0)))
116
a7f24614
RB
117/* X / -1 is -X. */
118(for div (trunc_div ceil_div floor_div round_div exact_div)
119 (simplify
09240451
MG
120 (div @0 integer_minus_onep@1)
121 (if (!TYPE_UNSIGNED (type))
a7f24614
RB
122 (negate @0))))
123
124/* For unsigned integral types, FLOOR_DIV_EXPR is the same as
125 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
126(simplify
127 (floor_div @0 @1)
09240451
MG
128 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
129 && TYPE_UNSIGNED (type))
a7f24614
RB
130 (trunc_div @0 @1)))
131
28093105
RB
132/* Combine two successive divisions. Note that combining ceil_div
133 and floor_div is trickier and combining round_div even more so. */
134(for div (trunc_div exact_div)
c306cfaf
RB
135 (simplify
136 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
137 (with {
138 bool overflow_p;
139 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
140 }
141 (if (!overflow_p)
142 (div @0 { wide_int_to_tree (type, mul); }))
ac19a303
RB
143 (if (overflow_p
144 && (TYPE_UNSIGNED (type)
145 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)))
c306cfaf
RB
146 { build_zero_cst (type); }))))
147
a7f24614 148/* Optimize A / A to 1.0 if we don't care about
09240451 149 NaNs or Infinities. */
a7f24614
RB
150(simplify
151 (rdiv @0 @0)
09240451
MG
152 (if (FLOAT_TYPE_P (type)
153 && ! HONOR_NANS (element_mode (type))
154 && ! HONOR_INFINITIES (element_mode (type)))
155 { build_one_cst (type); }))
156
157/* Optimize -A / A to -1.0 if we don't care about
158 NaNs or Infinities. */
159(simplify
160 (rdiv:c @0 (negate @0))
161 (if (FLOAT_TYPE_P (type)
162 && ! HONOR_NANS (element_mode (type))
163 && ! HONOR_INFINITIES (element_mode (type)))
164 { build_minus_one_cst (type); }))
a7f24614
RB
165
166/* In IEEE floating point, x/1 is not equivalent to x for snans. */
167(simplify
168 (rdiv @0 real_onep)
09240451 169 (if (!HONOR_SNANS (element_mode (type)))
a7f24614
RB
170 (non_lvalue @0)))
171
172/* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
173(simplify
174 (rdiv @0 real_minus_onep)
09240451 175 (if (!HONOR_SNANS (element_mode (type)))
a7f24614
RB
176 (negate @0)))
177
178/* If ARG1 is a constant, we can convert this to a multiply by the
179 reciprocal. This does not have the same rounding properties,
180 so only do this if -freciprocal-math. We can actually
181 always safely do it if ARG1 is a power of two, but it's hard to
182 tell if it is or not in a portable manner. */
183(for cst (REAL_CST COMPLEX_CST VECTOR_CST)
184 (simplify
185 (rdiv @0 cst@1)
186 (if (optimize)
53bc4b3a
RB
187 (if (flag_reciprocal_math
188 && !real_zerop (@1))
a7f24614
RB
189 (with
190 { tree tem = fold_binary (RDIV_EXPR, type, build_one_cst (type), @1); }
191 (if (tem)
192 (mult @0 { tem; } ))))
193 (if (cst != COMPLEX_CST)
194 (with { tree inverse = exact_inverse (type, @1); }
195 (if (inverse)
196 (mult @0 { inverse; } )))))))
197
e0ee10ed
RB
198/* Same applies to modulo operations, but fold is inconsistent here
199 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
a7f24614 200(for mod (ceil_mod floor_mod round_mod trunc_mod)
e0ee10ed
RB
201 /* 0 % X is always zero. */
202 (simplify
a7f24614 203 (mod integer_zerop@0 @1)
e0ee10ed
RB
204 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
205 (if (!integer_zerop (@1))
206 @0))
207 /* X % 1 is always zero. */
208 (simplify
a7f24614
RB
209 (mod @0 integer_onep)
210 { build_zero_cst (type); })
211 /* X % -1 is zero. */
212 (simplify
09240451
MG
213 (mod @0 integer_minus_onep@1)
214 (if (!TYPE_UNSIGNED (type))
a7f24614
RB
215 { build_zero_cst (type); })))
216
217/* X % -C is the same as X % C. */
218(simplify
219 (trunc_mod @0 INTEGER_CST@1)
220 (if (TYPE_SIGN (type) == SIGNED
221 && !TREE_OVERFLOW (@1)
222 && wi::neg_p (@1)
223 && !TYPE_OVERFLOW_TRAPS (type)
224 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
225 && !sign_bit_p (@1, @1))
226 (trunc_mod @0 (negate @1))))
e0ee10ed
RB
227
228/* x | ~0 -> ~0 */
229(simplify
230 (bit_ior @0 integer_all_onesp@1)
231 @1)
232
233/* x & 0 -> 0 */
234(simplify
235 (bit_and @0 integer_zerop@1)
236 @1)
237
238/* x ^ x -> 0 */
239(simplify
240 (bit_xor @0 @0)
241 { build_zero_cst (type); })
242
36a60e48
RB
243/* Canonicalize X ^ ~0 to ~X. */
244(simplify
245 (bit_xor @0 integer_all_onesp@1)
246 (bit_not @0))
247
248/* x & ~0 -> x */
249(simplify
250 (bit_and @0 integer_all_onesp)
251 (non_lvalue @0))
252
253/* x & x -> x, x | x -> x */
254(for bitop (bit_and bit_ior)
255 (simplify
256 (bitop @0 @0)
257 (non_lvalue @0)))
258
f3582e54
RB
259(simplify
260 (abs (negate @0))
261 (abs @0))
262(simplify
263 (abs tree_expr_nonnegative_p@0)
264 @0)
265
d4573ffe 266
5609420f
RB
267/* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
268 when profitable.
269 For bitwise binary operations apply operand conversions to the
270 binary operation result instead of to the operands. This allows
271 to combine successive conversions and bitwise binary operations.
272 We combine the above two cases by using a conditional convert. */
273(for bitop (bit_and bit_ior bit_xor)
274 (simplify
275 (bitop (convert @0) (convert? @1))
276 (if (((TREE_CODE (@1) == INTEGER_CST
277 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
ad6f996c
RB
278 && int_fits_type_p (@1, TREE_TYPE (@0)))
279 || (GIMPLE && types_compatible_p (TREE_TYPE (@0), TREE_TYPE (@1)))
280 || (GENERIC && TREE_TYPE (@0) == TREE_TYPE (@1)))
281 /* ??? This transform conflicts with fold-const.c doing
282 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
283 constants (if x has signed type, the sign bit cannot be set
284 in c). This folds extension into the BIT_AND_EXPR.
285 Restrict it to GIMPLE to avoid endless recursions. */
286 && (bitop != BIT_AND_EXPR || GIMPLE)
5609420f
RB
287 && (/* That's a good idea if the conversion widens the operand, thus
288 after hoisting the conversion the operation will be narrower. */
289 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
290 /* It's also a good idea if the conversion is to a non-integer
291 mode. */
292 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
293 /* Or if the precision of TO is not the same as the precision
294 of its mode. */
295 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
296 (convert (bitop @0 (convert @1))))))
297
298/* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
299(for bitop (bit_and bit_ior bit_xor)
300 (simplify
301 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
302 (bit_and (bitop @0 @2) @1)))
303
304/* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
305(simplify
306 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
307 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
308
309/* Combine successive equal operations with constants. */
310(for bitop (bit_and bit_ior bit_xor)
311 (simplify
312 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
313 (bitop @0 (bitop @1 @2))))
314
315/* Try simple folding for X op !X, and X op X with the help
316 of the truth_valued_p and logical_inverted_value predicates. */
317(match truth_valued_p
318 @0
319 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
f84e7fd6 320(for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
5609420f
RB
321 (match truth_valued_p
322 (op @0 @1)))
323(match truth_valued_p
324 (truth_not @0))
325
326(match (logical_inverted_value @0)
327 (bit_not truth_valued_p@0))
328(match (logical_inverted_value @0)
09240451 329 (eq @0 integer_zerop))
5609420f 330(match (logical_inverted_value @0)
09240451 331 (ne truth_valued_p@0 integer_truep))
5609420f 332(match (logical_inverted_value @0)
09240451 333 (bit_xor truth_valued_p@0 integer_truep))
5609420f
RB
334
335/* X & !X -> 0. */
336(simplify
337 (bit_and:c @0 (logical_inverted_value @0))
338 { build_zero_cst (type); })
339/* X | !X and X ^ !X -> 1, , if X is truth-valued. */
340(for op (bit_ior bit_xor)
341 (simplify
342 (op:c truth_valued_p@0 (logical_inverted_value @0))
f84e7fd6 343 { constant_boolean_node (true, type); }))
5609420f
RB
344
345(for bitop (bit_and bit_ior)
346 rbitop (bit_ior bit_and)
347 /* (x | y) & x -> x */
348 /* (x & y) | x -> x */
349 (simplify
350 (bitop:c (rbitop:c @0 @1) @0)
351 @0)
352 /* (~x | y) & x -> x & y */
353 /* (~x & y) | x -> x | y */
354 (simplify
355 (bitop:c (rbitop:c (bit_not @0) @1) @0)
356 (bitop @0 @1)))
357
358/* If arg1 and arg2 are booleans (or any single bit type)
359 then try to simplify:
360
361 (~X & Y) -> X < Y
362 (X & ~Y) -> Y < X
363 (~X | Y) -> X <= Y
364 (X | ~Y) -> Y <= X
365
366 But only do this if our result feeds into a comparison as
367 this transformation is not always a win, particularly on
368 targets with and-not instructions.
369 -> simplify_bitwise_binary_boolean */
370(simplify
371 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
372 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
373 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
374 (lt @0 @1)))
375(simplify
376 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
377 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
378 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
379 (le @0 @1)))
380
5609420f
RB
381/* ~~x -> x */
382(simplify
383 (bit_not (bit_not @0))
384 @0)
385
5609420f 386
a499aac5
RB
387/* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
388(simplify
e6121733
RB
389 (pointer_plus (pointer_plus@2 @0 @1) @3)
390 (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2))
391 (pointer_plus @0 (plus @1 @3))))
a499aac5
RB
392
393/* Pattern match
394 tem1 = (long) ptr1;
395 tem2 = (long) ptr2;
396 tem3 = tem2 - tem1;
397 tem4 = (unsigned long) tem3;
398 tem5 = ptr1 + tem4;
399 and produce
400 tem5 = ptr2; */
401(simplify
402 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
403 /* Conditionally look through a sign-changing conversion. */
404 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
405 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
406 || (GENERIC && type == TREE_TYPE (@1))))
407 @1))
408
409/* Pattern match
410 tem = (sizetype) ptr;
411 tem = tem & algn;
412 tem = -tem;
413 ... = ptr p+ tem;
414 and produce the simpler and easier to analyze with respect to alignment
415 ... = ptr & ~algn; */
416(simplify
417 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
418 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
419 (bit_and @0 { algn; })))
420
421
cc7b5acf
RB
422/* We can't reassociate at all for saturating types. */
423(if (!TYPE_SATURATING (type))
424
425 /* Contract negates. */
426 /* A + (-B) -> A - B */
427 (simplify
428 (plus:c (convert1? @0) (convert2? (negate @1)))
429 /* Apply STRIP_NOPS on @0 and the negate. */
430 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
431 && tree_nop_conversion_p (type, TREE_TYPE (@1))
6a4f0678 432 && !TYPE_OVERFLOW_SANITIZED (type))
cc7b5acf
RB
433 (minus (convert @0) (convert @1))))
434 /* A - (-B) -> A + B */
435 (simplify
436 (minus (convert1? @0) (convert2? (negate @1)))
437 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2f68e8bc 438 && tree_nop_conversion_p (type, TREE_TYPE (@1))
6a4f0678 439 && !TYPE_OVERFLOW_SANITIZED (type))
cc7b5acf
RB
440 (plus (convert @0) (convert @1))))
441 /* -(-A) -> A */
442 (simplify
443 (negate (convert? (negate @1)))
444 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
6a4f0678 445 && !TYPE_OVERFLOW_SANITIZED (type))
a0f12cf8 446 (convert @1)))
cc7b5acf
RB
447
448 /* We can't reassociate floating-point or fixed-point plus or minus
449 because of saturation to +-Inf. */
450 (if (!FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type))
451
452 /* Match patterns that allow contracting a plus-minus pair
453 irrespective of overflow issues. */
454 /* (A +- B) - A -> +- B */
455 /* (A +- B) -+ B -> A */
456 /* A - (A +- B) -> -+ B */
457 /* A +- (B -+ A) -> +- B */
458 (simplify
459 (minus (plus:c @0 @1) @0)
460 @1)
461 (simplify
462 (minus (minus @0 @1) @0)
463 (negate @1))
464 (simplify
465 (plus:c (minus @0 @1) @1)
466 @0)
467 (simplify
468 (minus @0 (plus:c @0 @1))
469 (negate @1))
470 (simplify
471 (minus @0 (minus @0 @1))
472 @1)
473
474 /* (A +- CST) +- CST -> A + CST */
475 (for outer_op (plus minus)
476 (for inner_op (plus minus)
477 (simplify
478 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
479 /* If the constant operation overflows we cannot do the transform
480 as we would introduce undefined overflow, for example
481 with (a - 1) + INT_MIN. */
482 (with { tree cst = fold_binary (outer_op == inner_op
483 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
484 (if (cst && !TREE_OVERFLOW (cst))
485 (inner_op @0 { cst; } ))))))
486
487 /* (CST - A) +- CST -> CST - A */
488 (for outer_op (plus minus)
489 (simplify
490 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
491 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
492 (if (cst && !TREE_OVERFLOW (cst))
493 (minus { cst; } @0)))))
494
495 /* ~A + A -> -1 */
496 (simplify
497 (plus:c (bit_not @0) @0)
498 (if (!TYPE_OVERFLOW_TRAPS (type))
499 { build_all_ones_cst (type); }))
500
501 /* ~A + 1 -> -A */
502 (simplify
e19740ae
RB
503 (plus (convert? (bit_not @0)) integer_each_onep)
504 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
505 (negate (convert @0))))
506
507 /* -A - 1 -> ~A */
508 (simplify
509 (minus (convert? (negate @0)) integer_each_onep)
510 (if (!TYPE_OVERFLOW_TRAPS (type)
511 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
512 (bit_not (convert @0))))
513
514 /* -1 - A -> ~A */
515 (simplify
516 (minus integer_all_onesp @0)
517 (if (TREE_CODE (type) != COMPLEX_TYPE)
518 (bit_not @0)))
cc7b5acf
RB
519
520 /* (T)(P + A) - (T)P -> (T) A */
521 (for add (plus pointer_plus)
522 (simplify
523 (minus (convert (add @0 @1))
524 (convert @0))
09240451 525 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
cc7b5acf
RB
526 /* For integer types, if A has a smaller type
527 than T the result depends on the possible
528 overflow in P + A.
529 E.g. T=size_t, A=(unsigned)429497295, P>0.
530 However, if an overflow in P + A would cause
531 undefined behavior, we can assume that there
532 is no overflow. */
533 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
534 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
535 /* For pointer types, if the conversion of A to the
536 final type requires a sign- or zero-extension,
537 then we have to punt - it is not defined which
538 one is correct. */
539 || (POINTER_TYPE_P (TREE_TYPE (@0))
540 && TREE_CODE (@1) == INTEGER_CST
541 && tree_int_cst_sign_bit (@1) == 0))
542 (convert @1))))))
543
544
a7f24614
RB
545/* Simplifications of MIN_EXPR and MAX_EXPR. */
546
547(for minmax (min max)
548 (simplify
549 (minmax @0 @0)
550 @0))
551(simplify
552 (min @0 @1)
553 (if (INTEGRAL_TYPE_P (type)
554 && TYPE_MIN_VALUE (type)
555 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
556 @1))
557(simplify
558 (max @0 @1)
559 (if (INTEGRAL_TYPE_P (type)
560 && TYPE_MAX_VALUE (type)
561 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
562 @1))
563
564
565/* Simplifications of shift and rotates. */
566
567(for rotate (lrotate rrotate)
568 (simplify
569 (rotate integer_all_onesp@0 @1)
570 @0))
571
572/* Optimize -1 >> x for arithmetic right shifts. */
573(simplify
574 (rshift integer_all_onesp@0 @1)
575 (if (!TYPE_UNSIGNED (type)
576 && tree_expr_nonnegative_p (@1))
577 @0))
578
579(for shiftrotate (lrotate rrotate lshift rshift)
580 (simplify
581 (shiftrotate @0 integer_zerop)
582 (non_lvalue @0))
583 (simplify
584 (shiftrotate integer_zerop@0 @1)
585 @0)
586 /* Prefer vector1 << scalar to vector1 << vector2
587 if vector2 is uniform. */
588 (for vec (VECTOR_CST CONSTRUCTOR)
589 (simplify
590 (shiftrotate @0 vec@1)
591 (with { tree tem = uniform_vector_p (@1); }
592 (if (tem)
593 (shiftrotate @0 { tem; }))))))
594
595/* Rewrite an LROTATE_EXPR by a constant into an
596 RROTATE_EXPR by a new constant. */
597(simplify
598 (lrotate @0 INTEGER_CST@1)
599 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
600 build_int_cst (TREE_TYPE (@1),
601 element_precision (type)), @1); }))
602
cc7b5acf 603
d4573ffe
RB
604/* Simplifications of conversions. */
605
606/* Basic strip-useless-type-conversions / strip_nops. */
f3582e54 607(for cvt (convert view_convert float fix_trunc)
d4573ffe
RB
608 (simplify
609 (cvt @0)
610 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
611 || (GENERIC && type == TREE_TYPE (@0)))
612 @0)))
613
614/* Contract view-conversions. */
615(simplify
616 (view_convert (view_convert @0))
617 (view_convert @0))
618
619/* For integral conversions with the same precision or pointer
620 conversions use a NOP_EXPR instead. */
621(simplify
622 (view_convert @0)
623 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
624 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
625 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
626 (convert @0)))
627
628/* Strip inner integral conversions that do not change precision or size. */
629(simplify
630 (view_convert (convert@0 @1))
631 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
632 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
633 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
634 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
635 (view_convert @1)))
636
637/* Re-association barriers around constants and other re-association
638 barriers can be removed. */
639(simplify
640 (paren CONSTANT_CLASS_P@0)
641 @0)
642(simplify
643 (paren (paren@1 @0))
644 @1)
1e51d0a2
RB
645
646/* Handle cases of two conversions in a row. */
647(for ocvt (convert float fix_trunc)
648 (for icvt (convert float)
649 (simplify
650 (ocvt (icvt@1 @0))
651 (with
652 {
653 tree inside_type = TREE_TYPE (@0);
654 tree inter_type = TREE_TYPE (@1);
655 int inside_int = INTEGRAL_TYPE_P (inside_type);
656 int inside_ptr = POINTER_TYPE_P (inside_type);
657 int inside_float = FLOAT_TYPE_P (inside_type);
09240451 658 int inside_vec = VECTOR_TYPE_P (inside_type);
1e51d0a2
RB
659 unsigned int inside_prec = TYPE_PRECISION (inside_type);
660 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
661 int inter_int = INTEGRAL_TYPE_P (inter_type);
662 int inter_ptr = POINTER_TYPE_P (inter_type);
663 int inter_float = FLOAT_TYPE_P (inter_type);
09240451 664 int inter_vec = VECTOR_TYPE_P (inter_type);
1e51d0a2
RB
665 unsigned int inter_prec = TYPE_PRECISION (inter_type);
666 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
667 int final_int = INTEGRAL_TYPE_P (type);
668 int final_ptr = POINTER_TYPE_P (type);
669 int final_float = FLOAT_TYPE_P (type);
09240451 670 int final_vec = VECTOR_TYPE_P (type);
1e51d0a2
RB
671 unsigned int final_prec = TYPE_PRECISION (type);
672 int final_unsignedp = TYPE_UNSIGNED (type);
673 }
674 /* In addition to the cases of two conversions in a row
675 handled below, if we are converting something to its own
676 type via an object of identical or wider precision, neither
677 conversion is needed. */
678 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
679 || (GENERIC
680 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
681 && (((inter_int || inter_ptr) && final_int)
682 || (inter_float && final_float))
683 && inter_prec >= final_prec)
684 (ocvt @0))
685
686 /* Likewise, if the intermediate and initial types are either both
687 float or both integer, we don't need the middle conversion if the
688 former is wider than the latter and doesn't change the signedness
689 (for integers). Avoid this if the final type is a pointer since
690 then we sometimes need the middle conversion. Likewise if the
691 final type has a precision not equal to the size of its mode. */
692 (if (((inter_int && inside_int)
693 || (inter_float && inside_float)
694 || (inter_vec && inside_vec))
695 && inter_prec >= inside_prec
696 && (inter_float || inter_vec
697 || inter_unsignedp == inside_unsignedp)
09240451
MG
698 && ! (final_prec != GET_MODE_PRECISION (element_mode (type))
699 && element_mode (type) == element_mode (inter_type))
1e51d0a2
RB
700 && ! final_ptr
701 && (! final_vec || inter_prec == inside_prec))
702 (ocvt @0))
703
704 /* If we have a sign-extension of a zero-extended value, we can
705 replace that by a single zero-extension. Likewise if the
706 final conversion does not change precision we can drop the
707 intermediate conversion. */
708 (if (inside_int && inter_int && final_int
709 && ((inside_prec < inter_prec && inter_prec < final_prec
710 && inside_unsignedp && !inter_unsignedp)
711 || final_prec == inter_prec))
712 (ocvt @0))
713
714 /* Two conversions in a row are not needed unless:
715 - some conversion is floating-point (overstrict for now), or
716 - some conversion is a vector (overstrict for now), or
717 - the intermediate type is narrower than both initial and
718 final, or
719 - the intermediate type and innermost type differ in signedness,
720 and the outermost type is wider than the intermediate, or
721 - the initial type is a pointer type and the precisions of the
722 intermediate and final types differ, or
723 - the final type is a pointer type and the precisions of the
724 initial and intermediate types differ. */
725 (if (! inside_float && ! inter_float && ! final_float
726 && ! inside_vec && ! inter_vec && ! final_vec
727 && (inter_prec >= inside_prec || inter_prec >= final_prec)
728 && ! (inside_int && inter_int
729 && inter_unsignedp != inside_unsignedp
730 && inter_prec < final_prec)
731 && ((inter_unsignedp && inter_prec > inside_prec)
732 == (final_unsignedp && final_prec > inter_prec))
733 && ! (inside_ptr && inter_prec != final_prec)
734 && ! (final_ptr && inside_prec != inter_prec)
735 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
736 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1f00c1b9
RB
737 (ocvt @0))
738
739 /* A truncation to an unsigned type (a zero-extension) should be
740 canonicalized as bitwise and of a mask. */
741 (if (final_int && inter_int && inside_int
742 && final_prec == inside_prec
743 && final_prec > inter_prec
744 && inter_unsignedp)
745 (convert (bit_and @0 { wide_int_to_tree
746 (inside_type,
747 wi::mask (inter_prec, false,
748 TYPE_PRECISION (inside_type))); })))
749
750 /* If we are converting an integer to a floating-point that can
751 represent it exactly and back to an integer, we can skip the
752 floating-point conversion. */
753 (if (inside_int && inter_float && final_int &&
754 (unsigned) significand_size (TYPE_MODE (inter_type))
755 >= inside_prec - !inside_unsignedp)
756 (convert @0))))))
ea2042ba
RB
757
758/* If we have a narrowing conversion to an integral type that is fed by a
759 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
760 masks off bits outside the final type (and nothing else). */
761(simplify
762 (convert (bit_and @0 INTEGER_CST@1))
763 (if (INTEGRAL_TYPE_P (type)
764 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
765 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
766 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
767 TYPE_PRECISION (type)), 0))
768 (convert @0)))
a25454ea
RB
769
770
771/* (X /[ex] A) * A -> X. */
772(simplify
773 (mult (convert? (exact_div @0 @1)) @1)
774 /* Look through a sign-changing conversion. */
775 (if (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
776 (convert @0)))
eaeba53a 777
a7f24614
RB
778/* Canonicalization of binary operations. */
779
780/* Convert X + -C into X - C. */
781(simplify
782 (plus @0 REAL_CST@1)
783 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
784 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
785 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
786 (minus @0 { tem; })))))
787
788/* Convert x+x into x*2.0. */
789(simplify
790 (plus @0 @0)
791 (if (SCALAR_FLOAT_TYPE_P (type))
792 (mult @0 { build_real (type, dconst2); })))
793
794(simplify
795 (minus integer_zerop @1)
796 (negate @1))
797
798/* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
799 ARG0 is zero and X + ARG0 reduces to X, since that would mean
800 (-ARG1 + ARG0) reduces to -ARG1. */
801(simplify
802 (minus real_zerop@0 @1)
803 (if (fold_real_zero_addition_p (type, @0, 0))
804 (negate @1)))
805
806/* Transform x * -1 into -x. */
807(simplify
808 (mult @0 integer_minus_onep)
809 (negate @0))
eaeba53a
RB
810
811/* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
812(simplify
813 (complex (realpart @0) (imagpart @0))
814 @0)
815(simplify
816 (realpart (complex @0 @1))
817 @0)
818(simplify
819 (imagpart (complex @0 @1))
820 @1)
83633539
RB
821
822
823/* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
824(for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
825 (simplify
826 (bswap (bswap @0))
827 @0)
828 (simplify
829 (bswap (bit_not (bswap @0)))
830 (bit_not @0))
831 (for bitop (bit_xor bit_ior bit_and)
832 (simplify
833 (bswap (bitop:c (bswap @0) @1))
834 (bitop @0 (bswap @1)))))
96994de0
RB
835
836
837/* Combine COND_EXPRs and VEC_COND_EXPRs. */
838
839/* Simplify constant conditions.
840 Only optimize constant conditions when the selected branch
841 has the same type as the COND_EXPR. This avoids optimizing
842 away "c ? x : throw", where the throw has a void type.
843 Note that we cannot throw away the fold-const.c variant nor
844 this one as we depend on doing this transform before possibly
845 A ? B : B -> B triggers and the fold-const.c one can optimize
846 0 ? A : B to B even if A has side-effects. Something
847 genmatch cannot handle. */
848(simplify
849 (cond INTEGER_CST@0 @1 @2)
850 (if (integer_zerop (@0)
851 && (!VOID_TYPE_P (TREE_TYPE (@2))
852 || VOID_TYPE_P (type)))
853 @2)
854 (if (!integer_zerop (@0)
855 && (!VOID_TYPE_P (TREE_TYPE (@1))
856 || VOID_TYPE_P (type)))
857 @1))
858(simplify
859 (vec_cond VECTOR_CST@0 @1 @2)
860 (if (integer_all_onesp (@0))
861 @1)
862 (if (integer_zerop (@0))
863 @2))
864
865(for cnd (cond vec_cond)
866 /* A ? B : (A ? X : C) -> A ? B : C. */
867 (simplify
868 (cnd @0 (cnd @0 @1 @2) @3)
869 (cnd @0 @1 @3))
870 (simplify
871 (cnd @0 @1 (cnd @0 @2 @3))
872 (cnd @0 @1 @3))
873
874 /* A ? B : B -> B. */
875 (simplify
876 (cnd @0 @1 @1)
09240451 877 @1)
96994de0 878
09240451
MG
879 /* !A ? B : C -> A ? C : B. */
880 (simplify
881 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
882 (cnd @0 @2 @1)))
f84e7fd6
RB
883
884
885/* Simplifications of comparisons. */
886
887/* We can simplify a logical negation of a comparison to the
888 inverted comparison. As we cannot compute an expression
889 operator using invert_tree_comparison we have to simulate
890 that with expression code iteration. */
891(for cmp (tcc_comparison)
892 icmp (inverted_tcc_comparison)
893 ncmp (inverted_tcc_comparison_with_nans)
894 /* Ideally we'd like to combine the following two patterns
895 and handle some more cases by using
896 (logical_inverted_value (cmp @0 @1))
897 here but for that genmatch would need to "inline" that.
898 For now implement what forward_propagate_comparison did. */
899 (simplify
900 (bit_not (cmp @0 @1))
901 (if (VECTOR_TYPE_P (type)
902 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
903 /* Comparison inversion may be impossible for trapping math,
904 invert_tree_comparison will tell us. But we can't use
905 a computed operator in the replacement tree thus we have
906 to play the trick below. */
907 (with { enum tree_code ic = invert_tree_comparison
09240451 908 (cmp, HONOR_NANS (element_mode (@0))); }
f84e7fd6
RB
909 (if (ic == icmp)
910 (icmp @0 @1))
911 (if (ic == ncmp)
912 (ncmp @0 @1)))))
913 (simplify
09240451
MG
914 (bit_xor (cmp @0 @1) integer_truep)
915 (with { enum tree_code ic = invert_tree_comparison
916 (cmp, HONOR_NANS (element_mode (@0))); }
917 (if (ic == icmp)
918 (icmp @0 @1))
919 (if (ic == ncmp)
920 (ncmp @0 @1)))))