comparison gcc/match.pd @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding. 1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c 2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it. 3 and generic-match.c from it.
4 4
5 Copyright (C) 2014-2018 Free Software Foundation, Inc. 5 Copyright (C) 2014-2020 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de> 6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com> 7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8 8
9 This file is part of GCC. 9 This file is part of GCC.
10 10
27 (define_predicates 27 (define_predicates
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep 28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop 29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep 30 real_zerop real_onep real_minus_onep
31 zerop 31 zerop
32 initializer_each_zero_or_onep
32 CONSTANT_CLASS_P 33 CONSTANT_CLASS_P
33 tree_expr_nonnegative_p 34 tree_expr_nonnegative_p
34 tree_expr_nonzero_p 35 tree_expr_nonzero_p
35 integer_valued_real_p 36 integer_valued_real_p
36 integer_pow2p 37 integer_pow2p
37 HONOR_NANS) 38 uniform_integer_cst_p
39 HONOR_NANS
40 uniform_vector_p)
38 41
39 /* Operator lists. */ 42 /* Operator lists. */
40 (define_operator_list tcc_comparison 43 (define_operator_list tcc_comparison
41 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) 44 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
42 (define_operator_list inverted_tcc_comparison 45 (define_operator_list inverted_tcc_comparison
78 /* Binary operations and their associated IFN_COND_* function. */ 81 /* Binary operations and their associated IFN_COND_* function. */
79 (define_operator_list UNCOND_BINARY 82 (define_operator_list UNCOND_BINARY
80 plus minus 83 plus minus
81 mult trunc_div trunc_mod rdiv 84 mult trunc_div trunc_mod rdiv
82 min max 85 min max
83 bit_and bit_ior bit_xor) 86 bit_and bit_ior bit_xor
87 lshift rshift)
84 (define_operator_list COND_BINARY 88 (define_operator_list COND_BINARY
85 IFN_COND_ADD IFN_COND_SUB 89 IFN_COND_ADD IFN_COND_SUB
86 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV 90 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
87 IFN_COND_MIN IFN_COND_MAX 91 IFN_COND_MIN IFN_COND_MAX
88 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR) 92 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
93 IFN_COND_SHL IFN_COND_SHR)
89 94
90 /* Same for ternary operations. */ 95 /* Same for ternary operations. */
91 (define_operator_list UNCOND_TERNARY 96 (define_operator_list UNCOND_TERNARY
92 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS) 97 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
93 (define_operator_list COND_TERNARY 98 (define_operator_list COND_TERNARY
94 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS) 99 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
95 100
96 /* As opposed to convert?, this still creates a single pattern, so 101 /* With nop_convert? combine convert? and view_convert? in one pattern
97 it is not a suitable replacement for convert? in all cases. */ 102 plus conditionalize on tree_nop_conversion_p conversions. */
98 (match (nop_convert @0) 103 (match (nop_convert @0)
99 (convert @0) 104 (convert @0)
100 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))))) 105 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
101 (match (nop_convert @0) 106 (match (nop_convert @0)
102 (view_convert @0) 107 (view_convert @0)
103 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0)) 108 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
104 && known_eq (TYPE_VECTOR_SUBPARTS (type), 109 && known_eq (TYPE_VECTOR_SUBPARTS (type),
105 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))) 110 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
106 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) 111 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
107 /* This one has to be last, or it shadows the others. */
108 (match (nop_convert @0)
109 @0)
110 112
111 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x> 113 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
112 ABSU_EXPR returns unsigned absolute value of the operand and the operand 114 ABSU_EXPR returns unsigned absolute value of the operand and the operand
113 of the ABSU_EXPR will have the corresponding signed type. */ 115 of the ABSU_EXPR will have the corresponding signed type. */
114 (simplify (abs (convert @0)) 116 (simplify (abs (convert @0))
148 (simplify 150 (simplify
149 (minus @0 real_zerop@1) 151 (minus @0 real_zerop@1)
150 (if (fold_real_zero_addition_p (type, @1, 1)) 152 (if (fold_real_zero_addition_p (type, @1, 1))
151 (non_lvalue @0))) 153 (non_lvalue @0)))
152 154
155 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
156 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
157 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
158 if not -frounding-math. For sNaNs the first operation would raise
159 exceptions but turn the result into qNan, so the second operation
160 would not raise it. */
161 (for inner_op (plus minus)
162 (for outer_op (plus minus)
163 (simplify
164 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
165 (if (real_zerop (@1)
166 && real_zerop (@2)
167 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
168 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
169 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
170 bool outer_plus
171 = ((outer_op == PLUS_EXPR)
172 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
173 (if (outer_plus && !inner_plus)
174 (outer_op @0 @2)
175 @3))))))
176
153 /* Simplify x - x. 177 /* Simplify x - x.
154 This is unsafe for certain floats even in non-IEEE formats. 178 This is unsafe for certain floats even in non-IEEE formats.
155 In IEEE, it is unsafe because it does wrong for NaNs. 179 In IEEE, it is unsafe because it does wrong for NaNs.
156 Also note that operand_equal_p is always false if an operand 180 Also note that operand_equal_p is always false if an operand
157 is volatile. */ 181 is volatile. */
190 (mult @0 real_minus_onep) 214 (mult @0 real_minus_onep)
191 (if (!HONOR_SNANS (type) 215 (if (!HONOR_SNANS (type)
192 && (!HONOR_SIGNED_ZEROS (type) 216 && (!HONOR_SIGNED_ZEROS (type)
193 || !COMPLEX_FLOAT_TYPE_P (type))) 217 || !COMPLEX_FLOAT_TYPE_P (type)))
194 (negate @0))) 218 (negate @0)))
219
220 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
221 (simplify
222 (mult SSA_NAME@1 SSA_NAME@2)
223 (if (INTEGRAL_TYPE_P (type)
224 && get_nonzero_bits (@1) == 1
225 && get_nonzero_bits (@2) == 1)
226 (bit_and @1 @2)))
227
228 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
229 unless the target has native support for the former but not the latter. */
230 (simplify
231 (mult @0 VECTOR_CST@1)
232 (if (initializer_each_zero_or_onep (@1)
233 && !HONOR_SNANS (type)
234 && !HONOR_SIGNED_ZEROS (type))
235 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
236 (if (itype
237 && (!VECTOR_MODE_P (TYPE_MODE (type))
238 || (VECTOR_MODE_P (TYPE_MODE (itype))
239 && optab_handler (and_optab,
240 TYPE_MODE (itype)) != CODE_FOR_nothing)))
241 (view_convert (bit_and:itype (view_convert @0)
242 (ne @1 { build_zero_cst (type); })))))))
195 243
196 (for cmp (gt ge lt le) 244 (for cmp (gt ge lt le)
197 outp (convert convert negate negate) 245 outp (convert convert negate negate)
198 outn (negate negate convert convert) 246 outn (negate negate convert convert)
199 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */ 247 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
252 (non_lvalue @0))) 300 (non_lvalue @0)))
253 301
254 /* (A / (1 << B)) -> (A >> B). 302 /* (A / (1 << B)) -> (A >> B).
255 Only for unsigned A. For signed A, this would not preserve rounding 303 Only for unsigned A. For signed A, this would not preserve rounding
256 toward zero. 304 toward zero.
257 For example: (-1 / ( 1 << B)) != -1 >> B. */ 305 For example: (-1 / ( 1 << B)) != -1 >> B.
258 (simplify 306 Also also widening conversions, like:
259 (trunc_div @0 (lshift integer_onep@1 @2)) 307 (A / (unsigned long long) (1U << B)) -> (A >> B)
308 or
309 (A / (unsigned long long) (1 << B)) -> (A >> B).
310 If the left shift is signed, it can be done only if the upper bits
311 of A starting from shift's type sign bit are zero, as
312 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
313 so it is valid only if A >> 31 is zero. */
314 (simplify
315 (trunc_div @0 (convert? (lshift integer_onep@1 @2)))
260 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) 316 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
261 && (!VECTOR_TYPE_P (type) 317 && (!VECTOR_TYPE_P (type)
262 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector) 318 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
263 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))) 319 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
320 && (useless_type_conversion_p (type, TREE_TYPE (@1))
321 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
322 && (TYPE_UNSIGNED (TREE_TYPE (@1))
323 || (element_precision (type)
324 == element_precision (TREE_TYPE (@1)))
325 || (INTEGRAL_TYPE_P (type)
326 && (tree_nonzero_bits (@0)
327 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
328 true,
329 element_precision (type))) == 0)))))
264 (rshift @0 @2))) 330 (rshift @0 @2)))
265 331
266 /* Preserve explicit divisions by 0: the C++ front-end wants to detect 332 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
267 undefined behavior in constexpr evaluation, and assuming that the division 333 undefined behavior in constexpr evaluation, and assuming that the division
268 traps enables better optimizations than these anyway. */ 334 traps enables better optimizations than these anyway. */
309 375
310 /* Combine two successive divisions. Note that combining ceil_div 376 /* Combine two successive divisions. Note that combining ceil_div
311 and floor_div is trickier and combining round_div even more so. */ 377 and floor_div is trickier and combining round_div even more so. */
312 (for div (trunc_div exact_div) 378 (for div (trunc_div exact_div)
313 (simplify 379 (simplify
314 (div (div @0 INTEGER_CST@1) INTEGER_CST@2) 380 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
315 (with { 381 (with {
316 wi::overflow_type overflow; 382 wi::overflow_type overflow;
317 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), 383 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
318 TYPE_SIGN (type), &overflow); 384 TYPE_SIGN (type), &overflow);
319 } 385 }
320 (if (!overflow) 386 (if (div == EXACT_DIV_EXPR
321 (div @0 { wide_int_to_tree (type, mul); }) 387 || optimize_successive_divisions_p (@2, @3))
322 (if (TYPE_UNSIGNED (type) 388 (if (!overflow)
323 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)) 389 (div @0 { wide_int_to_tree (type, mul); })
324 { build_zero_cst (type); }))))) 390 (if (TYPE_UNSIGNED (type)
391 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
392 { build_zero_cst (type); }))))))
325 393
326 /* Combine successive multiplications. Similar to above, but handling 394 /* Combine successive multiplications. Similar to above, but handling
327 overflow is different. */ 395 overflow is different. */
328 (simplify 396 (simplify
329 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2) 397 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
402 470
403 /* Simplify x / (- y) to -x / y. */ 471 /* Simplify x / (- y) to -x / y. */
404 (simplify 472 (simplify
405 (rdiv @0 (negate @1)) 473 (rdiv @0 (negate @1))
406 (rdiv (negate @0) @1)) 474 (rdiv (negate @0) @1))
475
476 (if (flag_unsafe_math_optimizations)
477 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
478 Since C / x may underflow to zero, do this only for unsafe math. */
479 (for op (lt le gt ge)
480 neg_op (gt ge lt le)
481 (simplify
482 (op (rdiv REAL_CST@0 @1) real_zerop@2)
483 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
484 (switch
485 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
486 (op @1 @2))
487 /* For C < 0, use the inverted operator. */
488 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
489 (neg_op @1 @2)))))))
407 490
408 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */ 491 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
409 (for div (trunc_div ceil_div floor_div round_div exact_div) 492 (for div (trunc_div ceil_div floor_div round_div exact_div)
410 (simplify 493 (simplify
411 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2) 494 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
588 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */ 671 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
589 (simplify 672 (simplify
590 (mult (abs@1 @0) @1) 673 (mult (abs@1 @0) @1)
591 (mult @0 @0)) 674 (mult @0 @0))
592 675
676 /* Convert absu(x)*absu(x) -> x*x. */
677 (simplify
678 (mult (absu@1 @0) @1)
679 (mult (convert@2 @0) @2))
680
593 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */ 681 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
594 (for coss (COS COSH) 682 (for coss (COS COSH)
595 copysigns (COPYSIGN) 683 copysigns (COPYSIGN)
596 (simplify 684 (simplify
597 (coss (copysigns @0 @1)) 685 (coss (copysigns @0 @1))
755 843
756 /* (a & ~b) ^ ~a --> ~(a & b) */ 844 /* (a & ~b) ^ ~a --> ~(a & b) */
757 (simplify 845 (simplify
758 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0)) 846 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
759 (bit_not (bit_and @0 @1))) 847 (bit_not (bit_and @0 @1)))
848
849 /* (~a & b) ^ a --> (a | b) */
850 (simplify
851 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
852 (bit_ior @0 @1))
760 853
761 /* (a | b) & ~(a ^ b) --> a & b */ 854 /* (a | b) & ~(a ^ b) --> a & b */
762 (simplify 855 (simplify
763 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1))) 856 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
764 (bit_and @0 @1)) 857 (bit_and @0 @1))
1119 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2))) 1212 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1120 (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) 1213 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1121 && tree_nop_conversion_p (type, TREE_TYPE (@2))) 1214 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1122 (bit_xor (convert @1) (convert @2)))) 1215 (bit_xor (convert @1) (convert @2))))
1123 1216
1217 /* Convert abs (abs (X)) into abs (X).
1218 also absu (absu (X)) into absu (X). */
1124 (simplify 1219 (simplify
1125 (abs (abs@1 @0)) 1220 (abs (abs@1 @0))
1126 @1) 1221 @1)
1222
1223 (simplify
1224 (absu (convert@2 (absu@1 @0)))
1225 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1226 @1))
1227
1228 /* Convert abs[u] (-X) -> abs[u] (X). */
1127 (simplify 1229 (simplify
1128 (abs (negate @0)) 1230 (abs (negate @0))
1129 (abs @0)) 1231 (abs @0))
1232
1233 (simplify
1234 (absu (negate @0))
1235 (absu @0))
1236
1237 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1130 (simplify 1238 (simplify
1131 (abs tree_expr_nonnegative_p@0) 1239 (abs tree_expr_nonnegative_p@0)
1132 @0) 1240 @0)
1241
1242 (simplify
1243 (absu tree_expr_nonnegative_p@0)
1244 (convert @0))
1133 1245
1134 /* A few cases of fold-const.c negate_expr_p predicate. */ 1246 /* A few cases of fold-const.c negate_expr_p predicate. */
1135 (match negate_expr_p 1247 (match negate_expr_p
1136 INTEGER_CST 1248 INTEGER_CST
1137 (if ((INTEGRAL_TYPE_P (type) 1249 (if ((INTEGRAL_TYPE_P (type)
1311 || !TYPE_UNSIGNED (TREE_TYPE (@0))) 1423 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1312 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); })))) 1424 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1313 1425
1314 /* Convert - (~A) to A + 1. */ 1426 /* Convert - (~A) to A + 1. */
1315 (simplify 1427 (simplify
1316 (negate (nop_convert (bit_not @0))) 1428 (negate (nop_convert? (bit_not @0)))
1317 (plus (view_convert @0) { build_each_one_cst (type); })) 1429 (plus (view_convert @0) { build_each_one_cst (type); }))
1318 1430
1319 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ 1431 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1320 (simplify 1432 (simplify
1321 (bit_not (convert? (minus @0 integer_each_onep))) 1433 (bit_not (convert? (minus @0 integer_each_onep)))
1338 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 1450 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1339 (convert (bit_xor @0 @1)))) 1451 (convert (bit_xor @0 @1))))
1340 1452
1341 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */ 1453 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1342 (simplify 1454 (simplify
1343 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1) 1455 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1344 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 1456 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1345 (bit_not (bit_xor (view_convert @0) @1)))) 1457 (bit_not (bit_xor (view_convert @0) @1))))
1346 1458
1347 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ 1459 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1348 (simplify 1460 (simplify
1412 (icmp (convert:stype @0) { build_int_cst (stype, 0); }))))) 1524 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1413 1525
1414 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */ 1526 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1415 (for cmp (simple_comparison) 1527 (for cmp (simple_comparison)
1416 (simplify 1528 (simplify
1417 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2)) 1529 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1418 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))) 1530 (if (element_precision (@3) >= element_precision (@0)
1419 (cmp @0 @1)))) 1531 && types_match (@0, @1))
1532 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1533 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1534 (cmp @1 @0)
1535 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1536 (with
1537 {
1538 tree utype = unsigned_type_for (TREE_TYPE (@0));
1539 }
1540 (cmp (convert:utype @1) (convert:utype @0)))))
1541 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1542 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1543 (cmp @0 @1)
1544 (with
1545 {
1546 tree utype = unsigned_type_for (TREE_TYPE (@0));
1547 }
1548 (cmp (convert:utype @0) (convert:utype @1)))))))))
1420 1549
1421 /* X / C1 op C2 into a simple range test. */ 1550 /* X / C1 op C2 into a simple range test. */
1422 (for cmp (simple_comparison) 1551 (for cmp (simple_comparison)
1423 (simplify 1552 (simplify
1424 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2) 1553 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1458 (with 1587 (with
1459 { 1588 {
1460 tree etype = range_check_type (TREE_TYPE (@0)); 1589 tree etype = range_check_type (TREE_TYPE (@0));
1461 if (etype) 1590 if (etype)
1462 { 1591 {
1463 if (! TYPE_UNSIGNED (etype))
1464 etype = unsigned_type_for (etype);
1465 hi = fold_convert (etype, hi); 1592 hi = fold_convert (etype, hi);
1466 lo = fold_convert (etype, lo); 1593 lo = fold_convert (etype, lo);
1467 hi = const_binop (MINUS_EXPR, etype, hi, lo); 1594 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1468 } 1595 }
1469 } 1596 }
1546 (for op (lt le gt ge) 1673 (for op (lt le gt ge)
1547 (simplify 1674 (simplify
1548 (op:c (plus:c@2 @0 @1) @1) 1675 (op:c (plus:c@2 @0 @1) @1)
1549 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1676 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1550 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 1677 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1678 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1551 && (CONSTANT_CLASS_P (@0) || single_use (@2))) 1679 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1552 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))) 1680 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1553 /* For equality, this is also true with wrapping overflow. */ 1681 /* For equality, this is also true with wrapping overflow. */
1554 (for op (eq ne) 1682 (for op (eq ne)
1555 (simplify 1683 (simplify
1556 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1)) 1684 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1557 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1685 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1558 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 1686 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1559 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) 1687 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1560 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3))) 1688 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1561 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2)) 1689 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1562 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))) 1690 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1563 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))) 1691 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1564 (simplify 1692 (simplify
1565 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0)) 1693 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1566 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)) 1694 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1567 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) 1695 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1568 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3)))) 1696 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1569 (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) 1697 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1570 1698
1750 } 1878 }
1751 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT)) 1879 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1752 { wide_int_to_tree (type, (wi::to_wide (@1) 1880 { wide_int_to_tree (type, (wi::to_wide (@1)
1753 & (bitpos / BITS_PER_UNIT))); })))) 1881 & (bitpos / BITS_PER_UNIT))); }))))
1754 1882
1883 (match min_value
1884 INTEGER_CST
1885 (if (INTEGRAL_TYPE_P (type)
1886 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
1887
1888 (match max_value
1889 INTEGER_CST
1890 (if (INTEGRAL_TYPE_P (type)
1891 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
1892
1893 /* x > y && x != XXX_MIN --> x > y
1894 x > y && x == XXX_MIN --> false . */
1895 (for eqne (eq ne)
1896 (simplify
1897 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
1898 (switch
1899 (if (eqne == EQ_EXPR)
1900 { constant_boolean_node (false, type); })
1901 (if (eqne == NE_EXPR)
1902 @2)
1903 )))
1904
1905 /* x < y && x != XXX_MAX --> x < y
1906 x < y && x == XXX_MAX --> false. */
1907 (for eqne (eq ne)
1908 (simplify
1909 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
1910 (switch
1911 (if (eqne == EQ_EXPR)
1912 { constant_boolean_node (false, type); })
1913 (if (eqne == NE_EXPR)
1914 @2)
1915 )))
1916
1917 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
1918 (simplify
1919 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
1920 @2)
1921
1922 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
1923 (simplify
1924 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
1925 @2)
1926
1927 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
1928 (simplify
1929 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
1930 @2)
1931
1932 /* x <= y || x != XXX_MIN --> true. */
1933 (simplify
1934 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
1935 { constant_boolean_node (true, type); })
1936
1937 /* x <= y || x == XXX_MIN --> x <= y. */
1938 (simplify
1939 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
1940 @2)
1941
1942 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
1943 (simplify
1944 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
1945 @2)
1946
1947 /* x >= y || x != XXX_MAX --> true
1948 x >= y || x == XXX_MAX --> x >= y. */
1949 (for eqne (eq ne)
1950 (simplify
1951 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
1952 (switch
1953 (if (eqne == EQ_EXPR)
1954 @2)
1955 (if (eqne == NE_EXPR)
1956 { constant_boolean_node (true, type); }))))
1957
1958 /* Convert (X == CST1) && (X OP2 CST2) to a known value
1959 based on CST1 OP2 CST2. Similarly for (X != CST1). */
1960
1961 (for code1 (eq ne)
1962 (for code2 (eq ne lt gt le ge)
1963 (simplify
1964 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
1965 (with
1966 {
1967 int cmp = tree_int_cst_compare (@1, @2);
1968 bool val;
1969 switch (code2)
1970 {
1971 case EQ_EXPR: val = (cmp == 0); break;
1972 case NE_EXPR: val = (cmp != 0); break;
1973 case LT_EXPR: val = (cmp < 0); break;
1974 case GT_EXPR: val = (cmp > 0); break;
1975 case LE_EXPR: val = (cmp <= 0); break;
1976 case GE_EXPR: val = (cmp >= 0); break;
1977 default: gcc_unreachable ();
1978 }
1979 }
1980 (switch
1981 (if (code1 == EQ_EXPR && val) @3)
1982 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
1983 (if (code1 == NE_EXPR && !val) @4))))))
1984
1985 /* Convert (X OP1 CST1) && (X OP2 CST2). */
1986
1987 (for code1 (lt le gt ge)
1988 (for code2 (lt le gt ge)
1989 (simplify
1990 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
1991 (with
1992 {
1993 int cmp = tree_int_cst_compare (@1, @2);
1994 }
1995 (switch
1996 /* Choose the more restrictive of two < or <= comparisons. */
1997 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
1998 && (code2 == LT_EXPR || code2 == LE_EXPR))
1999 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2000 @3
2001 @4))
2002 /* Likewise chose the more restrictive of two > or >= comparisons. */
2003 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2004 && (code2 == GT_EXPR || code2 == GE_EXPR))
2005 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2006 @3
2007 @4))
2008 /* Check for singleton ranges. */
2009 (if (cmp == 0
2010 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2011 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2012 (eq @0 @1))
2013 /* Check for disjoint ranges. */
2014 (if (cmp <= 0
2015 && (code1 == LT_EXPR || code1 == LE_EXPR)
2016 && (code2 == GT_EXPR || code2 == GE_EXPR))
2017 { constant_boolean_node (false, type); })
2018 (if (cmp >= 0
2019 && (code1 == GT_EXPR || code1 == GE_EXPR)
2020 && (code2 == LT_EXPR || code2 == LE_EXPR))
2021 { constant_boolean_node (false, type); })
2022 )))))
2023
2024 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2025 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2026
2027 (for code1 (eq ne)
2028 (for code2 (eq ne lt gt le ge)
2029 (simplify
2030 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2031 (with
2032 {
2033 int cmp = tree_int_cst_compare (@1, @2);
2034 bool val;
2035 switch (code2)
2036 {
2037 case EQ_EXPR: val = (cmp == 0); break;
2038 case NE_EXPR: val = (cmp != 0); break;
2039 case LT_EXPR: val = (cmp < 0); break;
2040 case GT_EXPR: val = (cmp > 0); break;
2041 case LE_EXPR: val = (cmp <= 0); break;
2042 case GE_EXPR: val = (cmp >= 0); break;
2043 default: gcc_unreachable ();
2044 }
2045 }
2046 (switch
2047 (if (code1 == EQ_EXPR && val) @4)
2048 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2049 (if (code1 == NE_EXPR && !val) @3))))))
2050
2051 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2052
2053 (for code1 (lt le gt ge)
2054 (for code2 (lt le gt ge)
2055 (simplify
2056 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2057 (with
2058 {
2059 int cmp = tree_int_cst_compare (@1, @2);
2060 }
2061 (switch
2062 /* Choose the more restrictive of two < or <= comparisons. */
2063 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2064 && (code2 == LT_EXPR || code2 == LE_EXPR))
2065 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2066 @4
2067 @3))
2068 /* Likewise chose the more restrictive of two > or >= comparisons. */
2069 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2070 && (code2 == GT_EXPR || code2 == GE_EXPR))
2071 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2072 @4
2073 @3))
2074 /* Check for singleton ranges. */
2075 (if (cmp == 0
2076 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2077 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2078 (ne @0 @2))
2079 /* Check for disjoint ranges. */
2080 (if (cmp >= 0
2081 && (code1 == LT_EXPR || code1 == LE_EXPR)
2082 && (code2 == GT_EXPR || code2 == GE_EXPR))
2083 { constant_boolean_node (true, type); })
2084 (if (cmp <= 0
2085 && (code1 == GT_EXPR || code1 == GE_EXPR)
2086 && (code2 == LT_EXPR || code2 == LE_EXPR))
2087 { constant_boolean_node (true, type); })
2088 )))))
1755 2089
1756 /* We can't reassociate at all for saturating types. */ 2090 /* We can't reassociate at all for saturating types. */
1757 (if (!TYPE_SATURATING (type)) 2091 (if (!TYPE_SATURATING (type))
1758 2092
1759 /* Contract negates. */ 2093 /* Contract negates. */
1803 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)) 2137 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1804 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1))) 2138 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1805 || !HONOR_SIGN_DEPENDENT_ROUNDING (type))) 2139 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1806 (convert (negate @1)))) 2140 (convert (negate @1))))
1807 (simplify 2141 (simplify
1808 (negate (nop_convert (negate @1))) 2142 (negate (nop_convert? (negate @1)))
1809 (if (!TYPE_OVERFLOW_SANITIZED (type) 2143 (if (!TYPE_OVERFLOW_SANITIZED (type)
1810 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))) 2144 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1811 (view_convert @1))) 2145 (view_convert @1)))
1812 2146
1813 /* We can't reassociate floating-point unless -fassociative-math 2147 /* We can't reassociate floating-point unless -fassociative-math
1820 /* (A +- B) - A -> +- B */ 2154 /* (A +- B) - A -> +- B */
1821 /* (A +- B) -+ B -> A */ 2155 /* (A +- B) -+ B -> A */
1822 /* A - (A +- B) -> -+ B */ 2156 /* A - (A +- B) -> -+ B */
1823 /* A +- (B -+ A) -> +- B */ 2157 /* A +- (B -+ A) -> +- B */
1824 (simplify 2158 (simplify
1825 (minus (plus:c @0 @1) @0) 2159 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
1826 @1) 2160 (view_convert @1))
1827 (simplify 2161 (simplify
1828 (minus (minus @0 @1) @0) 2162 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
1829 (negate @1)) 2163 (if (!ANY_INTEGRAL_TYPE_P (type)
1830 (simplify 2164 || TYPE_OVERFLOW_WRAPS (type))
1831 (plus:c (minus @0 @1) @1) 2165 (negate (view_convert @1))
1832 @0) 2166 (view_convert (negate @1))))
1833 (simplify 2167 (simplify
1834 (minus @0 (plus:c @0 @1)) 2168 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
1835 (negate @1)) 2169 (view_convert @0))
1836 (simplify 2170 (simplify
1837 (minus @0 (minus @0 @1)) 2171 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
1838 @1) 2172 (if (!ANY_INTEGRAL_TYPE_P (type)
2173 || TYPE_OVERFLOW_WRAPS (type))
2174 (negate (view_convert @1))
2175 (view_convert (negate @1))))
2176 (simplify
2177 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2178 (view_convert @1))
1839 /* (A +- B) + (C - A) -> C +- B */ 2179 /* (A +- B) + (C - A) -> C +- B */
1840 /* (A + B) - (A - C) -> B + C */ 2180 /* (A + B) - (A - C) -> B + C */
1841 /* More cases are handled with comparisons. */ 2181 /* More cases are handled with comparisons. */
1842 (simplify 2182 (simplify
1843 (plus:c (plus:c @0 @1) (minus @2 @0)) 2183 (plus:c (plus:c @0 @1) (minus @2 @0))
1859 scalars. */ 2199 scalars. */
1860 (for outer_op (plus minus) 2200 (for outer_op (plus minus)
1861 (for inner_op (plus minus) 2201 (for inner_op (plus minus)
1862 neg_inner_op (minus plus) 2202 neg_inner_op (minus plus)
1863 (simplify 2203 (simplify
1864 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1)) 2204 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
1865 CONSTANT_CLASS_P@2) 2205 CONSTANT_CLASS_P@2)
1866 /* If one of the types wraps, use that one. */ 2206 /* If one of the types wraps, use that one. */
1867 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) 2207 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1868 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse 2208 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1869 forever if something doesn't simplify into a constant. */ 2209 forever if something doesn't simplify into a constant. */
1898 { drop_tree_overflow (cst); })))))))))))))) 2238 { drop_tree_overflow (cst); }))))))))))))))
1899 2239
1900 /* (CST1 - A) +- CST2 -> CST3 - A */ 2240 /* (CST1 - A) +- CST2 -> CST3 - A */
1901 (for outer_op (plus minus) 2241 (for outer_op (plus minus)
1902 (simplify 2242 (simplify
1903 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2) 2243 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
1904 (with { tree cst = const_binop (outer_op, type, @1, @2); } 2244 /* If one of the types wraps, use that one. */
1905 (if (cst && !TREE_OVERFLOW (cst)) 2245 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1906 (minus { cst; } @0))))) 2246 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1907 2247 forever if something doesn't simplify into a constant. */
1908 /* CST1 - (CST2 - A) -> CST3 + A */ 2248 (if (!CONSTANT_CLASS_P (@0))
1909 (simplify 2249 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
1910 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0)) 2250 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1911 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); } 2251 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1912 (if (cst && !TREE_OVERFLOW (cst)) 2252 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
1913 (plus { cst; } @0)))) 2253 (if (types_match (type, @0))
2254 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2255 (if (cst && !TREE_OVERFLOW (cst))
2256 (minus { cst; } @0))))))))
2257
2258 /* CST1 - (CST2 - A) -> CST3 + A
2259 Use view_convert because it is safe for vectors and equivalent for
2260 scalars. */
2261 (simplify
2262 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2263 /* If one of the types wraps, use that one. */
2264 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2265 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2266 forever if something doesn't simplify into a constant. */
2267 (if (!CONSTANT_CLASS_P (@0))
2268 (plus (view_convert @0) (minus @1 (view_convert @2))))
2269 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2270 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2271 (view_convert (plus @0 (minus (view_convert @1) @2)))
2272 (if (types_match (type, @0))
2273 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2274 (if (cst && !TREE_OVERFLOW (cst))
2275 (plus { cst; } @0)))))))
2276
2277 /* ((T)(A)) + CST -> (T)(A + CST) */
2278 #if GIMPLE
2279 (simplify
2280 (plus (convert SSA_NAME@0) INTEGER_CST@1)
2281 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2282 && TREE_CODE (type) == INTEGER_TYPE
2283 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2284 && int_fits_type_p (@1, TREE_TYPE (@0)))
2285 /* Perform binary operation inside the cast if the constant fits
2286 and (A + CST)'s range does not overflow. */
2287 (with
2288 {
2289 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2290 max_ovf = wi::OVF_OVERFLOW;
2291 tree inner_type = TREE_TYPE (@0);
2292
2293 wide_int w1
2294 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2295 TYPE_SIGN (inner_type));
2296
2297 wide_int wmin0, wmax0;
2298 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2299 {
2300 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2301 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2302 }
2303 }
2304 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2305 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2306 )))
2307 #endif
2308
2309 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2310 #if GIMPLE
2311 (for op (plus minus)
2312 (simplify
2313 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2314 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2315 && TREE_CODE (type) == INTEGER_TYPE
2316 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2317 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2318 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2319 && TYPE_OVERFLOW_WRAPS (type))
2320 (plus (convert @0) (op @2 (convert @1))))))
2321 #endif
1914 2322
1915 /* ~A + A -> -1 */ 2323 /* ~A + A -> -1 */
1916 (simplify 2324 (simplify
1917 (plus:c (bit_not @0) @0) 2325 (plus:c (bit_not @0) @0)
1918 (if (!TYPE_OVERFLOW_TRAPS (type)) 2326 (if (!TYPE_OVERFLOW_TRAPS (type))
2095 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))) 2503 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2096 (simplify 2504 (simplify
2097 (plusminus @0 (mult:c@3 @0 @2)) 2505 (plusminus @0 (mult:c@3 @0 @2))
2098 (if ((!ANY_INTEGRAL_TYPE_P (type) 2506 (if ((!ANY_INTEGRAL_TYPE_P (type)
2099 || TYPE_OVERFLOW_WRAPS (type) 2507 || TYPE_OVERFLOW_WRAPS (type)
2508 /* For @0 + @0*@2 this transformation would introduce UB
2509 (where there was none before) for @0 in [-1,0] and @2 max.
2510 For @0 - @0*@2 this transformation would introduce UB
2511 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2100 || (INTEGRAL_TYPE_P (type) 2512 || (INTEGRAL_TYPE_P (type)
2101 && tree_expr_nonzero_p (@0) 2513 && ((tree_expr_nonzero_p (@0)
2102 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type))))) 2514 && expr_not_equal_to (@0,
2515 wi::minus_one (TYPE_PRECISION (type))))
2516 || (plusminus == PLUS_EXPR
2517 ? expr_not_equal_to (@2,
2518 wi::max_value (TYPE_PRECISION (type), SIGNED))
2519 /* Let's ignore the @0 -1 and @2 min case. */
2520 : (expr_not_equal_to (@2,
2521 wi::min_value (TYPE_PRECISION (type), SIGNED))
2522 && expr_not_equal_to (@2,
2523 wi::min_value (TYPE_PRECISION (type), SIGNED)
2524 + 1))))))
2103 && single_use (@3)) 2525 && single_use (@3))
2104 (mult (plusminus { build_one_cst (type); } @2) @0))) 2526 (mult (plusminus { build_one_cst (type); } @2) @0)))
2105 (simplify 2527 (simplify
2106 (plusminus (mult:c@3 @0 @2) @0) 2528 (plusminus (mult:c@3 @0 @2) @0)
2107 (if ((!ANY_INTEGRAL_TYPE_P (type) 2529 (if ((!ANY_INTEGRAL_TYPE_P (type)
2108 || TYPE_OVERFLOW_WRAPS (type) 2530 || TYPE_OVERFLOW_WRAPS (type)
2531 /* For @0*@2 + @0 this transformation would introduce UB
2532 (where there was none before) for @0 in [-1,0] and @2 max.
2533 For @0*@2 - @0 this transformation would introduce UB
2534 for @0 0 and @2 min. */
2109 || (INTEGRAL_TYPE_P (type) 2535 || (INTEGRAL_TYPE_P (type)
2110 && tree_expr_nonzero_p (@0) 2536 && ((tree_expr_nonzero_p (@0)
2111 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type))))) 2537 && (plusminus == MINUS_EXPR
2538 || expr_not_equal_to (@0,
2539 wi::minus_one (TYPE_PRECISION (type)))))
2540 || expr_not_equal_to (@2,
2541 (plusminus == PLUS_EXPR
2542 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2543 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2112 && single_use (@3)) 2544 && single_use (@3))
2113 (mult (plusminus @2 { build_one_cst (type); }) @0)))))) 2545 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2114 2546
2115 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */ 2547 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2116 2548
2263 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and) 2695 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2264 (simplify 2696 (simplify
2265 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2) 2697 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2266 (comb (cmp @0 @2) (cmp @1 @2)))) 2698 (comb (cmp @0 @2) (cmp @1 @2))))
2267 2699
2700 /* Undo fancy way of writing max/min or other ?: expressions,
2701 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2702 People normally use ?: and that is what we actually try to optimize. */
2703 (for cmp (simple_comparison)
2704 (simplify
2705 (minus @0 (bit_and:c (minus @0 @1)
2706 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2707 (if (INTEGRAL_TYPE_P (type)
2708 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2709 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2710 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2711 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2712 || !TYPE_UNSIGNED (TREE_TYPE (@4))))
2713 (cond (cmp @2 @3) @1 @0)))
2714 (simplify
2715 (plus:c @0 (bit_and:c (minus @1 @0)
2716 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2717 (if (INTEGRAL_TYPE_P (type)
2718 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2719 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2720 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2721 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2722 || !TYPE_UNSIGNED (TREE_TYPE (@4))))
2723 (cond (cmp @2 @3) @1 @0))))
2724
2268 /* Simplifications of shift and rotates. */ 2725 /* Simplifications of shift and rotates. */
2269 2726
2270 (for rotate (lrotate rrotate) 2727 (for rotate (lrotate rrotate)
2271 (simplify 2728 (simplify
2272 (rotate integer_all_onesp@0 @1) 2729 (rotate integer_all_onesp@0 @1)
2279 && tree_expr_nonnegative_p (@1)) 2736 && tree_expr_nonnegative_p (@1))
2280 @0)) 2737 @0))
2281 2738
2282 /* Optimize (x >> c) << c into x & (-1<<c). */ 2739 /* Optimize (x >> c) << c into x & (-1<<c). */
2283 (simplify 2740 (simplify
2284 (lshift (rshift @0 INTEGER_CST@1) @1) 2741 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
2285 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))) 2742 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2286 (bit_and @0 (lshift { build_minus_one_cst (type); } @1)))) 2743 /* It doesn't matter if the right shift is arithmetic or logical. */
2744 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
2745
2746 (simplify
2747 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
2748 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
2749 /* Allow intermediate conversion to integral type with whatever sign, as
2750 long as the low TYPE_PRECISION (type)
2751 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
2752 && INTEGRAL_TYPE_P (type)
2753 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2754 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2755 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
2756 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
2757 || wi::geu_p (wi::to_wide (@1),
2758 TYPE_PRECISION (type)
2759 - TYPE_PRECISION (TREE_TYPE (@2)))))
2760 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
2287 2761
2288 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned 2762 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2289 types. */ 2763 types. */
2290 (simplify 2764 (simplify
2291 (rshift (lshift @0 INTEGER_CST@1) @1) 2765 (rshift (lshift @0 INTEGER_CST@1) @1)
2534 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)) 3008 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2535 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1)) 3009 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2536 && TYPE_UNSIGNED (TREE_TYPE (@1))))) 3010 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2537 (view_convert @1))) 3011 (view_convert @1)))
2538 3012
3013 /* Simplify a view-converted empty constructor. */
3014 (simplify
3015 (view_convert CONSTRUCTOR@0)
3016 (if (TREE_CODE (@0) != SSA_NAME
3017 && CONSTRUCTOR_NELTS (@0) == 0)
3018 { build_zero_cst (type); }))
3019
2539 /* Re-association barriers around constants and other re-association 3020 /* Re-association barriers around constants and other re-association
2540 barriers can be removed. */ 3021 barriers can be removed. */
2541 (simplify 3022 (simplify
2542 (paren CONSTANT_CLASS_P@0) 3023 (paren CONSTANT_CLASS_P@0)
2543 @0) 3024 @0)
2667 /* (X /[ex] A) * A -> X. */ 3148 /* (X /[ex] A) * A -> X. */
2668 (simplify 3149 (simplify
2669 (mult (convert1? (exact_div @0 @@1)) (convert2? @1)) 3150 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2670 (convert @0)) 3151 (convert @0))
2671 3152
3153 /* Simplify (A / B) * B + (A % B) -> A. */
3154 (for div (trunc_div ceil_div floor_div round_div)
3155 mod (trunc_mod ceil_mod floor_mod round_mod)
3156 (simplify
3157 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3158 @0))
3159
2672 /* ((X /[ex] A) +- B) * A --> X +- A * B. */ 3160 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
2673 (for op (plus minus) 3161 (for op (plus minus)
2674 (simplify 3162 (simplify
2675 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1) 3163 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
2676 (if (tree_nop_conversion_p (type, TREE_TYPE (@2)) 3164 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
2819 (vec_cond VECTOR_CST@0 @1 @2) 3307 (vec_cond VECTOR_CST@0 @1 @2)
2820 (if (integer_all_onesp (@0)) 3308 (if (integer_all_onesp (@0))
2821 @1 3309 @1
2822 (if (integer_zerop (@0)) 3310 (if (integer_zerop (@0))
2823 @2))) 3311 @2)))
3312
3313 /* Sink unary operations to constant branches, but only if we do fold it to
3314 constants. */
3315 (for op (negate bit_not abs absu)
3316 (simplify
3317 (op (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2))
3318 (with
3319 {
3320 tree cst1, cst2;
3321 cst1 = const_unop (op, type, @1);
3322 if (cst1)
3323 cst2 = const_unop (op, type, @2);
3324 }
3325 (if (cst1 && cst2)
3326 (vec_cond @0 { cst1; } { cst2; })))))
2824 3327
2825 /* Simplification moved from fold_cond_expr_with_comparison. It may also 3328 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2826 be extended. */ 3329 be extended. */
2827 /* This pattern implements two kinds simplification: 3330 /* This pattern implements two kinds simplification:
2828 3331
3065 comparison by changing the comparison code. This is a canonicalization 3568 comparison by changing the comparison code. This is a canonicalization
3066 formerly done by maybe_canonicalize_comparison_1. */ 3569 formerly done by maybe_canonicalize_comparison_1. */
3067 (for cmp (le gt) 3570 (for cmp (le gt)
3068 acmp (lt ge) 3571 acmp (lt ge)
3069 (simplify 3572 (simplify
3070 (cmp @0 INTEGER_CST@1) 3573 (cmp @0 uniform_integer_cst_p@1)
3071 (if (tree_int_cst_sgn (@1) == -1) 3574 (with { tree cst = uniform_integer_cst_p (@1); }
3072 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); })))) 3575 (if (tree_int_cst_sgn (cst) == -1)
3576 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3577 wide_int_to_tree (TREE_TYPE (cst),
3578 wi::to_wide (cst)
3579 + 1)); })))))
3073 (for cmp (ge lt) 3580 (for cmp (ge lt)
3074 acmp (gt le) 3581 acmp (gt le)
3075 (simplify 3582 (simplify
3076 (cmp @0 INTEGER_CST@1) 3583 (cmp @0 uniform_integer_cst_p@1)
3077 (if (tree_int_cst_sgn (@1) == 1) 3584 (with { tree cst = uniform_integer_cst_p (@1); }
3078 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); })))) 3585 (if (tree_int_cst_sgn (cst) == 1)
3079 3586 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3587 wide_int_to_tree (TREE_TYPE (cst),
3588 wi::to_wide (cst) - 1)); })))))
3080 3589
3081 /* We can simplify a logical negation of a comparison to the 3590 /* We can simplify a logical negation of a comparison to the
3082 inverted comparison. As we cannot compute an expression 3591 inverted comparison. As we cannot compute an expression
3083 operator using invert_tree_comparison we have to simulate 3592 operator using invert_tree_comparison we have to simulate
3084 that with expression code iteration. */ 3593 that with expression code iteration. */
3306 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); } 3815 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3307 (if (tem && !TREE_OVERFLOW (tem)) 3816 (if (tem && !TREE_OVERFLOW (tem))
3308 (cmp { tem; } @1))))) 3817 (cmp { tem; } @1)))))
3309 3818
3310 /* Fold comparisons against built-in math functions. */ 3819 /* Fold comparisons against built-in math functions. */
3311 (if (flag_unsafe_math_optimizations 3820 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
3312 && ! flag_errno_math)
3313 (for sq (SQRT) 3821 (for sq (SQRT)
3314 (simplify 3822 (simplify
3315 (cmp (sq @0) REAL_CST@1) 3823 (cmp (sq @0) REAL_CST@1)
3316 (switch 3824 (switch
3317 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) 3825 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3342 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0) 3850 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3343 3851
3344 if x is negative or NaN. Due to -funsafe-math-optimizations, 3852 if x is negative or NaN. Due to -funsafe-math-optimizations,
3345 the results for other x follow from natural arithmetic. */ 3853 the results for other x follow from natural arithmetic. */
3346 (cmp @0 @1))) 3854 (cmp @0 @1)))
3347 (if (cmp == GT_EXPR || cmp == GE_EXPR) 3855 (if ((cmp == LT_EXPR
3856 || cmp == LE_EXPR
3857 || cmp == GT_EXPR
3858 || cmp == GE_EXPR)
3859 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3860 /* Give up for -frounding-math. */
3861 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
3348 (with 3862 (with
3349 { 3863 {
3350 REAL_VALUE_TYPE c2; 3864 REAL_VALUE_TYPE c2;
3865 enum tree_code ncmp = cmp;
3866 const real_format *fmt
3867 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
3351 real_arithmetic (&c2, MULT_EXPR, 3868 real_arithmetic (&c2, MULT_EXPR,
3352 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); 3869 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3353 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); 3870 real_convert (&c2, fmt, &c2);
3871 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
3872 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
3873 if (!REAL_VALUE_ISINF (c2))
3874 {
3875 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
3876 build_real (TREE_TYPE (@0), c2));
3877 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
3878 ncmp = ERROR_MARK;
3879 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
3880 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
3881 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
3882 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
3883 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
3884 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
3885 else
3886 {
3887 /* With rounding to even, sqrt of up to 3 different values
3888 gives the same normal result, so in some cases c2 needs
3889 to be adjusted. */
3890 REAL_VALUE_TYPE c2alt, tow;
3891 if (cmp == LT_EXPR || cmp == GE_EXPR)
3892 tow = dconst0;
3893 else
3894 real_inf (&tow);
3895 real_nextafter (&c2alt, fmt, &c2, &tow);
3896 real_convert (&c2alt, fmt, &c2alt);
3897 if (REAL_VALUE_ISINF (c2alt))
3898 ncmp = ERROR_MARK;
3899 else
3900 {
3901 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
3902 build_real (TREE_TYPE (@0), c2alt));
3903 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
3904 ncmp = ERROR_MARK;
3905 else if (real_equal (&TREE_REAL_CST (c3),
3906 &TREE_REAL_CST (@1)))
3907 c2 = c2alt;
3908 }
3909 }
3910 }
3354 } 3911 }
3355 (if (REAL_VALUE_ISINF (c2)) 3912 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3356 /* sqrt(x) > y is x == +Inf, when y is very large. */ 3913 (if (REAL_VALUE_ISINF (c2))
3357 (if (HONOR_INFINITIES (@0)) 3914 /* sqrt(x) > y is x == +Inf, when y is very large. */
3358 (eq @0 { build_real (TREE_TYPE (@0), c2); }) 3915 (if (HONOR_INFINITIES (@0))
3359 { constant_boolean_node (false, type); }) 3916 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3360 /* sqrt(x) > c is the same as x > c*c. */ 3917 { constant_boolean_node (false, type); })
3361 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))) 3918 /* sqrt(x) > c is the same as x > c*c. */
3362 (if (cmp == LT_EXPR || cmp == LE_EXPR) 3919 (if (ncmp != ERROR_MARK)
3363 (with 3920 (if (ncmp == GE_EXPR)
3364 { 3921 (ge @0 { build_real (TREE_TYPE (@0), c2); })
3365 REAL_VALUE_TYPE c2; 3922 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
3366 real_arithmetic (&c2, MULT_EXPR, 3923 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
3367 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); 3924 (if (REAL_VALUE_ISINF (c2))
3368 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); 3925 (switch
3369 } 3926 /* sqrt(x) < y is always true, when y is a very large
3370 (if (REAL_VALUE_ISINF (c2)) 3927 value and we don't care about NaNs or Infinities. */
3371 (switch 3928 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3372 /* sqrt(x) < y is always true, when y is a very large 3929 { constant_boolean_node (true, type); })
3373 value and we don't care about NaNs or Infinities. */ 3930 /* sqrt(x) < y is x != +Inf when y is very large and we
3374 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0)) 3931 don't care about NaNs. */
3375 { constant_boolean_node (true, type); }) 3932 (if (! HONOR_NANS (@0))
3376 /* sqrt(x) < y is x != +Inf when y is very large and we 3933 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3377 don't care about NaNs. */ 3934 /* sqrt(x) < y is x >= 0 when y is very large and we
3378 (if (! HONOR_NANS (@0)) 3935 don't care about Infinities. */
3379 (ne @0 { build_real (TREE_TYPE (@0), c2); })) 3936 (if (! HONOR_INFINITIES (@0))
3380 /* sqrt(x) < y is x >= 0 when y is very large and we 3937 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3381 don't care about Infinities. */ 3938 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3382 (if (! HONOR_INFINITIES (@0)) 3939 (if (GENERIC)
3383 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })) 3940 (truth_andif
3384 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ 3941 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3385 (if (GENERIC) 3942 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3386 (truth_andif 3943 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3387 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) 3944 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
3388 (ne @0 { build_real (TREE_TYPE (@0), c2); })))) 3945 (if (ncmp == LT_EXPR)
3389 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ 3946 (lt @0 { build_real (TREE_TYPE (@0), c2); })
3390 (if (! HONOR_NANS (@0)) 3947 (le @0 { build_real (TREE_TYPE (@0), c2); }))
3391 (cmp @0 { build_real (TREE_TYPE (@0), c2); }) 3948 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3392 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ 3949 (if (ncmp != ERROR_MARK && GENERIC)
3393 (if (GENERIC) 3950 (if (ncmp == LT_EXPR)
3394 (truth_andif 3951 (truth_andif
3395 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) 3952 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3396 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))) 3953 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
3954 (truth_andif
3955 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3956 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
3397 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */ 3957 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3398 (simplify 3958 (simplify
3399 (cmp (sq @0) (sq @1)) 3959 (cmp (sq @0) (sq @1))
3400 (if (! HONOR_NANS (@0)) 3960 (if (! HONOR_NANS (@0))
3401 (cmp @0 @1)))))) 3961 (cmp @0 @1))))))
3528 (if (ovf) 4088 (if (ovf)
3529 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0, 4089 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3530 TYPE_SIGN (TREE_TYPE (@2))) 4090 TYPE_SIGN (TREE_TYPE (@2)))
3531 != (cmp == LT_EXPR || cmp == LE_EXPR), type); } 4091 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3532 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); })))))) 4092 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4093
4094 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4095
4096 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4097 For large C (more than min/B+2^size), this is also true, with the
4098 multiplication computed modulo 2^size.
4099 For intermediate C, this just tests the sign of A. */
4100 (for cmp (lt le gt ge)
4101 cmp2 (ge ge lt lt)
4102 (simplify
4103 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4104 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4105 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4106 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4107 (with
4108 {
4109 tree utype = TREE_TYPE (@2);
4110 wide_int denom = wi::to_wide (@1);
4111 wide_int right = wi::to_wide (@2);
4112 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4113 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4114 bool small = wi::leu_p (right, smax);
4115 bool large = wi::geu_p (right, smin);
4116 }
4117 (if (small || large)
4118 (cmp (convert:utype @0) (mult @2 (convert @1)))
4119 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
3533 4120
3534 /* Unordered tests if either argument is a NaN. */ 4121 /* Unordered tests if either argument is a NaN. */
3535 (simplify 4122 (simplify
3536 (bit_ior (unordered @0 @0) (unordered @1 @1)) 4123 (bit_ior (unordered @0 @0) (unordered @1 @1))
3537 (if (types_match (@0, @1)) 4124 (if (types_match (@0, @1))
3678 { constant_boolean_node (above ? true : false, type); } 4265 { constant_boolean_node (above ? true : false, type); }
3679 (if (cmp == GT_EXPR || cmp == GE_EXPR) 4266 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3680 { constant_boolean_node (above ? false : true, type); })))))))))))) 4267 { constant_boolean_node (above ? false : true, type); }))))))))))))
3681 4268
3682 (for cmp (eq ne) 4269 (for cmp (eq ne)
3683 /* A local variable can never be pointed to by 4270 (simplify
3684 the default SSA name of an incoming parameter. 4271 /* SSA names are canonicalized to 2nd place. */
3685 SSA names are canonicalized to 2nd place. */
3686 (simplify
3687 (cmp addr@0 SSA_NAME@1) 4272 (cmp addr@0 SSA_NAME@1)
3688 (if (SSA_NAME_IS_DEFAULT_DEF (@1) 4273 (with
3689 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL) 4274 { poly_int64 off; tree base; }
3690 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); } 4275 /* A local variable can never be pointed to by
3691 (if (TREE_CODE (base) == VAR_DECL 4276 the default SSA name of an incoming parameter. */
3692 && auto_var_in_fn_p (base, current_function_decl)) 4277 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3693 (if (cmp == NE_EXPR) 4278 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
3694 { constant_boolean_node (true, type); } 4279 && (base = get_base_address (TREE_OPERAND (@0, 0)))
3695 { constant_boolean_node (false, type); })))))) 4280 && TREE_CODE (base) == VAR_DECL
4281 && auto_var_in_fn_p (base, current_function_decl))
4282 (if (cmp == NE_EXPR)
4283 { constant_boolean_node (true, type); }
4284 { constant_boolean_node (false, type); })
4285 /* If the address is based on @1 decide using the offset. */
4286 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4287 && TREE_CODE (base) == MEM_REF
4288 && TREE_OPERAND (base, 0) == @1)
4289 (with { off += mem_ref_offset (base).force_shwi (); }
4290 (if (known_ne (off, 0))
4291 { constant_boolean_node (cmp == NE_EXPR, type); }
4292 (if (known_eq (off, 0))
4293 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
3696 4294
3697 /* Equality compare simplifications from fold_binary */ 4295 /* Equality compare simplifications from fold_binary */
3698 (for cmp (eq ne) 4296 (for cmp (eq ne)
3699 4297
3700 /* If we have (A | C) == D where C & ~D != 0, convert this into 0. 4298 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3828 || TREE_CODE (base0) == STRING_CST) 4426 || TREE_CODE (base0) == STRING_CST)
3829 && (DECL_P (base1) 4427 && (DECL_P (base1)
3830 || TREE_CODE (base1) == SSA_NAME 4428 || TREE_CODE (base1) == SSA_NAME
3831 || TREE_CODE (base1) == STRING_CST)) 4429 || TREE_CODE (base1) == STRING_CST))
3832 equal = (base0 == base1); 4430 equal = (base0 == base1);
4431 if (equal == 0)
4432 {
4433 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4434 off0.is_constant (&ioff0);
4435 off1.is_constant (&ioff1);
4436 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4437 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4438 || (TREE_CODE (base0) == STRING_CST
4439 && TREE_CODE (base1) == STRING_CST
4440 && ioff0 >= 0 && ioff1 >= 0
4441 && ioff0 < TREE_STRING_LENGTH (base0)
4442 && ioff1 < TREE_STRING_LENGTH (base1)
4443 /* This is a too conservative test that the STRING_CSTs
4444 will not end up being string-merged. */
4445 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4446 TREE_STRING_POINTER (base1) + ioff1,
4447 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4448 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4449 ;
4450 else if (!DECL_P (base0) || !DECL_P (base1))
4451 equal = 2;
4452 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4453 equal = 2;
4454 /* If this is a pointer comparison, ignore for now even
4455 valid equalities where one pointer is the offset zero
4456 of one object and the other to one past end of another one. */
4457 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4458 ;
4459 /* Assume that automatic variables can't be adjacent to global
4460 variables. */
4461 else if (is_global_var (base0) != is_global_var (base1))
4462 ;
4463 else
4464 {
4465 tree sz0 = DECL_SIZE_UNIT (base0);
4466 tree sz1 = DECL_SIZE_UNIT (base1);
4467 /* If sizes are unknown, e.g. VLA or not representable,
4468 punt. */
4469 if (!tree_fits_poly_int64_p (sz0)
4470 || !tree_fits_poly_int64_p (sz1))
4471 equal = 2;
4472 else
4473 {
4474 poly_int64 size0 = tree_to_poly_int64 (sz0);
4475 poly_int64 size1 = tree_to_poly_int64 (sz1);
4476 /* If one offset is pointing (or could be) to the beginning
4477 of one object and the other is pointing to one past the
4478 last byte of the other object, punt. */
4479 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4480 equal = 2;
4481 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4482 equal = 2;
4483 /* If both offsets are the same, there are some cases
4484 we know that are ok. Either if we know they aren't
4485 zero, or if we know both sizes are no zero. */
4486 if (equal == 2
4487 && known_eq (off0, off1)
4488 && (known_ne (off0, 0)
4489 || (known_ne (size0, 0) && known_ne (size1, 0))))
4490 equal = 0;
4491 }
4492 }
4493 }
3833 } 4494 }
3834 (if (equal == 1 4495 (if (equal == 1
3835 && (cmp == EQ_EXPR || cmp == NE_EXPR 4496 && (cmp == EQ_EXPR || cmp == NE_EXPR
3836 /* If the offsets are equal we can ignore overflow. */ 4497 /* If the offsets are equal we can ignore overflow. */
3837 || known_eq (off0, off1) 4498 || known_eq (off0, off1)
3850 { constant_boolean_node (known_le (off0, off1), type); }) 4511 { constant_boolean_node (known_le (off0, off1), type); })
3851 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1))) 4512 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3852 { constant_boolean_node (known_ge (off0, off1), type); }) 4513 { constant_boolean_node (known_ge (off0, off1), type); })
3853 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1))) 4514 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3854 { constant_boolean_node (known_gt (off0, off1), type); })) 4515 { constant_boolean_node (known_gt (off0, off1), type); }))
3855 (if (equal == 0 4516 (if (equal == 0)
3856 && DECL_P (base0) && DECL_P (base1) 4517 (switch
3857 /* If we compare this as integers require equal offset. */ 4518 (if (cmp == EQ_EXPR)
3858 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2)) 4519 { constant_boolean_node (false, type); })
3859 || known_eq (off0, off1))) 4520 (if (cmp == NE_EXPR)
3860 (switch 4521 { constant_boolean_node (true, type); })))))))))
3861 (if (cmp == EQ_EXPR)
3862 { constant_boolean_node (false, type); })
3863 (if (cmp == NE_EXPR)
3864 { constant_boolean_node (true, type); })))))))))
3865 4522
3866 /* Simplify pointer equality compares using PTA. */ 4523 /* Simplify pointer equality compares using PTA. */
3867 (for neeq (ne eq) 4524 (for neeq (ne eq)
3868 (simplify 4525 (simplify
3869 (neeq @0 @1) 4526 (neeq @0 @1)
3892 /* Non-equality compare simplifications from fold_binary */ 4549 /* Non-equality compare simplifications from fold_binary */
3893 (for cmp (lt gt le ge) 4550 (for cmp (lt gt le ge)
3894 /* Comparisons with the highest or lowest possible integer of 4551 /* Comparisons with the highest or lowest possible integer of
3895 the specified precision will have known values. */ 4552 the specified precision will have known values. */
3896 (simplify 4553 (simplify
3897 (cmp (convert?@2 @0) INTEGER_CST@1) 4554 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
3898 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) 4555 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4556 || POINTER_TYPE_P (TREE_TYPE (@1))
4557 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
3899 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))) 4558 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3900 (with 4559 (with
3901 { 4560 {
3902 tree arg1_type = TREE_TYPE (@1); 4561 tree cst = uniform_integer_cst_p (@1);
4562 tree arg1_type = TREE_TYPE (cst);
3903 unsigned int prec = TYPE_PRECISION (arg1_type); 4563 unsigned int prec = TYPE_PRECISION (arg1_type);
3904 wide_int max = wi::max_value (arg1_type); 4564 wide_int max = wi::max_value (arg1_type);
3905 wide_int signed_max = wi::max_value (prec, SIGNED); 4565 wide_int signed_max = wi::max_value (prec, SIGNED);
3906 wide_int min = wi::min_value (arg1_type); 4566 wide_int min = wi::min_value (arg1_type);
3907 } 4567 }
3908 (switch 4568 (switch
3909 (if (wi::to_wide (@1) == max) 4569 (if (wi::to_wide (cst) == max)
3910 (switch 4570 (switch
3911 (if (cmp == GT_EXPR) 4571 (if (cmp == GT_EXPR)
3912 { constant_boolean_node (false, type); }) 4572 { constant_boolean_node (false, type); })
3913 (if (cmp == GE_EXPR) 4573 (if (cmp == GE_EXPR)
3914 (eq @2 @1)) 4574 (eq @2 @1))
3915 (if (cmp == LE_EXPR) 4575 (if (cmp == LE_EXPR)
3916 { constant_boolean_node (true, type); }) 4576 { constant_boolean_node (true, type); })
3917 (if (cmp == LT_EXPR) 4577 (if (cmp == LT_EXPR)
3918 (ne @2 @1)))) 4578 (ne @2 @1))))
3919 (if (wi::to_wide (@1) == min) 4579 (if (wi::to_wide (cst) == min)
3920 (switch 4580 (switch
3921 (if (cmp == LT_EXPR) 4581 (if (cmp == LT_EXPR)
3922 { constant_boolean_node (false, type); }) 4582 { constant_boolean_node (false, type); })
3923 (if (cmp == LE_EXPR) 4583 (if (cmp == LE_EXPR)
3924 (eq @2 @1)) 4584 (eq @2 @1))
3925 (if (cmp == GE_EXPR) 4585 (if (cmp == GE_EXPR)
3926 { constant_boolean_node (true, type); }) 4586 { constant_boolean_node (true, type); })
3927 (if (cmp == GT_EXPR) 4587 (if (cmp == GT_EXPR)
3928 (ne @2 @1)))) 4588 (ne @2 @1))))
3929 (if (wi::to_wide (@1) == max - 1) 4589 (if (wi::to_wide (cst) == max - 1)
3930 (switch 4590 (switch
3931 (if (cmp == GT_EXPR) 4591 (if (cmp == GT_EXPR)
3932 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); })) 4592 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4593 wide_int_to_tree (TREE_TYPE (cst),
4594 wi::to_wide (cst)
4595 + 1)); }))
3933 (if (cmp == LE_EXPR) 4596 (if (cmp == LE_EXPR)
3934 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); })))) 4597 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
3935 (if (wi::to_wide (@1) == min + 1) 4598 wide_int_to_tree (TREE_TYPE (cst),
4599 wi::to_wide (cst)
4600 + 1)); }))))
4601 (if (wi::to_wide (cst) == min + 1)
3936 (switch 4602 (switch
3937 (if (cmp == GE_EXPR) 4603 (if (cmp == GE_EXPR)
3938 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); })) 4604 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4605 wide_int_to_tree (TREE_TYPE (cst),
4606 wi::to_wide (cst)
4607 - 1)); }))
3939 (if (cmp == LT_EXPR) 4608 (if (cmp == LT_EXPR)
3940 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); })))) 4609 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
3941 (if (wi::to_wide (@1) == signed_max 4610 wide_int_to_tree (TREE_TYPE (cst),
4611 wi::to_wide (cst)
4612 - 1)); }))))
4613 (if (wi::to_wide (cst) == signed_max
3942 && TYPE_UNSIGNED (arg1_type) 4614 && TYPE_UNSIGNED (arg1_type)
3943 /* We will flip the signedness of the comparison operator 4615 /* We will flip the signedness of the comparison operator
3944 associated with the mode of @1, so the sign bit is 4616 associated with the mode of @1, so the sign bit is
3945 specified by this mode. Check that @1 is the signed 4617 specified by this mode. Check that @1 is the signed
3946 max associated with this sign bit. */ 4618 max associated with this sign bit. */
3948 /* signed_type does not work on pointer types. */ 4620 /* signed_type does not work on pointer types. */
3949 && INTEGRAL_TYPE_P (arg1_type)) 4621 && INTEGRAL_TYPE_P (arg1_type))
3950 /* The following case also applies to X < signed_max+1 4622 /* The following case also applies to X < signed_max+1
3951 and X >= signed_max+1 because previous transformations. */ 4623 and X >= signed_max+1 because previous transformations. */
3952 (if (cmp == LE_EXPR || cmp == GT_EXPR) 4624 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3953 (with { tree st = signed_type_for (arg1_type); } 4625 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
3954 (if (cmp == LE_EXPR) 4626 (switch
3955 (ge (convert:st @0) { build_zero_cst (st); }) 4627 (if (cst == @1 && cmp == LE_EXPR)
3956 (lt (convert:st @0) { build_zero_cst (st); })))))))))) 4628 (ge (convert:st @0) { build_zero_cst (st); }))
4629 (if (cst == @1 && cmp == GT_EXPR)
4630 (lt (convert:st @0) { build_zero_cst (st); }))
4631 (if (cmp == LE_EXPR)
4632 (ge (view_convert:st @0) { build_zero_cst (st); }))
4633 (if (cmp == GT_EXPR)
4634 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
3957 4635
3958 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt) 4636 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3959 /* If the second operand is NaN, the result is constant. */ 4637 /* If the second operand is NaN, the result is constant. */
3960 (simplify 4638 (simplify
3961 (cmp @0 REAL_CST@1) 4639 (cmp @0 REAL_CST@1)
4070 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */ 4748 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4071 (simplify 4749 (simplify
4072 (op (rdiv @0 @1) 4750 (op (rdiv @0 @1)
4073 (rdiv @2 @1)) 4751 (rdiv @2 @1))
4074 (rdiv (op @0 @2) @1))) 4752 (rdiv (op @0 @2) @1)))
4753
4754 (for cmp (lt le gt ge)
4755 neg_cmp (gt ge lt le)
4756 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
4757 (simplify
4758 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4759 (with
4760 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4761 (if (tem
4762 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4763 || (real_zerop (tem) && !real_zerop (@1))))
4764 (switch
4765 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4766 (cmp @0 { tem; }))
4767 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4768 (neg_cmp @0 { tem; })))))))
4075 4769
4076 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */ 4770 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4077 (for root (SQRT CBRT) 4771 (for root (SQRT CBRT)
4078 (simplify 4772 (simplify
4079 (mult (root:s @0) (root:s @1)) 4773 (mult (root:s @0) (root:s @1))
4260 build_sinatan_real (&r_cst, type); 4954 build_sinatan_real (&r_cst, type);
4261 tree t_cst = build_real (type, r_cst); 4955 tree t_cst = build_real (type, r_cst);
4262 tree t_one = build_one_cst (type); 4956 tree t_one = build_one_cst (type);
4263 } 4957 }
4264 (if (SCALAR_FLOAT_TYPE_P (type)) 4958 (if (SCALAR_FLOAT_TYPE_P (type))
4265 (cond (le (abs @0) { t_cst; }) 4959 (cond (lt (abs @0) { t_cst; })
4266 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; }))) 4960 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4267 (copysigns { t_one; } @0)))))) 4961 (copysigns { t_one; } @0))))))
4268 4962
4269 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */ 4963 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4270 (for coss (COS) 4964 (for coss (COS)
4280 tree t_cst = build_real (type, r_cst); 4974 tree t_cst = build_real (type, r_cst);
4281 tree t_one = build_one_cst (type); 4975 tree t_one = build_one_cst (type);
4282 tree t_zero = build_zero_cst (type); 4976 tree t_zero = build_zero_cst (type);
4283 } 4977 }
4284 (if (SCALAR_FLOAT_TYPE_P (type)) 4978 (if (SCALAR_FLOAT_TYPE_P (type))
4285 (cond (le (abs @0) { t_cst; }) 4979 (cond (lt (abs @0) { t_cst; })
4286 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; }))) 4980 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4287 (copysigns { t_zero; } @0)))))) 4981 (copysigns { t_zero; } @0))))))
4982
4983 (if (!flag_errno_math)
4984 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4985 (for sinhs (SINH)
4986 atanhs (ATANH)
4987 sqrts (SQRT)
4988 (simplify
4989 (sinhs (atanhs:s @0))
4990 (with { tree t_one = build_one_cst (type); }
4991 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4992
4993 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4994 (for coshs (COSH)
4995 atanhs (ATANH)
4996 sqrts (SQRT)
4997 (simplify
4998 (coshs (atanhs:s @0))
4999 (with { tree t_one = build_one_cst (type); }
5000 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
4288 5001
4289 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */ 5002 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4290 (simplify 5003 (simplify
4291 (CABS (complex:C @0 real_zerop@1)) 5004 (CABS (complex:C @0 real_zerop@1))
4292 (abs @0)) 5005 (abs @0))
4359 5072
4360 /* Simplify sin(x) / cos(x) -> tan(x). */ 5073 /* Simplify sin(x) / cos(x) -> tan(x). */
4361 (simplify 5074 (simplify
4362 (rdiv (SIN:s @0) (COS:s @0)) 5075 (rdiv (SIN:s @0) (COS:s @0))
4363 (TAN @0)) 5076 (TAN @0))
5077
5078 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5079 (simplify
5080 (rdiv (SINH:s @0) (COSH:s @0))
5081 (TANH @0))
4364 5082
4365 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */ 5083 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4366 (simplify 5084 (simplify
4367 (rdiv (COS:s @0) (SIN:s @0)) 5085 (rdiv (COS:s @0) (SIN:s @0))
4368 (rdiv { build_one_cst (type); } (TAN @0))) 5086 (rdiv { build_one_cst (type); } (TAN @0)))
4651 5369
4652 These are conceptually similar to the transformations performed for 5370 These are conceptually similar to the transformations performed for
4653 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long 5371 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4654 term we want to move all that code out of the front-ends into here. */ 5372 term we want to move all that code out of the front-ends into here. */
4655 5373
4656 /* If we have a narrowing conversion of an arithmetic operation where 5374 /* Convert (outertype)((innertype0)a+(innertype1)b)
4657 both operands are widening conversions from the same type as the outer 5375 into ((newtype)a+(newtype)b) where newtype
4658 narrowing conversion. Then convert the innermost operands to a suitable 5376 is the widest mode from all of these. */
4659 unsigned type (to avoid introducing undefined behavior), perform the 5377 (for op (plus minus mult rdiv)
4660 operation and convert the result to the desired type. */ 5378 (simplify
4661 (for op (plus minus) 5379 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
4662 (simplify 5380 /* If we have a narrowing conversion of an arithmetic operation where
4663 (convert (op:s (convert@2 @0) (convert?@3 @1))) 5381 both operands are widening conversions from the same type as the outer
4664 (if (INTEGRAL_TYPE_P (type) 5382 narrowing conversion. Then convert the innermost operands to a
4665 /* We check for type compatibility between @0 and @1 below, 5383 suitable unsigned type (to avoid introducing undefined behavior),
4666 so there's no need to check that @1/@3 are integral types. */ 5384 perform the operation and convert the result to the desired type. */
4667 && INTEGRAL_TYPE_P (TREE_TYPE (@0)) 5385 (if (INTEGRAL_TYPE_P (type)
4668 && INTEGRAL_TYPE_P (TREE_TYPE (@2)) 5386 && op != MULT_EXPR
4669 /* The precision of the type of each operand must match the 5387 && op != RDIV_EXPR
4670 precision of the mode of each operand, similarly for the 5388 /* We check for type compatibility between @0 and @1 below,
4671 result. */ 5389 so there's no need to check that @2/@4 are integral types. */
4672 && type_has_mode_precision_p (TREE_TYPE (@0)) 5390 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
4673 && type_has_mode_precision_p (TREE_TYPE (@1)) 5391 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
4674 && type_has_mode_precision_p (type) 5392 /* The precision of the type of each operand must match the
4675 /* The inner conversion must be a widening conversion. */ 5393 precision of the mode of each operand, similarly for the
4676 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) 5394 result. */
4677 && types_match (@0, type) 5395 && type_has_mode_precision_p (TREE_TYPE (@1))
4678 && (types_match (@0, @1) 5396 && type_has_mode_precision_p (TREE_TYPE (@2))
4679 /* Or the second operand is const integer or converted const 5397 && type_has_mode_precision_p (type)
4680 integer from valueize. */ 5398 /* The inner conversion must be a widening conversion. */
4681 || TREE_CODE (@1) == INTEGER_CST)) 5399 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
4682 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) 5400 && types_match (@1, type)
4683 (op @0 (convert @1)) 5401 && (types_match (@1, @2)
4684 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } 5402 /* Or the second operand is const integer or converted const
4685 (convert (op (convert:utype @0) 5403 integer from valueize. */
4686 (convert:utype @1)))))))) 5404 || TREE_CODE (@2) == INTEGER_CST))
5405 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5406 (op @1 (convert @2))
5407 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5408 (convert (op (convert:utype @1)
5409 (convert:utype @2)))))
5410 (if (FLOAT_TYPE_P (type)
5411 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5412 == DECIMAL_FLOAT_TYPE_P (type))
5413 (with { tree arg0 = strip_float_extensions (@1);
5414 tree arg1 = strip_float_extensions (@2);
5415 tree itype = TREE_TYPE (@0);
5416 tree ty1 = TREE_TYPE (arg0);
5417 tree ty2 = TREE_TYPE (arg1);
5418 enum tree_code code = TREE_CODE (itype); }
5419 (if (FLOAT_TYPE_P (ty1)
5420 && FLOAT_TYPE_P (ty2))
5421 (with { tree newtype = type;
5422 if (TYPE_MODE (ty1) == SDmode
5423 || TYPE_MODE (ty2) == SDmode
5424 || TYPE_MODE (type) == SDmode)
5425 newtype = dfloat32_type_node;
5426 if (TYPE_MODE (ty1) == DDmode
5427 || TYPE_MODE (ty2) == DDmode
5428 || TYPE_MODE (type) == DDmode)
5429 newtype = dfloat64_type_node;
5430 if (TYPE_MODE (ty1) == TDmode
5431 || TYPE_MODE (ty2) == TDmode
5432 || TYPE_MODE (type) == TDmode)
5433 newtype = dfloat128_type_node; }
5434 (if ((newtype == dfloat32_type_node
5435 || newtype == dfloat64_type_node
5436 || newtype == dfloat128_type_node)
5437 && newtype == type
5438 && types_match (newtype, type))
5439 (op (convert:newtype @1) (convert:newtype @2))
5440 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5441 newtype = ty1;
5442 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5443 newtype = ty2; }
5444 /* Sometimes this transformation is safe (cannot
5445 change results through affecting double rounding
5446 cases) and sometimes it is not. If NEWTYPE is
5447 wider than TYPE, e.g. (float)((long double)double
5448 + (long double)double) converted to
5449 (float)(double + double), the transformation is
5450 unsafe regardless of the details of the types
5451 involved; double rounding can arise if the result
5452 of NEWTYPE arithmetic is a NEWTYPE value half way
5453 between two representable TYPE values but the
5454 exact value is sufficiently different (in the
5455 right direction) for this difference to be
5456 visible in ITYPE arithmetic. If NEWTYPE is the
5457 same as TYPE, however, the transformation may be
5458 safe depending on the types involved: it is safe
5459 if the ITYPE has strictly more than twice as many
5460 mantissa bits as TYPE, can represent infinities
5461 and NaNs if the TYPE can, and has sufficient
5462 exponent range for the product or ratio of two
5463 values representable in the TYPE to be within the
5464 range of normal values of ITYPE. */
5465 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5466 && (flag_unsafe_math_optimizations
5467 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5468 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5469 TYPE_MODE (type))
5470 && !excess_precision_type (newtype)))
5471 && !types_match (itype, newtype))
5472 (convert:type (op (convert:newtype @1)
5473 (convert:newtype @2)))
5474 )))) )
5475 ))
5476 )))
4687 5477
4688 /* This is another case of narrowing, specifically when there's an outer 5478 /* This is another case of narrowing, specifically when there's an outer
4689 BIT_AND_EXPR which masks off bits outside the type of the innermost 5479 BIT_AND_EXPR which masks off bits outside the type of the innermost
4690 operands. Like the previous case we have to convert the operands 5480 operands. Like the previous case we have to convert the operands
4691 to unsigned types to avoid introducing undefined behavior for the 5481 to unsigned types to avoid introducing undefined behavior for the
4875 { build_constructor (type, NULL); } 5665 { build_constructor (type, NULL); }
4876 (if (count == 1) 5666 (if (count == 1)
4877 (if (elt < CONSTRUCTOR_NELTS (ctor)) 5667 (if (elt < CONSTRUCTOR_NELTS (ctor))
4878 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; }) 5668 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
4879 { build_zero_cst (type); }) 5669 { build_zero_cst (type); })
4880 { 5670 /* We don't want to emit new CTORs unless the old one goes away.
4881 vec<constructor_elt, va_gc> *vals; 5671 ??? Eventually allow this if the CTOR ends up constant or
4882 vec_alloc (vals, count); 5672 uniform. */
4883 for (unsigned i = 0; 5673 (if (single_use (@0))
4884 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i) 5674 {
4885 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, 5675 vec<constructor_elt, va_gc> *vals;
4886 CONSTRUCTOR_ELT (ctor, elt + i)->value); 5676 vec_alloc (vals, count);
4887 build_constructor (type, vals); 5677 for (unsigned i = 0;
4888 }))) 5678 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5679 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5680 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5681 build_constructor (type, vals);
5682 }))))
4889 /* The bitfield references a single constructor element. */ 5683 /* The bitfield references a single constructor element. */
4890 (if (k.is_constant (&const_k) 5684 (if (k.is_constant (&const_k)
4891 && idx + n <= (idx / const_k + 1) * const_k) 5685 && idx + n <= (idx / const_k + 1) * const_k)
4892 (switch 5686 (switch
4893 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k) 5687 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
4999 rep (eq eq ne ne) 5793 rep (eq eq ne ne)
5000 (simplify 5794 (simplify
5001 (cmp (popcount @0) integer_zerop) 5795 (cmp (popcount @0) integer_zerop)
5002 (rep @0 { build_zero_cst (TREE_TYPE (@0)); })))) 5796 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5003 5797
5798 #if GIMPLE
5799 /* 64- and 32-bits branchless implementations of popcount are detected:
5800
5801 int popcount64c (uint64_t x)
5802 {
5803 x -= (x >> 1) & 0x5555555555555555ULL;
5804 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
5805 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
5806 return (x * 0x0101010101010101ULL) >> 56;
5807 }
5808
5809 int popcount32c (uint32_t x)
5810 {
5811 x -= (x >> 1) & 0x55555555;
5812 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
5813 x = (x + (x >> 4)) & 0x0f0f0f0f;
5814 return (x * 0x01010101) >> 24;
5815 } */
5816 (simplify
5817 (rshift
5818 (mult
5819 (bit_and
5820 (plus:c
5821 (rshift @8 INTEGER_CST@5)
5822 (plus:c@8
5823 (bit_and @6 INTEGER_CST@7)
5824 (bit_and
5825 (rshift
5826 (minus@6 @0
5827 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
5828 INTEGER_CST@10)
5829 INTEGER_CST@9)))
5830 INTEGER_CST@3)
5831 INTEGER_CST@2)
5832 INTEGER_CST@1)
5833 /* Check constants and optab. */
5834 (with { unsigned prec = TYPE_PRECISION (type);
5835 int shift = (64 - prec) & 63;
5836 unsigned HOST_WIDE_INT c1
5837 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
5838 unsigned HOST_WIDE_INT c2
5839 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
5840 unsigned HOST_WIDE_INT c3
5841 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
5842 unsigned HOST_WIDE_INT c4
5843 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
5844 }
5845 (if (prec >= 16
5846 && prec <= 64
5847 && pow2p_hwi (prec)
5848 && TYPE_UNSIGNED (type)
5849 && integer_onep (@4)
5850 && wi::to_widest (@10) == 2
5851 && wi::to_widest (@5) == 4
5852 && wi::to_widest (@1) == prec - 8
5853 && tree_to_uhwi (@2) == c1
5854 && tree_to_uhwi (@3) == c2
5855 && tree_to_uhwi (@9) == c3
5856 && tree_to_uhwi (@7) == c3
5857 && tree_to_uhwi (@11) == c4
5858 && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
5859 OPTIMIZE_FOR_BOTH))
5860 (convert (IFN_POPCOUNT:type @0)))))
5861 #endif
5862
5004 /* Simplify: 5863 /* Simplify:
5005 5864
5006 a = a1 op a2 5865 a = a1 op a2
5007 r = c ? a : b; 5866 r = c ? a : b;
5008 5867
5010 5869
5011 r = c ? a1 op a2 : b; 5870 r = c ? a1 op a2 : b;
5012 5871
5013 if the target can do it in one go. This makes the operation conditional 5872 if the target can do it in one go. This makes the operation conditional
5014 on c, so could drop potentially-trapping arithmetic, but that's a valid 5873 on c, so could drop potentially-trapping arithmetic, but that's a valid
5015 simplification if the result of the operation isn't needed. */ 5874 simplification if the result of the operation isn't needed.
5875
5876 Avoid speculatively generating a stand-alone vector comparison
5877 on targets that might not support them. Any target implementing
5878 conditional internal functions must support the same comparisons
5879 inside and outside a VEC_COND_EXPR. */
5880
5881 #if GIMPLE
5016 (for uncond_op (UNCOND_BINARY) 5882 (for uncond_op (UNCOND_BINARY)
5017 cond_op (COND_BINARY) 5883 cond_op (COND_BINARY)
5018 (simplify 5884 (simplify
5019 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3) 5885 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5020 (with { tree op_type = TREE_TYPE (@4); } 5886 (with { tree op_type = TREE_TYPE (@4); }
5021 (if (element_precision (type) == element_precision (op_type)) 5887 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5888 && element_precision (type) == element_precision (op_type))
5022 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3)))))) 5889 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5023 (simplify 5890 (simplify
5024 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3))) 5891 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5025 (with { tree op_type = TREE_TYPE (@4); } 5892 (with { tree op_type = TREE_TYPE (@4); }
5026 (if (element_precision (type) == element_precision (op_type)) 5893 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5894 && element_precision (type) == element_precision (op_type))
5027 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1))))))) 5895 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
5028 5896
5029 /* Same for ternary operations. */ 5897 /* Same for ternary operations. */
5030 (for uncond_op (UNCOND_TERNARY) 5898 (for uncond_op (UNCOND_TERNARY)
5031 cond_op (COND_TERNARY) 5899 cond_op (COND_TERNARY)
5032 (simplify 5900 (simplify
5033 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4) 5901 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5034 (with { tree op_type = TREE_TYPE (@5); } 5902 (with { tree op_type = TREE_TYPE (@5); }
5035 (if (element_precision (type) == element_precision (op_type)) 5903 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5904 && element_precision (type) == element_precision (op_type))
5036 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4)))))) 5905 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5037 (simplify 5906 (simplify
5038 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4))) 5907 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5039 (with { tree op_type = TREE_TYPE (@5); } 5908 (with { tree op_type = TREE_TYPE (@5); }
5040 (if (element_precision (type) == element_precision (op_type)) 5909 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5910 && element_precision (type) == element_precision (op_type))
5041 (view_convert (cond_op (bit_not @0) @2 @3 @4 5911 (view_convert (cond_op (bit_not @0) @2 @3 @4
5042 (view_convert:op_type @1))))))) 5912 (view_convert:op_type @1)))))))
5913 #endif
5043 5914
5044 /* Detect cases in which a VEC_COND_EXPR effectively replaces the 5915 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
5045 "else" value of an IFN_COND_*. */ 5916 "else" value of an IFN_COND_*. */
5046 (for cond_op (COND_BINARY) 5917 (for cond_op (COND_BINARY)
5047 (simplify 5918 (simplify
5127 (pointer_diff:ssizetype 5998 (pointer_diff:ssizetype
5128 (pointer_plus { swap_p ? @2 : @0; } 5999 (pointer_plus { swap_p ? @2 : @0; }
5129 { wide_int_to_tree (sizetype, off); }) 6000 { wide_int_to_tree (sizetype, off); })
5130 { swap_p ? @0 : @2; })) 6001 { swap_p ? @0 : @2; }))
5131 { rhs_tree; }))))))))) 6002 { rhs_tree; })))))))))
6003
6004 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6005 element of @1. */
6006 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6007 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6008 (with { int i = single_nonzero_element (@1); }
6009 (if (i >= 0)
6010 (with { tree elt = vector_cst_elt (@1, i);
6011 tree elt_type = TREE_TYPE (elt);
6012 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6013 tree size = bitsize_int (elt_bits);
6014 tree pos = bitsize_int (elt_bits * i); }
6015 (view_convert
6016 (bit_and:elt_type
6017 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6018 { elt; })))))))
6019
6020 (simplify
6021 (vec_perm @0 @1 VECTOR_CST@2)
6022 (with
6023 {
6024 tree op0 = @0, op1 = @1, op2 = @2;
6025
6026 /* Build a vector of integers from the tree mask. */
6027 vec_perm_builder builder;
6028 if (!tree_to_vec_perm_builder (&builder, op2))
6029 return NULL_TREE;
6030
6031 /* Create a vec_perm_indices for the integer vector. */
6032 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6033 bool single_arg = (op0 == op1);
6034 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6035 }
6036 (if (sel.series_p (0, 1, 0, 1))
6037 { op0; }
6038 (if (sel.series_p (0, 1, nelts, 1))
6039 { op1; }
6040 (with
6041 {
6042 if (!single_arg)
6043 {
6044 if (sel.all_from_input_p (0))
6045 op1 = op0;
6046 else if (sel.all_from_input_p (1))
6047 {
6048 op0 = op1;
6049 sel.rotate_inputs (1);
6050 }
6051 else if (known_ge (poly_uint64 (sel[0]), nelts))
6052 {
6053 std::swap (op0, op1);
6054 sel.rotate_inputs (1);
6055 }
6056 }
6057 gassign *def;
6058 tree cop0 = op0, cop1 = op1;
6059 if (TREE_CODE (op0) == SSA_NAME
6060 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6061 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6062 cop0 = gimple_assign_rhs1 (def);
6063 if (TREE_CODE (op1) == SSA_NAME
6064 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6065 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6066 cop1 = gimple_assign_rhs1 (def);
6067
6068 tree t;
6069 }
6070 (if ((TREE_CODE (cop0) == VECTOR_CST
6071 || TREE_CODE (cop0) == CONSTRUCTOR)
6072 && (TREE_CODE (cop1) == VECTOR_CST
6073 || TREE_CODE (cop1) == CONSTRUCTOR)
6074 && (t = fold_vec_perm (type, cop0, cop1, sel)))
6075 { t; }
6076 (with
6077 {
6078 bool changed = (op0 == op1 && !single_arg);
6079 tree ins = NULL_TREE;
6080 unsigned at = 0;
6081
6082 /* See if the permutation is performing a single element
6083 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6084 in that case. But only if the vector mode is supported,
6085 otherwise this is invalid GIMPLE. */
6086 if (TYPE_MODE (type) != BLKmode
6087 && (TREE_CODE (cop0) == VECTOR_CST
6088 || TREE_CODE (cop0) == CONSTRUCTOR
6089 || TREE_CODE (cop1) == VECTOR_CST
6090 || TREE_CODE (cop1) == CONSTRUCTOR))
6091 {
6092 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6093 if (insert_first_p)
6094 {
6095 /* After canonicalizing the first elt to come from the
6096 first vector we only can insert the first elt from
6097 the first vector. */
6098 at = 0;
6099 if ((ins = fold_read_from_vector (cop0, sel[0])))
6100 op0 = op1;
6101 }
6102 /* The above can fail for two-element vectors which always
6103 appear to insert the first element, so try inserting
6104 into the second lane as well. For more than two
6105 elements that's wasted time. */
6106 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6107 {
6108 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6109 for (at = 0; at < encoded_nelts; ++at)
6110 if (maybe_ne (sel[at], at))
6111 break;
6112 if (at < encoded_nelts
6113 && (known_eq (at + 1, nelts)
6114 || sel.series_p (at + 1, 1, at + 1, 1)))
6115 {
6116 if (known_lt (poly_uint64 (sel[at]), nelts))
6117 ins = fold_read_from_vector (cop0, sel[at]);
6118 else
6119 ins = fold_read_from_vector (cop1, sel[at] - nelts);
6120 }
6121 }
6122 }
6123
6124 /* Generate a canonical form of the selector. */
6125 if (!ins && sel.encoding () != builder)
6126 {
6127 /* Some targets are deficient and fail to expand a single
6128 argument permutation while still allowing an equivalent
6129 2-argument version. */
6130 tree oldop2 = op2;
6131 if (sel.ninputs () == 2
6132 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6133 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6134 else
6135 {
6136 vec_perm_indices sel2 (builder, 2, nelts);
6137 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6138 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6139 else
6140 /* Not directly supported with either encoding,
6141 so use the preferred form. */
6142 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6143 }
6144 if (!operand_equal_p (op2, oldop2, 0))
6145 changed = true;
6146 }
6147 }
6148 (if (ins)
6149 (bit_insert { op0; } { ins; }
6150 { bitsize_int (at * tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))); })
6151 (if (changed)
6152 (vec_perm { op0; } { op1; } { op2; }))))))))))
6153
6154 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
6155
6156 (match vec_same_elem_p
6157 @0
6158 (if (uniform_vector_p (@0))))
6159
6160 (match vec_same_elem_p
6161 (vec_duplicate @0))
6162
6163 (simplify
6164 (vec_perm vec_same_elem_p@0 @0 @1)
6165 @0)
6166
6167 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6168 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6169 constant which when multiplied by a power of 2 contains a unique value
6170 in the top 5 or 6 bits. This is then indexed into a table which maps it
6171 to the number of trailing zeroes. */
6172 (match (ctz_table_index @1 @2 @3)
6173 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))