re PR target/69894 (dependency of gcc-plugin.h not installed on aarch64-linux-gnu)
[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-2016 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 integer_nonzerop
30 real_zerop real_onep real_minus_onep
31 zerop
32 CONSTANT_CLASS_P
33 tree_expr_nonnegative_p
34 integer_valued_real_p
35 integer_pow2p
36 HONOR_NANS)
37
38 /* Operator lists. */
39 (define_operator_list tcc_comparison
40 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
41 (define_operator_list inverted_tcc_comparison
42 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
43 (define_operator_list inverted_tcc_comparison_with_nans
44 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
45 (define_operator_list swapped_tcc_comparison
46 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
47 (define_operator_list simple_comparison lt le eq ne ge gt)
48 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
49
50 #include "cfn-operators.pd"
51
52 /* Define operand lists for math rounding functions {,i,l,ll}FN,
53 where the versions prefixed with "i" return an int, those prefixed with
54 "l" return a long and those prefixed with "ll" return a long long.
55
56 Also define operand lists:
57
58 X<FN>F for all float functions, in the order i, l, ll
59 X<FN> for all double functions, in the same order
60 X<FN>L for all long double functions, in the same order. */
61 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
62 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
63 BUILT_IN_L##FN##F \
64 BUILT_IN_LL##FN##F) \
65 (define_operator_list X##FN BUILT_IN_I##FN \
66 BUILT_IN_L##FN \
67 BUILT_IN_LL##FN) \
68 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
69 BUILT_IN_L##FN##L \
70 BUILT_IN_LL##FN##L)
71
72 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
73 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
76
77 /* Simplifications of operations with one constant operand and
78 simplifications to constants or single values. */
79
80 (for op (plus pointer_plus minus bit_ior bit_xor)
81 (simplify
82 (op @0 integer_zerop)
83 (non_lvalue @0)))
84
85 /* 0 +p index -> (type)index */
86 (simplify
87 (pointer_plus integer_zerop @1)
88 (non_lvalue (convert @1)))
89
90 /* See if ARG1 is zero and X + ARG1 reduces to X.
91 Likewise if the operands are reversed. */
92 (simplify
93 (plus:c @0 real_zerop@1)
94 (if (fold_real_zero_addition_p (type, @1, 0))
95 (non_lvalue @0)))
96
97 /* See if ARG1 is zero and X - ARG1 reduces to X. */
98 (simplify
99 (minus @0 real_zerop@1)
100 (if (fold_real_zero_addition_p (type, @1, 1))
101 (non_lvalue @0)))
102
103 /* Simplify x - x.
104 This is unsafe for certain floats even in non-IEEE formats.
105 In IEEE, it is unsafe because it does wrong for NaNs.
106 Also note that operand_equal_p is always false if an operand
107 is volatile. */
108 (simplify
109 (minus @0 @0)
110 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
111 { build_zero_cst (type); }))
112
113 (simplify
114 (mult @0 integer_zerop@1)
115 @1)
116
117 /* Maybe fold x * 0 to 0. The expressions aren't the same
118 when x is NaN, since x * 0 is also NaN. Nor are they the
119 same in modes with signed zeros, since multiplying a
120 negative value by 0 gives -0, not +0. */
121 (simplify
122 (mult @0 real_zerop@1)
123 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
124 @1))
125
126 /* In IEEE floating point, x*1 is not equivalent to x for snans.
127 Likewise for complex arithmetic with signed zeros. */
128 (simplify
129 (mult @0 real_onep)
130 (if (!HONOR_SNANS (type)
131 && (!HONOR_SIGNED_ZEROS (type)
132 || !COMPLEX_FLOAT_TYPE_P (type)))
133 (non_lvalue @0)))
134
135 /* Transform x * -1.0 into -x. */
136 (simplify
137 (mult @0 real_minus_onep)
138 (if (!HONOR_SNANS (type)
139 && (!HONOR_SIGNED_ZEROS (type)
140 || !COMPLEX_FLOAT_TYPE_P (type)))
141 (negate @0)))
142
143 /* Make sure to preserve divisions by zero. This is the reason why
144 we don't simplify x / x to 1 or 0 / x to 0. */
145 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
146 (simplify
147 (op @0 integer_onep)
148 (non_lvalue @0)))
149
150 /* X / -1 is -X. */
151 (for div (trunc_div ceil_div floor_div round_div exact_div)
152 (simplify
153 (div @0 integer_minus_onep@1)
154 (if (!TYPE_UNSIGNED (type))
155 (negate @0))))
156
157 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
158 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
159 (simplify
160 (floor_div @0 @1)
161 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
162 && TYPE_UNSIGNED (type))
163 (trunc_div @0 @1)))
164
165 /* Combine two successive divisions. Note that combining ceil_div
166 and floor_div is trickier and combining round_div even more so. */
167 (for div (trunc_div exact_div)
168 (simplify
169 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
170 (with {
171 bool overflow_p;
172 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
173 }
174 (if (!overflow_p)
175 (div @0 { wide_int_to_tree (type, mul); })
176 (if (TYPE_UNSIGNED (type)
177 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
178 { build_zero_cst (type); })))))
179
180 /* Optimize A / A to 1.0 if we don't care about
181 NaNs or Infinities. */
182 (simplify
183 (rdiv @0 @0)
184 (if (FLOAT_TYPE_P (type)
185 && ! HONOR_NANS (type)
186 && ! HONOR_INFINITIES (type))
187 { build_one_cst (type); }))
188
189 /* Optimize -A / A to -1.0 if we don't care about
190 NaNs or Infinities. */
191 (simplify
192 (rdiv:c @0 (negate @0))
193 (if (FLOAT_TYPE_P (type)
194 && ! HONOR_NANS (type)
195 && ! HONOR_INFINITIES (type))
196 { build_minus_one_cst (type); }))
197
198 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
199 (simplify
200 (rdiv @0 real_onep)
201 (if (!HONOR_SNANS (type))
202 (non_lvalue @0)))
203
204 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
205 (simplify
206 (rdiv @0 real_minus_onep)
207 (if (!HONOR_SNANS (type))
208 (negate @0)))
209
210 (if (flag_reciprocal_math)
211 /* Convert (A/B)/C to A/(B*C) */
212 (simplify
213 (rdiv (rdiv:s @0 @1) @2)
214 (rdiv @0 (mult @1 @2)))
215
216 /* Convert A/(B/C) to (A/B)*C */
217 (simplify
218 (rdiv @0 (rdiv:s @1 @2))
219 (mult (rdiv @0 @1) @2)))
220
221 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
222 (for div (trunc_div ceil_div floor_div round_div exact_div)
223 (simplify
224 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
225 (if (integer_pow2p (@2)
226 && tree_int_cst_sgn (@2) > 0
227 && wi::add (@2, @1) == 0
228 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
229 (rshift (convert @0) { build_int_cst (integer_type_node,
230 wi::exact_log2 (@2)); }))))
231
232 /* If ARG1 is a constant, we can convert this to a multiply by the
233 reciprocal. This does not have the same rounding properties,
234 so only do this if -freciprocal-math. We can actually
235 always safely do it if ARG1 is a power of two, but it's hard to
236 tell if it is or not in a portable manner. */
237 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
238 (simplify
239 (rdiv @0 cst@1)
240 (if (optimize)
241 (if (flag_reciprocal_math
242 && !real_zerop (@1))
243 (with
244 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
245 (if (tem)
246 (mult @0 { tem; } )))
247 (if (cst != COMPLEX_CST)
248 (with { tree inverse = exact_inverse (type, @1); }
249 (if (inverse)
250 (mult @0 { inverse; } ))))))))
251
252 /* Same applies to modulo operations, but fold is inconsistent here
253 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
254 (for mod (ceil_mod floor_mod round_mod trunc_mod)
255 /* 0 % X is always zero. */
256 (simplify
257 (mod integer_zerop@0 @1)
258 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
259 (if (!integer_zerop (@1))
260 @0))
261 /* X % 1 is always zero. */
262 (simplify
263 (mod @0 integer_onep)
264 { build_zero_cst (type); })
265 /* X % -1 is zero. */
266 (simplify
267 (mod @0 integer_minus_onep@1)
268 (if (!TYPE_UNSIGNED (type))
269 { build_zero_cst (type); }))
270 /* (X % Y) % Y is just X % Y. */
271 (simplify
272 (mod (mod@2 @0 @1) @1)
273 @2)
274 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
275 (simplify
276 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
277 (if (ANY_INTEGRAL_TYPE_P (type)
278 && TYPE_OVERFLOW_UNDEFINED (type)
279 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
280 { build_zero_cst (type); })))
281
282 /* X % -C is the same as X % C. */
283 (simplify
284 (trunc_mod @0 INTEGER_CST@1)
285 (if (TYPE_SIGN (type) == SIGNED
286 && !TREE_OVERFLOW (@1)
287 && wi::neg_p (@1)
288 && !TYPE_OVERFLOW_TRAPS (type)
289 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
290 && !sign_bit_p (@1, @1))
291 (trunc_mod @0 (negate @1))))
292
293 /* X % -Y is the same as X % Y. */
294 (simplify
295 (trunc_mod @0 (convert? (negate @1)))
296 (if (!TYPE_UNSIGNED (type)
297 && !TYPE_OVERFLOW_TRAPS (type)
298 && tree_nop_conversion_p (type, TREE_TYPE (@1))
299 /* Avoid this transformation if X might be INT_MIN or
300 Y might be -1, because we would then change valid
301 INT_MIN % -(-1) into invalid INT_MIN % -1. */
302 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
303 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
304 (TREE_TYPE (@1))))))
305 (trunc_mod @0 (convert @1))))
306
307 /* X - (X / Y) * Y is the same as X % Y. */
308 (simplify
309 (minus (convert1? @2) (convert2? (mult:c (trunc_div @0 @1) @1)))
310 /* We cannot use matching captures here, since in the case of
311 constants we really want the type of @0, not @2. */
312 (if (operand_equal_p (@0, @2, 0)
313 && (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)))
314 (convert (trunc_mod @0 @1))))
315
316 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
317 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
318 Also optimize A % (C << N) where C is a power of 2,
319 to A & ((C << N) - 1). */
320 (match (power_of_two_cand @1)
321 INTEGER_CST@1)
322 (match (power_of_two_cand @1)
323 (lshift INTEGER_CST@1 @2))
324 (for mod (trunc_mod floor_mod)
325 (simplify
326 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
327 (if ((TYPE_UNSIGNED (type)
328 || tree_expr_nonnegative_p (@0))
329 && tree_nop_conversion_p (type, TREE_TYPE (@3))
330 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
331 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
332
333 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
334 (simplify
335 (trunc_div (mult @0 integer_pow2p@1) @1)
336 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
337 (bit_and @0 { wide_int_to_tree
338 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
339 false, TYPE_PRECISION (type))); })))
340
341 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
342 (simplify
343 (mult (trunc_div @0 integer_pow2p@1) @1)
344 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
345 (bit_and @0 (negate @1))))
346
347 /* Simplify (t * 2) / 2) -> t. */
348 (for div (trunc_div ceil_div floor_div round_div exact_div)
349 (simplify
350 (div (mult @0 @1) @1)
351 (if (ANY_INTEGRAL_TYPE_P (type)
352 && TYPE_OVERFLOW_UNDEFINED (type))
353 @0)))
354
355 (for op (negate abs)
356 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
357 (for coss (COS COSH)
358 (simplify
359 (coss (op @0))
360 (coss @0)))
361 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
362 (for pows (POW)
363 (simplify
364 (pows (op @0) REAL_CST@1)
365 (with { HOST_WIDE_INT n; }
366 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
367 (pows @0 @1)))))
368 /* Strip negate and abs from both operands of hypot. */
369 (for hypots (HYPOT)
370 (simplify
371 (hypots (op @0) @1)
372 (hypots @0 @1))
373 (simplify
374 (hypots @0 (op @1))
375 (hypots @0 @1)))
376 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
377 (for copysigns (COPYSIGN)
378 (simplify
379 (copysigns (op @0) @1)
380 (copysigns @0 @1))))
381
382 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
383 (simplify
384 (mult (abs@1 @0) @1)
385 (mult @0 @0))
386
387 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
388 (for coss (COS COSH)
389 copysigns (COPYSIGN)
390 (simplify
391 (coss (copysigns @0 @1))
392 (coss @0)))
393
394 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
395 (for pows (POW)
396 copysigns (COPYSIGN)
397 (simplify
398 (pows (copysigns @0 @1) REAL_CST@1)
399 (with { HOST_WIDE_INT n; }
400 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
401 (pows @0 @1)))))
402
403 (for hypots (HYPOT)
404 copysigns (COPYSIGN)
405 /* hypot(copysign(x, y), z) -> hypot(x, z). */
406 (simplify
407 (hypots (copysigns @0 @1) @2)
408 (hypots @0 @2))
409 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
410 (simplify
411 (hypots @0 (copysigns @1 @2))
412 (hypots @0 @1)))
413
414 /* copysign(copysign(x, y), z) -> copysign(x, z). */
415 (for copysigns (COPYSIGN)
416 (simplify
417 (copysigns (copysigns @0 @1) @2)
418 (copysigns @0 @2)))
419
420 /* copysign(x,y)*copysign(x,y) -> x*x. */
421 (for copysigns (COPYSIGN)
422 (simplify
423 (mult (copysigns@2 @0 @1) @2)
424 (mult @0 @0)))
425
426 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
427 (for ccoss (CCOS CCOSH)
428 (simplify
429 (ccoss (negate @0))
430 (ccoss @0)))
431
432 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
433 (for ops (conj negate)
434 (for cabss (CABS)
435 (simplify
436 (cabss (ops @0))
437 (cabss @0))))
438
439 /* Fold (a * (1 << b)) into (a << b) */
440 (simplify
441 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
442 (if (! FLOAT_TYPE_P (type)
443 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
444 (lshift @0 @2)))
445
446 /* Fold (C1/X)*C2 into (C1*C2)/X. */
447 (simplify
448 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
449 (if (flag_associative_math
450 && single_use (@3))
451 (with
452 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
453 (if (tem)
454 (rdiv { tem; } @1)))))
455
456 /* Convert C1/(X*C2) into (C1/C2)/X */
457 (simplify
458 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
459 (if (flag_reciprocal_math)
460 (with
461 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
462 (if (tem)
463 (rdiv { tem; } @1)))))
464
465 /* Simplify ~X & X as zero. */
466 (simplify
467 (bit_and:c (convert? @0) (convert? (bit_not @0)))
468 { build_zero_cst (type); })
469
470 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
471 (simplify
472 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
473 (minus (bit_xor @0 @1) @1))
474 (simplify
475 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
476 (if (wi::bit_not (@2) == @1)
477 (minus (bit_xor @0 @1) @1)))
478
479 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
480 (simplify
481 (minus (bit_and:s @0 @1) (bit_and:cs @0 (bit_not @1)))
482 (minus @1 (bit_xor @0 @1)))
483
484 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
485 (simplify
486 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
487 (bit_xor @0 @1))
488 (simplify
489 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
490 (if (wi::bit_not (@2) == @1)
491 (bit_xor @0 @1)))
492
493 /* X % Y is smaller than Y. */
494 (for cmp (lt ge)
495 (simplify
496 (cmp (trunc_mod @0 @1) @1)
497 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
498 { constant_boolean_node (cmp == LT_EXPR, type); })))
499 (for cmp (gt le)
500 (simplify
501 (cmp @1 (trunc_mod @0 @1))
502 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
503 { constant_boolean_node (cmp == GT_EXPR, type); })))
504
505 /* x | ~0 -> ~0 */
506 (simplify
507 (bit_ior @0 integer_all_onesp@1)
508 @1)
509
510 /* x & 0 -> 0 */
511 (simplify
512 (bit_and @0 integer_zerop@1)
513 @1)
514
515 /* ~x | x -> -1 */
516 /* ~x ^ x -> -1 */
517 /* ~x + x -> -1 */
518 (for op (bit_ior bit_xor plus)
519 (simplify
520 (op:c (convert? @0) (convert? (bit_not @0)))
521 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
522
523 /* x ^ x -> 0 */
524 (simplify
525 (bit_xor @0 @0)
526 { build_zero_cst (type); })
527
528 /* Canonicalize X ^ ~0 to ~X. */
529 (simplify
530 (bit_xor @0 integer_all_onesp@1)
531 (bit_not @0))
532
533 /* x & ~0 -> x */
534 (simplify
535 (bit_and @0 integer_all_onesp)
536 (non_lvalue @0))
537
538 /* x & x -> x, x | x -> x */
539 (for bitop (bit_and bit_ior)
540 (simplify
541 (bitop @0 @0)
542 (non_lvalue @0)))
543
544 /* x + (x & 1) -> (x + 1) & ~1 */
545 (simplify
546 (plus:c @0 (bit_and:s @0 integer_onep@1))
547 (bit_and (plus @0 @1) (bit_not @1)))
548
549 /* x & ~(x & y) -> x & ~y */
550 /* x | ~(x | y) -> x | ~y */
551 (for bitop (bit_and bit_ior)
552 (simplify
553 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
554 (bitop @0 (bit_not @1))))
555
556 /* (x | y) & ~x -> y & ~x */
557 /* (x & y) | ~x -> y | ~x */
558 (for bitop (bit_and bit_ior)
559 rbitop (bit_ior bit_and)
560 (simplify
561 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
562 (bitop @1 @2)))
563
564 /* (x & y) ^ (x | y) -> x ^ y */
565 (simplify
566 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
567 (bit_xor @0 @1))
568
569 /* (x ^ y) ^ (x | y) -> x & y */
570 (simplify
571 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
572 (bit_and @0 @1))
573
574 /* (x & y) + (x ^ y) -> x | y */
575 /* (x & y) | (x ^ y) -> x | y */
576 /* (x & y) ^ (x ^ y) -> x | y */
577 (for op (plus bit_ior bit_xor)
578 (simplify
579 (op:c (bit_and @0 @1) (bit_xor @0 @1))
580 (bit_ior @0 @1)))
581
582 /* (x & y) + (x | y) -> x + y */
583 (simplify
584 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
585 (plus @0 @1))
586
587 /* (x + y) - (x | y) -> x & y */
588 (simplify
589 (minus (plus @0 @1) (bit_ior @0 @1))
590 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
591 && !TYPE_SATURATING (type))
592 (bit_and @0 @1)))
593
594 /* (x + y) - (x & y) -> x | y */
595 (simplify
596 (minus (plus @0 @1) (bit_and @0 @1))
597 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
598 && !TYPE_SATURATING (type))
599 (bit_ior @0 @1)))
600
601 /* (x | y) - (x ^ y) -> x & y */
602 (simplify
603 (minus (bit_ior @0 @1) (bit_xor @0 @1))
604 (bit_and @0 @1))
605
606 /* (x | y) - (x & y) -> x ^ y */
607 (simplify
608 (minus (bit_ior @0 @1) (bit_and @0 @1))
609 (bit_xor @0 @1))
610
611 /* (x | y) & ~(x & y) -> x ^ y */
612 (simplify
613 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
614 (bit_xor @0 @1))
615
616 /* (x | y) & (~x ^ y) -> x & y */
617 (simplify
618 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
619 (bit_and @0 @1))
620
621 /* ~x & ~y -> ~(x | y)
622 ~x | ~y -> ~(x & y) */
623 (for op (bit_and bit_ior)
624 rop (bit_ior bit_and)
625 (simplify
626 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
627 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
628 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
629 (bit_not (rop (convert @0) (convert @1))))))
630
631 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
632 with a constant, and the two constants have no bits in common,
633 we should treat this as a BIT_IOR_EXPR since this may produce more
634 simplifications. */
635 (for op (bit_xor plus)
636 (simplify
637 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
638 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
639 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
640 && tree_nop_conversion_p (type, TREE_TYPE (@2))
641 && wi::bit_and (@1, @3) == 0)
642 (bit_ior (convert @4) (convert @5)))))
643
644 /* (X | Y) ^ X -> Y & ~ X*/
645 (simplify
646 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
647 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
648 (convert (bit_and @1 (bit_not @0)))))
649
650 /* Convert ~X ^ ~Y to X ^ Y. */
651 (simplify
652 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
653 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
654 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
655 (bit_xor (convert @0) (convert @1))))
656
657 /* Convert ~X ^ C to X ^ ~C. */
658 (simplify
659 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
660 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
661 (bit_xor (convert @0) (bit_not @1))))
662
663 /* Fold (X & Y) ^ Y as ~X & Y. */
664 (simplify
665 (bit_xor:c (bit_and:c @0 @1) @1)
666 (bit_and (bit_not @0) @1))
667
668 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
669 operands are another bit-wise operation with a common input. If so,
670 distribute the bit operations to save an operation and possibly two if
671 constants are involved. For example, convert
672 (A | B) & (A | C) into A | (B & C)
673 Further simplification will occur if B and C are constants. */
674 (for op (bit_and bit_ior)
675 rop (bit_ior bit_and)
676 (simplify
677 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
678 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
679 (rop (convert @0) (op (convert @1) (convert @2))))))
680
681
682 (simplify
683 (abs (abs@1 @0))
684 @1)
685 (simplify
686 (abs (negate @0))
687 (abs @0))
688 (simplify
689 (abs tree_expr_nonnegative_p@0)
690 @0)
691
692 /* A few cases of fold-const.c negate_expr_p predicate. */
693 (match negate_expr_p
694 INTEGER_CST
695 (if ((INTEGRAL_TYPE_P (type)
696 && TYPE_OVERFLOW_WRAPS (type))
697 || (!TYPE_OVERFLOW_SANITIZED (type)
698 && may_negate_without_overflow_p (t)))))
699 (match negate_expr_p
700 FIXED_CST)
701 (match negate_expr_p
702 (negate @0)
703 (if (!TYPE_OVERFLOW_SANITIZED (type))))
704 (match negate_expr_p
705 REAL_CST
706 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
707 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
708 ways. */
709 (match negate_expr_p
710 VECTOR_CST
711 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
712
713 /* (-A) * (-B) -> A * B */
714 (simplify
715 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
716 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
717 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
718 (mult (convert @0) (convert (negate @1)))))
719
720 /* -(A + B) -> (-B) - A. */
721 (simplify
722 (negate (plus:c @0 negate_expr_p@1))
723 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
724 && !HONOR_SIGNED_ZEROS (element_mode (type)))
725 (minus (negate @1) @0)))
726
727 /* A - B -> A + (-B) if B is easily negatable. */
728 (simplify
729 (minus @0 negate_expr_p@1)
730 (if (!FIXED_POINT_TYPE_P (type))
731 (plus @0 (negate @1))))
732
733 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
734 when profitable.
735 For bitwise binary operations apply operand conversions to the
736 binary operation result instead of to the operands. This allows
737 to combine successive conversions and bitwise binary operations.
738 We combine the above two cases by using a conditional convert. */
739 (for bitop (bit_and bit_ior bit_xor)
740 (simplify
741 (bitop (convert @0) (convert? @1))
742 (if (((TREE_CODE (@1) == INTEGER_CST
743 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
744 && int_fits_type_p (@1, TREE_TYPE (@0)))
745 || types_match (@0, @1))
746 /* ??? This transform conflicts with fold-const.c doing
747 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
748 constants (if x has signed type, the sign bit cannot be set
749 in c). This folds extension into the BIT_AND_EXPR.
750 Restrict it to GIMPLE to avoid endless recursions. */
751 && (bitop != BIT_AND_EXPR || GIMPLE)
752 && (/* That's a good idea if the conversion widens the operand, thus
753 after hoisting the conversion the operation will be narrower. */
754 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
755 /* It's also a good idea if the conversion is to a non-integer
756 mode. */
757 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
758 /* Or if the precision of TO is not the same as the precision
759 of its mode. */
760 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
761 (convert (bitop @0 (convert @1))))))
762
763 (for bitop (bit_and bit_ior)
764 rbitop (bit_ior bit_and)
765 /* (x | y) & x -> x */
766 /* (x & y) | x -> x */
767 (simplify
768 (bitop:c (rbitop:c @0 @1) @0)
769 @0)
770 /* (~x | y) & x -> x & y */
771 /* (~x & y) | x -> x | y */
772 (simplify
773 (bitop:c (rbitop:c (bit_not @0) @1) @0)
774 (bitop @0 @1)))
775
776 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
777 (for bitop (bit_and bit_ior bit_xor)
778 (simplify
779 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
780 (bit_and (bitop @0 @2) @1)))
781
782 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
783 (simplify
784 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
785 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
786
787 /* Combine successive equal operations with constants. */
788 (for bitop (bit_and bit_ior bit_xor)
789 (simplify
790 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
791 (bitop @0 (bitop @1 @2))))
792
793 /* Try simple folding for X op !X, and X op X with the help
794 of the truth_valued_p and logical_inverted_value predicates. */
795 (match truth_valued_p
796 @0
797 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
798 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
799 (match truth_valued_p
800 (op @0 @1)))
801 (match truth_valued_p
802 (truth_not @0))
803
804 (match (logical_inverted_value @0)
805 (truth_not @0))
806 (match (logical_inverted_value @0)
807 (bit_not truth_valued_p@0))
808 (match (logical_inverted_value @0)
809 (eq @0 integer_zerop))
810 (match (logical_inverted_value @0)
811 (ne truth_valued_p@0 integer_truep))
812 (match (logical_inverted_value @0)
813 (bit_xor truth_valued_p@0 integer_truep))
814
815 /* X & !X -> 0. */
816 (simplify
817 (bit_and:c @0 (logical_inverted_value @0))
818 { build_zero_cst (type); })
819 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
820 (for op (bit_ior bit_xor)
821 (simplify
822 (op:c truth_valued_p@0 (logical_inverted_value @0))
823 { constant_boolean_node (true, type); }))
824 /* X ==/!= !X is false/true. */
825 (for op (eq ne)
826 (simplify
827 (op:c truth_valued_p@0 (logical_inverted_value @0))
828 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
829
830 /* If arg1 and arg2 are booleans (or any single bit type)
831 then try to simplify:
832
833 (~X & Y) -> X < Y
834 (X & ~Y) -> Y < X
835 (~X | Y) -> X <= Y
836 (X | ~Y) -> Y <= X
837
838 But only do this if our result feeds into a comparison as
839 this transformation is not always a win, particularly on
840 targets with and-not instructions.
841 -> simplify_bitwise_binary_boolean */
842 (simplify
843 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
844 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
845 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
846 (lt @0 @1)))
847 (simplify
848 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
849 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
850 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
851 (le @0 @1)))
852
853 /* ~~x -> x */
854 (simplify
855 (bit_not (bit_not @0))
856 @0)
857
858 /* Convert ~ (-A) to A - 1. */
859 (simplify
860 (bit_not (convert? (negate @0)))
861 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
862 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
863
864 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
865 (simplify
866 (bit_not (convert? (minus @0 integer_each_onep)))
867 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
868 (convert (negate @0))))
869 (simplify
870 (bit_not (convert? (plus @0 integer_all_onesp)))
871 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
872 (convert (negate @0))))
873
874 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
875 (simplify
876 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
877 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
878 (convert (bit_xor @0 (bit_not @1)))))
879 (simplify
880 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
881 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
882 (convert (bit_xor @0 @1))))
883
884 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
885 (simplify
886 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
887 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
888
889 /* Fold A - (A & B) into ~B & A. */
890 (simplify
891 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
892 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
893 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
894 (convert (bit_and (bit_not @1) @0))))
895
896
897
898 /* ((X inner_op C0) outer_op C1)
899 With X being a tree where value_range has reasoned certain bits to always be
900 zero throughout its computed value range,
901 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
902 where zero_mask has 1's for all bits that are sure to be 0 in
903 and 0's otherwise.
904 if (inner_op == '^') C0 &= ~C1;
905 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
906 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
907 */
908 (for inner_op (bit_ior bit_xor)
909 outer_op (bit_xor bit_ior)
910 (simplify
911 (outer_op
912 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
913 (with
914 {
915 bool fail = false;
916 wide_int zero_mask_not;
917 wide_int C0;
918 wide_int cst_emit;
919
920 if (TREE_CODE (@2) == SSA_NAME)
921 zero_mask_not = get_nonzero_bits (@2);
922 else
923 fail = true;
924
925 if (inner_op == BIT_XOR_EXPR)
926 {
927 C0 = wi::bit_and_not (@0, @1);
928 cst_emit = wi::bit_or (C0, @1);
929 }
930 else
931 {
932 C0 = @0;
933 cst_emit = wi::bit_xor (@0, @1);
934 }
935 }
936 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
937 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
938 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
939 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
940
941 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
942 (simplify
943 (pointer_plus (pointer_plus:s @0 @1) @3)
944 (pointer_plus @0 (plus @1 @3)))
945
946 /* Pattern match
947 tem1 = (long) ptr1;
948 tem2 = (long) ptr2;
949 tem3 = tem2 - tem1;
950 tem4 = (unsigned long) tem3;
951 tem5 = ptr1 + tem4;
952 and produce
953 tem5 = ptr2; */
954 (simplify
955 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
956 /* Conditionally look through a sign-changing conversion. */
957 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
958 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
959 || (GENERIC && type == TREE_TYPE (@1))))
960 @1))
961
962 /* Pattern match
963 tem = (sizetype) ptr;
964 tem = tem & algn;
965 tem = -tem;
966 ... = ptr p+ tem;
967 and produce the simpler and easier to analyze with respect to alignment
968 ... = ptr & ~algn; */
969 (simplify
970 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
971 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
972 (bit_and @0 { algn; })))
973
974 /* Try folding difference of addresses. */
975 (simplify
976 (minus (convert ADDR_EXPR@0) (convert @1))
977 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
978 (with { HOST_WIDE_INT diff; }
979 (if (ptr_difference_const (@0, @1, &diff))
980 { build_int_cst_type (type, diff); }))))
981 (simplify
982 (minus (convert @0) (convert ADDR_EXPR@1))
983 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
984 (with { HOST_WIDE_INT diff; }
985 (if (ptr_difference_const (@0, @1, &diff))
986 { build_int_cst_type (type, diff); }))))
987
988 /* If arg0 is derived from the address of an object or function, we may
989 be able to fold this expression using the object or function's
990 alignment. */
991 (simplify
992 (bit_and (convert? @0) INTEGER_CST@1)
993 (if (POINTER_TYPE_P (TREE_TYPE (@0))
994 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
995 (with
996 {
997 unsigned int align;
998 unsigned HOST_WIDE_INT bitpos;
999 get_pointer_alignment_1 (@0, &align, &bitpos);
1000 }
1001 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1002 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1003
1004
1005 /* We can't reassociate at all for saturating types. */
1006 (if (!TYPE_SATURATING (type))
1007
1008 /* Contract negates. */
1009 /* A + (-B) -> A - B */
1010 (simplify
1011 (plus:c (convert1? @0) (convert2? (negate @1)))
1012 /* Apply STRIP_NOPS on @0 and the negate. */
1013 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1014 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1015 && !TYPE_OVERFLOW_SANITIZED (type))
1016 (minus (convert @0) (convert @1))))
1017 /* A - (-B) -> A + B */
1018 (simplify
1019 (minus (convert1? @0) (convert2? (negate @1)))
1020 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1021 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1022 && !TYPE_OVERFLOW_SANITIZED (type))
1023 (plus (convert @0) (convert @1))))
1024 /* -(-A) -> A */
1025 (simplify
1026 (negate (convert? (negate @1)))
1027 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1028 && !TYPE_OVERFLOW_SANITIZED (type))
1029 (convert @1)))
1030
1031 /* We can't reassociate floating-point unless -fassociative-math
1032 or fixed-point plus or minus because of saturation to +-Inf. */
1033 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1034 && !FIXED_POINT_TYPE_P (type))
1035
1036 /* Match patterns that allow contracting a plus-minus pair
1037 irrespective of overflow issues. */
1038 /* (A +- B) - A -> +- B */
1039 /* (A +- B) -+ B -> A */
1040 /* A - (A +- B) -> -+ B */
1041 /* A +- (B -+ A) -> +- B */
1042 (simplify
1043 (minus (plus:c @0 @1) @0)
1044 @1)
1045 (simplify
1046 (minus (minus @0 @1) @0)
1047 (negate @1))
1048 (simplify
1049 (plus:c (minus @0 @1) @1)
1050 @0)
1051 (simplify
1052 (minus @0 (plus:c @0 @1))
1053 (negate @1))
1054 (simplify
1055 (minus @0 (minus @0 @1))
1056 @1)
1057
1058 /* (A +- CST) +- CST -> A + CST */
1059 (for outer_op (plus minus)
1060 (for inner_op (plus minus)
1061 (simplify
1062 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1063 /* If the constant operation overflows we cannot do the transform
1064 as we would introduce undefined overflow, for example
1065 with (a - 1) + INT_MIN. */
1066 (with { tree cst = const_binop (outer_op == inner_op
1067 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1068 (if (cst && !TREE_OVERFLOW (cst))
1069 (inner_op @0 { cst; } ))))))
1070
1071 /* (CST - A) +- CST -> CST - A */
1072 (for outer_op (plus minus)
1073 (simplify
1074 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1075 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1076 (if (cst && !TREE_OVERFLOW (cst))
1077 (minus { cst; } @0)))))
1078
1079 /* ~A + A -> -1 */
1080 (simplify
1081 (plus:c (bit_not @0) @0)
1082 (if (!TYPE_OVERFLOW_TRAPS (type))
1083 { build_all_ones_cst (type); }))
1084
1085 /* ~A + 1 -> -A */
1086 (simplify
1087 (plus (convert? (bit_not @0)) integer_each_onep)
1088 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1089 (negate (convert @0))))
1090
1091 /* -A - 1 -> ~A */
1092 (simplify
1093 (minus (convert? (negate @0)) integer_each_onep)
1094 (if (!TYPE_OVERFLOW_TRAPS (type)
1095 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1096 (bit_not (convert @0))))
1097
1098 /* -1 - A -> ~A */
1099 (simplify
1100 (minus integer_all_onesp @0)
1101 (bit_not @0))
1102
1103 /* (T)(P + A) - (T)P -> (T) A */
1104 (for add (plus pointer_plus)
1105 (simplify
1106 (minus (convert (add @0 @1))
1107 (convert @0))
1108 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1109 /* For integer types, if A has a smaller type
1110 than T the result depends on the possible
1111 overflow in P + A.
1112 E.g. T=size_t, A=(unsigned)429497295, P>0.
1113 However, if an overflow in P + A would cause
1114 undefined behavior, we can assume that there
1115 is no overflow. */
1116 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1117 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1118 /* For pointer types, if the conversion of A to the
1119 final type requires a sign- or zero-extension,
1120 then we have to punt - it is not defined which
1121 one is correct. */
1122 || (POINTER_TYPE_P (TREE_TYPE (@0))
1123 && TREE_CODE (@1) == INTEGER_CST
1124 && tree_int_cst_sign_bit (@1) == 0))
1125 (convert @1))))
1126
1127 /* (T)P - (T)(P + A) -> -(T) A */
1128 (for add (plus pointer_plus)
1129 (simplify
1130 (minus (convert @0)
1131 (convert (add @0 @1)))
1132 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1133 /* For integer types, if A has a smaller type
1134 than T the result depends on the possible
1135 overflow in P + A.
1136 E.g. T=size_t, A=(unsigned)429497295, P>0.
1137 However, if an overflow in P + A would cause
1138 undefined behavior, we can assume that there
1139 is no overflow. */
1140 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1141 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1142 /* For pointer types, if the conversion of A to the
1143 final type requires a sign- or zero-extension,
1144 then we have to punt - it is not defined which
1145 one is correct. */
1146 || (POINTER_TYPE_P (TREE_TYPE (@0))
1147 && TREE_CODE (@1) == INTEGER_CST
1148 && tree_int_cst_sign_bit (@1) == 0))
1149 (negate (convert @1)))))
1150
1151 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1152 (for add (plus pointer_plus)
1153 (simplify
1154 (minus (convert (add @0 @1))
1155 (convert (add @0 @2)))
1156 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1157 /* For integer types, if A has a smaller type
1158 than T the result depends on the possible
1159 overflow in P + A.
1160 E.g. T=size_t, A=(unsigned)429497295, P>0.
1161 However, if an overflow in P + A would cause
1162 undefined behavior, we can assume that there
1163 is no overflow. */
1164 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1165 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1166 /* For pointer types, if the conversion of A to the
1167 final type requires a sign- or zero-extension,
1168 then we have to punt - it is not defined which
1169 one is correct. */
1170 || (POINTER_TYPE_P (TREE_TYPE (@0))
1171 && TREE_CODE (@1) == INTEGER_CST
1172 && tree_int_cst_sign_bit (@1) == 0
1173 && TREE_CODE (@2) == INTEGER_CST
1174 && tree_int_cst_sign_bit (@2) == 0))
1175 (minus (convert @1) (convert @2)))))))
1176
1177
1178 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1179
1180 (for minmax (min max FMIN FMAX)
1181 (simplify
1182 (minmax @0 @0)
1183 @0))
1184 /* min(max(x,y),y) -> y. */
1185 (simplify
1186 (min:c (max:c @0 @1) @1)
1187 @1)
1188 /* max(min(x,y),y) -> y. */
1189 (simplify
1190 (max:c (min:c @0 @1) @1)
1191 @1)
1192 (simplify
1193 (min @0 @1)
1194 (if (INTEGRAL_TYPE_P (type)
1195 && TYPE_MIN_VALUE (type)
1196 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1197 @1))
1198 (simplify
1199 (max @0 @1)
1200 (if (INTEGRAL_TYPE_P (type)
1201 && TYPE_MAX_VALUE (type)
1202 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1203 @1))
1204 (for minmax (FMIN FMAX)
1205 /* If either argument is NaN, return the other one. Avoid the
1206 transformation if we get (and honor) a signalling NaN. */
1207 (simplify
1208 (minmax:c @0 REAL_CST@1)
1209 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1210 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1211 @0)))
1212 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1213 functions to return the numeric arg if the other one is NaN.
1214 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1215 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1216 worry about it either. */
1217 (if (flag_finite_math_only)
1218 (simplify
1219 (FMIN @0 @1)
1220 (min @0 @1))
1221 (simplify
1222 (FMAX @0 @1)
1223 (max @0 @1)))
1224
1225 /* Simplifications of shift and rotates. */
1226
1227 (for rotate (lrotate rrotate)
1228 (simplify
1229 (rotate integer_all_onesp@0 @1)
1230 @0))
1231
1232 /* Optimize -1 >> x for arithmetic right shifts. */
1233 (simplify
1234 (rshift integer_all_onesp@0 @1)
1235 (if (!TYPE_UNSIGNED (type)
1236 && tree_expr_nonnegative_p (@1))
1237 @0))
1238
1239 /* Optimize (x >> c) << c into x & (-1<<c). */
1240 (simplify
1241 (lshift (rshift @0 INTEGER_CST@1) @1)
1242 (if (wi::ltu_p (@1, element_precision (type)))
1243 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1244
1245 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1246 types. */
1247 (simplify
1248 (rshift (lshift @0 INTEGER_CST@1) @1)
1249 (if (TYPE_UNSIGNED (type)
1250 && (wi::ltu_p (@1, element_precision (type))))
1251 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1252
1253 (for shiftrotate (lrotate rrotate lshift rshift)
1254 (simplify
1255 (shiftrotate @0 integer_zerop)
1256 (non_lvalue @0))
1257 (simplify
1258 (shiftrotate integer_zerop@0 @1)
1259 @0)
1260 /* Prefer vector1 << scalar to vector1 << vector2
1261 if vector2 is uniform. */
1262 (for vec (VECTOR_CST CONSTRUCTOR)
1263 (simplify
1264 (shiftrotate @0 vec@1)
1265 (with { tree tem = uniform_vector_p (@1); }
1266 (if (tem)
1267 (shiftrotate @0 { tem; }))))))
1268
1269 /* Rewrite an LROTATE_EXPR by a constant into an
1270 RROTATE_EXPR by a new constant. */
1271 (simplify
1272 (lrotate @0 INTEGER_CST@1)
1273 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1274 build_int_cst (TREE_TYPE (@1),
1275 element_precision (type)), @1); }))
1276
1277 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1278 (for op (lrotate rrotate rshift lshift)
1279 (simplify
1280 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1281 (with { unsigned int prec = element_precision (type); }
1282 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1283 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1284 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1285 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1286 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1287 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1288 being well defined. */
1289 (if (low >= prec)
1290 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1291 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1292 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1293 { build_zero_cst (type); }
1294 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1295 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1296
1297
1298 /* ((1 << A) & 1) != 0 -> A == 0
1299 ((1 << A) & 1) == 0 -> A != 0 */
1300 (for cmp (ne eq)
1301 icmp (eq ne)
1302 (simplify
1303 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1304 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1305
1306 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1307 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1308 if CST2 != 0. */
1309 (for cmp (ne eq)
1310 (simplify
1311 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1312 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1313 (if (cand < 0
1314 || (!integer_zerop (@2)
1315 && wi::ne_p (wi::lshift (@0, cand), @2)))
1316 { constant_boolean_node (cmp == NE_EXPR, type); }
1317 (if (!integer_zerop (@2)
1318 && wi::eq_p (wi::lshift (@0, cand), @2))
1319 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1320
1321 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1322 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1323 if the new mask might be further optimized. */
1324 (for shift (lshift rshift)
1325 (simplify
1326 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1327 INTEGER_CST@2)
1328 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1329 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1330 && tree_fits_uhwi_p (@1)
1331 && tree_to_uhwi (@1) > 0
1332 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1333 (with
1334 {
1335 unsigned int shiftc = tree_to_uhwi (@1);
1336 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1337 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1338 tree shift_type = TREE_TYPE (@3);
1339 unsigned int prec;
1340
1341 if (shift == LSHIFT_EXPR)
1342 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1343 else if (shift == RSHIFT_EXPR
1344 && (TYPE_PRECISION (shift_type)
1345 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1346 {
1347 prec = TYPE_PRECISION (TREE_TYPE (@3));
1348 tree arg00 = @0;
1349 /* See if more bits can be proven as zero because of
1350 zero extension. */
1351 if (@3 != @0
1352 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1353 {
1354 tree inner_type = TREE_TYPE (@0);
1355 if ((TYPE_PRECISION (inner_type)
1356 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1357 && TYPE_PRECISION (inner_type) < prec)
1358 {
1359 prec = TYPE_PRECISION (inner_type);
1360 /* See if we can shorten the right shift. */
1361 if (shiftc < prec)
1362 shift_type = inner_type;
1363 /* Otherwise X >> C1 is all zeros, so we'll optimize
1364 it into (X, 0) later on by making sure zerobits
1365 is all ones. */
1366 }
1367 }
1368 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1369 if (shiftc < prec)
1370 {
1371 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1372 zerobits <<= prec - shiftc;
1373 }
1374 /* For arithmetic shift if sign bit could be set, zerobits
1375 can contain actually sign bits, so no transformation is
1376 possible, unless MASK masks them all away. In that
1377 case the shift needs to be converted into logical shift. */
1378 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1379 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1380 {
1381 if ((mask & zerobits) == 0)
1382 shift_type = unsigned_type_for (TREE_TYPE (@3));
1383 else
1384 zerobits = 0;
1385 }
1386 }
1387 }
1388 /* ((X << 16) & 0xff00) is (X, 0). */
1389 (if ((mask & zerobits) == mask)
1390 { build_int_cst (type, 0); }
1391 (with { newmask = mask | zerobits; }
1392 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1393 (with
1394 {
1395 /* Only do the transformation if NEWMASK is some integer
1396 mode's mask. */
1397 for (prec = BITS_PER_UNIT;
1398 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1399 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1400 break;
1401 }
1402 (if (prec < HOST_BITS_PER_WIDE_INT
1403 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1404 (with
1405 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1406 (if (!tree_int_cst_equal (newmaskt, @2))
1407 (if (shift_type != TREE_TYPE (@3))
1408 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1409 (bit_and @4 { newmaskt; })))))))))))))
1410
1411 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1412 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1413 (for shift (lshift rshift)
1414 (for bit_op (bit_and bit_xor bit_ior)
1415 (simplify
1416 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1417 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1418 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1419 (bit_op (shift (convert @0) @1) { mask; }))))))
1420
1421
1422 /* Simplifications of conversions. */
1423
1424 /* Basic strip-useless-type-conversions / strip_nops. */
1425 (for cvt (convert view_convert float fix_trunc)
1426 (simplify
1427 (cvt @0)
1428 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1429 || (GENERIC && type == TREE_TYPE (@0)))
1430 @0)))
1431
1432 /* Contract view-conversions. */
1433 (simplify
1434 (view_convert (view_convert @0))
1435 (view_convert @0))
1436
1437 /* For integral conversions with the same precision or pointer
1438 conversions use a NOP_EXPR instead. */
1439 (simplify
1440 (view_convert @0)
1441 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1442 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1443 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1444 (convert @0)))
1445
1446 /* Strip inner integral conversions that do not change precision or size. */
1447 (simplify
1448 (view_convert (convert@0 @1))
1449 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1450 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1451 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1452 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1453 (view_convert @1)))
1454
1455 /* Re-association barriers around constants and other re-association
1456 barriers can be removed. */
1457 (simplify
1458 (paren CONSTANT_CLASS_P@0)
1459 @0)
1460 (simplify
1461 (paren (paren@1 @0))
1462 @1)
1463
1464 /* Handle cases of two conversions in a row. */
1465 (for ocvt (convert float fix_trunc)
1466 (for icvt (convert float)
1467 (simplify
1468 (ocvt (icvt@1 @0))
1469 (with
1470 {
1471 tree inside_type = TREE_TYPE (@0);
1472 tree inter_type = TREE_TYPE (@1);
1473 int inside_int = INTEGRAL_TYPE_P (inside_type);
1474 int inside_ptr = POINTER_TYPE_P (inside_type);
1475 int inside_float = FLOAT_TYPE_P (inside_type);
1476 int inside_vec = VECTOR_TYPE_P (inside_type);
1477 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1478 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1479 int inter_int = INTEGRAL_TYPE_P (inter_type);
1480 int inter_ptr = POINTER_TYPE_P (inter_type);
1481 int inter_float = FLOAT_TYPE_P (inter_type);
1482 int inter_vec = VECTOR_TYPE_P (inter_type);
1483 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1484 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1485 int final_int = INTEGRAL_TYPE_P (type);
1486 int final_ptr = POINTER_TYPE_P (type);
1487 int final_float = FLOAT_TYPE_P (type);
1488 int final_vec = VECTOR_TYPE_P (type);
1489 unsigned int final_prec = TYPE_PRECISION (type);
1490 int final_unsignedp = TYPE_UNSIGNED (type);
1491 }
1492 (switch
1493 /* In addition to the cases of two conversions in a row
1494 handled below, if we are converting something to its own
1495 type via an object of identical or wider precision, neither
1496 conversion is needed. */
1497 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1498 || (GENERIC
1499 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1500 && (((inter_int || inter_ptr) && final_int)
1501 || (inter_float && final_float))
1502 && inter_prec >= final_prec)
1503 (ocvt @0))
1504
1505 /* Likewise, if the intermediate and initial types are either both
1506 float or both integer, we don't need the middle conversion if the
1507 former is wider than the latter and doesn't change the signedness
1508 (for integers). Avoid this if the final type is a pointer since
1509 then we sometimes need the middle conversion. Likewise if the
1510 final type has a precision not equal to the size of its mode. */
1511 (if (((inter_int && inside_int) || (inter_float && inside_float))
1512 && (final_int || final_float)
1513 && inter_prec >= inside_prec
1514 && (inter_float || inter_unsignedp == inside_unsignedp)
1515 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1516 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1517 (ocvt @0))
1518
1519 /* If we have a sign-extension of a zero-extended value, we can
1520 replace that by a single zero-extension. Likewise if the
1521 final conversion does not change precision we can drop the
1522 intermediate conversion. */
1523 (if (inside_int && inter_int && final_int
1524 && ((inside_prec < inter_prec && inter_prec < final_prec
1525 && inside_unsignedp && !inter_unsignedp)
1526 || final_prec == inter_prec))
1527 (ocvt @0))
1528
1529 /* Two conversions in a row are not needed unless:
1530 - some conversion is floating-point (overstrict for now), or
1531 - some conversion is a vector (overstrict for now), or
1532 - the intermediate type is narrower than both initial and
1533 final, or
1534 - the intermediate type and innermost type differ in signedness,
1535 and the outermost type is wider than the intermediate, or
1536 - the initial type is a pointer type and the precisions of the
1537 intermediate and final types differ, or
1538 - the final type is a pointer type and the precisions of the
1539 initial and intermediate types differ. */
1540 (if (! inside_float && ! inter_float && ! final_float
1541 && ! inside_vec && ! inter_vec && ! final_vec
1542 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1543 && ! (inside_int && inter_int
1544 && inter_unsignedp != inside_unsignedp
1545 && inter_prec < final_prec)
1546 && ((inter_unsignedp && inter_prec > inside_prec)
1547 == (final_unsignedp && final_prec > inter_prec))
1548 && ! (inside_ptr && inter_prec != final_prec)
1549 && ! (final_ptr && inside_prec != inter_prec)
1550 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1551 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1552 (ocvt @0))
1553
1554 /* A truncation to an unsigned type (a zero-extension) should be
1555 canonicalized as bitwise and of a mask. */
1556 (if (final_int && inter_int && inside_int
1557 && final_prec == inside_prec
1558 && final_prec > inter_prec
1559 && inter_unsignedp)
1560 (convert (bit_and @0 { wide_int_to_tree
1561 (inside_type,
1562 wi::mask (inter_prec, false,
1563 TYPE_PRECISION (inside_type))); })))
1564
1565 /* If we are converting an integer to a floating-point that can
1566 represent it exactly and back to an integer, we can skip the
1567 floating-point conversion. */
1568 (if (GIMPLE /* PR66211 */
1569 && inside_int && inter_float && final_int &&
1570 (unsigned) significand_size (TYPE_MODE (inter_type))
1571 >= inside_prec - !inside_unsignedp)
1572 (convert @0)))))))
1573
1574 /* If we have a narrowing conversion to an integral type that is fed by a
1575 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1576 masks off bits outside the final type (and nothing else). */
1577 (simplify
1578 (convert (bit_and @0 INTEGER_CST@1))
1579 (if (INTEGRAL_TYPE_P (type)
1580 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1581 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1582 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1583 TYPE_PRECISION (type)), 0))
1584 (convert @0)))
1585
1586
1587 /* (X /[ex] A) * A -> X. */
1588 (simplify
1589 (mult (convert? (exact_div @0 @1)) @1)
1590 /* Look through a sign-changing conversion. */
1591 (convert @0))
1592
1593 /* Canonicalization of binary operations. */
1594
1595 /* Convert X + -C into X - C. */
1596 (simplify
1597 (plus @0 REAL_CST@1)
1598 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1599 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1600 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1601 (minus @0 { tem; })))))
1602
1603 /* Convert x+x into x*2.0. */
1604 (simplify
1605 (plus @0 @0)
1606 (if (SCALAR_FLOAT_TYPE_P (type))
1607 (mult @0 { build_real (type, dconst2); })))
1608
1609 (simplify
1610 (minus integer_zerop @1)
1611 (negate @1))
1612
1613 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1614 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1615 (-ARG1 + ARG0) reduces to -ARG1. */
1616 (simplify
1617 (minus real_zerop@0 @1)
1618 (if (fold_real_zero_addition_p (type, @0, 0))
1619 (negate @1)))
1620
1621 /* Transform x * -1 into -x. */
1622 (simplify
1623 (mult @0 integer_minus_onep)
1624 (negate @0))
1625
1626 /* True if we can easily extract the real and imaginary parts of a complex
1627 number. */
1628 (match compositional_complex
1629 (convert? (complex @0 @1)))
1630
1631 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1632 (simplify
1633 (complex (realpart @0) (imagpart @0))
1634 @0)
1635 (simplify
1636 (realpart (complex @0 @1))
1637 @0)
1638 (simplify
1639 (imagpart (complex @0 @1))
1640 @1)
1641
1642 /* Sometimes we only care about half of a complex expression. */
1643 (simplify
1644 (realpart (convert?:s (conj:s @0)))
1645 (convert (realpart @0)))
1646 (simplify
1647 (imagpart (convert?:s (conj:s @0)))
1648 (convert (negate (imagpart @0))))
1649 (for part (realpart imagpart)
1650 (for op (plus minus)
1651 (simplify
1652 (part (convert?:s@2 (op:s @0 @1)))
1653 (convert (op (part @0) (part @1))))))
1654 (simplify
1655 (realpart (convert?:s (CEXPI:s @0)))
1656 (convert (COS @0)))
1657 (simplify
1658 (imagpart (convert?:s (CEXPI:s @0)))
1659 (convert (SIN @0)))
1660
1661 /* conj(conj(x)) -> x */
1662 (simplify
1663 (conj (convert? (conj @0)))
1664 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1665 (convert @0)))
1666
1667 /* conj({x,y}) -> {x,-y} */
1668 (simplify
1669 (conj (convert?:s (complex:s @0 @1)))
1670 (with { tree itype = TREE_TYPE (type); }
1671 (complex (convert:itype @0) (negate (convert:itype @1)))))
1672
1673 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1674 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1675 (simplify
1676 (bswap (bswap @0))
1677 @0)
1678 (simplify
1679 (bswap (bit_not (bswap @0)))
1680 (bit_not @0))
1681 (for bitop (bit_xor bit_ior bit_and)
1682 (simplify
1683 (bswap (bitop:c (bswap @0) @1))
1684 (bitop @0 (bswap @1)))))
1685
1686
1687 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1688
1689 /* Simplify constant conditions.
1690 Only optimize constant conditions when the selected branch
1691 has the same type as the COND_EXPR. This avoids optimizing
1692 away "c ? x : throw", where the throw has a void type.
1693 Note that we cannot throw away the fold-const.c variant nor
1694 this one as we depend on doing this transform before possibly
1695 A ? B : B -> B triggers and the fold-const.c one can optimize
1696 0 ? A : B to B even if A has side-effects. Something
1697 genmatch cannot handle. */
1698 (simplify
1699 (cond INTEGER_CST@0 @1 @2)
1700 (if (integer_zerop (@0))
1701 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1702 @2)
1703 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1704 @1)))
1705 (simplify
1706 (vec_cond VECTOR_CST@0 @1 @2)
1707 (if (integer_all_onesp (@0))
1708 @1
1709 (if (integer_zerop (@0))
1710 @2)))
1711
1712 (for cnd (cond vec_cond)
1713 /* A ? B : (A ? X : C) -> A ? B : C. */
1714 (simplify
1715 (cnd @0 (cnd @0 @1 @2) @3)
1716 (cnd @0 @1 @3))
1717 (simplify
1718 (cnd @0 @1 (cnd @0 @2 @3))
1719 (cnd @0 @1 @3))
1720 /* A ? B : (!A ? C : X) -> A ? B : C. */
1721 /* ??? This matches embedded conditions open-coded because genmatch
1722 would generate matching code for conditions in separate stmts only.
1723 The following is still important to merge then and else arm cases
1724 from if-conversion. */
1725 (simplify
1726 (cnd @0 @1 (cnd @2 @3 @4))
1727 (if (COMPARISON_CLASS_P (@0)
1728 && COMPARISON_CLASS_P (@2)
1729 && invert_tree_comparison
1730 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1731 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1732 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1733 (cnd @0 @1 @3)))
1734 (simplify
1735 (cnd @0 (cnd @1 @2 @3) @4)
1736 (if (COMPARISON_CLASS_P (@0)
1737 && COMPARISON_CLASS_P (@1)
1738 && invert_tree_comparison
1739 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1740 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1741 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1742 (cnd @0 @3 @4)))
1743
1744 /* A ? B : B -> B. */
1745 (simplify
1746 (cnd @0 @1 @1)
1747 @1)
1748
1749 /* !A ? B : C -> A ? C : B. */
1750 (simplify
1751 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1752 (cnd @0 @2 @1)))
1753
1754 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1755 return all-1 or all-0 results. */
1756 /* ??? We could instead convert all instances of the vec_cond to negate,
1757 but that isn't necessarily a win on its own. */
1758 (simplify
1759 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1760 (if (VECTOR_TYPE_P (type)
1761 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1762 && (TYPE_MODE (TREE_TYPE (type))
1763 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1764 (minus @3 (view_convert @0))))
1765
1766 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1767 (simplify
1768 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1769 (if (VECTOR_TYPE_P (type)
1770 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1771 && (TYPE_MODE (TREE_TYPE (type))
1772 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1773 (plus @3 (view_convert @0))))
1774
1775
1776 /* Simplifications of comparisons. */
1777
1778 /* See if we can reduce the magnitude of a constant involved in a
1779 comparison by changing the comparison code. This is a canonicalization
1780 formerly done by maybe_canonicalize_comparison_1. */
1781 (for cmp (le gt)
1782 acmp (lt ge)
1783 (simplify
1784 (cmp @0 INTEGER_CST@1)
1785 (if (tree_int_cst_sgn (@1) == -1)
1786 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1787 (for cmp (ge lt)
1788 acmp (gt le)
1789 (simplify
1790 (cmp @0 INTEGER_CST@1)
1791 (if (tree_int_cst_sgn (@1) == 1)
1792 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1793
1794
1795 /* We can simplify a logical negation of a comparison to the
1796 inverted comparison. As we cannot compute an expression
1797 operator using invert_tree_comparison we have to simulate
1798 that with expression code iteration. */
1799 (for cmp (tcc_comparison)
1800 icmp (inverted_tcc_comparison)
1801 ncmp (inverted_tcc_comparison_with_nans)
1802 /* Ideally we'd like to combine the following two patterns
1803 and handle some more cases by using
1804 (logical_inverted_value (cmp @0 @1))
1805 here but for that genmatch would need to "inline" that.
1806 For now implement what forward_propagate_comparison did. */
1807 (simplify
1808 (bit_not (cmp @0 @1))
1809 (if (VECTOR_TYPE_P (type)
1810 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1811 /* Comparison inversion may be impossible for trapping math,
1812 invert_tree_comparison will tell us. But we can't use
1813 a computed operator in the replacement tree thus we have
1814 to play the trick below. */
1815 (with { enum tree_code ic = invert_tree_comparison
1816 (cmp, HONOR_NANS (@0)); }
1817 (if (ic == icmp)
1818 (icmp @0 @1)
1819 (if (ic == ncmp)
1820 (ncmp @0 @1))))))
1821 (simplify
1822 (bit_xor (cmp @0 @1) integer_truep)
1823 (with { enum tree_code ic = invert_tree_comparison
1824 (cmp, HONOR_NANS (@0)); }
1825 (if (ic == icmp)
1826 (icmp @0 @1)
1827 (if (ic == ncmp)
1828 (ncmp @0 @1))))))
1829
1830 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1831 ??? The transformation is valid for the other operators if overflow
1832 is undefined for the type, but performing it here badly interacts
1833 with the transformation in fold_cond_expr_with_comparison which
1834 attempts to synthetize ABS_EXPR. */
1835 (for cmp (eq ne)
1836 (simplify
1837 (cmp (minus@2 @0 @1) integer_zerop)
1838 (if (single_use (@2))
1839 (cmp @0 @1))))
1840
1841 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1842 signed arithmetic case. That form is created by the compiler
1843 often enough for folding it to be of value. One example is in
1844 computing loop trip counts after Operator Strength Reduction. */
1845 (for cmp (simple_comparison)
1846 scmp (swapped_simple_comparison)
1847 (simplify
1848 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
1849 /* Handle unfolded multiplication by zero. */
1850 (if (integer_zerop (@1))
1851 (cmp @1 @2)
1852 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1853 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1854 && single_use (@3))
1855 /* If @1 is negative we swap the sense of the comparison. */
1856 (if (tree_int_cst_sgn (@1) < 0)
1857 (scmp @0 @2)
1858 (cmp @0 @2))))))
1859
1860 /* Simplify comparison of something with itself. For IEEE
1861 floating-point, we can only do some of these simplifications. */
1862 (for cmp (eq ge le)
1863 (simplify
1864 (cmp @0 @0)
1865 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1866 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1867 { constant_boolean_node (true, type); }
1868 (if (cmp != EQ_EXPR)
1869 (eq @0 @0)))))
1870 (for cmp (ne gt lt)
1871 (simplify
1872 (cmp @0 @0)
1873 (if (cmp != NE_EXPR
1874 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1875 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1876 { constant_boolean_node (false, type); })))
1877 (for cmp (unle unge uneq)
1878 (simplify
1879 (cmp @0 @0)
1880 { constant_boolean_node (true, type); }))
1881 (simplify
1882 (ltgt @0 @0)
1883 (if (!flag_trapping_math)
1884 { constant_boolean_node (false, type); }))
1885
1886 /* Fold ~X op ~Y as Y op X. */
1887 (for cmp (simple_comparison)
1888 (simplify
1889 (cmp (bit_not@2 @0) (bit_not@3 @1))
1890 (if (single_use (@2) && single_use (@3))
1891 (cmp @1 @0))))
1892
1893 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1894 (for cmp (simple_comparison)
1895 scmp (swapped_simple_comparison)
1896 (simplify
1897 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
1898 (if (single_use (@2)
1899 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
1900 (scmp @0 (bit_not @1)))))
1901
1902 (for cmp (simple_comparison)
1903 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1904 (simplify
1905 (cmp (convert@2 @0) (convert? @1))
1906 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1907 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1908 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1909 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1910 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1911 (with
1912 {
1913 tree type1 = TREE_TYPE (@1);
1914 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1915 {
1916 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1917 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1918 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1919 type1 = float_type_node;
1920 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1921 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1922 type1 = double_type_node;
1923 }
1924 tree newtype
1925 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1926 ? TREE_TYPE (@0) : type1);
1927 }
1928 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1929 (cmp (convert:newtype @0) (convert:newtype @1))))))
1930
1931 (simplify
1932 (cmp @0 REAL_CST@1)
1933 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1934 (switch
1935 /* a CMP (-0) -> a CMP 0 */
1936 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1937 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1938 /* x != NaN is always true, other ops are always false. */
1939 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1940 && ! HONOR_SNANS (@1))
1941 { constant_boolean_node (cmp == NE_EXPR, type); })
1942 /* Fold comparisons against infinity. */
1943 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1944 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1945 (with
1946 {
1947 REAL_VALUE_TYPE max;
1948 enum tree_code code = cmp;
1949 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1950 if (neg)
1951 code = swap_tree_comparison (code);
1952 }
1953 (switch
1954 /* x > +Inf is always false, if with ignore sNANs. */
1955 (if (code == GT_EXPR
1956 && ! HONOR_SNANS (@0))
1957 { constant_boolean_node (false, type); })
1958 (if (code == LE_EXPR)
1959 /* x <= +Inf is always true, if we don't case about NaNs. */
1960 (if (! HONOR_NANS (@0))
1961 { constant_boolean_node (true, type); }
1962 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1963 (eq @0 @0)))
1964 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1965 (if (code == EQ_EXPR || code == GE_EXPR)
1966 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1967 (if (neg)
1968 (lt @0 { build_real (TREE_TYPE (@0), max); })
1969 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1970 /* x < +Inf is always equal to x <= DBL_MAX. */
1971 (if (code == LT_EXPR)
1972 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1973 (if (neg)
1974 (ge @0 { build_real (TREE_TYPE (@0), max); })
1975 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1976 /* x != +Inf is always equal to !(x > DBL_MAX). */
1977 (if (code == NE_EXPR)
1978 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1979 (if (! HONOR_NANS (@0))
1980 (if (neg)
1981 (ge @0 { build_real (TREE_TYPE (@0), max); })
1982 (le @0 { build_real (TREE_TYPE (@0), max); }))
1983 (if (neg)
1984 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1985 { build_one_cst (type); })
1986 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1987 { build_one_cst (type); }))))))))))
1988
1989 /* If this is a comparison of a real constant with a PLUS_EXPR
1990 or a MINUS_EXPR of a real constant, we can convert it into a
1991 comparison with a revised real constant as long as no overflow
1992 occurs when unsafe_math_optimizations are enabled. */
1993 (if (flag_unsafe_math_optimizations)
1994 (for op (plus minus)
1995 (simplify
1996 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1997 (with
1998 {
1999 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2000 TREE_TYPE (@1), @2, @1);
2001 }
2002 (if (tem && !TREE_OVERFLOW (tem))
2003 (cmp @0 { tem; }))))))
2004
2005 /* Likewise, we can simplify a comparison of a real constant with
2006 a MINUS_EXPR whose first operand is also a real constant, i.e.
2007 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2008 floating-point types only if -fassociative-math is set. */
2009 (if (flag_associative_math)
2010 (simplify
2011 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2012 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2013 (if (tem && !TREE_OVERFLOW (tem))
2014 (cmp { tem; } @1)))))
2015
2016 /* Fold comparisons against built-in math functions. */
2017 (if (flag_unsafe_math_optimizations
2018 && ! flag_errno_math)
2019 (for sq (SQRT)
2020 (simplify
2021 (cmp (sq @0) REAL_CST@1)
2022 (switch
2023 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2024 (switch
2025 /* sqrt(x) < y is always false, if y is negative. */
2026 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2027 { constant_boolean_node (false, type); })
2028 /* sqrt(x) > y is always true, if y is negative and we
2029 don't care about NaNs, i.e. negative values of x. */
2030 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2031 { constant_boolean_node (true, type); })
2032 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2033 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2034 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2035 (switch
2036 /* sqrt(x) < 0 is always false. */
2037 (if (cmp == LT_EXPR)
2038 { constant_boolean_node (false, type); })
2039 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2040 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2041 { constant_boolean_node (true, type); })
2042 /* sqrt(x) <= 0 -> x == 0. */
2043 (if (cmp == LE_EXPR)
2044 (eq @0 @1))
2045 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2046 == or !=. In the last case:
2047
2048 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2049
2050 if x is negative or NaN. Due to -funsafe-math-optimizations,
2051 the results for other x follow from natural arithmetic. */
2052 (cmp @0 @1)))
2053 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2054 (with
2055 {
2056 REAL_VALUE_TYPE c2;
2057 real_arithmetic (&c2, MULT_EXPR,
2058 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2059 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2060 }
2061 (if (REAL_VALUE_ISINF (c2))
2062 /* sqrt(x) > y is x == +Inf, when y is very large. */
2063 (if (HONOR_INFINITIES (@0))
2064 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2065 { constant_boolean_node (false, type); })
2066 /* sqrt(x) > c is the same as x > c*c. */
2067 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2068 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2069 (with
2070 {
2071 REAL_VALUE_TYPE c2;
2072 real_arithmetic (&c2, MULT_EXPR,
2073 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2074 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2075 }
2076 (if (REAL_VALUE_ISINF (c2))
2077 (switch
2078 /* sqrt(x) < y is always true, when y is a very large
2079 value and we don't care about NaNs or Infinities. */
2080 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2081 { constant_boolean_node (true, type); })
2082 /* sqrt(x) < y is x != +Inf when y is very large and we
2083 don't care about NaNs. */
2084 (if (! HONOR_NANS (@0))
2085 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2086 /* sqrt(x) < y is x >= 0 when y is very large and we
2087 don't care about Infinities. */
2088 (if (! HONOR_INFINITIES (@0))
2089 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2090 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2091 (if (GENERIC)
2092 (truth_andif
2093 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2094 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2095 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2096 (if (! HONOR_NANS (@0))
2097 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2098 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2099 (if (GENERIC)
2100 (truth_andif
2101 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2102 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2103
2104 /* Unordered tests if either argument is a NaN. */
2105 (simplify
2106 (bit_ior (unordered @0 @0) (unordered @1 @1))
2107 (if (types_match (@0, @1))
2108 (unordered @0 @1)))
2109 (simplify
2110 (bit_and (ordered @0 @0) (ordered @1 @1))
2111 (if (types_match (@0, @1))
2112 (ordered @0 @1)))
2113 (simplify
2114 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2115 @2)
2116 (simplify
2117 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2118 @2)
2119
2120 /* Simple range test simplifications. */
2121 /* A < B || A >= B -> true. */
2122 (for test1 (lt le le le ne ge)
2123 test2 (ge gt ge ne eq ne)
2124 (simplify
2125 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2126 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2127 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2128 { constant_boolean_node (true, type); })))
2129 /* A < B && A >= B -> false. */
2130 (for test1 (lt lt lt le ne eq)
2131 test2 (ge gt eq gt eq gt)
2132 (simplify
2133 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2134 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2135 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2136 { constant_boolean_node (false, type); })))
2137
2138 /* -A CMP -B -> B CMP A. */
2139 (for cmp (tcc_comparison)
2140 scmp (swapped_tcc_comparison)
2141 (simplify
2142 (cmp (negate @0) (negate @1))
2143 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2144 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2145 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2146 (scmp @0 @1)))
2147 (simplify
2148 (cmp (negate @0) CONSTANT_CLASS_P@1)
2149 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2150 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2151 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2152 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2153 (if (tem && !TREE_OVERFLOW (tem))
2154 (scmp @0 { tem; }))))))
2155
2156 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2157 (for op (eq ne)
2158 (simplify
2159 (op (abs @0) zerop@1)
2160 (op @0 @1)))
2161
2162 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2163 (for cmp (simple_comparison)
2164 (simplify
2165 (cmp (convert@0 @00) (convert?@1 @10))
2166 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2167 /* Disable this optimization if we're casting a function pointer
2168 type on targets that require function pointer canonicalization. */
2169 && !(targetm.have_canonicalize_funcptr_for_compare ()
2170 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2171 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2172 && single_use (@0))
2173 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2174 && (TREE_CODE (@10) == INTEGER_CST
2175 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2176 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2177 || cmp == NE_EXPR
2178 || cmp == EQ_EXPR)
2179 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2180 /* ??? The special-casing of INTEGER_CST conversion was in the original
2181 code and here to avoid a spurious overflow flag on the resulting
2182 constant which fold_convert produces. */
2183 (if (TREE_CODE (@1) == INTEGER_CST)
2184 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2185 TREE_OVERFLOW (@1)); })
2186 (cmp @00 (convert @1)))
2187
2188 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2189 /* If possible, express the comparison in the shorter mode. */
2190 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2191 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2192 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2193 || ((TYPE_PRECISION (TREE_TYPE (@00))
2194 >= TYPE_PRECISION (TREE_TYPE (@10)))
2195 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2196 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2197 || (TREE_CODE (@10) == INTEGER_CST
2198 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2199 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2200 (cmp @00 (convert @10))
2201 (if (TREE_CODE (@10) == INTEGER_CST
2202 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2203 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2204 (with
2205 {
2206 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2207 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2208 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2209 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2210 }
2211 (if (above || below)
2212 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2213 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2214 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2215 { constant_boolean_node (above ? true : false, type); }
2216 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2217 { constant_boolean_node (above ? false : true, type); }))))))))))))
2218
2219 (for cmp (eq ne)
2220 /* A local variable can never be pointed to by
2221 the default SSA name of an incoming parameter.
2222 SSA names are canonicalized to 2nd place. */
2223 (simplify
2224 (cmp addr@0 SSA_NAME@1)
2225 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2226 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2227 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2228 (if (TREE_CODE (base) == VAR_DECL
2229 && auto_var_in_fn_p (base, current_function_decl))
2230 (if (cmp == NE_EXPR)
2231 { constant_boolean_node (true, type); }
2232 { constant_boolean_node (false, type); }))))))
2233
2234 /* Equality compare simplifications from fold_binary */
2235 (for cmp (eq ne)
2236
2237 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2238 Similarly for NE_EXPR. */
2239 (simplify
2240 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2241 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2242 && wi::bit_and_not (@1, @2) != 0)
2243 { constant_boolean_node (cmp == NE_EXPR, type); }))
2244
2245 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2246 (simplify
2247 (cmp (bit_xor @0 @1) integer_zerop)
2248 (cmp @0 @1))
2249
2250 /* (X ^ Y) == Y becomes X == 0.
2251 Likewise (X ^ Y) == X becomes Y == 0. */
2252 (simplify
2253 (cmp:c (bit_xor:c @0 @1) @0)
2254 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2255
2256 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2257 (simplify
2258 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2259 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2260 (cmp @0 (bit_xor @1 (convert @2)))))
2261
2262 (simplify
2263 (cmp (convert? addr@0) integer_zerop)
2264 (if (tree_single_nonzero_warnv_p (@0, NULL))
2265 { constant_boolean_node (cmp == NE_EXPR, type); })))
2266
2267 /* If we have (A & C) == C where C is a power of 2, convert this into
2268 (A & C) != 0. Similarly for NE_EXPR. */
2269 (for cmp (eq ne)
2270 icmp (ne eq)
2271 (simplify
2272 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2273 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2274
2275 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2276 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2277 (for cmp (eq ne)
2278 ncmp (ge lt)
2279 (simplify
2280 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2281 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2282 && (TYPE_PRECISION (TREE_TYPE (@0))
2283 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2284 && element_precision (@2) >= element_precision (@0)
2285 && wi::only_sign_bit_p (@1, element_precision (@0)))
2286 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2287 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2288
2289 /* When the addresses are not directly of decls compare base and offset.
2290 This implements some remaining parts of fold_comparison address
2291 comparisons but still no complete part of it. Still it is good
2292 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2293 (for cmp (simple_comparison)
2294 (simplify
2295 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2296 (with
2297 {
2298 HOST_WIDE_INT off0, off1;
2299 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2300 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2301 if (base0 && TREE_CODE (base0) == MEM_REF)
2302 {
2303 off0 += mem_ref_offset (base0).to_short_addr ();
2304 base0 = TREE_OPERAND (base0, 0);
2305 }
2306 if (base1 && TREE_CODE (base1) == MEM_REF)
2307 {
2308 off1 += mem_ref_offset (base1).to_short_addr ();
2309 base1 = TREE_OPERAND (base1, 0);
2310 }
2311 }
2312 (if (base0 && base1)
2313 (with
2314 {
2315 int equal = 2;
2316 if (decl_in_symtab_p (base0)
2317 && decl_in_symtab_p (base1))
2318 equal = symtab_node::get_create (base0)
2319 ->equal_address_to (symtab_node::get_create (base1));
2320 else if ((DECL_P (base0)
2321 || TREE_CODE (base0) == SSA_NAME
2322 || TREE_CODE (base0) == STRING_CST)
2323 && (DECL_P (base1)
2324 || TREE_CODE (base1) == SSA_NAME
2325 || TREE_CODE (base1) == STRING_CST))
2326 equal = (base0 == base1);
2327 }
2328 (if (equal == 1
2329 && (cmp == EQ_EXPR || cmp == NE_EXPR
2330 /* If the offsets are equal we can ignore overflow. */
2331 || off0 == off1
2332 || POINTER_TYPE_OVERFLOW_UNDEFINED
2333 /* Or if we compare using pointers to decls or strings. */
2334 || (POINTER_TYPE_P (TREE_TYPE (@2))
2335 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2336 (switch
2337 (if (cmp == EQ_EXPR)
2338 { constant_boolean_node (off0 == off1, type); })
2339 (if (cmp == NE_EXPR)
2340 { constant_boolean_node (off0 != off1, type); })
2341 (if (cmp == LT_EXPR)
2342 { constant_boolean_node (off0 < off1, type); })
2343 (if (cmp == LE_EXPR)
2344 { constant_boolean_node (off0 <= off1, type); })
2345 (if (cmp == GE_EXPR)
2346 { constant_boolean_node (off0 >= off1, type); })
2347 (if (cmp == GT_EXPR)
2348 { constant_boolean_node (off0 > off1, type); }))
2349 (if (equal == 0
2350 && DECL_P (base0) && DECL_P (base1)
2351 /* If we compare this as integers require equal offset. */
2352 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2353 || off0 == off1))
2354 (switch
2355 (if (cmp == EQ_EXPR)
2356 { constant_boolean_node (false, type); })
2357 (if (cmp == NE_EXPR)
2358 { constant_boolean_node (true, type); })))))))))
2359
2360 /* Non-equality compare simplifications from fold_binary */
2361 (for cmp (lt gt le ge)
2362 /* Comparisons with the highest or lowest possible integer of
2363 the specified precision will have known values. */
2364 (simplify
2365 (cmp (convert?@2 @0) INTEGER_CST@1)
2366 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2367 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2368 (with
2369 {
2370 tree arg1_type = TREE_TYPE (@1);
2371 unsigned int prec = TYPE_PRECISION (arg1_type);
2372 wide_int max = wi::max_value (arg1_type);
2373 wide_int signed_max = wi::max_value (prec, SIGNED);
2374 wide_int min = wi::min_value (arg1_type);
2375 }
2376 (switch
2377 (if (wi::eq_p (@1, max))
2378 (switch
2379 (if (cmp == GT_EXPR)
2380 { constant_boolean_node (false, type); })
2381 (if (cmp == GE_EXPR)
2382 (eq @2 @1))
2383 (if (cmp == LE_EXPR)
2384 { constant_boolean_node (true, type); })
2385 (if (cmp == LT_EXPR)
2386 (ne @2 @1))))
2387 (if (wi::eq_p (@1, min))
2388 (switch
2389 (if (cmp == LT_EXPR)
2390 { constant_boolean_node (false, type); })
2391 (if (cmp == LE_EXPR)
2392 (eq @2 @1))
2393 (if (cmp == GE_EXPR)
2394 { constant_boolean_node (true, type); })
2395 (if (cmp == GT_EXPR)
2396 (ne @2 @1))))
2397 (if (wi::eq_p (@1, max - 1))
2398 (switch
2399 (if (cmp == GT_EXPR)
2400 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2401 (if (cmp == LE_EXPR)
2402 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2403 (if (wi::eq_p (@1, min + 1))
2404 (switch
2405 (if (cmp == GE_EXPR)
2406 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2407 (if (cmp == LT_EXPR)
2408 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2409 (if (wi::eq_p (@1, signed_max)
2410 && TYPE_UNSIGNED (arg1_type)
2411 /* We will flip the signedness of the comparison operator
2412 associated with the mode of @1, so the sign bit is
2413 specified by this mode. Check that @1 is the signed
2414 max associated with this sign bit. */
2415 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2416 /* signed_type does not work on pointer types. */
2417 && INTEGRAL_TYPE_P (arg1_type))
2418 /* The following case also applies to X < signed_max+1
2419 and X >= signed_max+1 because previous transformations. */
2420 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2421 (with { tree st = signed_type_for (arg1_type); }
2422 (if (cmp == LE_EXPR)
2423 (ge (convert:st @0) { build_zero_cst (st); })
2424 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2425
2426 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2427 /* If the second operand is NaN, the result is constant. */
2428 (simplify
2429 (cmp @0 REAL_CST@1)
2430 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2431 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2432 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2433 ? false : true, type); })))
2434
2435 /* bool_var != 0 becomes bool_var. */
2436 (simplify
2437 (ne @0 integer_zerop)
2438 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2439 && types_match (type, TREE_TYPE (@0)))
2440 (non_lvalue @0)))
2441 /* bool_var == 1 becomes bool_var. */
2442 (simplify
2443 (eq @0 integer_onep)
2444 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2445 && types_match (type, TREE_TYPE (@0)))
2446 (non_lvalue @0)))
2447 /* Do not handle
2448 bool_var == 0 becomes !bool_var or
2449 bool_var != 1 becomes !bool_var
2450 here because that only is good in assignment context as long
2451 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2452 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2453 clearly less optimal and which we'll transform again in forwprop. */
2454
2455
2456 /* Simplification of math builtins. These rules must all be optimizations
2457 as well as IL simplifications. If there is a possibility that the new
2458 form could be a pessimization, the rule should go in the canonicalization
2459 section that follows this one.
2460
2461 Rules can generally go in this section if they satisfy one of
2462 the following:
2463
2464 - the rule describes an identity
2465
2466 - the rule replaces calls with something as simple as addition or
2467 multiplication
2468
2469 - the rule contains unary calls only and simplifies the surrounding
2470 arithmetic. (The idea here is to exclude non-unary calls in which
2471 one operand is constant and in which the call is known to be cheap
2472 when the operand has that value.) */
2473
2474 (if (flag_unsafe_math_optimizations)
2475 /* Simplify sqrt(x) * sqrt(x) -> x. */
2476 (simplify
2477 (mult (SQRT@1 @0) @1)
2478 (if (!HONOR_SNANS (type))
2479 @0))
2480
2481 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2482 (for root (SQRT CBRT)
2483 (simplify
2484 (mult (root:s @0) (root:s @1))
2485 (root (mult @0 @1))))
2486
2487 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2488 (for exps (EXP EXP2 EXP10 POW10)
2489 (simplify
2490 (mult (exps:s @0) (exps:s @1))
2491 (exps (plus @0 @1))))
2492
2493 /* Simplify a/root(b/c) into a*root(c/b). */
2494 (for root (SQRT CBRT)
2495 (simplify
2496 (rdiv @0 (root:s (rdiv:s @1 @2)))
2497 (mult @0 (root (rdiv @2 @1)))))
2498
2499 /* Simplify x/expN(y) into x*expN(-y). */
2500 (for exps (EXP EXP2 EXP10 POW10)
2501 (simplify
2502 (rdiv @0 (exps:s @1))
2503 (mult @0 (exps (negate @1)))))
2504
2505 (for logs (LOG LOG2 LOG10 LOG10)
2506 exps (EXP EXP2 EXP10 POW10)
2507 /* logN(expN(x)) -> x. */
2508 (simplify
2509 (logs (exps @0))
2510 @0)
2511 /* expN(logN(x)) -> x. */
2512 (simplify
2513 (exps (logs @0))
2514 @0))
2515
2516 /* Optimize logN(func()) for various exponential functions. We
2517 want to determine the value "x" and the power "exponent" in
2518 order to transform logN(x**exponent) into exponent*logN(x). */
2519 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2520 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2521 (simplify
2522 (logs (exps @0))
2523 (if (SCALAR_FLOAT_TYPE_P (type))
2524 (with {
2525 tree x;
2526 switch (exps)
2527 {
2528 CASE_CFN_EXP:
2529 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2530 x = build_real_truncate (type, dconst_e ());
2531 break;
2532 CASE_CFN_EXP2:
2533 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2534 x = build_real (type, dconst2);
2535 break;
2536 CASE_CFN_EXP10:
2537 CASE_CFN_POW10:
2538 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2539 {
2540 REAL_VALUE_TYPE dconst10;
2541 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2542 x = build_real (type, dconst10);
2543 }
2544 break;
2545 default:
2546 gcc_unreachable ();
2547 }
2548 }
2549 (mult (logs { x; }) @0)))))
2550
2551 (for logs (LOG LOG
2552 LOG2 LOG2
2553 LOG10 LOG10)
2554 exps (SQRT CBRT)
2555 (simplify
2556 (logs (exps @0))
2557 (if (SCALAR_FLOAT_TYPE_P (type))
2558 (with {
2559 tree x;
2560 switch (exps)
2561 {
2562 CASE_CFN_SQRT:
2563 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2564 x = build_real (type, dconsthalf);
2565 break;
2566 CASE_CFN_CBRT:
2567 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2568 x = build_real_truncate (type, dconst_third ());
2569 break;
2570 default:
2571 gcc_unreachable ();
2572 }
2573 }
2574 (mult { x; } (logs @0))))))
2575
2576 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2577 (for logs (LOG LOG2 LOG10)
2578 pows (POW)
2579 (simplify
2580 (logs (pows @0 @1))
2581 (mult @1 (logs @0))))
2582
2583 (for sqrts (SQRT)
2584 cbrts (CBRT)
2585 pows (POW)
2586 exps (EXP EXP2 EXP10 POW10)
2587 /* sqrt(expN(x)) -> expN(x*0.5). */
2588 (simplify
2589 (sqrts (exps @0))
2590 (exps (mult @0 { build_real (type, dconsthalf); })))
2591 /* cbrt(expN(x)) -> expN(x/3). */
2592 (simplify
2593 (cbrts (exps @0))
2594 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2595 /* pow(expN(x), y) -> expN(x*y). */
2596 (simplify
2597 (pows (exps @0) @1)
2598 (exps (mult @0 @1))))
2599
2600 /* tan(atan(x)) -> x. */
2601 (for tans (TAN)
2602 atans (ATAN)
2603 (simplify
2604 (tans (atans @0))
2605 @0)))
2606
2607 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2608 (simplify
2609 (CABS (complex:c @0 real_zerop@1))
2610 (abs @0))
2611
2612 /* trunc(trunc(x)) -> trunc(x), etc. */
2613 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2614 (simplify
2615 (fns (fns @0))
2616 (fns @0)))
2617 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2618 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2619 (simplify
2620 (fns integer_valued_real_p@0)
2621 @0))
2622
2623 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2624 (simplify
2625 (HYPOT:c @0 real_zerop@1)
2626 (abs @0))
2627
2628 /* pow(1,x) -> 1. */
2629 (simplify
2630 (POW real_onep@0 @1)
2631 @0)
2632
2633 (simplify
2634 /* copysign(x,x) -> x. */
2635 (COPYSIGN @0 @0)
2636 @0)
2637
2638 (simplify
2639 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2640 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2641 (abs @0))
2642
2643 (for scale (LDEXP SCALBN SCALBLN)
2644 /* ldexp(0, x) -> 0. */
2645 (simplify
2646 (scale real_zerop@0 @1)
2647 @0)
2648 /* ldexp(x, 0) -> x. */
2649 (simplify
2650 (scale @0 integer_zerop@1)
2651 @0)
2652 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2653 (simplify
2654 (scale REAL_CST@0 @1)
2655 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2656 @0)))
2657
2658 /* Canonicalization of sequences of math builtins. These rules represent
2659 IL simplifications but are not necessarily optimizations.
2660
2661 The sincos pass is responsible for picking "optimal" implementations
2662 of math builtins, which may be more complicated and can sometimes go
2663 the other way, e.g. converting pow into a sequence of sqrts.
2664 We only want to do these canonicalizations before the pass has run. */
2665
2666 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2667 /* Simplify tan(x) * cos(x) -> sin(x). */
2668 (simplify
2669 (mult:c (TAN:s @0) (COS:s @0))
2670 (SIN @0))
2671
2672 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2673 (simplify
2674 (mult @0 (POW:s @0 REAL_CST@1))
2675 (if (!TREE_OVERFLOW (@1))
2676 (POW @0 (plus @1 { build_one_cst (type); }))))
2677
2678 /* Simplify sin(x) / cos(x) -> tan(x). */
2679 (simplify
2680 (rdiv (SIN:s @0) (COS:s @0))
2681 (TAN @0))
2682
2683 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2684 (simplify
2685 (rdiv (COS:s @0) (SIN:s @0))
2686 (rdiv { build_one_cst (type); } (TAN @0)))
2687
2688 /* Simplify sin(x) / tan(x) -> cos(x). */
2689 (simplify
2690 (rdiv (SIN:s @0) (TAN:s @0))
2691 (if (! HONOR_NANS (@0)
2692 && ! HONOR_INFINITIES (@0))
2693 (COS @0)))
2694
2695 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2696 (simplify
2697 (rdiv (TAN:s @0) (SIN:s @0))
2698 (if (! HONOR_NANS (@0)
2699 && ! HONOR_INFINITIES (@0))
2700 (rdiv { build_one_cst (type); } (COS @0))))
2701
2702 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2703 (simplify
2704 (mult (POW:s @0 @1) (POW:s @0 @2))
2705 (POW @0 (plus @1 @2)))
2706
2707 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2708 (simplify
2709 (mult (POW:s @0 @1) (POW:s @2 @1))
2710 (POW (mult @0 @2) @1))
2711
2712 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2713 (simplify
2714 (rdiv (POW:s @0 REAL_CST@1) @0)
2715 (if (!TREE_OVERFLOW (@1))
2716 (POW @0 (minus @1 { build_one_cst (type); }))))
2717
2718 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2719 (simplify
2720 (rdiv @0 (POW:s @1 @2))
2721 (mult @0 (POW @1 (negate @2))))
2722
2723 (for sqrts (SQRT)
2724 cbrts (CBRT)
2725 pows (POW)
2726 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2727 (simplify
2728 (sqrts (sqrts @0))
2729 (pows @0 { build_real (type, dconst_quarter ()); }))
2730 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2731 (simplify
2732 (sqrts (cbrts @0))
2733 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2734 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2735 (simplify
2736 (cbrts (sqrts @0))
2737 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2738 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2739 (simplify
2740 (cbrts (cbrts tree_expr_nonnegative_p@0))
2741 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2742 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2743 (simplify
2744 (sqrts (pows @0 @1))
2745 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2746 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2747 (simplify
2748 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2749 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2750 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
2751 (simplify
2752 (pows (sqrts @0) @1)
2753 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2754 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
2755 (simplify
2756 (pows (cbrts tree_expr_nonnegative_p@0) @1)
2757 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2758 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
2759 (simplify
2760 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
2761 (pows @0 (mult @1 @2))))
2762
2763 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2764 (simplify
2765 (CABS (complex @0 @0))
2766 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2767
2768 /* hypot(x,x) -> fabs(x)*sqrt(2). */
2769 (simplify
2770 (HYPOT @0 @0)
2771 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2772
2773 /* cexp(x+yi) -> exp(x)*cexpi(y). */
2774 (for cexps (CEXP)
2775 exps (EXP)
2776 cexpis (CEXPI)
2777 (simplify
2778 (cexps compositional_complex@0)
2779 (if (targetm.libc_has_function (function_c99_math_complex))
2780 (complex
2781 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
2782 (mult @1 (imagpart @2)))))))
2783
2784 (if (canonicalize_math_p ())
2785 /* floor(x) -> trunc(x) if x is nonnegative. */
2786 (for floors (FLOOR)
2787 truncs (TRUNC)
2788 (simplify
2789 (floors tree_expr_nonnegative_p@0)
2790 (truncs @0))))
2791
2792 (match double_value_p
2793 @0
2794 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
2795 (for froms (BUILT_IN_TRUNCL
2796 BUILT_IN_FLOORL
2797 BUILT_IN_CEILL
2798 BUILT_IN_ROUNDL
2799 BUILT_IN_NEARBYINTL
2800 BUILT_IN_RINTL)
2801 tos (BUILT_IN_TRUNC
2802 BUILT_IN_FLOOR
2803 BUILT_IN_CEIL
2804 BUILT_IN_ROUND
2805 BUILT_IN_NEARBYINT
2806 BUILT_IN_RINT)
2807 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
2808 (if (optimize && canonicalize_math_p ())
2809 (simplify
2810 (froms (convert double_value_p@0))
2811 (convert (tos @0)))))
2812
2813 (match float_value_p
2814 @0
2815 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
2816 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
2817 BUILT_IN_FLOORL BUILT_IN_FLOOR
2818 BUILT_IN_CEILL BUILT_IN_CEIL
2819 BUILT_IN_ROUNDL BUILT_IN_ROUND
2820 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
2821 BUILT_IN_RINTL BUILT_IN_RINT)
2822 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
2823 BUILT_IN_FLOORF BUILT_IN_FLOORF
2824 BUILT_IN_CEILF BUILT_IN_CEILF
2825 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
2826 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
2827 BUILT_IN_RINTF BUILT_IN_RINTF)
2828 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
2829 if x is a float. */
2830 (if (optimize && canonicalize_math_p ()
2831 && targetm.libc_has_function (function_c99_misc))
2832 (simplify
2833 (froms (convert float_value_p@0))
2834 (convert (tos @0)))))
2835
2836 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
2837 tos (XFLOOR XCEIL XROUND XRINT)
2838 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
2839 (if (optimize && canonicalize_math_p ())
2840 (simplify
2841 (froms (convert double_value_p@0))
2842 (tos @0))))
2843
2844 (for froms (XFLOORL XCEILL XROUNDL XRINTL
2845 XFLOOR XCEIL XROUND XRINT)
2846 tos (XFLOORF XCEILF XROUNDF XRINTF)
2847 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
2848 if x is a float. */
2849 (if (optimize && canonicalize_math_p ())
2850 (simplify
2851 (froms (convert float_value_p@0))
2852 (tos @0))))
2853
2854 (if (canonicalize_math_p ())
2855 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
2856 (for floors (IFLOOR LFLOOR LLFLOOR)
2857 (simplify
2858 (floors tree_expr_nonnegative_p@0)
2859 (fix_trunc @0))))
2860
2861 (if (canonicalize_math_p ())
2862 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
2863 (for fns (IFLOOR LFLOOR LLFLOOR
2864 ICEIL LCEIL LLCEIL
2865 IROUND LROUND LLROUND)
2866 (simplify
2867 (fns integer_valued_real_p@0)
2868 (fix_trunc @0)))
2869 (if (!flag_errno_math)
2870 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
2871 (for rints (IRINT LRINT LLRINT)
2872 (simplify
2873 (rints integer_valued_real_p@0)
2874 (fix_trunc @0)))))
2875
2876 (if (canonicalize_math_p ())
2877 (for ifn (IFLOOR ICEIL IROUND IRINT)
2878 lfn (LFLOOR LCEIL LROUND LRINT)
2879 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
2880 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
2881 sizeof (int) == sizeof (long). */
2882 (if (TYPE_PRECISION (integer_type_node)
2883 == TYPE_PRECISION (long_integer_type_node))
2884 (simplify
2885 (ifn @0)
2886 (lfn:long_integer_type_node @0)))
2887 /* Canonicalize llround (x) to lround (x) on LP64 targets where
2888 sizeof (long long) == sizeof (long). */
2889 (if (TYPE_PRECISION (long_long_integer_type_node)
2890 == TYPE_PRECISION (long_integer_type_node))
2891 (simplify
2892 (llfn @0)
2893 (lfn:long_integer_type_node @0)))))
2894
2895 /* cproj(x) -> x if we're ignoring infinities. */
2896 (simplify
2897 (CPROJ @0)
2898 (if (!HONOR_INFINITIES (type))
2899 @0))
2900
2901 /* If the real part is inf and the imag part is known to be
2902 nonnegative, return (inf + 0i). */
2903 (simplify
2904 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2905 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2906 { build_complex_inf (type, false); }))
2907
2908 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
2909 (simplify
2910 (CPROJ (complex @0 REAL_CST@1))
2911 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2912 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
2913
2914 (for pows (POW)
2915 sqrts (SQRT)
2916 cbrts (CBRT)
2917 (simplify
2918 (pows @0 REAL_CST@1)
2919 (with {
2920 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
2921 REAL_VALUE_TYPE tmp;
2922 }
2923 (switch
2924 /* pow(x,0) -> 1. */
2925 (if (real_equal (value, &dconst0))
2926 { build_real (type, dconst1); })
2927 /* pow(x,1) -> x. */
2928 (if (real_equal (value, &dconst1))
2929 @0)
2930 /* pow(x,-1) -> 1/x. */
2931 (if (real_equal (value, &dconstm1))
2932 (rdiv { build_real (type, dconst1); } @0))
2933 /* pow(x,0.5) -> sqrt(x). */
2934 (if (flag_unsafe_math_optimizations
2935 && canonicalize_math_p ()
2936 && real_equal (value, &dconsthalf))
2937 (sqrts @0))
2938 /* pow(x,1/3) -> cbrt(x). */
2939 (if (flag_unsafe_math_optimizations
2940 && canonicalize_math_p ()
2941 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
2942 real_equal (value, &tmp)))
2943 (cbrts @0))))))
2944
2945 /* powi(1,x) -> 1. */
2946 (simplify
2947 (POWI real_onep@0 @1)
2948 @0)
2949
2950 (simplify
2951 (POWI @0 INTEGER_CST@1)
2952 (switch
2953 /* powi(x,0) -> 1. */
2954 (if (wi::eq_p (@1, 0))
2955 { build_real (type, dconst1); })
2956 /* powi(x,1) -> x. */
2957 (if (wi::eq_p (@1, 1))
2958 @0)
2959 /* powi(x,-1) -> 1/x. */
2960 (if (wi::eq_p (@1, -1))
2961 (rdiv { build_real (type, dconst1); } @0))))
2962
2963 /* Narrowing of arithmetic and logical operations.
2964
2965 These are conceptually similar to the transformations performed for
2966 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2967 term we want to move all that code out of the front-ends into here. */
2968
2969 /* If we have a narrowing conversion of an arithmetic operation where
2970 both operands are widening conversions from the same type as the outer
2971 narrowing conversion. Then convert the innermost operands to a suitable
2972 unsigned type (to avoid introducing undefined behavior), perform the
2973 operation and convert the result to the desired type. */
2974 (for op (plus minus)
2975 (simplify
2976 (convert (op:s (convert@2 @0) (convert@3 @1)))
2977 (if (INTEGRAL_TYPE_P (type)
2978 /* We check for type compatibility between @0 and @1 below,
2979 so there's no need to check that @1/@3 are integral types. */
2980 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2981 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2982 /* The precision of the type of each operand must match the
2983 precision of the mode of each operand, similarly for the
2984 result. */
2985 && (TYPE_PRECISION (TREE_TYPE (@0))
2986 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2987 && (TYPE_PRECISION (TREE_TYPE (@1))
2988 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2989 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2990 /* The inner conversion must be a widening conversion. */
2991 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2992 && types_match (@0, @1)
2993 && types_match (@0, type))
2994 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2995 (convert (op @0 @1))
2996 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2997 (convert (op (convert:utype @0) (convert:utype @1))))))))
2998
2999 /* This is another case of narrowing, specifically when there's an outer
3000 BIT_AND_EXPR which masks off bits outside the type of the innermost
3001 operands. Like the previous case we have to convert the operands
3002 to unsigned types to avoid introducing undefined behavior for the
3003 arithmetic operation. */
3004 (for op (minus plus)
3005 (simplify
3006 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3007 (if (INTEGRAL_TYPE_P (type)
3008 /* We check for type compatibility between @0 and @1 below,
3009 so there's no need to check that @1/@3 are integral types. */
3010 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3011 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3012 /* The precision of the type of each operand must match the
3013 precision of the mode of each operand, similarly for the
3014 result. */
3015 && (TYPE_PRECISION (TREE_TYPE (@0))
3016 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3017 && (TYPE_PRECISION (TREE_TYPE (@1))
3018 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3019 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3020 /* The inner conversion must be a widening conversion. */
3021 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3022 && types_match (@0, @1)
3023 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3024 <= TYPE_PRECISION (TREE_TYPE (@0)))
3025 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3026 true, TYPE_PRECISION (type))) == 0))
3027 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3028 (with { tree ntype = TREE_TYPE (@0); }
3029 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3030 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3031 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3032 (convert:utype @4))))))))
3033
3034 /* Transform (@0 < @1 and @0 < @2) to use min,
3035 (@0 > @1 and @0 > @2) to use max */
3036 (for op (lt le gt ge)
3037 ext (min min max max)
3038 (simplify
3039 (bit_and (op:s @0 @1) (op:s @0 @2))
3040 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3041 (op @0 (ext @1 @2)))))
3042
3043 (simplify
3044 /* signbit(x) -> 0 if x is nonnegative. */
3045 (SIGNBIT tree_expr_nonnegative_p@0)
3046 { integer_zero_node; })
3047
3048 (simplify
3049 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3050 (SIGNBIT @0)
3051 (if (!HONOR_SIGNED_ZEROS (@0))
3052 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))