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