compiler: add support for reading embedcfg files
[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-2021 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 initializer_each_zero_or_onep
33 CONSTANT_CLASS_P
34 tree_expr_nonnegative_p
35 tree_expr_nonzero_p
36 integer_valued_real_p
37 integer_pow2p
38 uniform_integer_cst_p
39 HONOR_NANS
40 uniform_vector_p)
41
42 /* Operator lists. */
43 (define_operator_list tcc_comparison
44 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
45 (define_operator_list inverted_tcc_comparison
46 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
47 (define_operator_list inverted_tcc_comparison_with_nans
48 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list swapped_tcc_comparison
50 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
51 (define_operator_list simple_comparison lt le eq ne ge gt)
52 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
53
54 #include "cfn-operators.pd"
55
56 /* Define operand lists for math rounding functions {,i,l,ll}FN,
57 where the versions prefixed with "i" return an int, those prefixed with
58 "l" return a long and those prefixed with "ll" return a long long.
59
60 Also define operand lists:
61
62 X<FN>F for all float functions, in the order i, l, ll
63 X<FN> for all double functions, in the same order
64 X<FN>L for all long double functions, in the same order. */
65 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
66 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
67 BUILT_IN_L##FN##F \
68 BUILT_IN_LL##FN##F) \
69 (define_operator_list X##FN BUILT_IN_I##FN \
70 BUILT_IN_L##FN \
71 BUILT_IN_LL##FN) \
72 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
73 BUILT_IN_L##FN##L \
74 BUILT_IN_LL##FN##L)
75
76 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
80
81 /* Binary operations and their associated IFN_COND_* function. */
82 (define_operator_list UNCOND_BINARY
83 plus minus
84 mult trunc_div trunc_mod rdiv
85 min max
86 bit_and bit_ior bit_xor
87 lshift rshift)
88 (define_operator_list COND_BINARY
89 IFN_COND_ADD IFN_COND_SUB
90 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
91 IFN_COND_MIN IFN_COND_MAX
92 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
93 IFN_COND_SHL IFN_COND_SHR)
94
95 /* Same for ternary operations. */
96 (define_operator_list UNCOND_TERNARY
97 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
98 (define_operator_list COND_TERNARY
99 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
100
101 /* With nop_convert? combine convert? and view_convert? in one pattern
102 plus conditionalize on tree_nop_conversion_p conversions. */
103 (match (nop_convert @0)
104 (convert @0)
105 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
106 (match (nop_convert @0)
107 (view_convert @0)
108 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
109 && known_eq (TYPE_VECTOR_SUBPARTS (type),
110 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
111 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
112
113 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
114 ABSU_EXPR returns unsigned absolute value of the operand and the operand
115 of the ABSU_EXPR will have the corresponding signed type. */
116 (simplify (abs (convert @0))
117 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
118 && !TYPE_UNSIGNED (TREE_TYPE (@0))
119 && element_precision (type) > element_precision (TREE_TYPE (@0)))
120 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
121 (convert (absu:utype @0)))))
122
123 #if GIMPLE
124 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
125 (simplify
126 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
127 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
128 && !TYPE_UNSIGNED (TREE_TYPE (@0))
129 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
130 (abs @0)))
131 #endif
132
133 /* Simplifications of operations with one constant operand and
134 simplifications to constants or single values. */
135
136 (for op (plus pointer_plus minus bit_ior bit_xor)
137 (simplify
138 (op @0 integer_zerop)
139 (non_lvalue @0)))
140
141 /* 0 +p index -> (type)index */
142 (simplify
143 (pointer_plus integer_zerop @1)
144 (non_lvalue (convert @1)))
145
146 /* ptr - 0 -> (type)ptr */
147 (simplify
148 (pointer_diff @0 integer_zerop)
149 (convert @0))
150
151 /* See if ARG1 is zero and X + ARG1 reduces to X.
152 Likewise if the operands are reversed. */
153 (simplify
154 (plus:c @0 real_zerop@1)
155 (if (fold_real_zero_addition_p (type, @1, 0))
156 (non_lvalue @0)))
157
158 /* See if ARG1 is zero and X - ARG1 reduces to X. */
159 (simplify
160 (minus @0 real_zerop@1)
161 (if (fold_real_zero_addition_p (type, @1, 1))
162 (non_lvalue @0)))
163
164 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
165 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
166 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
167 if not -frounding-math. For sNaNs the first operation would raise
168 exceptions but turn the result into qNan, so the second operation
169 would not raise it. */
170 (for inner_op (plus minus)
171 (for outer_op (plus minus)
172 (simplify
173 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
174 (if (real_zerop (@1)
175 && real_zerop (@2)
176 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
177 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
178 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
179 bool outer_plus
180 = ((outer_op == PLUS_EXPR)
181 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
182 (if (outer_plus && !inner_plus)
183 (outer_op @0 @2)
184 @3))))))
185
186 /* Simplify x - x.
187 This is unsafe for certain floats even in non-IEEE formats.
188 In IEEE, it is unsafe because it does wrong for NaNs.
189 Also note that operand_equal_p is always false if an operand
190 is volatile. */
191 (simplify
192 (minus @0 @0)
193 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
194 { build_zero_cst (type); }))
195 (simplify
196 (pointer_diff @@0 @0)
197 { build_zero_cst (type); })
198
199 (simplify
200 (mult @0 integer_zerop@1)
201 @1)
202
203 /* Maybe fold x * 0 to 0. The expressions aren't the same
204 when x is NaN, since x * 0 is also NaN. Nor are they the
205 same in modes with signed zeros, since multiplying a
206 negative value by 0 gives -0, not +0. */
207 (simplify
208 (mult @0 real_zerop@1)
209 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
210 @1))
211
212 /* In IEEE floating point, x*1 is not equivalent to x for snans.
213 Likewise for complex arithmetic with signed zeros. */
214 (simplify
215 (mult @0 real_onep)
216 (if (!HONOR_SNANS (type)
217 && (!HONOR_SIGNED_ZEROS (type)
218 || !COMPLEX_FLOAT_TYPE_P (type)))
219 (non_lvalue @0)))
220
221 /* Transform x * -1.0 into -x. */
222 (simplify
223 (mult @0 real_minus_onep)
224 (if (!HONOR_SNANS (type)
225 && (!HONOR_SIGNED_ZEROS (type)
226 || !COMPLEX_FLOAT_TYPE_P (type)))
227 (negate @0)))
228
229 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
230 (simplify
231 (mult SSA_NAME@1 SSA_NAME@2)
232 (if (INTEGRAL_TYPE_P (type)
233 && get_nonzero_bits (@1) == 1
234 && get_nonzero_bits (@2) == 1)
235 (bit_and @1 @2)))
236
237 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
238 unless the target has native support for the former but not the latter. */
239 (simplify
240 (mult @0 VECTOR_CST@1)
241 (if (initializer_each_zero_or_onep (@1)
242 && !HONOR_SNANS (type)
243 && !HONOR_SIGNED_ZEROS (type))
244 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
245 (if (itype
246 && (!VECTOR_MODE_P (TYPE_MODE (type))
247 || (VECTOR_MODE_P (TYPE_MODE (itype))
248 && optab_handler (and_optab,
249 TYPE_MODE (itype)) != CODE_FOR_nothing)))
250 (view_convert (bit_and:itype (view_convert @0)
251 (ne @1 { build_zero_cst (type); })))))))
252
253 (for cmp (gt ge lt le)
254 outp (convert convert negate negate)
255 outn (negate negate convert convert)
256 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
257 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
258 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
259 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
260 (simplify
261 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
262 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
263 && types_match (type, TREE_TYPE (@0)))
264 (switch
265 (if (types_match (type, float_type_node))
266 (BUILT_IN_COPYSIGNF @1 (outp @0)))
267 (if (types_match (type, double_type_node))
268 (BUILT_IN_COPYSIGN @1 (outp @0)))
269 (if (types_match (type, long_double_type_node))
270 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
271 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
272 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
273 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
274 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
275 (simplify
276 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
277 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
278 && types_match (type, TREE_TYPE (@0)))
279 (switch
280 (if (types_match (type, float_type_node))
281 (BUILT_IN_COPYSIGNF @1 (outn @0)))
282 (if (types_match (type, double_type_node))
283 (BUILT_IN_COPYSIGN @1 (outn @0)))
284 (if (types_match (type, long_double_type_node))
285 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
286
287 /* Transform X * copysign (1.0, X) into abs(X). */
288 (simplify
289 (mult:c @0 (COPYSIGN_ALL real_onep @0))
290 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
291 (abs @0)))
292
293 /* Transform X * copysign (1.0, -X) into -abs(X). */
294 (simplify
295 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
296 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
297 (negate (abs @0))))
298
299 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
300 (simplify
301 (COPYSIGN_ALL REAL_CST@0 @1)
302 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
303 (COPYSIGN_ALL (negate @0) @1)))
304
305 /* X * 1, X / 1 -> X. */
306 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
307 (simplify
308 (op @0 integer_onep)
309 (non_lvalue @0)))
310
311 /* (A / (1 << B)) -> (A >> B).
312 Only for unsigned A. For signed A, this would not preserve rounding
313 toward zero.
314 For example: (-1 / ( 1 << B)) != -1 >> B.
315 Also also widening conversions, like:
316 (A / (unsigned long long) (1U << B)) -> (A >> B)
317 or
318 (A / (unsigned long long) (1 << B)) -> (A >> B).
319 If the left shift is signed, it can be done only if the upper bits
320 of A starting from shift's type sign bit are zero, as
321 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
322 so it is valid only if A >> 31 is zero. */
323 (simplify
324 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
325 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
326 && (!VECTOR_TYPE_P (type)
327 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
328 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
329 && (useless_type_conversion_p (type, TREE_TYPE (@1))
330 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
331 && (TYPE_UNSIGNED (TREE_TYPE (@1))
332 || (element_precision (type)
333 == element_precision (TREE_TYPE (@1)))
334 || (INTEGRAL_TYPE_P (type)
335 && (tree_nonzero_bits (@0)
336 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
337 true,
338 element_precision (type))) == 0)))))
339 (if (!VECTOR_TYPE_P (type)
340 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
341 && element_precision (TREE_TYPE (@3)) < element_precision (type))
342 (convert (rshift @3 @2))
343 (rshift @0 @2))))
344
345 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
346 undefined behavior in constexpr evaluation, and assuming that the division
347 traps enables better optimizations than these anyway. */
348 (for div (trunc_div ceil_div floor_div round_div exact_div)
349 /* 0 / X is always zero. */
350 (simplify
351 (div integer_zerop@0 @1)
352 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
353 (if (!integer_zerop (@1))
354 @0))
355 /* X / -1 is -X. */
356 (simplify
357 (div @0 integer_minus_onep@1)
358 (if (!TYPE_UNSIGNED (type))
359 (negate @0)))
360 /* X / bool_range_Y is X. */
361 (simplify
362 (div @0 SSA_NAME@1)
363 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
364 @0))
365 /* X / X is one. */
366 (simplify
367 (div @0 @0)
368 /* But not for 0 / 0 so that we can get the proper warnings and errors.
369 And not for _Fract types where we can't build 1. */
370 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
371 { build_one_cst (type); }))
372 /* X / abs (X) is X < 0 ? -1 : 1. */
373 (simplify
374 (div:C @0 (abs @0))
375 (if (INTEGRAL_TYPE_P (type)
376 && TYPE_OVERFLOW_UNDEFINED (type))
377 (cond (lt @0 { build_zero_cst (type); })
378 { build_minus_one_cst (type); } { build_one_cst (type); })))
379 /* X / -X is -1. */
380 (simplify
381 (div:C @0 (negate @0))
382 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
383 && TYPE_OVERFLOW_UNDEFINED (type))
384 { build_minus_one_cst (type); })))
385
386 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
387 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
388 (simplify
389 (floor_div @0 @1)
390 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
391 && TYPE_UNSIGNED (type))
392 (trunc_div @0 @1)))
393
394 /* Combine two successive divisions. Note that combining ceil_div
395 and floor_div is trickier and combining round_div even more so. */
396 (for div (trunc_div exact_div)
397 (simplify
398 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
399 (with {
400 wi::overflow_type overflow;
401 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
402 TYPE_SIGN (type), &overflow);
403 }
404 (if (div == EXACT_DIV_EXPR
405 || optimize_successive_divisions_p (@2, @3))
406 (if (!overflow)
407 (div @0 { wide_int_to_tree (type, mul); })
408 (if (TYPE_UNSIGNED (type)
409 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
410 { build_zero_cst (type); }))))))
411
412 /* Combine successive multiplications. Similar to above, but handling
413 overflow is different. */
414 (simplify
415 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
416 (with {
417 wi::overflow_type overflow;
418 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
419 TYPE_SIGN (type), &overflow);
420 }
421 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
422 otherwise undefined overflow implies that @0 must be zero. */
423 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
424 (mult @0 { wide_int_to_tree (type, mul); }))))
425
426 /* Optimize A / A to 1.0 if we don't care about
427 NaNs or Infinities. */
428 (simplify
429 (rdiv @0 @0)
430 (if (FLOAT_TYPE_P (type)
431 && ! HONOR_NANS (type)
432 && ! HONOR_INFINITIES (type))
433 { build_one_cst (type); }))
434
435 /* Optimize -A / A to -1.0 if we don't care about
436 NaNs or Infinities. */
437 (simplify
438 (rdiv:C @0 (negate @0))
439 (if (FLOAT_TYPE_P (type)
440 && ! HONOR_NANS (type)
441 && ! HONOR_INFINITIES (type))
442 { build_minus_one_cst (type); }))
443
444 /* PR71078: x / abs(x) -> copysign (1.0, x) */
445 (simplify
446 (rdiv:C (convert? @0) (convert? (abs @0)))
447 (if (SCALAR_FLOAT_TYPE_P (type)
448 && ! HONOR_NANS (type)
449 && ! HONOR_INFINITIES (type))
450 (switch
451 (if (types_match (type, float_type_node))
452 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
453 (if (types_match (type, double_type_node))
454 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
455 (if (types_match (type, long_double_type_node))
456 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
457
458 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
459 (simplify
460 (rdiv @0 real_onep)
461 (if (!HONOR_SNANS (type))
462 (non_lvalue @0)))
463
464 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
465 (simplify
466 (rdiv @0 real_minus_onep)
467 (if (!HONOR_SNANS (type))
468 (negate @0)))
469
470 (if (flag_reciprocal_math)
471 /* Convert (A/B)/C to A/(B*C). */
472 (simplify
473 (rdiv (rdiv:s @0 @1) @2)
474 (rdiv @0 (mult @1 @2)))
475
476 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
477 (simplify
478 (rdiv @0 (mult:s @1 REAL_CST@2))
479 (with
480 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
481 (if (tem)
482 (rdiv (mult @0 { tem; } ) @1))))
483
484 /* Convert A/(B/C) to (A/B)*C */
485 (simplify
486 (rdiv @0 (rdiv:s @1 @2))
487 (mult (rdiv @0 @1) @2)))
488
489 /* Simplify x / (- y) to -x / y. */
490 (simplify
491 (rdiv @0 (negate @1))
492 (rdiv (negate @0) @1))
493
494 (if (flag_unsafe_math_optimizations)
495 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
496 Since C / x may underflow to zero, do this only for unsafe math. */
497 (for op (lt le gt ge)
498 neg_op (gt ge lt le)
499 (simplify
500 (op (rdiv REAL_CST@0 @1) real_zerop@2)
501 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
502 (switch
503 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
504 (op @1 @2))
505 /* For C < 0, use the inverted operator. */
506 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
507 (neg_op @1 @2)))))))
508
509 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
510 (for div (trunc_div ceil_div floor_div round_div exact_div)
511 (simplify
512 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
513 (if (integer_pow2p (@2)
514 && tree_int_cst_sgn (@2) > 0
515 && tree_nop_conversion_p (type, TREE_TYPE (@0))
516 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
517 (rshift (convert @0)
518 { build_int_cst (integer_type_node,
519 wi::exact_log2 (wi::to_wide (@2))); }))))
520
521 /* If ARG1 is a constant, we can convert this to a multiply by the
522 reciprocal. This does not have the same rounding properties,
523 so only do this if -freciprocal-math. We can actually
524 always safely do it if ARG1 is a power of two, but it's hard to
525 tell if it is or not in a portable manner. */
526 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
527 (simplify
528 (rdiv @0 cst@1)
529 (if (optimize)
530 (if (flag_reciprocal_math
531 && !real_zerop (@1))
532 (with
533 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
534 (if (tem)
535 (mult @0 { tem; } )))
536 (if (cst != COMPLEX_CST)
537 (with { tree inverse = exact_inverse (type, @1); }
538 (if (inverse)
539 (mult @0 { inverse; } ))))))))
540
541 (for mod (ceil_mod floor_mod round_mod trunc_mod)
542 /* 0 % X is always zero. */
543 (simplify
544 (mod integer_zerop@0 @1)
545 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
546 (if (!integer_zerop (@1))
547 @0))
548 /* X % 1 is always zero. */
549 (simplify
550 (mod @0 integer_onep)
551 { build_zero_cst (type); })
552 /* X % -1 is zero. */
553 (simplify
554 (mod @0 integer_minus_onep@1)
555 (if (!TYPE_UNSIGNED (type))
556 { build_zero_cst (type); }))
557 /* X % X is zero. */
558 (simplify
559 (mod @0 @0)
560 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
561 (if (!integer_zerop (@0))
562 { build_zero_cst (type); }))
563 /* (X % Y) % Y is just X % Y. */
564 (simplify
565 (mod (mod@2 @0 @1) @1)
566 @2)
567 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
568 (simplify
569 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
570 (if (ANY_INTEGRAL_TYPE_P (type)
571 && TYPE_OVERFLOW_UNDEFINED (type)
572 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
573 TYPE_SIGN (type)))
574 { build_zero_cst (type); }))
575 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
576 modulo and comparison, since it is simpler and equivalent. */
577 (for cmp (eq ne)
578 (simplify
579 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
580 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
581 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
582 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
583
584 /* X % -C is the same as X % C. */
585 (simplify
586 (trunc_mod @0 INTEGER_CST@1)
587 (if (TYPE_SIGN (type) == SIGNED
588 && !TREE_OVERFLOW (@1)
589 && wi::neg_p (wi::to_wide (@1))
590 && !TYPE_OVERFLOW_TRAPS (type)
591 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
592 && !sign_bit_p (@1, @1))
593 (trunc_mod @0 (negate @1))))
594
595 /* X % -Y is the same as X % Y. */
596 (simplify
597 (trunc_mod @0 (convert? (negate @1)))
598 (if (INTEGRAL_TYPE_P (type)
599 && !TYPE_UNSIGNED (type)
600 && !TYPE_OVERFLOW_TRAPS (type)
601 && tree_nop_conversion_p (type, TREE_TYPE (@1))
602 /* Avoid this transformation if X might be INT_MIN or
603 Y might be -1, because we would then change valid
604 INT_MIN % -(-1) into invalid INT_MIN % -1. */
605 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
606 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
607 (TREE_TYPE (@1))))))
608 (trunc_mod @0 (convert @1))))
609
610 /* X - (X / Y) * Y is the same as X % Y. */
611 (simplify
612 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
613 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
614 (convert (trunc_mod @0 @1))))
615
616 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
617 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
618 Also optimize A % (C << N) where C is a power of 2,
619 to A & ((C << N) - 1).
620 Also optimize "A shift (B % C)", if C is a power of 2, to
621 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
622 and assume (B % C) is nonnegative as shifts negative values would
623 be UB. */
624 (match (power_of_two_cand @1)
625 INTEGER_CST@1)
626 (match (power_of_two_cand @1)
627 (lshift INTEGER_CST@1 @2))
628 (for mod (trunc_mod floor_mod)
629 (for shift (lshift rshift)
630 (simplify
631 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
632 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
633 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
634 1); }))))))
635 (simplify
636 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
637 (if ((TYPE_UNSIGNED (type)
638 || tree_expr_nonnegative_p (@0))
639 && tree_nop_conversion_p (type, TREE_TYPE (@3))
640 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
641 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
642
643 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
644 (simplify
645 (trunc_div (mult @0 integer_pow2p@1) @1)
646 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
647 (bit_and @0 { wide_int_to_tree
648 (type, wi::mask (TYPE_PRECISION (type)
649 - wi::exact_log2 (wi::to_wide (@1)),
650 false, TYPE_PRECISION (type))); })))
651
652 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
653 (simplify
654 (mult (trunc_div @0 integer_pow2p@1) @1)
655 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
656 (bit_and @0 (negate @1))))
657
658 /* Simplify (t * 2) / 2) -> t. */
659 (for div (trunc_div ceil_div floor_div round_div exact_div)
660 (simplify
661 (div (mult:c @0 @1) @1)
662 (if (ANY_INTEGRAL_TYPE_P (type))
663 (if (TYPE_OVERFLOW_UNDEFINED (type))
664 @0
665 #if GIMPLE
666 (with
667 {
668 bool overflowed = true;
669 wide_int wmin0, wmax0, wmin1, wmax1;
670 if (INTEGRAL_TYPE_P (type)
671 && get_range_info (@0, &wmin0, &wmax0) == VR_RANGE
672 && get_range_info (@1, &wmin1, &wmax1) == VR_RANGE)
673 {
674 /* If the multiplication can't overflow/wrap around, then
675 it can be optimized too. */
676 wi::overflow_type min_ovf, max_ovf;
677 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
678 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
679 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
680 {
681 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
682 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
683 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
684 overflowed = false;
685 }
686 }
687 }
688 (if (!overflowed)
689 @0))
690 #endif
691 ))))
692
693 (for op (negate abs)
694 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
695 (for coss (COS COSH)
696 (simplify
697 (coss (op @0))
698 (coss @0)))
699 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
700 (for pows (POW)
701 (simplify
702 (pows (op @0) REAL_CST@1)
703 (with { HOST_WIDE_INT n; }
704 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
705 (pows @0 @1)))))
706 /* Likewise for powi. */
707 (for pows (POWI)
708 (simplify
709 (pows (op @0) INTEGER_CST@1)
710 (if ((wi::to_wide (@1) & 1) == 0)
711 (pows @0 @1))))
712 /* Strip negate and abs from both operands of hypot. */
713 (for hypots (HYPOT)
714 (simplify
715 (hypots (op @0) @1)
716 (hypots @0 @1))
717 (simplify
718 (hypots @0 (op @1))
719 (hypots @0 @1)))
720 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
721 (for copysigns (COPYSIGN_ALL)
722 (simplify
723 (copysigns (op @0) @1)
724 (copysigns @0 @1))))
725
726 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
727 (simplify
728 (mult (abs@1 @0) @1)
729 (mult @0 @0))
730
731 /* Convert absu(x)*absu(x) -> x*x. */
732 (simplify
733 (mult (absu@1 @0) @1)
734 (mult (convert@2 @0) @2))
735
736 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
737 (for coss (COS COSH)
738 copysigns (COPYSIGN)
739 (simplify
740 (coss (copysigns @0 @1))
741 (coss @0)))
742
743 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
744 (for pows (POW)
745 copysigns (COPYSIGN)
746 (simplify
747 (pows (copysigns @0 @2) REAL_CST@1)
748 (with { HOST_WIDE_INT n; }
749 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
750 (pows @0 @1)))))
751 /* Likewise for powi. */
752 (for pows (POWI)
753 copysigns (COPYSIGN)
754 (simplify
755 (pows (copysigns @0 @2) INTEGER_CST@1)
756 (if ((wi::to_wide (@1) & 1) == 0)
757 (pows @0 @1))))
758
759 (for hypots (HYPOT)
760 copysigns (COPYSIGN)
761 /* hypot(copysign(x, y), z) -> hypot(x, z). */
762 (simplify
763 (hypots (copysigns @0 @1) @2)
764 (hypots @0 @2))
765 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
766 (simplify
767 (hypots @0 (copysigns @1 @2))
768 (hypots @0 @1)))
769
770 /* copysign(x, CST) -> [-]abs (x). */
771 (for copysigns (COPYSIGN_ALL)
772 (simplify
773 (copysigns @0 REAL_CST@1)
774 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
775 (negate (abs @0))
776 (abs @0))))
777
778 /* copysign(copysign(x, y), z) -> copysign(x, z). */
779 (for copysigns (COPYSIGN_ALL)
780 (simplify
781 (copysigns (copysigns @0 @1) @2)
782 (copysigns @0 @2)))
783
784 /* copysign(x,y)*copysign(x,y) -> x*x. */
785 (for copysigns (COPYSIGN_ALL)
786 (simplify
787 (mult (copysigns@2 @0 @1) @2)
788 (mult @0 @0)))
789
790 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
791 (for ccoss (CCOS CCOSH)
792 (simplify
793 (ccoss (negate @0))
794 (ccoss @0)))
795
796 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
797 (for ops (conj negate)
798 (for cabss (CABS)
799 (simplify
800 (cabss (ops @0))
801 (cabss @0))))
802
803 /* Fold (a * (1 << b)) into (a << b) */
804 (simplify
805 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
806 (if (! FLOAT_TYPE_P (type)
807 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
808 (lshift @0 @2)))
809
810 /* Fold (1 << (C - x)) where C = precision(type) - 1
811 into ((1 << C) >> x). */
812 (simplify
813 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
814 (if (INTEGRAL_TYPE_P (type)
815 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
816 && single_use (@1))
817 (if (TYPE_UNSIGNED (type))
818 (rshift (lshift @0 @2) @3)
819 (with
820 { tree utype = unsigned_type_for (type); }
821 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
822
823 /* Fold (C1/X)*C2 into (C1*C2)/X. */
824 (simplify
825 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
826 (if (flag_associative_math
827 && single_use (@3))
828 (with
829 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
830 (if (tem)
831 (rdiv { tem; } @1)))))
832
833 /* Simplify ~X & X as zero. */
834 (simplify
835 (bit_and:c (convert? @0) (convert? (bit_not @0)))
836 { build_zero_cst (type); })
837
838 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
839 (simplify
840 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
841 (if (TYPE_UNSIGNED (type))
842 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
843
844 (for bitop (bit_and bit_ior)
845 cmp (eq ne)
846 /* PR35691: Transform
847 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
848 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
849 (simplify
850 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
851 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
852 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
853 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
854 (cmp (bit_ior @0 (convert @1)) @2)))
855 /* Transform:
856 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
857 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
858 (simplify
859 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
860 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
861 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
862 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
863 (cmp (bit_and @0 (convert @1)) @2))))
864
865 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
866 (simplify
867 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
868 (minus (bit_xor @0 @1) @1))
869 (simplify
870 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
871 (if (~wi::to_wide (@2) == wi::to_wide (@1))
872 (minus (bit_xor @0 @1) @1)))
873
874 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
875 (simplify
876 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
877 (minus @1 (bit_xor @0 @1)))
878
879 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
880 (for op (bit_ior bit_xor plus)
881 (simplify
882 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
883 (bit_xor @0 @1))
884 (simplify
885 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
886 (if (~wi::to_wide (@2) == wi::to_wide (@1))
887 (bit_xor @0 @1))))
888
889 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
890 (simplify
891 (bit_ior:c (bit_xor:c @0 @1) @0)
892 (bit_ior @0 @1))
893
894 /* (a & ~b) | (a ^ b) --> a ^ b */
895 (simplify
896 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
897 @2)
898
899 /* (a & ~b) ^ ~a --> ~(a & b) */
900 (simplify
901 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
902 (bit_not (bit_and @0 @1)))
903
904 /* (~a & b) ^ a --> (a | b) */
905 (simplify
906 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
907 (bit_ior @0 @1))
908
909 /* (a | b) & ~(a ^ b) --> a & b */
910 (simplify
911 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
912 (bit_and @0 @1))
913
914 /* a | ~(a ^ b) --> a | ~b */
915 (simplify
916 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
917 (bit_ior @0 (bit_not @1)))
918
919 /* (a | b) | (a &^ b) --> a | b */
920 (for op (bit_and bit_xor)
921 (simplify
922 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
923 @2))
924
925 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
926 (simplify
927 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
928 @2)
929
930 /* ~(~a & b) --> a | ~b */
931 (simplify
932 (bit_not (bit_and:cs (bit_not @0) @1))
933 (bit_ior @0 (bit_not @1)))
934
935 /* ~(~a | b) --> a & ~b */
936 (simplify
937 (bit_not (bit_ior:cs (bit_not @0) @1))
938 (bit_and @0 (bit_not @1)))
939
940 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
941 (simplify
942 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
943 (bit_and @3 (bit_not @2)))
944
945 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
946 (simplify
947 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
948 (bit_ior @3 @2))
949
950 #if GIMPLE
951 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
952 (simplify
953 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
954 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
955
956 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
957 (simplify
958 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
959 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
960
961 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
962 (simplify
963 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
964 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
965 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
966 (bit_xor @0 @1)))
967 #endif
968
969 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
970 ((A & N) + B) & M -> (A + B) & M
971 Similarly if (N & M) == 0,
972 ((A | N) + B) & M -> (A + B) & M
973 and for - instead of + (or unary - instead of +)
974 and/or ^ instead of |.
975 If B is constant and (B & M) == 0, fold into A & M. */
976 (for op (plus minus)
977 (for bitop (bit_and bit_ior bit_xor)
978 (simplify
979 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
980 (with
981 { tree pmop[2];
982 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
983 @3, @4, @1, ERROR_MARK, NULL_TREE,
984 NULL_TREE, pmop); }
985 (if (utype)
986 (convert (bit_and (op (convert:utype { pmop[0]; })
987 (convert:utype { pmop[1]; }))
988 (convert:utype @2))))))
989 (simplify
990 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
991 (with
992 { tree pmop[2];
993 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
994 NULL_TREE, NULL_TREE, @1, bitop, @3,
995 @4, pmop); }
996 (if (utype)
997 (convert (bit_and (op (convert:utype { pmop[0]; })
998 (convert:utype { pmop[1]; }))
999 (convert:utype @2)))))))
1000 (simplify
1001 (bit_and (op:s @0 @1) INTEGER_CST@2)
1002 (with
1003 { tree pmop[2];
1004 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1005 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1006 NULL_TREE, NULL_TREE, pmop); }
1007 (if (utype)
1008 (convert (bit_and (op (convert:utype { pmop[0]; })
1009 (convert:utype { pmop[1]; }))
1010 (convert:utype @2)))))))
1011 (for bitop (bit_and bit_ior bit_xor)
1012 (simplify
1013 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1014 (with
1015 { tree pmop[2];
1016 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1017 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1018 NULL_TREE, NULL_TREE, pmop); }
1019 (if (utype)
1020 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1021 (convert:utype @1)))))))
1022
1023 /* X % Y is smaller than Y. */
1024 (for cmp (lt ge)
1025 (simplify
1026 (cmp (trunc_mod @0 @1) @1)
1027 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1028 { constant_boolean_node (cmp == LT_EXPR, type); })))
1029 (for cmp (gt le)
1030 (simplify
1031 (cmp @1 (trunc_mod @0 @1))
1032 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1033 { constant_boolean_node (cmp == GT_EXPR, type); })))
1034
1035 /* x | ~0 -> ~0 */
1036 (simplify
1037 (bit_ior @0 integer_all_onesp@1)
1038 @1)
1039
1040 /* x | 0 -> x */
1041 (simplify
1042 (bit_ior @0 integer_zerop)
1043 @0)
1044
1045 /* x & 0 -> 0 */
1046 (simplify
1047 (bit_and @0 integer_zerop@1)
1048 @1)
1049
1050 /* ~x | x -> -1 */
1051 /* ~x ^ x -> -1 */
1052 /* ~x + x -> -1 */
1053 (for op (bit_ior bit_xor plus)
1054 (simplify
1055 (op:c (convert? @0) (convert? (bit_not @0)))
1056 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1057
1058 /* x ^ x -> 0 */
1059 (simplify
1060 (bit_xor @0 @0)
1061 { build_zero_cst (type); })
1062
1063 /* Canonicalize X ^ ~0 to ~X. */
1064 (simplify
1065 (bit_xor @0 integer_all_onesp@1)
1066 (bit_not @0))
1067
1068 /* x & ~0 -> x */
1069 (simplify
1070 (bit_and @0 integer_all_onesp)
1071 (non_lvalue @0))
1072
1073 /* x & x -> x, x | x -> x */
1074 (for bitop (bit_and bit_ior)
1075 (simplify
1076 (bitop @0 @0)
1077 (non_lvalue @0)))
1078
1079 /* x & C -> x if we know that x & ~C == 0. */
1080 #if GIMPLE
1081 (simplify
1082 (bit_and SSA_NAME@0 INTEGER_CST@1)
1083 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1084 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1085 @0))
1086 #endif
1087
1088 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1089 (simplify
1090 (bit_not (minus (bit_not @0) @1))
1091 (plus @0 @1))
1092 (simplify
1093 (bit_not (plus:c (bit_not @0) @1))
1094 (minus @0 @1))
1095
1096 /* ~(X - Y) -> ~X + Y. */
1097 (simplify
1098 (bit_not (minus:s @0 @1))
1099 (plus (bit_not @0) @1))
1100 (simplify
1101 (bit_not (plus:s @0 INTEGER_CST@1))
1102 (if ((INTEGRAL_TYPE_P (type)
1103 && TYPE_UNSIGNED (type))
1104 || (!TYPE_OVERFLOW_SANITIZED (type)
1105 && may_negate_without_overflow_p (@1)))
1106 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1107
1108 #if GIMPLE
1109 /* ~X + Y -> (Y - X) - 1. */
1110 (simplify
1111 (plus:c (bit_not @0) @1)
1112 (if (ANY_INTEGRAL_TYPE_P (type)
1113 && TYPE_OVERFLOW_WRAPS (type)
1114 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1115 && !integer_all_onesp (@1))
1116 (plus (minus @1 @0) { build_minus_one_cst (type); })
1117 (if (INTEGRAL_TYPE_P (type)
1118 && TREE_CODE (@1) == INTEGER_CST
1119 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1120 SIGNED))
1121 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1122
1123 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1124 (simplify
1125 (bit_not (rshift:s @0 @1))
1126 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1127 (rshift (bit_not! @0) @1)
1128 /* For logical right shifts, this is possible only if @0 doesn't
1129 have MSB set and the logical right shift is changed into
1130 arithmetic shift. */
1131 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1132 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1133 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1134 #endif
1135
1136 /* x + (x & 1) -> (x + 1) & ~1 */
1137 (simplify
1138 (plus:c @0 (bit_and:s @0 integer_onep@1))
1139 (bit_and (plus @0 @1) (bit_not @1)))
1140
1141 /* x & ~(x & y) -> x & ~y */
1142 /* x | ~(x | y) -> x | ~y */
1143 (for bitop (bit_and bit_ior)
1144 (simplify
1145 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1146 (bitop @0 (bit_not @1))))
1147
1148 /* (~x & y) | ~(x | y) -> ~x */
1149 (simplify
1150 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1151 @2)
1152
1153 /* (x | y) ^ (x | ~y) -> ~x */
1154 (simplify
1155 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1156 (bit_not @0))
1157
1158 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1159 (simplify
1160 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1161 (bit_not (bit_xor @0 @1)))
1162
1163 /* (~x | y) ^ (x ^ y) -> x | ~y */
1164 (simplify
1165 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1166 (bit_ior @0 (bit_not @1)))
1167
1168 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1169 (simplify
1170 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1171 (bit_not (bit_and @0 @1)))
1172
1173 /* (x | y) & ~x -> y & ~x */
1174 /* (x & y) | ~x -> y | ~x */
1175 (for bitop (bit_and bit_ior)
1176 rbitop (bit_ior bit_and)
1177 (simplify
1178 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1179 (bitop @1 @2)))
1180
1181 /* (x & y) ^ (x | y) -> x ^ y */
1182 (simplify
1183 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1184 (bit_xor @0 @1))
1185
1186 /* (x ^ y) ^ (x | y) -> x & y */
1187 (simplify
1188 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1189 (bit_and @0 @1))
1190
1191 /* (x & y) + (x ^ y) -> x | y */
1192 /* (x & y) | (x ^ y) -> x | y */
1193 /* (x & y) ^ (x ^ y) -> x | y */
1194 (for op (plus bit_ior bit_xor)
1195 (simplify
1196 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1197 (bit_ior @0 @1)))
1198
1199 /* (x & y) + (x | y) -> x + y */
1200 (simplify
1201 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1202 (plus @0 @1))
1203
1204 /* (x + y) - (x | y) -> x & y */
1205 (simplify
1206 (minus (plus @0 @1) (bit_ior @0 @1))
1207 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1208 && !TYPE_SATURATING (type))
1209 (bit_and @0 @1)))
1210
1211 /* (x + y) - (x & y) -> x | y */
1212 (simplify
1213 (minus (plus @0 @1) (bit_and @0 @1))
1214 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1215 && !TYPE_SATURATING (type))
1216 (bit_ior @0 @1)))
1217
1218 /* (x | y) - y -> (x & ~y) */
1219 (simplify
1220 (minus (bit_ior:cs @0 @1) @1)
1221 (bit_and @0 (bit_not @1)))
1222
1223 /* (x | y) - (x ^ y) -> x & y */
1224 (simplify
1225 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1226 (bit_and @0 @1))
1227
1228 /* (x | y) - (x & y) -> x ^ y */
1229 (simplify
1230 (minus (bit_ior @0 @1) (bit_and @0 @1))
1231 (bit_xor @0 @1))
1232
1233 /* (x | y) & ~(x & y) -> x ^ y */
1234 (simplify
1235 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1236 (bit_xor @0 @1))
1237
1238 /* (x | y) & (~x ^ y) -> x & y */
1239 (simplify
1240 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1241 (bit_and @0 @1))
1242
1243 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1244 (simplify
1245 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1246 (bit_not (bit_xor @0 @1)))
1247
1248 /* (~x | y) ^ (x | ~y) -> x ^ y */
1249 (simplify
1250 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1251 (bit_xor @0 @1))
1252
1253 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1254 (simplify
1255 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1256 (nop_convert2? (bit_ior @0 @1))))
1257 integer_all_onesp)
1258 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1259 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1260 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1261 && !TYPE_SATURATING (TREE_TYPE (@2)))
1262 (bit_not (convert (bit_xor @0 @1)))))
1263 (simplify
1264 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1265 integer_all_onesp))
1266 (nop_convert3? (bit_ior @0 @1)))
1267 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1268 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1269 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1270 && !TYPE_SATURATING (TREE_TYPE (@2)))
1271 (bit_not (convert (bit_xor @0 @1)))))
1272 (simplify
1273 (minus (nop_convert1? (bit_and @0 @1))
1274 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1275 integer_onep)))
1276 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1277 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1278 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1279 && !TYPE_SATURATING (TREE_TYPE (@2)))
1280 (bit_not (convert (bit_xor @0 @1)))))
1281
1282 /* ~x & ~y -> ~(x | y)
1283 ~x | ~y -> ~(x & y) */
1284 (for op (bit_and bit_ior)
1285 rop (bit_ior bit_and)
1286 (simplify
1287 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1288 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1289 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1290 (bit_not (rop (convert @0) (convert @1))))))
1291
1292 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1293 with a constant, and the two constants have no bits in common,
1294 we should treat this as a BIT_IOR_EXPR since this may produce more
1295 simplifications. */
1296 (for op (bit_xor plus)
1297 (simplify
1298 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1299 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1300 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1301 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1302 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1303 (bit_ior (convert @4) (convert @5)))))
1304
1305 /* (X | Y) ^ X -> Y & ~ X*/
1306 (simplify
1307 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1308 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1309 (convert (bit_and @1 (bit_not @0)))))
1310
1311 /* Convert ~X ^ ~Y to X ^ Y. */
1312 (simplify
1313 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1314 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1315 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1316 (bit_xor (convert @0) (convert @1))))
1317
1318 /* Convert ~X ^ C to X ^ ~C. */
1319 (simplify
1320 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1321 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1322 (bit_xor (convert @0) (bit_not @1))))
1323
1324 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1325 (for opo (bit_and bit_xor)
1326 opi (bit_xor bit_and)
1327 (simplify
1328 (opo:c (opi:cs @0 @1) @1)
1329 (bit_and (bit_not @0) @1)))
1330
1331 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1332 operands are another bit-wise operation with a common input. If so,
1333 distribute the bit operations to save an operation and possibly two if
1334 constants are involved. For example, convert
1335 (A | B) & (A | C) into A | (B & C)
1336 Further simplification will occur if B and C are constants. */
1337 (for op (bit_and bit_ior bit_xor)
1338 rop (bit_ior bit_and bit_and)
1339 (simplify
1340 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1341 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1342 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1343 (rop (convert @0) (op (convert @1) (convert @2))))))
1344
1345 /* Some simple reassociation for bit operations, also handled in reassoc. */
1346 /* (X & Y) & Y -> X & Y
1347 (X | Y) | Y -> X | Y */
1348 (for op (bit_and bit_ior)
1349 (simplify
1350 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1351 @2))
1352 /* (X ^ Y) ^ Y -> X */
1353 (simplify
1354 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1355 (convert @0))
1356 /* (X & Y) & (X & Z) -> (X & Y) & Z
1357 (X | Y) | (X | Z) -> (X | Y) | Z */
1358 (for op (bit_and bit_ior)
1359 (simplify
1360 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1361 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1362 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1363 (if (single_use (@5) && single_use (@6))
1364 (op @3 (convert @2))
1365 (if (single_use (@3) && single_use (@4))
1366 (op (convert @1) @5))))))
1367 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1368 (simplify
1369 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1370 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1371 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1372 (bit_xor (convert @1) (convert @2))))
1373
1374 /* Convert abs (abs (X)) into abs (X).
1375 also absu (absu (X)) into absu (X). */
1376 (simplify
1377 (abs (abs@1 @0))
1378 @1)
1379
1380 (simplify
1381 (absu (convert@2 (absu@1 @0)))
1382 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1383 @1))
1384
1385 /* Convert abs[u] (-X) -> abs[u] (X). */
1386 (simplify
1387 (abs (negate @0))
1388 (abs @0))
1389
1390 (simplify
1391 (absu (negate @0))
1392 (absu @0))
1393
1394 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1395 (simplify
1396 (abs tree_expr_nonnegative_p@0)
1397 @0)
1398
1399 (simplify
1400 (absu tree_expr_nonnegative_p@0)
1401 (convert @0))
1402
1403 /* Simplify (-(X < 0) | 1) * X into abs (X). */
1404 (simplify
1405 (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
1406 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
1407 (abs @0)))
1408
1409 /* Similarly (-(X < 0) | 1U) * X into absu (X). */
1410 (simplify
1411 (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
1412 integer_onep) (nop_convert @0))
1413 (if (INTEGRAL_TYPE_P (type)
1414 && TYPE_UNSIGNED (type)
1415 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1416 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1417 (absu @0)))
1418
1419 /* A few cases of fold-const.c negate_expr_p predicate. */
1420 (match negate_expr_p
1421 INTEGER_CST
1422 (if ((INTEGRAL_TYPE_P (type)
1423 && TYPE_UNSIGNED (type))
1424 || (!TYPE_OVERFLOW_SANITIZED (type)
1425 && may_negate_without_overflow_p (t)))))
1426 (match negate_expr_p
1427 FIXED_CST)
1428 (match negate_expr_p
1429 (negate @0)
1430 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1431 (match negate_expr_p
1432 REAL_CST
1433 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1434 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1435 ways. */
1436 (match negate_expr_p
1437 VECTOR_CST
1438 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1439 (match negate_expr_p
1440 (minus @0 @1)
1441 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1442 || (FLOAT_TYPE_P (type)
1443 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1444 && !HONOR_SIGNED_ZEROS (type)))))
1445
1446 /* (-A) * (-B) -> A * B */
1447 (simplify
1448 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1449 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1450 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1451 (mult (convert @0) (convert (negate @1)))))
1452
1453 /* -(A + B) -> (-B) - A. */
1454 (simplify
1455 (negate (plus:c @0 negate_expr_p@1))
1456 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1457 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1458 (minus (negate @1) @0)))
1459
1460 /* -(A - B) -> B - A. */
1461 (simplify
1462 (negate (minus @0 @1))
1463 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1464 || (FLOAT_TYPE_P (type)
1465 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1466 && !HONOR_SIGNED_ZEROS (type)))
1467 (minus @1 @0)))
1468 (simplify
1469 (negate (pointer_diff @0 @1))
1470 (if (TYPE_OVERFLOW_UNDEFINED (type))
1471 (pointer_diff @1 @0)))
1472
1473 /* A - B -> A + (-B) if B is easily negatable. */
1474 (simplify
1475 (minus @0 negate_expr_p@1)
1476 (if (!FIXED_POINT_TYPE_P (type))
1477 (plus @0 (negate @1))))
1478
1479 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1480 when profitable.
1481 For bitwise binary operations apply operand conversions to the
1482 binary operation result instead of to the operands. This allows
1483 to combine successive conversions and bitwise binary operations.
1484 We combine the above two cases by using a conditional convert. */
1485 (for bitop (bit_and bit_ior bit_xor)
1486 (simplify
1487 (bitop (convert@2 @0) (convert?@3 @1))
1488 (if (((TREE_CODE (@1) == INTEGER_CST
1489 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1490 && int_fits_type_p (@1, TREE_TYPE (@0)))
1491 || types_match (@0, @1))
1492 /* ??? This transform conflicts with fold-const.c doing
1493 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1494 constants (if x has signed type, the sign bit cannot be set
1495 in c). This folds extension into the BIT_AND_EXPR.
1496 Restrict it to GIMPLE to avoid endless recursions. */
1497 && (bitop != BIT_AND_EXPR || GIMPLE)
1498 && (/* That's a good idea if the conversion widens the operand, thus
1499 after hoisting the conversion the operation will be narrower. */
1500 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1501 /* It's also a good idea if the conversion is to a non-integer
1502 mode. */
1503 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1504 /* Or if the precision of TO is not the same as the precision
1505 of its mode. */
1506 || !type_has_mode_precision_p (type)
1507 /* In GIMPLE, getting rid of 2 conversions for one new results
1508 in smaller IL. */
1509 || (GIMPLE
1510 && TREE_CODE (@1) != INTEGER_CST
1511 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1512 && single_use (@2)
1513 && single_use (@3))))
1514 (convert (bitop @0 (convert @1)))))
1515 /* In GIMPLE, getting rid of 2 conversions for one new results
1516 in smaller IL. */
1517 (simplify
1518 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1519 (if (GIMPLE
1520 && TREE_CODE (@1) != INTEGER_CST
1521 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1522 && types_match (type, @0))
1523 (bitop @0 (convert @1)))))
1524
1525 (for bitop (bit_and bit_ior)
1526 rbitop (bit_ior bit_and)
1527 /* (x | y) & x -> x */
1528 /* (x & y) | x -> x */
1529 (simplify
1530 (bitop:c (rbitop:c @0 @1) @0)
1531 @0)
1532 /* (~x | y) & x -> x & y */
1533 /* (~x & y) | x -> x | y */
1534 (simplify
1535 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1536 (bitop @0 @1)))
1537
1538 /* ((x | y) & z) | x -> (z & y) | x */
1539 (simplify
1540 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1541 (bit_ior (bit_and @2 @1) @0))
1542
1543 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1544 (simplify
1545 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1546 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1547
1548 /* Combine successive equal operations with constants. */
1549 (for bitop (bit_and bit_ior bit_xor)
1550 (simplify
1551 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1552 (if (!CONSTANT_CLASS_P (@0))
1553 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1554 folded to a constant. */
1555 (bitop @0 (bitop @1 @2))
1556 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1557 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1558 the values involved are such that the operation can't be decided at
1559 compile time. Try folding one of @0 or @1 with @2 to see whether
1560 that combination can be decided at compile time.
1561
1562 Keep the existing form if both folds fail, to avoid endless
1563 oscillation. */
1564 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1565 (if (cst1)
1566 (bitop @1 { cst1; })
1567 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1568 (if (cst2)
1569 (bitop @0 { cst2; }))))))))
1570
1571 /* Try simple folding for X op !X, and X op X with the help
1572 of the truth_valued_p and logical_inverted_value predicates. */
1573 (match truth_valued_p
1574 @0
1575 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1576 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1577 (match truth_valued_p
1578 (op @0 @1)))
1579 (match truth_valued_p
1580 (truth_not @0))
1581
1582 (match (logical_inverted_value @0)
1583 (truth_not @0))
1584 (match (logical_inverted_value @0)
1585 (bit_not truth_valued_p@0))
1586 (match (logical_inverted_value @0)
1587 (eq @0 integer_zerop))
1588 (match (logical_inverted_value @0)
1589 (ne truth_valued_p@0 integer_truep))
1590 (match (logical_inverted_value @0)
1591 (bit_xor truth_valued_p@0 integer_truep))
1592
1593 /* X & !X -> 0. */
1594 (simplify
1595 (bit_and:c @0 (logical_inverted_value @0))
1596 { build_zero_cst (type); })
1597 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1598 (for op (bit_ior bit_xor)
1599 (simplify
1600 (op:c truth_valued_p@0 (logical_inverted_value @0))
1601 { constant_boolean_node (true, type); }))
1602 /* X ==/!= !X is false/true. */
1603 (for op (eq ne)
1604 (simplify
1605 (op:c truth_valued_p@0 (logical_inverted_value @0))
1606 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1607
1608 /* ~~x -> x */
1609 (simplify
1610 (bit_not (bit_not @0))
1611 @0)
1612
1613 /* Convert ~ (-A) to A - 1. */
1614 (simplify
1615 (bit_not (convert? (negate @0)))
1616 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1617 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1618 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1619
1620 /* Convert - (~A) to A + 1. */
1621 (simplify
1622 (negate (nop_convert? (bit_not @0)))
1623 (plus (view_convert @0) { build_each_one_cst (type); }))
1624
1625 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1626 (simplify
1627 (bit_not (convert? (minus @0 integer_each_onep)))
1628 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1629 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1630 (convert (negate @0))))
1631 (simplify
1632 (bit_not (convert? (plus @0 integer_all_onesp)))
1633 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1634 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1635 (convert (negate @0))))
1636
1637 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1638 (simplify
1639 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1640 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1641 (convert (bit_xor @0 (bit_not @1)))))
1642 (simplify
1643 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1644 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1645 (convert (bit_xor @0 @1))))
1646
1647 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1648 (simplify
1649 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1650 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1651 (bit_not (bit_xor (view_convert @0) @1))))
1652
1653 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1654 (simplify
1655 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1656 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1657
1658 /* Fold A - (A & B) into ~B & A. */
1659 (simplify
1660 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1661 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1662 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1663 (convert (bit_and (bit_not @1) @0))))
1664
1665 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1666 (for cmp (gt lt ge le)
1667 (simplify
1668 (mult (convert (cmp @0 @1)) @2)
1669 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1670 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1671
1672 /* For integral types with undefined overflow and C != 0 fold
1673 x * C EQ/NE y * C into x EQ/NE y. */
1674 (for cmp (eq ne)
1675 (simplify
1676 (cmp (mult:c @0 @1) (mult:c @2 @1))
1677 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1678 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1679 && tree_expr_nonzero_p (@1))
1680 (cmp @0 @2))))
1681
1682 /* For integral types with wrapping overflow and C odd fold
1683 x * C EQ/NE y * C into x EQ/NE y. */
1684 (for cmp (eq ne)
1685 (simplify
1686 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1687 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1688 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1689 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1690 (cmp @0 @2))))
1691
1692 /* For integral types with undefined overflow and C != 0 fold
1693 x * C RELOP y * C into:
1694
1695 x RELOP y for nonnegative C
1696 y RELOP x for negative C */
1697 (for cmp (lt gt le ge)
1698 (simplify
1699 (cmp (mult:c @0 @1) (mult:c @2 @1))
1700 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1701 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1702 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1703 (cmp @0 @2)
1704 (if (TREE_CODE (@1) == INTEGER_CST
1705 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1706 (cmp @2 @0))))))
1707
1708 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1709 (for cmp (le gt)
1710 icmp (gt le)
1711 (simplify
1712 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1713 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1714 && TYPE_UNSIGNED (TREE_TYPE (@0))
1715 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1716 && (wi::to_wide (@2)
1717 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1718 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1719 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1720
1721 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1722 (for cmp (simple_comparison)
1723 (simplify
1724 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1725 (if (element_precision (@3) >= element_precision (@0)
1726 && types_match (@0, @1))
1727 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1728 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1729 (cmp @1 @0)
1730 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1731 (with
1732 {
1733 tree utype = unsigned_type_for (TREE_TYPE (@0));
1734 }
1735 (cmp (convert:utype @1) (convert:utype @0)))))
1736 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1737 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1738 (cmp @0 @1)
1739 (with
1740 {
1741 tree utype = unsigned_type_for (TREE_TYPE (@0));
1742 }
1743 (cmp (convert:utype @0) (convert:utype @1)))))))))
1744
1745 /* X / C1 op C2 into a simple range test. */
1746 (for cmp (simple_comparison)
1747 (simplify
1748 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1749 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1750 && integer_nonzerop (@1)
1751 && !TREE_OVERFLOW (@1)
1752 && !TREE_OVERFLOW (@2))
1753 (with { tree lo, hi; bool neg_overflow;
1754 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1755 &neg_overflow); }
1756 (switch
1757 (if (code == LT_EXPR || code == GE_EXPR)
1758 (if (TREE_OVERFLOW (lo))
1759 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1760 (if (code == LT_EXPR)
1761 (lt @0 { lo; })
1762 (ge @0 { lo; }))))
1763 (if (code == LE_EXPR || code == GT_EXPR)
1764 (if (TREE_OVERFLOW (hi))
1765 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1766 (if (code == LE_EXPR)
1767 (le @0 { hi; })
1768 (gt @0 { hi; }))))
1769 (if (!lo && !hi)
1770 { build_int_cst (type, code == NE_EXPR); })
1771 (if (code == EQ_EXPR && !hi)
1772 (ge @0 { lo; }))
1773 (if (code == EQ_EXPR && !lo)
1774 (le @0 { hi; }))
1775 (if (code == NE_EXPR && !hi)
1776 (lt @0 { lo; }))
1777 (if (code == NE_EXPR && !lo)
1778 (gt @0 { hi; }))
1779 (if (GENERIC)
1780 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1781 lo, hi); })
1782 (with
1783 {
1784 tree etype = range_check_type (TREE_TYPE (@0));
1785 if (etype)
1786 {
1787 hi = fold_convert (etype, hi);
1788 lo = fold_convert (etype, lo);
1789 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1790 }
1791 }
1792 (if (etype && hi && !TREE_OVERFLOW (hi))
1793 (if (code == EQ_EXPR)
1794 (le (minus (convert:etype @0) { lo; }) { hi; })
1795 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1796
1797 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1798 (for op (lt le ge gt)
1799 (simplify
1800 (op (plus:c @0 @2) (plus:c @1 @2))
1801 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1802 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1803 (op @0 @1))))
1804 /* For equality and subtraction, this is also true with wrapping overflow. */
1805 (for op (eq ne minus)
1806 (simplify
1807 (op (plus:c @0 @2) (plus:c @1 @2))
1808 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1809 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1810 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1811 (op @0 @1))))
1812
1813 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1814 (for op (lt le ge gt)
1815 (simplify
1816 (op (minus @0 @2) (minus @1 @2))
1817 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1818 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1819 (op @0 @1))))
1820 /* For equality and subtraction, this is also true with wrapping overflow. */
1821 (for op (eq ne minus)
1822 (simplify
1823 (op (minus @0 @2) (minus @1 @2))
1824 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1825 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1826 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1827 (op @0 @1))))
1828 /* And for pointers... */
1829 (for op (simple_comparison)
1830 (simplify
1831 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1832 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1833 (op @0 @1))))
1834 (simplify
1835 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1836 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1837 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1838 (pointer_diff @0 @1)))
1839
1840 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1841 (for op (lt le ge gt)
1842 (simplify
1843 (op (minus @2 @0) (minus @2 @1))
1844 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1845 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1846 (op @1 @0))))
1847 /* For equality and subtraction, this is also true with wrapping overflow. */
1848 (for op (eq ne minus)
1849 (simplify
1850 (op (minus @2 @0) (minus @2 @1))
1851 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1852 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1853 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1854 (op @1 @0))))
1855 /* And for pointers... */
1856 (for op (simple_comparison)
1857 (simplify
1858 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1859 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1860 (op @1 @0))))
1861 (simplify
1862 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1863 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1864 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1865 (pointer_diff @1 @0)))
1866
1867 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1868 (for op (lt le gt ge)
1869 (simplify
1870 (op:c (plus:c@2 @0 @1) @1)
1871 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1872 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1873 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1874 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1875 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1876 /* For equality, this is also true with wrapping overflow. */
1877 (for op (eq ne)
1878 (simplify
1879 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1880 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1881 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1882 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1883 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1884 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1885 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1886 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1887 (simplify
1888 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1889 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1890 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1891 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1892 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1893
1894 /* X - Y < X is the same as Y > 0 when there is no overflow.
1895 For equality, this is also true with wrapping overflow. */
1896 (for op (simple_comparison)
1897 (simplify
1898 (op:c @0 (minus@2 @0 @1))
1899 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1900 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1901 || ((op == EQ_EXPR || op == NE_EXPR)
1902 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1903 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1904 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1905
1906 /* Transform:
1907 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1908 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1909 (for cmp (eq ne)
1910 ocmp (lt ge)
1911 (simplify
1912 (cmp (trunc_div @0 @1) integer_zerop)
1913 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1914 /* Complex ==/!= is allowed, but not </>=. */
1915 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1916 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1917 (ocmp @0 @1))))
1918
1919 /* X == C - X can never be true if C is odd. */
1920 (for cmp (eq ne)
1921 (simplify
1922 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1923 (if (TREE_INT_CST_LOW (@1) & 1)
1924 { constant_boolean_node (cmp == NE_EXPR, type); })))
1925
1926 /* Arguments on which one can call get_nonzero_bits to get the bits
1927 possibly set. */
1928 (match with_possible_nonzero_bits
1929 INTEGER_CST@0)
1930 (match with_possible_nonzero_bits
1931 SSA_NAME@0
1932 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1933 /* Slightly extended version, do not make it recursive to keep it cheap. */
1934 (match (with_possible_nonzero_bits2 @0)
1935 with_possible_nonzero_bits@0)
1936 (match (with_possible_nonzero_bits2 @0)
1937 (bit_and:c with_possible_nonzero_bits@0 @2))
1938
1939 /* Same for bits that are known to be set, but we do not have
1940 an equivalent to get_nonzero_bits yet. */
1941 (match (with_certain_nonzero_bits2 @0)
1942 INTEGER_CST@0)
1943 (match (with_certain_nonzero_bits2 @0)
1944 (bit_ior @1 INTEGER_CST@0))
1945
1946 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1947 (for cmp (eq ne)
1948 (simplify
1949 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1950 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1951 { constant_boolean_node (cmp == NE_EXPR, type); })))
1952
1953 /* ((X inner_op C0) outer_op C1)
1954 With X being a tree where value_range has reasoned certain bits to always be
1955 zero throughout its computed value range,
1956 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1957 where zero_mask has 1's for all bits that are sure to be 0 in
1958 and 0's otherwise.
1959 if (inner_op == '^') C0 &= ~C1;
1960 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1961 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1962 */
1963 (for inner_op (bit_ior bit_xor)
1964 outer_op (bit_xor bit_ior)
1965 (simplify
1966 (outer_op
1967 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1968 (with
1969 {
1970 bool fail = false;
1971 wide_int zero_mask_not;
1972 wide_int C0;
1973 wide_int cst_emit;
1974
1975 if (TREE_CODE (@2) == SSA_NAME)
1976 zero_mask_not = get_nonzero_bits (@2);
1977 else
1978 fail = true;
1979
1980 if (inner_op == BIT_XOR_EXPR)
1981 {
1982 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1983 cst_emit = C0 | wi::to_wide (@1);
1984 }
1985 else
1986 {
1987 C0 = wi::to_wide (@0);
1988 cst_emit = C0 ^ wi::to_wide (@1);
1989 }
1990 }
1991 (if (!fail && (C0 & zero_mask_not) == 0)
1992 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1993 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1994 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1995
1996 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1997 (simplify
1998 (pointer_plus (pointer_plus:s @0 @1) @3)
1999 (pointer_plus @0 (plus @1 @3)))
2000
2001 /* Pattern match
2002 tem1 = (long) ptr1;
2003 tem2 = (long) ptr2;
2004 tem3 = tem2 - tem1;
2005 tem4 = (unsigned long) tem3;
2006 tem5 = ptr1 + tem4;
2007 and produce
2008 tem5 = ptr2; */
2009 (simplify
2010 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2011 /* Conditionally look through a sign-changing conversion. */
2012 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2013 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2014 || (GENERIC && type == TREE_TYPE (@1))))
2015 @1))
2016 (simplify
2017 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2018 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2019 (convert @1)))
2020
2021 /* Pattern match
2022 tem = (sizetype) ptr;
2023 tem = tem & algn;
2024 tem = -tem;
2025 ... = ptr p+ tem;
2026 and produce the simpler and easier to analyze with respect to alignment
2027 ... = ptr & ~algn; */
2028 (simplify
2029 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2030 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2031 (bit_and @0 { algn; })))
2032
2033 /* Try folding difference of addresses. */
2034 (simplify
2035 (minus (convert ADDR_EXPR@0) (convert @1))
2036 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2037 (with { poly_int64 diff; }
2038 (if (ptr_difference_const (@0, @1, &diff))
2039 { build_int_cst_type (type, diff); }))))
2040 (simplify
2041 (minus (convert @0) (convert ADDR_EXPR@1))
2042 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2043 (with { poly_int64 diff; }
2044 (if (ptr_difference_const (@0, @1, &diff))
2045 { build_int_cst_type (type, diff); }))))
2046 (simplify
2047 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2048 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2049 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2050 (with { poly_int64 diff; }
2051 (if (ptr_difference_const (@0, @1, &diff))
2052 { build_int_cst_type (type, diff); }))))
2053 (simplify
2054 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2055 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2056 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2057 (with { poly_int64 diff; }
2058 (if (ptr_difference_const (@0, @1, &diff))
2059 { build_int_cst_type (type, diff); }))))
2060
2061 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2062 (simplify
2063 (convert (pointer_diff @0 INTEGER_CST@1))
2064 (if (POINTER_TYPE_P (type))
2065 { build_fold_addr_expr_with_type
2066 (build2 (MEM_REF, char_type_node, @0,
2067 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2068 type); }))
2069
2070 /* If arg0 is derived from the address of an object or function, we may
2071 be able to fold this expression using the object or function's
2072 alignment. */
2073 (simplify
2074 (bit_and (convert? @0) INTEGER_CST@1)
2075 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2076 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2077 (with
2078 {
2079 unsigned int align;
2080 unsigned HOST_WIDE_INT bitpos;
2081 get_pointer_alignment_1 (@0, &align, &bitpos);
2082 }
2083 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2084 { wide_int_to_tree (type, (wi::to_wide (@1)
2085 & (bitpos / BITS_PER_UNIT))); }))))
2086
2087 (match min_value
2088 INTEGER_CST
2089 (if (INTEGRAL_TYPE_P (type)
2090 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2091
2092 (match max_value
2093 INTEGER_CST
2094 (if (INTEGRAL_TYPE_P (type)
2095 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2096
2097 /* x > y && x != XXX_MIN --> x > y
2098 x > y && x == XXX_MIN --> false . */
2099 (for eqne (eq ne)
2100 (simplify
2101 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2102 (switch
2103 (if (eqne == EQ_EXPR)
2104 { constant_boolean_node (false, type); })
2105 (if (eqne == NE_EXPR)
2106 @2)
2107 )))
2108
2109 /* x < y && x != XXX_MAX --> x < y
2110 x < y && x == XXX_MAX --> false. */
2111 (for eqne (eq ne)
2112 (simplify
2113 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2114 (switch
2115 (if (eqne == EQ_EXPR)
2116 { constant_boolean_node (false, type); })
2117 (if (eqne == NE_EXPR)
2118 @2)
2119 )))
2120
2121 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2122 (simplify
2123 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2124 @2)
2125
2126 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2127 (simplify
2128 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2129 @2)
2130
2131 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2132 (simplify
2133 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2134 @2)
2135
2136 /* x <= y || x != XXX_MIN --> true. */
2137 (simplify
2138 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2139 { constant_boolean_node (true, type); })
2140
2141 /* x <= y || x == XXX_MIN --> x <= y. */
2142 (simplify
2143 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2144 @2)
2145
2146 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2147 (simplify
2148 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2149 @2)
2150
2151 /* x >= y || x != XXX_MAX --> true
2152 x >= y || x == XXX_MAX --> x >= y. */
2153 (for eqne (eq ne)
2154 (simplify
2155 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2156 (switch
2157 (if (eqne == EQ_EXPR)
2158 @2)
2159 (if (eqne == NE_EXPR)
2160 { constant_boolean_node (true, type); }))))
2161
2162 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2163 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2164
2165 (for code1 (eq ne)
2166 (for code2 (eq ne lt gt le ge)
2167 (simplify
2168 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2169 (with
2170 {
2171 int cmp = tree_int_cst_compare (@1, @2);
2172 bool val;
2173 switch (code2)
2174 {
2175 case EQ_EXPR: val = (cmp == 0); break;
2176 case NE_EXPR: val = (cmp != 0); break;
2177 case LT_EXPR: val = (cmp < 0); break;
2178 case GT_EXPR: val = (cmp > 0); break;
2179 case LE_EXPR: val = (cmp <= 0); break;
2180 case GE_EXPR: val = (cmp >= 0); break;
2181 default: gcc_unreachable ();
2182 }
2183 }
2184 (switch
2185 (if (code1 == EQ_EXPR && val) @3)
2186 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2187 (if (code1 == NE_EXPR && !val) @4))))))
2188
2189 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2190
2191 (for code1 (lt le gt ge)
2192 (for code2 (lt le gt ge)
2193 (simplify
2194 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2195 (with
2196 {
2197 int cmp = tree_int_cst_compare (@1, @2);
2198 }
2199 (switch
2200 /* Choose the more restrictive of two < or <= comparisons. */
2201 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2202 && (code2 == LT_EXPR || code2 == LE_EXPR))
2203 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2204 @3
2205 @4))
2206 /* Likewise chose the more restrictive of two > or >= comparisons. */
2207 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2208 && (code2 == GT_EXPR || code2 == GE_EXPR))
2209 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2210 @3
2211 @4))
2212 /* Check for singleton ranges. */
2213 (if (cmp == 0
2214 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2215 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2216 (eq @0 @1))
2217 /* Check for disjoint ranges. */
2218 (if (cmp <= 0
2219 && (code1 == LT_EXPR || code1 == LE_EXPR)
2220 && (code2 == GT_EXPR || code2 == GE_EXPR))
2221 { constant_boolean_node (false, type); })
2222 (if (cmp >= 0
2223 && (code1 == GT_EXPR || code1 == GE_EXPR)
2224 && (code2 == LT_EXPR || code2 == LE_EXPR))
2225 { constant_boolean_node (false, type); })
2226 )))))
2227
2228 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2229 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2230
2231 (for code1 (eq ne)
2232 (for code2 (eq ne lt gt le ge)
2233 (simplify
2234 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2235 (with
2236 {
2237 int cmp = tree_int_cst_compare (@1, @2);
2238 bool val;
2239 switch (code2)
2240 {
2241 case EQ_EXPR: val = (cmp == 0); break;
2242 case NE_EXPR: val = (cmp != 0); break;
2243 case LT_EXPR: val = (cmp < 0); break;
2244 case GT_EXPR: val = (cmp > 0); break;
2245 case LE_EXPR: val = (cmp <= 0); break;
2246 case GE_EXPR: val = (cmp >= 0); break;
2247 default: gcc_unreachable ();
2248 }
2249 }
2250 (switch
2251 (if (code1 == EQ_EXPR && val) @4)
2252 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2253 (if (code1 == NE_EXPR && !val) @3))))))
2254
2255 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2256
2257 (for code1 (lt le gt ge)
2258 (for code2 (lt le gt ge)
2259 (simplify
2260 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2261 (with
2262 {
2263 int cmp = tree_int_cst_compare (@1, @2);
2264 }
2265 (switch
2266 /* Choose the more restrictive of two < or <= comparisons. */
2267 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2268 && (code2 == LT_EXPR || code2 == LE_EXPR))
2269 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2270 @4
2271 @3))
2272 /* Likewise chose the more restrictive of two > or >= comparisons. */
2273 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2274 && (code2 == GT_EXPR || code2 == GE_EXPR))
2275 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2276 @4
2277 @3))
2278 /* Check for singleton ranges. */
2279 (if (cmp == 0
2280 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2281 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2282 (ne @0 @2))
2283 /* Check for disjoint ranges. */
2284 (if (cmp >= 0
2285 && (code1 == LT_EXPR || code1 == LE_EXPR)
2286 && (code2 == GT_EXPR || code2 == GE_EXPR))
2287 { constant_boolean_node (true, type); })
2288 (if (cmp <= 0
2289 && (code1 == GT_EXPR || code1 == GE_EXPR)
2290 && (code2 == LT_EXPR || code2 == LE_EXPR))
2291 { constant_boolean_node (true, type); })
2292 )))))
2293
2294 /* We can't reassociate at all for saturating types. */
2295 (if (!TYPE_SATURATING (type))
2296
2297 /* Contract negates. */
2298 /* A + (-B) -> A - B */
2299 (simplify
2300 (plus:c @0 (convert? (negate @1)))
2301 /* Apply STRIP_NOPS on the negate. */
2302 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2303 && !TYPE_OVERFLOW_SANITIZED (type))
2304 (with
2305 {
2306 tree t1 = type;
2307 if (INTEGRAL_TYPE_P (type)
2308 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2309 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2310 }
2311 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2312 /* A - (-B) -> A + B */
2313 (simplify
2314 (minus @0 (convert? (negate @1)))
2315 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2316 && !TYPE_OVERFLOW_SANITIZED (type))
2317 (with
2318 {
2319 tree t1 = type;
2320 if (INTEGRAL_TYPE_P (type)
2321 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2322 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2323 }
2324 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2325 /* -(T)(-A) -> (T)A
2326 Sign-extension is ok except for INT_MIN, which thankfully cannot
2327 happen without overflow. */
2328 (simplify
2329 (negate (convert (negate @1)))
2330 (if (INTEGRAL_TYPE_P (type)
2331 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2332 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2333 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2334 && !TYPE_OVERFLOW_SANITIZED (type)
2335 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2336 (convert @1)))
2337 (simplify
2338 (negate (convert negate_expr_p@1))
2339 (if (SCALAR_FLOAT_TYPE_P (type)
2340 && ((DECIMAL_FLOAT_TYPE_P (type)
2341 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2342 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2343 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2344 (convert (negate @1))))
2345 (simplify
2346 (negate (nop_convert? (negate @1)))
2347 (if (!TYPE_OVERFLOW_SANITIZED (type)
2348 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2349 (view_convert @1)))
2350
2351 /* We can't reassociate floating-point unless -fassociative-math
2352 or fixed-point plus or minus because of saturation to +-Inf. */
2353 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2354 && !FIXED_POINT_TYPE_P (type))
2355
2356 /* Match patterns that allow contracting a plus-minus pair
2357 irrespective of overflow issues. */
2358 /* (A +- B) - A -> +- B */
2359 /* (A +- B) -+ B -> A */
2360 /* A - (A +- B) -> -+ B */
2361 /* A +- (B -+ A) -> +- B */
2362 (simplify
2363 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2364 (view_convert @1))
2365 (simplify
2366 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2367 (if (!ANY_INTEGRAL_TYPE_P (type)
2368 || TYPE_OVERFLOW_WRAPS (type))
2369 (negate (view_convert @1))
2370 (view_convert (negate @1))))
2371 (simplify
2372 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2373 (view_convert @0))
2374 (simplify
2375 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2376 (if (!ANY_INTEGRAL_TYPE_P (type)
2377 || TYPE_OVERFLOW_WRAPS (type))
2378 (negate (view_convert @1))
2379 (view_convert (negate @1))))
2380 (simplify
2381 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2382 (view_convert @1))
2383 /* (A +- B) + (C - A) -> C +- B */
2384 /* (A + B) - (A - C) -> B + C */
2385 /* More cases are handled with comparisons. */
2386 (simplify
2387 (plus:c (plus:c @0 @1) (minus @2 @0))
2388 (plus @2 @1))
2389 (simplify
2390 (plus:c (minus @0 @1) (minus @2 @0))
2391 (minus @2 @1))
2392 (simplify
2393 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2394 (if (TYPE_OVERFLOW_UNDEFINED (type)
2395 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2396 (pointer_diff @2 @1)))
2397 (simplify
2398 (minus (plus:c @0 @1) (minus @0 @2))
2399 (plus @1 @2))
2400
2401 /* (A +- CST1) +- CST2 -> A + CST3
2402 Use view_convert because it is safe for vectors and equivalent for
2403 scalars. */
2404 (for outer_op (plus minus)
2405 (for inner_op (plus minus)
2406 neg_inner_op (minus plus)
2407 (simplify
2408 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2409 CONSTANT_CLASS_P@2)
2410 /* If one of the types wraps, use that one. */
2411 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2412 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2413 forever if something doesn't simplify into a constant. */
2414 (if (!CONSTANT_CLASS_P (@0))
2415 (if (outer_op == PLUS_EXPR)
2416 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2417 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2418 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2419 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2420 (if (outer_op == PLUS_EXPR)
2421 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2422 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2423 /* If the constant operation overflows we cannot do the transform
2424 directly as we would introduce undefined overflow, for example
2425 with (a - 1) + INT_MIN. */
2426 (if (types_match (type, @0))
2427 (with { tree cst = const_binop (outer_op == inner_op
2428 ? PLUS_EXPR : MINUS_EXPR,
2429 type, @1, @2); }
2430 (if (cst && !TREE_OVERFLOW (cst))
2431 (inner_op @0 { cst; } )
2432 /* X+INT_MAX+1 is X-INT_MIN. */
2433 (if (INTEGRAL_TYPE_P (type) && cst
2434 && wi::to_wide (cst) == wi::min_value (type))
2435 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2436 /* Last resort, use some unsigned type. */
2437 (with { tree utype = unsigned_type_for (type); }
2438 (if (utype)
2439 (view_convert (inner_op
2440 (view_convert:utype @0)
2441 (view_convert:utype
2442 { drop_tree_overflow (cst); }))))))))))))))
2443
2444 /* (CST1 - A) +- CST2 -> CST3 - A */
2445 (for outer_op (plus minus)
2446 (simplify
2447 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2448 /* If one of the types wraps, use that one. */
2449 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2450 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2451 forever if something doesn't simplify into a constant. */
2452 (if (!CONSTANT_CLASS_P (@0))
2453 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2454 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2455 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2456 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2457 (if (types_match (type, @0))
2458 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2459 (if (cst && !TREE_OVERFLOW (cst))
2460 (minus { cst; } @0))))))))
2461
2462 /* CST1 - (CST2 - A) -> CST3 + A
2463 Use view_convert because it is safe for vectors and equivalent for
2464 scalars. */
2465 (simplify
2466 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2467 /* If one of the types wraps, use that one. */
2468 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2469 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2470 forever if something doesn't simplify into a constant. */
2471 (if (!CONSTANT_CLASS_P (@0))
2472 (plus (view_convert @0) (minus @1 (view_convert @2))))
2473 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2474 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2475 (view_convert (plus @0 (minus (view_convert @1) @2)))
2476 (if (types_match (type, @0))
2477 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2478 (if (cst && !TREE_OVERFLOW (cst))
2479 (plus { cst; } @0)))))))
2480
2481 /* ((T)(A)) + CST -> (T)(A + CST) */
2482 #if GIMPLE
2483 (simplify
2484 (plus (convert SSA_NAME@0) INTEGER_CST@1)
2485 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2486 && TREE_CODE (type) == INTEGER_TYPE
2487 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2488 && int_fits_type_p (@1, TREE_TYPE (@0)))
2489 /* Perform binary operation inside the cast if the constant fits
2490 and (A + CST)'s range does not overflow. */
2491 (with
2492 {
2493 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2494 max_ovf = wi::OVF_OVERFLOW;
2495 tree inner_type = TREE_TYPE (@0);
2496
2497 wide_int w1
2498 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2499 TYPE_SIGN (inner_type));
2500
2501 wide_int wmin0, wmax0;
2502 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2503 {
2504 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2505 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2506 }
2507 }
2508 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2509 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2510 )))
2511 #endif
2512
2513 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2514 #if GIMPLE
2515 (for op (plus minus)
2516 (simplify
2517 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2518 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2519 && TREE_CODE (type) == INTEGER_TYPE
2520 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2521 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2522 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2523 && TYPE_OVERFLOW_WRAPS (type))
2524 (plus (convert @0) (op @2 (convert @1))))))
2525 #endif
2526
2527 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2528 to a simple value. */
2529 #if GIMPLE
2530 (for op (plus minus)
2531 (simplify
2532 (op (convert @0) (convert @1))
2533 (if (INTEGRAL_TYPE_P (type)
2534 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2535 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2536 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2537 && !TYPE_OVERFLOW_TRAPS (type)
2538 && !TYPE_OVERFLOW_SANITIZED (type))
2539 (convert (op! @0 @1)))))
2540 #endif
2541
2542 /* ~A + A -> -1 */
2543 (simplify
2544 (plus:c (bit_not @0) @0)
2545 (if (!TYPE_OVERFLOW_TRAPS (type))
2546 { build_all_ones_cst (type); }))
2547
2548 /* ~A + 1 -> -A */
2549 (simplify
2550 (plus (convert? (bit_not @0)) integer_each_onep)
2551 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2552 (negate (convert @0))))
2553
2554 /* -A - 1 -> ~A */
2555 (simplify
2556 (minus (convert? (negate @0)) integer_each_onep)
2557 (if (!TYPE_OVERFLOW_TRAPS (type)
2558 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2559 (bit_not (convert @0))))
2560
2561 /* -1 - A -> ~A */
2562 (simplify
2563 (minus integer_all_onesp @0)
2564 (bit_not @0))
2565
2566 /* (T)(P + A) - (T)P -> (T) A */
2567 (simplify
2568 (minus (convert (plus:c @@0 @1))
2569 (convert? @0))
2570 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2571 /* For integer types, if A has a smaller type
2572 than T the result depends on the possible
2573 overflow in P + A.
2574 E.g. T=size_t, A=(unsigned)429497295, P>0.
2575 However, if an overflow in P + A would cause
2576 undefined behavior, we can assume that there
2577 is no overflow. */
2578 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2579 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2580 (convert @1)))
2581 (simplify
2582 (minus (convert (pointer_plus @@0 @1))
2583 (convert @0))
2584 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2585 /* For pointer types, if the conversion of A to the
2586 final type requires a sign- or zero-extension,
2587 then we have to punt - it is not defined which
2588 one is correct. */
2589 || (POINTER_TYPE_P (TREE_TYPE (@0))
2590 && TREE_CODE (@1) == INTEGER_CST
2591 && tree_int_cst_sign_bit (@1) == 0))
2592 (convert @1)))
2593 (simplify
2594 (pointer_diff (pointer_plus @@0 @1) @0)
2595 /* The second argument of pointer_plus must be interpreted as signed, and
2596 thus sign-extended if necessary. */
2597 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2598 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2599 second arg is unsigned even when we need to consider it as signed,
2600 we don't want to diagnose overflow here. */
2601 (convert (view_convert:stype @1))))
2602
2603 /* (T)P - (T)(P + A) -> -(T) A */
2604 (simplify
2605 (minus (convert? @0)
2606 (convert (plus:c @@0 @1)))
2607 (if (INTEGRAL_TYPE_P (type)
2608 && TYPE_OVERFLOW_UNDEFINED (type)
2609 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2610 (with { tree utype = unsigned_type_for (type); }
2611 (convert (negate (convert:utype @1))))
2612 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2613 /* For integer types, if A has a smaller type
2614 than T the result depends on the possible
2615 overflow in P + A.
2616 E.g. T=size_t, A=(unsigned)429497295, P>0.
2617 However, if an overflow in P + A would cause
2618 undefined behavior, we can assume that there
2619 is no overflow. */
2620 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2621 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2622 (negate (convert @1)))))
2623 (simplify
2624 (minus (convert @0)
2625 (convert (pointer_plus @@0 @1)))
2626 (if (INTEGRAL_TYPE_P (type)
2627 && TYPE_OVERFLOW_UNDEFINED (type)
2628 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2629 (with { tree utype = unsigned_type_for (type); }
2630 (convert (negate (convert:utype @1))))
2631 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2632 /* For pointer types, if the conversion of A to the
2633 final type requires a sign- or zero-extension,
2634 then we have to punt - it is not defined which
2635 one is correct. */
2636 || (POINTER_TYPE_P (TREE_TYPE (@0))
2637 && TREE_CODE (@1) == INTEGER_CST
2638 && tree_int_cst_sign_bit (@1) == 0))
2639 (negate (convert @1)))))
2640 (simplify
2641 (pointer_diff @0 (pointer_plus @@0 @1))
2642 /* The second argument of pointer_plus must be interpreted as signed, and
2643 thus sign-extended if necessary. */
2644 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2645 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2646 second arg is unsigned even when we need to consider it as signed,
2647 we don't want to diagnose overflow here. */
2648 (negate (convert (view_convert:stype @1)))))
2649
2650 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2651 (simplify
2652 (minus (convert (plus:c @@0 @1))
2653 (convert (plus:c @0 @2)))
2654 (if (INTEGRAL_TYPE_P (type)
2655 && TYPE_OVERFLOW_UNDEFINED (type)
2656 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2657 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2658 (with { tree utype = unsigned_type_for (type); }
2659 (convert (minus (convert:utype @1) (convert:utype @2))))
2660 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2661 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2662 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2663 /* For integer types, if A has a smaller type
2664 than T the result depends on the possible
2665 overflow in P + A.
2666 E.g. T=size_t, A=(unsigned)429497295, P>0.
2667 However, if an overflow in P + A would cause
2668 undefined behavior, we can assume that there
2669 is no overflow. */
2670 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2671 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2672 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2673 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2674 (minus (convert @1) (convert @2)))))
2675 (simplify
2676 (minus (convert (pointer_plus @@0 @1))
2677 (convert (pointer_plus @0 @2)))
2678 (if (INTEGRAL_TYPE_P (type)
2679 && TYPE_OVERFLOW_UNDEFINED (type)
2680 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2681 (with { tree utype = unsigned_type_for (type); }
2682 (convert (minus (convert:utype @1) (convert:utype @2))))
2683 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2684 /* For pointer types, if the conversion of A to the
2685 final type requires a sign- or zero-extension,
2686 then we have to punt - it is not defined which
2687 one is correct. */
2688 || (POINTER_TYPE_P (TREE_TYPE (@0))
2689 && TREE_CODE (@1) == INTEGER_CST
2690 && tree_int_cst_sign_bit (@1) == 0
2691 && TREE_CODE (@2) == INTEGER_CST
2692 && tree_int_cst_sign_bit (@2) == 0))
2693 (minus (convert @1) (convert @2)))))
2694 (simplify
2695 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2696 (pointer_diff @0 @1))
2697 (simplify
2698 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2699 /* The second argument of pointer_plus must be interpreted as signed, and
2700 thus sign-extended if necessary. */
2701 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2702 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2703 second arg is unsigned even when we need to consider it as signed,
2704 we don't want to diagnose overflow here. */
2705 (minus (convert (view_convert:stype @1))
2706 (convert (view_convert:stype @2)))))))
2707
2708 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2709 Modeled after fold_plusminus_mult_expr. */
2710 (if (!TYPE_SATURATING (type)
2711 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2712 (for plusminus (plus minus)
2713 (simplify
2714 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2715 (if (!ANY_INTEGRAL_TYPE_P (type)
2716 || TYPE_OVERFLOW_WRAPS (type)
2717 || (INTEGRAL_TYPE_P (type)
2718 && tree_expr_nonzero_p (@0)
2719 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2720 (if (single_use (@3) || single_use (@4))
2721 /* If @1 +- @2 is constant require a hard single-use on either
2722 original operand (but not on both). */
2723 (mult (plusminus @1 @2) @0)
2724 #if GIMPLE
2725 (mult! (plusminus @1 @2) @0)
2726 #endif
2727 )))
2728 /* We cannot generate constant 1 for fract. */
2729 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2730 (simplify
2731 (plusminus @0 (mult:c@3 @0 @2))
2732 (if ((!ANY_INTEGRAL_TYPE_P (type)
2733 || TYPE_OVERFLOW_WRAPS (type)
2734 /* For @0 + @0*@2 this transformation would introduce UB
2735 (where there was none before) for @0 in [-1,0] and @2 max.
2736 For @0 - @0*@2 this transformation would introduce UB
2737 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2738 || (INTEGRAL_TYPE_P (type)
2739 && ((tree_expr_nonzero_p (@0)
2740 && expr_not_equal_to (@0,
2741 wi::minus_one (TYPE_PRECISION (type))))
2742 || (plusminus == PLUS_EXPR
2743 ? expr_not_equal_to (@2,
2744 wi::max_value (TYPE_PRECISION (type), SIGNED))
2745 /* Let's ignore the @0 -1 and @2 min case. */
2746 : (expr_not_equal_to (@2,
2747 wi::min_value (TYPE_PRECISION (type), SIGNED))
2748 && expr_not_equal_to (@2,
2749 wi::min_value (TYPE_PRECISION (type), SIGNED)
2750 + 1))))))
2751 && single_use (@3))
2752 (mult (plusminus { build_one_cst (type); } @2) @0)))
2753 (simplify
2754 (plusminus (mult:c@3 @0 @2) @0)
2755 (if ((!ANY_INTEGRAL_TYPE_P (type)
2756 || TYPE_OVERFLOW_WRAPS (type)
2757 /* For @0*@2 + @0 this transformation would introduce UB
2758 (where there was none before) for @0 in [-1,0] and @2 max.
2759 For @0*@2 - @0 this transformation would introduce UB
2760 for @0 0 and @2 min. */
2761 || (INTEGRAL_TYPE_P (type)
2762 && ((tree_expr_nonzero_p (@0)
2763 && (plusminus == MINUS_EXPR
2764 || expr_not_equal_to (@0,
2765 wi::minus_one (TYPE_PRECISION (type)))))
2766 || expr_not_equal_to (@2,
2767 (plusminus == PLUS_EXPR
2768 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2769 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2770 && single_use (@3))
2771 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2772
2773 #if GIMPLE
2774 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2775 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2776 (simplify
2777 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2778 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2779 && tree_fits_uhwi_p (@1)
2780 && tree_to_uhwi (@1) < element_precision (type))
2781 (with { tree t = type;
2782 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2783 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2784 element_precision (type));
2785 w += 1;
2786 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2787 : t, w);
2788 cst = build_uniform_cst (t, cst); }
2789 (convert (mult (convert:t @0) { cst; })))))
2790 (simplify
2791 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2792 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2793 && tree_fits_uhwi_p (@1)
2794 && tree_to_uhwi (@1) < element_precision (type)
2795 && tree_fits_uhwi_p (@2)
2796 && tree_to_uhwi (@2) < element_precision (type))
2797 (with { tree t = type;
2798 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2799 unsigned int prec = element_precision (type);
2800 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2801 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2802 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2803 : t, w);
2804 cst = build_uniform_cst (t, cst); }
2805 (convert (mult (convert:t @0) { cst; })))))
2806 #endif
2807
2808 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2809
2810 (for minmax (min max FMIN_ALL FMAX_ALL)
2811 (simplify
2812 (minmax @0 @0)
2813 @0))
2814 /* min(max(x,y),y) -> y. */
2815 (simplify
2816 (min:c (max:c @0 @1) @1)
2817 @1)
2818 /* max(min(x,y),y) -> y. */
2819 (simplify
2820 (max:c (min:c @0 @1) @1)
2821 @1)
2822 /* max(a,-a) -> abs(a). */
2823 (simplify
2824 (max:c @0 (negate @0))
2825 (if (TREE_CODE (type) != COMPLEX_TYPE
2826 && (! ANY_INTEGRAL_TYPE_P (type)
2827 || TYPE_OVERFLOW_UNDEFINED (type)))
2828 (abs @0)))
2829 /* min(a,-a) -> -abs(a). */
2830 (simplify
2831 (min:c @0 (negate @0))
2832 (if (TREE_CODE (type) != COMPLEX_TYPE
2833 && (! ANY_INTEGRAL_TYPE_P (type)
2834 || TYPE_OVERFLOW_UNDEFINED (type)))
2835 (negate (abs @0))))
2836 (simplify
2837 (min @0 @1)
2838 (switch
2839 (if (INTEGRAL_TYPE_P (type)
2840 && TYPE_MIN_VALUE (type)
2841 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2842 @1)
2843 (if (INTEGRAL_TYPE_P (type)
2844 && TYPE_MAX_VALUE (type)
2845 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2846 @0)))
2847 (simplify
2848 (max @0 @1)
2849 (switch
2850 (if (INTEGRAL_TYPE_P (type)
2851 && TYPE_MAX_VALUE (type)
2852 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2853 @1)
2854 (if (INTEGRAL_TYPE_P (type)
2855 && TYPE_MIN_VALUE (type)
2856 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2857 @0)))
2858
2859 /* max (a, a + CST) -> a + CST where CST is positive. */
2860 /* max (a, a + CST) -> a where CST is negative. */
2861 (simplify
2862 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2863 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2864 (if (tree_int_cst_sgn (@1) > 0)
2865 @2
2866 @0)))
2867
2868 /* min (a, a + CST) -> a where CST is positive. */
2869 /* min (a, a + CST) -> a + CST where CST is negative. */
2870 (simplify
2871 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2872 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2873 (if (tree_int_cst_sgn (@1) > 0)
2874 @0
2875 @2)))
2876
2877 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2878 and the outer convert demotes the expression back to x's type. */
2879 (for minmax (min max)
2880 (simplify
2881 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2882 (if (INTEGRAL_TYPE_P (type)
2883 && types_match (@1, type) && int_fits_type_p (@2, type)
2884 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2885 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2886 (minmax @1 (convert @2)))))
2887
2888 (for minmax (FMIN_ALL FMAX_ALL)
2889 /* If either argument is NaN, return the other one. Avoid the
2890 transformation if we get (and honor) a signalling NaN. */
2891 (simplify
2892 (minmax:c @0 REAL_CST@1)
2893 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2894 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2895 @0)))
2896 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2897 functions to return the numeric arg if the other one is NaN.
2898 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2899 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2900 worry about it either. */
2901 (if (flag_finite_math_only)
2902 (simplify
2903 (FMIN_ALL @0 @1)
2904 (min @0 @1))
2905 (simplify
2906 (FMAX_ALL @0 @1)
2907 (max @0 @1)))
2908 /* min (-A, -B) -> -max (A, B) */
2909 (for minmax (min max FMIN_ALL FMAX_ALL)
2910 maxmin (max min FMAX_ALL FMIN_ALL)
2911 (simplify
2912 (minmax (negate:s@2 @0) (negate:s@3 @1))
2913 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2914 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2915 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2916 (negate (maxmin @0 @1)))))
2917 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2918 MAX (~X, ~Y) -> ~MIN (X, Y) */
2919 (for minmax (min max)
2920 maxmin (max min)
2921 (simplify
2922 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2923 (bit_not (maxmin @0 @1))))
2924
2925 /* MIN (X, Y) == X -> X <= Y */
2926 (for minmax (min min max max)
2927 cmp (eq ne eq ne )
2928 out (le gt ge lt )
2929 (simplify
2930 (cmp:c (minmax:c @0 @1) @0)
2931 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2932 (out @0 @1))))
2933 /* MIN (X, 5) == 0 -> X == 0
2934 MIN (X, 5) == 7 -> false */
2935 (for cmp (eq ne)
2936 (simplify
2937 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2938 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2939 TYPE_SIGN (TREE_TYPE (@0))))
2940 { constant_boolean_node (cmp == NE_EXPR, type); }
2941 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2942 TYPE_SIGN (TREE_TYPE (@0))))
2943 (cmp @0 @2)))))
2944 (for cmp (eq ne)
2945 (simplify
2946 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2947 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2948 TYPE_SIGN (TREE_TYPE (@0))))
2949 { constant_boolean_node (cmp == NE_EXPR, type); }
2950 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2951 TYPE_SIGN (TREE_TYPE (@0))))
2952 (cmp @0 @2)))))
2953 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2954 (for minmax (min min max max min min max max )
2955 cmp (lt le gt ge gt ge lt le )
2956 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2957 (simplify
2958 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2959 (comb (cmp @0 @2) (cmp @1 @2))))
2960
2961 /* X <= MAX(X, Y) -> true
2962 X > MAX(X, Y) -> false
2963 X >= MIN(X, Y) -> true
2964 X < MIN(X, Y) -> false */
2965 (for minmax (min min max max )
2966 cmp (ge lt le gt )
2967 (simplify
2968 (cmp @0 (minmax:c @0 @1))
2969 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
2970
2971 /* Undo fancy way of writing max/min or other ?: expressions,
2972 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2973 People normally use ?: and that is what we actually try to optimize. */
2974 (for cmp (simple_comparison)
2975 (simplify
2976 (minus @0 (bit_and:c (minus @0 @1)
2977 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2978 (if (INTEGRAL_TYPE_P (type)
2979 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2980 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2981 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2982 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2983 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2984 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2985 (cond (cmp @2 @3) @1 @0)))
2986 (simplify
2987 (plus:c @0 (bit_and:c (minus @1 @0)
2988 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2989 (if (INTEGRAL_TYPE_P (type)
2990 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2991 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2992 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2993 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2994 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2995 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2996 (cond (cmp @2 @3) @1 @0)))
2997 /* Similarly with ^ instead of - though in that case with :c. */
2998 (simplify
2999 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3000 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3001 (if (INTEGRAL_TYPE_P (type)
3002 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3003 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3004 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3005 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3006 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3007 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3008 (cond (cmp @2 @3) @1 @0))))
3009
3010 /* Simplifications of shift and rotates. */
3011
3012 (for rotate (lrotate rrotate)
3013 (simplify
3014 (rotate integer_all_onesp@0 @1)
3015 @0))
3016
3017 /* Optimize -1 >> x for arithmetic right shifts. */
3018 (simplify
3019 (rshift integer_all_onesp@0 @1)
3020 (if (!TYPE_UNSIGNED (type))
3021 @0))
3022
3023 /* Optimize (x >> c) << c into x & (-1<<c). */
3024 (simplify
3025 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3026 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3027 /* It doesn't matter if the right shift is arithmetic or logical. */
3028 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3029
3030 (simplify
3031 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3032 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3033 /* Allow intermediate conversion to integral type with whatever sign, as
3034 long as the low TYPE_PRECISION (type)
3035 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3036 && INTEGRAL_TYPE_P (type)
3037 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3038 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3039 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3040 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3041 || wi::geu_p (wi::to_wide (@1),
3042 TYPE_PRECISION (type)
3043 - TYPE_PRECISION (TREE_TYPE (@2)))))
3044 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3045
3046 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3047 types. */
3048 (simplify
3049 (rshift (lshift @0 INTEGER_CST@1) @1)
3050 (if (TYPE_UNSIGNED (type)
3051 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3052 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3053
3054 /* Optimize x >> x into 0 */
3055 (simplify
3056 (rshift @0 @0)
3057 { build_zero_cst (type); })
3058
3059 (for shiftrotate (lrotate rrotate lshift rshift)
3060 (simplify
3061 (shiftrotate @0 integer_zerop)
3062 (non_lvalue @0))
3063 (simplify
3064 (shiftrotate integer_zerop@0 @1)
3065 @0)
3066 /* Prefer vector1 << scalar to vector1 << vector2
3067 if vector2 is uniform. */
3068 (for vec (VECTOR_CST CONSTRUCTOR)
3069 (simplify
3070 (shiftrotate @0 vec@1)
3071 (with { tree tem = uniform_vector_p (@1); }
3072 (if (tem)
3073 (shiftrotate @0 { tem; }))))))
3074
3075 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3076 Y is 0. Similarly for X >> Y. */
3077 #if GIMPLE
3078 (for shift (lshift rshift)
3079 (simplify
3080 (shift @0 SSA_NAME@1)
3081 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3082 (with {
3083 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3084 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3085 }
3086 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3087 @0)))))
3088 #endif
3089
3090 /* Rewrite an LROTATE_EXPR by a constant into an
3091 RROTATE_EXPR by a new constant. */
3092 (simplify
3093 (lrotate @0 INTEGER_CST@1)
3094 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3095 build_int_cst (TREE_TYPE (@1),
3096 element_precision (type)), @1); }))
3097
3098 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3099 (for op (lrotate rrotate rshift lshift)
3100 (simplify
3101 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3102 (with { unsigned int prec = element_precision (type); }
3103 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3104 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3105 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3106 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3107 (with { unsigned int low = (tree_to_uhwi (@1)
3108 + tree_to_uhwi (@2)); }
3109 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3110 being well defined. */
3111 (if (low >= prec)
3112 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3113 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3114 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3115 { build_zero_cst (type); }
3116 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3117 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3118
3119
3120 /* ((1 << A) & 1) != 0 -> A == 0
3121 ((1 << A) & 1) == 0 -> A != 0 */
3122 (for cmp (ne eq)
3123 icmp (eq ne)
3124 (simplify
3125 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
3126 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
3127
3128 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3129 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3130 if CST2 != 0. */
3131 (for cmp (ne eq)
3132 (simplify
3133 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3134 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3135 (if (cand < 0
3136 || (!integer_zerop (@2)
3137 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3138 { constant_boolean_node (cmp == NE_EXPR, type); }
3139 (if (!integer_zerop (@2)
3140 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3141 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3142
3143 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3144 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3145 if the new mask might be further optimized. */
3146 (for shift (lshift rshift)
3147 (simplify
3148 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3149 INTEGER_CST@2)
3150 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3151 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3152 && tree_fits_uhwi_p (@1)
3153 && tree_to_uhwi (@1) > 0
3154 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3155 (with
3156 {
3157 unsigned int shiftc = tree_to_uhwi (@1);
3158 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3159 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3160 tree shift_type = TREE_TYPE (@3);
3161 unsigned int prec;
3162
3163 if (shift == LSHIFT_EXPR)
3164 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3165 else if (shift == RSHIFT_EXPR
3166 && type_has_mode_precision_p (shift_type))
3167 {
3168 prec = TYPE_PRECISION (TREE_TYPE (@3));
3169 tree arg00 = @0;
3170 /* See if more bits can be proven as zero because of
3171 zero extension. */
3172 if (@3 != @0
3173 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3174 {
3175 tree inner_type = TREE_TYPE (@0);
3176 if (type_has_mode_precision_p (inner_type)
3177 && TYPE_PRECISION (inner_type) < prec)
3178 {
3179 prec = TYPE_PRECISION (inner_type);
3180 /* See if we can shorten the right shift. */
3181 if (shiftc < prec)
3182 shift_type = inner_type;
3183 /* Otherwise X >> C1 is all zeros, so we'll optimize
3184 it into (X, 0) later on by making sure zerobits
3185 is all ones. */
3186 }
3187 }
3188 zerobits = HOST_WIDE_INT_M1U;
3189 if (shiftc < prec)
3190 {
3191 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3192 zerobits <<= prec - shiftc;
3193 }
3194 /* For arithmetic shift if sign bit could be set, zerobits
3195 can contain actually sign bits, so no transformation is
3196 possible, unless MASK masks them all away. In that
3197 case the shift needs to be converted into logical shift. */
3198 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3199 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3200 {
3201 if ((mask & zerobits) == 0)
3202 shift_type = unsigned_type_for (TREE_TYPE (@3));
3203 else
3204 zerobits = 0;
3205 }
3206 }
3207 }
3208 /* ((X << 16) & 0xff00) is (X, 0). */
3209 (if ((mask & zerobits) == mask)
3210 { build_int_cst (type, 0); }
3211 (with { newmask = mask | zerobits; }
3212 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3213 (with
3214 {
3215 /* Only do the transformation if NEWMASK is some integer
3216 mode's mask. */
3217 for (prec = BITS_PER_UNIT;
3218 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3219 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3220 break;
3221 }
3222 (if (prec < HOST_BITS_PER_WIDE_INT
3223 || newmask == HOST_WIDE_INT_M1U)
3224 (with
3225 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3226 (if (!tree_int_cst_equal (newmaskt, @2))
3227 (if (shift_type != TREE_TYPE (@3))
3228 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3229 (bit_and @4 { newmaskt; })))))))))))))
3230
3231 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3232 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3233 (for shift (lshift rshift)
3234 (for bit_op (bit_and bit_xor bit_ior)
3235 (simplify
3236 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3237 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3238 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3239 (if (mask)
3240 (bit_op (shift (convert @0) @1) { mask; })))))))
3241
3242 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3243 (simplify
3244 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3245 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3246 && (element_precision (TREE_TYPE (@0))
3247 <= element_precision (TREE_TYPE (@1))
3248 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3249 (with
3250 { tree shift_type = TREE_TYPE (@0); }
3251 (convert (rshift (convert:shift_type @1) @2)))))
3252
3253 /* ~(~X >>r Y) -> X >>r Y
3254 ~(~X <<r Y) -> X <<r Y */
3255 (for rotate (lrotate rrotate)
3256 (simplify
3257 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3258 (if ((element_precision (TREE_TYPE (@0))
3259 <= element_precision (TREE_TYPE (@1))
3260 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3261 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3262 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3263 (with
3264 { tree rotate_type = TREE_TYPE (@0); }
3265 (convert (rotate (convert:rotate_type @1) @2))))))
3266
3267 /* Simplifications of conversions. */
3268
3269 /* Basic strip-useless-type-conversions / strip_nops. */
3270 (for cvt (convert view_convert float fix_trunc)
3271 (simplify
3272 (cvt @0)
3273 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3274 || (GENERIC && type == TREE_TYPE (@0)))
3275 @0)))
3276
3277 /* Contract view-conversions. */
3278 (simplify
3279 (view_convert (view_convert @0))
3280 (view_convert @0))
3281
3282 /* For integral conversions with the same precision or pointer
3283 conversions use a NOP_EXPR instead. */
3284 (simplify
3285 (view_convert @0)
3286 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3287 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3288 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3289 (convert @0)))
3290
3291 /* Strip inner integral conversions that do not change precision or size, or
3292 zero-extend while keeping the same size (for bool-to-char). */
3293 (simplify
3294 (view_convert (convert@0 @1))
3295 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3296 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3297 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3298 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3299 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3300 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3301 (view_convert @1)))
3302
3303 /* Simplify a view-converted empty constructor. */
3304 (simplify
3305 (view_convert CONSTRUCTOR@0)
3306 (if (TREE_CODE (@0) != SSA_NAME
3307 && CONSTRUCTOR_NELTS (@0) == 0)
3308 { build_zero_cst (type); }))
3309
3310 /* Re-association barriers around constants and other re-association
3311 barriers can be removed. */
3312 (simplify
3313 (paren CONSTANT_CLASS_P@0)
3314 @0)
3315 (simplify
3316 (paren (paren@1 @0))
3317 @1)
3318
3319 /* Handle cases of two conversions in a row. */
3320 (for ocvt (convert float fix_trunc)
3321 (for icvt (convert float)
3322 (simplify
3323 (ocvt (icvt@1 @0))
3324 (with
3325 {
3326 tree inside_type = TREE_TYPE (@0);
3327 tree inter_type = TREE_TYPE (@1);
3328 int inside_int = INTEGRAL_TYPE_P (inside_type);
3329 int inside_ptr = POINTER_TYPE_P (inside_type);
3330 int inside_float = FLOAT_TYPE_P (inside_type);
3331 int inside_vec = VECTOR_TYPE_P (inside_type);
3332 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3333 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3334 int inter_int = INTEGRAL_TYPE_P (inter_type);
3335 int inter_ptr = POINTER_TYPE_P (inter_type);
3336 int inter_float = FLOAT_TYPE_P (inter_type);
3337 int inter_vec = VECTOR_TYPE_P (inter_type);
3338 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3339 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3340 int final_int = INTEGRAL_TYPE_P (type);
3341 int final_ptr = POINTER_TYPE_P (type);
3342 int final_float = FLOAT_TYPE_P (type);
3343 int final_vec = VECTOR_TYPE_P (type);
3344 unsigned int final_prec = TYPE_PRECISION (type);
3345 int final_unsignedp = TYPE_UNSIGNED (type);
3346 }
3347 (switch
3348 /* In addition to the cases of two conversions in a row
3349 handled below, if we are converting something to its own
3350 type via an object of identical or wider precision, neither
3351 conversion is needed. */
3352 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3353 || (GENERIC
3354 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3355 && (((inter_int || inter_ptr) && final_int)
3356 || (inter_float && final_float))
3357 && inter_prec >= final_prec)
3358 (ocvt @0))
3359
3360 /* Likewise, if the intermediate and initial types are either both
3361 float or both integer, we don't need the middle conversion if the
3362 former is wider than the latter and doesn't change the signedness
3363 (for integers). Avoid this if the final type is a pointer since
3364 then we sometimes need the middle conversion. */
3365 (if (((inter_int && inside_int) || (inter_float && inside_float))
3366 && (final_int || final_float)
3367 && inter_prec >= inside_prec
3368 && (inter_float || inter_unsignedp == inside_unsignedp))
3369 (ocvt @0))
3370
3371 /* If we have a sign-extension of a zero-extended value, we can
3372 replace that by a single zero-extension. Likewise if the
3373 final conversion does not change precision we can drop the
3374 intermediate conversion. */
3375 (if (inside_int && inter_int && final_int
3376 && ((inside_prec < inter_prec && inter_prec < final_prec
3377 && inside_unsignedp && !inter_unsignedp)
3378 || final_prec == inter_prec))
3379 (ocvt @0))
3380
3381 /* Two conversions in a row are not needed unless:
3382 - some conversion is floating-point (overstrict for now), or
3383 - some conversion is a vector (overstrict for now), or
3384 - the intermediate type is narrower than both initial and
3385 final, or
3386 - the intermediate type and innermost type differ in signedness,
3387 and the outermost type is wider than the intermediate, or
3388 - the initial type is a pointer type and the precisions of the
3389 intermediate and final types differ, or
3390 - the final type is a pointer type and the precisions of the
3391 initial and intermediate types differ. */
3392 (if (! inside_float && ! inter_float && ! final_float
3393 && ! inside_vec && ! inter_vec && ! final_vec
3394 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3395 && ! (inside_int && inter_int
3396 && inter_unsignedp != inside_unsignedp
3397 && inter_prec < final_prec)
3398 && ((inter_unsignedp && inter_prec > inside_prec)
3399 == (final_unsignedp && final_prec > inter_prec))
3400 && ! (inside_ptr && inter_prec != final_prec)
3401 && ! (final_ptr && inside_prec != inter_prec))
3402 (ocvt @0))
3403
3404 /* A truncation to an unsigned type (a zero-extension) should be
3405 canonicalized as bitwise and of a mask. */
3406 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3407 && final_int && inter_int && inside_int
3408 && final_prec == inside_prec
3409 && final_prec > inter_prec
3410 && inter_unsignedp)
3411 (convert (bit_and @0 { wide_int_to_tree
3412 (inside_type,
3413 wi::mask (inter_prec, false,
3414 TYPE_PRECISION (inside_type))); })))
3415
3416 /* If we are converting an integer to a floating-point that can
3417 represent it exactly and back to an integer, we can skip the
3418 floating-point conversion. */
3419 (if (GIMPLE /* PR66211 */
3420 && inside_int && inter_float && final_int &&
3421 (unsigned) significand_size (TYPE_MODE (inter_type))
3422 >= inside_prec - !inside_unsignedp)
3423 (convert @0)))))))
3424
3425 /* If we have a narrowing conversion to an integral type that is fed by a
3426 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3427 masks off bits outside the final type (and nothing else). */
3428 (simplify
3429 (convert (bit_and @0 INTEGER_CST@1))
3430 (if (INTEGRAL_TYPE_P (type)
3431 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3432 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3433 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3434 TYPE_PRECISION (type)), 0))
3435 (convert @0)))
3436
3437
3438 /* (X /[ex] A) * A -> X. */
3439 (simplify
3440 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3441 (convert @0))
3442
3443 /* Simplify (A / B) * B + (A % B) -> A. */
3444 (for div (trunc_div ceil_div floor_div round_div)
3445 mod (trunc_mod ceil_mod floor_mod round_mod)
3446 (simplify
3447 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3448 @0))
3449
3450 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3451 (for op (plus minus)
3452 (simplify
3453 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3454 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3455 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3456 (with
3457 {
3458 wi::overflow_type overflow;
3459 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3460 TYPE_SIGN (type), &overflow);
3461 }
3462 (if (types_match (type, TREE_TYPE (@2))
3463 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3464 (op @0 { wide_int_to_tree (type, mul); })
3465 (with { tree utype = unsigned_type_for (type); }
3466 (convert (op (convert:utype @0)
3467 (mult (convert:utype @1) (convert:utype @2))))))))))
3468
3469 /* Canonicalization of binary operations. */
3470
3471 /* Convert X + -C into X - C. */
3472 (simplify
3473 (plus @0 REAL_CST@1)
3474 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3475 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3476 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3477 (minus @0 { tem; })))))
3478
3479 /* Convert x+x into x*2. */
3480 (simplify
3481 (plus @0 @0)
3482 (if (SCALAR_FLOAT_TYPE_P (type))
3483 (mult @0 { build_real (type, dconst2); })
3484 (if (INTEGRAL_TYPE_P (type))
3485 (mult @0 { build_int_cst (type, 2); }))))
3486
3487 /* 0 - X -> -X. */
3488 (simplify
3489 (minus integer_zerop @1)
3490 (negate @1))
3491 (simplify
3492 (pointer_diff integer_zerop @1)
3493 (negate (convert @1)))
3494
3495 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3496 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3497 (-ARG1 + ARG0) reduces to -ARG1. */
3498 (simplify
3499 (minus real_zerop@0 @1)
3500 (if (fold_real_zero_addition_p (type, @0, 0))
3501 (negate @1)))
3502
3503 /* Transform x * -1 into -x. */
3504 (simplify
3505 (mult @0 integer_minus_onep)
3506 (negate @0))
3507
3508 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3509 signed overflow for CST != 0 && CST != -1. */
3510 (simplify
3511 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3512 (if (TREE_CODE (@2) != INTEGER_CST
3513 && single_use (@3)
3514 && !integer_zerop (@1) && !integer_minus_onep (@1))
3515 (mult (mult @0 @2) @1)))
3516
3517 /* True if we can easily extract the real and imaginary parts of a complex
3518 number. */
3519 (match compositional_complex
3520 (convert? (complex @0 @1)))
3521
3522 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3523 (simplify
3524 (complex (realpart @0) (imagpart @0))
3525 @0)
3526 (simplify
3527 (realpart (complex @0 @1))
3528 @0)
3529 (simplify
3530 (imagpart (complex @0 @1))
3531 @1)
3532
3533 /* Sometimes we only care about half of a complex expression. */
3534 (simplify
3535 (realpart (convert?:s (conj:s @0)))
3536 (convert (realpart @0)))
3537 (simplify
3538 (imagpart (convert?:s (conj:s @0)))
3539 (convert (negate (imagpart @0))))
3540 (for part (realpart imagpart)
3541 (for op (plus minus)
3542 (simplify
3543 (part (convert?:s@2 (op:s @0 @1)))
3544 (convert (op (part @0) (part @1))))))
3545 (simplify
3546 (realpart (convert?:s (CEXPI:s @0)))
3547 (convert (COS @0)))
3548 (simplify
3549 (imagpart (convert?:s (CEXPI:s @0)))
3550 (convert (SIN @0)))
3551
3552 /* conj(conj(x)) -> x */
3553 (simplify
3554 (conj (convert? (conj @0)))
3555 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3556 (convert @0)))
3557
3558 /* conj({x,y}) -> {x,-y} */
3559 (simplify
3560 (conj (convert?:s (complex:s @0 @1)))
3561 (with { tree itype = TREE_TYPE (type); }
3562 (complex (convert:itype @0) (negate (convert:itype @1)))))
3563
3564 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3565 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3566 (simplify
3567 (bswap (bswap @0))
3568 @0)
3569 (simplify
3570 (bswap (bit_not (bswap @0)))
3571 (bit_not @0))
3572 (for bitop (bit_xor bit_ior bit_and)
3573 (simplify
3574 (bswap (bitop:c (bswap @0) @1))
3575 (bitop @0 (bswap @1)))))
3576
3577
3578 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3579
3580 /* Simplify constant conditions.
3581 Only optimize constant conditions when the selected branch
3582 has the same type as the COND_EXPR. This avoids optimizing
3583 away "c ? x : throw", where the throw has a void type.
3584 Note that we cannot throw away the fold-const.c variant nor
3585 this one as we depend on doing this transform before possibly
3586 A ? B : B -> B triggers and the fold-const.c one can optimize
3587 0 ? A : B to B even if A has side-effects. Something
3588 genmatch cannot handle. */
3589 (simplify
3590 (cond INTEGER_CST@0 @1 @2)
3591 (if (integer_zerop (@0))
3592 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3593 @2)
3594 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3595 @1)))
3596 (simplify
3597 (vec_cond VECTOR_CST@0 @1 @2)
3598 (if (integer_all_onesp (@0))
3599 @1
3600 (if (integer_zerop (@0))
3601 @2)))
3602
3603 #if GIMPLE
3604 /* Sink unary operations to branches, but only if we do fold both. */
3605 (for op (negate bit_not abs absu)
3606 (simplify
3607 (op (vec_cond:s @0 @1 @2))
3608 (vec_cond @0 (op! @1) (op! @2))))
3609
3610 /* Sink binary operation to branches, but only if we can fold it. */
3611 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3612 rdiv trunc_div ceil_div floor_div round_div
3613 trunc_mod ceil_mod floor_mod round_mod min max)
3614 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
3615 (simplify
3616 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3617 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3618
3619 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
3620 (simplify
3621 (op (vec_cond:s @0 @1 @2) @3)
3622 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3623 (simplify
3624 (op @3 (vec_cond:s @0 @1 @2))
3625 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3626 #endif
3627
3628 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3629 Currently disabled after pass lvec because ARM understands
3630 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
3631 (simplify
3632 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3633 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3634 (vec_cond (bit_and @0 @3) @1 @2)))
3635 (simplify
3636 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3637 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3638 (vec_cond (bit_ior @0 @3) @1 @2)))
3639 (simplify
3640 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3641 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3642 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3643 (simplify
3644 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3645 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3646 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3647
3648 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
3649 (simplify
3650 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3651 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3652 (vec_cond (bit_and @0 @1) @2 @3)))
3653 (simplify
3654 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3655 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3656 (vec_cond (bit_ior @0 @1) @2 @3)))
3657 (simplify
3658 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3659 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3660 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3661 (simplify
3662 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3663 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3664 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3665
3666 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3667 types are compatible. */
3668 (simplify
3669 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3670 (if (VECTOR_BOOLEAN_TYPE_P (type)
3671 && types_match (type, TREE_TYPE (@0)))
3672 (if (integer_zerop (@1) && integer_all_onesp (@2))
3673 (bit_not @0)
3674 (if (integer_all_onesp (@1) && integer_zerop (@2))
3675 @0))))
3676
3677 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3678 be extended. */
3679 /* This pattern implements two kinds simplification:
3680
3681 Case 1)
3682 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3683 1) Conversions are type widening from smaller type.
3684 2) Const c1 equals to c2 after canonicalizing comparison.
3685 3) Comparison has tree code LT, LE, GT or GE.
3686 This specific pattern is needed when (cmp (convert x) c) may not
3687 be simplified by comparison patterns because of multiple uses of
3688 x. It also makes sense here because simplifying across multiple
3689 referred var is always benefitial for complicated cases.
3690
3691 Case 2)
3692 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3693 (for cmp (lt le gt ge eq)
3694 (simplify
3695 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3696 (with
3697 {
3698 tree from_type = TREE_TYPE (@1);
3699 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3700 enum tree_code code = ERROR_MARK;
3701
3702 if (INTEGRAL_TYPE_P (from_type)
3703 && int_fits_type_p (@2, from_type)
3704 && (types_match (c1_type, from_type)
3705 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3706 && (TYPE_UNSIGNED (from_type)
3707 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3708 && (types_match (c2_type, from_type)
3709 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3710 && (TYPE_UNSIGNED (from_type)
3711 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3712 {
3713 if (cmp != EQ_EXPR)
3714 {
3715 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3716 {
3717 /* X <= Y - 1 equals to X < Y. */
3718 if (cmp == LE_EXPR)
3719 code = LT_EXPR;
3720 /* X > Y - 1 equals to X >= Y. */
3721 if (cmp == GT_EXPR)
3722 code = GE_EXPR;
3723 }
3724 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3725 {
3726 /* X < Y + 1 equals to X <= Y. */
3727 if (cmp == LT_EXPR)
3728 code = LE_EXPR;
3729 /* X >= Y + 1 equals to X > Y. */
3730 if (cmp == GE_EXPR)
3731 code = GT_EXPR;
3732 }
3733 if (code != ERROR_MARK
3734 || wi::to_widest (@2) == wi::to_widest (@3))
3735 {
3736 if (cmp == LT_EXPR || cmp == LE_EXPR)
3737 code = MIN_EXPR;
3738 if (cmp == GT_EXPR || cmp == GE_EXPR)
3739 code = MAX_EXPR;
3740 }
3741 }
3742 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3743 else if (int_fits_type_p (@3, from_type))
3744 code = EQ_EXPR;
3745 }
3746 }
3747 (if (code == MAX_EXPR)
3748 (convert (max @1 (convert @2)))
3749 (if (code == MIN_EXPR)
3750 (convert (min @1 (convert @2)))
3751 (if (code == EQ_EXPR)
3752 (convert (cond (eq @1 (convert @3))
3753 (convert:from_type @3) (convert:from_type @2)))))))))
3754
3755 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3756
3757 1) OP is PLUS or MINUS.
3758 2) CMP is LT, LE, GT or GE.
3759 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3760
3761 This pattern also handles special cases like:
3762
3763 A) Operand x is a unsigned to signed type conversion and c1 is
3764 integer zero. In this case,
3765 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3766 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3767 B) Const c1 may not equal to (C3 op' C2). In this case we also
3768 check equality for (c1+1) and (c1-1) by adjusting comparison
3769 code.
3770
3771 TODO: Though signed type is handled by this pattern, it cannot be
3772 simplified at the moment because C standard requires additional
3773 type promotion. In order to match&simplify it here, the IR needs
3774 to be cleaned up by other optimizers, i.e, VRP. */
3775 (for op (plus minus)
3776 (for cmp (lt le gt ge)
3777 (simplify
3778 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3779 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3780 (if (types_match (from_type, to_type)
3781 /* Check if it is special case A). */
3782 || (TYPE_UNSIGNED (from_type)
3783 && !TYPE_UNSIGNED (to_type)
3784 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3785 && integer_zerop (@1)
3786 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3787 (with
3788 {
3789 wi::overflow_type overflow = wi::OVF_NONE;
3790 enum tree_code code, cmp_code = cmp;
3791 wide_int real_c1;
3792 wide_int c1 = wi::to_wide (@1);
3793 wide_int c2 = wi::to_wide (@2);
3794 wide_int c3 = wi::to_wide (@3);
3795 signop sgn = TYPE_SIGN (from_type);
3796
3797 /* Handle special case A), given x of unsigned type:
3798 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3799 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3800 if (!types_match (from_type, to_type))
3801 {
3802 if (cmp_code == LT_EXPR)
3803 cmp_code = GT_EXPR;
3804 if (cmp_code == GE_EXPR)
3805 cmp_code = LE_EXPR;
3806 c1 = wi::max_value (to_type);
3807 }
3808 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3809 compute (c3 op' c2) and check if it equals to c1 with op' being
3810 the inverted operator of op. Make sure overflow doesn't happen
3811 if it is undefined. */
3812 if (op == PLUS_EXPR)
3813 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3814 else
3815 real_c1 = wi::add (c3, c2, sgn, &overflow);
3816
3817 code = cmp_code;
3818 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3819 {
3820 /* Check if c1 equals to real_c1. Boundary condition is handled
3821 by adjusting comparison operation if necessary. */
3822 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3823 && !overflow)
3824 {
3825 /* X <= Y - 1 equals to X < Y. */
3826 if (cmp_code == LE_EXPR)
3827 code = LT_EXPR;
3828 /* X > Y - 1 equals to X >= Y. */
3829 if (cmp_code == GT_EXPR)
3830 code = GE_EXPR;
3831 }
3832 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3833 && !overflow)
3834 {
3835 /* X < Y + 1 equals to X <= Y. */
3836 if (cmp_code == LT_EXPR)
3837 code = LE_EXPR;
3838 /* X >= Y + 1 equals to X > Y. */
3839 if (cmp_code == GE_EXPR)
3840 code = GT_EXPR;
3841 }
3842 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3843 {
3844 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3845 code = MIN_EXPR;
3846 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3847 code = MAX_EXPR;
3848 }
3849 }
3850 }
3851 (if (code == MAX_EXPR)
3852 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3853 { wide_int_to_tree (from_type, c2); })
3854 (if (code == MIN_EXPR)
3855 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3856 { wide_int_to_tree (from_type, c2); })))))))))
3857
3858 (for cnd (cond vec_cond)
3859 /* A ? B : (A ? X : C) -> A ? B : C. */
3860 (simplify
3861 (cnd @0 (cnd @0 @1 @2) @3)
3862 (cnd @0 @1 @3))
3863 (simplify
3864 (cnd @0 @1 (cnd @0 @2 @3))
3865 (cnd @0 @1 @3))
3866 /* A ? B : (!A ? C : X) -> A ? B : C. */
3867 /* ??? This matches embedded conditions open-coded because genmatch
3868 would generate matching code for conditions in separate stmts only.
3869 The following is still important to merge then and else arm cases
3870 from if-conversion. */
3871 (simplify
3872 (cnd @0 @1 (cnd @2 @3 @4))
3873 (if (inverse_conditions_p (@0, @2))
3874 (cnd @0 @1 @3)))
3875 (simplify
3876 (cnd @0 (cnd @1 @2 @3) @4)
3877 (if (inverse_conditions_p (@0, @1))
3878 (cnd @0 @3 @4)))
3879
3880 /* A ? B : B -> B. */
3881 (simplify
3882 (cnd @0 @1 @1)
3883 @1)
3884
3885 /* !A ? B : C -> A ? C : B. */
3886 (simplify
3887 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3888 (cnd @0 @2 @1)))
3889
3890 /* -(type)!A -> (type)A - 1. */
3891 (simplify
3892 (negate (convert?:s (logical_inverted_value:s @0)))
3893 (if (INTEGRAL_TYPE_P (type)
3894 && TREE_CODE (type) != BOOLEAN_TYPE
3895 && TYPE_PRECISION (type) > 1
3896 && TREE_CODE (@0) == SSA_NAME
3897 && ssa_name_has_boolean_range (@0))
3898 (plus (convert:type @0) { build_all_ones_cst (type); })))
3899
3900 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3901 return all -1 or all 0 results. */
3902 /* ??? We could instead convert all instances of the vec_cond to negate,
3903 but that isn't necessarily a win on its own. */
3904 (simplify
3905 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3906 (if (VECTOR_TYPE_P (type)
3907 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3908 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3909 && (TYPE_MODE (TREE_TYPE (type))
3910 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3911 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3912
3913 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3914 (simplify
3915 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3916 (if (VECTOR_TYPE_P (type)
3917 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3918 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3919 && (TYPE_MODE (TREE_TYPE (type))
3920 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3921 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3922
3923
3924 /* Simplifications of comparisons. */
3925
3926 /* See if we can reduce the magnitude of a constant involved in a
3927 comparison by changing the comparison code. This is a canonicalization
3928 formerly done by maybe_canonicalize_comparison_1. */
3929 (for cmp (le gt)
3930 acmp (lt ge)
3931 (simplify
3932 (cmp @0 uniform_integer_cst_p@1)
3933 (with { tree cst = uniform_integer_cst_p (@1); }
3934 (if (tree_int_cst_sgn (cst) == -1)
3935 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3936 wide_int_to_tree (TREE_TYPE (cst),
3937 wi::to_wide (cst)
3938 + 1)); })))))
3939 (for cmp (ge lt)
3940 acmp (gt le)
3941 (simplify
3942 (cmp @0 uniform_integer_cst_p@1)
3943 (with { tree cst = uniform_integer_cst_p (@1); }
3944 (if (tree_int_cst_sgn (cst) == 1)
3945 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3946 wide_int_to_tree (TREE_TYPE (cst),
3947 wi::to_wide (cst) - 1)); })))))
3948
3949 /* We can simplify a logical negation of a comparison to the
3950 inverted comparison. As we cannot compute an expression
3951 operator using invert_tree_comparison we have to simulate
3952 that with expression code iteration. */
3953 (for cmp (tcc_comparison)
3954 icmp (inverted_tcc_comparison)
3955 ncmp (inverted_tcc_comparison_with_nans)
3956 /* Ideally we'd like to combine the following two patterns
3957 and handle some more cases by using
3958 (logical_inverted_value (cmp @0 @1))
3959 here but for that genmatch would need to "inline" that.
3960 For now implement what forward_propagate_comparison did. */
3961 (simplify
3962 (bit_not (cmp @0 @1))
3963 (if (VECTOR_TYPE_P (type)
3964 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3965 /* Comparison inversion may be impossible for trapping math,
3966 invert_tree_comparison will tell us. But we can't use
3967 a computed operator in the replacement tree thus we have
3968 to play the trick below. */
3969 (with { enum tree_code ic = invert_tree_comparison
3970 (cmp, HONOR_NANS (@0)); }
3971 (if (ic == icmp)
3972 (icmp @0 @1)
3973 (if (ic == ncmp)
3974 (ncmp @0 @1))))))
3975 (simplify
3976 (bit_xor (cmp @0 @1) integer_truep)
3977 (with { enum tree_code ic = invert_tree_comparison
3978 (cmp, HONOR_NANS (@0)); }
3979 (if (ic == icmp)
3980 (icmp @0 @1)
3981 (if (ic == ncmp)
3982 (ncmp @0 @1))))))
3983
3984 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3985 ??? The transformation is valid for the other operators if overflow
3986 is undefined for the type, but performing it here badly interacts
3987 with the transformation in fold_cond_expr_with_comparison which
3988 attempts to synthetize ABS_EXPR. */
3989 (for cmp (eq ne)
3990 (for sub (minus pointer_diff)
3991 (simplify
3992 (cmp (sub@2 @0 @1) integer_zerop)
3993 (if (single_use (@2))
3994 (cmp @0 @1)))))
3995
3996 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3997 signed arithmetic case. That form is created by the compiler
3998 often enough for folding it to be of value. One example is in
3999 computing loop trip counts after Operator Strength Reduction. */
4000 (for cmp (simple_comparison)
4001 scmp (swapped_simple_comparison)
4002 (simplify
4003 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4004 /* Handle unfolded multiplication by zero. */
4005 (if (integer_zerop (@1))
4006 (cmp @1 @2)
4007 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4008 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4009 && single_use (@3))
4010 /* If @1 is negative we swap the sense of the comparison. */
4011 (if (tree_int_cst_sgn (@1) < 0)
4012 (scmp @0 @2)
4013 (cmp @0 @2))))))
4014
4015 /* For integral types with undefined overflow fold
4016 x * C1 == C2 into x == C2 / C1 or false.
4017 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4018 Z / 2^n Z. */
4019 (for cmp (eq ne)
4020 (simplify
4021 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4022 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4023 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4024 && wi::to_wide (@1) != 0)
4025 (with { widest_int quot; }
4026 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4027 TYPE_SIGN (TREE_TYPE (@0)), &quot))
4028 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4029 { constant_boolean_node (cmp == NE_EXPR, type); }))
4030 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4031 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4032 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4033 (cmp @0
4034 {
4035 tree itype = TREE_TYPE (@0);
4036 int p = TYPE_PRECISION (itype);
4037 wide_int m = wi::one (p + 1) << p;
4038 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4039 wide_int i = wide_int::from (wi::mod_inv (a, m),
4040 p, TYPE_SIGN (itype));
4041 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4042 })))))
4043
4044 /* Simplify comparison of something with itself. For IEEE
4045 floating-point, we can only do some of these simplifications. */
4046 (for cmp (eq ge le)
4047 (simplify
4048 (cmp @0 @0)
4049 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4050 || ! HONOR_NANS (@0))
4051 { constant_boolean_node (true, type); }
4052 (if (cmp != EQ_EXPR)
4053 (eq @0 @0)))))
4054 (for cmp (ne gt lt)
4055 (simplify
4056 (cmp @0 @0)
4057 (if (cmp != NE_EXPR
4058 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4059 || ! HONOR_NANS (@0))
4060 { constant_boolean_node (false, type); })))
4061 (for cmp (unle unge uneq)
4062 (simplify
4063 (cmp @0 @0)
4064 { constant_boolean_node (true, type); }))
4065 (for cmp (unlt ungt)
4066 (simplify
4067 (cmp @0 @0)
4068 (unordered @0 @0)))
4069 (simplify
4070 (ltgt @0 @0)
4071 (if (!flag_trapping_math)
4072 { constant_boolean_node (false, type); }))
4073
4074 /* x == ~x -> false */
4075 /* x != ~x -> true */
4076 (for cmp (eq ne)
4077 (simplify
4078 (cmp:c @0 (bit_not @0))
4079 { constant_boolean_node (cmp == NE_EXPR, type); }))
4080
4081 /* Fold ~X op ~Y as Y op X. */
4082 (for cmp (simple_comparison)
4083 (simplify
4084 (cmp (bit_not@2 @0) (bit_not@3 @1))
4085 (if (single_use (@2) && single_use (@3))
4086 (cmp @1 @0))))
4087
4088 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
4089 (for cmp (simple_comparison)
4090 scmp (swapped_simple_comparison)
4091 (simplify
4092 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4093 (if (single_use (@2)
4094 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4095 (scmp @0 (bit_not @1)))))
4096
4097 (for cmp (simple_comparison)
4098 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
4099 (simplify
4100 (cmp (convert@2 @0) (convert? @1))
4101 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4102 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4103 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4104 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4105 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4106 (with
4107 {
4108 tree type1 = TREE_TYPE (@1);
4109 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4110 {
4111 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4112 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4113 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4114 type1 = float_type_node;
4115 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4116 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4117 type1 = double_type_node;
4118 }
4119 tree newtype
4120 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4121 ? TREE_TYPE (@0) : type1);
4122 }
4123 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4124 (cmp (convert:newtype @0) (convert:newtype @1))))))
4125
4126 (simplify
4127 (cmp @0 REAL_CST@1)
4128 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
4129 (switch
4130 /* a CMP (-0) -> a CMP 0 */
4131 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4132 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4133 /* x != NaN is always true, other ops are always false. */
4134 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4135 && ! HONOR_SNANS (@1))
4136 { constant_boolean_node (cmp == NE_EXPR, type); })
4137 /* Fold comparisons against infinity. */
4138 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4139 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4140 (with
4141 {
4142 REAL_VALUE_TYPE max;
4143 enum tree_code code = cmp;
4144 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4145 if (neg)
4146 code = swap_tree_comparison (code);
4147 }
4148 (switch
4149 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4150 (if (code == GT_EXPR
4151 && !(HONOR_NANS (@0) && flag_trapping_math))
4152 { constant_boolean_node (false, type); })
4153 (if (code == LE_EXPR)
4154 /* x <= +Inf is always true, if we don't care about NaNs. */
4155 (if (! HONOR_NANS (@0))
4156 { constant_boolean_node (true, type); }
4157 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4158 an "invalid" exception. */
4159 (if (!flag_trapping_math)
4160 (eq @0 @0))))
4161 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4162 for == this introduces an exception for x a NaN. */
4163 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4164 || code == GE_EXPR)
4165 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4166 (if (neg)
4167 (lt @0 { build_real (TREE_TYPE (@0), max); })
4168 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4169 /* x < +Inf is always equal to x <= DBL_MAX. */
4170 (if (code == LT_EXPR)
4171 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4172 (if (neg)
4173 (ge @0 { build_real (TREE_TYPE (@0), max); })
4174 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4175 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4176 an exception for x a NaN so use an unordered comparison. */
4177 (if (code == NE_EXPR)
4178 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4179 (if (! HONOR_NANS (@0))
4180 (if (neg)
4181 (ge @0 { build_real (TREE_TYPE (@0), max); })
4182 (le @0 { build_real (TREE_TYPE (@0), max); }))
4183 (if (neg)
4184 (unge @0 { build_real (TREE_TYPE (@0), max); })
4185 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4186
4187 /* If this is a comparison of a real constant with a PLUS_EXPR
4188 or a MINUS_EXPR of a real constant, we can convert it into a
4189 comparison with a revised real constant as long as no overflow
4190 occurs when unsafe_math_optimizations are enabled. */
4191 (if (flag_unsafe_math_optimizations)
4192 (for op (plus minus)
4193 (simplify
4194 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4195 (with
4196 {
4197 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4198 TREE_TYPE (@1), @2, @1);
4199 }
4200 (if (tem && !TREE_OVERFLOW (tem))
4201 (cmp @0 { tem; }))))))
4202
4203 /* Likewise, we can simplify a comparison of a real constant with
4204 a MINUS_EXPR whose first operand is also a real constant, i.e.
4205 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4206 floating-point types only if -fassociative-math is set. */
4207 (if (flag_associative_math)
4208 (simplify
4209 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4210 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4211 (if (tem && !TREE_OVERFLOW (tem))
4212 (cmp { tem; } @1)))))
4213
4214 /* Fold comparisons against built-in math functions. */
4215 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4216 (for sq (SQRT)
4217 (simplify
4218 (cmp (sq @0) REAL_CST@1)
4219 (switch
4220 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4221 (switch
4222 /* sqrt(x) < y is always false, if y is negative. */
4223 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4224 { constant_boolean_node (false, type); })
4225 /* sqrt(x) > y is always true, if y is negative and we
4226 don't care about NaNs, i.e. negative values of x. */
4227 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4228 { constant_boolean_node (true, type); })
4229 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4230 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4231 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4232 (switch
4233 /* sqrt(x) < 0 is always false. */
4234 (if (cmp == LT_EXPR)
4235 { constant_boolean_node (false, type); })
4236 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4237 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4238 { constant_boolean_node (true, type); })
4239 /* sqrt(x) <= 0 -> x == 0. */
4240 (if (cmp == LE_EXPR)
4241 (eq @0 @1))
4242 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4243 == or !=. In the last case:
4244
4245 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4246
4247 if x is negative or NaN. Due to -funsafe-math-optimizations,
4248 the results for other x follow from natural arithmetic. */
4249 (cmp @0 @1)))
4250 (if ((cmp == LT_EXPR
4251 || cmp == LE_EXPR
4252 || cmp == GT_EXPR
4253 || cmp == GE_EXPR)
4254 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4255 /* Give up for -frounding-math. */
4256 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4257 (with
4258 {
4259 REAL_VALUE_TYPE c2;
4260 enum tree_code ncmp = cmp;
4261 const real_format *fmt
4262 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4263 real_arithmetic (&c2, MULT_EXPR,
4264 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4265 real_convert (&c2, fmt, &c2);
4266 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4267 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4268 if (!REAL_VALUE_ISINF (c2))
4269 {
4270 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4271 build_real (TREE_TYPE (@0), c2));
4272 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4273 ncmp = ERROR_MARK;
4274 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4275 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4276 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4277 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4278 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4279 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4280 else
4281 {
4282 /* With rounding to even, sqrt of up to 3 different values
4283 gives the same normal result, so in some cases c2 needs
4284 to be adjusted. */
4285 REAL_VALUE_TYPE c2alt, tow;
4286 if (cmp == LT_EXPR || cmp == GE_EXPR)
4287 tow = dconst0;
4288 else
4289 real_inf (&tow);
4290 real_nextafter (&c2alt, fmt, &c2, &tow);
4291 real_convert (&c2alt, fmt, &c2alt);
4292 if (REAL_VALUE_ISINF (c2alt))
4293 ncmp = ERROR_MARK;
4294 else
4295 {
4296 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4297 build_real (TREE_TYPE (@0), c2alt));
4298 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4299 ncmp = ERROR_MARK;
4300 else if (real_equal (&TREE_REAL_CST (c3),
4301 &TREE_REAL_CST (@1)))
4302 c2 = c2alt;
4303 }
4304 }
4305 }
4306 }
4307 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4308 (if (REAL_VALUE_ISINF (c2))
4309 /* sqrt(x) > y is x == +Inf, when y is very large. */
4310 (if (HONOR_INFINITIES (@0))
4311 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4312 { constant_boolean_node (false, type); })
4313 /* sqrt(x) > c is the same as x > c*c. */
4314 (if (ncmp != ERROR_MARK)
4315 (if (ncmp == GE_EXPR)
4316 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4317 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4318 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4319 (if (REAL_VALUE_ISINF (c2))
4320 (switch
4321 /* sqrt(x) < y is always true, when y is a very large
4322 value and we don't care about NaNs or Infinities. */
4323 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4324 { constant_boolean_node (true, type); })
4325 /* sqrt(x) < y is x != +Inf when y is very large and we
4326 don't care about NaNs. */
4327 (if (! HONOR_NANS (@0))
4328 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4329 /* sqrt(x) < y is x >= 0 when y is very large and we
4330 don't care about Infinities. */
4331 (if (! HONOR_INFINITIES (@0))
4332 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4333 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4334 (if (GENERIC)
4335 (truth_andif
4336 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4337 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4338 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4339 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4340 (if (ncmp == LT_EXPR)
4341 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4342 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4343 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4344 (if (ncmp != ERROR_MARK && GENERIC)
4345 (if (ncmp == LT_EXPR)
4346 (truth_andif
4347 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4348 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4349 (truth_andif
4350 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4351 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4352 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4353 (simplify
4354 (cmp (sq @0) (sq @1))
4355 (if (! HONOR_NANS (@0))
4356 (cmp @0 @1))))))
4357
4358 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4359 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4360 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4361 (simplify
4362 (cmp (float@0 @1) (float @2))
4363 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4364 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4365 (with
4366 {
4367 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4368 tree type1 = TREE_TYPE (@1);
4369 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4370 tree type2 = TREE_TYPE (@2);
4371 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4372 }
4373 (if (fmt.can_represent_integral_type_p (type1)
4374 && fmt.can_represent_integral_type_p (type2))
4375 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4376 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4377 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4378 && type1_signed_p >= type2_signed_p)
4379 (icmp @1 (convert @2))
4380 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4381 && type1_signed_p <= type2_signed_p)
4382 (icmp (convert:type2 @1) @2)
4383 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4384 && type1_signed_p == type2_signed_p)
4385 (icmp @1 @2))))))))))
4386
4387 /* Optimize various special cases of (FTYPE) N CMP CST. */
4388 (for cmp (lt le eq ne ge gt)
4389 icmp (le le eq ne ge ge)
4390 (simplify
4391 (cmp (float @0) REAL_CST@1)
4392 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4393 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4394 (with
4395 {
4396 tree itype = TREE_TYPE (@0);
4397 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4398 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4399 /* Be careful to preserve any potential exceptions due to
4400 NaNs. qNaNs are ok in == or != context.
4401 TODO: relax under -fno-trapping-math or
4402 -fno-signaling-nans. */
4403 bool exception_p
4404 = real_isnan (cst) && (cst->signalling
4405 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4406 }
4407 /* TODO: allow non-fitting itype and SNaNs when
4408 -fno-trapping-math. */
4409 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4410 (with
4411 {
4412 signop isign = TYPE_SIGN (itype);
4413 REAL_VALUE_TYPE imin, imax;
4414 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4415 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4416
4417 REAL_VALUE_TYPE icst;
4418 if (cmp == GT_EXPR || cmp == GE_EXPR)
4419 real_ceil (&icst, fmt, cst);
4420 else if (cmp == LT_EXPR || cmp == LE_EXPR)
4421 real_floor (&icst, fmt, cst);
4422 else
4423 real_trunc (&icst, fmt, cst);
4424
4425 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4426
4427 bool overflow_p = false;
4428 wide_int icst_val
4429 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4430 }
4431 (switch
4432 /* Optimize cases when CST is outside of ITYPE's range. */
4433 (if (real_compare (LT_EXPR, cst, &imin))
4434 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4435 type); })
4436 (if (real_compare (GT_EXPR, cst, &imax))
4437 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4438 type); })
4439 /* Remove cast if CST is an integer representable by ITYPE. */
4440 (if (cst_int_p)
4441 (cmp @0 { gcc_assert (!overflow_p);
4442 wide_int_to_tree (itype, icst_val); })
4443 )
4444 /* When CST is fractional, optimize
4445 (FTYPE) N == CST -> 0
4446 (FTYPE) N != CST -> 1. */
4447 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4448 { constant_boolean_node (cmp == NE_EXPR, type); })
4449 /* Otherwise replace with sensible integer constant. */
4450 (with
4451 {
4452 gcc_checking_assert (!overflow_p);
4453 }
4454 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4455
4456 /* Fold A /[ex] B CMP C to A CMP B * C. */
4457 (for cmp (eq ne)
4458 (simplify
4459 (cmp (exact_div @0 @1) INTEGER_CST@2)
4460 (if (!integer_zerop (@1))
4461 (if (wi::to_wide (@2) == 0)
4462 (cmp @0 @2)
4463 (if (TREE_CODE (@1) == INTEGER_CST)
4464 (with
4465 {
4466 wi::overflow_type ovf;
4467 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4468 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4469 }
4470 (if (ovf)
4471 { constant_boolean_node (cmp == NE_EXPR, type); }
4472 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4473 (for cmp (lt le gt ge)
4474 (simplify
4475 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4476 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4477 (with
4478 {
4479 wi::overflow_type ovf;
4480 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4481 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4482 }
4483 (if (ovf)
4484 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4485 TYPE_SIGN (TREE_TYPE (@2)))
4486 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4487 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4488
4489 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4490
4491 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4492 For large C (more than min/B+2^size), this is also true, with the
4493 multiplication computed modulo 2^size.
4494 For intermediate C, this just tests the sign of A. */
4495 (for cmp (lt le gt ge)
4496 cmp2 (ge ge lt lt)
4497 (simplify
4498 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4499 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4500 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4501 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4502 (with
4503 {
4504 tree utype = TREE_TYPE (@2);
4505 wide_int denom = wi::to_wide (@1);
4506 wide_int right = wi::to_wide (@2);
4507 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4508 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4509 bool small = wi::leu_p (right, smax);
4510 bool large = wi::geu_p (right, smin);
4511 }
4512 (if (small || large)
4513 (cmp (convert:utype @0) (mult @2 (convert @1)))
4514 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4515
4516 /* Unordered tests if either argument is a NaN. */
4517 (simplify
4518 (bit_ior (unordered @0 @0) (unordered @1 @1))
4519 (if (types_match (@0, @1))
4520 (unordered @0 @1)))
4521 (simplify
4522 (bit_and (ordered @0 @0) (ordered @1 @1))
4523 (if (types_match (@0, @1))
4524 (ordered @0 @1)))
4525 (simplify
4526 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4527 @2)
4528 (simplify
4529 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4530 @2)
4531
4532 /* Simple range test simplifications. */
4533 /* A < B || A >= B -> true. */
4534 (for test1 (lt le le le ne ge)
4535 test2 (ge gt ge ne eq ne)
4536 (simplify
4537 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4538 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4539 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4540 { constant_boolean_node (true, type); })))
4541 /* A < B && A >= B -> false. */
4542 (for test1 (lt lt lt le ne eq)
4543 test2 (ge gt eq gt eq gt)
4544 (simplify
4545 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4546 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4547 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4548 { constant_boolean_node (false, type); })))
4549
4550 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4551 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4552
4553 Note that comparisons
4554 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4555 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
4556 will be canonicalized to above so there's no need to
4557 consider them here.
4558 */
4559
4560 (for cmp (le gt)
4561 eqcmp (eq ne)
4562 (simplify
4563 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4564 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4565 (with
4566 {
4567 tree ty = TREE_TYPE (@0);
4568 unsigned prec = TYPE_PRECISION (ty);
4569 wide_int mask = wi::to_wide (@2, prec);
4570 wide_int rhs = wi::to_wide (@3, prec);
4571 signop sgn = TYPE_SIGN (ty);
4572 }
4573 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4574 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4575 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4576 { build_zero_cst (ty); }))))))
4577
4578 /* -A CMP -B -> B CMP A. */
4579 (for cmp (tcc_comparison)
4580 scmp (swapped_tcc_comparison)
4581 (simplify
4582 (cmp (negate @0) (negate @1))
4583 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4584 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4585 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4586 (scmp @0 @1)))
4587 (simplify
4588 (cmp (negate @0) CONSTANT_CLASS_P@1)
4589 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4590 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4591 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4592 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4593 (if (tem && !TREE_OVERFLOW (tem))
4594 (scmp @0 { tem; }))))))
4595
4596 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
4597 (for op (eq ne)
4598 (simplify
4599 (op (abs @0) zerop@1)
4600 (op @0 @1)))
4601
4602 /* From fold_sign_changed_comparison and fold_widened_comparison.
4603 FIXME: the lack of symmetry is disturbing. */
4604 (for cmp (simple_comparison)
4605 (simplify
4606 (cmp (convert@0 @00) (convert?@1 @10))
4607 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4608 /* Disable this optimization if we're casting a function pointer
4609 type on targets that require function pointer canonicalization. */
4610 && !(targetm.have_canonicalize_funcptr_for_compare ()
4611 && ((POINTER_TYPE_P (TREE_TYPE (@00))
4612 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4613 || (POINTER_TYPE_P (TREE_TYPE (@10))
4614 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4615 && single_use (@0))
4616 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4617 && (TREE_CODE (@10) == INTEGER_CST
4618 || @1 != @10)
4619 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4620 || cmp == NE_EXPR
4621 || cmp == EQ_EXPR)
4622 && !POINTER_TYPE_P (TREE_TYPE (@00)))
4623 /* ??? The special-casing of INTEGER_CST conversion was in the original
4624 code and here to avoid a spurious overflow flag on the resulting
4625 constant which fold_convert produces. */
4626 (if (TREE_CODE (@1) == INTEGER_CST)
4627 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4628 TREE_OVERFLOW (@1)); })
4629 (cmp @00 (convert @1)))
4630
4631 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4632 /* If possible, express the comparison in the shorter mode. */
4633 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4634 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4635 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4636 && TYPE_UNSIGNED (TREE_TYPE (@00))))
4637 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4638 || ((TYPE_PRECISION (TREE_TYPE (@00))
4639 >= TYPE_PRECISION (TREE_TYPE (@10)))
4640 && (TYPE_UNSIGNED (TREE_TYPE (@00))
4641 == TYPE_UNSIGNED (TREE_TYPE (@10))))
4642 || (TREE_CODE (@10) == INTEGER_CST
4643 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4644 && int_fits_type_p (@10, TREE_TYPE (@00)))))
4645 (cmp @00 (convert @10))
4646 (if (TREE_CODE (@10) == INTEGER_CST
4647 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4648 && !int_fits_type_p (@10, TREE_TYPE (@00)))
4649 (with
4650 {
4651 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4652 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4653 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4654 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4655 }
4656 (if (above || below)
4657 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4658 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4659 (if (cmp == LT_EXPR || cmp == LE_EXPR)
4660 { constant_boolean_node (above ? true : false, type); }
4661 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4662 { constant_boolean_node (above ? false : true, type); }))))))))))))
4663
4664 (for cmp (eq ne)
4665 (simplify
4666 /* SSA names are canonicalized to 2nd place. */
4667 (cmp addr@0 SSA_NAME@1)
4668 (with
4669 { poly_int64 off; tree base; }
4670 /* A local variable can never be pointed to by
4671 the default SSA name of an incoming parameter. */
4672 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4673 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
4674 && (base = get_base_address (TREE_OPERAND (@0, 0)))
4675 && TREE_CODE (base) == VAR_DECL
4676 && auto_var_in_fn_p (base, current_function_decl))
4677 (if (cmp == NE_EXPR)
4678 { constant_boolean_node (true, type); }
4679 { constant_boolean_node (false, type); })
4680 /* If the address is based on @1 decide using the offset. */
4681 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4682 && TREE_CODE (base) == MEM_REF
4683 && TREE_OPERAND (base, 0) == @1)
4684 (with { off += mem_ref_offset (base).force_shwi (); }
4685 (if (known_ne (off, 0))
4686 { constant_boolean_node (cmp == NE_EXPR, type); }
4687 (if (known_eq (off, 0))
4688 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
4689
4690 /* Equality compare simplifications from fold_binary */
4691 (for cmp (eq ne)
4692
4693 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4694 Similarly for NE_EXPR. */
4695 (simplify
4696 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4697 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4698 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4699 { constant_boolean_node (cmp == NE_EXPR, type); }))
4700
4701 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
4702 (simplify
4703 (cmp (bit_xor @0 @1) integer_zerop)
4704 (cmp @0 @1))
4705
4706 /* (X ^ Y) == Y becomes X == 0.
4707 Likewise (X ^ Y) == X becomes Y == 0. */
4708 (simplify
4709 (cmp:c (bit_xor:c @0 @1) @0)
4710 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4711
4712 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
4713 (simplify
4714 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4715 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4716 (cmp @0 (bit_xor @1 (convert @2)))))
4717
4718 (simplify
4719 (cmp (convert? addr@0) integer_zerop)
4720 (if (tree_single_nonzero_warnv_p (@0, NULL))
4721 { constant_boolean_node (cmp == NE_EXPR, type); }))
4722
4723 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
4724 (simplify
4725 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
4726 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
4727
4728 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
4729 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
4730 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
4731 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
4732 (for cmp (eq ne)
4733 ncmp (ge lt)
4734 (for sgncmp (ge lt)
4735 (simplify
4736 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
4737 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4738 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4739 && types_match (@0, @1))
4740 (ncmp (bit_xor @0 @1) @2)))))
4741 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
4742 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
4743 (for cmp (eq ne)
4744 ncmp (lt ge)
4745 (simplify
4746 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
4747 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4748 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4749 && types_match (@0, @1))
4750 (ncmp (bit_xor @0 @1) @2))))
4751
4752 /* If we have (A & C) == C where C is a power of 2, convert this into
4753 (A & C) != 0. Similarly for NE_EXPR. */
4754 (for cmp (eq ne)
4755 icmp (ne eq)
4756 (simplify
4757 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4758 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4759
4760 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4761 convert this into a shift followed by ANDing with D. */
4762 (simplify
4763 (cond
4764 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4765 INTEGER_CST@2 integer_zerop)
4766 (if (integer_pow2p (@2))
4767 (with {
4768 int shift = (wi::exact_log2 (wi::to_wide (@2))
4769 - wi::exact_log2 (wi::to_wide (@1)));
4770 }
4771 (if (shift > 0)
4772 (bit_and
4773 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4774 (bit_and
4775 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4776 @2)))))
4777
4778 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4779 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
4780 (for cmp (eq ne)
4781 ncmp (ge lt)
4782 (simplify
4783 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4784 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4785 && type_has_mode_precision_p (TREE_TYPE (@0))
4786 && element_precision (@2) >= element_precision (@0)
4787 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4788 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4789 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4790
4791 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4792 this into a right shift or sign extension followed by ANDing with C. */
4793 (simplify
4794 (cond
4795 (lt @0 integer_zerop)
4796 INTEGER_CST@1 integer_zerop)
4797 (if (integer_pow2p (@1)
4798 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4799 (with {
4800 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4801 }
4802 (if (shift >= 0)
4803 (bit_and
4804 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4805 @1)
4806 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4807 sign extension followed by AND with C will achieve the effect. */
4808 (bit_and (convert @0) @1)))))
4809
4810 /* When the addresses are not directly of decls compare base and offset.
4811 This implements some remaining parts of fold_comparison address
4812 comparisons but still no complete part of it. Still it is good
4813 enough to make fold_stmt not regress when not dispatching to fold_binary. */
4814 (for cmp (simple_comparison)
4815 (simplify
4816 (cmp (convert1?@2 addr@0) (convert2? addr@1))
4817 (with
4818 {
4819 poly_int64 off0, off1;
4820 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4821 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4822 if (base0 && TREE_CODE (base0) == MEM_REF)
4823 {
4824 off0 += mem_ref_offset (base0).force_shwi ();
4825 base0 = TREE_OPERAND (base0, 0);
4826 }
4827 if (base1 && TREE_CODE (base1) == MEM_REF)
4828 {
4829 off1 += mem_ref_offset (base1).force_shwi ();
4830 base1 = TREE_OPERAND (base1, 0);
4831 }
4832 }
4833 (if (base0 && base1)
4834 (with
4835 {
4836 int equal = 2;
4837 /* Punt in GENERIC on variables with value expressions;
4838 the value expressions might point to fields/elements
4839 of other vars etc. */
4840 if (GENERIC
4841 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4842 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4843 ;
4844 else if (decl_in_symtab_p (base0)
4845 && decl_in_symtab_p (base1))
4846 equal = symtab_node::get_create (base0)
4847 ->equal_address_to (symtab_node::get_create (base1));
4848 else if ((DECL_P (base0)
4849 || TREE_CODE (base0) == SSA_NAME
4850 || TREE_CODE (base0) == STRING_CST)
4851 && (DECL_P (base1)
4852 || TREE_CODE (base1) == SSA_NAME
4853 || TREE_CODE (base1) == STRING_CST))
4854 equal = (base0 == base1);
4855 if (equal == 0)
4856 {
4857 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4858 off0.is_constant (&ioff0);
4859 off1.is_constant (&ioff1);
4860 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4861 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4862 || (TREE_CODE (base0) == STRING_CST
4863 && TREE_CODE (base1) == STRING_CST
4864 && ioff0 >= 0 && ioff1 >= 0
4865 && ioff0 < TREE_STRING_LENGTH (base0)
4866 && ioff1 < TREE_STRING_LENGTH (base1)
4867 /* This is a too conservative test that the STRING_CSTs
4868 will not end up being string-merged. */
4869 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4870 TREE_STRING_POINTER (base1) + ioff1,
4871 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4872 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4873 ;
4874 else if (!DECL_P (base0) || !DECL_P (base1))
4875 equal = 2;
4876 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4877 equal = 2;
4878 /* If this is a pointer comparison, ignore for now even
4879 valid equalities where one pointer is the offset zero
4880 of one object and the other to one past end of another one. */
4881 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4882 ;
4883 /* Assume that automatic variables can't be adjacent to global
4884 variables. */
4885 else if (is_global_var (base0) != is_global_var (base1))
4886 ;
4887 else
4888 {
4889 tree sz0 = DECL_SIZE_UNIT (base0);
4890 tree sz1 = DECL_SIZE_UNIT (base1);
4891 /* If sizes are unknown, e.g. VLA or not representable,
4892 punt. */
4893 if (!tree_fits_poly_int64_p (sz0)
4894 || !tree_fits_poly_int64_p (sz1))
4895 equal = 2;
4896 else
4897 {
4898 poly_int64 size0 = tree_to_poly_int64 (sz0);
4899 poly_int64 size1 = tree_to_poly_int64 (sz1);
4900 /* If one offset is pointing (or could be) to the beginning
4901 of one object and the other is pointing to one past the
4902 last byte of the other object, punt. */
4903 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4904 equal = 2;
4905 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4906 equal = 2;
4907 /* If both offsets are the same, there are some cases
4908 we know that are ok. Either if we know they aren't
4909 zero, or if we know both sizes are no zero. */
4910 if (equal == 2
4911 && known_eq (off0, off1)
4912 && (known_ne (off0, 0)
4913 || (known_ne (size0, 0) && known_ne (size1, 0))))
4914 equal = 0;
4915 }
4916 }
4917 }
4918 }
4919 (if (equal == 1
4920 && (cmp == EQ_EXPR || cmp == NE_EXPR
4921 /* If the offsets are equal we can ignore overflow. */
4922 || known_eq (off0, off1)
4923 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4924 /* Or if we compare using pointers to decls or strings. */
4925 || (POINTER_TYPE_P (TREE_TYPE (@2))
4926 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4927 (switch
4928 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4929 { constant_boolean_node (known_eq (off0, off1), type); })
4930 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4931 { constant_boolean_node (known_ne (off0, off1), type); })
4932 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4933 { constant_boolean_node (known_lt (off0, off1), type); })
4934 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4935 { constant_boolean_node (known_le (off0, off1), type); })
4936 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4937 { constant_boolean_node (known_ge (off0, off1), type); })
4938 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4939 { constant_boolean_node (known_gt (off0, off1), type); }))
4940 (if (equal == 0)
4941 (switch
4942 (if (cmp == EQ_EXPR)
4943 { constant_boolean_node (false, type); })
4944 (if (cmp == NE_EXPR)
4945 { constant_boolean_node (true, type); })))))))))
4946
4947 /* Simplify pointer equality compares using PTA. */
4948 (for neeq (ne eq)
4949 (simplify
4950 (neeq @0 @1)
4951 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4952 && ptrs_compare_unequal (@0, @1))
4953 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4954
4955 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4956 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4957 Disable the transform if either operand is pointer to function.
4958 This broke pr22051-2.c for arm where function pointer
4959 canonicalizaion is not wanted. */
4960
4961 (for cmp (ne eq)
4962 (simplify
4963 (cmp (convert @0) INTEGER_CST@1)
4964 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4965 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4966 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4967 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4968 && POINTER_TYPE_P (TREE_TYPE (@1))
4969 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4970 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4971 (cmp @0 (convert @1)))))
4972
4973 /* Non-equality compare simplifications from fold_binary */
4974 (for cmp (lt gt le ge)
4975 /* Comparisons with the highest or lowest possible integer of
4976 the specified precision will have known values. */
4977 (simplify
4978 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4979 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4980 || POINTER_TYPE_P (TREE_TYPE (@1))
4981 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4982 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4983 (with
4984 {
4985 tree cst = uniform_integer_cst_p (@1);
4986 tree arg1_type = TREE_TYPE (cst);
4987 unsigned int prec = TYPE_PRECISION (arg1_type);
4988 wide_int max = wi::max_value (arg1_type);
4989 wide_int signed_max = wi::max_value (prec, SIGNED);
4990 wide_int min = wi::min_value (arg1_type);
4991 }
4992 (switch
4993 (if (wi::to_wide (cst) == max)
4994 (switch
4995 (if (cmp == GT_EXPR)
4996 { constant_boolean_node (false, type); })
4997 (if (cmp == GE_EXPR)
4998 (eq @2 @1))
4999 (if (cmp == LE_EXPR)
5000 { constant_boolean_node (true, type); })
5001 (if (cmp == LT_EXPR)
5002 (ne @2 @1))))
5003 (if (wi::to_wide (cst) == min)
5004 (switch
5005 (if (cmp == LT_EXPR)
5006 { constant_boolean_node (false, type); })
5007 (if (cmp == LE_EXPR)
5008 (eq @2 @1))
5009 (if (cmp == GE_EXPR)
5010 { constant_boolean_node (true, type); })
5011 (if (cmp == GT_EXPR)
5012 (ne @2 @1))))
5013 (if (wi::to_wide (cst) == max - 1)
5014 (switch
5015 (if (cmp == GT_EXPR)
5016 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5017 wide_int_to_tree (TREE_TYPE (cst),
5018 wi::to_wide (cst)
5019 + 1)); }))
5020 (if (cmp == LE_EXPR)
5021 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5022 wide_int_to_tree (TREE_TYPE (cst),
5023 wi::to_wide (cst)
5024 + 1)); }))))
5025 (if (wi::to_wide (cst) == min + 1)
5026 (switch
5027 (if (cmp == GE_EXPR)
5028 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5029 wide_int_to_tree (TREE_TYPE (cst),
5030 wi::to_wide (cst)
5031 - 1)); }))
5032 (if (cmp == LT_EXPR)
5033 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5034 wide_int_to_tree (TREE_TYPE (cst),
5035 wi::to_wide (cst)
5036 - 1)); }))))
5037 (if (wi::to_wide (cst) == signed_max
5038 && TYPE_UNSIGNED (arg1_type)
5039 /* We will flip the signedness of the comparison operator
5040 associated with the mode of @1, so the sign bit is
5041 specified by this mode. Check that @1 is the signed
5042 max associated with this sign bit. */
5043 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5044 /* signed_type does not work on pointer types. */
5045 && INTEGRAL_TYPE_P (arg1_type))
5046 /* The following case also applies to X < signed_max+1
5047 and X >= signed_max+1 because previous transformations. */
5048 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5049 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5050 (switch
5051 (if (cst == @1 && cmp == LE_EXPR)
5052 (ge (convert:st @0) { build_zero_cst (st); }))
5053 (if (cst == @1 && cmp == GT_EXPR)
5054 (lt (convert:st @0) { build_zero_cst (st); }))
5055 (if (cmp == LE_EXPR)
5056 (ge (view_convert:st @0) { build_zero_cst (st); }))
5057 (if (cmp == GT_EXPR)
5058 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5059
5060 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5061 /* If the second operand is NaN, the result is constant. */
5062 (simplify
5063 (cmp @0 REAL_CST@1)
5064 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5065 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5066 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5067 ? false : true, type); })))
5068
5069 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5070 (simplify
5071 (unordered @0 @1)
5072 (switch
5073 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5074 { constant_boolean_node (true, type); })
5075 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5076 { constant_boolean_node (false, type); })))
5077
5078 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5079 (simplify
5080 (ordered @0 @1)
5081 (switch
5082 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5083 { constant_boolean_node (false, type); })
5084 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5085 { constant_boolean_node (true, type); })))
5086
5087 /* bool_var != 0 becomes bool_var. */
5088 (simplify
5089 (ne @0 integer_zerop)
5090 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5091 && types_match (type, TREE_TYPE (@0)))
5092 (non_lvalue @0)))
5093 /* bool_var == 1 becomes bool_var. */
5094 (simplify
5095 (eq @0 integer_onep)
5096 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5097 && types_match (type, TREE_TYPE (@0)))
5098 (non_lvalue @0)))
5099 /* Do not handle
5100 bool_var == 0 becomes !bool_var or
5101 bool_var != 1 becomes !bool_var
5102 here because that only is good in assignment context as long
5103 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5104 replace if (x == 0) with tem = ~x; if (tem != 0) which is
5105 clearly less optimal and which we'll transform again in forwprop. */
5106
5107 /* When one argument is a constant, overflow detection can be simplified.
5108 Currently restricted to single use so as not to interfere too much with
5109 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5110 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
5111 (for cmp (lt le ge gt)
5112 out (gt gt le le)
5113 (simplify
5114 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5115 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5116 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5117 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5118 && wi::to_wide (@1) != 0
5119 && single_use (@2))
5120 (with {
5121 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5122 signop sign = TYPE_SIGN (TREE_TYPE (@0));
5123 }
5124 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5125 wi::max_value (prec, sign)
5126 - wi::to_wide (@1)); })))))
5127
5128 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5129 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5130 expects the long form, so we restrict the transformation for now. */
5131 (for cmp (gt le)
5132 (simplify
5133 (cmp:c (minus@2 @0 @1) @0)
5134 (if (single_use (@2)
5135 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5136 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5137 (cmp @1 @0))))
5138
5139 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
5140 (for cmp (ge lt)
5141 (simplify
5142 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5143 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5144 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5145 (cmp @1 @0))))
5146
5147 /* Testing for overflow is unnecessary if we already know the result. */
5148 /* A - B > A */
5149 (for cmp (gt le)
5150 out (ne eq)
5151 (simplify
5152 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5153 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5154 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5155 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5156 /* A + B < A */
5157 (for cmp (lt ge)
5158 out (ne eq)
5159 (simplify
5160 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5161 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5162 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5163 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5164
5165 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5166 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5167 (for cmp (lt ge)
5168 out (ne eq)
5169 (simplify
5170 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5171 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5172 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5173 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5174
5175 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5176 is at least twice as wide as type of A and B, simplify to
5177 __builtin_mul_overflow (A, B, <unused>). */
5178 (for cmp (eq ne)
5179 (simplify
5180 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5181 integer_zerop)
5182 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5183 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5184 && TYPE_UNSIGNED (TREE_TYPE (@0))
5185 && (TYPE_PRECISION (TREE_TYPE (@3))
5186 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5187 && tree_fits_uhwi_p (@2)
5188 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5189 && types_match (@0, @1)
5190 && type_has_mode_precision_p (TREE_TYPE (@0))
5191 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5192 != CODE_FOR_nothing))
5193 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5194 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5195
5196 /* Simplification of math builtins. These rules must all be optimizations
5197 as well as IL simplifications. If there is a possibility that the new
5198 form could be a pessimization, the rule should go in the canonicalization
5199 section that follows this one.
5200
5201 Rules can generally go in this section if they satisfy one of
5202 the following:
5203
5204 - the rule describes an identity
5205
5206 - the rule replaces calls with something as simple as addition or
5207 multiplication
5208
5209 - the rule contains unary calls only and simplifies the surrounding
5210 arithmetic. (The idea here is to exclude non-unary calls in which
5211 one operand is constant and in which the call is known to be cheap
5212 when the operand has that value.) */
5213
5214 (if (flag_unsafe_math_optimizations)
5215 /* Simplify sqrt(x) * sqrt(x) -> x. */
5216 (simplify
5217 (mult (SQRT_ALL@1 @0) @1)
5218 (if (!tree_expr_maybe_signaling_nan_p (@0))
5219 @0))
5220
5221 (for op (plus minus)
5222 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5223 (simplify
5224 (op (rdiv @0 @1)
5225 (rdiv @2 @1))
5226 (rdiv (op @0 @2) @1)))
5227
5228 (for cmp (lt le gt ge)
5229 neg_cmp (gt ge lt le)
5230 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5231 (simplify
5232 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5233 (with
5234 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5235 (if (tem
5236 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5237 || (real_zerop (tem) && !real_zerop (@1))))
5238 (switch
5239 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5240 (cmp @0 { tem; }))
5241 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5242 (neg_cmp @0 { tem; })))))))
5243
5244 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5245 (for root (SQRT CBRT)
5246 (simplify
5247 (mult (root:s @0) (root:s @1))
5248 (root (mult @0 @1))))
5249
5250 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5251 (for exps (EXP EXP2 EXP10 POW10)
5252 (simplify
5253 (mult (exps:s @0) (exps:s @1))
5254 (exps (plus @0 @1))))
5255
5256 /* Simplify a/root(b/c) into a*root(c/b). */
5257 (for root (SQRT CBRT)
5258 (simplify
5259 (rdiv @0 (root:s (rdiv:s @1 @2)))
5260 (mult @0 (root (rdiv @2 @1)))))
5261
5262 /* Simplify x/expN(y) into x*expN(-y). */
5263 (for exps (EXP EXP2 EXP10 POW10)
5264 (simplify
5265 (rdiv @0 (exps:s @1))
5266 (mult @0 (exps (negate @1)))))
5267
5268 (for logs (LOG LOG2 LOG10 LOG10)
5269 exps (EXP EXP2 EXP10 POW10)
5270 /* logN(expN(x)) -> x. */
5271 (simplify
5272 (logs (exps @0))
5273 @0)
5274 /* expN(logN(x)) -> x. */
5275 (simplify
5276 (exps (logs @0))
5277 @0))
5278
5279 /* Optimize logN(func()) for various exponential functions. We
5280 want to determine the value "x" and the power "exponent" in
5281 order to transform logN(x**exponent) into exponent*logN(x). */
5282 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5283 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5284 (simplify
5285 (logs (exps @0))
5286 (if (SCALAR_FLOAT_TYPE_P (type))
5287 (with {
5288 tree x;
5289 switch (exps)
5290 {
5291 CASE_CFN_EXP:
5292 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5293 x = build_real_truncate (type, dconst_e ());
5294 break;
5295 CASE_CFN_EXP2:
5296 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5297 x = build_real (type, dconst2);
5298 break;
5299 CASE_CFN_EXP10:
5300 CASE_CFN_POW10:
5301 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5302 {
5303 REAL_VALUE_TYPE dconst10;
5304 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5305 x = build_real (type, dconst10);
5306 }
5307 break;
5308 default:
5309 gcc_unreachable ();
5310 }
5311 }
5312 (mult (logs { x; }) @0)))))
5313
5314 (for logs (LOG LOG
5315 LOG2 LOG2
5316 LOG10 LOG10)
5317 exps (SQRT CBRT)
5318 (simplify
5319 (logs (exps @0))
5320 (if (SCALAR_FLOAT_TYPE_P (type))
5321 (with {
5322 tree x;
5323 switch (exps)
5324 {
5325 CASE_CFN_SQRT:
5326 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5327 x = build_real (type, dconsthalf);
5328 break;
5329 CASE_CFN_CBRT:
5330 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5331 x = build_real_truncate (type, dconst_third ());
5332 break;
5333 default:
5334 gcc_unreachable ();
5335 }
5336 }
5337 (mult { x; } (logs @0))))))
5338
5339 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5340 (for logs (LOG LOG2 LOG10)
5341 pows (POW)
5342 (simplify
5343 (logs (pows @0 @1))
5344 (mult @1 (logs @0))))
5345
5346 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5347 or if C is a positive power of 2,
5348 pow(C,x) -> exp2(log2(C)*x). */
5349 #if GIMPLE
5350 (for pows (POW)
5351 exps (EXP)
5352 logs (LOG)
5353 exp2s (EXP2)
5354 log2s (LOG2)
5355 (simplify
5356 (pows REAL_CST@0 @1)
5357 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5358 && real_isfinite (TREE_REAL_CST_PTR (@0))
5359 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5360 the use_exp2 case until after vectorization. It seems actually
5361 beneficial for all constants to postpone this until later,
5362 because exp(log(C)*x), while faster, will have worse precision
5363 and if x folds into a constant too, that is unnecessary
5364 pessimization. */
5365 && canonicalize_math_after_vectorization_p ())
5366 (with {
5367 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5368 bool use_exp2 = false;
5369 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5370 && value->cl == rvc_normal)
5371 {
5372 REAL_VALUE_TYPE frac_rvt = *value;
5373 SET_REAL_EXP (&frac_rvt, 1);
5374 if (real_equal (&frac_rvt, &dconst1))
5375 use_exp2 = true;
5376 }
5377 }
5378 (if (!use_exp2)
5379 (if (optimize_pow_to_exp (@0, @1))
5380 (exps (mult (logs @0) @1)))
5381 (exp2s (mult (log2s @0) @1)))))))
5382 #endif
5383
5384 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5385 (for pows (POW)
5386 exps (EXP EXP2 EXP10 POW10)
5387 logs (LOG LOG2 LOG10 LOG10)
5388 (simplify
5389 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5390 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5391 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5392 (exps (plus (mult (logs @0) @1) @2)))))
5393
5394 (for sqrts (SQRT)
5395 cbrts (CBRT)
5396 pows (POW)
5397 exps (EXP EXP2 EXP10 POW10)
5398 /* sqrt(expN(x)) -> expN(x*0.5). */
5399 (simplify
5400 (sqrts (exps @0))
5401 (exps (mult @0 { build_real (type, dconsthalf); })))
5402 /* cbrt(expN(x)) -> expN(x/3). */
5403 (simplify
5404 (cbrts (exps @0))
5405 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5406 /* pow(expN(x), y) -> expN(x*y). */
5407 (simplify
5408 (pows (exps @0) @1)
5409 (exps (mult @0 @1))))
5410
5411 /* tan(atan(x)) -> x. */
5412 (for tans (TAN)
5413 atans (ATAN)
5414 (simplify
5415 (tans (atans @0))
5416 @0)))
5417
5418 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5419 (for sins (SIN)
5420 atans (ATAN)
5421 sqrts (SQRT)
5422 copysigns (COPYSIGN)
5423 (simplify
5424 (sins (atans:s @0))
5425 (with
5426 {
5427 REAL_VALUE_TYPE r_cst;
5428 build_sinatan_real (&r_cst, type);
5429 tree t_cst = build_real (type, r_cst);
5430 tree t_one = build_one_cst (type);
5431 }
5432 (if (SCALAR_FLOAT_TYPE_P (type))
5433 (cond (lt (abs @0) { t_cst; })
5434 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5435 (copysigns { t_one; } @0))))))
5436
5437 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5438 (for coss (COS)
5439 atans (ATAN)
5440 sqrts (SQRT)
5441 copysigns (COPYSIGN)
5442 (simplify
5443 (coss (atans:s @0))
5444 (with
5445 {
5446 REAL_VALUE_TYPE r_cst;
5447 build_sinatan_real (&r_cst, type);
5448 tree t_cst = build_real (type, r_cst);
5449 tree t_one = build_one_cst (type);
5450 tree t_zero = build_zero_cst (type);
5451 }
5452 (if (SCALAR_FLOAT_TYPE_P (type))
5453 (cond (lt (abs @0) { t_cst; })
5454 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5455 (copysigns { t_zero; } @0))))))
5456
5457 (if (!flag_errno_math)
5458 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5459 (for sinhs (SINH)
5460 atanhs (ATANH)
5461 sqrts (SQRT)
5462 (simplify
5463 (sinhs (atanhs:s @0))
5464 (with { tree t_one = build_one_cst (type); }
5465 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5466
5467 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5468 (for coshs (COSH)
5469 atanhs (ATANH)
5470 sqrts (SQRT)
5471 (simplify
5472 (coshs (atanhs:s @0))
5473 (with { tree t_one = build_one_cst (type); }
5474 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5475
5476 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
5477 (simplify
5478 (CABS (complex:C @0 real_zerop@1))
5479 (abs @0))
5480
5481 /* trunc(trunc(x)) -> trunc(x), etc. */
5482 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5483 (simplify
5484 (fns (fns @0))
5485 (fns @0)))
5486 /* f(x) -> x if x is integer valued and f does nothing for such values. */
5487 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5488 (simplify
5489 (fns integer_valued_real_p@0)
5490 @0))
5491
5492 /* hypot(x,0) and hypot(0,x) -> abs(x). */
5493 (simplify
5494 (HYPOT:c @0 real_zerop@1)
5495 (abs @0))
5496
5497 /* pow(1,x) -> 1. */
5498 (simplify
5499 (POW real_onep@0 @1)
5500 @0)
5501
5502 (simplify
5503 /* copysign(x,x) -> x. */
5504 (COPYSIGN_ALL @0 @0)
5505 @0)
5506
5507 (simplify
5508 /* copysign(x,-x) -> -x. */
5509 (COPYSIGN_ALL @0 (negate@1 @0))
5510 @1)
5511
5512 (simplify
5513 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
5514 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5515 (abs @0))
5516
5517 (for scale (LDEXP SCALBN SCALBLN)
5518 /* ldexp(0, x) -> 0. */
5519 (simplify
5520 (scale real_zerop@0 @1)
5521 @0)
5522 /* ldexp(x, 0) -> x. */
5523 (simplify
5524 (scale @0 integer_zerop@1)
5525 @0)
5526 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
5527 (simplify
5528 (scale REAL_CST@0 @1)
5529 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5530 @0)))
5531
5532 /* Canonicalization of sequences of math builtins. These rules represent
5533 IL simplifications but are not necessarily optimizations.
5534
5535 The sincos pass is responsible for picking "optimal" implementations
5536 of math builtins, which may be more complicated and can sometimes go
5537 the other way, e.g. converting pow into a sequence of sqrts.
5538 We only want to do these canonicalizations before the pass has run. */
5539
5540 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5541 /* Simplify tan(x) * cos(x) -> sin(x). */
5542 (simplify
5543 (mult:c (TAN:s @0) (COS:s @0))
5544 (SIN @0))
5545
5546 /* Simplify x * pow(x,c) -> pow(x,c+1). */
5547 (simplify
5548 (mult:c @0 (POW:s @0 REAL_CST@1))
5549 (if (!TREE_OVERFLOW (@1))
5550 (POW @0 (plus @1 { build_one_cst (type); }))))
5551
5552 /* Simplify sin(x) / cos(x) -> tan(x). */
5553 (simplify
5554 (rdiv (SIN:s @0) (COS:s @0))
5555 (TAN @0))
5556
5557 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5558 (simplify
5559 (rdiv (SINH:s @0) (COSH:s @0))
5560 (TANH @0))
5561
5562 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
5563 (simplify
5564 (rdiv (TANH:s @0) (SINH:s @0))
5565 (rdiv {build_one_cst (type);} (COSH @0)))
5566
5567 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5568 (simplify
5569 (rdiv (COS:s @0) (SIN:s @0))
5570 (rdiv { build_one_cst (type); } (TAN @0)))
5571
5572 /* Simplify sin(x) / tan(x) -> cos(x). */
5573 (simplify
5574 (rdiv (SIN:s @0) (TAN:s @0))
5575 (if (! HONOR_NANS (@0)
5576 && ! HONOR_INFINITIES (@0))
5577 (COS @0)))
5578
5579 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5580 (simplify
5581 (rdiv (TAN:s @0) (SIN:s @0))
5582 (if (! HONOR_NANS (@0)
5583 && ! HONOR_INFINITIES (@0))
5584 (rdiv { build_one_cst (type); } (COS @0))))
5585
5586 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5587 (simplify
5588 (mult (POW:s @0 @1) (POW:s @0 @2))
5589 (POW @0 (plus @1 @2)))
5590
5591 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5592 (simplify
5593 (mult (POW:s @0 @1) (POW:s @2 @1))
5594 (POW (mult @0 @2) @1))
5595
5596 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5597 (simplify
5598 (mult (POWI:s @0 @1) (POWI:s @2 @1))
5599 (POWI (mult @0 @2) @1))
5600
5601 /* Simplify pow(x,c) / x -> pow(x,c-1). */
5602 (simplify
5603 (rdiv (POW:s @0 REAL_CST@1) @0)
5604 (if (!TREE_OVERFLOW (@1))
5605 (POW @0 (minus @1 { build_one_cst (type); }))))
5606
5607 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5608 (simplify
5609 (rdiv @0 (POW:s @1 @2))
5610 (mult @0 (POW @1 (negate @2))))
5611
5612 (for sqrts (SQRT)
5613 cbrts (CBRT)
5614 pows (POW)
5615 /* sqrt(sqrt(x)) -> pow(x,1/4). */
5616 (simplify
5617 (sqrts (sqrts @0))
5618 (pows @0 { build_real (type, dconst_quarter ()); }))
5619 /* sqrt(cbrt(x)) -> pow(x,1/6). */
5620 (simplify
5621 (sqrts (cbrts @0))
5622 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5623 /* cbrt(sqrt(x)) -> pow(x,1/6). */
5624 (simplify
5625 (cbrts (sqrts @0))
5626 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5627 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
5628 (simplify
5629 (cbrts (cbrts tree_expr_nonnegative_p@0))
5630 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5631 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
5632 (simplify
5633 (sqrts (pows @0 @1))
5634 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5635 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
5636 (simplify
5637 (cbrts (pows tree_expr_nonnegative_p@0 @1))
5638 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5639 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
5640 (simplify
5641 (pows (sqrts @0) @1)
5642 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5643 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
5644 (simplify
5645 (pows (cbrts tree_expr_nonnegative_p@0) @1)
5646 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5647 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
5648 (simplify
5649 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5650 (pows @0 (mult @1 @2))))
5651
5652 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
5653 (simplify
5654 (CABS (complex @0 @0))
5655 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5656
5657 /* hypot(x,x) -> fabs(x)*sqrt(2). */
5658 (simplify
5659 (HYPOT @0 @0)
5660 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5661
5662 /* cexp(x+yi) -> exp(x)*cexpi(y). */
5663 (for cexps (CEXP)
5664 exps (EXP)
5665 cexpis (CEXPI)
5666 (simplify
5667 (cexps compositional_complex@0)
5668 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
5669 (complex
5670 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5671 (mult @1 (imagpart @2)))))))
5672
5673 (if (canonicalize_math_p ())
5674 /* floor(x) -> trunc(x) if x is nonnegative. */
5675 (for floors (FLOOR_ALL)
5676 truncs (TRUNC_ALL)
5677 (simplify
5678 (floors tree_expr_nonnegative_p@0)
5679 (truncs @0))))
5680
5681 (match double_value_p
5682 @0
5683 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5684 (for froms (BUILT_IN_TRUNCL
5685 BUILT_IN_FLOORL
5686 BUILT_IN_CEILL
5687 BUILT_IN_ROUNDL
5688 BUILT_IN_NEARBYINTL
5689 BUILT_IN_RINTL)
5690 tos (BUILT_IN_TRUNC
5691 BUILT_IN_FLOOR
5692 BUILT_IN_CEIL
5693 BUILT_IN_ROUND
5694 BUILT_IN_NEARBYINT
5695 BUILT_IN_RINT)
5696 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
5697 (if (optimize && canonicalize_math_p ())
5698 (simplify
5699 (froms (convert double_value_p@0))
5700 (convert (tos @0)))))
5701
5702 (match float_value_p
5703 @0
5704 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5705 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5706 BUILT_IN_FLOORL BUILT_IN_FLOOR
5707 BUILT_IN_CEILL BUILT_IN_CEIL
5708 BUILT_IN_ROUNDL BUILT_IN_ROUND
5709 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5710 BUILT_IN_RINTL BUILT_IN_RINT)
5711 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5712 BUILT_IN_FLOORF BUILT_IN_FLOORF
5713 BUILT_IN_CEILF BUILT_IN_CEILF
5714 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5715 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5716 BUILT_IN_RINTF BUILT_IN_RINTF)
5717 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5718 if x is a float. */
5719 (if (optimize && canonicalize_math_p ()
5720 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
5721 (simplify
5722 (froms (convert float_value_p@0))
5723 (convert (tos @0)))))
5724
5725 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5726 tos (XFLOOR XCEIL XROUND XRINT)
5727 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
5728 (if (optimize && canonicalize_math_p ())
5729 (simplify
5730 (froms (convert double_value_p@0))
5731 (tos @0))))
5732
5733 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5734 XFLOOR XCEIL XROUND XRINT)
5735 tos (XFLOORF XCEILF XROUNDF XRINTF)
5736 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5737 if x is a float. */
5738 (if (optimize && canonicalize_math_p ())
5739 (simplify
5740 (froms (convert float_value_p@0))
5741 (tos @0))))
5742
5743 (if (canonicalize_math_p ())
5744 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
5745 (for floors (IFLOOR LFLOOR LLFLOOR)
5746 (simplify
5747 (floors tree_expr_nonnegative_p@0)
5748 (fix_trunc @0))))
5749
5750 (if (canonicalize_math_p ())
5751 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
5752 (for fns (IFLOOR LFLOOR LLFLOOR
5753 ICEIL LCEIL LLCEIL
5754 IROUND LROUND LLROUND)
5755 (simplify
5756 (fns integer_valued_real_p@0)
5757 (fix_trunc @0)))
5758 (if (!flag_errno_math)
5759 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
5760 (for rints (IRINT LRINT LLRINT)
5761 (simplify
5762 (rints integer_valued_real_p@0)
5763 (fix_trunc @0)))))
5764
5765 (if (canonicalize_math_p ())
5766 (for ifn (IFLOOR ICEIL IROUND IRINT)
5767 lfn (LFLOOR LCEIL LROUND LRINT)
5768 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5769 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5770 sizeof (int) == sizeof (long). */
5771 (if (TYPE_PRECISION (integer_type_node)
5772 == TYPE_PRECISION (long_integer_type_node))
5773 (simplify
5774 (ifn @0)
5775 (lfn:long_integer_type_node @0)))
5776 /* Canonicalize llround (x) to lround (x) on LP64 targets where
5777 sizeof (long long) == sizeof (long). */
5778 (if (TYPE_PRECISION (long_long_integer_type_node)
5779 == TYPE_PRECISION (long_integer_type_node))
5780 (simplify
5781 (llfn @0)
5782 (lfn:long_integer_type_node @0)))))
5783
5784 /* cproj(x) -> x if we're ignoring infinities. */
5785 (simplify
5786 (CPROJ @0)
5787 (if (!HONOR_INFINITIES (type))
5788 @0))
5789
5790 /* If the real part is inf and the imag part is known to be
5791 nonnegative, return (inf + 0i). */
5792 (simplify
5793 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5794 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5795 { build_complex_inf (type, false); }))
5796
5797 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
5798 (simplify
5799 (CPROJ (complex @0 REAL_CST@1))
5800 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5801 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5802
5803 (for pows (POW)
5804 sqrts (SQRT)
5805 cbrts (CBRT)
5806 (simplify
5807 (pows @0 REAL_CST@1)
5808 (with {
5809 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5810 REAL_VALUE_TYPE tmp;
5811 }
5812 (switch
5813 /* pow(x,0) -> 1. */
5814 (if (real_equal (value, &dconst0))
5815 { build_real (type, dconst1); })
5816 /* pow(x,1) -> x. */
5817 (if (real_equal (value, &dconst1))
5818 @0)
5819 /* pow(x,-1) -> 1/x. */
5820 (if (real_equal (value, &dconstm1))
5821 (rdiv { build_real (type, dconst1); } @0))
5822 /* pow(x,0.5) -> sqrt(x). */
5823 (if (flag_unsafe_math_optimizations
5824 && canonicalize_math_p ()
5825 && real_equal (value, &dconsthalf))
5826 (sqrts @0))
5827 /* pow(x,1/3) -> cbrt(x). */
5828 (if (flag_unsafe_math_optimizations
5829 && canonicalize_math_p ()
5830 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5831 real_equal (value, &tmp)))
5832 (cbrts @0))))))
5833
5834 /* powi(1,x) -> 1. */
5835 (simplify
5836 (POWI real_onep@0 @1)
5837 @0)
5838
5839 (simplify
5840 (POWI @0 INTEGER_CST@1)
5841 (switch
5842 /* powi(x,0) -> 1. */
5843 (if (wi::to_wide (@1) == 0)
5844 { build_real (type, dconst1); })
5845 /* powi(x,1) -> x. */
5846 (if (wi::to_wide (@1) == 1)
5847 @0)
5848 /* powi(x,-1) -> 1/x. */
5849 (if (wi::to_wide (@1) == -1)
5850 (rdiv { build_real (type, dconst1); } @0))))
5851
5852 /* Narrowing of arithmetic and logical operations.
5853
5854 These are conceptually similar to the transformations performed for
5855 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
5856 term we want to move all that code out of the front-ends into here. */
5857
5858 /* Convert (outertype)((innertype0)a+(innertype1)b)
5859 into ((newtype)a+(newtype)b) where newtype
5860 is the widest mode from all of these. */
5861 (for op (plus minus mult rdiv)
5862 (simplify
5863 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5864 /* If we have a narrowing conversion of an arithmetic operation where
5865 both operands are widening conversions from the same type as the outer
5866 narrowing conversion. Then convert the innermost operands to a
5867 suitable unsigned type (to avoid introducing undefined behavior),
5868 perform the operation and convert the result to the desired type. */
5869 (if (INTEGRAL_TYPE_P (type)
5870 && op != MULT_EXPR
5871 && op != RDIV_EXPR
5872 /* We check for type compatibility between @0 and @1 below,
5873 so there's no need to check that @2/@4 are integral types. */
5874 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5875 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5876 /* The precision of the type of each operand must match the
5877 precision of the mode of each operand, similarly for the
5878 result. */
5879 && type_has_mode_precision_p (TREE_TYPE (@1))
5880 && type_has_mode_precision_p (TREE_TYPE (@2))
5881 && type_has_mode_precision_p (type)
5882 /* The inner conversion must be a widening conversion. */
5883 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5884 && types_match (@1, type)
5885 && (types_match (@1, @2)
5886 /* Or the second operand is const integer or converted const
5887 integer from valueize. */
5888 || TREE_CODE (@2) == INTEGER_CST))
5889 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5890 (op @1 (convert @2))
5891 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5892 (convert (op (convert:utype @1)
5893 (convert:utype @2)))))
5894 (if (FLOAT_TYPE_P (type)
5895 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5896 == DECIMAL_FLOAT_TYPE_P (type))
5897 (with { tree arg0 = strip_float_extensions (@1);
5898 tree arg1 = strip_float_extensions (@2);
5899 tree itype = TREE_TYPE (@0);
5900 tree ty1 = TREE_TYPE (arg0);
5901 tree ty2 = TREE_TYPE (arg1);
5902 enum tree_code code = TREE_CODE (itype); }
5903 (if (FLOAT_TYPE_P (ty1)
5904 && FLOAT_TYPE_P (ty2))
5905 (with { tree newtype = type;
5906 if (TYPE_MODE (ty1) == SDmode
5907 || TYPE_MODE (ty2) == SDmode
5908 || TYPE_MODE (type) == SDmode)
5909 newtype = dfloat32_type_node;
5910 if (TYPE_MODE (ty1) == DDmode
5911 || TYPE_MODE (ty2) == DDmode
5912 || TYPE_MODE (type) == DDmode)
5913 newtype = dfloat64_type_node;
5914 if (TYPE_MODE (ty1) == TDmode
5915 || TYPE_MODE (ty2) == TDmode
5916 || TYPE_MODE (type) == TDmode)
5917 newtype = dfloat128_type_node; }
5918 (if ((newtype == dfloat32_type_node
5919 || newtype == dfloat64_type_node
5920 || newtype == dfloat128_type_node)
5921 && newtype == type
5922 && types_match (newtype, type))
5923 (op (convert:newtype @1) (convert:newtype @2))
5924 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5925 newtype = ty1;
5926 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5927 newtype = ty2; }
5928 /* Sometimes this transformation is safe (cannot
5929 change results through affecting double rounding
5930 cases) and sometimes it is not. If NEWTYPE is
5931 wider than TYPE, e.g. (float)((long double)double
5932 + (long double)double) converted to
5933 (float)(double + double), the transformation is
5934 unsafe regardless of the details of the types
5935 involved; double rounding can arise if the result
5936 of NEWTYPE arithmetic is a NEWTYPE value half way
5937 between two representable TYPE values but the
5938 exact value is sufficiently different (in the
5939 right direction) for this difference to be
5940 visible in ITYPE arithmetic. If NEWTYPE is the
5941 same as TYPE, however, the transformation may be
5942 safe depending on the types involved: it is safe
5943 if the ITYPE has strictly more than twice as many
5944 mantissa bits as TYPE, can represent infinities
5945 and NaNs if the TYPE can, and has sufficient
5946 exponent range for the product or ratio of two
5947 values representable in the TYPE to be within the
5948 range of normal values of ITYPE. */
5949 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5950 && (flag_unsafe_math_optimizations
5951 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5952 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5953 TYPE_MODE (type))
5954 && !excess_precision_type (newtype)))
5955 && !types_match (itype, newtype))
5956 (convert:type (op (convert:newtype @1)
5957 (convert:newtype @2)))
5958 )))) )
5959 ))
5960 )))
5961
5962 /* This is another case of narrowing, specifically when there's an outer
5963 BIT_AND_EXPR which masks off bits outside the type of the innermost
5964 operands. Like the previous case we have to convert the operands
5965 to unsigned types to avoid introducing undefined behavior for the
5966 arithmetic operation. */
5967 (for op (minus plus)
5968 (simplify
5969 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
5970 (if (INTEGRAL_TYPE_P (type)
5971 /* We check for type compatibility between @0 and @1 below,
5972 so there's no need to check that @1/@3 are integral types. */
5973 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5974 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5975 /* The precision of the type of each operand must match the
5976 precision of the mode of each operand, similarly for the
5977 result. */
5978 && type_has_mode_precision_p (TREE_TYPE (@0))
5979 && type_has_mode_precision_p (TREE_TYPE (@1))
5980 && type_has_mode_precision_p (type)
5981 /* The inner conversion must be a widening conversion. */
5982 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5983 && types_match (@0, @1)
5984 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
5985 <= TYPE_PRECISION (TREE_TYPE (@0)))
5986 && (wi::to_wide (@4)
5987 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
5988 true, TYPE_PRECISION (type))) == 0)
5989 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5990 (with { tree ntype = TREE_TYPE (@0); }
5991 (convert (bit_and (op @0 @1) (convert:ntype @4))))
5992 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5993 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
5994 (convert:utype @4))))))))
5995
5996 /* Transform (@0 < @1 and @0 < @2) to use min,
5997 (@0 > @1 and @0 > @2) to use max */
5998 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
5999 op (lt le gt ge lt le gt ge )
6000 ext (min min max max max max min min )
6001 (simplify
6002 (logic (op:cs @0 @1) (op:cs @0 @2))
6003 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6004 && TREE_CODE (@0) != INTEGER_CST)
6005 (op @0 (ext @1 @2)))))
6006
6007 (simplify
6008 /* signbit(x) -> 0 if x is nonnegative. */
6009 (SIGNBIT tree_expr_nonnegative_p@0)
6010 { integer_zero_node; })
6011
6012 (simplify
6013 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
6014 (SIGNBIT @0)
6015 (if (!HONOR_SIGNED_ZEROS (@0))
6016 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6017
6018 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
6019 (for cmp (eq ne)
6020 (for op (plus minus)
6021 rop (minus plus)
6022 (simplify
6023 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6024 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6025 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6026 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6027 && !TYPE_SATURATING (TREE_TYPE (@0)))
6028 (with { tree res = int_const_binop (rop, @2, @1); }
6029 (if (TREE_OVERFLOW (res)
6030 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6031 { constant_boolean_node (cmp == NE_EXPR, type); }
6032 (if (single_use (@3))
6033 (cmp @0 { TREE_OVERFLOW (res)
6034 ? drop_tree_overflow (res) : res; }))))))))
6035 (for cmp (lt le gt ge)
6036 (for op (plus minus)
6037 rop (minus plus)
6038 (simplify
6039 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6040 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6041 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6042 (with { tree res = int_const_binop (rop, @2, @1); }
6043 (if (TREE_OVERFLOW (res))
6044 {
6045 fold_overflow_warning (("assuming signed overflow does not occur "
6046 "when simplifying conditional to constant"),
6047 WARN_STRICT_OVERFLOW_CONDITIONAL);
6048 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6049 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
6050 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6051 TYPE_SIGN (TREE_TYPE (@1)))
6052 != (op == MINUS_EXPR);
6053 constant_boolean_node (less == ovf_high, type);
6054 }
6055 (if (single_use (@3))
6056 (with
6057 {
6058 fold_overflow_warning (("assuming signed overflow does not occur "
6059 "when changing X +- C1 cmp C2 to "
6060 "X cmp C2 -+ C1"),
6061 WARN_STRICT_OVERFLOW_COMPARISON);
6062 }
6063 (cmp @0 { res; })))))))))
6064
6065 /* Canonicalizations of BIT_FIELD_REFs. */
6066
6067 (simplify
6068 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6069 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6070
6071 (simplify
6072 (BIT_FIELD_REF (view_convert @0) @1 @2)
6073 (BIT_FIELD_REF @0 @1 @2))
6074
6075 (simplify
6076 (BIT_FIELD_REF @0 @1 integer_zerop)
6077 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6078 (view_convert @0)))
6079
6080 (simplify
6081 (BIT_FIELD_REF @0 @1 @2)
6082 (switch
6083 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6084 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6085 (switch
6086 (if (integer_zerop (@2))
6087 (view_convert (realpart @0)))
6088 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6089 (view_convert (imagpart @0)))))
6090 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6091 && INTEGRAL_TYPE_P (type)
6092 /* On GIMPLE this should only apply to register arguments. */
6093 && (! GIMPLE || is_gimple_reg (@0))
6094 /* A bit-field-ref that referenced the full argument can be stripped. */
6095 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6096 && integer_zerop (@2))
6097 /* Low-parts can be reduced to integral conversions.
6098 ??? The following doesn't work for PDP endian. */
6099 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6100 /* But only do this after vectorization. */
6101 && canonicalize_math_after_vectorization_p ()
6102 /* Don't even think about BITS_BIG_ENDIAN. */
6103 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6104 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6105 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6106 ? (TYPE_PRECISION (TREE_TYPE (@0))
6107 - TYPE_PRECISION (type))
6108 : 0)) == 0)))
6109 (convert @0))))
6110
6111 /* Simplify vector extracts. */
6112
6113 (simplify
6114 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6115 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6116 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
6117 || (VECTOR_TYPE_P (type)
6118 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
6119 (with
6120 {
6121 tree ctor = (TREE_CODE (@0) == SSA_NAME
6122 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6123 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6124 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6125 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6126 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6127 }
6128 (if (n != 0
6129 && (idx % width) == 0
6130 && (n % width) == 0
6131 && known_le ((idx + n) / width,
6132 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6133 (with
6134 {
6135 idx = idx / width;
6136 n = n / width;
6137 /* Constructor elements can be subvectors. */
6138 poly_uint64 k = 1;
6139 if (CONSTRUCTOR_NELTS (ctor) != 0)
6140 {
6141 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6142 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6143 k = TYPE_VECTOR_SUBPARTS (cons_elem);
6144 }
6145 unsigned HOST_WIDE_INT elt, count, const_k;
6146 }
6147 (switch
6148 /* We keep an exact subset of the constructor elements. */
6149 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6150 (if (CONSTRUCTOR_NELTS (ctor) == 0)
6151 { build_constructor (type, NULL); }
6152 (if (count == 1)
6153 (if (elt < CONSTRUCTOR_NELTS (ctor))
6154 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6155 { build_zero_cst (type); })
6156 /* We don't want to emit new CTORs unless the old one goes away.
6157 ??? Eventually allow this if the CTOR ends up constant or
6158 uniform. */
6159 (if (single_use (@0))
6160 {
6161 vec<constructor_elt, va_gc> *vals;
6162 vec_alloc (vals, count);
6163 for (unsigned i = 0;
6164 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6165 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
6166 CONSTRUCTOR_ELT (ctor, elt + i)->value);
6167 build_constructor (type, vals);
6168 }))))
6169 /* The bitfield references a single constructor element. */
6170 (if (k.is_constant (&const_k)
6171 && idx + n <= (idx / const_k + 1) * const_k)
6172 (switch
6173 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6174 { build_zero_cst (type); })
6175 (if (n == const_k)
6176 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6177 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6178 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6179
6180 /* Simplify a bit extraction from a bit insertion for the cases with
6181 the inserted element fully covering the extraction or the insertion
6182 not touching the extraction. */
6183 (simplify
6184 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6185 (with
6186 {
6187 unsigned HOST_WIDE_INT isize;
6188 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6189 isize = TYPE_PRECISION (TREE_TYPE (@1));
6190 else
6191 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6192 }
6193 (switch
6194 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6195 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6196 wi::to_wide (@ipos) + isize))
6197 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6198 wi::to_wide (@rpos)
6199 - wi::to_wide (@ipos)); }))
6200 (if (wi::geu_p (wi::to_wide (@ipos),
6201 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6202 || wi::geu_p (wi::to_wide (@rpos),
6203 wi::to_wide (@ipos) + isize))
6204 (BIT_FIELD_REF @0 @rsize @rpos)))))
6205
6206 (if (canonicalize_math_after_vectorization_p ())
6207 (for fmas (FMA)
6208 (simplify
6209 (fmas:c (negate @0) @1 @2)
6210 (IFN_FNMA @0 @1 @2))
6211 (simplify
6212 (fmas @0 @1 (negate @2))
6213 (IFN_FMS @0 @1 @2))
6214 (simplify
6215 (fmas:c (negate @0) @1 (negate @2))
6216 (IFN_FNMS @0 @1 @2))
6217 (simplify
6218 (negate (fmas@3 @0 @1 @2))
6219 (if (single_use (@3))
6220 (IFN_FNMS @0 @1 @2))))
6221
6222 (simplify
6223 (IFN_FMS:c (negate @0) @1 @2)
6224 (IFN_FNMS @0 @1 @2))
6225 (simplify
6226 (IFN_FMS @0 @1 (negate @2))
6227 (IFN_FMA @0 @1 @2))
6228 (simplify
6229 (IFN_FMS:c (negate @0) @1 (negate @2))
6230 (IFN_FNMA @0 @1 @2))
6231 (simplify
6232 (negate (IFN_FMS@3 @0 @1 @2))
6233 (if (single_use (@3))
6234 (IFN_FNMA @0 @1 @2)))
6235
6236 (simplify
6237 (IFN_FNMA:c (negate @0) @1 @2)
6238 (IFN_FMA @0 @1 @2))
6239 (simplify
6240 (IFN_FNMA @0 @1 (negate @2))
6241 (IFN_FNMS @0 @1 @2))
6242 (simplify
6243 (IFN_FNMA:c (negate @0) @1 (negate @2))
6244 (IFN_FMS @0 @1 @2))
6245 (simplify
6246 (negate (IFN_FNMA@3 @0 @1 @2))
6247 (if (single_use (@3))
6248 (IFN_FMS @0 @1 @2)))
6249
6250 (simplify
6251 (IFN_FNMS:c (negate @0) @1 @2)
6252 (IFN_FMS @0 @1 @2))
6253 (simplify
6254 (IFN_FNMS @0 @1 (negate @2))
6255 (IFN_FNMA @0 @1 @2))
6256 (simplify
6257 (IFN_FNMS:c (negate @0) @1 (negate @2))
6258 (IFN_FMA @0 @1 @2))
6259 (simplify
6260 (negate (IFN_FNMS@3 @0 @1 @2))
6261 (if (single_use (@3))
6262 (IFN_FMA @0 @1 @2))))
6263
6264 /* CLZ simplifications. */
6265 (for clz (CLZ)
6266 (for op (eq ne)
6267 cmp (lt ge)
6268 (simplify
6269 (op (clz:s @0) INTEGER_CST@1)
6270 (if (integer_zerop (@1))
6271 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
6272 (with { tree stype = signed_type_for (TREE_TYPE (@0));
6273 HOST_WIDE_INT val = 0;
6274 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6275 /* Punt on hypothetical weird targets. */
6276 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6277 val) == 2
6278 && val == 0)
6279 stype = NULL_TREE;
6280 #endif
6281 }
6282 (if (stype)
6283 (cmp (convert:stype @0) { build_zero_cst (stype); })))
6284 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
6285 (with { bool ok = true;
6286 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6287 /* Punt on hypothetical weird targets. */
6288 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6289 val) == 2
6290 && val == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
6291 ok = false;
6292 #endif
6293 }
6294 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (TREE_TYPE (@0)) - 1))
6295 (op @0 { build_one_cst (TREE_TYPE (@0)); })))))))
6296
6297 /* POPCOUNT simplifications. */
6298 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
6299 (simplify
6300 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6301 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6302 (POPCOUNT (bit_ior @0 @1))))
6303
6304 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
6305 (for popcount (POPCOUNT)
6306 (for cmp (le eq ne gt)
6307 rep (eq eq ne ne)
6308 (simplify
6309 (cmp (popcount @0) integer_zerop)
6310 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6311
6312 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
6313 (simplify
6314 (bit_and (POPCOUNT @0) integer_onep)
6315 (PARITY @0))
6316
6317 /* PARITY simplifications. */
6318 /* parity(~X) is parity(X). */
6319 (simplify
6320 (PARITY (bit_not @0))
6321 (PARITY @0))
6322
6323 /* parity(X)^parity(Y) is parity(X^Y). */
6324 (simplify
6325 (bit_xor (PARITY:s @0) (PARITY:s @1))
6326 (PARITY (bit_xor @0 @1)))
6327
6328 /* Common POPCOUNT/PARITY simplifications. */
6329 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
6330 (for pfun (POPCOUNT PARITY)
6331 (simplify
6332 (pfun @0)
6333 (with { wide_int nz = tree_nonzero_bits (@0); }
6334 (switch
6335 (if (nz == 1)
6336 (convert @0))
6337 (if (wi::popcount (nz) == 1)
6338 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6339 (convert (rshift:utype (convert:utype @0)
6340 { build_int_cst (integer_type_node,
6341 wi::ctz (nz)); }))))))))
6342
6343 #if GIMPLE
6344 /* 64- and 32-bits branchless implementations of popcount are detected:
6345
6346 int popcount64c (uint64_t x)
6347 {
6348 x -= (x >> 1) & 0x5555555555555555ULL;
6349 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6350 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6351 return (x * 0x0101010101010101ULL) >> 56;
6352 }
6353
6354 int popcount32c (uint32_t x)
6355 {
6356 x -= (x >> 1) & 0x55555555;
6357 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6358 x = (x + (x >> 4)) & 0x0f0f0f0f;
6359 return (x * 0x01010101) >> 24;
6360 } */
6361 (simplify
6362 (rshift
6363 (mult
6364 (bit_and
6365 (plus:c
6366 (rshift @8 INTEGER_CST@5)
6367 (plus:c@8
6368 (bit_and @6 INTEGER_CST@7)
6369 (bit_and
6370 (rshift
6371 (minus@6 @0
6372 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6373 INTEGER_CST@10)
6374 INTEGER_CST@9)))
6375 INTEGER_CST@3)
6376 INTEGER_CST@2)
6377 INTEGER_CST@1)
6378 /* Check constants and optab. */
6379 (with { unsigned prec = TYPE_PRECISION (type);
6380 int shift = (64 - prec) & 63;
6381 unsigned HOST_WIDE_INT c1
6382 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6383 unsigned HOST_WIDE_INT c2
6384 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6385 unsigned HOST_WIDE_INT c3
6386 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6387 unsigned HOST_WIDE_INT c4
6388 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6389 }
6390 (if (prec >= 16
6391 && prec <= 64
6392 && pow2p_hwi (prec)
6393 && TYPE_UNSIGNED (type)
6394 && integer_onep (@4)
6395 && wi::to_widest (@10) == 2
6396 && wi::to_widest (@5) == 4
6397 && wi::to_widest (@1) == prec - 8
6398 && tree_to_uhwi (@2) == c1
6399 && tree_to_uhwi (@3) == c2
6400 && tree_to_uhwi (@9) == c3
6401 && tree_to_uhwi (@7) == c3
6402 && tree_to_uhwi (@11) == c4
6403 && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6404 OPTIMIZE_FOR_BOTH))
6405 (convert (IFN_POPCOUNT:type @0)))))
6406
6407 /* __builtin_ffs needs to deal on many targets with the possible zero
6408 argument. If we know the argument is always non-zero, __builtin_ctz + 1
6409 should lead to better code. */
6410 (simplify
6411 (FFS tree_expr_nonzero_p@0)
6412 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6413 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6414 OPTIMIZE_FOR_SPEED))
6415 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6416 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6417 #endif
6418
6419 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6420 BUILT_IN_FFSIMAX)
6421 /* __builtin_ffs (X) == 0 -> X == 0.
6422 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
6423 (for cmp (eq ne)
6424 (simplify
6425 (cmp (ffs@2 @0) INTEGER_CST@1)
6426 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6427 (switch
6428 (if (integer_zerop (@1))
6429 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6430 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6431 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6432 (if (single_use (@2))
6433 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6434 wi::mask (tree_to_uhwi (@1),
6435 false, prec)); })
6436 { wide_int_to_tree (TREE_TYPE (@0),
6437 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6438 false, prec)); }))))))
6439
6440 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
6441 (for cmp (gt le)
6442 cmp2 (ne eq)
6443 cmp3 (eq ne)
6444 bit_op (bit_and bit_ior)
6445 (simplify
6446 (cmp (ffs@2 @0) INTEGER_CST@1)
6447 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6448 (switch
6449 (if (integer_zerop (@1))
6450 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
6451 (if (tree_int_cst_sgn (@1) < 0)
6452 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
6453 (if (wi::to_widest (@1) >= prec)
6454 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
6455 (if (wi::to_widest (@1) == prec - 1)
6456 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
6457 wi::shifted_mask (prec - 1, 1,
6458 false, prec)); }))
6459 (if (single_use (@2))
6460 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
6461 (cmp3 (bit_and @0
6462 { wide_int_to_tree (TREE_TYPE (@0),
6463 wi::mask (tree_to_uhwi (@1),
6464 false, prec)); })
6465 { build_zero_cst (TREE_TYPE (@0)); }))))))))
6466
6467 /* Simplify:
6468
6469 a = a1 op a2
6470 r = c ? a : b;
6471
6472 to:
6473
6474 r = c ? a1 op a2 : b;
6475
6476 if the target can do it in one go. This makes the operation conditional
6477 on c, so could drop potentially-trapping arithmetic, but that's a valid
6478 simplification if the result of the operation isn't needed.
6479
6480 Avoid speculatively generating a stand-alone vector comparison
6481 on targets that might not support them. Any target implementing
6482 conditional internal functions must support the same comparisons
6483 inside and outside a VEC_COND_EXPR. */
6484
6485 #if GIMPLE
6486 (for uncond_op (UNCOND_BINARY)
6487 cond_op (COND_BINARY)
6488 (simplify
6489 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
6490 (with { tree op_type = TREE_TYPE (@4); }
6491 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6492 && element_precision (type) == element_precision (op_type))
6493 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
6494 (simplify
6495 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
6496 (with { tree op_type = TREE_TYPE (@4); }
6497 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6498 && element_precision (type) == element_precision (op_type))
6499 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
6500
6501 /* Same for ternary operations. */
6502 (for uncond_op (UNCOND_TERNARY)
6503 cond_op (COND_TERNARY)
6504 (simplify
6505 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
6506 (with { tree op_type = TREE_TYPE (@5); }
6507 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6508 && element_precision (type) == element_precision (op_type))
6509 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
6510 (simplify
6511 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
6512 (with { tree op_type = TREE_TYPE (@5); }
6513 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6514 && element_precision (type) == element_precision (op_type))
6515 (view_convert (cond_op (bit_not @0) @2 @3 @4
6516 (view_convert:op_type @1)))))))
6517 #endif
6518
6519 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
6520 "else" value of an IFN_COND_*. */
6521 (for cond_op (COND_BINARY)
6522 (simplify
6523 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
6524 (with { tree op_type = TREE_TYPE (@3); }
6525 (if (element_precision (type) == element_precision (op_type))
6526 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
6527 (simplify
6528 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
6529 (with { tree op_type = TREE_TYPE (@5); }
6530 (if (inverse_conditions_p (@0, @2)
6531 && element_precision (type) == element_precision (op_type))
6532 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
6533
6534 /* Same for ternary operations. */
6535 (for cond_op (COND_TERNARY)
6536 (simplify
6537 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
6538 (with { tree op_type = TREE_TYPE (@4); }
6539 (if (element_precision (type) == element_precision (op_type))
6540 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
6541 (simplify
6542 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
6543 (with { tree op_type = TREE_TYPE (@6); }
6544 (if (inverse_conditions_p (@0, @2)
6545 && element_precision (type) == element_precision (op_type))
6546 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
6547
6548 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
6549 expressions like:
6550
6551 A: (@0 + @1 < @2) | (@2 + @1 < @0)
6552 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
6553
6554 If pointers are known not to wrap, B checks whether @1 bytes starting
6555 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
6556 bytes. A is more efficiently tested as:
6557
6558 A: (sizetype) (@0 + @1 - @2) > @1 * 2
6559
6560 The equivalent expression for B is given by replacing @1 with @1 - 1:
6561
6562 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
6563
6564 @0 and @2 can be swapped in both expressions without changing the result.
6565
6566 The folds rely on sizetype's being unsigned (which is always true)
6567 and on its being the same width as the pointer (which we have to check).
6568
6569 The fold replaces two pointer_plus expressions, two comparisons and
6570 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
6571 the best case it's a saving of two operations. The A fold retains one
6572 of the original pointer_pluses, so is a win even if both pointer_pluses
6573 are used elsewhere. The B fold is a wash if both pointer_pluses are
6574 used elsewhere, since all we end up doing is replacing a comparison with
6575 a pointer_plus. We do still apply the fold under those circumstances
6576 though, in case applying it to other conditions eventually makes one of the
6577 pointer_pluses dead. */
6578 (for ior (truth_orif truth_or bit_ior)
6579 (for cmp (le lt)
6580 (simplify
6581 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
6582 (cmp:cs (pointer_plus@4 @2 @1) @0))
6583 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6584 && TYPE_OVERFLOW_WRAPS (sizetype)
6585 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
6586 /* Calculate the rhs constant. */
6587 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
6588 offset_int rhs = off * 2; }
6589 /* Always fails for negative values. */
6590 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
6591 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
6592 pick a canonical order. This increases the chances of using the
6593 same pointer_plus in multiple checks. */
6594 (with { bool swap_p = tree_swap_operands_p (@0, @2);
6595 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
6596 (if (cmp == LT_EXPR)
6597 (gt (convert:sizetype
6598 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
6599 { swap_p ? @0 : @2; }))
6600 { rhs_tree; })
6601 (gt (convert:sizetype
6602 (pointer_diff:ssizetype
6603 (pointer_plus { swap_p ? @2 : @0; }
6604 { wide_int_to_tree (sizetype, off); })
6605 { swap_p ? @0 : @2; }))
6606 { rhs_tree; })))))))))
6607
6608 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6609 element of @1. */
6610 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6611 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6612 (with { int i = single_nonzero_element (@1); }
6613 (if (i >= 0)
6614 (with { tree elt = vector_cst_elt (@1, i);
6615 tree elt_type = TREE_TYPE (elt);
6616 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6617 tree size = bitsize_int (elt_bits);
6618 tree pos = bitsize_int (elt_bits * i); }
6619 (view_convert
6620 (bit_and:elt_type
6621 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6622 { elt; })))))))
6623
6624 (simplify
6625 (vec_perm @0 @1 VECTOR_CST@2)
6626 (with
6627 {
6628 tree op0 = @0, op1 = @1, op2 = @2;
6629
6630 /* Build a vector of integers from the tree mask. */
6631 vec_perm_builder builder;
6632 if (!tree_to_vec_perm_builder (&builder, op2))
6633 return NULL_TREE;
6634
6635 /* Create a vec_perm_indices for the integer vector. */
6636 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6637 bool single_arg = (op0 == op1);
6638 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6639 }
6640 (if (sel.series_p (0, 1, 0, 1))
6641 { op0; }
6642 (if (sel.series_p (0, 1, nelts, 1))
6643 { op1; }
6644 (with
6645 {
6646 if (!single_arg)
6647 {
6648 if (sel.all_from_input_p (0))
6649 op1 = op0;
6650 else if (sel.all_from_input_p (1))
6651 {
6652 op0 = op1;
6653 sel.rotate_inputs (1);
6654 }
6655 else if (known_ge (poly_uint64 (sel[0]), nelts))
6656 {
6657 std::swap (op0, op1);
6658 sel.rotate_inputs (1);
6659 }
6660 }
6661 gassign *def;
6662 tree cop0 = op0, cop1 = op1;
6663 if (TREE_CODE (op0) == SSA_NAME
6664 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6665 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6666 cop0 = gimple_assign_rhs1 (def);
6667 if (TREE_CODE (op1) == SSA_NAME
6668 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6669 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6670 cop1 = gimple_assign_rhs1 (def);
6671
6672 tree t;
6673 }
6674 (if ((TREE_CODE (cop0) == VECTOR_CST
6675 || TREE_CODE (cop0) == CONSTRUCTOR)
6676 && (TREE_CODE (cop1) == VECTOR_CST
6677 || TREE_CODE (cop1) == CONSTRUCTOR)
6678 && (t = fold_vec_perm (type, cop0, cop1, sel)))
6679 { t; }
6680 (with
6681 {
6682 bool changed = (op0 == op1 && !single_arg);
6683 tree ins = NULL_TREE;
6684 unsigned at = 0;
6685
6686 /* See if the permutation is performing a single element
6687 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6688 in that case. But only if the vector mode is supported,
6689 otherwise this is invalid GIMPLE. */
6690 if (TYPE_MODE (type) != BLKmode
6691 && (TREE_CODE (cop0) == VECTOR_CST
6692 || TREE_CODE (cop0) == CONSTRUCTOR
6693 || TREE_CODE (cop1) == VECTOR_CST
6694 || TREE_CODE (cop1) == CONSTRUCTOR))
6695 {
6696 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6697 if (insert_first_p)
6698 {
6699 /* After canonicalizing the first elt to come from the
6700 first vector we only can insert the first elt from
6701 the first vector. */
6702 at = 0;
6703 if ((ins = fold_read_from_vector (cop0, sel[0])))
6704 op0 = op1;
6705 }
6706 /* The above can fail for two-element vectors which always
6707 appear to insert the first element, so try inserting
6708 into the second lane as well. For more than two
6709 elements that's wasted time. */
6710 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6711 {
6712 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6713 for (at = 0; at < encoded_nelts; ++at)
6714 if (maybe_ne (sel[at], at))
6715 break;
6716 if (at < encoded_nelts
6717 && (known_eq (at + 1, nelts)
6718 || sel.series_p (at + 1, 1, at + 1, 1)))
6719 {
6720 if (known_lt (poly_uint64 (sel[at]), nelts))
6721 ins = fold_read_from_vector (cop0, sel[at]);
6722 else
6723 ins = fold_read_from_vector (cop1, sel[at] - nelts);
6724 }
6725 }
6726 }
6727
6728 /* Generate a canonical form of the selector. */
6729 if (!ins && sel.encoding () != builder)
6730 {
6731 /* Some targets are deficient and fail to expand a single
6732 argument permutation while still allowing an equivalent
6733 2-argument version. */
6734 tree oldop2 = op2;
6735 if (sel.ninputs () == 2
6736 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6737 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6738 else
6739 {
6740 vec_perm_indices sel2 (builder, 2, nelts);
6741 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6742 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6743 else
6744 /* Not directly supported with either encoding,
6745 so use the preferred form. */
6746 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6747 }
6748 if (!operand_equal_p (op2, oldop2, 0))
6749 changed = true;
6750 }
6751 }
6752 (if (ins)
6753 (bit_insert { op0; } { ins; }
6754 { bitsize_int (at * vector_element_bits (type)); })
6755 (if (changed)
6756 (vec_perm { op0; } { op1; } { op2; }))))))))))
6757
6758 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
6759
6760 (match vec_same_elem_p
6761 @0
6762 (if (uniform_vector_p (@0))))
6763
6764 (match vec_same_elem_p
6765 (vec_duplicate @0))
6766
6767 (simplify
6768 (vec_perm vec_same_elem_p@0 @0 @1)
6769 @0)
6770
6771 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6772 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6773 constant which when multiplied by a power of 2 contains a unique value
6774 in the top 5 or 6 bits. This is then indexed into a table which maps it
6775 to the number of trailing zeroes. */
6776 (match (ctz_table_index @1 @2 @3)
6777 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))