]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/match.pd
Add IFN_COND_{MUL,DIV,MOD,RDIV}
[thirdparty/gcc.git] / gcc / match.pd
CommitLineData
2165588a 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
8e8f6434 5 Copyright (C) 2014-2018 Free Software Foundation, Inc.
2165588a 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
662274ee 28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
18b38664 29 integer_each_onep integer_truep integer_nonzerop
662274ee 30 real_zerop real_onep real_minus_onep
06e4870e 31 zerop
2ad7e37a 32 CONSTANT_CLASS_P
71486465 33 tree_expr_nonnegative_p
70229d3b 34 tree_expr_nonzero_p
6f5f406a 35 integer_valued_real_p
18b38664 36 integer_pow2p
37 HONOR_NANS)
55534d34 38
9ec260f0 39/* Operator lists. */
40(define_operator_list tcc_comparison
41 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
42(define_operator_list inverted_tcc_comparison
43 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
44(define_operator_list inverted_tcc_comparison_with_nans
45 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
0c3c84e3 46(define_operator_list swapped_tcc_comparison
47 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
c915fa32 48(define_operator_list simple_comparison lt le eq ne ge gt)
49(define_operator_list swapped_simple_comparison gt ge eq ne le lt)
50
ff690865 51#include "cfn-operators.pd"
89ff0c87 52
1117ed35 53/* Define operand lists for math rounding functions {,i,l,ll}FN,
54 where the versions prefixed with "i" return an int, those prefixed with
55 "l" return a long and those prefixed with "ll" return a long long.
56
57 Also define operand lists:
58
59 X<FN>F for all float functions, in the order i, l, ll
60 X<FN> for all double functions, in the same order
61 X<FN>L for all long double functions, in the same order. */
62#define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
1117ed35 63 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
64 BUILT_IN_L##FN##F \
65 BUILT_IN_LL##FN##F) \
66 (define_operator_list X##FN BUILT_IN_I##FN \
67 BUILT_IN_L##FN \
68 BUILT_IN_LL##FN) \
69 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
70 BUILT_IN_L##FN##L \
71 BUILT_IN_LL##FN##L)
72
1117ed35 73DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
74DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
75DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
76DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
dbc7e6ae 77
78/* Binary operations and their associated IFN_COND_* function. */
79(define_operator_list UNCOND_BINARY
80 plus minus
cdb4d5d0 81 mult trunc_div trunc_mod rdiv
dbc7e6ae 82 min max
83 bit_and bit_ior bit_xor)
84(define_operator_list COND_BINARY
85 IFN_COND_ADD IFN_COND_SUB
cdb4d5d0 86 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
dbc7e6ae 87 IFN_COND_MIN IFN_COND_MAX
88 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
049318f9 89
90/* As opposed to convert?, this still creates a single pattern, so
91 it is not a suitable replacement for convert? in all cases. */
92(match (nop_convert @0)
93 (convert @0)
94 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
95(match (nop_convert @0)
96 (view_convert @0)
97 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
f08ee65f 98 && known_eq (TYPE_VECTOR_SUBPARTS (type),
99 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
049318f9 100 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
101/* This one has to be last, or it shadows the others. */
102(match (nop_convert @0)
103 @0)
9ec260f0 104
55534d34 105/* Simplifications of operations with one constant operand and
58810b92 106 simplifications to constants or single values. */
55534d34 107
108(for op (plus pointer_plus minus bit_ior bit_xor)
109 (simplify
110 (op @0 integer_zerop)
111 (non_lvalue @0)))
112
770ae4bb 113/* 0 +p index -> (type)index */
114(simplify
115 (pointer_plus integer_zerop @1)
116 (non_lvalue (convert @1)))
117
e52afc20 118/* ptr - 0 -> (type)ptr */
119(simplify
120 (pointer_diff @0 integer_zerop)
121 (convert @0))
122
6a78ea5d 123/* See if ARG1 is zero and X + ARG1 reduces to X.
124 Likewise if the operands are reversed. */
125(simplify
126 (plus:c @0 real_zerop@1)
127 (if (fold_real_zero_addition_p (type, @1, 0))
128 (non_lvalue @0)))
129
130/* See if ARG1 is zero and X - ARG1 reduces to X. */
131(simplify
132 (minus @0 real_zerop@1)
133 (if (fold_real_zero_addition_p (type, @1, 1))
134 (non_lvalue @0)))
135
55534d34 136/* Simplify x - x.
137 This is unsafe for certain floats even in non-IEEE formats.
138 In IEEE, it is unsafe because it does wrong for NaNs.
139 Also note that operand_equal_p is always false if an operand
140 is volatile. */
141(simplify
6a78ea5d 142 (minus @0 @0)
93633022 143 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
6a78ea5d 144 { build_zero_cst (type); }))
57e83b58 145(simplify
146 (pointer_diff @@0 @0)
147 { build_zero_cst (type); })
55534d34 148
149(simplify
6a78ea5d 150 (mult @0 integer_zerop@1)
151 @1)
152
153/* Maybe fold x * 0 to 0. The expressions aren't the same
154 when x is NaN, since x * 0 is also NaN. Nor are they the
155 same in modes with signed zeros, since multiplying a
156 negative value by 0 gives -0, not +0. */
157(simplify
158 (mult @0 real_zerop@1)
c8047e01 159 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
6a78ea5d 160 @1))
161
162/* In IEEE floating point, x*1 is not equivalent to x for snans.
163 Likewise for complex arithmetic with signed zeros. */
164(simplify
165 (mult @0 real_onep)
c8047e01 166 (if (!HONOR_SNANS (type)
167 && (!HONOR_SIGNED_ZEROS (type)
6a78ea5d 168 || !COMPLEX_FLOAT_TYPE_P (type)))
169 (non_lvalue @0)))
170
171/* Transform x * -1.0 into -x. */
172(simplify
173 (mult @0 real_minus_onep)
c8047e01 174 (if (!HONOR_SNANS (type)
175 && (!HONOR_SIGNED_ZEROS (type)
6a78ea5d 176 || !COMPLEX_FLOAT_TYPE_P (type)))
177 (negate @0)))
55534d34 178
6e0b4434 179(for cmp (gt ge lt le)
180 outp (convert convert negate negate)
181 outn (negate negate convert convert)
182 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
183 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
184 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
185 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
186 (simplify
187 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
188 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
189 && types_match (type, TREE_TYPE (@0)))
190 (switch
191 (if (types_match (type, float_type_node))
192 (BUILT_IN_COPYSIGNF @1 (outp @0)))
193 (if (types_match (type, double_type_node))
194 (BUILT_IN_COPYSIGN @1 (outp @0)))
195 (if (types_match (type, long_double_type_node))
196 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
197 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
198 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
199 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
200 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
201 (simplify
202 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
203 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
204 && types_match (type, TREE_TYPE (@0)))
205 (switch
206 (if (types_match (type, float_type_node))
207 (BUILT_IN_COPYSIGNF @1 (outn @0)))
208 (if (types_match (type, double_type_node))
209 (BUILT_IN_COPYSIGN @1 (outn @0)))
210 (if (types_match (type, long_double_type_node))
211 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
212
213/* Transform X * copysign (1.0, X) into abs(X). */
214(simplify
054e9558 215 (mult:c @0 (COPYSIGN_ALL real_onep @0))
6e0b4434 216 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
217 (abs @0)))
218
219/* Transform X * copysign (1.0, -X) into -abs(X). */
220(simplify
054e9558 221 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
6e0b4434 222 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
223 (negate (abs @0))))
224
225/* Transform copysign (CST, X) into copysign (ABS(CST), X). */
226(simplify
054e9558 227 (COPYSIGN_ALL REAL_CST@0 @1)
6e0b4434 228 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
054e9558 229 (COPYSIGN_ALL (negate @0) @1)))
6e0b4434 230
6062d7aa 231/* X * 1, X / 1 -> X. */
55534d34 232(for op (mult trunc_div ceil_div floor_div round_div exact_div)
233 (simplify
234 (op @0 integer_onep)
235 (non_lvalue @0)))
236
f769c6cf 237/* (A / (1 << B)) -> (A >> B).
238 Only for unsigned A. For signed A, this would not preserve rounding
239 toward zero.
240 For example: (-1 / ( 1 << B)) != -1 >> B. */
241(simplify
242 (trunc_div @0 (lshift integer_onep@1 @2))
243 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
244 && (!VECTOR_TYPE_P (type)
245 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
246 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
247 (rshift @0 @2)))
248
6062d7aa 249/* Preserve explicit divisions by 0: the C++ front-end wants to detect
250 undefined behavior in constexpr evaluation, and assuming that the division
251 traps enables better optimizations than these anyway. */
6a78ea5d 252(for div (trunc_div ceil_div floor_div round_div exact_div)
6062d7aa 253 /* 0 / X is always zero. */
254 (simplify
255 (div integer_zerop@0 @1)
256 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
257 (if (!integer_zerop (@1))
258 @0))
98da8d3b 259 /* X / -1 is -X. */
6a78ea5d 260 (simplify
ccb14741 261 (div @0 integer_minus_onep@1)
262 (if (!TYPE_UNSIGNED (type))
98da8d3b 263 (negate @0)))
6062d7aa 264 /* X / X is one. */
265 (simplify
266 (div @0 @0)
63d693d7 267 /* But not for 0 / 0 so that we can get the proper warnings and errors.
268 And not for _Fract types where we can't build 1. */
269 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
6062d7aa 270 { build_one_cst (type); }))
98da8d3b 271 /* X / abs (X) is X < 0 ? -1 : 1. */
272 (simplify
22b30400 273 (div:C @0 (abs @0))
274 (if (INTEGRAL_TYPE_P (type)
98da8d3b 275 && TYPE_OVERFLOW_UNDEFINED (type))
276 (cond (lt @0 { build_zero_cst (type); })
277 { build_minus_one_cst (type); } { build_one_cst (type); })))
278 /* X / -X is -1. */
279 (simplify
22b30400 280 (div:C @0 (negate @0))
98da8d3b 281 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
282 && TYPE_OVERFLOW_UNDEFINED (type))
283 { build_minus_one_cst (type); })))
6a78ea5d 284
285/* For unsigned integral types, FLOOR_DIV_EXPR is the same as
286 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
287(simplify
288 (floor_div @0 @1)
ccb14741 289 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
290 && TYPE_UNSIGNED (type))
6a78ea5d 291 (trunc_div @0 @1)))
292
62d4473c 293/* Combine two successive divisions. Note that combining ceil_div
294 and floor_div is trickier and combining round_div even more so. */
295(for div (trunc_div exact_div)
2309dbdc 296 (simplify
297 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
298 (with {
299 bool overflow_p;
e3d0f65c 300 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
301 TYPE_SIGN (type), &overflow_p);
2309dbdc 302 }
303 (if (!overflow_p)
d74b7335 304 (div @0 { wide_int_to_tree (type, mul); })
305 (if (TYPE_UNSIGNED (type)
306 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
307 { build_zero_cst (type); })))))
2309dbdc 308
e4f48180 309/* Combine successive multiplications. Similar to above, but handling
310 overflow is different. */
311(simplify
312 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
313 (with {
314 bool overflow_p;
e3d0f65c 315 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
316 TYPE_SIGN (type), &overflow_p);
e4f48180 317 }
318 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
319 otherwise undefined overflow implies that @0 must be zero. */
320 (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
321 (mult @0 { wide_int_to_tree (type, mul); }))))
322
6a78ea5d 323/* Optimize A / A to 1.0 if we don't care about
ccb14741 324 NaNs or Infinities. */
6a78ea5d 325(simplify
326 (rdiv @0 @0)
ccb14741 327 (if (FLOAT_TYPE_P (type)
93633022 328 && ! HONOR_NANS (type)
c8047e01 329 && ! HONOR_INFINITIES (type))
ccb14741 330 { build_one_cst (type); }))
331
332/* Optimize -A / A to -1.0 if we don't care about
333 NaNs or Infinities. */
334(simplify
2f99fc04 335 (rdiv:C @0 (negate @0))
ccb14741 336 (if (FLOAT_TYPE_P (type)
93633022 337 && ! HONOR_NANS (type)
c8047e01 338 && ! HONOR_INFINITIES (type))
ccb14741 339 { build_minus_one_cst (type); }))
6a78ea5d 340
12dabcee 341/* PR71078: x / abs(x) -> copysign (1.0, x) */
342(simplify
343 (rdiv:C (convert? @0) (convert? (abs @0)))
344 (if (SCALAR_FLOAT_TYPE_P (type)
345 && ! HONOR_NANS (type)
346 && ! HONOR_INFINITIES (type))
347 (switch
348 (if (types_match (type, float_type_node))
349 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
350 (if (types_match (type, double_type_node))
351 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
352 (if (types_match (type, long_double_type_node))
353 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
354
6a78ea5d 355/* In IEEE floating point, x/1 is not equivalent to x for snans. */
356(simplify
357 (rdiv @0 real_onep)
c8047e01 358 (if (!HONOR_SNANS (type))
6a78ea5d 359 (non_lvalue @0)))
360
361/* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
362(simplify
363 (rdiv @0 real_minus_onep)
c8047e01 364 (if (!HONOR_SNANS (type))
6a78ea5d 365 (negate @0)))
366
ee5e3723 367(if (flag_reciprocal_math)
07d4034c 368 /* Convert (A/B)/C to A/(B*C). */
ee5e3723 369 (simplify
370 (rdiv (rdiv:s @0 @1) @2)
07d4034c 371 (rdiv @0 (mult @1 @2)))
372
373 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
374 (simplify
375 (rdiv @0 (mult:s @1 REAL_CST@2))
376 (with
377 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
378 (if (tem)
379 (rdiv (mult @0 { tem; } ) @1))))
ee5e3723 380
381 /* Convert A/(B/C) to (A/B)*C */
382 (simplify
383 (rdiv @0 (rdiv:s @1 @2))
384 (mult (rdiv @0 @1) @2)))
385
081337d3 386/* Simplify x / (- y) to -x / y. */
387(simplify
388 (rdiv @0 (negate @1))
389 (rdiv (negate @0) @1))
390
ee5e3723 391/* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
392(for div (trunc_div ceil_div floor_div round_div exact_div)
393 (simplify
394 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
395 (if (integer_pow2p (@2)
396 && tree_int_cst_sgn (@2) > 0
cb7bca5f 397 && tree_nop_conversion_p (type, TREE_TYPE (@0))
e3d0f65c 398 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
399 (rshift (convert @0)
400 { build_int_cst (integer_type_node,
401 wi::exact_log2 (wi::to_wide (@2))); }))))
ee5e3723 402
6a78ea5d 403/* If ARG1 is a constant, we can convert this to a multiply by the
404 reciprocal. This does not have the same rounding properties,
405 so only do this if -freciprocal-math. We can actually
406 always safely do it if ARG1 is a power of two, but it's hard to
407 tell if it is or not in a portable manner. */
408(for cst (REAL_CST COMPLEX_CST VECTOR_CST)
409 (simplify
410 (rdiv @0 cst@1)
411 (if (optimize)
cd6db277 412 (if (flag_reciprocal_math
413 && !real_zerop (@1))
6a78ea5d 414 (with
abe4e033 415 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
6a78ea5d 416 (if (tem)
d74b7335 417 (mult @0 { tem; } )))
418 (if (cst != COMPLEX_CST)
419 (with { tree inverse = exact_inverse (type, @1); }
420 (if (inverse)
421 (mult @0 { inverse; } ))))))))
6a78ea5d 422
6a78ea5d 423(for mod (ceil_mod floor_mod round_mod trunc_mod)
55534d34 424 /* 0 % X is always zero. */
425 (simplify
6a78ea5d 426 (mod integer_zerop@0 @1)
55534d34 427 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
428 (if (!integer_zerop (@1))
429 @0))
430 /* X % 1 is always zero. */
431 (simplify
6a78ea5d 432 (mod @0 integer_onep)
433 { build_zero_cst (type); })
434 /* X % -1 is zero. */
435 (simplify
ccb14741 436 (mod @0 integer_minus_onep@1)
437 (if (!TYPE_UNSIGNED (type))
d792dcdf 438 { build_zero_cst (type); }))
6062d7aa 439 /* X % X is zero. */
440 (simplify
441 (mod @0 @0)
442 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
443 (if (!integer_zerop (@0))
444 { build_zero_cst (type); }))
d792dcdf 445 /* (X % Y) % Y is just X % Y. */
446 (simplify
447 (mod (mod@2 @0 @1) @1)
54c06b77 448 @2)
449 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
450 (simplify
451 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
452 (if (ANY_INTEGRAL_TYPE_P (type)
453 && TYPE_OVERFLOW_UNDEFINED (type)
e3d0f65c 454 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
455 TYPE_SIGN (type)))
54c06b77 456 { build_zero_cst (type); })))
6a78ea5d 457
458/* X % -C is the same as X % C. */
459(simplify
460 (trunc_mod @0 INTEGER_CST@1)
461 (if (TYPE_SIGN (type) == SIGNED
462 && !TREE_OVERFLOW (@1)
e3d0f65c 463 && wi::neg_p (wi::to_wide (@1))
6a78ea5d 464 && !TYPE_OVERFLOW_TRAPS (type)
465 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
466 && !sign_bit_p (@1, @1))
467 (trunc_mod @0 (negate @1))))
55534d34 468
c09f2960 469/* X % -Y is the same as X % Y. */
470(simplify
471 (trunc_mod @0 (convert? (negate @1)))
1bc579ed 472 (if (INTEGRAL_TYPE_P (type)
473 && !TYPE_UNSIGNED (type)
c09f2960 474 && !TYPE_OVERFLOW_TRAPS (type)
7dff0499 475 && tree_nop_conversion_p (type, TREE_TYPE (@1))
476 /* Avoid this transformation if X might be INT_MIN or
477 Y might be -1, because we would then change valid
478 INT_MIN % -(-1) into invalid INT_MIN % -1. */
e3d0f65c 479 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
7dff0499 480 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
481 (TREE_TYPE (@1))))))
c09f2960 482 (trunc_mod @0 (convert @1))))
483
ef5e7d0d 484/* X - (X / Y) * Y is the same as X % Y. */
485(simplify
c76c65ae 486 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
487 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
f801d40f 488 (convert (trunc_mod @0 @1))))
ef5e7d0d 489
c09f2960 490/* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
491 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
492 Also optimize A % (C << N) where C is a power of 2,
493 to A & ((C << N) - 1). */
494(match (power_of_two_cand @1)
495 INTEGER_CST@1)
496(match (power_of_two_cand @1)
497 (lshift INTEGER_CST@1 @2))
498(for mod (trunc_mod floor_mod)
499 (simplify
928ea46c 500 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
c09f2960 501 (if ((TYPE_UNSIGNED (type)
502 || tree_expr_nonnegative_p (@0))
928ea46c 503 && tree_nop_conversion_p (type, TREE_TYPE (@3))
c09f2960 504 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
928ea46c 505 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
c09f2960 506
71486465 507/* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
508(simplify
509 (trunc_div (mult @0 integer_pow2p@1) @1)
510 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
511 (bit_and @0 { wide_int_to_tree
e3d0f65c 512 (type, wi::mask (TYPE_PRECISION (type)
513 - wi::exact_log2 (wi::to_wide (@1)),
71486465 514 false, TYPE_PRECISION (type))); })))
515
20c41e0d 516/* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
517(simplify
518 (mult (trunc_div @0 integer_pow2p@1) @1)
519 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
520 (bit_and @0 (negate @1))))
521
3849736e 522/* Simplify (t * 2) / 2) -> t. */
523(for div (trunc_div ceil_div floor_div round_div exact_div)
524 (simplify
ac947eae 525 (div (mult:c @0 @1) @1)
3849736e 526 (if (ANY_INTEGRAL_TYPE_P (type)
527 && TYPE_OVERFLOW_UNDEFINED (type))
528 @0)))
529
5702129f 530(for op (negate abs)
a614e82c 531 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
532 (for coss (COS COSH)
533 (simplify
534 (coss (op @0))
535 (coss @0)))
536 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
537 (for pows (POW)
538 (simplify
539 (pows (op @0) REAL_CST@1)
540 (with { HOST_WIDE_INT n; }
541 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
a15183eb 542 (pows @0 @1)))))
cbcde149 543 /* Likewise for powi. */
544 (for pows (POWI)
545 (simplify
546 (pows (op @0) INTEGER_CST@1)
e3d0f65c 547 (if ((wi::to_wide (@1) & 1) == 0)
cbcde149 548 (pows @0 @1))))
a15183eb 549 /* Strip negate and abs from both operands of hypot. */
550 (for hypots (HYPOT)
551 (simplify
552 (hypots (op @0) @1)
553 (hypots @0 @1))
554 (simplify
555 (hypots @0 (op @1))
556 (hypots @0 @1)))
557 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
054e9558 558 (for copysigns (COPYSIGN_ALL)
a15183eb 559 (simplify
560 (copysigns (op @0) @1)
561 (copysigns @0 @1))))
562
563/* abs(x)*abs(x) -> x*x. Should be valid for all types. */
564(simplify
565 (mult (abs@1 @0) @1)
566 (mult @0 @0))
567
568/* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
569(for coss (COS COSH)
570 copysigns (COPYSIGN)
571 (simplify
572 (coss (copysigns @0 @1))
573 (coss @0)))
574
575/* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
576(for pows (POW)
577 copysigns (COPYSIGN)
578 (simplify
cbcde149 579 (pows (copysigns @0 @2) REAL_CST@1)
a15183eb 580 (with { HOST_WIDE_INT n; }
581 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
582 (pows @0 @1)))))
cbcde149 583/* Likewise for powi. */
584(for pows (POWI)
585 copysigns (COPYSIGN)
586 (simplify
587 (pows (copysigns @0 @2) INTEGER_CST@1)
e3d0f65c 588 (if ((wi::to_wide (@1) & 1) == 0)
cbcde149 589 (pows @0 @1))))
a15183eb 590
591(for hypots (HYPOT)
592 copysigns (COPYSIGN)
593 /* hypot(copysign(x, y), z) -> hypot(x, z). */
594 (simplify
595 (hypots (copysigns @0 @1) @2)
596 (hypots @0 @2))
597 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
598 (simplify
599 (hypots @0 (copysigns @1 @2))
600 (hypots @0 @1)))
601
eb93b162 602/* copysign(x, CST) -> [-]abs (x). */
054e9558 603(for copysigns (COPYSIGN_ALL)
eb93b162 604 (simplify
605 (copysigns @0 REAL_CST@1)
606 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
607 (negate (abs @0))
608 (abs @0))))
609
a15183eb 610/* copysign(copysign(x, y), z) -> copysign(x, z). */
054e9558 611(for copysigns (COPYSIGN_ALL)
a15183eb 612 (simplify
613 (copysigns (copysigns @0 @1) @2)
614 (copysigns @0 @2)))
615
616/* copysign(x,y)*copysign(x,y) -> x*x. */
054e9558 617(for copysigns (COPYSIGN_ALL)
a15183eb 618 (simplify
619 (mult (copysigns@2 @0 @1) @2)
620 (mult @0 @0)))
621
622/* ccos(-x) -> ccos(x). Similarly for ccosh. */
623(for ccoss (CCOS CCOSH)
624 (simplify
625 (ccoss (negate @0))
626 (ccoss @0)))
5702129f 627
d064d976 628/* cabs(-x) and cos(conj(x)) -> cabs(x). */
629(for ops (conj negate)
630 (for cabss (CABS)
631 (simplify
632 (cabss (ops @0))
633 (cabss @0))))
634
35c8219c 635/* Fold (a * (1 << b)) into (a << b) */
636(simplify
637 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
638 (if (! FLOAT_TYPE_P (type)
51a43c06 639 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
35c8219c 640 (lshift @0 @2)))
641
a6eef1ed 642/* Fold (1 << (C - x)) where C = precision(type) - 1
643 into ((1 << C) >> x). */
644(simplify
645 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
646 (if (INTEGRAL_TYPE_P (type)
12f26ffd 647 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
a6eef1ed 648 && single_use (@1))
649 (if (TYPE_UNSIGNED (type))
650 (rshift (lshift @0 @2) @3)
651 (with
652 { tree utype = unsigned_type_for (type); }
653 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
654
35c8219c 655/* Fold (C1/X)*C2 into (C1*C2)/X. */
656(simplify
b4007655 657 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
658 (if (flag_associative_math
659 && single_use (@3))
35c8219c 660 (with
661 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
662 (if (tem)
663 (rdiv { tem; } @1)))))
664
665/* Simplify ~X & X as zero. */
666(simplify
667 (bit_and:c (convert? @0) (convert? (bit_not @0)))
668 { build_zero_cst (type); })
669
6a0d39d6 670/* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
671(simplify
672 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
673 (if (TYPE_UNSIGNED (type))
674 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
675
4a9eb7eb 676(for bitop (bit_and bit_ior)
677 cmp (eq ne)
8ced4965 678 /* PR35691: Transform
679 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
680 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
4a9eb7eb 681 (simplify
682 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
683 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8ced4965 684 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
685 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
686 (cmp (bit_ior @0 (convert @1)) @2)))
687 /* Transform:
688 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
689 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
690 (simplify
691 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
692 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
693 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
694 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
695 (cmp (bit_and @0 (convert @1)) @2))))
4a9eb7eb 696
7bbdeb32 697/* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
698(simplify
99618e62 699 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
7bbdeb32 700 (minus (bit_xor @0 @1) @1))
701(simplify
702 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
e3d0f65c 703 (if (~wi::to_wide (@2) == wi::to_wide (@1))
7bbdeb32 704 (minus (bit_xor @0 @1) @1)))
705
706/* Fold (A & B) - (A & ~B) into B - (A ^ B). */
707(simplify
a1a80007 708 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
7bbdeb32 709 (minus @1 (bit_xor @0 @1)))
710
20ce76cb 711/* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
712(for op (bit_ior bit_xor plus)
713 (simplify
714 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
715 (bit_xor @0 @1))
716 (simplify
717 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
e3d0f65c 718 (if (~wi::to_wide (@2) == wi::to_wide (@1))
20ce76cb 719 (bit_xor @0 @1))))
ab056ef4 720
721/* PR53979: Transform ((a ^ b) | a) -> (a | b) */
722(simplify
723 (bit_ior:c (bit_xor:c @0 @1) @0)
724 (bit_ior @0 @1))
725
270a468f 726/* (a & ~b) | (a ^ b) --> a ^ b */
727(simplify
728 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
729 @2)
730
731/* (a & ~b) ^ ~a --> ~(a & b) */
732(simplify
733 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
734 (bit_not (bit_and @0 @1)))
735
736/* (a | b) & ~(a ^ b) --> a & b */
737(simplify
738 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
739 (bit_and @0 @1))
740
741/* a | ~(a ^ b) --> a | ~b */
742(simplify
743 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
744 (bit_ior @0 (bit_not @1)))
745
746/* (a | b) | (a &^ b) --> a | b */
747(for op (bit_and bit_xor)
748 (simplify
749 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
750 @2))
751
752/* (a & b) | ~(a ^ b) --> ~(a ^ b) */
753(simplify
754 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
755 @2)
756
757/* ~(~a & b) --> a | ~b */
758(simplify
759 (bit_not (bit_and:cs (bit_not @0) @1))
760 (bit_ior @0 (bit_not @1)))
761
a8719545 762/* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
763#if GIMPLE
764(simplify
765 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
766 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
e3d0f65c 767 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
a8719545 768 (bit_xor @0 @1)))
769#endif
7bbdeb32 770
d792dcdf 771/* X % Y is smaller than Y. */
772(for cmp (lt ge)
773 (simplify
774 (cmp (trunc_mod @0 @1) @1)
775 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
776 { constant_boolean_node (cmp == LT_EXPR, type); })))
777(for cmp (gt le)
778 (simplify
779 (cmp @1 (trunc_mod @0 @1))
780 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
781 { constant_boolean_node (cmp == GT_EXPR, type); })))
782
55534d34 783/* x | ~0 -> ~0 */
784(simplify
e728d5ba 785 (bit_ior @0 integer_all_onesp@1)
786 @1)
787
788/* x | 0 -> x */
789(simplify
790 (bit_ior @0 integer_zerop)
791 @0)
55534d34 792
793/* x & 0 -> 0 */
794(simplify
e728d5ba 795 (bit_and @0 integer_zerop@1)
796 @1)
55534d34 797
f6f5b85f 798/* ~x | x -> -1 */
c8047e01 799/* ~x ^ x -> -1 */
800/* ~x + x -> -1 */
801(for op (bit_ior bit_xor plus)
802 (simplify
803 (op:c (convert? @0) (convert? (bit_not @0)))
804 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
f6f5b85f 805
55534d34 806/* x ^ x -> 0 */
807(simplify
808 (bit_xor @0 @0)
809 { build_zero_cst (type); })
810
58810b92 811/* Canonicalize X ^ ~0 to ~X. */
812(simplify
813 (bit_xor @0 integer_all_onesp@1)
814 (bit_not @0))
815
816/* x & ~0 -> x */
817(simplify
818 (bit_and @0 integer_all_onesp)
819 (non_lvalue @0))
820
821/* x & x -> x, x | x -> x */
822(for bitop (bit_and bit_ior)
823 (simplify
824 (bitop @0 @0)
825 (non_lvalue @0)))
826
b6dd53bf 827/* x & C -> x if we know that x & ~C == 0. */
828#if GIMPLE
829(simplify
830 (bit_and SSA_NAME@0 INTEGER_CST@1)
831 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
e3d0f65c 832 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
b6dd53bf 833 @0))
834#endif
835
c0cf1b24 836/* x + (x & 1) -> (x + 1) & ~1 */
837(simplify
2bc3151f 838 (plus:c @0 (bit_and:s @0 integer_onep@1))
839 (bit_and (plus @0 @1) (bit_not @1)))
c0cf1b24 840
841/* x & ~(x & y) -> x & ~y */
842/* x | ~(x | y) -> x | ~y */
843(for bitop (bit_and bit_ior)
5af95dbe 844 (simplify
2bc3151f 845 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
846 (bitop @0 (bit_not @1))))
5af95dbe 847
848/* (x | y) & ~x -> y & ~x */
849/* (x & y) | ~x -> y | ~x */
850(for bitop (bit_and bit_ior)
851 rbitop (bit_ior bit_and)
852 (simplify
853 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
854 (bitop @1 @2)))
c0cf1b24 855
88fc652a 856/* (x & y) ^ (x | y) -> x ^ y */
857(simplify
2652cce5 858 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
859 (bit_xor @0 @1))
88fc652a 860
800f9fe9 861/* (x ^ y) ^ (x | y) -> x & y */
862(simplify
863 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
864 (bit_and @0 @1))
865
866/* (x & y) + (x ^ y) -> x | y */
867/* (x & y) | (x ^ y) -> x | y */
868/* (x & y) ^ (x ^ y) -> x | y */
869(for op (plus bit_ior bit_xor)
870 (simplify
871 (op:c (bit_and @0 @1) (bit_xor @0 @1))
872 (bit_ior @0 @1)))
873
874/* (x & y) + (x | y) -> x + y */
875(simplify
876 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
877 (plus @0 @1))
878
88323eef 879/* (x + y) - (x | y) -> x & y */
880(simplify
881 (minus (plus @0 @1) (bit_ior @0 @1))
882 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
883 && !TYPE_SATURATING (type))
884 (bit_and @0 @1)))
885
886/* (x + y) - (x & y) -> x | y */
887(simplify
888 (minus (plus @0 @1) (bit_and @0 @1))
889 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
890 && !TYPE_SATURATING (type))
891 (bit_ior @0 @1)))
892
800f9fe9 893/* (x | y) - (x ^ y) -> x & y */
894(simplify
895 (minus (bit_ior @0 @1) (bit_xor @0 @1))
896 (bit_and @0 @1))
897
898/* (x | y) - (x & y) -> x ^ y */
899(simplify
900 (minus (bit_ior @0 @1) (bit_and @0 @1))
901 (bit_xor @0 @1))
902
001eae4f 903/* (x | y) & ~(x & y) -> x ^ y */
904(simplify
905 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
906 (bit_xor @0 @1))
907
908/* (x | y) & (~x ^ y) -> x & y */
909(simplify
910 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
911 (bit_and @0 @1))
912
168a6cd2 913/* ~x & ~y -> ~(x | y)
914 ~x | ~y -> ~(x & y) */
915(for op (bit_and bit_ior)
916 rop (bit_ior bit_and)
917 (simplify
918 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
c0205bab 919 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
920 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
168a6cd2 921 (bit_not (rop (convert @0) (convert @1))))))
922
a76fc0fd 923/* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
168a6cd2 924 with a constant, and the two constants have no bits in common,
925 we should treat this as a BIT_IOR_EXPR since this may produce more
926 simplifications. */
a76fc0fd 927(for op (bit_xor plus)
928 (simplify
929 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
930 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
931 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
932 && tree_nop_conversion_p (type, TREE_TYPE (@2))
e3d0f65c 933 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
a76fc0fd 934 (bit_ior (convert @4) (convert @5)))))
168a6cd2 935
936/* (X | Y) ^ X -> Y & ~ X*/
937(simplify
c76c65ae 938 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
168a6cd2 939 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
940 (convert (bit_and @1 (bit_not @0)))))
941
942/* Convert ~X ^ ~Y to X ^ Y. */
943(simplify
944 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
c0205bab 945 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
946 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
168a6cd2 947 (bit_xor (convert @0) (convert @1))))
948
949/* Convert ~X ^ C to X ^ ~C. */
950(simplify
951 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
535b528e 952 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
953 (bit_xor (convert @0) (bit_not @1))))
168a6cd2 954
f9153689 955/* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
956(for opo (bit_and bit_xor)
957 opi (bit_xor bit_and)
958 (simplify
959 (opo:c (opi:c @0 @1) @1)
960 (bit_and (bit_not @0) @1)))
486bdb6a 961
a76fc0fd 962/* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
963 operands are another bit-wise operation with a common input. If so,
964 distribute the bit operations to save an operation and possibly two if
965 constants are involved. For example, convert
966 (A | B) & (A | C) into A | (B & C)
967 Further simplification will occur if B and C are constants. */
846521ce 968(for op (bit_and bit_ior bit_xor)
969 rop (bit_ior bit_and bit_and)
a76fc0fd 970 (simplify
c76c65ae 971 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
846521ce 972 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
973 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
a76fc0fd 974 (rop (convert @0) (op (convert @1) (convert @2))))))
975
f9153689 976/* Some simple reassociation for bit operations, also handled in reassoc. */
977/* (X & Y) & Y -> X & Y
978 (X | Y) | Y -> X | Y */
979(for op (bit_and bit_ior)
980 (simplify
c76c65ae 981 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
f9153689 982 @2))
983/* (X ^ Y) ^ Y -> X */
984(simplify
c76c65ae 985 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
c0205bab 986 (convert @0))
f9153689 987/* (X & Y) & (X & Z) -> (X & Y) & Z
988 (X | Y) | (X | Z) -> (X | Y) | Z */
989(for op (bit_and bit_ior)
990 (simplify
460001de 991 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
f9153689 992 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
993 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
994 (if (single_use (@5) && single_use (@6))
995 (op @3 (convert @2))
996 (if (single_use (@3) && single_use (@4))
997 (op (convert @1) @5))))))
998/* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
999(simplify
1000 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1001 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1002 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
92e3ffbf 1003 (bit_xor (convert @1) (convert @2))))
168a6cd2 1004
98b69bcc 1005(simplify
1006 (abs (abs@1 @0))
1007 @1)
2ad7e37a 1008(simplify
1009 (abs (negate @0))
1010 (abs @0))
1011(simplify
1012 (abs tree_expr_nonnegative_p@0)
1013 @0)
1014
004b4ca3 1015/* A few cases of fold-const.c negate_expr_p predicate. */
1016(match negate_expr_p
1017 INTEGER_CST
98b69bcc 1018 (if ((INTEGRAL_TYPE_P (type)
da78c088 1019 && TYPE_UNSIGNED (type))
98b69bcc 1020 || (!TYPE_OVERFLOW_SANITIZED (type)
004b4ca3 1021 && may_negate_without_overflow_p (t)))))
1022(match negate_expr_p
1023 FIXED_CST)
1024(match negate_expr_p
1025 (negate @0)
1026 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1027(match negate_expr_p
1028 REAL_CST
1029 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1030/* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1031 ways. */
1032(match negate_expr_p
1033 VECTOR_CST
1034 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
c3aa270e 1035(match negate_expr_p
1036 (minus @0 @1)
1037 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1038 || (FLOAT_TYPE_P (type)
1039 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1040 && !HONOR_SIGNED_ZEROS (type)))))
35c8219c 1041
1042/* (-A) * (-B) -> A * B */
1043(simplify
1044 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1045 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1046 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1047 (mult (convert @0) (convert (negate @1)))))
004b4ca3 1048
1049/* -(A + B) -> (-B) - A. */
98b69bcc 1050(simplify
004b4ca3 1051 (negate (plus:c @0 negate_expr_p@1))
1052 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1053 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1054 (minus (negate @1) @0)))
1055
c3aa270e 1056/* -(A - B) -> B - A. */
1057(simplify
1058 (negate (minus @0 @1))
1059 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1060 || (FLOAT_TYPE_P (type)
1061 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1062 && !HONOR_SIGNED_ZEROS (type)))
1063 (minus @1 @0)))
57e83b58 1064(simplify
1065 (negate (pointer_diff @0 @1))
1066 (if (TYPE_OVERFLOW_UNDEFINED (type))
1067 (pointer_diff @1 @0)))
c3aa270e 1068
004b4ca3 1069/* A - B -> A + (-B) if B is easily negatable. */
98b69bcc 1070(simplify
004b4ca3 1071 (minus @0 negate_expr_p@1)
b1eac901 1072 (if (!FIXED_POINT_TYPE_P (type))
1073 (plus @0 (negate @1))))
6d58fd21 1074
d0eb9b3d 1075/* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1076 when profitable.
1077 For bitwise binary operations apply operand conversions to the
1078 binary operation result instead of to the operands. This allows
1079 to combine successive conversions and bitwise binary operations.
1080 We combine the above two cases by using a conditional convert. */
1081(for bitop (bit_and bit_ior bit_xor)
1082 (simplify
1083 (bitop (convert @0) (convert? @1))
1084 (if (((TREE_CODE (@1) == INTEGER_CST
1085 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
a6e46537 1086 && int_fits_type_p (@1, TREE_TYPE (@0)))
b1306f12 1087 || types_match (@0, @1))
a6e46537 1088 /* ??? This transform conflicts with fold-const.c doing
1089 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1090 constants (if x has signed type, the sign bit cannot be set
1091 in c). This folds extension into the BIT_AND_EXPR.
1092 Restrict it to GIMPLE to avoid endless recursions. */
1093 && (bitop != BIT_AND_EXPR || GIMPLE)
d0eb9b3d 1094 && (/* That's a good idea if the conversion widens the operand, thus
1095 after hoisting the conversion the operation will be narrower. */
1096 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1097 /* It's also a good idea if the conversion is to a non-integer
1098 mode. */
1099 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1100 /* Or if the precision of TO is not the same as the precision
1101 of its mode. */
654ba22c 1102 || !type_has_mode_precision_p (type)))
d0eb9b3d 1103 (convert (bitop @0 (convert @1))))))
1104
98b69bcc 1105(for bitop (bit_and bit_ior)
1106 rbitop (bit_ior bit_and)
1107 /* (x | y) & x -> x */
1108 /* (x & y) | x -> x */
1109 (simplify
1110 (bitop:c (rbitop:c @0 @1) @0)
1111 @0)
1112 /* (~x | y) & x -> x & y */
1113 /* (~x & y) | x -> x | y */
1114 (simplify
1115 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1116 (bitop @0 @1)))
1117
d0eb9b3d 1118/* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1119(simplify
1120 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1121 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1122
1123/* Combine successive equal operations with constants. */
1124(for bitop (bit_and bit_ior bit_xor)
1125 (simplify
1126 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
6a833a2e 1127 (if (!CONSTANT_CLASS_P (@0))
1128 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1129 folded to a constant. */
1130 (bitop @0 (bitop @1 @2))
1131 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1132 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1133 the values involved are such that the operation can't be decided at
1134 compile time. Try folding one of @0 or @1 with @2 to see whether
1135 that combination can be decided at compile time.
1136
1137 Keep the existing form if both folds fail, to avoid endless
1138 oscillation. */
1139 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1140 (if (cst1)
1141 (bitop @1 { cst1; })
1142 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1143 (if (cst2)
1144 (bitop @0 { cst2; }))))))))
d0eb9b3d 1145
1146/* Try simple folding for X op !X, and X op X with the help
1147 of the truth_valued_p and logical_inverted_value predicates. */
1148(match truth_valued_p
1149 @0
1150 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
9ec260f0 1151(for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
d0eb9b3d 1152 (match truth_valued_p
1153 (op @0 @1)))
1154(match truth_valued_p
1155 (truth_not @0))
1156
35c8219c 1157(match (logical_inverted_value @0)
1158 (truth_not @0))
d0eb9b3d 1159(match (logical_inverted_value @0)
1160 (bit_not truth_valued_p@0))
1161(match (logical_inverted_value @0)
ccb14741 1162 (eq @0 integer_zerop))
d0eb9b3d 1163(match (logical_inverted_value @0)
ccb14741 1164 (ne truth_valued_p@0 integer_truep))
d0eb9b3d 1165(match (logical_inverted_value @0)
ccb14741 1166 (bit_xor truth_valued_p@0 integer_truep))
d0eb9b3d 1167
1168/* X & !X -> 0. */
1169(simplify
1170 (bit_and:c @0 (logical_inverted_value @0))
1171 { build_zero_cst (type); })
1172/* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1173(for op (bit_ior bit_xor)
1174 (simplify
1175 (op:c truth_valued_p@0 (logical_inverted_value @0))
9ec260f0 1176 { constant_boolean_node (true, type); }))
373bcaaf 1177/* X ==/!= !X is false/true. */
1178(for op (eq ne)
1179 (simplify
1180 (op:c truth_valued_p@0 (logical_inverted_value @0))
1181 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
d0eb9b3d 1182
d0eb9b3d 1183/* ~~x -> x */
1184(simplify
1185 (bit_not (bit_not @0))
1186 @0)
1187
98b69bcc 1188/* Convert ~ (-A) to A - 1. */
1189(simplify
1190 (bit_not (convert? (negate @0)))
c0205bab 1191 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1192 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
c8047e01 1193 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
98b69bcc 1194
c3aa270e 1195/* Convert - (~A) to A + 1. */
1196(simplify
1197 (negate (nop_convert (bit_not @0)))
1198 (plus (view_convert @0) { build_each_one_cst (type); }))
1199
98b69bcc 1200/* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1201(simplify
c8047e01 1202 (bit_not (convert? (minus @0 integer_each_onep)))
c0205bab 1203 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1204 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
98b69bcc 1205 (convert (negate @0))))
1206(simplify
1207 (bit_not (convert? (plus @0 integer_all_onesp)))
c0205bab 1208 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1209 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
98b69bcc 1210 (convert (negate @0))))
1211
1212/* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1213(simplify
1214 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1215 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1216 (convert (bit_xor @0 (bit_not @1)))))
1217(simplify
1218 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1219 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1220 (convert (bit_xor @0 @1))))
1221
270a468f 1222/* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1223(simplify
1224 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1225 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1226 (bit_not (bit_xor (view_convert @0) @1))))
1227
277f7164 1228/* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1229(simplify
2bc3151f 1230 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1231 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
277f7164 1232
bd9846d0 1233/* Fold A - (A & B) into ~B & A. */
1234(simplify
c76c65ae 1235 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
bd9846d0 1236 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1237 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1238 (convert (bit_and (bit_not @1) @0))))
d0eb9b3d 1239
0aa23bd4 1240/* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1241(for cmp (gt lt ge le)
1242(simplify
1243 (mult (convert (cmp @0 @1)) @2)
1244 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1245
70229d3b 1246/* For integral types with undefined overflow and C != 0 fold
1247 x * C EQ/NE y * C into x EQ/NE y. */
1248(for cmp (eq ne)
1249 (simplify
1250 (cmp (mult:c @0 @1) (mult:c @2 @1))
1251 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1252 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1253 && tree_expr_nonzero_p (@1))
1254 (cmp @0 @2))))
1255
20ce76cb 1256/* For integral types with wrapping overflow and C odd fold
1257 x * C EQ/NE y * C into x EQ/NE y. */
1258(for cmp (eq ne)
1259 (simplify
1260 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1261 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1262 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1263 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1264 (cmp @0 @2))))
1265
70229d3b 1266/* For integral types with undefined overflow and C != 0 fold
1267 x * C RELOP y * C into:
4de74241 1268
70229d3b 1269 x RELOP y for nonnegative C
1270 y RELOP x for negative C */
1271(for cmp (lt gt le ge)
1272 (simplify
1273 (cmp (mult:c @0 @1) (mult:c @2 @1))
1274 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1275 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1276 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1277 (cmp @0 @2)
1278 (if (TREE_CODE (@1) == INTEGER_CST
e3d0f65c 1279 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
70229d3b 1280 (cmp @2 @0))))))
4de74241 1281
205b32be 1282/* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1283(for cmp (le gt)
1284 icmp (gt le)
1285 (simplify
1286 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1287 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1288 && TYPE_UNSIGNED (TREE_TYPE (@0))
1289 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
e3d0f65c 1290 && (wi::to_wide (@2)
1291 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
205b32be 1292 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1293 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1294
c487041b 1295/* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1296(for cmp (simple_comparison)
1297 (simplify
1298 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
e3d0f65c 1299 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
c487041b 1300 (cmp @0 @1))))
1301
14c7029b 1302/* X / C1 op C2 into a simple range test. */
1303(for cmp (simple_comparison)
1304 (simplify
1305 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1306 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1307 && integer_nonzerop (@1)
1308 && !TREE_OVERFLOW (@1)
1309 && !TREE_OVERFLOW (@2))
1310 (with { tree lo, hi; bool neg_overflow;
1311 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1312 &neg_overflow); }
1313 (switch
1314 (if (code == LT_EXPR || code == GE_EXPR)
1315 (if (TREE_OVERFLOW (lo))
1316 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1317 (if (code == LT_EXPR)
1318 (lt @0 { lo; })
1319 (ge @0 { lo; }))))
1320 (if (code == LE_EXPR || code == GT_EXPR)
1321 (if (TREE_OVERFLOW (hi))
1322 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1323 (if (code == LE_EXPR)
1324 (le @0 { hi; })
1325 (gt @0 { hi; }))))
1326 (if (!lo && !hi)
1327 { build_int_cst (type, code == NE_EXPR); })
1328 (if (code == EQ_EXPR && !hi)
1329 (ge @0 { lo; }))
1330 (if (code == EQ_EXPR && !lo)
1331 (le @0 { hi; }))
1332 (if (code == NE_EXPR && !hi)
1333 (lt @0 { lo; }))
1334 (if (code == NE_EXPR && !lo)
1335 (gt @0 { hi; }))
1336 (if (GENERIC)
1337 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1338 lo, hi); })
1339 (with
1340 {
1341 tree etype = range_check_type (TREE_TYPE (@0));
1342 if (etype)
1343 {
1344 if (! TYPE_UNSIGNED (etype))
1345 etype = unsigned_type_for (etype);
1346 hi = fold_convert (etype, hi);
1347 lo = fold_convert (etype, lo);
1348 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1349 }
1350 }
1351 (if (etype && hi && !TREE_OVERFLOW (hi))
1352 (if (code == EQ_EXPR)
1353 (le (minus (convert:etype @0) { lo; }) { hi; })
1354 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1355
9222792c 1356/* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1357(for op (lt le ge gt)
1358 (simplify
1359 (op (plus:c @0 @2) (plus:c @1 @2))
1360 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1361 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1362 (op @0 @1))))
1363/* For equality and subtraction, this is also true with wrapping overflow. */
1364(for op (eq ne minus)
1365 (simplify
1366 (op (plus:c @0 @2) (plus:c @1 @2))
1367 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1368 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1369 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1370 (op @0 @1))))
1371
1372/* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1373(for op (lt le ge gt)
1374 (simplify
1375 (op (minus @0 @2) (minus @1 @2))
1376 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1377 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1378 (op @0 @1))))
1379/* For equality and subtraction, this is also true with wrapping overflow. */
1380(for op (eq ne minus)
1381 (simplify
1382 (op (minus @0 @2) (minus @1 @2))
1383 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1384 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1385 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1386 (op @0 @1))))
57e83b58 1387/* And for pointers... */
1388(for op (simple_comparison)
1389 (simplify
1390 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1391 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1392 (op @0 @1))))
1393(simplify
1394 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1395 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1396 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1397 (pointer_diff @0 @1)))
9222792c 1398
1399/* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1400(for op (lt le ge gt)
1401 (simplify
1402 (op (minus @2 @0) (minus @2 @1))
1403 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1404 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1405 (op @1 @0))))
1406/* For equality and subtraction, this is also true with wrapping overflow. */
1407(for op (eq ne minus)
1408 (simplify
1409 (op (minus @2 @0) (minus @2 @1))
1410 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1411 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1412 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1413 (op @1 @0))))
57e83b58 1414/* And for pointers... */
1415(for op (simple_comparison)
1416 (simplify
1417 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1418 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1419 (op @1 @0))))
1420(simplify
1421 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1422 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1423 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1424 (pointer_diff @1 @0)))
9222792c 1425
3e39e829 1426/* X + Y < Y is the same as X < 0 when there is no overflow. */
1427(for op (lt le gt ge)
1428 (simplify
1429 (op:c (plus:c@2 @0 @1) @1)
1430 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1431 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1432 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1433 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1434/* For equality, this is also true with wrapping overflow. */
1435(for op (eq ne)
1436 (simplify
1437 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1438 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1439 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1440 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1441 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1442 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1443 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1444 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1445 (simplify
1446 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1447 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1448 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1449 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1450 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1451
1452/* X - Y < X is the same as Y > 0 when there is no overflow.
1453 For equality, this is also true with wrapping overflow. */
1454(for op (simple_comparison)
1455 (simplify
1456 (op:c @0 (minus@2 @0 @1))
1457 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1458 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1459 || ((op == EQ_EXPR || op == NE_EXPR)
1460 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1461 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1462 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1463
ed14c914 1464/* Transform:
1465 * (X / Y) == 0 -> X < Y if X, Y are unsigned.
1466 * (X / Y) != 0 -> X >= Y, if X, Y are unsigned.
1467 */
1468(for cmp (eq ne)
1469 ocmp (lt ge)
1470 (simplify
1471 (cmp (trunc_div @0 @1) integer_zerop)
1472 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1473 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1474 (ocmp @0 @1))))
1475
7eb46067 1476/* X == C - X can never be true if C is odd. */
1477(for cmp (eq ne)
1478 (simplify
1479 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1480 (if (TREE_INT_CST_LOW (@1) & 1)
1481 { constant_boolean_node (cmp == NE_EXPR, type); })))
1482
ef820217 1483/* Arguments on which one can call get_nonzero_bits to get the bits
1484 possibly set. */
1485(match with_possible_nonzero_bits
1486 INTEGER_CST@0)
1487(match with_possible_nonzero_bits
1488 SSA_NAME@0
1489 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1490/* Slightly extended version, do not make it recursive to keep it cheap. */
1491(match (with_possible_nonzero_bits2 @0)
1492 with_possible_nonzero_bits@0)
1493(match (with_possible_nonzero_bits2 @0)
1494 (bit_and:c with_possible_nonzero_bits@0 @2))
1495
1496/* Same for bits that are known to be set, but we do not have
1497 an equivalent to get_nonzero_bits yet. */
1498(match (with_certain_nonzero_bits2 @0)
1499 INTEGER_CST@0)
1500(match (with_certain_nonzero_bits2 @0)
1501 (bit_ior @1 INTEGER_CST@0))
1502
1503/* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1504(for cmp (eq ne)
1505 (simplify
1506 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
e3d0f65c 1507 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
ef820217 1508 { constant_boolean_node (cmp == NE_EXPR, type); })))
1509
4de74241 1510/* ((X inner_op C0) outer_op C1)
1511 With X being a tree where value_range has reasoned certain bits to always be
1512 zero throughout its computed value range,
1513 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1514 where zero_mask has 1's for all bits that are sure to be 0 in
1515 and 0's otherwise.
1516 if (inner_op == '^') C0 &= ~C1;
1517 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1518 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1519*/
1520(for inner_op (bit_ior bit_xor)
1521 outer_op (bit_xor bit_ior)
1522(simplify
1523 (outer_op
1524 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1525 (with
1526 {
1527 bool fail = false;
1528 wide_int zero_mask_not;
1529 wide_int C0;
1530 wide_int cst_emit;
1531
1532 if (TREE_CODE (@2) == SSA_NAME)
1533 zero_mask_not = get_nonzero_bits (@2);
1534 else
1535 fail = true;
1536
1537 if (inner_op == BIT_XOR_EXPR)
1538 {
e3d0f65c 1539 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1540 cst_emit = C0 | wi::to_wide (@1);
4de74241 1541 }
1542 else
1543 {
e3d0f65c 1544 C0 = wi::to_wide (@0);
1545 cst_emit = C0 ^ wi::to_wide (@1);
4de74241 1546 }
1547 }
e3d0f65c 1548 (if (!fail && (C0 & zero_mask_not) == 0)
4de74241 1549 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
e3d0f65c 1550 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
4de74241 1551 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1552
770ae4bb 1553/* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1554(simplify
2bc3151f 1555 (pointer_plus (pointer_plus:s @0 @1) @3)
1556 (pointer_plus @0 (plus @1 @3)))
770ae4bb 1557
1558/* Pattern match
1559 tem1 = (long) ptr1;
1560 tem2 = (long) ptr2;
1561 tem3 = tem2 - tem1;
1562 tem4 = (unsigned long) tem3;
1563 tem5 = ptr1 + tem4;
1564 and produce
1565 tem5 = ptr2; */
1566(simplify
1567 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1568 /* Conditionally look through a sign-changing conversion. */
1569 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1570 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1571 || (GENERIC && type == TREE_TYPE (@1))))
1572 @1))
57e83b58 1573(simplify
1574 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1575 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1576 (convert @1)))
770ae4bb 1577
1578/* Pattern match
1579 tem = (sizetype) ptr;
1580 tem = tem & algn;
1581 tem = -tem;
1582 ... = ptr p+ tem;
1583 and produce the simpler and easier to analyze with respect to alignment
1584 ... = ptr & ~algn; */
1585(simplify
1586 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
e3d0f65c 1587 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
770ae4bb 1588 (bit_and @0 { algn; })))
1589
6b440285 1590/* Try folding difference of addresses. */
1591(simplify
1592 (minus (convert ADDR_EXPR@0) (convert @1))
1593 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
81bc0f0f 1594 (with { poly_int64 diff; }
6b440285 1595 (if (ptr_difference_const (@0, @1, &diff))
1596 { build_int_cst_type (type, diff); }))))
1597(simplify
1598 (minus (convert @0) (convert ADDR_EXPR@1))
1599 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
81bc0f0f 1600 (with { poly_int64 diff; }
6b440285 1601 (if (ptr_difference_const (@0, @1, &diff))
1602 { build_int_cst_type (type, diff); }))))
57e83b58 1603(simplify
1604 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert?@3 @1))
1605 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1606 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
81bc0f0f 1607 (with { poly_int64 diff; }
57e83b58 1608 (if (ptr_difference_const (@0, @1, &diff))
1609 { build_int_cst_type (type, diff); }))))
1610(simplify
1611 (pointer_diff (convert?@2 @0) (convert?@3 ADDR_EXPR@1))
1612 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1613 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
81bc0f0f 1614 (with { poly_int64 diff; }
57e83b58 1615 (if (ptr_difference_const (@0, @1, &diff))
1616 { build_int_cst_type (type, diff); }))))
6b440285 1617
a2f9b7c9 1618/* If arg0 is derived from the address of an object or function, we may
1619 be able to fold this expression using the object or function's
1620 alignment. */
1621(simplify
1622 (bit_and (convert? @0) INTEGER_CST@1)
1623 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1624 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1625 (with
1626 {
1627 unsigned int align;
1628 unsigned HOST_WIDE_INT bitpos;
1629 get_pointer_alignment_1 (@0, &align, &bitpos);
1630 }
e3d0f65c 1631 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1632 { wide_int_to_tree (type, (wi::to_wide (@1)
1633 & (bitpos / BITS_PER_UNIT))); }))))
6b440285 1634
770ae4bb 1635
662274ee 1636/* We can't reassociate at all for saturating types. */
1637(if (!TYPE_SATURATING (type))
1638
1639 /* Contract negates. */
1640 /* A + (-B) -> A - B */
1641 (simplify
2bbfc137 1642 (plus:c @0 (convert? (negate @1)))
1643 /* Apply STRIP_NOPS on the negate. */
1644 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
a8875ee0 1645 && !TYPE_OVERFLOW_SANITIZED (type))
2bbfc137 1646 (with
1647 {
1648 tree t1 = type;
1649 if (INTEGRAL_TYPE_P (type)
1650 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1651 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1652 }
1653 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
662274ee 1654 /* A - (-B) -> A + B */
1655 (simplify
2bbfc137 1656 (minus @0 (convert? (negate @1)))
1657 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
a8875ee0 1658 && !TYPE_OVERFLOW_SANITIZED (type))
2bbfc137 1659 (with
1660 {
1661 tree t1 = type;
1662 if (INTEGRAL_TYPE_P (type)
1663 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1664 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1665 }
1666 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
48eb107e 1667 /* -(T)(-A) -> (T)A
1668 Sign-extension is ok except for INT_MIN, which thankfully cannot
1669 happen without overflow. */
1670 (simplify
1671 (negate (convert (negate @1)))
1672 (if (INTEGRAL_TYPE_P (type)
1673 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1674 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1675 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1676 && !TYPE_OVERFLOW_SANITIZED (type)
1677 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
c229fef3 1678 (convert @1)))
48eb107e 1679 (simplify
1680 (negate (convert negate_expr_p@1))
1681 (if (SCALAR_FLOAT_TYPE_P (type)
1682 && ((DECIMAL_FLOAT_TYPE_P (type)
1683 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1684 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1685 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1686 (convert (negate @1))))
1687 (simplify
1688 (negate (nop_convert (negate @1)))
1689 (if (!TYPE_OVERFLOW_SANITIZED (type)
1690 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1691 (view_convert @1)))
662274ee 1692
f215226b 1693 /* We can't reassociate floating-point unless -fassociative-math
1694 or fixed-point plus or minus because of saturation to +-Inf. */
1695 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1696 && !FIXED_POINT_TYPE_P (type))
662274ee 1697
1698 /* Match patterns that allow contracting a plus-minus pair
1699 irrespective of overflow issues. */
1700 /* (A +- B) - A -> +- B */
1701 /* (A +- B) -+ B -> A */
1702 /* A - (A +- B) -> -+ B */
1703 /* A +- (B -+ A) -> +- B */
1704 (simplify
1705 (minus (plus:c @0 @1) @0)
1706 @1)
1707 (simplify
1708 (minus (minus @0 @1) @0)
1709 (negate @1))
1710 (simplify
1711 (plus:c (minus @0 @1) @1)
1712 @0)
1713 (simplify
1714 (minus @0 (plus:c @0 @1))
1715 (negate @1))
1716 (simplify
1717 (minus @0 (minus @0 @1))
1718 @1)
bf1cce2e 1719 /* (A +- B) + (C - A) -> C +- B */
1720 /* (A + B) - (A - C) -> B + C */
1721 /* More cases are handled with comparisons. */
1722 (simplify
1723 (plus:c (plus:c @0 @1) (minus @2 @0))
1724 (plus @2 @1))
1725 (simplify
1726 (plus:c (minus @0 @1) (minus @2 @0))
1727 (minus @2 @1))
57e83b58 1728 (simplify
1729 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1730 (if (TYPE_OVERFLOW_UNDEFINED (type)
1731 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1732 (pointer_diff @2 @1)))
bf1cce2e 1733 (simplify
1734 (minus (plus:c @0 @1) (minus @0 @2))
1735 (plus @1 @2))
662274ee 1736
049318f9 1737 /* (A +- CST1) +- CST2 -> A + CST3
1738 Use view_convert because it is safe for vectors and equivalent for
1739 scalars. */
662274ee 1740 (for outer_op (plus minus)
1741 (for inner_op (plus minus)
049318f9 1742 neg_inner_op (minus plus)
662274ee 1743 (simplify
049318f9 1744 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1745 CONSTANT_CLASS_P@2)
1746 /* If one of the types wraps, use that one. */
1747 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
50e96fc3 1748 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1749 forever if something doesn't simplify into a constant. */
1750 (if (!CONSTANT_CLASS_P (@0))
1751 (if (outer_op == PLUS_EXPR)
1752 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1753 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
049318f9 1754 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1755 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1756 (if (outer_op == PLUS_EXPR)
1757 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1758 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1759 /* If the constant operation overflows we cannot do the transform
1760 directly as we would introduce undefined overflow, for example
1761 with (a - 1) + INT_MIN. */
1762 (if (types_match (type, @0))
1763 (with { tree cst = const_binop (outer_op == inner_op
1764 ? PLUS_EXPR : MINUS_EXPR,
1765 type, @1, @2); }
1766 (if (cst && !TREE_OVERFLOW (cst))
1767 (inner_op @0 { cst; } )
1768 /* X+INT_MAX+1 is X-INT_MIN. */
1769 (if (INTEGRAL_TYPE_P (type) && cst
e3d0f65c 1770 && wi::to_wide (cst) == wi::min_value (type))
1771 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
049318f9 1772 /* Last resort, use some unsigned type. */
1773 (with { tree utype = unsigned_type_for (type); }
1774 (view_convert (inner_op
1775 (view_convert:utype @0)
1776 (view_convert:utype
1777 { drop_tree_overflow (cst); })))))))))))))
662274ee 1778
e5c2af08 1779 /* (CST1 - A) +- CST2 -> CST3 - A */
662274ee 1780 (for outer_op (plus minus)
1781 (simplify
1782 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
0808dfce 1783 (with { tree cst = const_binop (outer_op, type, @1, @2); }
662274ee 1784 (if (cst && !TREE_OVERFLOW (cst))
1785 (minus { cst; } @0)))))
1786
e5c2af08 1787 /* CST1 - (CST2 - A) -> CST3 + A */
1788 (simplify
1789 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1790 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1791 (if (cst && !TREE_OVERFLOW (cst))
1792 (plus { cst; } @0))))
1793
662274ee 1794 /* ~A + A -> -1 */
1795 (simplify
1796 (plus:c (bit_not @0) @0)
1797 (if (!TYPE_OVERFLOW_TRAPS (type))
1798 { build_all_ones_cst (type); }))
1799
1800 /* ~A + 1 -> -A */
1801 (simplify
c0a7a7df 1802 (plus (convert? (bit_not @0)) integer_each_onep)
1803 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1804 (negate (convert @0))))
1805
1806 /* -A - 1 -> ~A */
1807 (simplify
1808 (minus (convert? (negate @0)) integer_each_onep)
1809 (if (!TYPE_OVERFLOW_TRAPS (type)
1810 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1811 (bit_not (convert @0))))
1812
1813 /* -1 - A -> ~A */
1814 (simplify
1815 (minus integer_all_onesp @0)
d792dcdf 1816 (bit_not @0))
662274ee 1817
1818 /* (T)(P + A) - (T)P -> (T) A */
bdcc09da 1819 (simplify
f3e5187b 1820 (minus (convert (plus:c @@0 @1))
1821 (convert? @0))
bdcc09da 1822 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1823 /* For integer types, if A has a smaller type
1824 than T the result depends on the possible
1825 overflow in P + A.
1826 E.g. T=size_t, A=(unsigned)429497295, P>0.
1827 However, if an overflow in P + A would cause
1828 undefined behavior, we can assume that there
1829 is no overflow. */
f3e5187b 1830 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1831 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
bdcc09da 1832 (convert @1)))
1833 (simplify
1834 (minus (convert (pointer_plus @@0 @1))
1835 (convert @0))
1836 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1837 /* For pointer types, if the conversion of A to the
1838 final type requires a sign- or zero-extension,
1839 then we have to punt - it is not defined which
1840 one is correct. */
1841 || (POINTER_TYPE_P (TREE_TYPE (@0))
1842 && TREE_CODE (@1) == INTEGER_CST
1843 && tree_int_cst_sign_bit (@1) == 0))
1844 (convert @1)))
57e83b58 1845 (simplify
1846 (pointer_diff (pointer_plus @@0 @1) @0)
1847 /* The second argument of pointer_plus must be interpreted as signed, and
1848 thus sign-extended if necessary. */
1849 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
992a51f2 1850 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1851 second arg is unsigned even when we need to consider it as signed,
1852 we don't want to diagnose overflow here. */
1853 (convert (view_convert:stype @1))))
844cece0 1854
1855 /* (T)P - (T)(P + A) -> -(T) A */
bdcc09da 1856 (simplify
f3e5187b 1857 (minus (convert? @0)
1858 (convert (plus:c @@0 @1)))
bdcc09da 1859 (if (INTEGRAL_TYPE_P (type)
1860 && TYPE_OVERFLOW_UNDEFINED (type)
1861 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1862 (with { tree utype = unsigned_type_for (type); }
1863 (convert (negate (convert:utype @1))))
844cece0 1864 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1865 /* For integer types, if A has a smaller type
1866 than T the result depends on the possible
1867 overflow in P + A.
1868 E.g. T=size_t, A=(unsigned)429497295, P>0.
1869 However, if an overflow in P + A would cause
1870 undefined behavior, we can assume that there
1871 is no overflow. */
f3e5187b 1872 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1873 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
bdcc09da 1874 (negate (convert @1)))))
1875 (simplify
1876 (minus (convert @0)
1877 (convert (pointer_plus @@0 @1)))
1878 (if (INTEGRAL_TYPE_P (type)
1879 && TYPE_OVERFLOW_UNDEFINED (type)
1880 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1881 (with { tree utype = unsigned_type_for (type); }
1882 (convert (negate (convert:utype @1))))
1883 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
844cece0 1884 /* For pointer types, if the conversion of A to the
1885 final type requires a sign- or zero-extension,
1886 then we have to punt - it is not defined which
1887 one is correct. */
1888 || (POINTER_TYPE_P (TREE_TYPE (@0))
1889 && TREE_CODE (@1) == INTEGER_CST
1890 && tree_int_cst_sign_bit (@1) == 0))
1891 (negate (convert @1)))))
57e83b58 1892 (simplify
1893 (pointer_diff @0 (pointer_plus @@0 @1))
1894 /* The second argument of pointer_plus must be interpreted as signed, and
1895 thus sign-extended if necessary. */
1896 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
992a51f2 1897 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1898 second arg is unsigned even when we need to consider it as signed,
1899 we don't want to diagnose overflow here. */
1900 (negate (convert (view_convert:stype @1)))))
844cece0 1901
1902 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
bdcc09da 1903 (simplify
f3e5187b 1904 (minus (convert (plus:c @@0 @1))
bdcc09da 1905 (convert (plus:c @0 @2)))
1906 (if (INTEGRAL_TYPE_P (type)
1907 && TYPE_OVERFLOW_UNDEFINED (type)
f3e5187b 1908 && element_precision (type) <= element_precision (TREE_TYPE (@1))
1909 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
bdcc09da 1910 (with { tree utype = unsigned_type_for (type); }
1911 (convert (minus (convert:utype @1) (convert:utype @2))))
f3e5187b 1912 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
1913 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
1914 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
1915 /* For integer types, if A has a smaller type
1916 than T the result depends on the possible
1917 overflow in P + A.
1918 E.g. T=size_t, A=(unsigned)429497295, P>0.
1919 However, if an overflow in P + A would cause
1920 undefined behavior, we can assume that there
1921 is no overflow. */
1922 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1923 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1924 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
1925 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
bdcc09da 1926 (minus (convert @1) (convert @2)))))
1927 (simplify
1928 (minus (convert (pointer_plus @@0 @1))
1929 (convert (pointer_plus @0 @2)))
1930 (if (INTEGRAL_TYPE_P (type)
1931 && TYPE_OVERFLOW_UNDEFINED (type)
1932 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1933 (with { tree utype = unsigned_type_for (type); }
1934 (convert (minus (convert:utype @1) (convert:utype @2))))
1935 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
844cece0 1936 /* For pointer types, if the conversion of A to the
1937 final type requires a sign- or zero-extension,
1938 then we have to punt - it is not defined which
1939 one is correct. */
1940 || (POINTER_TYPE_P (TREE_TYPE (@0))
1941 && TREE_CODE (@1) == INTEGER_CST
1942 && tree_int_cst_sign_bit (@1) == 0
1943 && TREE_CODE (@2) == INTEGER_CST
1944 && tree_int_cst_sign_bit (@2) == 0))
bdcc09da 1945 (minus (convert @1) (convert @2)))))
57e83b58 1946 (simplify
1947 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
1948 /* The second argument of pointer_plus must be interpreted as signed, and
1949 thus sign-extended if necessary. */
1950 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
992a51f2 1951 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1952 second arg is unsigned even when we need to consider it as signed,
1953 we don't want to diagnose overflow here. */
1954 (minus (convert (view_convert:stype @1))
1955 (convert (view_convert:stype @2)))))))
662274ee 1956
6227228e 1957/* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
1958 Modeled after fold_plusminus_mult_expr. */
1959(if (!TYPE_SATURATING (type)
1960 && (!FLOAT_TYPE_P (type) || flag_associative_math))
1961 (for plusminus (plus minus)
1962 (simplify
60bd955f 1963 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
1964 (if ((!ANY_INTEGRAL_TYPE_P (type)
6227228e 1965 || TYPE_OVERFLOW_WRAPS (type)
1966 || (INTEGRAL_TYPE_P (type)
1967 && tree_expr_nonzero_p (@0)
1968 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
60bd955f 1969 /* If @1 +- @2 is constant require a hard single-use on either
1970 original operand (but not on both). */
1971 && (single_use (@3) || single_use (@4)))
1972 (mult (plusminus @1 @2) @0)))
1973 /* We cannot generate constant 1 for fract. */
1974 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
1975 (simplify
1976 (plusminus @0 (mult:c@3 @0 @2))
1977 (if ((!ANY_INTEGRAL_TYPE_P (type)
1978 || TYPE_OVERFLOW_WRAPS (type)
1979 || (INTEGRAL_TYPE_P (type)
1980 && tree_expr_nonzero_p (@0)
1981 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1982 && single_use (@3))
6227228e 1983 (mult (plusminus { build_one_cst (type); } @2) @0)))
1984 (simplify
60bd955f 1985 (plusminus (mult:c@3 @0 @2) @0)
1986 (if ((!ANY_INTEGRAL_TYPE_P (type)
1987 || TYPE_OVERFLOW_WRAPS (type)
1988 || (INTEGRAL_TYPE_P (type)
1989 && tree_expr_nonzero_p (@0)
1990 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1991 && single_use (@3))
6227228e 1992 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
662274ee 1993
129004d7 1994/* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
6a78ea5d 1995
054e9558 1996(for minmax (min max FMIN_ALL FMAX_ALL)
6a78ea5d 1997 (simplify
1998 (minmax @0 @0)
1999 @0))
b2edae6c 2000/* min(max(x,y),y) -> y. */
2001(simplify
2002 (min:c (max:c @0 @1) @1)
2003 @1)
2004/* max(min(x,y),y) -> y. */
2005(simplify
2006 (max:c (min:c @0 @1) @1)
2007 @1)
b0ab3ca0 2008/* max(a,-a) -> abs(a). */
2009(simplify
2010 (max:c @0 (negate @0))
2011 (if (TREE_CODE (type) != COMPLEX_TYPE
2012 && (! ANY_INTEGRAL_TYPE_P (type)
2013 || TYPE_OVERFLOW_UNDEFINED (type)))
2014 (abs @0)))
d82d27a3 2015/* min(a,-a) -> -abs(a). */
2016(simplify
2017 (min:c @0 (negate @0))
2018 (if (TREE_CODE (type) != COMPLEX_TYPE
2019 && (! ANY_INTEGRAL_TYPE_P (type)
2020 || TYPE_OVERFLOW_UNDEFINED (type)))
2021 (negate (abs @0))))
6a78ea5d 2022(simplify
2023 (min @0 @1)
523f3a9d 2024 (switch
2025 (if (INTEGRAL_TYPE_P (type)
2026 && TYPE_MIN_VALUE (type)
2027 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2028 @1)
2029 (if (INTEGRAL_TYPE_P (type)
2030 && TYPE_MAX_VALUE (type)
2031 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2032 @0)))
6a78ea5d 2033(simplify
2034 (max @0 @1)
523f3a9d 2035 (switch
2036 (if (INTEGRAL_TYPE_P (type)
2037 && TYPE_MAX_VALUE (type)
2038 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2039 @1)
2040 (if (INTEGRAL_TYPE_P (type)
2041 && TYPE_MIN_VALUE (type)
2042 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2043 @0)))
15eeb126 2044
5f645ad1 2045/* max (a, a + CST) -> a + CST where CST is positive. */
2046/* max (a, a + CST) -> a where CST is negative. */
2047(simplify
2048 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2049 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2050 (if (tree_int_cst_sgn (@1) > 0)
2051 @2
2052 @0)))
2053
2054/* min (a, a + CST) -> a where CST is positive. */
2055/* min (a, a + CST) -> a + CST where CST is negative. */
2056(simplify
2057 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2058 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2059 (if (tree_int_cst_sgn (@1) > 0)
2060 @0
2061 @2)))
2062
15eeb126 2063/* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2064 and the outer convert demotes the expression back to x's type. */
2065(for minmax (min max)
2066 (simplify
2067 (convert (minmax@0 (convert @1) INTEGER_CST@2))
a671e245 2068 (if (INTEGRAL_TYPE_P (type)
2069 && types_match (@1, type) && int_fits_type_p (@2, type)
15eeb126 2070 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2071 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2072 (minmax @1 (convert @2)))))
2073
054e9558 2074(for minmax (FMIN_ALL FMAX_ALL)
129004d7 2075 /* If either argument is NaN, return the other one. Avoid the
2076 transformation if we get (and honor) a signalling NaN. */
2077 (simplify
2078 (minmax:c @0 REAL_CST@1)
2079 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2080 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2081 @0)))
2082/* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2083 functions to return the numeric arg if the other one is NaN.
2084 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2085 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2086 worry about it either. */
2087(if (flag_finite_math_only)
2088 (simplify
054e9558 2089 (FMIN_ALL @0 @1)
129004d7 2090 (min @0 @1))
7d3df64a 2091 (simplify
054e9558 2092 (FMAX_ALL @0 @1)
129004d7 2093 (max @0 @1)))
f3ec0970 2094/* min (-A, -B) -> -max (A, B) */
054e9558 2095(for minmax (min max FMIN_ALL FMAX_ALL)
2096 maxmin (max min FMAX_ALL FMIN_ALL)
f3ec0970 2097 (simplify
2098 (minmax (negate:s@2 @0) (negate:s@3 @1))
2099 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2100 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2101 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2102 (negate (maxmin @0 @1)))))
2103/* MIN (~X, ~Y) -> ~MAX (X, Y)
2104 MAX (~X, ~Y) -> ~MIN (X, Y) */
2105(for minmax (min max)
2106 maxmin (max min)
2107 (simplify
2108 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2109 (bit_not (maxmin @0 @1))))
6a78ea5d 2110
81787808 2111/* MIN (X, Y) == X -> X <= Y */
2112(for minmax (min min max max)
2113 cmp (eq ne eq ne )
2114 out (le gt ge lt )
2115 (simplify
2116 (cmp:c (minmax:c @0 @1) @0)
2117 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2118 (out @0 @1))))
2119/* MIN (X, 5) == 0 -> X == 0
2120 MIN (X, 5) == 7 -> false */
2121(for cmp (eq ne)
2122 (simplify
2123 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
e3d0f65c 2124 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2125 TYPE_SIGN (TREE_TYPE (@0))))
81787808 2126 { constant_boolean_node (cmp == NE_EXPR, type); }
e3d0f65c 2127 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2128 TYPE_SIGN (TREE_TYPE (@0))))
81787808 2129 (cmp @0 @2)))))
2130(for cmp (eq ne)
2131 (simplify
2132 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
e3d0f65c 2133 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2134 TYPE_SIGN (TREE_TYPE (@0))))
81787808 2135 { constant_boolean_node (cmp == NE_EXPR, type); }
e3d0f65c 2136 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2137 TYPE_SIGN (TREE_TYPE (@0))))
81787808 2138 (cmp @0 @2)))))
2139/* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2140(for minmax (min min max max min min max max )
2141 cmp (lt le gt ge gt ge lt le )
2142 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2143 (simplify
2144 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2145 (comb (cmp @0 @2) (cmp @1 @2))))
2146
6a78ea5d 2147/* Simplifications of shift and rotates. */
2148
2149(for rotate (lrotate rrotate)
2150 (simplify
2151 (rotate integer_all_onesp@0 @1)
2152 @0))
2153
2154/* Optimize -1 >> x for arithmetic right shifts. */
2155(simplify
2156 (rshift integer_all_onesp@0 @1)
2157 (if (!TYPE_UNSIGNED (type)
2158 && tree_expr_nonnegative_p (@1))
2159 @0))
2160
f0e28f23 2161/* Optimize (x >> c) << c into x & (-1<<c). */
2162(simplify
2163 (lshift (rshift @0 INTEGER_CST@1) @1)
e3d0f65c 2164 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
f0e28f23 2165 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2166
2167/* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2168 types. */
2169(simplify
2170 (rshift (lshift @0 INTEGER_CST@1) @1)
2171 (if (TYPE_UNSIGNED (type)
e3d0f65c 2172 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
f0e28f23 2173 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2174
6a78ea5d 2175(for shiftrotate (lrotate rrotate lshift rshift)
2176 (simplify
2177 (shiftrotate @0 integer_zerop)
2178 (non_lvalue @0))
2179 (simplify
2180 (shiftrotate integer_zerop@0 @1)
2181 @0)
2182 /* Prefer vector1 << scalar to vector1 << vector2
2183 if vector2 is uniform. */
2184 (for vec (VECTOR_CST CONSTRUCTOR)
2185 (simplify
2186 (shiftrotate @0 vec@1)
2187 (with { tree tem = uniform_vector_p (@1); }
2188 (if (tem)
2189 (shiftrotate @0 { tem; }))))))
2190
aff1170a 2191/* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2192 Y is 0. Similarly for X >> Y. */
2193#if GIMPLE
2194(for shift (lshift rshift)
2195 (simplify
2196 (shift @0 SSA_NAME@1)
2197 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2198 (with {
2199 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2200 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2201 }
2202 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2203 @0)))))
2204#endif
2205
6a78ea5d 2206/* Rewrite an LROTATE_EXPR by a constant into an
2207 RROTATE_EXPR by a new constant. */
2208(simplify
2209 (lrotate @0 INTEGER_CST@1)
0808dfce 2210 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
6a78ea5d 2211 build_int_cst (TREE_TYPE (@1),
2212 element_precision (type)), @1); }))
2213
a76fc0fd 2214/* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2215(for op (lrotate rrotate rshift lshift)
2216 (simplify
2217 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2218 (with { unsigned int prec = element_precision (type); }
e3d0f65c 2219 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2220 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2221 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2222 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
cb7bca5f 2223 (with { unsigned int low = (tree_to_uhwi (@1)
2224 + tree_to_uhwi (@2)); }
a76fc0fd 2225 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2226 being well defined. */
2227 (if (low >= prec)
2228 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
d74b7335 2229 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
05999bfc 2230 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
d74b7335 2231 { build_zero_cst (type); }
2232 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2233 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
a76fc0fd 2234
2235
2e4fb82e 2236/* ((1 << A) & 1) != 0 -> A == 0
2237 ((1 << A) & 1) == 0 -> A != 0 */
2238(for cmp (ne eq)
2239 icmp (eq ne)
2240 (simplify
2241 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2242 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
662274ee 2243
9d5c80c1 2244/* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2245 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2246 if CST2 != 0. */
2247(for cmp (ne eq)
2248 (simplify
2249 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
e3d0f65c 2250 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
9d5c80c1 2251 (if (cand < 0
2252 || (!integer_zerop (@2)
e3d0f65c 2253 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
d74b7335 2254 { constant_boolean_node (cmp == NE_EXPR, type); }
2255 (if (!integer_zerop (@2)
e3d0f65c 2256 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
d74b7335 2257 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
9d5c80c1 2258
4c8f3f72 2259/* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2260 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2261 if the new mask might be further optimized. */
2262(for shift (lshift rshift)
2263 (simplify
2bc3151f 2264 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2265 INTEGER_CST@2)
4c8f3f72 2266 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2267 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2268 && tree_fits_uhwi_p (@1)
2269 && tree_to_uhwi (@1) > 0
2270 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2271 (with
2272 {
2273 unsigned int shiftc = tree_to_uhwi (@1);
2274 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2275 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2276 tree shift_type = TREE_TYPE (@3);
2277 unsigned int prec;
2278
2279 if (shift == LSHIFT_EXPR)
edc19fd0 2280 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4c8f3f72 2281 else if (shift == RSHIFT_EXPR
654ba22c 2282 && type_has_mode_precision_p (shift_type))
4c8f3f72 2283 {
2284 prec = TYPE_PRECISION (TREE_TYPE (@3));
2285 tree arg00 = @0;
2286 /* See if more bits can be proven as zero because of
2287 zero extension. */
2288 if (@3 != @0
2289 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2290 {
2291 tree inner_type = TREE_TYPE (@0);
654ba22c 2292 if (type_has_mode_precision_p (inner_type)
4c8f3f72 2293 && TYPE_PRECISION (inner_type) < prec)
2294 {
2295 prec = TYPE_PRECISION (inner_type);
2296 /* See if we can shorten the right shift. */
2297 if (shiftc < prec)
2298 shift_type = inner_type;
2299 /* Otherwise X >> C1 is all zeros, so we'll optimize
2300 it into (X, 0) later on by making sure zerobits
2301 is all ones. */
2302 }
2303 }
7097b942 2304 zerobits = HOST_WIDE_INT_M1U;
4c8f3f72 2305 if (shiftc < prec)
2306 {
2307 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2308 zerobits <<= prec - shiftc;
2309 }
2310 /* For arithmetic shift if sign bit could be set, zerobits
2311 can contain actually sign bits, so no transformation is
2312 possible, unless MASK masks them all away. In that
2313 case the shift needs to be converted into logical shift. */
2314 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2315 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2316 {
2317 if ((mask & zerobits) == 0)
2318 shift_type = unsigned_type_for (TREE_TYPE (@3));
2319 else
2320 zerobits = 0;
2321 }
2322 }
2323 }
2324 /* ((X << 16) & 0xff00) is (X, 0). */
2325 (if ((mask & zerobits) == mask)
d74b7335 2326 { build_int_cst (type, 0); }
2327 (with { newmask = mask | zerobits; }
2328 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2329 (with
2330 {
2331 /* Only do the transformation if NEWMASK is some integer
2332 mode's mask. */
2333 for (prec = BITS_PER_UNIT;
2334 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
edc19fd0 2335 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
d74b7335 2336 break;
2337 }
2338 (if (prec < HOST_BITS_PER_WIDE_INT
7097b942 2339 || newmask == HOST_WIDE_INT_M1U)
d74b7335 2340 (with
2341 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2342 (if (!tree_int_cst_equal (newmaskt, @2))
2343 (if (shift_type != TREE_TYPE (@3))
2344 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2345 (bit_and @4 { newmaskt; })))))))))))))
4c8f3f72 2346
4de74241 2347/* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2348 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
54c06b77 2349(for shift (lshift rshift)
4de74241 2350 (for bit_op (bit_and bit_xor bit_ior)
2351 (simplify
2352 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2353 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2354 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2355 (bit_op (shift (convert @0) @1) { mask; }))))))
54c06b77 2356
2f6a4d21 2357/* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2358(simplify
2359 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2360 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
c0205bab 2361 && (element_precision (TREE_TYPE (@0))
2362 <= element_precision (TREE_TYPE (@1))
2363 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2f6a4d21 2364 (with
2365 { tree shift_type = TREE_TYPE (@0); }
2366 (convert (rshift (convert:shift_type @1) @2)))))
2367
2368/* ~(~X >>r Y) -> X >>r Y
2369 ~(~X <<r Y) -> X <<r Y */
2370(for rotate (lrotate rrotate)
2371 (simplify
2372 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
c0205bab 2373 (if ((element_precision (TREE_TYPE (@0))
2374 <= element_precision (TREE_TYPE (@1))
2375 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2376 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2377 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2f6a4d21 2378 (with
2379 { tree rotate_type = TREE_TYPE (@0); }
2380 (convert (rotate (convert:rotate_type @1) @2))))))
54c06b77 2381
6d58fd21 2382/* Simplifications of conversions. */
2383
2384/* Basic strip-useless-type-conversions / strip_nops. */
2ad7e37a 2385(for cvt (convert view_convert float fix_trunc)
6d58fd21 2386 (simplify
2387 (cvt @0)
2388 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2389 || (GENERIC && type == TREE_TYPE (@0)))
2390 @0)))
2391
2392/* Contract view-conversions. */
2393(simplify
2394 (view_convert (view_convert @0))
2395 (view_convert @0))
2396
2397/* For integral conversions with the same precision or pointer
2398 conversions use a NOP_EXPR instead. */
2399(simplify
2400 (view_convert @0)
2401 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2402 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2403 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2404 (convert @0)))
2405
ac857b3d 2406/* Strip inner integral conversions that do not change precision or size, or
2407 zero-extend while keeping the same size (for bool-to-char). */
6d58fd21 2408(simplify
2409 (view_convert (convert@0 @1))
2410 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2411 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
ac857b3d 2412 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2413 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2414 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2415 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
6d58fd21 2416 (view_convert @1)))
2417
2418/* Re-association barriers around constants and other re-association
2419 barriers can be removed. */
2420(simplify
2421 (paren CONSTANT_CLASS_P@0)
2422 @0)
2423(simplify
2424 (paren (paren@1 @0))
2425 @1)
c434260f 2426
2427/* Handle cases of two conversions in a row. */
2428(for ocvt (convert float fix_trunc)
2429 (for icvt (convert float)
2430 (simplify
2431 (ocvt (icvt@1 @0))
2432 (with
2433 {
2434 tree inside_type = TREE_TYPE (@0);
2435 tree inter_type = TREE_TYPE (@1);
2436 int inside_int = INTEGRAL_TYPE_P (inside_type);
2437 int inside_ptr = POINTER_TYPE_P (inside_type);
2438 int inside_float = FLOAT_TYPE_P (inside_type);
ccb14741 2439 int inside_vec = VECTOR_TYPE_P (inside_type);
c434260f 2440 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2441 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2442 int inter_int = INTEGRAL_TYPE_P (inter_type);
2443 int inter_ptr = POINTER_TYPE_P (inter_type);
2444 int inter_float = FLOAT_TYPE_P (inter_type);
ccb14741 2445 int inter_vec = VECTOR_TYPE_P (inter_type);
c434260f 2446 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2447 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2448 int final_int = INTEGRAL_TYPE_P (type);
2449 int final_ptr = POINTER_TYPE_P (type);
2450 int final_float = FLOAT_TYPE_P (type);
ccb14741 2451 int final_vec = VECTOR_TYPE_P (type);
c434260f 2452 unsigned int final_prec = TYPE_PRECISION (type);
2453 int final_unsignedp = TYPE_UNSIGNED (type);
2454 }
f998447f 2455 (switch
2456 /* In addition to the cases of two conversions in a row
2457 handled below, if we are converting something to its own
2458 type via an object of identical or wider precision, neither
2459 conversion is needed. */
2460 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2461 || (GENERIC
2462 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2463 && (((inter_int || inter_ptr) && final_int)
2464 || (inter_float && final_float))
2465 && inter_prec >= final_prec)
2466 (ocvt @0))
2467
2468 /* Likewise, if the intermediate and initial types are either both
2469 float or both integer, we don't need the middle conversion if the
2470 former is wider than the latter and doesn't change the signedness
2471 (for integers). Avoid this if the final type is a pointer since
f2bfcc60 2472 then we sometimes need the middle conversion. */
f998447f 2473 (if (((inter_int && inside_int) || (inter_float && inside_float))
2474 && (final_int || final_float)
2475 && inter_prec >= inside_prec
f2bfcc60 2476 && (inter_float || inter_unsignedp == inside_unsignedp))
f998447f 2477 (ocvt @0))
2478
2479 /* If we have a sign-extension of a zero-extended value, we can
2480 replace that by a single zero-extension. Likewise if the
2481 final conversion does not change precision we can drop the
2482 intermediate conversion. */
2483 (if (inside_int && inter_int && final_int
2484 && ((inside_prec < inter_prec && inter_prec < final_prec
2485 && inside_unsignedp && !inter_unsignedp)
2486 || final_prec == inter_prec))
2487 (ocvt @0))
2488
2489 /* Two conversions in a row are not needed unless:
c434260f 2490 - some conversion is floating-point (overstrict for now), or
2491 - some conversion is a vector (overstrict for now), or
2492 - the intermediate type is narrower than both initial and
2493 final, or
2494 - the intermediate type and innermost type differ in signedness,
2495 and the outermost type is wider than the intermediate, or
2496 - the initial type is a pointer type and the precisions of the
2497 intermediate and final types differ, or
2498 - the final type is a pointer type and the precisions of the
2499 initial and intermediate types differ. */
f998447f 2500 (if (! inside_float && ! inter_float && ! final_float
2501 && ! inside_vec && ! inter_vec && ! final_vec
2502 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2503 && ! (inside_int && inter_int
2504 && inter_unsignedp != inside_unsignedp
2505 && inter_prec < final_prec)
2506 && ((inter_unsignedp && inter_prec > inside_prec)
2507 == (final_unsignedp && final_prec > inter_prec))
2508 && ! (inside_ptr && inter_prec != final_prec)
f2bfcc60 2509 && ! (final_ptr && inside_prec != inter_prec))
f998447f 2510 (ocvt @0))
2511
2512 /* A truncation to an unsigned type (a zero-extension) should be
2513 canonicalized as bitwise and of a mask. */
645a9d8e 2514 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2515 && final_int && inter_int && inside_int
f998447f 2516 && final_prec == inside_prec
2517 && final_prec > inter_prec
2518 && inter_unsignedp)
2519 (convert (bit_and @0 { wide_int_to_tree
2520 (inside_type,
2521 wi::mask (inter_prec, false,
2522 TYPE_PRECISION (inside_type))); })))
2523
2524 /* If we are converting an integer to a floating-point that can
2525 represent it exactly and back to an integer, we can skip the
2526 floating-point conversion. */
2527 (if (GIMPLE /* PR66211 */
2528 && inside_int && inter_float && final_int &&
2529 (unsigned) significand_size (TYPE_MODE (inter_type))
2530 >= inside_prec - !inside_unsignedp)
2531 (convert @0)))))))
e4076540 2532
2533/* If we have a narrowing conversion to an integral type that is fed by a
2534 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2535 masks off bits outside the final type (and nothing else). */
2536(simplify
2537 (convert (bit_and @0 INTEGER_CST@1))
2538 (if (INTEGRAL_TYPE_P (type)
2539 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2540 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2541 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2542 TYPE_PRECISION (type)), 0))
2543 (convert @0)))
6f5ffe81 2544
2545
2546/* (X /[ex] A) * A -> X. */
2547(simplify
c76c65ae 2548 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2549 (convert @0))
20ba0684 2550
6a78ea5d 2551/* Canonicalization of binary operations. */
2552
2553/* Convert X + -C into X - C. */
2554(simplify
2555 (plus @0 REAL_CST@1)
2556 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
0808dfce 2557 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
6a78ea5d 2558 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2559 (minus @0 { tem; })))))
2560
bff407f2 2561/* Convert x+x into x*2. */
6a78ea5d 2562(simplify
2563 (plus @0 @0)
2564 (if (SCALAR_FLOAT_TYPE_P (type))
bff407f2 2565 (mult @0 { build_real (type, dconst2); })
2566 (if (INTEGRAL_TYPE_P (type))
2567 (mult @0 { build_int_cst (type, 2); }))))
6a78ea5d 2568
ff3aa16e 2569/* 0 - X -> -X. */
6a78ea5d 2570(simplify
2571 (minus integer_zerop @1)
2572 (negate @1))
ff3aa16e 2573(simplify
2574 (pointer_diff integer_zerop @1)
2575 (negate (convert @1)))
6a78ea5d 2576
2577/* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2578 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2579 (-ARG1 + ARG0) reduces to -ARG1. */
2580(simplify
2581 (minus real_zerop@0 @1)
2582 (if (fold_real_zero_addition_p (type, @0, 0))
2583 (negate @1)))
2584
2585/* Transform x * -1 into -x. */
2586(simplify
2587 (mult @0 integer_minus_onep)
2588 (negate @0))
20ba0684 2589
6b7eaf05 2590/* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2591 signed overflow for CST != 0 && CST != -1. */
2592(simplify
f359ebf1 2593 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
6b7eaf05 2594 (if (TREE_CODE (@2) != INTEGER_CST
f359ebf1 2595 && single_use (@3)
6b7eaf05 2596 && !integer_zerop (@1) && !integer_minus_onep (@1))
2597 (mult (mult @0 @2) @1)))
2598
5e324b84 2599/* True if we can easily extract the real and imaginary parts of a complex
2600 number. */
2601(match compositional_complex
2602 (convert? (complex @0 @1)))
2603
20ba0684 2604/* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2605(simplify
2606 (complex (realpart @0) (imagpart @0))
2607 @0)
2608(simplify
2609 (realpart (complex @0 @1))
2610 @0)
2611(simplify
2612 (imagpart (complex @0 @1))
2613 @1)
e18f6095 2614
d92f7c8d 2615/* Sometimes we only care about half of a complex expression. */
2616(simplify
2617 (realpart (convert?:s (conj:s @0)))
2618 (convert (realpart @0)))
2619(simplify
2620 (imagpart (convert?:s (conj:s @0)))
2621 (convert (negate (imagpart @0))))
2622(for part (realpart imagpart)
2623 (for op (plus minus)
2624 (simplify
2625 (part (convert?:s@2 (op:s @0 @1)))
2626 (convert (op (part @0) (part @1))))))
2627(simplify
2628 (realpart (convert?:s (CEXPI:s @0)))
2629 (convert (COS @0)))
2630(simplify
2631 (imagpart (convert?:s (CEXPI:s @0)))
2632 (convert (SIN @0)))
2633
2634/* conj(conj(x)) -> x */
2635(simplify
2636 (conj (convert? (conj @0)))
2637 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2638 (convert @0)))
2639
2640/* conj({x,y}) -> {x,-y} */
2641(simplify
2642 (conj (convert?:s (complex:s @0 @1)))
2643 (with { tree itype = TREE_TYPE (type); }
2644 (complex (convert:itype @0) (negate (convert:itype @1)))))
e18f6095 2645
2646/* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2647(for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2648 (simplify
2649 (bswap (bswap @0))
2650 @0)
2651 (simplify
2652 (bswap (bit_not (bswap @0)))
2653 (bit_not @0))
2654 (for bitop (bit_xor bit_ior bit_and)
2655 (simplify
2656 (bswap (bitop:c (bswap @0) @1))
2657 (bitop @0 (bswap @1)))))
84debb86 2658
2659
2660/* Combine COND_EXPRs and VEC_COND_EXPRs. */
2661
2662/* Simplify constant conditions.
2663 Only optimize constant conditions when the selected branch
2664 has the same type as the COND_EXPR. This avoids optimizing
2665 away "c ? x : throw", where the throw has a void type.
2666 Note that we cannot throw away the fold-const.c variant nor
2667 this one as we depend on doing this transform before possibly
2668 A ? B : B -> B triggers and the fold-const.c one can optimize
2669 0 ? A : B to B even if A has side-effects. Something
2670 genmatch cannot handle. */
2671(simplify
2672 (cond INTEGER_CST@0 @1 @2)
d74b7335 2673 (if (integer_zerop (@0))
2674 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2675 @2)
2676 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2677 @1)))
84debb86 2678(simplify
2679 (vec_cond VECTOR_CST@0 @1 @2)
2680 (if (integer_all_onesp (@0))
d74b7335 2681 @1
2682 (if (integer_zerop (@0))
2683 @2)))
84debb86 2684
83c0fb43 2685/* Simplification moved from fold_cond_expr_with_comparison. It may also
2686 be extended. */
4e33b632 2687/* This pattern implements two kinds simplification:
2688
2689 Case 1)
2690 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
83c0fb43 2691 1) Conversions are type widening from smaller type.
2692 2) Const c1 equals to c2 after canonicalizing comparison.
2693 3) Comparison has tree code LT, LE, GT or GE.
2694 This specific pattern is needed when (cmp (convert x) c) may not
2695 be simplified by comparison patterns because of multiple uses of
2696 x. It also makes sense here because simplifying across multiple
4e33b632 2697 referred var is always benefitial for complicated cases.
2698
2699 Case 2)
2700 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2701(for cmp (lt le gt ge eq)
83c0fb43 2702 (simplify
a5a96d9f 2703 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
83c0fb43 2704 (with
2705 {
2706 tree from_type = TREE_TYPE (@1);
2707 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
a5a96d9f 2708 enum tree_code code = ERROR_MARK;
83c0fb43 2709
a5a96d9f 2710 if (INTEGRAL_TYPE_P (from_type)
2711 && int_fits_type_p (@2, from_type)
83c0fb43 2712 && (types_match (c1_type, from_type)
2713 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2714 && (TYPE_UNSIGNED (from_type)
2715 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2716 && (types_match (c2_type, from_type)
2717 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2718 && (TYPE_UNSIGNED (from_type)
2719 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2720 {
a5a96d9f 2721 if (cmp != EQ_EXPR)
83c0fb43 2722 {
4e33b632 2723 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2724 {
2725 /* X <= Y - 1 equals to X < Y. */
a5a96d9f 2726 if (cmp == LE_EXPR)
4e33b632 2727 code = LT_EXPR;
2728 /* X > Y - 1 equals to X >= Y. */
a5a96d9f 2729 if (cmp == GT_EXPR)
4e33b632 2730 code = GE_EXPR;
2731 }
2732 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2733 {
2734 /* X < Y + 1 equals to X <= Y. */
a5a96d9f 2735 if (cmp == LT_EXPR)
4e33b632 2736 code = LE_EXPR;
2737 /* X >= Y + 1 equals to X > Y. */
a5a96d9f 2738 if (cmp == GE_EXPR)
4e33b632 2739 code = GT_EXPR;
2740 }
a5a96d9f 2741 if (code != ERROR_MARK
2742 || wi::to_widest (@2) == wi::to_widest (@3))
4e33b632 2743 {
a5a96d9f 2744 if (cmp == LT_EXPR || cmp == LE_EXPR)
4e33b632 2745 code = MIN_EXPR;
a5a96d9f 2746 if (cmp == GT_EXPR || cmp == GE_EXPR)
4e33b632 2747 code = MAX_EXPR;
2748 }
83c0fb43 2749 }
4e33b632 2750 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
a5a96d9f 2751 else if (int_fits_type_p (@3, from_type))
2752 code = EQ_EXPR;
83c0fb43 2753 }
2754 }
2755 (if (code == MAX_EXPR)
70241e81 2756 (convert (max @1 (convert @2)))
83c0fb43 2757 (if (code == MIN_EXPR)
70241e81 2758 (convert (min @1 (convert @2)))
4e33b632 2759 (if (code == EQ_EXPR)
a5a96d9f 2760 (convert (cond (eq @1 (convert @3))
70241e81 2761 (convert:from_type @3) (convert:from_type @2)))))))))
83c0fb43 2762
e7a6ef61 2763/* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2764
2765 1) OP is PLUS or MINUS.
2766 2) CMP is LT, LE, GT or GE.
2767 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2768
2769 This pattern also handles special cases like:
2770
2771 A) Operand x is a unsigned to signed type conversion and c1 is
2772 integer zero. In this case,
2773 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2774 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2775 B) Const c1 may not equal to (C3 op' C2). In this case we also
2776 check equality for (c1+1) and (c1-1) by adjusting comparison
2777 code.
2778
2779 TODO: Though signed type is handled by this pattern, it cannot be
2780 simplified at the moment because C standard requires additional
2781 type promotion. In order to match&simplify it here, the IR needs
2782 to be cleaned up by other optimizers, i.e, VRP. */
2783(for op (plus minus)
2784 (for cmp (lt le gt ge)
2785 (simplify
2786 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2787 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2788 (if (types_match (from_type, to_type)
2789 /* Check if it is special case A). */
2790 || (TYPE_UNSIGNED (from_type)
2791 && !TYPE_UNSIGNED (to_type)
2792 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2793 && integer_zerop (@1)
2794 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2795 (with
2796 {
2797 bool overflow = false;
2798 enum tree_code code, cmp_code = cmp;
e3d0f65c 2799 wide_int real_c1;
2800 wide_int c1 = wi::to_wide (@1);
2801 wide_int c2 = wi::to_wide (@2);
2802 wide_int c3 = wi::to_wide (@3);
e7a6ef61 2803 signop sgn = TYPE_SIGN (from_type);
2804
2805 /* Handle special case A), given x of unsigned type:
2806 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2807 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2808 if (!types_match (from_type, to_type))
2809 {
2810 if (cmp_code == LT_EXPR)
2811 cmp_code = GT_EXPR;
2812 if (cmp_code == GE_EXPR)
2813 cmp_code = LE_EXPR;
2814 c1 = wi::max_value (to_type);
2815 }
2816 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2817 compute (c3 op' c2) and check if it equals to c1 with op' being
2818 the inverted operator of op. Make sure overflow doesn't happen
2819 if it is undefined. */
2820 if (op == PLUS_EXPR)
2821 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2822 else
2823 real_c1 = wi::add (c3, c2, sgn, &overflow);
2824
2825 code = cmp_code;
2826 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2827 {
2828 /* Check if c1 equals to real_c1. Boundary condition is handled
2829 by adjusting comparison operation if necessary. */
2830 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2831 && !overflow)
2832 {
2833 /* X <= Y - 1 equals to X < Y. */
2834 if (cmp_code == LE_EXPR)
2835 code = LT_EXPR;
2836 /* X > Y - 1 equals to X >= Y. */
2837 if (cmp_code == GT_EXPR)
2838 code = GE_EXPR;
2839 }
2840 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2841 && !overflow)
2842 {
2843 /* X < Y + 1 equals to X <= Y. */
2844 if (cmp_code == LT_EXPR)
2845 code = LE_EXPR;
2846 /* X >= Y + 1 equals to X > Y. */
2847 if (cmp_code == GE_EXPR)
2848 code = GT_EXPR;
2849 }
2850 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2851 {
2852 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2853 code = MIN_EXPR;
2854 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2855 code = MAX_EXPR;
2856 }
2857 }
2858 }
2859 (if (code == MAX_EXPR)
2860 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2861 { wide_int_to_tree (from_type, c2); })
2862 (if (code == MIN_EXPR)
2863 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2864 { wide_int_to_tree (from_type, c2); })))))))))
2865
84debb86 2866(for cnd (cond vec_cond)
2867 /* A ? B : (A ? X : C) -> A ? B : C. */
2868 (simplify
2869 (cnd @0 (cnd @0 @1 @2) @3)
2870 (cnd @0 @1 @3))
2871 (simplify
2872 (cnd @0 @1 (cnd @0 @2 @3))
2873 (cnd @0 @1 @3))
79f1ed25 2874 /* A ? B : (!A ? C : X) -> A ? B : C. */
2875 /* ??? This matches embedded conditions open-coded because genmatch
2876 would generate matching code for conditions in separate stmts only.
2877 The following is still important to merge then and else arm cases
2878 from if-conversion. */
2879 (simplify
2880 (cnd @0 @1 (cnd @2 @3 @4))
2881 (if (COMPARISON_CLASS_P (@0)
2882 && COMPARISON_CLASS_P (@2)
2883 && invert_tree_comparison
2884 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2885 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2886 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2887 (cnd @0 @1 @3)))
2888 (simplify
2889 (cnd @0 (cnd @1 @2 @3) @4)
2890 (if (COMPARISON_CLASS_P (@0)
2891 && COMPARISON_CLASS_P (@1)
2892 && invert_tree_comparison
2893 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2894 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2895 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2896 (cnd @0 @3 @4)))
84debb86 2897
2898 /* A ? B : B -> B. */
2899 (simplify
2900 (cnd @0 @1 @1)
ccb14741 2901 @1)
84debb86 2902
ccb14741 2903 /* !A ? B : C -> A ? C : B. */
2904 (simplify
2905 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2906 (cnd @0 @2 @1)))
9ec260f0 2907
19db1ec8 2908/* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2909 return all -1 or all 0 results. */
b13fec6a 2910/* ??? We could instead convert all instances of the vec_cond to negate,
2911 but that isn't necessarily a win on its own. */
2912(simplify
19db1ec8 2913 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
b13fec6a 2914 (if (VECTOR_TYPE_P (type)
f08ee65f 2915 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2916 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
b13fec6a 2917 && (TYPE_MODE (TREE_TYPE (type))
1a86097c 2918 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
19db1ec8 2919 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
b13fec6a 2920
19db1ec8 2921/* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
b13fec6a 2922(simplify
19db1ec8 2923 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
b13fec6a 2924 (if (VECTOR_TYPE_P (type)
f08ee65f 2925 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2926 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
b13fec6a 2927 && (TYPE_MODE (TREE_TYPE (type))
1a86097c 2928 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
19db1ec8 2929 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
9ec260f0 2930
fd425e62 2931
9ec260f0 2932/* Simplifications of comparisons. */
2933
67d07229 2934/* See if we can reduce the magnitude of a constant involved in a
2935 comparison by changing the comparison code. This is a canonicalization
2936 formerly done by maybe_canonicalize_comparison_1. */
2937(for cmp (le gt)
2938 acmp (lt ge)
2939 (simplify
2940 (cmp @0 INTEGER_CST@1)
2941 (if (tree_int_cst_sgn (@1) == -1)
e3d0f65c 2942 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
67d07229 2943(for cmp (ge lt)
2944 acmp (gt le)
2945 (simplify
2946 (cmp @0 INTEGER_CST@1)
2947 (if (tree_int_cst_sgn (@1) == 1)
e3d0f65c 2948 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
67d07229 2949
2950
9ec260f0 2951/* We can simplify a logical negation of a comparison to the
2952 inverted comparison. As we cannot compute an expression
2953 operator using invert_tree_comparison we have to simulate
2954 that with expression code iteration. */
2955(for cmp (tcc_comparison)
2956 icmp (inverted_tcc_comparison)
2957 ncmp (inverted_tcc_comparison_with_nans)
2958 /* Ideally we'd like to combine the following two patterns
2959 and handle some more cases by using
2960 (logical_inverted_value (cmp @0 @1))
2961 here but for that genmatch would need to "inline" that.
2962 For now implement what forward_propagate_comparison did. */
2963 (simplify
2964 (bit_not (cmp @0 @1))
2965 (if (VECTOR_TYPE_P (type)
2966 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2967 /* Comparison inversion may be impossible for trapping math,
2968 invert_tree_comparison will tell us. But we can't use
2969 a computed operator in the replacement tree thus we have
2970 to play the trick below. */
2971 (with { enum tree_code ic = invert_tree_comparison
93633022 2972 (cmp, HONOR_NANS (@0)); }
9ec260f0 2973 (if (ic == icmp)
d74b7335 2974 (icmp @0 @1)
2975 (if (ic == ncmp)
2976 (ncmp @0 @1))))))
9ec260f0 2977 (simplify
ccb14741 2978 (bit_xor (cmp @0 @1) integer_truep)
2979 (with { enum tree_code ic = invert_tree_comparison
93633022 2980 (cmp, HONOR_NANS (@0)); }
ccb14741 2981 (if (ic == icmp)
d74b7335 2982 (icmp @0 @1)
2983 (if (ic == ncmp)
2984 (ncmp @0 @1))))))
f8dad9b4 2985
fd425e62 2986/* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2987 ??? The transformation is valid for the other operators if overflow
2988 is undefined for the type, but performing it here badly interacts
2989 with the transformation in fold_cond_expr_with_comparison which
2990 attempts to synthetize ABS_EXPR. */
2991(for cmp (eq ne)
57e83b58 2992 (for sub (minus pointer_diff)
2993 (simplify
2994 (cmp (sub@2 @0 @1) integer_zerop)
2995 (if (single_use (@2))
2996 (cmp @0 @1)))))
fd425e62 2997
2998/* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2999 signed arithmetic case. That form is created by the compiler
3000 often enough for folding it to be of value. One example is in
3001 computing loop trip counts after Operator Strength Reduction. */
c915fa32 3002(for cmp (simple_comparison)
3003 scmp (swapped_simple_comparison)
fd425e62 3004 (simplify
cf293d8e 3005 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
fd425e62 3006 /* Handle unfolded multiplication by zero. */
3007 (if (integer_zerop (@1))
d74b7335 3008 (cmp @1 @2)
3009 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
cf293d8e 3010 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3011 && single_use (@3))
d74b7335 3012 /* If @1 is negative we swap the sense of the comparison. */
3013 (if (tree_int_cst_sgn (@1) < 0)
3014 (scmp @0 @2)
3015 (cmp @0 @2))))))
fd425e62 3016
3017/* Simplify comparison of something with itself. For IEEE
3018 floating-point, we can only do some of these simplifications. */
be6e4283 3019(for cmp (eq ge le)
fd425e62 3020 (simplify
3021 (cmp @0 @0)
be6e4283 3022 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2daa2cda 3023 || ! HONOR_NANS (@0))
be6e4283 3024 { constant_boolean_node (true, type); }
3025 (if (cmp != EQ_EXPR)
3026 (eq @0 @0)))))
fd425e62 3027(for cmp (ne gt lt)
3028 (simplify
3029 (cmp @0 @0)
3030 (if (cmp != NE_EXPR
3031 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2daa2cda 3032 || ! HONOR_NANS (@0))
fd425e62 3033 { constant_boolean_node (false, type); })))
56b4132e 3034(for cmp (unle unge uneq)
3035 (simplify
3036 (cmp @0 @0)
3037 { constant_boolean_node (true, type); }))
47ef9633 3038(for cmp (unlt ungt)
3039 (simplify
3040 (cmp @0 @0)
3041 (unordered @0 @0)))
56b4132e 3042(simplify
3043 (ltgt @0 @0)
3044 (if (!flag_trapping_math)
3045 { constant_boolean_node (false, type); }))
fd425e62 3046
3047/* Fold ~X op ~Y as Y op X. */
c915fa32 3048(for cmp (simple_comparison)
fd425e62 3049 (simplify
46c54c0f 3050 (cmp (bit_not@2 @0) (bit_not@3 @1))
3051 (if (single_use (@2) && single_use (@3))
3052 (cmp @1 @0))))
fd425e62 3053
3054/* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
c915fa32 3055(for cmp (simple_comparison)
3056 scmp (swapped_simple_comparison)
fd425e62 3057 (simplify
46c54c0f 3058 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3059 (if (single_use (@2)
3060 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
fd425e62 3061 (scmp @0 (bit_not @1)))))
3062
c915fa32 3063(for cmp (simple_comparison)
3064 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3065 (simplify
3066 (cmp (convert@2 @0) (convert? @1))
3067 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3068 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3069 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3070 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3071 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3072 (with
3073 {
3074 tree type1 = TREE_TYPE (@1);
3075 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3076 {
3077 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3078 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3079 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3080 type1 = float_type_node;
3081 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3082 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3083 type1 = double_type_node;
3084 }
3085 tree newtype
3086 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3087 ? TREE_TYPE (@0) : type1);
3088 }
3089 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3090 (cmp (convert:newtype @0) (convert:newtype @1))))))
3091
3092 (simplify
3093 (cmp @0 REAL_CST@1)
3094 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
f998447f 3095 (switch
3096 /* a CMP (-0) -> a CMP 0 */
3097 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3098 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3099 /* x != NaN is always true, other ops are always false. */
3100 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3101 && ! HONOR_SNANS (@1))
3102 { constant_boolean_node (cmp == NE_EXPR, type); })
3103 /* Fold comparisons against infinity. */
3104 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3105 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3106 (with
3107 {
3108 REAL_VALUE_TYPE max;
3109 enum tree_code code = cmp;
3110 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3111 if (neg)
3112 code = swap_tree_comparison (code);
3113 }
3114 (switch
38f4caae 3115 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
f998447f 3116 (if (code == GT_EXPR
38f4caae 3117 && !(HONOR_NANS (@0) && flag_trapping_math))
f998447f 3118 { constant_boolean_node (false, type); })
3119 (if (code == LE_EXPR)
38f4caae 3120 /* x <= +Inf is always true, if we don't care about NaNs. */
f998447f 3121 (if (! HONOR_NANS (@0))
3122 { constant_boolean_node (true, type); }
38f4caae 3123 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3124 an "invalid" exception. */
3125 (if (!flag_trapping_math)
3126 (eq @0 @0))))
3127 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3128 for == this introduces an exception for x a NaN. */
3129 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3130 || code == GE_EXPR)
f998447f 3131 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3132 (if (neg)
3133 (lt @0 { build_real (TREE_TYPE (@0), max); })
3134 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3135 /* x < +Inf is always equal to x <= DBL_MAX. */
3136 (if (code == LT_EXPR)
3137 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3138 (if (neg)
3139 (ge @0 { build_real (TREE_TYPE (@0), max); })
3140 (le @0 { build_real (TREE_TYPE (@0), max); }))))
38f4caae 3141 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3142 an exception for x a NaN so use an unordered comparison. */
f998447f 3143 (if (code == NE_EXPR)
3144 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3145 (if (! HONOR_NANS (@0))
3146 (if (neg)
3147 (ge @0 { build_real (TREE_TYPE (@0), max); })
3148 (le @0 { build_real (TREE_TYPE (@0), max); }))
3149 (if (neg)
38f4caae 3150 (unge @0 { build_real (TREE_TYPE (@0), max); })
3151 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
c915fa32 3152
3153 /* If this is a comparison of a real constant with a PLUS_EXPR
3154 or a MINUS_EXPR of a real constant, we can convert it into a
3155 comparison with a revised real constant as long as no overflow
3156 occurs when unsafe_math_optimizations are enabled. */
3157 (if (flag_unsafe_math_optimizations)
3158 (for op (plus minus)
3159 (simplify
3160 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3161 (with
3162 {
3163 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3164 TREE_TYPE (@1), @2, @1);
3165 }
a7b8025b 3166 (if (tem && !TREE_OVERFLOW (tem))
c915fa32 3167 (cmp @0 { tem; }))))))
3168
3169 /* Likewise, we can simplify a comparison of a real constant with
3170 a MINUS_EXPR whose first operand is also a real constant, i.e.
3171 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3172 floating-point types only if -fassociative-math is set. */
3173 (if (flag_associative_math)
3174 (simplify
e7966fe9 3175 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
c915fa32 3176 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
a7b8025b 3177 (if (tem && !TREE_OVERFLOW (tem))
c915fa32 3178 (cmp { tem; } @1)))))
3179
3180 /* Fold comparisons against built-in math functions. */
3181 (if (flag_unsafe_math_optimizations
3182 && ! flag_errno_math)
3183 (for sq (SQRT)
3184 (simplify
3185 (cmp (sq @0) REAL_CST@1)
f998447f 3186 (switch
3187 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3188 (switch
3189 /* sqrt(x) < y is always false, if y is negative. */
3190 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
d74b7335 3191 { constant_boolean_node (false, type); })
f998447f 3192 /* sqrt(x) > y is always true, if y is negative and we
3193 don't care about NaNs, i.e. negative values of x. */
3194 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3195 { constant_boolean_node (true, type); })
3196 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3197 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
b5cb0512 3198 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3199 (switch
3200 /* sqrt(x) < 0 is always false. */
3201 (if (cmp == LT_EXPR)
3202 { constant_boolean_node (false, type); })
3203 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3204 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3205 { constant_boolean_node (true, type); })
3206 /* sqrt(x) <= 0 -> x == 0. */
3207 (if (cmp == LE_EXPR)
3208 (eq @0 @1))
3209 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3210 == or !=. In the last case:
3211
3212 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3213
3214 if x is negative or NaN. Due to -funsafe-math-optimizations,
3215 the results for other x follow from natural arithmetic. */
3216 (cmp @0 @1)))
f998447f 3217 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3218 (with
3219 {
3220 REAL_VALUE_TYPE c2;
f2ad9e38 3221 real_arithmetic (&c2, MULT_EXPR,
3222 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
f998447f 3223 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3224 }
3225 (if (REAL_VALUE_ISINF (c2))
3226 /* sqrt(x) > y is x == +Inf, when y is very large. */
3227 (if (HONOR_INFINITIES (@0))
3228 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3229 { constant_boolean_node (false, type); })
3230 /* sqrt(x) > c is the same as x > c*c. */
3231 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3232 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3233 (with
3234 {
3235 REAL_VALUE_TYPE c2;
f2ad9e38 3236 real_arithmetic (&c2, MULT_EXPR,
3237 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
f998447f 3238 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3239 }
3240 (if (REAL_VALUE_ISINF (c2))
3241 (switch
3242 /* sqrt(x) < y is always true, when y is a very large
3243 value and we don't care about NaNs or Infinities. */
3244 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3245 { constant_boolean_node (true, type); })
3246 /* sqrt(x) < y is x != +Inf when y is very large and we
3247 don't care about NaNs. */
3248 (if (! HONOR_NANS (@0))
3249 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3250 /* sqrt(x) < y is x >= 0 when y is very large and we
3251 don't care about Infinities. */
3252 (if (! HONOR_INFINITIES (@0))
3253 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3254 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3255 (if (GENERIC)
3256 (truth_andif
3257 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3258 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3259 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3260 (if (! HONOR_NANS (@0))
3261 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3262 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3263 (if (GENERIC)
3264 (truth_andif
3265 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
44142143 3266 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3267 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3268 (simplify
3269 (cmp (sq @0) (sq @1))
3270 (if (! HONOR_NANS (@0))
3271 (cmp @0 @1))))))
fd425e62 3272
29587d84 3273/* Optimize various special cases of (FTYPE) N CMP CST. */
3274(for cmp (lt le eq ne ge gt)
3275 icmp (le le eq ne ge ge)
3276 (simplify
3277 (cmp (float @0) REAL_CST@1)
3278 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3279 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3280 (with
3281 {
3282 tree itype = TREE_TYPE (@0);
3283 signop isign = TYPE_SIGN (itype);
3284 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3285 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3286 /* Be careful to preserve any potential exceptions due to
3287 NaNs. qNaNs are ok in == or != context.
3288 TODO: relax under -fno-trapping-math or
3289 -fno-signaling-nans. */
3290 bool exception_p
3291 = real_isnan (cst) && (cst->signalling
a924ac80 3292 || (cmp != EQ_EXPR && cmp != NE_EXPR));
29587d84 3293 /* INT?_MIN is power-of-two so it takes
3294 only one mantissa bit. */
3295 bool signed_p = isign == SIGNED;
3296 bool itype_fits_ftype_p
3297 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3298 }
3299 /* TODO: allow non-fitting itype and SNaNs when
3300 -fno-trapping-math. */
3301 (if (itype_fits_ftype_p && ! exception_p)
3302 (with
3303 {
3304 REAL_VALUE_TYPE imin, imax;
3305 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3306 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3307
3308 REAL_VALUE_TYPE icst;
3309 if (cmp == GT_EXPR || cmp == GE_EXPR)
3310 real_ceil (&icst, fmt, cst);
3311 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3312 real_floor (&icst, fmt, cst);
3313 else
3314 real_trunc (&icst, fmt, cst);
3315
8360e5df 3316 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
29587d84 3317
3318 bool overflow_p = false;
3319 wide_int icst_val
3320 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3321 }
3322 (switch
3323 /* Optimize cases when CST is outside of ITYPE's range. */
3324 (if (real_compare (LT_EXPR, cst, &imin))
3325 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3326 type); })
3327 (if (real_compare (GT_EXPR, cst, &imax))
3328 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3329 type); })
3330 /* Remove cast if CST is an integer representable by ITYPE. */
3331 (if (cst_int_p)
3332 (cmp @0 { gcc_assert (!overflow_p);
3333 wide_int_to_tree (itype, icst_val); })
3334 )
3335 /* When CST is fractional, optimize
3336 (FTYPE) N == CST -> 0
3337 (FTYPE) N != CST -> 1. */
3338 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3339 { constant_boolean_node (cmp == NE_EXPR, type); })
3340 /* Otherwise replace with sensible integer constant. */
3341 (with
3342 {
3343 gcc_checking_assert (!overflow_p);
3344 }
3345 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3346
d040acf9 3347/* Fold A /[ex] B CMP C to A CMP B * C. */
3348(for cmp (eq ne)
3349 (simplify
3350 (cmp (exact_div @0 @1) INTEGER_CST@2)
3351 (if (!integer_zerop (@1))
e3d0f65c 3352 (if (wi::to_wide (@2) == 0)
d040acf9 3353 (cmp @0 @2)
3354 (if (TREE_CODE (@1) == INTEGER_CST)
3355 (with
3356 {
3357 bool ovf;
e3d0f65c 3358 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3359 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
d040acf9 3360 }
3361 (if (ovf)
3362 { constant_boolean_node (cmp == NE_EXPR, type); }
3363 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3364(for cmp (lt le gt ge)
3365 (simplify
3366 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
e3d0f65c 3367 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
d040acf9 3368 (with
3369 {
3370 bool ovf;
e3d0f65c 3371 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3372 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
d040acf9 3373 }
3374 (if (ovf)
e3d0f65c 3375 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3376 TYPE_SIGN (TREE_TYPE (@2)))
d040acf9 3377 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3378 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3379
57d742ac 3380/* Unordered tests if either argument is a NaN. */
3381(simplify
3382 (bit_ior (unordered @0 @0) (unordered @1 @1))
b1306f12 3383 (if (types_match (@0, @1))
57d742ac 3384 (unordered @0 @1)))
c59372d1 3385(simplify
3386 (bit_and (ordered @0 @0) (ordered @1 @1))
3387 (if (types_match (@0, @1))
3388 (ordered @0 @1)))
57d742ac 3389(simplify
3390 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3391 @2)
c59372d1 3392(simplify
3393 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3394 @2)
f8dad9b4 3395
f9ec342f 3396/* Simple range test simplifications. */
3397/* A < B || A >= B -> true. */
7717d90b 3398(for test1 (lt le le le ne ge)
3399 test2 (ge gt ge ne eq ne)
f9ec342f 3400 (simplify
3401 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3402 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3403 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3404 { constant_boolean_node (true, type); })))
3405/* A < B && A >= B -> false. */
3406(for test1 (lt lt lt le ne eq)
3407 test2 (ge gt eq gt eq gt)
3408 (simplify
3409 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3410 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3411 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3412 { constant_boolean_node (false, type); })))
3413
77b822cc 3414/* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3415 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3416
3417 Note that comparisons
3418 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3419 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3420 will be canonicalized to above so there's no need to
3421 consider them here.
3422 */
3423
3424(for cmp (le gt)
3425 eqcmp (eq ne)
3426 (simplify
3427 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3428 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3429 (with
3430 {
3431 tree ty = TREE_TYPE (@0);
3432 unsigned prec = TYPE_PRECISION (ty);
3433 wide_int mask = wi::to_wide (@2, prec);
3434 wide_int rhs = wi::to_wide (@3, prec);
3435 signop sgn = TYPE_SIGN (ty);
3436 }
3437 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3438 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3439 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3440 { build_zero_cst (ty); }))))))
3441
0c3c84e3 3442/* -A CMP -B -> B CMP A. */
3443(for cmp (tcc_comparison)
3444 scmp (swapped_tcc_comparison)
3445 (simplify
3446 (cmp (negate @0) (negate @1))
3447 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3448 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3449 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3450 (scmp @0 @1)))
3451 (simplify
3452 (cmp (negate @0) CONSTANT_CLASS_P@1)
3453 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3454 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3455 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
0808dfce 3456 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
0c3c84e3 3457 (if (tem && !TREE_OVERFLOW (tem))
3458 (scmp @0 { tem; }))))))
3459
06e4870e 3460/* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3461(for op (eq ne)
3462 (simplify
3463 (op (abs @0) zerop@1)
3464 (op @0 @1)))
3465
3e39e829 3466/* From fold_sign_changed_comparison and fold_widened_comparison.
3467 FIXME: the lack of symmetry is disturbing. */
7725cd5b 3468(for cmp (simple_comparison)
3469 (simplify
3470 (cmp (convert@0 @00) (convert?@1 @10))
7550d855 3471 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7725cd5b 3472 /* Disable this optimization if we're casting a function pointer
3473 type on targets that require function pointer canonicalization. */
3474 && !(targetm.have_canonicalize_funcptr_for_compare ()
3475 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
4e503e6f 3476 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3477 && single_use (@0))
7725cd5b 3478 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3479 && (TREE_CODE (@10) == INTEGER_CST
3e39e829 3480 || @1 != @10)
7725cd5b 3481 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3482 || cmp == NE_EXPR
3483 || cmp == EQ_EXPR)
3e39e829 3484 && !POINTER_TYPE_P (TREE_TYPE (@00)))
7725cd5b 3485 /* ??? The special-casing of INTEGER_CST conversion was in the original
3486 code and here to avoid a spurious overflow flag on the resulting
3487 constant which fold_convert produces. */
3488 (if (TREE_CODE (@1) == INTEGER_CST)
3489 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3490 TREE_OVERFLOW (@1)); })
3491 (cmp @00 (convert @1)))
3492
3493 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3494 /* If possible, express the comparison in the shorter mode. */
3495 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
7079d57a 3496 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3497 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3498 && TYPE_UNSIGNED (TREE_TYPE (@00))))
7725cd5b 3499 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3500 || ((TYPE_PRECISION (TREE_TYPE (@00))
3501 >= TYPE_PRECISION (TREE_TYPE (@10)))
3502 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3503 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3504 || (TREE_CODE (@10) == INTEGER_CST
5f223778 3505 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
7725cd5b 3506 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3507 (cmp @00 (convert @10))
3508 (if (TREE_CODE (@10) == INTEGER_CST
5f223778 3509 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
7725cd5b 3510 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3511 (with
3512 {
3513 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3514 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3515 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3516 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3517 }
3518 (if (above || below)
3519 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3520 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3521 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3522 { constant_boolean_node (above ? true : false, type); }
3523 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3524 { constant_boolean_node (above ? false : true, type); }))))))))))))
69693ea7 3525
8218bff7 3526(for cmp (eq ne)
3527 /* A local variable can never be pointed to by
3528 the default SSA name of an incoming parameter.
3529 SSA names are canonicalized to 2nd place. */
3530 (simplify
3531 (cmp addr@0 SSA_NAME@1)
3532 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3533 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3534 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3535 (if (TREE_CODE (base) == VAR_DECL
3536 && auto_var_in_fn_p (base, current_function_decl))
3537 (if (cmp == NE_EXPR)
3538 { constant_boolean_node (true, type); }
3539 { constant_boolean_node (false, type); }))))))
3540
69693ea7 3541/* Equality compare simplifications from fold_binary */
3542(for cmp (eq ne)
3543
3544 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3545 Similarly for NE_EXPR. */
3546 (simplify
3547 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3548 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
e3d0f65c 3549 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
69693ea7 3550 { constant_boolean_node (cmp == NE_EXPR, type); }))
3551
3552 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3553 (simplify
3554 (cmp (bit_xor @0 @1) integer_zerop)
3555 (cmp @0 @1))
3556
3557 /* (X ^ Y) == Y becomes X == 0.
3558 Likewise (X ^ Y) == X becomes Y == 0. */
3559 (simplify
6b440285 3560 (cmp:c (bit_xor:c @0 @1) @0)
69693ea7 3561 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3562
3563 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3564 (simplify
3565 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3566 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
f1054432 3567 (cmp @0 (bit_xor @1 (convert @2)))))
f1054432 3568
3569 (simplify
3570 (cmp (convert? addr@0) integer_zerop)
3571 (if (tree_single_nonzero_warnv_p (@0, NULL))
3572 { constant_boolean_node (cmp == NE_EXPR, type); })))
3573
06e4870e 3574/* If we have (A & C) == C where C is a power of 2, convert this into
3575 (A & C) != 0. Similarly for NE_EXPR. */
3576(for cmp (eq ne)
3577 icmp (ne eq)
3578 (simplify
3579 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3580 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3581
458de25d 3582/* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3583 convert this into a shift followed by ANDing with D. */
3584(simplify
3585 (cond
3586 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
9859f662 3587 INTEGER_CST@2 integer_zerop)
3588 (if (integer_pow2p (@2))
3589 (with {
3590 int shift = (wi::exact_log2 (wi::to_wide (@2))
3591 - wi::exact_log2 (wi::to_wide (@1)));
3592 }
3593 (if (shift > 0)
3594 (bit_and
3595 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3596 (bit_and
3597 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3598 @2)))))
458de25d 3599
06e4870e 3600/* If we have (A & C) != 0 where C is the sign bit of A, convert
3601 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3602(for cmp (eq ne)
3603 ncmp (ge lt)
3604 (simplify
3605 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3606 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
654ba22c 3607 && type_has_mode_precision_p (TREE_TYPE (@0))
06e4870e 3608 && element_precision (@2) >= element_precision (@0)
e3d0f65c 3609 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
06e4870e 3610 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3611 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3612
458de25d 3613/* If we have A < 0 ? C : 0 where C is a power of 2, convert
fbfc7874 3614 this into a right shift or sign extension followed by ANDing with C. */
458de25d 3615(simplify
3616 (cond
3617 (lt @0 integer_zerop)
9859f662 3618 INTEGER_CST@1 integer_zerop)
3619 (if (integer_pow2p (@1)
3620 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
fbfc7874 3621 (with {
e3d0f65c 3622 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
fbfc7874 3623 }
3624 (if (shift >= 0)
3625 (bit_and
3626 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3627 @1)
3628 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3629 sign extension followed by AND with C will achieve the effect. */
3630 (bit_and (convert @0) @1)))))
458de25d 3631
aa0bf078 3632/* When the addresses are not directly of decls compare base and offset.
3633 This implements some remaining parts of fold_comparison address
3634 comparisons but still no complete part of it. Still it is good
3635 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3636(for cmp (simple_comparison)
3637 (simplify
c4699ff5 3638 (cmp (convert1?@2 addr@0) (convert2? addr@1))
aa0bf078 3639 (with
3640 {
773078cb 3641 poly_int64 off0, off1;
aa0bf078 3642 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3643 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3644 if (base0 && TREE_CODE (base0) == MEM_REF)
3645 {
90ca1268 3646 off0 += mem_ref_offset (base0).force_shwi ();
aa0bf078 3647 base0 = TREE_OPERAND (base0, 0);
3648 }
3649 if (base1 && TREE_CODE (base1) == MEM_REF)
3650 {
90ca1268 3651 off1 += mem_ref_offset (base1).force_shwi ();
aa0bf078 3652 base1 = TREE_OPERAND (base1, 0);
3653 }
3654 }
5e0b3d2f 3655 (if (base0 && base1)
3656 (with
3657 {
84ad0bc1 3658 int equal = 2;
9c8aeb66 3659 /* Punt in GENERIC on variables with value expressions;
3660 the value expressions might point to fields/elements
3661 of other vars etc. */
3662 if (GENERIC
3663 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3664 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3665 ;
3666 else if (decl_in_symtab_p (base0)
3667 && decl_in_symtab_p (base1))
5e0b3d2f 3668 equal = symtab_node::get_create (base0)
3669 ->equal_address_to (symtab_node::get_create (base1));
04a37b12 3670 else if ((DECL_P (base0)
3671 || TREE_CODE (base0) == SSA_NAME
3672 || TREE_CODE (base0) == STRING_CST)
3673 && (DECL_P (base1)
3674 || TREE_CODE (base1) == SSA_NAME
3675 || TREE_CODE (base1) == STRING_CST))
84ad0bc1 3676 equal = (base0 == base1);
5e0b3d2f 3677 }
977361a1 3678 (if (equal == 1
3679 && (cmp == EQ_EXPR || cmp == NE_EXPR
3680 /* If the offsets are equal we can ignore overflow. */
3681 || known_eq (off0, off1)
3682 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3683 /* Or if we compare using pointers to decls or strings. */
3684 || (POINTER_TYPE_P (TREE_TYPE (@2))
3685 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
5e0b3d2f 3686 (switch
773078cb 3687 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3688 { constant_boolean_node (known_eq (off0, off1), type); })
3689 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3690 { constant_boolean_node (known_ne (off0, off1), type); })
3691 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3692 { constant_boolean_node (known_lt (off0, off1), type); })
3693 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3694 { constant_boolean_node (known_le (off0, off1), type); })
3695 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3696 { constant_boolean_node (known_ge (off0, off1), type); })
3697 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3698 { constant_boolean_node (known_gt (off0, off1), type); }))
5e0b3d2f 3699 (if (equal == 0
3700 && DECL_P (base0) && DECL_P (base1)
3701 /* If we compare this as integers require equal offset. */
3702 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
773078cb 3703 || known_eq (off0, off1)))
5e0b3d2f 3704 (switch
3705 (if (cmp == EQ_EXPR)
3706 { constant_boolean_node (false, type); })
3707 (if (cmp == NE_EXPR)
3708 { constant_boolean_node (true, type); })))))))))
69693ea7 3709
73447cc5 3710/* Simplify pointer equality compares using PTA. */
3711(for neeq (ne eq)
3712 (simplify
3713 (neeq @0 @1)
3714 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3715 && ptrs_compare_unequal (@0, @1))
326e3d4e 3716 { constant_boolean_node (neeq != EQ_EXPR, type); })))
73447cc5 3717
b2930f09 3718/* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
c91ee73b 3719 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3720 Disable the transform if either operand is pointer to function.
3721 This broke pr22051-2.c for arm where function pointer
3722 canonicalizaion is not wanted. */
a2f87f25 3723
b2930f09 3724(for cmp (ne eq)
3725 (simplify
3726 (cmp (convert @0) INTEGER_CST@1)
ba3186de 3727 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
3728 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3729 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3730 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3731 && POINTER_TYPE_P (TREE_TYPE (@1))
3732 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3733 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
b2930f09 3734 (cmp @0 (convert @1)))))
3735
0a45b027 3736/* Non-equality compare simplifications from fold_binary */
3737(for cmp (lt gt le ge)
3738 /* Comparisons with the highest or lowest possible integer of
3739 the specified precision will have known values. */
3740 (simplify
3741 (cmp (convert?@2 @0) INTEGER_CST@1)
3742 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3743 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3744 (with
3745 {
3746 tree arg1_type = TREE_TYPE (@1);
3747 unsigned int prec = TYPE_PRECISION (arg1_type);
3748 wide_int max = wi::max_value (arg1_type);
3749 wide_int signed_max = wi::max_value (prec, SIGNED);
3750 wide_int min = wi::min_value (arg1_type);
3751 }
3752 (switch
e3d0f65c 3753 (if (wi::to_wide (@1) == max)
0a45b027 3754 (switch
3755 (if (cmp == GT_EXPR)
3756 { constant_boolean_node (false, type); })
3757 (if (cmp == GE_EXPR)
3758 (eq @2 @1))
3759 (if (cmp == LE_EXPR)
3760 { constant_boolean_node (true, type); })
3761 (if (cmp == LT_EXPR)
3762 (ne @2 @1))))
e3d0f65c 3763 (if (wi::to_wide (@1) == min)
0a45b027 3764 (switch
3765 (if (cmp == LT_EXPR)
3766 { constant_boolean_node (false, type); })
3767 (if (cmp == LE_EXPR)
3768 (eq @2 @1))
3769 (if (cmp == GE_EXPR)
3770 { constant_boolean_node (true, type); })
3771 (if (cmp == GT_EXPR)
3772 (ne @2 @1))))
e3d0f65c 3773 (if (wi::to_wide (@1) == max - 1)
39e21ca8 3774 (switch
3775 (if (cmp == GT_EXPR)
e3d0f65c 3776 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
39e21ca8 3777 (if (cmp == LE_EXPR)
e3d0f65c 3778 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3779 (if (wi::to_wide (@1) == min + 1)
0a45b027 3780 (switch
3781 (if (cmp == GE_EXPR)
e3d0f65c 3782 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
0a45b027 3783 (if (cmp == LT_EXPR)
e3d0f65c 3784 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3785 (if (wi::to_wide (@1) == signed_max
0a45b027 3786 && TYPE_UNSIGNED (arg1_type)
3787 /* We will flip the signedness of the comparison operator
3788 associated with the mode of @1, so the sign bit is
3789 specified by this mode. Check that @1 is the signed
3790 max associated with this sign bit. */
03b7a719 3791 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
0a45b027 3792 /* signed_type does not work on pointer types. */
3793 && INTEGRAL_TYPE_P (arg1_type))
3794 /* The following case also applies to X < signed_max+1
3795 and X >= signed_max+1 because previous transformations. */
3796 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3797 (with { tree st = signed_type_for (arg1_type); }
3798 (if (cmp == LE_EXPR)
3799 (ge (convert:st @0) { build_zero_cst (st); })
3800 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3801
56b4132e 3802(for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3803 /* If the second operand is NaN, the result is constant. */
3804 (simplify
3805 (cmp @0 REAL_CST@1)
3806 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3807 && (cmp != LTGT_EXPR || ! flag_trapping_math))
05999bfc 3808 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
56b4132e 3809 ? false : true, type); })))
0a45b027 3810
004b4ca3 3811/* bool_var != 0 becomes bool_var. */
3812(simplify
56b4132e 3813 (ne @0 integer_zerop)
004b4ca3 3814 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3815 && types_match (type, TREE_TYPE (@0)))
3816 (non_lvalue @0)))
3817/* bool_var == 1 becomes bool_var. */
3818(simplify
56b4132e 3819 (eq @0 integer_onep)
004b4ca3 3820 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3821 && types_match (type, TREE_TYPE (@0)))
3822 (non_lvalue @0)))
56b4132e 3823/* Do not handle
3824 bool_var == 0 becomes !bool_var or
3825 bool_var != 1 becomes !bool_var
3826 here because that only is good in assignment context as long
3827 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3828 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3829 clearly less optimal and which we'll transform again in forwprop. */
004b4ca3 3830
8c51f92f 3831/* When one argument is a constant, overflow detection can be simplified.
3832 Currently restricted to single use so as not to interfere too much with
3833 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3834 A + CST CMP A -> A CMP' CST' */
3835(for cmp (lt le ge gt)
3836 out (gt gt le le)
3837 (simplify
a1a80007 3838 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
8c51f92f 3839 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3840 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
e3d0f65c 3841 && wi::to_wide (@1) != 0
8c51f92f 3842 && single_use (@2))
e3d0f65c 3843 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3844 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3845 wi::max_value (prec, UNSIGNED)
3846 - wi::to_wide (@1)); })))))
8c51f92f 3847
dfba0905 3848/* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3849 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3850 expects the long form, so we restrict the transformation for now. */
3851(for cmp (gt le)
3852 (simplify
a1a80007 3853 (cmp:c (minus@2 @0 @1) @0)
dfba0905 3854 (if (single_use (@2)
3855 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3856 && TYPE_UNSIGNED (TREE_TYPE (@0))
3857 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3858 (cmp @1 @0))))
dfba0905 3859
3860/* Testing for overflow is unnecessary if we already know the result. */
dfba0905 3861/* A - B > A */
3862(for cmp (gt le)
3863 out (ne eq)
3864 (simplify
a1a80007 3865 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
dfba0905 3866 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3867 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3868 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3869/* A + B < A */
3870(for cmp (lt ge)
3871 out (ne eq)
3872 (simplify
a1a80007 3873 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
dfba0905 3874 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3875 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3876 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3877
8928f8d3 3878/* For unsigned operands, -1 / B < A checks whether A * B would overflow.
765557c0 3879 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
765557c0 3880(for cmp (lt ge)
3881 out (ne eq)
3882 (simplify
8928f8d3 3883 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
765557c0 3884 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3885 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3886 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
004b4ca3 3887
a153e7b3 3888/* Simplification of math builtins. These rules must all be optimizations
3889 as well as IL simplifications. If there is a possibility that the new
3890 form could be a pessimization, the rule should go in the canonicalization
3891 section that follows this one.
f8dad9b4 3892
a153e7b3 3893 Rules can generally go in this section if they satisfy one of
3894 the following:
3895
3896 - the rule describes an identity
3897
3898 - the rule replaces calls with something as simple as addition or
3899 multiplication
3900
3901 - the rule contains unary calls only and simplifies the surrounding
3902 arithmetic. (The idea here is to exclude non-unary calls in which
3903 one operand is constant and in which the call is known to be cheap
3904 when the operand has that value.) */
e09fc89c 3905
a153e7b3 3906(if (flag_unsafe_math_optimizations)
e09fc89c 3907 /* Simplify sqrt(x) * sqrt(x) -> x. */
3908 (simplify
054e9558 3909 (mult (SQRT_ALL@1 @0) @1)
e09fc89c 3910 (if (!HONOR_SNANS (type))
3911 @0))
3912
402ad641 3913 (for op (plus minus)
3914 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3915 (simplify
3916 (op (rdiv @0 @1)
3917 (rdiv @2 @1))
3918 (rdiv (op @0 @2) @1)))
3919
b036e74b 3920 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3921 (for root (SQRT CBRT)
3922 (simplify
3923 (mult (root:s @0) (root:s @1))
3924 (root (mult @0 @1))))
3925
b036e74b 3926 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3927 (for exps (EXP EXP2 EXP10 POW10)
3928 (simplify
3929 (mult (exps:s @0) (exps:s @1))
3930 (exps (plus @0 @1))))
3931
e09fc89c 3932 /* Simplify a/root(b/c) into a*root(c/b). */
b036e74b 3933 (for root (SQRT CBRT)
3934 (simplify
3935 (rdiv @0 (root:s (rdiv:s @1 @2)))
3936 (mult @0 (root (rdiv @2 @1)))))
3937
3938 /* Simplify x/expN(y) into x*expN(-y). */
3939 (for exps (EXP EXP2 EXP10 POW10)
3940 (simplify
3941 (rdiv @0 (exps:s @1))
3942 (mult @0 (exps (negate @1)))))
e09fc89c 3943
6b0c8e84 3944 (for logs (LOG LOG2 LOG10 LOG10)
3945 exps (EXP EXP2 EXP10 POW10)
762f6b3b 3946 /* logN(expN(x)) -> x. */
f8dad9b4 3947 (simplify
3948 (logs (exps @0))
762f6b3b 3949 @0)
3950 /* expN(logN(x)) -> x. */
3951 (simplify
3952 (exps (logs @0))
3953 @0))
a153e7b3 3954
f8dad9b4 3955 /* Optimize logN(func()) for various exponential functions. We
3956 want to determine the value "x" and the power "exponent" in
3957 order to transform logN(x**exponent) into exponent*logN(x). */
6b0c8e84 3958 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3959 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
f8dad9b4 3960 (simplify
3961 (logs (exps @0))
3a18d05c 3962 (if (SCALAR_FLOAT_TYPE_P (type))
3963 (with {
3964 tree x;
3965 switch (exps)
3966 {
3967 CASE_CFN_EXP:
3968 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3969 x = build_real_truncate (type, dconst_e ());
3970 break;
3971 CASE_CFN_EXP2:
3972 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3973 x = build_real (type, dconst2);
3974 break;
3975 CASE_CFN_EXP10:
3976 CASE_CFN_POW10:
3977 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3978 {
3979 REAL_VALUE_TYPE dconst10;
3980 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3981 x = build_real (type, dconst10);
3982 }
3983 break;
3984 default:
3985 gcc_unreachable ();
3986 }
3987 }
3988 (mult (logs { x; }) @0)))))
a153e7b3 3989
f8dad9b4 3990 (for logs (LOG LOG
3991 LOG2 LOG2
3992 LOG10 LOG10)
3993 exps (SQRT CBRT)
3994 (simplify
3995 (logs (exps @0))
3a18d05c 3996 (if (SCALAR_FLOAT_TYPE_P (type))
3997 (with {
3998 tree x;
3999 switch (exps)
4000 {
4001 CASE_CFN_SQRT:
4002 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4003 x = build_real (type, dconsthalf);
4004 break;
4005 CASE_CFN_CBRT:
4006 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4007 x = build_real_truncate (type, dconst_third ());
4008 break;
4009 default:
4010 gcc_unreachable ();
4011 }
4012 }
4013 (mult { x; } (logs @0))))))
a153e7b3 4014
4015 /* logN(pow(x,exponent)) -> exponent*logN(x). */
f8dad9b4 4016 (for logs (LOG LOG2 LOG10)
4017 pows (POW)
4018 (simplify
4019 (logs (pows @0 @1))
a153e7b3 4020 (mult @1 (logs @0))))
4021
a35ca444 4022 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4023 or if C is a positive power of 2,
4024 pow(C,x) -> exp2(log2(C)*x). */
91674b2a 4025#if GIMPLE
6b0b3b89 4026 (for pows (POW)
4027 exps (EXP)
4028 logs (LOG)
a35ca444 4029 exp2s (EXP2)
4030 log2s (LOG2)
6b0b3b89 4031 (simplify
4032 (pows REAL_CST@0 @1)
a35ca444 4033 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
d38b0287 4034 && real_isfinite (TREE_REAL_CST_PTR (@0))
4035 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4036 the use_exp2 case until after vectorization. It seems actually
4037 beneficial for all constants to postpone this until later,
4038 because exp(log(C)*x), while faster, will have worse precision
4039 and if x folds into a constant too, that is unnecessary
4040 pessimization. */
4041 && canonicalize_math_after_vectorization_p ())
a35ca444 4042 (with {
4043 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4044 bool use_exp2 = false;
4045 if (targetm.libc_has_function (function_c99_misc)
4046 && value->cl == rvc_normal)
4047 {
4048 REAL_VALUE_TYPE frac_rvt = *value;
4049 SET_REAL_EXP (&frac_rvt, 1);
4050 if (real_equal (&frac_rvt, &dconst1))
4051 use_exp2 = true;
4052 }
4053 }
4054 (if (!use_exp2)
91674b2a 4055 (if (optimize_pow_to_exp (@0, @1))
4056 (exps (mult (logs @0) @1)))
d38b0287 4057 (exp2s (mult (log2s @0) @1)))))))
91674b2a 4058#endif
6b0b3b89 4059
8664f262 4060 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4061 (for pows (POW)
4062 exps (EXP EXP2 EXP10 POW10)
4063 logs (LOG LOG2 LOG10 LOG10)
4064 (simplify
4065 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4066 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4067 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4068 (exps (plus (mult (logs @0) @1) @2)))))
4069
a153e7b3 4070 (for sqrts (SQRT)
4071 cbrts (CBRT)
cef15d09 4072 pows (POW)
a153e7b3 4073 exps (EXP EXP2 EXP10 POW10)
4074 /* sqrt(expN(x)) -> expN(x*0.5). */
4075 (simplify
4076 (sqrts (exps @0))
4077 (exps (mult @0 { build_real (type, dconsthalf); })))
4078 /* cbrt(expN(x)) -> expN(x/3). */
4079 (simplify
4080 (cbrts (exps @0))
cef15d09 4081 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4082 /* pow(expN(x), y) -> expN(x*y). */
4083 (simplify
4084 (pows (exps @0) @1)
4085 (exps (mult @0 @1))))
390c3cff 4086
4087 /* tan(atan(x)) -> x. */
4088 (for tans (TAN)
4089 atans (ATAN)
4090 (simplify
4091 (tans (atans @0))
4092 @0)))
a153e7b3 4093
d064d976 4094/* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4095(simplify
2f99fc04 4096 (CABS (complex:C @0 real_zerop@1))
d064d976 4097 (abs @0))
4098
6f5f406a 4099/* trunc(trunc(x)) -> trunc(x), etc. */
054e9558 4100(for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6f5f406a 4101 (simplify
4102 (fns (fns @0))
4103 (fns @0)))
4104/* f(x) -> x if x is integer valued and f does nothing for such values. */
054e9558 4105(for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6f5f406a 4106 (simplify
4107 (fns integer_valued_real_p@0)
4108 @0))
6f5f406a 4109
35c5f68c 4110/* hypot(x,0) and hypot(0,x) -> abs(x). */
4111(simplify
3a18d05c 4112 (HYPOT:c @0 real_zerop@1)
35c5f68c 4113 (abs @0))
4114
cef15d09 4115/* pow(1,x) -> 1. */
4116(simplify
4117 (POW real_onep@0 @1)
4118 @0)
4119
66dc9fcd 4120(simplify
4121 /* copysign(x,x) -> x. */
054e9558 4122 (COPYSIGN_ALL @0 @0)
66dc9fcd 4123 @0)
4124
4125(simplify
4126 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
054e9558 4127 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
66dc9fcd 4128 (abs @0))
4129
7d705d57 4130(for scale (LDEXP SCALBN SCALBLN)
4131 /* ldexp(0, x) -> 0. */
4132 (simplify
4133 (scale real_zerop@0 @1)
4134 @0)
4135 /* ldexp(x, 0) -> x. */
4136 (simplify
4137 (scale @0 integer_zerop@1)
4138 @0)
4139 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4140 (simplify
4141 (scale REAL_CST@0 @1)
4142 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4143 @0)))
4144
a153e7b3 4145/* Canonicalization of sequences of math builtins. These rules represent
4146 IL simplifications but are not necessarily optimizations.
4147
4148 The sincos pass is responsible for picking "optimal" implementations
4149 of math builtins, which may be more complicated and can sometimes go
4150 the other way, e.g. converting pow into a sequence of sqrts.
4151 We only want to do these canonicalizations before the pass has run. */
4152
4153(if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4154 /* Simplify tan(x) * cos(x) -> sin(x). */
4155 (simplify
4156 (mult:c (TAN:s @0) (COS:s @0))
4157 (SIN @0))
4158
4159 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4160 (simplify
cbcde149 4161 (mult:c @0 (POW:s @0 REAL_CST@1))
a153e7b3 4162 (if (!TREE_OVERFLOW (@1))
4163 (POW @0 (plus @1 { build_one_cst (type); }))))
4164
4165 /* Simplify sin(x) / cos(x) -> tan(x). */
4166 (simplify
4167 (rdiv (SIN:s @0) (COS:s @0))
4168 (TAN @0))
4169
4170 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4171 (simplify
4172 (rdiv (COS:s @0) (SIN:s @0))
4173 (rdiv { build_one_cst (type); } (TAN @0)))
4174
4175 /* Simplify sin(x) / tan(x) -> cos(x). */
4176 (simplify
4177 (rdiv (SIN:s @0) (TAN:s @0))
4178 (if (! HONOR_NANS (@0)
4179 && ! HONOR_INFINITIES (@0))
3a18d05c 4180 (COS @0)))
a153e7b3 4181
4182 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4183 (simplify
4184 (rdiv (TAN:s @0) (SIN:s @0))
4185 (if (! HONOR_NANS (@0)
4186 && ! HONOR_INFINITIES (@0))
4187 (rdiv { build_one_cst (type); } (COS @0))))
4188
4189 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4190 (simplify
4191 (mult (POW:s @0 @1) (POW:s @0 @2))
4192 (POW @0 (plus @1 @2)))
4193
4194 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4195 (simplify
4196 (mult (POW:s @0 @1) (POW:s @2 @1))
4197 (POW (mult @0 @2) @1))
4198
cbcde149 4199 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4200 (simplify
4201 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4202 (POWI (mult @0 @2) @1))
4203
a153e7b3 4204 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4205 (simplify
4206 (rdiv (POW:s @0 REAL_CST@1) @0)
4207 (if (!TREE_OVERFLOW (@1))
4208 (POW @0 (minus @1 { build_one_cst (type); }))))
4209
4210 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4211 (simplify
4212 (rdiv @0 (POW:s @1 @2))
4213 (mult @0 (POW @1 (negate @2))))
4214
4215 (for sqrts (SQRT)
4216 cbrts (CBRT)
4217 pows (POW)
4218 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4219 (simplify
4220 (sqrts (sqrts @0))
4221 (pows @0 { build_real (type, dconst_quarter ()); }))
4222 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4223 (simplify
4224 (sqrts (cbrts @0))
4225 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4226 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4227 (simplify
4228 (cbrts (sqrts @0))
4229 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4230 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4231 (simplify
4232 (cbrts (cbrts tree_expr_nonnegative_p@0))
4233 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4234 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4235 (simplify
4236 (sqrts (pows @0 @1))
4237 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4238 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4239 (simplify
4240 (cbrts (pows tree_expr_nonnegative_p@0 @1))
cef15d09 4241 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4242 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4243 (simplify
4244 (pows (sqrts @0) @1)
4245 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4246 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4247 (simplify
4248 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4249 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4250 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4251 (simplify
4252 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4253 (pows @0 (mult @1 @2))))
d064d976 4254
4255 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4256 (simplify
4257 (CABS (complex @0 @0))
5e324b84 4258 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4259
35c5f68c 4260 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4261 (simplify
4262 (HYPOT @0 @0)
4263 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4264
5e324b84 4265 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4266 (for cexps (CEXP)
4267 exps (EXP)
4268 cexpis (CEXPI)
4269 (simplify
4270 (cexps compositional_complex@0)
4271 (if (targetm.libc_has_function (function_c99_math_complex))
4272 (complex
4273 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4274 (mult @1 (imagpart @2)))))))
f8dad9b4 4275
6f5f406a 4276(if (canonicalize_math_p ())
4277 /* floor(x) -> trunc(x) if x is nonnegative. */
054e9558 4278 (for floors (FLOOR_ALL)
4279 truncs (TRUNC_ALL)
6f5f406a 4280 (simplify
4281 (floors tree_expr_nonnegative_p@0)
4282 (truncs @0))))
4283
4284(match double_value_p
4285 @0
4286 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4287(for froms (BUILT_IN_TRUNCL
4288 BUILT_IN_FLOORL
4289 BUILT_IN_CEILL
4290 BUILT_IN_ROUNDL
4291 BUILT_IN_NEARBYINTL
4292 BUILT_IN_RINTL)
4293 tos (BUILT_IN_TRUNC
4294 BUILT_IN_FLOOR
4295 BUILT_IN_CEIL
4296 BUILT_IN_ROUND
4297 BUILT_IN_NEARBYINT
4298 BUILT_IN_RINT)
4299 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4300 (if (optimize && canonicalize_math_p ())
4301 (simplify
4302 (froms (convert double_value_p@0))
4303 (convert (tos @0)))))
4304
4305(match float_value_p
4306 @0
4307 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4308(for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4309 BUILT_IN_FLOORL BUILT_IN_FLOOR
4310 BUILT_IN_CEILL BUILT_IN_CEIL
4311 BUILT_IN_ROUNDL BUILT_IN_ROUND
4312 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4313 BUILT_IN_RINTL BUILT_IN_RINT)
4314 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4315 BUILT_IN_FLOORF BUILT_IN_FLOORF
4316 BUILT_IN_CEILF BUILT_IN_CEILF
4317 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4318 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4319 BUILT_IN_RINTF BUILT_IN_RINTF)
4320 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4321 if x is a float. */
92159af9 4322 (if (optimize && canonicalize_math_p ()
4323 && targetm.libc_has_function (function_c99_misc))
6f5f406a 4324 (simplify
4325 (froms (convert float_value_p@0))
4326 (convert (tos @0)))))
4327
1117ed35 4328(for froms (XFLOORL XCEILL XROUNDL XRINTL)
4329 tos (XFLOOR XCEIL XROUND XRINT)
4330 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4331 (if (optimize && canonicalize_math_p ())
4332 (simplify
4333 (froms (convert double_value_p@0))
4334 (tos @0))))
4335
4336(for froms (XFLOORL XCEILL XROUNDL XRINTL
4337 XFLOOR XCEIL XROUND XRINT)
4338 tos (XFLOORF XCEILF XROUNDF XRINTF)
4339 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4340 if x is a float. */
4341 (if (optimize && canonicalize_math_p ())
4342 (simplify
4343 (froms (convert float_value_p@0))
4344 (tos @0))))
4345
4346(if (canonicalize_math_p ())
4347 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4348 (for floors (IFLOOR LFLOOR LLFLOOR)
4349 (simplify
4350 (floors tree_expr_nonnegative_p@0)
4351 (fix_trunc @0))))
4352
4353(if (canonicalize_math_p ())
4354 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4355 (for fns (IFLOOR LFLOOR LLFLOOR
4356 ICEIL LCEIL LLCEIL
4357 IROUND LROUND LLROUND)
4358 (simplify
4359 (fns integer_valued_real_p@0)
4360 (fix_trunc @0)))
4361 (if (!flag_errno_math)
4362 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4363 (for rints (IRINT LRINT LLRINT)
4364 (simplify
4365 (rints integer_valued_real_p@0)
4366 (fix_trunc @0)))))
4367
4368(if (canonicalize_math_p ())
4369 (for ifn (IFLOOR ICEIL IROUND IRINT)
4370 lfn (LFLOOR LCEIL LROUND LRINT)
4371 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4372 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4373 sizeof (int) == sizeof (long). */
4374 (if (TYPE_PRECISION (integer_type_node)
4375 == TYPE_PRECISION (long_integer_type_node))
4376 (simplify
4377 (ifn @0)
4378 (lfn:long_integer_type_node @0)))
4379 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4380 sizeof (long long) == sizeof (long). */
4381 (if (TYPE_PRECISION (long_long_integer_type_node)
4382 == TYPE_PRECISION (long_integer_type_node))
4383 (simplify
4384 (llfn @0)
4385 (lfn:long_integer_type_node @0)))))
4386
92f3c1b2 4387/* cproj(x) -> x if we're ignoring infinities. */
4388(simplify
4389 (CPROJ @0)
4390 (if (!HONOR_INFINITIES (type))
4391 @0))
4392
ec11da34 4393/* If the real part is inf and the imag part is known to be
4394 nonnegative, return (inf + 0i). */
4395(simplify
4396 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4397 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
92f3c1b2 4398 { build_complex_inf (type, false); }))
4399
ec11da34 4400/* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4401(simplify
4402 (CPROJ (complex @0 REAL_CST@1))
4403 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
92f3c1b2 4404 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
ec11da34 4405
cef15d09 4406(for pows (POW)
4407 sqrts (SQRT)
4408 cbrts (CBRT)
4409 (simplify
4410 (pows @0 REAL_CST@1)
4411 (with {
4412 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4413 REAL_VALUE_TYPE tmp;
4414 }
4415 (switch
4416 /* pow(x,0) -> 1. */
4417 (if (real_equal (value, &dconst0))
4418 { build_real (type, dconst1); })
4419 /* pow(x,1) -> x. */
4420 (if (real_equal (value, &dconst1))
4421 @0)
4422 /* pow(x,-1) -> 1/x. */
4423 (if (real_equal (value, &dconstm1))
4424 (rdiv { build_real (type, dconst1); } @0))
4425 /* pow(x,0.5) -> sqrt(x). */
4426 (if (flag_unsafe_math_optimizations
4427 && canonicalize_math_p ()
4428 && real_equal (value, &dconsthalf))
4429 (sqrts @0))
4430 /* pow(x,1/3) -> cbrt(x). */
4431 (if (flag_unsafe_math_optimizations
4432 && canonicalize_math_p ()
4433 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4434 real_equal (value, &tmp)))
4435 (cbrts @0))))))
ec11da34 4436
ff190980 4437/* powi(1,x) -> 1. */
4438(simplify
4439 (POWI real_onep@0 @1)
4440 @0)
4441
4442(simplify
4443 (POWI @0 INTEGER_CST@1)
4444 (switch
4445 /* powi(x,0) -> 1. */
e3d0f65c 4446 (if (wi::to_wide (@1) == 0)
ff190980 4447 { build_real (type, dconst1); })
4448 /* powi(x,1) -> x. */
e3d0f65c 4449 (if (wi::to_wide (@1) == 1)
ff190980 4450 @0)
4451 /* powi(x,-1) -> 1/x. */
e3d0f65c 4452 (if (wi::to_wide (@1) == -1)
ff190980 4453 (rdiv { build_real (type, dconst1); } @0))))
4454
eaa6752e 4455/* Narrowing of arithmetic and logical operations.
4456
4457 These are conceptually similar to the transformations performed for
4458 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4459 term we want to move all that code out of the front-ends into here. */
4460
4461/* If we have a narrowing conversion of an arithmetic operation where
4462 both operands are widening conversions from the same type as the outer
4463 narrowing conversion. Then convert the innermost operands to a suitable
67cf9b55 4464 unsigned type (to avoid introducing undefined behavior), perform the
eaa6752e 4465 operation and convert the result to the desired type. */
4466(for op (plus minus)
4467 (simplify
641ae1b8 4468 (convert (op:s (convert@2 @0) (convert?@3 @1)))
eaa6752e 4469 (if (INTEGRAL_TYPE_P (type)
4470 /* We check for type compatibility between @0 and @1 below,
4471 so there's no need to check that @1/@3 are integral types. */
4472 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4473 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4474 /* The precision of the type of each operand must match the
4475 precision of the mode of each operand, similarly for the
4476 result. */
654ba22c 4477 && type_has_mode_precision_p (TREE_TYPE (@0))
4478 && type_has_mode_precision_p (TREE_TYPE (@1))
4479 && type_has_mode_precision_p (type)
eaa6752e 4480 /* The inner conversion must be a widening conversion. */
4481 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
641ae1b8 4482 && types_match (@0, type)
4483 && (types_match (@0, @1)
4484 /* Or the second operand is const integer or converted const
4485 integer from valueize. */
4486 || TREE_CODE (@1) == INTEGER_CST))
eaa6752e 4487 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
641ae1b8 4488 (op @0 (convert @1))
d74b7335 4489 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
641ae1b8 4490 (convert (op (convert:utype @0)
4491 (convert:utype @1))))))))
8cd9143e 4492
4493/* This is another case of narrowing, specifically when there's an outer
4494 BIT_AND_EXPR which masks off bits outside the type of the innermost
4495 operands. Like the previous case we have to convert the operands
67cf9b55 4496 to unsigned types to avoid introducing undefined behavior for the
8cd9143e 4497 arithmetic operation. */
4498(for op (minus plus)
d74b7335 4499 (simplify
4500 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4501 (if (INTEGRAL_TYPE_P (type)
4502 /* We check for type compatibility between @0 and @1 below,
4503 so there's no need to check that @1/@3 are integral types. */
4504 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4505 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4506 /* The precision of the type of each operand must match the
4507 precision of the mode of each operand, similarly for the
4508 result. */
654ba22c 4509 && type_has_mode_precision_p (TREE_TYPE (@0))
4510 && type_has_mode_precision_p (TREE_TYPE (@1))
4511 && type_has_mode_precision_p (type)
d74b7335 4512 /* The inner conversion must be a widening conversion. */
4513 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4514 && types_match (@0, @1)
4515 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4516 <= TYPE_PRECISION (TREE_TYPE (@0)))
e3d0f65c 4517 && (wi::to_wide (@4)
4518 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4519 true, TYPE_PRECISION (type))) == 0)
d74b7335 4520 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4521 (with { tree ntype = TREE_TYPE (@0); }
4522 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4523 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4524 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4525 (convert:utype @4))))))))
6a8b7746 4526
4527/* Transform (@0 < @1 and @0 < @2) to use min,
4528 (@0 > @1 and @0 > @2) to use max */
2e933017 4529(for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4530 op (lt le gt ge lt le gt ge )
4531 ext (min min max max max max min min )
6a8b7746 4532 (simplify
2e933017 4533 (logic (op:cs @0 @1) (op:cs @0 @2))
0c1f7d17 4534 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4535 && TREE_CODE (@0) != INTEGER_CST)
6a8b7746 4536 (op @0 (ext @1 @2)))))
4537
01156bcb 4538(simplify
4539 /* signbit(x) -> 0 if x is nonnegative. */
4540 (SIGNBIT tree_expr_nonnegative_p@0)
4541 { integer_zero_node; })
4542
4543(simplify
4544 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4545 (SIGNBIT @0)
4546 (if (!HONOR_SIGNED_ZEROS (@0))
4547 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
eb1a077c 4548
4549/* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4550(for cmp (eq ne)
4551 (for op (plus minus)
4552 rop (minus plus)
4553 (simplify
4554 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4555 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4556 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4557 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4558 && !TYPE_SATURATING (TREE_TYPE (@0)))
4559 (with { tree res = int_const_binop (rop, @2, @1); }
af8ad81e 4560 (if (TREE_OVERFLOW (res)
4561 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
eb1a077c 4562 { constant_boolean_node (cmp == NE_EXPR, type); }
4563 (if (single_use (@3))
07b69d3f 4564 (cmp @0 { TREE_OVERFLOW (res)
4565 ? drop_tree_overflow (res) : res; }))))))))
eb1a077c 4566(for cmp (lt le gt ge)
4567 (for op (plus minus)
4568 rop (minus plus)
4569 (simplify
4570 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4571 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4572 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4573 (with { tree res = int_const_binop (rop, @2, @1); }
4574 (if (TREE_OVERFLOW (res))
4575 {
4576 fold_overflow_warning (("assuming signed overflow does not occur "
4577 "when simplifying conditional to constant"),
4578 WARN_STRICT_OVERFLOW_CONDITIONAL);
4579 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4580 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
e3d0f65c 4581 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4582 TYPE_SIGN (TREE_TYPE (@1)))
eb1a077c 4583 != (op == MINUS_EXPR);
4584 constant_boolean_node (less == ovf_high, type);
4585 }
4586 (if (single_use (@3))
4587 (with
4588 {
4589 fold_overflow_warning (("assuming signed overflow does not occur "
4590 "when changing X +- C1 cmp C2 to "
4591 "X cmp C2 -+ C1"),
4592 WARN_STRICT_OVERFLOW_COMPARISON);
4593 }
4594 (cmp @0 { res; })))))))))
26280bcd 4595
4596/* Canonicalizations of BIT_FIELD_REFs. */
4597
4598(simplify
4599 (BIT_FIELD_REF @0 @1 @2)
4600 (switch
4601 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4602 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4603 (switch
4604 (if (integer_zerop (@2))
4605 (view_convert (realpart @0)))
4606 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4607 (view_convert (imagpart @0)))))
4608 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4609 && INTEGRAL_TYPE_P (type)
a83b7b63 4610 /* On GIMPLE this should only apply to register arguments. */
4611 && (! GIMPLE || is_gimple_reg (@0))
26280bcd 4612 /* A bit-field-ref that referenced the full argument can be stripped. */
4613 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4614 && integer_zerop (@2))
4615 /* Low-parts can be reduced to integral conversions.
4616 ??? The following doesn't work for PDP endian. */
4617 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4618 /* Don't even think about BITS_BIG_ENDIAN. */
4619 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4620 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4621 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4622 ? (TYPE_PRECISION (TREE_TYPE (@0))
4623 - TYPE_PRECISION (type))
4624 : 0)) == 0)))
4625 (convert @0))))
4626
4627/* Simplify vector extracts. */
4628
4629(simplify
4630 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4631 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4632 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4633 || (VECTOR_TYPE_P (type)
4634 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4635 (with
4636 {
4637 tree ctor = (TREE_CODE (@0) == SSA_NAME
4638 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4639 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4640 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4641 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4642 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4643 }
4644 (if (n != 0
4645 && (idx % width) == 0
4646 && (n % width) == 0
f08ee65f 4647 && known_le ((idx + n) / width,
4648 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
26280bcd 4649 (with
4650 {
4651 idx = idx / width;
4652 n = n / width;
4653 /* Constructor elements can be subvectors. */
c64de46c 4654 poly_uint64 k = 1;
26280bcd 4655 if (CONSTRUCTOR_NELTS (ctor) != 0)
4656 {
4657 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4658 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4659 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4660 }
c64de46c 4661 unsigned HOST_WIDE_INT elt, count, const_k;
26280bcd 4662 }
4663 (switch
4664 /* We keep an exact subset of the constructor elements. */
c64de46c 4665 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
26280bcd 4666 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4667 { build_constructor (type, NULL); }
c64de46c 4668 (if (count == 1)
4669 (if (elt < CONSTRUCTOR_NELTS (ctor))
93b93567 4670 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
c64de46c 4671 { build_zero_cst (type); })
26280bcd 4672 {
c64de46c 4673 vec<constructor_elt, va_gc> *vals;
4674 vec_alloc (vals, count);
4675 for (unsigned i = 0;
4676 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
4677 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4678 CONSTRUCTOR_ELT (ctor, elt + i)->value);
4679 build_constructor (type, vals);
4680 })))
26280bcd 4681 /* The bitfield references a single constructor element. */
c64de46c 4682 (if (k.is_constant (&const_k)
4683 && idx + n <= (idx / const_k + 1) * const_k)
26280bcd 4684 (switch
c64de46c 4685 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
26280bcd 4686 { build_zero_cst (type); })
c64de46c 4687 (if (n == const_k)
93b93567 4688 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
c64de46c 4689 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
4690 @1 { bitsize_int ((idx % const_k) * width); })))))))))
a07b1b15 4691
4692/* Simplify a bit extraction from a bit insertion for the cases with
4693 the inserted element fully covering the extraction or the insertion
4694 not touching the extraction. */
4695(simplify
4696 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4697 (with
4698 {
4699 unsigned HOST_WIDE_INT isize;
4700 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4701 isize = TYPE_PRECISION (TREE_TYPE (@1));
4702 else
4703 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4704 }
4705 (switch
e3d0f65c 4706 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4707 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4708 wi::to_wide (@ipos) + isize))
a07b1b15 4709 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
e3d0f65c 4710 wi::to_wide (@rpos)
4711 - wi::to_wide (@ipos)); }))
4712 (if (wi::geu_p (wi::to_wide (@ipos),
4713 wi::to_wide (@rpos) + wi::to_wide (@rsize))
4714 || wi::geu_p (wi::to_wide (@rpos),
4715 wi::to_wide (@ipos) + isize))
a07b1b15 4716 (BIT_FIELD_REF @0 @rsize @rpos)))))
143c3c9a 4717
bdcde03a 4718(if (canonicalize_math_after_vectorization_p ())
4719 (for fmas (FMA)
4720 (simplify
4721 (fmas:c (negate @0) @1 @2)
4722 (IFN_FNMA @0 @1 @2))
4723 (simplify
4724 (fmas @0 @1 (negate @2))
4725 (IFN_FMS @0 @1 @2))
4726 (simplify
4727 (fmas:c (negate @0) @1 (negate @2))
4728 (IFN_FNMS @0 @1 @2))
4729 (simplify
4730 (negate (fmas@3 @0 @1 @2))
4731 (if (single_use (@3))
4732 (IFN_FNMS @0 @1 @2))))
4733
143c3c9a 4734 (simplify
bdcde03a 4735 (IFN_FMS:c (negate @0) @1 @2)
4736 (IFN_FNMS @0 @1 @2))
4737 (simplify
4738 (IFN_FMS @0 @1 (negate @2))
4739 (IFN_FMA @0 @1 @2))
4740 (simplify
4741 (IFN_FMS:c (negate @0) @1 (negate @2))
143c3c9a 4742 (IFN_FNMA @0 @1 @2))
4743 (simplify
bdcde03a 4744 (negate (IFN_FMS@3 @0 @1 @2))
4745 (if (single_use (@3))
4746 (IFN_FNMA @0 @1 @2)))
4747
4748 (simplify
4749 (IFN_FNMA:c (negate @0) @1 @2)
4750 (IFN_FMA @0 @1 @2))
143c3c9a 4751 (simplify
bdcde03a 4752 (IFN_FNMA @0 @1 (negate @2))
143c3c9a 4753 (IFN_FNMS @0 @1 @2))
4754 (simplify
bdcde03a 4755 (IFN_FNMA:c (negate @0) @1 (negate @2))
4756 (IFN_FMS @0 @1 @2))
4757 (simplify
4758 (negate (IFN_FNMA@3 @0 @1 @2))
143c3c9a 4759 (if (single_use (@3))
bdcde03a 4760 (IFN_FMS @0 @1 @2)))
143c3c9a 4761
bdcde03a 4762 (simplify
4763 (IFN_FNMS:c (negate @0) @1 @2)
4764 (IFN_FMS @0 @1 @2))
4765 (simplify
4766 (IFN_FNMS @0 @1 (negate @2))
4767 (IFN_FNMA @0 @1 @2))
4768 (simplify
4769 (IFN_FNMS:c (negate @0) @1 (negate @2))
4770 (IFN_FMA @0 @1 @2))
4771 (simplify
4772 (negate (IFN_FNMS@3 @0 @1 @2))
143c3c9a 4773 (if (single_use (@3))
bdcde03a 4774 (IFN_FMA @0 @1 @2))))
d1f6ca14 4775
4776/* POPCOUNT simplifications. */
4777(for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
4778 BUILT_IN_POPCOUNTIMAX)
4779 /* popcount(X&1) is nop_expr(X&1). */
4780 (simplify
4781 (popcount @0)
4782 (if (tree_nonzero_bits (@0) == 1)
4783 (convert @0)))
4784 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
4785 (simplify
4786 (plus (popcount:s @0) (popcount:s @1))
4787 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
4788 (popcount (bit_ior @0 @1))))
4789 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
4790 (for cmp (le eq ne gt)
4791 rep (eq eq ne ne)
4792 (simplify
4793 (cmp (popcount @0) integer_zerop)
4794 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
dbc7e6ae 4795
4796/* Simplify:
4797
4798 a = a1 op a2
4799 r = c ? a : b;
4800
4801 to:
4802
4803 r = c ? a1 op a2 : b;
4804
4805 if the target can do it in one go. This makes the operation conditional
4806 on c, so could drop potentially-trapping arithmetic, but that's a valid
4807 simplification if the result of the operation isn't needed. */
4808(for uncond_op (UNCOND_BINARY)
4809 cond_op (COND_BINARY)
4810 (simplify
4811 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
4812 (with { tree op_type = TREE_TYPE (@4); }
4813 (if (element_precision (type) == element_precision (op_type))
4814 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
4815 (simplify
4816 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
4817 (with { tree op_type = TREE_TYPE (@4); }
4818 (if (element_precision (type) == element_precision (op_type))
4819 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))