match.pd: Optimize __builtin_mul_overflow_p (x, cst, (utype)0) to x > ~(utype)0 ...
[platform/upstream/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.cc
3    and generic-match.cc from it.
4
5    Copyright (C) 2014-2022 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    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p
41    expand_vec_cmp_expr_p
42    bitmask_inv_cst_vector_p)
43
44 /* Operator lists.  */
45 (define_operator_list tcc_comparison
46   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
54 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
55
56 #include "cfn-operators.pd"
57
58 /* Define operand lists for math rounding functions {,i,l,ll}FN,
59    where the versions prefixed with "i" return an int, those prefixed with
60    "l" return a long and those prefixed with "ll" return a long long.
61
62    Also define operand lists:
63
64      X<FN>F for all float functions, in the order i, l, ll
65      X<FN> for all double functions, in the same order
66      X<FN>L for all long double functions, in the same order.  */
67 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
68   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
69                                  BUILT_IN_L##FN##F \
70                                  BUILT_IN_LL##FN##F) \
71   (define_operator_list X##FN BUILT_IN_I##FN \
72                               BUILT_IN_L##FN \
73                               BUILT_IN_LL##FN) \
74   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
75                                  BUILT_IN_L##FN##L \
76                                  BUILT_IN_LL##FN##L)
77
78 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
82
83 /* Unary operations and their associated IFN_COND_* function.  */
84 (define_operator_list UNCOND_UNARY
85   negate)
86 (define_operator_list COND_UNARY
87   IFN_COND_NEG)
88
89 /* Binary operations and their associated IFN_COND_* function.  */
90 (define_operator_list UNCOND_BINARY
91   plus minus
92   mult trunc_div trunc_mod rdiv
93   min max
94   IFN_FMIN IFN_FMAX
95   bit_and bit_ior bit_xor
96   lshift rshift)
97 (define_operator_list COND_BINARY
98   IFN_COND_ADD IFN_COND_SUB
99   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
100   IFN_COND_MIN IFN_COND_MAX
101   IFN_COND_FMIN IFN_COND_FMAX
102   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
103   IFN_COND_SHL IFN_COND_SHR)
104
105 /* Same for ternary operations.  */
106 (define_operator_list UNCOND_TERNARY
107   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
108 (define_operator_list COND_TERNARY
109   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
110
111 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
112 (define_operator_list ATOMIC_FETCH_OR_XOR_N
113   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
114   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
115   BUILT_IN_ATOMIC_FETCH_OR_16
116   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
117   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
118   BUILT_IN_ATOMIC_FETCH_XOR_16
119   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
120   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
121   BUILT_IN_ATOMIC_XOR_FETCH_16)
122 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
123 (define_operator_list SYNC_FETCH_OR_XOR_N
124   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
125   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
126   BUILT_IN_SYNC_FETCH_AND_OR_16
127   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
128   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
129   BUILT_IN_SYNC_FETCH_AND_XOR_16
130   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
131   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
132   BUILT_IN_SYNC_XOR_AND_FETCH_16)
133 /* __atomic_fetch_and_*.  */
134 (define_operator_list ATOMIC_FETCH_AND_N
135   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
136   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
137   BUILT_IN_ATOMIC_FETCH_AND_16)
138 /* __sync_fetch_and_and_*.  */
139 (define_operator_list SYNC_FETCH_AND_AND_N
140   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
141   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
142   BUILT_IN_SYNC_FETCH_AND_AND_16)
143
144 /* With nop_convert? combine convert? and view_convert? in one pattern
145    plus conditionalize on tree_nop_conversion_p conversions.  */
146 (match (nop_convert @0)
147  (convert @0)
148  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
149 (match (nop_convert @0)
150  (view_convert @0)
151  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
152       && known_eq (TYPE_VECTOR_SUBPARTS (type),
153                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
154       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
155
156 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
157    ABSU_EXPR returns unsigned absolute value of the operand and the operand
158    of the ABSU_EXPR will have the corresponding signed type.  */
159 (simplify (abs (convert @0))
160  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
161       && !TYPE_UNSIGNED (TREE_TYPE (@0))
162       && element_precision (type) > element_precision (TREE_TYPE (@0)))
163   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
164    (convert (absu:utype @0)))))
165
166 #if GIMPLE
167 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
168 (simplify
169  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
170  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
171       && !TYPE_UNSIGNED (TREE_TYPE (@0))
172       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
173   (abs @0)))
174 #endif
175
176 /* Simplifications of operations with one constant operand and
177    simplifications to constants or single values.  */
178
179 (for op (plus pointer_plus minus bit_ior bit_xor)
180   (simplify
181     (op @0 integer_zerop)
182     (non_lvalue @0)))
183
184 /* 0 +p index -> (type)index */
185 (simplify
186  (pointer_plus integer_zerop @1)
187  (non_lvalue (convert @1)))
188
189 /* ptr - 0 -> (type)ptr */
190 (simplify
191  (pointer_diff @0 integer_zerop)
192  (convert @0))
193
194 /* See if ARG1 is zero and X + ARG1 reduces to X.
195    Likewise if the operands are reversed.  */
196 (simplify
197  (plus:c @0 real_zerop@1)
198  (if (fold_real_zero_addition_p (type, @0, @1, 0))
199   (non_lvalue @0)))
200
201 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
202 (simplify
203  (minus @0 real_zerop@1)
204  (if (fold_real_zero_addition_p (type, @0, @1, 1))
205   (non_lvalue @0)))
206
207 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
208    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
209    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
210    if not -frounding-math.  For sNaNs the first operation would raise
211    exceptions but turn the result into qNan, so the second operation
212    would not raise it.   */
213 (for inner_op (plus minus)
214  (for outer_op (plus minus)
215   (simplify
216    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
217     (if (real_zerop (@1)
218          && real_zerop (@2)
219          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
220      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
221                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
222              bool outer_plus
223                = ((outer_op == PLUS_EXPR)
224                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
225       (if (outer_plus && !inner_plus)
226        (outer_op @0 @2)
227        @3))))))
228
229 /* Simplify x - x.
230    This is unsafe for certain floats even in non-IEEE formats.
231    In IEEE, it is unsafe because it does wrong for NaNs.
232    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
233    Also note that operand_equal_p is always false if an operand
234    is volatile.  */
235 (simplify
236  (minus @0 @0)
237  (if (!FLOAT_TYPE_P (type)
238       || (!tree_expr_maybe_nan_p (@0)
239           && !tree_expr_maybe_infinite_p (@0)
240           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
241               || !HONOR_SIGNED_ZEROS (type))))
242   { build_zero_cst (type); }))
243 (simplify
244  (pointer_diff @@0 @0)
245  { build_zero_cst (type); })
246
247 (simplify
248  (mult @0 integer_zerop@1)
249  @1)
250
251 /* -x == x -> x == 0 */
252 (for cmp (eq ne)
253  (simplify
254   (cmp:c @0 (negate @0))
255    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
256         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
257     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
258
259 /* Maybe fold x * 0 to 0.  The expressions aren't the same
260    when x is NaN, since x * 0 is also NaN.  Nor are they the
261    same in modes with signed zeros, since multiplying a
262    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
263    since x * 0 is NaN.  */
264 (simplify
265  (mult @0 real_zerop@1)
266  (if (!tree_expr_maybe_nan_p (@0)
267       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
268       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
269   @1))
270
271 /* In IEEE floating point, x*1 is not equivalent to x for snans.
272    Likewise for complex arithmetic with signed zeros.  */
273 (simplify
274  (mult @0 real_onep)
275  (if (!tree_expr_maybe_signaling_nan_p (@0)
276       && (!HONOR_SIGNED_ZEROS (type)
277           || !COMPLEX_FLOAT_TYPE_P (type)))
278   (non_lvalue @0)))
279
280 /* Transform x * -1.0 into -x.  */
281 (simplify
282  (mult @0 real_minus_onep)
283   (if (!tree_expr_maybe_signaling_nan_p (@0)
284        && (!HONOR_SIGNED_ZEROS (type)
285            || !COMPLEX_FLOAT_TYPE_P (type)))
286    (negate @0)))
287
288 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
289    unless the target has native support for the former but not the latter.  */
290 (simplify
291  (mult @0 VECTOR_CST@1)
292  (if (initializer_each_zero_or_onep (@1)
293       && !HONOR_SNANS (type)
294       && !HONOR_SIGNED_ZEROS (type))
295   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
296    (if (itype
297         && (!VECTOR_MODE_P (TYPE_MODE (type))
298             || (VECTOR_MODE_P (TYPE_MODE (itype))
299                 && optab_handler (and_optab,
300                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
301     (view_convert (bit_and:itype (view_convert @0)
302                                  (ne @1 { build_zero_cst (type); })))))))
303
304 (for cmp (gt ge lt le)
305      outp (convert convert negate negate)
306      outn (negate negate convert convert)
307  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
308  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
309  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
310  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
311  (simplify
312   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
313   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
314    (outp (abs @0))))
315  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
316  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
317  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
318  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
319  (simplify
320   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
321   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
322    (outn (abs @0)))))
323
324 /* Transform X * copysign (1.0, X) into abs(X). */
325 (simplify
326  (mult:c @0 (COPYSIGN_ALL real_onep @0))
327  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
328   (abs @0)))
329
330 /* Transform X * copysign (1.0, -X) into -abs(X). */
331 (simplify
332  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
333  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
334   (negate (abs @0))))
335
336 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
337 (simplify
338  (COPYSIGN_ALL REAL_CST@0 @1)
339  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
340   (COPYSIGN_ALL (negate @0) @1)))
341
342 /* X * 1, X / 1 -> X.  */
343 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
344   (simplify
345     (op @0 integer_onep)
346     (non_lvalue @0)))
347
348 /* (A / (1 << B)) -> (A >> B).
349    Only for unsigned A.  For signed A, this would not preserve rounding
350    toward zero.
351    For example: (-1 / ( 1 << B)) !=  -1 >> B.
352    Also also widening conversions, like:
353    (A / (unsigned long long) (1U << B)) -> (A >> B)
354    or
355    (A / (unsigned long long) (1 << B)) -> (A >> B).
356    If the left shift is signed, it can be done only if the upper bits
357    of A starting from shift's type sign bit are zero, as
358    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
359    so it is valid only if A >> 31 is zero.  */
360 (simplify
361  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
362  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
363       && (!VECTOR_TYPE_P (type)
364           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
365           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
366       && (useless_type_conversion_p (type, TREE_TYPE (@1))
367           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
368               && (TYPE_UNSIGNED (TREE_TYPE (@1))
369                   || (element_precision (type)
370                       == element_precision (TREE_TYPE (@1)))
371                   || (INTEGRAL_TYPE_P (type)
372                       && (tree_nonzero_bits (@0)
373                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
374                                       true,
375                                       element_precision (type))) == 0)))))
376    (if (!VECTOR_TYPE_P (type)
377         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
378         && element_precision (TREE_TYPE (@3)) < element_precision (type))
379     (convert (rshift @3 @2))
380     (rshift @0 @2))))
381
382 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
383    undefined behavior in constexpr evaluation, and assuming that the division
384    traps enables better optimizations than these anyway.  */
385 (for div (trunc_div ceil_div floor_div round_div exact_div)
386  /* 0 / X is always zero.  */
387  (simplify
388   (div integer_zerop@0 @1)
389   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
390   (if (!integer_zerop (@1))
391    @0))
392  /* X / -1 is -X.  */
393  (simplify
394   (div @0 integer_minus_onep@1)
395   (if (!TYPE_UNSIGNED (type))
396    (negate @0)))
397  /* X / bool_range_Y is X.  */ 
398  (simplify
399   (div @0 SSA_NAME@1)
400   (if (INTEGRAL_TYPE_P (type)
401        && ssa_name_has_boolean_range (@1)
402        && !flag_non_call_exceptions)
403    @0))
404  /* X / X is one.  */
405  (simplify
406   (div @0 @0)
407   /* But not for 0 / 0 so that we can get the proper warnings and errors.
408      And not for _Fract types where we can't build 1.  */
409   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
410        && !integer_zerop (@0)
411        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
412    { build_one_cst (type); }))
413  /* X / abs (X) is X < 0 ? -1 : 1.  */
414  (simplify
415    (div:C @0 (abs @0))
416    (if (INTEGRAL_TYPE_P (type)
417         && TYPE_OVERFLOW_UNDEFINED (type)
418         && !integer_zerop (@0)
419         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
420     (cond (lt @0 { build_zero_cst (type); })
421           { build_minus_one_cst (type); } { build_one_cst (type); })))
422  /* X / -X is -1.  */
423  (simplify
424    (div:C @0 (negate @0))
425    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
426         && TYPE_OVERFLOW_UNDEFINED (type)
427         && !integer_zerop (@0)
428         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
429     { build_minus_one_cst (type); })))
430
431 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
432    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
433    for MOD instead of DIV.  */
434 (for floor_divmod (floor_div floor_mod)
435      trunc_divmod (trunc_div trunc_mod)
436  (simplify
437   (floor_divmod @0 @1)
438   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
439        && TYPE_UNSIGNED (type))
440    (trunc_divmod @0 @1))))
441
442 /* 1 / X -> X == 1 for unsigned integer X.
443    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
444    But not for 1 / 0 so that we can get proper warnings and errors,
445    and not for 1-bit integers as they are edge cases better handled
446    elsewhere.  */
447 (simplify
448  (trunc_div integer_onep@0 @1)
449  (if (INTEGRAL_TYPE_P (type)
450       && TYPE_PRECISION (type) > 1
451       && !integer_zerop (@1)
452       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
453   (if (TYPE_UNSIGNED (type))
454    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
455    (with { tree utype = unsigned_type_for (type); }
456     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
457               { build_int_cst (utype, 2); })
458      @1 { build_zero_cst (type); })))))
459
460 /* Combine two successive divisions.  Note that combining ceil_div
461    and floor_div is trickier and combining round_div even more so.  */
462 (for div (trunc_div exact_div)
463  (simplify
464   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
465   (with {
466     wi::overflow_type overflow;
467     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
468                             TYPE_SIGN (type), &overflow);
469    }
470    (if (div == EXACT_DIV_EXPR
471         || optimize_successive_divisions_p (@2, @3))
472     (if (!overflow)
473      (div @0 { wide_int_to_tree (type, mul); })
474      (if (TYPE_UNSIGNED (type)
475           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
476       { build_zero_cst (type); }))))))
477
478 /* Combine successive multiplications.  Similar to above, but handling
479    overflow is different.  */
480 (simplify
481  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
482  (with {
483    wi::overflow_type overflow;
484    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
485                            TYPE_SIGN (type), &overflow);
486   }
487   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
488      otherwise undefined overflow implies that @0 must be zero.  */
489   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
490    (mult @0 { wide_int_to_tree (type, mul); }))))
491
492 /* Optimize A / A to 1.0 if we don't care about
493    NaNs or Infinities.  */
494 (simplify
495  (rdiv @0 @0)
496  (if (FLOAT_TYPE_P (type)
497       && ! HONOR_NANS (type)
498       && ! HONOR_INFINITIES (type))
499   { build_one_cst (type); }))
500
501 /* Optimize -A / A to -1.0 if we don't care about
502    NaNs or Infinities.  */
503 (simplify
504  (rdiv:C @0 (negate @0))
505  (if (FLOAT_TYPE_P (type)
506       && ! HONOR_NANS (type)
507       && ! HONOR_INFINITIES (type))
508   { build_minus_one_cst (type); }))
509
510 /* PR71078: x / abs(x) -> copysign (1.0, x) */
511 (simplify
512  (rdiv:C (convert? @0) (convert? (abs @0)))
513   (if (SCALAR_FLOAT_TYPE_P (type)
514        && ! HONOR_NANS (type)
515        && ! HONOR_INFINITIES (type))
516    (switch
517     (if (types_match (type, float_type_node))
518      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
519     (if (types_match (type, double_type_node))
520      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
521     (if (types_match (type, long_double_type_node))
522      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
523
524 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
525 (simplify
526  (rdiv @0 real_onep)
527  (if (!tree_expr_maybe_signaling_nan_p (@0))
528   (non_lvalue @0)))
529
530 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
531 (simplify
532  (rdiv @0 real_minus_onep)
533  (if (!tree_expr_maybe_signaling_nan_p (@0))
534   (negate @0)))
535
536 (if (flag_reciprocal_math)
537  /* Convert (A/B)/C to A/(B*C). */
538  (simplify
539   (rdiv (rdiv:s @0 @1) @2)
540   (rdiv @0 (mult @1 @2)))
541
542  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
543  (simplify
544   (rdiv @0 (mult:s @1 REAL_CST@2))
545   (with
546    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
547    (if (tem)
548     (rdiv (mult @0 { tem; } ) @1))))
549
550  /* Convert A/(B/C) to (A/B)*C  */
551  (simplify
552   (rdiv @0 (rdiv:s @1 @2))
553    (mult (rdiv @0 @1) @2)))
554
555 /* Simplify x / (- y) to -x / y.  */
556 (simplify
557  (rdiv @0 (negate @1))
558  (rdiv (negate @0) @1))
559
560 (if (flag_unsafe_math_optimizations)
561  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
562     Since C / x may underflow to zero, do this only for unsafe math.  */
563  (for op (lt le gt ge)
564       neg_op (gt ge lt le)
565   (simplify
566    (op (rdiv REAL_CST@0 @1) real_zerop@2)
567    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
568     (switch
569      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
570       (op @1 @2))
571      /* For C < 0, use the inverted operator.  */
572      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
573       (neg_op @1 @2)))))))
574
575 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
576 (for div (trunc_div ceil_div floor_div round_div exact_div)
577  (simplify
578   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
579   (if (integer_pow2p (@2)
580        && tree_int_cst_sgn (@2) > 0
581        && tree_nop_conversion_p (type, TREE_TYPE (@0))
582        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
583    (rshift (convert @0)
584            { build_int_cst (integer_type_node,
585                             wi::exact_log2 (wi::to_wide (@2))); }))))
586
587 /* If ARG1 is a constant, we can convert this to a multiply by the
588    reciprocal.  This does not have the same rounding properties,
589    so only do this if -freciprocal-math.  We can actually
590    always safely do it if ARG1 is a power of two, but it's hard to
591    tell if it is or not in a portable manner.  */
592 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
593  (simplify
594   (rdiv @0 cst@1)
595   (if (optimize)
596    (if (flag_reciprocal_math
597         && !real_zerop (@1))
598     (with
599      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
600      (if (tem)
601       (mult @0 { tem; } )))
602     (if (cst != COMPLEX_CST)
603      (with { tree inverse = exact_inverse (type, @1); }
604       (if (inverse)
605        (mult @0 { inverse; } ))))))))
606
607 (for mod (ceil_mod floor_mod round_mod trunc_mod)
608  /* 0 % X is always zero.  */
609  (simplify
610   (mod integer_zerop@0 @1)
611   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
612   (if (!integer_zerop (@1))
613    @0))
614  /* X % 1 is always zero.  */
615  (simplify
616   (mod @0 integer_onep)
617   { build_zero_cst (type); })
618  /* X % -1 is zero.  */
619  (simplify
620   (mod @0 integer_minus_onep@1)
621   (if (!TYPE_UNSIGNED (type))
622    { build_zero_cst (type); }))
623  /* X % X is zero.  */
624  (simplify
625   (mod @0 @0)
626   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
627   (if (!integer_zerop (@0))
628    { build_zero_cst (type); }))
629  /* (X % Y) % Y is just X % Y.  */
630  (simplify
631   (mod (mod@2 @0 @1) @1)
632   @2)
633  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
634  (simplify
635   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
636   (if (ANY_INTEGRAL_TYPE_P (type)
637        && TYPE_OVERFLOW_UNDEFINED (type)
638        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
639                              TYPE_SIGN (type)))
640    { build_zero_cst (type); }))
641  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
642     modulo and comparison, since it is simpler and equivalent.  */
643  (for cmp (eq ne)
644   (simplify
645    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
646    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
647     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
648      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
649
650 /* X % -C is the same as X % C.  */
651 (simplify
652  (trunc_mod @0 INTEGER_CST@1)
653   (if (TYPE_SIGN (type) == SIGNED
654        && !TREE_OVERFLOW (@1)
655        && wi::neg_p (wi::to_wide (@1))
656        && !TYPE_OVERFLOW_TRAPS (type)
657        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
658        && !sign_bit_p (@1, @1))
659    (trunc_mod @0 (negate @1))))
660
661 /* X % -Y is the same as X % Y.  */
662 (simplify
663  (trunc_mod @0 (convert? (negate @1)))
664  (if (INTEGRAL_TYPE_P (type)
665       && !TYPE_UNSIGNED (type)
666       && !TYPE_OVERFLOW_TRAPS (type)
667       && tree_nop_conversion_p (type, TREE_TYPE (@1))
668       /* Avoid this transformation if X might be INT_MIN or
669          Y might be -1, because we would then change valid
670          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
671       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
672           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
673                                                         (TREE_TYPE (@1))))))
674   (trunc_mod @0 (convert @1))))
675
676 /* X - (X / Y) * Y is the same as X % Y.  */
677 (simplify
678  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
679  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
680   (convert (trunc_mod @0 @1))))
681
682 /* x * (1 + y / x) - y -> x - y % x */
683 (simplify
684  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
685  (if (INTEGRAL_TYPE_P (type))
686   (minus @0 (trunc_mod @1 @0))))
687
688 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
689    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
690    Also optimize A % (C << N)  where C is a power of 2,
691    to A & ((C << N) - 1).
692    Also optimize "A shift (B % C)", if C is a power of 2, to
693    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
694    and assume (B % C) is nonnegative as shifts negative values would
695    be UB.  */
696 (match (power_of_two_cand @1)
697  INTEGER_CST@1)
698 (match (power_of_two_cand @1)
699  (lshift INTEGER_CST@1 @2))
700 (for mod (trunc_mod floor_mod)
701  (for shift (lshift rshift)
702   (simplify
703    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
704    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
705     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
706                                                       1); }))))))
707  (simplify
708   (mod @0 (convert? (power_of_two_cand@1 @2)))
709   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
710        /* Allow any integral conversions of the divisor, except
711           conversion from narrower signed to wider unsigned type
712           where if @1 would be negative power of two, the divisor
713           would not be a power of two.  */
714        && INTEGRAL_TYPE_P (type)
715        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
716        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
717            || TYPE_UNSIGNED (TREE_TYPE (@1))
718            || !TYPE_UNSIGNED (type))
719        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
720    (with { tree utype = TREE_TYPE (@1);
721            if (!TYPE_OVERFLOW_WRAPS (utype))
722              utype = unsigned_type_for (utype); }
723     (bit_and @0 (convert (minus (convert:utype @1)
724                                 { build_one_cst (utype); })))))))
725
726 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
727 (simplify
728  (trunc_div (mult @0 integer_pow2p@1) @1)
729  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
730   (bit_and @0 { wide_int_to_tree
731                 (type, wi::mask (TYPE_PRECISION (type)
732                                  - wi::exact_log2 (wi::to_wide (@1)),
733                                  false, TYPE_PRECISION (type))); })))
734
735 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
736 (simplify
737  (mult (trunc_div @0 integer_pow2p@1) @1)
738  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
739   (bit_and @0 (negate @1))))
740
741 /* Simplify (t * 2) / 2) -> t.  */
742 (for div (trunc_div ceil_div floor_div round_div exact_div)
743  (simplify
744   (div (mult:c @0 @1) @1)
745   (if (ANY_INTEGRAL_TYPE_P (type))
746    (if (TYPE_OVERFLOW_UNDEFINED (type))
747     @0
748 #if GIMPLE
749     (with
750      {
751        bool overflowed = true;
752        value_range vr0, vr1;
753        if (INTEGRAL_TYPE_P (type)
754            && get_global_range_query ()->range_of_expr (vr0, @0)
755            && get_global_range_query ()->range_of_expr (vr1, @1)
756            && vr0.kind () == VR_RANGE
757            && vr1.kind () == VR_RANGE)
758          {
759            wide_int wmin0 = vr0.lower_bound ();
760            wide_int wmax0 = vr0.upper_bound ();
761            wide_int wmin1 = vr1.lower_bound ();
762            wide_int wmax1 = vr1.upper_bound ();
763            /* If the multiplication can't overflow/wrap around, then
764               it can be optimized too.  */
765            wi::overflow_type min_ovf, max_ovf;
766            wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
767            wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
768            if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
769              {
770                wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
771                wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
772                if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
773                  overflowed = false;
774              }
775          }
776      }
777     (if (!overflowed)
778      @0))
779 #endif
780    ))))
781
782 (for op (negate abs)
783  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
784  (for coss (COS COSH)
785   (simplify
786    (coss (op @0))
787     (coss @0)))
788  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
789  (for pows (POW)
790   (simplify
791    (pows (op @0) REAL_CST@1)
792    (with { HOST_WIDE_INT n; }
793     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
794      (pows @0 @1)))))
795  /* Likewise for powi.  */
796  (for pows (POWI)
797   (simplify
798    (pows (op @0) INTEGER_CST@1)
799    (if ((wi::to_wide (@1) & 1) == 0)
800     (pows @0 @1))))
801  /* Strip negate and abs from both operands of hypot.  */
802  (for hypots (HYPOT)
803   (simplify
804    (hypots (op @0) @1)
805    (hypots @0 @1))
806   (simplify
807    (hypots @0 (op @1))
808    (hypots @0 @1)))
809  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
810  (for copysigns (COPYSIGN_ALL)
811   (simplify
812    (copysigns (op @0) @1)
813    (copysigns @0 @1))))
814
815 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
816 (simplify
817  (mult (abs@1 @0) @1)
818  (mult @0 @0))
819
820 /* Convert absu(x)*absu(x) -> x*x.  */
821 (simplify
822  (mult (absu@1 @0) @1)
823  (mult (convert@2 @0) @2))
824
825 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
826 (for coss (COS COSH)
827      copysigns (COPYSIGN)
828  (simplify
829   (coss (copysigns @0 @1))
830    (coss @0)))
831
832 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
833 (for pows (POW)
834      copysigns (COPYSIGN)
835  (simplify
836   (pows (copysigns @0 @2) REAL_CST@1)
837   (with { HOST_WIDE_INT n; }
838    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
839     (pows @0 @1)))))
840 /* Likewise for powi.  */
841 (for pows (POWI)
842      copysigns (COPYSIGN)
843  (simplify
844   (pows (copysigns @0 @2) INTEGER_CST@1)
845   (if ((wi::to_wide (@1) & 1) == 0)
846    (pows @0 @1))))
847
848 (for hypots (HYPOT)
849      copysigns (COPYSIGN)
850  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
851  (simplify
852   (hypots (copysigns @0 @1) @2)
853   (hypots @0 @2))
854  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
855  (simplify
856   (hypots @0 (copysigns @1 @2))
857   (hypots @0 @1)))
858
859 /* copysign(x, CST) -> [-]abs (x).  */
860 (for copysigns (COPYSIGN_ALL)
861  (simplify
862   (copysigns @0 REAL_CST@1)
863   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
864    (negate (abs @0))
865    (abs @0))))
866
867 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
868 (for copysigns (COPYSIGN_ALL)
869  (simplify
870   (copysigns (copysigns @0 @1) @2)
871   (copysigns @0 @2)))
872
873 /* copysign(x,y)*copysign(x,y) -> x*x.  */
874 (for copysigns (COPYSIGN_ALL)
875  (simplify
876   (mult (copysigns@2 @0 @1) @2)
877   (mult @0 @0)))
878
879 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
880 (for ccoss (CCOS CCOSH)
881  (simplify
882   (ccoss (negate @0))
883    (ccoss @0)))
884
885 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
886 (for ops (conj negate)
887  (for cabss (CABS)
888   (simplify
889    (cabss (ops @0))
890    (cabss @0))))
891
892 /* Fold (a * (1 << b)) into (a << b)  */
893 (simplify
894  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
895   (if (! FLOAT_TYPE_P (type)
896        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
897    (lshift @0 @2)))
898
899 /* Fold (1 << (C - x)) where C = precision(type) - 1
900    into ((1 << C) >> x). */
901 (simplify
902  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
903   (if (INTEGRAL_TYPE_P (type)
904        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
905        && single_use (@1))
906    (if (TYPE_UNSIGNED (type))
907      (rshift (lshift @0 @2) @3)
908    (with
909     { tree utype = unsigned_type_for (type); }
910     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
911
912 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
913 (simplify
914  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
915  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
916       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
917   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
918    (bit_and (convert @0)
919             { wide_int_to_tree (type,
920                                 wi::lshift (wone, wi::to_wide (@2))); }))))
921
922 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
923 (for cst (INTEGER_CST VECTOR_CST)
924  (simplify
925   (rshift (negate:s @0) cst@1)
926    (if (!TYPE_UNSIGNED (type)
927         && TYPE_OVERFLOW_UNDEFINED (type))
928     (with { tree stype = TREE_TYPE (@1);
929             tree bt = truth_type_for (type);
930             tree zeros = build_zero_cst (type);
931             tree cst = NULL_TREE; }
932      (switch
933       /* Handle scalar case.  */
934       (if (INTEGRAL_TYPE_P (type)
935            /* If we apply the rule to the scalar type before vectorization
936               we will enforce the result of the comparison being a bool
937               which will require an extra AND on the result that will be
938               indistinguishable from when the user did actually want 0
939               or 1 as the result so it can't be removed.  */
940            && canonicalize_math_after_vectorization_p ()
941            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
942        (negate (convert (gt @0 { zeros; }))))
943       /* Handle vector case.  */
944       (if (VECTOR_INTEGER_TYPE_P (type)
945            /* First check whether the target has the same mode for vector
946               comparison results as it's operands do.  */
947            && TYPE_MODE (bt) == TYPE_MODE (type)
948            /* Then check to see if the target is able to expand the comparison
949               with the given type later on, otherwise we may ICE.  */
950            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
951            && (cst = uniform_integer_cst_p (@1)) != NULL
952            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
953        (view_convert (gt:bt @0 { zeros; }))))))))
954
955 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
956 (simplify
957  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
958   (if (flag_associative_math
959        && single_use (@3))
960    (with
961     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
962     (if (tem)
963      (rdiv { tem; } @1)))))
964
965 /* Simplify ~X & X as zero.  */
966 (simplify
967  (bit_and:c (convert? @0) (convert? (bit_not @0)))
968   { build_zero_cst (type); })
969
970 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
971 (simplify
972   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
973   (if (TYPE_UNSIGNED (type))
974     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
975
976 (for bitop (bit_and bit_ior)
977      cmp (eq ne)
978  /* PR35691: Transform
979     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
980     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
981  (simplify
982   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
983    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
984         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
985         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
986     (cmp (bit_ior @0 (convert @1)) @2)))
987  /* Transform:
988     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
989     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
990  (simplify
991   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
992    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
993         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
994         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
995     (cmp (bit_and @0 (convert @1)) @2))))
996
997 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
998 (simplify
999  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1000   (minus (bit_xor @0 @1) @1))
1001 (simplify
1002  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1003  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1004   (minus (bit_xor @0 @1) @1)))
1005
1006 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1007 (simplify
1008  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1009   (minus @1 (bit_xor @0 @1)))
1010
1011 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1012 (for op (bit_ior bit_xor plus)
1013  (simplify
1014   (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
1015    (bit_xor @0 @1))
1016  (simplify
1017   (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
1018   (if (~wi::to_wide (@2) == wi::to_wide (@1))
1019    (bit_xor @0 @1))))
1020
1021 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1022 (simplify
1023   (bit_ior:c (bit_xor:c @0 @1) @0)
1024   (bit_ior @0 @1))
1025
1026 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1027 (simplify
1028  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1029  @2)
1030
1031 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1032 (simplify
1033  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1034  (bit_not (bit_and @0 @1)))
1035
1036 /* (~a & b) ^ a  -->   (a | b)   */
1037 (simplify
1038  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1039  (bit_ior @0 @1))
1040
1041 /* (a | b) & ~(a ^ b)  -->  a & b  */
1042 (simplify
1043  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1044  (bit_and @0 @1))
1045
1046 /* a | ~(a ^ b)  -->  a | ~b  */
1047 (simplify
1048  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1049  (bit_ior @0 (bit_not @1)))
1050
1051 /* (a | b) | (a &^ b)  -->  a | b  */
1052 (for op (bit_and bit_xor)
1053  (simplify
1054   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1055   @2))
1056
1057 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1058 (simplify
1059  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1060  @2)
1061
1062 /* ~(~a & b)  -->  a | ~b  */
1063 (simplify
1064  (bit_not (bit_and:cs (bit_not @0) @1))
1065  (bit_ior @0 (bit_not @1)))
1066
1067 /* ~(~a | b) --> a & ~b */
1068 (simplify
1069  (bit_not (bit_ior:cs (bit_not @0) @1))
1070  (bit_and @0 (bit_not @1)))
1071
1072 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1073 (simplify
1074  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1075  (bit_and @3 (bit_not @2)))
1076
1077 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1078 (simplify
1079  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1080  (bit_ior @3 @2))
1081
1082 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1083 (simplify
1084  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1085   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1086
1087 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1088 (simplify
1089  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1090   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1091
1092 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1093 (simplify
1094  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1095  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1096       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1097   (bit_xor @0 @1)))
1098
1099 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1100    ((A & N) + B) & M -> (A + B) & M
1101    Similarly if (N & M) == 0,
1102    ((A | N) + B) & M -> (A + B) & M
1103    and for - instead of + (or unary - instead of +)
1104    and/or ^ instead of |.
1105    If B is constant and (B & M) == 0, fold into A & M.  */
1106 (for op (plus minus)
1107  (for bitop (bit_and bit_ior bit_xor)
1108   (simplify
1109    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1110     (with
1111      { tree pmop[2];
1112        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1113                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1114                                        NULL_TREE, pmop); }
1115      (if (utype)
1116       (convert (bit_and (op (convert:utype { pmop[0]; })
1117                             (convert:utype { pmop[1]; }))
1118                         (convert:utype @2))))))
1119   (simplify
1120    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1121     (with
1122      { tree pmop[2];
1123        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1124                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1125                                        @4, pmop); }
1126      (if (utype)
1127       (convert (bit_and (op (convert:utype { pmop[0]; })
1128                             (convert:utype { pmop[1]; }))
1129                         (convert:utype @2)))))))
1130  (simplify
1131   (bit_and (op:s @0 @1) INTEGER_CST@2)
1132    (with
1133     { tree pmop[2];
1134       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1135                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1136                                       NULL_TREE, NULL_TREE, pmop); }
1137     (if (utype)
1138      (convert (bit_and (op (convert:utype { pmop[0]; })
1139                            (convert:utype { pmop[1]; }))
1140                        (convert:utype @2)))))))
1141 (for bitop (bit_and bit_ior bit_xor)
1142  (simplify
1143   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1144    (with
1145     { tree pmop[2];
1146       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1147                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1148                                       NULL_TREE, NULL_TREE, pmop); }
1149     (if (utype)
1150      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1151                        (convert:utype @1)))))))
1152
1153 /* X % Y is smaller than Y.  */
1154 (for cmp (lt ge)
1155  (simplify
1156   (cmp (trunc_mod @0 @1) @1)
1157   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1158    { constant_boolean_node (cmp == LT_EXPR, type); })))
1159 (for cmp (gt le)
1160  (simplify
1161   (cmp @1 (trunc_mod @0 @1))
1162   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1163    { constant_boolean_node (cmp == GT_EXPR, type); })))
1164
1165 /* x | ~0 -> ~0  */
1166 (simplify
1167  (bit_ior @0 integer_all_onesp@1)
1168  @1)
1169
1170 /* x | 0 -> x  */
1171 (simplify
1172  (bit_ior @0 integer_zerop)
1173  @0)
1174
1175 /* x & 0 -> 0  */
1176 (simplify
1177  (bit_and @0 integer_zerop@1)
1178  @1)
1179
1180 /* ~x | x -> -1 */
1181 /* ~x ^ x -> -1 */
1182 /* ~x + x -> -1 */
1183 (for op (bit_ior bit_xor plus)
1184  (simplify
1185   (op:c (convert? @0) (convert? (bit_not @0)))
1186   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1187
1188 /* x ^ x -> 0 */
1189 (simplify
1190   (bit_xor @0 @0)
1191   { build_zero_cst (type); })
1192
1193 /* Canonicalize X ^ ~0 to ~X.  */
1194 (simplify
1195   (bit_xor @0 integer_all_onesp@1)
1196   (bit_not @0))
1197
1198 /* x & ~0 -> x  */
1199 (simplify
1200  (bit_and @0 integer_all_onesp)
1201   (non_lvalue @0))
1202
1203 /* x & x -> x,  x | x -> x  */
1204 (for bitop (bit_and bit_ior)
1205  (simplify
1206   (bitop @0 @0)
1207   (non_lvalue @0)))
1208
1209 /* x & C -> x if we know that x & ~C == 0.  */
1210 #if GIMPLE
1211 (simplify
1212  (bit_and SSA_NAME@0 INTEGER_CST@1)
1213  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1214       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1215   @0))
1216 #endif
1217
1218 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1219 (simplify
1220  (bit_not (minus (bit_not @0) @1))
1221  (plus @0 @1))
1222 (simplify
1223  (bit_not (plus:c (bit_not @0) @1))
1224  (minus @0 @1))
1225
1226 /* ~(X - Y) -> ~X + Y.  */
1227 (simplify
1228  (bit_not (minus:s @0 @1))
1229  (plus (bit_not @0) @1))
1230 (simplify
1231  (bit_not (plus:s @0 INTEGER_CST@1))
1232  (if ((INTEGRAL_TYPE_P (type)
1233        && TYPE_UNSIGNED (type))
1234       || (!TYPE_OVERFLOW_SANITIZED (type)
1235           && may_negate_without_overflow_p (@1)))
1236   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1237
1238 #if GIMPLE
1239 /* ~X + Y -> (Y - X) - 1.  */
1240 (simplify
1241  (plus:c (bit_not @0) @1)
1242   (if (ANY_INTEGRAL_TYPE_P (type)
1243        && TYPE_OVERFLOW_WRAPS (type)
1244        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1245        && !integer_all_onesp (@1))
1246    (plus (minus @1 @0) { build_minus_one_cst (type); })
1247    (if (INTEGRAL_TYPE_P (type)
1248         && TREE_CODE (@1) == INTEGER_CST
1249         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1250                                               SIGNED))
1251     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1252 #endif
1253
1254 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1255 (simplify
1256  (bit_not (rshift:s @0 @1))
1257   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1258    (rshift (bit_not! @0) @1)
1259    /* For logical right shifts, this is possible only if @0 doesn't
1260       have MSB set and the logical right shift is changed into
1261       arithmetic shift.  */
1262    (if (!wi::neg_p (tree_nonzero_bits (@0)))
1263     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1264      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1265
1266 /* x + (x & 1) -> (x + 1) & ~1 */
1267 (simplify
1268  (plus:c @0 (bit_and:s @0 integer_onep@1))
1269  (bit_and (plus @0 @1) (bit_not @1)))
1270
1271 /* x & ~(x & y) -> x & ~y */
1272 /* x | ~(x | y) -> x | ~y  */
1273 (for bitop (bit_and bit_ior)
1274  (simplify
1275   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1276   (bitop @0 (bit_not @1))))
1277
1278 /* (~x & y) | ~(x | y) -> ~x */
1279 (simplify
1280  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1281  @2)
1282
1283 /* (x | y) ^ (x | ~y) -> ~x */
1284 (simplify
1285  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1286  (bit_not @0))
1287
1288 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1289 (simplify
1290  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1291  (bit_not (bit_xor @0 @1)))
1292
1293 /* (~x | y) ^ (x ^ y) -> x | ~y */
1294 (simplify
1295  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1296  (bit_ior @0 (bit_not @1)))
1297
1298 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1299 (simplify
1300  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1301  (bit_not (bit_and @0 @1)))
1302
1303 /* (x | y) & ~x -> y & ~x */
1304 /* (x & y) | ~x -> y | ~x */
1305 (for bitop (bit_and bit_ior)
1306      rbitop (bit_ior bit_and)
1307  (simplify
1308   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1309   (bitop @1 @2)))
1310
1311 /* (x & y) ^ (x | y) -> x ^ y */
1312 (simplify
1313  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1314  (bit_xor @0 @1))
1315
1316 /* (x ^ y) ^ (x | y) -> x & y */
1317 (simplify
1318  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1319  (bit_and @0 @1))
1320
1321 /* (x & y) + (x ^ y) -> x | y */
1322 /* (x & y) | (x ^ y) -> x | y */
1323 /* (x & y) ^ (x ^ y) -> x | y */
1324 (for op (plus bit_ior bit_xor)
1325  (simplify
1326   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1327   (bit_ior @0 @1)))
1328
1329 /* (x & y) + (x | y) -> x + y */
1330 (simplify
1331  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1332  (plus @0 @1))
1333
1334 /* (x + y) - (x | y) -> x & y */
1335 (simplify
1336  (minus (plus @0 @1) (bit_ior @0 @1))
1337  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1338       && !TYPE_SATURATING (type))
1339   (bit_and @0 @1)))
1340
1341 /* (x + y) - (x & y) -> x | y */
1342 (simplify
1343  (minus (plus @0 @1) (bit_and @0 @1))
1344  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1345       && !TYPE_SATURATING (type))
1346   (bit_ior @0 @1)))
1347
1348 /* (x | y) - y -> (x & ~y) */
1349 (simplify
1350  (minus (bit_ior:cs @0 @1) @1)
1351  (bit_and @0 (bit_not @1)))
1352
1353 /* (x | y) - (x ^ y) -> x & y */
1354 (simplify
1355  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1356  (bit_and @0 @1))
1357
1358 /* (x | y) - (x & y) -> x ^ y */
1359 (simplify
1360  (minus (bit_ior @0 @1) (bit_and @0 @1))
1361  (bit_xor @0 @1))
1362
1363 /* (x | y) & ~(x & y) -> x ^ y */
1364 (simplify
1365  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1366  (bit_xor @0 @1))
1367
1368 /* (x | y) & (~x ^ y) -> x & y */
1369 (simplify
1370  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1371  (bit_and @0 @1))
1372
1373 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1374 (simplify
1375  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1376  (bit_not (bit_xor @0 @1)))
1377
1378 /* (~x | y) ^ (x | ~y) -> x ^ y */
1379 (simplify
1380  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1381  (bit_xor @0 @1))
1382
1383 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1384 (simplify
1385  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1386                               (nop_convert2? (bit_ior @0 @1))))
1387        integer_all_onesp)
1388  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1389       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1390       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1391       && !TYPE_SATURATING (TREE_TYPE (@2)))
1392  (bit_not (convert (bit_xor @0 @1)))))
1393 (simplify
1394  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1395                                integer_all_onesp))
1396        (nop_convert3? (bit_ior @0 @1)))
1397  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1398       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1399       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1400       && !TYPE_SATURATING (TREE_TYPE (@2)))
1401  (bit_not (convert (bit_xor @0 @1)))))
1402 (simplify
1403  (minus (nop_convert1? (bit_and @0 @1))
1404        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1405                                integer_onep)))
1406  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1407       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1408       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1409       && !TYPE_SATURATING (TREE_TYPE (@2)))
1410  (bit_not (convert (bit_xor @0 @1)))))
1411
1412 /* ~x & ~y -> ~(x | y)
1413    ~x | ~y -> ~(x & y) */
1414 (for op (bit_and bit_ior)
1415      rop (bit_ior bit_and)
1416  (simplify
1417   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1418   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1419        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1420    (bit_not (rop (convert @0) (convert @1))))))
1421
1422 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1423    with a constant, and the two constants have no bits in common,
1424    we should treat this as a BIT_IOR_EXPR since this may produce more
1425    simplifications.  */
1426 (for op (bit_xor plus)
1427  (simplify
1428   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1429       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1430   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1431        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1432        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1433    (bit_ior (convert @4) (convert @5)))))
1434
1435 /* (X | Y) ^ X -> Y & ~ X*/
1436 (simplify
1437  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1438  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1439   (convert (bit_and @1 (bit_not @0)))))
1440
1441 /* Convert ~X ^ ~Y to X ^ Y.  */
1442 (simplify
1443  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1444  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1445       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1446   (bit_xor (convert @0) (convert @1))))
1447
1448 /* Convert ~X ^ C to X ^ ~C.  */
1449 (simplify
1450  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1451  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1452   (bit_xor (convert @0) (bit_not @1))))
1453
1454 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1455 (for opo (bit_and bit_xor)
1456      opi (bit_xor bit_and)
1457  (simplify
1458   (opo:c (opi:cs @0 @1) @1)
1459   (bit_and (bit_not @0) @1)))
1460
1461 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1462    operands are another bit-wise operation with a common input.  If so,
1463    distribute the bit operations to save an operation and possibly two if
1464    constants are involved.  For example, convert
1465      (A | B) & (A | C) into A | (B & C)
1466    Further simplification will occur if B and C are constants.  */
1467 (for op (bit_and bit_ior bit_xor)
1468      rop (bit_ior bit_and bit_and)
1469  (simplify
1470   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1471   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1472        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1473    (rop (convert @0) (op (convert @1) (convert @2))))))
1474
1475 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1476 /* (X & Y) & Y -> X & Y
1477    (X | Y) | Y -> X | Y  */
1478 (for op (bit_and bit_ior)
1479  (simplify
1480   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1481   @2))
1482 /* (X ^ Y) ^ Y -> X  */
1483 (simplify
1484  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1485  (convert @0))
1486 /* (X & Y) & (X & Z) -> (X & Y) & Z
1487    (X | Y) | (X | Z) -> (X | Y) | Z  */
1488 (for op (bit_and bit_ior)
1489  (simplify
1490   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1491   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1492        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1493    (if (single_use (@5) && single_use (@6))
1494     (op @3 (convert @2))
1495     (if (single_use (@3) && single_use (@4))
1496      (op (convert @1) @5))))))
1497 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1498 (simplify
1499  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1500  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1501       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1502   (bit_xor (convert @1) (convert @2))))
1503
1504 /* Convert abs (abs (X)) into abs (X).
1505    also absu (absu (X)) into absu (X).  */
1506 (simplify
1507  (abs (abs@1 @0))
1508  @1)
1509
1510 (simplify
1511  (absu (convert@2 (absu@1 @0)))
1512  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1513   @1))
1514
1515 /* Convert abs[u] (-X) -> abs[u] (X).  */
1516 (simplify
1517  (abs (negate @0))
1518  (abs @0))
1519
1520 (simplify
1521  (absu (negate @0))
1522  (absu @0))
1523
1524 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1525 (simplify
1526  (abs tree_expr_nonnegative_p@0)
1527  @0)
1528
1529 (simplify
1530  (absu tree_expr_nonnegative_p@0)
1531  (convert @0))
1532
1533 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1534 (simplify
1535  (mult:c (nop_convert1?
1536           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1537                     integer_onep))
1538          (nop_convert3? @0))
1539  (if (INTEGRAL_TYPE_P (type)
1540       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1541       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1542   (if (TYPE_UNSIGNED (type))
1543    (absu @0)
1544    (abs @0)
1545   )
1546  )
1547 )
1548
1549 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1550 (match negate_expr_p
1551  INTEGER_CST
1552  (if ((INTEGRAL_TYPE_P (type)
1553        && TYPE_UNSIGNED (type))
1554       || (!TYPE_OVERFLOW_SANITIZED (type)
1555           && may_negate_without_overflow_p (t)))))
1556 (match negate_expr_p
1557  FIXED_CST)
1558 (match negate_expr_p
1559  (negate @0)
1560  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1561 (match negate_expr_p
1562  REAL_CST
1563  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1564 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1565    ways.  */
1566 (match negate_expr_p
1567  VECTOR_CST
1568  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1569 (match negate_expr_p
1570  (minus @0 @1)
1571  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1572       || (FLOAT_TYPE_P (type)
1573           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1574           && !HONOR_SIGNED_ZEROS (type)))))
1575
1576 /* (-A) * (-B) -> A * B  */
1577 (simplify
1578  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1579   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1580        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1581    (mult (convert @0) (convert (negate @1)))))
1582
1583 /* -(A + B) -> (-B) - A.  */
1584 (simplify
1585  (negate (plus:c @0 negate_expr_p@1))
1586  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1587       && !HONOR_SIGNED_ZEROS (type))
1588   (minus (negate @1) @0)))
1589
1590 /* -(A - B) -> B - A.  */
1591 (simplify
1592  (negate (minus @0 @1))
1593  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1594       || (FLOAT_TYPE_P (type)
1595           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1596           && !HONOR_SIGNED_ZEROS (type)))
1597   (minus @1 @0)))
1598 (simplify
1599  (negate (pointer_diff @0 @1))
1600  (if (TYPE_OVERFLOW_UNDEFINED (type))
1601   (pointer_diff @1 @0)))
1602
1603 /* A - B -> A + (-B) if B is easily negatable.  */
1604 (simplify
1605  (minus @0 negate_expr_p@1)
1606  (if (!FIXED_POINT_TYPE_P (type))
1607  (plus @0 (negate @1))))
1608
1609 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
1610 (simplify
1611  (negate (mult:c@0 @1 negate_expr_p@2))
1612  (if (! TYPE_UNSIGNED (type)
1613       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1614       && single_use (@0))
1615   (mult @1 (negate @2))))
1616
1617 (simplify
1618  (negate (rdiv@0 @1 negate_expr_p@2))
1619  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1620       && single_use (@0))
1621   (rdiv @1 (negate @2))))
1622
1623 (simplify
1624  (negate (rdiv@0 negate_expr_p@1 @2))
1625  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1626       && single_use (@0))
1627   (rdiv (negate @1) @2)))
1628
1629 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
1630 (simplify
1631  (negate (convert? (rshift @0 INTEGER_CST@1)))
1632  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1633       && wi::to_wide (@1) == element_precision (type) - 1)
1634   (with { tree stype = TREE_TYPE (@0);
1635           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1636                                              : unsigned_type_for (stype); }
1637    (if (VECTOR_TYPE_P (type))
1638     (view_convert (rshift (view_convert:ntype @0) @1))
1639     (convert (rshift (convert:ntype @0) @1))))))
1640
1641 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1642    when profitable.
1643    For bitwise binary operations apply operand conversions to the
1644    binary operation result instead of to the operands.  This allows
1645    to combine successive conversions and bitwise binary operations.
1646    We combine the above two cases by using a conditional convert.  */
1647 (for bitop (bit_and bit_ior bit_xor)
1648  (simplify
1649   (bitop (convert@2 @0) (convert?@3 @1))
1650   (if (((TREE_CODE (@1) == INTEGER_CST
1651          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1652          && (int_fits_type_p (@1, TREE_TYPE (@0))
1653              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1654         || types_match (@0, @1))
1655        /* ???  This transform conflicts with fold-const.cc doing
1656           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1657           constants (if x has signed type, the sign bit cannot be set
1658           in c).  This folds extension into the BIT_AND_EXPR.
1659           Restrict it to GIMPLE to avoid endless recursions.  */
1660        && (bitop != BIT_AND_EXPR || GIMPLE)
1661        && (/* That's a good idea if the conversion widens the operand, thus
1662               after hoisting the conversion the operation will be narrower.
1663               It is also a good if the conversion is a nop as moves the
1664               conversion to one side; allowing for combining of the conversions.  */
1665            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1666            /* The conversion check for being a nop can only be done at the gimple
1667               level as fold_binary has some re-association code which can conflict
1668               with this if there is a "constant" which is not a full INTEGER_CST.  */
1669            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1670            /* It's also a good idea if the conversion is to a non-integer
1671               mode.  */
1672            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1673            /* Or if the precision of TO is not the same as the precision
1674               of its mode.  */
1675            || !type_has_mode_precision_p (type)
1676            /* In GIMPLE, getting rid of 2 conversions for one new results
1677               in smaller IL.  */
1678            || (GIMPLE
1679                && TREE_CODE (@1) != INTEGER_CST
1680                && tree_nop_conversion_p (type, TREE_TYPE (@0))
1681                && single_use (@2)
1682                && single_use (@3))))
1683    (convert (bitop @0 (convert @1)))))
1684  /* In GIMPLE, getting rid of 2 conversions for one new results
1685     in smaller IL.  */
1686  (simplify
1687   (convert (bitop:cs@2 (nop_convert:s @0) @1))
1688   (if (GIMPLE
1689        && TREE_CODE (@1) != INTEGER_CST
1690        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1691        && types_match (type, @0))
1692    (bitop @0 (convert @1)))))
1693
1694 (for bitop (bit_and bit_ior)
1695      rbitop (bit_ior bit_and)
1696   /* (x | y) & x -> x */
1697   /* (x & y) | x -> x */
1698  (simplify
1699   (bitop:c (rbitop:c @0 @1) @0)
1700   @0)
1701  /* (~x | y) & x -> x & y */
1702  /* (~x & y) | x -> x | y */
1703  (simplify
1704   (bitop:c (rbitop:c (bit_not @0) @1) @0)
1705   (bitop @0 @1)))
1706
1707 /* ((x | y) & z) | x -> (z & y) | x */
1708 (simplify
1709   (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1710   (bit_ior (bit_and @2 @1) @0))
1711
1712 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1713 (simplify
1714   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1715   (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1716
1717 /* Combine successive equal operations with constants.  */
1718 (for bitop (bit_and bit_ior bit_xor)
1719  (simplify
1720   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1721   (if (!CONSTANT_CLASS_P (@0))
1722    /* This is the canonical form regardless of whether (bitop @1 @2) can be
1723       folded to a constant.  */
1724    (bitop @0 (bitop @1 @2))
1725    /* In this case we have three constants and (bitop @0 @1) doesn't fold
1726       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
1727       the values involved are such that the operation can't be decided at
1728       compile time.  Try folding one of @0 or @1 with @2 to see whether
1729       that combination can be decided at compile time.
1730
1731       Keep the existing form if both folds fail, to avoid endless
1732       oscillation.  */
1733    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1734     (if (cst1)
1735      (bitop @1 { cst1; })
1736      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1737       (if (cst2)
1738        (bitop @0 { cst2; }))))))))
1739
1740 /* Try simple folding for X op !X, and X op X with the help
1741    of the truth_valued_p and logical_inverted_value predicates.  */
1742 (match truth_valued_p
1743  @0
1744  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1745 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1746  (match truth_valued_p
1747   (op @0 @1)))
1748 (match truth_valued_p
1749   (truth_not @0))
1750
1751 (match (logical_inverted_value @0)
1752  (truth_not @0))
1753 (match (logical_inverted_value @0)
1754  (bit_not truth_valued_p@0))
1755 (match (logical_inverted_value @0)
1756  (eq @0 integer_zerop))
1757 (match (logical_inverted_value @0)
1758  (ne truth_valued_p@0 integer_truep))
1759 (match (logical_inverted_value @0)
1760  (bit_xor truth_valued_p@0 integer_truep))
1761
1762 /* X & !X -> 0.  */
1763 (simplify
1764  (bit_and:c @0 (logical_inverted_value @0))
1765  { build_zero_cst (type); })
1766 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
1767 (for op (bit_ior bit_xor)
1768  (simplify
1769   (op:c truth_valued_p@0 (logical_inverted_value @0))
1770   { constant_boolean_node (true, type); }))
1771 /* X ==/!= !X is false/true.  */
1772 (for op (eq ne)
1773  (simplify
1774   (op:c truth_valued_p@0 (logical_inverted_value @0))
1775   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1776
1777 /* ~~x -> x */
1778 (simplify
1779   (bit_not (bit_not @0))
1780   @0)
1781
1782 (match zero_one_valued_p
1783  @0
1784  (if (INTEGRAL_TYPE_P (type) && tree_nonzero_bits (@0) == 1)))
1785 (match zero_one_valued_p
1786  truth_valued_p@0)
1787
1788 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
1789 (simplify
1790  (mult zero_one_valued_p@0 zero_one_valued_p@1)
1791  (if (INTEGRAL_TYPE_P (type))
1792   (bit_and @0 @1)))
1793
1794 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
1795 (simplify
1796  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
1797  (if (INTEGRAL_TYPE_P (type)
1798       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1799       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
1800       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1801   (mult (convert @0) @1)))
1802
1803 /* Convert ~ (-A) to A - 1.  */
1804 (simplify
1805  (bit_not (convert? (negate @0)))
1806  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1807       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1808   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1809
1810 /* Convert - (~A) to A + 1.  */
1811 (simplify
1812  (negate (nop_convert? (bit_not @0)))
1813  (plus (view_convert @0) { build_each_one_cst (type); }))
1814
1815 /* (a & b) ^ (a == b) -> !(a | b) */
1816 /* (a & b) == (a ^ b) -> !(a | b) */
1817 (for first_op (bit_xor eq)
1818      second_op (eq bit_xor)
1819  (simplify
1820   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
1821     (bit_not (bit_ior @0 @1))))
1822
1823 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
1824 (simplify
1825  (bit_not (convert? (minus @0 integer_each_onep)))
1826  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1827       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1828   (convert (negate @0))))
1829 (simplify
1830  (bit_not (convert? (plus @0 integer_all_onesp)))
1831  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1832       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1833   (convert (negate @0))))
1834
1835 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
1836 (simplify
1837  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1838  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1839   (convert (bit_xor @0 (bit_not @1)))))
1840 (simplify
1841  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1842  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1843   (convert (bit_xor @0 @1))))
1844
1845 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
1846 (simplify
1847  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1848  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1849   (bit_not (bit_xor (view_convert @0) @1))))
1850
1851 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1852 (simplify
1853  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1854  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1855
1856 /* Fold A - (A & B) into ~B & A.  */
1857 (simplify
1858  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1859  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1860       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1861   (convert (bit_and (bit_not @1) @0))))
1862
1863 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
1864 (if (!canonicalize_math_p ())
1865  (for cmp (gt lt ge le)
1866   (simplify
1867    (mult (convert (cmp @0 @1)) @2)
1868    (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1869
1870 /* For integral types with undefined overflow and C != 0 fold
1871    x * C EQ/NE y * C into x EQ/NE y.  */
1872 (for cmp (eq ne)
1873  (simplify
1874   (cmp (mult:c @0 @1) (mult:c @2 @1))
1875   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1876        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1877        && tree_expr_nonzero_p (@1))
1878    (cmp @0 @2))))
1879
1880 /* For integral types with wrapping overflow and C odd fold
1881    x * C EQ/NE y * C into x EQ/NE y.  */
1882 (for cmp (eq ne)
1883  (simplify
1884   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1885   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1886        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1887        && (TREE_INT_CST_LOW (@1) & 1) != 0)
1888    (cmp @0 @2))))
1889
1890 /* For integral types with undefined overflow and C != 0 fold
1891    x * C RELOP y * C into:
1892
1893    x RELOP y for nonnegative C
1894    y RELOP x for negative C  */
1895 (for cmp (lt gt le ge)
1896  (simplify
1897   (cmp (mult:c @0 @1) (mult:c @2 @1))
1898   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1899        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1900    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1901     (cmp @0 @2)
1902    (if (TREE_CODE (@1) == INTEGER_CST
1903         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1904     (cmp @2 @0))))))
1905
1906 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
1907 (for cmp (le gt)
1908      icmp (gt le)
1909  (simplify
1910   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1911    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1912         && TYPE_UNSIGNED (TREE_TYPE (@0))
1913         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1914         && (wi::to_wide (@2)
1915             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1916     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1917      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1918
1919 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
1920 (for cmp (simple_comparison)
1921  (simplify
1922   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1923   (if (element_precision (@3) >= element_precision (@0)
1924        && types_match (@0, @1))
1925    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1926     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1927      (cmp @1 @0)
1928      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1929       (with
1930        {
1931         tree utype = unsigned_type_for (TREE_TYPE (@0));
1932        }
1933        (cmp (convert:utype @1) (convert:utype @0)))))
1934     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1935      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1936       (cmp @0 @1)
1937       (with
1938        {
1939         tree utype = unsigned_type_for (TREE_TYPE (@0));
1940        }
1941        (cmp (convert:utype @0) (convert:utype @1)))))))))
1942
1943 /* X / C1 op C2 into a simple range test.  */
1944 (for cmp (simple_comparison)
1945  (simplify
1946   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1947   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1948        && integer_nonzerop (@1)
1949        && !TREE_OVERFLOW (@1)
1950        && !TREE_OVERFLOW (@2))
1951    (with { tree lo, hi; bool neg_overflow;
1952            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1953                                                    &neg_overflow); }
1954     (switch
1955      (if (code == LT_EXPR || code == GE_EXPR)
1956        (if (TREE_OVERFLOW (lo))
1957         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1958         (if (code == LT_EXPR)
1959          (lt @0 { lo; })
1960          (ge @0 { lo; }))))
1961      (if (code == LE_EXPR || code == GT_EXPR)
1962        (if (TREE_OVERFLOW (hi))
1963         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1964         (if (code == LE_EXPR)
1965          (le @0 { hi; })
1966          (gt @0 { hi; }))))
1967      (if (!lo && !hi)
1968       { build_int_cst (type, code == NE_EXPR); })
1969      (if (code == EQ_EXPR && !hi)
1970       (ge @0 { lo; }))
1971      (if (code == EQ_EXPR && !lo)
1972       (le @0 { hi; }))
1973      (if (code == NE_EXPR && !hi)
1974       (lt @0 { lo; }))
1975      (if (code == NE_EXPR && !lo)
1976       (gt @0 { hi; }))
1977      (if (GENERIC)
1978       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1979                            lo, hi); })
1980      (with
1981       {
1982         tree etype = range_check_type (TREE_TYPE (@0));
1983         if (etype)
1984           {
1985             hi = fold_convert (etype, hi);
1986             lo = fold_convert (etype, lo);
1987             hi = const_binop (MINUS_EXPR, etype, hi, lo);
1988           }
1989       }
1990       (if (etype && hi && !TREE_OVERFLOW (hi))
1991        (if (code == EQ_EXPR)
1992         (le (minus (convert:etype @0) { lo; }) { hi; })
1993         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1994
1995 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
1996 (for op (lt le ge gt)
1997  (simplify
1998   (op (plus:c @0 @2) (plus:c @1 @2))
1999   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2000        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2001    (op @0 @1))))
2002 /* For equality and subtraction, this is also true with wrapping overflow.  */
2003 (for op (eq ne minus)
2004  (simplify
2005   (op (plus:c @0 @2) (plus:c @1 @2))
2006   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2007        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2008            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2009    (op @0 @1))))
2010
2011 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2012 (for op (lt le ge gt)
2013  (simplify
2014   (op (minus @0 @2) (minus @1 @2))
2015   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2016        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2017    (op @0 @1))))
2018 /* For equality and subtraction, this is also true with wrapping overflow.  */
2019 (for op (eq ne minus)
2020  (simplify
2021   (op (minus @0 @2) (minus @1 @2))
2022   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2023        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2024            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2025    (op @0 @1))))
2026 /* And for pointers...  */
2027 (for op (simple_comparison)
2028  (simplify
2029   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2030   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2031    (op @0 @1))))
2032 (simplify
2033  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2034  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2035       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2036   (pointer_diff @0 @1)))
2037
2038 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2039 (for op (lt le ge gt)
2040  (simplify
2041   (op (minus @2 @0) (minus @2 @1))
2042   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2043        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2044    (op @1 @0))))
2045 /* For equality and subtraction, this is also true with wrapping overflow.  */
2046 (for op (eq ne minus)
2047  (simplify
2048   (op (minus @2 @0) (minus @2 @1))
2049   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2050        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2051            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2052    (op @1 @0))))
2053 /* And for pointers...  */
2054 (for op (simple_comparison)
2055  (simplify
2056   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2057   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2058    (op @1 @0))))
2059 (simplify
2060  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2061  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2062       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2063   (pointer_diff @1 @0)))
2064
2065 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2066 (for op (lt le gt ge)
2067  (simplify
2068   (op:c (plus:c@2 @0 @1) @1)
2069   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2070        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2071        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2072        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2073    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2074 /* For equality, this is also true with wrapping overflow.  */
2075 (for op (eq ne)
2076  (simplify
2077   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2078   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2079        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2080            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2081        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2082        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2083        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2084    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2085  (simplify
2086   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2087   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2088        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2089        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2090    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2091
2092 /* X - Y < X is the same as Y > 0 when there is no overflow.
2093    For equality, this is also true with wrapping overflow.  */
2094 (for op (simple_comparison)
2095  (simplify
2096   (op:c @0 (minus@2 @0 @1))
2097   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2098        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2099            || ((op == EQ_EXPR || op == NE_EXPR)
2100                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2101        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2102    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2103
2104 /* Transform:
2105    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2106    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2107 (for cmp (eq ne)
2108      ocmp (lt ge)
2109  (simplify
2110   (cmp (trunc_div @0 @1) integer_zerop)
2111   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2112        /* Complex ==/!= is allowed, but not </>=.  */
2113        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2114        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2115    (ocmp @0 @1))))
2116
2117 /* X == C - X can never be true if C is odd.  */
2118 (for cmp (eq ne)
2119  (simplify
2120   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2121   (if (TREE_INT_CST_LOW (@1) & 1)
2122    { constant_boolean_node (cmp == NE_EXPR, type); })))
2123
2124 /* Arguments on which one can call get_nonzero_bits to get the bits
2125    possibly set.  */
2126 (match with_possible_nonzero_bits
2127  INTEGER_CST@0)
2128 (match with_possible_nonzero_bits
2129  SSA_NAME@0
2130  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2131 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2132 (match (with_possible_nonzero_bits2 @0)
2133  with_possible_nonzero_bits@0)
2134 (match (with_possible_nonzero_bits2 @0)
2135  (bit_and:c with_possible_nonzero_bits@0 @2))
2136
2137 /* Same for bits that are known to be set, but we do not have
2138    an equivalent to get_nonzero_bits yet.  */
2139 (match (with_certain_nonzero_bits2 @0)
2140  INTEGER_CST@0)
2141 (match (with_certain_nonzero_bits2 @0)
2142  (bit_ior @1 INTEGER_CST@0))
2143
2144 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2145 (for cmp (eq ne)
2146  (simplify
2147   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2148   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2149    { constant_boolean_node (cmp == NE_EXPR, type); })))
2150
2151 /* ((X inner_op C0) outer_op C1)
2152    With X being a tree where value_range has reasoned certain bits to always be
2153    zero throughout its computed value range,
2154    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2155    where zero_mask has 1's for all bits that are sure to be 0 in
2156    and 0's otherwise.
2157    if (inner_op == '^') C0 &= ~C1;
2158    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2159    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2160 */
2161 (for inner_op (bit_ior bit_xor)
2162      outer_op (bit_xor bit_ior)
2163 (simplify
2164  (outer_op
2165   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2166  (with
2167   {
2168     bool fail = false;
2169     wide_int zero_mask_not;
2170     wide_int C0;
2171     wide_int cst_emit;
2172
2173     if (TREE_CODE (@2) == SSA_NAME)
2174       zero_mask_not = get_nonzero_bits (@2);
2175     else
2176       fail = true;
2177
2178     if (inner_op == BIT_XOR_EXPR)
2179       {
2180         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2181         cst_emit = C0 | wi::to_wide (@1);
2182       }
2183     else
2184       {
2185         C0 = wi::to_wide (@0);
2186         cst_emit = C0 ^ wi::to_wide (@1);
2187       }
2188   }
2189   (if (!fail && (C0 & zero_mask_not) == 0)
2190    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2191    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2192     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2193
2194 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2195 (simplify
2196   (pointer_plus (pointer_plus:s @0 @1) @3)
2197   (pointer_plus @0 (plus @1 @3)))
2198 #if GENERIC
2199 (simplify
2200   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2201   (convert:type (pointer_plus @0 (plus @1 @3))))
2202 #endif
2203
2204 /* Pattern match
2205      tem1 = (long) ptr1;
2206      tem2 = (long) ptr2;
2207      tem3 = tem2 - tem1;
2208      tem4 = (unsigned long) tem3;
2209      tem5 = ptr1 + tem4;
2210    and produce
2211      tem5 = ptr2;  */
2212 (simplify
2213   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2214   /* Conditionally look through a sign-changing conversion.  */
2215   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2216        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2217             || (GENERIC && type == TREE_TYPE (@1))))
2218    @1))
2219 (simplify
2220   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2221   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2222    (convert @1)))
2223
2224 /* Pattern match
2225      tem = (sizetype) ptr;
2226      tem = tem & algn;
2227      tem = -tem;
2228      ... = ptr p+ tem;
2229    and produce the simpler and easier to analyze with respect to alignment
2230      ... = ptr & ~algn;  */
2231 (simplify
2232   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2233   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2234    (bit_and @0 { algn; })))
2235
2236 /* Try folding difference of addresses.  */
2237 (simplify
2238  (minus (convert ADDR_EXPR@0) (convert @1))
2239  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2240   (with { poly_int64 diff; }
2241    (if (ptr_difference_const (@0, @1, &diff))
2242     { build_int_cst_type (type, diff); }))))
2243 (simplify
2244  (minus (convert @0) (convert ADDR_EXPR@1))
2245  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2246   (with { poly_int64 diff; }
2247    (if (ptr_difference_const (@0, @1, &diff))
2248     { build_int_cst_type (type, diff); }))))
2249 (simplify
2250  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2251  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2252       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2253   (with { poly_int64 diff; }
2254    (if (ptr_difference_const (@0, @1, &diff))
2255     { build_int_cst_type (type, diff); }))))
2256 (simplify
2257  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2258  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2259       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2260   (with { poly_int64 diff; }
2261    (if (ptr_difference_const (@0, @1, &diff))
2262     { build_int_cst_type (type, diff); }))))
2263
2264 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2265 (simplify
2266  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2267  (with { poly_int64 diff; }
2268    (if (ptr_difference_const (@0, @2, &diff))
2269     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2270
2271 /* (&a+b) !=/== (&a[1] + c) ->  sizeof(a[0]) + b !=/== c */
2272 (for neeq (ne eq)
2273  (simplify
2274   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2275    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2276     (if (ptr_difference_const (@0, @2, &diff))
2277      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2278
2279 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2280 (simplify
2281  (convert (pointer_diff @0 INTEGER_CST@1))
2282  (if (POINTER_TYPE_P (type))
2283   { build_fold_addr_expr_with_type
2284       (build2 (MEM_REF, char_type_node, @0,
2285                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2286                type); }))
2287
2288 /* If arg0 is derived from the address of an object or function, we may
2289    be able to fold this expression using the object or function's
2290    alignment.  */
2291 (simplify
2292  (bit_and (convert? @0) INTEGER_CST@1)
2293  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2294       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2295   (with
2296    {
2297      unsigned int align;
2298      unsigned HOST_WIDE_INT bitpos;
2299      get_pointer_alignment_1 (@0, &align, &bitpos);
2300    }
2301    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2302     { wide_int_to_tree (type, (wi::to_wide (@1)
2303                                & (bitpos / BITS_PER_UNIT))); }))))
2304
2305 (match min_value
2306  INTEGER_CST
2307  (if (INTEGRAL_TYPE_P (type)
2308       && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2309
2310 (match max_value
2311  INTEGER_CST
2312  (if (INTEGRAL_TYPE_P (type)
2313       && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2314
2315 /* x >  y  &&  x != XXX_MIN  -->  x > y
2316    x >  y  &&  x == XXX_MIN  -->  false . */
2317 (for eqne (eq ne)
2318  (simplify
2319   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2320    (switch
2321     (if (eqne == EQ_EXPR)
2322      { constant_boolean_node (false, type); })
2323     (if (eqne == NE_EXPR)
2324      @2)
2325     )))
2326
2327 /* x <  y  &&  x != XXX_MAX  -->  x < y
2328    x <  y  &&  x == XXX_MAX  -->  false.  */
2329 (for eqne (eq ne)
2330  (simplify
2331   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2332    (switch
2333     (if (eqne == EQ_EXPR)
2334      { constant_boolean_node (false, type); })
2335     (if (eqne == NE_EXPR)
2336      @2)
2337     )))
2338
2339 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2340 (simplify
2341  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2342   @2)
2343
2344 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2345 (simplify
2346  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2347   @2)
2348
2349 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2350 (simplify
2351  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2352   @2)
2353
2354 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2355 (simplify
2356  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2357   { constant_boolean_node (true, type); })
2358
2359 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2360 (simplify
2361  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2362   @2)
2363
2364 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2365 (simplify
2366  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2367   @2)
2368
2369 /* x >=  y  ||  x != XXX_MAX   -->  true
2370    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2371 (for eqne (eq ne)
2372  (simplify
2373   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2374    (switch
2375     (if (eqne == EQ_EXPR)
2376      @2)
2377     (if (eqne == NE_EXPR)
2378      { constant_boolean_node (true, type); }))))
2379
2380 /* y == XXX_MIN || x < y --> x <= y - 1 */
2381 (simplify
2382  (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
2383   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2384        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2385   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2386
2387 /* y != XXX_MIN && x >= y --> x > y - 1 */
2388 (simplify
2389  (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
2390   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2391        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2392   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2393
2394 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2395    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2396
2397 (for code1 (eq ne)
2398  (for code2 (eq ne lt gt le ge)
2399   (simplify
2400    (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2401     (with
2402      {
2403       int cmp = tree_int_cst_compare (@1, @2);
2404       bool val;
2405       switch (code2)
2406          {
2407         case EQ_EXPR: val = (cmp == 0); break;
2408         case NE_EXPR: val = (cmp != 0); break;
2409         case LT_EXPR: val = (cmp < 0); break;
2410         case GT_EXPR: val = (cmp > 0); break;
2411         case LE_EXPR: val = (cmp <= 0); break;
2412         case GE_EXPR: val = (cmp >= 0); break;
2413         default: gcc_unreachable ();
2414         }
2415      }
2416      (switch
2417       (if (code1 == EQ_EXPR && val) @3)
2418       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2419       (if (code1 == NE_EXPR && !val) @4))))))
2420
2421 /* Convert (X OP1 CST1) && (X OP2 CST2).  */
2422
2423 (for code1 (lt le gt ge)
2424  (for code2 (lt le gt ge)
2425   (simplify
2426   (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2427    (with
2428     {
2429      int cmp = tree_int_cst_compare (@1, @2);
2430     }
2431     (switch
2432      /* Choose the more restrictive of two < or <= comparisons.  */
2433      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2434           && (code2 == LT_EXPR || code2 == LE_EXPR))
2435       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2436        @3
2437        @4))
2438      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2439      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2440           && (code2 == GT_EXPR || code2 == GE_EXPR))
2441       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2442        @3
2443        @4))
2444      /* Check for singleton ranges.  */
2445      (if (cmp == 0
2446           && ((code1 == LE_EXPR && code2 == GE_EXPR)
2447             || (code1 == GE_EXPR && code2 == LE_EXPR)))
2448       (eq @0 @1))
2449      /* Check for disjoint ranges.  */
2450      (if (cmp <= 0
2451           && (code1 == LT_EXPR || code1 == LE_EXPR)
2452           && (code2 == GT_EXPR || code2 == GE_EXPR))
2453       { constant_boolean_node (false, type); })
2454      (if (cmp >= 0
2455           && (code1 == GT_EXPR || code1 == GE_EXPR)
2456           && (code2 == LT_EXPR || code2 == LE_EXPR))
2457       { constant_boolean_node (false, type); })
2458      )))))
2459
2460 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2461    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2462
2463 (for code1 (eq ne)
2464  (for code2 (eq ne lt gt le ge)
2465   (simplify
2466    (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2467     (with
2468      {
2469       int cmp = tree_int_cst_compare (@1, @2);
2470       bool val;
2471       switch (code2)
2472         {
2473         case EQ_EXPR: val = (cmp == 0); break;
2474         case NE_EXPR: val = (cmp != 0); break;
2475         case LT_EXPR: val = (cmp < 0); break;
2476         case GT_EXPR: val = (cmp > 0); break;
2477         case LE_EXPR: val = (cmp <= 0); break;
2478         case GE_EXPR: val = (cmp >= 0); break;
2479         default: gcc_unreachable ();
2480         }
2481      }
2482      (switch
2483       (if (code1 == EQ_EXPR && val) @4)
2484       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2485       (if (code1 == NE_EXPR && !val) @3))))))
2486
2487 /* Convert (X OP1 CST1) || (X OP2 CST2).  */
2488
2489 (for code1 (lt le gt ge)
2490  (for code2 (lt le gt ge)
2491   (simplify
2492   (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2493    (with
2494     {
2495      int cmp = tree_int_cst_compare (@1, @2);
2496     }
2497     (switch
2498      /* Choose the more restrictive of two < or <= comparisons.  */
2499      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2500           && (code2 == LT_EXPR || code2 == LE_EXPR))
2501       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2502        @4
2503        @3))
2504      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2505      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2506           && (code2 == GT_EXPR || code2 == GE_EXPR))
2507       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2508        @4
2509        @3))
2510      /* Check for singleton ranges.  */
2511      (if (cmp == 0
2512           && ((code1 == LT_EXPR && code2 == GT_EXPR)
2513               || (code1 == GT_EXPR && code2 == LT_EXPR)))
2514       (ne @0 @2))
2515      /* Check for disjoint ranges.  */
2516      (if (cmp >= 0
2517           && (code1 == LT_EXPR || code1 == LE_EXPR)
2518           && (code2 == GT_EXPR || code2 == GE_EXPR))
2519       { constant_boolean_node (true, type); })
2520      (if (cmp <= 0
2521           && (code1 == GT_EXPR || code1 == GE_EXPR)
2522           && (code2 == LT_EXPR || code2 == LE_EXPR))
2523       { constant_boolean_node (true, type); })
2524      )))))
2525
2526 /* We can't reassociate at all for saturating types.  */
2527 (if (!TYPE_SATURATING (type))
2528
2529  /* Contract negates.  */
2530  /* A + (-B) -> A - B */
2531  (simplify
2532   (plus:c @0 (convert? (negate @1)))
2533   /* Apply STRIP_NOPS on the negate.  */
2534   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2535        && !TYPE_OVERFLOW_SANITIZED (type))
2536    (with
2537     {
2538      tree t1 = type;
2539      if (INTEGRAL_TYPE_P (type)
2540          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2541        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2542     }
2543     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2544  /* A - (-B) -> A + B */
2545  (simplify
2546   (minus @0 (convert? (negate @1)))
2547   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2548        && !TYPE_OVERFLOW_SANITIZED (type))
2549    (with
2550     {
2551      tree t1 = type;
2552      if (INTEGRAL_TYPE_P (type)
2553          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2554        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2555     }
2556     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2557  /* -(T)(-A) -> (T)A
2558     Sign-extension is ok except for INT_MIN, which thankfully cannot
2559     happen without overflow.  */
2560  (simplify
2561   (negate (convert (negate @1)))
2562   (if (INTEGRAL_TYPE_P (type)
2563        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2564            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2565                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2566        && !TYPE_OVERFLOW_SANITIZED (type)
2567        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2568    (convert @1)))
2569  (simplify
2570   (negate (convert negate_expr_p@1))
2571   (if (SCALAR_FLOAT_TYPE_P (type)
2572        && ((DECIMAL_FLOAT_TYPE_P (type)
2573             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2574             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2575            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2576    (convert (negate @1))))
2577  (simplify
2578   (negate (nop_convert? (negate @1)))
2579   (if (!TYPE_OVERFLOW_SANITIZED (type)
2580        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2581    (view_convert @1)))
2582
2583  /* We can't reassociate floating-point unless -fassociative-math
2584     or fixed-point plus or minus because of saturation to +-Inf.  */
2585  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2586       && !FIXED_POINT_TYPE_P (type))
2587
2588   /* Match patterns that allow contracting a plus-minus pair
2589      irrespective of overflow issues.  */
2590   /* (A +- B) - A       ->  +- B */
2591   /* (A +- B) -+ B      ->  A */
2592   /* A - (A +- B)       -> -+ B */
2593   /* A +- (B -+ A)      ->  +- B */
2594   (simplify
2595    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2596    (view_convert @1))
2597   (simplify
2598    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2599    (if (!ANY_INTEGRAL_TYPE_P (type)
2600         || TYPE_OVERFLOW_WRAPS (type))
2601    (negate (view_convert @1))
2602    (view_convert (negate @1))))
2603   (simplify
2604    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2605    (view_convert @0))
2606   (simplify
2607    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2608     (if (!ANY_INTEGRAL_TYPE_P (type)
2609          || TYPE_OVERFLOW_WRAPS (type))
2610      (negate (view_convert @1))
2611      (view_convert (negate @1))))
2612   (simplify
2613    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2614    (view_convert @1))
2615   /* (A +- B) + (C - A)   -> C +- B */
2616   /* (A +  B) - (A - C)   -> B + C */
2617   /* More cases are handled with comparisons.  */
2618   (simplify
2619    (plus:c (plus:c @0 @1) (minus @2 @0))
2620    (plus @2 @1))
2621   (simplify
2622    (plus:c (minus @0 @1) (minus @2 @0))
2623    (minus @2 @1))
2624   (simplify
2625    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2626    (if (TYPE_OVERFLOW_UNDEFINED (type)
2627         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2628     (pointer_diff @2 @1)))
2629   (simplify
2630    (minus (plus:c @0 @1) (minus @0 @2))
2631    (plus @1 @2))
2632
2633   /* (A +- CST1) +- CST2 -> A + CST3
2634      Use view_convert because it is safe for vectors and equivalent for
2635      scalars.  */
2636   (for outer_op (plus minus)
2637    (for inner_op (plus minus)
2638         neg_inner_op (minus plus)
2639     (simplify
2640      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2641                CONSTANT_CLASS_P@2)
2642      /* If one of the types wraps, use that one.  */
2643      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2644       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2645          forever if something doesn't simplify into a constant.  */
2646       (if (!CONSTANT_CLASS_P (@0))
2647        (if (outer_op == PLUS_EXPR)
2648         (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2649         (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2650       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2651            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2652        (if (outer_op == PLUS_EXPR)
2653         (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2654         (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2655        /* If the constant operation overflows we cannot do the transform
2656           directly as we would introduce undefined overflow, for example
2657           with (a - 1) + INT_MIN.  */
2658        (if (types_match (type, @0))
2659         (with { tree cst = const_binop (outer_op == inner_op
2660                                         ? PLUS_EXPR : MINUS_EXPR,
2661                                         type, @1, @2); }
2662          (if (cst && !TREE_OVERFLOW (cst))
2663           (inner_op @0 { cst; } )
2664           /* X+INT_MAX+1 is X-INT_MIN.  */
2665           (if (INTEGRAL_TYPE_P (type) && cst
2666                && wi::to_wide (cst) == wi::min_value (type))
2667            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2668            /* Last resort, use some unsigned type.  */
2669            (with { tree utype = unsigned_type_for (type); }
2670             (if (utype)
2671              (view_convert (inner_op
2672                             (view_convert:utype @0)
2673                             (view_convert:utype
2674                              { drop_tree_overflow (cst); }))))))))))))))
2675
2676   /* (CST1 - A) +- CST2 -> CST3 - A  */
2677   (for outer_op (plus minus)
2678    (simplify
2679     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2680     /* If one of the types wraps, use that one.  */
2681     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2682      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2683         forever if something doesn't simplify into a constant.  */
2684      (if (!CONSTANT_CLASS_P (@0))
2685       (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2686      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2687           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2688       (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2689       (if (types_match (type, @0))
2690        (with { tree cst = const_binop (outer_op, type, @1, @2); }
2691         (if (cst && !TREE_OVERFLOW (cst))
2692          (minus { cst; } @0))))))))
2693
2694   /* CST1 - (CST2 - A) -> CST3 + A
2695      Use view_convert because it is safe for vectors and equivalent for
2696      scalars.  */
2697   (simplify
2698    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2699    /* If one of the types wraps, use that one.  */
2700    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2701     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2702       forever if something doesn't simplify into a constant.  */
2703     (if (!CONSTANT_CLASS_P (@0))
2704      (plus (view_convert @0) (minus @1 (view_convert @2))))
2705     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2706          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2707      (view_convert (plus @0 (minus (view_convert @1) @2)))
2708      (if (types_match (type, @0))
2709       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2710        (if (cst && !TREE_OVERFLOW (cst))
2711         (plus { cst; } @0)))))))
2712
2713 /* ((T)(A)) + CST -> (T)(A + CST)  */
2714 #if GIMPLE
2715   (simplify
2716    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2717     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2718          && TREE_CODE (type) == INTEGER_TYPE
2719          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2720          && int_fits_type_p (@1, TREE_TYPE (@0)))
2721      /* Perform binary operation inside the cast if the constant fits
2722         and (A + CST)'s range does not overflow.  */
2723      (with
2724       {
2725         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2726                           max_ovf = wi::OVF_OVERFLOW;
2727         tree inner_type = TREE_TYPE (@0);
2728
2729         wide_int w1
2730           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2731                             TYPE_SIGN (inner_type));
2732
2733         value_range vr;
2734         if (get_global_range_query ()->range_of_expr (vr, @0)
2735             && vr.kind () == VR_RANGE)
2736           {
2737             wide_int wmin0 = vr.lower_bound ();
2738             wide_int wmax0 = vr.upper_bound ();
2739             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2740             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2741           }
2742       }
2743      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2744       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2745      )))
2746 #endif
2747
2748 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
2749 #if GIMPLE
2750   (for op (plus minus)
2751    (simplify
2752     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2753      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2754           && TREE_CODE (type) == INTEGER_TYPE
2755           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2756           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2757           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2758           && TYPE_OVERFLOW_WRAPS (type))
2759        (plus (convert @0) (op @2 (convert @1))))))
2760 #endif
2761
2762 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2763    to a simple value.  */
2764   (for op (plus minus)
2765    (simplify
2766     (op (convert @0) (convert @1))
2767      (if (INTEGRAL_TYPE_P (type)
2768           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2769           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2770           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2771           && !TYPE_OVERFLOW_TRAPS (type)
2772           && !TYPE_OVERFLOW_SANITIZED (type))
2773       (convert (op! @0 @1)))))
2774
2775   /* ~A + A -> -1 */
2776   (simplify
2777    (plus:c (bit_not @0) @0)
2778    (if (!TYPE_OVERFLOW_TRAPS (type))
2779     { build_all_ones_cst (type); }))
2780
2781   /* ~A + 1 -> -A */
2782   (simplify
2783    (plus (convert? (bit_not @0)) integer_each_onep)
2784    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2785     (negate (convert @0))))
2786
2787   /* -A - 1 -> ~A */
2788   (simplify
2789    (minus (convert? (negate @0)) integer_each_onep)
2790    (if (!TYPE_OVERFLOW_TRAPS (type)
2791         && TREE_CODE (type) != COMPLEX_TYPE
2792         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2793     (bit_not (convert @0))))
2794
2795   /* -1 - A -> ~A */
2796   (simplify
2797    (minus integer_all_onesp @0)
2798    (if (TREE_CODE (type) != COMPLEX_TYPE)
2799     (bit_not @0)))
2800
2801   /* (T)(P + A) - (T)P -> (T) A */
2802   (simplify
2803    (minus (convert (plus:c @@0 @1))
2804     (convert? @0))
2805    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2806         /* For integer types, if A has a smaller type
2807            than T the result depends on the possible
2808            overflow in P + A.
2809            E.g. T=size_t, A=(unsigned)429497295, P>0.
2810            However, if an overflow in P + A would cause
2811            undefined behavior, we can assume that there
2812            is no overflow.  */
2813         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2814             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2815     (convert @1)))
2816   (simplify
2817    (minus (convert (pointer_plus @@0 @1))
2818     (convert @0))
2819    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2820         /* For pointer types, if the conversion of A to the
2821            final type requires a sign- or zero-extension,
2822            then we have to punt - it is not defined which
2823            one is correct.  */
2824         || (POINTER_TYPE_P (TREE_TYPE (@0))
2825             && TREE_CODE (@1) == INTEGER_CST
2826             && tree_int_cst_sign_bit (@1) == 0))
2827     (convert @1)))
2828    (simplify
2829     (pointer_diff (pointer_plus @@0 @1) @0)
2830     /* The second argument of pointer_plus must be interpreted as signed, and
2831        thus sign-extended if necessary.  */
2832     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2833      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2834         second arg is unsigned even when we need to consider it as signed,
2835         we don't want to diagnose overflow here.  */
2836      (convert (view_convert:stype @1))))
2837
2838   /* (T)P - (T)(P + A) -> -(T) A */
2839   (simplify
2840    (minus (convert? @0)
2841     (convert (plus:c @@0 @1)))
2842    (if (INTEGRAL_TYPE_P (type)
2843         && TYPE_OVERFLOW_UNDEFINED (type)
2844         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2845     (with { tree utype = unsigned_type_for (type); }
2846      (convert (negate (convert:utype @1))))
2847     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2848          /* For integer types, if A has a smaller type
2849             than T the result depends on the possible
2850             overflow in P + A.
2851             E.g. T=size_t, A=(unsigned)429497295, P>0.
2852             However, if an overflow in P + A would cause
2853             undefined behavior, we can assume that there
2854             is no overflow.  */
2855          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2856              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2857      (negate (convert @1)))))
2858   (simplify
2859    (minus (convert @0)
2860     (convert (pointer_plus @@0 @1)))
2861    (if (INTEGRAL_TYPE_P (type)
2862         && TYPE_OVERFLOW_UNDEFINED (type)
2863         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2864     (with { tree utype = unsigned_type_for (type); }
2865      (convert (negate (convert:utype @1))))
2866     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2867          /* For pointer types, if the conversion of A to the
2868             final type requires a sign- or zero-extension,
2869             then we have to punt - it is not defined which
2870             one is correct.  */
2871          || (POINTER_TYPE_P (TREE_TYPE (@0))
2872              && TREE_CODE (@1) == INTEGER_CST
2873              && tree_int_cst_sign_bit (@1) == 0))
2874      (negate (convert @1)))))
2875    (simplify
2876     (pointer_diff @0 (pointer_plus @@0 @1))
2877     /* The second argument of pointer_plus must be interpreted as signed, and
2878        thus sign-extended if necessary.  */
2879     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2880      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2881         second arg is unsigned even when we need to consider it as signed,
2882         we don't want to diagnose overflow here.  */
2883      (negate (convert (view_convert:stype @1)))))
2884
2885   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2886   (simplify
2887    (minus (convert (plus:c @@0 @1))
2888     (convert (plus:c @0 @2)))
2889    (if (INTEGRAL_TYPE_P (type)
2890         && TYPE_OVERFLOW_UNDEFINED (type)
2891         && element_precision (type) <= element_precision (TREE_TYPE (@1))
2892         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2893     (with { tree utype = unsigned_type_for (type); }
2894      (convert (minus (convert:utype @1) (convert:utype @2))))
2895     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2896           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2897          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2898              /* For integer types, if A has a smaller type
2899                 than T the result depends on the possible
2900                 overflow in P + A.
2901                 E.g. T=size_t, A=(unsigned)429497295, P>0.
2902                 However, if an overflow in P + A would cause
2903                 undefined behavior, we can assume that there
2904                 is no overflow.  */
2905              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2906                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2907                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2908                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2909      (minus (convert @1) (convert @2)))))
2910   (simplify
2911    (minus (convert (pointer_plus @@0 @1))
2912     (convert (pointer_plus @0 @2)))
2913    (if (INTEGRAL_TYPE_P (type)
2914         && TYPE_OVERFLOW_UNDEFINED (type)
2915         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2916     (with { tree utype = unsigned_type_for (type); }
2917      (convert (minus (convert:utype @1) (convert:utype @2))))
2918     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2919          /* For pointer types, if the conversion of A to the
2920             final type requires a sign- or zero-extension,
2921             then we have to punt - it is not defined which
2922             one is correct.  */
2923          || (POINTER_TYPE_P (TREE_TYPE (@0))
2924              && TREE_CODE (@1) == INTEGER_CST
2925              && tree_int_cst_sign_bit (@1) == 0
2926              && TREE_CODE (@2) == INTEGER_CST
2927              && tree_int_cst_sign_bit (@2) == 0))
2928      (minus (convert @1) (convert @2)))))
2929    (simplify
2930     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2931      (pointer_diff @0 @1))
2932    (simplify
2933     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2934     /* The second argument of pointer_plus must be interpreted as signed, and
2935        thus sign-extended if necessary.  */
2936     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2937      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2938         second arg is unsigned even when we need to consider it as signed,
2939         we don't want to diagnose overflow here.  */
2940      (minus (convert (view_convert:stype @1))
2941             (convert (view_convert:stype @2)))))))
2942
2943 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2944     Modeled after fold_plusminus_mult_expr.  */
2945 (if (!TYPE_SATURATING (type)
2946      && (!FLOAT_TYPE_P (type) || flag_associative_math))
2947  (for plusminus (plus minus)
2948   (simplify
2949    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2950    (if (!ANY_INTEGRAL_TYPE_P (type)
2951         || TYPE_OVERFLOW_WRAPS (type)
2952         || (INTEGRAL_TYPE_P (type)
2953             && tree_expr_nonzero_p (@0)
2954             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2955     (if (single_use (@3) || single_use (@4))
2956      /* If @1 +- @2 is constant require a hard single-use on either
2957         original operand (but not on both).  */
2958      (mult (plusminus @1 @2) @0)
2959      (mult! (plusminus @1 @2) @0)
2960   )))
2961   /* We cannot generate constant 1 for fract.  */
2962   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2963    (simplify
2964     (plusminus @0 (mult:c@3 @0 @2))
2965     (if ((!ANY_INTEGRAL_TYPE_P (type)
2966           || TYPE_OVERFLOW_WRAPS (type)
2967           /* For @0 + @0*@2 this transformation would introduce UB
2968              (where there was none before) for @0 in [-1,0] and @2 max.
2969              For @0 - @0*@2 this transformation would introduce UB
2970              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
2971           || (INTEGRAL_TYPE_P (type)
2972               && ((tree_expr_nonzero_p (@0)
2973                    && expr_not_equal_to (@0,
2974                                 wi::minus_one (TYPE_PRECISION (type))))
2975                   || (plusminus == PLUS_EXPR
2976                       ? expr_not_equal_to (@2,
2977                             wi::max_value (TYPE_PRECISION (type), SIGNED))
2978                       /* Let's ignore the @0 -1 and @2 min case.  */
2979                       : (expr_not_equal_to (@2,
2980                             wi::min_value (TYPE_PRECISION (type), SIGNED))
2981                          && expr_not_equal_to (@2,
2982                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
2983                                 + 1))))))
2984          && single_use (@3))
2985      (mult (plusminus { build_one_cst (type); } @2) @0)))
2986    (simplify
2987     (plusminus (mult:c@3 @0 @2) @0)
2988     (if ((!ANY_INTEGRAL_TYPE_P (type)
2989           || TYPE_OVERFLOW_WRAPS (type)
2990           /* For @0*@2 + @0 this transformation would introduce UB
2991              (where there was none before) for @0 in [-1,0] and @2 max.
2992              For @0*@2 - @0 this transformation would introduce UB
2993              for @0 0 and @2 min.  */
2994           || (INTEGRAL_TYPE_P (type)
2995               && ((tree_expr_nonzero_p (@0)
2996                    && (plusminus == MINUS_EXPR
2997                        || expr_not_equal_to (@0,
2998                                 wi::minus_one (TYPE_PRECISION (type)))))
2999                   || expr_not_equal_to (@2,
3000                         (plusminus == PLUS_EXPR
3001                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3002                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3003          && single_use (@3))
3004      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3005
3006 #if GIMPLE
3007 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3008    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3009 (simplify
3010  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3011   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3012        && tree_fits_uhwi_p (@1)
3013        && tree_to_uhwi (@1) < element_precision (type)
3014        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3015            || optab_handler (smul_optab,
3016                              TYPE_MODE (type)) != CODE_FOR_nothing))
3017    (with { tree t = type;
3018            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3019            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3020                                              element_precision (type));
3021            w += 1;
3022            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3023                                         : t, w);
3024            cst = build_uniform_cst (t, cst); }
3025     (convert (mult (convert:t @0) { cst; })))))
3026 (simplify
3027  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3028   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3029        && tree_fits_uhwi_p (@1)
3030        && tree_to_uhwi (@1) < element_precision (type)
3031        && tree_fits_uhwi_p (@2)
3032        && tree_to_uhwi (@2) < element_precision (type)
3033        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3034            || optab_handler (smul_optab,
3035                              TYPE_MODE (type)) != CODE_FOR_nothing))
3036    (with { tree t = type;
3037            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3038            unsigned int prec = element_precision (type);
3039            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3040            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3041            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3042                                         : t, w);
3043            cst = build_uniform_cst (t, cst); }
3044     (convert (mult (convert:t @0) { cst; })))))
3045 #endif
3046
3047 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3048    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3049    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3050 (for op (bit_ior bit_xor)
3051  (simplify
3052   (op (mult:s@0 @1 INTEGER_CST@2)
3053       (mult:s@3 @1 INTEGER_CST@4))
3054   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3055        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3056    (mult @1
3057          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3058  (simplify
3059   (op:c (mult:s@0 @1 INTEGER_CST@2)
3060         (lshift:s@3 @1 INTEGER_CST@4))
3061   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3062        && tree_int_cst_sgn (@4) > 0
3063        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3064    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3065            wide_int c = wi::add (wi::to_wide (@2),
3066                                  wi::lshift (wone, wi::to_wide (@4))); }
3067     (mult @1 { wide_int_to_tree (type, c); }))))
3068  (simplify
3069   (op:c (mult:s@0 @1 INTEGER_CST@2)
3070         @1)
3071   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3072        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3073    (mult @1
3074          { wide_int_to_tree (type,
3075                              wi::add (wi::to_wide (@2), 1)); })))
3076  (simplify
3077   (op (lshift:s@0 @1 INTEGER_CST@2)
3078       (lshift:s@3 @1 INTEGER_CST@4))
3079   (if (INTEGRAL_TYPE_P (type)
3080        && tree_int_cst_sgn (@2) > 0
3081        && tree_int_cst_sgn (@4) > 0
3082        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3083    (with { tree t = type;
3084            if (!TYPE_OVERFLOW_WRAPS (t))
3085              t = unsigned_type_for (t);
3086            wide_int wone = wi::one (TYPE_PRECISION (t));
3087            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3088                                  wi::lshift (wone, wi::to_wide (@4))); }
3089     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3090  (simplify
3091   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3092         @1)
3093   (if (INTEGRAL_TYPE_P (type)
3094        && tree_int_cst_sgn (@2) > 0
3095        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3096    (with { tree t = type;
3097            if (!TYPE_OVERFLOW_WRAPS (t))
3098              t = unsigned_type_for (t);
3099            wide_int wone = wi::one (TYPE_PRECISION (t));
3100            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3101     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3102
3103 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3104
3105 (for minmax (min max)
3106  (simplify
3107   (minmax @0 @0)
3108   @0))
3109 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3110 (for minmax (FMIN_ALL FMAX_ALL)
3111  (simplify
3112   (minmax @0 @0)
3113   (if (!tree_expr_maybe_signaling_nan_p (@0))
3114     @0)))
3115 /* min(max(x,y),y) -> y.  */
3116 (simplify
3117  (min:c (max:c @0 @1) @1)
3118  @1)
3119 /* max(min(x,y),y) -> y.  */
3120 (simplify
3121  (max:c (min:c @0 @1) @1)
3122  @1)
3123 /* max(a,-a) -> abs(a).  */
3124 (simplify
3125  (max:c @0 (negate @0))
3126  (if (TREE_CODE (type) != COMPLEX_TYPE
3127       && (! ANY_INTEGRAL_TYPE_P (type)
3128           || TYPE_OVERFLOW_UNDEFINED (type)))
3129   (abs @0)))
3130 /* min(a,-a) -> -abs(a).  */
3131 (simplify
3132  (min:c @0 (negate @0))
3133  (if (TREE_CODE (type) != COMPLEX_TYPE
3134       && (! ANY_INTEGRAL_TYPE_P (type)
3135           || TYPE_OVERFLOW_UNDEFINED (type)))
3136   (negate (abs @0))))
3137 (simplify
3138  (min @0 @1)
3139  (switch
3140   (if (INTEGRAL_TYPE_P (type)
3141        && TYPE_MIN_VALUE (type)
3142        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3143    @1)
3144   (if (INTEGRAL_TYPE_P (type)
3145        && TYPE_MAX_VALUE (type)
3146        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3147    @0)))
3148 (simplify
3149  (max @0 @1)
3150  (switch
3151   (if (INTEGRAL_TYPE_P (type)
3152        && TYPE_MAX_VALUE (type)
3153        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3154    @1)
3155   (if (INTEGRAL_TYPE_P (type)
3156        && TYPE_MIN_VALUE (type)
3157        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3158    @0)))
3159
3160 /* max (a, a + CST) -> a + CST where CST is positive.  */
3161 /* max (a, a + CST) -> a where CST is negative.  */
3162 (simplify
3163  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3164   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3165    (if (tree_int_cst_sgn (@1) > 0)
3166     @2
3167     @0)))
3168
3169 /* min (a, a + CST) -> a where CST is positive.  */
3170 /* min (a, a + CST) -> a + CST where CST is negative. */
3171 (simplify
3172  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3173   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3174    (if (tree_int_cst_sgn (@1) > 0)
3175     @0
3176     @2)))
3177
3178 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3179    the addresses are known to be less, equal or greater.  */
3180 (for minmax (min max)
3181      cmp (lt gt)
3182  (simplify
3183   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3184   (with
3185    {
3186      poly_int64 off0, off1;
3187      tree base0, base1;
3188      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3189                                   off0, off1, GENERIC);
3190    }
3191    (if (equal == 1)
3192     (if (minmax == MIN_EXPR)
3193      (if (known_le (off0, off1))
3194       @2
3195       (if (known_gt (off0, off1))
3196        @3))
3197      (if (known_ge (off0, off1))
3198       @2
3199       (if (known_lt (off0, off1))
3200        @3)))))))
3201
3202 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3203    and the outer convert demotes the expression back to x's type.  */
3204 (for minmax (min max)
3205  (simplify
3206   (convert (minmax@0 (convert @1) INTEGER_CST@2))
3207   (if (INTEGRAL_TYPE_P (type)
3208        && types_match (@1, type) && int_fits_type_p (@2, type)
3209        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3210        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3211    (minmax @1 (convert @2)))))
3212
3213 (for minmax (FMIN_ALL FMAX_ALL)
3214  /* If either argument is NaN and other one is not sNaN, return the other
3215     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
3216  (simplify
3217   (minmax:c @0 REAL_CST@1)
3218    (if (real_isnan (TREE_REAL_CST_PTR (@1))
3219        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3220        && !tree_expr_maybe_signaling_nan_p (@0))
3221    @0)))
3222 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
3223    functions to return the numeric arg if the other one is NaN.
3224    MIN and MAX don't honor that, so only transform if -ffinite-math-only
3225    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
3226    worry about it either.  */
3227 (if (flag_finite_math_only)
3228  (simplify
3229   (FMIN_ALL @0 @1)
3230   (min @0 @1))
3231  (simplify
3232   (FMAX_ALL @0 @1)
3233   (max @0 @1)))
3234 /* min (-A, -B) -> -max (A, B)  */
3235 (for minmax (min max FMIN_ALL FMAX_ALL)
3236      maxmin (max min FMAX_ALL FMIN_ALL)
3237  (simplify
3238   (minmax (negate:s@2 @0) (negate:s@3 @1))
3239   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3240        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3241            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3242    (negate (maxmin @0 @1)))))
3243 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3244    MAX (~X, ~Y) -> ~MIN (X, Y)  */
3245 (for minmax (min max)
3246  maxmin (max min)
3247  (simplify
3248   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3249   (bit_not (maxmin @0 @1))))
3250
3251 /* MIN (X, Y) == X -> X <= Y  */
3252 (for minmax (min min max max)
3253      cmp    (eq  ne  eq  ne )
3254      out    (le  gt  ge  lt )
3255  (simplify
3256   (cmp:c (minmax:c @0 @1) @0)
3257   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3258    (out @0 @1))))
3259 /* MIN (X, 5) == 0 -> X == 0
3260    MIN (X, 5) == 7 -> false  */
3261 (for cmp (eq ne)
3262  (simplify
3263   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3264   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3265                  TYPE_SIGN (TREE_TYPE (@0))))
3266    { constant_boolean_node (cmp == NE_EXPR, type); }
3267    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3268                   TYPE_SIGN (TREE_TYPE (@0))))
3269     (cmp @0 @2)))))
3270 (for cmp (eq ne)
3271  (simplify
3272   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3273   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3274                  TYPE_SIGN (TREE_TYPE (@0))))
3275    { constant_boolean_node (cmp == NE_EXPR, type); }
3276    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3277                   TYPE_SIGN (TREE_TYPE (@0))))
3278     (cmp @0 @2)))))
3279 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
3280 (for minmax (min     min     max     max     min     min     max     max    )
3281      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
3282      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3283  (simplify
3284   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3285   (comb (cmp @0 @2) (cmp @1 @2))))
3286
3287 /* X <= MAX(X, Y) -> true
3288    X > MAX(X, Y) -> false 
3289    X >= MIN(X, Y) -> true
3290    X < MIN(X, Y) -> false */
3291 (for minmax (min     min     max     max     )
3292      cmp    (ge      lt      le      gt      )
3293  (simplify
3294   (cmp @0 (minmax:c @0 @1))
3295   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3296
3297 /* Undo fancy ways of writing max/min or other ?: expressions, like
3298    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
3299    People normally use ?: and that is what we actually try to optimize.  */
3300 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
3301 (simplify
3302  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3303  (if (INTEGRAL_TYPE_P (type)
3304       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3305   (cond (convert:boolean_type_node @2) @1 @0)))
3306 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
3307 (simplify
3308  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3309  (if (INTEGRAL_TYPE_P (type)
3310       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3311   (cond (convert:boolean_type_node @2) @1 @0)))
3312 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
3313 (simplify
3314  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3315  (if (INTEGRAL_TYPE_P (type)
3316       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3317   (cond (convert:boolean_type_node @2) @1 @0)))
3318
3319 /* Simplifications of shift and rotates.  */
3320
3321 (for rotate (lrotate rrotate)
3322  (simplify
3323   (rotate integer_all_onesp@0 @1)
3324   @0))
3325
3326 /* Optimize -1 >> x for arithmetic right shifts.  */
3327 (simplify
3328  (rshift integer_all_onesp@0 @1)
3329  (if (!TYPE_UNSIGNED (type))
3330   @0))
3331
3332 /* Optimize (x >> c) << c into x & (-1<<c).  */
3333 (simplify
3334  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3335  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3336   /* It doesn't matter if the right shift is arithmetic or logical.  */
3337   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3338
3339 (simplify
3340  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3341  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3342       /* Allow intermediate conversion to integral type with whatever sign, as
3343          long as the low TYPE_PRECISION (type)
3344          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
3345       && INTEGRAL_TYPE_P (type)
3346       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3347       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3348       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3349       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3350           || wi::geu_p (wi::to_wide (@1),
3351                         TYPE_PRECISION (type)
3352                         - TYPE_PRECISION (TREE_TYPE (@2)))))
3353   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3354
3355 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3356    types.  */
3357 (simplify
3358  (rshift (lshift @0 INTEGER_CST@1) @1)
3359  (if (TYPE_UNSIGNED (type)
3360       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3361   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3362
3363 /* Optimize x >> x into 0 */
3364 (simplify
3365  (rshift @0 @0)
3366   { build_zero_cst (type); })
3367
3368 (for shiftrotate (lrotate rrotate lshift rshift)
3369  (simplify
3370   (shiftrotate @0 integer_zerop)
3371   (non_lvalue @0))
3372  (simplify
3373   (shiftrotate integer_zerop@0 @1)
3374   @0)
3375  /* Prefer vector1 << scalar to vector1 << vector2
3376     if vector2 is uniform.  */
3377  (for vec (VECTOR_CST CONSTRUCTOR)
3378   (simplify
3379    (shiftrotate @0 vec@1)
3380    (with { tree tem = uniform_vector_p (@1); }
3381     (if (tem)
3382      (shiftrotate @0 { tem; }))))))
3383
3384 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3385    Y is 0.  Similarly for X >> Y.  */
3386 #if GIMPLE
3387 (for shift (lshift rshift)
3388  (simplify
3389   (shift @0 SSA_NAME@1)
3390    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3391     (with {
3392       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3393       int prec = TYPE_PRECISION (TREE_TYPE (@1));
3394      }
3395      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3396       @0)))))
3397 #endif
3398
3399 /* Rewrite an LROTATE_EXPR by a constant into an
3400    RROTATE_EXPR by a new constant.  */
3401 (simplify
3402  (lrotate @0 INTEGER_CST@1)
3403  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3404                             build_int_cst (TREE_TYPE (@1),
3405                                            element_precision (type)), @1); }))
3406
3407 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
3408 (for op (lrotate rrotate rshift lshift)
3409  (simplify
3410   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3411   (with { unsigned int prec = element_precision (type); }
3412    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3413         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3414         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3415         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3416     (with { unsigned int low = (tree_to_uhwi (@1)
3417                                 + tree_to_uhwi (@2)); }
3418      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3419         being well defined.  */
3420      (if (low >= prec)
3421       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3422        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3423        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3424         { build_zero_cst (type); }
3425         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3426       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3427
3428
3429 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
3430 (simplify
3431  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3432   (if ((wi::to_wide (@1) & 1) != 0)
3433    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3434    { build_zero_cst (type); }))
3435
3436 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3437    either to false if D is smaller (unsigned comparison) than C, or to
3438    x == log2 (D) - log2 (C).  Similarly for right shifts.  */
3439 (for cmp (ne eq)
3440      icmp (eq ne)
3441  (simplify
3442   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3443    (with { int c1 = wi::clz (wi::to_wide (@1));
3444            int c2 = wi::clz (wi::to_wide (@2)); }
3445     (if (c1 < c2)
3446      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3447      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3448  (simplify
3449   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3450    (if (tree_int_cst_sgn (@1) > 0)
3451     (with { int c1 = wi::clz (wi::to_wide (@1));
3452             int c2 = wi::clz (wi::to_wide (@2)); }
3453      (if (c1 > c2)
3454       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3455       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3456
3457 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3458    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3459    if CST2 != 0.  */
3460 (for cmp (ne eq)
3461  (simplify
3462   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3463   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3464    (if (cand < 0
3465         || (!integer_zerop (@2)
3466             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3467     { constant_boolean_node (cmp == NE_EXPR, type); }
3468     (if (!integer_zerop (@2)
3469          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3470      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3471
3472 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3473         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3474    if the new mask might be further optimized.  */
3475 (for shift (lshift rshift)
3476  (simplify
3477   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3478            INTEGER_CST@2)
3479    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3480         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3481         && tree_fits_uhwi_p (@1)
3482         && tree_to_uhwi (@1) > 0
3483         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3484     (with
3485      {
3486        unsigned int shiftc = tree_to_uhwi (@1);
3487        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3488        unsigned HOST_WIDE_INT newmask, zerobits = 0;
3489        tree shift_type = TREE_TYPE (@3);
3490        unsigned int prec;
3491
3492        if (shift == LSHIFT_EXPR)
3493          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3494        else if (shift == RSHIFT_EXPR
3495                 && type_has_mode_precision_p (shift_type))
3496          {
3497            prec = TYPE_PRECISION (TREE_TYPE (@3));
3498            tree arg00 = @0;
3499            /* See if more bits can be proven as zero because of
3500               zero extension.  */
3501            if (@3 != @0
3502                && TYPE_UNSIGNED (TREE_TYPE (@0)))
3503              {
3504                tree inner_type = TREE_TYPE (@0);
3505                if (type_has_mode_precision_p (inner_type)
3506                    && TYPE_PRECISION (inner_type) < prec)
3507                  {
3508                    prec = TYPE_PRECISION (inner_type);
3509                    /* See if we can shorten the right shift.  */
3510                    if (shiftc < prec)
3511                      shift_type = inner_type;
3512                    /* Otherwise X >> C1 is all zeros, so we'll optimize
3513                       it into (X, 0) later on by making sure zerobits
3514                       is all ones.  */
3515                  }
3516              }
3517            zerobits = HOST_WIDE_INT_M1U;
3518            if (shiftc < prec)
3519              {
3520                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3521                zerobits <<= prec - shiftc;
3522              }
3523            /* For arithmetic shift if sign bit could be set, zerobits
3524               can contain actually sign bits, so no transformation is
3525               possible, unless MASK masks them all away.  In that
3526               case the shift needs to be converted into logical shift.  */
3527            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3528                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3529              {
3530                if ((mask & zerobits) == 0)
3531                  shift_type = unsigned_type_for (TREE_TYPE (@3));
3532                else
3533                  zerobits = 0;
3534              }
3535          }
3536      }
3537      /* ((X << 16) & 0xff00) is (X, 0).  */
3538      (if ((mask & zerobits) == mask)
3539       { build_int_cst (type, 0); }
3540       (with { newmask = mask | zerobits; }
3541        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3542         (with
3543          {
3544            /* Only do the transformation if NEWMASK is some integer
3545               mode's mask.  */
3546            for (prec = BITS_PER_UNIT;
3547                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3548              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3549                break;
3550          }
3551          (if (prec < HOST_BITS_PER_WIDE_INT
3552               || newmask == HOST_WIDE_INT_M1U)
3553           (with
3554            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3555            (if (!tree_int_cst_equal (newmaskt, @2))
3556             (if (shift_type != TREE_TYPE (@3))
3557              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3558              (bit_and @4 { newmaskt; })))))))))))))
3559
3560 /* ((1 << n) & M) != 0  -> n == log2 (M) */
3561 (for cmp (ne eq)
3562        icmp (eq ne)
3563  (simplify
3564   (cmp
3565    (bit_and
3566     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
3567   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3568    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
3569                                 wi::exact_log2 (wi::to_wide (@1))); }))))
3570
3571 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3572    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
3573 (for shift (lshift rshift)
3574  (for bit_op (bit_and bit_xor bit_ior)
3575   (simplify
3576    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3577    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3578     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3579      (if (mask)
3580       (bit_op (shift (convert @0) @1) { mask; })))))))
3581
3582 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
3583 (simplify
3584  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3585   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3586        && (element_precision (TREE_TYPE (@0))
3587            <= element_precision (TREE_TYPE (@1))
3588            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3589    (with
3590     { tree shift_type = TREE_TYPE (@0); }
3591      (convert (rshift (convert:shift_type @1) @2)))))
3592
3593 /* ~(~X >>r Y) -> X >>r Y
3594    ~(~X <<r Y) -> X <<r Y */
3595 (for rotate (lrotate rrotate)
3596  (simplify
3597   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3598    (if ((element_precision (TREE_TYPE (@0))
3599          <= element_precision (TREE_TYPE (@1))
3600          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3601         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3602             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3603     (with
3604      { tree rotate_type = TREE_TYPE (@0); }
3605       (convert (rotate (convert:rotate_type @1) @2))))))
3606
3607 (for cmp (eq ne)
3608  (for rotate (lrotate rrotate)
3609       invrot (rrotate lrotate)
3610   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3611   (simplify
3612    (cmp (rotate @1 @0) (rotate @2 @0))
3613    (cmp @1 @2))
3614   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3615   (simplify
3616    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3617    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3618   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
3619   (simplify
3620    (cmp (rotate @0 @1) INTEGER_CST@2)
3621     (if (integer_zerop (@2) || integer_all_onesp (@2))
3622      (cmp @0 @2)))))
3623
3624 /* Both signed and unsigned lshift produce the same result, so use
3625    the form that minimizes the number of conversions.  Postpone this
3626    transformation until after shifts by zero have been folded.  */
3627 (simplify
3628  (convert (lshift:s@0 (convert:s@1 @2) INTEGER_CST@3))
3629  (if (INTEGRAL_TYPE_P (type)
3630       && tree_nop_conversion_p (type, TREE_TYPE (@0))
3631       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3632       && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type)
3633       && !integer_zerop (@3))
3634   (lshift (convert @2) @3)))
3635
3636 /* Simplifications of conversions.  */
3637
3638 /* Basic strip-useless-type-conversions / strip_nops.  */
3639 (for cvt (convert view_convert float fix_trunc)
3640  (simplify
3641   (cvt @0)
3642   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3643        || (GENERIC && type == TREE_TYPE (@0)))
3644    @0)))
3645
3646 /* Contract view-conversions.  */
3647 (simplify
3648   (view_convert (view_convert @0))
3649   (view_convert @0))
3650
3651 /* For integral conversions with the same precision or pointer
3652    conversions use a NOP_EXPR instead.  */
3653 (simplify
3654   (view_convert @0)
3655   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3656        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3657        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3658    (convert @0)))
3659
3660 /* Strip inner integral conversions that do not change precision or size, or
3661    zero-extend while keeping the same size (for bool-to-char).  */
3662 (simplify
3663   (view_convert (convert@0 @1))
3664   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3665        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3666        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3667        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3668            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3669                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3670    (view_convert @1)))
3671
3672 /* Simplify a view-converted empty constructor.  */
3673 (simplify
3674   (view_convert CONSTRUCTOR@0)
3675   (if (TREE_CODE (@0) != SSA_NAME
3676        && CONSTRUCTOR_NELTS (@0) == 0)
3677    { build_zero_cst (type); }))
3678
3679 /* Re-association barriers around constants and other re-association
3680    barriers can be removed.  */
3681 (simplify
3682  (paren CONSTANT_CLASS_P@0)
3683  @0)
3684 (simplify
3685  (paren (paren@1 @0))
3686  @1)
3687
3688 /* Handle cases of two conversions in a row.  */
3689 (for ocvt (convert float fix_trunc)
3690  (for icvt (convert float)
3691   (simplify
3692    (ocvt (icvt@1 @0))
3693    (with
3694     {
3695       tree inside_type = TREE_TYPE (@0);
3696       tree inter_type = TREE_TYPE (@1);
3697       int inside_int = INTEGRAL_TYPE_P (inside_type);
3698       int inside_ptr = POINTER_TYPE_P (inside_type);
3699       int inside_float = FLOAT_TYPE_P (inside_type);
3700       int inside_vec = VECTOR_TYPE_P (inside_type);
3701       unsigned int inside_prec = TYPE_PRECISION (inside_type);
3702       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3703       int inter_int = INTEGRAL_TYPE_P (inter_type);
3704       int inter_ptr = POINTER_TYPE_P (inter_type);
3705       int inter_float = FLOAT_TYPE_P (inter_type);
3706       int inter_vec = VECTOR_TYPE_P (inter_type);
3707       unsigned int inter_prec = TYPE_PRECISION (inter_type);
3708       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3709       int final_int = INTEGRAL_TYPE_P (type);
3710       int final_ptr = POINTER_TYPE_P (type);
3711       int final_float = FLOAT_TYPE_P (type);
3712       int final_vec = VECTOR_TYPE_P (type);
3713       unsigned int final_prec = TYPE_PRECISION (type);
3714       int final_unsignedp = TYPE_UNSIGNED (type);
3715     }
3716    (switch
3717     /* In addition to the cases of two conversions in a row
3718        handled below, if we are converting something to its own
3719        type via an object of identical or wider precision, neither
3720        conversion is needed.  */
3721     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3722           || (GENERIC
3723               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3724          && (((inter_int || inter_ptr) && final_int)
3725              || (inter_float && final_float))
3726          && inter_prec >= final_prec)
3727      (ocvt @0))
3728
3729     /* Likewise, if the intermediate and initial types are either both
3730        float or both integer, we don't need the middle conversion if the
3731        former is wider than the latter and doesn't change the signedness
3732        (for integers).  Avoid this if the final type is a pointer since
3733        then we sometimes need the middle conversion.  */
3734     (if (((inter_int && inside_int) || (inter_float && inside_float))
3735          && (final_int || final_float)
3736          && inter_prec >= inside_prec
3737          && (inter_float || inter_unsignedp == inside_unsignedp))
3738      (ocvt @0))
3739
3740     /* If we have a sign-extension of a zero-extended value, we can
3741        replace that by a single zero-extension.  Likewise if the
3742        final conversion does not change precision we can drop the
3743        intermediate conversion.  */
3744     (if (inside_int && inter_int && final_int
3745          && ((inside_prec < inter_prec && inter_prec < final_prec
3746               && inside_unsignedp && !inter_unsignedp)
3747              || final_prec == inter_prec))
3748      (ocvt @0))
3749
3750     /* Two conversions in a row are not needed unless:
3751         - some conversion is floating-point (overstrict for now), or
3752         - some conversion is a vector (overstrict for now), or
3753         - the intermediate type is narrower than both initial and
3754           final, or
3755         - the intermediate type and innermost type differ in signedness,
3756           and the outermost type is wider than the intermediate, or
3757         - the initial type is a pointer type and the precisions of the
3758           intermediate and final types differ, or
3759         - the final type is a pointer type and the precisions of the
3760           initial and intermediate types differ.  */
3761     (if (! inside_float && ! inter_float && ! final_float
3762          && ! inside_vec && ! inter_vec && ! final_vec
3763          && (inter_prec >= inside_prec || inter_prec >= final_prec)
3764          && ! (inside_int && inter_int
3765                && inter_unsignedp != inside_unsignedp
3766                && inter_prec < final_prec)
3767          && ((inter_unsignedp && inter_prec > inside_prec)
3768              == (final_unsignedp && final_prec > inter_prec))
3769          && ! (inside_ptr && inter_prec != final_prec)
3770          && ! (final_ptr && inside_prec != inter_prec))
3771      (ocvt @0))
3772
3773     /* A truncation to an unsigned type (a zero-extension) should be
3774        canonicalized as bitwise and of a mask.  */
3775     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
3776          && final_int && inter_int && inside_int
3777          && final_prec == inside_prec
3778          && final_prec > inter_prec
3779          && inter_unsignedp)
3780      (convert (bit_and @0 { wide_int_to_tree
3781                               (inside_type,
3782                                wi::mask (inter_prec, false,
3783                                          TYPE_PRECISION (inside_type))); })))
3784
3785     /* If we are converting an integer to a floating-point that can
3786        represent it exactly and back to an integer, we can skip the
3787        floating-point conversion.  */
3788     (if (GIMPLE /* PR66211 */
3789          && inside_int && inter_float && final_int &&
3790          (unsigned) significand_size (TYPE_MODE (inter_type))
3791          >= inside_prec - !inside_unsignedp)
3792      (convert @0)))))))
3793
3794 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
3795    float_type.  Only do the transformation if we do not need to preserve
3796    trapping behaviour, so require !flag_trapping_math. */
3797 #if GIMPLE
3798 (simplify
3799    (float (fix_trunc @0))
3800    (if (!flag_trapping_math
3801         && types_match (type, TREE_TYPE (@0))
3802         && direct_internal_fn_supported_p (IFN_TRUNC, type,
3803                                           OPTIMIZE_FOR_BOTH))
3804       (IFN_TRUNC @0)))
3805 #endif
3806
3807 /* If we have a narrowing conversion to an integral type that is fed by a
3808    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3809    masks off bits outside the final type (and nothing else).  */
3810 (simplify
3811   (convert (bit_and @0 INTEGER_CST@1))
3812   (if (INTEGRAL_TYPE_P (type)
3813        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3814        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3815        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3816                                                     TYPE_PRECISION (type)), 0))
3817    (convert @0)))
3818
3819
3820 /* (X /[ex] A) * A -> X.  */
3821 (simplify
3822   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3823   (convert @0))
3824
3825 /* Simplify (A / B) * B + (A % B) -> A.  */
3826 (for div (trunc_div ceil_div floor_div round_div)
3827      mod (trunc_mod ceil_mod floor_mod round_mod)
3828   (simplify
3829    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3830    @0))
3831
3832 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
3833 (for op (plus minus)
3834  (simplify
3835   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3836   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3837        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3838    (with
3839      {
3840        wi::overflow_type overflow;
3841        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3842                                TYPE_SIGN (type), &overflow);
3843      }
3844      (if (types_match (type, TREE_TYPE (@2))
3845          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3846       (op @0 { wide_int_to_tree (type, mul); })
3847       (with { tree utype = unsigned_type_for (type); }
3848        (convert (op (convert:utype @0)
3849                     (mult (convert:utype @1) (convert:utype @2))))))))))
3850
3851 /* Canonicalization of binary operations.  */
3852
3853 /* Convert X + -C into X - C.  */
3854 (simplify
3855  (plus @0 REAL_CST@1)
3856  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3857   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3858    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3859     (minus @0 { tem; })))))
3860
3861 /* Convert x+x into x*2.  */
3862 (simplify
3863  (plus @0 @0)
3864  (if (SCALAR_FLOAT_TYPE_P (type))
3865   (mult @0 { build_real (type, dconst2); })
3866   (if (INTEGRAL_TYPE_P (type))
3867    (mult @0 { build_int_cst (type, 2); }))))
3868
3869 /* 0 - X  ->  -X.  */
3870 (simplify
3871  (minus integer_zerop @1)
3872  (negate @1))
3873 (simplify
3874  (pointer_diff integer_zerop @1)
3875  (negate (convert @1)))
3876
3877 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
3878    ARG0 is zero and X + ARG0 reduces to X, since that would mean
3879    (-ARG1 + ARG0) reduces to -ARG1.  */
3880 (simplify
3881  (minus real_zerop@0 @1)
3882  (if (fold_real_zero_addition_p (type, @1, @0, 0))
3883   (negate @1)))
3884
3885 /* Transform x * -1 into -x.  */
3886 (simplify
3887  (mult @0 integer_minus_onep)
3888  (negate @0))
3889
3890 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
3891    signed overflow for CST != 0 && CST != -1.  */
3892 (simplify
3893  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3894  (if (TREE_CODE (@2) != INTEGER_CST
3895       && single_use (@3)
3896       && !integer_zerop (@1) && !integer_minus_onep (@1))
3897   (mult (mult @0 @2) @1)))
3898
3899 /* True if we can easily extract the real and imaginary parts of a complex
3900    number.  */
3901 (match compositional_complex
3902  (convert? (complex @0 @1)))
3903
3904 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
3905 (simplify
3906  (complex (realpart @0) (imagpart @0))
3907  @0)
3908 (simplify
3909  (realpart (complex @0 @1))
3910  @0)
3911 (simplify
3912  (imagpart (complex @0 @1))
3913  @1)
3914
3915 /* Sometimes we only care about half of a complex expression.  */
3916 (simplify
3917  (realpart (convert?:s (conj:s @0)))
3918  (convert (realpart @0)))
3919 (simplify
3920  (imagpart (convert?:s (conj:s @0)))
3921  (convert (negate (imagpart @0))))
3922 (for part (realpart imagpart)
3923  (for op (plus minus)
3924   (simplify
3925    (part (convert?:s@2 (op:s @0 @1)))
3926    (convert (op (part @0) (part @1))))))
3927 (simplify
3928  (realpart (convert?:s (CEXPI:s @0)))
3929  (convert (COS @0)))
3930 (simplify
3931  (imagpart (convert?:s (CEXPI:s @0)))
3932  (convert (SIN @0)))
3933
3934 /* conj(conj(x)) -> x  */
3935 (simplify
3936  (conj (convert? (conj @0)))
3937  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3938   (convert @0)))
3939
3940 /* conj({x,y}) -> {x,-y}  */
3941 (simplify
3942  (conj (convert?:s (complex:s @0 @1)))
3943  (with { tree itype = TREE_TYPE (type); }
3944   (complex (convert:itype @0) (negate (convert:itype @1)))))
3945
3946 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
3947 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
3948             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
3949  (simplify
3950   (bswap (bswap @0))
3951   @0)
3952  (simplify
3953   (bswap (bit_not (bswap @0)))
3954   (bit_not @0))
3955  (for bitop (bit_xor bit_ior bit_and)
3956   (simplify
3957    (bswap (bitop:c (bswap @0) @1))
3958    (bitop @0 (bswap @1))))
3959  (for cmp (eq ne)
3960   (simplify
3961    (cmp (bswap@2 @0) (bswap @1))
3962    (with { tree ctype = TREE_TYPE (@2); }
3963     (cmp (convert:ctype @0) (convert:ctype @1))))
3964   (simplify
3965    (cmp (bswap @0) INTEGER_CST@1)
3966    (with { tree ctype = TREE_TYPE (@1); }
3967     (cmp (convert:ctype @0) (bswap! @1)))))
3968  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
3969  (simplify
3970   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
3971            INTEGER_CST@3)
3972    (if (BITS_PER_UNIT == 8
3973         && tree_fits_uhwi_p (@2)
3974         && tree_fits_uhwi_p (@3))
3975     (with
3976      {
3977       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
3978       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
3979       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
3980       unsigned HOST_WIDE_INT lo = bits & 7;
3981       unsigned HOST_WIDE_INT hi = bits - lo;
3982      }
3983      (if (bits < prec
3984           && mask < (256u>>lo)
3985           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
3986       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
3987        (if (ns == 0)
3988         (bit_and (convert @1) @3)
3989         (with
3990          {
3991           tree utype = unsigned_type_for (TREE_TYPE (@1));
3992           tree nst = build_int_cst (integer_type_node, ns);
3993          }
3994          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
3995  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
3996  (simplify
3997   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
3998    (if (BITS_PER_UNIT == 8
3999         && CHAR_TYPE_SIZE == 8
4000         && tree_fits_uhwi_p (@1))
4001     (with
4002      {
4003       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4004       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4005       /* If the bswap was extended before the original shift, this
4006          byte (shift) has the sign of the extension, not the sign of
4007          the original shift.  */
4008       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4009      }
4010      /* Special case: logical right shift of sign-extended bswap.
4011         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4012      (if (TYPE_PRECISION (type) > prec
4013           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4014           && TYPE_UNSIGNED (type)
4015           && bits < prec && bits + 8 >= prec)
4016       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4017        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4018       (if (bits + 8 == prec)
4019        (if (TYPE_UNSIGNED (st))
4020         (convert (convert:unsigned_char_type_node @0))
4021         (convert (convert:signed_char_type_node @0)))
4022        (if (bits < prec && bits + 8 > prec)
4023         (with 
4024          {
4025           tree nst = build_int_cst (integer_type_node, bits & 7);
4026           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4027                                        : signed_char_type_node;
4028          }
4029          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4030  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4031  (simplify
4032   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4033    (if (BITS_PER_UNIT == 8
4034         && tree_fits_uhwi_p (@1)
4035         && tree_to_uhwi (@1) < 256)
4036     (with
4037      {
4038       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4039       tree utype = unsigned_type_for (TREE_TYPE (@0));
4040       tree nst = build_int_cst (integer_type_node, prec - 8);
4041      }
4042      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4043
4044
4045 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
4046
4047 /* Simplify constant conditions.
4048    Only optimize constant conditions when the selected branch
4049    has the same type as the COND_EXPR.  This avoids optimizing
4050    away "c ? x : throw", where the throw has a void type.
4051    Note that we cannot throw away the fold-const.cc variant nor
4052    this one as we depend on doing this transform before possibly
4053    A ? B : B -> B triggers and the fold-const.cc one can optimize
4054    0 ? A : B to B even if A has side-effects.  Something
4055    genmatch cannot handle.  */
4056 (simplify
4057  (cond INTEGER_CST@0 @1 @2)
4058  (if (integer_zerop (@0))
4059   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4060    @2)
4061   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4062    @1)))
4063 (simplify
4064  (vec_cond VECTOR_CST@0 @1 @2)
4065  (if (integer_all_onesp (@0))
4066   @1
4067   (if (integer_zerop (@0))
4068    @2)))
4069
4070 /* Sink unary operations to branches, but only if we do fold both.  */
4071 (for op (negate bit_not abs absu)
4072  (simplify
4073   (op (vec_cond:s @0 @1 @2))
4074   (vec_cond @0 (op! @1) (op! @2))))
4075
4076 /* Sink binary operation to branches, but only if we can fold it.  */
4077 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4078          lshift rshift rdiv trunc_div ceil_div floor_div round_div
4079          trunc_mod ceil_mod floor_mod round_mod min max)
4080 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
4081  (simplify
4082   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4083   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4084
4085 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
4086  (simplify
4087   (op (vec_cond:s @0 @1 @2) @3)
4088   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4089  (simplify
4090   (op @3 (vec_cond:s @0 @1 @2))
4091   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4092
4093 #if GIMPLE
4094 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4095  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4096            INTEGER_CST@1)
4097  (with {
4098          int ibit = tree_log2 (@0);
4099          int ibit2 = tree_log2 (@1);
4100        }
4101   (if (ibit == ibit2
4102       && ibit >= 0
4103       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4104
4105 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4106  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4107           INTEGER_CST@1)
4108  (with {
4109          int ibit = tree_log2 (@0);
4110          int ibit2 = tree_log2 (@1);
4111        }
4112   (if (ibit == ibit2
4113       && ibit >= 0
4114       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4115
4116 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4117  (bit_and:c
4118   (convert1?@4
4119    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4120   (convert2? @0))
4121  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4122
4123 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4124  (bit_and:c
4125   (convert1?@4
4126    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4127   (convert2? @0))
4128  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4129
4130 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4131  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4132             INTEGER_CST@1)
4133  (with {
4134          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4135                                               TYPE_PRECISION(type)));
4136          int ibit2 = tree_log2 (@1);
4137        }
4138   (if (ibit == ibit2
4139       && ibit >= 0
4140       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4141
4142 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4143  (bit_and@4
4144   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4145   INTEGER_CST@1)
4146  (with {
4147          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4148                                               TYPE_PRECISION(type)));
4149          int ibit2 = tree_log2 (@1);
4150        }
4151   (if (ibit == ibit2
4152       && ibit >= 0
4153       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4154
4155 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4156  (bit_and:c
4157   (convert1?@3
4158    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4159   (convert2? @0))
4160  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4161
4162 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4163  (bit_and:c
4164   (convert1?@3
4165    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4166   (convert2? @0))
4167   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4168
4169 #endif
4170
4171 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4172    Currently disabled after pass lvec because ARM understands
4173    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
4174 (simplify
4175  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4176  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4177   (vec_cond (bit_and @0 @3) @1 @2)))
4178 (simplify
4179  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4180  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4181   (vec_cond (bit_ior @0 @3) @1 @2)))
4182 (simplify
4183  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4184  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4185   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4186 (simplify
4187  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4188  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4189   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4190
4191 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
4192 (simplify
4193  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4194  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4195   (vec_cond (bit_and @0 @1) @2 @3)))
4196 (simplify
4197  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4198  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4199   (vec_cond (bit_ior @0 @1) @2 @3)))
4200 (simplify
4201  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4202  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4203   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4204 (simplify
4205  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4206  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4207   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4208
4209 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4210    types are compatible.  */
4211 (simplify
4212  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4213  (if (VECTOR_BOOLEAN_TYPE_P (type)
4214       && types_match (type, TREE_TYPE (@0)))
4215   (if (integer_zerop (@1) && integer_all_onesp (@2))
4216    (bit_not @0)
4217    (if (integer_all_onesp (@1) && integer_zerop (@2))
4218     @0))))
4219
4220 /* A few simplifications of "a ? CST1 : CST2". */
4221 /* NOTE: Only do this on gimple as the if-chain-to-switch
4222    optimization depends on the gimple to have if statements in it. */
4223 #if GIMPLE
4224 (simplify
4225  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4226  (switch
4227   (if (integer_zerop (@2))
4228    (switch
4229     /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4230     (if (integer_onep (@1))
4231      (convert (convert:boolean_type_node @0)))
4232     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4233     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4234      (with {
4235        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4236       }
4237       (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4238     /* a ? -1 : 0 -> -a.  No need to check the TYPE_PRECISION not being 1
4239        here as the powerof2cst case above will handle that case correctly.  */
4240     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4241      (negate (convert (convert:boolean_type_node @0))))))
4242   (if (integer_zerop (@1))
4243    (with {
4244       tree booltrue = constant_boolean_node (true, boolean_type_node);
4245     }
4246     (switch
4247      /* a ? 0 : 1 -> !a. */
4248      (if (integer_onep (@2))
4249       (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4250      /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4251      (if (INTEGRAL_TYPE_P (type) &&  integer_pow2p (@2))
4252       (with {
4253         tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4254        }
4255        (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4256         { shift; })))
4257      /* a ? -1 : 0 -> -(!a).  No need to check the TYPE_PRECISION not being 1
4258        here as the powerof2cst case above will handle that case correctly.  */
4259      (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4260       (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4261     )
4262    )
4263   )
4264  )
4265 )
4266 #endif
4267
4268 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
4269    be extended.  */
4270 /* This pattern implements two kinds simplification:
4271
4272    Case 1)
4273    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4274      1) Conversions are type widening from smaller type.
4275      2) Const c1 equals to c2 after canonicalizing comparison.
4276      3) Comparison has tree code LT, LE, GT or GE.
4277    This specific pattern is needed when (cmp (convert x) c) may not
4278    be simplified by comparison patterns because of multiple uses of
4279    x.  It also makes sense here because simplifying across multiple
4280    referred var is always benefitial for complicated cases.
4281
4282    Case 2)
4283    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
4284 (for cmp (lt le gt ge eq)
4285  (simplify
4286   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4287   (with
4288    {
4289      tree from_type = TREE_TYPE (@1);
4290      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4291      enum tree_code code = ERROR_MARK;
4292
4293      if (INTEGRAL_TYPE_P (from_type)
4294          && int_fits_type_p (@2, from_type)
4295          && (types_match (c1_type, from_type)
4296              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4297                  && (TYPE_UNSIGNED (from_type)
4298                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4299          && (types_match (c2_type, from_type)
4300              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4301                  && (TYPE_UNSIGNED (from_type)
4302                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4303        {
4304          if (cmp != EQ_EXPR)
4305            {
4306              if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
4307                {
4308                  /* X <= Y - 1 equals to X < Y.  */
4309                  if (cmp == LE_EXPR)
4310                    code = LT_EXPR;
4311                  /* X > Y - 1 equals to X >= Y.  */
4312                  if (cmp == GT_EXPR)
4313                    code = GE_EXPR;
4314                }
4315              if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
4316                {
4317                  /* X < Y + 1 equals to X <= Y.  */
4318                  if (cmp == LT_EXPR)
4319                    code = LE_EXPR;
4320                  /* X >= Y + 1 equals to X > Y.  */
4321                  if (cmp == GE_EXPR)
4322                    code = GT_EXPR;
4323                }
4324              if (code != ERROR_MARK
4325                  || wi::to_widest (@2) == wi::to_widest (@3))
4326                {
4327                  if (cmp == LT_EXPR || cmp == LE_EXPR)
4328                    code = MIN_EXPR;
4329                  if (cmp == GT_EXPR || cmp == GE_EXPR)
4330                    code = MAX_EXPR;
4331                }
4332            }
4333          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
4334          else if (int_fits_type_p (@3, from_type))
4335            code = EQ_EXPR;
4336        }
4337    }
4338    (if (code == MAX_EXPR)
4339     (convert (max @1 (convert @2)))
4340     (if (code == MIN_EXPR)
4341      (convert (min @1 (convert @2)))
4342      (if (code == EQ_EXPR)
4343       (convert (cond (eq @1 (convert @3))
4344                      (convert:from_type @3) (convert:from_type @2)))))))))
4345
4346 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4347
4348      1) OP is PLUS or MINUS.
4349      2) CMP is LT, LE, GT or GE.
4350      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4351
4352    This pattern also handles special cases like:
4353
4354      A) Operand x is a unsigned to signed type conversion and c1 is
4355         integer zero.  In this case,
4356           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
4357           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
4358      B) Const c1 may not equal to (C3 op' C2).  In this case we also
4359         check equality for (c1+1) and (c1-1) by adjusting comparison
4360         code.
4361
4362    TODO: Though signed type is handled by this pattern, it cannot be
4363    simplified at the moment because C standard requires additional
4364    type promotion.  In order to match&simplify it here, the IR needs
4365    to be cleaned up by other optimizers, i.e, VRP.  */
4366 (for op (plus minus)
4367  (for cmp (lt le gt ge)
4368   (simplify
4369    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4370    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4371     (if (types_match (from_type, to_type)
4372          /* Check if it is special case A).  */
4373          || (TYPE_UNSIGNED (from_type)
4374              && !TYPE_UNSIGNED (to_type)
4375              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4376              && integer_zerop (@1)
4377              && (cmp == LT_EXPR || cmp == GE_EXPR)))
4378      (with
4379       {
4380         wi::overflow_type overflow = wi::OVF_NONE;
4381         enum tree_code code, cmp_code = cmp;
4382         wide_int real_c1;
4383         wide_int c1 = wi::to_wide (@1);
4384         wide_int c2 = wi::to_wide (@2);
4385         wide_int c3 = wi::to_wide (@3);
4386         signop sgn = TYPE_SIGN (from_type);
4387
4388         /* Handle special case A), given x of unsigned type:
4389             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
4390             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
4391         if (!types_match (from_type, to_type))
4392           {
4393             if (cmp_code == LT_EXPR)
4394               cmp_code = GT_EXPR;
4395             if (cmp_code == GE_EXPR)
4396               cmp_code = LE_EXPR;
4397             c1 = wi::max_value (to_type);
4398           }
4399         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
4400            compute (c3 op' c2) and check if it equals to c1 with op' being
4401            the inverted operator of op.  Make sure overflow doesn't happen
4402            if it is undefined.  */
4403         if (op == PLUS_EXPR)
4404           real_c1 = wi::sub (c3, c2, sgn, &overflow);
4405         else
4406           real_c1 = wi::add (c3, c2, sgn, &overflow);
4407
4408         code = cmp_code;
4409         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4410           {
4411             /* Check if c1 equals to real_c1.  Boundary condition is handled
4412                by adjusting comparison operation if necessary.  */
4413             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4414                 && !overflow)
4415               {
4416                 /* X <= Y - 1 equals to X < Y.  */
4417                 if (cmp_code == LE_EXPR)
4418                   code = LT_EXPR;
4419                 /* X > Y - 1 equals to X >= Y.  */
4420                 if (cmp_code == GT_EXPR)
4421                   code = GE_EXPR;
4422               }
4423             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4424                 && !overflow)
4425               {
4426                 /* X < Y + 1 equals to X <= Y.  */
4427                 if (cmp_code == LT_EXPR)
4428                   code = LE_EXPR;
4429                 /* X >= Y + 1 equals to X > Y.  */
4430                 if (cmp_code == GE_EXPR)
4431                   code = GT_EXPR;
4432               }
4433             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4434               {
4435                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4436                   code = MIN_EXPR;
4437                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4438                   code = MAX_EXPR;
4439               }
4440           }
4441       }
4442       (if (code == MAX_EXPR)
4443        (op (max @X { wide_int_to_tree (from_type, real_c1); })
4444            { wide_int_to_tree (from_type, c2); })
4445        (if (code == MIN_EXPR)
4446         (op (min @X { wide_int_to_tree (from_type, real_c1); })
4447             { wide_int_to_tree (from_type, c2); })))))))))
4448
4449 #if GIMPLE
4450 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
4451    in fold_cond_expr_with_comparison for GENERIC folding with
4452    some extra constraints.  */
4453 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
4454  (simplify
4455   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
4456         (convert3? @0) (convert4? @1))
4457   (if (!HONOR_SIGNED_ZEROS (type)
4458        && ((INTEGRAL_TYPE_P (type)
4459             /* Allow widening conversions of the data.  */
4460             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
4461             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
4462            || (tree_nop_conversion_p (type, TREE_TYPE (@0))
4463                && tree_nop_conversion_p (type, TREE_TYPE (@1)))))
4464    (switch
4465     (if (cmp == EQ_EXPR)
4466      (if (VECTOR_TYPE_P (type))
4467       (view_convert @c1)
4468       (convert @c1)))
4469     (if (cmp == NE_EXPR)
4470      (if (VECTOR_TYPE_P (type))
4471       (view_convert @c0)
4472       (convert @c0)))
4473     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
4474      (if (!HONOR_NANS (type))
4475       (if (VECTOR_TYPE_P (type))
4476        (view_convert (min @c0 @c1))
4477        (convert (min @c0 @c1)))))
4478     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
4479      (if (!HONOR_NANS (type))
4480       (if (VECTOR_TYPE_P (type))
4481        (view_convert (max @c0 @c1))
4482        (convert (max @c0 @c1)))))
4483     (if (cmp == UNEQ_EXPR)
4484      (if (!HONOR_NANS (type))
4485       (if (VECTOR_TYPE_P (type))
4486        (view_convert @c1)
4487        (convert @c1))))
4488     (if (cmp == LTGT_EXPR)
4489      (if (!HONOR_NANS (type))
4490       (if (VECTOR_TYPE_P (type))
4491        (view_convert @c0)
4492        (convert @c0))))))))
4493 #endif
4494
4495 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
4496 (simplify
4497  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
4498  (if (!TYPE_SATURATING (type)
4499       && (TYPE_OVERFLOW_WRAPS (type)
4500           || !wi::only_sign_bit_p (wi::to_wide (@1)))
4501       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
4502   @3))
4503
4504 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
4505 (simplify
4506  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
4507  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
4508   @3))
4509
4510 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
4511    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
4512 (simplify
4513  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
4514  (if (TYPE_UNSIGNED (type))
4515   (cond (ge @0 @1) (negate @0) @2)))
4516
4517 (for cnd (cond vec_cond)
4518  /* A ? B : (A ? X : C) -> A ? B : C.  */
4519  (simplify
4520   (cnd @0 (cnd @0 @1 @2) @3)
4521   (cnd @0 @1 @3))
4522  (simplify
4523   (cnd @0 @1 (cnd @0 @2 @3))
4524   (cnd @0 @1 @3))
4525  /* A ? B : (!A ? C : X) -> A ? B : C.  */
4526  /* ???  This matches embedded conditions open-coded because genmatch
4527     would generate matching code for conditions in separate stmts only.
4528     The following is still important to merge then and else arm cases
4529     from if-conversion.  */
4530  (simplify
4531   (cnd @0 @1 (cnd @2 @3 @4))
4532   (if (inverse_conditions_p (@0, @2))
4533    (cnd @0 @1 @3)))
4534  (simplify
4535   (cnd @0 (cnd @1 @2 @3) @4)
4536   (if (inverse_conditions_p (@0, @1))
4537    (cnd @0 @3 @4)))
4538
4539  /* A ? B : B -> B.  */
4540  (simplify
4541   (cnd @0 @1 @1)
4542   @1)
4543
4544  /* !A ? B : C -> A ? C : B.  */
4545  (simplify
4546   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4547   (cnd @0 @2 @1)))
4548
4549 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4550    Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4551    Need to handle UN* comparisons.
4552
4553    None of these transformations work for modes with signed
4554    zeros.  If A is +/-0, the first two transformations will
4555    change the sign of the result (from +0 to -0, or vice
4556    versa).  The last four will fix the sign of the result,
4557    even though the original expressions could be positive or
4558    negative, depending on the sign of A.
4559
4560    Note that all these transformations are correct if A is
4561    NaN, since the two alternatives (A and -A) are also NaNs.  */
4562
4563 (for cnd (cond vec_cond)
4564  /* A == 0 ? A : -A    same as -A */
4565  (for cmp (eq uneq)
4566   (simplify
4567    (cnd (cmp @0 zerop) @0 (negate@1 @0))
4568     (if (!HONOR_SIGNED_ZEROS (type))
4569      @1))
4570   (simplify
4571    (cnd (cmp @0 zerop) integer_zerop (negate@1 @0))
4572     (if (!HONOR_SIGNED_ZEROS (type))
4573      @1))
4574  )
4575  /* A != 0 ? A : -A    same as A */
4576  (for cmp (ne ltgt)
4577   (simplify
4578    (cnd (cmp @0 zerop) @0 (negate @0))
4579     (if (!HONOR_SIGNED_ZEROS (type))
4580      @0))
4581   (simplify
4582    (cnd (cmp @0 zerop) @0 integer_zerop)
4583     (if (!HONOR_SIGNED_ZEROS (type))
4584      @0))
4585  )
4586  /* A >=/> 0 ? A : -A    same as abs (A) */
4587  (for cmp (ge gt)
4588   (simplify
4589    (cnd (cmp @0 zerop) @0 (negate @0))
4590     (if (!HONOR_SIGNED_ZEROS (type)
4591          && !TYPE_UNSIGNED (type))
4592      (abs @0))))
4593  /* A <=/< 0 ? A : -A    same as -abs (A) */
4594  (for cmp (le lt)
4595   (simplify
4596    (cnd (cmp @0 zerop) @0 (negate @0))
4597     (if (!HONOR_SIGNED_ZEROS (type)
4598          && !TYPE_UNSIGNED (type))
4599      (if (ANY_INTEGRAL_TYPE_P (type)
4600           && !TYPE_OVERFLOW_WRAPS (type))
4601       (with {
4602         tree utype = unsigned_type_for (type);
4603        }
4604        (convert (negate (absu:utype @0))))
4605        (negate (abs @0)))))
4606  )
4607 )
4608
4609 /* -(type)!A -> (type)A - 1.  */
4610 (simplify
4611  (negate (convert?:s (logical_inverted_value:s @0)))
4612  (if (INTEGRAL_TYPE_P (type)
4613       && TREE_CODE (type) != BOOLEAN_TYPE
4614       && TYPE_PRECISION (type) > 1
4615       && TREE_CODE (@0) == SSA_NAME
4616       && ssa_name_has_boolean_range (@0))
4617   (plus (convert:type @0) { build_all_ones_cst (type); })))
4618
4619 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4620    return all -1 or all 0 results.  */
4621 /* ??? We could instead convert all instances of the vec_cond to negate,
4622    but that isn't necessarily a win on its own.  */
4623 (simplify
4624  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4625  (if (VECTOR_TYPE_P (type)
4626       && known_eq (TYPE_VECTOR_SUBPARTS (type),
4627                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4628       && (TYPE_MODE (TREE_TYPE (type))
4629           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4630   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4631
4632 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
4633 (simplify
4634  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4635  (if (VECTOR_TYPE_P (type)
4636       && known_eq (TYPE_VECTOR_SUBPARTS (type),
4637                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4638       && (TYPE_MODE (TREE_TYPE (type))
4639           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4640   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4641
4642
4643 /* Simplifications of comparisons.  */
4644
4645 /* See if we can reduce the magnitude of a constant involved in a
4646    comparison by changing the comparison code.  This is a canonicalization
4647    formerly done by maybe_canonicalize_comparison_1.  */
4648 (for cmp  (le gt)
4649      acmp (lt ge)
4650  (simplify
4651   (cmp @0 uniform_integer_cst_p@1)
4652   (with { tree cst = uniform_integer_cst_p (@1); }
4653    (if (tree_int_cst_sgn (cst) == -1)
4654      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4655                                    wide_int_to_tree (TREE_TYPE (cst),
4656                                                      wi::to_wide (cst)
4657                                                      + 1)); })))))
4658 (for cmp  (ge lt)
4659      acmp (gt le)
4660  (simplify
4661   (cmp @0 uniform_integer_cst_p@1)
4662   (with { tree cst = uniform_integer_cst_p (@1); }
4663    (if (tree_int_cst_sgn (cst) == 1)
4664     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4665                                   wide_int_to_tree (TREE_TYPE (cst),
4666                                   wi::to_wide (cst) - 1)); })))))
4667
4668 /* We can simplify a logical negation of a comparison to the
4669    inverted comparison.  As we cannot compute an expression
4670    operator using invert_tree_comparison we have to simulate
4671    that with expression code iteration.  */
4672 (for cmp (tcc_comparison)
4673      icmp (inverted_tcc_comparison)
4674      ncmp (inverted_tcc_comparison_with_nans)
4675  /* Ideally we'd like to combine the following two patterns
4676     and handle some more cases by using
4677       (logical_inverted_value (cmp @0 @1))
4678     here but for that genmatch would need to "inline" that.
4679     For now implement what forward_propagate_comparison did.  */
4680  (simplify
4681   (bit_not (cmp @0 @1))
4682   (if (VECTOR_TYPE_P (type)
4683        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4684    /* Comparison inversion may be impossible for trapping math,
4685       invert_tree_comparison will tell us.  But we can't use
4686       a computed operator in the replacement tree thus we have
4687       to play the trick below.  */
4688    (with { enum tree_code ic = invert_tree_comparison
4689              (cmp, HONOR_NANS (@0)); }
4690     (if (ic == icmp)
4691      (icmp @0 @1)
4692      (if (ic == ncmp)
4693       (ncmp @0 @1))))))
4694  (simplify
4695   (bit_xor (cmp @0 @1) integer_truep)
4696   (with { enum tree_code ic = invert_tree_comparison
4697             (cmp, HONOR_NANS (@0)); }
4698    (if (ic == icmp)
4699     (icmp @0 @1)
4700     (if (ic == ncmp)
4701      (ncmp @0 @1)))))
4702  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
4703  (simplify
4704   (ne (cmp@2 @0 @1) integer_zerop)
4705   (if (types_match (type, TREE_TYPE (@2)))
4706    (cmp @0 @1)))
4707  (simplify
4708   (eq (cmp@2 @0 @1) integer_truep)
4709   (if (types_match (type, TREE_TYPE (@2)))
4710    (cmp @0 @1)))
4711  (simplify
4712   (ne (cmp@2 @0 @1) integer_truep)
4713   (if (types_match (type, TREE_TYPE (@2)))
4714    (with { enum tree_code ic = invert_tree_comparison
4715              (cmp, HONOR_NANS (@0)); }
4716     (if (ic == icmp)
4717      (icmp @0 @1)
4718      (if (ic == ncmp)
4719       (ncmp @0 @1))))))
4720  (simplify
4721   (eq (cmp@2 @0 @1) integer_zerop)
4722   (if (types_match (type, TREE_TYPE (@2)))
4723    (with { enum tree_code ic = invert_tree_comparison
4724              (cmp, HONOR_NANS (@0)); }
4725     (if (ic == icmp)
4726      (icmp @0 @1)
4727      (if (ic == ncmp)
4728       (ncmp @0 @1)))))))
4729
4730 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4731    ??? The transformation is valid for the other operators if overflow
4732    is undefined for the type, but performing it here badly interacts
4733    with the transformation in fold_cond_expr_with_comparison which
4734    attempts to synthetize ABS_EXPR.  */
4735 (for cmp (eq ne)
4736  (for sub (minus pointer_diff)
4737   (simplify
4738    (cmp (sub@2 @0 @1) integer_zerop)
4739    (if (single_use (@2))
4740     (cmp @0 @1)))))
4741
4742 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4743    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
4744 (for cmp (lt ge)
4745  (simplify
4746   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4747    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4748         && !TYPE_UNSIGNED (TREE_TYPE (@0))
4749         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4750     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4751 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4752    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
4753 (simplify
4754  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4755   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4756        && !TYPE_UNSIGNED (TREE_TYPE (@0))
4757        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4758    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4759
4760 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4761    signed arithmetic case.  That form is created by the compiler
4762    often enough for folding it to be of value.  One example is in
4763    computing loop trip counts after Operator Strength Reduction.  */
4764 (for cmp (simple_comparison)
4765      scmp (swapped_simple_comparison)
4766  (simplify
4767   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4768   /* Handle unfolded multiplication by zero.  */
4769   (if (integer_zerop (@1))
4770    (cmp @1 @2)
4771    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4772         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4773         && single_use (@3))
4774     /* If @1 is negative we swap the sense of the comparison.  */
4775     (if (tree_int_cst_sgn (@1) < 0)
4776      (scmp @0 @2)
4777      (cmp @0 @2))))))
4778
4779 /* For integral types with undefined overflow fold
4780    x * C1 == C2 into x == C2 / C1 or false.
4781    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4782    Z / 2^n Z.  */
4783 (for cmp (eq ne)
4784  (simplify
4785   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4786   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4787        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4788        && wi::to_wide (@1) != 0)
4789    (with { widest_int quot; }
4790     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4791                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
4792      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4793      { constant_boolean_node (cmp == NE_EXPR, type); }))
4794    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4795         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4796         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4797     (cmp @0
4798      {
4799        tree itype = TREE_TYPE (@0);
4800        int p = TYPE_PRECISION (itype);
4801        wide_int m = wi::one (p + 1) << p;
4802        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4803        wide_int i = wide_int::from (wi::mod_inv (a, m),
4804                                     p, TYPE_SIGN (itype));
4805        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4806      })))))
4807
4808 /* Simplify comparison of something with itself.  For IEEE
4809    floating-point, we can only do some of these simplifications.  */
4810 (for cmp (eq ge le)
4811  (simplify
4812   (cmp @0 @0)
4813   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4814        || ! tree_expr_maybe_nan_p (@0))
4815    { constant_boolean_node (true, type); }
4816    (if (cmp != EQ_EXPR
4817         /* With -ftrapping-math conversion to EQ loses an exception.  */
4818         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
4819             || ! flag_trapping_math))
4820     (eq @0 @0)))))
4821 (for cmp (ne gt lt)
4822  (simplify
4823   (cmp @0 @0)
4824   (if (cmp != NE_EXPR
4825        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4826        || ! tree_expr_maybe_nan_p (@0))
4827    { constant_boolean_node (false, type); })))
4828 (for cmp (unle unge uneq)
4829  (simplify
4830   (cmp @0 @0)
4831   { constant_boolean_node (true, type); }))
4832 (for cmp (unlt ungt)
4833  (simplify
4834   (cmp @0 @0)
4835   (unordered @0 @0)))
4836 (simplify
4837  (ltgt @0 @0)
4838  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
4839   { constant_boolean_node (false, type); }))
4840
4841 /* x == ~x -> false */
4842 /* x != ~x -> true */
4843 (for cmp (eq ne)
4844  (simplify
4845   (cmp:c @0 (bit_not @0))
4846   { constant_boolean_node (cmp == NE_EXPR, type); }))
4847
4848 /* Fold ~X op ~Y as Y op X.  */
4849 (for cmp (simple_comparison)
4850  (simplify
4851   (cmp (bit_not@2 @0) (bit_not@3 @1))
4852   (if (single_use (@2) && single_use (@3))
4853    (cmp @1 @0))))
4854
4855 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
4856 (for cmp (simple_comparison)
4857      scmp (swapped_simple_comparison)
4858  (simplify
4859   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4860   (if (single_use (@2)
4861        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4862    (scmp @0 (bit_not @1)))))
4863
4864 (for cmp (simple_comparison)
4865  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
4866  (simplify
4867   (cmp (convert@2 @0) (convert? @1))
4868   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4869        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4870            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4871        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4872            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4873    (with
4874     {
4875       tree type1 = TREE_TYPE (@1);
4876       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4877         {
4878           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4879           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4880               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4881             type1 = float_type_node;
4882           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4883               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4884             type1 = double_type_node;
4885         }
4886       tree newtype
4887         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4888            ? TREE_TYPE (@0) : type1);
4889     }
4890     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4891      (cmp (convert:newtype @0) (convert:newtype @1))))))
4892
4893  (simplify
4894   (cmp @0 REAL_CST@1)
4895   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
4896   (switch
4897    /* a CMP (-0) -> a CMP 0  */
4898    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4899     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4900    /* (-0) CMP b -> 0 CMP b.  */
4901    (if (TREE_CODE (@0) == REAL_CST
4902         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
4903     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
4904    /* x != NaN is always true, other ops are always false.  */
4905    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4906         && !tree_expr_signaling_nan_p (@1)
4907         && !tree_expr_maybe_signaling_nan_p (@0))
4908     { constant_boolean_node (cmp == NE_EXPR, type); })
4909    /* NaN != y is always true, other ops are always false.  */
4910    (if (TREE_CODE (@0) == REAL_CST
4911         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
4912         && !tree_expr_signaling_nan_p (@0)
4913         && !tree_expr_signaling_nan_p (@1))
4914     { constant_boolean_node (cmp == NE_EXPR, type); })
4915    /* Fold comparisons against infinity.  */
4916    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4917         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4918     (with
4919      {
4920        REAL_VALUE_TYPE max;
4921        enum tree_code code = cmp;
4922        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4923        if (neg)
4924          code = swap_tree_comparison (code);
4925      }
4926      (switch
4927       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
4928       (if (code == GT_EXPR
4929            && !(HONOR_NANS (@0) && flag_trapping_math))
4930        { constant_boolean_node (false, type); })
4931       (if (code == LE_EXPR)
4932        /* x <= +Inf is always true, if we don't care about NaNs.  */
4933        (if (! HONOR_NANS (@0))
4934         { constant_boolean_node (true, type); }
4935         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4936            an "invalid" exception.  */
4937         (if (!flag_trapping_math)
4938          (eq @0 @0))))
4939       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4940          for == this introduces an exception for x a NaN.  */
4941       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4942            || code == GE_EXPR)
4943        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4944         (if (neg)
4945          (lt @0 { build_real (TREE_TYPE (@0), max); })
4946          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4947       /* x < +Inf is always equal to x <= DBL_MAX.  */
4948       (if (code == LT_EXPR)
4949        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4950         (if (neg)
4951          (ge @0 { build_real (TREE_TYPE (@0), max); })
4952          (le @0 { build_real (TREE_TYPE (@0), max); }))))
4953       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4954          an exception for x a NaN so use an unordered comparison.  */
4955       (if (code == NE_EXPR)
4956        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4957         (if (! HONOR_NANS (@0))
4958          (if (neg)
4959           (ge @0 { build_real (TREE_TYPE (@0), max); })
4960           (le @0 { build_real (TREE_TYPE (@0), max); }))
4961          (if (neg)
4962           (unge @0 { build_real (TREE_TYPE (@0), max); })
4963           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4964
4965  /* If this is a comparison of a real constant with a PLUS_EXPR
4966     or a MINUS_EXPR of a real constant, we can convert it into a
4967     comparison with a revised real constant as long as no overflow
4968     occurs when unsafe_math_optimizations are enabled.  */
4969  (if (flag_unsafe_math_optimizations)
4970   (for op (plus minus)
4971    (simplify
4972     (cmp (op @0 REAL_CST@1) REAL_CST@2)
4973     (with
4974      {
4975        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4976                                TREE_TYPE (@1), @2, @1);
4977      }
4978      (if (tem && !TREE_OVERFLOW (tem))
4979       (cmp @0 { tem; }))))))
4980
4981  /* Likewise, we can simplify a comparison of a real constant with
4982     a MINUS_EXPR whose first operand is also a real constant, i.e.
4983     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
4984     floating-point types only if -fassociative-math is set.  */
4985  (if (flag_associative_math)
4986   (simplify
4987    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4988    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4989     (if (tem && !TREE_OVERFLOW (tem))
4990      (cmp { tem; } @1)))))
4991
4992  /* Fold comparisons against built-in math functions.  */
4993  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4994   (for sq (SQRT)
4995    (simplify
4996     (cmp (sq @0) REAL_CST@1)
4997     (switch
4998      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4999       (switch
5000        /* sqrt(x) < y is always false, if y is negative.  */
5001        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5002         { constant_boolean_node (false, type); })
5003        /* sqrt(x) > y is always true, if y is negative and we
5004           don't care about NaNs, i.e. negative values of x.  */
5005        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5006         { constant_boolean_node (true, type); })
5007        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5008        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5009      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5010       (switch
5011        /* sqrt(x) < 0 is always false.  */
5012        (if (cmp == LT_EXPR)
5013         { constant_boolean_node (false, type); })
5014        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
5015        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5016         { constant_boolean_node (true, type); })
5017        /* sqrt(x) <= 0 -> x == 0.  */
5018        (if (cmp == LE_EXPR)
5019         (eq @0 @1))
5020        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
5021           == or !=.  In the last case:
5022
5023             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5024
5025           if x is negative or NaN.  Due to -funsafe-math-optimizations,
5026           the results for other x follow from natural arithmetic.  */
5027        (cmp @0 @1)))
5028      (if ((cmp == LT_EXPR
5029            || cmp == LE_EXPR
5030            || cmp == GT_EXPR
5031            || cmp == GE_EXPR)
5032           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5033           /* Give up for -frounding-math.  */
5034           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5035       (with
5036        {
5037          REAL_VALUE_TYPE c2;
5038          enum tree_code ncmp = cmp;
5039          const real_format *fmt
5040            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5041          real_arithmetic (&c2, MULT_EXPR,
5042                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5043          real_convert (&c2, fmt, &c2);
5044          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5045             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
5046          if (!REAL_VALUE_ISINF (c2))
5047            {
5048              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5049                                         build_real (TREE_TYPE (@0), c2));
5050              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5051                ncmp = ERROR_MARK;
5052              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5053                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5054                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5055              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5056                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5057                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5058              else
5059                {
5060                  /* With rounding to even, sqrt of up to 3 different values
5061                     gives the same normal result, so in some cases c2 needs
5062                     to be adjusted.  */
5063                  REAL_VALUE_TYPE c2alt, tow;
5064                  if (cmp == LT_EXPR || cmp == GE_EXPR)
5065                    tow = dconst0;
5066                  else
5067                    real_inf (&tow);
5068                  real_nextafter (&c2alt, fmt, &c2, &tow);
5069                  real_convert (&c2alt, fmt, &c2alt);
5070                  if (REAL_VALUE_ISINF (c2alt))
5071                    ncmp = ERROR_MARK;
5072                  else
5073                    {
5074                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5075                                            build_real (TREE_TYPE (@0), c2alt));
5076                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5077                        ncmp = ERROR_MARK;
5078                      else if (real_equal (&TREE_REAL_CST (c3),
5079                                           &TREE_REAL_CST (@1)))
5080                        c2 = c2alt;
5081                    }
5082                }
5083            }
5084        }
5085        (if (cmp == GT_EXPR || cmp == GE_EXPR)
5086         (if (REAL_VALUE_ISINF (c2))
5087          /* sqrt(x) > y is x == +Inf, when y is very large.  */
5088          (if (HONOR_INFINITIES (@0))
5089           (eq @0 { build_real (TREE_TYPE (@0), c2); })
5090           { constant_boolean_node (false, type); })
5091          /* sqrt(x) > c is the same as x > c*c.  */
5092          (if (ncmp != ERROR_MARK)
5093           (if (ncmp == GE_EXPR)
5094            (ge @0 { build_real (TREE_TYPE (@0), c2); })
5095            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
5096         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
5097         (if (REAL_VALUE_ISINF (c2))
5098          (switch
5099           /* sqrt(x) < y is always true, when y is a very large
5100              value and we don't care about NaNs or Infinities.  */
5101           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
5102            { constant_boolean_node (true, type); })
5103           /* sqrt(x) < y is x != +Inf when y is very large and we
5104              don't care about NaNs.  */
5105           (if (! HONOR_NANS (@0))
5106            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
5107           /* sqrt(x) < y is x >= 0 when y is very large and we
5108              don't care about Infinities.  */
5109           (if (! HONOR_INFINITIES (@0))
5110            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
5111           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
5112           (if (GENERIC)
5113            (truth_andif
5114             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5115             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
5116          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
5117          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
5118           (if (ncmp == LT_EXPR)
5119            (lt @0 { build_real (TREE_TYPE (@0), c2); })
5120            (le @0 { build_real (TREE_TYPE (@0), c2); }))
5121           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
5122           (if (ncmp != ERROR_MARK && GENERIC)
5123            (if (ncmp == LT_EXPR)
5124             (truth_andif
5125              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5126              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
5127             (truth_andif
5128              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5129              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
5130    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
5131    (simplify
5132     (cmp (sq @0) (sq @1))
5133       (if (! HONOR_NANS (@0))
5134         (cmp @0 @1))))))
5135
5136 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
5137 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
5138      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
5139  (simplify
5140   (cmp (float@0 @1) (float @2))
5141    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5142         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5143     (with
5144      {
5145        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5146        tree type1 = TREE_TYPE (@1);
5147        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5148        tree type2 = TREE_TYPE (@2);
5149        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5150      }
5151      (if (fmt.can_represent_integral_type_p (type1)
5152           && fmt.can_represent_integral_type_p (type2))
5153       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5154        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5155        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5156             && type1_signed_p >= type2_signed_p)
5157         (icmp @1 (convert @2))
5158         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5159              && type1_signed_p <= type2_signed_p)
5160          (icmp (convert:type2 @1) @2)
5161          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5162               && type1_signed_p == type2_signed_p)
5163           (icmp @1 @2))))))))))
5164
5165 /* Optimize various special cases of (FTYPE) N CMP CST.  */
5166 (for cmp  (lt le eq ne ge gt)
5167      icmp (le le eq ne ge ge)
5168  (simplify
5169   (cmp (float @0) REAL_CST@1)
5170    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5171         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5172     (with
5173      {
5174        tree itype = TREE_TYPE (@0);
5175        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5176        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5177        /* Be careful to preserve any potential exceptions due to
5178           NaNs.  qNaNs are ok in == or != context.
5179           TODO: relax under -fno-trapping-math or
5180           -fno-signaling-nans.  */
5181        bool exception_p
5182          = real_isnan (cst) && (cst->signalling
5183                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5184      }
5185      /* TODO: allow non-fitting itype and SNaNs when
5186         -fno-trapping-math.  */
5187      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5188       (with
5189        {
5190          signop isign = TYPE_SIGN (itype);
5191          REAL_VALUE_TYPE imin, imax;
5192          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5193          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5194
5195          REAL_VALUE_TYPE icst;
5196          if (cmp == GT_EXPR || cmp == GE_EXPR)
5197            real_ceil (&icst, fmt, cst);
5198          else if (cmp == LT_EXPR || cmp == LE_EXPR)
5199            real_floor (&icst, fmt, cst);
5200          else
5201            real_trunc (&icst, fmt, cst);
5202
5203          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5204
5205          bool overflow_p = false;
5206          wide_int icst_val
5207            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5208        }
5209        (switch
5210         /* Optimize cases when CST is outside of ITYPE's range.  */
5211         (if (real_compare (LT_EXPR, cst, &imin))
5212          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5213                                   type); })
5214         (if (real_compare (GT_EXPR, cst, &imax))
5215          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5216                                   type); })
5217         /* Remove cast if CST is an integer representable by ITYPE.  */
5218         (if (cst_int_p)
5219          (cmp @0 { gcc_assert (!overflow_p);
5220                    wide_int_to_tree (itype, icst_val); })
5221         )
5222         /* When CST is fractional, optimize
5223             (FTYPE) N == CST -> 0
5224             (FTYPE) N != CST -> 1.  */
5225         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5226          { constant_boolean_node (cmp == NE_EXPR, type); })
5227         /* Otherwise replace with sensible integer constant.  */
5228         (with
5229          {
5230            gcc_checking_assert (!overflow_p);
5231          }
5232          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5233
5234 /* Fold A /[ex] B CMP C to A CMP B * C.  */
5235 (for cmp (eq ne)
5236  (simplify
5237   (cmp (exact_div @0 @1) INTEGER_CST@2)
5238   (if (!integer_zerop (@1))
5239    (if (wi::to_wide (@2) == 0)
5240     (cmp @0 @2)
5241     (if (TREE_CODE (@1) == INTEGER_CST)
5242      (with
5243       {
5244         wi::overflow_type ovf;
5245         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5246                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5247       }
5248       (if (ovf)
5249        { constant_boolean_node (cmp == NE_EXPR, type); }
5250        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5251 (for cmp (lt le gt ge)
5252  (simplify
5253   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5254   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5255    (with
5256     {
5257       wi::overflow_type ovf;
5258       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5259                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5260     }
5261     (if (ovf)
5262      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5263                                         TYPE_SIGN (TREE_TYPE (@2)))
5264                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5265      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5266
5267 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5268
5269    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5270    For large C (more than min/B+2^size), this is also true, with the
5271    multiplication computed modulo 2^size.
5272    For intermediate C, this just tests the sign of A.  */
5273 (for cmp  (lt le gt ge)
5274      cmp2 (ge ge lt lt)
5275  (simplify
5276   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5277   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5278        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5279        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5280    (with
5281     {
5282       tree utype = TREE_TYPE (@2);
5283       wide_int denom = wi::to_wide (@1);
5284       wide_int right = wi::to_wide (@2);
5285       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5286       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5287       bool small = wi::leu_p (right, smax);
5288       bool large = wi::geu_p (right, smin);
5289     }
5290     (if (small || large)
5291      (cmp (convert:utype @0) (mult @2 (convert @1)))
5292      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5293
5294 /* Unordered tests if either argument is a NaN.  */
5295 (simplify
5296  (bit_ior (unordered @0 @0) (unordered @1 @1))
5297  (if (types_match (@0, @1))
5298   (unordered @0 @1)))
5299 (simplify
5300  (bit_and (ordered @0 @0) (ordered @1 @1))
5301  (if (types_match (@0, @1))
5302   (ordered @0 @1)))
5303 (simplify
5304  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5305  @2)
5306 (simplify
5307  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5308  @2)
5309
5310 /* Simple range test simplifications.  */
5311 /* A < B || A >= B -> true.  */
5312 (for test1 (lt le le le ne ge)
5313      test2 (ge gt ge ne eq ne)
5314  (simplify
5315   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5316   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5317        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5318    { constant_boolean_node (true, type); })))
5319 /* A < B && A >= B -> false.  */
5320 (for test1 (lt lt lt le ne eq)
5321      test2 (ge gt eq gt eq gt)
5322  (simplify
5323   (bit_and:c (test1 @0 @1) (test2 @0 @1))
5324   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5325        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5326    { constant_boolean_node (false, type); })))
5327
5328 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5329    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
5330
5331    Note that comparisons
5332      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
5333      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
5334    will be canonicalized to above so there's no need to
5335    consider them here.
5336  */
5337
5338 (for cmp (le gt)
5339      eqcmp (eq ne)
5340  (simplify
5341   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5342   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5343    (with
5344     {
5345      tree ty = TREE_TYPE (@0);
5346      unsigned prec = TYPE_PRECISION (ty);
5347      wide_int mask = wi::to_wide (@2, prec);
5348      wide_int rhs = wi::to_wide (@3, prec);
5349      signop sgn = TYPE_SIGN (ty);
5350     }
5351     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5352          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5353       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5354              { build_zero_cst (ty); }))))))
5355
5356 /* -A CMP -B -> B CMP A.  */
5357 (for cmp (tcc_comparison)
5358      scmp (swapped_tcc_comparison)
5359  (simplify
5360   (cmp (negate @0) (negate @1))
5361   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5362        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5363            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5364    (scmp @0 @1)))
5365  (simplify
5366   (cmp (negate @0) CONSTANT_CLASS_P@1)
5367   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5368        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5369            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5370    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5371     (if (tem && !TREE_OVERFLOW (tem))
5372      (scmp @0 { tem; }))))))
5373
5374 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
5375 (for op (eq ne)
5376  (simplify
5377   (op (abs @0) zerop@1)
5378   (op @0 @1)))
5379
5380 /* From fold_sign_changed_comparison and fold_widened_comparison.
5381    FIXME: the lack of symmetry is disturbing.  */
5382 (for cmp (simple_comparison)
5383  (simplify
5384   (cmp (convert@0 @00) (convert?@1 @10))
5385   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5386        /* Disable this optimization if we're casting a function pointer
5387           type on targets that require function pointer canonicalization.  */
5388        && !(targetm.have_canonicalize_funcptr_for_compare ()
5389             && ((POINTER_TYPE_P (TREE_TYPE (@00))
5390                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5391                 || (POINTER_TYPE_P (TREE_TYPE (@10))
5392                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5393        && single_use (@0))
5394    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5395         && (TREE_CODE (@10) == INTEGER_CST
5396             || @1 != @10)
5397         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5398             || cmp == NE_EXPR
5399             || cmp == EQ_EXPR)
5400         && !POINTER_TYPE_P (TREE_TYPE (@00)))
5401     /* ???  The special-casing of INTEGER_CST conversion was in the original
5402        code and here to avoid a spurious overflow flag on the resulting
5403        constant which fold_convert produces.  */
5404     (if (TREE_CODE (@1) == INTEGER_CST)
5405      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5406                                 TREE_OVERFLOW (@1)); })
5407      (cmp @00 (convert @1)))
5408
5409     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5410      /* If possible, express the comparison in the shorter mode.  */
5411      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5412            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5413            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5414                && TYPE_UNSIGNED (TREE_TYPE (@00))))
5415           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5416               || ((TYPE_PRECISION (TREE_TYPE (@00))
5417                    >= TYPE_PRECISION (TREE_TYPE (@10)))
5418                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
5419                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
5420               || (TREE_CODE (@10) == INTEGER_CST
5421                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5422                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
5423       (cmp @00 (convert @10))
5424       (if (TREE_CODE (@10) == INTEGER_CST
5425            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5426            && !int_fits_type_p (@10, TREE_TYPE (@00)))
5427        (with
5428         {
5429           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5430           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5431           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5432           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5433         }
5434         (if (above || below)
5435          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5436           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5437           (if (cmp == LT_EXPR || cmp == LE_EXPR)
5438            { constant_boolean_node (above ? true : false, type); }
5439            (if (cmp == GT_EXPR || cmp == GE_EXPR)
5440             { constant_boolean_node (above ? false : true, type); }))))))))))))
5441
5442 (for cmp (eq ne)
5443  (simplify
5444   /* SSA names are canonicalized to 2nd place.  */
5445   (cmp addr@0 SSA_NAME@1)
5446   (with
5447    { poly_int64 off; tree base; }
5448    /* A local variable can never be pointed to by
5449       the default SSA name of an incoming parameter.  */
5450    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5451         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5452         && (base = get_base_address (TREE_OPERAND (@0, 0)))
5453         && TREE_CODE (base) == VAR_DECL
5454         && auto_var_in_fn_p (base, current_function_decl))
5455     (if (cmp == NE_EXPR)
5456      { constant_boolean_node (true, type); }
5457      { constant_boolean_node (false, type); })
5458     /* If the address is based on @1 decide using the offset.  */
5459     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5460          && TREE_CODE (base) == MEM_REF
5461          && TREE_OPERAND (base, 0) == @1)
5462      (with { off += mem_ref_offset (base).force_shwi (); }
5463       (if (known_ne (off, 0))
5464        { constant_boolean_node (cmp == NE_EXPR, type); }
5465        (if (known_eq (off, 0))
5466         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5467
5468 /* Equality compare simplifications from fold_binary  */
5469 (for cmp (eq ne)
5470
5471  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5472     Similarly for NE_EXPR.  */
5473  (simplify
5474   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5475   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5476        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5477    { constant_boolean_node (cmp == NE_EXPR, type); }))
5478
5479  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
5480  (simplify
5481   (cmp (bit_xor @0 @1) integer_zerop)
5482   (cmp @0 @1))
5483
5484  /* (X ^ Y) == Y becomes X == 0.
5485     Likewise (X ^ Y) == X becomes Y == 0.  */
5486  (simplify
5487   (cmp:c (bit_xor:c @0 @1) @0)
5488   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5489
5490  /* (X & Y) == X becomes (X & ~Y) == 0.  */
5491  (simplify
5492   (cmp:c (bit_and:c @0 @1) @0)
5493   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5494  (simplify
5495   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5496   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5497        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5498        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5499        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5500        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5501        && !wi::neg_p (wi::to_wide (@1)))
5502    (cmp (bit_and @0 (convert (bit_not @1)))
5503         { build_zero_cst (TREE_TYPE (@0)); })))
5504
5505  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
5506  (simplify
5507   (cmp:c (bit_ior:c @0 @1) @1)
5508   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5509
5510  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
5511  (simplify
5512   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5513   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5514    (cmp @0 (bit_xor @1 (convert @2)))))
5515
5516  (simplify
5517   (cmp (convert? addr@0) integer_zerop)
5518   (if (tree_single_nonzero_warnv_p (@0, NULL))
5519    { constant_boolean_node (cmp == NE_EXPR, type); }))
5520
5521  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
5522  (simplify
5523   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5524   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5525
5526 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5527    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5528    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5529    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
5530 (for cmp (eq ne)
5531      ncmp (ge lt)
5532  (for sgncmp (ge lt)
5533   (simplify
5534    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5535    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5536         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5537         && types_match (@0, @1))
5538     (ncmp (bit_xor @0 @1) @2)))))
5539 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5540    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
5541 (for cmp (eq ne)
5542      ncmp (lt ge)
5543  (simplify
5544   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5545    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5546         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5547         && types_match (@0, @1))
5548     (ncmp (bit_xor @0 @1) @2))))
5549
5550 /* If we have (A & C) == C where C is a power of 2, convert this into
5551    (A & C) != 0.  Similarly for NE_EXPR.  */
5552 (for cmp (eq ne)
5553      icmp (ne eq)
5554  (simplify
5555   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5556   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5557
5558 /* From fold_binary_op_with_conditional_arg handle the case of
5559    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
5560    compares simplify.  */
5561 (for cmp (simple_comparison)
5562  (simplify
5563   (cmp:c (cond @0 @1 @2) @3)
5564   /* Do not move possibly trapping operations into the conditional as this
5565      pessimizes code and causes gimplification issues when applied late.  */
5566   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
5567        || operation_could_trap_p (cmp, true, false, @3))
5568    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
5569
5570 (for cmp (ge lt)
5571 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5572 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5573  (simplify
5574   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5575    (if (INTEGRAL_TYPE_P (type)
5576         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5577         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5578         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5579     (with
5580      {
5581        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5582      }
5583     (if (cmp == LT_EXPR)
5584      (bit_xor (convert (rshift @0 {shifter;})) @1)
5585      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5586 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5587 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5588  (simplify
5589   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5590    (if (INTEGRAL_TYPE_P (type)
5591         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5592         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5593         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5594     (with
5595      {
5596        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5597      }
5598     (if (cmp == GE_EXPR)
5599      (bit_xor (convert (rshift @0 {shifter;})) @1)
5600      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5601
5602 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5603    convert this into a shift followed by ANDing with D.  */
5604 (simplify
5605  (cond
5606   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5607   INTEGER_CST@2 integer_zerop)
5608  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5609   (with {
5610      int shift = (wi::exact_log2 (wi::to_wide (@2))
5611                   - wi::exact_log2 (wi::to_wide (@1)));
5612    }
5613    (if (shift > 0)
5614     (bit_and
5615      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5616     (bit_and
5617      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5618      @2)))))
5619
5620 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5621    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
5622 (for cmp (eq ne)
5623      ncmp (ge lt)
5624  (simplify
5625   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5626   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5627        && type_has_mode_precision_p (TREE_TYPE (@0))
5628        && element_precision (@2) >= element_precision (@0)
5629        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5630    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5631     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5632
5633 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5634    this into a right shift or sign extension followed by ANDing with C.  */
5635 (simplify
5636  (cond
5637   (lt @0 integer_zerop)
5638   INTEGER_CST@1 integer_zerop)
5639  (if (integer_pow2p (@1)
5640       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5641   (with {
5642     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5643    }
5644    (if (shift >= 0)
5645     (bit_and
5646      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5647      @1)
5648     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5649        sign extension followed by AND with C will achieve the effect.  */
5650     (bit_and (convert @0) @1)))))
5651
5652 /* When the addresses are not directly of decls compare base and offset.
5653    This implements some remaining parts of fold_comparison address
5654    comparisons but still no complete part of it.  Still it is good
5655    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
5656 (for cmp (simple_comparison)
5657  (simplify
5658   (cmp (convert1?@2 addr@0) (convert2? addr@1))
5659   (with
5660    {
5661      poly_int64 off0, off1;
5662      tree base0, base1;
5663      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
5664                                   off0, off1, GENERIC);
5665    }
5666    (if (equal == 1)
5667     (switch
5668      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5669       { constant_boolean_node (known_eq (off0, off1), type); })
5670      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5671       { constant_boolean_node (known_ne (off0, off1), type); })
5672      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5673       { constant_boolean_node (known_lt (off0, off1), type); })
5674      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5675       { constant_boolean_node (known_le (off0, off1), type); })
5676      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5677       { constant_boolean_node (known_ge (off0, off1), type); })
5678      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5679       { constant_boolean_node (known_gt (off0, off1), type); }))
5680     (if (equal == 0)
5681      (switch
5682       (if (cmp == EQ_EXPR)
5683        { constant_boolean_node (false, type); })
5684       (if (cmp == NE_EXPR)
5685        { constant_boolean_node (true, type); })))))))
5686
5687 /* Simplify pointer equality compares using PTA.  */
5688 (for neeq (ne eq)
5689  (simplify
5690   (neeq @0 @1)
5691   (if (POINTER_TYPE_P (TREE_TYPE (@0))
5692        && ptrs_compare_unequal (@0, @1))
5693    { constant_boolean_node (neeq != EQ_EXPR, type); })))
5694
5695 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5696    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5697    Disable the transform if either operand is pointer to function.
5698    This broke pr22051-2.c for arm where function pointer
5699    canonicalizaion is not wanted.  */
5700
5701 (for cmp (ne eq)
5702  (simplify
5703   (cmp (convert @0) INTEGER_CST@1)
5704   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5705          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5706          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5707          /* Don't perform this optimization in GENERIC if @0 has reference
5708             type when sanitizing.  See PR101210.  */
5709          && !(GENERIC
5710               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5711               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5712         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5713             && POINTER_TYPE_P (TREE_TYPE (@1))
5714             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5715        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5716    (cmp @0 (convert @1)))))
5717
5718 /* Non-equality compare simplifications from fold_binary  */
5719 (for cmp (lt gt le ge)
5720  /* Comparisons with the highest or lowest possible integer of
5721     the specified precision will have known values.  */
5722  (simplify
5723   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5724   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5725         || POINTER_TYPE_P (TREE_TYPE (@1))
5726         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5727        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5728    (with
5729     {
5730       tree cst = uniform_integer_cst_p (@1);
5731       tree arg1_type = TREE_TYPE (cst);
5732       unsigned int prec = TYPE_PRECISION (arg1_type);
5733       wide_int max = wi::max_value (arg1_type);
5734       wide_int signed_max = wi::max_value (prec, SIGNED);
5735       wide_int min = wi::min_value (arg1_type);
5736     }
5737     (switch
5738      (if (wi::to_wide (cst) == max)
5739       (switch
5740        (if (cmp == GT_EXPR)
5741         { constant_boolean_node (false, type); })
5742        (if (cmp == GE_EXPR)
5743         (eq @2 @1))
5744        (if (cmp == LE_EXPR)
5745         { constant_boolean_node (true, type); })
5746        (if (cmp == LT_EXPR)
5747         (ne @2 @1))))
5748      (if (wi::to_wide (cst) == min)
5749       (switch
5750        (if (cmp == LT_EXPR)
5751         { constant_boolean_node (false, type); })
5752        (if (cmp == LE_EXPR)
5753         (eq @2 @1))
5754        (if (cmp == GE_EXPR)
5755         { constant_boolean_node (true, type); })
5756        (if (cmp == GT_EXPR)
5757         (ne @2 @1))))
5758      (if (wi::to_wide (cst) == max - 1)
5759       (switch
5760        (if (cmp == GT_EXPR)
5761         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5762                                     wide_int_to_tree (TREE_TYPE (cst),
5763                                                       wi::to_wide (cst)
5764                                                       + 1)); }))
5765        (if (cmp == LE_EXPR)
5766         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5767                                     wide_int_to_tree (TREE_TYPE (cst),
5768                                                       wi::to_wide (cst)
5769                                                       + 1)); }))))
5770      (if (wi::to_wide (cst) == min + 1)
5771       (switch
5772        (if (cmp == GE_EXPR)
5773         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5774                                     wide_int_to_tree (TREE_TYPE (cst),
5775                                                       wi::to_wide (cst)
5776                                                       - 1)); }))
5777        (if (cmp == LT_EXPR)
5778         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5779                                     wide_int_to_tree (TREE_TYPE (cst),
5780                                                       wi::to_wide (cst)
5781                                                       - 1)); }))))
5782      (if (wi::to_wide (cst) == signed_max
5783           && TYPE_UNSIGNED (arg1_type)
5784           /* We will flip the signedness of the comparison operator
5785              associated with the mode of @1, so the sign bit is
5786              specified by this mode.  Check that @1 is the signed
5787              max associated with this sign bit.  */
5788           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5789           /* signed_type does not work on pointer types.  */
5790           && INTEGRAL_TYPE_P (arg1_type))
5791       /* The following case also applies to X < signed_max+1
5792          and X >= signed_max+1 because previous transformations.  */
5793       (if (cmp == LE_EXPR || cmp == GT_EXPR)
5794        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5795         (switch
5796          (if (cst == @1 && cmp == LE_EXPR)
5797           (ge (convert:st @0) { build_zero_cst (st); }))
5798          (if (cst == @1 && cmp == GT_EXPR)
5799           (lt (convert:st @0) { build_zero_cst (st); }))
5800          (if (cmp == LE_EXPR)
5801           (ge (view_convert:st @0) { build_zero_cst (st); }))
5802          (if (cmp == GT_EXPR)
5803           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5804
5805 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5806  /* If the second operand is NaN, the result is constant.  */
5807  (simplify
5808   (cmp @0 REAL_CST@1)
5809   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5810        && (cmp != LTGT_EXPR || ! flag_trapping_math))
5811    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5812                             ? false : true, type); })))
5813
5814 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
5815 (simplify
5816   (unordered @0 @1)
5817   (switch
5818     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5819         { constant_boolean_node (true, type); })
5820     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5821         { constant_boolean_node (false, type); })))
5822
5823 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
5824 (simplify
5825   (ordered @0 @1)
5826   (switch
5827     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5828         { constant_boolean_node (false, type); })
5829     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5830         { constant_boolean_node (true, type); })))
5831
5832 /* bool_var != 0 becomes bool_var.  */
5833 (simplify
5834  (ne @0 integer_zerop)
5835  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5836       && types_match (type, TREE_TYPE (@0)))
5837   (non_lvalue @0)))
5838 /* bool_var == 1 becomes bool_var.  */
5839 (simplify
5840  (eq @0 integer_onep)
5841  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5842       && types_match (type, TREE_TYPE (@0)))
5843   (non_lvalue @0)))
5844 /* Do not handle
5845    bool_var == 0 becomes !bool_var or
5846    bool_var != 1 becomes !bool_var
5847    here because that only is good in assignment context as long
5848    as we require a tcc_comparison in GIMPLE_CONDs where we'd
5849    replace if (x == 0) with tem = ~x; if (tem != 0) which is
5850    clearly less optimal and which we'll transform again in forwprop.  */
5851
5852 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
5853    where ~Y + 1 == pow2 and Z = ~Y.  */
5854 (for cst (VECTOR_CST INTEGER_CST)
5855  (for cmp (eq ne)
5856       icmp (le gt)
5857   (simplify
5858    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
5859     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
5860      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
5861       (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
5862        (icmp @0 { csts; })
5863        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5864          (icmp (view_convert:utype @0) { csts; }))))))))
5865
5866 /* When one argument is a constant, overflow detection can be simplified.
5867    Currently restricted to single use so as not to interfere too much with
5868    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
5869    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
5870 (for cmp (lt le ge gt)
5871      out (gt gt le le)
5872  (simplify
5873   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5874   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5875        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5876        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5877        && wi::to_wide (@1) != 0
5878        && single_use (@2))
5879    (with {
5880      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5881      signop sign = TYPE_SIGN (TREE_TYPE (@0));
5882     }
5883     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5884                                 wi::max_value (prec, sign)
5885                                 - wi::to_wide (@1)); })))))
5886
5887 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5888    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
5889    expects the long form, so we restrict the transformation for now.  */
5890 (for cmp (gt le)
5891  (simplify
5892   (cmp:c (minus@2 @0 @1) @0)
5893   (if (single_use (@2)
5894        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5895        && TYPE_UNSIGNED (TREE_TYPE (@0)))
5896    (cmp @1 @0))))
5897
5898 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
5899 (for cmp (ge lt)
5900  (simplify
5901   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5902    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5903         && TYPE_UNSIGNED (TREE_TYPE (@0)))
5904     (cmp @1 @0))))
5905
5906 /* Testing for overflow is unnecessary if we already know the result.  */
5907 /* A - B > A  */
5908 (for cmp (gt le)
5909      out (ne eq)
5910  (simplify
5911   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5912   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5913        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5914    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5915 /* A + B < A  */
5916 (for cmp (lt ge)
5917      out (ne eq)
5918  (simplify
5919   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5920   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5921        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5922    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5923
5924 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5925    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
5926 (for cmp (lt ge)
5927      out (ne eq)
5928  (simplify
5929   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5930   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5931    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5932     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5933
5934 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5935    is at least twice as wide as type of A and B, simplify to
5936    __builtin_mul_overflow (A, B, <unused>).  */
5937 (for cmp (eq ne)
5938  (simplify
5939   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5940        integer_zerop)
5941   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5942        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5943        && TYPE_UNSIGNED (TREE_TYPE (@0))
5944        && (TYPE_PRECISION (TREE_TYPE (@3))
5945            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5946        && tree_fits_uhwi_p (@2)
5947        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5948        && types_match (@0, @1)
5949        && type_has_mode_precision_p (TREE_TYPE (@0))
5950        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5951            != CODE_FOR_nothing))
5952    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5953     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5954
5955 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
5956 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
5957  (simplify
5958   (ovf (convert@2 @0) @1)
5959   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5960        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5961        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5962        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5963    (ovf @0 @1)))
5964  (simplify
5965   (ovf @1 (convert@2 @0))
5966   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5967        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5968        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5969        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5970    (ovf @1 @0))))
5971
5972 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
5973    are unsigned to x > (umax / cst).  */
5974 (simplify
5975  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
5976   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5977        && TYPE_UNSIGNED (TREE_TYPE (@0))
5978        && TYPE_MAX_VALUE (TREE_TYPE (@0))
5979        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
5980        && int_fits_type_p (@1, TREE_TYPE (@0)))
5981    (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))))
5982
5983 /* Simplification of math builtins.  These rules must all be optimizations
5984    as well as IL simplifications.  If there is a possibility that the new
5985    form could be a pessimization, the rule should go in the canonicalization
5986    section that follows this one.
5987
5988    Rules can generally go in this section if they satisfy one of
5989    the following:
5990
5991    - the rule describes an identity
5992
5993    - the rule replaces calls with something as simple as addition or
5994      multiplication
5995
5996    - the rule contains unary calls only and simplifies the surrounding
5997      arithmetic.  (The idea here is to exclude non-unary calls in which
5998      one operand is constant and in which the call is known to be cheap
5999      when the operand has that value.)  */
6000
6001 (if (flag_unsafe_math_optimizations)
6002  /* Simplify sqrt(x) * sqrt(x) -> x.  */
6003  (simplify
6004   (mult (SQRT_ALL@1 @0) @1)
6005   (if (!tree_expr_maybe_signaling_nan_p (@0))
6006    @0))
6007
6008  (for op (plus minus)
6009   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
6010   (simplify
6011    (op (rdiv @0 @1)
6012        (rdiv @2 @1))
6013    (rdiv (op @0 @2) @1)))
6014
6015  (for cmp (lt le gt ge)
6016       neg_cmp (gt ge lt le)
6017   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
6018   (simplify
6019    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
6020    (with
6021     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
6022     (if (tem
6023          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
6024               || (real_zerop (tem) && !real_zerop (@1))))
6025      (switch
6026       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
6027        (cmp @0 { tem; }))
6028       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
6029        (neg_cmp @0 { tem; })))))))
6030
6031  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
6032  (for root (SQRT CBRT)
6033   (simplify
6034    (mult (root:s @0) (root:s @1))
6035     (root (mult @0 @1))))
6036
6037  /* Simplify expN(x) * expN(y) -> expN(x+y). */
6038  (for exps (EXP EXP2 EXP10 POW10)
6039   (simplify
6040    (mult (exps:s @0) (exps:s @1))
6041     (exps (plus @0 @1))))
6042
6043  /* Simplify a/root(b/c) into a*root(c/b).  */
6044  (for root (SQRT CBRT)
6045   (simplify
6046    (rdiv @0 (root:s (rdiv:s @1 @2)))
6047     (mult @0 (root (rdiv @2 @1)))))
6048
6049  /* Simplify x/expN(y) into x*expN(-y).  */
6050  (for exps (EXP EXP2 EXP10 POW10)
6051   (simplify
6052    (rdiv @0 (exps:s @1))
6053     (mult @0 (exps (negate @1)))))
6054
6055  (for logs (LOG LOG2 LOG10 LOG10)
6056       exps (EXP EXP2 EXP10 POW10)
6057   /* logN(expN(x)) -> x.  */
6058   (simplify
6059    (logs (exps @0))
6060    @0)
6061   /* expN(logN(x)) -> x.  */
6062   (simplify
6063    (exps (logs @0))
6064    @0))
6065
6066  /* Optimize logN(func()) for various exponential functions.  We
6067     want to determine the value "x" and the power "exponent" in
6068     order to transform logN(x**exponent) into exponent*logN(x).  */
6069  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
6070       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
6071   (simplify
6072    (logs (exps @0))
6073    (if (SCALAR_FLOAT_TYPE_P (type))
6074     (with {
6075       tree x;
6076       switch (exps)
6077         {
6078         CASE_CFN_EXP:
6079           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
6080           x = build_real_truncate (type, dconst_e ());
6081           break;
6082         CASE_CFN_EXP2:
6083           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
6084           x = build_real (type, dconst2);
6085           break;
6086         CASE_CFN_EXP10:
6087         CASE_CFN_POW10:
6088           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
6089           {
6090             REAL_VALUE_TYPE dconst10;
6091             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
6092             x = build_real (type, dconst10);
6093           }
6094           break;
6095         default:
6096           gcc_unreachable ();
6097         }
6098       }
6099      (mult (logs { x; }) @0)))))
6100
6101  (for logs (LOG LOG
6102             LOG2 LOG2
6103             LOG10 LOG10)
6104       exps (SQRT CBRT)
6105   (simplify
6106    (logs (exps @0))
6107    (if (SCALAR_FLOAT_TYPE_P (type))
6108     (with {
6109       tree x;
6110       switch (exps)
6111         {
6112         CASE_CFN_SQRT:
6113           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
6114           x = build_real (type, dconsthalf);
6115           break;
6116         CASE_CFN_CBRT:
6117           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
6118           x = build_real_truncate (type, dconst_third ());
6119           break;
6120         default:
6121           gcc_unreachable ();
6122         }
6123       }
6124      (mult { x; } (logs @0))))))
6125
6126  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
6127  (for logs (LOG LOG2 LOG10)
6128       pows (POW)
6129   (simplify
6130    (logs (pows @0 @1))
6131    (mult @1 (logs @0))))
6132
6133  /* pow(C,x) -> exp(log(C)*x) if C > 0,
6134     or if C is a positive power of 2,
6135     pow(C,x) -> exp2(log2(C)*x).  */
6136 #if GIMPLE
6137  (for pows (POW)
6138       exps (EXP)
6139       logs (LOG)
6140       exp2s (EXP2)
6141       log2s (LOG2)
6142   (simplify
6143    (pows REAL_CST@0 @1)
6144    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6145         && real_isfinite (TREE_REAL_CST_PTR (@0))
6146         /* As libmvec doesn't have a vectorized exp2, defer optimizing
6147            the use_exp2 case until after vectorization.  It seems actually
6148            beneficial for all constants to postpone this until later,
6149            because exp(log(C)*x), while faster, will have worse precision
6150            and if x folds into a constant too, that is unnecessary
6151            pessimization.  */
6152         && canonicalize_math_after_vectorization_p ())
6153     (with {
6154        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
6155        bool use_exp2 = false;
6156        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
6157            && value->cl == rvc_normal)
6158          {
6159            REAL_VALUE_TYPE frac_rvt = *value;
6160            SET_REAL_EXP (&frac_rvt, 1);
6161            if (real_equal (&frac_rvt, &dconst1))
6162              use_exp2 = true;
6163          }
6164      }
6165      (if (!use_exp2)
6166       (if (optimize_pow_to_exp (@0, @1))
6167        (exps (mult (logs @0) @1)))
6168       (exp2s (mult (log2s @0) @1)))))))
6169 #endif
6170
6171  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
6172  (for pows (POW)
6173       exps (EXP EXP2 EXP10 POW10)
6174       logs (LOG LOG2 LOG10 LOG10)
6175   (simplify
6176    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
6177    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6178         && real_isfinite (TREE_REAL_CST_PTR (@0)))
6179     (exps (plus (mult (logs @0) @1) @2)))))
6180
6181  (for sqrts (SQRT)
6182       cbrts (CBRT)
6183       pows (POW)
6184       exps (EXP EXP2 EXP10 POW10)
6185   /* sqrt(expN(x)) -> expN(x*0.5).  */
6186   (simplify
6187    (sqrts (exps @0))
6188    (exps (mult @0 { build_real (type, dconsthalf); })))
6189   /* cbrt(expN(x)) -> expN(x/3).  */
6190   (simplify
6191    (cbrts (exps @0))
6192    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6193   /* pow(expN(x), y) -> expN(x*y).  */
6194   (simplify
6195    (pows (exps @0) @1)
6196    (exps (mult @0 @1))))
6197
6198  /* tan(atan(x)) -> x.  */
6199  (for tans (TAN)
6200       atans (ATAN)
6201   (simplify
6202    (tans (atans @0))
6203    @0)))
6204
6205  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6206  (for sins (SIN)
6207       atans (ATAN)
6208       sqrts (SQRT)
6209       copysigns (COPYSIGN)
6210   (simplify
6211    (sins (atans:s @0))
6212    (with
6213      {
6214       REAL_VALUE_TYPE r_cst;
6215       build_sinatan_real (&r_cst, type);
6216       tree t_cst = build_real (type, r_cst);
6217       tree t_one = build_one_cst (type);
6218      }
6219     (if (SCALAR_FLOAT_TYPE_P (type))
6220      (cond (lt (abs @0) { t_cst; })
6221       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6222       (copysigns { t_one; } @0))))))
6223
6224 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6225  (for coss (COS)
6226       atans (ATAN)
6227       sqrts (SQRT)
6228       copysigns (COPYSIGN)
6229   (simplify
6230    (coss (atans:s @0))
6231    (with
6232      {
6233       REAL_VALUE_TYPE r_cst;
6234       build_sinatan_real (&r_cst, type);
6235       tree t_cst = build_real (type, r_cst);
6236       tree t_one = build_one_cst (type);
6237       tree t_zero = build_zero_cst (type);
6238      }
6239     (if (SCALAR_FLOAT_TYPE_P (type))
6240      (cond (lt (abs @0) { t_cst; })
6241       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6242       (copysigns { t_zero; } @0))))))
6243
6244  (if (!flag_errno_math)
6245   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6246   (for sinhs (SINH)
6247        atanhs (ATANH)
6248        sqrts (SQRT)
6249    (simplify
6250     (sinhs (atanhs:s @0))
6251     (with { tree t_one = build_one_cst (type); }
6252     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6253
6254   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6255   (for coshs (COSH)
6256        atanhs (ATANH)
6257        sqrts (SQRT)
6258    (simplify
6259     (coshs (atanhs:s @0))
6260     (with { tree t_one = build_one_cst (type); }
6261     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6262
6263 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
6264 (simplify
6265  (CABS (complex:C @0 real_zerop@1))
6266  (abs @0))
6267
6268 /* trunc(trunc(x)) -> trunc(x), etc.  */
6269 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6270  (simplify
6271   (fns (fns @0))
6272   (fns @0)))
6273 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
6274 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6275  (simplify
6276   (fns integer_valued_real_p@0)
6277   @0))
6278
6279 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
6280 (simplify
6281  (HYPOT:c @0 real_zerop@1)
6282  (abs @0))
6283
6284 /* pow(1,x) -> 1.  */
6285 (simplify
6286  (POW real_onep@0 @1)
6287  @0)
6288
6289 (simplify
6290  /* copysign(x,x) -> x.  */
6291  (COPYSIGN_ALL @0 @0)
6292  @0)
6293
6294 (simplify
6295  /* copysign(x,-x) -> -x.  */
6296  (COPYSIGN_ALL @0 (negate@1 @0))
6297  @1)
6298
6299 (simplify
6300  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
6301  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6302  (abs @0))
6303
6304 (for scale (LDEXP SCALBN SCALBLN)
6305  /* ldexp(0, x) -> 0.  */
6306  (simplify
6307   (scale real_zerop@0 @1)
6308   @0)
6309  /* ldexp(x, 0) -> x.  */
6310  (simplify
6311   (scale @0 integer_zerop@1)
6312   @0)
6313  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
6314  (simplify
6315   (scale REAL_CST@0 @1)
6316   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6317    @0)))
6318
6319 /* Canonicalization of sequences of math builtins.  These rules represent
6320    IL simplifications but are not necessarily optimizations.
6321
6322    The sincos pass is responsible for picking "optimal" implementations
6323    of math builtins, which may be more complicated and can sometimes go
6324    the other way, e.g. converting pow into a sequence of sqrts.
6325    We only want to do these canonicalizations before the pass has run.  */
6326
6327 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6328  /* Simplify tan(x) * cos(x) -> sin(x). */
6329  (simplify
6330   (mult:c (TAN:s @0) (COS:s @0))
6331    (SIN @0))
6332
6333  /* Simplify x * pow(x,c) -> pow(x,c+1). */
6334  (simplify
6335   (mult:c @0 (POW:s @0 REAL_CST@1))
6336   (if (!TREE_OVERFLOW (@1))
6337    (POW @0 (plus @1 { build_one_cst (type); }))))
6338
6339  /* Simplify sin(x) / cos(x) -> tan(x). */
6340  (simplify
6341   (rdiv (SIN:s @0) (COS:s @0))
6342    (TAN @0))
6343
6344  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6345  (simplify
6346   (rdiv (SINH:s @0) (COSH:s @0))
6347    (TANH @0))
6348
6349  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6350  (simplify
6351    (rdiv (TANH:s @0) (SINH:s @0))
6352    (rdiv {build_one_cst (type);} (COSH @0)))
6353
6354  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6355  (simplify
6356   (rdiv (COS:s @0) (SIN:s @0))
6357    (rdiv { build_one_cst (type); } (TAN @0)))
6358
6359  /* Simplify sin(x) / tan(x) -> cos(x). */
6360  (simplify
6361   (rdiv (SIN:s @0) (TAN:s @0))
6362   (if (! HONOR_NANS (@0)
6363        && ! HONOR_INFINITIES (@0))
6364    (COS @0)))
6365
6366  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6367  (simplify
6368   (rdiv (TAN:s @0) (SIN:s @0))
6369   (if (! HONOR_NANS (@0)
6370        && ! HONOR_INFINITIES (@0))
6371    (rdiv { build_one_cst (type); } (COS @0))))
6372
6373  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6374  (simplify
6375   (mult (POW:s @0 @1) (POW:s @0 @2))
6376    (POW @0 (plus @1 @2)))
6377
6378  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6379  (simplify
6380   (mult (POW:s @0 @1) (POW:s @2 @1))
6381    (POW (mult @0 @2) @1))
6382
6383  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6384  (simplify
6385   (mult (POWI:s @0 @1) (POWI:s @2 @1))
6386    (POWI (mult @0 @2) @1))
6387
6388  /* Simplify pow(x,c) / x -> pow(x,c-1). */
6389  (simplify
6390   (rdiv (POW:s @0 REAL_CST@1) @0)
6391   (if (!TREE_OVERFLOW (@1))
6392    (POW @0 (minus @1 { build_one_cst (type); }))))
6393
6394  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6395  (simplify
6396   (rdiv @0 (POW:s @1 @2))
6397    (mult @0 (POW @1 (negate @2))))
6398
6399  (for sqrts (SQRT)
6400       cbrts (CBRT)
6401       pows (POW)
6402   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
6403   (simplify
6404    (sqrts (sqrts @0))
6405    (pows @0 { build_real (type, dconst_quarter ()); }))
6406   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
6407   (simplify
6408    (sqrts (cbrts @0))
6409    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6410   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
6411   (simplify
6412    (cbrts (sqrts @0))
6413    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6414   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
6415   (simplify
6416    (cbrts (cbrts tree_expr_nonnegative_p@0))
6417    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6418   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
6419   (simplify
6420    (sqrts (pows @0 @1))
6421    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6422   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
6423   (simplify
6424    (cbrts (pows tree_expr_nonnegative_p@0 @1))
6425    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6426   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
6427   (simplify
6428    (pows (sqrts @0) @1)
6429    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6430   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
6431   (simplify
6432    (pows (cbrts tree_expr_nonnegative_p@0) @1)
6433    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6434   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
6435   (simplify
6436    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6437    (pows @0 (mult @1 @2))))
6438
6439  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
6440  (simplify
6441   (CABS (complex @0 @0))
6442   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6443
6444  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
6445  (simplify
6446   (HYPOT @0 @0)
6447   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6448
6449  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
6450  (for cexps (CEXP)
6451       exps (EXP)
6452       cexpis (CEXPI)
6453   (simplify
6454    (cexps compositional_complex@0)
6455    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6456     (complex
6457      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6458      (mult @1 (imagpart @2)))))))
6459
6460 (if (canonicalize_math_p ())
6461  /* floor(x) -> trunc(x) if x is nonnegative.  */
6462  (for floors (FLOOR_ALL)
6463       truncs (TRUNC_ALL)
6464   (simplify
6465    (floors tree_expr_nonnegative_p@0)
6466    (truncs @0))))
6467
6468 (match double_value_p
6469  @0
6470  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6471 (for froms (BUILT_IN_TRUNCL
6472             BUILT_IN_FLOORL
6473             BUILT_IN_CEILL
6474             BUILT_IN_ROUNDL
6475             BUILT_IN_NEARBYINTL
6476             BUILT_IN_RINTL)
6477      tos (BUILT_IN_TRUNC
6478           BUILT_IN_FLOOR
6479           BUILT_IN_CEIL
6480           BUILT_IN_ROUND
6481           BUILT_IN_NEARBYINT
6482           BUILT_IN_RINT)
6483  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
6484  (if (optimize && canonicalize_math_p ())
6485   (simplify
6486    (froms (convert double_value_p@0))
6487    (convert (tos @0)))))
6488
6489 (match float_value_p
6490  @0
6491  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6492 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6493             BUILT_IN_FLOORL BUILT_IN_FLOOR
6494             BUILT_IN_CEILL BUILT_IN_CEIL
6495             BUILT_IN_ROUNDL BUILT_IN_ROUND
6496             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6497             BUILT_IN_RINTL BUILT_IN_RINT)
6498      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6499           BUILT_IN_FLOORF BUILT_IN_FLOORF
6500           BUILT_IN_CEILF BUILT_IN_CEILF
6501           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6502           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6503           BUILT_IN_RINTF BUILT_IN_RINTF)
6504  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6505     if x is a float.  */
6506  (if (optimize && canonicalize_math_p ()
6507       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6508   (simplify
6509    (froms (convert float_value_p@0))
6510    (convert (tos @0)))))
6511
6512 #if GIMPLE
6513 (match float16_value_p
6514  @0
6515  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
6516 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
6517             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
6518             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
6519             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
6520             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
6521             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
6522             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
6523             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
6524      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
6525           IFN_FLOOR IFN_FLOOR IFN_FLOOR
6526           IFN_CEIL IFN_CEIL IFN_CEIL
6527           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
6528           IFN_ROUND IFN_ROUND IFN_ROUND
6529           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
6530           IFN_RINT IFN_RINT IFN_RINT
6531           IFN_SQRT IFN_SQRT IFN_SQRT)
6532  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
6533     if x is a _Float16.  */
6534  (simplify
6535    (convert (froms (convert float16_value_p@0)))
6536      (if (optimize
6537           && types_match (type, TREE_TYPE (@0))
6538           && direct_internal_fn_supported_p (as_internal_fn (tos),
6539                                              type, OPTIMIZE_FOR_BOTH))
6540        (tos @0))))
6541
6542 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
6543    x,y is float value, similar for _Float16/double.  */
6544 (for copysigns (COPYSIGN_ALL)
6545  (simplify
6546   (convert (copysigns (convert@2 @0) (convert @1)))
6547    (if (optimize
6548        && !HONOR_SNANS (@2)
6549        && types_match (type, TREE_TYPE (@0))
6550        && types_match (type, TREE_TYPE (@1))
6551        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
6552        && direct_internal_fn_supported_p (IFN_COPYSIGN,
6553                                           type, OPTIMIZE_FOR_BOTH))
6554     (IFN_COPYSIGN @0 @1))))
6555
6556 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
6557      tos (IFN_FMA IFN_FMA IFN_FMA)
6558  (simplify
6559   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
6560    (if (flag_unsafe_math_optimizations
6561        && optimize
6562        && FLOAT_TYPE_P (type)
6563        && FLOAT_TYPE_P (TREE_TYPE (@3))
6564        && types_match (type, TREE_TYPE (@0))
6565        && types_match (type, TREE_TYPE (@1))
6566        && types_match (type, TREE_TYPE (@2))
6567        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
6568        && direct_internal_fn_supported_p (as_internal_fn (tos),
6569                                           type, OPTIMIZE_FOR_BOTH))
6570     (tos @0 @1 @2))))
6571
6572 (for maxmin (max min)
6573  (simplify
6574   (convert (maxmin (convert@2 @0) (convert @1)))
6575    (if (optimize
6576        && FLOAT_TYPE_P (type)
6577        && FLOAT_TYPE_P (TREE_TYPE (@2))
6578        && types_match (type, TREE_TYPE (@0))
6579        && types_match (type, TREE_TYPE (@1))
6580        && element_precision (type) < element_precision (TREE_TYPE (@2)))
6581     (maxmin @0 @1))))
6582 #endif
6583
6584 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6585      tos (XFLOOR XCEIL XROUND XRINT)
6586  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
6587  (if (optimize && canonicalize_math_p ())
6588   (simplify
6589    (froms (convert double_value_p@0))
6590    (tos @0))))
6591
6592 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6593             XFLOOR XCEIL XROUND XRINT)
6594      tos (XFLOORF XCEILF XROUNDF XRINTF)
6595  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6596     if x is a float.  */
6597  (if (optimize && canonicalize_math_p ())
6598   (simplify
6599    (froms (convert float_value_p@0))
6600    (tos @0))))
6601
6602 (if (canonicalize_math_p ())
6603  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
6604  (for floors (IFLOOR LFLOOR LLFLOOR)
6605   (simplify
6606    (floors tree_expr_nonnegative_p@0)
6607    (fix_trunc @0))))
6608
6609 (if (canonicalize_math_p ())
6610  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
6611  (for fns (IFLOOR LFLOOR LLFLOOR
6612            ICEIL LCEIL LLCEIL
6613            IROUND LROUND LLROUND)
6614   (simplify
6615    (fns integer_valued_real_p@0)
6616    (fix_trunc @0)))
6617  (if (!flag_errno_math)
6618   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
6619   (for rints (IRINT LRINT LLRINT)
6620    (simplify
6621     (rints integer_valued_real_p@0)
6622     (fix_trunc @0)))))
6623
6624 (if (canonicalize_math_p ())
6625  (for ifn (IFLOOR ICEIL IROUND IRINT)
6626       lfn (LFLOOR LCEIL LROUND LRINT)
6627       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6628   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6629      sizeof (int) == sizeof (long).  */
6630   (if (TYPE_PRECISION (integer_type_node)
6631        == TYPE_PRECISION (long_integer_type_node))
6632    (simplify
6633     (ifn @0)
6634     (lfn:long_integer_type_node @0)))
6635   /* Canonicalize llround (x) to lround (x) on LP64 targets where
6636      sizeof (long long) == sizeof (long).  */
6637   (if (TYPE_PRECISION (long_long_integer_type_node)
6638        == TYPE_PRECISION (long_integer_type_node))
6639    (simplify
6640     (llfn @0)
6641     (lfn:long_integer_type_node @0)))))
6642
6643 /* cproj(x) -> x if we're ignoring infinities.  */
6644 (simplify
6645  (CPROJ @0)
6646  (if (!HONOR_INFINITIES (type))
6647    @0))
6648
6649 /* If the real part is inf and the imag part is known to be
6650    nonnegative, return (inf + 0i).  */
6651 (simplify
6652  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6653  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6654   { build_complex_inf (type, false); }))
6655
6656 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
6657 (simplify
6658  (CPROJ (complex @0 REAL_CST@1))
6659  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6660   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6661
6662 (for pows (POW)
6663      sqrts (SQRT)
6664      cbrts (CBRT)
6665  (simplify
6666   (pows @0 REAL_CST@1)
6667   (with {
6668     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6669     REAL_VALUE_TYPE tmp;
6670    }
6671    (switch
6672     /* pow(x,0) -> 1.  */
6673     (if (real_equal (value, &dconst0))
6674      { build_real (type, dconst1); })
6675     /* pow(x,1) -> x.  */
6676     (if (real_equal (value, &dconst1))
6677      @0)
6678     /* pow(x,-1) -> 1/x.  */
6679     (if (real_equal (value, &dconstm1))
6680      (rdiv { build_real (type, dconst1); } @0))
6681     /* pow(x,0.5) -> sqrt(x).  */
6682     (if (flag_unsafe_math_optimizations
6683          && canonicalize_math_p ()
6684          && real_equal (value, &dconsthalf))
6685      (sqrts @0))
6686     /* pow(x,1/3) -> cbrt(x).  */
6687     (if (flag_unsafe_math_optimizations
6688          && canonicalize_math_p ()
6689          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6690              real_equal (value, &tmp)))
6691      (cbrts @0))))))
6692
6693 /* powi(1,x) -> 1.  */
6694 (simplify
6695  (POWI real_onep@0 @1)
6696  @0)
6697
6698 (simplify
6699  (POWI @0 INTEGER_CST@1)
6700  (switch
6701   /* powi(x,0) -> 1.  */
6702   (if (wi::to_wide (@1) == 0)
6703    { build_real (type, dconst1); })
6704   /* powi(x,1) -> x.  */
6705   (if (wi::to_wide (@1) == 1)
6706    @0)
6707   /* powi(x,-1) -> 1/x.  */
6708   (if (wi::to_wide (@1) == -1)
6709    (rdiv { build_real (type, dconst1); } @0))))
6710
6711 /* Narrowing of arithmetic and logical operations.
6712
6713    These are conceptually similar to the transformations performed for
6714    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
6715    term we want to move all that code out of the front-ends into here.  */
6716
6717 /* Convert (outertype)((innertype0)a+(innertype1)b)
6718    into ((newtype)a+(newtype)b) where newtype
6719    is the widest mode from all of these.  */
6720 (for op (plus minus mult rdiv)
6721  (simplify
6722    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
6723    /* If we have a narrowing conversion of an arithmetic operation where
6724       both operands are widening conversions from the same type as the outer
6725       narrowing conversion.  Then convert the innermost operands to a
6726       suitable unsigned type (to avoid introducing undefined behavior),
6727       perform the operation and convert the result to the desired type.  */
6728    (if (INTEGRAL_TYPE_P (type)
6729         && op != MULT_EXPR
6730         && op != RDIV_EXPR
6731         /* We check for type compatibility between @0 and @1 below,
6732            so there's no need to check that @2/@4 are integral types.  */
6733         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6734         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6735         /* The precision of the type of each operand must match the
6736            precision of the mode of each operand, similarly for the
6737            result.  */
6738         && type_has_mode_precision_p (TREE_TYPE (@1))
6739         && type_has_mode_precision_p (TREE_TYPE (@2))
6740         && type_has_mode_precision_p (type)
6741         /* The inner conversion must be a widening conversion.  */
6742         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
6743         && types_match (@1, type)
6744         && (types_match (@1, @2)
6745             /* Or the second operand is const integer or converted const
6746                integer from valueize.  */
6747             || poly_int_tree_p (@4)))
6748      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
6749        (op @1 (convert @2))
6750        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6751         (convert (op (convert:utype @1)
6752                      (convert:utype @2)))))
6753      (if (FLOAT_TYPE_P (type)
6754           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6755                == DECIMAL_FLOAT_TYPE_P (type))
6756       (with { tree arg0 = strip_float_extensions (@1);
6757               tree arg1 = strip_float_extensions (@2);
6758               tree itype = TREE_TYPE (@0);
6759               tree ty1 = TREE_TYPE (arg0);
6760               tree ty2 = TREE_TYPE (arg1);
6761               enum tree_code code = TREE_CODE (itype); }
6762         (if (FLOAT_TYPE_P (ty1)
6763              && FLOAT_TYPE_P (ty2))
6764          (with { tree newtype = type;
6765                  if (TYPE_MODE (ty1) == SDmode
6766                      || TYPE_MODE (ty2) == SDmode
6767                      || TYPE_MODE (type) == SDmode)
6768                    newtype = dfloat32_type_node;
6769                  if (TYPE_MODE (ty1) == DDmode
6770                      || TYPE_MODE (ty2) == DDmode
6771                      || TYPE_MODE (type) == DDmode)
6772                    newtype = dfloat64_type_node;
6773                  if (TYPE_MODE (ty1) == TDmode
6774                      || TYPE_MODE (ty2) == TDmode
6775                      || TYPE_MODE (type) == TDmode)
6776                    newtype = dfloat128_type_node; }
6777           (if ((newtype == dfloat32_type_node
6778                 || newtype == dfloat64_type_node
6779                 || newtype == dfloat128_type_node)
6780               && newtype == type
6781               && types_match (newtype, type))
6782             (op (convert:newtype @1) (convert:newtype @2))
6783             (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
6784                       newtype = ty1;
6785                     if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
6786                       newtype = ty2; }
6787                /* Sometimes this transformation is safe (cannot
6788                   change results through affecting double rounding
6789                   cases) and sometimes it is not.  If NEWTYPE is
6790                   wider than TYPE, e.g. (float)((long double)double
6791                   + (long double)double) converted to
6792                   (float)(double + double), the transformation is
6793                   unsafe regardless of the details of the types
6794                   involved; double rounding can arise if the result
6795                   of NEWTYPE arithmetic is a NEWTYPE value half way
6796                   between two representable TYPE values but the
6797                   exact value is sufficiently different (in the
6798                   right direction) for this difference to be
6799                   visible in ITYPE arithmetic.  If NEWTYPE is the
6800                   same as TYPE, however, the transformation may be
6801                   safe depending on the types involved: it is safe
6802                   if the ITYPE has strictly more than twice as many
6803                   mantissa bits as TYPE, can represent infinities
6804                   and NaNs if the TYPE can, and has sufficient
6805                   exponent range for the product or ratio of two
6806                   values representable in the TYPE to be within the
6807                   range of normal values of ITYPE.  */
6808               (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
6809                    && (flag_unsafe_math_optimizations
6810                        || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6811                            && real_can_shorten_arithmetic (TYPE_MODE (itype),
6812                                                            TYPE_MODE (type))
6813                            && !excess_precision_type (newtype)))
6814                    && !types_match (itype, newtype))
6815                  (convert:type (op (convert:newtype @1)
6816                                    (convert:newtype @2)))
6817          )))) )
6818    ))
6819 )))
6820
6821 /* This is another case of narrowing, specifically when there's an outer
6822    BIT_AND_EXPR which masks off bits outside the type of the innermost
6823    operands.   Like the previous case we have to convert the operands
6824    to unsigned types to avoid introducing undefined behavior for the
6825    arithmetic operation.  */
6826 (for op (minus plus)
6827  (simplify
6828   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6829   (if (INTEGRAL_TYPE_P (type)
6830        /* We check for type compatibility between @0 and @1 below,
6831           so there's no need to check that @1/@3 are integral types.  */
6832        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6833        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6834        /* The precision of the type of each operand must match the
6835           precision of the mode of each operand, similarly for the
6836           result.  */
6837        && type_has_mode_precision_p (TREE_TYPE (@0))
6838        && type_has_mode_precision_p (TREE_TYPE (@1))
6839        && type_has_mode_precision_p (type)
6840        /* The inner conversion must be a widening conversion.  */
6841        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6842        && types_match (@0, @1)
6843        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6844            <= TYPE_PRECISION (TREE_TYPE (@0)))
6845        && (wi::to_wide (@4)
6846            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6847                        true, TYPE_PRECISION (type))) == 0)
6848    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6849     (with { tree ntype = TREE_TYPE (@0); }
6850      (convert (bit_and (op @0 @1) (convert:ntype @4))))
6851     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6852      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6853                (convert:utype @4))))))))
6854
6855 /* Transform (@0 < @1 and @0 < @2) to use min,
6856    (@0 > @1 and @0 > @2) to use max */
6857 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6858      op    (lt      le      gt      ge      lt      le      gt      ge     )
6859      ext   (min     min     max     max     max     max     min     min    )
6860  (simplify
6861   (logic (op:cs @0 @1) (op:cs @0 @2))
6862   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6863        && TREE_CODE (@0) != INTEGER_CST)
6864    (op @0 (ext @1 @2)))))
6865
6866 (simplify
6867  /* signbit(x) -> 0 if x is nonnegative.  */
6868  (SIGNBIT tree_expr_nonnegative_p@0)
6869  { integer_zero_node; })
6870
6871 (simplify
6872  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
6873  (SIGNBIT @0)
6874  (if (!HONOR_SIGNED_ZEROS (@0))
6875   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6876
6877 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
6878 (for cmp (eq ne)
6879  (for op (plus minus)
6880       rop (minus plus)
6881   (simplify
6882    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6883    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6884         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6885         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6886         && !TYPE_SATURATING (TREE_TYPE (@0)))
6887     (with { tree res = int_const_binop (rop, @2, @1); }
6888      (if (TREE_OVERFLOW (res)
6889           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6890       { constant_boolean_node (cmp == NE_EXPR, type); }
6891       (if (single_use (@3))
6892        (cmp @0 { TREE_OVERFLOW (res)
6893                  ? drop_tree_overflow (res) : res; }))))))))
6894 (for cmp (lt le gt ge)
6895  (for op (plus minus)
6896       rop (minus plus)
6897   (simplify
6898    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6899    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6900         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6901     (with { tree res = int_const_binop (rop, @2, @1); }
6902      (if (TREE_OVERFLOW (res))
6903       {
6904         fold_overflow_warning (("assuming signed overflow does not occur "
6905                                 "when simplifying conditional to constant"),
6906                                WARN_STRICT_OVERFLOW_CONDITIONAL);
6907         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6908         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
6909         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6910                                   TYPE_SIGN (TREE_TYPE (@1)))
6911                         != (op == MINUS_EXPR);
6912         constant_boolean_node (less == ovf_high, type);
6913       }
6914       (if (single_use (@3))
6915        (with
6916         {
6917           fold_overflow_warning (("assuming signed overflow does not occur "
6918                                   "when changing X +- C1 cmp C2 to "
6919                                   "X cmp C2 -+ C1"),
6920                                  WARN_STRICT_OVERFLOW_COMPARISON);
6921         }
6922         (cmp @0 { res; })))))))))
6923
6924 /* Canonicalizations of BIT_FIELD_REFs.  */
6925
6926 (simplify
6927  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6928  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6929
6930 (simplify
6931  (BIT_FIELD_REF (view_convert @0) @1 @2)
6932  (BIT_FIELD_REF @0 @1 @2))
6933
6934 (simplify
6935  (BIT_FIELD_REF @0 @1 integer_zerop)
6936  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6937   (view_convert @0)))
6938
6939 (simplify
6940  (BIT_FIELD_REF @0 @1 @2)
6941  (switch
6942   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6943        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6944    (switch
6945     (if (integer_zerop (@2))
6946      (view_convert (realpart @0)))
6947     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6948      (view_convert (imagpart @0)))))
6949   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6950        && INTEGRAL_TYPE_P (type)
6951        /* On GIMPLE this should only apply to register arguments.  */
6952        && (! GIMPLE || is_gimple_reg (@0))
6953        /* A bit-field-ref that referenced the full argument can be stripped.  */
6954        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6955             && integer_zerop (@2))
6956            /* Low-parts can be reduced to integral conversions.
6957               ???  The following doesn't work for PDP endian.  */
6958            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6959                /* But only do this after vectorization.  */
6960                && canonicalize_math_after_vectorization_p ()
6961                /* Don't even think about BITS_BIG_ENDIAN.  */
6962                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6963                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6964                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6965                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
6966                                             - TYPE_PRECISION (type))
6967                                          : 0)) == 0)))
6968    (convert @0))))
6969
6970 /* Simplify vector extracts.  */
6971
6972 (simplify
6973  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6974  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6975       && tree_fits_uhwi_p (TYPE_SIZE (type))
6976       && ((tree_to_uhwi (TYPE_SIZE (type))
6977            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6978           || (VECTOR_TYPE_P (type)
6979               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
6980                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
6981   (with
6982    {
6983      tree ctor = (TREE_CODE (@0) == SSA_NAME
6984                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6985      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6986      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6987      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6988      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6989    }
6990    (if (n != 0
6991         && (idx % width) == 0
6992         && (n % width) == 0
6993         && known_le ((idx + n) / width,
6994                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6995     (with
6996      {
6997        idx = idx / width;
6998        n = n / width;
6999        /* Constructor elements can be subvectors.  */
7000        poly_uint64 k = 1;
7001        if (CONSTRUCTOR_NELTS (ctor) != 0)
7002          {
7003            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
7004            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
7005              k = TYPE_VECTOR_SUBPARTS (cons_elem);
7006          }
7007        unsigned HOST_WIDE_INT elt, count, const_k;
7008      }
7009      (switch
7010       /* We keep an exact subset of the constructor elements.  */
7011       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
7012        (if (CONSTRUCTOR_NELTS (ctor) == 0)
7013         { build_zero_cst (type); }
7014         (if (count == 1)
7015          (if (elt < CONSTRUCTOR_NELTS (ctor))
7016           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
7017           { build_zero_cst (type); })
7018          /* We don't want to emit new CTORs unless the old one goes away.
7019             ???  Eventually allow this if the CTOR ends up constant or
7020             uniform.  */
7021          (if (single_use (@0))
7022           (with
7023             {
7024               vec<constructor_elt, va_gc> *vals;
7025               vec_alloc (vals, count);
7026               bool constant_p = true;
7027               tree res;
7028               for (unsigned i = 0;
7029                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
7030                 {
7031                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
7032                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
7033                   if (!CONSTANT_CLASS_P (e))
7034                     constant_p = false;
7035                 }
7036               tree evtype = (types_match (TREE_TYPE (type),
7037                                           TREE_TYPE (TREE_TYPE (ctor)))
7038                              ? type
7039                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
7040                                                   count * k));
7041               res = (constant_p ? build_vector_from_ctor (evtype, vals)
7042                      : build_constructor (evtype, vals));
7043             }
7044             (view_convert { res; }))))))
7045       /* The bitfield references a single constructor element.  */
7046       (if (k.is_constant (&const_k)
7047            && idx + n <= (idx / const_k + 1) * const_k)
7048        (switch
7049         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
7050          { build_zero_cst (type); })
7051         (if (n == const_k)
7052          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
7053         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
7054                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
7055
7056 /* Simplify a bit extraction from a bit insertion for the cases with
7057    the inserted element fully covering the extraction or the insertion
7058    not touching the extraction.  */
7059 (simplify
7060  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
7061  (with
7062   {
7063     unsigned HOST_WIDE_INT isize;
7064     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7065       isize = TYPE_PRECISION (TREE_TYPE (@1));
7066     else
7067       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
7068   }
7069   (switch
7070    (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7071         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
7072                       wi::to_wide (@ipos) + isize))
7073     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
7074                                                  wi::to_wide (@rpos)
7075                                                  - wi::to_wide (@ipos)); }))
7076    (if (wi::geu_p (wi::to_wide (@ipos),
7077                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
7078         || wi::geu_p (wi::to_wide (@rpos),
7079                       wi::to_wide (@ipos) + isize))
7080     (BIT_FIELD_REF @0 @rsize @rpos)))))
7081
7082 (if (canonicalize_math_after_vectorization_p ())
7083  (for fmas (FMA)
7084   (simplify
7085    (fmas:c (negate @0) @1 @2)
7086    (IFN_FNMA @0 @1 @2))
7087   (simplify
7088    (fmas @0 @1 (negate @2))
7089    (IFN_FMS @0 @1 @2))
7090   (simplify
7091    (fmas:c (negate @0) @1 (negate @2))
7092    (IFN_FNMS @0 @1 @2))
7093   (simplify
7094    (negate (fmas@3 @0 @1 @2))
7095    (if (single_use (@3))
7096     (IFN_FNMS @0 @1 @2))))
7097
7098  (simplify
7099   (IFN_FMS:c (negate @0) @1 @2)
7100   (IFN_FNMS @0 @1 @2))
7101  (simplify
7102   (IFN_FMS @0 @1 (negate @2))
7103   (IFN_FMA @0 @1 @2))
7104  (simplify
7105   (IFN_FMS:c (negate @0) @1 (negate @2))
7106   (IFN_FNMA @0 @1 @2))
7107  (simplify
7108   (negate (IFN_FMS@3 @0 @1 @2))
7109    (if (single_use (@3))
7110     (IFN_FNMA @0 @1 @2)))
7111
7112  (simplify
7113   (IFN_FNMA:c (negate @0) @1 @2)
7114   (IFN_FMA @0 @1 @2))
7115  (simplify
7116   (IFN_FNMA @0 @1 (negate @2))
7117   (IFN_FNMS @0 @1 @2))
7118  (simplify
7119   (IFN_FNMA:c (negate @0) @1 (negate @2))
7120   (IFN_FMS @0 @1 @2))
7121  (simplify
7122   (negate (IFN_FNMA@3 @0 @1 @2))
7123   (if (single_use (@3))
7124    (IFN_FMS @0 @1 @2)))
7125
7126  (simplify
7127   (IFN_FNMS:c (negate @0) @1 @2)
7128   (IFN_FMS @0 @1 @2))
7129  (simplify
7130   (IFN_FNMS @0 @1 (negate @2))
7131   (IFN_FNMA @0 @1 @2))
7132  (simplify
7133   (IFN_FNMS:c (negate @0) @1 (negate @2))
7134   (IFN_FMA @0 @1 @2))
7135  (simplify
7136   (negate (IFN_FNMS@3 @0 @1 @2))
7137   (if (single_use (@3))
7138    (IFN_FMA @0 @1 @2))))
7139
7140 /* CLZ simplifications.  */
7141 (for clz (CLZ)
7142  (for op (eq ne)
7143       cmp (lt ge)
7144   (simplify
7145    (op (clz:s@2 @0) INTEGER_CST@1)
7146    (if (integer_zerop (@1) && single_use (@2))
7147     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
7148     (with { tree type0 = TREE_TYPE (@0);
7149             tree stype = signed_type_for (type0);
7150             HOST_WIDE_INT val = 0;
7151             /* Punt on hypothetical weird targets.  */
7152             if (clz == CFN_CLZ
7153                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7154                                               val) == 2
7155                 && val == 0)
7156               stype = NULL_TREE;
7157           }
7158      (if (stype)
7159       (cmp (convert:stype @0) { build_zero_cst (stype); })))
7160     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
7161     (with { bool ok = true;
7162             HOST_WIDE_INT val = 0;
7163             tree type0 = TREE_TYPE (@0);
7164             /* Punt on hypothetical weird targets.  */
7165             if (clz == CFN_CLZ
7166                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7167                                               val) == 2
7168                 && val == TYPE_PRECISION (type0) - 1)
7169               ok = false;
7170           }
7171      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
7172       (op @0 { build_one_cst (type0); })))))))
7173
7174 /* CTZ simplifications.  */
7175 (for ctz (CTZ)
7176  (for op (ge gt le lt)
7177       cmp (eq eq ne ne)
7178   (simplify
7179    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
7180    (op (ctz:s @0) INTEGER_CST@1)
7181     (with { bool ok = true;
7182             HOST_WIDE_INT val = 0;
7183             if (!tree_fits_shwi_p (@1))
7184               ok = false;
7185             else
7186               {
7187                 val = tree_to_shwi (@1);
7188                 /* Canonicalize to >= or <.  */
7189                 if (op == GT_EXPR || op == LE_EXPR)
7190                   {
7191                     if (val == HOST_WIDE_INT_MAX)
7192                       ok = false;
7193                     else
7194                       val++;
7195                   }
7196               }
7197             bool zero_res = false;
7198             HOST_WIDE_INT zero_val = 0;
7199             tree type0 = TREE_TYPE (@0);
7200             int prec = TYPE_PRECISION (type0);
7201             if (ctz == CFN_CTZ
7202                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7203                                               zero_val) == 2)
7204               zero_res = true;
7205           }
7206      (if (val <= 0)
7207       (if (ok && (!zero_res || zero_val >= val))
7208        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7209       (if (val >= prec)
7210        (if (ok && (!zero_res || zero_val < val))
7211         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7212        (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7213         (cmp (bit_and @0 { wide_int_to_tree (type0,
7214                                              wi::mask (val, false, prec)); })
7215              { build_zero_cst (type0); })))))))
7216  (for op (eq ne)
7217   (simplify
7218    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
7219    (op (ctz:s @0) INTEGER_CST@1)
7220     (with { bool zero_res = false;
7221             HOST_WIDE_INT zero_val = 0;
7222             tree type0 = TREE_TYPE (@0);
7223             int prec = TYPE_PRECISION (type0);
7224             if (ctz == CFN_CTZ
7225                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7226                                               zero_val) == 2)
7227               zero_res = true;
7228           }
7229      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7230       (if (!zero_res || zero_val != wi::to_widest (@1))
7231        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7232       (if (!zero_res || zero_val < 0 || zero_val >= prec)
7233        (op (bit_and @0 { wide_int_to_tree (type0,
7234                                            wi::mask (tree_to_uhwi (@1) + 1,
7235                                                      false, prec)); })
7236            { wide_int_to_tree (type0,
7237                                wi::shifted_mask (tree_to_uhwi (@1), 1,
7238                                                  false, prec)); })))))))
7239
7240 /* POPCOUNT simplifications.  */
7241 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
7242 (simplify
7243   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7244   (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7245     (POPCOUNT (bit_ior @0 @1))))
7246
7247 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
7248 (for popcount (POPCOUNT)
7249   (for cmp (le eq ne gt)
7250        rep (eq eq ne ne)
7251     (simplify
7252       (cmp (popcount @0) integer_zerop)
7253       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7254
7255 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
7256 (simplify
7257   (bit_and (POPCOUNT @0) integer_onep)
7258   (PARITY @0))
7259
7260 /* PARITY simplifications.  */
7261 /* parity(~X) is parity(X).  */
7262 (simplify
7263   (PARITY (bit_not @0))
7264   (PARITY @0))
7265
7266 /* parity(X)^parity(Y) is parity(X^Y).  */
7267 (simplify
7268   (bit_xor (PARITY:s @0) (PARITY:s @1))
7269   (PARITY (bit_xor @0 @1)))
7270
7271 /* Common POPCOUNT/PARITY simplifications.  */
7272 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
7273 (for pfun (POPCOUNT PARITY)
7274   (simplify
7275     (pfun @0)
7276     (with { wide_int nz = tree_nonzero_bits (@0); }
7277       (switch
7278         (if (nz == 1)
7279           (convert @0))
7280         (if (wi::popcount (nz) == 1)
7281           (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7282             (convert (rshift:utype (convert:utype @0)
7283                                    { build_int_cst (integer_type_node,
7284                                                     wi::ctz (nz)); }))))))))
7285
7286 #if GIMPLE
7287 /* 64- and 32-bits branchless implementations of popcount are detected:
7288
7289    int popcount64c (uint64_t x)
7290    {
7291      x -= (x >> 1) & 0x5555555555555555ULL;
7292      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
7293      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
7294      return (x * 0x0101010101010101ULL) >> 56;
7295    }
7296
7297    int popcount32c (uint32_t x)
7298    {
7299      x -= (x >> 1) & 0x55555555;
7300      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
7301      x = (x + (x >> 4)) & 0x0f0f0f0f;
7302      return (x * 0x01010101) >> 24;
7303    }  */
7304 (simplify
7305  (rshift
7306   (mult
7307    (bit_and
7308     (plus:c
7309      (rshift @8 INTEGER_CST@5)
7310       (plus:c@8
7311        (bit_and @6 INTEGER_CST@7)
7312         (bit_and
7313          (rshift
7314           (minus@6 @0
7315            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
7316           INTEGER_CST@10)
7317          INTEGER_CST@9)))
7318     INTEGER_CST@3)
7319    INTEGER_CST@2)
7320   INTEGER_CST@1)
7321   /* Check constants and optab.  */
7322   (with { unsigned prec = TYPE_PRECISION (type);
7323           int shift = (64 - prec) & 63;
7324           unsigned HOST_WIDE_INT c1
7325             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
7326           unsigned HOST_WIDE_INT c2
7327             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
7328           unsigned HOST_WIDE_INT c3
7329             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
7330           unsigned HOST_WIDE_INT c4
7331             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
7332    }
7333    (if (prec >= 16
7334         && prec <= 64
7335         && pow2p_hwi (prec)
7336         && TYPE_UNSIGNED (type)
7337         && integer_onep (@4)
7338         && wi::to_widest (@10) == 2
7339         && wi::to_widest (@5) == 4
7340         && wi::to_widest (@1) == prec - 8
7341         && tree_to_uhwi (@2) == c1
7342         && tree_to_uhwi (@3) == c2
7343         && tree_to_uhwi (@9) == c3
7344         && tree_to_uhwi (@7) == c3
7345         && tree_to_uhwi (@11) == c4)
7346     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
7347                                          OPTIMIZE_FOR_BOTH))
7348      (convert (IFN_POPCOUNT:type @0))
7349      /* Try to do popcount in two halves.  PREC must be at least
7350         five bits for this to work without extension before adding.  */
7351      (with {
7352        tree half_type = NULL_TREE;
7353        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
7354        int half_prec = 8;
7355        if (m.exists ()
7356            && m.require () != TYPE_MODE (type))
7357          {
7358            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
7359            half_type = build_nonstandard_integer_type (half_prec, 1);
7360          }
7361        gcc_assert (half_prec > 2);
7362       }
7363       (if (half_type != NULL_TREE
7364            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
7365                                               OPTIMIZE_FOR_BOTH))
7366        (convert (plus
7367          (IFN_POPCOUNT:half_type (convert @0))
7368          (IFN_POPCOUNT:half_type (convert (rshift @0
7369             { build_int_cst (integer_type_node, half_prec); } )))))))))))
7370
7371 /* __builtin_ffs needs to deal on many targets with the possible zero
7372    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
7373    should lead to better code.  */
7374 (simplify
7375  (FFS tree_expr_nonzero_p@0)
7376  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7377       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
7378                                          OPTIMIZE_FOR_SPEED))
7379   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7380    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
7381 #endif
7382
7383 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
7384           BUILT_IN_FFSIMAX)
7385  /* __builtin_ffs (X) == 0 -> X == 0.
7386     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
7387  (for cmp (eq ne)
7388   (simplify
7389    (cmp (ffs@2 @0) INTEGER_CST@1)
7390     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7391      (switch
7392       (if (integer_zerop (@1))
7393        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
7394       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
7395        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
7396       (if (single_use (@2))
7397        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
7398                                             wi::mask (tree_to_uhwi (@1),
7399                                                       false, prec)); })
7400             { wide_int_to_tree (TREE_TYPE (@0),
7401                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
7402                                                   false, prec)); }))))))
7403
7404  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
7405  (for cmp (gt le)
7406       cmp2 (ne eq)
7407       cmp3 (eq ne)
7408       bit_op (bit_and bit_ior)
7409   (simplify
7410    (cmp (ffs@2 @0) INTEGER_CST@1)
7411     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7412      (switch
7413       (if (integer_zerop (@1))
7414        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7415       (if (tree_int_cst_sgn (@1) < 0)
7416        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7417       (if (wi::to_widest (@1) >= prec)
7418        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7419       (if (wi::to_widest (@1) == prec - 1)
7420        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7421                                     wi::shifted_mask (prec - 1, 1,
7422                                                       false, prec)); }))
7423       (if (single_use (@2))
7424        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7425                (cmp3 (bit_and @0
7426                               { wide_int_to_tree (TREE_TYPE (@0),
7427                                                   wi::mask (tree_to_uhwi (@1),
7428                                                   false, prec)); })
7429                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
7430
7431 #if GIMPLE
7432
7433 /* Simplify:
7434      a = op a1
7435      r = cond ? a : b
7436      --> r = .COND_FN (cond, a, b)
7437 and,
7438     a = op a1
7439     r = cond ? b : a
7440     --> r = .COND_FN (~cond, b, a).  */
7441
7442 (for uncond_op (UNCOND_UNARY)
7443      cond_op (COND_UNARY)
7444  (simplify
7445   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
7446    (with { tree op_type = TREE_TYPE (@3); }
7447     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7448         && is_truth_type_for (op_type, TREE_TYPE (@0)))
7449      (cond_op @0 @1 @2))))
7450  (simplify
7451   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
7452    (with { tree op_type = TREE_TYPE (@3); }
7453     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7454         && is_truth_type_for (op_type, TREE_TYPE (@0)))
7455      (cond_op (bit_not @0) @2 @1)))))
7456
7457 /* Simplify:
7458
7459      a = a1 op a2
7460      r = c ? a : b;
7461
7462    to:
7463
7464      r = c ? a1 op a2 : b;
7465
7466    if the target can do it in one go.  This makes the operation conditional
7467    on c, so could drop potentially-trapping arithmetic, but that's a valid
7468    simplification if the result of the operation isn't needed.
7469
7470    Avoid speculatively generating a stand-alone vector comparison
7471    on targets that might not support them.  Any target implementing
7472    conditional internal functions must support the same comparisons
7473    inside and outside a VEC_COND_EXPR.  */
7474
7475 (for uncond_op (UNCOND_BINARY)
7476      cond_op (COND_BINARY)
7477  (simplify
7478   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7479   (with { tree op_type = TREE_TYPE (@4); }
7480    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7481         && is_truth_type_for (op_type, TREE_TYPE (@0))
7482         && single_use (@4))
7483     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7484  (simplify
7485   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7486   (with { tree op_type = TREE_TYPE (@4); }
7487    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7488         && is_truth_type_for (op_type, TREE_TYPE (@0))
7489         && single_use (@4))
7490     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7491
7492 /* Same for ternary operations.  */
7493 (for uncond_op (UNCOND_TERNARY)
7494      cond_op (COND_TERNARY)
7495  (simplify
7496   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7497   (with { tree op_type = TREE_TYPE (@5); }
7498    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7499         && is_truth_type_for (op_type, TREE_TYPE (@0))
7500         && single_use (@5))
7501     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7502  (simplify
7503   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7504   (with { tree op_type = TREE_TYPE (@5); }
7505    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7506         && is_truth_type_for (op_type, TREE_TYPE (@0))
7507         && single_use (@5))
7508     (view_convert (cond_op (bit_not @0) @2 @3 @4
7509                   (view_convert:op_type @1)))))))
7510 #endif
7511
7512 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7513    "else" value of an IFN_COND_*.  */
7514 (for cond_op (COND_BINARY)
7515  (simplify
7516   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7517   (with { tree op_type = TREE_TYPE (@3); }
7518    (if (element_precision (type) == element_precision (op_type))
7519     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7520  (simplify
7521   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7522   (with { tree op_type = TREE_TYPE (@5); }
7523    (if (inverse_conditions_p (@0, @2)
7524         && element_precision (type) == element_precision (op_type))
7525     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7526
7527 /* Same for ternary operations.  */
7528 (for cond_op (COND_TERNARY)
7529  (simplify
7530   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7531   (with { tree op_type = TREE_TYPE (@4); }
7532    (if (element_precision (type) == element_precision (op_type))
7533     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7534  (simplify
7535   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7536   (with { tree op_type = TREE_TYPE (@6); }
7537    (if (inverse_conditions_p (@0, @2)
7538         && element_precision (type) == element_precision (op_type))
7539     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7540
7541 /* Detect simplication for a conditional reduction where
7542
7543    a = mask1 ? b : 0
7544    c = mask2 ? d + a : d
7545
7546    is turned into
7547
7548    c = mask1 && mask2 ? d + b : d.  */
7549 (simplify
7550   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
7551    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
7552
7553 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7554    expressions like:
7555
7556    A: (@0 + @1 < @2) | (@2 + @1 < @0)
7557    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7558
7559    If pointers are known not to wrap, B checks whether @1 bytes starting
7560    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7561    bytes.  A is more efficiently tested as:
7562
7563    A: (sizetype) (@0 + @1 - @2) > @1 * 2
7564
7565    The equivalent expression for B is given by replacing @1 with @1 - 1:
7566
7567    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7568
7569    @0 and @2 can be swapped in both expressions without changing the result.
7570
7571    The folds rely on sizetype's being unsigned (which is always true)
7572    and on its being the same width as the pointer (which we have to check).
7573
7574    The fold replaces two pointer_plus expressions, two comparisons and
7575    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7576    the best case it's a saving of two operations.  The A fold retains one
7577    of the original pointer_pluses, so is a win even if both pointer_pluses
7578    are used elsewhere.  The B fold is a wash if both pointer_pluses are
7579    used elsewhere, since all we end up doing is replacing a comparison with
7580    a pointer_plus.  We do still apply the fold under those circumstances
7581    though, in case applying it to other conditions eventually makes one of the
7582    pointer_pluses dead.  */
7583 (for ior (truth_orif truth_or bit_ior)
7584  (for cmp (le lt)
7585   (simplify
7586    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7587         (cmp:cs (pointer_plus@4 @2 @1) @0))
7588    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7589         && TYPE_OVERFLOW_WRAPS (sizetype)
7590         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7591     /* Calculate the rhs constant.  */
7592     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7593             offset_int rhs = off * 2; }
7594      /* Always fails for negative values.  */
7595      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7596       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7597          pick a canonical order.  This increases the chances of using the
7598          same pointer_plus in multiple checks.  */
7599       (with { bool swap_p = tree_swap_operands_p (@0, @2);
7600               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7601        (if (cmp == LT_EXPR)
7602         (gt (convert:sizetype
7603              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7604                                      { swap_p ? @0 : @2; }))
7605             { rhs_tree; })
7606         (gt (convert:sizetype
7607              (pointer_diff:ssizetype
7608               (pointer_plus { swap_p ? @2 : @0; }
7609                             { wide_int_to_tree (sizetype, off); })
7610               { swap_p ? @0 : @2; }))
7611             { rhs_tree; })))))))))
7612
7613 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7614    element of @1.  */
7615 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7616  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7617   (with { int i = single_nonzero_element (@1); }
7618    (if (i >= 0)
7619     (with { tree elt = vector_cst_elt (@1, i);
7620             tree elt_type = TREE_TYPE (elt);
7621             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7622             tree size = bitsize_int (elt_bits);
7623             tree pos = bitsize_int (elt_bits * i); }
7624      (view_convert
7625       (bit_and:elt_type
7626        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7627        { elt; })))))))
7628
7629 /* Fold reduction of a single nonzero element constructor.  */
7630 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7631   (simplify (reduc (CONSTRUCTOR@0))
7632     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
7633                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7634             tree elt = ctor_single_nonzero_element (ctor); }
7635       (if (elt
7636            && !HONOR_SNANS (type)
7637            && !HONOR_SIGNED_ZEROS (type))
7638         { elt; }))))
7639
7640 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
7641 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
7642             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
7643      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
7644   (simplify (reduc (op @0 VECTOR_CST@1))
7645     (op (reduc:type @0) (reduc:type @1))))
7646
7647 (simplify
7648  (vec_perm @0 @1 VECTOR_CST@2)
7649  (with
7650   {
7651     tree op0 = @0, op1 = @1, op2 = @2;
7652     machine_mode result_mode = TYPE_MODE (type);
7653     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
7654
7655     /* Build a vector of integers from the tree mask.  */
7656     vec_perm_builder builder;
7657     if (!tree_to_vec_perm_builder (&builder, op2))
7658       return NULL_TREE;
7659
7660     /* Create a vec_perm_indices for the integer vector.  */
7661     poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7662     bool single_arg = (op0 == op1);
7663     vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
7664   }
7665   (if (sel.series_p (0, 1, 0, 1))
7666    { op0; }
7667    (if (sel.series_p (0, 1, nelts, 1))
7668     { op1; }
7669     (with
7670      {
7671        if (!single_arg)
7672          {
7673            if (sel.all_from_input_p (0))
7674              op1 = op0;
7675            else if (sel.all_from_input_p (1))
7676              {
7677                op0 = op1;
7678                sel.rotate_inputs (1);
7679              }
7680            else if (known_ge (poly_uint64 (sel[0]), nelts))
7681              {
7682                std::swap (op0, op1);
7683                sel.rotate_inputs (1);
7684              }
7685          }
7686        gassign *def;
7687        tree cop0 = op0, cop1 = op1;
7688        if (TREE_CODE (op0) == SSA_NAME
7689            && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
7690            && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7691          cop0 = gimple_assign_rhs1 (def);
7692        if (TREE_CODE (op1) == SSA_NAME
7693            && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
7694            && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7695          cop1 = gimple_assign_rhs1 (def);
7696
7697        tree t;
7698     }
7699     (if ((TREE_CODE (cop0) == VECTOR_CST
7700           || TREE_CODE (cop0) == CONSTRUCTOR)
7701          && (TREE_CODE (cop1) == VECTOR_CST
7702              || TREE_CODE (cop1) == CONSTRUCTOR)
7703          && (t = fold_vec_perm (type, cop0, cop1, sel)))
7704      { t; }
7705      (with
7706       {
7707         bool changed = (op0 == op1 && !single_arg);
7708         tree ins = NULL_TREE;
7709         unsigned at = 0;
7710
7711         /* See if the permutation is performing a single element
7712            insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
7713            in that case.  But only if the vector mode is supported,
7714            otherwise this is invalid GIMPLE.  */
7715         if (op_mode != BLKmode
7716             && (TREE_CODE (cop0) == VECTOR_CST
7717                 || TREE_CODE (cop0) == CONSTRUCTOR
7718                 || TREE_CODE (cop1) == VECTOR_CST
7719                 || TREE_CODE (cop1) == CONSTRUCTOR))
7720           {
7721             bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
7722             if (insert_first_p)
7723               {
7724                 /* After canonicalizing the first elt to come from the
7725                    first vector we only can insert the first elt from
7726                    the first vector.  */
7727                 at = 0;
7728                 if ((ins = fold_read_from_vector (cop0, sel[0])))
7729                   op0 = op1;
7730               }
7731             /* The above can fail for two-element vectors which always
7732                appear to insert the first element, so try inserting
7733                into the second lane as well.  For more than two
7734                elements that's wasted time.  */
7735             if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
7736               {
7737                 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
7738                 for (at = 0; at < encoded_nelts; ++at)
7739                   if (maybe_ne (sel[at], at))
7740                     break;
7741                 if (at < encoded_nelts
7742                     && (known_eq (at + 1, nelts)
7743                         || sel.series_p (at + 1, 1, at + 1, 1)))
7744                   {
7745                     if (known_lt (poly_uint64 (sel[at]), nelts))
7746                       ins = fold_read_from_vector (cop0, sel[at]);
7747                     else
7748                       ins = fold_read_from_vector (cop1, sel[at] - nelts);
7749                   }
7750               }
7751           }
7752
7753         /* Generate a canonical form of the selector.  */
7754         if (!ins && sel.encoding () != builder)
7755           {
7756             /* Some targets are deficient and fail to expand a single
7757                argument permutation while still allowing an equivalent
7758                2-argument version.  */
7759             tree oldop2 = op2;
7760             if (sel.ninputs () == 2
7761                || can_vec_perm_const_p (result_mode, op_mode, sel, false))
7762               op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7763             else
7764               {
7765                 vec_perm_indices sel2 (builder, 2, nelts);
7766                 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
7767                   op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
7768                 else
7769                   /* Not directly supported with either encoding,
7770                      so use the preferred form.  */
7771                   op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7772               }
7773             if (!operand_equal_p (op2, oldop2, 0))
7774               changed = true;
7775           }
7776       }
7777       (if (ins)
7778        (bit_insert { op0; } { ins; }
7779          { bitsize_int (at * vector_element_bits (type)); })
7780        (if (changed)
7781         (vec_perm { op0; } { op1; } { op2; }))))))))))
7782
7783 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
7784
7785 (match vec_same_elem_p
7786  (vec_duplicate @0))
7787
7788 (match vec_same_elem_p
7789  CONSTRUCTOR@0
7790  (if (TREE_CODE (@0) == SSA_NAME
7791       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
7792
7793 (match vec_same_elem_p
7794  @0
7795  (if (uniform_vector_p (@0))))
7796
7797
7798 (simplify
7799  (vec_perm vec_same_elem_p@0 @0 @1)
7800  @0)
7801
7802 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
7803 (simplify
7804  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
7805  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
7806   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
7807 (simplify
7808  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
7809  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
7810   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
7811
7812
7813 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
7814    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
7815    constant which when multiplied by a power of 2 contains a unique value
7816    in the top 5 or 6 bits.  This is then indexed into a table which maps it
7817    to the number of trailing zeroes.  */
7818 (match (ctz_table_index @1 @2 @3)
7819   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
7820
7821 (match (cond_expr_convert_p @0 @2 @3 @6)
7822  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
7823   (if (INTEGRAL_TYPE_P (type)
7824        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7825        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7826        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7827        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
7828        && TYPE_PRECISION (TREE_TYPE (@0))
7829           == TYPE_PRECISION (TREE_TYPE (@2))
7830        && TYPE_PRECISION (TREE_TYPE (@0))
7831           == TYPE_PRECISION (TREE_TYPE (@3))
7832        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
7833           signess when convert is truncation, but not ok for extension since
7834           it's sign_extend vs zero_extend.  */
7835        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
7836            || (TYPE_UNSIGNED (TREE_TYPE (@2))
7837                == TYPE_UNSIGNED (TREE_TYPE (@3))))
7838        && single_use (@4)
7839        && single_use (@5))))
7840
7841 (for bit_op (bit_and bit_ior bit_xor)
7842  (match (bitwise_induction_p @0 @2 @3)
7843   (bit_op:c
7844    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
7845    @3)))
7846
7847 (match (bitwise_induction_p @0 @2 @3)
7848  (bit_not
7849   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))