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