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