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