genmatch.c (struct operand): Add OP_IF and OP_WITH op_types.
[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
30 real_zerop real_onep real_minus_onep
31 CONSTANT_CLASS_P
32 tree_expr_nonnegative_p)
33
34 /* Operator lists. */
35 (define_operator_list tcc_comparison
36 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
37 (define_operator_list inverted_tcc_comparison
38 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
39 (define_operator_list inverted_tcc_comparison_with_nans
40 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
41 (define_operator_list swapped_tcc_comparison
42 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
43 (define_operator_list simple_comparison lt le eq ne ge gt)
44 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
45
46 (define_operator_list LOG BUILT_IN_LOGF BUILT_IN_LOG BUILT_IN_LOGL)
47 (define_operator_list EXP BUILT_IN_EXPF BUILT_IN_EXP BUILT_IN_EXPL)
48 (define_operator_list LOG2 BUILT_IN_LOG2F BUILT_IN_LOG2 BUILT_IN_LOG2L)
49 (define_operator_list EXP2 BUILT_IN_EXP2F BUILT_IN_EXP2 BUILT_IN_EXP2L)
50 (define_operator_list LOG10 BUILT_IN_LOG10F BUILT_IN_LOG10 BUILT_IN_LOG10L)
51 (define_operator_list EXP10 BUILT_IN_EXP10F BUILT_IN_EXP10 BUILT_IN_EXP10L)
52 (define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL)
53 (define_operator_list POW10 BUILT_IN_POW10F BUILT_IN_POW10 BUILT_IN_POW10L)
54 (define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL)
55 (define_operator_list CBRT BUILT_IN_CBRTF BUILT_IN_CBRT BUILT_IN_CBRTL)
56
57
58 /* Simplifications of operations with one constant operand and
59 simplifications to constants or single values. */
60
61 (for op (plus pointer_plus minus bit_ior bit_xor)
62 (simplify
63 (op @0 integer_zerop)
64 (non_lvalue @0)))
65
66 /* 0 +p index -> (type)index */
67 (simplify
68 (pointer_plus integer_zerop @1)
69 (non_lvalue (convert @1)))
70
71 /* See if ARG1 is zero and X + ARG1 reduces to X.
72 Likewise if the operands are reversed. */
73 (simplify
74 (plus:c @0 real_zerop@1)
75 (if (fold_real_zero_addition_p (type, @1, 0))
76 (non_lvalue @0)))
77
78 /* See if ARG1 is zero and X - ARG1 reduces to X. */
79 (simplify
80 (minus @0 real_zerop@1)
81 (if (fold_real_zero_addition_p (type, @1, 1))
82 (non_lvalue @0)))
83
84 /* Simplify x - x.
85 This is unsafe for certain floats even in non-IEEE formats.
86 In IEEE, it is unsafe because it does wrong for NaNs.
87 Also note that operand_equal_p is always false if an operand
88 is volatile. */
89 (simplify
90 (minus @0 @0)
91 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
92 { build_zero_cst (type); }))
93
94 (simplify
95 (mult @0 integer_zerop@1)
96 @1)
97
98 /* Maybe fold x * 0 to 0. The expressions aren't the same
99 when x is NaN, since x * 0 is also NaN. Nor are they the
100 same in modes with signed zeros, since multiplying a
101 negative value by 0 gives -0, not +0. */
102 (simplify
103 (mult @0 real_zerop@1)
104 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
105 @1))
106
107 /* In IEEE floating point, x*1 is not equivalent to x for snans.
108 Likewise for complex arithmetic with signed zeros. */
109 (simplify
110 (mult @0 real_onep)
111 (if (!HONOR_SNANS (type)
112 && (!HONOR_SIGNED_ZEROS (type)
113 || !COMPLEX_FLOAT_TYPE_P (type)))
114 (non_lvalue @0)))
115
116 /* Transform x * -1.0 into -x. */
117 (simplify
118 (mult @0 real_minus_onep)
119 (if (!HONOR_SNANS (type)
120 && (!HONOR_SIGNED_ZEROS (type)
121 || !COMPLEX_FLOAT_TYPE_P (type)))
122 (negate @0)))
123
124 /* Make sure to preserve divisions by zero. This is the reason why
125 we don't simplify x / x to 1 or 0 / x to 0. */
126 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
127 (simplify
128 (op @0 integer_onep)
129 (non_lvalue @0)))
130
131 /* X / -1 is -X. */
132 (for div (trunc_div ceil_div floor_div round_div exact_div)
133 (simplify
134 (div @0 integer_minus_onep@1)
135 (if (!TYPE_UNSIGNED (type))
136 (negate @0))))
137
138 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
139 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
140 (simplify
141 (floor_div @0 @1)
142 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
143 && TYPE_UNSIGNED (type))
144 (trunc_div @0 @1)))
145
146 /* Combine two successive divisions. Note that combining ceil_div
147 and floor_div is trickier and combining round_div even more so. */
148 (for div (trunc_div exact_div)
149 (simplify
150 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
151 (with {
152 bool overflow_p;
153 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
154 }
155 (if (!overflow_p)
156 (div @0 { wide_int_to_tree (type, mul); })
157 (if (TYPE_UNSIGNED (type)
158 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
159 { build_zero_cst (type); })))))
160
161 /* Optimize A / A to 1.0 if we don't care about
162 NaNs or Infinities. */
163 (simplify
164 (rdiv @0 @0)
165 (if (FLOAT_TYPE_P (type)
166 && ! HONOR_NANS (type)
167 && ! HONOR_INFINITIES (type))
168 { build_one_cst (type); }))
169
170 /* Optimize -A / A to -1.0 if we don't care about
171 NaNs or Infinities. */
172 (simplify
173 (rdiv:c @0 (negate @0))
174 (if (FLOAT_TYPE_P (type)
175 && ! HONOR_NANS (type)
176 && ! HONOR_INFINITIES (type))
177 { build_minus_one_cst (type); }))
178
179 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
180 (simplify
181 (rdiv @0 real_onep)
182 (if (!HONOR_SNANS (type))
183 (non_lvalue @0)))
184
185 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
186 (simplify
187 (rdiv @0 real_minus_onep)
188 (if (!HONOR_SNANS (type))
189 (negate @0)))
190
191 /* If ARG1 is a constant, we can convert this to a multiply by the
192 reciprocal. This does not have the same rounding properties,
193 so only do this if -freciprocal-math. We can actually
194 always safely do it if ARG1 is a power of two, but it's hard to
195 tell if it is or not in a portable manner. */
196 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
197 (simplify
198 (rdiv @0 cst@1)
199 (if (optimize)
200 (if (flag_reciprocal_math
201 && !real_zerop (@1))
202 (with
203 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
204 (if (tem)
205 (mult @0 { tem; } )))
206 (if (cst != COMPLEX_CST)
207 (with { tree inverse = exact_inverse (type, @1); }
208 (if (inverse)
209 (mult @0 { inverse; } ))))))))
210
211 /* Same applies to modulo operations, but fold is inconsistent here
212 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
213 (for mod (ceil_mod floor_mod round_mod trunc_mod)
214 /* 0 % X is always zero. */
215 (simplify
216 (mod integer_zerop@0 @1)
217 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
218 (if (!integer_zerop (@1))
219 @0))
220 /* X % 1 is always zero. */
221 (simplify
222 (mod @0 integer_onep)
223 { build_zero_cst (type); })
224 /* X % -1 is zero. */
225 (simplify
226 (mod @0 integer_minus_onep@1)
227 (if (!TYPE_UNSIGNED (type))
228 { build_zero_cst (type); }))
229 /* (X % Y) % Y is just X % Y. */
230 (simplify
231 (mod (mod@2 @0 @1) @1)
232 @2)
233 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
234 (simplify
235 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
236 (if (ANY_INTEGRAL_TYPE_P (type)
237 && TYPE_OVERFLOW_UNDEFINED (type)
238 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
239 { build_zero_cst (type); })))
240
241 /* X % -C is the same as X % C. */
242 (simplify
243 (trunc_mod @0 INTEGER_CST@1)
244 (if (TYPE_SIGN (type) == SIGNED
245 && !TREE_OVERFLOW (@1)
246 && wi::neg_p (@1)
247 && !TYPE_OVERFLOW_TRAPS (type)
248 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
249 && !sign_bit_p (@1, @1))
250 (trunc_mod @0 (negate @1))))
251
252 /* X % -Y is the same as X % Y. */
253 (simplify
254 (trunc_mod @0 (convert? (negate @1)))
255 (if (!TYPE_UNSIGNED (type)
256 && !TYPE_OVERFLOW_TRAPS (type)
257 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
258 (trunc_mod @0 (convert @1))))
259
260 /* X - (X / Y) * Y is the same as X % Y. */
261 (simplify
262 (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1)))
263 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
264 (trunc_mod (convert @0) (convert @1))))
265
266 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
267 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
268 Also optimize A % (C << N) where C is a power of 2,
269 to A & ((C << N) - 1). */
270 (match (power_of_two_cand @1)
271 INTEGER_CST@1)
272 (match (power_of_two_cand @1)
273 (lshift INTEGER_CST@1 @2))
274 (for mod (trunc_mod floor_mod)
275 (simplify
276 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
277 (if ((TYPE_UNSIGNED (type)
278 || tree_expr_nonnegative_p (@0))
279 && tree_nop_conversion_p (type, TREE_TYPE (@3))
280 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
281 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
282
283 /* X % Y is smaller than Y. */
284 (for cmp (lt ge)
285 (simplify
286 (cmp (trunc_mod @0 @1) @1)
287 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
288 { constant_boolean_node (cmp == LT_EXPR, type); })))
289 (for cmp (gt le)
290 (simplify
291 (cmp @1 (trunc_mod @0 @1))
292 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
293 { constant_boolean_node (cmp == GT_EXPR, type); })))
294
295 /* x | ~0 -> ~0 */
296 (simplify
297 (bit_ior @0 integer_all_onesp@1)
298 @1)
299
300 /* x & 0 -> 0 */
301 (simplify
302 (bit_and @0 integer_zerop@1)
303 @1)
304
305 /* ~x | x -> -1 */
306 /* ~x ^ x -> -1 */
307 /* ~x + x -> -1 */
308 (for op (bit_ior bit_xor plus)
309 (simplify
310 (op:c (convert? @0) (convert? (bit_not @0)))
311 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
312
313 /* x ^ x -> 0 */
314 (simplify
315 (bit_xor @0 @0)
316 { build_zero_cst (type); })
317
318 /* Canonicalize X ^ ~0 to ~X. */
319 (simplify
320 (bit_xor @0 integer_all_onesp@1)
321 (bit_not @0))
322
323 /* x & ~0 -> x */
324 (simplify
325 (bit_and @0 integer_all_onesp)
326 (non_lvalue @0))
327
328 /* x & x -> x, x | x -> x */
329 (for bitop (bit_and bit_ior)
330 (simplify
331 (bitop @0 @0)
332 (non_lvalue @0)))
333
334 /* x + (x & 1) -> (x + 1) & ~1 */
335 (simplify
336 (plus:c @0 (bit_and:s @0 integer_onep@1))
337 (bit_and (plus @0 @1) (bit_not @1)))
338
339 /* x & ~(x & y) -> x & ~y */
340 /* x | ~(x | y) -> x | ~y */
341 (for bitop (bit_and bit_ior)
342 (simplify
343 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
344 (bitop @0 (bit_not @1))))
345
346 /* (x | y) & ~x -> y & ~x */
347 /* (x & y) | ~x -> y | ~x */
348 (for bitop (bit_and bit_ior)
349 rbitop (bit_ior bit_and)
350 (simplify
351 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
352 (bitop @1 @2)))
353
354 /* (x & y) ^ (x | y) -> x ^ y */
355 (simplify
356 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
357 (bit_xor @0 @1))
358
359 /* (x ^ y) ^ (x | y) -> x & y */
360 (simplify
361 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
362 (bit_and @0 @1))
363
364 /* (x & y) + (x ^ y) -> x | y */
365 /* (x & y) | (x ^ y) -> x | y */
366 /* (x & y) ^ (x ^ y) -> x | y */
367 (for op (plus bit_ior bit_xor)
368 (simplify
369 (op:c (bit_and @0 @1) (bit_xor @0 @1))
370 (bit_ior @0 @1)))
371
372 /* (x & y) + (x | y) -> x + y */
373 (simplify
374 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
375 (plus @0 @1))
376
377 /* (x + y) - (x | y) -> x & y */
378 (simplify
379 (minus (plus @0 @1) (bit_ior @0 @1))
380 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
381 && !TYPE_SATURATING (type))
382 (bit_and @0 @1)))
383
384 /* (x + y) - (x & y) -> x | y */
385 (simplify
386 (minus (plus @0 @1) (bit_and @0 @1))
387 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
388 && !TYPE_SATURATING (type))
389 (bit_ior @0 @1)))
390
391 /* (x | y) - (x ^ y) -> x & y */
392 (simplify
393 (minus (bit_ior @0 @1) (bit_xor @0 @1))
394 (bit_and @0 @1))
395
396 /* (x | y) - (x & y) -> x ^ y */
397 (simplify
398 (minus (bit_ior @0 @1) (bit_and @0 @1))
399 (bit_xor @0 @1))
400
401 /* (x | y) & ~(x & y) -> x ^ y */
402 (simplify
403 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
404 (bit_xor @0 @1))
405
406 /* (x | y) & (~x ^ y) -> x & y */
407 (simplify
408 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
409 (bit_and @0 @1))
410
411 /* ~x & ~y -> ~(x | y)
412 ~x | ~y -> ~(x & y) */
413 (for op (bit_and bit_ior)
414 rop (bit_ior bit_and)
415 (simplify
416 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
417 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
418 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
419 (bit_not (rop (convert @0) (convert @1))))))
420
421 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
422 with a constant, and the two constants have no bits in common,
423 we should treat this as a BIT_IOR_EXPR since this may produce more
424 simplifications. */
425 (for op (bit_xor plus)
426 (simplify
427 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
428 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
429 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
430 && tree_nop_conversion_p (type, TREE_TYPE (@2))
431 && wi::bit_and (@1, @3) == 0)
432 (bit_ior (convert @4) (convert @5)))))
433
434 /* (X | Y) ^ X -> Y & ~ X*/
435 (simplify
436 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
437 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
438 (convert (bit_and @1 (bit_not @0)))))
439
440 /* Convert ~X ^ ~Y to X ^ Y. */
441 (simplify
442 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
443 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
444 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
445 (bit_xor (convert @0) (convert @1))))
446
447 /* Convert ~X ^ C to X ^ ~C. */
448 (simplify
449 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
450 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
451 (bit_xor (convert @0) (bit_not @1))))
452
453 /* Fold (X & Y) ^ Y as ~X & Y. */
454 (simplify
455 (bit_xor:c (bit_and:c @0 @1) @1)
456 (bit_and (bit_not @0) @1))
457
458 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
459 operands are another bit-wise operation with a common input. If so,
460 distribute the bit operations to save an operation and possibly two if
461 constants are involved. For example, convert
462 (A | B) & (A | C) into A | (B & C)
463 Further simplification will occur if B and C are constants. */
464 (for op (bit_and bit_ior)
465 rop (bit_ior bit_and)
466 (simplify
467 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
468 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
469 (rop (convert @0) (op (convert @1) (convert @2))))))
470
471
472 (simplify
473 (abs (abs@1 @0))
474 @1)
475 (simplify
476 (abs (negate @0))
477 (abs @0))
478 (simplify
479 (abs tree_expr_nonnegative_p@0)
480 @0)
481
482 /* A - B -> A + (-B) if B is easily negatable. This just covers
483 very few cases of "easily negatable", effectively inlining negate_expr_p. */
484 (simplify
485 (minus @0 INTEGER_CST@1)
486 (if ((INTEGRAL_TYPE_P (type)
487 && TYPE_OVERFLOW_WRAPS (type))
488 || (!TYPE_OVERFLOW_SANITIZED (type)
489 && may_negate_without_overflow_p (@1)))
490 (plus @0 (negate @1))))
491 (simplify
492 (minus @0 REAL_CST@1)
493 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
494 (plus @0 (negate @1))))
495 (simplify
496 (minus @0 VECTOR_CST@1)
497 (if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
498 (plus @0 (negate @1))))
499
500
501 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
502 when profitable.
503 For bitwise binary operations apply operand conversions to the
504 binary operation result instead of to the operands. This allows
505 to combine successive conversions and bitwise binary operations.
506 We combine the above two cases by using a conditional convert. */
507 (for bitop (bit_and bit_ior bit_xor)
508 (simplify
509 (bitop (convert @0) (convert? @1))
510 (if (((TREE_CODE (@1) == INTEGER_CST
511 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
512 && int_fits_type_p (@1, TREE_TYPE (@0)))
513 || types_match (@0, @1))
514 /* ??? This transform conflicts with fold-const.c doing
515 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
516 constants (if x has signed type, the sign bit cannot be set
517 in c). This folds extension into the BIT_AND_EXPR.
518 Restrict it to GIMPLE to avoid endless recursions. */
519 && (bitop != BIT_AND_EXPR || GIMPLE)
520 && (/* That's a good idea if the conversion widens the operand, thus
521 after hoisting the conversion the operation will be narrower. */
522 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
523 /* It's also a good idea if the conversion is to a non-integer
524 mode. */
525 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
526 /* Or if the precision of TO is not the same as the precision
527 of its mode. */
528 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
529 (convert (bitop @0 (convert @1))))))
530
531 (for bitop (bit_and bit_ior)
532 rbitop (bit_ior bit_and)
533 /* (x | y) & x -> x */
534 /* (x & y) | x -> x */
535 (simplify
536 (bitop:c (rbitop:c @0 @1) @0)
537 @0)
538 /* (~x | y) & x -> x & y */
539 /* (~x & y) | x -> x | y */
540 (simplify
541 (bitop:c (rbitop:c (bit_not @0) @1) @0)
542 (bitop @0 @1)))
543
544 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
545 (for bitop (bit_and bit_ior bit_xor)
546 (simplify
547 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
548 (bit_and (bitop @0 @2) @1)))
549
550 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
551 (simplify
552 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
553 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
554
555 /* Combine successive equal operations with constants. */
556 (for bitop (bit_and bit_ior bit_xor)
557 (simplify
558 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
559 (bitop @0 (bitop @1 @2))))
560
561 /* Try simple folding for X op !X, and X op X with the help
562 of the truth_valued_p and logical_inverted_value predicates. */
563 (match truth_valued_p
564 @0
565 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
566 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
567 (match truth_valued_p
568 (op @0 @1)))
569 (match truth_valued_p
570 (truth_not @0))
571
572 (match (logical_inverted_value @0)
573 (bit_not truth_valued_p@0))
574 (match (logical_inverted_value @0)
575 (eq @0 integer_zerop))
576 (match (logical_inverted_value @0)
577 (ne truth_valued_p@0 integer_truep))
578 (match (logical_inverted_value @0)
579 (bit_xor truth_valued_p@0 integer_truep))
580
581 /* X & !X -> 0. */
582 (simplify
583 (bit_and:c @0 (logical_inverted_value @0))
584 { build_zero_cst (type); })
585 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
586 (for op (bit_ior bit_xor)
587 (simplify
588 (op:c truth_valued_p@0 (logical_inverted_value @0))
589 { constant_boolean_node (true, type); }))
590
591 /* If arg1 and arg2 are booleans (or any single bit type)
592 then try to simplify:
593
594 (~X & Y) -> X < Y
595 (X & ~Y) -> Y < X
596 (~X | Y) -> X <= Y
597 (X | ~Y) -> Y <= X
598
599 But only do this if our result feeds into a comparison as
600 this transformation is not always a win, particularly on
601 targets with and-not instructions.
602 -> simplify_bitwise_binary_boolean */
603 (simplify
604 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
605 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
606 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
607 (lt @0 @1)))
608 (simplify
609 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
610 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
611 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
612 (le @0 @1)))
613
614 /* ~~x -> x */
615 (simplify
616 (bit_not (bit_not @0))
617 @0)
618
619 /* Convert ~ (-A) to A - 1. */
620 (simplify
621 (bit_not (convert? (negate @0)))
622 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
623 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
624
625 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
626 (simplify
627 (bit_not (convert? (minus @0 integer_each_onep)))
628 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
629 (convert (negate @0))))
630 (simplify
631 (bit_not (convert? (plus @0 integer_all_onesp)))
632 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
633 (convert (negate @0))))
634
635 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
636 (simplify
637 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
638 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
639 (convert (bit_xor @0 (bit_not @1)))))
640 (simplify
641 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
642 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
643 (convert (bit_xor @0 @1))))
644
645 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
646 (simplify
647 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
648 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
649
650
651 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
652 (simplify
653 (pointer_plus (pointer_plus:s @0 @1) @3)
654 (pointer_plus @0 (plus @1 @3)))
655
656 /* Pattern match
657 tem1 = (long) ptr1;
658 tem2 = (long) ptr2;
659 tem3 = tem2 - tem1;
660 tem4 = (unsigned long) tem3;
661 tem5 = ptr1 + tem4;
662 and produce
663 tem5 = ptr2; */
664 (simplify
665 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
666 /* Conditionally look through a sign-changing conversion. */
667 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
668 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
669 || (GENERIC && type == TREE_TYPE (@1))))
670 @1))
671
672 /* Pattern match
673 tem = (sizetype) ptr;
674 tem = tem & algn;
675 tem = -tem;
676 ... = ptr p+ tem;
677 and produce the simpler and easier to analyze with respect to alignment
678 ... = ptr & ~algn; */
679 (simplify
680 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
681 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
682 (bit_and @0 { algn; })))
683
684 /* Try folding difference of addresses. */
685 (simplify
686 (minus (convert ADDR_EXPR@0) (convert @1))
687 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
688 (with { HOST_WIDE_INT diff; }
689 (if (ptr_difference_const (@0, @1, &diff))
690 { build_int_cst_type (type, diff); }))))
691 (simplify
692 (minus (convert @0) (convert ADDR_EXPR@1))
693 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
694 (with { HOST_WIDE_INT diff; }
695 (if (ptr_difference_const (@0, @1, &diff))
696 { build_int_cst_type (type, diff); }))))
697
698 /* If arg0 is derived from the address of an object or function, we may
699 be able to fold this expression using the object or function's
700 alignment. */
701 (simplify
702 (bit_and (convert? @0) INTEGER_CST@1)
703 (if (POINTER_TYPE_P (TREE_TYPE (@0))
704 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
705 (with
706 {
707 unsigned int align;
708 unsigned HOST_WIDE_INT bitpos;
709 get_pointer_alignment_1 (@0, &align, &bitpos);
710 }
711 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
712 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
713
714
715 /* We can't reassociate at all for saturating types. */
716 (if (!TYPE_SATURATING (type))
717
718 /* Contract negates. */
719 /* A + (-B) -> A - B */
720 (simplify
721 (plus:c (convert1? @0) (convert2? (negate @1)))
722 /* Apply STRIP_NOPS on @0 and the negate. */
723 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
724 && tree_nop_conversion_p (type, TREE_TYPE (@1))
725 && !TYPE_OVERFLOW_SANITIZED (type))
726 (minus (convert @0) (convert @1))))
727 /* A - (-B) -> A + B */
728 (simplify
729 (minus (convert1? @0) (convert2? (negate @1)))
730 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
731 && tree_nop_conversion_p (type, TREE_TYPE (@1))
732 && !TYPE_OVERFLOW_SANITIZED (type))
733 (plus (convert @0) (convert @1))))
734 /* -(-A) -> A */
735 (simplify
736 (negate (convert? (negate @1)))
737 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
738 && !TYPE_OVERFLOW_SANITIZED (type))
739 (convert @1)))
740
741 /* We can't reassociate floating-point unless -fassociative-math
742 or fixed-point plus or minus because of saturation to +-Inf. */
743 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
744 && !FIXED_POINT_TYPE_P (type))
745
746 /* Match patterns that allow contracting a plus-minus pair
747 irrespective of overflow issues. */
748 /* (A +- B) - A -> +- B */
749 /* (A +- B) -+ B -> A */
750 /* A - (A +- B) -> -+ B */
751 /* A +- (B -+ A) -> +- B */
752 (simplify
753 (minus (plus:c @0 @1) @0)
754 @1)
755 (simplify
756 (minus (minus @0 @1) @0)
757 (negate @1))
758 (simplify
759 (plus:c (minus @0 @1) @1)
760 @0)
761 (simplify
762 (minus @0 (plus:c @0 @1))
763 (negate @1))
764 (simplify
765 (minus @0 (minus @0 @1))
766 @1)
767
768 /* (A +- CST) +- CST -> A + CST */
769 (for outer_op (plus minus)
770 (for inner_op (plus minus)
771 (simplify
772 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
773 /* If the constant operation overflows we cannot do the transform
774 as we would introduce undefined overflow, for example
775 with (a - 1) + INT_MIN. */
776 (with { tree cst = fold_binary (outer_op == inner_op
777 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
778 (if (cst && !TREE_OVERFLOW (cst))
779 (inner_op @0 { cst; } ))))))
780
781 /* (CST - A) +- CST -> CST - A */
782 (for outer_op (plus minus)
783 (simplify
784 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
785 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
786 (if (cst && !TREE_OVERFLOW (cst))
787 (minus { cst; } @0)))))
788
789 /* ~A + A -> -1 */
790 (simplify
791 (plus:c (bit_not @0) @0)
792 (if (!TYPE_OVERFLOW_TRAPS (type))
793 { build_all_ones_cst (type); }))
794
795 /* ~A + 1 -> -A */
796 (simplify
797 (plus (convert? (bit_not @0)) integer_each_onep)
798 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
799 (negate (convert @0))))
800
801 /* -A - 1 -> ~A */
802 (simplify
803 (minus (convert? (negate @0)) integer_each_onep)
804 (if (!TYPE_OVERFLOW_TRAPS (type)
805 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
806 (bit_not (convert @0))))
807
808 /* -1 - A -> ~A */
809 (simplify
810 (minus integer_all_onesp @0)
811 (bit_not @0))
812
813 /* (T)(P + A) - (T)P -> (T) A */
814 (for add (plus pointer_plus)
815 (simplify
816 (minus (convert (add @0 @1))
817 (convert @0))
818 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
819 /* For integer types, if A has a smaller type
820 than T the result depends on the possible
821 overflow in P + A.
822 E.g. T=size_t, A=(unsigned)429497295, P>0.
823 However, if an overflow in P + A would cause
824 undefined behavior, we can assume that there
825 is no overflow. */
826 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
827 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
828 /* For pointer types, if the conversion of A to the
829 final type requires a sign- or zero-extension,
830 then we have to punt - it is not defined which
831 one is correct. */
832 || (POINTER_TYPE_P (TREE_TYPE (@0))
833 && TREE_CODE (@1) == INTEGER_CST
834 && tree_int_cst_sign_bit (@1) == 0))
835 (convert @1))))))
836
837
838 /* Simplifications of MIN_EXPR and MAX_EXPR. */
839
840 (for minmax (min max)
841 (simplify
842 (minmax @0 @0)
843 @0))
844 (simplify
845 (min @0 @1)
846 (if (INTEGRAL_TYPE_P (type)
847 && TYPE_MIN_VALUE (type)
848 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
849 @1))
850 (simplify
851 (max @0 @1)
852 (if (INTEGRAL_TYPE_P (type)
853 && TYPE_MAX_VALUE (type)
854 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
855 @1))
856
857
858 /* Simplifications of shift and rotates. */
859
860 (for rotate (lrotate rrotate)
861 (simplify
862 (rotate integer_all_onesp@0 @1)
863 @0))
864
865 /* Optimize -1 >> x for arithmetic right shifts. */
866 (simplify
867 (rshift integer_all_onesp@0 @1)
868 (if (!TYPE_UNSIGNED (type)
869 && tree_expr_nonnegative_p (@1))
870 @0))
871
872 (for shiftrotate (lrotate rrotate lshift rshift)
873 (simplify
874 (shiftrotate @0 integer_zerop)
875 (non_lvalue @0))
876 (simplify
877 (shiftrotate integer_zerop@0 @1)
878 @0)
879 /* Prefer vector1 << scalar to vector1 << vector2
880 if vector2 is uniform. */
881 (for vec (VECTOR_CST CONSTRUCTOR)
882 (simplify
883 (shiftrotate @0 vec@1)
884 (with { tree tem = uniform_vector_p (@1); }
885 (if (tem)
886 (shiftrotate @0 { tem; }))))))
887
888 /* Rewrite an LROTATE_EXPR by a constant into an
889 RROTATE_EXPR by a new constant. */
890 (simplify
891 (lrotate @0 INTEGER_CST@1)
892 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
893 build_int_cst (TREE_TYPE (@1),
894 element_precision (type)), @1); }))
895
896 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
897 (for op (lrotate rrotate rshift lshift)
898 (simplify
899 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
900 (with { unsigned int prec = element_precision (type); }
901 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
902 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
903 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
904 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
905 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
906 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
907 being well defined. */
908 (if (low >= prec)
909 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
910 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
911 (if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
912 { build_zero_cst (type); }
913 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
914 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
915
916
917 /* ((1 << A) & 1) != 0 -> A == 0
918 ((1 << A) & 1) == 0 -> A != 0 */
919 (for cmp (ne eq)
920 icmp (eq ne)
921 (simplify
922 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
923 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
924
925 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
926 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
927 if CST2 != 0. */
928 (for cmp (ne eq)
929 (simplify
930 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
931 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
932 (if (cand < 0
933 || (!integer_zerop (@2)
934 && wi::ne_p (wi::lshift (@0, cand), @2)))
935 { constant_boolean_node (cmp == NE_EXPR, type); }
936 (if (!integer_zerop (@2)
937 && wi::eq_p (wi::lshift (@0, cand), @2))
938 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
939
940 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
941 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
942 if the new mask might be further optimized. */
943 (for shift (lshift rshift)
944 (simplify
945 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
946 INTEGER_CST@2)
947 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
948 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
949 && tree_fits_uhwi_p (@1)
950 && tree_to_uhwi (@1) > 0
951 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
952 (with
953 {
954 unsigned int shiftc = tree_to_uhwi (@1);
955 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
956 unsigned HOST_WIDE_INT newmask, zerobits = 0;
957 tree shift_type = TREE_TYPE (@3);
958 unsigned int prec;
959
960 if (shift == LSHIFT_EXPR)
961 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
962 else if (shift == RSHIFT_EXPR
963 && (TYPE_PRECISION (shift_type)
964 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
965 {
966 prec = TYPE_PRECISION (TREE_TYPE (@3));
967 tree arg00 = @0;
968 /* See if more bits can be proven as zero because of
969 zero extension. */
970 if (@3 != @0
971 && TYPE_UNSIGNED (TREE_TYPE (@0)))
972 {
973 tree inner_type = TREE_TYPE (@0);
974 if ((TYPE_PRECISION (inner_type)
975 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
976 && TYPE_PRECISION (inner_type) < prec)
977 {
978 prec = TYPE_PRECISION (inner_type);
979 /* See if we can shorten the right shift. */
980 if (shiftc < prec)
981 shift_type = inner_type;
982 /* Otherwise X >> C1 is all zeros, so we'll optimize
983 it into (X, 0) later on by making sure zerobits
984 is all ones. */
985 }
986 }
987 zerobits = ~(unsigned HOST_WIDE_INT) 0;
988 if (shiftc < prec)
989 {
990 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
991 zerobits <<= prec - shiftc;
992 }
993 /* For arithmetic shift if sign bit could be set, zerobits
994 can contain actually sign bits, so no transformation is
995 possible, unless MASK masks them all away. In that
996 case the shift needs to be converted into logical shift. */
997 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
998 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
999 {
1000 if ((mask & zerobits) == 0)
1001 shift_type = unsigned_type_for (TREE_TYPE (@3));
1002 else
1003 zerobits = 0;
1004 }
1005 }
1006 }
1007 /* ((X << 16) & 0xff00) is (X, 0). */
1008 (if ((mask & zerobits) == mask)
1009 { build_int_cst (type, 0); }
1010 (with { newmask = mask | zerobits; }
1011 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1012 (with
1013 {
1014 /* Only do the transformation if NEWMASK is some integer
1015 mode's mask. */
1016 for (prec = BITS_PER_UNIT;
1017 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1018 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1019 break;
1020 }
1021 (if (prec < HOST_BITS_PER_WIDE_INT
1022 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1023 (with
1024 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1025 (if (!tree_int_cst_equal (newmaskt, @2))
1026 (if (shift_type != TREE_TYPE (@3))
1027 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1028 (bit_and @4 { newmaskt; })))))))))))))
1029
1030 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
1031 (X & C2) >> C1 into (X >> C1) & (C2 >> C1). */
1032 (for shift (lshift rshift)
1033 (simplify
1034 (shift (convert? (bit_and @0 INTEGER_CST@2)) INTEGER_CST@1)
1035 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1036 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1037 (bit_and (shift (convert @0) @1) { mask; })))))
1038
1039
1040 /* Simplifications of conversions. */
1041
1042 /* Basic strip-useless-type-conversions / strip_nops. */
1043 (for cvt (convert view_convert float fix_trunc)
1044 (simplify
1045 (cvt @0)
1046 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1047 || (GENERIC && type == TREE_TYPE (@0)))
1048 @0)))
1049
1050 /* Contract view-conversions. */
1051 (simplify
1052 (view_convert (view_convert @0))
1053 (view_convert @0))
1054
1055 /* For integral conversions with the same precision or pointer
1056 conversions use a NOP_EXPR instead. */
1057 (simplify
1058 (view_convert @0)
1059 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1060 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1061 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1062 (convert @0)))
1063
1064 /* Strip inner integral conversions that do not change precision or size. */
1065 (simplify
1066 (view_convert (convert@0 @1))
1067 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1068 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1069 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1070 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1071 (view_convert @1)))
1072
1073 /* Re-association barriers around constants and other re-association
1074 barriers can be removed. */
1075 (simplify
1076 (paren CONSTANT_CLASS_P@0)
1077 @0)
1078 (simplify
1079 (paren (paren@1 @0))
1080 @1)
1081
1082 /* Handle cases of two conversions in a row. */
1083 (for ocvt (convert float fix_trunc)
1084 (for icvt (convert float)
1085 (simplify
1086 (ocvt (icvt@1 @0))
1087 (with
1088 {
1089 tree inside_type = TREE_TYPE (@0);
1090 tree inter_type = TREE_TYPE (@1);
1091 int inside_int = INTEGRAL_TYPE_P (inside_type);
1092 int inside_ptr = POINTER_TYPE_P (inside_type);
1093 int inside_float = FLOAT_TYPE_P (inside_type);
1094 int inside_vec = VECTOR_TYPE_P (inside_type);
1095 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1096 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1097 int inter_int = INTEGRAL_TYPE_P (inter_type);
1098 int inter_ptr = POINTER_TYPE_P (inter_type);
1099 int inter_float = FLOAT_TYPE_P (inter_type);
1100 int inter_vec = VECTOR_TYPE_P (inter_type);
1101 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1102 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1103 int final_int = INTEGRAL_TYPE_P (type);
1104 int final_ptr = POINTER_TYPE_P (type);
1105 int final_float = FLOAT_TYPE_P (type);
1106 int final_vec = VECTOR_TYPE_P (type);
1107 unsigned int final_prec = TYPE_PRECISION (type);
1108 int final_unsignedp = TYPE_UNSIGNED (type);
1109 }
1110 /* In addition to the cases of two conversions in a row
1111 handled below, if we are converting something to its own
1112 type via an object of identical or wider precision, neither
1113 conversion is needed. */
1114 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1115 || (GENERIC
1116 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1117 && (((inter_int || inter_ptr) && final_int)
1118 || (inter_float && final_float))
1119 && inter_prec >= final_prec)
1120 (ocvt @0)
1121
1122 /* Likewise, if the intermediate and initial types are either both
1123 float or both integer, we don't need the middle conversion if the
1124 former is wider than the latter and doesn't change the signedness
1125 (for integers). Avoid this if the final type is a pointer since
1126 then we sometimes need the middle conversion. Likewise if the
1127 final type has a precision not equal to the size of its mode. */
1128 (if (((inter_int && inside_int) || (inter_float && inside_float))
1129 && (final_int || final_float)
1130 && inter_prec >= inside_prec
1131 && (inter_float || inter_unsignedp == inside_unsignedp)
1132 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1133 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1134 (ocvt @0)
1135
1136 /* If we have a sign-extension of a zero-extended value, we can
1137 replace that by a single zero-extension. Likewise if the
1138 final conversion does not change precision we can drop the
1139 intermediate conversion. */
1140 (if (inside_int && inter_int && final_int
1141 && ((inside_prec < inter_prec && inter_prec < final_prec
1142 && inside_unsignedp && !inter_unsignedp)
1143 || final_prec == inter_prec))
1144 (ocvt @0)
1145
1146 /* Two conversions in a row are not needed unless:
1147 - some conversion is floating-point (overstrict for now), or
1148 - some conversion is a vector (overstrict for now), or
1149 - the intermediate type is narrower than both initial and
1150 final, or
1151 - the intermediate type and innermost type differ in signedness,
1152 and the outermost type is wider than the intermediate, or
1153 - the initial type is a pointer type and the precisions of the
1154 intermediate and final types differ, or
1155 - the final type is a pointer type and the precisions of the
1156 initial and intermediate types differ. */
1157 (if (! inside_float && ! inter_float && ! final_float
1158 && ! inside_vec && ! inter_vec && ! final_vec
1159 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1160 && ! (inside_int && inter_int
1161 && inter_unsignedp != inside_unsignedp
1162 && inter_prec < final_prec)
1163 && ((inter_unsignedp && inter_prec > inside_prec)
1164 == (final_unsignedp && final_prec > inter_prec))
1165 && ! (inside_ptr && inter_prec != final_prec)
1166 && ! (final_ptr && inside_prec != inter_prec)
1167 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1168 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1169 (ocvt @0)
1170
1171 /* A truncation to an unsigned type (a zero-extension) should be
1172 canonicalized as bitwise and of a mask. */
1173 (if (final_int && inter_int && inside_int
1174 && final_prec == inside_prec
1175 && final_prec > inter_prec
1176 && inter_unsignedp)
1177 (convert (bit_and @0 { wide_int_to_tree
1178 (inside_type,
1179 wi::mask (inter_prec, false,
1180 TYPE_PRECISION (inside_type))); }))
1181
1182 /* If we are converting an integer to a floating-point that can
1183 represent it exactly and back to an integer, we can skip the
1184 floating-point conversion. */
1185 (if (GIMPLE /* PR66211 */
1186 && inside_int && inter_float && final_int &&
1187 (unsigned) significand_size (TYPE_MODE (inter_type))
1188 >= inside_prec - !inside_unsignedp)
1189 (convert @0)))))))))))
1190
1191 /* If we have a narrowing conversion to an integral type that is fed by a
1192 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1193 masks off bits outside the final type (and nothing else). */
1194 (simplify
1195 (convert (bit_and @0 INTEGER_CST@1))
1196 (if (INTEGRAL_TYPE_P (type)
1197 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1198 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1199 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1200 TYPE_PRECISION (type)), 0))
1201 (convert @0)))
1202
1203
1204 /* (X /[ex] A) * A -> X. */
1205 (simplify
1206 (mult (convert? (exact_div @0 @1)) @1)
1207 /* Look through a sign-changing conversion. */
1208 (convert @0))
1209
1210 /* Canonicalization of binary operations. */
1211
1212 /* Convert X + -C into X - C. */
1213 (simplify
1214 (plus @0 REAL_CST@1)
1215 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1216 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
1217 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1218 (minus @0 { tem; })))))
1219
1220 /* Convert x+x into x*2.0. */
1221 (simplify
1222 (plus @0 @0)
1223 (if (SCALAR_FLOAT_TYPE_P (type))
1224 (mult @0 { build_real (type, dconst2); })))
1225
1226 (simplify
1227 (minus integer_zerop @1)
1228 (negate @1))
1229
1230 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1231 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1232 (-ARG1 + ARG0) reduces to -ARG1. */
1233 (simplify
1234 (minus real_zerop@0 @1)
1235 (if (fold_real_zero_addition_p (type, @0, 0))
1236 (negate @1)))
1237
1238 /* Transform x * -1 into -x. */
1239 (simplify
1240 (mult @0 integer_minus_onep)
1241 (negate @0))
1242
1243 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1244 (simplify
1245 (complex (realpart @0) (imagpart @0))
1246 @0)
1247 (simplify
1248 (realpart (complex @0 @1))
1249 @0)
1250 (simplify
1251 (imagpart (complex @0 @1))
1252 @1)
1253
1254
1255 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1256 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1257 (simplify
1258 (bswap (bswap @0))
1259 @0)
1260 (simplify
1261 (bswap (bit_not (bswap @0)))
1262 (bit_not @0))
1263 (for bitop (bit_xor bit_ior bit_and)
1264 (simplify
1265 (bswap (bitop:c (bswap @0) @1))
1266 (bitop @0 (bswap @1)))))
1267
1268
1269 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1270
1271 /* Simplify constant conditions.
1272 Only optimize constant conditions when the selected branch
1273 has the same type as the COND_EXPR. This avoids optimizing
1274 away "c ? x : throw", where the throw has a void type.
1275 Note that we cannot throw away the fold-const.c variant nor
1276 this one as we depend on doing this transform before possibly
1277 A ? B : B -> B triggers and the fold-const.c one can optimize
1278 0 ? A : B to B even if A has side-effects. Something
1279 genmatch cannot handle. */
1280 (simplify
1281 (cond INTEGER_CST@0 @1 @2)
1282 (if (integer_zerop (@0))
1283 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1284 @2)
1285 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1286 @1)))
1287 (simplify
1288 (vec_cond VECTOR_CST@0 @1 @2)
1289 (if (integer_all_onesp (@0))
1290 @1
1291 (if (integer_zerop (@0))
1292 @2)))
1293
1294 (for cnd (cond vec_cond)
1295 /* A ? B : (A ? X : C) -> A ? B : C. */
1296 (simplify
1297 (cnd @0 (cnd @0 @1 @2) @3)
1298 (cnd @0 @1 @3))
1299 (simplify
1300 (cnd @0 @1 (cnd @0 @2 @3))
1301 (cnd @0 @1 @3))
1302
1303 /* A ? B : B -> B. */
1304 (simplify
1305 (cnd @0 @1 @1)
1306 @1)
1307
1308 /* !A ? B : C -> A ? C : B. */
1309 (simplify
1310 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1311 (cnd @0 @2 @1)))
1312
1313 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1314 return all-1 or all-0 results. */
1315 /* ??? We could instead convert all instances of the vec_cond to negate,
1316 but that isn't necessarily a win on its own. */
1317 (simplify
1318 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1319 (if (VECTOR_TYPE_P (type)
1320 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1321 && (TYPE_MODE (TREE_TYPE (type))
1322 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1323 (minus @3 (view_convert @0))))
1324
1325 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1326 (simplify
1327 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1328 (if (VECTOR_TYPE_P (type)
1329 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1330 && (TYPE_MODE (TREE_TYPE (type))
1331 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1332 (plus @3 (view_convert @0))))
1333
1334
1335 /* Simplifications of comparisons. */
1336
1337 /* We can simplify a logical negation of a comparison to the
1338 inverted comparison. As we cannot compute an expression
1339 operator using invert_tree_comparison we have to simulate
1340 that with expression code iteration. */
1341 (for cmp (tcc_comparison)
1342 icmp (inverted_tcc_comparison)
1343 ncmp (inverted_tcc_comparison_with_nans)
1344 /* Ideally we'd like to combine the following two patterns
1345 and handle some more cases by using
1346 (logical_inverted_value (cmp @0 @1))
1347 here but for that genmatch would need to "inline" that.
1348 For now implement what forward_propagate_comparison did. */
1349 (simplify
1350 (bit_not (cmp @0 @1))
1351 (if (VECTOR_TYPE_P (type)
1352 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1353 /* Comparison inversion may be impossible for trapping math,
1354 invert_tree_comparison will tell us. But we can't use
1355 a computed operator in the replacement tree thus we have
1356 to play the trick below. */
1357 (with { enum tree_code ic = invert_tree_comparison
1358 (cmp, HONOR_NANS (@0)); }
1359 (if (ic == icmp)
1360 (icmp @0 @1)
1361 (if (ic == ncmp)
1362 (ncmp @0 @1))))))
1363 (simplify
1364 (bit_xor (cmp @0 @1) integer_truep)
1365 (with { enum tree_code ic = invert_tree_comparison
1366 (cmp, HONOR_NANS (@0)); }
1367 (if (ic == icmp)
1368 (icmp @0 @1)
1369 (if (ic == ncmp)
1370 (ncmp @0 @1))))))
1371
1372 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1373 ??? The transformation is valid for the other operators if overflow
1374 is undefined for the type, but performing it here badly interacts
1375 with the transformation in fold_cond_expr_with_comparison which
1376 attempts to synthetize ABS_EXPR. */
1377 (for cmp (eq ne)
1378 (simplify
1379 (cmp (minus@2 @0 @1) integer_zerop)
1380 (if (single_use (@2))
1381 (cmp @0 @1))))
1382
1383 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1384 signed arithmetic case. That form is created by the compiler
1385 often enough for folding it to be of value. One example is in
1386 computing loop trip counts after Operator Strength Reduction. */
1387 (for cmp (simple_comparison)
1388 scmp (swapped_simple_comparison)
1389 (simplify
1390 (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
1391 /* Handle unfolded multiplication by zero. */
1392 (if (integer_zerop (@1))
1393 (cmp @1 @2)
1394 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1395 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1396 /* If @1 is negative we swap the sense of the comparison. */
1397 (if (tree_int_cst_sgn (@1) < 0)
1398 (scmp @0 @2)
1399 (cmp @0 @2))))))
1400
1401 /* Simplify comparison of something with itself. For IEEE
1402 floating-point, we can only do some of these simplifications. */
1403 (simplify
1404 (eq @0 @0)
1405 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1406 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1407 { constant_boolean_node (true, type); }))
1408 (for cmp (ge le)
1409 (simplify
1410 (cmp @0 @0)
1411 (eq @0 @0)))
1412 (for cmp (ne gt lt)
1413 (simplify
1414 (cmp @0 @0)
1415 (if (cmp != NE_EXPR
1416 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1417 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1418 { constant_boolean_node (false, type); })))
1419
1420 /* Fold ~X op ~Y as Y op X. */
1421 (for cmp (simple_comparison)
1422 (simplify
1423 (cmp (bit_not @0) (bit_not @1))
1424 (cmp @1 @0)))
1425
1426 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1427 (for cmp (simple_comparison)
1428 scmp (swapped_simple_comparison)
1429 (simplify
1430 (cmp (bit_not @0) CONSTANT_CLASS_P@1)
1431 (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
1432 (scmp @0 (bit_not @1)))))
1433
1434 (for cmp (simple_comparison)
1435 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1436 (simplify
1437 (cmp (convert@2 @0) (convert? @1))
1438 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1439 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1440 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1441 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1442 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1443 (with
1444 {
1445 tree type1 = TREE_TYPE (@1);
1446 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1447 {
1448 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1449 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1450 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1451 type1 = float_type_node;
1452 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1453 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1454 type1 = double_type_node;
1455 }
1456 tree newtype
1457 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1458 ? TREE_TYPE (@0) : type1);
1459 }
1460 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1461 (cmp (convert:newtype @0) (convert:newtype @1))))))
1462
1463 (simplify
1464 (cmp @0 REAL_CST@1)
1465 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1466 /* a CMP (-0) -> a CMP 0 */
1467 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1468 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); })
1469 /* x != NaN is always true, other ops are always false. */
1470 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1471 && ! HONOR_SNANS (@1))
1472 { constant_boolean_node (cmp == NE_EXPR, type); }
1473 /* Fold comparisons against infinity. */
1474 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1475 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1476 (with
1477 {
1478 REAL_VALUE_TYPE max;
1479 enum tree_code code = cmp;
1480 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1481 if (neg)
1482 code = swap_tree_comparison (code);
1483 }
1484 /* x > +Inf is always false, if with ignore sNANs. */
1485 (if (code == GT_EXPR
1486 && ! HONOR_SNANS (@0))
1487 { constant_boolean_node (false, type); }
1488 (if (code == LE_EXPR)
1489 /* x <= +Inf is always true, if we don't case about NaNs. */
1490 (if (! HONOR_NANS (@0))
1491 { constant_boolean_node (true, type); }
1492 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
1493 (eq @0 @0))
1494 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1495 (if (code == EQ_EXPR || code == GE_EXPR)
1496 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1497 (if (neg)
1498 (lt @0 { build_real (TREE_TYPE (@0), max); })
1499 (gt @0 { build_real (TREE_TYPE (@0), max); })))
1500 /* x < +Inf is always equal to x <= DBL_MAX. */
1501 (if (code == LT_EXPR)
1502 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1503 (if (neg)
1504 (ge @0 { build_real (TREE_TYPE (@0), max); })
1505 (le @0 { build_real (TREE_TYPE (@0), max); })))
1506 /* x != +Inf is always equal to !(x > DBL_MAX). */
1507 (if (code == NE_EXPR)
1508 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1509 (if (! HONOR_NANS (@0))
1510 (if (neg)
1511 (ge @0 { build_real (TREE_TYPE (@0), max); })
1512 (le @0 { build_real (TREE_TYPE (@0), max); }))
1513 (if (neg)
1514 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1515 { build_one_cst (type); })
1516 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1517 { build_one_cst (type); }))))))))))))))
1518
1519 /* If this is a comparison of a real constant with a PLUS_EXPR
1520 or a MINUS_EXPR of a real constant, we can convert it into a
1521 comparison with a revised real constant as long as no overflow
1522 occurs when unsafe_math_optimizations are enabled. */
1523 (if (flag_unsafe_math_optimizations)
1524 (for op (plus minus)
1525 (simplify
1526 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1527 (with
1528 {
1529 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1530 TREE_TYPE (@1), @2, @1);
1531 }
1532 (if (!TREE_OVERFLOW (tem))
1533 (cmp @0 { tem; }))))))
1534
1535 /* Likewise, we can simplify a comparison of a real constant with
1536 a MINUS_EXPR whose first operand is also a real constant, i.e.
1537 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
1538 floating-point types only if -fassociative-math is set. */
1539 (if (flag_associative_math)
1540 (simplify
1541 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1542 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1543 (if (!TREE_OVERFLOW (tem))
1544 (cmp { tem; } @1)))))
1545
1546 /* Fold comparisons against built-in math functions. */
1547 (if (flag_unsafe_math_optimizations
1548 && ! flag_errno_math)
1549 (for sq (SQRT)
1550 (simplify
1551 (cmp (sq @0) REAL_CST@1)
1552 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1553 /* sqrt(x) < y is always false, if y is negative. */
1554 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
1555 { constant_boolean_node (false, type); }
1556 /* sqrt(x) > y is always true, if y is negative and we
1557 don't care about NaNs, i.e. negative values of x. */
1558 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
1559 { constant_boolean_node (true, type); }
1560 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
1561 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
1562 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1563 (with
1564 {
1565 REAL_VALUE_TYPE c2;
1566 REAL_ARITHMETIC (c2, MULT_EXPR, TREE_REAL_CST (@1), TREE_REAL_CST (@1));
1567 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1568 }
1569 (if (REAL_VALUE_ISINF (c2))
1570 /* sqrt(x) > y is x == +Inf, when y is very large. */
1571 (if (HONOR_INFINITIES (@0))
1572 (eq @0 { build_real (TREE_TYPE (@0), c2); })
1573 { constant_boolean_node (false, type); })
1574 /* sqrt(x) > c is the same as x > c*c. */
1575 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))
1576 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1577 (with
1578 {
1579 REAL_VALUE_TYPE c2;
1580 REAL_ARITHMETIC (c2, MULT_EXPR, TREE_REAL_CST (@1), TREE_REAL_CST (@1));
1581 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1582 }
1583 (if (REAL_VALUE_ISINF (c2))
1584 /* sqrt(x) < y is always true, when y is a very large
1585 value and we don't care about NaNs or Infinities. */
1586 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
1587 { constant_boolean_node (true, type); }
1588 /* sqrt(x) < y is x != +Inf when y is very large and we
1589 don't care about NaNs. */
1590 (if (! HONOR_NANS (@0))
1591 (ne @0 { build_real (TREE_TYPE (@0), c2); })
1592 /* sqrt(x) < y is x >= 0 when y is very large and we
1593 don't care about Infinities. */
1594 (if (! HONOR_INFINITIES (@0))
1595 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1596 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
1597 (if (GENERIC)
1598 (truth_andif
1599 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1600 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))))
1601 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
1602 (if (! REAL_VALUE_ISINF (c2)
1603 && ! HONOR_NANS (@0))
1604 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
1605 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
1606 (if (! REAL_VALUE_ISINF (c2)
1607 && GENERIC)
1608 (truth_andif
1609 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1610 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))))))
1611
1612 /* Unordered tests if either argument is a NaN. */
1613 (simplify
1614 (bit_ior (unordered @0 @0) (unordered @1 @1))
1615 (if (types_match (@0, @1))
1616 (unordered @0 @1)))
1617 (simplify
1618 (bit_and (ordered @0 @0) (ordered @1 @1))
1619 (if (types_match (@0, @1))
1620 (ordered @0 @1)))
1621 (simplify
1622 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
1623 @2)
1624 (simplify
1625 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
1626 @2)
1627
1628 /* -A CMP -B -> B CMP A. */
1629 (for cmp (tcc_comparison)
1630 scmp (swapped_tcc_comparison)
1631 (simplify
1632 (cmp (negate @0) (negate @1))
1633 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1634 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1635 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1636 (scmp @0 @1)))
1637 (simplify
1638 (cmp (negate @0) CONSTANT_CLASS_P@1)
1639 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1640 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1641 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1642 (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
1643 (if (tem && !TREE_OVERFLOW (tem))
1644 (scmp @0 { tem; }))))))
1645
1646
1647 /* Equality compare simplifications from fold_binary */
1648 (for cmp (eq ne)
1649
1650 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
1651 Similarly for NE_EXPR. */
1652 (simplify
1653 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
1654 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1655 && wi::bit_and_not (@1, @2) != 0)
1656 { constant_boolean_node (cmp == NE_EXPR, type); }))
1657
1658 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
1659 (simplify
1660 (cmp (bit_xor @0 @1) integer_zerop)
1661 (cmp @0 @1))
1662
1663 /* (X ^ Y) == Y becomes X == 0.
1664 Likewise (X ^ Y) == X becomes Y == 0. */
1665 (simplify
1666 (cmp:c (bit_xor:c @0 @1) @0)
1667 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
1668
1669 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
1670 (simplify
1671 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
1672 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
1673 (cmp @0 (bit_xor @1 (convert @2))))))
1674
1675 /* Simplification of math builtins. */
1676
1677 /* fold_builtin_logarithm */
1678 (if (flag_unsafe_math_optimizations)
1679 /* Special case, optimize logN(expN(x)) = x. */
1680 (for logs (LOG LOG2 LOG10)
1681 exps (EXP EXP2 EXP10)
1682 (simplify
1683 (logs (exps @0))
1684 @0))
1685 /* Optimize logN(func()) for various exponential functions. We
1686 want to determine the value "x" and the power "exponent" in
1687 order to transform logN(x**exponent) into exponent*logN(x). */
1688 (for logs (LOG LOG LOG LOG
1689 LOG2 LOG2 LOG2 LOG2
1690 LOG10 LOG10 LOG10 LOG10)
1691 exps (EXP EXP2 EXP10 POW10)
1692 (simplify
1693 (logs (exps @0))
1694 (with {
1695 tree x;
1696 switch (exps)
1697 {
1698 CASE_FLT_FN (BUILT_IN_EXP):
1699 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
1700 x = build_real (type, real_value_truncate (TYPE_MODE (type),
1701 dconst_e ()));
1702 break;
1703 CASE_FLT_FN (BUILT_IN_EXP2):
1704 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
1705 x = build_real (type, dconst2);
1706 break;
1707 CASE_FLT_FN (BUILT_IN_EXP10):
1708 CASE_FLT_FN (BUILT_IN_POW10):
1709 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
1710 {
1711 REAL_VALUE_TYPE dconst10;
1712 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
1713 x = build_real (type, dconst10);
1714 }
1715 break;
1716 }
1717 }
1718 (mult (logs { x; }) @0))))
1719 (for logs (LOG LOG
1720 LOG2 LOG2
1721 LOG10 LOG10)
1722 exps (SQRT CBRT)
1723 (simplify
1724 (logs (exps @0))
1725 (with {
1726 tree x;
1727 switch (exps)
1728 {
1729 CASE_FLT_FN (BUILT_IN_SQRT):
1730 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
1731 x = build_real (type, dconsthalf);
1732 break;
1733 CASE_FLT_FN (BUILT_IN_CBRT):
1734 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
1735 x = build_real (type, real_value_truncate (TYPE_MODE (type),
1736 dconst_third ()));
1737 break;
1738 }
1739 }
1740 (mult { x; } (logs @0)))))
1741 /* logN(pow(x,exponent) -> exponent*logN(x). */
1742 (for logs (LOG LOG2 LOG10)
1743 pows (POW)
1744 (simplify
1745 (logs (pows @0 @1))
1746 (mult @1 (logs @0)))))
1747
1748 /* Narrowing of arithmetic and logical operations.
1749
1750 These are conceptually similar to the transformations performed for
1751 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
1752 term we want to move all that code out of the front-ends into here. */
1753
1754 /* If we have a narrowing conversion of an arithmetic operation where
1755 both operands are widening conversions from the same type as the outer
1756 narrowing conversion. Then convert the innermost operands to a suitable
1757 unsigned type (to avoid introducing undefined behaviour), perform the
1758 operation and convert the result to the desired type. */
1759 (for op (plus minus)
1760 (simplify
1761 (convert (op:s (convert@2 @0) (convert@3 @1)))
1762 (if (INTEGRAL_TYPE_P (type)
1763 /* We check for type compatibility between @0 and @1 below,
1764 so there's no need to check that @1/@3 are integral types. */
1765 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1766 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1767 /* The precision of the type of each operand must match the
1768 precision of the mode of each operand, similarly for the
1769 result. */
1770 && (TYPE_PRECISION (TREE_TYPE (@0))
1771 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
1772 && (TYPE_PRECISION (TREE_TYPE (@1))
1773 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
1774 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
1775 /* The inner conversion must be a widening conversion. */
1776 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
1777 && types_match (@0, @1)
1778 && types_match (@0, type))
1779 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1780 (convert (op @0 @1))
1781 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
1782 (convert (op (convert:utype @0) (convert:utype @1))))))))
1783
1784 /* This is another case of narrowing, specifically when there's an outer
1785 BIT_AND_EXPR which masks off bits outside the type of the innermost
1786 operands. Like the previous case we have to convert the operands
1787 to unsigned types to avoid introducing undefined behaviour for the
1788 arithmetic operation. */
1789 (for op (minus plus)
1790 (simplify
1791 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
1792 (if (INTEGRAL_TYPE_P (type)
1793 /* We check for type compatibility between @0 and @1 below,
1794 so there's no need to check that @1/@3 are integral types. */
1795 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1796 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1797 /* The precision of the type of each operand must match the
1798 precision of the mode of each operand, similarly for the
1799 result. */
1800 && (TYPE_PRECISION (TREE_TYPE (@0))
1801 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
1802 && (TYPE_PRECISION (TREE_TYPE (@1))
1803 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
1804 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
1805 /* The inner conversion must be a widening conversion. */
1806 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
1807 && types_match (@0, @1)
1808 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
1809 <= TYPE_PRECISION (TREE_TYPE (@0)))
1810 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1811 || tree_int_cst_sgn (@4) >= 0))
1812 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1813 (with { tree ntype = TREE_TYPE (@0); }
1814 (convert (bit_and (op @0 @1) (convert:ntype @4))))
1815 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
1816 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
1817 (convert:utype @4))))))))