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