]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/match.pd
b399786bac2e85b15a84b80b9131a85f8cc5193b
[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-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 /* (T)P - (T)(P + A) -> -(T) A */
1062 (for add (plus pointer_plus)
1063 (simplify
1064 (minus (convert @0)
1065 (convert (add @0 @1)))
1066 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1067 /* For integer types, if A has a smaller type
1068 than T the result depends on the possible
1069 overflow in P + A.
1070 E.g. T=size_t, A=(unsigned)429497295, P>0.
1071 However, if an overflow in P + A would cause
1072 undefined behavior, we can assume that there
1073 is no overflow. */
1074 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1075 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1076 /* For pointer types, if the conversion of A to the
1077 final type requires a sign- or zero-extension,
1078 then we have to punt - it is not defined which
1079 one is correct. */
1080 || (POINTER_TYPE_P (TREE_TYPE (@0))
1081 && TREE_CODE (@1) == INTEGER_CST
1082 && tree_int_cst_sign_bit (@1) == 0))
1083 (negate (convert @1)))))
1084
1085 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1086 (for add (plus pointer_plus)
1087 (simplify
1088 (minus (convert (add @0 @1))
1089 (convert (add @0 @2)))
1090 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1091 /* For integer types, if A has a smaller type
1092 than T the result depends on the possible
1093 overflow in P + A.
1094 E.g. T=size_t, A=(unsigned)429497295, P>0.
1095 However, if an overflow in P + A would cause
1096 undefined behavior, we can assume that there
1097 is no overflow. */
1098 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1099 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1100 /* For pointer types, if the conversion of A to the
1101 final type requires a sign- or zero-extension,
1102 then we have to punt - it is not defined which
1103 one is correct. */
1104 || (POINTER_TYPE_P (TREE_TYPE (@0))
1105 && TREE_CODE (@1) == INTEGER_CST
1106 && tree_int_cst_sign_bit (@1) == 0
1107 && TREE_CODE (@2) == INTEGER_CST
1108 && tree_int_cst_sign_bit (@2) == 0))
1109 (minus (convert @1) (convert @2)))))))
1110
1111
1112 /* Simplifications of MIN_EXPR and MAX_EXPR. */
1113
1114 (for minmax (min max)
1115 (simplify
1116 (minmax @0 @0)
1117 @0))
1118 (simplify
1119 (min @0 @1)
1120 (if (INTEGRAL_TYPE_P (type)
1121 && TYPE_MIN_VALUE (type)
1122 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1123 @1))
1124 (simplify
1125 (max @0 @1)
1126 (if (INTEGRAL_TYPE_P (type)
1127 && TYPE_MAX_VALUE (type)
1128 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1129 @1))
1130
1131
1132 /* Simplifications of shift and rotates. */
1133
1134 (for rotate (lrotate rrotate)
1135 (simplify
1136 (rotate integer_all_onesp@0 @1)
1137 @0))
1138
1139 /* Optimize -1 >> x for arithmetic right shifts. */
1140 (simplify
1141 (rshift integer_all_onesp@0 @1)
1142 (if (!TYPE_UNSIGNED (type)
1143 && tree_expr_nonnegative_p (@1))
1144 @0))
1145
1146 /* Optimize (x >> c) << c into x & (-1<<c). */
1147 (simplify
1148 (lshift (rshift @0 INTEGER_CST@1) @1)
1149 (if (wi::ltu_p (@1, element_precision (type)))
1150 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1151
1152 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1153 types. */
1154 (simplify
1155 (rshift (lshift @0 INTEGER_CST@1) @1)
1156 (if (TYPE_UNSIGNED (type)
1157 && (wi::ltu_p (@1, element_precision (type))))
1158 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1159
1160 (for shiftrotate (lrotate rrotate lshift rshift)
1161 (simplify
1162 (shiftrotate @0 integer_zerop)
1163 (non_lvalue @0))
1164 (simplify
1165 (shiftrotate integer_zerop@0 @1)
1166 @0)
1167 /* Prefer vector1 << scalar to vector1 << vector2
1168 if vector2 is uniform. */
1169 (for vec (VECTOR_CST CONSTRUCTOR)
1170 (simplify
1171 (shiftrotate @0 vec@1)
1172 (with { tree tem = uniform_vector_p (@1); }
1173 (if (tem)
1174 (shiftrotate @0 { tem; }))))))
1175
1176 /* Rewrite an LROTATE_EXPR by a constant into an
1177 RROTATE_EXPR by a new constant. */
1178 (simplify
1179 (lrotate @0 INTEGER_CST@1)
1180 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
1181 build_int_cst (TREE_TYPE (@1),
1182 element_precision (type)), @1); }))
1183
1184 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1185 (for op (lrotate rrotate rshift lshift)
1186 (simplify
1187 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1188 (with { unsigned int prec = element_precision (type); }
1189 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1190 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1191 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1192 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1193 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1194 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1195 being well defined. */
1196 (if (low >= prec)
1197 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1198 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1199 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1200 { build_zero_cst (type); }
1201 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1202 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1203
1204
1205 /* ((1 << A) & 1) != 0 -> A == 0
1206 ((1 << A) & 1) == 0 -> A != 0 */
1207 (for cmp (ne eq)
1208 icmp (eq ne)
1209 (simplify
1210 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1211 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1212
1213 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1214 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1215 if CST2 != 0. */
1216 (for cmp (ne eq)
1217 (simplify
1218 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1219 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1220 (if (cand < 0
1221 || (!integer_zerop (@2)
1222 && wi::ne_p (wi::lshift (@0, cand), @2)))
1223 { constant_boolean_node (cmp == NE_EXPR, type); }
1224 (if (!integer_zerop (@2)
1225 && wi::eq_p (wi::lshift (@0, cand), @2))
1226 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1227
1228 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1229 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1230 if the new mask might be further optimized. */
1231 (for shift (lshift rshift)
1232 (simplify
1233 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1234 INTEGER_CST@2)
1235 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1236 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1237 && tree_fits_uhwi_p (@1)
1238 && tree_to_uhwi (@1) > 0
1239 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1240 (with
1241 {
1242 unsigned int shiftc = tree_to_uhwi (@1);
1243 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1244 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1245 tree shift_type = TREE_TYPE (@3);
1246 unsigned int prec;
1247
1248 if (shift == LSHIFT_EXPR)
1249 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1250 else if (shift == RSHIFT_EXPR
1251 && (TYPE_PRECISION (shift_type)
1252 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1253 {
1254 prec = TYPE_PRECISION (TREE_TYPE (@3));
1255 tree arg00 = @0;
1256 /* See if more bits can be proven as zero because of
1257 zero extension. */
1258 if (@3 != @0
1259 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1260 {
1261 tree inner_type = TREE_TYPE (@0);
1262 if ((TYPE_PRECISION (inner_type)
1263 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1264 && TYPE_PRECISION (inner_type) < prec)
1265 {
1266 prec = TYPE_PRECISION (inner_type);
1267 /* See if we can shorten the right shift. */
1268 if (shiftc < prec)
1269 shift_type = inner_type;
1270 /* Otherwise X >> C1 is all zeros, so we'll optimize
1271 it into (X, 0) later on by making sure zerobits
1272 is all ones. */
1273 }
1274 }
1275 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1276 if (shiftc < prec)
1277 {
1278 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1279 zerobits <<= prec - shiftc;
1280 }
1281 /* For arithmetic shift if sign bit could be set, zerobits
1282 can contain actually sign bits, so no transformation is
1283 possible, unless MASK masks them all away. In that
1284 case the shift needs to be converted into logical shift. */
1285 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1286 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1287 {
1288 if ((mask & zerobits) == 0)
1289 shift_type = unsigned_type_for (TREE_TYPE (@3));
1290 else
1291 zerobits = 0;
1292 }
1293 }
1294 }
1295 /* ((X << 16) & 0xff00) is (X, 0). */
1296 (if ((mask & zerobits) == mask)
1297 { build_int_cst (type, 0); }
1298 (with { newmask = mask | zerobits; }
1299 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1300 (with
1301 {
1302 /* Only do the transformation if NEWMASK is some integer
1303 mode's mask. */
1304 for (prec = BITS_PER_UNIT;
1305 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1306 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1307 break;
1308 }
1309 (if (prec < HOST_BITS_PER_WIDE_INT
1310 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1311 (with
1312 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1313 (if (!tree_int_cst_equal (newmaskt, @2))
1314 (if (shift_type != TREE_TYPE (@3))
1315 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1316 (bit_and @4 { newmaskt; })))))))))))))
1317
1318 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1319 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1320 (for shift (lshift rshift)
1321 (for bit_op (bit_and bit_xor bit_ior)
1322 (simplify
1323 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1324 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1325 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1326 (bit_op (shift (convert @0) @1) { mask; }))))))
1327
1328
1329 /* Simplifications of conversions. */
1330
1331 /* Basic strip-useless-type-conversions / strip_nops. */
1332 (for cvt (convert view_convert float fix_trunc)
1333 (simplify
1334 (cvt @0)
1335 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1336 || (GENERIC && type == TREE_TYPE (@0)))
1337 @0)))
1338
1339 /* Contract view-conversions. */
1340 (simplify
1341 (view_convert (view_convert @0))
1342 (view_convert @0))
1343
1344 /* For integral conversions with the same precision or pointer
1345 conversions use a NOP_EXPR instead. */
1346 (simplify
1347 (view_convert @0)
1348 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1349 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1350 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1351 (convert @0)))
1352
1353 /* Strip inner integral conversions that do not change precision or size. */
1354 (simplify
1355 (view_convert (convert@0 @1))
1356 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1357 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1358 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1359 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1360 (view_convert @1)))
1361
1362 /* Re-association barriers around constants and other re-association
1363 barriers can be removed. */
1364 (simplify
1365 (paren CONSTANT_CLASS_P@0)
1366 @0)
1367 (simplify
1368 (paren (paren@1 @0))
1369 @1)
1370
1371 /* Handle cases of two conversions in a row. */
1372 (for ocvt (convert float fix_trunc)
1373 (for icvt (convert float)
1374 (simplify
1375 (ocvt (icvt@1 @0))
1376 (with
1377 {
1378 tree inside_type = TREE_TYPE (@0);
1379 tree inter_type = TREE_TYPE (@1);
1380 int inside_int = INTEGRAL_TYPE_P (inside_type);
1381 int inside_ptr = POINTER_TYPE_P (inside_type);
1382 int inside_float = FLOAT_TYPE_P (inside_type);
1383 int inside_vec = VECTOR_TYPE_P (inside_type);
1384 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1385 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1386 int inter_int = INTEGRAL_TYPE_P (inter_type);
1387 int inter_ptr = POINTER_TYPE_P (inter_type);
1388 int inter_float = FLOAT_TYPE_P (inter_type);
1389 int inter_vec = VECTOR_TYPE_P (inter_type);
1390 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1391 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1392 int final_int = INTEGRAL_TYPE_P (type);
1393 int final_ptr = POINTER_TYPE_P (type);
1394 int final_float = FLOAT_TYPE_P (type);
1395 int final_vec = VECTOR_TYPE_P (type);
1396 unsigned int final_prec = TYPE_PRECISION (type);
1397 int final_unsignedp = TYPE_UNSIGNED (type);
1398 }
1399 (switch
1400 /* In addition to the cases of two conversions in a row
1401 handled below, if we are converting something to its own
1402 type via an object of identical or wider precision, neither
1403 conversion is needed. */
1404 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1405 || (GENERIC
1406 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1407 && (((inter_int || inter_ptr) && final_int)
1408 || (inter_float && final_float))
1409 && inter_prec >= final_prec)
1410 (ocvt @0))
1411
1412 /* Likewise, if the intermediate and initial types are either both
1413 float or both integer, we don't need the middle conversion if the
1414 former is wider than the latter and doesn't change the signedness
1415 (for integers). Avoid this if the final type is a pointer since
1416 then we sometimes need the middle conversion. Likewise if the
1417 final type has a precision not equal to the size of its mode. */
1418 (if (((inter_int && inside_int) || (inter_float && inside_float))
1419 && (final_int || final_float)
1420 && inter_prec >= inside_prec
1421 && (inter_float || inter_unsignedp == inside_unsignedp)
1422 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1423 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1424 (ocvt @0))
1425
1426 /* If we have a sign-extension of a zero-extended value, we can
1427 replace that by a single zero-extension. Likewise if the
1428 final conversion does not change precision we can drop the
1429 intermediate conversion. */
1430 (if (inside_int && inter_int && final_int
1431 && ((inside_prec < inter_prec && inter_prec < final_prec
1432 && inside_unsignedp && !inter_unsignedp)
1433 || final_prec == inter_prec))
1434 (ocvt @0))
1435
1436 /* Two conversions in a row are not needed unless:
1437 - some conversion is floating-point (overstrict for now), or
1438 - some conversion is a vector (overstrict for now), or
1439 - the intermediate type is narrower than both initial and
1440 final, or
1441 - the intermediate type and innermost type differ in signedness,
1442 and the outermost type is wider than the intermediate, or
1443 - the initial type is a pointer type and the precisions of the
1444 intermediate and final types differ, or
1445 - the final type is a pointer type and the precisions of the
1446 initial and intermediate types differ. */
1447 (if (! inside_float && ! inter_float && ! final_float
1448 && ! inside_vec && ! inter_vec && ! final_vec
1449 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1450 && ! (inside_int && inter_int
1451 && inter_unsignedp != inside_unsignedp
1452 && inter_prec < final_prec)
1453 && ((inter_unsignedp && inter_prec > inside_prec)
1454 == (final_unsignedp && final_prec > inter_prec))
1455 && ! (inside_ptr && inter_prec != final_prec)
1456 && ! (final_ptr && inside_prec != inter_prec)
1457 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1458 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1459 (ocvt @0))
1460
1461 /* A truncation to an unsigned type (a zero-extension) should be
1462 canonicalized as bitwise and of a mask. */
1463 (if (final_int && inter_int && inside_int
1464 && final_prec == inside_prec
1465 && final_prec > inter_prec
1466 && inter_unsignedp)
1467 (convert (bit_and @0 { wide_int_to_tree
1468 (inside_type,
1469 wi::mask (inter_prec, false,
1470 TYPE_PRECISION (inside_type))); })))
1471
1472 /* If we are converting an integer to a floating-point that can
1473 represent it exactly and back to an integer, we can skip the
1474 floating-point conversion. */
1475 (if (GIMPLE /* PR66211 */
1476 && inside_int && inter_float && final_int &&
1477 (unsigned) significand_size (TYPE_MODE (inter_type))
1478 >= inside_prec - !inside_unsignedp)
1479 (convert @0)))))))
1480
1481 /* If we have a narrowing conversion to an integral type that is fed by a
1482 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1483 masks off bits outside the final type (and nothing else). */
1484 (simplify
1485 (convert (bit_and @0 INTEGER_CST@1))
1486 (if (INTEGRAL_TYPE_P (type)
1487 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1488 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1489 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1490 TYPE_PRECISION (type)), 0))
1491 (convert @0)))
1492
1493
1494 /* (X /[ex] A) * A -> X. */
1495 (simplify
1496 (mult (convert? (exact_div @0 @1)) @1)
1497 /* Look through a sign-changing conversion. */
1498 (convert @0))
1499
1500 /* Canonicalization of binary operations. */
1501
1502 /* Convert X + -C into X - C. */
1503 (simplify
1504 (plus @0 REAL_CST@1)
1505 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1506 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
1507 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1508 (minus @0 { tem; })))))
1509
1510 /* Convert x+x into x*2.0. */
1511 (simplify
1512 (plus @0 @0)
1513 (if (SCALAR_FLOAT_TYPE_P (type))
1514 (mult @0 { build_real (type, dconst2); })))
1515
1516 (simplify
1517 (minus integer_zerop @1)
1518 (negate @1))
1519
1520 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1521 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1522 (-ARG1 + ARG0) reduces to -ARG1. */
1523 (simplify
1524 (minus real_zerop@0 @1)
1525 (if (fold_real_zero_addition_p (type, @0, 0))
1526 (negate @1)))
1527
1528 /* Transform x * -1 into -x. */
1529 (simplify
1530 (mult @0 integer_minus_onep)
1531 (negate @0))
1532
1533 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1534 (simplify
1535 (complex (realpart @0) (imagpart @0))
1536 @0)
1537 (simplify
1538 (realpart (complex @0 @1))
1539 @0)
1540 (simplify
1541 (imagpart (complex @0 @1))
1542 @1)
1543
1544 /* Sometimes we only care about half of a complex expression. */
1545 (simplify
1546 (realpart (convert?:s (conj:s @0)))
1547 (convert (realpart @0)))
1548 (simplify
1549 (imagpart (convert?:s (conj:s @0)))
1550 (convert (negate (imagpart @0))))
1551 (for part (realpart imagpart)
1552 (for op (plus minus)
1553 (simplify
1554 (part (convert?:s@2 (op:s @0 @1)))
1555 (convert (op (part @0) (part @1))))))
1556 (simplify
1557 (realpart (convert?:s (CEXPI:s @0)))
1558 (convert (COS @0)))
1559 (simplify
1560 (imagpart (convert?:s (CEXPI:s @0)))
1561 (convert (SIN @0)))
1562
1563 /* conj(conj(x)) -> x */
1564 (simplify
1565 (conj (convert? (conj @0)))
1566 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1567 (convert @0)))
1568
1569 /* conj({x,y}) -> {x,-y} */
1570 (simplify
1571 (conj (convert?:s (complex:s @0 @1)))
1572 (with { tree itype = TREE_TYPE (type); }
1573 (complex (convert:itype @0) (negate (convert:itype @1)))))
1574
1575 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1576 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1577 (simplify
1578 (bswap (bswap @0))
1579 @0)
1580 (simplify
1581 (bswap (bit_not (bswap @0)))
1582 (bit_not @0))
1583 (for bitop (bit_xor bit_ior bit_and)
1584 (simplify
1585 (bswap (bitop:c (bswap @0) @1))
1586 (bitop @0 (bswap @1)))))
1587
1588
1589 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1590
1591 /* Simplify constant conditions.
1592 Only optimize constant conditions when the selected branch
1593 has the same type as the COND_EXPR. This avoids optimizing
1594 away "c ? x : throw", where the throw has a void type.
1595 Note that we cannot throw away the fold-const.c variant nor
1596 this one as we depend on doing this transform before possibly
1597 A ? B : B -> B triggers and the fold-const.c one can optimize
1598 0 ? A : B to B even if A has side-effects. Something
1599 genmatch cannot handle. */
1600 (simplify
1601 (cond INTEGER_CST@0 @1 @2)
1602 (if (integer_zerop (@0))
1603 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1604 @2)
1605 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1606 @1)))
1607 (simplify
1608 (vec_cond VECTOR_CST@0 @1 @2)
1609 (if (integer_all_onesp (@0))
1610 @1
1611 (if (integer_zerop (@0))
1612 @2)))
1613
1614 (for cnd (cond vec_cond)
1615 /* A ? B : (A ? X : C) -> A ? B : C. */
1616 (simplify
1617 (cnd @0 (cnd @0 @1 @2) @3)
1618 (cnd @0 @1 @3))
1619 (simplify
1620 (cnd @0 @1 (cnd @0 @2 @3))
1621 (cnd @0 @1 @3))
1622
1623 /* A ? B : B -> B. */
1624 (simplify
1625 (cnd @0 @1 @1)
1626 @1)
1627
1628 /* !A ? B : C -> A ? C : B. */
1629 (simplify
1630 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1631 (cnd @0 @2 @1)))
1632
1633 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1634 return all-1 or all-0 results. */
1635 /* ??? We could instead convert all instances of the vec_cond to negate,
1636 but that isn't necessarily a win on its own. */
1637 (simplify
1638 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1639 (if (VECTOR_TYPE_P (type)
1640 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1641 && (TYPE_MODE (TREE_TYPE (type))
1642 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1643 (minus @3 (view_convert @0))))
1644
1645 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1646 (simplify
1647 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1648 (if (VECTOR_TYPE_P (type)
1649 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1650 && (TYPE_MODE (TREE_TYPE (type))
1651 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1652 (plus @3 (view_convert @0))))
1653
1654
1655 /* Simplifications of comparisons. */
1656
1657 /* See if we can reduce the magnitude of a constant involved in a
1658 comparison by changing the comparison code. This is a canonicalization
1659 formerly done by maybe_canonicalize_comparison_1. */
1660 (for cmp (le gt)
1661 acmp (lt ge)
1662 (simplify
1663 (cmp @0 INTEGER_CST@1)
1664 (if (tree_int_cst_sgn (@1) == -1)
1665 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1666 (for cmp (ge lt)
1667 acmp (gt le)
1668 (simplify
1669 (cmp @0 INTEGER_CST@1)
1670 (if (tree_int_cst_sgn (@1) == 1)
1671 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1672
1673
1674 /* We can simplify a logical negation of a comparison to the
1675 inverted comparison. As we cannot compute an expression
1676 operator using invert_tree_comparison we have to simulate
1677 that with expression code iteration. */
1678 (for cmp (tcc_comparison)
1679 icmp (inverted_tcc_comparison)
1680 ncmp (inverted_tcc_comparison_with_nans)
1681 /* Ideally we'd like to combine the following two patterns
1682 and handle some more cases by using
1683 (logical_inverted_value (cmp @0 @1))
1684 here but for that genmatch would need to "inline" that.
1685 For now implement what forward_propagate_comparison did. */
1686 (simplify
1687 (bit_not (cmp @0 @1))
1688 (if (VECTOR_TYPE_P (type)
1689 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1690 /* Comparison inversion may be impossible for trapping math,
1691 invert_tree_comparison will tell us. But we can't use
1692 a computed operator in the replacement tree thus we have
1693 to play the trick below. */
1694 (with { enum tree_code ic = invert_tree_comparison
1695 (cmp, HONOR_NANS (@0)); }
1696 (if (ic == icmp)
1697 (icmp @0 @1)
1698 (if (ic == ncmp)
1699 (ncmp @0 @1))))))
1700 (simplify
1701 (bit_xor (cmp @0 @1) integer_truep)
1702 (with { enum tree_code ic = invert_tree_comparison
1703 (cmp, HONOR_NANS (@0)); }
1704 (if (ic == icmp)
1705 (icmp @0 @1)
1706 (if (ic == ncmp)
1707 (ncmp @0 @1))))))
1708
1709 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1710 ??? The transformation is valid for the other operators if overflow
1711 is undefined for the type, but performing it here badly interacts
1712 with the transformation in fold_cond_expr_with_comparison which
1713 attempts to synthetize ABS_EXPR. */
1714 (for cmp (eq ne)
1715 (simplify
1716 (cmp (minus@2 @0 @1) integer_zerop)
1717 (if (single_use (@2))
1718 (cmp @0 @1))))
1719
1720 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1721 signed arithmetic case. That form is created by the compiler
1722 often enough for folding it to be of value. One example is in
1723 computing loop trip counts after Operator Strength Reduction. */
1724 (for cmp (simple_comparison)
1725 scmp (swapped_simple_comparison)
1726 (simplify
1727 (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
1728 /* Handle unfolded multiplication by zero. */
1729 (if (integer_zerop (@1))
1730 (cmp @1 @2)
1731 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1732 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1733 /* If @1 is negative we swap the sense of the comparison. */
1734 (if (tree_int_cst_sgn (@1) < 0)
1735 (scmp @0 @2)
1736 (cmp @0 @2))))))
1737
1738 /* Simplify comparison of something with itself. For IEEE
1739 floating-point, we can only do some of these simplifications. */
1740 (simplify
1741 (eq @0 @0)
1742 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1743 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1744 { constant_boolean_node (true, type); }))
1745 (for cmp (ge le)
1746 (simplify
1747 (cmp @0 @0)
1748 (eq @0 @0)))
1749 (for cmp (ne gt lt)
1750 (simplify
1751 (cmp @0 @0)
1752 (if (cmp != NE_EXPR
1753 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1754 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1755 { constant_boolean_node (false, type); })))
1756 (for cmp (unle unge uneq)
1757 (simplify
1758 (cmp @0 @0)
1759 { constant_boolean_node (true, type); }))
1760 (simplify
1761 (ltgt @0 @0)
1762 (if (!flag_trapping_math)
1763 { constant_boolean_node (false, type); }))
1764
1765 /* Fold ~X op ~Y as Y op X. */
1766 (for cmp (simple_comparison)
1767 (simplify
1768 (cmp (bit_not @0) (bit_not @1))
1769 (cmp @1 @0)))
1770
1771 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1772 (for cmp (simple_comparison)
1773 scmp (swapped_simple_comparison)
1774 (simplify
1775 (cmp (bit_not @0) CONSTANT_CLASS_P@1)
1776 (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
1777 (scmp @0 (bit_not @1)))))
1778
1779 (for cmp (simple_comparison)
1780 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1781 (simplify
1782 (cmp (convert@2 @0) (convert? @1))
1783 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1784 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1785 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1786 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1787 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1788 (with
1789 {
1790 tree type1 = TREE_TYPE (@1);
1791 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1792 {
1793 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1794 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1795 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1796 type1 = float_type_node;
1797 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1798 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1799 type1 = double_type_node;
1800 }
1801 tree newtype
1802 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1803 ? TREE_TYPE (@0) : type1);
1804 }
1805 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1806 (cmp (convert:newtype @0) (convert:newtype @1))))))
1807
1808 (simplify
1809 (cmp @0 REAL_CST@1)
1810 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1811 (switch
1812 /* a CMP (-0) -> a CMP 0 */
1813 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1814 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1815 /* x != NaN is always true, other ops are always false. */
1816 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1817 && ! HONOR_SNANS (@1))
1818 { constant_boolean_node (cmp == NE_EXPR, type); })
1819 /* Fold comparisons against infinity. */
1820 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1821 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1822 (with
1823 {
1824 REAL_VALUE_TYPE max;
1825 enum tree_code code = cmp;
1826 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1827 if (neg)
1828 code = swap_tree_comparison (code);
1829 }
1830 (switch
1831 /* x > +Inf is always false, if with ignore sNANs. */
1832 (if (code == GT_EXPR
1833 && ! HONOR_SNANS (@0))
1834 { constant_boolean_node (false, type); })
1835 (if (code == LE_EXPR)
1836 /* x <= +Inf is always true, if we don't case about NaNs. */
1837 (if (! HONOR_NANS (@0))
1838 { constant_boolean_node (true, type); }
1839 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1840 (eq @0 @0)))
1841 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1842 (if (code == EQ_EXPR || code == GE_EXPR)
1843 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1844 (if (neg)
1845 (lt @0 { build_real (TREE_TYPE (@0), max); })
1846 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1847 /* x < +Inf is always equal to x <= DBL_MAX. */
1848 (if (code == LT_EXPR)
1849 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1850 (if (neg)
1851 (ge @0 { build_real (TREE_TYPE (@0), max); })
1852 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1853 /* x != +Inf is always equal to !(x > DBL_MAX). */
1854 (if (code == NE_EXPR)
1855 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1856 (if (! HONOR_NANS (@0))
1857 (if (neg)
1858 (ge @0 { build_real (TREE_TYPE (@0), max); })
1859 (le @0 { build_real (TREE_TYPE (@0), max); }))
1860 (if (neg)
1861 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1862 { build_one_cst (type); })
1863 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1864 { build_one_cst (type); }))))))))))
1865
1866 /* If this is a comparison of a real constant with a PLUS_EXPR
1867 or a MINUS_EXPR of a real constant, we can convert it into a
1868 comparison with a revised real constant as long as no overflow
1869 occurs when unsafe_math_optimizations are enabled. */
1870 (if (flag_unsafe_math_optimizations)
1871 (for op (plus minus)
1872 (simplify
1873 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1874 (with
1875 {
1876 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1877 TREE_TYPE (@1), @2, @1);
1878 }
1879 (if (tem && !TREE_OVERFLOW (tem))
1880 (cmp @0 { tem; }))))))
1881
1882 /* Likewise, we can simplify a comparison of a real constant with
1883 a MINUS_EXPR whose first operand is also a real constant, i.e.
1884 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
1885 floating-point types only if -fassociative-math is set. */
1886 (if (flag_associative_math)
1887 (simplify
1888 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1889 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1890 (if (tem && !TREE_OVERFLOW (tem))
1891 (cmp { tem; } @1)))))
1892
1893 /* Fold comparisons against built-in math functions. */
1894 (if (flag_unsafe_math_optimizations
1895 && ! flag_errno_math)
1896 (for sq (SQRT)
1897 (simplify
1898 (cmp (sq @0) REAL_CST@1)
1899 (switch
1900 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1901 (switch
1902 /* sqrt(x) < y is always false, if y is negative. */
1903 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
1904 { constant_boolean_node (false, type); })
1905 /* sqrt(x) > y is always true, if y is negative and we
1906 don't care about NaNs, i.e. negative values of x. */
1907 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
1908 { constant_boolean_node (true, type); })
1909 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
1910 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
1911 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1912 (with
1913 {
1914 REAL_VALUE_TYPE c2;
1915 real_arithmetic (&c2, MULT_EXPR,
1916 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1917 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1918 }
1919 (if (REAL_VALUE_ISINF (c2))
1920 /* sqrt(x) > y is x == +Inf, when y is very large. */
1921 (if (HONOR_INFINITIES (@0))
1922 (eq @0 { build_real (TREE_TYPE (@0), c2); })
1923 { constant_boolean_node (false, type); })
1924 /* sqrt(x) > c is the same as x > c*c. */
1925 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
1926 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1927 (with
1928 {
1929 REAL_VALUE_TYPE c2;
1930 real_arithmetic (&c2, MULT_EXPR,
1931 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1932 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1933 }
1934 (if (REAL_VALUE_ISINF (c2))
1935 (switch
1936 /* sqrt(x) < y is always true, when y is a very large
1937 value and we don't care about NaNs or Infinities. */
1938 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
1939 { constant_boolean_node (true, type); })
1940 /* sqrt(x) < y is x != +Inf when y is very large and we
1941 don't care about NaNs. */
1942 (if (! HONOR_NANS (@0))
1943 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
1944 /* sqrt(x) < y is x >= 0 when y is very large and we
1945 don't care about Infinities. */
1946 (if (! HONOR_INFINITIES (@0))
1947 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
1948 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
1949 (if (GENERIC)
1950 (truth_andif
1951 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1952 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
1953 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
1954 (if (! HONOR_NANS (@0))
1955 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
1956 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
1957 (if (GENERIC)
1958 (truth_andif
1959 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1960 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
1961
1962 /* Unordered tests if either argument is a NaN. */
1963 (simplify
1964 (bit_ior (unordered @0 @0) (unordered @1 @1))
1965 (if (types_match (@0, @1))
1966 (unordered @0 @1)))
1967 (simplify
1968 (bit_and (ordered @0 @0) (ordered @1 @1))
1969 (if (types_match (@0, @1))
1970 (ordered @0 @1)))
1971 (simplify
1972 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
1973 @2)
1974 (simplify
1975 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
1976 @2)
1977
1978 /* -A CMP -B -> B CMP A. */
1979 (for cmp (tcc_comparison)
1980 scmp (swapped_tcc_comparison)
1981 (simplify
1982 (cmp (negate @0) (negate @1))
1983 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1984 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1985 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1986 (scmp @0 @1)))
1987 (simplify
1988 (cmp (negate @0) CONSTANT_CLASS_P@1)
1989 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1990 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1991 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1992 (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
1993 (if (tem && !TREE_OVERFLOW (tem))
1994 (scmp @0 { tem; }))))))
1995
1996 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
1997 (for op (eq ne)
1998 (simplify
1999 (op (abs @0) zerop@1)
2000 (op @0 @1)))
2001
2002 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2003 (for cmp (simple_comparison)
2004 (simplify
2005 (cmp (convert@0 @00) (convert?@1 @10))
2006 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2007 /* Disable this optimization if we're casting a function pointer
2008 type on targets that require function pointer canonicalization. */
2009 && !(targetm.have_canonicalize_funcptr_for_compare ()
2010 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2011 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2012 && single_use (@0))
2013 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2014 && (TREE_CODE (@10) == INTEGER_CST
2015 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2016 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2017 || cmp == NE_EXPR
2018 || cmp == EQ_EXPR)
2019 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2020 /* ??? The special-casing of INTEGER_CST conversion was in the original
2021 code and here to avoid a spurious overflow flag on the resulting
2022 constant which fold_convert produces. */
2023 (if (TREE_CODE (@1) == INTEGER_CST)
2024 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2025 TREE_OVERFLOW (@1)); })
2026 (cmp @00 (convert @1)))
2027
2028 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2029 /* If possible, express the comparison in the shorter mode. */
2030 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2031 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2032 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2033 || ((TYPE_PRECISION (TREE_TYPE (@00))
2034 >= TYPE_PRECISION (TREE_TYPE (@10)))
2035 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2036 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2037 || (TREE_CODE (@10) == INTEGER_CST
2038 && (TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
2039 || TREE_CODE (TREE_TYPE (@00)) == BOOLEAN_TYPE)
2040 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2041 (cmp @00 (convert @10))
2042 (if (TREE_CODE (@10) == INTEGER_CST
2043 && TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
2044 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2045 (with
2046 {
2047 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2048 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2049 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2050 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2051 }
2052 (if (above || below)
2053 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2054 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2055 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2056 { constant_boolean_node (above ? true : false, type); }
2057 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2058 { constant_boolean_node (above ? false : true, type); }))))))))))))
2059
2060 (for cmp (eq ne)
2061 /* A local variable can never be pointed to by
2062 the default SSA name of an incoming parameter.
2063 SSA names are canonicalized to 2nd place. */
2064 (simplify
2065 (cmp addr@0 SSA_NAME@1)
2066 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2067 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2068 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2069 (if (TREE_CODE (base) == VAR_DECL
2070 && auto_var_in_fn_p (base, current_function_decl))
2071 (if (cmp == NE_EXPR)
2072 { constant_boolean_node (true, type); }
2073 { constant_boolean_node (false, type); }))))))
2074
2075 /* Equality compare simplifications from fold_binary */
2076 (for cmp (eq ne)
2077
2078 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2079 Similarly for NE_EXPR. */
2080 (simplify
2081 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2082 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2083 && wi::bit_and_not (@1, @2) != 0)
2084 { constant_boolean_node (cmp == NE_EXPR, type); }))
2085
2086 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2087 (simplify
2088 (cmp (bit_xor @0 @1) integer_zerop)
2089 (cmp @0 @1))
2090
2091 /* (X ^ Y) == Y becomes X == 0.
2092 Likewise (X ^ Y) == X becomes Y == 0. */
2093 (simplify
2094 (cmp:c (bit_xor:c @0 @1) @0)
2095 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2096
2097 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2098 (simplify
2099 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2100 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2101 (cmp @0 (bit_xor @1 (convert @2)))))
2102
2103 (simplify
2104 (cmp (convert? addr@0) integer_zerop)
2105 (if (tree_single_nonzero_warnv_p (@0, NULL))
2106 { constant_boolean_node (cmp == NE_EXPR, type); })))
2107
2108 /* If we have (A & C) == C where C is a power of 2, convert this into
2109 (A & C) != 0. Similarly for NE_EXPR. */
2110 (for cmp (eq ne)
2111 icmp (ne eq)
2112 (simplify
2113 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2114 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2115
2116 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2117 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2118 (for cmp (eq ne)
2119 ncmp (ge lt)
2120 (simplify
2121 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2122 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2123 && (TYPE_PRECISION (TREE_TYPE (@0))
2124 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2125 && element_precision (@2) >= element_precision (@0)
2126 && wi::only_sign_bit_p (@1, element_precision (@0)))
2127 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2128 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2129
2130 /* When the addresses are not directly of decls compare base and offset.
2131 This implements some remaining parts of fold_comparison address
2132 comparisons but still no complete part of it. Still it is good
2133 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2134 (for cmp (simple_comparison)
2135 (simplify
2136 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2137 (with
2138 {
2139 HOST_WIDE_INT off0, off1;
2140 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2141 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2142 if (base0 && TREE_CODE (base0) == MEM_REF)
2143 {
2144 off0 += mem_ref_offset (base0).to_short_addr ();
2145 base0 = TREE_OPERAND (base0, 0);
2146 }
2147 if (base1 && TREE_CODE (base1) == MEM_REF)
2148 {
2149 off1 += mem_ref_offset (base1).to_short_addr ();
2150 base1 = TREE_OPERAND (base1, 0);
2151 }
2152 }
2153 (if (base0 && base1)
2154 (with
2155 {
2156 int equal = 2;
2157 if (decl_in_symtab_p (base0)
2158 && decl_in_symtab_p (base1))
2159 equal = symtab_node::get_create (base0)
2160 ->equal_address_to (symtab_node::get_create (base1));
2161 else if ((DECL_P (base0)
2162 || TREE_CODE (base0) == SSA_NAME
2163 || TREE_CODE (base0) == STRING_CST)
2164 && (DECL_P (base1)
2165 || TREE_CODE (base1) == SSA_NAME
2166 || TREE_CODE (base1) == STRING_CST))
2167 equal = (base0 == base1);
2168 }
2169 (if (equal == 1
2170 && (cmp == EQ_EXPR || cmp == NE_EXPR
2171 /* If the offsets are equal we can ignore overflow. */
2172 || off0 == off1
2173 || POINTER_TYPE_OVERFLOW_UNDEFINED
2174 /* Or if we compare using pointers to decls or strings. */
2175 || (POINTER_TYPE_P (TREE_TYPE (@2))
2176 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2177 (switch
2178 (if (cmp == EQ_EXPR)
2179 { constant_boolean_node (off0 == off1, type); })
2180 (if (cmp == NE_EXPR)
2181 { constant_boolean_node (off0 != off1, type); })
2182 (if (cmp == LT_EXPR)
2183 { constant_boolean_node (off0 < off1, type); })
2184 (if (cmp == LE_EXPR)
2185 { constant_boolean_node (off0 <= off1, type); })
2186 (if (cmp == GE_EXPR)
2187 { constant_boolean_node (off0 >= off1, type); })
2188 (if (cmp == GT_EXPR)
2189 { constant_boolean_node (off0 > off1, type); }))
2190 (if (equal == 0
2191 && DECL_P (base0) && DECL_P (base1)
2192 /* If we compare this as integers require equal offset. */
2193 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2194 || off0 == off1))
2195 (switch
2196 (if (cmp == EQ_EXPR)
2197 { constant_boolean_node (false, type); })
2198 (if (cmp == NE_EXPR)
2199 { constant_boolean_node (true, type); })))))))))
2200
2201 /* Non-equality compare simplifications from fold_binary */
2202 (for cmp (lt gt le ge)
2203 /* Comparisons with the highest or lowest possible integer of
2204 the specified precision will have known values. */
2205 (simplify
2206 (cmp (convert?@2 @0) INTEGER_CST@1)
2207 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2208 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2209 (with
2210 {
2211 tree arg1_type = TREE_TYPE (@1);
2212 unsigned int prec = TYPE_PRECISION (arg1_type);
2213 wide_int max = wi::max_value (arg1_type);
2214 wide_int signed_max = wi::max_value (prec, SIGNED);
2215 wide_int min = wi::min_value (arg1_type);
2216 }
2217 (switch
2218 (if (wi::eq_p (@1, max))
2219 (switch
2220 (if (cmp == GT_EXPR)
2221 { constant_boolean_node (false, type); })
2222 (if (cmp == GE_EXPR)
2223 (eq @2 @1))
2224 (if (cmp == LE_EXPR)
2225 { constant_boolean_node (true, type); })
2226 (if (cmp == LT_EXPR)
2227 (ne @2 @1))))
2228 (if (wi::eq_p (@1, min))
2229 (switch
2230 (if (cmp == LT_EXPR)
2231 { constant_boolean_node (false, type); })
2232 (if (cmp == LE_EXPR)
2233 (eq @2 @1))
2234 (if (cmp == GE_EXPR)
2235 { constant_boolean_node (true, type); })
2236 (if (cmp == GT_EXPR)
2237 (ne @2 @1))))
2238 (if (wi::eq_p (@1, max - 1))
2239 (switch
2240 (if (cmp == GT_EXPR)
2241 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2242 (if (cmp == LE_EXPR)
2243 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2244 (if (wi::eq_p (@1, min + 1))
2245 (switch
2246 (if (cmp == GE_EXPR)
2247 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2248 (if (cmp == LT_EXPR)
2249 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2250 (if (wi::eq_p (@1, signed_max)
2251 && TYPE_UNSIGNED (arg1_type)
2252 /* We will flip the signedness of the comparison operator
2253 associated with the mode of @1, so the sign bit is
2254 specified by this mode. Check that @1 is the signed
2255 max associated with this sign bit. */
2256 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2257 /* signed_type does not work on pointer types. */
2258 && INTEGRAL_TYPE_P (arg1_type))
2259 /* The following case also applies to X < signed_max+1
2260 and X >= signed_max+1 because previous transformations. */
2261 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2262 (with { tree st = signed_type_for (arg1_type); }
2263 (if (cmp == LE_EXPR)
2264 (ge (convert:st @0) { build_zero_cst (st); })
2265 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2266
2267 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2268 /* If the second operand is NaN, the result is constant. */
2269 (simplify
2270 (cmp @0 REAL_CST@1)
2271 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2272 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2273 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2274 ? false : true, type); })))
2275
2276 /* bool_var != 0 becomes bool_var. */
2277 (simplify
2278 (ne @0 integer_zerop)
2279 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2280 && types_match (type, TREE_TYPE (@0)))
2281 (non_lvalue @0)))
2282 /* bool_var == 1 becomes bool_var. */
2283 (simplify
2284 (eq @0 integer_onep)
2285 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2286 && types_match (type, TREE_TYPE (@0)))
2287 (non_lvalue @0)))
2288 /* Do not handle
2289 bool_var == 0 becomes !bool_var or
2290 bool_var != 1 becomes !bool_var
2291 here because that only is good in assignment context as long
2292 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2293 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2294 clearly less optimal and which we'll transform again in forwprop. */
2295
2296
2297 /* Simplification of math builtins. These rules must all be optimizations
2298 as well as IL simplifications. If there is a possibility that the new
2299 form could be a pessimization, the rule should go in the canonicalization
2300 section that follows this one.
2301
2302 Rules can generally go in this section if they satisfy one of
2303 the following:
2304
2305 - the rule describes an identity
2306
2307 - the rule replaces calls with something as simple as addition or
2308 multiplication
2309
2310 - the rule contains unary calls only and simplifies the surrounding
2311 arithmetic. (The idea here is to exclude non-unary calls in which
2312 one operand is constant and in which the call is known to be cheap
2313 when the operand has that value.) */
2314
2315 (if (flag_unsafe_math_optimizations)
2316 /* Simplify sqrt(x) * sqrt(x) -> x. */
2317 (simplify
2318 (mult (SQRT@1 @0) @1)
2319 (if (!HONOR_SNANS (type))
2320 @0))
2321
2322 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2323 (for root (SQRT CBRT)
2324 (simplify
2325 (mult (root:s @0) (root:s @1))
2326 (root (mult @0 @1))))
2327
2328 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2329 (for exps (EXP EXP2 EXP10 POW10)
2330 (simplify
2331 (mult (exps:s @0) (exps:s @1))
2332 (exps (plus @0 @1))))
2333
2334 /* Simplify a/root(b/c) into a*root(c/b). */
2335 (for root (SQRT CBRT)
2336 (simplify
2337 (rdiv @0 (root:s (rdiv:s @1 @2)))
2338 (mult @0 (root (rdiv @2 @1)))))
2339
2340 /* Simplify x/expN(y) into x*expN(-y). */
2341 (for exps (EXP EXP2 EXP10 POW10)
2342 (simplify
2343 (rdiv @0 (exps:s @1))
2344 (mult @0 (exps (negate @1)))))
2345
2346 /* Special case, optimize logN(expN(x)) = x. */
2347 (for logs (LOG LOG2 LOG10 LOG10)
2348 exps (EXP EXP2 EXP10 POW10)
2349 (simplify
2350 (logs (exps @0))
2351 @0))
2352
2353 /* Optimize logN(func()) for various exponential functions. We
2354 want to determine the value "x" and the power "exponent" in
2355 order to transform logN(x**exponent) into exponent*logN(x). */
2356 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2357 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2358 (simplify
2359 (logs (exps @0))
2360 (with {
2361 tree x;
2362 switch (exps)
2363 {
2364 CASE_FLT_FN (BUILT_IN_EXP):
2365 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2366 x = build_real_truncate (type, dconst_e ());
2367 break;
2368 CASE_FLT_FN (BUILT_IN_EXP2):
2369 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2370 x = build_real (type, dconst2);
2371 break;
2372 CASE_FLT_FN (BUILT_IN_EXP10):
2373 CASE_FLT_FN (BUILT_IN_POW10):
2374 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2375 {
2376 REAL_VALUE_TYPE dconst10;
2377 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2378 x = build_real (type, dconst10);
2379 }
2380 break;
2381 default:
2382 gcc_unreachable ();
2383 }
2384 }
2385 (mult (logs { x; }) @0))))
2386
2387 (for logs (LOG LOG
2388 LOG2 LOG2
2389 LOG10 LOG10)
2390 exps (SQRT CBRT)
2391 (simplify
2392 (logs (exps @0))
2393 (with {
2394 tree x;
2395 switch (exps)
2396 {
2397 CASE_FLT_FN (BUILT_IN_SQRT):
2398 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2399 x = build_real (type, dconsthalf);
2400 break;
2401 CASE_FLT_FN (BUILT_IN_CBRT):
2402 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2403 x = build_real_truncate (type, dconst_third ());
2404 break;
2405 default:
2406 gcc_unreachable ();
2407 }
2408 }
2409 (mult { x; } (logs @0)))))
2410
2411 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2412 (for logs (LOG LOG2 LOG10)
2413 pows (POW)
2414 (simplify
2415 (logs (pows @0 @1))
2416 (mult @1 (logs @0))))
2417
2418 (for sqrts (SQRT)
2419 cbrts (CBRT)
2420 exps (EXP EXP2 EXP10 POW10)
2421 /* sqrt(expN(x)) -> expN(x*0.5). */
2422 (simplify
2423 (sqrts (exps @0))
2424 (exps (mult @0 { build_real (type, dconsthalf); })))
2425 /* cbrt(expN(x)) -> expN(x/3). */
2426 (simplify
2427 (cbrts (exps @0))
2428 (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))))
2429
2430 /* tan(atan(x)) -> x. */
2431 (for tans (TAN)
2432 atans (ATAN)
2433 (simplify
2434 (tans (atans @0))
2435 @0)))
2436
2437 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2438 (simplify
2439 (CABS (complex:c @0 real_zerop@1))
2440 (abs @0))
2441
2442 /* Canonicalization of sequences of math builtins. These rules represent
2443 IL simplifications but are not necessarily optimizations.
2444
2445 The sincos pass is responsible for picking "optimal" implementations
2446 of math builtins, which may be more complicated and can sometimes go
2447 the other way, e.g. converting pow into a sequence of sqrts.
2448 We only want to do these canonicalizations before the pass has run. */
2449
2450 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2451 /* Simplify tan(x) * cos(x) -> sin(x). */
2452 (simplify
2453 (mult:c (TAN:s @0) (COS:s @0))
2454 (SIN @0))
2455
2456 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2457 (simplify
2458 (mult @0 (POW:s @0 REAL_CST@1))
2459 (if (!TREE_OVERFLOW (@1))
2460 (POW @0 (plus @1 { build_one_cst (type); }))))
2461
2462 /* Simplify sin(x) / cos(x) -> tan(x). */
2463 (simplify
2464 (rdiv (SIN:s @0) (COS:s @0))
2465 (TAN @0))
2466
2467 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2468 (simplify
2469 (rdiv (COS:s @0) (SIN:s @0))
2470 (rdiv { build_one_cst (type); } (TAN @0)))
2471
2472 /* Simplify sin(x) / tan(x) -> cos(x). */
2473 (simplify
2474 (rdiv (SIN:s @0) (TAN:s @0))
2475 (if (! HONOR_NANS (@0)
2476 && ! HONOR_INFINITIES (@0))
2477 (cos @0)))
2478
2479 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2480 (simplify
2481 (rdiv (TAN:s @0) (SIN:s @0))
2482 (if (! HONOR_NANS (@0)
2483 && ! HONOR_INFINITIES (@0))
2484 (rdiv { build_one_cst (type); } (COS @0))))
2485
2486 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2487 (simplify
2488 (mult (POW:s @0 @1) (POW:s @0 @2))
2489 (POW @0 (plus @1 @2)))
2490
2491 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2492 (simplify
2493 (mult (POW:s @0 @1) (POW:s @2 @1))
2494 (POW (mult @0 @2) @1))
2495
2496 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2497 (simplify
2498 (rdiv (POW:s @0 REAL_CST@1) @0)
2499 (if (!TREE_OVERFLOW (@1))
2500 (POW @0 (minus @1 { build_one_cst (type); }))))
2501
2502 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2503 (simplify
2504 (rdiv @0 (POW:s @1 @2))
2505 (mult @0 (POW @1 (negate @2))))
2506
2507 (for sqrts (SQRT)
2508 cbrts (CBRT)
2509 pows (POW)
2510 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2511 (simplify
2512 (sqrts (sqrts @0))
2513 (pows @0 { build_real (type, dconst_quarter ()); }))
2514 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2515 (simplify
2516 (sqrts (cbrts @0))
2517 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2518 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2519 (simplify
2520 (cbrts (sqrts @0))
2521 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2522 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2523 (simplify
2524 (cbrts (cbrts tree_expr_nonnegative_p@0))
2525 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2526 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2527 (simplify
2528 (sqrts (pows @0 @1))
2529 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2530 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2531 (simplify
2532 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2533 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))))
2534
2535 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2536 (simplify
2537 (CABS (complex @0 @0))
2538 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })))
2539
2540 /* cproj(x) -> x if we're ignoring infinities. */
2541 (simplify
2542 (CPROJ @0)
2543 (if (!HONOR_INFINITIES (type))
2544 @0))
2545
2546 /* If the real part is inf and the imag part is known to be
2547 nonnegative, return (inf + 0i). */
2548 (simplify
2549 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2550 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2551 { build_complex_inf (type, false); }))
2552
2553 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
2554 (simplify
2555 (CPROJ (complex @0 REAL_CST@1))
2556 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2557 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
2558
2559
2560 /* Narrowing of arithmetic and logical operations.
2561
2562 These are conceptually similar to the transformations performed for
2563 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2564 term we want to move all that code out of the front-ends into here. */
2565
2566 /* If we have a narrowing conversion of an arithmetic operation where
2567 both operands are widening conversions from the same type as the outer
2568 narrowing conversion. Then convert the innermost operands to a suitable
2569 unsigned type (to avoid introducing undefined behaviour), perform the
2570 operation and convert the result to the desired type. */
2571 (for op (plus minus)
2572 (simplify
2573 (convert (op:s (convert@2 @0) (convert@3 @1)))
2574 (if (INTEGRAL_TYPE_P (type)
2575 /* We check for type compatibility between @0 and @1 below,
2576 so there's no need to check that @1/@3 are integral types. */
2577 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2578 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2579 /* The precision of the type of each operand must match the
2580 precision of the mode of each operand, similarly for the
2581 result. */
2582 && (TYPE_PRECISION (TREE_TYPE (@0))
2583 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2584 && (TYPE_PRECISION (TREE_TYPE (@1))
2585 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2586 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2587 /* The inner conversion must be a widening conversion. */
2588 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2589 && types_match (@0, @1)
2590 && types_match (@0, type))
2591 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2592 (convert (op @0 @1))
2593 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2594 (convert (op (convert:utype @0) (convert:utype @1))))))))
2595
2596 /* This is another case of narrowing, specifically when there's an outer
2597 BIT_AND_EXPR which masks off bits outside the type of the innermost
2598 operands. Like the previous case we have to convert the operands
2599 to unsigned types to avoid introducing undefined behaviour for the
2600 arithmetic operation. */
2601 (for op (minus plus)
2602 (simplify
2603 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
2604 (if (INTEGRAL_TYPE_P (type)
2605 /* We check for type compatibility between @0 and @1 below,
2606 so there's no need to check that @1/@3 are integral types. */
2607 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2608 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2609 /* The precision of the type of each operand must match the
2610 precision of the mode of each operand, similarly for the
2611 result. */
2612 && (TYPE_PRECISION (TREE_TYPE (@0))
2613 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2614 && (TYPE_PRECISION (TREE_TYPE (@1))
2615 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2616 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2617 /* The inner conversion must be a widening conversion. */
2618 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2619 && types_match (@0, @1)
2620 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
2621 <= TYPE_PRECISION (TREE_TYPE (@0)))
2622 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2623 || tree_int_cst_sgn (@4) >= 0))
2624 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2625 (with { tree ntype = TREE_TYPE (@0); }
2626 (convert (bit_and (op @0 @1) (convert:ntype @4))))
2627 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2628 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
2629 (convert:utype @4))))))))
2630
2631 /* Transform (@0 < @1 and @0 < @2) to use min,
2632 (@0 > @1 and @0 > @2) to use max */
2633 (for op (lt le gt ge)
2634 ext (min min max max)
2635 (simplify
2636 (bit_and (op:s @0 @1) (op:s @0 @2))
2637 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2638 (op @0 (ext @1 @2)))))
2639