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