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