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