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