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