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