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