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