]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/match.pd
Lower sampling rate for autofdo bootstrap
[thirdparty/gcc.git] / gcc / match.pd
CommitLineData
3d2cf79f
RB
1/* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
4
a5544970 5 Copyright (C) 2014-2019 Free Software Foundation, Inc.
3d2cf79f
RB
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8
9This file is part of GCC.
10
11GCC is free software; you can redistribute it and/or modify it under
12the terms of the GNU General Public License as published by the Free
13Software Foundation; either version 3, or (at your option) any later
14version.
15
16GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17WARRANTY; without even the implied warranty of MERCHANTABILITY or
18FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19for more details.
20
21You should have received a copy of the GNU General Public License
22along 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
cc7b5acf 28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
53a19317 29 integer_each_onep integer_truep integer_nonzerop
cc7b5acf 30 real_zerop real_onep real_minus_onep
b0eb889b 31 zerop
46c66a46 32 initializer_each_zero_or_onep
f3582e54 33 CONSTANT_CLASS_P
887ab609 34 tree_expr_nonnegative_p
e36c1cfe 35 tree_expr_nonzero_p
67dbe582 36 integer_valued_real_p
53a19317 37 integer_pow2p
f06e47d7 38 uniform_integer_cst_p
53a19317 39 HONOR_NANS)
e0ee10ed 40
f84e7fd6
RB
41/* Operator lists. */
42(define_operator_list tcc_comparison
43 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
44(define_operator_list inverted_tcc_comparison
45 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
46(define_operator_list inverted_tcc_comparison_with_nans
47 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
534bd33b
MG
48(define_operator_list swapped_tcc_comparison
49 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
07cdc2b8
RB
50(define_operator_list simple_comparison lt le eq ne ge gt)
51(define_operator_list swapped_simple_comparison gt ge eq ne le lt)
52
b1dc4a20 53#include "cfn-operators.pd"
257aecb4 54
543a9bcd
RS
55/* Define operand lists for math rounding functions {,i,l,ll}FN,
56 where the versions prefixed with "i" return an int, those prefixed with
57 "l" return a long and those prefixed with "ll" return a long long.
58
59 Also define operand lists:
60
61 X<FN>F for all float functions, in the order i, l, ll
62 X<FN> for all double functions, in the same order
63 X<FN>L for all long double functions, in the same order. */
64#define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
543a9bcd
RS
65 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
66 BUILT_IN_L##FN##F \
67 BUILT_IN_LL##FN##F) \
68 (define_operator_list X##FN BUILT_IN_I##FN \
69 BUILT_IN_L##FN \
70 BUILT_IN_LL##FN) \
71 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
72 BUILT_IN_L##FN##L \
73 BUILT_IN_LL##FN##L)
74
543a9bcd
RS
75DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
76DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
77DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
78DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
0d2b3bca
RS
79
80/* Binary operations and their associated IFN_COND_* function. */
81(define_operator_list UNCOND_BINARY
82 plus minus
6c4fd4a9 83 mult trunc_div trunc_mod rdiv
0d2b3bca
RS
84 min max
85 bit_and bit_ior bit_xor)
86(define_operator_list COND_BINARY
87 IFN_COND_ADD IFN_COND_SUB
6c4fd4a9 88 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
0d2b3bca
RS
89 IFN_COND_MIN IFN_COND_MAX
90 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
b41d1f6e
RS
91
92/* Same for ternary operations. */
93(define_operator_list UNCOND_TERNARY
94 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
95(define_operator_list COND_TERNARY
96 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
03cc70b5 97
ed73f46f
MG
98/* As opposed to convert?, this still creates a single pattern, so
99 it is not a suitable replacement for convert? in all cases. */
100(match (nop_convert @0)
101 (convert @0)
102 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
103(match (nop_convert @0)
104 (view_convert @0)
105 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
928686b1
RS
106 && known_eq (TYPE_VECTOR_SUBPARTS (type),
107 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
ed73f46f
MG
108 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
109/* This one has to be last, or it shadows the others. */
110(match (nop_convert @0)
03cc70b5 111 @0)
f84e7fd6 112
e197e64e
KV
113/* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
114 ABSU_EXPR returns unsigned absolute value of the operand and the operand
115 of the ABSU_EXPR will have the corresponding signed type. */
116(simplify (abs (convert @0))
117 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
118 && !TYPE_UNSIGNED (TREE_TYPE (@0))
119 && element_precision (type) > element_precision (TREE_TYPE (@0)))
120 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
121 (convert (absu:utype @0)))))
122
123
e0ee10ed 124/* Simplifications of operations with one constant operand and
36a60e48 125 simplifications to constants or single values. */
e0ee10ed
RB
126
127(for op (plus pointer_plus minus bit_ior bit_xor)
128 (simplify
129 (op @0 integer_zerop)
130 (non_lvalue @0)))
131
a499aac5
RB
132/* 0 +p index -> (type)index */
133(simplify
134 (pointer_plus integer_zerop @1)
135 (non_lvalue (convert @1)))
136
d43177ad
MG
137/* ptr - 0 -> (type)ptr */
138(simplify
139 (pointer_diff @0 integer_zerop)
140 (convert @0))
141
a7f24614
RB
142/* See if ARG1 is zero and X + ARG1 reduces to X.
143 Likewise if the operands are reversed. */
144(simplify
145 (plus:c @0 real_zerop@1)
146 (if (fold_real_zero_addition_p (type, @1, 0))
147 (non_lvalue @0)))
148
149/* See if ARG1 is zero and X - ARG1 reduces to X. */
150(simplify
151 (minus @0 real_zerop@1)
152 (if (fold_real_zero_addition_p (type, @1, 1))
153 (non_lvalue @0)))
154
e0ee10ed
RB
155/* Simplify x - x.
156 This is unsafe for certain floats even in non-IEEE formats.
157 In IEEE, it is unsafe because it does wrong for NaNs.
158 Also note that operand_equal_p is always false if an operand
159 is volatile. */
160(simplify
a7f24614 161 (minus @0 @0)
1b457aa4 162 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
a7f24614 163 { build_zero_cst (type); }))
1af4ebf5
MG
164(simplify
165 (pointer_diff @@0 @0)
166 { build_zero_cst (type); })
e0ee10ed
RB
167
168(simplify
a7f24614
RB
169 (mult @0 integer_zerop@1)
170 @1)
171
172/* Maybe fold x * 0 to 0. The expressions aren't the same
173 when x is NaN, since x * 0 is also NaN. Nor are they the
174 same in modes with signed zeros, since multiplying a
175 negative value by 0 gives -0, not +0. */
176(simplify
177 (mult @0 real_zerop@1)
8b5ee871 178 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
a7f24614
RB
179 @1))
180
181/* In IEEE floating point, x*1 is not equivalent to x for snans.
182 Likewise for complex arithmetic with signed zeros. */
183(simplify
184 (mult @0 real_onep)
8b5ee871
MG
185 (if (!HONOR_SNANS (type)
186 && (!HONOR_SIGNED_ZEROS (type)
a7f24614
RB
187 || !COMPLEX_FLOAT_TYPE_P (type)))
188 (non_lvalue @0)))
189
190/* Transform x * -1.0 into -x. */
191(simplify
192 (mult @0 real_minus_onep)
8b5ee871
MG
193 (if (!HONOR_SNANS (type)
194 && (!HONOR_SIGNED_ZEROS (type)
a7f24614
RB
195 || !COMPLEX_FLOAT_TYPE_P (type)))
196 (negate @0)))
e0ee10ed 197
46c66a46
RS
198/* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
199 unless the target has native support for the former but not the latter. */
200(simplify
201 (mult @0 VECTOR_CST@1)
202 (if (initializer_each_zero_or_onep (@1)
203 && !HONOR_SNANS (type)
204 && !HONOR_SIGNED_ZEROS (type))
205 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
206 (if (itype
207 && (!VECTOR_MODE_P (TYPE_MODE (type))
208 || (VECTOR_MODE_P (TYPE_MODE (itype))
209 && optab_handler (and_optab,
210 TYPE_MODE (itype)) != CODE_FOR_nothing)))
211 (view_convert (bit_and:itype (view_convert @0)
212 (ne @1 { build_zero_cst (type); })))))))
213
8c2805bb
AP
214(for cmp (gt ge lt le)
215 outp (convert convert negate negate)
216 outn (negate negate convert convert)
217 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
218 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
219 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
220 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
221 (simplify
222 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
223 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
224 && types_match (type, TREE_TYPE (@0)))
225 (switch
226 (if (types_match (type, float_type_node))
227 (BUILT_IN_COPYSIGNF @1 (outp @0)))
228 (if (types_match (type, double_type_node))
229 (BUILT_IN_COPYSIGN @1 (outp @0)))
230 (if (types_match (type, long_double_type_node))
231 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
232 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
233 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
234 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
235 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
236 (simplify
237 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
238 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
239 && types_match (type, TREE_TYPE (@0)))
240 (switch
241 (if (types_match (type, float_type_node))
242 (BUILT_IN_COPYSIGNF @1 (outn @0)))
243 (if (types_match (type, double_type_node))
244 (BUILT_IN_COPYSIGN @1 (outn @0)))
245 (if (types_match (type, long_double_type_node))
246 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
247
248/* Transform X * copysign (1.0, X) into abs(X). */
249(simplify
c6cfa2bf 250 (mult:c @0 (COPYSIGN_ALL real_onep @0))
8c2805bb
AP
251 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
252 (abs @0)))
253
254/* Transform X * copysign (1.0, -X) into -abs(X). */
255(simplify
c6cfa2bf 256 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
8c2805bb
AP
257 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
258 (negate (abs @0))))
259
260/* Transform copysign (CST, X) into copysign (ABS(CST), X). */
261(simplify
c6cfa2bf 262 (COPYSIGN_ALL REAL_CST@0 @1)
8c2805bb 263 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
c6cfa2bf 264 (COPYSIGN_ALL (negate @0) @1)))
8c2805bb 265
5b7f6ed0 266/* X * 1, X / 1 -> X. */
e0ee10ed
RB
267(for op (mult trunc_div ceil_div floor_div round_div exact_div)
268 (simplify
269 (op @0 integer_onep)
270 (non_lvalue @0)))
271
71f82be9
JG
272/* (A / (1 << B)) -> (A >> B).
273 Only for unsigned A. For signed A, this would not preserve rounding
274 toward zero.
275 For example: (-1 / ( 1 << B)) != -1 >> B. */
276(simplify
277 (trunc_div @0 (lshift integer_onep@1 @2))
278 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
279 && (!VECTOR_TYPE_P (type)
280 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
281 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
282 (rshift @0 @2)))
283
5b7f6ed0
MG
284/* Preserve explicit divisions by 0: the C++ front-end wants to detect
285 undefined behavior in constexpr evaluation, and assuming that the division
286 traps enables better optimizations than these anyway. */
a7f24614 287(for div (trunc_div ceil_div floor_div round_div exact_div)
5b7f6ed0
MG
288 /* 0 / X is always zero. */
289 (simplify
290 (div integer_zerop@0 @1)
291 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
292 (if (!integer_zerop (@1))
293 @0))
da186c1f 294 /* X / -1 is -X. */
a7f24614 295 (simplify
09240451
MG
296 (div @0 integer_minus_onep@1)
297 (if (!TYPE_UNSIGNED (type))
da186c1f 298 (negate @0)))
5b7f6ed0
MG
299 /* X / X is one. */
300 (simplify
301 (div @0 @0)
9ebce098
JJ
302 /* But not for 0 / 0 so that we can get the proper warnings and errors.
303 And not for _Fract types where we can't build 1. */
304 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
5b7f6ed0 305 { build_one_cst (type); }))
03cc70b5 306 /* X / abs (X) is X < 0 ? -1 : 1. */
da186c1f 307 (simplify
d96a5585
RB
308 (div:C @0 (abs @0))
309 (if (INTEGRAL_TYPE_P (type)
da186c1f
RB
310 && TYPE_OVERFLOW_UNDEFINED (type))
311 (cond (lt @0 { build_zero_cst (type); })
312 { build_minus_one_cst (type); } { build_one_cst (type); })))
313 /* X / -X is -1. */
314 (simplify
d96a5585 315 (div:C @0 (negate @0))
da186c1f
RB
316 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
317 && TYPE_OVERFLOW_UNDEFINED (type))
318 { build_minus_one_cst (type); })))
a7f24614
RB
319
320/* For unsigned integral types, FLOOR_DIV_EXPR is the same as
321 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
322(simplify
323 (floor_div @0 @1)
09240451
MG
324 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
325 && TYPE_UNSIGNED (type))
a7f24614
RB
326 (trunc_div @0 @1)))
327
28093105
RB
328/* Combine two successive divisions. Note that combining ceil_div
329 and floor_div is trickier and combining round_div even more so. */
330(for div (trunc_div exact_div)
c306cfaf 331 (simplify
98610dc5 332 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
c306cfaf 333 (with {
4a669ac3 334 wi::overflow_type overflow;
8e6cdc90 335 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4a669ac3 336 TYPE_SIGN (type), &overflow);
c306cfaf 337 }
98610dc5
JJ
338 (if (div == EXACT_DIV_EXPR
339 || optimize_successive_divisions_p (@2, @3))
340 (if (!overflow)
341 (div @0 { wide_int_to_tree (type, mul); })
342 (if (TYPE_UNSIGNED (type)
343 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
344 { build_zero_cst (type); }))))))
c306cfaf 345
288fe52e
AM
346/* Combine successive multiplications. Similar to above, but handling
347 overflow is different. */
348(simplify
349 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
350 (with {
4a669ac3 351 wi::overflow_type overflow;
8e6cdc90 352 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4a669ac3 353 TYPE_SIGN (type), &overflow);
288fe52e
AM
354 }
355 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
356 otherwise undefined overflow implies that @0 must be zero. */
4a669ac3 357 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
288fe52e
AM
358 (mult @0 { wide_int_to_tree (type, mul); }))))
359
a7f24614 360/* Optimize A / A to 1.0 if we don't care about
09240451 361 NaNs or Infinities. */
a7f24614
RB
362(simplify
363 (rdiv @0 @0)
09240451 364 (if (FLOAT_TYPE_P (type)
1b457aa4 365 && ! HONOR_NANS (type)
8b5ee871 366 && ! HONOR_INFINITIES (type))
09240451
MG
367 { build_one_cst (type); }))
368
369/* Optimize -A / A to -1.0 if we don't care about
370 NaNs or Infinities. */
371(simplify
e04d2a35 372 (rdiv:C @0 (negate @0))
09240451 373 (if (FLOAT_TYPE_P (type)
1b457aa4 374 && ! HONOR_NANS (type)
8b5ee871 375 && ! HONOR_INFINITIES (type))
09240451 376 { build_minus_one_cst (type); }))
a7f24614 377
8c6961ca
PK
378/* PR71078: x / abs(x) -> copysign (1.0, x) */
379(simplify
380 (rdiv:C (convert? @0) (convert? (abs @0)))
381 (if (SCALAR_FLOAT_TYPE_P (type)
382 && ! HONOR_NANS (type)
383 && ! HONOR_INFINITIES (type))
384 (switch
385 (if (types_match (type, float_type_node))
386 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
387 (if (types_match (type, double_type_node))
388 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
389 (if (types_match (type, long_double_type_node))
390 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
391
a7f24614
RB
392/* In IEEE floating point, x/1 is not equivalent to x for snans. */
393(simplify
394 (rdiv @0 real_onep)
8b5ee871 395 (if (!HONOR_SNANS (type))
a7f24614
RB
396 (non_lvalue @0)))
397
398/* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
399(simplify
400 (rdiv @0 real_minus_onep)
8b5ee871 401 (if (!HONOR_SNANS (type))
a7f24614
RB
402 (negate @0)))
403
5711ac88 404(if (flag_reciprocal_math)
81825e28 405 /* Convert (A/B)/C to A/(B*C). */
5711ac88
N
406 (simplify
407 (rdiv (rdiv:s @0 @1) @2)
81825e28
WD
408 (rdiv @0 (mult @1 @2)))
409
410 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
411 (simplify
412 (rdiv @0 (mult:s @1 REAL_CST@2))
413 (with
414 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
415 (if (tem)
416 (rdiv (mult @0 { tem; } ) @1))))
5711ac88
N
417
418 /* Convert A/(B/C) to (A/B)*C */
419 (simplify
420 (rdiv @0 (rdiv:s @1 @2))
421 (mult (rdiv @0 @1) @2)))
422
6a435314
WD
423/* Simplify x / (- y) to -x / y. */
424(simplify
425 (rdiv @0 (negate @1))
426 (rdiv (negate @0) @1))
427
5e21d765
WD
428(if (flag_unsafe_math_optimizations)
429 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
430 Since C / x may underflow to zero, do this only for unsafe math. */
431 (for op (lt le gt ge)
432 neg_op (gt ge lt le)
433 (simplify
434 (op (rdiv REAL_CST@0 @1) real_zerop@2)
435 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
436 (switch
437 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
438 (op @1 @2))
439 /* For C < 0, use the inverted operator. */
440 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
441 (neg_op @1 @2)))))))
442
5711ac88
N
443/* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
444(for div (trunc_div ceil_div floor_div round_div exact_div)
445 (simplify
446 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
447 (if (integer_pow2p (@2)
448 && tree_int_cst_sgn (@2) > 0
a1488398 449 && tree_nop_conversion_p (type, TREE_TYPE (@0))
8e6cdc90
RS
450 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
451 (rshift (convert @0)
452 { build_int_cst (integer_type_node,
453 wi::exact_log2 (wi::to_wide (@2))); }))))
5711ac88 454
a7f24614
RB
455/* If ARG1 is a constant, we can convert this to a multiply by the
456 reciprocal. This does not have the same rounding properties,
457 so only do this if -freciprocal-math. We can actually
458 always safely do it if ARG1 is a power of two, but it's hard to
459 tell if it is or not in a portable manner. */
460(for cst (REAL_CST COMPLEX_CST VECTOR_CST)
461 (simplify
462 (rdiv @0 cst@1)
463 (if (optimize)
53bc4b3a
RB
464 (if (flag_reciprocal_math
465 && !real_zerop (@1))
a7f24614 466 (with
249700b5 467 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
a7f24614 468 (if (tem)
8fdc6c67
RB
469 (mult @0 { tem; } )))
470 (if (cst != COMPLEX_CST)
471 (with { tree inverse = exact_inverse (type, @1); }
472 (if (inverse)
473 (mult @0 { inverse; } ))))))))
a7f24614 474
a7f24614 475(for mod (ceil_mod floor_mod round_mod trunc_mod)
e0ee10ed
RB
476 /* 0 % X is always zero. */
477 (simplify
a7f24614 478 (mod integer_zerop@0 @1)
e0ee10ed
RB
479 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
480 (if (!integer_zerop (@1))
481 @0))
482 /* X % 1 is always zero. */
483 (simplify
a7f24614
RB
484 (mod @0 integer_onep)
485 { build_zero_cst (type); })
486 /* X % -1 is zero. */
487 (simplify
09240451
MG
488 (mod @0 integer_minus_onep@1)
489 (if (!TYPE_UNSIGNED (type))
bc4315fb 490 { build_zero_cst (type); }))
5b7f6ed0
MG
491 /* X % X is zero. */
492 (simplify
493 (mod @0 @0)
494 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
495 (if (!integer_zerop (@0))
496 { build_zero_cst (type); }))
bc4315fb
MG
497 /* (X % Y) % Y is just X % Y. */
498 (simplify
499 (mod (mod@2 @0 @1) @1)
98e30e51
RB
500 @2)
501 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
502 (simplify
503 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
504 (if (ANY_INTEGRAL_TYPE_P (type)
505 && TYPE_OVERFLOW_UNDEFINED (type)
8e6cdc90
RS
506 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
507 TYPE_SIGN (type)))
392750c5
JJ
508 { build_zero_cst (type); }))
509 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
510 modulo and comparison, since it is simpler and equivalent. */
511 (for cmp (eq ne)
512 (simplify
513 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
514 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
515 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
516 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
a7f24614
RB
517
518/* X % -C is the same as X % C. */
519(simplify
520 (trunc_mod @0 INTEGER_CST@1)
521 (if (TYPE_SIGN (type) == SIGNED
522 && !TREE_OVERFLOW (@1)
8e6cdc90 523 && wi::neg_p (wi::to_wide (@1))
a7f24614
RB
524 && !TYPE_OVERFLOW_TRAPS (type)
525 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
526 && !sign_bit_p (@1, @1))
527 (trunc_mod @0 (negate @1))))
e0ee10ed 528
8f0c696a
RB
529/* X % -Y is the same as X % Y. */
530(simplify
531 (trunc_mod @0 (convert? (negate @1)))
a2a743a1
MP
532 (if (INTEGRAL_TYPE_P (type)
533 && !TYPE_UNSIGNED (type)
8f0c696a 534 && !TYPE_OVERFLOW_TRAPS (type)
20b8d734
JJ
535 && tree_nop_conversion_p (type, TREE_TYPE (@1))
536 /* Avoid this transformation if X might be INT_MIN or
537 Y might be -1, because we would then change valid
538 INT_MIN % -(-1) into invalid INT_MIN % -1. */
8e6cdc90 539 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
20b8d734
JJ
540 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
541 (TREE_TYPE (@1))))))
8f0c696a
RB
542 (trunc_mod @0 (convert @1))))
543
f461569a
MP
544/* X - (X / Y) * Y is the same as X % Y. */
545(simplify
2eef1fc1
RB
546 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
547 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
fba46f03 548 (convert (trunc_mod @0 @1))))
f461569a 549
8f0c696a
RB
550/* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
551 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
552 Also optimize A % (C << N) where C is a power of 2,
553 to A & ((C << N) - 1). */
554(match (power_of_two_cand @1)
555 INTEGER_CST@1)
556(match (power_of_two_cand @1)
557 (lshift INTEGER_CST@1 @2))
558(for mod (trunc_mod floor_mod)
559 (simplify
4ab1e111 560 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
8f0c696a
RB
561 (if ((TYPE_UNSIGNED (type)
562 || tree_expr_nonnegative_p (@0))
4ab1e111 563 && tree_nop_conversion_p (type, TREE_TYPE (@3))
8f0c696a 564 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
4ab1e111 565 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
8f0c696a 566
887ab609
N
567/* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
568(simplify
569 (trunc_div (mult @0 integer_pow2p@1) @1)
570 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
571 (bit_and @0 { wide_int_to_tree
8e6cdc90
RS
572 (type, wi::mask (TYPE_PRECISION (type)
573 - wi::exact_log2 (wi::to_wide (@1)),
887ab609
N
574 false, TYPE_PRECISION (type))); })))
575
5f8d832e
N
576/* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
577(simplify
578 (mult (trunc_div @0 integer_pow2p@1) @1)
579 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
580 (bit_and @0 (negate @1))))
581
95765f36
N
582/* Simplify (t * 2) / 2) -> t. */
583(for div (trunc_div ceil_div floor_div round_div exact_div)
584 (simplify
55d84e61 585 (div (mult:c @0 @1) @1)
95765f36
N
586 (if (ANY_INTEGRAL_TYPE_P (type)
587 && TYPE_OVERFLOW_UNDEFINED (type))
588 @0)))
589
d202f9bd 590(for op (negate abs)
9b054b08
RS
591 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
592 (for coss (COS COSH)
593 (simplify
594 (coss (op @0))
595 (coss @0)))
596 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
597 (for pows (POW)
598 (simplify
599 (pows (op @0) REAL_CST@1)
600 (with { HOST_WIDE_INT n; }
601 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
5d3498b4 602 (pows @0 @1)))))
de3fbea3
RB
603 /* Likewise for powi. */
604 (for pows (POWI)
605 (simplify
606 (pows (op @0) INTEGER_CST@1)
8e6cdc90 607 (if ((wi::to_wide (@1) & 1) == 0)
de3fbea3 608 (pows @0 @1))))
5d3498b4
RS
609 /* Strip negate and abs from both operands of hypot. */
610 (for hypots (HYPOT)
611 (simplify
612 (hypots (op @0) @1)
613 (hypots @0 @1))
614 (simplify
615 (hypots @0 (op @1))
616 (hypots @0 @1)))
617 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
c6cfa2bf 618 (for copysigns (COPYSIGN_ALL)
5d3498b4
RS
619 (simplify
620 (copysigns (op @0) @1)
621 (copysigns @0 @1))))
622
623/* abs(x)*abs(x) -> x*x. Should be valid for all types. */
624(simplify
625 (mult (abs@1 @0) @1)
626 (mult @0 @0))
627
64f7ea7c
KV
628/* Convert absu(x)*absu(x) -> x*x. */
629(simplify
630 (mult (absu@1 @0) @1)
631 (mult (convert@2 @0) @2))
632
5d3498b4
RS
633/* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
634(for coss (COS COSH)
635 copysigns (COPYSIGN)
636 (simplify
637 (coss (copysigns @0 @1))
638 (coss @0)))
639
640/* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
641(for pows (POW)
642 copysigns (COPYSIGN)
643 (simplify
de3fbea3 644 (pows (copysigns @0 @2) REAL_CST@1)
5d3498b4
RS
645 (with { HOST_WIDE_INT n; }
646 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
647 (pows @0 @1)))))
de3fbea3
RB
648/* Likewise for powi. */
649(for pows (POWI)
650 copysigns (COPYSIGN)
651 (simplify
652 (pows (copysigns @0 @2) INTEGER_CST@1)
8e6cdc90 653 (if ((wi::to_wide (@1) & 1) == 0)
de3fbea3 654 (pows @0 @1))))
5d3498b4
RS
655
656(for hypots (HYPOT)
657 copysigns (COPYSIGN)
658 /* hypot(copysign(x, y), z) -> hypot(x, z). */
659 (simplify
660 (hypots (copysigns @0 @1) @2)
661 (hypots @0 @2))
662 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
663 (simplify
664 (hypots @0 (copysigns @1 @2))
665 (hypots @0 @1)))
666
eeb57981 667/* copysign(x, CST) -> [-]abs (x). */
c6cfa2bf 668(for copysigns (COPYSIGN_ALL)
eeb57981
RB
669 (simplify
670 (copysigns @0 REAL_CST@1)
671 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
672 (negate (abs @0))
673 (abs @0))))
674
5d3498b4 675/* copysign(copysign(x, y), z) -> copysign(x, z). */
c6cfa2bf 676(for copysigns (COPYSIGN_ALL)
5d3498b4
RS
677 (simplify
678 (copysigns (copysigns @0 @1) @2)
679 (copysigns @0 @2)))
680
681/* copysign(x,y)*copysign(x,y) -> x*x. */
c6cfa2bf 682(for copysigns (COPYSIGN_ALL)
5d3498b4
RS
683 (simplify
684 (mult (copysigns@2 @0 @1) @2)
685 (mult @0 @0)))
686
687/* ccos(-x) -> ccos(x). Similarly for ccosh. */
688(for ccoss (CCOS CCOSH)
689 (simplify
690 (ccoss (negate @0))
691 (ccoss @0)))
d202f9bd 692
abcc43f5
RS
693/* cabs(-x) and cos(conj(x)) -> cabs(x). */
694(for ops (conj negate)
695 (for cabss (CABS)
696 (simplify
697 (cabss (ops @0))
698 (cabss @0))))
699
0a8f32b8
RB
700/* Fold (a * (1 << b)) into (a << b) */
701(simplify
702 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
703 (if (! FLOAT_TYPE_P (type)
9ff6fb6e 704 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
0a8f32b8
RB
705 (lshift @0 @2)))
706
4349b15f
SD
707/* Fold (1 << (C - x)) where C = precision(type) - 1
708 into ((1 << C) >> x). */
709(simplify
710 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
711 (if (INTEGRAL_TYPE_P (type)
56ccfbd6 712 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
4349b15f
SD
713 && single_use (@1))
714 (if (TYPE_UNSIGNED (type))
715 (rshift (lshift @0 @2) @3)
716 (with
717 { tree utype = unsigned_type_for (type); }
718 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
719
0a8f32b8
RB
720/* Fold (C1/X)*C2 into (C1*C2)/X. */
721(simplify
ff86345f
RB
722 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
723 (if (flag_associative_math
724 && single_use (@3))
0a8f32b8
RB
725 (with
726 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
727 (if (tem)
728 (rdiv { tem; } @1)))))
729
730/* Simplify ~X & X as zero. */
731(simplify
732 (bit_and:c (convert? @0) (convert? (bit_not @0)))
733 { build_zero_cst (type); })
734
89b80c42
PK
735/* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
736(simplify
737 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
738 (if (TYPE_UNSIGNED (type))
739 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
740
7aa13860
PK
741(for bitop (bit_and bit_ior)
742 cmp (eq ne)
a93952d2
JJ
743 /* PR35691: Transform
744 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
745 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
7aa13860
PK
746 (simplify
747 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
748 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
a93952d2
JJ
749 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
750 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
751 (cmp (bit_ior @0 (convert @1)) @2)))
752 /* Transform:
753 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
754 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
755 (simplify
756 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
757 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
758 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
759 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
760 (cmp (bit_and @0 (convert @1)) @2))))
7aa13860 761
10158317
RB
762/* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
763(simplify
a9658b11 764 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
10158317
RB
765 (minus (bit_xor @0 @1) @1))
766(simplify
767 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
8e6cdc90 768 (if (~wi::to_wide (@2) == wi::to_wide (@1))
10158317
RB
769 (minus (bit_xor @0 @1) @1)))
770
771/* Fold (A & B) - (A & ~B) into B - (A ^ B). */
772(simplify
a8e9f9a3 773 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
10158317
RB
774 (minus @1 (bit_xor @0 @1)))
775
42bd89ce
MG
776/* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
777(for op (bit_ior bit_xor plus)
778 (simplify
779 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
780 (bit_xor @0 @1))
781 (simplify
782 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
8e6cdc90 783 (if (~wi::to_wide (@2) == wi::to_wide (@1))
42bd89ce 784 (bit_xor @0 @1))))
2066ef6a
PK
785
786/* PR53979: Transform ((a ^ b) | a) -> (a | b) */
787(simplify
788 (bit_ior:c (bit_xor:c @0 @1) @0)
789 (bit_ior @0 @1))
790
e268a77b
MG
791/* (a & ~b) | (a ^ b) --> a ^ b */
792(simplify
793 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
794 @2)
795
796/* (a & ~b) ^ ~a --> ~(a & b) */
797(simplify
798 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
799 (bit_not (bit_and @0 @1)))
800
801/* (a | b) & ~(a ^ b) --> a & b */
802(simplify
803 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
804 (bit_and @0 @1))
805
806/* a | ~(a ^ b) --> a | ~b */
807(simplify
808 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
809 (bit_ior @0 (bit_not @1)))
810
811/* (a | b) | (a &^ b) --> a | b */
812(for op (bit_and bit_xor)
813 (simplify
814 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
815 @2))
816
817/* (a & b) | ~(a ^ b) --> ~(a ^ b) */
818(simplify
819 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
820 @2)
821
822/* ~(~a & b) --> a | ~b */
823(simplify
824 (bit_not (bit_and:cs (bit_not @0) @1))
825 (bit_ior @0 (bit_not @1)))
826
fd8303a5
MC
827/* ~(~a | b) --> a & ~b */
828(simplify
829 (bit_not (bit_ior:cs (bit_not @0) @1))
830 (bit_and @0 (bit_not @1)))
831
d982c5b7
MG
832/* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
833#if GIMPLE
834(simplify
835 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
836 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8e6cdc90 837 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
d982c5b7
MG
838 (bit_xor @0 @1)))
839#endif
10158317 840
f2901002
JJ
841/* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
842 ((A & N) + B) & M -> (A + B) & M
843 Similarly if (N & M) == 0,
844 ((A | N) + B) & M -> (A + B) & M
845 and for - instead of + (or unary - instead of +)
846 and/or ^ instead of |.
847 If B is constant and (B & M) == 0, fold into A & M. */
848(for op (plus minus)
849 (for bitop (bit_and bit_ior bit_xor)
850 (simplify
851 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
852 (with
853 { tree pmop[2];
854 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
855 @3, @4, @1, ERROR_MARK, NULL_TREE,
856 NULL_TREE, pmop); }
857 (if (utype)
858 (convert (bit_and (op (convert:utype { pmop[0]; })
859 (convert:utype { pmop[1]; }))
860 (convert:utype @2))))))
861 (simplify
862 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
863 (with
864 { tree pmop[2];
865 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
866 NULL_TREE, NULL_TREE, @1, bitop, @3,
867 @4, pmop); }
868 (if (utype)
869 (convert (bit_and (op (convert:utype { pmop[0]; })
870 (convert:utype { pmop[1]; }))
871 (convert:utype @2)))))))
872 (simplify
873 (bit_and (op:s @0 @1) INTEGER_CST@2)
874 (with
875 { tree pmop[2];
876 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
877 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
878 NULL_TREE, NULL_TREE, pmop); }
879 (if (utype)
880 (convert (bit_and (op (convert:utype { pmop[0]; })
881 (convert:utype { pmop[1]; }))
882 (convert:utype @2)))))))
883(for bitop (bit_and bit_ior bit_xor)
884 (simplify
885 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
886 (with
887 { tree pmop[2];
888 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
889 bitop, @2, @3, NULL_TREE, ERROR_MARK,
890 NULL_TREE, NULL_TREE, pmop); }
891 (if (utype)
892 (convert (bit_and (negate (convert:utype { pmop[0]; }))
893 (convert:utype @1)))))))
894
bc4315fb
MG
895/* X % Y is smaller than Y. */
896(for cmp (lt ge)
897 (simplify
898 (cmp (trunc_mod @0 @1) @1)
899 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
900 { constant_boolean_node (cmp == LT_EXPR, type); })))
901(for cmp (gt le)
902 (simplify
903 (cmp @1 (trunc_mod @0 @1))
904 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
905 { constant_boolean_node (cmp == GT_EXPR, type); })))
906
e0ee10ed
RB
907/* x | ~0 -> ~0 */
908(simplify
ca0b7ece
RB
909 (bit_ior @0 integer_all_onesp@1)
910 @1)
911
912/* x | 0 -> x */
913(simplify
914 (bit_ior @0 integer_zerop)
915 @0)
e0ee10ed
RB
916
917/* x & 0 -> 0 */
918(simplify
ca0b7ece
RB
919 (bit_and @0 integer_zerop@1)
920 @1)
e0ee10ed 921
a4398a30 922/* ~x | x -> -1 */
8b5ee871
MG
923/* ~x ^ x -> -1 */
924/* ~x + x -> -1 */
925(for op (bit_ior bit_xor plus)
926 (simplify
927 (op:c (convert? @0) (convert? (bit_not @0)))
928 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
a4398a30 929
e0ee10ed
RB
930/* x ^ x -> 0 */
931(simplify
932 (bit_xor @0 @0)
933 { build_zero_cst (type); })
934
36a60e48
RB
935/* Canonicalize X ^ ~0 to ~X. */
936(simplify
937 (bit_xor @0 integer_all_onesp@1)
938 (bit_not @0))
939
940/* x & ~0 -> x */
941(simplify
942 (bit_and @0 integer_all_onesp)
943 (non_lvalue @0))
944
945/* x & x -> x, x | x -> x */
946(for bitop (bit_and bit_ior)
947 (simplify
948 (bitop @0 @0)
949 (non_lvalue @0)))
950
c7986356
MG
951/* x & C -> x if we know that x & ~C == 0. */
952#if GIMPLE
953(simplify
954 (bit_and SSA_NAME@0 INTEGER_CST@1)
955 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8e6cdc90 956 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
c7986356
MG
957 @0))
958#endif
959
0f770b01
RV
960/* x + (x & 1) -> (x + 1) & ~1 */
961(simplify
44fc0a51
RB
962 (plus:c @0 (bit_and:s @0 integer_onep@1))
963 (bit_and (plus @0 @1) (bit_not @1)))
0f770b01
RV
964
965/* x & ~(x & y) -> x & ~y */
966/* x | ~(x | y) -> x | ~y */
967(for bitop (bit_and bit_ior)
af563d4b 968 (simplify
44fc0a51
RB
969 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
970 (bitop @0 (bit_not @1))))
af563d4b 971
03cc70b5
MC
972/* (~x & y) | ~(x | y) -> ~x */
973(simplify
974 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
975 @2)
976
977/* (x | y) ^ (x | ~y) -> ~x */
978(simplify
979 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
980 (bit_not @0))
981
982/* (x & y) | ~(x | y) -> ~(x ^ y) */
983(simplify
984 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
985 (bit_not (bit_xor @0 @1)))
986
987/* (~x | y) ^ (x ^ y) -> x | ~y */
988(simplify
989 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
990 (bit_ior @0 (bit_not @1)))
991
992/* (x ^ y) | ~(x | y) -> ~(x & y) */
993(simplify
994 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
995 (bit_not (bit_and @0 @1)))
996
af563d4b
MG
997/* (x | y) & ~x -> y & ~x */
998/* (x & y) | ~x -> y | ~x */
999(for bitop (bit_and bit_ior)
1000 rbitop (bit_ior bit_and)
1001 (simplify
1002 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1003 (bitop @1 @2)))
0f770b01 1004
f13c4673
MP
1005/* (x & y) ^ (x | y) -> x ^ y */
1006(simplify
2d6f2dce
MP
1007 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1008 (bit_xor @0 @1))
f13c4673 1009
9ea65ca6
MP
1010/* (x ^ y) ^ (x | y) -> x & y */
1011(simplify
1012 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1013 (bit_and @0 @1))
1014
1015/* (x & y) + (x ^ y) -> x | y */
1016/* (x & y) | (x ^ y) -> x | y */
1017/* (x & y) ^ (x ^ y) -> x | y */
1018(for op (plus bit_ior bit_xor)
1019 (simplify
1020 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1021 (bit_ior @0 @1)))
1022
1023/* (x & y) + (x | y) -> x + y */
1024(simplify
1025 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1026 (plus @0 @1))
1027
9737efaf
MP
1028/* (x + y) - (x | y) -> x & y */
1029(simplify
1030 (minus (plus @0 @1) (bit_ior @0 @1))
1031 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1032 && !TYPE_SATURATING (type))
1033 (bit_and @0 @1)))
1034
1035/* (x + y) - (x & y) -> x | y */
1036(simplify
1037 (minus (plus @0 @1) (bit_and @0 @1))
1038 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1039 && !TYPE_SATURATING (type))
1040 (bit_ior @0 @1)))
1041
9ea65ca6
MP
1042/* (x | y) - (x ^ y) -> x & y */
1043(simplify
1044 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1045 (bit_and @0 @1))
1046
1047/* (x | y) - (x & y) -> x ^ y */
1048(simplify
1049 (minus (bit_ior @0 @1) (bit_and @0 @1))
1050 (bit_xor @0 @1))
1051
66cc6273
MP
1052/* (x | y) & ~(x & y) -> x ^ y */
1053(simplify
1054 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1055 (bit_xor @0 @1))
1056
1057/* (x | y) & (~x ^ y) -> x & y */
1058(simplify
1059 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1060 (bit_and @0 @1))
1061
fd8303a5
MC
1062/* (~x | y) & (x | ~y) -> ~(x ^ y) */
1063(simplify
1064 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1065 (bit_not (bit_xor @0 @1)))
1066
1067/* (~x | y) ^ (x | ~y) -> x ^ y */
1068(simplify
1069 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1070 (bit_xor @0 @1))
1071
5b00d921
RB
1072/* ~x & ~y -> ~(x | y)
1073 ~x | ~y -> ~(x & y) */
1074(for op (bit_and bit_ior)
1075 rop (bit_ior bit_and)
1076 (simplify
1077 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
ece46666
MG
1078 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1079 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
5b00d921
RB
1080 (bit_not (rop (convert @0) (convert @1))))))
1081
14ea9f92 1082/* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
5b00d921
RB
1083 with a constant, and the two constants have no bits in common,
1084 we should treat this as a BIT_IOR_EXPR since this may produce more
1085 simplifications. */
14ea9f92
RB
1086(for op (bit_xor plus)
1087 (simplify
1088 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1089 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1090 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1091 && tree_nop_conversion_p (type, TREE_TYPE (@2))
8e6cdc90 1092 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
14ea9f92 1093 (bit_ior (convert @4) (convert @5)))))
5b00d921
RB
1094
1095/* (X | Y) ^ X -> Y & ~ X*/
1096(simplify
2eef1fc1 1097 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
5b00d921
RB
1098 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1099 (convert (bit_and @1 (bit_not @0)))))
1100
1101/* Convert ~X ^ ~Y to X ^ Y. */
1102(simplify
1103 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
ece46666
MG
1104 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1105 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
5b00d921
RB
1106 (bit_xor (convert @0) (convert @1))))
1107
1108/* Convert ~X ^ C to X ^ ~C. */
1109(simplify
1110 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
c8ba6498
EB
1111 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1112 (bit_xor (convert @0) (bit_not @1))))
5b00d921 1113
e39dab2c
MG
1114/* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1115(for opo (bit_and bit_xor)
1116 opi (bit_xor bit_and)
1117 (simplify
de5b5228 1118 (opo:c (opi:cs @0 @1) @1)
e39dab2c 1119 (bit_and (bit_not @0) @1)))
97e77391 1120
14ea9f92
RB
1121/* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1122 operands are another bit-wise operation with a common input. If so,
1123 distribute the bit operations to save an operation and possibly two if
1124 constants are involved. For example, convert
1125 (A | B) & (A | C) into A | (B & C)
1126 Further simplification will occur if B and C are constants. */
e07ab2fe
MG
1127(for op (bit_and bit_ior bit_xor)
1128 rop (bit_ior bit_and bit_and)
14ea9f92 1129 (simplify
2eef1fc1 1130 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
e07ab2fe
MG
1131 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1132 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
14ea9f92
RB
1133 (rop (convert @0) (op (convert @1) (convert @2))))))
1134
e39dab2c
MG
1135/* Some simple reassociation for bit operations, also handled in reassoc. */
1136/* (X & Y) & Y -> X & Y
1137 (X | Y) | Y -> X | Y */
1138(for op (bit_and bit_ior)
1139 (simplify
2eef1fc1 1140 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
e39dab2c
MG
1141 @2))
1142/* (X ^ Y) ^ Y -> X */
1143(simplify
2eef1fc1 1144 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
ece46666 1145 (convert @0))
e39dab2c
MG
1146/* (X & Y) & (X & Z) -> (X & Y) & Z
1147 (X | Y) | (X | Z) -> (X | Y) | Z */
1148(for op (bit_and bit_ior)
1149 (simplify
6c35e5b0 1150 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
e39dab2c
MG
1151 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1152 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1153 (if (single_use (@5) && single_use (@6))
1154 (op @3 (convert @2))
1155 (if (single_use (@3) && single_use (@4))
1156 (op (convert @1) @5))))))
1157/* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1158(simplify
1159 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1160 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1161 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
d78789f5 1162 (bit_xor (convert @1) (convert @2))))
5b00d921 1163
64f7ea7c
KV
1164/* Convert abs (abs (X)) into abs (X).
1165 also absu (absu (X)) into absu (X). */
b14a9c57
RB
1166(simplify
1167 (abs (abs@1 @0))
1168 @1)
64f7ea7c
KV
1169
1170(simplify
1171 (absu (convert@2 (absu@1 @0)))
1172 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1173 @1))
1174
1175/* Convert abs[u] (-X) -> abs[u] (X). */
f3582e54
RB
1176(simplify
1177 (abs (negate @0))
1178 (abs @0))
64f7ea7c
KV
1179
1180(simplify
1181 (absu (negate @0))
1182 (absu @0))
1183
1184/* Convert abs[u] (X) where X is nonnegative -> (X). */
f3582e54
RB
1185(simplify
1186 (abs tree_expr_nonnegative_p@0)
1187 @0)
1188
64f7ea7c
KV
1189(simplify
1190 (absu tree_expr_nonnegative_p@0)
1191 (convert @0))
1192
55cf3946
RB
1193/* A few cases of fold-const.c negate_expr_p predicate. */
1194(match negate_expr_p
1195 INTEGER_CST
b14a9c57 1196 (if ((INTEGRAL_TYPE_P (type)
56a6d474 1197 && TYPE_UNSIGNED (type))
b14a9c57 1198 || (!TYPE_OVERFLOW_SANITIZED (type)
55cf3946
RB
1199 && may_negate_without_overflow_p (t)))))
1200(match negate_expr_p
1201 FIXED_CST)
1202(match negate_expr_p
1203 (negate @0)
1204 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1205(match negate_expr_p
1206 REAL_CST
1207 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1208/* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1209 ways. */
1210(match negate_expr_p
1211 VECTOR_CST
1212 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
81bd903a
MG
1213(match negate_expr_p
1214 (minus @0 @1)
1215 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1216 || (FLOAT_TYPE_P (type)
1217 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1218 && !HONOR_SIGNED_ZEROS (type)))))
0a8f32b8
RB
1219
1220/* (-A) * (-B) -> A * B */
1221(simplify
1222 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1223 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1224 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1225 (mult (convert @0) (convert (negate @1)))))
03cc70b5 1226
55cf3946 1227/* -(A + B) -> (-B) - A. */
b14a9c57 1228(simplify
55cf3946
RB
1229 (negate (plus:c @0 negate_expr_p@1))
1230 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1231 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1232 (minus (negate @1) @0)))
1233
81bd903a
MG
1234/* -(A - B) -> B - A. */
1235(simplify
1236 (negate (minus @0 @1))
1237 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1238 || (FLOAT_TYPE_P (type)
1239 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1240 && !HONOR_SIGNED_ZEROS (type)))
1241 (minus @1 @0)))
1af4ebf5
MG
1242(simplify
1243 (negate (pointer_diff @0 @1))
1244 (if (TYPE_OVERFLOW_UNDEFINED (type))
1245 (pointer_diff @1 @0)))
81bd903a 1246
55cf3946 1247/* A - B -> A + (-B) if B is easily negatable. */
b14a9c57 1248(simplify
55cf3946 1249 (minus @0 negate_expr_p@1)
e4e96a4f
KT
1250 (if (!FIXED_POINT_TYPE_P (type))
1251 (plus @0 (negate @1))))
d4573ffe 1252
5609420f
RB
1253/* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1254 when profitable.
1255 For bitwise binary operations apply operand conversions to the
1256 binary operation result instead of to the operands. This allows
1257 to combine successive conversions and bitwise binary operations.
1258 We combine the above two cases by using a conditional convert. */
1259(for bitop (bit_and bit_ior bit_xor)
1260 (simplify
1261 (bitop (convert @0) (convert? @1))
1262 (if (((TREE_CODE (@1) == INTEGER_CST
1263 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
ad6f996c 1264 && int_fits_type_p (@1, TREE_TYPE (@0)))
aea417d7 1265 || types_match (@0, @1))
ad6f996c
RB
1266 /* ??? This transform conflicts with fold-const.c doing
1267 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1268 constants (if x has signed type, the sign bit cannot be set
1269 in c). This folds extension into the BIT_AND_EXPR.
1270 Restrict it to GIMPLE to avoid endless recursions. */
1271 && (bitop != BIT_AND_EXPR || GIMPLE)
5609420f
RB
1272 && (/* That's a good idea if the conversion widens the operand, thus
1273 after hoisting the conversion the operation will be narrower. */
1274 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1275 /* It's also a good idea if the conversion is to a non-integer
1276 mode. */
1277 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1278 /* Or if the precision of TO is not the same as the precision
1279 of its mode. */
2be65d9e 1280 || !type_has_mode_precision_p (type)))
5609420f
RB
1281 (convert (bitop @0 (convert @1))))))
1282
b14a9c57
RB
1283(for bitop (bit_and bit_ior)
1284 rbitop (bit_ior bit_and)
1285 /* (x | y) & x -> x */
1286 /* (x & y) | x -> x */
1287 (simplify
1288 (bitop:c (rbitop:c @0 @1) @0)
1289 @0)
1290 /* (~x | y) & x -> x & y */
1291 /* (~x & y) | x -> x | y */
1292 (simplify
1293 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1294 (bitop @0 @1)))
1295
5609420f
RB
1296/* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1297(simplify
1298 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1299 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1300
1301/* Combine successive equal operations with constants. */
1302(for bitop (bit_and bit_ior bit_xor)
1303 (simplify
1304 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
fba05d9e
RS
1305 (if (!CONSTANT_CLASS_P (@0))
1306 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1307 folded to a constant. */
1308 (bitop @0 (bitop @1 @2))
1309 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1310 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1311 the values involved are such that the operation can't be decided at
1312 compile time. Try folding one of @0 or @1 with @2 to see whether
1313 that combination can be decided at compile time.
1314
1315 Keep the existing form if both folds fail, to avoid endless
1316 oscillation. */
1317 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1318 (if (cst1)
1319 (bitop @1 { cst1; })
1320 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1321 (if (cst2)
1322 (bitop @0 { cst2; }))))))))
5609420f
RB
1323
1324/* Try simple folding for X op !X, and X op X with the help
1325 of the truth_valued_p and logical_inverted_value predicates. */
1326(match truth_valued_p
1327 @0
1328 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
f84e7fd6 1329(for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
5609420f
RB
1330 (match truth_valued_p
1331 (op @0 @1)))
1332(match truth_valued_p
1333 (truth_not @0))
1334
0a8f32b8
RB
1335(match (logical_inverted_value @0)
1336 (truth_not @0))
5609420f
RB
1337(match (logical_inverted_value @0)
1338 (bit_not truth_valued_p@0))
1339(match (logical_inverted_value @0)
09240451 1340 (eq @0 integer_zerop))
5609420f 1341(match (logical_inverted_value @0)
09240451 1342 (ne truth_valued_p@0 integer_truep))
5609420f 1343(match (logical_inverted_value @0)
09240451 1344 (bit_xor truth_valued_p@0 integer_truep))
5609420f
RB
1345
1346/* X & !X -> 0. */
1347(simplify
1348 (bit_and:c @0 (logical_inverted_value @0))
1349 { build_zero_cst (type); })
1350/* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1351(for op (bit_ior bit_xor)
1352 (simplify
1353 (op:c truth_valued_p@0 (logical_inverted_value @0))
f84e7fd6 1354 { constant_boolean_node (true, type); }))
59c20dc7
RB
1355/* X ==/!= !X is false/true. */
1356(for op (eq ne)
1357 (simplify
1358 (op:c truth_valued_p@0 (logical_inverted_value @0))
1359 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
5609420f 1360
5609420f
RB
1361/* ~~x -> x */
1362(simplify
1363 (bit_not (bit_not @0))
1364 @0)
1365
b14a9c57
RB
1366/* Convert ~ (-A) to A - 1. */
1367(simplify
1368 (bit_not (convert? (negate @0)))
ece46666
MG
1369 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1370 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
8b5ee871 1371 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
b14a9c57 1372
81bd903a
MG
1373/* Convert - (~A) to A + 1. */
1374(simplify
1375 (negate (nop_convert (bit_not @0)))
1376 (plus (view_convert @0) { build_each_one_cst (type); }))
1377
b14a9c57
RB
1378/* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1379(simplify
8b5ee871 1380 (bit_not (convert? (minus @0 integer_each_onep)))
ece46666
MG
1381 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1382 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
b14a9c57
RB
1383 (convert (negate @0))))
1384(simplify
1385 (bit_not (convert? (plus @0 integer_all_onesp)))
ece46666
MG
1386 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1387 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
b14a9c57
RB
1388 (convert (negate @0))))
1389
1390/* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1391(simplify
1392 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1393 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1394 (convert (bit_xor @0 (bit_not @1)))))
1395(simplify
1396 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1397 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1398 (convert (bit_xor @0 @1))))
1399
e268a77b
MG
1400/* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1401(simplify
1402 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1403 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1404 (bit_not (bit_xor (view_convert @0) @1))))
1405
f52baa7b
MP
1406/* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1407(simplify
44fc0a51
RB
1408 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1409 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
f52baa7b 1410
f7b7b0aa
MP
1411/* Fold A - (A & B) into ~B & A. */
1412(simplify
2eef1fc1 1413 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
f7b7b0aa
MP
1414 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1415 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1416 (convert (bit_and (bit_not @1) @0))))
5609420f 1417
2071f8f9
N
1418/* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1419(for cmp (gt lt ge le)
1420(simplify
1421 (mult (convert (cmp @0 @1)) @2)
1422 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1423
e36c1cfe
N
1424/* For integral types with undefined overflow and C != 0 fold
1425 x * C EQ/NE y * C into x EQ/NE y. */
1426(for cmp (eq ne)
1427 (simplify
1428 (cmp (mult:c @0 @1) (mult:c @2 @1))
1429 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1430 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1431 && tree_expr_nonzero_p (@1))
1432 (cmp @0 @2))))
1433
42bd89ce
MG
1434/* For integral types with wrapping overflow and C odd fold
1435 x * C EQ/NE y * C into x EQ/NE y. */
1436(for cmp (eq ne)
1437 (simplify
1438 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1439 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1440 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1441 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1442 (cmp @0 @2))))
1443
e36c1cfe
N
1444/* For integral types with undefined overflow and C != 0 fold
1445 x * C RELOP y * C into:
84ff66b8 1446
e36c1cfe
N
1447 x RELOP y for nonnegative C
1448 y RELOP x for negative C */
1449(for cmp (lt gt le ge)
1450 (simplify
1451 (cmp (mult:c @0 @1) (mult:c @2 @1))
1452 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1453 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1454 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1455 (cmp @0 @2)
1456 (if (TREE_CODE (@1) == INTEGER_CST
8e6cdc90 1457 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
e36c1cfe 1458 (cmp @2 @0))))))
84ff66b8 1459
564e405c
JJ
1460/* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1461(for cmp (le gt)
1462 icmp (gt le)
1463 (simplify
1464 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1465 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1466 && TYPE_UNSIGNED (TREE_TYPE (@0))
1467 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
8e6cdc90
RS
1468 && (wi::to_wide (@2)
1469 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
564e405c
JJ
1470 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1471 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1472
a8492d5e
MG
1473/* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1474(for cmp (simple_comparison)
1475 (simplify
1476 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
8e6cdc90 1477 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
a8492d5e
MG
1478 (cmp @0 @1))))
1479
8d1628eb
JJ
1480/* X / C1 op C2 into a simple range test. */
1481(for cmp (simple_comparison)
1482 (simplify
1483 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1484 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1485 && integer_nonzerop (@1)
1486 && !TREE_OVERFLOW (@1)
1487 && !TREE_OVERFLOW (@2))
1488 (with { tree lo, hi; bool neg_overflow;
1489 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1490 &neg_overflow); }
1491 (switch
1492 (if (code == LT_EXPR || code == GE_EXPR)
1493 (if (TREE_OVERFLOW (lo))
1494 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1495 (if (code == LT_EXPR)
1496 (lt @0 { lo; })
1497 (ge @0 { lo; }))))
1498 (if (code == LE_EXPR || code == GT_EXPR)
1499 (if (TREE_OVERFLOW (hi))
1500 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1501 (if (code == LE_EXPR)
1502 (le @0 { hi; })
1503 (gt @0 { hi; }))))
1504 (if (!lo && !hi)
1505 { build_int_cst (type, code == NE_EXPR); })
1506 (if (code == EQ_EXPR && !hi)
1507 (ge @0 { lo; }))
1508 (if (code == EQ_EXPR && !lo)
1509 (le @0 { hi; }))
1510 (if (code == NE_EXPR && !hi)
1511 (lt @0 { lo; }))
1512 (if (code == NE_EXPR && !lo)
1513 (gt @0 { hi; }))
1514 (if (GENERIC)
1515 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1516 lo, hi); })
1517 (with
1518 {
1519 tree etype = range_check_type (TREE_TYPE (@0));
1520 if (etype)
1521 {
1522 if (! TYPE_UNSIGNED (etype))
1523 etype = unsigned_type_for (etype);
1524 hi = fold_convert (etype, hi);
1525 lo = fold_convert (etype, lo);
1526 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1527 }
1528 }
1529 (if (etype && hi && !TREE_OVERFLOW (hi))
1530 (if (code == EQ_EXPR)
1531 (le (minus (convert:etype @0) { lo; }) { hi; })
1532 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1533
d35256b6
MG
1534/* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1535(for op (lt le ge gt)
1536 (simplify
1537 (op (plus:c @0 @2) (plus:c @1 @2))
1538 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1539 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1540 (op @0 @1))))
1541/* For equality and subtraction, this is also true with wrapping overflow. */
1542(for op (eq ne minus)
1543 (simplify
1544 (op (plus:c @0 @2) (plus:c @1 @2))
1545 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1546 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1547 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1548 (op @0 @1))))
1549
1550/* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1551(for op (lt le ge gt)
1552 (simplify
1553 (op (minus @0 @2) (minus @1 @2))
1554 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1555 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1556 (op @0 @1))))
1557/* For equality and subtraction, this is also true with wrapping overflow. */
1558(for op (eq ne minus)
1559 (simplify
1560 (op (minus @0 @2) (minus @1 @2))
1561 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1562 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1563 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1564 (op @0 @1))))
1af4ebf5
MG
1565/* And for pointers... */
1566(for op (simple_comparison)
1567 (simplify
1568 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1569 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1570 (op @0 @1))))
1571(simplify
1572 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1573 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1574 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1575 (pointer_diff @0 @1)))
d35256b6
MG
1576
1577/* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1578(for op (lt le ge gt)
1579 (simplify
1580 (op (minus @2 @0) (minus @2 @1))
1581 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1582 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1583 (op @1 @0))))
1584/* For equality and subtraction, this is also true with wrapping overflow. */
1585(for op (eq ne minus)
1586 (simplify
1587 (op (minus @2 @0) (minus @2 @1))
1588 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1589 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1590 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1591 (op @1 @0))))
1af4ebf5
MG
1592/* And for pointers... */
1593(for op (simple_comparison)
1594 (simplify
1595 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1596 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1597 (op @1 @0))))
1598(simplify
1599 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1600 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1601 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1602 (pointer_diff @1 @0)))
d35256b6 1603
6358a676
MG
1604/* X + Y < Y is the same as X < 0 when there is no overflow. */
1605(for op (lt le gt ge)
1606 (simplify
1607 (op:c (plus:c@2 @0 @1) @1)
1608 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1609 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
cbd42900 1610 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6358a676
MG
1611 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1612 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1613/* For equality, this is also true with wrapping overflow. */
1614(for op (eq ne)
1615 (simplify
1616 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1617 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1618 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1619 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1620 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1621 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1622 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1623 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1624 (simplify
1625 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1626 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1627 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1628 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1629 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1630
1631/* X - Y < X is the same as Y > 0 when there is no overflow.
1632 For equality, this is also true with wrapping overflow. */
1633(for op (simple_comparison)
1634 (simplify
1635 (op:c @0 (minus@2 @0 @1))
1636 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1637 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1638 || ((op == EQ_EXPR || op == NE_EXPR)
1639 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1640 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1641 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1642
1d6fadee 1643/* Transform:
b8d85005
JJ
1644 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1645 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1d6fadee
PK
1646(for cmp (eq ne)
1647 ocmp (lt ge)
1648 (simplify
1649 (cmp (trunc_div @0 @1) integer_zerop)
1650 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
b8d85005
JJ
1651 /* Complex ==/!= is allowed, but not </>=. */
1652 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1d6fadee
PK
1653 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1654 (ocmp @0 @1))))
1655
8b656ca7
MG
1656/* X == C - X can never be true if C is odd. */
1657(for cmp (eq ne)
1658 (simplify
1659 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1660 (if (TREE_INT_CST_LOW (@1) & 1)
1661 { constant_boolean_node (cmp == NE_EXPR, type); })))
1662
10bc8017
MG
1663/* Arguments on which one can call get_nonzero_bits to get the bits
1664 possibly set. */
1665(match with_possible_nonzero_bits
1666 INTEGER_CST@0)
1667(match with_possible_nonzero_bits
1668 SSA_NAME@0
1669 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1670/* Slightly extended version, do not make it recursive to keep it cheap. */
1671(match (with_possible_nonzero_bits2 @0)
1672 with_possible_nonzero_bits@0)
1673(match (with_possible_nonzero_bits2 @0)
1674 (bit_and:c with_possible_nonzero_bits@0 @2))
1675
1676/* Same for bits that are known to be set, but we do not have
1677 an equivalent to get_nonzero_bits yet. */
1678(match (with_certain_nonzero_bits2 @0)
1679 INTEGER_CST@0)
1680(match (with_certain_nonzero_bits2 @0)
1681 (bit_ior @1 INTEGER_CST@0))
1682
1683/* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1684(for cmp (eq ne)
1685 (simplify
1686 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
8e6cdc90 1687 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
10bc8017
MG
1688 { constant_boolean_node (cmp == NE_EXPR, type); })))
1689
84ff66b8
AV
1690/* ((X inner_op C0) outer_op C1)
1691 With X being a tree where value_range has reasoned certain bits to always be
1692 zero throughout its computed value range,
1693 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1694 where zero_mask has 1's for all bits that are sure to be 0 in
1695 and 0's otherwise.
1696 if (inner_op == '^') C0 &= ~C1;
1697 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1698 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1699*/
1700(for inner_op (bit_ior bit_xor)
1701 outer_op (bit_xor bit_ior)
1702(simplify
1703 (outer_op
1704 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1705 (with
1706 {
1707 bool fail = false;
1708 wide_int zero_mask_not;
1709 wide_int C0;
1710 wide_int cst_emit;
1711
1712 if (TREE_CODE (@2) == SSA_NAME)
1713 zero_mask_not = get_nonzero_bits (@2);
1714 else
1715 fail = true;
1716
1717 if (inner_op == BIT_XOR_EXPR)
1718 {
8e6cdc90
RS
1719 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1720 cst_emit = C0 | wi::to_wide (@1);
84ff66b8
AV
1721 }
1722 else
1723 {
8e6cdc90
RS
1724 C0 = wi::to_wide (@0);
1725 cst_emit = C0 ^ wi::to_wide (@1);
84ff66b8
AV
1726 }
1727 }
8e6cdc90 1728 (if (!fail && (C0 & zero_mask_not) == 0)
84ff66b8 1729 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
8e6cdc90 1730 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
84ff66b8
AV
1731 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1732
a499aac5
RB
1733/* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1734(simplify
44fc0a51
RB
1735 (pointer_plus (pointer_plus:s @0 @1) @3)
1736 (pointer_plus @0 (plus @1 @3)))
a499aac5
RB
1737
1738/* Pattern match
1739 tem1 = (long) ptr1;
1740 tem2 = (long) ptr2;
1741 tem3 = tem2 - tem1;
1742 tem4 = (unsigned long) tem3;
1743 tem5 = ptr1 + tem4;
1744 and produce
1745 tem5 = ptr2; */
1746(simplify
1747 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1748 /* Conditionally look through a sign-changing conversion. */
1749 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1750 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1751 || (GENERIC && type == TREE_TYPE (@1))))
1752 @1))
1af4ebf5
MG
1753(simplify
1754 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1755 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1756 (convert @1)))
a499aac5
RB
1757
1758/* Pattern match
1759 tem = (sizetype) ptr;
1760 tem = tem & algn;
1761 tem = -tem;
1762 ... = ptr p+ tem;
1763 and produce the simpler and easier to analyze with respect to alignment
1764 ... = ptr & ~algn; */
1765(simplify
1766 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
8e6cdc90 1767 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
a499aac5
RB
1768 (bit_and @0 { algn; })))
1769
99e943a2
RB
1770/* Try folding difference of addresses. */
1771(simplify
1772 (minus (convert ADDR_EXPR@0) (convert @1))
1773 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
f37fac2b 1774 (with { poly_int64 diff; }
99e943a2
RB
1775 (if (ptr_difference_const (@0, @1, &diff))
1776 { build_int_cst_type (type, diff); }))))
1777(simplify
1778 (minus (convert @0) (convert ADDR_EXPR@1))
1779 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
f37fac2b 1780 (with { poly_int64 diff; }
99e943a2
RB
1781 (if (ptr_difference_const (@0, @1, &diff))
1782 { build_int_cst_type (type, diff); }))))
1af4ebf5 1783(simplify
67fccea4 1784 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1af4ebf5
MG
1785 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1786 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
f37fac2b 1787 (with { poly_int64 diff; }
1af4ebf5
MG
1788 (if (ptr_difference_const (@0, @1, &diff))
1789 { build_int_cst_type (type, diff); }))))
1790(simplify
67fccea4 1791 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1af4ebf5
MG
1792 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1793 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
f37fac2b 1794 (with { poly_int64 diff; }
1af4ebf5
MG
1795 (if (ptr_difference_const (@0, @1, &diff))
1796 { build_int_cst_type (type, diff); }))))
99e943a2 1797
bab73f11
RB
1798/* If arg0 is derived from the address of an object or function, we may
1799 be able to fold this expression using the object or function's
1800 alignment. */
1801(simplify
1802 (bit_and (convert? @0) INTEGER_CST@1)
1803 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1804 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1805 (with
1806 {
1807 unsigned int align;
1808 unsigned HOST_WIDE_INT bitpos;
1809 get_pointer_alignment_1 (@0, &align, &bitpos);
1810 }
8e6cdc90
RS
1811 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1812 { wide_int_to_tree (type, (wi::to_wide (@1)
1813 & (bitpos / BITS_PER_UNIT))); }))))
99e943a2 1814
a499aac5 1815
cc7b5acf
RB
1816/* We can't reassociate at all for saturating types. */
1817(if (!TYPE_SATURATING (type))
1818
1819 /* Contract negates. */
1820 /* A + (-B) -> A - B */
1821 (simplify
248179b5
RB
1822 (plus:c @0 (convert? (negate @1)))
1823 /* Apply STRIP_NOPS on the negate. */
1824 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
6a4f0678 1825 && !TYPE_OVERFLOW_SANITIZED (type))
248179b5
RB
1826 (with
1827 {
1828 tree t1 = type;
1829 if (INTEGRAL_TYPE_P (type)
1830 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1831 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1832 }
1833 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
cc7b5acf
RB
1834 /* A - (-B) -> A + B */
1835 (simplify
248179b5
RB
1836 (minus @0 (convert? (negate @1)))
1837 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
6a4f0678 1838 && !TYPE_OVERFLOW_SANITIZED (type))
248179b5
RB
1839 (with
1840 {
1841 tree t1 = type;
1842 if (INTEGRAL_TYPE_P (type)
1843 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1844 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1845 }
1846 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
63626547
MG
1847 /* -(T)(-A) -> (T)A
1848 Sign-extension is ok except for INT_MIN, which thankfully cannot
1849 happen without overflow. */
1850 (simplify
1851 (negate (convert (negate @1)))
1852 (if (INTEGRAL_TYPE_P (type)
1853 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1854 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1855 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1856 && !TYPE_OVERFLOW_SANITIZED (type)
1857 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
a0f12cf8 1858 (convert @1)))
63626547
MG
1859 (simplify
1860 (negate (convert negate_expr_p@1))
1861 (if (SCALAR_FLOAT_TYPE_P (type)
1862 && ((DECIMAL_FLOAT_TYPE_P (type)
1863 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1864 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1865 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1866 (convert (negate @1))))
1867 (simplify
1868 (negate (nop_convert (negate @1)))
1869 (if (!TYPE_OVERFLOW_SANITIZED (type)
1870 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1871 (view_convert @1)))
cc7b5acf 1872
7318e44f
RB
1873 /* We can't reassociate floating-point unless -fassociative-math
1874 or fixed-point plus or minus because of saturation to +-Inf. */
1875 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1876 && !FIXED_POINT_TYPE_P (type))
cc7b5acf
RB
1877
1878 /* Match patterns that allow contracting a plus-minus pair
1879 irrespective of overflow issues. */
1880 /* (A +- B) - A -> +- B */
1881 /* (A +- B) -+ B -> A */
1882 /* A - (A +- B) -> -+ B */
1883 /* A +- (B -+ A) -> +- B */
1884 (simplify
1885 (minus (plus:c @0 @1) @0)
1886 @1)
1887 (simplify
1888 (minus (minus @0 @1) @0)
1889 (negate @1))
1890 (simplify
1891 (plus:c (minus @0 @1) @1)
1892 @0)
1893 (simplify
1894 (minus @0 (plus:c @0 @1))
1895 (negate @1))
1896 (simplify
1897 (minus @0 (minus @0 @1))
1898 @1)
1e7df2e6
MG
1899 /* (A +- B) + (C - A) -> C +- B */
1900 /* (A + B) - (A - C) -> B + C */
1901 /* More cases are handled with comparisons. */
1902 (simplify
1903 (plus:c (plus:c @0 @1) (minus @2 @0))
1904 (plus @2 @1))
1905 (simplify
1906 (plus:c (minus @0 @1) (minus @2 @0))
1907 (minus @2 @1))
1af4ebf5
MG
1908 (simplify
1909 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1910 (if (TYPE_OVERFLOW_UNDEFINED (type)
1911 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1912 (pointer_diff @2 @1)))
1e7df2e6
MG
1913 (simplify
1914 (minus (plus:c @0 @1) (minus @0 @2))
1915 (plus @1 @2))
cc7b5acf 1916
ed73f46f
MG
1917 /* (A +- CST1) +- CST2 -> A + CST3
1918 Use view_convert because it is safe for vectors and equivalent for
1919 scalars. */
cc7b5acf
RB
1920 (for outer_op (plus minus)
1921 (for inner_op (plus minus)
ed73f46f 1922 neg_inner_op (minus plus)
cc7b5acf 1923 (simplify
ed73f46f
MG
1924 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1925 CONSTANT_CLASS_P@2)
1926 /* If one of the types wraps, use that one. */
1927 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3eb1eecf
JJ
1928 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1929 forever if something doesn't simplify into a constant. */
1930 (if (!CONSTANT_CLASS_P (@0))
1931 (if (outer_op == PLUS_EXPR)
1932 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1933 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
ed73f46f
MG
1934 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1935 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1936 (if (outer_op == PLUS_EXPR)
1937 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1938 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1939 /* If the constant operation overflows we cannot do the transform
1940 directly as we would introduce undefined overflow, for example
1941 with (a - 1) + INT_MIN. */
1942 (if (types_match (type, @0))
1943 (with { tree cst = const_binop (outer_op == inner_op
1944 ? PLUS_EXPR : MINUS_EXPR,
1945 type, @1, @2); }
1946 (if (cst && !TREE_OVERFLOW (cst))
1947 (inner_op @0 { cst; } )
1948 /* X+INT_MAX+1 is X-INT_MIN. */
1949 (if (INTEGRAL_TYPE_P (type) && cst
8e6cdc90
RS
1950 && wi::to_wide (cst) == wi::min_value (type))
1951 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
ed73f46f
MG
1952 /* Last resort, use some unsigned type. */
1953 (with { tree utype = unsigned_type_for (type); }
48fcd201
JJ
1954 (if (utype)
1955 (view_convert (inner_op
1956 (view_convert:utype @0)
1957 (view_convert:utype
1958 { drop_tree_overflow (cst); }))))))))))))))
cc7b5acf 1959
b302f2e0 1960 /* (CST1 - A) +- CST2 -> CST3 - A */
cc7b5acf
RB
1961 (for outer_op (plus minus)
1962 (simplify
1963 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
23f27839 1964 (with { tree cst = const_binop (outer_op, type, @1, @2); }
cc7b5acf
RB
1965 (if (cst && !TREE_OVERFLOW (cst))
1966 (minus { cst; } @0)))))
1967
b302f2e0
RB
1968 /* CST1 - (CST2 - A) -> CST3 + A */
1969 (simplify
1970 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1971 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1972 (if (cst && !TREE_OVERFLOW (cst))
1973 (plus { cst; } @0))))
1974
cc7b5acf
RB
1975 /* ~A + A -> -1 */
1976 (simplify
1977 (plus:c (bit_not @0) @0)
1978 (if (!TYPE_OVERFLOW_TRAPS (type))
1979 { build_all_ones_cst (type); }))
1980
1981 /* ~A + 1 -> -A */
1982 (simplify
e19740ae
RB
1983 (plus (convert? (bit_not @0)) integer_each_onep)
1984 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1985 (negate (convert @0))))
1986
1987 /* -A - 1 -> ~A */
1988 (simplify
1989 (minus (convert? (negate @0)) integer_each_onep)
1990 (if (!TYPE_OVERFLOW_TRAPS (type)
1991 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1992 (bit_not (convert @0))))
1993
1994 /* -1 - A -> ~A */
1995 (simplify
1996 (minus integer_all_onesp @0)
bc4315fb 1997 (bit_not @0))
cc7b5acf
RB
1998
1999 /* (T)(P + A) - (T)P -> (T) A */
d7f44d4d 2000 (simplify
a72610d4
JJ
2001 (minus (convert (plus:c @@0 @1))
2002 (convert? @0))
d7f44d4d
JJ
2003 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2004 /* For integer types, if A has a smaller type
2005 than T the result depends on the possible
2006 overflow in P + A.
2007 E.g. T=size_t, A=(unsigned)429497295, P>0.
2008 However, if an overflow in P + A would cause
2009 undefined behavior, we can assume that there
2010 is no overflow. */
a72610d4
JJ
2011 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2012 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
d7f44d4d
JJ
2013 (convert @1)))
2014 (simplify
2015 (minus (convert (pointer_plus @@0 @1))
2016 (convert @0))
2017 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2018 /* For pointer types, if the conversion of A to the
2019 final type requires a sign- or zero-extension,
2020 then we have to punt - it is not defined which
2021 one is correct. */
2022 || (POINTER_TYPE_P (TREE_TYPE (@0))
2023 && TREE_CODE (@1) == INTEGER_CST
2024 && tree_int_cst_sign_bit (@1) == 0))
2025 (convert @1)))
1af4ebf5
MG
2026 (simplify
2027 (pointer_diff (pointer_plus @@0 @1) @0)
2028 /* The second argument of pointer_plus must be interpreted as signed, and
2029 thus sign-extended if necessary. */
2030 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
8ae43881
JJ
2031 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2032 second arg is unsigned even when we need to consider it as signed,
2033 we don't want to diagnose overflow here. */
2034 (convert (view_convert:stype @1))))
a8fc2579
RB
2035
2036 /* (T)P - (T)(P + A) -> -(T) A */
d7f44d4d 2037 (simplify
a72610d4
JJ
2038 (minus (convert? @0)
2039 (convert (plus:c @@0 @1)))
d7f44d4d
JJ
2040 (if (INTEGRAL_TYPE_P (type)
2041 && TYPE_OVERFLOW_UNDEFINED (type)
2042 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2043 (with { tree utype = unsigned_type_for (type); }
2044 (convert (negate (convert:utype @1))))
a8fc2579
RB
2045 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2046 /* For integer types, if A has a smaller type
2047 than T the result depends on the possible
2048 overflow in P + A.
2049 E.g. T=size_t, A=(unsigned)429497295, P>0.
2050 However, if an overflow in P + A would cause
2051 undefined behavior, we can assume that there
2052 is no overflow. */
a72610d4
JJ
2053 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2054 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
d7f44d4d
JJ
2055 (negate (convert @1)))))
2056 (simplify
2057 (minus (convert @0)
2058 (convert (pointer_plus @@0 @1)))
2059 (if (INTEGRAL_TYPE_P (type)
2060 && TYPE_OVERFLOW_UNDEFINED (type)
2061 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2062 (with { tree utype = unsigned_type_for (type); }
2063 (convert (negate (convert:utype @1))))
2064 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
a8fc2579
RB
2065 /* For pointer types, if the conversion of A to the
2066 final type requires a sign- or zero-extension,
2067 then we have to punt - it is not defined which
2068 one is correct. */
2069 || (POINTER_TYPE_P (TREE_TYPE (@0))
2070 && TREE_CODE (@1) == INTEGER_CST
2071 && tree_int_cst_sign_bit (@1) == 0))
2072 (negate (convert @1)))))
1af4ebf5
MG
2073 (simplify
2074 (pointer_diff @0 (pointer_plus @@0 @1))
2075 /* The second argument of pointer_plus must be interpreted as signed, and
2076 thus sign-extended if necessary. */
2077 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
8ae43881
JJ
2078 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2079 second arg is unsigned even when we need to consider it as signed,
2080 we don't want to diagnose overflow here. */
2081 (negate (convert (view_convert:stype @1)))))
a8fc2579
RB
2082
2083 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
d7f44d4d 2084 (simplify
a72610d4 2085 (minus (convert (plus:c @@0 @1))
d7f44d4d
JJ
2086 (convert (plus:c @0 @2)))
2087 (if (INTEGRAL_TYPE_P (type)
2088 && TYPE_OVERFLOW_UNDEFINED (type)
a72610d4
JJ
2089 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2090 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
d7f44d4d
JJ
2091 (with { tree utype = unsigned_type_for (type); }
2092 (convert (minus (convert:utype @1) (convert:utype @2))))
a72610d4
JJ
2093 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2094 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2095 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2096 /* For integer types, if A has a smaller type
2097 than T the result depends on the possible
2098 overflow in P + A.
2099 E.g. T=size_t, A=(unsigned)429497295, P>0.
2100 However, if an overflow in P + A would cause
2101 undefined behavior, we can assume that there
2102 is no overflow. */
2103 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2104 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2105 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2106 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
d7f44d4d
JJ
2107 (minus (convert @1) (convert @2)))))
2108 (simplify
2109 (minus (convert (pointer_plus @@0 @1))
2110 (convert (pointer_plus @0 @2)))
2111 (if (INTEGRAL_TYPE_P (type)
2112 && TYPE_OVERFLOW_UNDEFINED (type)
2113 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2114 (with { tree utype = unsigned_type_for (type); }
2115 (convert (minus (convert:utype @1) (convert:utype @2))))
2116 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
a8fc2579
RB
2117 /* For pointer types, if the conversion of A to the
2118 final type requires a sign- or zero-extension,
2119 then we have to punt - it is not defined which
2120 one is correct. */
2121 || (POINTER_TYPE_P (TREE_TYPE (@0))
2122 && TREE_CODE (@1) == INTEGER_CST
2123 && tree_int_cst_sign_bit (@1) == 0
2124 && TREE_CODE (@2) == INTEGER_CST
2125 && tree_int_cst_sign_bit (@2) == 0))
d7f44d4d 2126 (minus (convert @1) (convert @2)))))
1af4ebf5
MG
2127 (simplify
2128 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2129 /* The second argument of pointer_plus must be interpreted as signed, and
2130 thus sign-extended if necessary. */
2131 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
8ae43881
JJ
2132 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2133 second arg is unsigned even when we need to consider it as signed,
2134 we don't want to diagnose overflow here. */
2135 (minus (convert (view_convert:stype @1))
2136 (convert (view_convert:stype @2)))))))
cc7b5acf 2137
5b55e6e3
RB
2138/* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2139 Modeled after fold_plusminus_mult_expr. */
2140(if (!TYPE_SATURATING (type)
2141 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2142 (for plusminus (plus minus)
2143 (simplify
c1bbe5b3
RB
2144 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2145 (if ((!ANY_INTEGRAL_TYPE_P (type)
5b55e6e3
RB
2146 || TYPE_OVERFLOW_WRAPS (type)
2147 || (INTEGRAL_TYPE_P (type)
2148 && tree_expr_nonzero_p (@0)
2149 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
c1bbe5b3
RB
2150 /* If @1 +- @2 is constant require a hard single-use on either
2151 original operand (but not on both). */
2152 && (single_use (@3) || single_use (@4)))
2153 (mult (plusminus @1 @2) @0)))
2154 /* We cannot generate constant 1 for fract. */
2155 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2156 (simplify
2157 (plusminus @0 (mult:c@3 @0 @2))
2158 (if ((!ANY_INTEGRAL_TYPE_P (type)
2159 || TYPE_OVERFLOW_WRAPS (type)
2160 || (INTEGRAL_TYPE_P (type)
2161 && tree_expr_nonzero_p (@0)
2162 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2163 && single_use (@3))
5b55e6e3
RB
2164 (mult (plusminus { build_one_cst (type); } @2) @0)))
2165 (simplify
c1bbe5b3
RB
2166 (plusminus (mult:c@3 @0 @2) @0)
2167 (if ((!ANY_INTEGRAL_TYPE_P (type)
2168 || TYPE_OVERFLOW_WRAPS (type)
2169 || (INTEGRAL_TYPE_P (type)
2170 && tree_expr_nonzero_p (@0)
2171 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2172 && single_use (@3))
5b55e6e3 2173 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
cc7b5acf 2174
0122e8e5 2175/* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
a7f24614 2176
c6cfa2bf 2177(for minmax (min max FMIN_ALL FMAX_ALL)
a7f24614
RB
2178 (simplify
2179 (minmax @0 @0)
2180 @0))
4a334cba
RS
2181/* min(max(x,y),y) -> y. */
2182(simplify
2183 (min:c (max:c @0 @1) @1)
2184 @1)
2185/* max(min(x,y),y) -> y. */
2186(simplify
2187 (max:c (min:c @0 @1) @1)
2188 @1)
d657e995
RB
2189/* max(a,-a) -> abs(a). */
2190(simplify
2191 (max:c @0 (negate @0))
2192 (if (TREE_CODE (type) != COMPLEX_TYPE
2193 && (! ANY_INTEGRAL_TYPE_P (type)
2194 || TYPE_OVERFLOW_UNDEFINED (type)))
2195 (abs @0)))
54f84ca9
RB
2196/* min(a,-a) -> -abs(a). */
2197(simplify
2198 (min:c @0 (negate @0))
2199 (if (TREE_CODE (type) != COMPLEX_TYPE
2200 && (! ANY_INTEGRAL_TYPE_P (type)
2201 || TYPE_OVERFLOW_UNDEFINED (type)))
2202 (negate (abs @0))))
a7f24614
RB
2203(simplify
2204 (min @0 @1)
2c2870a1
MG
2205 (switch
2206 (if (INTEGRAL_TYPE_P (type)
2207 && TYPE_MIN_VALUE (type)
2208 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2209 @1)
2210 (if (INTEGRAL_TYPE_P (type)
2211 && TYPE_MAX_VALUE (type)
2212 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2213 @0)))
a7f24614
RB
2214(simplify
2215 (max @0 @1)
2c2870a1
MG
2216 (switch
2217 (if (INTEGRAL_TYPE_P (type)
2218 && TYPE_MAX_VALUE (type)
2219 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2220 @1)
2221 (if (INTEGRAL_TYPE_P (type)
2222 && TYPE_MIN_VALUE (type)
2223 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2224 @0)))
ad6e4ba8 2225
182f37c9
N
2226/* max (a, a + CST) -> a + CST where CST is positive. */
2227/* max (a, a + CST) -> a where CST is negative. */
2228(simplify
2229 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2230 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2231 (if (tree_int_cst_sgn (@1) > 0)
2232 @2
2233 @0)))
2234
2235/* min (a, a + CST) -> a where CST is positive. */
2236/* min (a, a + CST) -> a + CST where CST is negative. */
2237(simplify
2238 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2239 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2240 (if (tree_int_cst_sgn (@1) > 0)
2241 @0
2242 @2)))
2243
ad6e4ba8
BC
2244/* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2245 and the outer convert demotes the expression back to x's type. */
2246(for minmax (min max)
2247 (simplify
2248 (convert (minmax@0 (convert @1) INTEGER_CST@2))
ebf41734
BC
2249 (if (INTEGRAL_TYPE_P (type)
2250 && types_match (@1, type) && int_fits_type_p (@2, type)
ad6e4ba8
BC
2251 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2252 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2253 (minmax @1 (convert @2)))))
2254
c6cfa2bf 2255(for minmax (FMIN_ALL FMAX_ALL)
0122e8e5
RS
2256 /* If either argument is NaN, return the other one. Avoid the
2257 transformation if we get (and honor) a signalling NaN. */
2258 (simplify
2259 (minmax:c @0 REAL_CST@1)
2260 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2261 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2262 @0)))
2263/* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2264 functions to return the numeric arg if the other one is NaN.
2265 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2266 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2267 worry about it either. */
2268(if (flag_finite_math_only)
2269 (simplify
c6cfa2bf 2270 (FMIN_ALL @0 @1)
0122e8e5 2271 (min @0 @1))
4119b2eb 2272 (simplify
c6cfa2bf 2273 (FMAX_ALL @0 @1)
0122e8e5 2274 (max @0 @1)))
ce0e66ff 2275/* min (-A, -B) -> -max (A, B) */
c6cfa2bf
MM
2276(for minmax (min max FMIN_ALL FMAX_ALL)
2277 maxmin (max min FMAX_ALL FMIN_ALL)
ce0e66ff
MG
2278 (simplify
2279 (minmax (negate:s@2 @0) (negate:s@3 @1))
2280 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2281 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2282 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2283 (negate (maxmin @0 @1)))))
2284/* MIN (~X, ~Y) -> ~MAX (X, Y)
2285 MAX (~X, ~Y) -> ~MIN (X, Y) */
2286(for minmax (min max)
2287 maxmin (max min)
2288 (simplify
2289 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2290 (bit_not (maxmin @0 @1))))
a7f24614 2291
b4817bd6
MG
2292/* MIN (X, Y) == X -> X <= Y */
2293(for minmax (min min max max)
2294 cmp (eq ne eq ne )
2295 out (le gt ge lt )
2296 (simplify
2297 (cmp:c (minmax:c @0 @1) @0)
2298 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2299 (out @0 @1))))
2300/* MIN (X, 5) == 0 -> X == 0
2301 MIN (X, 5) == 7 -> false */
2302(for cmp (eq ne)
2303 (simplify
2304 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
8e6cdc90
RS
2305 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2306 TYPE_SIGN (TREE_TYPE (@0))))
b4817bd6 2307 { constant_boolean_node (cmp == NE_EXPR, type); }
8e6cdc90
RS
2308 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2309 TYPE_SIGN (TREE_TYPE (@0))))
b4817bd6
MG
2310 (cmp @0 @2)))))
2311(for cmp (eq ne)
2312 (simplify
2313 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
8e6cdc90
RS
2314 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2315 TYPE_SIGN (TREE_TYPE (@0))))
b4817bd6 2316 { constant_boolean_node (cmp == NE_EXPR, type); }
8e6cdc90
RS
2317 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2318 TYPE_SIGN (TREE_TYPE (@0))))
b4817bd6
MG
2319 (cmp @0 @2)))))
2320/* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2321(for minmax (min min max max min min max max )
2322 cmp (lt le gt ge gt ge lt le )
2323 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2324 (simplify
2325 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2326 (comb (cmp @0 @2) (cmp @1 @2))))
2327
a7f24614
RB
2328/* Simplifications of shift and rotates. */
2329
2330(for rotate (lrotate rrotate)
2331 (simplify
2332 (rotate integer_all_onesp@0 @1)
2333 @0))
2334
2335/* Optimize -1 >> x for arithmetic right shifts. */
2336(simplify
2337 (rshift integer_all_onesp@0 @1)
2338 (if (!TYPE_UNSIGNED (type)
2339 && tree_expr_nonnegative_p (@1))
2340 @0))
2341
12085390
N
2342/* Optimize (x >> c) << c into x & (-1<<c). */
2343(simplify
2344 (lshift (rshift @0 INTEGER_CST@1) @1)
8e6cdc90 2345 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
12085390
N
2346 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2347
2348/* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2349 types. */
2350(simplify
2351 (rshift (lshift @0 INTEGER_CST@1) @1)
2352 (if (TYPE_UNSIGNED (type)
8e6cdc90 2353 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
12085390
N
2354 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2355
a7f24614
RB
2356(for shiftrotate (lrotate rrotate lshift rshift)
2357 (simplify
2358 (shiftrotate @0 integer_zerop)
2359 (non_lvalue @0))
2360 (simplify
2361 (shiftrotate integer_zerop@0 @1)
2362 @0)
2363 /* Prefer vector1 << scalar to vector1 << vector2
2364 if vector2 is uniform. */
2365 (for vec (VECTOR_CST CONSTRUCTOR)
2366 (simplify
2367 (shiftrotate @0 vec@1)
2368 (with { tree tem = uniform_vector_p (@1); }
2369 (if (tem)
2370 (shiftrotate @0 { tem; }))))))
2371
165ba2e9
JJ
2372/* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2373 Y is 0. Similarly for X >> Y. */
2374#if GIMPLE
2375(for shift (lshift rshift)
2376 (simplify
2377 (shift @0 SSA_NAME@1)
2378 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2379 (with {
2380 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2381 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2382 }
2383 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2384 @0)))))
2385#endif
2386
a7f24614
RB
2387/* Rewrite an LROTATE_EXPR by a constant into an
2388 RROTATE_EXPR by a new constant. */
2389(simplify
2390 (lrotate @0 INTEGER_CST@1)
23f27839 2391 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
a7f24614
RB
2392 build_int_cst (TREE_TYPE (@1),
2393 element_precision (type)), @1); }))
2394
14ea9f92
RB
2395/* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2396(for op (lrotate rrotate rshift lshift)
2397 (simplify
2398 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2399 (with { unsigned int prec = element_precision (type); }
8e6cdc90
RS
2400 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2401 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2402 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2403 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
a1488398
RS
2404 (with { unsigned int low = (tree_to_uhwi (@1)
2405 + tree_to_uhwi (@2)); }
14ea9f92
RB
2406 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2407 being well defined. */
2408 (if (low >= prec)
2409 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
8fdc6c67 2410 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
50301115 2411 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
8fdc6c67
RB
2412 { build_zero_cst (type); }
2413 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2414 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
14ea9f92
RB
2415
2416
01ada710
MP
2417/* ((1 << A) & 1) != 0 -> A == 0
2418 ((1 << A) & 1) == 0 -> A != 0 */
2419(for cmp (ne eq)
2420 icmp (eq ne)
2421 (simplify
2422 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2423 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
cc7b5acf 2424
f2e609c3
MP
2425/* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2426 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2427 if CST2 != 0. */
2428(for cmp (ne eq)
2429 (simplify
2430 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
8e6cdc90 2431 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
f2e609c3
MP
2432 (if (cand < 0
2433 || (!integer_zerop (@2)
8e6cdc90 2434 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
8fdc6c67
RB
2435 { constant_boolean_node (cmp == NE_EXPR, type); }
2436 (if (!integer_zerop (@2)
8e6cdc90 2437 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
8fdc6c67 2438 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
f2e609c3 2439
1ffbaa3f
RB
2440/* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2441 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2442 if the new mask might be further optimized. */
2443(for shift (lshift rshift)
2444 (simplify
44fc0a51
RB
2445 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2446 INTEGER_CST@2)
1ffbaa3f
RB
2447 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2448 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2449 && tree_fits_uhwi_p (@1)
2450 && tree_to_uhwi (@1) > 0
2451 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2452 (with
2453 {
2454 unsigned int shiftc = tree_to_uhwi (@1);
2455 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2456 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2457 tree shift_type = TREE_TYPE (@3);
2458 unsigned int prec;
2459
2460 if (shift == LSHIFT_EXPR)
fecfbfa4 2461 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1ffbaa3f 2462 else if (shift == RSHIFT_EXPR
2be65d9e 2463 && type_has_mode_precision_p (shift_type))
1ffbaa3f
RB
2464 {
2465 prec = TYPE_PRECISION (TREE_TYPE (@3));
2466 tree arg00 = @0;
2467 /* See if more bits can be proven as zero because of
2468 zero extension. */
2469 if (@3 != @0
2470 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2471 {
2472 tree inner_type = TREE_TYPE (@0);
2be65d9e 2473 if (type_has_mode_precision_p (inner_type)
1ffbaa3f
RB
2474 && TYPE_PRECISION (inner_type) < prec)
2475 {
2476 prec = TYPE_PRECISION (inner_type);
2477 /* See if we can shorten the right shift. */
2478 if (shiftc < prec)
2479 shift_type = inner_type;
2480 /* Otherwise X >> C1 is all zeros, so we'll optimize
2481 it into (X, 0) later on by making sure zerobits
2482 is all ones. */
2483 }
2484 }
dd4786fe 2485 zerobits = HOST_WIDE_INT_M1U;
1ffbaa3f
RB
2486 if (shiftc < prec)
2487 {
2488 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2489 zerobits <<= prec - shiftc;
2490 }
2491 /* For arithmetic shift if sign bit could be set, zerobits
2492 can contain actually sign bits, so no transformation is
2493 possible, unless MASK masks them all away. In that
2494 case the shift needs to be converted into logical shift. */
2495 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2496 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2497 {
2498 if ((mask & zerobits) == 0)
2499 shift_type = unsigned_type_for (TREE_TYPE (@3));
2500 else
2501 zerobits = 0;
2502 }
2503 }
2504 }
2505 /* ((X << 16) & 0xff00) is (X, 0). */
2506 (if ((mask & zerobits) == mask)
8fdc6c67
RB
2507 { build_int_cst (type, 0); }
2508 (with { newmask = mask | zerobits; }
2509 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2510 (with
2511 {
2512 /* Only do the transformation if NEWMASK is some integer
2513 mode's mask. */
2514 for (prec = BITS_PER_UNIT;
2515 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
fecfbfa4 2516 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
8fdc6c67
RB
2517 break;
2518 }
2519 (if (prec < HOST_BITS_PER_WIDE_INT
dd4786fe 2520 || newmask == HOST_WIDE_INT_M1U)
8fdc6c67
RB
2521 (with
2522 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2523 (if (!tree_int_cst_equal (newmaskt, @2))
2524 (if (shift_type != TREE_TYPE (@3))
2525 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2526 (bit_and @4 { newmaskt; })))))))))))))
1ffbaa3f 2527
84ff66b8
AV
2528/* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2529 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
98e30e51 2530(for shift (lshift rshift)
84ff66b8
AV
2531 (for bit_op (bit_and bit_xor bit_ior)
2532 (simplify
2533 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2534 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2535 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2536 (bit_op (shift (convert @0) @1) { mask; }))))))
98e30e51 2537
ad1d92ab
MM
2538/* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2539(simplify
2540 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2541 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
ece46666
MG
2542 && (element_precision (TREE_TYPE (@0))
2543 <= element_precision (TREE_TYPE (@1))
2544 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
ad1d92ab
MM
2545 (with
2546 { tree shift_type = TREE_TYPE (@0); }
2547 (convert (rshift (convert:shift_type @1) @2)))))
2548
2549/* ~(~X >>r Y) -> X >>r Y
2550 ~(~X <<r Y) -> X <<r Y */
2551(for rotate (lrotate rrotate)
2552 (simplify
2553 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
ece46666
MG
2554 (if ((element_precision (TREE_TYPE (@0))
2555 <= element_precision (TREE_TYPE (@1))
2556 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2557 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2558 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
ad1d92ab
MM
2559 (with
2560 { tree rotate_type = TREE_TYPE (@0); }
2561 (convert (rotate (convert:rotate_type @1) @2))))))
98e30e51 2562
d4573ffe
RB
2563/* Simplifications of conversions. */
2564
2565/* Basic strip-useless-type-conversions / strip_nops. */
f3582e54 2566(for cvt (convert view_convert float fix_trunc)
d4573ffe
RB
2567 (simplify
2568 (cvt @0)
2569 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2570 || (GENERIC && type == TREE_TYPE (@0)))
2571 @0)))
2572
2573/* Contract view-conversions. */
2574(simplify
2575 (view_convert (view_convert @0))
2576 (view_convert @0))
2577
2578/* For integral conversions with the same precision or pointer
2579 conversions use a NOP_EXPR instead. */
2580(simplify
2581 (view_convert @0)
2582 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2583 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2584 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2585 (convert @0)))
2586
bce8ef71
MG
2587/* Strip inner integral conversions that do not change precision or size, or
2588 zero-extend while keeping the same size (for bool-to-char). */
d4573ffe
RB
2589(simplify
2590 (view_convert (convert@0 @1))
2591 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2592 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
bce8ef71
MG
2593 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2594 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2595 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2596 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
d4573ffe
RB
2597 (view_convert @1)))
2598
2599/* Re-association barriers around constants and other re-association
2600 barriers can be removed. */
2601(simplify
2602 (paren CONSTANT_CLASS_P@0)
2603 @0)
2604(simplify
2605 (paren (paren@1 @0))
2606 @1)
1e51d0a2
RB
2607
2608/* Handle cases of two conversions in a row. */
2609(for ocvt (convert float fix_trunc)
2610 (for icvt (convert float)
2611 (simplify
2612 (ocvt (icvt@1 @0))
2613 (with
2614 {
2615 tree inside_type = TREE_TYPE (@0);
2616 tree inter_type = TREE_TYPE (@1);
2617 int inside_int = INTEGRAL_TYPE_P (inside_type);
2618 int inside_ptr = POINTER_TYPE_P (inside_type);
2619 int inside_float = FLOAT_TYPE_P (inside_type);
09240451 2620 int inside_vec = VECTOR_TYPE_P (inside_type);
1e51d0a2
RB
2621 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2622 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2623 int inter_int = INTEGRAL_TYPE_P (inter_type);
2624 int inter_ptr = POINTER_TYPE_P (inter_type);
2625 int inter_float = FLOAT_TYPE_P (inter_type);
09240451 2626 int inter_vec = VECTOR_TYPE_P (inter_type);
1e51d0a2
RB
2627 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2628 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2629 int final_int = INTEGRAL_TYPE_P (type);
2630 int final_ptr = POINTER_TYPE_P (type);
2631 int final_float = FLOAT_TYPE_P (type);
09240451 2632 int final_vec = VECTOR_TYPE_P (type);
1e51d0a2
RB
2633 unsigned int final_prec = TYPE_PRECISION (type);
2634 int final_unsignedp = TYPE_UNSIGNED (type);
2635 }
64d3a1f0
RB
2636 (switch
2637 /* In addition to the cases of two conversions in a row
2638 handled below, if we are converting something to its own
2639 type via an object of identical or wider precision, neither
2640 conversion is needed. */
2641 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2642 || (GENERIC
2643 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2644 && (((inter_int || inter_ptr) && final_int)
2645 || (inter_float && final_float))
2646 && inter_prec >= final_prec)
2647 (ocvt @0))
2648
2649 /* Likewise, if the intermediate and initial types are either both
2650 float or both integer, we don't need the middle conversion if the
2651 former is wider than the latter and doesn't change the signedness
2652 (for integers). Avoid this if the final type is a pointer since
36088299 2653 then we sometimes need the middle conversion. */
64d3a1f0
RB
2654 (if (((inter_int && inside_int) || (inter_float && inside_float))
2655 && (final_int || final_float)
2656 && inter_prec >= inside_prec
36088299 2657 && (inter_float || inter_unsignedp == inside_unsignedp))
64d3a1f0
RB
2658 (ocvt @0))
2659
2660 /* If we have a sign-extension of a zero-extended value, we can
2661 replace that by a single zero-extension. Likewise if the
2662 final conversion does not change precision we can drop the
2663 intermediate conversion. */
2664 (if (inside_int && inter_int && final_int
2665 && ((inside_prec < inter_prec && inter_prec < final_prec
2666 && inside_unsignedp && !inter_unsignedp)
2667 || final_prec == inter_prec))
2668 (ocvt @0))
2669
2670 /* Two conversions in a row are not needed unless:
1e51d0a2
RB
2671 - some conversion is floating-point (overstrict for now), or
2672 - some conversion is a vector (overstrict for now), or
2673 - the intermediate type is narrower than both initial and
2674 final, or
2675 - the intermediate type and innermost type differ in signedness,
2676 and the outermost type is wider than the intermediate, or
2677 - the initial type is a pointer type and the precisions of the
2678 intermediate and final types differ, or
2679 - the final type is a pointer type and the precisions of the
2680 initial and intermediate types differ. */
64d3a1f0
RB
2681 (if (! inside_float && ! inter_float && ! final_float
2682 && ! inside_vec && ! inter_vec && ! final_vec
2683 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2684 && ! (inside_int && inter_int
2685 && inter_unsignedp != inside_unsignedp
2686 && inter_prec < final_prec)
2687 && ((inter_unsignedp && inter_prec > inside_prec)
2688 == (final_unsignedp && final_prec > inter_prec))
2689 && ! (inside_ptr && inter_prec != final_prec)
36088299 2690 && ! (final_ptr && inside_prec != inter_prec))
64d3a1f0
RB
2691 (ocvt @0))
2692
2693 /* A truncation to an unsigned type (a zero-extension) should be
2694 canonicalized as bitwise and of a mask. */
1d510e04
JJ
2695 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2696 && final_int && inter_int && inside_int
64d3a1f0
RB
2697 && final_prec == inside_prec
2698 && final_prec > inter_prec
2699 && inter_unsignedp)
2700 (convert (bit_and @0 { wide_int_to_tree
2701 (inside_type,
2702 wi::mask (inter_prec, false,
2703 TYPE_PRECISION (inside_type))); })))
2704
2705 /* If we are converting an integer to a floating-point that can
2706 represent it exactly and back to an integer, we can skip the
2707 floating-point conversion. */
2708 (if (GIMPLE /* PR66211 */
2709 && inside_int && inter_float && final_int &&
2710 (unsigned) significand_size (TYPE_MODE (inter_type))
2711 >= inside_prec - !inside_unsignedp)
2712 (convert @0)))))))
ea2042ba
RB
2713
2714/* If we have a narrowing conversion to an integral type that is fed by a
2715 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2716 masks off bits outside the final type (and nothing else). */
2717(simplify
2718 (convert (bit_and @0 INTEGER_CST@1))
2719 (if (INTEGRAL_TYPE_P (type)
2720 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2721 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2722 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2723 TYPE_PRECISION (type)), 0))
2724 (convert @0)))
a25454ea
RB
2725
2726
2727/* (X /[ex] A) * A -> X. */
2728(simplify
2eef1fc1
RB
2729 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2730 (convert @0))
eaeba53a 2731
0036218b
MG
2732/* ((X /[ex] A) +- B) * A --> X +- A * B. */
2733(for op (plus minus)
2734 (simplify
2735 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
2736 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
2737 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
2738 (with
2739 {
2740 wi::overflow_type overflow;
2741 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
2742 TYPE_SIGN (type), &overflow);
2743 }
2744 (if (types_match (type, TREE_TYPE (@2))
2745 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
2746 (op @0 { wide_int_to_tree (type, mul); })
2747 (with { tree utype = unsigned_type_for (type); }
2748 (convert (op (convert:utype @0)
2749 (mult (convert:utype @1) (convert:utype @2))))))))))
2750
a7f24614
RB
2751/* Canonicalization of binary operations. */
2752
2753/* Convert X + -C into X - C. */
2754(simplify
2755 (plus @0 REAL_CST@1)
2756 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
23f27839 2757 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
a7f24614
RB
2758 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2759 (minus @0 { tem; })))))
2760
6b6aa8d3 2761/* Convert x+x into x*2. */
a7f24614
RB
2762(simplify
2763 (plus @0 @0)
2764 (if (SCALAR_FLOAT_TYPE_P (type))
6b6aa8d3
MG
2765 (mult @0 { build_real (type, dconst2); })
2766 (if (INTEGRAL_TYPE_P (type))
2767 (mult @0 { build_int_cst (type, 2); }))))
a7f24614 2768
406520e2 2769/* 0 - X -> -X. */
a7f24614
RB
2770(simplify
2771 (minus integer_zerop @1)
2772 (negate @1))
406520e2
MG
2773(simplify
2774 (pointer_diff integer_zerop @1)
2775 (negate (convert @1)))
a7f24614
RB
2776
2777/* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2778 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2779 (-ARG1 + ARG0) reduces to -ARG1. */
2780(simplify
2781 (minus real_zerop@0 @1)
2782 (if (fold_real_zero_addition_p (type, @0, 0))
2783 (negate @1)))
2784
2785/* Transform x * -1 into -x. */
2786(simplify
2787 (mult @0 integer_minus_onep)
2788 (negate @0))
eaeba53a 2789
b771c609
AM
2790/* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2791 signed overflow for CST != 0 && CST != -1. */
2792(simplify
b46ebc6c 2793 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
b771c609 2794 (if (TREE_CODE (@2) != INTEGER_CST
b46ebc6c 2795 && single_use (@3)
b771c609
AM
2796 && !integer_zerop (@1) && !integer_minus_onep (@1))
2797 (mult (mult @0 @2) @1)))
2798
96285749
RS
2799/* True if we can easily extract the real and imaginary parts of a complex
2800 number. */
2801(match compositional_complex
2802 (convert? (complex @0 @1)))
2803
eaeba53a
RB
2804/* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2805(simplify
2806 (complex (realpart @0) (imagpart @0))
2807 @0)
2808(simplify
2809 (realpart (complex @0 @1))
2810 @0)
2811(simplify
2812 (imagpart (complex @0 @1))
2813 @1)
83633539 2814
77c028c5
MG
2815/* Sometimes we only care about half of a complex expression. */
2816(simplify
2817 (realpart (convert?:s (conj:s @0)))
2818 (convert (realpart @0)))
2819(simplify
2820 (imagpart (convert?:s (conj:s @0)))
2821 (convert (negate (imagpart @0))))
2822(for part (realpart imagpart)
2823 (for op (plus minus)
2824 (simplify
2825 (part (convert?:s@2 (op:s @0 @1)))
2826 (convert (op (part @0) (part @1))))))
2827(simplify
2828 (realpart (convert?:s (CEXPI:s @0)))
2829 (convert (COS @0)))
2830(simplify
2831 (imagpart (convert?:s (CEXPI:s @0)))
2832 (convert (SIN @0)))
2833
2834/* conj(conj(x)) -> x */
2835(simplify
2836 (conj (convert? (conj @0)))
2837 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2838 (convert @0)))
2839
2840/* conj({x,y}) -> {x,-y} */
2841(simplify
2842 (conj (convert?:s (complex:s @0 @1)))
2843 (with { tree itype = TREE_TYPE (type); }
2844 (complex (convert:itype @0) (negate (convert:itype @1)))))
83633539
RB
2845
2846/* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2847(for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2848 (simplify
2849 (bswap (bswap @0))
2850 @0)
2851 (simplify
2852 (bswap (bit_not (bswap @0)))
2853 (bit_not @0))
2854 (for bitop (bit_xor bit_ior bit_and)
2855 (simplify
2856 (bswap (bitop:c (bswap @0) @1))
2857 (bitop @0 (bswap @1)))))
96994de0
RB
2858
2859
2860/* Combine COND_EXPRs and VEC_COND_EXPRs. */
2861
2862/* Simplify constant conditions.
2863 Only optimize constant conditions when the selected branch
2864 has the same type as the COND_EXPR. This avoids optimizing
2865 away "c ? x : throw", where the throw has a void type.
2866 Note that we cannot throw away the fold-const.c variant nor
2867 this one as we depend on doing this transform before possibly
2868 A ? B : B -> B triggers and the fold-const.c one can optimize
2869 0 ? A : B to B even if A has side-effects. Something
2870 genmatch cannot handle. */
2871(simplify
2872 (cond INTEGER_CST@0 @1 @2)
8fdc6c67
RB
2873 (if (integer_zerop (@0))
2874 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2875 @2)
2876 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2877 @1)))
96994de0
RB
2878(simplify
2879 (vec_cond VECTOR_CST@0 @1 @2)
2880 (if (integer_all_onesp (@0))
8fdc6c67
RB
2881 @1
2882 (if (integer_zerop (@0))
2883 @2)))
96994de0 2884
b5481987
BC
2885/* Simplification moved from fold_cond_expr_with_comparison. It may also
2886 be extended. */
e2535011
BC
2887/* This pattern implements two kinds simplification:
2888
2889 Case 1)
2890 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
b5481987
BC
2891 1) Conversions are type widening from smaller type.
2892 2) Const c1 equals to c2 after canonicalizing comparison.
2893 3) Comparison has tree code LT, LE, GT or GE.
2894 This specific pattern is needed when (cmp (convert x) c) may not
2895 be simplified by comparison patterns because of multiple uses of
2896 x. It also makes sense here because simplifying across multiple
e2535011
BC
2897 referred var is always benefitial for complicated cases.
2898
2899 Case 2)
2900 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2901(for cmp (lt le gt ge eq)
b5481987 2902 (simplify
ae22bc5d 2903 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
b5481987
BC
2904 (with
2905 {
2906 tree from_type = TREE_TYPE (@1);
2907 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
ae22bc5d 2908 enum tree_code code = ERROR_MARK;
b5481987 2909
ae22bc5d
BC
2910 if (INTEGRAL_TYPE_P (from_type)
2911 && int_fits_type_p (@2, from_type)
b5481987
BC
2912 && (types_match (c1_type, from_type)
2913 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2914 && (TYPE_UNSIGNED (from_type)
2915 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2916 && (types_match (c2_type, from_type)
2917 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2918 && (TYPE_UNSIGNED (from_type)
2919 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2920 {
ae22bc5d 2921 if (cmp != EQ_EXPR)
b5481987 2922 {
e2535011
BC
2923 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2924 {
2925 /* X <= Y - 1 equals to X < Y. */
ae22bc5d 2926 if (cmp == LE_EXPR)
e2535011
BC
2927 code = LT_EXPR;
2928 /* X > Y - 1 equals to X >= Y. */
ae22bc5d 2929 if (cmp == GT_EXPR)
e2535011
BC
2930 code = GE_EXPR;
2931 }
2932 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2933 {
2934 /* X < Y + 1 equals to X <= Y. */
ae22bc5d 2935 if (cmp == LT_EXPR)
e2535011
BC
2936 code = LE_EXPR;
2937 /* X >= Y + 1 equals to X > Y. */
ae22bc5d 2938 if (cmp == GE_EXPR)
e2535011
BC
2939 code = GT_EXPR;
2940 }
ae22bc5d
BC
2941 if (code != ERROR_MARK
2942 || wi::to_widest (@2) == wi::to_widest (@3))
e2535011 2943 {
ae22bc5d 2944 if (cmp == LT_EXPR || cmp == LE_EXPR)
e2535011 2945 code = MIN_EXPR;
ae22bc5d 2946 if (cmp == GT_EXPR || cmp == GE_EXPR)
e2535011
BC
2947 code = MAX_EXPR;
2948 }
b5481987 2949 }
e2535011 2950 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
ae22bc5d
BC
2951 else if (int_fits_type_p (@3, from_type))
2952 code = EQ_EXPR;
b5481987
BC
2953 }
2954 }
2955 (if (code == MAX_EXPR)
21aaaf1e 2956 (convert (max @1 (convert @2)))
b5481987 2957 (if (code == MIN_EXPR)
21aaaf1e 2958 (convert (min @1 (convert @2)))
e2535011 2959 (if (code == EQ_EXPR)
ae22bc5d 2960 (convert (cond (eq @1 (convert @3))
21aaaf1e 2961 (convert:from_type @3) (convert:from_type @2)))))))))
b5481987 2962
714445ae
BC
2963/* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2964
2965 1) OP is PLUS or MINUS.
2966 2) CMP is LT, LE, GT or GE.
2967 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2968
2969 This pattern also handles special cases like:
2970
2971 A) Operand x is a unsigned to signed type conversion and c1 is
2972 integer zero. In this case,
2973 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2974 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2975 B) Const c1 may not equal to (C3 op' C2). In this case we also
2976 check equality for (c1+1) and (c1-1) by adjusting comparison
2977 code.
2978
2979 TODO: Though signed type is handled by this pattern, it cannot be
2980 simplified at the moment because C standard requires additional
2981 type promotion. In order to match&simplify it here, the IR needs
2982 to be cleaned up by other optimizers, i.e, VRP. */
2983(for op (plus minus)
2984 (for cmp (lt le gt ge)
2985 (simplify
2986 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2987 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2988 (if (types_match (from_type, to_type)
2989 /* Check if it is special case A). */
2990 || (TYPE_UNSIGNED (from_type)
2991 && !TYPE_UNSIGNED (to_type)
2992 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2993 && integer_zerop (@1)
2994 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2995 (with
2996 {
4a669ac3 2997 wi::overflow_type overflow = wi::OVF_NONE;
714445ae 2998 enum tree_code code, cmp_code = cmp;
8e6cdc90
RS
2999 wide_int real_c1;
3000 wide_int c1 = wi::to_wide (@1);
3001 wide_int c2 = wi::to_wide (@2);
3002 wide_int c3 = wi::to_wide (@3);
714445ae
BC
3003 signop sgn = TYPE_SIGN (from_type);
3004
3005 /* Handle special case A), given x of unsigned type:
3006 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3007 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3008 if (!types_match (from_type, to_type))
3009 {
3010 if (cmp_code == LT_EXPR)
3011 cmp_code = GT_EXPR;
3012 if (cmp_code == GE_EXPR)
3013 cmp_code = LE_EXPR;
3014 c1 = wi::max_value (to_type);
3015 }
3016 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3017 compute (c3 op' c2) and check if it equals to c1 with op' being
3018 the inverted operator of op. Make sure overflow doesn't happen
3019 if it is undefined. */
3020 if (op == PLUS_EXPR)
3021 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3022 else
3023 real_c1 = wi::add (c3, c2, sgn, &overflow);
3024
3025 code = cmp_code;
3026 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3027 {
3028 /* Check if c1 equals to real_c1. Boundary condition is handled
3029 by adjusting comparison operation if necessary. */
3030 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3031 && !overflow)
3032 {
3033 /* X <= Y - 1 equals to X < Y. */
3034 if (cmp_code == LE_EXPR)
3035 code = LT_EXPR;
3036 /* X > Y - 1 equals to X >= Y. */
3037 if (cmp_code == GT_EXPR)
3038 code = GE_EXPR;
3039 }
3040 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3041 && !overflow)
3042 {
3043 /* X < Y + 1 equals to X <= Y. */
3044 if (cmp_code == LT_EXPR)
3045 code = LE_EXPR;
3046 /* X >= Y + 1 equals to X > Y. */
3047 if (cmp_code == GE_EXPR)
3048 code = GT_EXPR;
3049 }
3050 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3051 {
3052 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3053 code = MIN_EXPR;
3054 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3055 code = MAX_EXPR;
3056 }
3057 }
3058 }
3059 (if (code == MAX_EXPR)
3060 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3061 { wide_int_to_tree (from_type, c2); })
3062 (if (code == MIN_EXPR)
3063 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3064 { wide_int_to_tree (from_type, c2); })))))))))
3065
96994de0
RB
3066(for cnd (cond vec_cond)
3067 /* A ? B : (A ? X : C) -> A ? B : C. */
3068 (simplify
3069 (cnd @0 (cnd @0 @1 @2) @3)
3070 (cnd @0 @1 @3))
3071 (simplify
3072 (cnd @0 @1 (cnd @0 @2 @3))
3073 (cnd @0 @1 @3))
24a179f8
RB
3074 /* A ? B : (!A ? C : X) -> A ? B : C. */
3075 /* ??? This matches embedded conditions open-coded because genmatch
3076 would generate matching code for conditions in separate stmts only.
3077 The following is still important to merge then and else arm cases
3078 from if-conversion. */
3079 (simplify
3080 (cnd @0 @1 (cnd @2 @3 @4))
2c58d42c 3081 (if (inverse_conditions_p (@0, @2))
24a179f8
RB
3082 (cnd @0 @1 @3)))
3083 (simplify
3084 (cnd @0 (cnd @1 @2 @3) @4)
2c58d42c 3085 (if (inverse_conditions_p (@0, @1))
24a179f8 3086 (cnd @0 @3 @4)))
96994de0
RB
3087
3088 /* A ? B : B -> B. */
3089 (simplify
3090 (cnd @0 @1 @1)
09240451 3091 @1)
96994de0 3092
09240451
MG
3093 /* !A ? B : C -> A ? C : B. */
3094 (simplify
3095 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3096 (cnd @0 @2 @1)))
f84e7fd6 3097
a3ca1bc5
RB
3098/* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3099 return all -1 or all 0 results. */
f43d102e
RS
3100/* ??? We could instead convert all instances of the vec_cond to negate,
3101 but that isn't necessarily a win on its own. */
3102(simplify
a3ca1bc5 3103 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
f43d102e 3104 (if (VECTOR_TYPE_P (type)
928686b1
RS
3105 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3106 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
f43d102e 3107 && (TYPE_MODE (TREE_TYPE (type))
4d8989d5 3108 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
a3ca1bc5 3109 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
f43d102e 3110
a3ca1bc5 3111/* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
f43d102e 3112(simplify
a3ca1bc5 3113 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
f43d102e 3114 (if (VECTOR_TYPE_P (type)
928686b1
RS
3115 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3116 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
f43d102e 3117 && (TYPE_MODE (TREE_TYPE (type))
4d8989d5 3118 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
a3ca1bc5 3119 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
f84e7fd6 3120
2ee05f1e 3121
f84e7fd6
RB
3122/* Simplifications of comparisons. */
3123
24f1db9c
RB
3124/* See if we can reduce the magnitude of a constant involved in a
3125 comparison by changing the comparison code. This is a canonicalization
3126 formerly done by maybe_canonicalize_comparison_1. */
3127(for cmp (le gt)
3128 acmp (lt ge)
3129 (simplify
f06e47d7
JJ
3130 (cmp @0 uniform_integer_cst_p@1)
3131 (with { tree cst = uniform_integer_cst_p (@1); }
3132 (if (tree_int_cst_sgn (cst) == -1)
3133 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3134 wide_int_to_tree (TREE_TYPE (cst),
3135 wi::to_wide (cst)
3136 + 1)); })))))
24f1db9c
RB
3137(for cmp (ge lt)
3138 acmp (gt le)
3139 (simplify
f06e47d7
JJ
3140 (cmp @0 uniform_integer_cst_p@1)
3141 (with { tree cst = uniform_integer_cst_p (@1); }
3142 (if (tree_int_cst_sgn (cst) == 1)
3143 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3144 wide_int_to_tree (TREE_TYPE (cst),
3145 wi::to_wide (cst) - 1)); })))))
24f1db9c 3146
f84e7fd6
RB
3147/* We can simplify a logical negation of a comparison to the
3148 inverted comparison. As we cannot compute an expression
3149 operator using invert_tree_comparison we have to simulate
3150 that with expression code iteration. */
3151(for cmp (tcc_comparison)
3152 icmp (inverted_tcc_comparison)
3153 ncmp (inverted_tcc_comparison_with_nans)
3154 /* Ideally we'd like to combine the following two patterns
3155 and handle some more cases by using
3156 (logical_inverted_value (cmp @0 @1))
3157 here but for that genmatch would need to "inline" that.
3158 For now implement what forward_propagate_comparison did. */
3159 (simplify
3160 (bit_not (cmp @0 @1))
3161 (if (VECTOR_TYPE_P (type)
3162 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3163 /* Comparison inversion may be impossible for trapping math,
3164 invert_tree_comparison will tell us. But we can't use
3165 a computed operator in the replacement tree thus we have
3166 to play the trick below. */
3167 (with { enum tree_code ic = invert_tree_comparison
1b457aa4 3168 (cmp, HONOR_NANS (@0)); }
f84e7fd6 3169 (if (ic == icmp)
8fdc6c67
RB
3170 (icmp @0 @1)
3171 (if (ic == ncmp)
3172 (ncmp @0 @1))))))
f84e7fd6 3173 (simplify
09240451
MG
3174 (bit_xor (cmp @0 @1) integer_truep)
3175 (with { enum tree_code ic = invert_tree_comparison
1b457aa4 3176 (cmp, HONOR_NANS (@0)); }
09240451 3177 (if (ic == icmp)
8fdc6c67
RB
3178 (icmp @0 @1)
3179 (if (ic == ncmp)
3180 (ncmp @0 @1))))))
e18c1d66 3181
2ee05f1e
RB
3182/* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3183 ??? The transformation is valid for the other operators if overflow
3184 is undefined for the type, but performing it here badly interacts
3185 with the transformation in fold_cond_expr_with_comparison which
3186 attempts to synthetize ABS_EXPR. */
3187(for cmp (eq ne)
1af4ebf5
MG
3188 (for sub (minus pointer_diff)
3189 (simplify
3190 (cmp (sub@2 @0 @1) integer_zerop)
3191 (if (single_use (@2))
3192 (cmp @0 @1)))))
2ee05f1e
RB
3193
3194/* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3195 signed arithmetic case. That form is created by the compiler
3196 often enough for folding it to be of value. One example is in
3197 computing loop trip counts after Operator Strength Reduction. */
07cdc2b8
RB
3198(for cmp (simple_comparison)
3199 scmp (swapped_simple_comparison)
2ee05f1e 3200 (simplify
bc6e9db4 3201 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2ee05f1e
RB
3202 /* Handle unfolded multiplication by zero. */
3203 (if (integer_zerop (@1))
8fdc6c67
RB
3204 (cmp @1 @2)
3205 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
bc6e9db4
RB
3206 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3207 && single_use (@3))
8fdc6c67
RB
3208 /* If @1 is negative we swap the sense of the comparison. */
3209 (if (tree_int_cst_sgn (@1) < 0)
3210 (scmp @0 @2)
3211 (cmp @0 @2))))))
03cc70b5 3212
2ee05f1e
RB
3213/* Simplify comparison of something with itself. For IEEE
3214 floating-point, we can only do some of these simplifications. */
287f8f17 3215(for cmp (eq ge le)
2ee05f1e
RB
3216 (simplify
3217 (cmp @0 @0)
287f8f17 3218 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
b9407883 3219 || ! HONOR_NANS (@0))
287f8f17
RB
3220 { constant_boolean_node (true, type); }
3221 (if (cmp != EQ_EXPR)
3222 (eq @0 @0)))))
2ee05f1e
RB
3223(for cmp (ne gt lt)
3224 (simplify
3225 (cmp @0 @0)
3226 (if (cmp != NE_EXPR
3227 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
b9407883 3228 || ! HONOR_NANS (@0))
2ee05f1e 3229 { constant_boolean_node (false, type); })))
b5d3d787
RB
3230(for cmp (unle unge uneq)
3231 (simplify
3232 (cmp @0 @0)
3233 { constant_boolean_node (true, type); }))
dd53d197
MG
3234(for cmp (unlt ungt)
3235 (simplify
3236 (cmp @0 @0)
3237 (unordered @0 @0)))
b5d3d787
RB
3238(simplify
3239 (ltgt @0 @0)
3240 (if (!flag_trapping_math)
3241 { constant_boolean_node (false, type); }))
2ee05f1e
RB
3242
3243/* Fold ~X op ~Y as Y op X. */
07cdc2b8 3244(for cmp (simple_comparison)
2ee05f1e 3245 (simplify
7fe996ba
RB
3246 (cmp (bit_not@2 @0) (bit_not@3 @1))
3247 (if (single_use (@2) && single_use (@3))
3248 (cmp @1 @0))))
2ee05f1e
RB
3249
3250/* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
07cdc2b8
RB
3251(for cmp (simple_comparison)
3252 scmp (swapped_simple_comparison)
2ee05f1e 3253 (simplify
7fe996ba
RB
3254 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3255 (if (single_use (@2)
3256 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2ee05f1e
RB
3257 (scmp @0 (bit_not @1)))))
3258
07cdc2b8
RB
3259(for cmp (simple_comparison)
3260 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3261 (simplify
3262 (cmp (convert@2 @0) (convert? @1))
3263 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3264 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3265 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3266 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3267 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3268 (with
3269 {
3270 tree type1 = TREE_TYPE (@1);
3271 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3272 {
3273 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3274 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3275 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3276 type1 = float_type_node;
3277 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3278 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3279 type1 = double_type_node;
3280 }
3281 tree newtype
3282 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
03cc70b5 3283 ? TREE_TYPE (@0) : type1);
07cdc2b8
RB
3284 }
3285 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3286 (cmp (convert:newtype @0) (convert:newtype @1))))))
03cc70b5 3287
07cdc2b8
RB
3288 (simplify
3289 (cmp @0 REAL_CST@1)
3290 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
64d3a1f0
RB
3291 (switch
3292 /* a CMP (-0) -> a CMP 0 */
3293 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3294 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3295 /* x != NaN is always true, other ops are always false. */
3296 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3297 && ! HONOR_SNANS (@1))
3298 { constant_boolean_node (cmp == NE_EXPR, type); })
3299 /* Fold comparisons against infinity. */
3300 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3301 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3302 (with
3303 {
3304 REAL_VALUE_TYPE max;
3305 enum tree_code code = cmp;
3306 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3307 if (neg)
3308 code = swap_tree_comparison (code);
3309 }
3310 (switch
e96a5786 3311 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
64d3a1f0 3312 (if (code == GT_EXPR
e96a5786 3313 && !(HONOR_NANS (@0) && flag_trapping_math))
64d3a1f0
RB
3314 { constant_boolean_node (false, type); })
3315 (if (code == LE_EXPR)
e96a5786 3316 /* x <= +Inf is always true, if we don't care about NaNs. */
64d3a1f0
RB
3317 (if (! HONOR_NANS (@0))
3318 { constant_boolean_node (true, type); }
e96a5786
JM
3319 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3320 an "invalid" exception. */
3321 (if (!flag_trapping_math)
3322 (eq @0 @0))))
3323 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3324 for == this introduces an exception for x a NaN. */
3325 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3326 || code == GE_EXPR)
64d3a1f0
RB
3327 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3328 (if (neg)
3329 (lt @0 { build_real (TREE_TYPE (@0), max); })
3330 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3331 /* x < +Inf is always equal to x <= DBL_MAX. */
3332 (if (code == LT_EXPR)
3333 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3334 (if (neg)
3335 (ge @0 { build_real (TREE_TYPE (@0), max); })
3336 (le @0 { build_real (TREE_TYPE (@0), max); }))))
e96a5786
JM
3337 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3338 an exception for x a NaN so use an unordered comparison. */
64d3a1f0
RB
3339 (if (code == NE_EXPR)
3340 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3341 (if (! HONOR_NANS (@0))
3342 (if (neg)
3343 (ge @0 { build_real (TREE_TYPE (@0), max); })
3344 (le @0 { build_real (TREE_TYPE (@0), max); }))
3345 (if (neg)
e96a5786
JM
3346 (unge @0 { build_real (TREE_TYPE (@0), max); })
3347 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
07cdc2b8
RB
3348
3349 /* If this is a comparison of a real constant with a PLUS_EXPR
3350 or a MINUS_EXPR of a real constant, we can convert it into a
3351 comparison with a revised real constant as long as no overflow
3352 occurs when unsafe_math_optimizations are enabled. */
3353 (if (flag_unsafe_math_optimizations)
3354 (for op (plus minus)
3355 (simplify
3356 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3357 (with
3358 {
3359 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3360 TREE_TYPE (@1), @2, @1);
3361 }
f980c9a2 3362 (if (tem && !TREE_OVERFLOW (tem))
07cdc2b8
RB
3363 (cmp @0 { tem; }))))))
3364
3365 /* Likewise, we can simplify a comparison of a real constant with
3366 a MINUS_EXPR whose first operand is also a real constant, i.e.
3367 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3368 floating-point types only if -fassociative-math is set. */
3369 (if (flag_associative_math)
3370 (simplify
0409237b 3371 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
07cdc2b8 3372 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
f980c9a2 3373 (if (tem && !TREE_OVERFLOW (tem))
07cdc2b8
RB
3374 (cmp { tem; } @1)))))
3375
3376 /* Fold comparisons against built-in math functions. */
3377 (if (flag_unsafe_math_optimizations
3378 && ! flag_errno_math)
3379 (for sq (SQRT)
3380 (simplify
3381 (cmp (sq @0) REAL_CST@1)
64d3a1f0
RB
3382 (switch
3383 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3384 (switch
3385 /* sqrt(x) < y is always false, if y is negative. */
3386 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
8fdc6c67 3387 { constant_boolean_node (false, type); })
64d3a1f0
RB
3388 /* sqrt(x) > y is always true, if y is negative and we
3389 don't care about NaNs, i.e. negative values of x. */
3390 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3391 { constant_boolean_node (true, type); })
3392 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3393 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
c53233c6
RS
3394 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3395 (switch
3396 /* sqrt(x) < 0 is always false. */
3397 (if (cmp == LT_EXPR)
3398 { constant_boolean_node (false, type); })
3399 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3400 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3401 { constant_boolean_node (true, type); })
3402 /* sqrt(x) <= 0 -> x == 0. */
3403 (if (cmp == LE_EXPR)
3404 (eq @0 @1))
3405 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3406 == or !=. In the last case:
3407
3408 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3409
3410 if x is negative or NaN. Due to -funsafe-math-optimizations,
3411 the results for other x follow from natural arithmetic. */
3412 (cmp @0 @1)))
64d3a1f0
RB
3413 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3414 (with
3415 {
3416 REAL_VALUE_TYPE c2;
5c88ea94
RS
3417 real_arithmetic (&c2, MULT_EXPR,
3418 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
64d3a1f0
RB
3419 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3420 }
3421 (if (REAL_VALUE_ISINF (c2))
3422 /* sqrt(x) > y is x == +Inf, when y is very large. */
3423 (if (HONOR_INFINITIES (@0))
3424 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3425 { constant_boolean_node (false, type); })
3426 /* sqrt(x) > c is the same as x > c*c. */
3427 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3428 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3429 (with
3430 {
3431 REAL_VALUE_TYPE c2;
5c88ea94
RS
3432 real_arithmetic (&c2, MULT_EXPR,
3433 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
64d3a1f0
RB
3434 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3435 }
3436 (if (REAL_VALUE_ISINF (c2))
3437 (switch
3438 /* sqrt(x) < y is always true, when y is a very large
3439 value and we don't care about NaNs or Infinities. */
3440 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3441 { constant_boolean_node (true, type); })
3442 /* sqrt(x) < y is x != +Inf when y is very large and we
3443 don't care about NaNs. */
3444 (if (! HONOR_NANS (@0))
3445 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3446 /* sqrt(x) < y is x >= 0 when y is very large and we
3447 don't care about Infinities. */
3448 (if (! HONOR_INFINITIES (@0))
3449 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3450 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3451 (if (GENERIC)
3452 (truth_andif
3453 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3454 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3455 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3456 (if (! HONOR_NANS (@0))
3457 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3458 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3459 (if (GENERIC)
3460 (truth_andif
3461 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
0ca2e7f7
PK
3462 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3463 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3464 (simplify
3465 (cmp (sq @0) (sq @1))
3466 (if (! HONOR_NANS (@0))
3467 (cmp @0 @1))))))
2ee05f1e 3468
e41ec71b 3469/* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
f3842847
YG
3470(for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
3471 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
e41ec71b
YG
3472 (simplify
3473 (cmp (float@0 @1) (float @2))
3474 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
3475 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3476 (with
3477 {
3478 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
3479 tree type1 = TREE_TYPE (@1);
3480 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
3481 tree type2 = TREE_TYPE (@2);
3482 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
3483 }
3484 (if (fmt.can_represent_integral_type_p (type1)
3485 && fmt.can_represent_integral_type_p (type2))
f3842847
YG
3486 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
3487 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
3488 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
3489 && type1_signed_p >= type2_signed_p)
3490 (icmp @1 (convert @2))
3491 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
3492 && type1_signed_p <= type2_signed_p)
3493 (icmp (convert:type2 @1) @2)
3494 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
3495 && type1_signed_p == type2_signed_p)
3496 (icmp @1 @2))))))))))
e41ec71b 3497
c779bea5
YG
3498/* Optimize various special cases of (FTYPE) N CMP CST. */
3499(for cmp (lt le eq ne ge gt)
3500 icmp (le le eq ne ge ge)
3501 (simplify
3502 (cmp (float @0) REAL_CST@1)
3503 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3504 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3505 (with
3506 {
3507 tree itype = TREE_TYPE (@0);
c779bea5
YG
3508 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3509 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3510 /* Be careful to preserve any potential exceptions due to
3511 NaNs. qNaNs are ok in == or != context.
3512 TODO: relax under -fno-trapping-math or
3513 -fno-signaling-nans. */
3514 bool exception_p
3515 = real_isnan (cst) && (cst->signalling
c651dca2 3516 || (cmp != EQ_EXPR && cmp != NE_EXPR));
c779bea5
YG
3517 }
3518 /* TODO: allow non-fitting itype and SNaNs when
3519 -fno-trapping-math. */
e41ec71b 3520 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
c779bea5
YG
3521 (with
3522 {
e41ec71b 3523 signop isign = TYPE_SIGN (itype);
c779bea5
YG
3524 REAL_VALUE_TYPE imin, imax;
3525 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3526 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3527
3528 REAL_VALUE_TYPE icst;
3529 if (cmp == GT_EXPR || cmp == GE_EXPR)
3530 real_ceil (&icst, fmt, cst);
3531 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3532 real_floor (&icst, fmt, cst);
3533 else
3534 real_trunc (&icst, fmt, cst);
3535
b09bf97b 3536 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
c779bea5
YG
3537
3538 bool overflow_p = false;
3539 wide_int icst_val
3540 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3541 }
3542 (switch
3543 /* Optimize cases when CST is outside of ITYPE's range. */
3544 (if (real_compare (LT_EXPR, cst, &imin))
3545 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3546 type); })
3547 (if (real_compare (GT_EXPR, cst, &imax))
3548 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3549 type); })
3550 /* Remove cast if CST is an integer representable by ITYPE. */
3551 (if (cst_int_p)
3552 (cmp @0 { gcc_assert (!overflow_p);
3553 wide_int_to_tree (itype, icst_val); })
3554 )
3555 /* When CST is fractional, optimize
3556 (FTYPE) N == CST -> 0
3557 (FTYPE) N != CST -> 1. */
3558 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
03cc70b5 3559 { constant_boolean_node (cmp == NE_EXPR, type); })
c779bea5
YG
3560 /* Otherwise replace with sensible integer constant. */
3561 (with
3562 {
3563 gcc_checking_assert (!overflow_p);
3564 }
3565 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3566
40fd269a
MG
3567/* Fold A /[ex] B CMP C to A CMP B * C. */
3568(for cmp (eq ne)
3569 (simplify
3570 (cmp (exact_div @0 @1) INTEGER_CST@2)
3571 (if (!integer_zerop (@1))
8e6cdc90 3572 (if (wi::to_wide (@2) == 0)
40fd269a
MG
3573 (cmp @0 @2)
3574 (if (TREE_CODE (@1) == INTEGER_CST)
3575 (with
3576 {
4a669ac3 3577 wi::overflow_type ovf;
8e6cdc90
RS
3578 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3579 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
40fd269a
MG
3580 }
3581 (if (ovf)
3582 { constant_boolean_node (cmp == NE_EXPR, type); }
3583 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3584(for cmp (lt le gt ge)
3585 (simplify
3586 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
8e6cdc90 3587 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
40fd269a
MG
3588 (with
3589 {
4a669ac3 3590 wi::overflow_type ovf;
8e6cdc90
RS
3591 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3592 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
40fd269a
MG
3593 }
3594 (if (ovf)
8e6cdc90
RS
3595 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3596 TYPE_SIGN (TREE_TYPE (@2)))
40fd269a
MG
3597 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3598 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3599
cfdc4f33
MG
3600/* Unordered tests if either argument is a NaN. */
3601(simplify
3602 (bit_ior (unordered @0 @0) (unordered @1 @1))
aea417d7 3603 (if (types_match (@0, @1))
cfdc4f33 3604 (unordered @0 @1)))
257b01ba
MG
3605(simplify
3606 (bit_and (ordered @0 @0) (ordered @1 @1))
3607 (if (types_match (@0, @1))
3608 (ordered @0 @1)))
cfdc4f33
MG
3609(simplify
3610 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3611 @2)
257b01ba
MG
3612(simplify
3613 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3614 @2)
e18c1d66 3615
90c6f26c
RB
3616/* Simple range test simplifications. */
3617/* A < B || A >= B -> true. */
5d30c58d
RB
3618(for test1 (lt le le le ne ge)
3619 test2 (ge gt ge ne eq ne)
90c6f26c
RB
3620 (simplify
3621 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3622 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3623 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3624 { constant_boolean_node (true, type); })))
3625/* A < B && A >= B -> false. */
3626(for test1 (lt lt lt le ne eq)
3627 test2 (ge gt eq gt eq gt)
3628 (simplify
3629 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3630 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3631 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3632 { constant_boolean_node (false, type); })))
3633
9ebc3467
YG
3634/* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3635 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3636
3637 Note that comparisons
3638 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3639 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3640 will be canonicalized to above so there's no need to
3641 consider them here.
3642 */
3643
3644(for cmp (le gt)
3645 eqcmp (eq ne)
3646 (simplify
3647 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3648 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3649 (with
3650 {
3651 tree ty = TREE_TYPE (@0);
3652 unsigned prec = TYPE_PRECISION (ty);
3653 wide_int mask = wi::to_wide (@2, prec);
3654 wide_int rhs = wi::to_wide (@3, prec);
3655 signop sgn = TYPE_SIGN (ty);
3656 }
3657 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3658 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3659 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3660 { build_zero_cst (ty); }))))))
3661
534bd33b
MG
3662/* -A CMP -B -> B CMP A. */
3663(for cmp (tcc_comparison)
3664 scmp (swapped_tcc_comparison)
3665 (simplify
3666 (cmp (negate @0) (negate @1))
3667 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3668 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3669 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3670 (scmp @0 @1)))
3671 (simplify
3672 (cmp (negate @0) CONSTANT_CLASS_P@1)
3673 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3674 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3675 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
23f27839 3676 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
534bd33b
MG
3677 (if (tem && !TREE_OVERFLOW (tem))
3678 (scmp @0 { tem; }))))))
3679
b0eb889b
MG
3680/* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3681(for op (eq ne)
3682 (simplify
3683 (op (abs @0) zerop@1)
3684 (op @0 @1)))
3685
6358a676
MG
3686/* From fold_sign_changed_comparison and fold_widened_comparison.
3687 FIXME: the lack of symmetry is disturbing. */
79d4f7c6
RB
3688(for cmp (simple_comparison)
3689 (simplify
3690 (cmp (convert@0 @00) (convert?@1 @10))
452ec2a5 3691 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
79d4f7c6
RB
3692 /* Disable this optimization if we're casting a function pointer
3693 type on targets that require function pointer canonicalization. */
3694 && !(targetm.have_canonicalize_funcptr_for_compare ()
400bc526
JDA
3695 && ((POINTER_TYPE_P (TREE_TYPE (@00))
3696 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
3697 || (POINTER_TYPE_P (TREE_TYPE (@10))
3698 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
2fde61e3 3699 && single_use (@0))
79d4f7c6
RB
3700 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3701 && (TREE_CODE (@10) == INTEGER_CST
6358a676 3702 || @1 != @10)
79d4f7c6
RB
3703 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3704 || cmp == NE_EXPR
3705 || cmp == EQ_EXPR)
6358a676 3706 && !POINTER_TYPE_P (TREE_TYPE (@00)))
79d4f7c6
RB
3707 /* ??? The special-casing of INTEGER_CST conversion was in the original
3708 code and here to avoid a spurious overflow flag on the resulting
3709 constant which fold_convert produces. */
3710 (if (TREE_CODE (@1) == INTEGER_CST)
3711 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3712 TREE_OVERFLOW (@1)); })
3713 (cmp @00 (convert @1)))
3714
3715 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3716 /* If possible, express the comparison in the shorter mode. */
3717 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
7fd82d52
PP
3718 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3719 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3720 && TYPE_UNSIGNED (TREE_TYPE (@00))))
79d4f7c6
RB
3721 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3722 || ((TYPE_PRECISION (TREE_TYPE (@00))
3723 >= TYPE_PRECISION (TREE_TYPE (@10)))
3724 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3725 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3726 || (TREE_CODE (@10) == INTEGER_CST
f6c15759 3727 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
79d4f7c6
RB
3728 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3729 (cmp @00 (convert @10))
3730 (if (TREE_CODE (@10) == INTEGER_CST
f6c15759 3731 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
79d4f7c6
RB
3732 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3733 (with
3734 {
3735 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3736 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3737 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3738 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3739 }
3740 (if (above || below)
3741 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3742 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3743 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3744 { constant_boolean_node (above ? true : false, type); }
3745 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3746 { constant_boolean_node (above ? false : true, type); }))))))))))))
66e1cacf 3747
96a111a3
RB
3748(for cmp (eq ne)
3749 /* A local variable can never be pointed to by
3750 the default SSA name of an incoming parameter.
3751 SSA names are canonicalized to 2nd place. */
3752 (simplify
3753 (cmp addr@0 SSA_NAME@1)
3754 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3755 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3756 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3757 (if (TREE_CODE (base) == VAR_DECL
3758 && auto_var_in_fn_p (base, current_function_decl))
3759 (if (cmp == NE_EXPR)
3760 { constant_boolean_node (true, type); }
3761 { constant_boolean_node (false, type); }))))))
3762
66e1cacf
RB
3763/* Equality compare simplifications from fold_binary */
3764(for cmp (eq ne)
3765
3766 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3767 Similarly for NE_EXPR. */
3768 (simplify
3769 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3770 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
8e6cdc90 3771 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
66e1cacf
RB
3772 { constant_boolean_node (cmp == NE_EXPR, type); }))
3773
3774 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3775 (simplify
3776 (cmp (bit_xor @0 @1) integer_zerop)
3777 (cmp @0 @1))
3778
3779 /* (X ^ Y) == Y becomes X == 0.
3780 Likewise (X ^ Y) == X becomes Y == 0. */
3781 (simplify
99e943a2 3782 (cmp:c (bit_xor:c @0 @1) @0)
66e1cacf
RB
3783 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3784
3785 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3786 (simplify
3787 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3788 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
d057c866 3789 (cmp @0 (bit_xor @1 (convert @2)))))
d057c866
RB
3790
3791 (simplify
3792 (cmp (convert? addr@0) integer_zerop)
3793 (if (tree_single_nonzero_warnv_p (@0, NULL))
3794 { constant_boolean_node (cmp == NE_EXPR, type); })))
3795
b0eb889b
MG
3796/* If we have (A & C) == C where C is a power of 2, convert this into
3797 (A & C) != 0. Similarly for NE_EXPR. */
3798(for cmp (eq ne)
3799 icmp (ne eq)
3800 (simplify
3801 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3802 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
03cc70b5 3803
519e0faa
PB
3804/* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3805 convert this into a shift followed by ANDing with D. */
3806(simplify
3807 (cond
3808 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
9e61e48e
JJ
3809 INTEGER_CST@2 integer_zerop)
3810 (if (integer_pow2p (@2))
3811 (with {
3812 int shift = (wi::exact_log2 (wi::to_wide (@2))
3813 - wi::exact_log2 (wi::to_wide (@1)));
3814 }
3815 (if (shift > 0)
3816 (bit_and
3817 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3818 (bit_and
3819 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3820 @2)))))
519e0faa 3821
b0eb889b
MG
3822/* If we have (A & C) != 0 where C is the sign bit of A, convert
3823 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3824(for cmp (eq ne)
3825 ncmp (ge lt)
3826 (simplify
3827 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3828 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2be65d9e 3829 && type_has_mode_precision_p (TREE_TYPE (@0))
b0eb889b 3830 && element_precision (@2) >= element_precision (@0)
8e6cdc90 3831 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
b0eb889b
MG
3832 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3833 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3834
519e0faa 3835/* If we have A < 0 ? C : 0 where C is a power of 2, convert
c0140e3c 3836 this into a right shift or sign extension followed by ANDing with C. */
519e0faa
PB
3837(simplify
3838 (cond
3839 (lt @0 integer_zerop)
9e61e48e
JJ
3840 INTEGER_CST@1 integer_zerop)
3841 (if (integer_pow2p (@1)
3842 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
c0140e3c 3843 (with {
8e6cdc90 3844 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
c0140e3c
JJ
3845 }
3846 (if (shift >= 0)
3847 (bit_and
3848 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3849 @1)
3850 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3851 sign extension followed by AND with C will achieve the effect. */
3852 (bit_and (convert @0) @1)))))
519e0faa 3853
68aba1f6
RB
3854/* When the addresses are not directly of decls compare base and offset.
3855 This implements some remaining parts of fold_comparison address
3856 comparisons but still no complete part of it. Still it is good
3857 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3858(for cmp (simple_comparison)
3859 (simplify
f501d5cd 3860 (cmp (convert1?@2 addr@0) (convert2? addr@1))
68aba1f6
RB
3861 (with
3862 {
a90c8804 3863 poly_int64 off0, off1;
68aba1f6
RB
3864 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3865 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3866 if (base0 && TREE_CODE (base0) == MEM_REF)
3867 {
aca52e6f 3868 off0 += mem_ref_offset (base0).force_shwi ();
68aba1f6
RB
3869 base0 = TREE_OPERAND (base0, 0);
3870 }
3871 if (base1 && TREE_CODE (base1) == MEM_REF)
3872 {
aca52e6f 3873 off1 += mem_ref_offset (base1).force_shwi ();
68aba1f6
RB
3874 base1 = TREE_OPERAND (base1, 0);
3875 }
3876 }
da571fda
RB
3877 (if (base0 && base1)
3878 (with
3879 {
aad88aed 3880 int equal = 2;
70f40fea
JJ
3881 /* Punt in GENERIC on variables with value expressions;
3882 the value expressions might point to fields/elements
3883 of other vars etc. */
3884 if (GENERIC
3885 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3886 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3887 ;
3888 else if (decl_in_symtab_p (base0)
3889 && decl_in_symtab_p (base1))
da571fda
RB
3890 equal = symtab_node::get_create (base0)
3891 ->equal_address_to (symtab_node::get_create (base1));
c3bea076
RB
3892 else if ((DECL_P (base0)
3893 || TREE_CODE (base0) == SSA_NAME
3894 || TREE_CODE (base0) == STRING_CST)
3895 && (DECL_P (base1)
3896 || TREE_CODE (base1) == SSA_NAME
3897 || TREE_CODE (base1) == STRING_CST))
aad88aed 3898 equal = (base0 == base1);
da571fda 3899 }
3fccbb9e
JJ
3900 (if (equal == 1
3901 && (cmp == EQ_EXPR || cmp == NE_EXPR
3902 /* If the offsets are equal we can ignore overflow. */
3903 || known_eq (off0, off1)
3904 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3905 /* Or if we compare using pointers to decls or strings. */
3906 || (POINTER_TYPE_P (TREE_TYPE (@2))
3907 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
da571fda 3908 (switch
a90c8804
RS
3909 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3910 { constant_boolean_node (known_eq (off0, off1), type); })
3911 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3912 { constant_boolean_node (known_ne (off0, off1), type); })
3913 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3914 { constant_boolean_node (known_lt (off0, off1), type); })
3915 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3916 { constant_boolean_node (known_le (off0, off1), type); })
3917 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3918 { constant_boolean_node (known_ge (off0, off1), type); })
3919 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3920 { constant_boolean_node (known_gt (off0, off1), type); }))
da571fda
RB
3921 (if (equal == 0
3922 && DECL_P (base0) && DECL_P (base1)
3923 /* If we compare this as integers require equal offset. */
3924 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
a90c8804 3925 || known_eq (off0, off1)))
da571fda
RB
3926 (switch
3927 (if (cmp == EQ_EXPR)
3928 { constant_boolean_node (false, type); })
3929 (if (cmp == NE_EXPR)
3930 { constant_boolean_node (true, type); })))))))))
66e1cacf 3931
98998245
RB
3932/* Simplify pointer equality compares using PTA. */
3933(for neeq (ne eq)
3934 (simplify
3935 (neeq @0 @1)
3936 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3937 && ptrs_compare_unequal (@0, @1))
f913ff2a 3938 { constant_boolean_node (neeq != EQ_EXPR, type); })))
98998245 3939
8f63caf6 3940/* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
467719fb
PK
3941 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3942 Disable the transform if either operand is pointer to function.
3943 This broke pr22051-2.c for arm where function pointer
3944 canonicalizaion is not wanted. */
1c0a8806 3945
8f63caf6
RB
3946(for cmp (ne eq)
3947 (simplify
3948 (cmp (convert @0) INTEGER_CST@1)
f53e7e13
JJ
3949 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
3950 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3951 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3952 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3953 && POINTER_TYPE_P (TREE_TYPE (@1))
3954 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3955 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
8f63caf6
RB
3956 (cmp @0 (convert @1)))))
3957
21aacde4
RB
3958/* Non-equality compare simplifications from fold_binary */
3959(for cmp (lt gt le ge)
3960 /* Comparisons with the highest or lowest possible integer of
3961 the specified precision will have known values. */
3962 (simplify
f06e47d7
JJ
3963 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
3964 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3965 || POINTER_TYPE_P (TREE_TYPE (@1))
3966 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
21aacde4
RB
3967 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3968 (with
3969 {
f06e47d7
JJ
3970 tree cst = uniform_integer_cst_p (@1);
3971 tree arg1_type = TREE_TYPE (cst);
21aacde4
RB
3972 unsigned int prec = TYPE_PRECISION (arg1_type);
3973 wide_int max = wi::max_value (arg1_type);
3974 wide_int signed_max = wi::max_value (prec, SIGNED);
3975 wide_int min = wi::min_value (arg1_type);
3976 }
3977 (switch
f06e47d7 3978 (if (wi::to_wide (cst) == max)
21aacde4
RB
3979 (switch
3980 (if (cmp == GT_EXPR)
3981 { constant_boolean_node (false, type); })
3982 (if (cmp == GE_EXPR)
3983 (eq @2 @1))
3984 (if (cmp == LE_EXPR)
3985 { constant_boolean_node (true, type); })
3986 (if (cmp == LT_EXPR)
3987 (ne @2 @1))))
f06e47d7 3988 (if (wi::to_wide (cst) == min)
21aacde4
RB
3989 (switch
3990 (if (cmp == LT_EXPR)
3991 { constant_boolean_node (false, type); })
3992 (if (cmp == LE_EXPR)
3993 (eq @2 @1))
3994 (if (cmp == GE_EXPR)
3995 { constant_boolean_node (true, type); })
3996 (if (cmp == GT_EXPR)
3997 (ne @2 @1))))
f06e47d7 3998 (if (wi::to_wide (cst) == max - 1)
9bc22d19
RB
3999 (switch
4000 (if (cmp == GT_EXPR)
f06e47d7
JJ
4001 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4002 wide_int_to_tree (TREE_TYPE (cst),
4003 wi::to_wide (cst)
4004 + 1)); }))
9bc22d19 4005 (if (cmp == LE_EXPR)
f06e47d7
JJ
4006 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4007 wide_int_to_tree (TREE_TYPE (cst),
4008 wi::to_wide (cst)
4009 + 1)); }))))
4010 (if (wi::to_wide (cst) == min + 1)
21aacde4
RB
4011 (switch
4012 (if (cmp == GE_EXPR)
f06e47d7
JJ
4013 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4014 wide_int_to_tree (TREE_TYPE (cst),
4015 wi::to_wide (cst)
4016 - 1)); }))
21aacde4 4017 (if (cmp == LT_EXPR)
f06e47d7
JJ
4018 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4019 wide_int_to_tree (TREE_TYPE (cst),
4020 wi::to_wide (cst)
4021 - 1)); }))))
4022 (if (wi::to_wide (cst) == signed_max
21aacde4
RB
4023 && TYPE_UNSIGNED (arg1_type)
4024 /* We will flip the signedness of the comparison operator
4025 associated with the mode of @1, so the sign bit is
4026 specified by this mode. Check that @1 is the signed
4027 max associated with this sign bit. */
7a504f33 4028 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
21aacde4
RB
4029 /* signed_type does not work on pointer types. */
4030 && INTEGRAL_TYPE_P (arg1_type))
4031 /* The following case also applies to X < signed_max+1
4032 and X >= signed_max+1 because previous transformations. */
4033 (if (cmp == LE_EXPR || cmp == GT_EXPR)
f06e47d7
JJ
4034 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4035 (switch
4036 (if (cst == @1 && cmp == LE_EXPR)
4037 (ge (convert:st @0) { build_zero_cst (st); }))
4038 (if (cst == @1 && cmp == GT_EXPR)
4039 (lt (convert:st @0) { build_zero_cst (st); }))
4040 (if (cmp == LE_EXPR)
4041 (ge (view_convert:st @0) { build_zero_cst (st); }))
4042 (if (cmp == GT_EXPR)
4043 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
03cc70b5 4044
b5d3d787
RB
4045(for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4046 /* If the second operand is NaN, the result is constant. */
4047 (simplify
4048 (cmp @0 REAL_CST@1)
4049 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4050 && (cmp != LTGT_EXPR || ! flag_trapping_math))
50301115 4051 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
b5d3d787 4052 ? false : true, type); })))
21aacde4 4053
55cf3946
RB
4054/* bool_var != 0 becomes bool_var. */
4055(simplify
b5d3d787 4056 (ne @0 integer_zerop)
55cf3946
RB
4057 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4058 && types_match (type, TREE_TYPE (@0)))
4059 (non_lvalue @0)))
4060/* bool_var == 1 becomes bool_var. */
4061(simplify
b5d3d787 4062 (eq @0 integer_onep)
55cf3946
RB
4063 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4064 && types_match (type, TREE_TYPE (@0)))
4065 (non_lvalue @0)))
b5d3d787
RB
4066/* Do not handle
4067 bool_var == 0 becomes !bool_var or
4068 bool_var != 1 becomes !bool_var
4069 here because that only is good in assignment context as long
4070 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4071 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4072 clearly less optimal and which we'll transform again in forwprop. */
55cf3946 4073
ca1206be
MG
4074/* When one argument is a constant, overflow detection can be simplified.
4075 Currently restricted to single use so as not to interfere too much with
4076 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4077 A + CST CMP A -> A CMP' CST' */
4078(for cmp (lt le ge gt)
4079 out (gt gt le le)
4080 (simplify
a8e9f9a3 4081 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
ca1206be
MG
4082 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4083 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
8e6cdc90 4084 && wi::to_wide (@1) != 0
ca1206be 4085 && single_use (@2))
8e6cdc90
RS
4086 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4087 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4088 wi::max_value (prec, UNSIGNED)
4089 - wi::to_wide (@1)); })))))
ca1206be 4090
3563f78f
MG
4091/* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4092 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4093 expects the long form, so we restrict the transformation for now. */
4094(for cmp (gt le)
4095 (simplify
a8e9f9a3 4096 (cmp:c (minus@2 @0 @1) @0)
3563f78f
MG
4097 (if (single_use (@2)
4098 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4099 && TYPE_UNSIGNED (TREE_TYPE (@0))
4100 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4101 (cmp @1 @0))))
3563f78f
MG
4102
4103/* Testing for overflow is unnecessary if we already know the result. */
3563f78f
MG
4104/* A - B > A */
4105(for cmp (gt le)
4106 out (ne eq)
4107 (simplify
a8e9f9a3 4108 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3563f78f
MG
4109 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4110 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4111 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4112/* A + B < A */
4113(for cmp (lt ge)
4114 out (ne eq)
4115 (simplify
a8e9f9a3 4116 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3563f78f
MG
4117 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4118 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4119 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4120
603aeb87 4121/* For unsigned operands, -1 / B < A checks whether A * B would overflow.
0557293f 4122 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
0557293f
AM
4123(for cmp (lt ge)
4124 out (ne eq)
4125 (simplify
603aeb87 4126 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
0557293f
AM
4127 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4128 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4129 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
55cf3946 4130
53f3cd25
RS
4131/* Simplification of math builtins. These rules must all be optimizations
4132 as well as IL simplifications. If there is a possibility that the new
4133 form could be a pessimization, the rule should go in the canonicalization
4134 section that follows this one.
e18c1d66 4135
53f3cd25
RS
4136 Rules can generally go in this section if they satisfy one of
4137 the following:
4138
4139 - the rule describes an identity
4140
4141 - the rule replaces calls with something as simple as addition or
4142 multiplication
4143
4144 - the rule contains unary calls only and simplifies the surrounding
4145 arithmetic. (The idea here is to exclude non-unary calls in which
4146 one operand is constant and in which the call is known to be cheap
4147 when the operand has that value.) */
52c6378a 4148
53f3cd25 4149(if (flag_unsafe_math_optimizations)
52c6378a
N
4150 /* Simplify sqrt(x) * sqrt(x) -> x. */
4151 (simplify
c6cfa2bf 4152 (mult (SQRT_ALL@1 @0) @1)
52c6378a
N
4153 (if (!HONOR_SNANS (type))
4154 @0))
4155
ed17cb57
JW
4156 (for op (plus minus)
4157 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4158 (simplify
4159 (op (rdiv @0 @1)
4160 (rdiv @2 @1))
4161 (rdiv (op @0 @2) @1)))
4162
5e21d765
WD
4163 (for cmp (lt le gt ge)
4164 neg_cmp (gt ge lt le)
4165 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
4166 (simplify
4167 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4168 (with
4169 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4170 (if (tem
4171 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4172 || (real_zerop (tem) && !real_zerop (@1))))
4173 (switch
4174 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4175 (cmp @0 { tem; }))
4176 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4177 (neg_cmp @0 { tem; })))))))
4178
35401640
N
4179 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4180 (for root (SQRT CBRT)
4181 (simplify
4182 (mult (root:s @0) (root:s @1))
4183 (root (mult @0 @1))))
4184
35401640
N
4185 /* Simplify expN(x) * expN(y) -> expN(x+y). */
4186 (for exps (EXP EXP2 EXP10 POW10)
4187 (simplify
4188 (mult (exps:s @0) (exps:s @1))
4189 (exps (plus @0 @1))))
4190
52c6378a 4191 /* Simplify a/root(b/c) into a*root(c/b). */
35401640
N
4192 (for root (SQRT CBRT)
4193 (simplify
4194 (rdiv @0 (root:s (rdiv:s @1 @2)))
4195 (mult @0 (root (rdiv @2 @1)))))
4196
4197 /* Simplify x/expN(y) into x*expN(-y). */
4198 (for exps (EXP EXP2 EXP10 POW10)
4199 (simplify
4200 (rdiv @0 (exps:s @1))
4201 (mult @0 (exps (negate @1)))))
52c6378a 4202
eee7b6c4
RB
4203 (for logs (LOG LOG2 LOG10 LOG10)
4204 exps (EXP EXP2 EXP10 POW10)
8acda9b2 4205 /* logN(expN(x)) -> x. */
e18c1d66
RB
4206 (simplify
4207 (logs (exps @0))
8acda9b2
RS
4208 @0)
4209 /* expN(logN(x)) -> x. */
4210 (simplify
4211 (exps (logs @0))
4212 @0))
53f3cd25 4213
e18c1d66
RB
4214 /* Optimize logN(func()) for various exponential functions. We
4215 want to determine the value "x" and the power "exponent" in
4216 order to transform logN(x**exponent) into exponent*logN(x). */
eee7b6c4
RB
4217 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4218 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
e18c1d66
RB
4219 (simplify
4220 (logs (exps @0))
c9e926ce
RS
4221 (if (SCALAR_FLOAT_TYPE_P (type))
4222 (with {
4223 tree x;
4224 switch (exps)
4225 {
4226 CASE_CFN_EXP:
4227 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4228 x = build_real_truncate (type, dconst_e ());
4229 break;
4230 CASE_CFN_EXP2:
4231 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4232 x = build_real (type, dconst2);
4233 break;
4234 CASE_CFN_EXP10:
4235 CASE_CFN_POW10:
4236 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4237 {
4238 REAL_VALUE_TYPE dconst10;
4239 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4240 x = build_real (type, dconst10);
4241 }
4242 break;
4243 default:
4244 gcc_unreachable ();
4245 }
4246 }
4247 (mult (logs { x; }) @0)))))
53f3cd25 4248
e18c1d66
RB
4249 (for logs (LOG LOG
4250 LOG2 LOG2
4251 LOG10 LOG10)
4252 exps (SQRT CBRT)
4253 (simplify
4254 (logs (exps @0))
c9e926ce
RS
4255 (if (SCALAR_FLOAT_TYPE_P (type))
4256 (with {
4257 tree x;
4258 switch (exps)
4259 {
4260 CASE_CFN_SQRT:
4261 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4262 x = build_real (type, dconsthalf);
4263 break;
4264 CASE_CFN_CBRT:
4265 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4266 x = build_real_truncate (type, dconst_third ());
4267 break;
4268 default:
4269 gcc_unreachable ();
4270 }
4271 }
4272 (mult { x; } (logs @0))))))
53f3cd25
RS
4273
4274 /* logN(pow(x,exponent)) -> exponent*logN(x). */
e18c1d66
RB
4275 (for logs (LOG LOG2 LOG10)
4276 pows (POW)
4277 (simplify
4278 (logs (pows @0 @1))
53f3cd25
RS
4279 (mult @1 (logs @0))))
4280
848bb6fc
JJ
4281 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4282 or if C is a positive power of 2,
4283 pow(C,x) -> exp2(log2(C)*x). */
30a2c10e 4284#if GIMPLE
e83fe013
WD
4285 (for pows (POW)
4286 exps (EXP)
4287 logs (LOG)
848bb6fc
JJ
4288 exp2s (EXP2)
4289 log2s (LOG2)
e83fe013
WD
4290 (simplify
4291 (pows REAL_CST@0 @1)
848bb6fc 4292 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
ef7866a3
JJ
4293 && real_isfinite (TREE_REAL_CST_PTR (@0))
4294 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4295 the use_exp2 case until after vectorization. It seems actually
4296 beneficial for all constants to postpone this until later,
4297 because exp(log(C)*x), while faster, will have worse precision
4298 and if x folds into a constant too, that is unnecessary
4299 pessimization. */
4300 && canonicalize_math_after_vectorization_p ())
848bb6fc
JJ
4301 (with {
4302 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4303 bool use_exp2 = false;
4304 if (targetm.libc_has_function (function_c99_misc)
4305 && value->cl == rvc_normal)
4306 {
4307 REAL_VALUE_TYPE frac_rvt = *value;
4308 SET_REAL_EXP (&frac_rvt, 1);
4309 if (real_equal (&frac_rvt, &dconst1))
4310 use_exp2 = true;
4311 }
4312 }
4313 (if (!use_exp2)
30a2c10e
JJ
4314 (if (optimize_pow_to_exp (@0, @1))
4315 (exps (mult (logs @0) @1)))
ef7866a3 4316 (exp2s (mult (log2s @0) @1)))))))
30a2c10e 4317#endif
e83fe013 4318
16ef0a8c
JJ
4319 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4320 (for pows (POW)
4321 exps (EXP EXP2 EXP10 POW10)
4322 logs (LOG LOG2 LOG10 LOG10)
4323 (simplify
4324 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4325 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4326 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4327 (exps (plus (mult (logs @0) @1) @2)))))
4328
53f3cd25
RS
4329 (for sqrts (SQRT)
4330 cbrts (CBRT)
b4838d77 4331 pows (POW)
53f3cd25
RS
4332 exps (EXP EXP2 EXP10 POW10)
4333 /* sqrt(expN(x)) -> expN(x*0.5). */
4334 (simplify
4335 (sqrts (exps @0))
4336 (exps (mult @0 { build_real (type, dconsthalf); })))
4337 /* cbrt(expN(x)) -> expN(x/3). */
4338 (simplify
4339 (cbrts (exps @0))
b4838d77
RS
4340 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4341 /* pow(expN(x), y) -> expN(x*y). */
4342 (simplify
4343 (pows (exps @0) @1)
4344 (exps (mult @0 @1))))
cfed37a0
RS
4345
4346 /* tan(atan(x)) -> x. */
4347 (for tans (TAN)
4348 atans (ATAN)
4349 (simplify
4350 (tans (atans @0))
4351 @0)))
53f3cd25 4352
121ef08b
GB
4353 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
4354 (for sins (SIN)
4355 atans (ATAN)
4356 sqrts (SQRT)
4357 copysigns (COPYSIGN)
4358 (simplify
4359 (sins (atans:s @0))
4360 (with
4361 {
4362 REAL_VALUE_TYPE r_cst;
4363 build_sinatan_real (&r_cst, type);
4364 tree t_cst = build_real (type, r_cst);
4365 tree t_one = build_one_cst (type);
4366 }
4367 (if (SCALAR_FLOAT_TYPE_P (type))
4368 (cond (le (abs @0) { t_cst; })
4369 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4370 (copysigns { t_one; } @0))))))
4371
4372/* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4373 (for coss (COS)
4374 atans (ATAN)
4375 sqrts (SQRT)
4376 copysigns (COPYSIGN)
4377 (simplify
4378 (coss (atans:s @0))
4379 (with
4380 {
4381 REAL_VALUE_TYPE r_cst;
4382 build_sinatan_real (&r_cst, type);
4383 tree t_cst = build_real (type, r_cst);
4384 tree t_one = build_one_cst (type);
4385 tree t_zero = build_zero_cst (type);
4386 }
4387 (if (SCALAR_FLOAT_TYPE_P (type))
4388 (cond (le (abs @0) { t_cst; })
4389 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4390 (copysigns { t_zero; } @0))))))
4391
4aff6d17
GB
4392 (if (!flag_errno_math)
4393 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4394 (for sinhs (SINH)
4395 atanhs (ATANH)
4396 sqrts (SQRT)
4397 (simplify
4398 (sinhs (atanhs:s @0))
4399 (with { tree t_one = build_one_cst (type); }
4400 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4401
4402 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4403 (for coshs (COSH)
4404 atanhs (ATANH)
4405 sqrts (SQRT)
4406 (simplify
4407 (coshs (atanhs:s @0))
4408 (with { tree t_one = build_one_cst (type); }
4409 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
4410
abcc43f5
RS
4411/* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4412(simplify
e04d2a35 4413 (CABS (complex:C @0 real_zerop@1))
abcc43f5
RS
4414 (abs @0))
4415
67dbe582 4416/* trunc(trunc(x)) -> trunc(x), etc. */
c6cfa2bf 4417(for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
67dbe582
RS
4418 (simplify
4419 (fns (fns @0))
4420 (fns @0)))
4421/* f(x) -> x if x is integer valued and f does nothing for such values. */
c6cfa2bf 4422(for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
67dbe582
RS
4423 (simplify
4424 (fns integer_valued_real_p@0)
4425 @0))
67dbe582 4426
4d7836c4
RS
4427/* hypot(x,0) and hypot(0,x) -> abs(x). */
4428(simplify
c9e926ce 4429 (HYPOT:c @0 real_zerop@1)
4d7836c4
RS
4430 (abs @0))
4431
b4838d77
RS
4432/* pow(1,x) -> 1. */
4433(simplify
4434 (POW real_onep@0 @1)
4435 @0)
4436
461e4145
RS
4437(simplify
4438 /* copysign(x,x) -> x. */
c6cfa2bf 4439 (COPYSIGN_ALL @0 @0)
461e4145
RS
4440 @0)
4441
4442(simplify
4443 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
c6cfa2bf 4444 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
461e4145
RS
4445 (abs @0))
4446
86c0733f
RS
4447(for scale (LDEXP SCALBN SCALBLN)
4448 /* ldexp(0, x) -> 0. */
4449 (simplify
4450 (scale real_zerop@0 @1)
4451 @0)
4452 /* ldexp(x, 0) -> x. */
4453 (simplify
4454 (scale @0 integer_zerop@1)
4455 @0)
4456 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4457 (simplify
4458 (scale REAL_CST@0 @1)
4459 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4460 @0)))
4461
53f3cd25
RS
4462/* Canonicalization of sequences of math builtins. These rules represent
4463 IL simplifications but are not necessarily optimizations.
4464
4465 The sincos pass is responsible for picking "optimal" implementations
4466 of math builtins, which may be more complicated and can sometimes go
4467 the other way, e.g. converting pow into a sequence of sqrts.
4468 We only want to do these canonicalizations before the pass has run. */
4469
4470(if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4471 /* Simplify tan(x) * cos(x) -> sin(x). */
4472 (simplify
4473 (mult:c (TAN:s @0) (COS:s @0))
4474 (SIN @0))
4475
4476 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4477 (simplify
de3fbea3 4478 (mult:c @0 (POW:s @0 REAL_CST@1))
53f3cd25
RS
4479 (if (!TREE_OVERFLOW (@1))
4480 (POW @0 (plus @1 { build_one_cst (type); }))))
4481
4482 /* Simplify sin(x) / cos(x) -> tan(x). */
4483 (simplify
4484 (rdiv (SIN:s @0) (COS:s @0))
4485 (TAN @0))
4486
4487 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4488 (simplify
4489 (rdiv (COS:s @0) (SIN:s @0))
4490 (rdiv { build_one_cst (type); } (TAN @0)))
4491
4492 /* Simplify sin(x) / tan(x) -> cos(x). */
4493 (simplify
4494 (rdiv (SIN:s @0) (TAN:s @0))
4495 (if (! HONOR_NANS (@0)
4496 && ! HONOR_INFINITIES (@0))
c9e926ce 4497 (COS @0)))
53f3cd25
RS
4498
4499 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4500 (simplify
4501 (rdiv (TAN:s @0) (SIN:s @0))
4502 (if (! HONOR_NANS (@0)
4503 && ! HONOR_INFINITIES (@0))
4504 (rdiv { build_one_cst (type); } (COS @0))))
4505
4506 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4507 (simplify
4508 (mult (POW:s @0 @1) (POW:s @0 @2))
4509 (POW @0 (plus @1 @2)))
4510
4511 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4512 (simplify
4513 (mult (POW:s @0 @1) (POW:s @2 @1))
4514 (POW (mult @0 @2) @1))
4515
de3fbea3
RB
4516 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4517 (simplify
4518 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4519 (POWI (mult @0 @2) @1))
4520
53f3cd25
RS
4521 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4522 (simplify
4523 (rdiv (POW:s @0 REAL_CST@1) @0)
4524 (if (!TREE_OVERFLOW (@1))
4525 (POW @0 (minus @1 { build_one_cst (type); }))))
4526
4527 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4528 (simplify
4529 (rdiv @0 (POW:s @1 @2))
4530 (mult @0 (POW @1 (negate @2))))
4531
4532 (for sqrts (SQRT)
4533 cbrts (CBRT)
4534 pows (POW)
4535 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4536 (simplify
4537 (sqrts (sqrts @0))
4538 (pows @0 { build_real (type, dconst_quarter ()); }))
4539 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4540 (simplify
4541 (sqrts (cbrts @0))
4542 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4543 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4544 (simplify
4545 (cbrts (sqrts @0))
4546 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4547 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4548 (simplify
4549 (cbrts (cbrts tree_expr_nonnegative_p@0))
4550 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4551 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4552 (simplify
4553 (sqrts (pows @0 @1))
4554 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4555 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4556 (simplify
4557 (cbrts (pows tree_expr_nonnegative_p@0 @1))
b4838d77
RS
4558 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4559 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4560 (simplify
4561 (pows (sqrts @0) @1)
4562 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4563 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4564 (simplify
4565 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4566 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4567 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4568 (simplify
4569 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4570 (pows @0 (mult @1 @2))))
abcc43f5
RS
4571
4572 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4573 (simplify
4574 (CABS (complex @0 @0))
96285749
RS
4575 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4576
4d7836c4
RS
4577 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4578 (simplify
4579 (HYPOT @0 @0)
4580 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4581
96285749
RS
4582 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4583 (for cexps (CEXP)
4584 exps (EXP)
4585 cexpis (CEXPI)
4586 (simplify
4587 (cexps compositional_complex@0)
4588 (if (targetm.libc_has_function (function_c99_math_complex))
4589 (complex
4590 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4591 (mult @1 (imagpart @2)))))))
e18c1d66 4592
67dbe582
RS
4593(if (canonicalize_math_p ())
4594 /* floor(x) -> trunc(x) if x is nonnegative. */
c6cfa2bf
MM
4595 (for floors (FLOOR_ALL)
4596 truncs (TRUNC_ALL)
67dbe582
RS
4597 (simplify
4598 (floors tree_expr_nonnegative_p@0)
4599 (truncs @0))))
4600
4601(match double_value_p
4602 @0
4603 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4604(for froms (BUILT_IN_TRUNCL
4605 BUILT_IN_FLOORL
4606 BUILT_IN_CEILL
4607 BUILT_IN_ROUNDL
4608 BUILT_IN_NEARBYINTL
4609 BUILT_IN_RINTL)
4610 tos (BUILT_IN_TRUNC
4611 BUILT_IN_FLOOR
4612 BUILT_IN_CEIL
4613 BUILT_IN_ROUND
4614 BUILT_IN_NEARBYINT
4615 BUILT_IN_RINT)
4616 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4617 (if (optimize && canonicalize_math_p ())
4618 (simplify
4619 (froms (convert double_value_p@0))
4620 (convert (tos @0)))))
4621
4622(match float_value_p
4623 @0
4624 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4625(for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4626 BUILT_IN_FLOORL BUILT_IN_FLOOR
4627 BUILT_IN_CEILL BUILT_IN_CEIL
4628 BUILT_IN_ROUNDL BUILT_IN_ROUND
4629 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4630 BUILT_IN_RINTL BUILT_IN_RINT)
4631 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4632 BUILT_IN_FLOORF BUILT_IN_FLOORF
4633 BUILT_IN_CEILF BUILT_IN_CEILF
4634 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4635 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4636 BUILT_IN_RINTF BUILT_IN_RINTF)
4637 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4638 if x is a float. */
5dac7dbd
JDA
4639 (if (optimize && canonicalize_math_p ()
4640 && targetm.libc_has_function (function_c99_misc))
67dbe582
RS
4641 (simplify
4642 (froms (convert float_value_p@0))
4643 (convert (tos @0)))))
4644
543a9bcd
RS
4645(for froms (XFLOORL XCEILL XROUNDL XRINTL)
4646 tos (XFLOOR XCEIL XROUND XRINT)
4647 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4648 (if (optimize && canonicalize_math_p ())
4649 (simplify
4650 (froms (convert double_value_p@0))
4651 (tos @0))))
4652
4653(for froms (XFLOORL XCEILL XROUNDL XRINTL
4654 XFLOOR XCEIL XROUND XRINT)
4655 tos (XFLOORF XCEILF XROUNDF XRINTF)
4656 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4657 if x is a float. */
4658 (if (optimize && canonicalize_math_p ())
4659 (simplify
4660 (froms (convert float_value_p@0))
4661 (tos @0))))
4662
4663(if (canonicalize_math_p ())
4664 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4665 (for floors (IFLOOR LFLOOR LLFLOOR)
4666 (simplify
4667 (floors tree_expr_nonnegative_p@0)
4668 (fix_trunc @0))))
4669
4670(if (canonicalize_math_p ())
4671 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4672 (for fns (IFLOOR LFLOOR LLFLOOR
4673 ICEIL LCEIL LLCEIL
4674 IROUND LROUND LLROUND)
4675 (simplify
4676 (fns integer_valued_real_p@0)
4677 (fix_trunc @0)))
4678 (if (!flag_errno_math)
4679 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4680 (for rints (IRINT LRINT LLRINT)
4681 (simplify
4682 (rints integer_valued_real_p@0)
4683 (fix_trunc @0)))))
4684
4685(if (canonicalize_math_p ())
4686 (for ifn (IFLOOR ICEIL IROUND IRINT)
4687 lfn (LFLOOR LCEIL LROUND LRINT)
4688 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4689 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4690 sizeof (int) == sizeof (long). */
4691 (if (TYPE_PRECISION (integer_type_node)
4692 == TYPE_PRECISION (long_integer_type_node))
4693 (simplify
4694 (ifn @0)
4695 (lfn:long_integer_type_node @0)))
4696 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4697 sizeof (long long) == sizeof (long). */
4698 (if (TYPE_PRECISION (long_long_integer_type_node)
4699 == TYPE_PRECISION (long_integer_type_node))
4700 (simplify
4701 (llfn @0)
4702 (lfn:long_integer_type_node @0)))))
4703
92c52eab
RS
4704/* cproj(x) -> x if we're ignoring infinities. */
4705(simplify
4706 (CPROJ @0)
4707 (if (!HONOR_INFINITIES (type))
4708 @0))
4709
4534c203
RB
4710/* If the real part is inf and the imag part is known to be
4711 nonnegative, return (inf + 0i). */
4712(simplify
4713 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4714 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
92c52eab
RS
4715 { build_complex_inf (type, false); }))
4716
4534c203
RB
4717/* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4718(simplify
4719 (CPROJ (complex @0 REAL_CST@1))
4720 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
92c52eab 4721 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4534c203 4722
b4838d77
RS
4723(for pows (POW)
4724 sqrts (SQRT)
4725 cbrts (CBRT)
4726 (simplify
4727 (pows @0 REAL_CST@1)
4728 (with {
4729 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4730 REAL_VALUE_TYPE tmp;
4731 }
4732 (switch
4733 /* pow(x,0) -> 1. */
4734 (if (real_equal (value, &dconst0))
4735 { build_real (type, dconst1); })
4736 /* pow(x,1) -> x. */
4737 (if (real_equal (value, &dconst1))
4738 @0)
4739 /* pow(x,-1) -> 1/x. */
4740 (if (real_equal (value, &dconstm1))
4741 (rdiv { build_real (type, dconst1); } @0))
4742 /* pow(x,0.5) -> sqrt(x). */
4743 (if (flag_unsafe_math_optimizations
4744 && canonicalize_math_p ()
4745 && real_equal (value, &dconsthalf))
4746 (sqrts @0))
4747 /* pow(x,1/3) -> cbrt(x). */
4748 (if (flag_unsafe_math_optimizations
4749 && canonicalize_math_p ()
4750 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4751 real_equal (value, &tmp)))
4752 (cbrts @0))))))
4534c203 4753
5ddc84ca
RS
4754/* powi(1,x) -> 1. */
4755(simplify
4756 (POWI real_onep@0 @1)
4757 @0)
4758
4759(simplify
4760 (POWI @0 INTEGER_CST@1)
4761 (switch
4762 /* powi(x,0) -> 1. */
8e6cdc90 4763 (if (wi::to_wide (@1) == 0)
5ddc84ca
RS
4764 { build_real (type, dconst1); })
4765 /* powi(x,1) -> x. */
8e6cdc90 4766 (if (wi::to_wide (@1) == 1)
5ddc84ca
RS
4767 @0)
4768 /* powi(x,-1) -> 1/x. */
8e6cdc90 4769 (if (wi::to_wide (@1) == -1)
5ddc84ca
RS
4770 (rdiv { build_real (type, dconst1); } @0))))
4771
03cc70b5 4772/* Narrowing of arithmetic and logical operations.
be144838
JL
4773
4774 These are conceptually similar to the transformations performed for
4775 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4776 term we want to move all that code out of the front-ends into here. */
4777
4778/* If we have a narrowing conversion of an arithmetic operation where
4779 both operands are widening conversions from the same type as the outer
4780 narrowing conversion. Then convert the innermost operands to a suitable
9c582551 4781 unsigned type (to avoid introducing undefined behavior), perform the
be144838
JL
4782 operation and convert the result to the desired type. */
4783(for op (plus minus)
4784 (simplify
93f90bec 4785 (convert (op:s (convert@2 @0) (convert?@3 @1)))
be144838
JL
4786 (if (INTEGRAL_TYPE_P (type)
4787 /* We check for type compatibility between @0 and @1 below,
4788 so there's no need to check that @1/@3 are integral types. */
4789 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4790 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4791 /* The precision of the type of each operand must match the
4792 precision of the mode of each operand, similarly for the
4793 result. */
2be65d9e
RS
4794 && type_has_mode_precision_p (TREE_TYPE (@0))
4795 && type_has_mode_precision_p (TREE_TYPE (@1))
4796 && type_has_mode_precision_p (type)
be144838
JL
4797 /* The inner conversion must be a widening conversion. */
4798 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
93f90bec
BC
4799 && types_match (@0, type)
4800 && (types_match (@0, @1)
4801 /* Or the second operand is const integer or converted const
4802 integer from valueize. */
4803 || TREE_CODE (@1) == INTEGER_CST))
be144838 4804 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
93f90bec 4805 (op @0 (convert @1))
8fdc6c67 4806 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
93f90bec
BC
4807 (convert (op (convert:utype @0)
4808 (convert:utype @1))))))))
48451e8f
JL
4809
4810/* This is another case of narrowing, specifically when there's an outer
4811 BIT_AND_EXPR which masks off bits outside the type of the innermost
4812 operands. Like the previous case we have to convert the operands
9c582551 4813 to unsigned types to avoid introducing undefined behavior for the
48451e8f
JL
4814 arithmetic operation. */
4815(for op (minus plus)
8fdc6c67
RB
4816 (simplify
4817 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4818 (if (INTEGRAL_TYPE_P (type)
4819 /* We check for type compatibility between @0 and @1 below,
4820 so there's no need to check that @1/@3 are integral types. */
4821 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4822 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4823 /* The precision of the type of each operand must match the
4824 precision of the mode of each operand, similarly for the
4825 result. */
2be65d9e
RS
4826 && type_has_mode_precision_p (TREE_TYPE (@0))
4827 && type_has_mode_precision_p (TREE_TYPE (@1))
4828 && type_has_mode_precision_p (type)
8fdc6c67
RB
4829 /* The inner conversion must be a widening conversion. */
4830 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4831 && types_match (@0, @1)
4832 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4833 <= TYPE_PRECISION (TREE_TYPE (@0)))
8e6cdc90
RS
4834 && (wi::to_wide (@4)
4835 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4836 true, TYPE_PRECISION (type))) == 0)
8fdc6c67
RB
4837 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4838 (with { tree ntype = TREE_TYPE (@0); }
4839 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4840 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4841 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4842 (convert:utype @4))))))))
4f7a5692 4843
03cc70b5 4844/* Transform (@0 < @1 and @0 < @2) to use min,
4f7a5692 4845 (@0 > @1 and @0 > @2) to use max */
dac920e8
MG
4846(for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4847 op (lt le gt ge lt le gt ge )
4848 ext (min min max max max max min min )
4f7a5692 4849 (simplify
dac920e8 4850 (logic (op:cs @0 @1) (op:cs @0 @2))
4618c453
RB
4851 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4852 && TREE_CODE (@0) != INTEGER_CST)
4f7a5692
MC
4853 (op @0 (ext @1 @2)))))
4854
7317ef4a
RS
4855(simplify
4856 /* signbit(x) -> 0 if x is nonnegative. */
4857 (SIGNBIT tree_expr_nonnegative_p@0)
4858 { integer_zero_node; })
4859
4860(simplify
4861 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4862 (SIGNBIT @0)
4863 (if (!HONOR_SIGNED_ZEROS (@0))
4864 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
a8b85ce9
MG
4865
4866/* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4867(for cmp (eq ne)
4868 (for op (plus minus)
4869 rop (minus plus)
4870 (simplify
4871 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4872 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4873 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4874 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4875 && !TYPE_SATURATING (TREE_TYPE (@0)))
4876 (with { tree res = int_const_binop (rop, @2, @1); }
75473a91
RB
4877 (if (TREE_OVERFLOW (res)
4878 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
a8b85ce9
MG
4879 { constant_boolean_node (cmp == NE_EXPR, type); }
4880 (if (single_use (@3))
11c1e63c
JJ
4881 (cmp @0 { TREE_OVERFLOW (res)
4882 ? drop_tree_overflow (res) : res; }))))))))
a8b85ce9
MG
4883(for cmp (lt le gt ge)
4884 (for op (plus minus)
4885 rop (minus plus)
4886 (simplify
4887 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4888 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4889 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4890 (with { tree res = int_const_binop (rop, @2, @1); }
4891 (if (TREE_OVERFLOW (res))
4892 {
4893 fold_overflow_warning (("assuming signed overflow does not occur "
4894 "when simplifying conditional to constant"),
4895 WARN_STRICT_OVERFLOW_CONDITIONAL);
4896 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4897 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8e6cdc90
RS
4898 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4899 TYPE_SIGN (TREE_TYPE (@1)))
a8b85ce9
MG
4900 != (op == MINUS_EXPR);
4901 constant_boolean_node (less == ovf_high, type);
4902 }
4903 (if (single_use (@3))
4904 (with
4905 {
4906 fold_overflow_warning (("assuming signed overflow does not occur "
4907 "when changing X +- C1 cmp C2 to "
4908 "X cmp C2 -+ C1"),
4909 WARN_STRICT_OVERFLOW_COMPARISON);
4910 }
4911 (cmp @0 { res; })))))))))
d3e40b76
RB
4912
4913/* Canonicalizations of BIT_FIELD_REFs. */
4914
6ec96dcb
RB
4915(simplify
4916 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
4917 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
4918
4919(simplify
4920 (BIT_FIELD_REF (view_convert @0) @1 @2)
4921 (BIT_FIELD_REF @0 @1 @2))
4922
4923(simplify
4924 (BIT_FIELD_REF @0 @1 integer_zerop)
4925 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
4926 (view_convert @0)))
4927
d3e40b76
RB
4928(simplify
4929 (BIT_FIELD_REF @0 @1 @2)
4930 (switch
4931 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4932 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4933 (switch
4934 (if (integer_zerop (@2))
4935 (view_convert (realpart @0)))
4936 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4937 (view_convert (imagpart @0)))))
4938 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4939 && INTEGRAL_TYPE_P (type)
171f6f05
RB
4940 /* On GIMPLE this should only apply to register arguments. */
4941 && (! GIMPLE || is_gimple_reg (@0))
d3e40b76
RB
4942 /* A bit-field-ref that referenced the full argument can be stripped. */
4943 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4944 && integer_zerop (@2))
4945 /* Low-parts can be reduced to integral conversions.
4946 ??? The following doesn't work for PDP endian. */
4947 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4948 /* Don't even think about BITS_BIG_ENDIAN. */
4949 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4950 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4951 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4952 ? (TYPE_PRECISION (TREE_TYPE (@0))
4953 - TYPE_PRECISION (type))
4954 : 0)) == 0)))
4955 (convert @0))))
4956
4957/* Simplify vector extracts. */
4958
4959(simplify
4960 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4961 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4962 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4963 || (VECTOR_TYPE_P (type)
4964 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4965 (with
4966 {
4967 tree ctor = (TREE_CODE (@0) == SSA_NAME
4968 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4969 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4970 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4971 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4972 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4973 }
4974 (if (n != 0
4975 && (idx % width) == 0
4976 && (n % width) == 0
928686b1
RS
4977 && known_le ((idx + n) / width,
4978 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
d3e40b76
RB
4979 (with
4980 {
4981 idx = idx / width;
4982 n = n / width;
4983 /* Constructor elements can be subvectors. */
d34457c1 4984 poly_uint64 k = 1;
d3e40b76
RB
4985 if (CONSTRUCTOR_NELTS (ctor) != 0)
4986 {
4987 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4988 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4989 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4990 }
d34457c1 4991 unsigned HOST_WIDE_INT elt, count, const_k;
d3e40b76
RB
4992 }
4993 (switch
4994 /* We keep an exact subset of the constructor elements. */
d34457c1 4995 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
d3e40b76
RB
4996 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4997 { build_constructor (type, NULL); }
d34457c1
RS
4998 (if (count == 1)
4999 (if (elt < CONSTRUCTOR_NELTS (ctor))
4c1da8ea 5000 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
d34457c1 5001 { build_zero_cst (type); })
d3e40b76 5002 {
d34457c1
RS
5003 vec<constructor_elt, va_gc> *vals;
5004 vec_alloc (vals, count);
5005 for (unsigned i = 0;
5006 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5007 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5008 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5009 build_constructor (type, vals);
5010 })))
d3e40b76 5011 /* The bitfield references a single constructor element. */
d34457c1
RS
5012 (if (k.is_constant (&const_k)
5013 && idx + n <= (idx / const_k + 1) * const_k)
d3e40b76 5014 (switch
d34457c1 5015 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
d3e40b76 5016 { build_zero_cst (type); })
d34457c1 5017 (if (n == const_k)
4c1da8ea 5018 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
d34457c1
RS
5019 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5020 @1 { bitsize_int ((idx % const_k) * width); })))))))))
92e29a5e
RB
5021
5022/* Simplify a bit extraction from a bit insertion for the cases with
5023 the inserted element fully covering the extraction or the insertion
5024 not touching the extraction. */
5025(simplify
5026 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
5027 (with
5028 {
5029 unsigned HOST_WIDE_INT isize;
5030 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5031 isize = TYPE_PRECISION (TREE_TYPE (@1));
5032 else
5033 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
5034 }
5035 (switch
8e6cdc90
RS
5036 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
5037 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
5038 wi::to_wide (@ipos) + isize))
92e29a5e 5039 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8e6cdc90
RS
5040 wi::to_wide (@rpos)
5041 - wi::to_wide (@ipos)); }))
5042 (if (wi::geu_p (wi::to_wide (@ipos),
5043 wi::to_wide (@rpos) + wi::to_wide (@rsize))
5044 || wi::geu_p (wi::to_wide (@rpos),
5045 wi::to_wide (@ipos) + isize))
92e29a5e 5046 (BIT_FIELD_REF @0 @rsize @rpos)))))
c566cc9f 5047
c453ccc2
RS
5048(if (canonicalize_math_after_vectorization_p ())
5049 (for fmas (FMA)
5050 (simplify
5051 (fmas:c (negate @0) @1 @2)
5052 (IFN_FNMA @0 @1 @2))
5053 (simplify
5054 (fmas @0 @1 (negate @2))
5055 (IFN_FMS @0 @1 @2))
5056 (simplify
5057 (fmas:c (negate @0) @1 (negate @2))
5058 (IFN_FNMS @0 @1 @2))
5059 (simplify
5060 (negate (fmas@3 @0 @1 @2))
5061 (if (single_use (@3))
5062 (IFN_FNMS @0 @1 @2))))
5063
c566cc9f 5064 (simplify
c453ccc2
RS
5065 (IFN_FMS:c (negate @0) @1 @2)
5066 (IFN_FNMS @0 @1 @2))
5067 (simplify
5068 (IFN_FMS @0 @1 (negate @2))
5069 (IFN_FMA @0 @1 @2))
5070 (simplify
5071 (IFN_FMS:c (negate @0) @1 (negate @2))
c566cc9f
RS
5072 (IFN_FNMA @0 @1 @2))
5073 (simplify
c453ccc2
RS
5074 (negate (IFN_FMS@3 @0 @1 @2))
5075 (if (single_use (@3))
5076 (IFN_FNMA @0 @1 @2)))
5077
5078 (simplify
5079 (IFN_FNMA:c (negate @0) @1 @2)
5080 (IFN_FMA @0 @1 @2))
c566cc9f 5081 (simplify
c453ccc2 5082 (IFN_FNMA @0 @1 (negate @2))
c566cc9f
RS
5083 (IFN_FNMS @0 @1 @2))
5084 (simplify
c453ccc2
RS
5085 (IFN_FNMA:c (negate @0) @1 (negate @2))
5086 (IFN_FMS @0 @1 @2))
5087 (simplify
5088 (negate (IFN_FNMA@3 @0 @1 @2))
c566cc9f 5089 (if (single_use (@3))
c453ccc2 5090 (IFN_FMS @0 @1 @2)))
c566cc9f 5091
c453ccc2
RS
5092 (simplify
5093 (IFN_FNMS:c (negate @0) @1 @2)
5094 (IFN_FMS @0 @1 @2))
5095 (simplify
5096 (IFN_FNMS @0 @1 (negate @2))
5097 (IFN_FNMA @0 @1 @2))
5098 (simplify
5099 (IFN_FNMS:c (negate @0) @1 (negate @2))
5100 (IFN_FMA @0 @1 @2))
5101 (simplify
5102 (negate (IFN_FNMS@3 @0 @1 @2))
c566cc9f 5103 (if (single_use (@3))
c453ccc2 5104 (IFN_FMA @0 @1 @2))))
ba6557e2
RS
5105
5106/* POPCOUNT simplifications. */
5107(for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
5108 BUILT_IN_POPCOUNTIMAX)
5109 /* popcount(X&1) is nop_expr(X&1). */
5110 (simplify
5111 (popcount @0)
5112 (if (tree_nonzero_bits (@0) == 1)
5113 (convert @0)))
5114 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
5115 (simplify
5116 (plus (popcount:s @0) (popcount:s @1))
5117 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
5118 (popcount (bit_ior @0 @1))))
5119 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
5120 (for cmp (le eq ne gt)
5121 rep (eq eq ne ne)
5122 (simplify
5123 (cmp (popcount @0) integer_zerop)
5124 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
0d2b3bca
RS
5125
5126/* Simplify:
5127
5128 a = a1 op a2
5129 r = c ? a : b;
5130
5131 to:
5132
5133 r = c ? a1 op a2 : b;
5134
5135 if the target can do it in one go. This makes the operation conditional
5136 on c, so could drop potentially-trapping arithmetic, but that's a valid
5137 simplification if the result of the operation isn't needed. */
5138(for uncond_op (UNCOND_BINARY)
5139 cond_op (COND_BINARY)
5140 (simplify
5141 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5142 (with { tree op_type = TREE_TYPE (@4); }
5143 (if (element_precision (type) == element_precision (op_type))
5144 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5145 (simplify
5146 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5147 (with { tree op_type = TREE_TYPE (@4); }
5148 (if (element_precision (type) == element_precision (op_type))
5149 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
6a86928d 5150
b41d1f6e
RS
5151/* Same for ternary operations. */
5152(for uncond_op (UNCOND_TERNARY)
5153 cond_op (COND_TERNARY)
5154 (simplify
5155 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5156 (with { tree op_type = TREE_TYPE (@5); }
5157 (if (element_precision (type) == element_precision (op_type))
5158 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5159 (simplify
5160 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5161 (with { tree op_type = TREE_TYPE (@5); }
5162 (if (element_precision (type) == element_precision (op_type))
5163 (view_convert (cond_op (bit_not @0) @2 @3 @4
5164 (view_convert:op_type @1)))))))
5165
6a86928d
RS
5166/* Detect cases in which a VEC_COND_EXPR effectively replaces the
5167 "else" value of an IFN_COND_*. */
5168(for cond_op (COND_BINARY)
5169 (simplify
5170 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
5171 (with { tree op_type = TREE_TYPE (@3); }
5172 (if (element_precision (type) == element_precision (op_type))
2c58d42c
RS
5173 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
5174 (simplify
5175 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
5176 (with { tree op_type = TREE_TYPE (@5); }
5177 (if (inverse_conditions_p (@0, @2)
5178 && element_precision (type) == element_precision (op_type))
5179 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
b41d1f6e
RS
5180
5181/* Same for ternary operations. */
5182(for cond_op (COND_TERNARY)
5183 (simplify
5184 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5185 (with { tree op_type = TREE_TYPE (@4); }
5186 (if (element_precision (type) == element_precision (op_type))
2c58d42c
RS
5187 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5188 (simplify
5189 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5190 (with { tree op_type = TREE_TYPE (@6); }
5191 (if (inverse_conditions_p (@0, @2)
5192 && element_precision (type) == element_precision (op_type))
5193 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
a19f98d5
RS
5194
5195/* For pointers @0 and @2 and nonnegative constant offset @1, look for
5196 expressions like:
5197
5198 A: (@0 + @1 < @2) | (@2 + @1 < @0)
5199 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5200
5201 If pointers are known not to wrap, B checks whether @1 bytes starting
5202 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5203 bytes. A is more efficiently tested as:
5204
5205 A: (sizetype) (@0 + @1 - @2) > @1 * 2
5206
5207 The equivalent expression for B is given by replacing @1 with @1 - 1:
5208
5209 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5210
5211 @0 and @2 can be swapped in both expressions without changing the result.
5212
5213 The folds rely on sizetype's being unsigned (which is always true)
5214 and on its being the same width as the pointer (which we have to check).
5215
5216 The fold replaces two pointer_plus expressions, two comparisons and
5217 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5218 the best case it's a saving of two operations. The A fold retains one
5219 of the original pointer_pluses, so is a win even if both pointer_pluses
5220 are used elsewhere. The B fold is a wash if both pointer_pluses are
5221 used elsewhere, since all we end up doing is replacing a comparison with
5222 a pointer_plus. We do still apply the fold under those circumstances
5223 though, in case applying it to other conditions eventually makes one of the
5224 pointer_pluses dead. */
5225(for ior (truth_orif truth_or bit_ior)
5226 (for cmp (le lt)
5227 (simplify
5228 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5229 (cmp:cs (pointer_plus@4 @2 @1) @0))
5230 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5231 && TYPE_OVERFLOW_WRAPS (sizetype)
5232 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5233 /* Calculate the rhs constant. */
5234 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5235 offset_int rhs = off * 2; }
5236 /* Always fails for negative values. */
5237 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5238 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5239 pick a canonical order. This increases the chances of using the
5240 same pointer_plus in multiple checks. */
5241 (with { bool swap_p = tree_swap_operands_p (@0, @2);
5242 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5243 (if (cmp == LT_EXPR)
5244 (gt (convert:sizetype
5245 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5246 { swap_p ? @0 : @2; }))
5247 { rhs_tree; })
5248 (gt (convert:sizetype
5249 (pointer_diff:ssizetype
5250 (pointer_plus { swap_p ? @2 : @0; }
5251 { wide_int_to_tree (sizetype, off); })
5252 { swap_p ? @0 : @2; }))
5253 { rhs_tree; })))))))))
f4bf2aab
RS
5254
5255/* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
5256 element of @1. */
5257(for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
5258 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
5259 (with { int i = single_nonzero_element (@1); }
5260 (if (i >= 0)
5261 (with { tree elt = vector_cst_elt (@1, i);
5262 tree elt_type = TREE_TYPE (elt);
5263 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
5264 tree size = bitsize_int (elt_bits);
5265 tree pos = bitsize_int (elt_bits * i); }
5266 (view_convert
5267 (bit_and:elt_type
5268 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
5269 { elt; })))))))