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