re PR tree-optimization/64454 (optimize (x%5)%5)
[gcc.git] / gcc / match.pd
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-2015 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along 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
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep
30 real_zerop real_onep real_minus_onep
31 CONSTANT_CLASS_P
32 tree_expr_nonnegative_p)
33
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
42
43 /* Simplifications of operations with one constant operand and
44 simplifications to constants or single values. */
45
46 (for op (plus pointer_plus minus bit_ior bit_xor)
47 (simplify
48 (op @0 integer_zerop)
49 (non_lvalue @0)))
50
51 /* 0 +p index -> (type)index */
52 (simplify
53 (pointer_plus integer_zerop @1)
54 (non_lvalue (convert @1)))
55
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
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
75 (minus @0 @0)
76 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
77 { build_zero_cst (type); }))
78
79 (simplify
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)
89 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (element_mode (type)))
90 @1))
91
92 /* In IEEE floating point, x*1 is not equivalent to x for snans.
93 Likewise for complex arithmetic with signed zeros. */
94 (simplify
95 (mult @0 real_onep)
96 (if (!HONOR_SNANS (element_mode (type))
97 && (!HONOR_SIGNED_ZEROS (element_mode (type))
98 || !COMPLEX_FLOAT_TYPE_P (type)))
99 (non_lvalue @0)))
100
101 /* Transform x * -1.0 into -x. */
102 (simplify
103 (mult @0 real_minus_onep)
104 (if (!HONOR_SNANS (element_mode (type))
105 && (!HONOR_SIGNED_ZEROS (element_mode (type))
106 || !COMPLEX_FLOAT_TYPE_P (type)))
107 (negate @0)))
108
109 /* Make sure to preserve divisions by zero. This is the reason why
110 we don't simplify x / x to 1 or 0 / x to 0. */
111 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
112 (simplify
113 (op @0 integer_onep)
114 (non_lvalue @0)))
115
116 /* X / -1 is -X. */
117 (for div (trunc_div ceil_div floor_div round_div exact_div)
118 (simplify
119 (div @0 integer_minus_onep@1)
120 (if (!TYPE_UNSIGNED (type))
121 (negate @0))))
122
123 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
124 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
125 (simplify
126 (floor_div @0 @1)
127 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
128 && TYPE_UNSIGNED (type))
129 (trunc_div @0 @1)))
130
131 /* Combine two successive divisions. Note that combining ceil_div
132 and floor_div is trickier and combining round_div even more so. */
133 (for div (trunc_div exact_div)
134 (simplify
135 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
136 (with {
137 bool overflow_p;
138 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
139 }
140 (if (!overflow_p)
141 (div @0 { wide_int_to_tree (type, mul); }))
142 (if (overflow_p
143 && (TYPE_UNSIGNED (type)
144 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)))
145 { build_zero_cst (type); }))))
146
147 /* Optimize A / A to 1.0 if we don't care about
148 NaNs or Infinities. */
149 (simplify
150 (rdiv @0 @0)
151 (if (FLOAT_TYPE_P (type)
152 && ! HONOR_NANS (type)
153 && ! HONOR_INFINITIES (element_mode (type)))
154 { build_one_cst (type); }))
155
156 /* Optimize -A / A to -1.0 if we don't care about
157 NaNs or Infinities. */
158 (simplify
159 (rdiv:c @0 (negate @0))
160 (if (FLOAT_TYPE_P (type)
161 && ! HONOR_NANS (type)
162 && ! HONOR_INFINITIES (element_mode (type)))
163 { build_minus_one_cst (type); }))
164
165 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
166 (simplify
167 (rdiv @0 real_onep)
168 (if (!HONOR_SNANS (element_mode (type)))
169 (non_lvalue @0)))
170
171 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
172 (simplify
173 (rdiv @0 real_minus_onep)
174 (if (!HONOR_SNANS (element_mode (type)))
175 (negate @0)))
176
177 /* If ARG1 is a constant, we can convert this to a multiply by the
178 reciprocal. This does not have the same rounding properties,
179 so only do this if -freciprocal-math. We can actually
180 always safely do it if ARG1 is a power of two, but it's hard to
181 tell if it is or not in a portable manner. */
182 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
183 (simplify
184 (rdiv @0 cst@1)
185 (if (optimize)
186 (if (flag_reciprocal_math
187 && !real_zerop (@1))
188 (with
189 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
190 (if (tem)
191 (mult @0 { tem; } ))))
192 (if (cst != COMPLEX_CST)
193 (with { tree inverse = exact_inverse (type, @1); }
194 (if (inverse)
195 (mult @0 { inverse; } )))))))
196
197 /* Same applies to modulo operations, but fold is inconsistent here
198 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
199 (for mod (ceil_mod floor_mod round_mod trunc_mod)
200 /* 0 % X is always zero. */
201 (simplify
202 (mod integer_zerop@0 @1)
203 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
204 (if (!integer_zerop (@1))
205 @0))
206 /* X % 1 is always zero. */
207 (simplify
208 (mod @0 integer_onep)
209 { build_zero_cst (type); })
210 /* X % -1 is zero. */
211 (simplify
212 (mod @0 integer_minus_onep@1)
213 (if (!TYPE_UNSIGNED (type))
214 { build_zero_cst (type); }))
215 /* (X % Y) % Y is just X % Y. */
216 (simplify
217 (mod (mod@2 @0 @1) @1)
218 @2))
219
220 /* X % -C is the same as X % C. */
221 (simplify
222 (trunc_mod @0 INTEGER_CST@1)
223 (if (TYPE_SIGN (type) == SIGNED
224 && !TREE_OVERFLOW (@1)
225 && wi::neg_p (@1)
226 && !TYPE_OVERFLOW_TRAPS (type)
227 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
228 && !sign_bit_p (@1, @1))
229 (trunc_mod @0 (negate @1))))
230
231 /* X % Y is smaller than Y. */
232 (for cmp (lt ge)
233 (simplify
234 (cmp (trunc_mod @0 @1) @1)
235 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
236 { constant_boolean_node (cmp == LT_EXPR, type); })))
237 (for cmp (gt le)
238 (simplify
239 (cmp @1 (trunc_mod @0 @1))
240 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
241 { constant_boolean_node (cmp == GT_EXPR, type); })))
242
243 /* x | ~0 -> ~0 */
244 (simplify
245 (bit_ior @0 integer_all_onesp@1)
246 @1)
247
248 /* x & 0 -> 0 */
249 (simplify
250 (bit_and @0 integer_zerop@1)
251 @1)
252
253 /* x ^ x -> 0 */
254 (simplify
255 (bit_xor @0 @0)
256 { build_zero_cst (type); })
257
258 /* Canonicalize X ^ ~0 to ~X. */
259 (simplify
260 (bit_xor @0 integer_all_onesp@1)
261 (bit_not @0))
262
263 /* x & ~0 -> x */
264 (simplify
265 (bit_and @0 integer_all_onesp)
266 (non_lvalue @0))
267
268 /* x & x -> x, x | x -> x */
269 (for bitop (bit_and bit_ior)
270 (simplify
271 (bitop @0 @0)
272 (non_lvalue @0)))
273
274 /* x + (x & 1) -> (x + 1) & ~1 */
275 (simplify
276 (plus:c @0 (bit_and@2 @0 integer_onep@1))
277 (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2))
278 (bit_and (plus @0 @1) (bit_not @1))))
279
280 /* x & ~(x & y) -> x & ~y */
281 /* x | ~(x | y) -> x | ~y */
282 (for bitop (bit_and bit_ior)
283 (simplify
284 (bitop:c @0 (bit_not (bitop:c@2 @0 @1)))
285 (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2))
286 (bitop @0 (bit_not @1)))))
287
288 (simplify
289 (abs (negate @0))
290 (abs @0))
291 (simplify
292 (abs tree_expr_nonnegative_p@0)
293 @0)
294
295
296 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
297 when profitable.
298 For bitwise binary operations apply operand conversions to the
299 binary operation result instead of to the operands. This allows
300 to combine successive conversions and bitwise binary operations.
301 We combine the above two cases by using a conditional convert. */
302 (for bitop (bit_and bit_ior bit_xor)
303 (simplify
304 (bitop (convert @0) (convert? @1))
305 (if (((TREE_CODE (@1) == INTEGER_CST
306 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
307 && int_fits_type_p (@1, TREE_TYPE (@0)))
308 || types_match (@0, @1))
309 /* ??? This transform conflicts with fold-const.c doing
310 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
311 constants (if x has signed type, the sign bit cannot be set
312 in c). This folds extension into the BIT_AND_EXPR.
313 Restrict it to GIMPLE to avoid endless recursions. */
314 && (bitop != BIT_AND_EXPR || GIMPLE)
315 && (/* That's a good idea if the conversion widens the operand, thus
316 after hoisting the conversion the operation will be narrower. */
317 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
318 /* It's also a good idea if the conversion is to a non-integer
319 mode. */
320 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
321 /* Or if the precision of TO is not the same as the precision
322 of its mode. */
323 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
324 (convert (bitop @0 (convert @1))))))
325
326 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
327 (for bitop (bit_and bit_ior bit_xor)
328 (simplify
329 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
330 (bit_and (bitop @0 @2) @1)))
331
332 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
333 (simplify
334 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
335 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
336
337 /* Combine successive equal operations with constants. */
338 (for bitop (bit_and bit_ior bit_xor)
339 (simplify
340 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
341 (bitop @0 (bitop @1 @2))))
342
343 /* Try simple folding for X op !X, and X op X with the help
344 of the truth_valued_p and logical_inverted_value predicates. */
345 (match truth_valued_p
346 @0
347 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
348 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
349 (match truth_valued_p
350 (op @0 @1)))
351 (match truth_valued_p
352 (truth_not @0))
353
354 (match (logical_inverted_value @0)
355 (bit_not truth_valued_p@0))
356 (match (logical_inverted_value @0)
357 (eq @0 integer_zerop))
358 (match (logical_inverted_value @0)
359 (ne truth_valued_p@0 integer_truep))
360 (match (logical_inverted_value @0)
361 (bit_xor truth_valued_p@0 integer_truep))
362
363 /* X & !X -> 0. */
364 (simplify
365 (bit_and:c @0 (logical_inverted_value @0))
366 { build_zero_cst (type); })
367 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
368 (for op (bit_ior bit_xor)
369 (simplify
370 (op:c truth_valued_p@0 (logical_inverted_value @0))
371 { constant_boolean_node (true, type); }))
372
373 (for bitop (bit_and bit_ior)
374 rbitop (bit_ior bit_and)
375 /* (x | y) & x -> x */
376 /* (x & y) | x -> x */
377 (simplify
378 (bitop:c (rbitop:c @0 @1) @0)
379 @0)
380 /* (~x | y) & x -> x & y */
381 /* (~x & y) | x -> x | y */
382 (simplify
383 (bitop:c (rbitop:c (bit_not @0) @1) @0)
384 (bitop @0 @1)))
385
386 /* If arg1 and arg2 are booleans (or any single bit type)
387 then try to simplify:
388
389 (~X & Y) -> X < Y
390 (X & ~Y) -> Y < X
391 (~X | Y) -> X <= Y
392 (X | ~Y) -> Y <= X
393
394 But only do this if our result feeds into a comparison as
395 this transformation is not always a win, particularly on
396 targets with and-not instructions.
397 -> simplify_bitwise_binary_boolean */
398 (simplify
399 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
400 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
401 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
402 (lt @0 @1)))
403 (simplify
404 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
405 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
406 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
407 (le @0 @1)))
408
409 /* ~~x -> x */
410 (simplify
411 (bit_not (bit_not @0))
412 @0)
413
414 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
415 (simplify
416 (bit_ior:c (bit_and:c@3 @0 (bit_not @2)) (bit_and:c@4 @1 @2))
417 (if ((TREE_CODE (@3) != SSA_NAME || has_single_use (@3))
418 && (TREE_CODE (@4) != SSA_NAME || has_single_use (@4)))
419 (bit_xor (bit_and (bit_xor @0 @1) @2) @0)))
420
421
422 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
423 (simplify
424 (pointer_plus (pointer_plus@2 @0 @1) @3)
425 (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2))
426 (pointer_plus @0 (plus @1 @3))))
427
428 /* Pattern match
429 tem1 = (long) ptr1;
430 tem2 = (long) ptr2;
431 tem3 = tem2 - tem1;
432 tem4 = (unsigned long) tem3;
433 tem5 = ptr1 + tem4;
434 and produce
435 tem5 = ptr2; */
436 (simplify
437 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
438 /* Conditionally look through a sign-changing conversion. */
439 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
440 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
441 || (GENERIC && type == TREE_TYPE (@1))))
442 @1))
443
444 /* Pattern match
445 tem = (sizetype) ptr;
446 tem = tem & algn;
447 tem = -tem;
448 ... = ptr p+ tem;
449 and produce the simpler and easier to analyze with respect to alignment
450 ... = ptr & ~algn; */
451 (simplify
452 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
453 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
454 (bit_and @0 { algn; })))
455
456
457 /* We can't reassociate at all for saturating types. */
458 (if (!TYPE_SATURATING (type))
459
460 /* Contract negates. */
461 /* A + (-B) -> A - B */
462 (simplify
463 (plus:c (convert1? @0) (convert2? (negate @1)))
464 /* Apply STRIP_NOPS on @0 and the negate. */
465 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
466 && tree_nop_conversion_p (type, TREE_TYPE (@1))
467 && !TYPE_OVERFLOW_SANITIZED (type))
468 (minus (convert @0) (convert @1))))
469 /* A - (-B) -> A + B */
470 (simplify
471 (minus (convert1? @0) (convert2? (negate @1)))
472 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
473 && tree_nop_conversion_p (type, TREE_TYPE (@1))
474 && !TYPE_OVERFLOW_SANITIZED (type))
475 (plus (convert @0) (convert @1))))
476 /* -(-A) -> A */
477 (simplify
478 (negate (convert? (negate @1)))
479 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
480 && !TYPE_OVERFLOW_SANITIZED (type))
481 (convert @1)))
482
483 /* We can't reassociate floating-point or fixed-point plus or minus
484 because of saturation to +-Inf. */
485 (if (!FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type))
486
487 /* Match patterns that allow contracting a plus-minus pair
488 irrespective of overflow issues. */
489 /* (A +- B) - A -> +- B */
490 /* (A +- B) -+ B -> A */
491 /* A - (A +- B) -> -+ B */
492 /* A +- (B -+ A) -> +- B */
493 (simplify
494 (minus (plus:c @0 @1) @0)
495 @1)
496 (simplify
497 (minus (minus @0 @1) @0)
498 (negate @1))
499 (simplify
500 (plus:c (minus @0 @1) @1)
501 @0)
502 (simplify
503 (minus @0 (plus:c @0 @1))
504 (negate @1))
505 (simplify
506 (minus @0 (minus @0 @1))
507 @1)
508
509 /* (A +- CST) +- CST -> A + CST */
510 (for outer_op (plus minus)
511 (for inner_op (plus minus)
512 (simplify
513 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
514 /* If the constant operation overflows we cannot do the transform
515 as we would introduce undefined overflow, for example
516 with (a - 1) + INT_MIN. */
517 (with { tree cst = fold_binary (outer_op == inner_op
518 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
519 (if (cst && !TREE_OVERFLOW (cst))
520 (inner_op @0 { cst; } ))))))
521
522 /* (CST - A) +- CST -> CST - A */
523 (for outer_op (plus minus)
524 (simplify
525 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
526 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
527 (if (cst && !TREE_OVERFLOW (cst))
528 (minus { cst; } @0)))))
529
530 /* ~A + A -> -1 */
531 (simplify
532 (plus:c (bit_not @0) @0)
533 (if (!TYPE_OVERFLOW_TRAPS (type))
534 { build_all_ones_cst (type); }))
535
536 /* ~A + 1 -> -A */
537 (simplify
538 (plus (convert? (bit_not @0)) integer_each_onep)
539 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
540 (negate (convert @0))))
541
542 /* -A - 1 -> ~A */
543 (simplify
544 (minus (convert? (negate @0)) integer_each_onep)
545 (if (!TYPE_OVERFLOW_TRAPS (type)
546 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
547 (bit_not (convert @0))))
548
549 /* -1 - A -> ~A */
550 (simplify
551 (minus integer_all_onesp @0)
552 (bit_not @0))
553
554 /* (T)(P + A) - (T)P -> (T) A */
555 (for add (plus pointer_plus)
556 (simplify
557 (minus (convert (add @0 @1))
558 (convert @0))
559 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
560 /* For integer types, if A has a smaller type
561 than T the result depends on the possible
562 overflow in P + A.
563 E.g. T=size_t, A=(unsigned)429497295, P>0.
564 However, if an overflow in P + A would cause
565 undefined behavior, we can assume that there
566 is no overflow. */
567 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
568 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
569 /* For pointer types, if the conversion of A to the
570 final type requires a sign- or zero-extension,
571 then we have to punt - it is not defined which
572 one is correct. */
573 || (POINTER_TYPE_P (TREE_TYPE (@0))
574 && TREE_CODE (@1) == INTEGER_CST
575 && tree_int_cst_sign_bit (@1) == 0))
576 (convert @1))))))
577
578
579 /* Simplifications of MIN_EXPR and MAX_EXPR. */
580
581 (for minmax (min max)
582 (simplify
583 (minmax @0 @0)
584 @0))
585 (simplify
586 (min @0 @1)
587 (if (INTEGRAL_TYPE_P (type)
588 && TYPE_MIN_VALUE (type)
589 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
590 @1))
591 (simplify
592 (max @0 @1)
593 (if (INTEGRAL_TYPE_P (type)
594 && TYPE_MAX_VALUE (type)
595 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
596 @1))
597
598
599 /* Simplifications of shift and rotates. */
600
601 (for rotate (lrotate rrotate)
602 (simplify
603 (rotate integer_all_onesp@0 @1)
604 @0))
605
606 /* Optimize -1 >> x for arithmetic right shifts. */
607 (simplify
608 (rshift integer_all_onesp@0 @1)
609 (if (!TYPE_UNSIGNED (type)
610 && tree_expr_nonnegative_p (@1))
611 @0))
612
613 (for shiftrotate (lrotate rrotate lshift rshift)
614 (simplify
615 (shiftrotate @0 integer_zerop)
616 (non_lvalue @0))
617 (simplify
618 (shiftrotate integer_zerop@0 @1)
619 @0)
620 /* Prefer vector1 << scalar to vector1 << vector2
621 if vector2 is uniform. */
622 (for vec (VECTOR_CST CONSTRUCTOR)
623 (simplify
624 (shiftrotate @0 vec@1)
625 (with { tree tem = uniform_vector_p (@1); }
626 (if (tem)
627 (shiftrotate @0 { tem; }))))))
628
629 /* Rewrite an LROTATE_EXPR by a constant into an
630 RROTATE_EXPR by a new constant. */
631 (simplify
632 (lrotate @0 INTEGER_CST@1)
633 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
634 build_int_cst (TREE_TYPE (@1),
635 element_precision (type)), @1); }))
636
637 /* ((1 << A) & 1) != 0 -> A == 0
638 ((1 << A) & 1) == 0 -> A != 0 */
639 (for cmp (ne eq)
640 icmp (eq ne)
641 (simplify
642 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
643 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
644
645 /* Simplifications of conversions. */
646
647 /* Basic strip-useless-type-conversions / strip_nops. */
648 (for cvt (convert view_convert float fix_trunc)
649 (simplify
650 (cvt @0)
651 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
652 || (GENERIC && type == TREE_TYPE (@0)))
653 @0)))
654
655 /* Contract view-conversions. */
656 (simplify
657 (view_convert (view_convert @0))
658 (view_convert @0))
659
660 /* For integral conversions with the same precision or pointer
661 conversions use a NOP_EXPR instead. */
662 (simplify
663 (view_convert @0)
664 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
665 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
666 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
667 (convert @0)))
668
669 /* Strip inner integral conversions that do not change precision or size. */
670 (simplify
671 (view_convert (convert@0 @1))
672 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
673 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
674 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
675 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
676 (view_convert @1)))
677
678 /* Re-association barriers around constants and other re-association
679 barriers can be removed. */
680 (simplify
681 (paren CONSTANT_CLASS_P@0)
682 @0)
683 (simplify
684 (paren (paren@1 @0))
685 @1)
686
687 /* Handle cases of two conversions in a row. */
688 (for ocvt (convert float fix_trunc)
689 (for icvt (convert float)
690 (simplify
691 (ocvt (icvt@1 @0))
692 (with
693 {
694 tree inside_type = TREE_TYPE (@0);
695 tree inter_type = TREE_TYPE (@1);
696 int inside_int = INTEGRAL_TYPE_P (inside_type);
697 int inside_ptr = POINTER_TYPE_P (inside_type);
698 int inside_float = FLOAT_TYPE_P (inside_type);
699 int inside_vec = VECTOR_TYPE_P (inside_type);
700 unsigned int inside_prec = TYPE_PRECISION (inside_type);
701 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
702 int inter_int = INTEGRAL_TYPE_P (inter_type);
703 int inter_ptr = POINTER_TYPE_P (inter_type);
704 int inter_float = FLOAT_TYPE_P (inter_type);
705 int inter_vec = VECTOR_TYPE_P (inter_type);
706 unsigned int inter_prec = TYPE_PRECISION (inter_type);
707 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
708 int final_int = INTEGRAL_TYPE_P (type);
709 int final_ptr = POINTER_TYPE_P (type);
710 int final_float = FLOAT_TYPE_P (type);
711 int final_vec = VECTOR_TYPE_P (type);
712 unsigned int final_prec = TYPE_PRECISION (type);
713 int final_unsignedp = TYPE_UNSIGNED (type);
714 }
715 /* In addition to the cases of two conversions in a row
716 handled below, if we are converting something to its own
717 type via an object of identical or wider precision, neither
718 conversion is needed. */
719 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
720 || (GENERIC
721 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
722 && (((inter_int || inter_ptr) && final_int)
723 || (inter_float && final_float))
724 && inter_prec >= final_prec)
725 (ocvt @0))
726
727 /* Likewise, if the intermediate and initial types are either both
728 float or both integer, we don't need the middle conversion if the
729 former is wider than the latter and doesn't change the signedness
730 (for integers). Avoid this if the final type is a pointer since
731 then we sometimes need the middle conversion. Likewise if the
732 final type has a precision not equal to the size of its mode. */
733 (if (((inter_int && inside_int)
734 || (inter_float && inside_float)
735 || (inter_vec && inside_vec))
736 && inter_prec >= inside_prec
737 && (inter_float || inter_vec
738 || inter_unsignedp == inside_unsignedp)
739 && ! (final_prec != GET_MODE_PRECISION (element_mode (type))
740 && element_mode (type) == element_mode (inter_type))
741 && ! final_ptr
742 && (! final_vec || inter_prec == inside_prec))
743 (ocvt @0))
744
745 /* If we have a sign-extension of a zero-extended value, we can
746 replace that by a single zero-extension. Likewise if the
747 final conversion does not change precision we can drop the
748 intermediate conversion. */
749 (if (inside_int && inter_int && final_int
750 && ((inside_prec < inter_prec && inter_prec < final_prec
751 && inside_unsignedp && !inter_unsignedp)
752 || final_prec == inter_prec))
753 (ocvt @0))
754
755 /* Two conversions in a row are not needed unless:
756 - some conversion is floating-point (overstrict for now), or
757 - some conversion is a vector (overstrict for now), or
758 - the intermediate type is narrower than both initial and
759 final, or
760 - the intermediate type and innermost type differ in signedness,
761 and the outermost type is wider than the intermediate, or
762 - the initial type is a pointer type and the precisions of the
763 intermediate and final types differ, or
764 - the final type is a pointer type and the precisions of the
765 initial and intermediate types differ. */
766 (if (! inside_float && ! inter_float && ! final_float
767 && ! inside_vec && ! inter_vec && ! final_vec
768 && (inter_prec >= inside_prec || inter_prec >= final_prec)
769 && ! (inside_int && inter_int
770 && inter_unsignedp != inside_unsignedp
771 && inter_prec < final_prec)
772 && ((inter_unsignedp && inter_prec > inside_prec)
773 == (final_unsignedp && final_prec > inter_prec))
774 && ! (inside_ptr && inter_prec != final_prec)
775 && ! (final_ptr && inside_prec != inter_prec)
776 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
777 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
778 (ocvt @0))
779
780 /* A truncation to an unsigned type (a zero-extension) should be
781 canonicalized as bitwise and of a mask. */
782 (if (final_int && inter_int && inside_int
783 && final_prec == inside_prec
784 && final_prec > inter_prec
785 && inter_unsignedp)
786 (convert (bit_and @0 { wide_int_to_tree
787 (inside_type,
788 wi::mask (inter_prec, false,
789 TYPE_PRECISION (inside_type))); })))
790
791 /* If we are converting an integer to a floating-point that can
792 represent it exactly and back to an integer, we can skip the
793 floating-point conversion. */
794 (if (inside_int && inter_float && final_int &&
795 (unsigned) significand_size (TYPE_MODE (inter_type))
796 >= inside_prec - !inside_unsignedp)
797 (convert @0))))))
798
799 /* If we have a narrowing conversion to an integral type that is fed by a
800 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
801 masks off bits outside the final type (and nothing else). */
802 (simplify
803 (convert (bit_and @0 INTEGER_CST@1))
804 (if (INTEGRAL_TYPE_P (type)
805 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
806 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
807 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
808 TYPE_PRECISION (type)), 0))
809 (convert @0)))
810
811
812 /* (X /[ex] A) * A -> X. */
813 (simplify
814 (mult (convert? (exact_div @0 @1)) @1)
815 /* Look through a sign-changing conversion. */
816 (if (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
817 (convert @0)))
818
819 /* Canonicalization of binary operations. */
820
821 /* Convert X + -C into X - C. */
822 (simplify
823 (plus @0 REAL_CST@1)
824 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
825 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
826 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
827 (minus @0 { tem; })))))
828
829 /* Convert x+x into x*2.0. */
830 (simplify
831 (plus @0 @0)
832 (if (SCALAR_FLOAT_TYPE_P (type))
833 (mult @0 { build_real (type, dconst2); })))
834
835 (simplify
836 (minus integer_zerop @1)
837 (negate @1))
838
839 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
840 ARG0 is zero and X + ARG0 reduces to X, since that would mean
841 (-ARG1 + ARG0) reduces to -ARG1. */
842 (simplify
843 (minus real_zerop@0 @1)
844 (if (fold_real_zero_addition_p (type, @0, 0))
845 (negate @1)))
846
847 /* Transform x * -1 into -x. */
848 (simplify
849 (mult @0 integer_minus_onep)
850 (negate @0))
851
852 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
853 (simplify
854 (complex (realpart @0) (imagpart @0))
855 @0)
856 (simplify
857 (realpart (complex @0 @1))
858 @0)
859 (simplify
860 (imagpart (complex @0 @1))
861 @1)
862
863
864 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
865 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
866 (simplify
867 (bswap (bswap @0))
868 @0)
869 (simplify
870 (bswap (bit_not (bswap @0)))
871 (bit_not @0))
872 (for bitop (bit_xor bit_ior bit_and)
873 (simplify
874 (bswap (bitop:c (bswap @0) @1))
875 (bitop @0 (bswap @1)))))
876
877
878 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
879
880 /* Simplify constant conditions.
881 Only optimize constant conditions when the selected branch
882 has the same type as the COND_EXPR. This avoids optimizing
883 away "c ? x : throw", where the throw has a void type.
884 Note that we cannot throw away the fold-const.c variant nor
885 this one as we depend on doing this transform before possibly
886 A ? B : B -> B triggers and the fold-const.c one can optimize
887 0 ? A : B to B even if A has side-effects. Something
888 genmatch cannot handle. */
889 (simplify
890 (cond INTEGER_CST@0 @1 @2)
891 (if (integer_zerop (@0)
892 && (!VOID_TYPE_P (TREE_TYPE (@2))
893 || VOID_TYPE_P (type)))
894 @2)
895 (if (!integer_zerop (@0)
896 && (!VOID_TYPE_P (TREE_TYPE (@1))
897 || VOID_TYPE_P (type)))
898 @1))
899 (simplify
900 (vec_cond VECTOR_CST@0 @1 @2)
901 (if (integer_all_onesp (@0))
902 @1)
903 (if (integer_zerop (@0))
904 @2))
905
906 (for cnd (cond vec_cond)
907 /* A ? B : (A ? X : C) -> A ? B : C. */
908 (simplify
909 (cnd @0 (cnd @0 @1 @2) @3)
910 (cnd @0 @1 @3))
911 (simplify
912 (cnd @0 @1 (cnd @0 @2 @3))
913 (cnd @0 @1 @3))
914
915 /* A ? B : B -> B. */
916 (simplify
917 (cnd @0 @1 @1)
918 @1)
919
920 /* !A ? B : C -> A ? C : B. */
921 (simplify
922 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
923 (cnd @0 @2 @1)))
924
925
926 /* Simplifications of comparisons. */
927
928 /* We can simplify a logical negation of a comparison to the
929 inverted comparison. As we cannot compute an expression
930 operator using invert_tree_comparison we have to simulate
931 that with expression code iteration. */
932 (for cmp (tcc_comparison)
933 icmp (inverted_tcc_comparison)
934 ncmp (inverted_tcc_comparison_with_nans)
935 /* Ideally we'd like to combine the following two patterns
936 and handle some more cases by using
937 (logical_inverted_value (cmp @0 @1))
938 here but for that genmatch would need to "inline" that.
939 For now implement what forward_propagate_comparison did. */
940 (simplify
941 (bit_not (cmp @0 @1))
942 (if (VECTOR_TYPE_P (type)
943 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
944 /* Comparison inversion may be impossible for trapping math,
945 invert_tree_comparison will tell us. But we can't use
946 a computed operator in the replacement tree thus we have
947 to play the trick below. */
948 (with { enum tree_code ic = invert_tree_comparison
949 (cmp, HONOR_NANS (@0)); }
950 (if (ic == icmp)
951 (icmp @0 @1))
952 (if (ic == ncmp)
953 (ncmp @0 @1)))))
954 (simplify
955 (bit_xor (cmp @0 @1) integer_truep)
956 (with { enum tree_code ic = invert_tree_comparison
957 (cmp, HONOR_NANS (@0)); }
958 (if (ic == icmp)
959 (icmp @0 @1))
960 (if (ic == ncmp)
961 (ncmp @0 @1)))))
962
963 /* Unordered tests if either argument is a NaN. */
964 (simplify
965 (bit_ior (unordered @0 @0) (unordered @1 @1))
966 (if (types_match (@0, @1))
967 (unordered @0 @1)))
968 (simplify
969 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
970 @2)
971
972 /* Simplification of math builtins. */
973
974 (define_operator_list LOG BUILT_IN_LOGF BUILT_IN_LOG BUILT_IN_LOGL)
975 (define_operator_list EXP BUILT_IN_EXPF BUILT_IN_EXP BUILT_IN_EXPL)
976 (define_operator_list LOG2 BUILT_IN_LOG2F BUILT_IN_LOG2 BUILT_IN_LOG2L)
977 (define_operator_list EXP2 BUILT_IN_EXP2F BUILT_IN_EXP2 BUILT_IN_EXP2L)
978 (define_operator_list LOG10 BUILT_IN_LOG10F BUILT_IN_LOG10 BUILT_IN_LOG10L)
979 (define_operator_list EXP10 BUILT_IN_EXP10F BUILT_IN_EXP10 BUILT_IN_EXP10L)
980 (define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL)
981 (define_operator_list POW10 BUILT_IN_POW10F BUILT_IN_POW10 BUILT_IN_POW10L)
982 (define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL)
983 (define_operator_list CBRT BUILT_IN_CBRTF BUILT_IN_CBRT BUILT_IN_CBRTL)
984
985
986 /* fold_builtin_logarithm */
987 (if (flag_unsafe_math_optimizations)
988 /* Special case, optimize logN(expN(x)) = x. */
989 (for logs (LOG LOG2 LOG10)
990 exps (EXP EXP2 EXP10)
991 (simplify
992 (logs (exps @0))
993 @0))
994 /* Optimize logN(func()) for various exponential functions. We
995 want to determine the value "x" and the power "exponent" in
996 order to transform logN(x**exponent) into exponent*logN(x). */
997 (for logs (LOG LOG LOG LOG
998 LOG2 LOG2 LOG2 LOG2
999 LOG10 LOG10 LOG10 LOG10)
1000 exps (EXP EXP2 EXP10 POW10)
1001 (simplify
1002 (logs (exps @0))
1003 (with {
1004 tree x;
1005 switch (exps)
1006 {
1007 CASE_FLT_FN (BUILT_IN_EXP):
1008 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
1009 x = build_real (type, real_value_truncate (TYPE_MODE (type),
1010 dconst_e ()));
1011 break;
1012 CASE_FLT_FN (BUILT_IN_EXP2):
1013 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
1014 x = build_real (type, dconst2);
1015 break;
1016 CASE_FLT_FN (BUILT_IN_EXP10):
1017 CASE_FLT_FN (BUILT_IN_POW10):
1018 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
1019 {
1020 REAL_VALUE_TYPE dconst10;
1021 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
1022 x = build_real (type, dconst10);
1023 }
1024 break;
1025 }
1026 }
1027 (mult (logs { x; }) @0))))
1028 (for logs (LOG LOG
1029 LOG2 LOG2
1030 LOG10 LOG10)
1031 exps (SQRT CBRT)
1032 (simplify
1033 (logs (exps @0))
1034 (with {
1035 tree x;
1036 switch (exps)
1037 {
1038 CASE_FLT_FN (BUILT_IN_SQRT):
1039 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
1040 x = build_real (type, dconsthalf);
1041 break;
1042 CASE_FLT_FN (BUILT_IN_CBRT):
1043 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
1044 x = build_real (type, real_value_truncate (TYPE_MODE (type),
1045 dconst_third ()));
1046 break;
1047 }
1048 }
1049 (mult { x; } (logs @0)))))
1050 /* logN(pow(x,exponent) -> exponent*logN(x). */
1051 (for logs (LOG LOG2 LOG10)
1052 pows (POW)
1053 (simplify
1054 (logs (pows @0 @1))
1055 (mult @1 (logs @0)))))
1056
1057 /* Narrowing of arithmetic and logical operations.
1058
1059 These are conceptually similar to the transformations performed for
1060 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
1061 term we want to move all that code out of the front-ends into here. */
1062
1063 /* If we have a narrowing conversion of an arithmetic operation where
1064 both operands are widening conversions from the same type as the outer
1065 narrowing conversion. Then convert the innermost operands to a suitable
1066 unsigned type (to avoid introducing undefined behaviour), perform the
1067 operation and convert the result to the desired type. */
1068 (for op (plus minus)
1069 (simplify
1070 (convert (op@4 (convert@2 @0) (convert@3 @1)))
1071 (if (INTEGRAL_TYPE_P (type)
1072 /* We check for type compatibility between @0 and @1 below,
1073 so there's no need to check that @1/@3 are integral types. */
1074 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1075 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1076 /* The precision of the type of each operand must match the
1077 precision of the mode of each operand, similarly for the
1078 result. */
1079 && (TYPE_PRECISION (TREE_TYPE (@0))
1080 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
1081 && (TYPE_PRECISION (TREE_TYPE (@1))
1082 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
1083 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
1084 /* The inner conversion must be a widening conversion. */
1085 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
1086 && types_match (@0, @1)
1087 && types_match (@0, type)
1088 && single_use (@4))
1089 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1090 (convert (op @0 @1)))
1091 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
1092 (convert (op (convert:utype @0) (convert:utype @1)))))))
1093
1094 /* This is another case of narrowing, specifically when there's an outer
1095 BIT_AND_EXPR which masks off bits outside the type of the innermost
1096 operands. Like the previous case we have to convert the operands
1097 to unsigned types to avoid introducing undefined behaviour for the
1098 arithmetic operation. */
1099 (for op (minus plus)
1100 (simplify
1101 (bit_and (op@5 (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
1102 (if (INTEGRAL_TYPE_P (type)
1103 /* We check for type compatibility between @0 and @1 below,
1104 so there's no need to check that @1/@3 are integral types. */
1105 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1106 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1107 /* The precision of the type of each operand must match the
1108 precision of the mode of each operand, similarly for the
1109 result. */
1110 && (TYPE_PRECISION (TREE_TYPE (@0))
1111 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
1112 && (TYPE_PRECISION (TREE_TYPE (@1))
1113 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
1114 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
1115 /* The inner conversion must be a widening conversion. */
1116 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
1117 && types_match (@0, @1)
1118 && (tree_int_cst_min_precision (@4, UNSIGNED)
1119 <= TYPE_PRECISION (TREE_TYPE (@0)))
1120 && single_use (@5))
1121 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1122 (with { tree ntype = TREE_TYPE (@0); }
1123 (convert (bit_and (op @0 @1) (convert:ntype @4)))))
1124 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
1125 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
1126 (convert:utype @4)))))))
1127