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