Move tan simplifications to match.pd
[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-2015 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 integer_pow2p
35 HONOR_NANS)
36
37 /* Operator lists. */
38 (define_operator_list tcc_comparison
39 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
40 (define_operator_list inverted_tcc_comparison
41 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
42 (define_operator_list inverted_tcc_comparison_with_nans
43 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list swapped_tcc_comparison
45 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
46 (define_operator_list simple_comparison lt le eq ne ge gt)
47 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
48
49 (define_operator_list LOG BUILT_IN_LOGF BUILT_IN_LOG BUILT_IN_LOGL)
50 (define_operator_list EXP BUILT_IN_EXPF BUILT_IN_EXP BUILT_IN_EXPL)
51 (define_operator_list LOG2 BUILT_IN_LOG2F BUILT_IN_LOG2 BUILT_IN_LOG2L)
52 (define_operator_list EXP2 BUILT_IN_EXP2F BUILT_IN_EXP2 BUILT_IN_EXP2L)
53 (define_operator_list LOG10 BUILT_IN_LOG10F BUILT_IN_LOG10 BUILT_IN_LOG10L)
54 (define_operator_list EXP10 BUILT_IN_EXP10F BUILT_IN_EXP10 BUILT_IN_EXP10L)
55 (define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL)
56 (define_operator_list POW10 BUILT_IN_POW10F BUILT_IN_POW10 BUILT_IN_POW10L)
57 (define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL)
58 (define_operator_list CBRT BUILT_IN_CBRTF BUILT_IN_CBRT BUILT_IN_CBRTL)
59 (define_operator_list SIN BUILT_IN_SINF BUILT_IN_SIN BUILT_IN_SINL)
60 (define_operator_list COS BUILT_IN_COSF BUILT_IN_COS BUILT_IN_COSL)
61 (define_operator_list TAN BUILT_IN_TANF BUILT_IN_TAN BUILT_IN_TANL)
62 (define_operator_list ATAN BUILT_IN_ATANF BUILT_IN_ATAN BUILT_IN_ATANL)
63 (define_operator_list COSH BUILT_IN_COSHF BUILT_IN_COSH BUILT_IN_COSHL)
64 (define_operator_list CEXPI BUILT_IN_CEXPIF BUILT_IN_CEXPI BUILT_IN_CEXPIL)
65 (define_operator_list CPROJ BUILT_IN_CPROJF BUILT_IN_CPROJ BUILT_IN_CPROJL)
66 (define_operator_list CCOS BUILT_IN_CCOSF BUILT_IN_CCOS BUILT_IN_CCOSL)
67 (define_operator_list CCOSH BUILT_IN_CCOSHF BUILT_IN_CCOSH BUILT_IN_CCOSHL)
68 (define_operator_list HYPOT BUILT_IN_HYPOTF BUILT_IN_HYPOT BUILT_IN_HYPOTL)
69 (define_operator_list COPYSIGN BUILT_IN_COPYSIGNF
70 BUILT_IN_COPYSIGN
71 BUILT_IN_COPYSIGNL)
72 (define_operator_list CABS BUILT_IN_CABSF BUILT_IN_CABS BUILT_IN_CABSL)
73
74 /* Simplifications of operations with one constant operand and
75 simplifications to constants or single values. */
76
77 (for op (plus pointer_plus minus bit_ior bit_xor)
78 (simplify
79 (op @0 integer_zerop)
80 (non_lvalue @0)))
81
82 /* 0 +p index -> (type)index */
83 (simplify
84 (pointer_plus integer_zerop @1)
85 (non_lvalue (convert @1)))
86
87 /* See if ARG1 is zero and X + ARG1 reduces to X.
88 Likewise if the operands are reversed. */
89 (simplify
90 (plus:c @0 real_zerop@1)
91 (if (fold_real_zero_addition_p (type, @1, 0))
92 (non_lvalue @0)))
93
94 /* See if ARG1 is zero and X - ARG1 reduces to X. */
95 (simplify
96 (minus @0 real_zerop@1)
97 (if (fold_real_zero_addition_p (type, @1, 1))
98 (non_lvalue @0)))
99
100 /* Simplify x - x.
101 This is unsafe for certain floats even in non-IEEE formats.
102 In IEEE, it is unsafe because it does wrong for NaNs.
103 Also note that operand_equal_p is always false if an operand
104 is volatile. */
105 (simplify
106 (minus @0 @0)
107 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
108 { build_zero_cst (type); }))
109
110 (simplify
111 (mult @0 integer_zerop@1)
112 @1)
113
114 /* Maybe fold x * 0 to 0. The expressions aren't the same
115 when x is NaN, since x * 0 is also NaN. Nor are they the
116 same in modes with signed zeros, since multiplying a
117 negative value by 0 gives -0, not +0. */
118 (simplify
119 (mult @0 real_zerop@1)
120 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
121 @1))
122
123 /* In IEEE floating point, x*1 is not equivalent to x for snans.
124 Likewise for complex arithmetic with signed zeros. */
125 (simplify
126 (mult @0 real_onep)
127 (if (!HONOR_SNANS (type)
128 && (!HONOR_SIGNED_ZEROS (type)
129 || !COMPLEX_FLOAT_TYPE_P (type)))
130 (non_lvalue @0)))
131
132 /* Transform x * -1.0 into -x. */
133 (simplify
134 (mult @0 real_minus_onep)
135 (if (!HONOR_SNANS (type)
136 && (!HONOR_SIGNED_ZEROS (type)
137 || !COMPLEX_FLOAT_TYPE_P (type)))
138 (negate @0)))
139
140 /* Make sure to preserve divisions by zero. This is the reason why
141 we don't simplify x / x to 1 or 0 / x to 0. */
142 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
143 (simplify
144 (op @0 integer_onep)
145 (non_lvalue @0)))
146
147 /* X / -1 is -X. */
148 (for div (trunc_div ceil_div floor_div round_div exact_div)
149 (simplify
150 (div @0 integer_minus_onep@1)
151 (if (!TYPE_UNSIGNED (type))
152 (negate @0))))
153
154 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
155 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
156 (simplify
157 (floor_div @0 @1)
158 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
159 && TYPE_UNSIGNED (type))
160 (trunc_div @0 @1)))
161
162 /* Combine two successive divisions. Note that combining ceil_div
163 and floor_div is trickier and combining round_div even more so. */
164 (for div (trunc_div exact_div)
165 (simplify
166 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
167 (with {
168 bool overflow_p;
169 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
170 }
171 (if (!overflow_p)
172 (div @0 { wide_int_to_tree (type, mul); })
173 (if (TYPE_UNSIGNED (type)
174 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
175 { build_zero_cst (type); })))))
176
177 /* Optimize A / A to 1.0 if we don't care about
178 NaNs or Infinities. */
179 (simplify
180 (rdiv @0 @0)
181 (if (FLOAT_TYPE_P (type)
182 && ! HONOR_NANS (type)
183 && ! HONOR_INFINITIES (type))
184 { build_one_cst (type); }))
185
186 /* Optimize -A / A to -1.0 if we don't care about
187 NaNs or Infinities. */
188 (simplify
189 (rdiv:c @0 (negate @0))
190 (if (FLOAT_TYPE_P (type)
191 && ! HONOR_NANS (type)
192 && ! HONOR_INFINITIES (type))
193 { build_minus_one_cst (type); }))
194
195 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
196 (simplify
197 (rdiv @0 real_onep)
198 (if (!HONOR_SNANS (type))
199 (non_lvalue @0)))
200
201 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
202 (simplify
203 (rdiv @0 real_minus_onep)
204 (if (!HONOR_SNANS (type))
205 (negate @0)))
206
207 /* If ARG1 is a constant, we can convert this to a multiply by the
208 reciprocal. This does not have the same rounding properties,
209 so only do this if -freciprocal-math. We can actually
210 always safely do it if ARG1 is a power of two, but it's hard to
211 tell if it is or not in a portable manner. */
212 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
213 (simplify
214 (rdiv @0 cst@1)
215 (if (optimize)
216 (if (flag_reciprocal_math
217 && !real_zerop (@1))
218 (with
219 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
220 (if (tem)
221 (mult @0 { tem; } )))
222 (if (cst != COMPLEX_CST)
223 (with { tree inverse = exact_inverse (type, @1); }
224 (if (inverse)
225 (mult @0 { inverse; } ))))))))
226
227 /* Same applies to modulo operations, but fold is inconsistent here
228 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
229 (for mod (ceil_mod floor_mod round_mod trunc_mod)
230 /* 0 % X is always zero. */
231 (simplify
232 (mod integer_zerop@0 @1)
233 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
234 (if (!integer_zerop (@1))
235 @0))
236 /* X % 1 is always zero. */
237 (simplify
238 (mod @0 integer_onep)
239 { build_zero_cst (type); })
240 /* X % -1 is zero. */
241 (simplify
242 (mod @0 integer_minus_onep@1)
243 (if (!TYPE_UNSIGNED (type))
244 { build_zero_cst (type); }))
245 /* (X % Y) % Y is just X % Y. */
246 (simplify
247 (mod (mod@2 @0 @1) @1)
248 @2)
249 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
250 (simplify
251 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
252 (if (ANY_INTEGRAL_TYPE_P (type)
253 && TYPE_OVERFLOW_UNDEFINED (type)
254 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
255 { build_zero_cst (type); })))
256
257 /* X % -C is the same as X % C. */
258 (simplify
259 (trunc_mod @0 INTEGER_CST@1)
260 (if (TYPE_SIGN (type) == SIGNED
261 && !TREE_OVERFLOW (@1)
262 && wi::neg_p (@1)
263 && !TYPE_OVERFLOW_TRAPS (type)
264 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
265 && !sign_bit_p (@1, @1))
266 (trunc_mod @0 (negate @1))))
267
268 /* X % -Y is the same as X % Y. */
269 (simplify
270 (trunc_mod @0 (convert? (negate @1)))
271 (if (!TYPE_UNSIGNED (type)
272 && !TYPE_OVERFLOW_TRAPS (type)
273 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
274 (trunc_mod @0 (convert @1))))
275
276 /* X - (X / Y) * Y is the same as X % Y. */
277 (simplify
278 (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1)))
279 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
280 && TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (type))
281 (trunc_mod (convert @0) (convert @1))))
282
283 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
284 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
285 Also optimize A % (C << N) where C is a power of 2,
286 to A & ((C << N) - 1). */
287 (match (power_of_two_cand @1)
288 INTEGER_CST@1)
289 (match (power_of_two_cand @1)
290 (lshift INTEGER_CST@1 @2))
291 (for mod (trunc_mod floor_mod)
292 (simplify
293 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
294 (if ((TYPE_UNSIGNED (type)
295 || tree_expr_nonnegative_p (@0))
296 && tree_nop_conversion_p (type, TREE_TYPE (@3))
297 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
298 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
299
300 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
301 (simplify
302 (trunc_div (mult @0 integer_pow2p@1) @1)
303 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
304 (bit_and @0 { wide_int_to_tree
305 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
306 false, TYPE_PRECISION (type))); })))
307
308 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
309 (simplify
310 (mult (trunc_div @0 integer_pow2p@1) @1)
311 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
312 (bit_and @0 (negate @1))))
313
314 /* Simplify (t * 2) / 2) -> t. */
315 (for div (trunc_div ceil_div floor_div round_div exact_div)
316 (simplify
317 (div (mult @0 @1) @1)
318 (if (ANY_INTEGRAL_TYPE_P (type)
319 && TYPE_OVERFLOW_UNDEFINED (type))
320 @0)))
321
322 (for op (negate abs)
323 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
324 (for coss (COS COSH)
325 (simplify
326 (coss (op @0))
327 (coss @0)))
328 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
329 (for pows (POW)
330 (simplify
331 (pows (op @0) REAL_CST@1)
332 (with { HOST_WIDE_INT n; }
333 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
334 (pows @0 @1)))))
335 /* Strip negate and abs from both operands of hypot. */
336 (for hypots (HYPOT)
337 (simplify
338 (hypots (op @0) @1)
339 (hypots @0 @1))
340 (simplify
341 (hypots @0 (op @1))
342 (hypots @0 @1)))
343 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
344 (for copysigns (COPYSIGN)
345 (simplify
346 (copysigns (op @0) @1)
347 (copysigns @0 @1))))
348
349 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
350 (simplify
351 (mult (abs@1 @0) @1)
352 (mult @0 @0))
353
354 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
355 (for coss (COS COSH)
356 copysigns (COPYSIGN)
357 (simplify
358 (coss (copysigns @0 @1))
359 (coss @0)))
360
361 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
362 (for pows (POW)
363 copysigns (COPYSIGN)
364 (simplify
365 (pows (copysigns @0 @1) REAL_CST@1)
366 (with { HOST_WIDE_INT n; }
367 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
368 (pows @0 @1)))))
369
370 (for hypots (HYPOT)
371 copysigns (COPYSIGN)
372 /* hypot(copysign(x, y), z) -> hypot(x, z). */
373 (simplify
374 (hypots (copysigns @0 @1) @2)
375 (hypots @0 @2))
376 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
377 (simplify
378 (hypots @0 (copysigns @1 @2))
379 (hypots @0 @1)))
380
381 /* copysign(copysign(x, y), z) -> copysign(x, z). */
382 (for copysigns (COPYSIGN)
383 (simplify
384 (copysigns (copysigns @0 @1) @2)
385 (copysigns @0 @2)))
386
387 /* copysign(x,y)*copysign(x,y) -> x*x. */
388 (for copysigns (COPYSIGN)
389 (simplify
390 (mult (copysigns@2 @0 @1) @2)
391 (mult @0 @0)))
392
393 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
394 (for ccoss (CCOS CCOSH)
395 (simplify
396 (ccoss (negate @0))
397 (ccoss @0)))
398
399 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
400 (for ops (conj negate)
401 (for cabss (CABS)
402 (simplify
403 (cabss (ops @0))
404 (cabss @0))))
405
406 /* Fold (a * (1 << b)) into (a << b) */
407 (simplify
408 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
409 (if (! FLOAT_TYPE_P (type)
410 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
411 (lshift @0 @2)))
412
413 /* Fold (C1/X)*C2 into (C1*C2)/X. */
414 (simplify
415 (mult (rdiv:s REAL_CST@0 @1) REAL_CST@2)
416 (if (flag_associative_math)
417 (with
418 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
419 (if (tem)
420 (rdiv { tem; } @1)))))
421
422 /* Simplify ~X & X as zero. */
423 (simplify
424 (bit_and:c (convert? @0) (convert? (bit_not @0)))
425 { build_zero_cst (type); })
426
427 /* X % Y is smaller than Y. */
428 (for cmp (lt ge)
429 (simplify
430 (cmp (trunc_mod @0 @1) @1)
431 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
432 { constant_boolean_node (cmp == LT_EXPR, type); })))
433 (for cmp (gt le)
434 (simplify
435 (cmp @1 (trunc_mod @0 @1))
436 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
437 { constant_boolean_node (cmp == GT_EXPR, type); })))
438
439 /* x | ~0 -> ~0 */
440 (simplify
441 (bit_ior @0 integer_all_onesp@1)
442 @1)
443
444 /* x & 0 -> 0 */
445 (simplify
446 (bit_and @0 integer_zerop@1)
447 @1)
448
449 /* ~x | x -> -1 */
450 /* ~x ^ x -> -1 */
451 /* ~x + x -> -1 */
452 (for op (bit_ior bit_xor plus)
453 (simplify
454 (op:c (convert? @0) (convert? (bit_not @0)))
455 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
456
457 /* x ^ x -> 0 */
458 (simplify
459 (bit_xor @0 @0)
460 { build_zero_cst (type); })
461
462 /* Canonicalize X ^ ~0 to ~X. */
463 (simplify
464 (bit_xor @0 integer_all_onesp@1)
465 (bit_not @0))
466
467 /* x & ~0 -> x */
468 (simplify
469 (bit_and @0 integer_all_onesp)
470 (non_lvalue @0))
471
472 /* x & x -> x, x | x -> x */
473 (for bitop (bit_and bit_ior)
474 (simplify
475 (bitop @0 @0)
476 (non_lvalue @0)))
477
478 /* x + (x & 1) -> (x + 1) & ~1 */
479 (simplify
480 (plus:c @0 (bit_and:s @0 integer_onep@1))
481 (bit_and (plus @0 @1) (bit_not @1)))
482
483 /* x & ~(x & y) -> x & ~y */
484 /* x | ~(x | y) -> x | ~y */
485 (for bitop (bit_and bit_ior)
486 (simplify
487 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
488 (bitop @0 (bit_not @1))))
489
490 /* (x | y) & ~x -> y & ~x */
491 /* (x & y) | ~x -> y | ~x */
492 (for bitop (bit_and bit_ior)
493 rbitop (bit_ior bit_and)
494 (simplify
495 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
496 (bitop @1 @2)))
497
498 /* (x & y) ^ (x | y) -> x ^ y */
499 (simplify
500 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
501 (bit_xor @0 @1))
502
503 /* (x ^ y) ^ (x | y) -> x & y */
504 (simplify
505 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
506 (bit_and @0 @1))
507
508 /* (x & y) + (x ^ y) -> x | y */
509 /* (x & y) | (x ^ y) -> x | y */
510 /* (x & y) ^ (x ^ y) -> x | y */
511 (for op (plus bit_ior bit_xor)
512 (simplify
513 (op:c (bit_and @0 @1) (bit_xor @0 @1))
514 (bit_ior @0 @1)))
515
516 /* (x & y) + (x | y) -> x + y */
517 (simplify
518 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
519 (plus @0 @1))
520
521 /* (x + y) - (x | y) -> x & y */
522 (simplify
523 (minus (plus @0 @1) (bit_ior @0 @1))
524 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
525 && !TYPE_SATURATING (type))
526 (bit_and @0 @1)))
527
528 /* (x + y) - (x & y) -> x | y */
529 (simplify
530 (minus (plus @0 @1) (bit_and @0 @1))
531 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
532 && !TYPE_SATURATING (type))
533 (bit_ior @0 @1)))
534
535 /* (x | y) - (x ^ y) -> x & y */
536 (simplify
537 (minus (bit_ior @0 @1) (bit_xor @0 @1))
538 (bit_and @0 @1))
539
540 /* (x | y) - (x & y) -> x ^ y */
541 (simplify
542 (minus (bit_ior @0 @1) (bit_and @0 @1))
543 (bit_xor @0 @1))
544
545 /* (x | y) & ~(x & y) -> x ^ y */
546 (simplify
547 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
548 (bit_xor @0 @1))
549
550 /* (x | y) & (~x ^ y) -> x & y */
551 (simplify
552 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
553 (bit_and @0 @1))
554
555 /* ~x & ~y -> ~(x | y)
556 ~x | ~y -> ~(x & y) */
557 (for op (bit_and bit_ior)
558 rop (bit_ior bit_and)
559 (simplify
560 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
561 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
562 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
563 (bit_not (rop (convert @0) (convert @1))))))
564
565 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
566 with a constant, and the two constants have no bits in common,
567 we should treat this as a BIT_IOR_EXPR since this may produce more
568 simplifications. */
569 (for op (bit_xor plus)
570 (simplify
571 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
572 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
573 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
574 && tree_nop_conversion_p (type, TREE_TYPE (@2))
575 && wi::bit_and (@1, @3) == 0)
576 (bit_ior (convert @4) (convert @5)))))
577
578 /* (X | Y) ^ X -> Y & ~ X*/
579 (simplify
580 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
581 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
582 (convert (bit_and @1 (bit_not @0)))))
583
584 /* Convert ~X ^ ~Y to X ^ Y. */
585 (simplify
586 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
587 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
588 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
589 (bit_xor (convert @0) (convert @1))))
590
591 /* Convert ~X ^ C to X ^ ~C. */
592 (simplify
593 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
594 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
595 (bit_xor (convert @0) (bit_not @1))))
596
597 /* Fold (X & Y) ^ Y as ~X & Y. */
598 (simplify
599 (bit_xor:c (bit_and:c @0 @1) @1)
600 (bit_and (bit_not @0) @1))
601
602 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
603 operands are another bit-wise operation with a common input. If so,
604 distribute the bit operations to save an operation and possibly two if
605 constants are involved. For example, convert
606 (A | B) & (A | C) into A | (B & C)
607 Further simplification will occur if B and C are constants. */
608 (for op (bit_and bit_ior)
609 rop (bit_ior bit_and)
610 (simplify
611 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
612 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
613 (rop (convert @0) (op (convert @1) (convert @2))))))
614
615
616 (simplify
617 (abs (abs@1 @0))
618 @1)
619 (simplify
620 (abs (negate @0))
621 (abs @0))
622 (simplify
623 (abs tree_expr_nonnegative_p@0)
624 @0)
625
626 /* A few cases of fold-const.c negate_expr_p predicate. */
627 (match negate_expr_p
628 INTEGER_CST
629 (if ((INTEGRAL_TYPE_P (type)
630 && TYPE_OVERFLOW_WRAPS (type))
631 || (!TYPE_OVERFLOW_SANITIZED (type)
632 && may_negate_without_overflow_p (t)))))
633 (match negate_expr_p
634 FIXED_CST)
635 (match negate_expr_p
636 (negate @0)
637 (if (!TYPE_OVERFLOW_SANITIZED (type))))
638 (match negate_expr_p
639 REAL_CST
640 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
641 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
642 ways. */
643 (match negate_expr_p
644 VECTOR_CST
645 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
646
647 /* (-A) * (-B) -> A * B */
648 (simplify
649 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
650 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
651 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
652 (mult (convert @0) (convert (negate @1)))))
653
654 /* -(A + B) -> (-B) - A. */
655 (simplify
656 (negate (plus:c @0 negate_expr_p@1))
657 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
658 && !HONOR_SIGNED_ZEROS (element_mode (type)))
659 (minus (negate @1) @0)))
660
661 /* A - B -> A + (-B) if B is easily negatable. */
662 (simplify
663 (minus @0 negate_expr_p@1)
664 (if (!FIXED_POINT_TYPE_P (type))
665 (plus @0 (negate @1))))
666
667 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
668 when profitable.
669 For bitwise binary operations apply operand conversions to the
670 binary operation result instead of to the operands. This allows
671 to combine successive conversions and bitwise binary operations.
672 We combine the above two cases by using a conditional convert. */
673 (for bitop (bit_and bit_ior bit_xor)
674 (simplify
675 (bitop (convert @0) (convert? @1))
676 (if (((TREE_CODE (@1) == INTEGER_CST
677 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
678 && int_fits_type_p (@1, TREE_TYPE (@0)))
679 || types_match (@0, @1))
680 /* ??? This transform conflicts with fold-const.c doing
681 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
682 constants (if x has signed type, the sign bit cannot be set
683 in c). This folds extension into the BIT_AND_EXPR.
684 Restrict it to GIMPLE to avoid endless recursions. */
685 && (bitop != BIT_AND_EXPR || GIMPLE)
686 && (/* That's a good idea if the conversion widens the operand, thus
687 after hoisting the conversion the operation will be narrower. */
688 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
689 /* It's also a good idea if the conversion is to a non-integer
690 mode. */
691 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
692 /* Or if the precision of TO is not the same as the precision
693 of its mode. */
694 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
695 (convert (bitop @0 (convert @1))))))
696
697 (for bitop (bit_and bit_ior)
698 rbitop (bit_ior bit_and)
699 /* (x | y) & x -> x */
700 /* (x & y) | x -> x */
701 (simplify
702 (bitop:c (rbitop:c @0 @1) @0)
703 @0)
704 /* (~x | y) & x -> x & y */
705 /* (~x & y) | x -> x | y */
706 (simplify
707 (bitop:c (rbitop:c (bit_not @0) @1) @0)
708 (bitop @0 @1)))
709
710 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
711 (for bitop (bit_and bit_ior bit_xor)
712 (simplify
713 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
714 (bit_and (bitop @0 @2) @1)))
715
716 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
717 (simplify
718 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
719 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
720
721 /* Combine successive equal operations with constants. */
722 (for bitop (bit_and bit_ior bit_xor)
723 (simplify
724 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
725 (bitop @0 (bitop @1 @2))))
726
727 /* Try simple folding for X op !X, and X op X with the help
728 of the truth_valued_p and logical_inverted_value predicates. */
729 (match truth_valued_p
730 @0
731 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
732 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
733 (match truth_valued_p
734 (op @0 @1)))
735 (match truth_valued_p
736 (truth_not @0))
737
738 (match (logical_inverted_value @0)
739 (truth_not @0))
740 (match (logical_inverted_value @0)
741 (bit_not truth_valued_p@0))
742 (match (logical_inverted_value @0)
743 (eq @0 integer_zerop))
744 (match (logical_inverted_value @0)
745 (ne truth_valued_p@0 integer_truep))
746 (match (logical_inverted_value @0)
747 (bit_xor truth_valued_p@0 integer_truep))
748
749 /* X & !X -> 0. */
750 (simplify
751 (bit_and:c @0 (logical_inverted_value @0))
752 { build_zero_cst (type); })
753 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
754 (for op (bit_ior bit_xor)
755 (simplify
756 (op:c truth_valued_p@0 (logical_inverted_value @0))
757 { constant_boolean_node (true, type); }))
758 /* X ==/!= !X is false/true. */
759 (for op (eq ne)
760 (simplify
761 (op:c truth_valued_p@0 (logical_inverted_value @0))
762 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
763
764 /* If arg1 and arg2 are booleans (or any single bit type)
765 then try to simplify:
766
767 (~X & Y) -> X < Y
768 (X & ~Y) -> Y < X
769 (~X | Y) -> X <= Y
770 (X | ~Y) -> Y <= X
771
772 But only do this if our result feeds into a comparison as
773 this transformation is not always a win, particularly on
774 targets with and-not instructions.
775 -> simplify_bitwise_binary_boolean */
776 (simplify
777 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
778 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
779 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
780 (lt @0 @1)))
781 (simplify
782 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
783 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
784 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
785 (le @0 @1)))
786
787 /* ~~x -> x */
788 (simplify
789 (bit_not (bit_not @0))
790 @0)
791
792 /* Convert ~ (-A) to A - 1. */
793 (simplify
794 (bit_not (convert? (negate @0)))
795 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
796 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
797
798 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
799 (simplify
800 (bit_not (convert? (minus @0 integer_each_onep)))
801 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
802 (convert (negate @0))))
803 (simplify
804 (bit_not (convert? (plus @0 integer_all_onesp)))
805 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
806 (convert (negate @0))))
807
808 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
809 (simplify
810 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
811 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
812 (convert (bit_xor @0 (bit_not @1)))))
813 (simplify
814 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
815 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
816 (convert (bit_xor @0 @1))))
817
818 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
819 (simplify
820 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
821 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
822
823 /* Fold A - (A & B) into ~B & A. */
824 (simplify
825 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
826 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
827 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
828 (convert (bit_and (bit_not @1) @0))))
829
830
831
832 /* ((X inner_op C0) outer_op C1)
833 With X being a tree where value_range has reasoned certain bits to always be
834 zero throughout its computed value range,
835 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
836 where zero_mask has 1's for all bits that are sure to be 0 in
837 and 0's otherwise.
838 if (inner_op == '^') C0 &= ~C1;
839 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
840 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
841 */
842 (for inner_op (bit_ior bit_xor)
843 outer_op (bit_xor bit_ior)
844 (simplify
845 (outer_op
846 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
847 (with
848 {
849 bool fail = false;
850 wide_int zero_mask_not;
851 wide_int C0;
852 wide_int cst_emit;
853
854 if (TREE_CODE (@2) == SSA_NAME)
855 zero_mask_not = get_nonzero_bits (@2);
856 else
857 fail = true;
858
859 if (inner_op == BIT_XOR_EXPR)
860 {
861 C0 = wi::bit_and_not (@0, @1);
862 cst_emit = wi::bit_or (C0, @1);
863 }
864 else
865 {
866 C0 = @0;
867 cst_emit = wi::bit_xor (@0, @1);
868 }
869 }
870 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
871 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
872 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
873 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
874
875 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
876 (simplify
877 (pointer_plus (pointer_plus:s @0 @1) @3)
878 (pointer_plus @0 (plus @1 @3)))
879
880 /* Pattern match
881 tem1 = (long) ptr1;
882 tem2 = (long) ptr2;
883 tem3 = tem2 - tem1;
884 tem4 = (unsigned long) tem3;
885 tem5 = ptr1 + tem4;
886 and produce
887 tem5 = ptr2; */
888 (simplify
889 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
890 /* Conditionally look through a sign-changing conversion. */
891 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
892 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
893 || (GENERIC && type == TREE_TYPE (@1))))
894 @1))
895
896 /* Pattern match
897 tem = (sizetype) ptr;
898 tem = tem & algn;
899 tem = -tem;
900 ... = ptr p+ tem;
901 and produce the simpler and easier to analyze with respect to alignment
902 ... = ptr & ~algn; */
903 (simplify
904 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
905 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
906 (bit_and @0 { algn; })))
907
908 /* Try folding difference of addresses. */
909 (simplify
910 (minus (convert ADDR_EXPR@0) (convert @1))
911 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
912 (with { HOST_WIDE_INT diff; }
913 (if (ptr_difference_const (@0, @1, &diff))
914 { build_int_cst_type (type, diff); }))))
915 (simplify
916 (minus (convert @0) (convert ADDR_EXPR@1))
917 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
918 (with { HOST_WIDE_INT diff; }
919 (if (ptr_difference_const (@0, @1, &diff))
920 { build_int_cst_type (type, diff); }))))
921
922 /* If arg0 is derived from the address of an object or function, we may
923 be able to fold this expression using the object or function's
924 alignment. */
925 (simplify
926 (bit_and (convert? @0) INTEGER_CST@1)
927 (if (POINTER_TYPE_P (TREE_TYPE (@0))
928 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
929 (with
930 {
931 unsigned int align;
932 unsigned HOST_WIDE_INT bitpos;
933 get_pointer_alignment_1 (@0, &align, &bitpos);
934 }
935 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
936 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
937
938
939 /* We can't reassociate at all for saturating types. */
940 (if (!TYPE_SATURATING (type))
941
942 /* Contract negates. */
943 /* A + (-B) -> A - B */
944 (simplify
945 (plus:c (convert1? @0) (convert2? (negate @1)))
946 /* Apply STRIP_NOPS on @0 and the negate. */
947 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
948 && tree_nop_conversion_p (type, TREE_TYPE (@1))
949 && !TYPE_OVERFLOW_SANITIZED (type))
950 (minus (convert @0) (convert @1))))
951 /* A - (-B) -> A + B */
952 (simplify
953 (minus (convert1? @0) (convert2? (negate @1)))
954 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
955 && tree_nop_conversion_p (type, TREE_TYPE (@1))
956 && !TYPE_OVERFLOW_SANITIZED (type))
957 (plus (convert @0) (convert @1))))
958 /* -(-A) -> A */
959 (simplify
960 (negate (convert? (negate @1)))
961 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
962 && !TYPE_OVERFLOW_SANITIZED (type))
963 (convert @1)))
964
965 /* We can't reassociate floating-point unless -fassociative-math
966 or fixed-point plus or minus because of saturation to +-Inf. */
967 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
968 && !FIXED_POINT_TYPE_P (type))
969
970 /* Match patterns that allow contracting a plus-minus pair
971 irrespective of overflow issues. */
972 /* (A +- B) - A -> +- B */
973 /* (A +- B) -+ B -> A */
974 /* A - (A +- B) -> -+ B */
975 /* A +- (B -+ A) -> +- B */
976 (simplify
977 (minus (plus:c @0 @1) @0)
978 @1)
979 (simplify
980 (minus (minus @0 @1) @0)
981 (negate @1))
982 (simplify
983 (plus:c (minus @0 @1) @1)
984 @0)
985 (simplify
986 (minus @0 (plus:c @0 @1))
987 (negate @1))
988 (simplify
989 (minus @0 (minus @0 @1))
990 @1)
991
992 /* (A +- CST) +- CST -> A + CST */
993 (for outer_op (plus minus)
994 (for inner_op (plus minus)
995 (simplify
996 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
997 /* If the constant operation overflows we cannot do the transform
998 as we would introduce undefined overflow, for example
999 with (a - 1) + INT_MIN. */
1000 (with { tree cst = fold_binary (outer_op == inner_op
1001 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1002 (if (cst && !TREE_OVERFLOW (cst))
1003 (inner_op @0 { cst; } ))))))
1004
1005 /* (CST - A) +- CST -> CST - A */
1006 (for outer_op (plus minus)
1007 (simplify
1008 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1009 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
1010 (if (cst && !TREE_OVERFLOW (cst))
1011 (minus { cst; } @0)))))
1012
1013 /* ~A + A -> -1 */
1014 (simplify
1015 (plus:c (bit_not @0) @0)
1016 (if (!TYPE_OVERFLOW_TRAPS (type))
1017 { build_all_ones_cst (type); }))
1018
1019 /* ~A + 1 -> -A */
1020 (simplify
1021 (plus (convert? (bit_not @0)) integer_each_onep)
1022 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1023 (negate (convert @0))))
1024
1025 /* -A - 1 -> ~A */
1026 (simplify
1027 (minus (convert? (negate @0)) integer_each_onep)
1028 (if (!TYPE_OVERFLOW_TRAPS (type)
1029 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1030 (bit_not (convert @0))))
1031
1032 /* -1 - A -> ~A */
1033 (simplify
1034 (minus integer_all_onesp @0)
1035 (bit_not @0))
1036
1037 /* (T)(P + A) - (T)P -> (T) A */
1038 (for add (plus pointer_plus)
1039 (simplify
1040 (minus (convert (add @0 @1))
1041 (convert @0))
1042 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1043 /* For integer types, if A has a smaller type
1044 than T the result depends on the possible
1045 overflow in P + A.
1046 E.g. T=size_t, A=(unsigned)429497295, P>0.
1047 However, if an overflow in P + A would cause
1048 undefined behavior, we can assume that there
1049 is no overflow. */
1050 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1051 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1052 /* For pointer types, if the conversion of A to the
1053 final type requires a sign- or zero-extension,
1054 then we have to punt - it is not defined which
1055 one is correct. */
1056 || (POINTER_TYPE_P (TREE_TYPE (@0))
1057 && TREE_CODE (@1) == INTEGER_CST
1058 && tree_int_cst_sign_bit (@1) == 0))
1059 (convert @1))))))
1060
1061
1062 /* Simplifications of MIN_EXPR and MAX_EXPR. */
1063
1064 (for minmax (min max)
1065 (simplify
1066 (minmax @0 @0)
1067 @0))
1068 (simplify
1069 (min @0 @1)
1070 (if (INTEGRAL_TYPE_P (type)
1071 && TYPE_MIN_VALUE (type)
1072 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1073 @1))
1074 (simplify
1075 (max @0 @1)
1076 (if (INTEGRAL_TYPE_P (type)
1077 && TYPE_MAX_VALUE (type)
1078 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1079 @1))
1080
1081
1082 /* Simplifications of shift and rotates. */
1083
1084 (for rotate (lrotate rrotate)
1085 (simplify
1086 (rotate integer_all_onesp@0 @1)
1087 @0))
1088
1089 /* Optimize -1 >> x for arithmetic right shifts. */
1090 (simplify
1091 (rshift integer_all_onesp@0 @1)
1092 (if (!TYPE_UNSIGNED (type)
1093 && tree_expr_nonnegative_p (@1))
1094 @0))
1095
1096 /* Optimize (x >> c) << c into x & (-1<<c). */
1097 (simplify
1098 (lshift (rshift @0 INTEGER_CST@1) @1)
1099 (if (wi::ltu_p (@1, element_precision (type)))
1100 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1101
1102 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1103 types. */
1104 (simplify
1105 (rshift (lshift @0 INTEGER_CST@1) @1)
1106 (if (TYPE_UNSIGNED (type)
1107 && (wi::ltu_p (@1, element_precision (type))))
1108 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1109
1110 (for shiftrotate (lrotate rrotate lshift rshift)
1111 (simplify
1112 (shiftrotate @0 integer_zerop)
1113 (non_lvalue @0))
1114 (simplify
1115 (shiftrotate integer_zerop@0 @1)
1116 @0)
1117 /* Prefer vector1 << scalar to vector1 << vector2
1118 if vector2 is uniform. */
1119 (for vec (VECTOR_CST CONSTRUCTOR)
1120 (simplify
1121 (shiftrotate @0 vec@1)
1122 (with { tree tem = uniform_vector_p (@1); }
1123 (if (tem)
1124 (shiftrotate @0 { tem; }))))))
1125
1126 /* Rewrite an LROTATE_EXPR by a constant into an
1127 RROTATE_EXPR by a new constant. */
1128 (simplify
1129 (lrotate @0 INTEGER_CST@1)
1130 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
1131 build_int_cst (TREE_TYPE (@1),
1132 element_precision (type)), @1); }))
1133
1134 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1135 (for op (lrotate rrotate rshift lshift)
1136 (simplify
1137 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1138 (with { unsigned int prec = element_precision (type); }
1139 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1140 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1141 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1142 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1143 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1144 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1145 being well defined. */
1146 (if (low >= prec)
1147 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1148 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1149 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1150 { build_zero_cst (type); }
1151 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1152 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1153
1154
1155 /* ((1 << A) & 1) != 0 -> A == 0
1156 ((1 << A) & 1) == 0 -> A != 0 */
1157 (for cmp (ne eq)
1158 icmp (eq ne)
1159 (simplify
1160 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1161 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1162
1163 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1164 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1165 if CST2 != 0. */
1166 (for cmp (ne eq)
1167 (simplify
1168 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1169 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1170 (if (cand < 0
1171 || (!integer_zerop (@2)
1172 && wi::ne_p (wi::lshift (@0, cand), @2)))
1173 { constant_boolean_node (cmp == NE_EXPR, type); }
1174 (if (!integer_zerop (@2)
1175 && wi::eq_p (wi::lshift (@0, cand), @2))
1176 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1177
1178 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1179 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1180 if the new mask might be further optimized. */
1181 (for shift (lshift rshift)
1182 (simplify
1183 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1184 INTEGER_CST@2)
1185 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1186 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1187 && tree_fits_uhwi_p (@1)
1188 && tree_to_uhwi (@1) > 0
1189 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1190 (with
1191 {
1192 unsigned int shiftc = tree_to_uhwi (@1);
1193 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1194 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1195 tree shift_type = TREE_TYPE (@3);
1196 unsigned int prec;
1197
1198 if (shift == LSHIFT_EXPR)
1199 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1200 else if (shift == RSHIFT_EXPR
1201 && (TYPE_PRECISION (shift_type)
1202 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1203 {
1204 prec = TYPE_PRECISION (TREE_TYPE (@3));
1205 tree arg00 = @0;
1206 /* See if more bits can be proven as zero because of
1207 zero extension. */
1208 if (@3 != @0
1209 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1210 {
1211 tree inner_type = TREE_TYPE (@0);
1212 if ((TYPE_PRECISION (inner_type)
1213 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1214 && TYPE_PRECISION (inner_type) < prec)
1215 {
1216 prec = TYPE_PRECISION (inner_type);
1217 /* See if we can shorten the right shift. */
1218 if (shiftc < prec)
1219 shift_type = inner_type;
1220 /* Otherwise X >> C1 is all zeros, so we'll optimize
1221 it into (X, 0) later on by making sure zerobits
1222 is all ones. */
1223 }
1224 }
1225 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1226 if (shiftc < prec)
1227 {
1228 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1229 zerobits <<= prec - shiftc;
1230 }
1231 /* For arithmetic shift if sign bit could be set, zerobits
1232 can contain actually sign bits, so no transformation is
1233 possible, unless MASK masks them all away. In that
1234 case the shift needs to be converted into logical shift. */
1235 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1236 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1237 {
1238 if ((mask & zerobits) == 0)
1239 shift_type = unsigned_type_for (TREE_TYPE (@3));
1240 else
1241 zerobits = 0;
1242 }
1243 }
1244 }
1245 /* ((X << 16) & 0xff00) is (X, 0). */
1246 (if ((mask & zerobits) == mask)
1247 { build_int_cst (type, 0); }
1248 (with { newmask = mask | zerobits; }
1249 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1250 (with
1251 {
1252 /* Only do the transformation if NEWMASK is some integer
1253 mode's mask. */
1254 for (prec = BITS_PER_UNIT;
1255 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1256 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1257 break;
1258 }
1259 (if (prec < HOST_BITS_PER_WIDE_INT
1260 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1261 (with
1262 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1263 (if (!tree_int_cst_equal (newmaskt, @2))
1264 (if (shift_type != TREE_TYPE (@3))
1265 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1266 (bit_and @4 { newmaskt; })))))))))))))
1267
1268 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1269 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1270 (for shift (lshift rshift)
1271 (for bit_op (bit_and bit_xor bit_ior)
1272 (simplify
1273 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1274 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1275 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1276 (bit_op (shift (convert @0) @1) { mask; }))))))
1277
1278
1279 /* Simplifications of conversions. */
1280
1281 /* Basic strip-useless-type-conversions / strip_nops. */
1282 (for cvt (convert view_convert float fix_trunc)
1283 (simplify
1284 (cvt @0)
1285 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1286 || (GENERIC && type == TREE_TYPE (@0)))
1287 @0)))
1288
1289 /* Contract view-conversions. */
1290 (simplify
1291 (view_convert (view_convert @0))
1292 (view_convert @0))
1293
1294 /* For integral conversions with the same precision or pointer
1295 conversions use a NOP_EXPR instead. */
1296 (simplify
1297 (view_convert @0)
1298 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1299 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1300 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1301 (convert @0)))
1302
1303 /* Strip inner integral conversions that do not change precision or size. */
1304 (simplify
1305 (view_convert (convert@0 @1))
1306 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1307 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1308 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1309 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1310 (view_convert @1)))
1311
1312 /* Re-association barriers around constants and other re-association
1313 barriers can be removed. */
1314 (simplify
1315 (paren CONSTANT_CLASS_P@0)
1316 @0)
1317 (simplify
1318 (paren (paren@1 @0))
1319 @1)
1320
1321 /* Handle cases of two conversions in a row. */
1322 (for ocvt (convert float fix_trunc)
1323 (for icvt (convert float)
1324 (simplify
1325 (ocvt (icvt@1 @0))
1326 (with
1327 {
1328 tree inside_type = TREE_TYPE (@0);
1329 tree inter_type = TREE_TYPE (@1);
1330 int inside_int = INTEGRAL_TYPE_P (inside_type);
1331 int inside_ptr = POINTER_TYPE_P (inside_type);
1332 int inside_float = FLOAT_TYPE_P (inside_type);
1333 int inside_vec = VECTOR_TYPE_P (inside_type);
1334 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1335 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1336 int inter_int = INTEGRAL_TYPE_P (inter_type);
1337 int inter_ptr = POINTER_TYPE_P (inter_type);
1338 int inter_float = FLOAT_TYPE_P (inter_type);
1339 int inter_vec = VECTOR_TYPE_P (inter_type);
1340 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1341 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1342 int final_int = INTEGRAL_TYPE_P (type);
1343 int final_ptr = POINTER_TYPE_P (type);
1344 int final_float = FLOAT_TYPE_P (type);
1345 int final_vec = VECTOR_TYPE_P (type);
1346 unsigned int final_prec = TYPE_PRECISION (type);
1347 int final_unsignedp = TYPE_UNSIGNED (type);
1348 }
1349 (switch
1350 /* In addition to the cases of two conversions in a row
1351 handled below, if we are converting something to its own
1352 type via an object of identical or wider precision, neither
1353 conversion is needed. */
1354 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1355 || (GENERIC
1356 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1357 && (((inter_int || inter_ptr) && final_int)
1358 || (inter_float && final_float))
1359 && inter_prec >= final_prec)
1360 (ocvt @0))
1361
1362 /* Likewise, if the intermediate and initial types are either both
1363 float or both integer, we don't need the middle conversion if the
1364 former is wider than the latter and doesn't change the signedness
1365 (for integers). Avoid this if the final type is a pointer since
1366 then we sometimes need the middle conversion. Likewise if the
1367 final type has a precision not equal to the size of its mode. */
1368 (if (((inter_int && inside_int) || (inter_float && inside_float))
1369 && (final_int || final_float)
1370 && inter_prec >= inside_prec
1371 && (inter_float || inter_unsignedp == inside_unsignedp)
1372 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1373 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1374 (ocvt @0))
1375
1376 /* If we have a sign-extension of a zero-extended value, we can
1377 replace that by a single zero-extension. Likewise if the
1378 final conversion does not change precision we can drop the
1379 intermediate conversion. */
1380 (if (inside_int && inter_int && final_int
1381 && ((inside_prec < inter_prec && inter_prec < final_prec
1382 && inside_unsignedp && !inter_unsignedp)
1383 || final_prec == inter_prec))
1384 (ocvt @0))
1385
1386 /* Two conversions in a row are not needed unless:
1387 - some conversion is floating-point (overstrict for now), or
1388 - some conversion is a vector (overstrict for now), or
1389 - the intermediate type is narrower than both initial and
1390 final, or
1391 - the intermediate type and innermost type differ in signedness,
1392 and the outermost type is wider than the intermediate, or
1393 - the initial type is a pointer type and the precisions of the
1394 intermediate and final types differ, or
1395 - the final type is a pointer type and the precisions of the
1396 initial and intermediate types differ. */
1397 (if (! inside_float && ! inter_float && ! final_float
1398 && ! inside_vec && ! inter_vec && ! final_vec
1399 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1400 && ! (inside_int && inter_int
1401 && inter_unsignedp != inside_unsignedp
1402 && inter_prec < final_prec)
1403 && ((inter_unsignedp && inter_prec > inside_prec)
1404 == (final_unsignedp && final_prec > inter_prec))
1405 && ! (inside_ptr && inter_prec != final_prec)
1406 && ! (final_ptr && inside_prec != inter_prec)
1407 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1408 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1409 (ocvt @0))
1410
1411 /* A truncation to an unsigned type (a zero-extension) should be
1412 canonicalized as bitwise and of a mask. */
1413 (if (final_int && inter_int && inside_int
1414 && final_prec == inside_prec
1415 && final_prec > inter_prec
1416 && inter_unsignedp)
1417 (convert (bit_and @0 { wide_int_to_tree
1418 (inside_type,
1419 wi::mask (inter_prec, false,
1420 TYPE_PRECISION (inside_type))); })))
1421
1422 /* If we are converting an integer to a floating-point that can
1423 represent it exactly and back to an integer, we can skip the
1424 floating-point conversion. */
1425 (if (GIMPLE /* PR66211 */
1426 && inside_int && inter_float && final_int &&
1427 (unsigned) significand_size (TYPE_MODE (inter_type))
1428 >= inside_prec - !inside_unsignedp)
1429 (convert @0)))))))
1430
1431 /* If we have a narrowing conversion to an integral type that is fed by a
1432 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1433 masks off bits outside the final type (and nothing else). */
1434 (simplify
1435 (convert (bit_and @0 INTEGER_CST@1))
1436 (if (INTEGRAL_TYPE_P (type)
1437 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1438 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1439 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1440 TYPE_PRECISION (type)), 0))
1441 (convert @0)))
1442
1443
1444 /* (X /[ex] A) * A -> X. */
1445 (simplify
1446 (mult (convert? (exact_div @0 @1)) @1)
1447 /* Look through a sign-changing conversion. */
1448 (convert @0))
1449
1450 /* Canonicalization of binary operations. */
1451
1452 /* Convert X + -C into X - C. */
1453 (simplify
1454 (plus @0 REAL_CST@1)
1455 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1456 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
1457 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1458 (minus @0 { tem; })))))
1459
1460 /* Convert x+x into x*2.0. */
1461 (simplify
1462 (plus @0 @0)
1463 (if (SCALAR_FLOAT_TYPE_P (type))
1464 (mult @0 { build_real (type, dconst2); })))
1465
1466 (simplify
1467 (minus integer_zerop @1)
1468 (negate @1))
1469
1470 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1471 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1472 (-ARG1 + ARG0) reduces to -ARG1. */
1473 (simplify
1474 (minus real_zerop@0 @1)
1475 (if (fold_real_zero_addition_p (type, @0, 0))
1476 (negate @1)))
1477
1478 /* Transform x * -1 into -x. */
1479 (simplify
1480 (mult @0 integer_minus_onep)
1481 (negate @0))
1482
1483 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1484 (simplify
1485 (complex (realpart @0) (imagpart @0))
1486 @0)
1487 (simplify
1488 (realpart (complex @0 @1))
1489 @0)
1490 (simplify
1491 (imagpart (complex @0 @1))
1492 @1)
1493
1494 /* Sometimes we only care about half of a complex expression. */
1495 (simplify
1496 (realpart (convert?:s (conj:s @0)))
1497 (convert (realpart @0)))
1498 (simplify
1499 (imagpart (convert?:s (conj:s @0)))
1500 (convert (negate (imagpart @0))))
1501 (for part (realpart imagpart)
1502 (for op (plus minus)
1503 (simplify
1504 (part (convert?:s@2 (op:s @0 @1)))
1505 (convert (op (part @0) (part @1))))))
1506 (simplify
1507 (realpart (convert?:s (CEXPI:s @0)))
1508 (convert (COS @0)))
1509 (simplify
1510 (imagpart (convert?:s (CEXPI:s @0)))
1511 (convert (SIN @0)))
1512
1513 /* conj(conj(x)) -> x */
1514 (simplify
1515 (conj (convert? (conj @0)))
1516 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1517 (convert @0)))
1518
1519 /* conj({x,y}) -> {x,-y} */
1520 (simplify
1521 (conj (convert?:s (complex:s @0 @1)))
1522 (with { tree itype = TREE_TYPE (type); }
1523 (complex (convert:itype @0) (negate (convert:itype @1)))))
1524
1525 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1526 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1527 (simplify
1528 (bswap (bswap @0))
1529 @0)
1530 (simplify
1531 (bswap (bit_not (bswap @0)))
1532 (bit_not @0))
1533 (for bitop (bit_xor bit_ior bit_and)
1534 (simplify
1535 (bswap (bitop:c (bswap @0) @1))
1536 (bitop @0 (bswap @1)))))
1537
1538
1539 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1540
1541 /* Simplify constant conditions.
1542 Only optimize constant conditions when the selected branch
1543 has the same type as the COND_EXPR. This avoids optimizing
1544 away "c ? x : throw", where the throw has a void type.
1545 Note that we cannot throw away the fold-const.c variant nor
1546 this one as we depend on doing this transform before possibly
1547 A ? B : B -> B triggers and the fold-const.c one can optimize
1548 0 ? A : B to B even if A has side-effects. Something
1549 genmatch cannot handle. */
1550 (simplify
1551 (cond INTEGER_CST@0 @1 @2)
1552 (if (integer_zerop (@0))
1553 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1554 @2)
1555 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1556 @1)))
1557 (simplify
1558 (vec_cond VECTOR_CST@0 @1 @2)
1559 (if (integer_all_onesp (@0))
1560 @1
1561 (if (integer_zerop (@0))
1562 @2)))
1563
1564 (for cnd (cond vec_cond)
1565 /* A ? B : (A ? X : C) -> A ? B : C. */
1566 (simplify
1567 (cnd @0 (cnd @0 @1 @2) @3)
1568 (cnd @0 @1 @3))
1569 (simplify
1570 (cnd @0 @1 (cnd @0 @2 @3))
1571 (cnd @0 @1 @3))
1572
1573 /* A ? B : B -> B. */
1574 (simplify
1575 (cnd @0 @1 @1)
1576 @1)
1577
1578 /* !A ? B : C -> A ? C : B. */
1579 (simplify
1580 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1581 (cnd @0 @2 @1)))
1582
1583 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1584 return all-1 or all-0 results. */
1585 /* ??? We could instead convert all instances of the vec_cond to negate,
1586 but that isn't necessarily a win on its own. */
1587 (simplify
1588 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1589 (if (VECTOR_TYPE_P (type)
1590 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1591 && (TYPE_MODE (TREE_TYPE (type))
1592 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1593 (minus @3 (view_convert @0))))
1594
1595 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1596 (simplify
1597 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1598 (if (VECTOR_TYPE_P (type)
1599 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1600 && (TYPE_MODE (TREE_TYPE (type))
1601 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1602 (plus @3 (view_convert @0))))
1603
1604
1605 /* Simplifications of comparisons. */
1606
1607 /* See if we can reduce the magnitude of a constant involved in a
1608 comparison by changing the comparison code. This is a canonicalization
1609 formerly done by maybe_canonicalize_comparison_1. */
1610 (for cmp (le gt)
1611 acmp (lt ge)
1612 (simplify
1613 (cmp @0 INTEGER_CST@1)
1614 (if (tree_int_cst_sgn (@1) == -1)
1615 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1616 (for cmp (ge lt)
1617 acmp (gt le)
1618 (simplify
1619 (cmp @0 INTEGER_CST@1)
1620 (if (tree_int_cst_sgn (@1) == 1)
1621 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1622
1623
1624 /* We can simplify a logical negation of a comparison to the
1625 inverted comparison. As we cannot compute an expression
1626 operator using invert_tree_comparison we have to simulate
1627 that with expression code iteration. */
1628 (for cmp (tcc_comparison)
1629 icmp (inverted_tcc_comparison)
1630 ncmp (inverted_tcc_comparison_with_nans)
1631 /* Ideally we'd like to combine the following two patterns
1632 and handle some more cases by using
1633 (logical_inverted_value (cmp @0 @1))
1634 here but for that genmatch would need to "inline" that.
1635 For now implement what forward_propagate_comparison did. */
1636 (simplify
1637 (bit_not (cmp @0 @1))
1638 (if (VECTOR_TYPE_P (type)
1639 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1640 /* Comparison inversion may be impossible for trapping math,
1641 invert_tree_comparison will tell us. But we can't use
1642 a computed operator in the replacement tree thus we have
1643 to play the trick below. */
1644 (with { enum tree_code ic = invert_tree_comparison
1645 (cmp, HONOR_NANS (@0)); }
1646 (if (ic == icmp)
1647 (icmp @0 @1)
1648 (if (ic == ncmp)
1649 (ncmp @0 @1))))))
1650 (simplify
1651 (bit_xor (cmp @0 @1) integer_truep)
1652 (with { enum tree_code ic = invert_tree_comparison
1653 (cmp, HONOR_NANS (@0)); }
1654 (if (ic == icmp)
1655 (icmp @0 @1)
1656 (if (ic == ncmp)
1657 (ncmp @0 @1))))))
1658
1659 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1660 ??? The transformation is valid for the other operators if overflow
1661 is undefined for the type, but performing it here badly interacts
1662 with the transformation in fold_cond_expr_with_comparison which
1663 attempts to synthetize ABS_EXPR. */
1664 (for cmp (eq ne)
1665 (simplify
1666 (cmp (minus@2 @0 @1) integer_zerop)
1667 (if (single_use (@2))
1668 (cmp @0 @1))))
1669
1670 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1671 signed arithmetic case. That form is created by the compiler
1672 often enough for folding it to be of value. One example is in
1673 computing loop trip counts after Operator Strength Reduction. */
1674 (for cmp (simple_comparison)
1675 scmp (swapped_simple_comparison)
1676 (simplify
1677 (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
1678 /* Handle unfolded multiplication by zero. */
1679 (if (integer_zerop (@1))
1680 (cmp @1 @2)
1681 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1682 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1683 /* If @1 is negative we swap the sense of the comparison. */
1684 (if (tree_int_cst_sgn (@1) < 0)
1685 (scmp @0 @2)
1686 (cmp @0 @2))))))
1687
1688 /* Simplify comparison of something with itself. For IEEE
1689 floating-point, we can only do some of these simplifications. */
1690 (simplify
1691 (eq @0 @0)
1692 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1693 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1694 { constant_boolean_node (true, type); }))
1695 (for cmp (ge le)
1696 (simplify
1697 (cmp @0 @0)
1698 (eq @0 @0)))
1699 (for cmp (ne gt lt)
1700 (simplify
1701 (cmp @0 @0)
1702 (if (cmp != NE_EXPR
1703 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1704 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1705 { constant_boolean_node (false, type); })))
1706 (for cmp (unle unge uneq)
1707 (simplify
1708 (cmp @0 @0)
1709 { constant_boolean_node (true, type); }))
1710 (simplify
1711 (ltgt @0 @0)
1712 (if (!flag_trapping_math)
1713 { constant_boolean_node (false, type); }))
1714
1715 /* Fold ~X op ~Y as Y op X. */
1716 (for cmp (simple_comparison)
1717 (simplify
1718 (cmp (bit_not @0) (bit_not @1))
1719 (cmp @1 @0)))
1720
1721 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1722 (for cmp (simple_comparison)
1723 scmp (swapped_simple_comparison)
1724 (simplify
1725 (cmp (bit_not @0) CONSTANT_CLASS_P@1)
1726 (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
1727 (scmp @0 (bit_not @1)))))
1728
1729 (for cmp (simple_comparison)
1730 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1731 (simplify
1732 (cmp (convert@2 @0) (convert? @1))
1733 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1734 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1735 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1736 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1737 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1738 (with
1739 {
1740 tree type1 = TREE_TYPE (@1);
1741 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1742 {
1743 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1744 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1745 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1746 type1 = float_type_node;
1747 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1748 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1749 type1 = double_type_node;
1750 }
1751 tree newtype
1752 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1753 ? TREE_TYPE (@0) : type1);
1754 }
1755 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1756 (cmp (convert:newtype @0) (convert:newtype @1))))))
1757
1758 (simplify
1759 (cmp @0 REAL_CST@1)
1760 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1761 (switch
1762 /* a CMP (-0) -> a CMP 0 */
1763 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1764 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1765 /* x != NaN is always true, other ops are always false. */
1766 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1767 && ! HONOR_SNANS (@1))
1768 { constant_boolean_node (cmp == NE_EXPR, type); })
1769 /* Fold comparisons against infinity. */
1770 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1771 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1772 (with
1773 {
1774 REAL_VALUE_TYPE max;
1775 enum tree_code code = cmp;
1776 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1777 if (neg)
1778 code = swap_tree_comparison (code);
1779 }
1780 (switch
1781 /* x > +Inf is always false, if with ignore sNANs. */
1782 (if (code == GT_EXPR
1783 && ! HONOR_SNANS (@0))
1784 { constant_boolean_node (false, type); })
1785 (if (code == LE_EXPR)
1786 /* x <= +Inf is always true, if we don't case about NaNs. */
1787 (if (! HONOR_NANS (@0))
1788 { constant_boolean_node (true, type); }
1789 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1790 (eq @0 @0)))
1791 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1792 (if (code == EQ_EXPR || code == GE_EXPR)
1793 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1794 (if (neg)
1795 (lt @0 { build_real (TREE_TYPE (@0), max); })
1796 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1797 /* x < +Inf is always equal to x <= DBL_MAX. */
1798 (if (code == LT_EXPR)
1799 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1800 (if (neg)
1801 (ge @0 { build_real (TREE_TYPE (@0), max); })
1802 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1803 /* x != +Inf is always equal to !(x > DBL_MAX). */
1804 (if (code == NE_EXPR)
1805 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1806 (if (! HONOR_NANS (@0))
1807 (if (neg)
1808 (ge @0 { build_real (TREE_TYPE (@0), max); })
1809 (le @0 { build_real (TREE_TYPE (@0), max); }))
1810 (if (neg)
1811 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1812 { build_one_cst (type); })
1813 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1814 { build_one_cst (type); }))))))))))
1815
1816 /* If this is a comparison of a real constant with a PLUS_EXPR
1817 or a MINUS_EXPR of a real constant, we can convert it into a
1818 comparison with a revised real constant as long as no overflow
1819 occurs when unsafe_math_optimizations are enabled. */
1820 (if (flag_unsafe_math_optimizations)
1821 (for op (plus minus)
1822 (simplify
1823 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1824 (with
1825 {
1826 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1827 TREE_TYPE (@1), @2, @1);
1828 }
1829 (if (tem && !TREE_OVERFLOW (tem))
1830 (cmp @0 { tem; }))))))
1831
1832 /* Likewise, we can simplify a comparison of a real constant with
1833 a MINUS_EXPR whose first operand is also a real constant, i.e.
1834 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
1835 floating-point types only if -fassociative-math is set. */
1836 (if (flag_associative_math)
1837 (simplify
1838 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1839 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1840 (if (tem && !TREE_OVERFLOW (tem))
1841 (cmp { tem; } @1)))))
1842
1843 /* Fold comparisons against built-in math functions. */
1844 (if (flag_unsafe_math_optimizations
1845 && ! flag_errno_math)
1846 (for sq (SQRT)
1847 (simplify
1848 (cmp (sq @0) REAL_CST@1)
1849 (switch
1850 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1851 (switch
1852 /* sqrt(x) < y is always false, if y is negative. */
1853 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
1854 { constant_boolean_node (false, type); })
1855 /* sqrt(x) > y is always true, if y is negative and we
1856 don't care about NaNs, i.e. negative values of x. */
1857 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
1858 { constant_boolean_node (true, type); })
1859 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
1860 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
1861 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1862 (with
1863 {
1864 REAL_VALUE_TYPE c2;
1865 real_arithmetic (&c2, MULT_EXPR,
1866 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1867 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1868 }
1869 (if (REAL_VALUE_ISINF (c2))
1870 /* sqrt(x) > y is x == +Inf, when y is very large. */
1871 (if (HONOR_INFINITIES (@0))
1872 (eq @0 { build_real (TREE_TYPE (@0), c2); })
1873 { constant_boolean_node (false, type); })
1874 /* sqrt(x) > c is the same as x > c*c. */
1875 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
1876 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1877 (with
1878 {
1879 REAL_VALUE_TYPE c2;
1880 real_arithmetic (&c2, MULT_EXPR,
1881 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1882 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1883 }
1884 (if (REAL_VALUE_ISINF (c2))
1885 (switch
1886 /* sqrt(x) < y is always true, when y is a very large
1887 value and we don't care about NaNs or Infinities. */
1888 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
1889 { constant_boolean_node (true, type); })
1890 /* sqrt(x) < y is x != +Inf when y is very large and we
1891 don't care about NaNs. */
1892 (if (! HONOR_NANS (@0))
1893 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
1894 /* sqrt(x) < y is x >= 0 when y is very large and we
1895 don't care about Infinities. */
1896 (if (! HONOR_INFINITIES (@0))
1897 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
1898 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
1899 (if (GENERIC)
1900 (truth_andif
1901 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1902 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
1903 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
1904 (if (! HONOR_NANS (@0))
1905 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
1906 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
1907 (if (GENERIC)
1908 (truth_andif
1909 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1910 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
1911
1912 /* Unordered tests if either argument is a NaN. */
1913 (simplify
1914 (bit_ior (unordered @0 @0) (unordered @1 @1))
1915 (if (types_match (@0, @1))
1916 (unordered @0 @1)))
1917 (simplify
1918 (bit_and (ordered @0 @0) (ordered @1 @1))
1919 (if (types_match (@0, @1))
1920 (ordered @0 @1)))
1921 (simplify
1922 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
1923 @2)
1924 (simplify
1925 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
1926 @2)
1927
1928 /* -A CMP -B -> B CMP A. */
1929 (for cmp (tcc_comparison)
1930 scmp (swapped_tcc_comparison)
1931 (simplify
1932 (cmp (negate @0) (negate @1))
1933 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1934 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1935 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1936 (scmp @0 @1)))
1937 (simplify
1938 (cmp (negate @0) CONSTANT_CLASS_P@1)
1939 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1940 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1941 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1942 (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
1943 (if (tem && !TREE_OVERFLOW (tem))
1944 (scmp @0 { tem; }))))))
1945
1946 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
1947 (for op (eq ne)
1948 (simplify
1949 (op (abs @0) zerop@1)
1950 (op @0 @1)))
1951
1952 /* From fold_sign_changed_comparison and fold_widened_comparison. */
1953 (for cmp (simple_comparison)
1954 (simplify
1955 (cmp (convert@0 @00) (convert?@1 @10))
1956 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
1957 /* Disable this optimization if we're casting a function pointer
1958 type on targets that require function pointer canonicalization. */
1959 && !(targetm.have_canonicalize_funcptr_for_compare ()
1960 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
1961 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
1962 && single_use (@0))
1963 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
1964 && (TREE_CODE (@10) == INTEGER_CST
1965 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
1966 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
1967 || cmp == NE_EXPR
1968 || cmp == EQ_EXPR)
1969 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
1970 /* ??? The special-casing of INTEGER_CST conversion was in the original
1971 code and here to avoid a spurious overflow flag on the resulting
1972 constant which fold_convert produces. */
1973 (if (TREE_CODE (@1) == INTEGER_CST)
1974 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
1975 TREE_OVERFLOW (@1)); })
1976 (cmp @00 (convert @1)))
1977
1978 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
1979 /* If possible, express the comparison in the shorter mode. */
1980 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
1981 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
1982 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
1983 || ((TYPE_PRECISION (TREE_TYPE (@00))
1984 >= TYPE_PRECISION (TREE_TYPE (@10)))
1985 && (TYPE_UNSIGNED (TREE_TYPE (@00))
1986 == TYPE_UNSIGNED (TREE_TYPE (@10))))
1987 || (TREE_CODE (@10) == INTEGER_CST
1988 && (TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1989 || TREE_CODE (TREE_TYPE (@00)) == BOOLEAN_TYPE)
1990 && int_fits_type_p (@10, TREE_TYPE (@00)))))
1991 (cmp @00 (convert @10))
1992 (if (TREE_CODE (@10) == INTEGER_CST
1993 && TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1994 && !int_fits_type_p (@10, TREE_TYPE (@00)))
1995 (with
1996 {
1997 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1998 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1999 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2000 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2001 }
2002 (if (above || below)
2003 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2004 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2005 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2006 { constant_boolean_node (above ? true : false, type); }
2007 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2008 { constant_boolean_node (above ? false : true, type); }))))))))))))
2009
2010 (for cmp (eq ne)
2011 /* A local variable can never be pointed to by
2012 the default SSA name of an incoming parameter.
2013 SSA names are canonicalized to 2nd place. */
2014 (simplify
2015 (cmp addr@0 SSA_NAME@1)
2016 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2017 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2018 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2019 (if (TREE_CODE (base) == VAR_DECL
2020 && auto_var_in_fn_p (base, current_function_decl))
2021 (if (cmp == NE_EXPR)
2022 { constant_boolean_node (true, type); }
2023 { constant_boolean_node (false, type); }))))))
2024
2025 /* Equality compare simplifications from fold_binary */
2026 (for cmp (eq ne)
2027
2028 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2029 Similarly for NE_EXPR. */
2030 (simplify
2031 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2032 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2033 && wi::bit_and_not (@1, @2) != 0)
2034 { constant_boolean_node (cmp == NE_EXPR, type); }))
2035
2036 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2037 (simplify
2038 (cmp (bit_xor @0 @1) integer_zerop)
2039 (cmp @0 @1))
2040
2041 /* (X ^ Y) == Y becomes X == 0.
2042 Likewise (X ^ Y) == X becomes Y == 0. */
2043 (simplify
2044 (cmp:c (bit_xor:c @0 @1) @0)
2045 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2046
2047 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2048 (simplify
2049 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2050 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2051 (cmp @0 (bit_xor @1 (convert @2)))))
2052
2053 (simplify
2054 (cmp (convert? addr@0) integer_zerop)
2055 (if (tree_single_nonzero_warnv_p (@0, NULL))
2056 { constant_boolean_node (cmp == NE_EXPR, type); })))
2057
2058 /* If we have (A & C) == C where C is a power of 2, convert this into
2059 (A & C) != 0. Similarly for NE_EXPR. */
2060 (for cmp (eq ne)
2061 icmp (ne eq)
2062 (simplify
2063 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2064 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2065
2066 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2067 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2068 (for cmp (eq ne)
2069 ncmp (ge lt)
2070 (simplify
2071 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2072 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2073 && (TYPE_PRECISION (TREE_TYPE (@0))
2074 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2075 && element_precision (@2) >= element_precision (@0)
2076 && wi::only_sign_bit_p (@1, element_precision (@0)))
2077 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2078 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2079
2080 /* When the addresses are not directly of decls compare base and offset.
2081 This implements some remaining parts of fold_comparison address
2082 comparisons but still no complete part of it. Still it is good
2083 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2084 (for cmp (simple_comparison)
2085 (simplify
2086 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2087 (with
2088 {
2089 HOST_WIDE_INT off0, off1;
2090 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2091 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2092 if (base0 && TREE_CODE (base0) == MEM_REF)
2093 {
2094 off0 += mem_ref_offset (base0).to_short_addr ();
2095 base0 = TREE_OPERAND (base0, 0);
2096 }
2097 if (base1 && TREE_CODE (base1) == MEM_REF)
2098 {
2099 off1 += mem_ref_offset (base1).to_short_addr ();
2100 base1 = TREE_OPERAND (base1, 0);
2101 }
2102 }
2103 (if (base0 && base1)
2104 (with
2105 {
2106 int equal = 2;
2107 if (decl_in_symtab_p (base0)
2108 && decl_in_symtab_p (base1))
2109 equal = symtab_node::get_create (base0)
2110 ->equal_address_to (symtab_node::get_create (base1));
2111 else if ((DECL_P (base0)
2112 || TREE_CODE (base0) == SSA_NAME
2113 || TREE_CODE (base0) == STRING_CST)
2114 && (DECL_P (base1)
2115 || TREE_CODE (base1) == SSA_NAME
2116 || TREE_CODE (base1) == STRING_CST))
2117 equal = (base0 == base1);
2118 }
2119 (if (equal == 1
2120 && (cmp == EQ_EXPR || cmp == NE_EXPR
2121 /* If the offsets are equal we can ignore overflow. */
2122 || off0 == off1
2123 || POINTER_TYPE_OVERFLOW_UNDEFINED
2124 /* Or if we compare using pointers to decls or strings. */
2125 || (POINTER_TYPE_P (TREE_TYPE (@2))
2126 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2127 (switch
2128 (if (cmp == EQ_EXPR)
2129 { constant_boolean_node (off0 == off1, type); })
2130 (if (cmp == NE_EXPR)
2131 { constant_boolean_node (off0 != off1, type); })
2132 (if (cmp == LT_EXPR)
2133 { constant_boolean_node (off0 < off1, type); })
2134 (if (cmp == LE_EXPR)
2135 { constant_boolean_node (off0 <= off1, type); })
2136 (if (cmp == GE_EXPR)
2137 { constant_boolean_node (off0 >= off1, type); })
2138 (if (cmp == GT_EXPR)
2139 { constant_boolean_node (off0 > off1, type); }))
2140 (if (equal == 0
2141 && DECL_P (base0) && DECL_P (base1)
2142 /* If we compare this as integers require equal offset. */
2143 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2144 || off0 == off1))
2145 (switch
2146 (if (cmp == EQ_EXPR)
2147 { constant_boolean_node (false, type); })
2148 (if (cmp == NE_EXPR)
2149 { constant_boolean_node (true, type); })))))))))
2150
2151 /* Non-equality compare simplifications from fold_binary */
2152 (for cmp (lt gt le ge)
2153 /* Comparisons with the highest or lowest possible integer of
2154 the specified precision will have known values. */
2155 (simplify
2156 (cmp (convert?@2 @0) INTEGER_CST@1)
2157 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2158 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2159 (with
2160 {
2161 tree arg1_type = TREE_TYPE (@1);
2162 unsigned int prec = TYPE_PRECISION (arg1_type);
2163 wide_int max = wi::max_value (arg1_type);
2164 wide_int signed_max = wi::max_value (prec, SIGNED);
2165 wide_int min = wi::min_value (arg1_type);
2166 }
2167 (switch
2168 (if (wi::eq_p (@1, max))
2169 (switch
2170 (if (cmp == GT_EXPR)
2171 { constant_boolean_node (false, type); })
2172 (if (cmp == GE_EXPR)
2173 (eq @2 @1))
2174 (if (cmp == LE_EXPR)
2175 { constant_boolean_node (true, type); })
2176 (if (cmp == LT_EXPR)
2177 (ne @2 @1))))
2178 (if (wi::eq_p (@1, min))
2179 (switch
2180 (if (cmp == LT_EXPR)
2181 { constant_boolean_node (false, type); })
2182 (if (cmp == LE_EXPR)
2183 (eq @2 @1))
2184 (if (cmp == GE_EXPR)
2185 { constant_boolean_node (true, type); })
2186 (if (cmp == GT_EXPR)
2187 (ne @2 @1))))
2188 (if (wi::eq_p (@1, max - 1))
2189 (switch
2190 (if (cmp == GT_EXPR)
2191 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2192 (if (cmp == LE_EXPR)
2193 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2194 (if (wi::eq_p (@1, min + 1))
2195 (switch
2196 (if (cmp == GE_EXPR)
2197 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2198 (if (cmp == LT_EXPR)
2199 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2200 (if (wi::eq_p (@1, signed_max)
2201 && TYPE_UNSIGNED (arg1_type)
2202 /* We will flip the signedness of the comparison operator
2203 associated with the mode of @1, so the sign bit is
2204 specified by this mode. Check that @1 is the signed
2205 max associated with this sign bit. */
2206 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2207 /* signed_type does not work on pointer types. */
2208 && INTEGRAL_TYPE_P (arg1_type))
2209 /* The following case also applies to X < signed_max+1
2210 and X >= signed_max+1 because previous transformations. */
2211 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2212 (with { tree st = signed_type_for (arg1_type); }
2213 (if (cmp == LE_EXPR)
2214 (ge (convert:st @0) { build_zero_cst (st); })
2215 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2216
2217 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2218 /* If the second operand is NaN, the result is constant. */
2219 (simplify
2220 (cmp @0 REAL_CST@1)
2221 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2222 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2223 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2224 ? false : true, type); })))
2225
2226 /* bool_var != 0 becomes bool_var. */
2227 (simplify
2228 (ne @0 integer_zerop)
2229 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2230 && types_match (type, TREE_TYPE (@0)))
2231 (non_lvalue @0)))
2232 /* bool_var == 1 becomes bool_var. */
2233 (simplify
2234 (eq @0 integer_onep)
2235 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2236 && types_match (type, TREE_TYPE (@0)))
2237 (non_lvalue @0)))
2238 /* Do not handle
2239 bool_var == 0 becomes !bool_var or
2240 bool_var != 1 becomes !bool_var
2241 here because that only is good in assignment context as long
2242 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2243 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2244 clearly less optimal and which we'll transform again in forwprop. */
2245
2246
2247 /* Simplification of math builtins. These rules must all be optimizations
2248 as well as IL simplifications. If there is a possibility that the new
2249 form could be a pessimization, the rule should go in the canonicalization
2250 section that follows this one.
2251
2252 Rules can generally go in this section if they satisfy one of
2253 the following:
2254
2255 - the rule describes an identity
2256
2257 - the rule replaces calls with something as simple as addition or
2258 multiplication
2259
2260 - the rule contains unary calls only and simplifies the surrounding
2261 arithmetic. (The idea here is to exclude non-unary calls in which
2262 one operand is constant and in which the call is known to be cheap
2263 when the operand has that value.) */
2264
2265 (if (flag_unsafe_math_optimizations)
2266 /* Simplify sqrt(x) * sqrt(x) -> x. */
2267 (simplify
2268 (mult (SQRT@1 @0) @1)
2269 (if (!HONOR_SNANS (type))
2270 @0))
2271
2272 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2273 (for root (SQRT CBRT)
2274 (simplify
2275 (mult (root:s @0) (root:s @1))
2276 (root (mult @0 @1))))
2277
2278 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2279 (for exps (EXP EXP2 EXP10 POW10)
2280 (simplify
2281 (mult (exps:s @0) (exps:s @1))
2282 (exps (plus @0 @1))))
2283
2284 /* Simplify a/root(b/c) into a*root(c/b). */
2285 (for root (SQRT CBRT)
2286 (simplify
2287 (rdiv @0 (root:s (rdiv:s @1 @2)))
2288 (mult @0 (root (rdiv @2 @1)))))
2289
2290 /* Simplify x/expN(y) into x*expN(-y). */
2291 (for exps (EXP EXP2 EXP10 POW10)
2292 (simplify
2293 (rdiv @0 (exps:s @1))
2294 (mult @0 (exps (negate @1)))))
2295
2296 /* Special case, optimize logN(expN(x)) = x. */
2297 (for logs (LOG LOG2 LOG10 LOG10)
2298 exps (EXP EXP2 EXP10 POW10)
2299 (simplify
2300 (logs (exps @0))
2301 @0))
2302
2303 /* Optimize logN(func()) for various exponential functions. We
2304 want to determine the value "x" and the power "exponent" in
2305 order to transform logN(x**exponent) into exponent*logN(x). */
2306 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2307 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2308 (simplify
2309 (logs (exps @0))
2310 (with {
2311 tree x;
2312 switch (exps)
2313 {
2314 CASE_FLT_FN (BUILT_IN_EXP):
2315 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2316 x = build_real_truncate (type, dconst_e ());
2317 break;
2318 CASE_FLT_FN (BUILT_IN_EXP2):
2319 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2320 x = build_real (type, dconst2);
2321 break;
2322 CASE_FLT_FN (BUILT_IN_EXP10):
2323 CASE_FLT_FN (BUILT_IN_POW10):
2324 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2325 {
2326 REAL_VALUE_TYPE dconst10;
2327 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2328 x = build_real (type, dconst10);
2329 }
2330 break;
2331 default:
2332 gcc_unreachable ();
2333 }
2334 }
2335 (mult (logs { x; }) @0))))
2336
2337 (for logs (LOG LOG
2338 LOG2 LOG2
2339 LOG10 LOG10)
2340 exps (SQRT CBRT)
2341 (simplify
2342 (logs (exps @0))
2343 (with {
2344 tree x;
2345 switch (exps)
2346 {
2347 CASE_FLT_FN (BUILT_IN_SQRT):
2348 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2349 x = build_real (type, dconsthalf);
2350 break;
2351 CASE_FLT_FN (BUILT_IN_CBRT):
2352 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2353 x = build_real_truncate (type, dconst_third ());
2354 break;
2355 default:
2356 gcc_unreachable ();
2357 }
2358 }
2359 (mult { x; } (logs @0)))))
2360
2361 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2362 (for logs (LOG LOG2 LOG10)
2363 pows (POW)
2364 (simplify
2365 (logs (pows @0 @1))
2366 (mult @1 (logs @0))))
2367
2368 (for sqrts (SQRT)
2369 cbrts (CBRT)
2370 exps (EXP EXP2 EXP10 POW10)
2371 /* sqrt(expN(x)) -> expN(x*0.5). */
2372 (simplify
2373 (sqrts (exps @0))
2374 (exps (mult @0 { build_real (type, dconsthalf); })))
2375 /* cbrt(expN(x)) -> expN(x/3). */
2376 (simplify
2377 (cbrts (exps @0))
2378 (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))))
2379
2380 /* tan(atan(x)) -> x. */
2381 (for tans (TAN)
2382 atans (ATAN)
2383 (simplify
2384 (tans (atans @0))
2385 @0)))
2386
2387 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2388 (simplify
2389 (CABS (complex:c @0 real_zerop@1))
2390 (abs @0))
2391
2392 /* Canonicalization of sequences of math builtins. These rules represent
2393 IL simplifications but are not necessarily optimizations.
2394
2395 The sincos pass is responsible for picking "optimal" implementations
2396 of math builtins, which may be more complicated and can sometimes go
2397 the other way, e.g. converting pow into a sequence of sqrts.
2398 We only want to do these canonicalizations before the pass has run. */
2399
2400 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2401 /* Simplify tan(x) * cos(x) -> sin(x). */
2402 (simplify
2403 (mult:c (TAN:s @0) (COS:s @0))
2404 (SIN @0))
2405
2406 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2407 (simplify
2408 (mult @0 (POW:s @0 REAL_CST@1))
2409 (if (!TREE_OVERFLOW (@1))
2410 (POW @0 (plus @1 { build_one_cst (type); }))))
2411
2412 /* Simplify sin(x) / cos(x) -> tan(x). */
2413 (simplify
2414 (rdiv (SIN:s @0) (COS:s @0))
2415 (TAN @0))
2416
2417 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2418 (simplify
2419 (rdiv (COS:s @0) (SIN:s @0))
2420 (rdiv { build_one_cst (type); } (TAN @0)))
2421
2422 /* Simplify sin(x) / tan(x) -> cos(x). */
2423 (simplify
2424 (rdiv (SIN:s @0) (TAN:s @0))
2425 (if (! HONOR_NANS (@0)
2426 && ! HONOR_INFINITIES (@0))
2427 (cos @0)))
2428
2429 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2430 (simplify
2431 (rdiv (TAN:s @0) (SIN:s @0))
2432 (if (! HONOR_NANS (@0)
2433 && ! HONOR_INFINITIES (@0))
2434 (rdiv { build_one_cst (type); } (COS @0))))
2435
2436 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2437 (simplify
2438 (mult (POW:s @0 @1) (POW:s @0 @2))
2439 (POW @0 (plus @1 @2)))
2440
2441 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2442 (simplify
2443 (mult (POW:s @0 @1) (POW:s @2 @1))
2444 (POW (mult @0 @2) @1))
2445
2446 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2447 (simplify
2448 (rdiv (POW:s @0 REAL_CST@1) @0)
2449 (if (!TREE_OVERFLOW (@1))
2450 (POW @0 (minus @1 { build_one_cst (type); }))))
2451
2452 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2453 (simplify
2454 (rdiv @0 (POW:s @1 @2))
2455 (mult @0 (POW @1 (negate @2))))
2456
2457 (for sqrts (SQRT)
2458 cbrts (CBRT)
2459 pows (POW)
2460 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2461 (simplify
2462 (sqrts (sqrts @0))
2463 (pows @0 { build_real (type, dconst_quarter ()); }))
2464 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2465 (simplify
2466 (sqrts (cbrts @0))
2467 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2468 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2469 (simplify
2470 (cbrts (sqrts @0))
2471 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2472 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2473 (simplify
2474 (cbrts (cbrts tree_expr_nonnegative_p@0))
2475 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2476 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2477 (simplify
2478 (sqrts (pows @0 @1))
2479 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2480 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2481 (simplify
2482 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2483 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))))
2484
2485 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2486 (simplify
2487 (CABS (complex @0 @0))
2488 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })))
2489
2490 /* cproj(x) -> x if we're ignoring infinities. */
2491 (simplify
2492 (CPROJ @0)
2493 (if (!HONOR_INFINITIES (type))
2494 @0))
2495
2496 /* If the real part is inf and the imag part is known to be
2497 nonnegative, return (inf + 0i). */
2498 (simplify
2499 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2500 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2501 { build_complex_inf (type, false); }))
2502
2503 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
2504 (simplify
2505 (CPROJ (complex @0 REAL_CST@1))
2506 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2507 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
2508
2509
2510 /* Narrowing of arithmetic and logical operations.
2511
2512 These are conceptually similar to the transformations performed for
2513 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2514 term we want to move all that code out of the front-ends into here. */
2515
2516 /* If we have a narrowing conversion of an arithmetic operation where
2517 both operands are widening conversions from the same type as the outer
2518 narrowing conversion. Then convert the innermost operands to a suitable
2519 unsigned type (to avoid introducing undefined behaviour), perform the
2520 operation and convert the result to the desired type. */
2521 (for op (plus minus)
2522 (simplify
2523 (convert (op:s (convert@2 @0) (convert@3 @1)))
2524 (if (INTEGRAL_TYPE_P (type)
2525 /* We check for type compatibility between @0 and @1 below,
2526 so there's no need to check that @1/@3 are integral types. */
2527 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2528 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2529 /* The precision of the type of each operand must match the
2530 precision of the mode of each operand, similarly for the
2531 result. */
2532 && (TYPE_PRECISION (TREE_TYPE (@0))
2533 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2534 && (TYPE_PRECISION (TREE_TYPE (@1))
2535 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2536 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2537 /* The inner conversion must be a widening conversion. */
2538 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2539 && types_match (@0, @1)
2540 && types_match (@0, type))
2541 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2542 (convert (op @0 @1))
2543 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2544 (convert (op (convert:utype @0) (convert:utype @1))))))))
2545
2546 /* This is another case of narrowing, specifically when there's an outer
2547 BIT_AND_EXPR which masks off bits outside the type of the innermost
2548 operands. Like the previous case we have to convert the operands
2549 to unsigned types to avoid introducing undefined behaviour for the
2550 arithmetic operation. */
2551 (for op (minus plus)
2552 (simplify
2553 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
2554 (if (INTEGRAL_TYPE_P (type)
2555 /* We check for type compatibility between @0 and @1 below,
2556 so there's no need to check that @1/@3 are integral types. */
2557 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2558 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2559 /* The precision of the type of each operand must match the
2560 precision of the mode of each operand, similarly for the
2561 result. */
2562 && (TYPE_PRECISION (TREE_TYPE (@0))
2563 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2564 && (TYPE_PRECISION (TREE_TYPE (@1))
2565 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2566 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2567 /* The inner conversion must be a widening conversion. */
2568 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2569 && types_match (@0, @1)
2570 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
2571 <= TYPE_PRECISION (TREE_TYPE (@0)))
2572 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2573 || tree_int_cst_sgn (@4) >= 0))
2574 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2575 (with { tree ntype = TREE_TYPE (@0); }
2576 (convert (bit_and (op @0 @1) (convert:ntype @4))))
2577 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2578 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
2579 (convert:utype @4))))))))
2580
2581 /* Transform (@0 < @1 and @0 < @2) to use min,
2582 (@0 > @1 and @0 > @2) to use max */
2583 (for op (lt le gt ge)
2584 ext (min min max max)
2585 (simplify
2586 (bit_and (op:s @0 @1) (op:s @0 @2))
2587 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2588 (op @0 (ext @1 @2)))))
2589