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