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