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