1/* Match-and-simplify patterns for shared GENERIC and GIMPLE folding. 2 This file is consumed by genmatch which produces gimple-match.c 3 and generic-match.c from it. 4 5 Copyright (C) 2014-2021 Free Software Foundation, Inc. 6 Contributed by Richard Biener <rguenther@suse.de> 7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com> 8 9This file is part of GCC. 10 11GCC is free software; you can redistribute it and/or modify it under 12the terms of the GNU General Public License as published by the Free 13Software Foundation; either version 3, or (at your option) any later 14version. 15 16GCC is distributed in the hope that it will be useful, but WITHOUT ANY 17WARRANTY; without even the implied warranty of MERCHANTABILITY or 18FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 19for more details. 20 21You should have received a copy of the GNU General Public License 22along with GCC; see the file COPYING3. If not see 23<http://www.gnu.org/licenses/>. */ 24 25 26/* Generic tree predicates we inherit. */ 27(define_predicates 28 integer_onep integer_zerop integer_all_onesp integer_minus_onep 29 integer_each_onep integer_truep integer_nonzerop 30 real_zerop real_onep real_minus_onep 31 zerop 32 initializer_each_zero_or_onep 33 CONSTANT_CLASS_P 34 tree_expr_nonnegative_p 35 tree_expr_nonzero_p 36 integer_valued_real_p 37 integer_pow2p 38 uniform_integer_cst_p 39 HONOR_NANS 40 uniform_vector_p) 41 42/* Operator lists. */ 43(define_operator_list tcc_comparison 44 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) 45(define_operator_list inverted_tcc_comparison 46 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq) 47(define_operator_list inverted_tcc_comparison_with_nans 48 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq) 49(define_operator_list swapped_tcc_comparison 50 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt) 51(define_operator_list simple_comparison lt le eq ne ge gt) 52(define_operator_list swapped_simple_comparison gt ge eq ne le lt) 53 54#include "cfn-operators.pd" 55 56/* Define operand lists for math rounding functions {,i,l,ll}FN, 57 where the versions prefixed with "i" return an int, those prefixed with 58 "l" return a long and those prefixed with "ll" return a long long. 59 60 Also define operand lists: 61 62 X<FN>F for all float functions, in the order i, l, ll 63 X<FN> for all double functions, in the same order 64 X<FN>L for all long double functions, in the same order. */ 65#define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \ 66 (define_operator_list X##FN##F BUILT_IN_I##FN##F \ 67 BUILT_IN_L##FN##F \ 68 BUILT_IN_LL##FN##F) \ 69 (define_operator_list X##FN BUILT_IN_I##FN \ 70 BUILT_IN_L##FN \ 71 BUILT_IN_LL##FN) \ 72 (define_operator_list X##FN##L BUILT_IN_I##FN##L \ 73 BUILT_IN_L##FN##L \ 74 BUILT_IN_LL##FN##L) 75 76DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR) 77DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL) 78DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND) 79DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) 80 81/* Binary operations and their associated IFN_COND_* function. */ 82(define_operator_list UNCOND_BINARY 83 plus minus 84 mult trunc_div trunc_mod rdiv 85 min max 86 bit_and bit_ior bit_xor 87 lshift rshift) 88(define_operator_list COND_BINARY 89 IFN_COND_ADD IFN_COND_SUB 90 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV 91 IFN_COND_MIN IFN_COND_MAX 92 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR 93 IFN_COND_SHL IFN_COND_SHR) 94 95/* Same for ternary operations. */ 96(define_operator_list UNCOND_TERNARY 97 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS) 98(define_operator_list COND_TERNARY 99 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS) 100 101/* With nop_convert? combine convert? and view_convert? in one pattern 102 plus conditionalize on tree_nop_conversion_p conversions. */ 103(match (nop_convert @0) 104 (convert @0) 105 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))))) 106(match (nop_convert @0) 107 (view_convert @0) 108 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0)) 109 && known_eq (TYPE_VECTOR_SUBPARTS (type), 110 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))) 111 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) 112 113/* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x> 114 ABSU_EXPR returns unsigned absolute value of the operand and the operand 115 of the ABSU_EXPR will have the corresponding signed type. */ 116(simplify (abs (convert @0)) 117 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 118 && !TYPE_UNSIGNED (TREE_TYPE (@0)) 119 && element_precision (type) > element_precision (TREE_TYPE (@0))) 120 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } 121 (convert (absu:utype @0))))) 122 123#if GIMPLE 124/* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */ 125(simplify 126 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2) 127 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 128 && !TYPE_UNSIGNED (TREE_TYPE (@0)) 129 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1) 130 (abs @0))) 131#endif 132 133/* Simplifications of operations with one constant operand and 134 simplifications to constants or single values. */ 135 136(for op (plus pointer_plus minus bit_ior bit_xor) 137 (simplify 138 (op @0 integer_zerop) 139 (non_lvalue @0))) 140 141/* 0 +p index -> (type)index */ 142(simplify 143 (pointer_plus integer_zerop @1) 144 (non_lvalue (convert @1))) 145 146/* ptr - 0 -> (type)ptr */ 147(simplify 148 (pointer_diff @0 integer_zerop) 149 (convert @0)) 150 151/* See if ARG1 is zero and X + ARG1 reduces to X. 152 Likewise if the operands are reversed. */ 153(simplify 154 (plus:c @0 real_zerop@1) 155 (if (fold_real_zero_addition_p (type, @1, 0)) 156 (non_lvalue @0))) 157 158/* See if ARG1 is zero and X - ARG1 reduces to X. */ 159(simplify 160 (minus @0 real_zerop@1) 161 (if (fold_real_zero_addition_p (type, @1, 1)) 162 (non_lvalue @0))) 163 164/* Even if the fold_real_zero_addition_p can't simplify X + 0.0 165 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0 166 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0 167 if not -frounding-math. For sNaNs the first operation would raise 168 exceptions but turn the result into qNan, so the second operation 169 would not raise it. */ 170(for inner_op (plus minus) 171 (for outer_op (plus minus) 172 (simplify 173 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2) 174 (if (real_zerop (@1) 175 && real_zerop (@2) 176 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)) 177 (with { bool inner_plus = ((inner_op == PLUS_EXPR) 178 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))); 179 bool outer_plus 180 = ((outer_op == PLUS_EXPR) 181 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); } 182 (if (outer_plus && !inner_plus) 183 (outer_op @0 @2) 184 @3)))))) 185 186/* Simplify x - x. 187 This is unsafe for certain floats even in non-IEEE formats. 188 In IEEE, it is unsafe because it does wrong for NaNs. 189 Also note that operand_equal_p is always false if an operand 190 is volatile. */ 191(simplify 192 (minus @0 @0) 193 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type)) 194 { build_zero_cst (type); })) 195(simplify 196 (pointer_diff @@0 @0) 197 { build_zero_cst (type); }) 198 199(simplify 200 (mult @0 integer_zerop@1) 201 @1) 202 203/* Maybe fold x * 0 to 0. The expressions aren't the same 204 when x is NaN, since x * 0 is also NaN. Nor are they the 205 same in modes with signed zeros, since multiplying a 206 negative value by 0 gives -0, not +0. */ 207(simplify 208 (mult @0 real_zerop@1) 209 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) 210 @1)) 211 212/* In IEEE floating point, x*1 is not equivalent to x for snans. 213 Likewise for complex arithmetic with signed zeros. */ 214(simplify 215 (mult @0 real_onep) 216 (if (!HONOR_SNANS (type) 217 && (!HONOR_SIGNED_ZEROS (type) 218 || !COMPLEX_FLOAT_TYPE_P (type))) 219 (non_lvalue @0))) 220 221/* Transform x * -1.0 into -x. */ 222(simplify 223 (mult @0 real_minus_onep) 224 (if (!HONOR_SNANS (type) 225 && (!HONOR_SIGNED_ZEROS (type) 226 || !COMPLEX_FLOAT_TYPE_P (type))) 227 (negate @0))) 228 229/* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */ 230(simplify 231 (mult SSA_NAME@1 SSA_NAME@2) 232 (if (INTEGRAL_TYPE_P (type) 233 && get_nonzero_bits (@1) == 1 234 && get_nonzero_bits (@2) == 1) 235 (bit_and @1 @2))) 236 237/* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...}, 238 unless the target has native support for the former but not the latter. */ 239(simplify 240 (mult @0 VECTOR_CST@1) 241 (if (initializer_each_zero_or_onep (@1) 242 && !HONOR_SNANS (type) 243 && !HONOR_SIGNED_ZEROS (type)) 244 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; } 245 (if (itype 246 && (!VECTOR_MODE_P (TYPE_MODE (type)) 247 || (VECTOR_MODE_P (TYPE_MODE (itype)) 248 && optab_handler (and_optab, 249 TYPE_MODE (itype)) != CODE_FOR_nothing))) 250 (view_convert (bit_and:itype (view_convert @0) 251 (ne @1 { build_zero_cst (type); }))))))) 252 253(for cmp (gt ge lt le) 254 outp (convert convert negate negate) 255 outn (negate negate convert convert) 256 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */ 257 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */ 258 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */ 259 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */ 260 (simplify 261 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)) 262 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) 263 (outp (abs @0)))) 264 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */ 265 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */ 266 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */ 267 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */ 268 (simplify 269 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)) 270 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) 271 (outn (abs @0))))) 272 273/* Transform X * copysign (1.0, X) into abs(X). */ 274(simplify 275 (mult:c @0 (COPYSIGN_ALL real_onep @0)) 276 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) 277 (abs @0))) 278 279/* Transform X * copysign (1.0, -X) into -abs(X). */ 280(simplify 281 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0))) 282 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) 283 (negate (abs @0)))) 284 285/* Transform copysign (CST, X) into copysign (ABS(CST), X). */ 286(simplify 287 (COPYSIGN_ALL REAL_CST@0 @1) 288 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0))) 289 (COPYSIGN_ALL (negate @0) @1))) 290 291/* X * 1, X / 1 -> X. */ 292(for op (mult trunc_div ceil_div floor_div round_div exact_div) 293 (simplify 294 (op @0 integer_onep) 295 (non_lvalue @0))) 296 297/* (A / (1 << B)) -> (A >> B). 298 Only for unsigned A. For signed A, this would not preserve rounding 299 toward zero. 300 For example: (-1 / ( 1 << B)) != -1 >> B. 301 Also also widening conversions, like: 302 (A / (unsigned long long) (1U << B)) -> (A >> B) 303 or 304 (A / (unsigned long long) (1 << B)) -> (A >> B). 305 If the left shift is signed, it can be done only if the upper bits 306 of A starting from shift's type sign bit are zero, as 307 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL, 308 so it is valid only if A >> 31 is zero. */ 309(simplify 310 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2))) 311 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) 312 && (!VECTOR_TYPE_P (type) 313 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector) 314 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)) 315 && (useless_type_conversion_p (type, TREE_TYPE (@1)) 316 || (element_precision (type) >= element_precision (TREE_TYPE (@1)) 317 && (TYPE_UNSIGNED (TREE_TYPE (@1)) 318 || (element_precision (type) 319 == element_precision (TREE_TYPE (@1))) 320 || (INTEGRAL_TYPE_P (type) 321 && (tree_nonzero_bits (@0) 322 & wi::mask (element_precision (TREE_TYPE (@1)) - 1, 323 true, 324 element_precision (type))) == 0))))) 325 (if (!VECTOR_TYPE_P (type) 326 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)) 327 && element_precision (TREE_TYPE (@3)) < element_precision (type)) 328 (convert (rshift @3 @2)) 329 (rshift @0 @2)))) 330 331/* Preserve explicit divisions by 0: the C++ front-end wants to detect 332 undefined behavior in constexpr evaluation, and assuming that the division 333 traps enables better optimizations than these anyway. */ 334(for div (trunc_div ceil_div floor_div round_div exact_div) 335 /* 0 / X is always zero. */ 336 (simplify 337 (div integer_zerop@0 @1) 338 /* But not for 0 / 0 so that we can get the proper warnings and errors. */ 339 (if (!integer_zerop (@1)) 340 @0)) 341 /* X / -1 is -X. */ 342 (simplify 343 (div @0 integer_minus_onep@1) 344 (if (!TYPE_UNSIGNED (type)) 345 (negate @0))) 346 /* X / bool_range_Y is X. */ 347 (simplify 348 (div @0 SSA_NAME@1) 349 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1)) 350 @0)) 351 /* X / X is one. */ 352 (simplify 353 (div @0 @0) 354 /* But not for 0 / 0 so that we can get the proper warnings and errors. 355 And not for _Fract types where we can't build 1. */ 356 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type))) 357 { build_one_cst (type); })) 358 /* X / abs (X) is X < 0 ? -1 : 1. */ 359 (simplify 360 (div:C @0 (abs @0)) 361 (if (INTEGRAL_TYPE_P (type) 362 && TYPE_OVERFLOW_UNDEFINED (type)) 363 (cond (lt @0 { build_zero_cst (type); }) 364 { build_minus_one_cst (type); } { build_one_cst (type); }))) 365 /* X / -X is -1. */ 366 (simplify 367 (div:C @0 (negate @0)) 368 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) 369 && TYPE_OVERFLOW_UNDEFINED (type)) 370 { build_minus_one_cst (type); }))) 371 372/* For unsigned integral types, FLOOR_DIV_EXPR is the same as 373 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */ 374(simplify 375 (floor_div @0 @1) 376 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) 377 && TYPE_UNSIGNED (type)) 378 (trunc_div @0 @1))) 379 380/* Combine two successive divisions. Note that combining ceil_div 381 and floor_div is trickier and combining round_div even more so. */ 382(for div (trunc_div exact_div) 383 (simplify 384 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2) 385 (with { 386 wi::overflow_type overflow; 387 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), 388 TYPE_SIGN (type), &overflow); 389 } 390 (if (div == EXACT_DIV_EXPR 391 || optimize_successive_divisions_p (@2, @3)) 392 (if (!overflow) 393 (div @0 { wide_int_to_tree (type, mul); }) 394 (if (TYPE_UNSIGNED (type) 395 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)) 396 { build_zero_cst (type); })))))) 397 398/* Combine successive multiplications. Similar to above, but handling 399 overflow is different. */ 400(simplify 401 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2) 402 (with { 403 wi::overflow_type overflow; 404 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), 405 TYPE_SIGN (type), &overflow); 406 } 407 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN, 408 otherwise undefined overflow implies that @0 must be zero. */ 409 (if (!overflow || TYPE_OVERFLOW_WRAPS (type)) 410 (mult @0 { wide_int_to_tree (type, mul); })))) 411 412/* Optimize A / A to 1.0 if we don't care about 413 NaNs or Infinities. */ 414(simplify 415 (rdiv @0 @0) 416 (if (FLOAT_TYPE_P (type) 417 && ! HONOR_NANS (type) 418 && ! HONOR_INFINITIES (type)) 419 { build_one_cst (type); })) 420 421/* Optimize -A / A to -1.0 if we don't care about 422 NaNs or Infinities. */ 423(simplify 424 (rdiv:C @0 (negate @0)) 425 (if (FLOAT_TYPE_P (type) 426 && ! HONOR_NANS (type) 427 && ! HONOR_INFINITIES (type)) 428 { build_minus_one_cst (type); })) 429 430/* PR71078: x / abs(x) -> copysign (1.0, x) */ 431(simplify 432 (rdiv:C (convert? @0) (convert? (abs @0))) 433 (if (SCALAR_FLOAT_TYPE_P (type) 434 && ! HONOR_NANS (type) 435 && ! HONOR_INFINITIES (type)) 436 (switch 437 (if (types_match (type, float_type_node)) 438 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0))) 439 (if (types_match (type, double_type_node)) 440 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0))) 441 (if (types_match (type, long_double_type_node)) 442 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0)))))) 443 444/* In IEEE floating point, x/1 is not equivalent to x for snans. */ 445(simplify 446 (rdiv @0 real_onep) 447 (if (!HONOR_SNANS (type)) 448 (non_lvalue @0))) 449 450/* In IEEE floating point, x/-1 is not equivalent to -x for snans. */ 451(simplify 452 (rdiv @0 real_minus_onep) 453 (if (!HONOR_SNANS (type)) 454 (negate @0))) 455 456(if (flag_reciprocal_math) 457 /* Convert (A/B)/C to A/(B*C). */ 458 (simplify 459 (rdiv (rdiv:s @0 @1) @2) 460 (rdiv @0 (mult @1 @2))) 461 462 /* Canonicalize x / (C1 * y) to (x * C2) / y. */ 463 (simplify 464 (rdiv @0 (mult:s @1 REAL_CST@2)) 465 (with 466 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); } 467 (if (tem) 468 (rdiv (mult @0 { tem; } ) @1)))) 469 470 /* Convert A/(B/C) to (A/B)*C */ 471 (simplify 472 (rdiv @0 (rdiv:s @1 @2)) 473 (mult (rdiv @0 @1) @2))) 474 475/* Simplify x / (- y) to -x / y. */ 476(simplify 477 (rdiv @0 (negate @1)) 478 (rdiv (negate @0) @1)) 479 480(if (flag_unsafe_math_optimizations) 481 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan. 482 Since C / x may underflow to zero, do this only for unsafe math. */ 483 (for op (lt le gt ge) 484 neg_op (gt ge lt le) 485 (simplify 486 (op (rdiv REAL_CST@0 @1) real_zerop@2) 487 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1)) 488 (switch 489 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0))) 490 (op @1 @2)) 491 /* For C < 0, use the inverted operator. */ 492 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0)) 493 (neg_op @1 @2))))))) 494 495/* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */ 496(for div (trunc_div ceil_div floor_div round_div exact_div) 497 (simplify 498 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2) 499 (if (integer_pow2p (@2) 500 && tree_int_cst_sgn (@2) > 0 501 && tree_nop_conversion_p (type, TREE_TYPE (@0)) 502 && wi::to_wide (@2) + wi::to_wide (@1) == 0) 503 (rshift (convert @0) 504 { build_int_cst (integer_type_node, 505 wi::exact_log2 (wi::to_wide (@2))); })))) 506 507/* If ARG1 is a constant, we can convert this to a multiply by the 508 reciprocal. This does not have the same rounding properties, 509 so only do this if -freciprocal-math. We can actually 510 always safely do it if ARG1 is a power of two, but it's hard to 511 tell if it is or not in a portable manner. */ 512(for cst (REAL_CST COMPLEX_CST VECTOR_CST) 513 (simplify 514 (rdiv @0 cst@1) 515 (if (optimize) 516 (if (flag_reciprocal_math 517 && !real_zerop (@1)) 518 (with 519 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); } 520 (if (tem) 521 (mult @0 { tem; } ))) 522 (if (cst != COMPLEX_CST) 523 (with { tree inverse = exact_inverse (type, @1); } 524 (if (inverse) 525 (mult @0 { inverse; } )))))))) 526 527(for mod (ceil_mod floor_mod round_mod trunc_mod) 528 /* 0 % X is always zero. */ 529 (simplify 530 (mod integer_zerop@0 @1) 531 /* But not for 0 % 0 so that we can get the proper warnings and errors. */ 532 (if (!integer_zerop (@1)) 533 @0)) 534 /* X % 1 is always zero. */ 535 (simplify 536 (mod @0 integer_onep) 537 { build_zero_cst (type); }) 538 /* X % -1 is zero. */ 539 (simplify 540 (mod @0 integer_minus_onep@1) 541 (if (!TYPE_UNSIGNED (type)) 542 { build_zero_cst (type); })) 543 /* X % X is zero. */ 544 (simplify 545 (mod @0 @0) 546 /* But not for 0 % 0 so that we can get the proper warnings and errors. */ 547 (if (!integer_zerop (@0)) 548 { build_zero_cst (type); })) 549 /* (X % Y) % Y is just X % Y. */ 550 (simplify 551 (mod (mod@2 @0 @1) @1) 552 @2) 553 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */ 554 (simplify 555 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2) 556 (if (ANY_INTEGRAL_TYPE_P (type) 557 && TYPE_OVERFLOW_UNDEFINED (type) 558 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2), 559 TYPE_SIGN (type))) 560 { build_zero_cst (type); })) 561 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned 562 modulo and comparison, since it is simpler and equivalent. */ 563 (for cmp (eq ne) 564 (simplify 565 (cmp (mod @0 integer_pow2p@2) integer_zerop@1) 566 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))) 567 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } 568 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1))))))) 569 570/* X % -C is the same as X % C. */ 571(simplify 572 (trunc_mod @0 INTEGER_CST@1) 573 (if (TYPE_SIGN (type) == SIGNED 574 && !TREE_OVERFLOW (@1) 575 && wi::neg_p (wi::to_wide (@1)) 576 && !TYPE_OVERFLOW_TRAPS (type) 577 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ 578 && !sign_bit_p (@1, @1)) 579 (trunc_mod @0 (negate @1)))) 580 581/* X % -Y is the same as X % Y. */ 582(simplify 583 (trunc_mod @0 (convert? (negate @1))) 584 (if (INTEGRAL_TYPE_P (type) 585 && !TYPE_UNSIGNED (type) 586 && !TYPE_OVERFLOW_TRAPS (type) 587 && tree_nop_conversion_p (type, TREE_TYPE (@1)) 588 /* Avoid this transformation if X might be INT_MIN or 589 Y might be -1, because we would then change valid 590 INT_MIN % -(-1) into invalid INT_MIN % -1. */ 591 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type))) 592 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION 593 (TREE_TYPE (@1)))))) 594 (trunc_mod @0 (convert @1)))) 595 596/* X - (X / Y) * Y is the same as X % Y. */ 597(simplify 598 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1))) 599 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) 600 (convert (trunc_mod @0 @1)))) 601 602/* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, 603 i.e. "X % C" into "X & (C - 1)", if X and C are positive. 604 Also optimize A % (C << N) where C is a power of 2, 605 to A & ((C << N) - 1). 606 Also optimize "A shift (B % C)", if C is a power of 2, to 607 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>" 608 and assume (B % C) is nonnegative as shifts negative values would 609 be UB. */ 610(match (power_of_two_cand @1) 611 INTEGER_CST@1) 612(match (power_of_two_cand @1) 613 (lshift INTEGER_CST@1 @2)) 614(for mod (trunc_mod floor_mod) 615 (for shift (lshift rshift) 616 (simplify 617 (shift @0 (mod @1 (power_of_two_cand@2 @3))) 618 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0) 619 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2), 620 1); })))))) 621 (simplify 622 (mod @0 (convert? (power_of_two_cand@1 @2))) 623 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) 624 /* Allow any integral conversions of the divisor, except 625 conversion from narrower signed to wider unsigned type 626 where if @1 would be negative power of two, the divisor 627 would not be a power of two. */ 628 && INTEGRAL_TYPE_P (type) 629 && INTEGRAL_TYPE_P (TREE_TYPE (@1)) 630 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1)) 631 || TYPE_UNSIGNED (TREE_TYPE (@1)) 632 || !TYPE_UNSIGNED (type)) 633 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) 634 (with { tree utype = TREE_TYPE (@1); 635 if (!TYPE_OVERFLOW_WRAPS (utype)) 636 utype = unsigned_type_for (utype); } 637 (bit_and @0 (convert (minus (convert:utype @1) 638 { build_one_cst (utype); }))))))) 639 640/* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */ 641(simplify 642 (trunc_div (mult @0 integer_pow2p@1) @1) 643 (if (TYPE_UNSIGNED (TREE_TYPE (@0))) 644 (bit_and @0 { wide_int_to_tree 645 (type, wi::mask (TYPE_PRECISION (type) 646 - wi::exact_log2 (wi::to_wide (@1)), 647 false, TYPE_PRECISION (type))); }))) 648 649/* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */ 650(simplify 651 (mult (trunc_div @0 integer_pow2p@1) @1) 652 (if (TYPE_UNSIGNED (TREE_TYPE (@0))) 653 (bit_and @0 (negate @1)))) 654 655/* Simplify (t * 2) / 2) -> t. */ 656(for div (trunc_div ceil_div floor_div round_div exact_div) 657 (simplify 658 (div (mult:c @0 @1) @1) 659 (if (ANY_INTEGRAL_TYPE_P (type)) 660 (if (TYPE_OVERFLOW_UNDEFINED (type)) 661 @0 662#if GIMPLE 663 (with 664 { 665 bool overflowed = true; 666 wide_int wmin0, wmax0, wmin1, wmax1; 667 if (INTEGRAL_TYPE_P (type) 668 && get_range_info (@0, &wmin0, &wmax0) == VR_RANGE 669 && get_range_info (@1, &wmin1, &wmax1) == VR_RANGE) 670 { 671 /* If the multiplication can't overflow/wrap around, then 672 it can be optimized too. */ 673 wi::overflow_type min_ovf, max_ovf; 674 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf); 675 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf); 676 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE) 677 { 678 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf); 679 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf); 680 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE) 681 overflowed = false; 682 } 683 } 684 } 685 (if (!overflowed) 686 @0)) 687#endif 688 )))) 689 690(for op (negate abs) 691 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ 692 (for coss (COS COSH) 693 (simplify 694 (coss (op @0)) 695 (coss @0))) 696 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */ 697 (for pows (POW) 698 (simplify 699 (pows (op @0) REAL_CST@1) 700 (with { HOST_WIDE_INT n; } 701 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) 702 (pows @0 @1))))) 703 /* Likewise for powi. */ 704 (for pows (POWI) 705 (simplify 706 (pows (op @0) INTEGER_CST@1) 707 (if ((wi::to_wide (@1) & 1) == 0) 708 (pows @0 @1)))) 709 /* Strip negate and abs from both operands of hypot. */ 710 (for hypots (HYPOT) 711 (simplify 712 (hypots (op @0) @1) 713 (hypots @0 @1)) 714 (simplify 715 (hypots @0 (op @1)) 716 (hypots @0 @1))) 717 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */ 718 (for copysigns (COPYSIGN_ALL) 719 (simplify 720 (copysigns (op @0) @1) 721 (copysigns @0 @1)))) 722 723/* abs(x)*abs(x) -> x*x. Should be valid for all types. */ 724(simplify 725 (mult (abs@1 @0) @1) 726 (mult @0 @0)) 727 728/* Convert absu(x)*absu(x) -> x*x. */ 729(simplify 730 (mult (absu@1 @0) @1) 731 (mult (convert@2 @0) @2)) 732 733/* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */ 734(for coss (COS COSH) 735 copysigns (COPYSIGN) 736 (simplify 737 (coss (copysigns @0 @1)) 738 (coss @0))) 739 740/* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */ 741(for pows (POW) 742 copysigns (COPYSIGN) 743 (simplify 744 (pows (copysigns @0 @2) REAL_CST@1) 745 (with { HOST_WIDE_INT n; } 746 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) 747 (pows @0 @1))))) 748/* Likewise for powi. */ 749(for pows (POWI) 750 copysigns (COPYSIGN) 751 (simplify 752 (pows (copysigns @0 @2) INTEGER_CST@1) 753 (if ((wi::to_wide (@1) & 1) == 0) 754 (pows @0 @1)))) 755 756(for hypots (HYPOT) 757 copysigns (COPYSIGN) 758 /* hypot(copysign(x, y), z) -> hypot(x, z). */ 759 (simplify 760 (hypots (copysigns @0 @1) @2) 761 (hypots @0 @2)) 762 /* hypot(x, copysign(y, z)) -> hypot(x, y). */ 763 (simplify 764 (hypots @0 (copysigns @1 @2)) 765 (hypots @0 @1))) 766 767/* copysign(x, CST) -> [-]abs (x). */ 768(for copysigns (COPYSIGN_ALL) 769 (simplify 770 (copysigns @0 REAL_CST@1) 771 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) 772 (negate (abs @0)) 773 (abs @0)))) 774 775/* copysign(copysign(x, y), z) -> copysign(x, z). */ 776(for copysigns (COPYSIGN_ALL) 777 (simplify 778 (copysigns (copysigns @0 @1) @2) 779 (copysigns @0 @2))) 780 781/* copysign(x,y)*copysign(x,y) -> x*x. */ 782(for copysigns (COPYSIGN_ALL) 783 (simplify 784 (mult (copysigns@2 @0 @1) @2) 785 (mult @0 @0))) 786 787/* ccos(-x) -> ccos(x). Similarly for ccosh. */ 788(for ccoss (CCOS CCOSH) 789 (simplify 790 (ccoss (negate @0)) 791 (ccoss @0))) 792 793/* cabs(-x) and cos(conj(x)) -> cabs(x). */ 794(for ops (conj negate) 795 (for cabss (CABS) 796 (simplify 797 (cabss (ops @0)) 798 (cabss @0)))) 799 800/* Fold (a * (1 << b)) into (a << b) */ 801(simplify 802 (mult:c @0 (convert? (lshift integer_onep@1 @2))) 803 (if (! FLOAT_TYPE_P (type) 804 && tree_nop_conversion_p (type, TREE_TYPE (@1))) 805 (lshift @0 @2))) 806 807/* Fold (1 << (C - x)) where C = precision(type) - 1 808 into ((1 << C) >> x). */ 809(simplify 810 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3)) 811 (if (INTEGRAL_TYPE_P (type) 812 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1) 813 && single_use (@1)) 814 (if (TYPE_UNSIGNED (type)) 815 (rshift (lshift @0 @2) @3) 816 (with 817 { tree utype = unsigned_type_for (type); } 818 (convert (rshift (lshift (convert:utype @0) @2) @3)))))) 819 820/* Fold (C1/X)*C2 into (C1*C2)/X. */ 821(simplify 822 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2) 823 (if (flag_associative_math 824 && single_use (@3)) 825 (with 826 { tree tem = const_binop (MULT_EXPR, type, @0, @2); } 827 (if (tem) 828 (rdiv { tem; } @1))))) 829 830/* Simplify ~X & X as zero. */ 831(simplify 832 (bit_and:c (convert? @0) (convert? (bit_not @0))) 833 { build_zero_cst (type); }) 834 835/* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */ 836(simplify 837 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep)) 838 (if (TYPE_UNSIGNED (type)) 839 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1))))) 840 841(for bitop (bit_and bit_ior) 842 cmp (eq ne) 843 /* PR35691: Transform 844 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0. 845 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */ 846 (simplify 847 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop)) 848 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 849 && INTEGRAL_TYPE_P (TREE_TYPE (@1)) 850 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) 851 (cmp (bit_ior @0 (convert @1)) @2))) 852 /* Transform: 853 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1. 854 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */ 855 (simplify 856 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp)) 857 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 858 && INTEGRAL_TYPE_P (TREE_TYPE (@1)) 859 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) 860 (cmp (bit_and @0 (convert @1)) @2)))) 861 862/* Fold (A & ~B) - (A & B) into (A ^ B) - B. */ 863(simplify 864 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1)) 865 (minus (bit_xor @0 @1) @1)) 866(simplify 867 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1)) 868 (if (~wi::to_wide (@2) == wi::to_wide (@1)) 869 (minus (bit_xor @0 @1) @1))) 870 871/* Fold (A & B) - (A & ~B) into B - (A ^ B). */ 872(simplify 873 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1))) 874 (minus @1 (bit_xor @0 @1))) 875 876/* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */ 877(for op (bit_ior bit_xor plus) 878 (simplify 879 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1)) 880 (bit_xor @0 @1)) 881 (simplify 882 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1)) 883 (if (~wi::to_wide (@2) == wi::to_wide (@1)) 884 (bit_xor @0 @1)))) 885 886/* PR53979: Transform ((a ^ b) | a) -> (a | b) */ 887(simplify 888 (bit_ior:c (bit_xor:c @0 @1) @0) 889 (bit_ior @0 @1)) 890 891/* (a & ~b) | (a ^ b) --> a ^ b */ 892(simplify 893 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1)) 894 @2) 895 896/* (a & ~b) ^ ~a --> ~(a & b) */ 897(simplify 898 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0)) 899 (bit_not (bit_and @0 @1))) 900 901/* (~a & b) ^ a --> (a | b) */ 902(simplify 903 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0) 904 (bit_ior @0 @1)) 905 906/* (a | b) & ~(a ^ b) --> a & b */ 907(simplify 908 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1))) 909 (bit_and @0 @1)) 910 911/* a | ~(a ^ b) --> a | ~b */ 912(simplify 913 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1))) 914 (bit_ior @0 (bit_not @1))) 915 916/* (a | b) | (a &^ b) --> a | b */ 917(for op (bit_and bit_xor) 918 (simplify 919 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1)) 920 @2)) 921 922/* (a & b) | ~(a ^ b) --> ~(a ^ b) */ 923(simplify 924 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1))) 925 @2) 926 927/* ~(~a & b) --> a | ~b */ 928(simplify 929 (bit_not (bit_and:cs (bit_not @0) @1)) 930 (bit_ior @0 (bit_not @1))) 931 932/* ~(~a | b) --> a & ~b */ 933(simplify 934 (bit_not (bit_ior:cs (bit_not @0) @1)) 935 (bit_and @0 (bit_not @1))) 936 937/* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */ 938(simplify 939 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0)) 940 (bit_and @3 (bit_not @2))) 941 942/* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */ 943(simplify 944 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0)) 945 (bit_ior @3 @2)) 946 947#if GIMPLE 948/* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */ 949(simplify 950 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2) 951 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1))) 952 953/* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */ 954(simplify 955 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2) 956 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1))) 957 958/* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */ 959(simplify 960 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1) 961 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 962 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) 963 (bit_xor @0 @1))) 964#endif 965 966/* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M, 967 ((A & N) + B) & M -> (A + B) & M 968 Similarly if (N & M) == 0, 969 ((A | N) + B) & M -> (A + B) & M 970 and for - instead of + (or unary - instead of +) 971 and/or ^ instead of |. 972 If B is constant and (B & M) == 0, fold into A & M. */ 973(for op (plus minus) 974 (for bitop (bit_and bit_ior bit_xor) 975 (simplify 976 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2) 977 (with 978 { tree pmop[2]; 979 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop, 980 @3, @4, @1, ERROR_MARK, NULL_TREE, 981 NULL_TREE, pmop); } 982 (if (utype) 983 (convert (bit_and (op (convert:utype { pmop[0]; }) 984 (convert:utype { pmop[1]; })) 985 (convert:utype @2)))))) 986 (simplify 987 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2) 988 (with 989 { tree pmop[2]; 990 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK, 991 NULL_TREE, NULL_TREE, @1, bitop, @3, 992 @4, pmop); } 993 (if (utype) 994 (convert (bit_and (op (convert:utype { pmop[0]; }) 995 (convert:utype { pmop[1]; })) 996 (convert:utype @2))))))) 997 (simplify 998 (bit_and (op:s @0 @1) INTEGER_CST@2) 999 (with 1000 { tree pmop[2]; 1001 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK, 1002 NULL_TREE, NULL_TREE, @1, ERROR_MARK, 1003 NULL_TREE, NULL_TREE, pmop); } 1004 (if (utype) 1005 (convert (bit_and (op (convert:utype { pmop[0]; }) 1006 (convert:utype { pmop[1]; })) 1007 (convert:utype @2))))))) 1008(for bitop (bit_and bit_ior bit_xor) 1009 (simplify 1010 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1) 1011 (with 1012 { tree pmop[2]; 1013 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0, 1014 bitop, @2, @3, NULL_TREE, ERROR_MARK, 1015 NULL_TREE, NULL_TREE, pmop); } 1016 (if (utype) 1017 (convert (bit_and (negate (convert:utype { pmop[0]; })) 1018 (convert:utype @1))))))) 1019 1020/* X % Y is smaller than Y. */ 1021(for cmp (lt ge) 1022 (simplify 1023 (cmp (trunc_mod @0 @1) @1) 1024 (if (TYPE_UNSIGNED (TREE_TYPE (@0))) 1025 { constant_boolean_node (cmp == LT_EXPR, type); }))) 1026(for cmp (gt le) 1027 (simplify 1028 (cmp @1 (trunc_mod @0 @1)) 1029 (if (TYPE_UNSIGNED (TREE_TYPE (@0))) 1030 { constant_boolean_node (cmp == GT_EXPR, type); }))) 1031 1032/* x | ~0 -> ~0 */ 1033(simplify 1034 (bit_ior @0 integer_all_onesp@1) 1035 @1) 1036 1037/* x | 0 -> x */ 1038(simplify 1039 (bit_ior @0 integer_zerop) 1040 @0) 1041 1042/* x & 0 -> 0 */ 1043(simplify 1044 (bit_and @0 integer_zerop@1) 1045 @1) 1046 1047/* ~x | x -> -1 */ 1048/* ~x ^ x -> -1 */ 1049/* ~x + x -> -1 */ 1050(for op (bit_ior bit_xor plus) 1051 (simplify 1052 (op:c (convert? @0) (convert? (bit_not @0))) 1053 (convert { build_all_ones_cst (TREE_TYPE (@0)); }))) 1054 1055/* x ^ x -> 0 */ 1056(simplify 1057 (bit_xor @0 @0) 1058 { build_zero_cst (type); }) 1059 1060/* Canonicalize X ^ ~0 to ~X. */ 1061(simplify 1062 (bit_xor @0 integer_all_onesp@1) 1063 (bit_not @0)) 1064 1065/* x & ~0 -> x */ 1066(simplify 1067 (bit_and @0 integer_all_onesp) 1068 (non_lvalue @0)) 1069 1070/* x & x -> x, x | x -> x */ 1071(for bitop (bit_and bit_ior) 1072 (simplify 1073 (bitop @0 @0) 1074 (non_lvalue @0))) 1075 1076/* x & C -> x if we know that x & ~C == 0. */ 1077#if GIMPLE 1078(simplify 1079 (bit_and SSA_NAME@0 INTEGER_CST@1) 1080 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1081 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) 1082 @0)) 1083#endif 1084 1085/* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */ 1086(simplify 1087 (bit_not (minus (bit_not @0) @1)) 1088 (plus @0 @1)) 1089(simplify 1090 (bit_not (plus:c (bit_not @0) @1)) 1091 (minus @0 @1)) 1092 1093/* ~(X - Y) -> ~X + Y. */ 1094(simplify 1095 (bit_not (minus:s @0 @1)) 1096 (plus (bit_not @0) @1)) 1097(simplify 1098 (bit_not (plus:s @0 INTEGER_CST@1)) 1099 (if ((INTEGRAL_TYPE_P (type) 1100 && TYPE_UNSIGNED (type)) 1101 || (!TYPE_OVERFLOW_SANITIZED (type) 1102 && may_negate_without_overflow_p (@1))) 1103 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); }))) 1104 1105#if GIMPLE 1106/* ~X + Y -> (Y - X) - 1. */ 1107(simplify 1108 (plus:c (bit_not @0) @1) 1109 (if (ANY_INTEGRAL_TYPE_P (type) 1110 && TYPE_OVERFLOW_WRAPS (type) 1111 /* -1 - X is folded to ~X, so we'd recurse endlessly. */ 1112 && !integer_all_onesp (@1)) 1113 (plus (minus @1 @0) { build_minus_one_cst (type); }) 1114 (if (INTEGRAL_TYPE_P (type) 1115 && TREE_CODE (@1) == INTEGER_CST 1116 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type), 1117 SIGNED)) 1118 (minus (plus @1 { build_minus_one_cst (type); }) @0)))) 1119 1120/* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */ 1121(simplify 1122 (bit_not (rshift:s @0 @1)) 1123 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))) 1124 (rshift (bit_not! @0) @1) 1125 /* For logical right shifts, this is possible only if @0 doesn't 1126 have MSB set and the logical right shift is changed into 1127 arithmetic shift. */ 1128 (if (!wi::neg_p (tree_nonzero_bits (@0))) 1129 (with { tree stype = signed_type_for (TREE_TYPE (@0)); } 1130 (convert (rshift (bit_not! (convert:stype @0)) @1)))))) 1131#endif 1132 1133/* x + (x & 1) -> (x + 1) & ~1 */ 1134(simplify 1135 (plus:c @0 (bit_and:s @0 integer_onep@1)) 1136 (bit_and (plus @0 @1) (bit_not @1))) 1137 1138/* x & ~(x & y) -> x & ~y */ 1139/* x | ~(x | y) -> x | ~y */ 1140(for bitop (bit_and bit_ior) 1141 (simplify 1142 (bitop:c @0 (bit_not (bitop:cs @0 @1))) 1143 (bitop @0 (bit_not @1)))) 1144 1145/* (~x & y) | ~(x | y) -> ~x */ 1146(simplify 1147 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1))) 1148 @2) 1149 1150/* (x | y) ^ (x | ~y) -> ~x */ 1151(simplify 1152 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1))) 1153 (bit_not @0)) 1154 1155/* (x & y) | ~(x | y) -> ~(x ^ y) */ 1156(simplify 1157 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1))) 1158 (bit_not (bit_xor @0 @1))) 1159 1160/* (~x | y) ^ (x ^ y) -> x | ~y */ 1161(simplify 1162 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1)) 1163 (bit_ior @0 (bit_not @1))) 1164 1165/* (x ^ y) | ~(x | y) -> ~(x & y) */ 1166(simplify 1167 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1))) 1168 (bit_not (bit_and @0 @1))) 1169 1170/* (x | y) & ~x -> y & ~x */ 1171/* (x & y) | ~x -> y | ~x */ 1172(for bitop (bit_and bit_ior) 1173 rbitop (bit_ior bit_and) 1174 (simplify 1175 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0)) 1176 (bitop @1 @2))) 1177 1178/* (x & y) ^ (x | y) -> x ^ y */ 1179(simplify 1180 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1)) 1181 (bit_xor @0 @1)) 1182 1183/* (x ^ y) ^ (x | y) -> x & y */ 1184(simplify 1185 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1)) 1186 (bit_and @0 @1)) 1187 1188/* (x & y) + (x ^ y) -> x | y */ 1189/* (x & y) | (x ^ y) -> x | y */ 1190/* (x & y) ^ (x ^ y) -> x | y */ 1191(for op (plus bit_ior bit_xor) 1192 (simplify 1193 (op:c (bit_and @0 @1) (bit_xor @0 @1)) 1194 (bit_ior @0 @1))) 1195 1196/* (x & y) + (x | y) -> x + y */ 1197(simplify 1198 (plus:c (bit_and @0 @1) (bit_ior @0 @1)) 1199 (plus @0 @1)) 1200 1201/* (x + y) - (x | y) -> x & y */ 1202(simplify 1203 (minus (plus @0 @1) (bit_ior @0 @1)) 1204 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) 1205 && !TYPE_SATURATING (type)) 1206 (bit_and @0 @1))) 1207 1208/* (x + y) - (x & y) -> x | y */ 1209(simplify 1210 (minus (plus @0 @1) (bit_and @0 @1)) 1211 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) 1212 && !TYPE_SATURATING (type)) 1213 (bit_ior @0 @1))) 1214 1215/* (x | y) - y -> (x & ~y) */ 1216(simplify 1217 (minus (bit_ior:cs @0 @1) @1) 1218 (bit_and @0 (bit_not @1))) 1219 1220/* (x | y) - (x ^ y) -> x & y */ 1221(simplify 1222 (minus (bit_ior @0 @1) (bit_xor @0 @1)) 1223 (bit_and @0 @1)) 1224 1225/* (x | y) - (x & y) -> x ^ y */ 1226(simplify 1227 (minus (bit_ior @0 @1) (bit_and @0 @1)) 1228 (bit_xor @0 @1)) 1229 1230/* (x | y) & ~(x & y) -> x ^ y */ 1231(simplify 1232 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1))) 1233 (bit_xor @0 @1)) 1234 1235/* (x | y) & (~x ^ y) -> x & y */ 1236(simplify 1237 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0))) 1238 (bit_and @0 @1)) 1239 1240/* (~x | y) & (x | ~y) -> ~(x ^ y) */ 1241(simplify 1242 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1))) 1243 (bit_not (bit_xor @0 @1))) 1244 1245/* (~x | y) ^ (x | ~y) -> x ^ y */ 1246(simplify 1247 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1))) 1248 (bit_xor @0 @1)) 1249 1250/* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */ 1251(simplify 1252 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1)) 1253 (nop_convert2? (bit_ior @0 @1)))) 1254 integer_all_onesp) 1255 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) 1256 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)) 1257 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2)) 1258 && !TYPE_SATURATING (TREE_TYPE (@2))) 1259 (bit_not (convert (bit_xor @0 @1))))) 1260(simplify 1261 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1)) 1262 integer_all_onesp)) 1263 (nop_convert3? (bit_ior @0 @1))) 1264 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) 1265 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)) 1266 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2)) 1267 && !TYPE_SATURATING (TREE_TYPE (@2))) 1268 (bit_not (convert (bit_xor @0 @1))))) 1269(simplify 1270 (minus (nop_convert1? (bit_and @0 @1)) 1271 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1)) 1272 integer_onep))) 1273 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) 1274 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)) 1275 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2)) 1276 && !TYPE_SATURATING (TREE_TYPE (@2))) 1277 (bit_not (convert (bit_xor @0 @1))))) 1278 1279/* ~x & ~y -> ~(x | y) 1280 ~x | ~y -> ~(x & y) */ 1281(for op (bit_and bit_ior) 1282 rop (bit_ior bit_and) 1283 (simplify 1284 (op (convert1? (bit_not @0)) (convert2? (bit_not @1))) 1285 (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) 1286 && element_precision (type) <= element_precision (TREE_TYPE (@1))) 1287 (bit_not (rop (convert @0) (convert @1)))))) 1288 1289/* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing 1290 with a constant, and the two constants have no bits in common, 1291 we should treat this as a BIT_IOR_EXPR since this may produce more 1292 simplifications. */ 1293(for op (bit_xor plus) 1294 (simplify 1295 (op (convert1? (bit_and@4 @0 INTEGER_CST@1)) 1296 (convert2? (bit_and@5 @2 INTEGER_CST@3))) 1297 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) 1298 && tree_nop_conversion_p (type, TREE_TYPE (@2)) 1299 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0) 1300 (bit_ior (convert @4) (convert @5))))) 1301 1302/* (X | Y) ^ X -> Y & ~ X*/ 1303(simplify 1304 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0)) 1305 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 1306 (convert (bit_and @1 (bit_not @0))))) 1307 1308/* Convert ~X ^ ~Y to X ^ Y. */ 1309(simplify 1310 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1))) 1311 (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) 1312 && element_precision (type) <= element_precision (TREE_TYPE (@1))) 1313 (bit_xor (convert @0) (convert @1)))) 1314 1315/* Convert ~X ^ C to X ^ ~C. */ 1316(simplify 1317 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1) 1318 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 1319 (bit_xor (convert @0) (bit_not @1)))) 1320 1321/* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */ 1322(for opo (bit_and bit_xor) 1323 opi (bit_xor bit_and) 1324 (simplify 1325 (opo:c (opi:cs @0 @1) @1) 1326 (bit_and (bit_not @0) @1))) 1327 1328/* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both 1329 operands are another bit-wise operation with a common input. If so, 1330 distribute the bit operations to save an operation and possibly two if 1331 constants are involved. For example, convert 1332 (A | B) & (A | C) into A | (B & C) 1333 Further simplification will occur if B and C are constants. */ 1334(for op (bit_and bit_ior bit_xor) 1335 rop (bit_ior bit_and bit_and) 1336 (simplify 1337 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2))) 1338 (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) 1339 && tree_nop_conversion_p (type, TREE_TYPE (@2))) 1340 (rop (convert @0) (op (convert @1) (convert @2)))))) 1341 1342/* Some simple reassociation for bit operations, also handled in reassoc. */ 1343/* (X & Y) & Y -> X & Y 1344 (X | Y) | Y -> X | Y */ 1345(for op (bit_and bit_ior) 1346 (simplify 1347 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1)) 1348 @2)) 1349/* (X ^ Y) ^ Y -> X */ 1350(simplify 1351 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1)) 1352 (convert @0)) 1353/* (X & Y) & (X & Z) -> (X & Y) & Z 1354 (X | Y) | (X | Z) -> (X | Y) | Z */ 1355(for op (bit_and bit_ior) 1356 (simplify 1357 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2))) 1358 (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) 1359 && tree_nop_conversion_p (type, TREE_TYPE (@2))) 1360 (if (single_use (@5) && single_use (@6)) 1361 (op @3 (convert @2)) 1362 (if (single_use (@3) && single_use (@4)) 1363 (op (convert @1) @5)))))) 1364/* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */ 1365(simplify 1366 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2))) 1367 (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) 1368 && tree_nop_conversion_p (type, TREE_TYPE (@2))) 1369 (bit_xor (convert @1) (convert @2)))) 1370 1371/* Convert abs (abs (X)) into abs (X). 1372 also absu (absu (X)) into absu (X). */ 1373(simplify 1374 (abs (abs@1 @0)) 1375 @1) 1376 1377(simplify 1378 (absu (convert@2 (absu@1 @0))) 1379 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1))) 1380 @1)) 1381 1382/* Convert abs[u] (-X) -> abs[u] (X). */ 1383(simplify 1384 (abs (negate @0)) 1385 (abs @0)) 1386 1387(simplify 1388 (absu (negate @0)) 1389 (absu @0)) 1390 1391/* Convert abs[u] (X) where X is nonnegative -> (X). */ 1392(simplify 1393 (abs tree_expr_nonnegative_p@0) 1394 @0) 1395 1396(simplify 1397 (absu tree_expr_nonnegative_p@0) 1398 (convert @0)) 1399 1400/* Simplify (-(X < 0) | 1) * X into abs (X). */ 1401(simplify 1402 (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0) 1403 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type)) 1404 (abs @0))) 1405 1406/* Similarly (-(X < 0) | 1U) * X into absu (X). */ 1407(simplify 1408 (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop)))) 1409 integer_onep) (nop_convert @0)) 1410 (if (INTEGRAL_TYPE_P (type) 1411 && TYPE_UNSIGNED (type) 1412 && INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1413 && !TYPE_UNSIGNED (TREE_TYPE (@0))) 1414 (absu @0))) 1415 1416/* A few cases of fold-const.c negate_expr_p predicate. */ 1417(match negate_expr_p 1418 INTEGER_CST 1419 (if ((INTEGRAL_TYPE_P (type) 1420 && TYPE_UNSIGNED (type)) 1421 || (!TYPE_OVERFLOW_SANITIZED (type) 1422 && may_negate_without_overflow_p (t))))) 1423(match negate_expr_p 1424 FIXED_CST) 1425(match negate_expr_p 1426 (negate @0) 1427 (if (!TYPE_OVERFLOW_SANITIZED (type)))) 1428(match negate_expr_p 1429 REAL_CST 1430 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t))))) 1431/* VECTOR_CST handling of non-wrapping types would recurse in unsupported 1432 ways. */ 1433(match negate_expr_p 1434 VECTOR_CST 1435 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)))) 1436(match negate_expr_p 1437 (minus @0 @1) 1438 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)) 1439 || (FLOAT_TYPE_P (type) 1440 && !HONOR_SIGN_DEPENDENT_ROUNDING (type) 1441 && !HONOR_SIGNED_ZEROS (type))))) 1442 1443/* (-A) * (-B) -> A * B */ 1444(simplify 1445 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1)) 1446 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) 1447 && tree_nop_conversion_p (type, TREE_TYPE (@1))) 1448 (mult (convert @0) (convert (negate @1))))) 1449 1450/* -(A + B) -> (-B) - A. */ 1451(simplify 1452 (negate (plus:c @0 negate_expr_p@1)) 1453 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) 1454 && !HONOR_SIGNED_ZEROS (element_mode (type))) 1455 (minus (negate @1) @0))) 1456 1457/* -(A - B) -> B - A. */ 1458(simplify 1459 (negate (minus @0 @1)) 1460 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type)) 1461 || (FLOAT_TYPE_P (type) 1462 && !HONOR_SIGN_DEPENDENT_ROUNDING (type) 1463 && !HONOR_SIGNED_ZEROS (type))) 1464 (minus @1 @0))) 1465(simplify 1466 (negate (pointer_diff @0 @1)) 1467 (if (TYPE_OVERFLOW_UNDEFINED (type)) 1468 (pointer_diff @1 @0))) 1469 1470/* A - B -> A + (-B) if B is easily negatable. */ 1471(simplify 1472 (minus @0 negate_expr_p@1) 1473 (if (!FIXED_POINT_TYPE_P (type)) 1474 (plus @0 (negate @1)))) 1475 1476/* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) 1477 when profitable. 1478 For bitwise binary operations apply operand conversions to the 1479 binary operation result instead of to the operands. This allows 1480 to combine successive conversions and bitwise binary operations. 1481 We combine the above two cases by using a conditional convert. */ 1482(for bitop (bit_and bit_ior bit_xor) 1483 (simplify 1484 (bitop (convert@2 @0) (convert?@3 @1)) 1485 (if (((TREE_CODE (@1) == INTEGER_CST 1486 && INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1487 && int_fits_type_p (@1, TREE_TYPE (@0))) 1488 || types_match (@0, @1)) 1489 /* ??? This transform conflicts with fold-const.c doing 1490 Convert (T)(x & c) into (T)x & (T)c, if c is an integer 1491 constants (if x has signed type, the sign bit cannot be set 1492 in c). This folds extension into the BIT_AND_EXPR. 1493 Restrict it to GIMPLE to avoid endless recursions. */ 1494 && (bitop != BIT_AND_EXPR || GIMPLE) 1495 && (/* That's a good idea if the conversion widens the operand, thus 1496 after hoisting the conversion the operation will be narrower. */ 1497 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type) 1498 /* It's also a good idea if the conversion is to a non-integer 1499 mode. */ 1500 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT 1501 /* Or if the precision of TO is not the same as the precision 1502 of its mode. */ 1503 || !type_has_mode_precision_p (type) 1504 /* In GIMPLE, getting rid of 2 conversions for one new results 1505 in smaller IL. */ 1506 || (GIMPLE 1507 && TREE_CODE (@1) != INTEGER_CST 1508 && tree_nop_conversion_p (type, TREE_TYPE (@0)) 1509 && single_use (@2) 1510 && single_use (@3)))) 1511 (convert (bitop @0 (convert @1))))) 1512 /* In GIMPLE, getting rid of 2 conversions for one new results 1513 in smaller IL. */ 1514 (simplify 1515 (convert (bitop:cs@2 (nop_convert:s @0) @1)) 1516 (if (GIMPLE 1517 && TREE_CODE (@1) != INTEGER_CST 1518 && tree_nop_conversion_p (type, TREE_TYPE (@2)) 1519 && types_match (type, @0)) 1520 (bitop @0 (convert @1))))) 1521 1522(for bitop (bit_and bit_ior) 1523 rbitop (bit_ior bit_and) 1524 /* (x | y) & x -> x */ 1525 /* (x & y) | x -> x */ 1526 (simplify 1527 (bitop:c (rbitop:c @0 @1) @0) 1528 @0) 1529 /* (~x | y) & x -> x & y */ 1530 /* (~x & y) | x -> x | y */ 1531 (simplify 1532 (bitop:c (rbitop:c (bit_not @0) @1) @0) 1533 (bitop @0 @1))) 1534 1535/* ((x | y) & z) | x -> (z & y) | x */ 1536(simplify 1537 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0) 1538 (bit_ior (bit_and @2 @1) @0)) 1539 1540/* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ 1541(simplify 1542 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) 1543 (bit_ior (bit_and @0 @2) (bit_and @1 @2))) 1544 1545/* Combine successive equal operations with constants. */ 1546(for bitop (bit_and bit_ior bit_xor) 1547 (simplify 1548 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) 1549 (if (!CONSTANT_CLASS_P (@0)) 1550 /* This is the canonical form regardless of whether (bitop @1 @2) can be 1551 folded to a constant. */ 1552 (bitop @0 (bitop @1 @2)) 1553 /* In this case we have three constants and (bitop @0 @1) doesn't fold 1554 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if 1555 the values involved are such that the operation can't be decided at 1556 compile time. Try folding one of @0 or @1 with @2 to see whether 1557 that combination can be decided at compile time. 1558 1559 Keep the existing form if both folds fail, to avoid endless 1560 oscillation. */ 1561 (with { tree cst1 = const_binop (bitop, type, @0, @2); } 1562 (if (cst1) 1563 (bitop @1 { cst1; }) 1564 (with { tree cst2 = const_binop (bitop, type, @1, @2); } 1565 (if (cst2) 1566 (bitop @0 { cst2; })))))))) 1567 1568/* Try simple folding for X op !X, and X op X with the help 1569 of the truth_valued_p and logical_inverted_value predicates. */ 1570(match truth_valued_p 1571 @0 1572 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))) 1573(for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor) 1574 (match truth_valued_p 1575 (op @0 @1))) 1576(match truth_valued_p 1577 (truth_not @0)) 1578 1579(match (logical_inverted_value @0) 1580 (truth_not @0)) 1581(match (logical_inverted_value @0) 1582 (bit_not truth_valued_p@0)) 1583(match (logical_inverted_value @0) 1584 (eq @0 integer_zerop)) 1585(match (logical_inverted_value @0) 1586 (ne truth_valued_p@0 integer_truep)) 1587(match (logical_inverted_value @0) 1588 (bit_xor truth_valued_p@0 integer_truep)) 1589 1590/* X & !X -> 0. */ 1591(simplify 1592 (bit_and:c @0 (logical_inverted_value @0)) 1593 { build_zero_cst (type); }) 1594/* X | !X and X ^ !X -> 1, , if X is truth-valued. */ 1595(for op (bit_ior bit_xor) 1596 (simplify 1597 (op:c truth_valued_p@0 (logical_inverted_value @0)) 1598 { constant_boolean_node (true, type); })) 1599/* X ==/!= !X is false/true. */ 1600(for op (eq ne) 1601 (simplify 1602 (op:c truth_valued_p@0 (logical_inverted_value @0)) 1603 { constant_boolean_node (op == NE_EXPR ? true : false, type); })) 1604 1605/* ~~x -> x */ 1606(simplify 1607 (bit_not (bit_not @0)) 1608 @0) 1609 1610/* Convert ~ (-A) to A - 1. */ 1611(simplify 1612 (bit_not (convert? (negate @0))) 1613 (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) 1614 || !TYPE_UNSIGNED (TREE_TYPE (@0))) 1615 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); })))) 1616 1617/* Convert - (~A) to A + 1. */ 1618(simplify 1619 (negate (nop_convert? (bit_not @0))) 1620 (plus (view_convert @0) { build_each_one_cst (type); })) 1621 1622/* Convert ~ (A - 1) or ~ (A + -1) to -A. */ 1623(simplify 1624 (bit_not (convert? (minus @0 integer_each_onep))) 1625 (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) 1626 || !TYPE_UNSIGNED (TREE_TYPE (@0))) 1627 (convert (negate @0)))) 1628(simplify 1629 (bit_not (convert? (plus @0 integer_all_onesp))) 1630 (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) 1631 || !TYPE_UNSIGNED (TREE_TYPE (@0))) 1632 (convert (negate @0)))) 1633 1634/* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ 1635(simplify 1636 (bit_not (convert? (bit_xor @0 INTEGER_CST@1))) 1637 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 1638 (convert (bit_xor @0 (bit_not @1))))) 1639(simplify 1640 (bit_not (convert? (bit_xor:c (bit_not @0) @1))) 1641 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 1642 (convert (bit_xor @0 @1)))) 1643 1644/* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */ 1645(simplify 1646 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1) 1647 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 1648 (bit_not (bit_xor (view_convert @0) @1)))) 1649 1650/* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ 1651(simplify 1652 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2)) 1653 (bit_xor (bit_and (bit_xor @0 @1) @2) @0)) 1654 1655/* Fold A - (A & B) into ~B & A. */ 1656(simplify 1657 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1))) 1658 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) 1659 && tree_nop_conversion_p (type, TREE_TYPE (@1))) 1660 (convert (bit_and (bit_not @1) @0)))) 1661 1662/* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */ 1663(for cmp (gt lt ge le) 1664(simplify 1665 (mult (convert (cmp @0 @1)) @2) 1666 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2)) 1667 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))) 1668 1669/* For integral types with undefined overflow and C != 0 fold 1670 x * C EQ/NE y * C into x EQ/NE y. */ 1671(for cmp (eq ne) 1672 (simplify 1673 (cmp (mult:c @0 @1) (mult:c @2 @1)) 1674 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) 1675 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 1676 && tree_expr_nonzero_p (@1)) 1677 (cmp @0 @2)))) 1678 1679/* For integral types with wrapping overflow and C odd fold 1680 x * C EQ/NE y * C into x EQ/NE y. */ 1681(for cmp (eq ne) 1682 (simplify 1683 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1)) 1684 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) 1685 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) 1686 && (TREE_INT_CST_LOW (@1) & 1) != 0) 1687 (cmp @0 @2)))) 1688 1689/* For integral types with undefined overflow and C != 0 fold 1690 x * C RELOP y * C into: 1691 1692 x RELOP y for nonnegative C 1693 y RELOP x for negative C */ 1694(for cmp (lt gt le ge) 1695 (simplify 1696 (cmp (mult:c @0 @1) (mult:c @2 @1)) 1697 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) 1698 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) 1699 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1)) 1700 (cmp @0 @2) 1701 (if (TREE_CODE (@1) == INTEGER_CST 1702 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1)))) 1703 (cmp @2 @0)))))) 1704 1705/* (X - 1U) <= INT_MAX-1U into (int) X > 0. */ 1706(for cmp (le gt) 1707 icmp (gt le) 1708 (simplify 1709 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2) 1710 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1711 && TYPE_UNSIGNED (TREE_TYPE (@0)) 1712 && TYPE_PRECISION (TREE_TYPE (@0)) > 1 1713 && (wi::to_wide (@2) 1714 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1)) 1715 (with { tree stype = signed_type_for (TREE_TYPE (@0)); } 1716 (icmp (convert:stype @0) { build_int_cst (stype, 0); }))))) 1717 1718/* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */ 1719(for cmp (simple_comparison) 1720 (simplify 1721 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2))) 1722 (if (element_precision (@3) >= element_precision (@0) 1723 && types_match (@0, @1)) 1724 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))) 1725 (if (!TYPE_UNSIGNED (TREE_TYPE (@3))) 1726 (cmp @1 @0) 1727 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1)) 1728 (with 1729 { 1730 tree utype = unsigned_type_for (TREE_TYPE (@0)); 1731 } 1732 (cmp (convert:utype @1) (convert:utype @0))))) 1733 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2)))) 1734 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3))) 1735 (cmp @0 @1) 1736 (with 1737 { 1738 tree utype = unsigned_type_for (TREE_TYPE (@0)); 1739 } 1740 (cmp (convert:utype @0) (convert:utype @1))))))))) 1741 1742/* X / C1 op C2 into a simple range test. */ 1743(for cmp (simple_comparison) 1744 (simplify 1745 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2) 1746 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1747 && integer_nonzerop (@1) 1748 && !TREE_OVERFLOW (@1) 1749 && !TREE_OVERFLOW (@2)) 1750 (with { tree lo, hi; bool neg_overflow; 1751 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi, 1752 &neg_overflow); } 1753 (switch 1754 (if (code == LT_EXPR || code == GE_EXPR) 1755 (if (TREE_OVERFLOW (lo)) 1756 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); } 1757 (if (code == LT_EXPR) 1758 (lt @0 { lo; }) 1759 (ge @0 { lo; })))) 1760 (if (code == LE_EXPR || code == GT_EXPR) 1761 (if (TREE_OVERFLOW (hi)) 1762 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); } 1763 (if (code == LE_EXPR) 1764 (le @0 { hi; }) 1765 (gt @0 { hi; })))) 1766 (if (!lo && !hi) 1767 { build_int_cst (type, code == NE_EXPR); }) 1768 (if (code == EQ_EXPR && !hi) 1769 (ge @0 { lo; })) 1770 (if (code == EQ_EXPR && !lo) 1771 (le @0 { hi; })) 1772 (if (code == NE_EXPR && !hi) 1773 (lt @0 { lo; })) 1774 (if (code == NE_EXPR && !lo) 1775 (gt @0 { hi; })) 1776 (if (GENERIC) 1777 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR, 1778 lo, hi); }) 1779 (with 1780 { 1781 tree etype = range_check_type (TREE_TYPE (@0)); 1782 if (etype) 1783 { 1784 hi = fold_convert (etype, hi); 1785 lo = fold_convert (etype, lo); 1786 hi = const_binop (MINUS_EXPR, etype, hi, lo); 1787 } 1788 } 1789 (if (etype && hi && !TREE_OVERFLOW (hi)) 1790 (if (code == EQ_EXPR) 1791 (le (minus (convert:etype @0) { lo; }) { hi; }) 1792 (gt (minus (convert:etype @0) { lo; }) { hi; }))))))))) 1793 1794/* X + Z < Y + Z is the same as X < Y when there is no overflow. */ 1795(for op (lt le ge gt) 1796 (simplify 1797 (op (plus:c @0 @2) (plus:c @1 @2)) 1798 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1799 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) 1800 (op @0 @1)))) 1801/* For equality and subtraction, this is also true with wrapping overflow. */ 1802(for op (eq ne minus) 1803 (simplify 1804 (op (plus:c @0 @2) (plus:c @1 @2)) 1805 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1806 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 1807 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) 1808 (op @0 @1)))) 1809 1810/* X - Z < Y - Z is the same as X < Y when there is no overflow. */ 1811(for op (lt le ge gt) 1812 (simplify 1813 (op (minus @0 @2) (minus @1 @2)) 1814 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1815 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) 1816 (op @0 @1)))) 1817/* For equality and subtraction, this is also true with wrapping overflow. */ 1818(for op (eq ne minus) 1819 (simplify 1820 (op (minus @0 @2) (minus @1 @2)) 1821 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1822 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 1823 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) 1824 (op @0 @1)))) 1825/* And for pointers... */ 1826(for op (simple_comparison) 1827 (simplify 1828 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2)) 1829 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) 1830 (op @0 @1)))) 1831(simplify 1832 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2)) 1833 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3)) 1834 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) 1835 (pointer_diff @0 @1))) 1836 1837/* Z - X < Z - Y is the same as Y < X when there is no overflow. */ 1838(for op (lt le ge gt) 1839 (simplify 1840 (op (minus @2 @0) (minus @2 @1)) 1841 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1842 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) 1843 (op @1 @0)))) 1844/* For equality and subtraction, this is also true with wrapping overflow. */ 1845(for op (eq ne minus) 1846 (simplify 1847 (op (minus @2 @0) (minus @2 @1)) 1848 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1849 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 1850 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) 1851 (op @1 @0)))) 1852/* And for pointers... */ 1853(for op (simple_comparison) 1854 (simplify 1855 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1)) 1856 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) 1857 (op @1 @0)))) 1858(simplify 1859 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1)) 1860 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3)) 1861 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) 1862 (pointer_diff @1 @0))) 1863 1864/* X + Y < Y is the same as X < 0 when there is no overflow. */ 1865(for op (lt le gt ge) 1866 (simplify 1867 (op:c (plus:c@2 @0 @1) @1) 1868 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1869 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 1870 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) 1871 && (CONSTANT_CLASS_P (@0) || single_use (@2))) 1872 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))) 1873/* For equality, this is also true with wrapping overflow. */ 1874(for op (eq ne) 1875 (simplify 1876 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1)) 1877 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1878 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 1879 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) 1880 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3))) 1881 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2)) 1882 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))) 1883 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))) 1884 (simplify 1885 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0)) 1886 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)) 1887 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) 1888 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3)))) 1889 (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) 1890 1891/* X - Y < X is the same as Y > 0 when there is no overflow. 1892 For equality, this is also true with wrapping overflow. */ 1893(for op (simple_comparison) 1894 (simplify 1895 (op:c @0 (minus@2 @0 @1)) 1896 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 1897 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 1898 || ((op == EQ_EXPR || op == NE_EXPR) 1899 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) 1900 && (CONSTANT_CLASS_P (@1) || single_use (@2))) 1901 (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) 1902 1903/* Transform: 1904 (X / Y) == 0 -> X < Y if X, Y are unsigned. 1905 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */ 1906(for cmp (eq ne) 1907 ocmp (lt ge) 1908 (simplify 1909 (cmp (trunc_div @0 @1) integer_zerop) 1910 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) 1911 /* Complex ==/!= is allowed, but not </>=. */ 1912 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE 1913 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0)))) 1914 (ocmp @0 @1)))) 1915 1916/* X == C - X can never be true if C is odd. */ 1917(for cmp (eq ne) 1918 (simplify 1919 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0)))) 1920 (if (TREE_INT_CST_LOW (@1) & 1) 1921 { constant_boolean_node (cmp == NE_EXPR, type); }))) 1922 1923/* Arguments on which one can call get_nonzero_bits to get the bits 1924 possibly set. */ 1925(match with_possible_nonzero_bits 1926 INTEGER_CST@0) 1927(match with_possible_nonzero_bits 1928 SSA_NAME@0 1929 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))))) 1930/* Slightly extended version, do not make it recursive to keep it cheap. */ 1931(match (with_possible_nonzero_bits2 @0) 1932 with_possible_nonzero_bits@0) 1933(match (with_possible_nonzero_bits2 @0) 1934 (bit_and:c with_possible_nonzero_bits@0 @2)) 1935 1936/* Same for bits that are known to be set, but we do not have 1937 an equivalent to get_nonzero_bits yet. */ 1938(match (with_certain_nonzero_bits2 @0) 1939 INTEGER_CST@0) 1940(match (with_certain_nonzero_bits2 @0) 1941 (bit_ior @1 INTEGER_CST@0)) 1942 1943/* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */ 1944(for cmp (eq ne) 1945 (simplify 1946 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1)) 1947 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0) 1948 { constant_boolean_node (cmp == NE_EXPR, type); }))) 1949 1950/* ((X inner_op C0) outer_op C1) 1951 With X being a tree where value_range has reasoned certain bits to always be 1952 zero throughout its computed value range, 1953 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op 1954 where zero_mask has 1's for all bits that are sure to be 0 in 1955 and 0's otherwise. 1956 if (inner_op == '^') C0 &= ~C1; 1957 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1) 1958 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1) 1959*/ 1960(for inner_op (bit_ior bit_xor) 1961 outer_op (bit_xor bit_ior) 1962(simplify 1963 (outer_op 1964 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1) 1965 (with 1966 { 1967 bool fail = false; 1968 wide_int zero_mask_not; 1969 wide_int C0; 1970 wide_int cst_emit; 1971 1972 if (TREE_CODE (@2) == SSA_NAME) 1973 zero_mask_not = get_nonzero_bits (@2); 1974 else 1975 fail = true; 1976 1977 if (inner_op == BIT_XOR_EXPR) 1978 { 1979 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1)); 1980 cst_emit = C0 | wi::to_wide (@1); 1981 } 1982 else 1983 { 1984 C0 = wi::to_wide (@0); 1985 cst_emit = C0 ^ wi::to_wide (@1); 1986 } 1987 } 1988 (if (!fail && (C0 & zero_mask_not) == 0) 1989 (outer_op @2 { wide_int_to_tree (type, cst_emit); }) 1990 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0) 1991 (inner_op @2 { wide_int_to_tree (type, cst_emit); })))))) 1992 1993/* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */ 1994(simplify 1995 (pointer_plus (pointer_plus:s @0 @1) @3) 1996 (pointer_plus @0 (plus @1 @3))) 1997 1998/* Pattern match 1999 tem1 = (long) ptr1; 2000 tem2 = (long) ptr2; 2001 tem3 = tem2 - tem1; 2002 tem4 = (unsigned long) tem3; 2003 tem5 = ptr1 + tem4; 2004 and produce 2005 tem5 = ptr2; */ 2006(simplify 2007 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0)))) 2008 /* Conditionally look through a sign-changing conversion. */ 2009 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3)) 2010 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1))) 2011 || (GENERIC && type == TREE_TYPE (@1)))) 2012 @1)) 2013(simplify 2014 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0))) 2015 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3))) 2016 (convert @1))) 2017 2018/* Pattern match 2019 tem = (sizetype) ptr; 2020 tem = tem & algn; 2021 tem = -tem; 2022 ... = ptr p+ tem; 2023 and produce the simpler and easier to analyze with respect to alignment 2024 ... = ptr & ~algn; */ 2025(simplify 2026 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1))) 2027 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); } 2028 (bit_and @0 { algn; }))) 2029 2030/* Try folding difference of addresses. */ 2031(simplify 2032 (minus (convert ADDR_EXPR@0) (convert @1)) 2033 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 2034 (with { poly_int64 diff; } 2035 (if (ptr_difference_const (@0, @1, &diff)) 2036 { build_int_cst_type (type, diff); })))) 2037(simplify 2038 (minus (convert @0) (convert ADDR_EXPR@1)) 2039 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 2040 (with { poly_int64 diff; } 2041 (if (ptr_difference_const (@0, @1, &diff)) 2042 { build_int_cst_type (type, diff); })))) 2043(simplify 2044 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1)) 2045 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0)) 2046 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1))) 2047 (with { poly_int64 diff; } 2048 (if (ptr_difference_const (@0, @1, &diff)) 2049 { build_int_cst_type (type, diff); })))) 2050(simplify 2051 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1)) 2052 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0)) 2053 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1))) 2054 (with { poly_int64 diff; } 2055 (if (ptr_difference_const (@0, @1, &diff)) 2056 { build_int_cst_type (type, diff); })))) 2057 2058/* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */ 2059(simplify 2060 (convert (pointer_diff @0 INTEGER_CST@1)) 2061 (if (POINTER_TYPE_P (type)) 2062 { build_fold_addr_expr_with_type 2063 (build2 (MEM_REF, char_type_node, @0, 2064 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))), 2065 type); })) 2066 2067/* If arg0 is derived from the address of an object or function, we may 2068 be able to fold this expression using the object or function's 2069 alignment. */ 2070(simplify 2071 (bit_and (convert? @0) INTEGER_CST@1) 2072 (if (POINTER_TYPE_P (TREE_TYPE (@0)) 2073 && tree_nop_conversion_p (type, TREE_TYPE (@0))) 2074 (with 2075 { 2076 unsigned int align; 2077 unsigned HOST_WIDE_INT bitpos; 2078 get_pointer_alignment_1 (@0, &align, &bitpos); 2079 } 2080 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT)) 2081 { wide_int_to_tree (type, (wi::to_wide (@1) 2082 & (bitpos / BITS_PER_UNIT))); })))) 2083 2084(match min_value 2085 INTEGER_CST 2086 (if (INTEGRAL_TYPE_P (type) 2087 && wi::eq_p (wi::to_wide (t), wi::min_value (type))))) 2088 2089(match max_value 2090 INTEGER_CST 2091 (if (INTEGRAL_TYPE_P (type) 2092 && wi::eq_p (wi::to_wide (t), wi::max_value (type))))) 2093 2094/* x > y && x != XXX_MIN --> x > y 2095 x > y && x == XXX_MIN --> false . */ 2096(for eqne (eq ne) 2097 (simplify 2098 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value)) 2099 (switch 2100 (if (eqne == EQ_EXPR) 2101 { constant_boolean_node (false, type); }) 2102 (if (eqne == NE_EXPR) 2103 @2) 2104 ))) 2105 2106/* x < y && x != XXX_MAX --> x < y 2107 x < y && x == XXX_MAX --> false. */ 2108(for eqne (eq ne) 2109 (simplify 2110 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value)) 2111 (switch 2112 (if (eqne == EQ_EXPR) 2113 { constant_boolean_node (false, type); }) 2114 (if (eqne == NE_EXPR) 2115 @2) 2116 ))) 2117 2118/* x <= y && x == XXX_MIN --> x == XXX_MIN. */ 2119(simplify 2120 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value)) 2121 @2) 2122 2123/* x >= y && x == XXX_MAX --> x == XXX_MAX. */ 2124(simplify 2125 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value)) 2126 @2) 2127 2128/* x > y || x != XXX_MIN --> x != XXX_MIN. */ 2129(simplify 2130 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value)) 2131 @2) 2132 2133/* x <= y || x != XXX_MIN --> true. */ 2134(simplify 2135 (bit_ior:c (le:c @0 @1) (ne @0 min_value)) 2136 { constant_boolean_node (true, type); }) 2137 2138/* x <= y || x == XXX_MIN --> x <= y. */ 2139(simplify 2140 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value)) 2141 @2) 2142 2143/* x < y || x != XXX_MAX --> x != XXX_MAX. */ 2144(simplify 2145 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value)) 2146 @2) 2147 2148/* x >= y || x != XXX_MAX --> true 2149 x >= y || x == XXX_MAX --> x >= y. */ 2150(for eqne (eq ne) 2151 (simplify 2152 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value)) 2153 (switch 2154 (if (eqne == EQ_EXPR) 2155 @2) 2156 (if (eqne == NE_EXPR) 2157 { constant_boolean_node (true, type); })))) 2158 2159/* y == XXX_MIN || x < y --> x <= y - 1 */ 2160(simplify 2161 (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1)) 2162 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) 2163 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) 2164 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))) 2165 2166/* y != XXX_MIN && x >= y --> x > y - 1 */ 2167(simplify 2168 (bit_and:c (ne:s @1 min_value) (ge:s @0 @1)) 2169 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) 2170 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) 2171 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))) 2172 2173/* Convert (X == CST1) && (X OP2 CST2) to a known value 2174 based on CST1 OP2 CST2. Similarly for (X != CST1). */ 2175 2176(for code1 (eq ne) 2177 (for code2 (eq ne lt gt le ge) 2178 (simplify 2179 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2)) 2180 (with 2181 { 2182 int cmp = tree_int_cst_compare (@1, @2); 2183 bool val; 2184 switch (code2) 2185 { 2186 case EQ_EXPR: val = (cmp == 0); break; 2187 case NE_EXPR: val = (cmp != 0); break; 2188 case LT_EXPR: val = (cmp < 0); break; 2189 case GT_EXPR: val = (cmp > 0); break; 2190 case LE_EXPR: val = (cmp <= 0); break; 2191 case GE_EXPR: val = (cmp >= 0); break; 2192 default: gcc_unreachable (); 2193 } 2194 } 2195 (switch 2196 (if (code1 == EQ_EXPR && val) @3) 2197 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); }) 2198 (if (code1 == NE_EXPR && !val) @4)))))) 2199 2200/* Convert (X OP1 CST1) && (X OP2 CST2). */ 2201 2202(for code1 (lt le gt ge) 2203 (for code2 (lt le gt ge) 2204 (simplify 2205 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2)) 2206 (with 2207 { 2208 int cmp = tree_int_cst_compare (@1, @2); 2209 } 2210 (switch 2211 /* Choose the more restrictive of two < or <= comparisons. */ 2212 (if ((code1 == LT_EXPR || code1 == LE_EXPR) 2213 && (code2 == LT_EXPR || code2 == LE_EXPR)) 2214 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) 2215 @3 2216 @4)) 2217 /* Likewise chose the more restrictive of two > or >= comparisons. */ 2218 (if ((code1 == GT_EXPR || code1 == GE_EXPR) 2219 && (code2 == GT_EXPR || code2 == GE_EXPR)) 2220 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) 2221 @3 2222 @4)) 2223 /* Check for singleton ranges. */ 2224 (if (cmp == 0 2225 && ((code1 == LE_EXPR && code2 == GE_EXPR) 2226 || (code1 == GE_EXPR && code2 == LE_EXPR))) 2227 (eq @0 @1)) 2228 /* Check for disjoint ranges. */ 2229 (if (cmp <= 0 2230 && (code1 == LT_EXPR || code1 == LE_EXPR) 2231 && (code2 == GT_EXPR || code2 == GE_EXPR)) 2232 { constant_boolean_node (false, type); }) 2233 (if (cmp >= 0 2234 && (code1 == GT_EXPR || code1 == GE_EXPR) 2235 && (code2 == LT_EXPR || code2 == LE_EXPR)) 2236 { constant_boolean_node (false, type); }) 2237 ))))) 2238 2239/* Convert (X == CST1) || (X OP2 CST2) to a known value 2240 based on CST1 OP2 CST2. Similarly for (X != CST1). */ 2241 2242(for code1 (eq ne) 2243 (for code2 (eq ne lt gt le ge) 2244 (simplify 2245 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2)) 2246 (with 2247 { 2248 int cmp = tree_int_cst_compare (@1, @2); 2249 bool val; 2250 switch (code2) 2251 { 2252 case EQ_EXPR: val = (cmp == 0); break; 2253 case NE_EXPR: val = (cmp != 0); break; 2254 case LT_EXPR: val = (cmp < 0); break; 2255 case GT_EXPR: val = (cmp > 0); break; 2256 case LE_EXPR: val = (cmp <= 0); break; 2257 case GE_EXPR: val = (cmp >= 0); break; 2258 default: gcc_unreachable (); 2259 } 2260 } 2261 (switch 2262 (if (code1 == EQ_EXPR && val) @4) 2263 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); }) 2264 (if (code1 == NE_EXPR && !val) @3)))))) 2265 2266/* Convert (X OP1 CST1) || (X OP2 CST2). */ 2267 2268(for code1 (lt le gt ge) 2269 (for code2 (lt le gt ge) 2270 (simplify 2271 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2)) 2272 (with 2273 { 2274 int cmp = tree_int_cst_compare (@1, @2); 2275 } 2276 (switch 2277 /* Choose the more restrictive of two < or <= comparisons. */ 2278 (if ((code1 == LT_EXPR || code1 == LE_EXPR) 2279 && (code2 == LT_EXPR || code2 == LE_EXPR)) 2280 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) 2281 @4 2282 @3)) 2283 /* Likewise chose the more restrictive of two > or >= comparisons. */ 2284 (if ((code1 == GT_EXPR || code1 == GE_EXPR) 2285 && (code2 == GT_EXPR || code2 == GE_EXPR)) 2286 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) 2287 @4 2288 @3)) 2289 /* Check for singleton ranges. */ 2290 (if (cmp == 0 2291 && ((code1 == LT_EXPR && code2 == GT_EXPR) 2292 || (code1 == GT_EXPR && code2 == LT_EXPR))) 2293 (ne @0 @2)) 2294 /* Check for disjoint ranges. */ 2295 (if (cmp >= 0 2296 && (code1 == LT_EXPR || code1 == LE_EXPR) 2297 && (code2 == GT_EXPR || code2 == GE_EXPR)) 2298 { constant_boolean_node (true, type); }) 2299 (if (cmp <= 0 2300 && (code1 == GT_EXPR || code1 == GE_EXPR) 2301 && (code2 == LT_EXPR || code2 == LE_EXPR)) 2302 { constant_boolean_node (true, type); }) 2303 ))))) 2304 2305/* We can't reassociate at all for saturating types. */ 2306(if (!TYPE_SATURATING (type)) 2307 2308 /* Contract negates. */ 2309 /* A + (-B) -> A - B */ 2310 (simplify 2311 (plus:c @0 (convert? (negate @1))) 2312 /* Apply STRIP_NOPS on the negate. */ 2313 (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) 2314 && !TYPE_OVERFLOW_SANITIZED (type)) 2315 (with 2316 { 2317 tree t1 = type; 2318 if (INTEGRAL_TYPE_P (type) 2319 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) 2320 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1); 2321 } 2322 (convert (minus (convert:t1 @0) (convert:t1 @1)))))) 2323 /* A - (-B) -> A + B */ 2324 (simplify 2325 (minus @0 (convert? (negate @1))) 2326 (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) 2327 && !TYPE_OVERFLOW_SANITIZED (type)) 2328 (with 2329 { 2330 tree t1 = type; 2331 if (INTEGRAL_TYPE_P (type) 2332 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) 2333 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1); 2334 } 2335 (convert (plus (convert:t1 @0) (convert:t1 @1)))))) 2336 /* -(T)(-A) -> (T)A 2337 Sign-extension is ok except for INT_MIN, which thankfully cannot 2338 happen without overflow. */ 2339 (simplify 2340 (negate (convert (negate @1))) 2341 (if (INTEGRAL_TYPE_P (type) 2342 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1)) 2343 || (!TYPE_UNSIGNED (TREE_TYPE (@1)) 2344 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) 2345 && !TYPE_OVERFLOW_SANITIZED (type) 2346 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))) 2347 (convert @1))) 2348 (simplify 2349 (negate (convert negate_expr_p@1)) 2350 (if (SCALAR_FLOAT_TYPE_P (type) 2351 && ((DECIMAL_FLOAT_TYPE_P (type) 2352 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)) 2353 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1))) 2354 || !HONOR_SIGN_DEPENDENT_ROUNDING (type))) 2355 (convert (negate @1)))) 2356 (simplify 2357 (negate (nop_convert? (negate @1))) 2358 (if (!TYPE_OVERFLOW_SANITIZED (type) 2359 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))) 2360 (view_convert @1))) 2361 2362 /* We can't reassociate floating-point unless -fassociative-math 2363 or fixed-point plus or minus because of saturation to +-Inf. */ 2364 (if ((!FLOAT_TYPE_P (type) || flag_associative_math) 2365 && !FIXED_POINT_TYPE_P (type)) 2366 2367 /* Match patterns that allow contracting a plus-minus pair 2368 irrespective of overflow issues. */ 2369 /* (A +- B) - A -> +- B */ 2370 /* (A +- B) -+ B -> A */ 2371 /* A - (A +- B) -> -+ B */ 2372 /* A +- (B -+ A) -> +- B */ 2373 (simplify 2374 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0) 2375 (view_convert @1)) 2376 (simplify 2377 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0) 2378 (if (!ANY_INTEGRAL_TYPE_P (type) 2379 || TYPE_OVERFLOW_WRAPS (type)) 2380 (negate (view_convert @1)) 2381 (view_convert (negate @1)))) 2382 (simplify 2383 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1) 2384 (view_convert @0)) 2385 (simplify 2386 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1))) 2387 (if (!ANY_INTEGRAL_TYPE_P (type) 2388 || TYPE_OVERFLOW_WRAPS (type)) 2389 (negate (view_convert @1)) 2390 (view_convert (negate @1)))) 2391 (simplify 2392 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1))) 2393 (view_convert @1)) 2394 /* (A +- B) + (C - A) -> C +- B */ 2395 /* (A + B) - (A - C) -> B + C */ 2396 /* More cases are handled with comparisons. */ 2397 (simplify 2398 (plus:c (plus:c @0 @1) (minus @2 @0)) 2399 (plus @2 @1)) 2400 (simplify 2401 (plus:c (minus @0 @1) (minus @2 @0)) 2402 (minus @2 @1)) 2403 (simplify 2404 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0)) 2405 (if (TYPE_OVERFLOW_UNDEFINED (type) 2406 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))) 2407 (pointer_diff @2 @1))) 2408 (simplify 2409 (minus (plus:c @0 @1) (minus @0 @2)) 2410 (plus @1 @2)) 2411 2412 /* (A +- CST1) +- CST2 -> A + CST3 2413 Use view_convert because it is safe for vectors and equivalent for 2414 scalars. */ 2415 (for outer_op (plus minus) 2416 (for inner_op (plus minus) 2417 neg_inner_op (minus plus) 2418 (simplify 2419 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1)) 2420 CONSTANT_CLASS_P@2) 2421 /* If one of the types wraps, use that one. */ 2422 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) 2423 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse 2424 forever if something doesn't simplify into a constant. */ 2425 (if (!CONSTANT_CLASS_P (@0)) 2426 (if (outer_op == PLUS_EXPR) 2427 (plus (view_convert @0) (inner_op @2 (view_convert @1))) 2428 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))) 2429 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 2430 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) 2431 (if (outer_op == PLUS_EXPR) 2432 (view_convert (plus @0 (inner_op (view_convert @2) @1))) 2433 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1)))) 2434 /* If the constant operation overflows we cannot do the transform 2435 directly as we would introduce undefined overflow, for example 2436 with (a - 1) + INT_MIN. */ 2437 (if (types_match (type, @0)) 2438 (with { tree cst = const_binop (outer_op == inner_op 2439 ? PLUS_EXPR : MINUS_EXPR, 2440 type, @1, @2); } 2441 (if (cst && !TREE_OVERFLOW (cst)) 2442 (inner_op @0 { cst; } ) 2443 /* X+INT_MAX+1 is X-INT_MIN. */ 2444 (if (INTEGRAL_TYPE_P (type) && cst 2445 && wi::to_wide (cst) == wi::min_value (type)) 2446 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); }) 2447 /* Last resort, use some unsigned type. */ 2448 (with { tree utype = unsigned_type_for (type); } 2449 (if (utype) 2450 (view_convert (inner_op 2451 (view_convert:utype @0) 2452 (view_convert:utype 2453 { drop_tree_overflow (cst); })))))))))))))) 2454 2455 /* (CST1 - A) +- CST2 -> CST3 - A */ 2456 (for outer_op (plus minus) 2457 (simplify 2458 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2) 2459 /* If one of the types wraps, use that one. */ 2460 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) 2461 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse 2462 forever if something doesn't simplify into a constant. */ 2463 (if (!CONSTANT_CLASS_P (@0)) 2464 (minus (outer_op (view_convert @1) @2) (view_convert @0))) 2465 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 2466 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) 2467 (view_convert (minus (outer_op @1 (view_convert @2)) @0)) 2468 (if (types_match (type, @0)) 2469 (with { tree cst = const_binop (outer_op, type, @1, @2); } 2470 (if (cst && !TREE_OVERFLOW (cst)) 2471 (minus { cst; } @0)))))))) 2472 2473 /* CST1 - (CST2 - A) -> CST3 + A 2474 Use view_convert because it is safe for vectors and equivalent for 2475 scalars. */ 2476 (simplify 2477 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0))) 2478 /* If one of the types wraps, use that one. */ 2479 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) 2480 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse 2481 forever if something doesn't simplify into a constant. */ 2482 (if (!CONSTANT_CLASS_P (@0)) 2483 (plus (view_convert @0) (minus @1 (view_convert @2)))) 2484 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 2485 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) 2486 (view_convert (plus @0 (minus (view_convert @1) @2))) 2487 (if (types_match (type, @0)) 2488 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); } 2489 (if (cst && !TREE_OVERFLOW (cst)) 2490 (plus { cst; } @0))))))) 2491 2492/* ((T)(A)) + CST -> (T)(A + CST) */ 2493#if GIMPLE 2494 (simplify 2495 (plus (convert:s SSA_NAME@0) INTEGER_CST@1) 2496 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE 2497 && TREE_CODE (type) == INTEGER_TYPE 2498 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)) 2499 && int_fits_type_p (@1, TREE_TYPE (@0))) 2500 /* Perform binary operation inside the cast if the constant fits 2501 and (A + CST)'s range does not overflow. */ 2502 (with 2503 { 2504 wi::overflow_type min_ovf = wi::OVF_OVERFLOW, 2505 max_ovf = wi::OVF_OVERFLOW; 2506 tree inner_type = TREE_TYPE (@0); 2507 2508 wide_int w1 2509 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type), 2510 TYPE_SIGN (inner_type)); 2511 2512 wide_int wmin0, wmax0; 2513 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE) 2514 { 2515 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf); 2516 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf); 2517 } 2518 } 2519 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE) 2520 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } ))) 2521 ))) 2522#endif 2523 2524/* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */ 2525#if GIMPLE 2526 (for op (plus minus) 2527 (simplify 2528 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2) 2529 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE 2530 && TREE_CODE (type) == INTEGER_TYPE 2531 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)) 2532 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 2533 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) 2534 && TYPE_OVERFLOW_WRAPS (type)) 2535 (plus (convert @0) (op @2 (convert @1)))))) 2536#endif 2537 2538/* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified 2539 to a simple value. */ 2540#if GIMPLE 2541 (for op (plus minus) 2542 (simplify 2543 (op (convert @0) (convert @1)) 2544 (if (INTEGRAL_TYPE_P (type) 2545 && INTEGRAL_TYPE_P (TREE_TYPE (@0)) 2546 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)) 2547 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)) 2548 && !TYPE_OVERFLOW_TRAPS (type) 2549 && !TYPE_OVERFLOW_SANITIZED (type)) 2550 (convert (op! @0 @1))))) 2551#endif 2552 2553 /* ~A + A -> -1 */ 2554 (simplify 2555 (plus:c (bit_not @0) @0) 2556 (if (!TYPE_OVERFLOW_TRAPS (type)) 2557 { build_all_ones_cst (type); })) 2558 2559 /* ~A + 1 -> -A */ 2560 (simplify 2561 (plus (convert? (bit_not @0)) integer_each_onep) 2562 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 2563 (negate (convert @0)))) 2564 2565 /* -A - 1 -> ~A */ 2566 (simplify 2567 (minus (convert? (negate @0)) integer_each_onep) 2568 (if (!TYPE_OVERFLOW_TRAPS (type) 2569 && tree_nop_conversion_p (type, TREE_TYPE (@0))) 2570 (bit_not (convert @0)))) 2571 2572 /* -1 - A -> ~A */ 2573 (simplify 2574 (minus integer_all_onesp @0) 2575 (bit_not @0)) 2576 2577 /* (T)(P + A) - (T)P -> (T) A */ 2578 (simplify 2579 (minus (convert (plus:c @@0 @1)) 2580 (convert? @0)) 2581 (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) 2582 /* For integer types, if A has a smaller type 2583 than T the result depends on the possible 2584 overflow in P + A. 2585 E.g. T=size_t, A=(unsigned)429497295, P>0. 2586 However, if an overflow in P + A would cause 2587 undefined behavior, we can assume that there 2588 is no overflow. */ 2589 || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) 2590 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) 2591 (convert @1))) 2592 (simplify 2593 (minus (convert (pointer_plus @@0 @1)) 2594 (convert @0)) 2595 (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) 2596 /* For pointer types, if the conversion of A to the 2597 final type requires a sign- or zero-extension, 2598 then we have to punt - it is not defined which 2599 one is correct. */ 2600 || (POINTER_TYPE_P (TREE_TYPE (@0)) 2601 && TREE_CODE (@1) == INTEGER_CST 2602 && tree_int_cst_sign_bit (@1) == 0)) 2603 (convert @1))) 2604 (simplify 2605 (pointer_diff (pointer_plus @@0 @1) @0) 2606 /* The second argument of pointer_plus must be interpreted as signed, and 2607 thus sign-extended if necessary. */ 2608 (with { tree stype = signed_type_for (TREE_TYPE (@1)); } 2609 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR 2610 second arg is unsigned even when we need to consider it as signed, 2611 we don't want to diagnose overflow here. */ 2612 (convert (view_convert:stype @1)))) 2613 2614 /* (T)P - (T)(P + A) -> -(T) A */ 2615 (simplify 2616 (minus (convert? @0) 2617 (convert (plus:c @@0 @1))) 2618 (if (INTEGRAL_TYPE_P (type) 2619 && TYPE_OVERFLOW_UNDEFINED (type) 2620 && element_precision (type) <= element_precision (TREE_TYPE (@1))) 2621 (with { tree utype = unsigned_type_for (type); } 2622 (convert (negate (convert:utype @1)))) 2623 (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) 2624 /* For integer types, if A has a smaller type 2625 than T the result depends on the possible 2626 overflow in P + A. 2627 E.g. T=size_t, A=(unsigned)429497295, P>0. 2628 However, if an overflow in P + A would cause 2629 undefined behavior, we can assume that there 2630 is no overflow. */ 2631 || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) 2632 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) 2633 (negate (convert @1))))) 2634 (simplify 2635 (minus (convert @0) 2636 (convert (pointer_plus @@0 @1))) 2637 (if (INTEGRAL_TYPE_P (type) 2638 && TYPE_OVERFLOW_UNDEFINED (type) 2639 && element_precision (type) <= element_precision (TREE_TYPE (@1))) 2640 (with { tree utype = unsigned_type_for (type); } 2641 (convert (negate (convert:utype @1)))) 2642 (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) 2643 /* For pointer types, if the conversion of A to the 2644 final type requires a sign- or zero-extension, 2645 then we have to punt - it is not defined which 2646 one is correct. */ 2647 || (POINTER_TYPE_P (TREE_TYPE (@0)) 2648 && TREE_CODE (@1) == INTEGER_CST 2649 && tree_int_cst_sign_bit (@1) == 0)) 2650 (negate (convert @1))))) 2651 (simplify 2652 (pointer_diff @0 (pointer_plus @@0 @1)) 2653 /* The second argument of pointer_plus must be interpreted as signed, and 2654 thus sign-extended if necessary. */ 2655 (with { tree stype = signed_type_for (TREE_TYPE (@1)); } 2656 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR 2657 second arg is unsigned even when we need to consider it as signed, 2658 we don't want to diagnose overflow here. */ 2659 (negate (convert (view_convert:stype @1))))) 2660 2661 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */ 2662 (simplify 2663 (minus (convert (plus:c @@0 @1)) 2664 (convert (plus:c @0 @2))) 2665 (if (INTEGRAL_TYPE_P (type) 2666 && TYPE_OVERFLOW_UNDEFINED (type) 2667 && element_precision (type) <= element_precision (TREE_TYPE (@1)) 2668 && element_precision (type) <= element_precision (TREE_TYPE (@2))) 2669 (with { tree utype = unsigned_type_for (type); } 2670 (convert (minus (convert:utype @1) (convert:utype @2)))) 2671 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1))) 2672 == (element_precision (type) <= element_precision (TREE_TYPE (@2)))) 2673 && (element_precision (type) <= element_precision (TREE_TYPE (@1)) 2674 /* For integer types, if A has a smaller type 2675 than T the result depends on the possible 2676 overflow in P + A. 2677 E.g. T=size_t, A=(unsigned)429497295, P>0. 2678 However, if an overflow in P + A would cause 2679 undefined behavior, we can assume that there 2680 is no overflow. */ 2681 || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) 2682 && INTEGRAL_TYPE_P (TREE_TYPE (@2)) 2683 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)) 2684 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2))))) 2685 (minus (convert @1) (convert @2))))) 2686 (simplify 2687 (minus (convert (pointer_plus @@0 @1)) 2688 (convert (pointer_plus @0 @2))) 2689 (if (INTEGRAL_TYPE_P (type) 2690 && TYPE_OVERFLOW_UNDEFINED (type) 2691 && element_precision (type) <= element_precision (TREE_TYPE (@1))) 2692 (with { tree utype = unsigned_type_for (type); } 2693 (convert (minus (convert:utype @1) (convert:utype @2)))) 2694 (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) 2695 /* For pointer types, if the conversion of A to the 2696 final type requires a sign- or zero-extension, 2697 then we have to punt - it is not defined which 2698 one is correct. */ 2699 || (POINTER_TYPE_P (TREE_TYPE (@0)) 2700 && TREE_CODE (@1) == INTEGER_CST 2701 && tree_int_cst_sign_bit (@1) == 0 2702 && TREE_CODE (@2) == INTEGER_CST 2703 && tree_int_cst_sign_bit (@2) == 0)) 2704 (minus (convert @1) (convert @2))))) 2705 (simplify 2706 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2)) 2707 (pointer_diff @0 @1)) 2708 (simplify 2709 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2)) 2710 /* The second argument of pointer_plus must be interpreted as signed, and 2711 thus sign-extended if necessary. */ 2712 (with { tree stype = signed_type_for (TREE_TYPE (@1)); } 2713 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR 2714 second arg is unsigned even when we need to consider it as signed, 2715 we don't want to diagnose overflow here. */ 2716 (minus (convert (view_convert:stype @1)) 2717 (convert (view_convert:stype @2))))))) 2718 2719/* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1). 2720 Modeled after fold_plusminus_mult_expr. */ 2721(if (!TYPE_SATURATING (type) 2722 && (!FLOAT_TYPE_P (type) || flag_associative_math)) 2723 (for plusminus (plus minus) 2724 (simplify 2725 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2)) 2726 (if (!ANY_INTEGRAL_TYPE_P (type) 2727 || TYPE_OVERFLOW_WRAPS (type) 2728 || (INTEGRAL_TYPE_P (type) 2729 && tree_expr_nonzero_p (@0) 2730 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type))))) 2731 (if (single_use (@3) || single_use (@4)) 2732 /* If @1 +- @2 is constant require a hard single-use on either 2733 original operand (but not on both). */ 2734 (mult (plusminus @1 @2) @0) 2735#if GIMPLE 2736 (mult! (plusminus @1 @2) @0) 2737#endif 2738 ))) 2739 /* We cannot generate constant 1 for fract. */ 2740 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))) 2741 (simplify 2742 (plusminus @0 (mult:c@3 @0 @2)) 2743 (if ((!ANY_INTEGRAL_TYPE_P (type) 2744 || TYPE_OVERFLOW_WRAPS (type) 2745 /* For @0 + @0*@2 this transformation would introduce UB 2746 (where there was none before) for @0 in [-1,0] and @2 max. 2747 For @0 - @0*@2 this transformation would introduce UB 2748 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */ 2749 || (INTEGRAL_TYPE_P (type) 2750 && ((tree_expr_nonzero_p (@0) 2751 && expr_not_equal_to (@0, 2752 wi::minus_one (TYPE_PRECISION (type)))) 2753 || (plusminus == PLUS_EXPR 2754 ? expr_not_equal_to (@2, 2755 wi::max_value (TYPE_PRECISION (type), SIGNED)) 2756 /* Let's ignore the @0 -1 and @2 min case. */ 2757 : (expr_not_equal_to (@2, 2758 wi::min_value (TYPE_PRECISION (type), SIGNED)) 2759 && expr_not_equal_to (@2, 2760 wi::min_value (TYPE_PRECISION (type), SIGNED) 2761 + 1)))))) 2762 && single_use (@3)) 2763 (mult (plusminus { build_one_cst (type); } @2) @0))) 2764 (simplify 2765 (plusminus (mult:c@3 @0 @2) @0) 2766 (if ((!ANY_INTEGRAL_TYPE_P (type) 2767 || TYPE_OVERFLOW_WRAPS (type) 2768 /* For @0*@2 + @0 this transformation would introduce UB 2769 (where there was none before) for @0 in [-1,0] and @2 max. 2770 For @0*@2 - @0 this transformation would introduce UB 2771 for @0 0 and @2 min. */ 2772 || (INTEGRAL_TYPE_P (type) 2773 && ((tree_expr_nonzero_p (@0) 2774 && (plusminus == MINUS_EXPR 2775 || expr_not_equal_to (@0, 2776 wi::minus_one (TYPE_PRECISION (type))))) 2777 || expr_not_equal_to (@2, 2778 (plusminus == PLUS_EXPR 2779 ? wi::max_value (TYPE_PRECISION (type), SIGNED) 2780 : wi::min_value (TYPE_PRECISION (type), SIGNED)))))) 2781 && single_use (@3)) 2782 (mult (plusminus @2 { build_one_cst (type); }) @0)))))) 2783 2784#if GIMPLE 2785/* Canonicalize X + (X << C) into X * (1 + (1 << C)) and 2786 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */ 2787(simplify 2788 (plus:c @0 (lshift:s @0 INTEGER_CST@1)) 2789 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 2790 && tree_fits_uhwi_p (@1) 2791 && tree_to_uhwi (@1) < element_precision (type) 2792 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 2793 || optab_handler (smul_optab, 2794 TYPE_MODE (type)) != CODE_FOR_nothing)) 2795 (with { tree t = type; 2796 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t); 2797 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), 2798 element_precision (type)); 2799 w += 1; 2800 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t) 2801 : t, w); 2802 cst = build_uniform_cst (t, cst); } 2803 (convert (mult (convert:t @0) { cst; }))))) 2804(simplify 2805 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2)) 2806 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 2807 && tree_fits_uhwi_p (@1) 2808 && tree_to_uhwi (@1) < element_precision (type) 2809 && tree_fits_uhwi_p (@2) 2810 && tree_to_uhwi (@2) < element_precision (type) 2811 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 2812 || optab_handler (smul_optab, 2813 TYPE_MODE (type)) != CODE_FOR_nothing)) 2814 (with { tree t = type; 2815 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t); 2816 unsigned int prec = element_precision (type); 2817 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec); 2818 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec); 2819 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t) 2820 : t, w); 2821 cst = build_uniform_cst (t, cst); } 2822 (convert (mult (convert:t @0) { cst; }))))) 2823#endif 2824 2825/* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */ 2826 2827(for minmax (min max FMIN_ALL FMAX_ALL) 2828 (simplify 2829 (minmax @0 @0) 2830 @0)) 2831/* min(max(x,y),y) -> y. */ 2832(simplify 2833 (min:c (max:c @0 @1) @1) 2834 @1) 2835/* max(min(x,y),y) -> y. */ 2836(simplify 2837 (max:c (min:c @0 @1) @1) 2838 @1) 2839/* max(a,-a) -> abs(a). */ 2840(simplify 2841 (max:c @0 (negate @0)) 2842 (if (TREE_CODE (type) != COMPLEX_TYPE 2843 && (! ANY_INTEGRAL_TYPE_P (type) 2844 || TYPE_OVERFLOW_UNDEFINED (type))) 2845 (abs @0))) 2846/* min(a,-a) -> -abs(a). */ 2847(simplify 2848 (min:c @0 (negate @0)) 2849 (if (TREE_CODE (type) != COMPLEX_TYPE 2850 && (! ANY_INTEGRAL_TYPE_P (type) 2851 || TYPE_OVERFLOW_UNDEFINED (type))) 2852 (negate (abs @0)))) 2853(simplify 2854 (min @0 @1) 2855 (switch 2856 (if (INTEGRAL_TYPE_P (type) 2857 && TYPE_MIN_VALUE (type) 2858 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) 2859 @1) 2860 (if (INTEGRAL_TYPE_P (type) 2861 && TYPE_MAX_VALUE (type) 2862 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) 2863 @0))) 2864(simplify 2865 (max @0 @1) 2866 (switch 2867 (if (INTEGRAL_TYPE_P (type) 2868 && TYPE_MAX_VALUE (type) 2869 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) 2870 @1) 2871 (if (INTEGRAL_TYPE_P (type) 2872 && TYPE_MIN_VALUE (type) 2873 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) 2874 @0))) 2875 2876/* max (a, a + CST) -> a + CST where CST is positive. */ 2877/* max (a, a + CST) -> a where CST is negative. */ 2878(simplify 2879 (max:c @0 (plus@2 @0 INTEGER_CST@1)) 2880 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) 2881 (if (tree_int_cst_sgn (@1) > 0) 2882 @2 2883 @0))) 2884 2885/* min (a, a + CST) -> a where CST is positive. */ 2886/* min (a, a + CST) -> a + CST where CST is negative. */ 2887(simplify 2888 (min:c @0 (plus@2 @0 INTEGER_CST@1)) 2889 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) 2890 (if (tree_int_cst_sgn (@1) > 0) 2891 @0 2892 @2))) 2893 2894/* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted 2895 and the outer convert demotes the expression back to x's type. */ 2896(for minmax (min max) 2897 (simplify 2898 (convert (minmax@0 (convert @1) INTEGER_CST@2)) 2899 (if (INTEGRAL_TYPE_P (type) 2900 && types_match (@1, type) && int_fits_type_p (@2, type) 2901 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type) 2902 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)) 2903 (minmax @1 (convert @2))))) 2904 2905(for minmax (FMIN_ALL FMAX_ALL) 2906 /* If either argument is NaN, return the other one. Avoid the 2907 transformation if we get (and honor) a signalling NaN. */ 2908 (simplify 2909 (minmax:c @0 REAL_CST@1) 2910 (if (real_isnan (TREE_REAL_CST_PTR (@1)) 2911 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)) 2912 @0))) 2913/* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these 2914 functions to return the numeric arg if the other one is NaN. 2915 MIN and MAX don't honor that, so only transform if -ffinite-math-only 2916 is set. C99 doesn't require -0.0 to be handled, so we don't have to 2917 worry about it either. */ 2918(if (flag_finite_math_only) 2919 (simplify 2920 (FMIN_ALL @0 @1) 2921 (min @0 @1)) 2922 (simplify 2923 (FMAX_ALL @0 @1) 2924 (max @0 @1))) 2925/* min (-A, -B) -> -max (A, B) */ 2926(for minmax (min max FMIN_ALL FMAX_ALL) 2927 maxmin (max min FMAX_ALL FMIN_ALL) 2928 (simplify 2929 (minmax (negate:s@2 @0) (negate:s@3 @1)) 2930 (if (FLOAT_TYPE_P (TREE_TYPE (@0)) 2931 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 2932 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) 2933 (negate (maxmin @0 @1))))) 2934/* MIN (~X, ~Y) -> ~MAX (X, Y) 2935 MAX (~X, ~Y) -> ~MIN (X, Y) */ 2936(for minmax (min max) 2937 maxmin (max min) 2938 (simplify 2939 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1)) 2940 (bit_not (maxmin @0 @1)))) 2941 2942/* MIN (X, Y) == X -> X <= Y */ 2943(for minmax (min min max max) 2944 cmp (eq ne eq ne ) 2945 out (le gt ge lt ) 2946 (simplify 2947 (cmp:c (minmax:c @0 @1) @0) 2948 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))) 2949 (out @0 @1)))) 2950/* MIN (X, 5) == 0 -> X == 0 2951 MIN (X, 5) == 7 -> false */ 2952(for cmp (eq ne) 2953 (simplify 2954 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2) 2955 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2), 2956 TYPE_SIGN (TREE_TYPE (@0)))) 2957 { constant_boolean_node (cmp == NE_EXPR, type); } 2958 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2), 2959 TYPE_SIGN (TREE_TYPE (@0)))) 2960 (cmp @0 @2))))) 2961(for cmp (eq ne) 2962 (simplify 2963 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2) 2964 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2), 2965 TYPE_SIGN (TREE_TYPE (@0)))) 2966 { constant_boolean_node (cmp == NE_EXPR, type); } 2967 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2), 2968 TYPE_SIGN (TREE_TYPE (@0)))) 2969 (cmp @0 @2))))) 2970/* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */ 2971(for minmax (min min max max min min max max ) 2972 cmp (lt le gt ge gt ge lt le ) 2973 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and) 2974 (simplify 2975 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2) 2976 (comb (cmp @0 @2) (cmp @1 @2)))) 2977 2978/* X <= MAX(X, Y) -> true 2979 X > MAX(X, Y) -> false 2980 X >= MIN(X, Y) -> true 2981 X < MIN(X, Y) -> false */ 2982(for minmax (min min max max ) 2983 cmp (ge lt le gt ) 2984 (simplify 2985 (cmp @0 (minmax:c @0 @1)) 2986 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } )) 2987 2988/* Undo fancy way of writing max/min or other ?: expressions, 2989 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a. 2990 People normally use ?: and that is what we actually try to optimize. */ 2991(for cmp (simple_comparison) 2992 (simplify 2993 (minus @0 (bit_and:c (minus @0 @1) 2994 (convert? (negate@4 (convert? (cmp@5 @2 @3)))))) 2995 (if (INTEGRAL_TYPE_P (type) 2996 && INTEGRAL_TYPE_P (TREE_TYPE (@4)) 2997 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE 2998 && INTEGRAL_TYPE_P (TREE_TYPE (@5)) 2999 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type) 3000 || !TYPE_UNSIGNED (TREE_TYPE (@4))) 3001 && (GIMPLE || !TREE_SIDE_EFFECTS (@1))) 3002 (cond (cmp @2 @3) @1 @0))) 3003 (simplify 3004 (plus:c @0 (bit_and:c (minus @1 @0) 3005 (convert? (negate@4 (convert? (cmp@5 @2 @3)))))) 3006 (if (INTEGRAL_TYPE_P (type) 3007 && INTEGRAL_TYPE_P (TREE_TYPE (@4)) 3008 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE 3009 && INTEGRAL_TYPE_P (TREE_TYPE (@5)) 3010 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type) 3011 || !TYPE_UNSIGNED (TREE_TYPE (@4))) 3012 && (GIMPLE || !TREE_SIDE_EFFECTS (@1))) 3013 (cond (cmp @2 @3) @1 @0))) 3014 /* Similarly with ^ instead of - though in that case with :c. */ 3015 (simplify 3016 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1) 3017 (convert? (negate@4 (convert? (cmp@5 @2 @3)))))) 3018 (if (INTEGRAL_TYPE_P (type) 3019 && INTEGRAL_TYPE_P (TREE_TYPE (@4)) 3020 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE 3021 && INTEGRAL_TYPE_P (TREE_TYPE (@5)) 3022 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type) 3023 || !TYPE_UNSIGNED (TREE_TYPE (@4))) 3024 && (GIMPLE || !TREE_SIDE_EFFECTS (@1))) 3025 (cond (cmp @2 @3) @1 @0)))) 3026 3027/* Simplifications of shift and rotates. */ 3028 3029(for rotate (lrotate rrotate) 3030 (simplify 3031 (rotate integer_all_onesp@0 @1) 3032 @0)) 3033 3034/* Optimize -1 >> x for arithmetic right shifts. */ 3035(simplify 3036 (rshift integer_all_onesp@0 @1) 3037 (if (!TYPE_UNSIGNED (type)) 3038 @0)) 3039 3040/* Optimize (x >> c) << c into x & (-1<<c). */ 3041(simplify 3042 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1) 3043 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))) 3044 /* It doesn't matter if the right shift is arithmetic or logical. */ 3045 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1)))) 3046 3047(simplify 3048 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1) 3049 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)) 3050 /* Allow intermediate conversion to integral type with whatever sign, as 3051 long as the low TYPE_PRECISION (type) 3052 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */ 3053 && INTEGRAL_TYPE_P (type) 3054 && INTEGRAL_TYPE_P (TREE_TYPE (@2)) 3055 && INTEGRAL_TYPE_P (TREE_TYPE (@0)) 3056 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)) 3057 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type) 3058 || wi::geu_p (wi::to_wide (@1), 3059 TYPE_PRECISION (type) 3060 - TYPE_PRECISION (TREE_TYPE (@2))))) 3061 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1)))) 3062 3063/* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned 3064 types. */ 3065(simplify 3066 (rshift (lshift @0 INTEGER_CST@1) @1) 3067 (if (TYPE_UNSIGNED (type) 3068 && (wi::ltu_p (wi::to_wide (@1), element_precision (type)))) 3069 (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) 3070 3071/* Optimize x >> x into 0 */ 3072(simplify 3073 (rshift @0 @0) 3074 { build_zero_cst (type); }) 3075 3076(for shiftrotate (lrotate rrotate lshift rshift) 3077 (simplify 3078 (shiftrotate @0 integer_zerop) 3079 (non_lvalue @0)) 3080 (simplify 3081 (shiftrotate integer_zerop@0 @1) 3082 @0) 3083 /* Prefer vector1 << scalar to vector1 << vector2 3084 if vector2 is uniform. */ 3085 (for vec (VECTOR_CST CONSTRUCTOR) 3086 (simplify 3087 (shiftrotate @0 vec@1) 3088 (with { tree tem = uniform_vector_p (@1); } 3089 (if (tem) 3090 (shiftrotate @0 { tem; })))))) 3091 3092/* Simplify X << Y where Y's low width bits are 0 to X, as only valid 3093 Y is 0. Similarly for X >> Y. */ 3094#if GIMPLE 3095(for shift (lshift rshift) 3096 (simplify 3097 (shift @0 SSA_NAME@1) 3098 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))) 3099 (with { 3100 int width = ceil_log2 (element_precision (TREE_TYPE (@0))); 3101 int prec = TYPE_PRECISION (TREE_TYPE (@1)); 3102 } 3103 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0) 3104 @0))))) 3105#endif 3106 3107/* Rewrite an LROTATE_EXPR by a constant into an 3108 RROTATE_EXPR by a new constant. */ 3109(simplify 3110 (lrotate @0 INTEGER_CST@1) 3111 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1), 3112 build_int_cst (TREE_TYPE (@1), 3113 element_precision (type)), @1); })) 3114 3115/* Turn (a OP c1) OP c2 into a OP (c1+c2). */ 3116(for op (lrotate rrotate rshift lshift) 3117 (simplify 3118 (op (op @0 INTEGER_CST@1) INTEGER_CST@2) 3119 (with { unsigned int prec = element_precision (type); } 3120 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))) 3121 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1))) 3122 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))) 3123 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2)))) 3124 (with { unsigned int low = (tree_to_uhwi (@1) 3125 + tree_to_uhwi (@2)); } 3126 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2 3127 being well defined. */ 3128 (if (low >= prec) 3129 (if (op == LROTATE_EXPR || op == RROTATE_EXPR) 3130 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); }) 3131 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR) 3132 { build_zero_cst (type); } 3133 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); }))) 3134 (op @0 { build_int_cst (TREE_TYPE (@1), low); }))))))) 3135 3136 3137/* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */ 3138(simplify 3139 (bit_and (lshift INTEGER_CST@1 @0) integer_onep) 3140 (if ((wi::to_wide (@1) & 1) != 0) 3141 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); })) 3142 { build_zero_cst (type); })) 3143 3144/* Simplify ((C << x) & D) != 0 where C and D are power of two constants, 3145 either to false if D is smaller (unsigned comparison) than C, or to 3146 x == log2 (D) - log2 (C). Similarly for right shifts. */ 3147(for cmp (ne eq) 3148 icmp (eq ne) 3149 (simplify 3150 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop) 3151 (with { int c1 = wi::clz (wi::to_wide (@1)); 3152 int c2 = wi::clz (wi::to_wide (@2)); } 3153 (if (c1 < c2) 3154 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); } 3155 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); })))) 3156 (simplify 3157 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop) 3158 (if (tree_int_cst_sgn (@1) > 0) 3159 (with { int c1 = wi::clz (wi::to_wide (@1)); 3160 int c2 = wi::clz (wi::to_wide (@2)); } 3161 (if (c1 > c2) 3162 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); } 3163 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))) 3164 3165/* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1) 3166 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1) 3167 if CST2 != 0. */ 3168(for cmp (ne eq) 3169 (simplify 3170 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2) 3171 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); } 3172 (if (cand < 0 3173 || (!integer_zerop (@2) 3174 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2))) 3175 { constant_boolean_node (cmp == NE_EXPR, type); } 3176 (if (!integer_zerop (@2) 3177 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2)) 3178 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); })))))) 3179 3180/* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1)) 3181 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1)) 3182 if the new mask might be further optimized. */ 3183(for shift (lshift rshift) 3184 (simplify 3185 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1)) 3186 INTEGER_CST@2) 3187 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5)) 3188 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT 3189 && tree_fits_uhwi_p (@1) 3190 && tree_to_uhwi (@1) > 0 3191 && tree_to_uhwi (@1) < TYPE_PRECISION (type)) 3192 (with 3193 { 3194 unsigned int shiftc = tree_to_uhwi (@1); 3195 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2); 3196 unsigned HOST_WIDE_INT newmask, zerobits = 0; 3197 tree shift_type = TREE_TYPE (@3); 3198 unsigned int prec; 3199 3200 if (shift == LSHIFT_EXPR) 3201 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1); 3202 else if (shift == RSHIFT_EXPR 3203 && type_has_mode_precision_p (shift_type)) 3204 { 3205 prec = TYPE_PRECISION (TREE_TYPE (@3)); 3206 tree arg00 = @0; 3207 /* See if more bits can be proven as zero because of 3208 zero extension. */ 3209 if (@3 != @0 3210 && TYPE_UNSIGNED (TREE_TYPE (@0))) 3211 { 3212 tree inner_type = TREE_TYPE (@0); 3213 if (type_has_mode_precision_p (inner_type) 3214 && TYPE_PRECISION (inner_type) < prec) 3215 { 3216 prec = TYPE_PRECISION (inner_type); 3217 /* See if we can shorten the right shift. */ 3218 if (shiftc < prec) 3219 shift_type = inner_type; 3220 /* Otherwise X >> C1 is all zeros, so we'll optimize 3221 it into (X, 0) later on by making sure zerobits 3222 is all ones. */ 3223 } 3224 } 3225 zerobits = HOST_WIDE_INT_M1U; 3226 if (shiftc < prec) 3227 { 3228 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; 3229 zerobits <<= prec - shiftc; 3230 } 3231 /* For arithmetic shift if sign bit could be set, zerobits 3232 can contain actually sign bits, so no transformation is 3233 possible, unless MASK masks them all away. In that 3234 case the shift needs to be converted into logical shift. */ 3235 if (!TYPE_UNSIGNED (TREE_TYPE (@3)) 3236 && prec == TYPE_PRECISION (TREE_TYPE (@3))) 3237 { 3238 if ((mask & zerobits) == 0) 3239 shift_type = unsigned_type_for (TREE_TYPE (@3)); 3240 else 3241 zerobits = 0; 3242 } 3243 } 3244 } 3245 /* ((X << 16) & 0xff00) is (X, 0). */ 3246 (if ((mask & zerobits) == mask) 3247 { build_int_cst (type, 0); } 3248 (with { newmask = mask | zerobits; } 3249 (if (newmask != mask && (newmask & (newmask + 1)) == 0) 3250 (with 3251 { 3252 /* Only do the transformation if NEWMASK is some integer 3253 mode's mask. */ 3254 for (prec = BITS_PER_UNIT; 3255 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1) 3256 if (newmask == (HOST_WIDE_INT_1U << prec) - 1) 3257 break; 3258 } 3259 (if (prec < HOST_BITS_PER_WIDE_INT 3260 || newmask == HOST_WIDE_INT_M1U) 3261 (with 3262 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); } 3263 (if (!tree_int_cst_equal (newmaskt, @2)) 3264 (if (shift_type != TREE_TYPE (@3)) 3265 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; }) 3266 (bit_and @4 { newmaskt; }))))))))))))) 3267 3268/* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1) 3269 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */ 3270(for shift (lshift rshift) 3271 (for bit_op (bit_and bit_xor bit_ior) 3272 (simplify 3273 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1) 3274 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) 3275 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); } 3276 (if (mask) 3277 (bit_op (shift (convert @0) @1) { mask; }))))))) 3278 3279/* ~(~X >> Y) -> X >> Y (for arithmetic shift). */ 3280(simplify 3281 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2))) 3282 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)) 3283 && (element_precision (TREE_TYPE (@0)) 3284 <= element_precision (TREE_TYPE (@1)) 3285 || !TYPE_UNSIGNED (TREE_TYPE (@1)))) 3286 (with 3287 { tree shift_type = TREE_TYPE (@0); } 3288 (convert (rshift (convert:shift_type @1) @2))))) 3289 3290/* ~(~X >>r Y) -> X >>r Y 3291 ~(~X <<r Y) -> X <<r Y */ 3292(for rotate (lrotate rrotate) 3293 (simplify 3294 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2))) 3295 (if ((element_precision (TREE_TYPE (@0)) 3296 <= element_precision (TREE_TYPE (@1)) 3297 || !TYPE_UNSIGNED (TREE_TYPE (@1))) 3298 && (element_precision (type) <= element_precision (TREE_TYPE (@0)) 3299 || !TYPE_UNSIGNED (TREE_TYPE (@0)))) 3300 (with 3301 { tree rotate_type = TREE_TYPE (@0); } 3302 (convert (rotate (convert:rotate_type @1) @2)))))) 3303 3304/* Simplifications of conversions. */ 3305 3306/* Basic strip-useless-type-conversions / strip_nops. */ 3307(for cvt (convert view_convert float fix_trunc) 3308 (simplify 3309 (cvt @0) 3310 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0))) 3311 || (GENERIC && type == TREE_TYPE (@0))) 3312 @0))) 3313 3314/* Contract view-conversions. */ 3315(simplify 3316 (view_convert (view_convert @0)) 3317 (view_convert @0)) 3318 3319/* For integral conversions with the same precision or pointer 3320 conversions use a NOP_EXPR instead. */ 3321(simplify 3322 (view_convert @0) 3323 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)) 3324 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) 3325 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))) 3326 (convert @0))) 3327 3328/* Strip inner integral conversions that do not change precision or size, or 3329 zero-extend while keeping the same size (for bool-to-char). */ 3330(simplify 3331 (view_convert (convert@0 @1)) 3332 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) 3333 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) 3334 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1)) 3335 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)) 3336 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1)) 3337 && TYPE_UNSIGNED (TREE_TYPE (@1))))) 3338 (view_convert @1))) 3339 3340/* Simplify a view-converted empty constructor. */ 3341(simplify 3342 (view_convert CONSTRUCTOR@0) 3343 (if (TREE_CODE (@0) != SSA_NAME 3344 && CONSTRUCTOR_NELTS (@0) == 0) 3345 { build_zero_cst (type); })) 3346 3347/* Re-association barriers around constants and other re-association 3348 barriers can be removed. */ 3349(simplify 3350 (paren CONSTANT_CLASS_P@0) 3351 @0) 3352(simplify 3353 (paren (paren@1 @0)) 3354 @1) 3355 3356/* Handle cases of two conversions in a row. */ 3357(for ocvt (convert float fix_trunc) 3358 (for icvt (convert float) 3359 (simplify 3360 (ocvt (icvt@1 @0)) 3361 (with 3362 { 3363 tree inside_type = TREE_TYPE (@0); 3364 tree inter_type = TREE_TYPE (@1); 3365 int inside_int = INTEGRAL_TYPE_P (inside_type); 3366 int inside_ptr = POINTER_TYPE_P (inside_type); 3367 int inside_float = FLOAT_TYPE_P (inside_type); 3368 int inside_vec = VECTOR_TYPE_P (inside_type); 3369 unsigned int inside_prec = TYPE_PRECISION (inside_type); 3370 int inside_unsignedp = TYPE_UNSIGNED (inside_type); 3371 int inter_int = INTEGRAL_TYPE_P (inter_type); 3372 int inter_ptr = POINTER_TYPE_P (inter_type); 3373 int inter_float = FLOAT_TYPE_P (inter_type); 3374 int inter_vec = VECTOR_TYPE_P (inter_type); 3375 unsigned int inter_prec = TYPE_PRECISION (inter_type); 3376 int inter_unsignedp = TYPE_UNSIGNED (inter_type); 3377 int final_int = INTEGRAL_TYPE_P (type); 3378 int final_ptr = POINTER_TYPE_P (type); 3379 int final_float = FLOAT_TYPE_P (type); 3380 int final_vec = VECTOR_TYPE_P (type); 3381 unsigned int final_prec = TYPE_PRECISION (type); 3382 int final_unsignedp = TYPE_UNSIGNED (type); 3383 } 3384 (switch 3385 /* In addition to the cases of two conversions in a row 3386 handled below, if we are converting something to its own 3387 type via an object of identical or wider precision, neither 3388 conversion is needed. */ 3389 (if (((GIMPLE && useless_type_conversion_p (type, inside_type)) 3390 || (GENERIC 3391 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type))) 3392 && (((inter_int || inter_ptr) && final_int) 3393 || (inter_float && final_float)) 3394 && inter_prec >= final_prec) 3395 (ocvt @0)) 3396 3397 /* Likewise, if the intermediate and initial types are either both 3398 float or both integer, we don't need the middle conversion if the 3399 former is wider than the latter and doesn't change the signedness 3400 (for integers). Avoid this if the final type is a pointer since 3401 then we sometimes need the middle conversion. */ 3402 (if (((inter_int && inside_int) || (inter_float && inside_float)) 3403 && (final_int || final_float) 3404 && inter_prec >= inside_prec 3405 && (inter_float || inter_unsignedp == inside_unsignedp)) 3406 (ocvt @0)) 3407 3408 /* If we have a sign-extension of a zero-extended value, we can 3409 replace that by a single zero-extension. Likewise if the 3410 final conversion does not change precision we can drop the 3411 intermediate conversion. */ 3412 (if (inside_int && inter_int && final_int 3413 && ((inside_prec < inter_prec && inter_prec < final_prec 3414 && inside_unsignedp && !inter_unsignedp) 3415 || final_prec == inter_prec)) 3416 (ocvt @0)) 3417 3418 /* Two conversions in a row are not needed unless: 3419 - some conversion is floating-point (overstrict for now), or 3420 - some conversion is a vector (overstrict for now), or 3421 - the intermediate type is narrower than both initial and 3422 final, or 3423 - the intermediate type and innermost type differ in signedness, 3424 and the outermost type is wider than the intermediate, or 3425 - the initial type is a pointer type and the precisions of the 3426 intermediate and final types differ, or 3427 - the final type is a pointer type and the precisions of the 3428 initial and intermediate types differ. */ 3429 (if (! inside_float && ! inter_float && ! final_float 3430 && ! inside_vec && ! inter_vec && ! final_vec 3431 && (inter_prec >= inside_prec || inter_prec >= final_prec) 3432 && ! (inside_int && inter_int 3433 && inter_unsignedp != inside_unsignedp 3434 && inter_prec < final_prec) 3435 && ((inter_unsignedp && inter_prec > inside_prec) 3436 == (final_unsignedp && final_prec > inter_prec)) 3437 && ! (inside_ptr && inter_prec != final_prec) 3438 && ! (final_ptr && inside_prec != inter_prec)) 3439 (ocvt @0)) 3440 3441 /* A truncation to an unsigned type (a zero-extension) should be 3442 canonicalized as bitwise and of a mask. */ 3443 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */ 3444 && final_int && inter_int && inside_int 3445 && final_prec == inside_prec 3446 && final_prec > inter_prec 3447 && inter_unsignedp) 3448 (convert (bit_and @0 { wide_int_to_tree 3449 (inside_type, 3450 wi::mask (inter_prec, false, 3451 TYPE_PRECISION (inside_type))); }))) 3452 3453 /* If we are converting an integer to a floating-point that can 3454 represent it exactly and back to an integer, we can skip the 3455 floating-point conversion. */ 3456 (if (GIMPLE /* PR66211 */ 3457 && inside_int && inter_float && final_int && 3458 (unsigned) significand_size (TYPE_MODE (inter_type)) 3459 >= inside_prec - !inside_unsignedp) 3460 (convert @0))))))) 3461 3462/* If we have a narrowing conversion to an integral type that is fed by a 3463 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely 3464 masks off bits outside the final type (and nothing else). */ 3465(simplify 3466 (convert (bit_and @0 INTEGER_CST@1)) 3467 (if (INTEGRAL_TYPE_P (type) 3468 && INTEGRAL_TYPE_P (TREE_TYPE (@0)) 3469 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)) 3470 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1), 3471 TYPE_PRECISION (type)), 0)) 3472 (convert @0))) 3473 3474 3475/* (X /[ex] A) * A -> X. */ 3476(simplify 3477 (mult (convert1? (exact_div @0 @@1)) (convert2? @1)) 3478 (convert @0)) 3479 3480/* Simplify (A / B) * B + (A % B) -> A. */ 3481(for div (trunc_div ceil_div floor_div round_div) 3482 mod (trunc_mod ceil_mod floor_mod round_mod) 3483 (simplify 3484 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1)) 3485 @0)) 3486 3487/* ((X /[ex] A) +- B) * A --> X +- A * B. */ 3488(for op (plus minus) 3489 (simplify 3490 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1) 3491 (if (tree_nop_conversion_p (type, TREE_TYPE (@2)) 3492 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))) 3493 (with 3494 { 3495 wi::overflow_type overflow; 3496 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), 3497 TYPE_SIGN (type), &overflow); 3498 } 3499 (if (types_match (type, TREE_TYPE (@2)) 3500 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow) 3501 (op @0 { wide_int_to_tree (type, mul); }) 3502 (with { tree utype = unsigned_type_for (type); } 3503 (convert (op (convert:utype @0) 3504 (mult (convert:utype @1) (convert:utype @2)))))))))) 3505 3506/* Canonicalization of binary operations. */ 3507 3508/* Convert X + -C into X - C. */ 3509(simplify 3510 (plus @0 REAL_CST@1) 3511 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) 3512 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); } 3513 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math) 3514 (minus @0 { tem; }))))) 3515 3516/* Convert x+x into x*2. */ 3517(simplify 3518 (plus @0 @0) 3519 (if (SCALAR_FLOAT_TYPE_P (type)) 3520 (mult @0 { build_real (type, dconst2); }) 3521 (if (INTEGRAL_TYPE_P (type)) 3522 (mult @0 { build_int_cst (type, 2); })))) 3523 3524/* 0 - X -> -X. */ 3525(simplify 3526 (minus integer_zerop @1) 3527 (negate @1)) 3528(simplify 3529 (pointer_diff integer_zerop @1) 3530 (negate (convert @1))) 3531 3532/* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether 3533 ARG0 is zero and X + ARG0 reduces to X, since that would mean 3534 (-ARG1 + ARG0) reduces to -ARG1. */ 3535(simplify 3536 (minus real_zerop@0 @1) 3537 (if (fold_real_zero_addition_p (type, @0, 0)) 3538 (negate @1))) 3539 3540/* Transform x * -1 into -x. */ 3541(simplify 3542 (mult @0 integer_minus_onep) 3543 (negate @0)) 3544 3545/* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce 3546 signed overflow for CST != 0 && CST != -1. */ 3547(simplify 3548 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2) 3549 (if (TREE_CODE (@2) != INTEGER_CST 3550 && single_use (@3) 3551 && !integer_zerop (@1) && !integer_minus_onep (@1)) 3552 (mult (mult @0 @2) @1))) 3553 3554/* True if we can easily extract the real and imaginary parts of a complex 3555 number. */ 3556(match compositional_complex 3557 (convert? (complex @0 @1))) 3558 3559/* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */ 3560(simplify 3561 (complex (realpart @0) (imagpart @0)) 3562 @0) 3563(simplify 3564 (realpart (complex @0 @1)) 3565 @0) 3566(simplify 3567 (imagpart (complex @0 @1)) 3568 @1) 3569 3570/* Sometimes we only care about half of a complex expression. */ 3571(simplify 3572 (realpart (convert?:s (conj:s @0))) 3573 (convert (realpart @0))) 3574(simplify 3575 (imagpart (convert?:s (conj:s @0))) 3576 (convert (negate (imagpart @0)))) 3577(for part (realpart imagpart) 3578 (for op (plus minus) 3579 (simplify 3580 (part (convert?:s@2 (op:s @0 @1))) 3581 (convert (op (part @0) (part @1)))))) 3582(simplify 3583 (realpart (convert?:s (CEXPI:s @0))) 3584 (convert (COS @0))) 3585(simplify 3586 (imagpart (convert?:s (CEXPI:s @0))) 3587 (convert (SIN @0))) 3588 3589/* conj(conj(x)) -> x */ 3590(simplify 3591 (conj (convert? (conj @0))) 3592 (if (tree_nop_conversion_p (TREE_TYPE (@0), type)) 3593 (convert @0))) 3594 3595/* conj({x,y}) -> {x,-y} */ 3596(simplify 3597 (conj (convert?:s (complex:s @0 @1))) 3598 (with { tree itype = TREE_TYPE (type); } 3599 (complex (convert:itype @0) (negate (convert:itype @1))))) 3600 3601/* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */ 3602(for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64) 3603 (simplify 3604 (bswap (bswap @0)) 3605 @0) 3606 (simplify 3607 (bswap (bit_not (bswap @0))) 3608 (bit_not @0)) 3609 (for bitop (bit_xor bit_ior bit_and) 3610 (simplify 3611 (bswap (bitop:c (bswap @0) @1)) 3612 (bitop @0 (bswap @1))))) 3613 3614 3615/* Combine COND_EXPRs and VEC_COND_EXPRs. */ 3616 3617/* Simplify constant conditions. 3618 Only optimize constant conditions when the selected branch 3619 has the same type as the COND_EXPR. This avoids optimizing 3620 away "c ? x : throw", where the throw has a void type. 3621 Note that we cannot throw away the fold-const.c variant nor 3622 this one as we depend on doing this transform before possibly 3623 A ? B : B -> B triggers and the fold-const.c one can optimize 3624 0 ? A : B to B even if A has side-effects. Something 3625 genmatch cannot handle. */ 3626(simplify 3627 (cond INTEGER_CST@0 @1 @2) 3628 (if (integer_zerop (@0)) 3629 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type)) 3630 @2) 3631 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type)) 3632 @1))) 3633(simplify 3634 (vec_cond VECTOR_CST@0 @1 @2) 3635 (if (integer_all_onesp (@0)) 3636 @1 3637 (if (integer_zerop (@0)) 3638 @2))) 3639 3640#if GIMPLE 3641/* Sink unary operations to branches, but only if we do fold both. */ 3642(for op (negate bit_not abs absu) 3643 (simplify 3644 (op (vec_cond:s @0 @1 @2)) 3645 (vec_cond @0 (op! @1) (op! @2)))) 3646 3647/* Sink binary operation to branches, but only if we can fold it. */ 3648(for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor 3649 lshift rshift rdiv trunc_div ceil_div floor_div round_div 3650 trunc_mod ceil_mod floor_mod round_mod min max) 3651/* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */ 3652 (simplify 3653 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4)) 3654 (vec_cond @0 (op! @1 @3) (op! @2 @4))) 3655 3656/* (c ? a : b) op d --> c ? (a op d) : (b op d) */ 3657 (simplify 3658 (op (vec_cond:s @0 @1 @2) @3) 3659 (vec_cond @0 (op! @1 @3) (op! @2 @3))) 3660 (simplify 3661 (op @3 (vec_cond:s @0 @1 @2)) 3662 (vec_cond @0 (op! @3 @1) (op! @3 @2)))) 3663#endif 3664 3665/* (v ? w : 0) ? a : b is just (v & w) ? a : b 3666 Currently disabled after pass lvec because ARM understands 3667 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */ 3668(simplify 3669 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2) 3670 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3)) 3671 (vec_cond (bit_and @0 @3) @1 @2))) 3672(simplify 3673 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2) 3674 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3)) 3675 (vec_cond (bit_ior @0 @3) @1 @2))) 3676(simplify 3677 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2) 3678 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3)) 3679 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1))) 3680(simplify 3681 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2) 3682 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3)) 3683 (vec_cond (bit_and @0 (bit_not @3)) @2 @1))) 3684 3685/* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */ 3686(simplify 3687 (vec_cond @0 (vec_cond:s @1 @2 @3) @3) 3688 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1)) 3689 (vec_cond (bit_and @0 @1) @2 @3))) 3690(simplify 3691 (vec_cond @0 @2 (vec_cond:s @1 @2 @3)) 3692 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1)) 3693 (vec_cond (bit_ior @0 @1) @2 @3))) 3694(simplify 3695 (vec_cond @0 (vec_cond:s @1 @2 @3) @2) 3696 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1)) 3697 (vec_cond (bit_ior (bit_not @0) @1) @2 @3))) 3698(simplify 3699 (vec_cond @0 @3 (vec_cond:s @1 @2 @3)) 3700 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1)) 3701 (vec_cond (bit_and (bit_not @0) @1) @2 @3))) 3702 3703/* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask 3704 types are compatible. */ 3705(simplify 3706 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2) 3707 (if (VECTOR_BOOLEAN_TYPE_P (type) 3708 && types_match (type, TREE_TYPE (@0))) 3709 (if (integer_zerop (@1) && integer_all_onesp (@2)) 3710 (bit_not @0) 3711 (if (integer_all_onesp (@1) && integer_zerop (@2)) 3712 @0)))) 3713 3714/* Simplification moved from fold_cond_expr_with_comparison. It may also 3715 be extended. */ 3716/* This pattern implements two kinds simplification: 3717 3718 Case 1) 3719 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if: 3720 1) Conversions are type widening from smaller type. 3721 2) Const c1 equals to c2 after canonicalizing comparison. 3722 3) Comparison has tree code LT, LE, GT or GE. 3723 This specific pattern is needed when (cmp (convert x) c) may not 3724 be simplified by comparison patterns because of multiple uses of 3725 x. It also makes sense here because simplifying across multiple 3726 referred var is always benefitial for complicated cases. 3727 3728 Case 2) 3729 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */ 3730(for cmp (lt le gt ge eq) 3731 (simplify 3732 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2) 3733 (with 3734 { 3735 tree from_type = TREE_TYPE (@1); 3736 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2); 3737 enum tree_code code = ERROR_MARK; 3738 3739 if (INTEGRAL_TYPE_P (from_type) 3740 && int_fits_type_p (@2, from_type) 3741 && (types_match (c1_type, from_type) 3742 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type) 3743 && (TYPE_UNSIGNED (from_type) 3744 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type)))) 3745 && (types_match (c2_type, from_type) 3746 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type) 3747 && (TYPE_UNSIGNED (from_type) 3748 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type))))) 3749 { 3750 if (cmp != EQ_EXPR) 3751 { 3752 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1)) 3753 { 3754 /* X <= Y - 1 equals to X < Y. */ 3755 if (cmp == LE_EXPR) 3756 code = LT_EXPR; 3757 /* X > Y - 1 equals to X >= Y. */ 3758 if (cmp == GT_EXPR) 3759 code = GE_EXPR; 3760 } 3761 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1)) 3762 { 3763 /* X < Y + 1 equals to X <= Y. */ 3764 if (cmp == LT_EXPR) 3765 code = LE_EXPR; 3766 /* X >= Y + 1 equals to X > Y. */ 3767 if (cmp == GE_EXPR) 3768 code = GT_EXPR; 3769 } 3770 if (code != ERROR_MARK 3771 || wi::to_widest (@2) == wi::to_widest (@3)) 3772 { 3773 if (cmp == LT_EXPR || cmp == LE_EXPR) 3774 code = MIN_EXPR; 3775 if (cmp == GT_EXPR || cmp == GE_EXPR) 3776 code = MAX_EXPR; 3777 } 3778 } 3779 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */ 3780 else if (int_fits_type_p (@3, from_type)) 3781 code = EQ_EXPR; 3782 } 3783 } 3784 (if (code == MAX_EXPR) 3785 (convert (max @1 (convert @2))) 3786 (if (code == MIN_EXPR) 3787 (convert (min @1 (convert @2))) 3788 (if (code == EQ_EXPR) 3789 (convert (cond (eq @1 (convert @3)) 3790 (convert:from_type @3) (convert:from_type @2))))))))) 3791 3792/* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if: 3793 3794 1) OP is PLUS or MINUS. 3795 2) CMP is LT, LE, GT or GE. 3796 3) C3 == (C1 op C2), and computation doesn't have undefined behavior. 3797 3798 This pattern also handles special cases like: 3799 3800 A) Operand x is a unsigned to signed type conversion and c1 is 3801 integer zero. In this case, 3802 (signed type)x < 0 <=> x > MAX_VAL(signed type) 3803 (signed type)x >= 0 <=> x <= MAX_VAL(signed type) 3804 B) Const c1 may not equal to (C3 op' C2). In this case we also 3805 check equality for (c1+1) and (c1-1) by adjusting comparison 3806 code. 3807 3808 TODO: Though signed type is handled by this pattern, it cannot be 3809 simplified at the moment because C standard requires additional 3810 type promotion. In order to match&simplify it here, the IR needs 3811 to be cleaned up by other optimizers, i.e, VRP. */ 3812(for op (plus minus) 3813 (for cmp (lt le gt ge) 3814 (simplify 3815 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3) 3816 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); } 3817 (if (types_match (from_type, to_type) 3818 /* Check if it is special case A). */ 3819 || (TYPE_UNSIGNED (from_type) 3820 && !TYPE_UNSIGNED (to_type) 3821 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type) 3822 && integer_zerop (@1) 3823 && (cmp == LT_EXPR || cmp == GE_EXPR))) 3824 (with 3825 { 3826 wi::overflow_type overflow = wi::OVF_NONE; 3827 enum tree_code code, cmp_code = cmp; 3828 wide_int real_c1; 3829 wide_int c1 = wi::to_wide (@1); 3830 wide_int c2 = wi::to_wide (@2); 3831 wide_int c3 = wi::to_wide (@3); 3832 signop sgn = TYPE_SIGN (from_type); 3833 3834 /* Handle special case A), given x of unsigned type: 3835 ((signed type)x < 0) <=> (x > MAX_VAL(signed type)) 3836 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */ 3837 if (!types_match (from_type, to_type)) 3838 { 3839 if (cmp_code == LT_EXPR) 3840 cmp_code = GT_EXPR; 3841 if (cmp_code == GE_EXPR) 3842 cmp_code = LE_EXPR; 3843 c1 = wi::max_value (to_type); 3844 } 3845 /* To simplify this pattern, we require c3 = (c1 op c2). Here we 3846 compute (c3 op' c2) and check if it equals to c1 with op' being 3847 the inverted operator of op. Make sure overflow doesn't happen 3848 if it is undefined. */ 3849 if (op == PLUS_EXPR) 3850 real_c1 = wi::sub (c3, c2, sgn, &overflow); 3851 else 3852 real_c1 = wi::add (c3, c2, sgn, &overflow); 3853 3854 code = cmp_code; 3855 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type)) 3856 { 3857 /* Check if c1 equals to real_c1. Boundary condition is handled 3858 by adjusting comparison operation if necessary. */ 3859 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn) 3860 && !overflow) 3861 { 3862 /* X <= Y - 1 equals to X < Y. */ 3863 if (cmp_code == LE_EXPR) 3864 code = LT_EXPR; 3865 /* X > Y - 1 equals to X >= Y. */ 3866 if (cmp_code == GT_EXPR) 3867 code = GE_EXPR; 3868 } 3869 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn) 3870 && !overflow) 3871 { 3872 /* X < Y + 1 equals to X <= Y. */ 3873 if (cmp_code == LT_EXPR) 3874 code = LE_EXPR; 3875 /* X >= Y + 1 equals to X > Y. */ 3876 if (cmp_code == GE_EXPR) 3877 code = GT_EXPR; 3878 } 3879 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn)) 3880 { 3881 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR) 3882 code = MIN_EXPR; 3883 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR) 3884 code = MAX_EXPR; 3885 } 3886 } 3887 } 3888 (if (code == MAX_EXPR) 3889 (op (max @X { wide_int_to_tree (from_type, real_c1); }) 3890 { wide_int_to_tree (from_type, c2); }) 3891 (if (code == MIN_EXPR) 3892 (op (min @X { wide_int_to_tree (from_type, real_c1); }) 3893 { wide_int_to_tree (from_type, c2); }))))))))) 3894 3895(for cnd (cond vec_cond) 3896 /* A ? B : (A ? X : C) -> A ? B : C. */ 3897 (simplify 3898 (cnd @0 (cnd @0 @1 @2) @3) 3899 (cnd @0 @1 @3)) 3900 (simplify 3901 (cnd @0 @1 (cnd @0 @2 @3)) 3902 (cnd @0 @1 @3)) 3903 /* A ? B : (!A ? C : X) -> A ? B : C. */ 3904 /* ??? This matches embedded conditions open-coded because genmatch 3905 would generate matching code for conditions in separate stmts only. 3906 The following is still important to merge then and else arm cases 3907 from if-conversion. */ 3908 (simplify 3909 (cnd @0 @1 (cnd @2 @3 @4)) 3910 (if (inverse_conditions_p (@0, @2)) 3911 (cnd @0 @1 @3))) 3912 (simplify 3913 (cnd @0 (cnd @1 @2 @3) @4) 3914 (if (inverse_conditions_p (@0, @1)) 3915 (cnd @0 @3 @4))) 3916 3917 /* A ? B : B -> B. */ 3918 (simplify 3919 (cnd @0 @1 @1) 3920 @1) 3921 3922 /* !A ? B : C -> A ? C : B. */ 3923 (simplify 3924 (cnd (logical_inverted_value truth_valued_p@0) @1 @2) 3925 (cnd @0 @2 @1))) 3926 3927/* -(type)!A -> (type)A - 1. */ 3928(simplify 3929 (negate (convert?:s (logical_inverted_value:s @0))) 3930 (if (INTEGRAL_TYPE_P (type) 3931 && TREE_CODE (type) != BOOLEAN_TYPE 3932 && TYPE_PRECISION (type) > 1 3933 && TREE_CODE (@0) == SSA_NAME 3934 && ssa_name_has_boolean_range (@0)) 3935 (plus (convert:type @0) { build_all_ones_cst (type); }))) 3936 3937/* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons 3938 return all -1 or all 0 results. */ 3939/* ??? We could instead convert all instances of the vec_cond to negate, 3940 but that isn't necessarily a win on its own. */ 3941(simplify 3942 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) 3943 (if (VECTOR_TYPE_P (type) 3944 && known_eq (TYPE_VECTOR_SUBPARTS (type), 3945 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))) 3946 && (TYPE_MODE (TREE_TYPE (type)) 3947 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) 3948 (minus @3 (view_convert (vec_cond @0 (negate @1) @2))))) 3949 3950/* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */ 3951(simplify 3952 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) 3953 (if (VECTOR_TYPE_P (type) 3954 && known_eq (TYPE_VECTOR_SUBPARTS (type), 3955 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))) 3956 && (TYPE_MODE (TREE_TYPE (type)) 3957 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) 3958 (plus @3 (view_convert (vec_cond @0 (negate @1) @2))))) 3959 3960 3961/* Simplifications of comparisons. */ 3962 3963/* See if we can reduce the magnitude of a constant involved in a 3964 comparison by changing the comparison code. This is a canonicalization 3965 formerly done by maybe_canonicalize_comparison_1. */ 3966(for cmp (le gt) 3967 acmp (lt ge) 3968 (simplify 3969 (cmp @0 uniform_integer_cst_p@1) 3970 (with { tree cst = uniform_integer_cst_p (@1); } 3971 (if (tree_int_cst_sgn (cst) == -1) 3972 (acmp @0 { build_uniform_cst (TREE_TYPE (@1), 3973 wide_int_to_tree (TREE_TYPE (cst), 3974 wi::to_wide (cst) 3975 + 1)); }))))) 3976(for cmp (ge lt) 3977 acmp (gt le) 3978 (simplify 3979 (cmp @0 uniform_integer_cst_p@1) 3980 (with { tree cst = uniform_integer_cst_p (@1); } 3981 (if (tree_int_cst_sgn (cst) == 1) 3982 (acmp @0 { build_uniform_cst (TREE_TYPE (@1), 3983 wide_int_to_tree (TREE_TYPE (cst), 3984 wi::to_wide (cst) - 1)); }))))) 3985 3986/* We can simplify a logical negation of a comparison to the 3987 inverted comparison. As we cannot compute an expression 3988 operator using invert_tree_comparison we have to simulate 3989 that with expression code iteration. */ 3990(for cmp (tcc_comparison) 3991 icmp (inverted_tcc_comparison) 3992 ncmp (inverted_tcc_comparison_with_nans) 3993 /* Ideally we'd like to combine the following two patterns 3994 and handle some more cases by using 3995 (logical_inverted_value (cmp @0 @1)) 3996 here but for that genmatch would need to "inline" that. 3997 For now implement what forward_propagate_comparison did. */ 3998 (simplify 3999 (bit_not (cmp @0 @1)) 4000 (if (VECTOR_TYPE_P (type) 4001 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)) 4002 /* Comparison inversion may be impossible for trapping math, 4003 invert_tree_comparison will tell us. But we can't use 4004 a computed operator in the replacement tree thus we have 4005 to play the trick below. */ 4006 (with { enum tree_code ic = invert_tree_comparison 4007 (cmp, HONOR_NANS (@0)); } 4008 (if (ic == icmp) 4009 (icmp @0 @1) 4010 (if (ic == ncmp) 4011 (ncmp @0 @1)))))) 4012 (simplify 4013 (bit_xor (cmp @0 @1) integer_truep) 4014 (with { enum tree_code ic = invert_tree_comparison 4015 (cmp, HONOR_NANS (@0)); } 4016 (if (ic == icmp) 4017 (icmp @0 @1) 4018 (if (ic == ncmp) 4019 (ncmp @0 @1)))))) 4020 4021/* Transform comparisons of the form X - Y CMP 0 to X CMP Y. 4022 ??? The transformation is valid for the other operators if overflow 4023 is undefined for the type, but performing it here badly interacts 4024 with the transformation in fold_cond_expr_with_comparison which 4025 attempts to synthetize ABS_EXPR. */ 4026(for cmp (eq ne) 4027 (for sub (minus pointer_diff) 4028 (simplify 4029 (cmp (sub@2 @0 @1) integer_zerop) 4030 (if (single_use (@2)) 4031 (cmp @0 @1))))) 4032 4033/* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and 4034 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */ 4035(for cmp (lt ge) 4036 (simplify 4037 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop)) 4038 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4039 && !TYPE_UNSIGNED (TREE_TYPE (@0)) 4040 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) 4041 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))) 4042/* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and 4043 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */ 4044(simplify 4045 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop)) 4046 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4047 && !TYPE_UNSIGNED (TREE_TYPE (@0)) 4048 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) 4049 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))) 4050 4051/* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the 4052 signed arithmetic case. That form is created by the compiler 4053 often enough for folding it to be of value. One example is in 4054 computing loop trip counts after Operator Strength Reduction. */ 4055(for cmp (simple_comparison) 4056 scmp (swapped_simple_comparison) 4057 (simplify 4058 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2) 4059 /* Handle unfolded multiplication by zero. */ 4060 (if (integer_zerop (@1)) 4061 (cmp @1 @2) 4062 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4063 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 4064 && single_use (@3)) 4065 /* If @1 is negative we swap the sense of the comparison. */ 4066 (if (tree_int_cst_sgn (@1) < 0) 4067 (scmp @0 @2) 4068 (cmp @0 @2)))))) 4069 4070/* For integral types with undefined overflow fold 4071 x * C1 == C2 into x == C2 / C1 or false. 4072 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring 4073 Z / 2^n Z. */ 4074(for cmp (eq ne) 4075 (simplify 4076 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2) 4077 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4078 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 4079 && wi::to_wide (@1) != 0) 4080 (with { widest_int quot; } 4081 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1), 4082 TYPE_SIGN (TREE_TYPE (@0)), ")) 4083 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); }) 4084 { constant_boolean_node (cmp == NE_EXPR, type); })) 4085 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4086 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) 4087 && (wi::bit_and (wi::to_wide (@1), 1) == 1)) 4088 (cmp @0 4089 { 4090 tree itype = TREE_TYPE (@0); 4091 int p = TYPE_PRECISION (itype); 4092 wide_int m = wi::one (p + 1) << p; 4093 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED); 4094 wide_int i = wide_int::from (wi::mod_inv (a, m), 4095 p, TYPE_SIGN (itype)); 4096 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2))); 4097 }))))) 4098 4099/* Simplify comparison of something with itself. For IEEE 4100 floating-point, we can only do some of these simplifications. */ 4101(for cmp (eq ge le) 4102 (simplify 4103 (cmp @0 @0) 4104 (if (! FLOAT_TYPE_P (TREE_TYPE (@0)) 4105 || ! HONOR_NANS (@0)) 4106 { constant_boolean_node (true, type); } 4107 (if (cmp != EQ_EXPR) 4108 (eq @0 @0))))) 4109(for cmp (ne gt lt) 4110 (simplify 4111 (cmp @0 @0) 4112 (if (cmp != NE_EXPR 4113 || ! FLOAT_TYPE_P (TREE_TYPE (@0)) 4114 || ! HONOR_NANS (@0)) 4115 { constant_boolean_node (false, type); }))) 4116(for cmp (unle unge uneq) 4117 (simplify 4118 (cmp @0 @0) 4119 { constant_boolean_node (true, type); })) 4120(for cmp (unlt ungt) 4121 (simplify 4122 (cmp @0 @0) 4123 (unordered @0 @0))) 4124(simplify 4125 (ltgt @0 @0) 4126 (if (!flag_trapping_math) 4127 { constant_boolean_node (false, type); })) 4128 4129/* x == ~x -> false */ 4130/* x != ~x -> true */ 4131(for cmp (eq ne) 4132 (simplify 4133 (cmp:c @0 (bit_not @0)) 4134 { constant_boolean_node (cmp == NE_EXPR, type); })) 4135 4136/* Fold ~X op ~Y as Y op X. */ 4137(for cmp (simple_comparison) 4138 (simplify 4139 (cmp (bit_not@2 @0) (bit_not@3 @1)) 4140 (if (single_use (@2) && single_use (@3)) 4141 (cmp @1 @0)))) 4142 4143/* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ 4144(for cmp (simple_comparison) 4145 scmp (swapped_simple_comparison) 4146 (simplify 4147 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1) 4148 (if (single_use (@2) 4149 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)) 4150 (scmp @0 (bit_not @1))))) 4151 4152(for cmp (simple_comparison) 4153 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ 4154 (simplify 4155 (cmp (convert@2 @0) (convert? @1)) 4156 (if (FLOAT_TYPE_P (TREE_TYPE (@0)) 4157 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) 4158 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))) 4159 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) 4160 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))) 4161 (with 4162 { 4163 tree type1 = TREE_TYPE (@1); 4164 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1)) 4165 { 4166 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1); 4167 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node) 4168 && exact_real_truncate (TYPE_MODE (float_type_node), &orig)) 4169 type1 = float_type_node; 4170 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node) 4171 && exact_real_truncate (TYPE_MODE (double_type_node), &orig)) 4172 type1 = double_type_node; 4173 } 4174 tree newtype 4175 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1) 4176 ? TREE_TYPE (@0) : type1); 4177 } 4178 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype)) 4179 (cmp (convert:newtype @0) (convert:newtype @1)))))) 4180 4181 (simplify 4182 (cmp @0 REAL_CST@1) 4183 /* IEEE doesn't distinguish +0 and -0 in comparisons. */ 4184 (switch 4185 /* a CMP (-0) -> a CMP 0 */ 4186 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))) 4187 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); })) 4188 /* x != NaN is always true, other ops are always false. */ 4189 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) 4190 && ! HONOR_SNANS (@1)) 4191 { constant_boolean_node (cmp == NE_EXPR, type); }) 4192 /* Fold comparisons against infinity. */ 4193 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1)) 4194 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1)))) 4195 (with 4196 { 4197 REAL_VALUE_TYPE max; 4198 enum tree_code code = cmp; 4199 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)); 4200 if (neg) 4201 code = swap_tree_comparison (code); 4202 } 4203 (switch 4204 /* x > +Inf is always false, if we ignore NaNs or exceptions. */ 4205 (if (code == GT_EXPR 4206 && !(HONOR_NANS (@0) && flag_trapping_math)) 4207 { constant_boolean_node (false, type); }) 4208 (if (code == LE_EXPR) 4209 /* x <= +Inf is always true, if we don't care about NaNs. */ 4210 (if (! HONOR_NANS (@0)) 4211 { constant_boolean_node (true, type); } 4212 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses 4213 an "invalid" exception. */ 4214 (if (!flag_trapping_math) 4215 (eq @0 @0)))) 4216 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but 4217 for == this introduces an exception for x a NaN. */ 4218 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math)) 4219 || code == GE_EXPR) 4220 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } 4221 (if (neg) 4222 (lt @0 { build_real (TREE_TYPE (@0), max); }) 4223 (gt @0 { build_real (TREE_TYPE (@0), max); })))) 4224 /* x < +Inf is always equal to x <= DBL_MAX. */ 4225 (if (code == LT_EXPR) 4226 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } 4227 (if (neg) 4228 (ge @0 { build_real (TREE_TYPE (@0), max); }) 4229 (le @0 { build_real (TREE_TYPE (@0), max); })))) 4230 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces 4231 an exception for x a NaN so use an unordered comparison. */ 4232 (if (code == NE_EXPR) 4233 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } 4234 (if (! HONOR_NANS (@0)) 4235 (if (neg) 4236 (ge @0 { build_real (TREE_TYPE (@0), max); }) 4237 (le @0 { build_real (TREE_TYPE (@0), max); })) 4238 (if (neg) 4239 (unge @0 { build_real (TREE_TYPE (@0), max); }) 4240 (unle @0 { build_real (TREE_TYPE (@0), max); })))))))))) 4241 4242 /* If this is a comparison of a real constant with a PLUS_EXPR 4243 or a MINUS_EXPR of a real constant, we can convert it into a 4244 comparison with a revised real constant as long as no overflow 4245 occurs when unsafe_math_optimizations are enabled. */ 4246 (if (flag_unsafe_math_optimizations) 4247 (for op (plus minus) 4248 (simplify 4249 (cmp (op @0 REAL_CST@1) REAL_CST@2) 4250 (with 4251 { 4252 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR, 4253 TREE_TYPE (@1), @2, @1); 4254 } 4255 (if (tem && !TREE_OVERFLOW (tem)) 4256 (cmp @0 { tem; })))))) 4257 4258 /* Likewise, we can simplify a comparison of a real constant with 4259 a MINUS_EXPR whose first operand is also a real constant, i.e. 4260 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on 4261 floating-point types only if -fassociative-math is set. */ 4262 (if (flag_associative_math) 4263 (simplify 4264 (cmp (minus REAL_CST@0 @1) REAL_CST@2) 4265 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); } 4266 (if (tem && !TREE_OVERFLOW (tem)) 4267 (cmp { tem; } @1))))) 4268 4269 /* Fold comparisons against built-in math functions. */ 4270 (if (flag_unsafe_math_optimizations && ! flag_errno_math) 4271 (for sq (SQRT) 4272 (simplify 4273 (cmp (sq @0) REAL_CST@1) 4274 (switch 4275 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) 4276 (switch 4277 /* sqrt(x) < y is always false, if y is negative. */ 4278 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR) 4279 { constant_boolean_node (false, type); }) 4280 /* sqrt(x) > y is always true, if y is negative and we 4281 don't care about NaNs, i.e. negative values of x. */ 4282 (if (cmp == NE_EXPR || !HONOR_NANS (@0)) 4283 { constant_boolean_node (true, type); }) 4284 /* sqrt(x) > y is the same as x >= 0, if y is negative. */ 4285 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))) 4286 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0)) 4287 (switch 4288 /* sqrt(x) < 0 is always false. */ 4289 (if (cmp == LT_EXPR) 4290 { constant_boolean_node (false, type); }) 4291 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */ 4292 (if (cmp == GE_EXPR && !HONOR_NANS (@0)) 4293 { constant_boolean_node (true, type); }) 4294 /* sqrt(x) <= 0 -> x == 0. */ 4295 (if (cmp == LE_EXPR) 4296 (eq @0 @1)) 4297 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >, 4298 == or !=. In the last case: 4299 4300 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0) 4301 4302 if x is negative or NaN. Due to -funsafe-math-optimizations, 4303 the results for other x follow from natural arithmetic. */ 4304 (cmp @0 @1))) 4305 (if ((cmp == LT_EXPR 4306 || cmp == LE_EXPR 4307 || cmp == GT_EXPR 4308 || cmp == GE_EXPR) 4309 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) 4310 /* Give up for -frounding-math. */ 4311 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0))) 4312 (with 4313 { 4314 REAL_VALUE_TYPE c2; 4315 enum tree_code ncmp = cmp; 4316 const real_format *fmt 4317 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))); 4318 real_arithmetic (&c2, MULT_EXPR, 4319 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); 4320 real_convert (&c2, fmt, &c2); 4321 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c), 4322 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */ 4323 if (!REAL_VALUE_ISINF (c2)) 4324 { 4325 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0), 4326 build_real (TREE_TYPE (@0), c2)); 4327 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST) 4328 ncmp = ERROR_MARK; 4329 else if ((cmp == LT_EXPR || cmp == GE_EXPR) 4330 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1))) 4331 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR; 4332 else if ((cmp == LE_EXPR || cmp == GT_EXPR) 4333 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3))) 4334 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR; 4335 else 4336 { 4337 /* With rounding to even, sqrt of up to 3 different values 4338 gives the same normal result, so in some cases c2 needs 4339 to be adjusted. */ 4340 REAL_VALUE_TYPE c2alt, tow; 4341 if (cmp == LT_EXPR || cmp == GE_EXPR) 4342 tow = dconst0; 4343 else 4344 real_inf (&tow); 4345 real_nextafter (&c2alt, fmt, &c2, &tow); 4346 real_convert (&c2alt, fmt, &c2alt); 4347 if (REAL_VALUE_ISINF (c2alt)) 4348 ncmp = ERROR_MARK; 4349 else 4350 { 4351 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0), 4352 build_real (TREE_TYPE (@0), c2alt)); 4353 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST) 4354 ncmp = ERROR_MARK; 4355 else if (real_equal (&TREE_REAL_CST (c3), 4356 &TREE_REAL_CST (@1))) 4357 c2 = c2alt; 4358 } 4359 } 4360 } 4361 } 4362 (if (cmp == GT_EXPR || cmp == GE_EXPR) 4363 (if (REAL_VALUE_ISINF (c2)) 4364 /* sqrt(x) > y is x == +Inf, when y is very large. */ 4365 (if (HONOR_INFINITIES (@0)) 4366 (eq @0 { build_real (TREE_TYPE (@0), c2); }) 4367 { constant_boolean_node (false, type); }) 4368 /* sqrt(x) > c is the same as x > c*c. */ 4369 (if (ncmp != ERROR_MARK) 4370 (if (ncmp == GE_EXPR) 4371 (ge @0 { build_real (TREE_TYPE (@0), c2); }) 4372 (gt @0 { build_real (TREE_TYPE (@0), c2); })))) 4373 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */ 4374 (if (REAL_VALUE_ISINF (c2)) 4375 (switch 4376 /* sqrt(x) < y is always true, when y is a very large 4377 value and we don't care about NaNs or Infinities. */ 4378 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0)) 4379 { constant_boolean_node (true, type); }) 4380 /* sqrt(x) < y is x != +Inf when y is very large and we 4381 don't care about NaNs. */ 4382 (if (! HONOR_NANS (@0)) 4383 (ne @0 { build_real (TREE_TYPE (@0), c2); })) 4384 /* sqrt(x) < y is x >= 0 when y is very large and we 4385 don't care about Infinities. */ 4386 (if (! HONOR_INFINITIES (@0)) 4387 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })) 4388 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ 4389 (if (GENERIC) 4390 (truth_andif 4391 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) 4392 (ne @0 { build_real (TREE_TYPE (@0), c2); })))) 4393 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ 4394 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0)) 4395 (if (ncmp == LT_EXPR) 4396 (lt @0 { build_real (TREE_TYPE (@0), c2); }) 4397 (le @0 { build_real (TREE_TYPE (@0), c2); })) 4398 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ 4399 (if (ncmp != ERROR_MARK && GENERIC) 4400 (if (ncmp == LT_EXPR) 4401 (truth_andif 4402 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) 4403 (lt @0 { build_real (TREE_TYPE (@0), c2); })) 4404 (truth_andif 4405 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) 4406 (le @0 { build_real (TREE_TYPE (@0), c2); }))))))))))) 4407 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */ 4408 (simplify 4409 (cmp (sq @0) (sq @1)) 4410 (if (! HONOR_NANS (@0)) 4411 (cmp @0 @1)))))) 4412 4413/* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */ 4414(for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) 4415 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne) 4416 (simplify 4417 (cmp (float@0 @1) (float @2)) 4418 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0)) 4419 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))) 4420 (with 4421 { 4422 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)))); 4423 tree type1 = TREE_TYPE (@1); 4424 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED; 4425 tree type2 = TREE_TYPE (@2); 4426 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED; 4427 } 4428 (if (fmt.can_represent_integral_type_p (type1) 4429 && fmt.can_represent_integral_type_p (type2)) 4430 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR) 4431 { constant_boolean_node (cmp == ORDERED_EXPR, type); } 4432 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2) 4433 && type1_signed_p >= type2_signed_p) 4434 (icmp @1 (convert @2)) 4435 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2) 4436 && type1_signed_p <= type2_signed_p) 4437 (icmp (convert:type2 @1) @2) 4438 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2) 4439 && type1_signed_p == type2_signed_p) 4440 (icmp @1 @2)))))))))) 4441 4442/* Optimize various special cases of (FTYPE) N CMP CST. */ 4443(for cmp (lt le eq ne ge gt) 4444 icmp (le le eq ne ge ge) 4445 (simplify 4446 (cmp (float @0) REAL_CST@1) 4447 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1)) 4448 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))) 4449 (with 4450 { 4451 tree itype = TREE_TYPE (@0); 4452 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1)))); 4453 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1); 4454 /* Be careful to preserve any potential exceptions due to 4455 NaNs. qNaNs are ok in == or != context. 4456 TODO: relax under -fno-trapping-math or 4457 -fno-signaling-nans. */ 4458 bool exception_p 4459 = real_isnan (cst) && (cst->signalling 4460 || (cmp != EQ_EXPR && cmp != NE_EXPR)); 4461 } 4462 /* TODO: allow non-fitting itype and SNaNs when 4463 -fno-trapping-math. */ 4464 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p) 4465 (with 4466 { 4467 signop isign = TYPE_SIGN (itype); 4468 REAL_VALUE_TYPE imin, imax; 4469 real_from_integer (&imin, fmt, wi::min_value (itype), isign); 4470 real_from_integer (&imax, fmt, wi::max_value (itype), isign); 4471 4472 REAL_VALUE_TYPE icst; 4473 if (cmp == GT_EXPR || cmp == GE_EXPR) 4474 real_ceil (&icst, fmt, cst); 4475 else if (cmp == LT_EXPR || cmp == LE_EXPR) 4476 real_floor (&icst, fmt, cst); 4477 else 4478 real_trunc (&icst, fmt, cst); 4479 4480 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst); 4481 4482 bool overflow_p = false; 4483 wide_int icst_val 4484 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype)); 4485 } 4486 (switch 4487 /* Optimize cases when CST is outside of ITYPE's range. */ 4488 (if (real_compare (LT_EXPR, cst, &imin)) 4489 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR, 4490 type); }) 4491 (if (real_compare (GT_EXPR, cst, &imax)) 4492 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR, 4493 type); }) 4494 /* Remove cast if CST is an integer representable by ITYPE. */ 4495 (if (cst_int_p) 4496 (cmp @0 { gcc_assert (!overflow_p); 4497 wide_int_to_tree (itype, icst_val); }) 4498 ) 4499 /* When CST is fractional, optimize 4500 (FTYPE) N == CST -> 0 4501 (FTYPE) N != CST -> 1. */ 4502 (if (cmp == EQ_EXPR || cmp == NE_EXPR) 4503 { constant_boolean_node (cmp == NE_EXPR, type); }) 4504 /* Otherwise replace with sensible integer constant. */ 4505 (with 4506 { 4507 gcc_checking_assert (!overflow_p); 4508 } 4509 (icmp @0 { wide_int_to_tree (itype, icst_val); }))))))))) 4510 4511/* Fold A /[ex] B CMP C to A CMP B * C. */ 4512(for cmp (eq ne) 4513 (simplify 4514 (cmp (exact_div @0 @1) INTEGER_CST@2) 4515 (if (!integer_zerop (@1)) 4516 (if (wi::to_wide (@2) == 0) 4517 (cmp @0 @2) 4518 (if (TREE_CODE (@1) == INTEGER_CST) 4519 (with 4520 { 4521 wi::overflow_type ovf; 4522 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1), 4523 TYPE_SIGN (TREE_TYPE (@1)), &ovf); 4524 } 4525 (if (ovf) 4526 { constant_boolean_node (cmp == NE_EXPR, type); } 4527 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); })))))))) 4528(for cmp (lt le gt ge) 4529 (simplify 4530 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2) 4531 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))) 4532 (with 4533 { 4534 wi::overflow_type ovf; 4535 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1), 4536 TYPE_SIGN (TREE_TYPE (@1)), &ovf); 4537 } 4538 (if (ovf) 4539 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0, 4540 TYPE_SIGN (TREE_TYPE (@2))) 4541 != (cmp == LT_EXPR || cmp == LE_EXPR), type); } 4542 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); })))))) 4543 4544/* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0. 4545 4546 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C. 4547 For large C (more than min/B+2^size), this is also true, with the 4548 multiplication computed modulo 2^size. 4549 For intermediate C, this just tests the sign of A. */ 4550(for cmp (lt le gt ge) 4551 cmp2 (ge ge lt lt) 4552 (simplify 4553 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2) 4554 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)) 4555 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0)) 4556 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))) 4557 (with 4558 { 4559 tree utype = TREE_TYPE (@2); 4560 wide_int denom = wi::to_wide (@1); 4561 wide_int right = wi::to_wide (@2); 4562 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom); 4563 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom); 4564 bool small = wi::leu_p (right, smax); 4565 bool large = wi::geu_p (right, smin); 4566 } 4567 (if (small || large) 4568 (cmp (convert:utype @0) (mult @2 (convert @1))) 4569 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })))))) 4570 4571/* Unordered tests if either argument is a NaN. */ 4572(simplify 4573 (bit_ior (unordered @0 @0) (unordered @1 @1)) 4574 (if (types_match (@0, @1)) 4575 (unordered @0 @1))) 4576(simplify 4577 (bit_and (ordered @0 @0) (ordered @1 @1)) 4578 (if (types_match (@0, @1)) 4579 (ordered @0 @1))) 4580(simplify 4581 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1)) 4582 @2) 4583(simplify 4584 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1)) 4585 @2) 4586 4587/* Simple range test simplifications. */ 4588/* A < B || A >= B -> true. */ 4589(for test1 (lt le le le ne ge) 4590 test2 (ge gt ge ne eq ne) 4591 (simplify 4592 (bit_ior:c (test1 @0 @1) (test2 @0 @1)) 4593 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4594 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) 4595 { constant_boolean_node (true, type); }))) 4596/* A < B && A >= B -> false. */ 4597(for test1 (lt lt lt le ne eq) 4598 test2 (ge gt eq gt eq gt) 4599 (simplify 4600 (bit_and:c (test1 @0 @1) (test2 @0 @1)) 4601 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4602 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) 4603 { constant_boolean_node (false, type); }))) 4604 4605/* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0 4606 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0 4607 4608 Note that comparisons 4609 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0 4610 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0 4611 will be canonicalized to above so there's no need to 4612 consider them here. 4613 */ 4614 4615(for cmp (le gt) 4616 eqcmp (eq ne) 4617 (simplify 4618 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3) 4619 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))) 4620 (with 4621 { 4622 tree ty = TREE_TYPE (@0); 4623 unsigned prec = TYPE_PRECISION (ty); 4624 wide_int mask = wi::to_wide (@2, prec); 4625 wide_int rhs = wi::to_wide (@3, prec); 4626 signop sgn = TYPE_SIGN (ty); 4627 } 4628 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn) 4629 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn)) 4630 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); }) 4631 { build_zero_cst (ty); })))))) 4632 4633/* -A CMP -B -> B CMP A. */ 4634(for cmp (tcc_comparison) 4635 scmp (swapped_tcc_comparison) 4636 (simplify 4637 (cmp (negate @0) (negate @1)) 4638 (if (FLOAT_TYPE_P (TREE_TYPE (@0)) 4639 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4640 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) 4641 (scmp @0 @1))) 4642 (simplify 4643 (cmp (negate @0) CONSTANT_CLASS_P@1) 4644 (if (FLOAT_TYPE_P (TREE_TYPE (@0)) 4645 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4646 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) 4647 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); } 4648 (if (tem && !TREE_OVERFLOW (tem)) 4649 (scmp @0 { tem; })))))) 4650 4651/* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */ 4652(for op (eq ne) 4653 (simplify 4654 (op (abs @0) zerop@1) 4655 (op @0 @1))) 4656 4657/* From fold_sign_changed_comparison and fold_widened_comparison. 4658 FIXME: the lack of symmetry is disturbing. */ 4659(for cmp (simple_comparison) 4660 (simplify 4661 (cmp (convert@0 @00) (convert?@1 @10)) 4662 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4663 /* Disable this optimization if we're casting a function pointer 4664 type on targets that require function pointer canonicalization. */ 4665 && !(targetm.have_canonicalize_funcptr_for_compare () 4666 && ((POINTER_TYPE_P (TREE_TYPE (@00)) 4667 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00)))) 4668 || (POINTER_TYPE_P (TREE_TYPE (@10)) 4669 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10)))))) 4670 && single_use (@0)) 4671 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0)) 4672 && (TREE_CODE (@10) == INTEGER_CST 4673 || @1 != @10) 4674 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0)) 4675 || cmp == NE_EXPR 4676 || cmp == EQ_EXPR) 4677 && !POINTER_TYPE_P (TREE_TYPE (@00))) 4678 /* ??? The special-casing of INTEGER_CST conversion was in the original 4679 code and here to avoid a spurious overflow flag on the resulting 4680 constant which fold_convert produces. */ 4681 (if (TREE_CODE (@1) == INTEGER_CST) 4682 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0, 4683 TREE_OVERFLOW (@1)); }) 4684 (cmp @00 (convert @1))) 4685 4686 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00))) 4687 /* If possible, express the comparison in the shorter mode. */ 4688 (if ((cmp == EQ_EXPR || cmp == NE_EXPR 4689 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)) 4690 || (!TYPE_UNSIGNED (TREE_TYPE (@0)) 4691 && TYPE_UNSIGNED (TREE_TYPE (@00)))) 4692 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00)) 4693 || ((TYPE_PRECISION (TREE_TYPE (@00)) 4694 >= TYPE_PRECISION (TREE_TYPE (@10))) 4695 && (TYPE_UNSIGNED (TREE_TYPE (@00)) 4696 == TYPE_UNSIGNED (TREE_TYPE (@10)))) 4697 || (TREE_CODE (@10) == INTEGER_CST 4698 && INTEGRAL_TYPE_P (TREE_TYPE (@00)) 4699 && int_fits_type_p (@10, TREE_TYPE (@00))))) 4700 (cmp @00 (convert @10)) 4701 (if (TREE_CODE (@10) == INTEGER_CST 4702 && INTEGRAL_TYPE_P (TREE_TYPE (@00)) 4703 && !int_fits_type_p (@10, TREE_TYPE (@00))) 4704 (with 4705 { 4706 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); 4707 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); 4708 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10)); 4709 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min)); 4710 } 4711 (if (above || below) 4712 (if (cmp == EQ_EXPR || cmp == NE_EXPR) 4713 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); } 4714 (if (cmp == LT_EXPR || cmp == LE_EXPR) 4715 { constant_boolean_node (above ? true : false, type); } 4716 (if (cmp == GT_EXPR || cmp == GE_EXPR) 4717 { constant_boolean_node (above ? false : true, type); })))))))))))) 4718 4719(for cmp (eq ne) 4720 (simplify 4721 /* SSA names are canonicalized to 2nd place. */ 4722 (cmp addr@0 SSA_NAME@1) 4723 (with 4724 { poly_int64 off; tree base; } 4725 /* A local variable can never be pointed to by 4726 the default SSA name of an incoming parameter. */ 4727 (if (SSA_NAME_IS_DEFAULT_DEF (@1) 4728 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL 4729 && (base = get_base_address (TREE_OPERAND (@0, 0))) 4730 && TREE_CODE (base) == VAR_DECL 4731 && auto_var_in_fn_p (base, current_function_decl)) 4732 (if (cmp == NE_EXPR) 4733 { constant_boolean_node (true, type); } 4734 { constant_boolean_node (false, type); }) 4735 /* If the address is based on @1 decide using the offset. */ 4736 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off)) 4737 && TREE_CODE (base) == MEM_REF 4738 && TREE_OPERAND (base, 0) == @1) 4739 (with { off += mem_ref_offset (base).force_shwi (); } 4740 (if (known_ne (off, 0)) 4741 { constant_boolean_node (cmp == NE_EXPR, type); } 4742 (if (known_eq (off, 0)) 4743 { constant_boolean_node (cmp == EQ_EXPR, type); })))))))) 4744 4745/* Equality compare simplifications from fold_binary */ 4746(for cmp (eq ne) 4747 4748 /* If we have (A | C) == D where C & ~D != 0, convert this into 0. 4749 Similarly for NE_EXPR. */ 4750 (simplify 4751 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2) 4752 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) 4753 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0) 4754 { constant_boolean_node (cmp == NE_EXPR, type); })) 4755 4756 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */ 4757 (simplify 4758 (cmp (bit_xor @0 @1) integer_zerop) 4759 (cmp @0 @1)) 4760 4761 /* (X ^ Y) == Y becomes X == 0. 4762 Likewise (X ^ Y) == X becomes Y == 0. */ 4763 (simplify 4764 (cmp:c (bit_xor:c @0 @1) @0) 4765 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); })) 4766 4767 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */ 4768 (simplify 4769 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2) 4770 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))) 4771 (cmp @0 (bit_xor @1 (convert @2))))) 4772 4773 (simplify 4774 (cmp (convert? addr@0) integer_zerop) 4775 (if (tree_single_nonzero_warnv_p (@0, NULL)) 4776 { constant_boolean_node (cmp == NE_EXPR, type); })) 4777 4778 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */ 4779 (simplify 4780 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2)) 4781 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); }))) 4782 4783/* (X < 0) != (Y < 0) into (X ^ Y) < 0. 4784 (X >= 0) != (Y >= 0) into (X ^ Y) < 0. 4785 (X < 0) == (Y < 0) into (X ^ Y) >= 0. 4786 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */ 4787(for cmp (eq ne) 4788 ncmp (ge lt) 4789 (for sgncmp (ge lt) 4790 (simplify 4791 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop)) 4792 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4793 && !TYPE_UNSIGNED (TREE_TYPE (@0)) 4794 && types_match (@0, @1)) 4795 (ncmp (bit_xor @0 @1) @2))))) 4796/* (X < 0) == (Y >= 0) into (X ^ Y) < 0. 4797 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */ 4798(for cmp (eq ne) 4799 ncmp (lt ge) 4800 (simplify 4801 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop)) 4802 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4803 && !TYPE_UNSIGNED (TREE_TYPE (@0)) 4804 && types_match (@0, @1)) 4805 (ncmp (bit_xor @0 @1) @2)))) 4806 4807/* If we have (A & C) == C where C is a power of 2, convert this into 4808 (A & C) != 0. Similarly for NE_EXPR. */ 4809(for cmp (eq ne) 4810 icmp (ne eq) 4811 (simplify 4812 (cmp (bit_and@2 @0 integer_pow2p@1) @1) 4813 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); }))) 4814 4815/* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2, 4816 convert this into a shift followed by ANDing with D. */ 4817(simplify 4818 (cond 4819 (ne (bit_and @0 integer_pow2p@1) integer_zerop) 4820 INTEGER_CST@2 integer_zerop) 4821 (if (integer_pow2p (@2)) 4822 (with { 4823 int shift = (wi::exact_log2 (wi::to_wide (@2)) 4824 - wi::exact_log2 (wi::to_wide (@1))); 4825 } 4826 (if (shift > 0) 4827 (bit_and 4828 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2) 4829 (bit_and 4830 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) 4831 @2))))) 4832 4833/* If we have (A & C) != 0 where C is the sign bit of A, convert 4834 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */ 4835(for cmp (eq ne) 4836 ncmp (ge lt) 4837 (simplify 4838 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop) 4839 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 4840 && type_has_mode_precision_p (TREE_TYPE (@0)) 4841 && element_precision (@2) >= element_precision (@0) 4842 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0))) 4843 (with { tree stype = signed_type_for (TREE_TYPE (@0)); } 4844 (ncmp (convert:stype @0) { build_zero_cst (stype); }))))) 4845 4846/* If we have A < 0 ? C : 0 where C is a power of 2, convert 4847 this into a right shift or sign extension followed by ANDing with C. */ 4848(simplify 4849 (cond 4850 (lt @0 integer_zerop) 4851 INTEGER_CST@1 integer_zerop) 4852 (if (integer_pow2p (@1) 4853 && !TYPE_UNSIGNED (TREE_TYPE (@0))) 4854 (with { 4855 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1; 4856 } 4857 (if (shift >= 0) 4858 (bit_and 4859 (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) 4860 @1) 4861 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure 4862 sign extension followed by AND with C will achieve the effect. */ 4863 (bit_and (convert @0) @1))))) 4864 4865/* When the addresses are not directly of decls compare base and offset. 4866 This implements some remaining parts of fold_comparison address 4867 comparisons but still no complete part of it. Still it is good 4868 enough to make fold_stmt not regress when not dispatching to fold_binary. */ 4869(for cmp (simple_comparison) 4870 (simplify 4871 (cmp (convert1?@2 addr@0) (convert2? addr@1)) 4872 (with 4873 { 4874 poly_int64 off0, off1; 4875 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0); 4876 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1); 4877 if (base0 && TREE_CODE (base0) == MEM_REF) 4878 { 4879 off0 += mem_ref_offset (base0).force_shwi (); 4880 base0 = TREE_OPERAND (base0, 0); 4881 } 4882 if (base1 && TREE_CODE (base1) == MEM_REF) 4883 { 4884 off1 += mem_ref_offset (base1).force_shwi (); 4885 base1 = TREE_OPERAND (base1, 0); 4886 } 4887 } 4888 (if (base0 && base1) 4889 (with 4890 { 4891 int equal = 2; 4892 /* Punt in GENERIC on variables with value expressions; 4893 the value expressions might point to fields/elements 4894 of other vars etc. */ 4895 if (GENERIC 4896 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0)) 4897 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1)))) 4898 ; 4899 else if (decl_in_symtab_p (base0) 4900 && decl_in_symtab_p (base1)) 4901 equal = symtab_node::get_create (base0) 4902 ->equal_address_to (symtab_node::get_create (base1)); 4903 else if ((DECL_P (base0) 4904 || TREE_CODE (base0) == SSA_NAME 4905 || TREE_CODE (base0) == STRING_CST) 4906 && (DECL_P (base1) 4907 || TREE_CODE (base1) == SSA_NAME 4908 || TREE_CODE (base1) == STRING_CST)) 4909 equal = (base0 == base1); 4910 if (equal == 0) 4911 { 4912 HOST_WIDE_INT ioff0 = -1, ioff1 = -1; 4913 off0.is_constant (&ioff0); 4914 off1.is_constant (&ioff1); 4915 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST) 4916 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1)) 4917 || (TREE_CODE (base0) == STRING_CST 4918 && TREE_CODE (base1) == STRING_CST 4919 && ioff0 >= 0 && ioff1 >= 0 4920 && ioff0 < TREE_STRING_LENGTH (base0) 4921 && ioff1 < TREE_STRING_LENGTH (base1) 4922 /* This is a too conservative test that the STRING_CSTs 4923 will not end up being string-merged. */ 4924 && strncmp (TREE_STRING_POINTER (base0) + ioff0, 4925 TREE_STRING_POINTER (base1) + ioff1, 4926 MIN (TREE_STRING_LENGTH (base0) - ioff0, 4927 TREE_STRING_LENGTH (base1) - ioff1)) != 0)) 4928 ; 4929 else if (!DECL_P (base0) || !DECL_P (base1)) 4930 equal = 2; 4931 else if (cmp != EQ_EXPR && cmp != NE_EXPR) 4932 equal = 2; 4933 /* If this is a pointer comparison, ignore for now even 4934 valid equalities where one pointer is the offset zero 4935 of one object and the other to one past end of another one. */ 4936 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2))) 4937 ; 4938 /* Assume that automatic variables can't be adjacent to global 4939 variables. */ 4940 else if (is_global_var (base0) != is_global_var (base1)) 4941 ; 4942 else 4943 { 4944 tree sz0 = DECL_SIZE_UNIT (base0); 4945 tree sz1 = DECL_SIZE_UNIT (base1); 4946 /* If sizes are unknown, e.g. VLA or not representable, 4947 punt. */ 4948 if (!tree_fits_poly_int64_p (sz0) 4949 || !tree_fits_poly_int64_p (sz1)) 4950 equal = 2; 4951 else 4952 { 4953 poly_int64 size0 = tree_to_poly_int64 (sz0); 4954 poly_int64 size1 = tree_to_poly_int64 (sz1); 4955 /* If one offset is pointing (or could be) to the beginning 4956 of one object and the other is pointing to one past the 4957 last byte of the other object, punt. */ 4958 if (maybe_eq (off0, 0) && maybe_eq (off1, size1)) 4959 equal = 2; 4960 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0)) 4961 equal = 2; 4962 /* If both offsets are the same, there are some cases 4963 we know that are ok. Either if we know they aren't 4964 zero, or if we know both sizes are no zero. */ 4965 if (equal == 2 4966 && known_eq (off0, off1) 4967 && (known_ne (off0, 0) 4968 || (known_ne (size0, 0) && known_ne (size1, 0)))) 4969 equal = 0; 4970 } 4971 } 4972 } 4973 } 4974 (if (equal == 1 4975 && (cmp == EQ_EXPR || cmp == NE_EXPR 4976 /* If the offsets are equal we can ignore overflow. */ 4977 || known_eq (off0, off1) 4978 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 4979 /* Or if we compare using pointers to decls or strings. */ 4980 || (POINTER_TYPE_P (TREE_TYPE (@2)) 4981 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))) 4982 (switch 4983 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1))) 4984 { constant_boolean_node (known_eq (off0, off1), type); }) 4985 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1))) 4986 { constant_boolean_node (known_ne (off0, off1), type); }) 4987 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1))) 4988 { constant_boolean_node (known_lt (off0, off1), type); }) 4989 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1))) 4990 { constant_boolean_node (known_le (off0, off1), type); }) 4991 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1))) 4992 { constant_boolean_node (known_ge (off0, off1), type); }) 4993 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1))) 4994 { constant_boolean_node (known_gt (off0, off1), type); })) 4995 (if (equal == 0) 4996 (switch 4997 (if (cmp == EQ_EXPR) 4998 { constant_boolean_node (false, type); }) 4999 (if (cmp == NE_EXPR) 5000 { constant_boolean_node (true, type); }))))))))) 5001 5002/* Simplify pointer equality compares using PTA. */ 5003(for neeq (ne eq) 5004 (simplify 5005 (neeq @0 @1) 5006 (if (POINTER_TYPE_P (TREE_TYPE (@0)) 5007 && ptrs_compare_unequal (@0, @1)) 5008 { constant_boolean_node (neeq != EQ_EXPR, type); }))) 5009 5010/* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST. 5011 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST. 5012 Disable the transform if either operand is pointer to function. 5013 This broke pr22051-2.c for arm where function pointer 5014 canonicalizaion is not wanted. */ 5015 5016(for cmp (ne eq) 5017 (simplify 5018 (cmp (convert @0) INTEGER_CST@1) 5019 (if (((POINTER_TYPE_P (TREE_TYPE (@0)) 5020 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0))) 5021 && INTEGRAL_TYPE_P (TREE_TYPE (@1)) 5022 /* Don't perform this optimization in GENERIC if @0 has reference 5023 type when sanitizing. See PR101210. */ 5024 && !(GENERIC 5025 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE 5026 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT)))) 5027 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 5028 && POINTER_TYPE_P (TREE_TYPE (@1)) 5029 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1))))) 5030 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) 5031 (cmp @0 (convert @1))))) 5032 5033/* Non-equality compare simplifications from fold_binary */ 5034(for cmp (lt gt le ge) 5035 /* Comparisons with the highest or lowest possible integer of 5036 the specified precision will have known values. */ 5037 (simplify 5038 (cmp (convert?@2 @0) uniform_integer_cst_p@1) 5039 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) 5040 || POINTER_TYPE_P (TREE_TYPE (@1)) 5041 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1))) 5042 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))) 5043 (with 5044 { 5045 tree cst = uniform_integer_cst_p (@1); 5046 tree arg1_type = TREE_TYPE (cst); 5047 unsigned int prec = TYPE_PRECISION (arg1_type); 5048 wide_int max = wi::max_value (arg1_type); 5049 wide_int signed_max = wi::max_value (prec, SIGNED); 5050 wide_int min = wi::min_value (arg1_type); 5051 } 5052 (switch 5053 (if (wi::to_wide (cst) == max) 5054 (switch 5055 (if (cmp == GT_EXPR) 5056 { constant_boolean_node (false, type); }) 5057 (if (cmp == GE_EXPR) 5058 (eq @2 @1)) 5059 (if (cmp == LE_EXPR) 5060 { constant_boolean_node (true, type); }) 5061 (if (cmp == LT_EXPR) 5062 (ne @2 @1)))) 5063 (if (wi::to_wide (cst) == min) 5064 (switch 5065 (if (cmp == LT_EXPR) 5066 { constant_boolean_node (false, type); }) 5067 (if (cmp == LE_EXPR) 5068 (eq @2 @1)) 5069 (if (cmp == GE_EXPR) 5070 { constant_boolean_node (true, type); }) 5071 (if (cmp == GT_EXPR) 5072 (ne @2 @1)))) 5073 (if (wi::to_wide (cst) == max - 1) 5074 (switch 5075 (if (cmp == GT_EXPR) 5076 (eq @2 { build_uniform_cst (TREE_TYPE (@1), 5077 wide_int_to_tree (TREE_TYPE (cst), 5078 wi::to_wide (cst) 5079 + 1)); })) 5080 (if (cmp == LE_EXPR) 5081 (ne @2 { build_uniform_cst (TREE_TYPE (@1), 5082 wide_int_to_tree (TREE_TYPE (cst), 5083 wi::to_wide (cst) 5084 + 1)); })))) 5085 (if (wi::to_wide (cst) == min + 1) 5086 (switch 5087 (if (cmp == GE_EXPR) 5088 (ne @2 { build_uniform_cst (TREE_TYPE (@1), 5089 wide_int_to_tree (TREE_TYPE (cst), 5090 wi::to_wide (cst) 5091 - 1)); })) 5092 (if (cmp == LT_EXPR) 5093 (eq @2 { build_uniform_cst (TREE_TYPE (@1), 5094 wide_int_to_tree (TREE_TYPE (cst), 5095 wi::to_wide (cst) 5096 - 1)); })))) 5097 (if (wi::to_wide (cst) == signed_max 5098 && TYPE_UNSIGNED (arg1_type) 5099 /* We will flip the signedness of the comparison operator 5100 associated with the mode of @1, so the sign bit is 5101 specified by this mode. Check that @1 is the signed 5102 max associated with this sign bit. */ 5103 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type)) 5104 /* signed_type does not work on pointer types. */ 5105 && INTEGRAL_TYPE_P (arg1_type)) 5106 /* The following case also applies to X < signed_max+1 5107 and X >= signed_max+1 because previous transformations. */ 5108 (if (cmp == LE_EXPR || cmp == GT_EXPR) 5109 (with { tree st = signed_type_for (TREE_TYPE (@1)); } 5110 (switch 5111 (if (cst == @1 && cmp == LE_EXPR) 5112 (ge (convert:st @0) { build_zero_cst (st); })) 5113 (if (cst == @1 && cmp == GT_EXPR) 5114 (lt (convert:st @0) { build_zero_cst (st); })) 5115 (if (cmp == LE_EXPR) 5116 (ge (view_convert:st @0) { build_zero_cst (st); })) 5117 (if (cmp == GT_EXPR) 5118 (lt (view_convert:st @0) { build_zero_cst (st); }))))))))))) 5119 5120(for cmp (unordered ordered unlt unle ungt unge uneq ltgt) 5121 /* If the second operand is NaN, the result is constant. */ 5122 (simplify 5123 (cmp @0 REAL_CST@1) 5124 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) 5125 && (cmp != LTGT_EXPR || ! flag_trapping_math)) 5126 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR 5127 ? false : true, type); }))) 5128 5129/* Fold UNORDERED if either operand must be NaN, or neither can be. */ 5130(simplify 5131 (unordered @0 @1) 5132 (switch 5133 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1)) 5134 { constant_boolean_node (true, type); }) 5135 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1)) 5136 { constant_boolean_node (false, type); }))) 5137 5138/* Fold ORDERED if either operand must be NaN, or neither can be. */ 5139(simplify 5140 (ordered @0 @1) 5141 (switch 5142 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1)) 5143 { constant_boolean_node (false, type); }) 5144 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1)) 5145 { constant_boolean_node (true, type); }))) 5146 5147/* bool_var != 0 becomes bool_var. */ 5148(simplify 5149 (ne @0 integer_zerop) 5150 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE 5151 && types_match (type, TREE_TYPE (@0))) 5152 (non_lvalue @0))) 5153/* bool_var == 1 becomes bool_var. */ 5154(simplify 5155 (eq @0 integer_onep) 5156 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE 5157 && types_match (type, TREE_TYPE (@0))) 5158 (non_lvalue @0))) 5159/* Do not handle 5160 bool_var == 0 becomes !bool_var or 5161 bool_var != 1 becomes !bool_var 5162 here because that only is good in assignment context as long 5163 as we require a tcc_comparison in GIMPLE_CONDs where we'd 5164 replace if (x == 0) with tem = ~x; if (tem != 0) which is 5165 clearly less optimal and which we'll transform again in forwprop. */ 5166 5167/* When one argument is a constant, overflow detection can be simplified. 5168 Currently restricted to single use so as not to interfere too much with 5169 ADD_OVERFLOW detection in tree-ssa-math-opts.c. 5170 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */ 5171(for cmp (lt le ge gt) 5172 out (gt gt le le) 5173 (simplify 5174 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0) 5175 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2)) 5176 && types_match (TREE_TYPE (@0), TREE_TYPE (@3)) 5177 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0)) 5178 && wi::to_wide (@1) != 0 5179 && single_use (@2)) 5180 (with { 5181 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); 5182 signop sign = TYPE_SIGN (TREE_TYPE (@0)); 5183 } 5184 (out @0 { wide_int_to_tree (TREE_TYPE (@0), 5185 wi::max_value (prec, sign) 5186 - wi::to_wide (@1)); }))))) 5187 5188/* To detect overflow in unsigned A - B, A < B is simpler than A - B > A. 5189 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c 5190 expects the long form, so we restrict the transformation for now. */ 5191(for cmp (gt le) 5192 (simplify 5193 (cmp:c (minus@2 @0 @1) @0) 5194 (if (single_use (@2) 5195 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 5196 && TYPE_UNSIGNED (TREE_TYPE (@0))) 5197 (cmp @1 @0)))) 5198 5199/* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */ 5200(for cmp (ge lt) 5201 (simplify 5202 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0) 5203 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) 5204 && TYPE_UNSIGNED (TREE_TYPE (@0))) 5205 (cmp @1 @0)))) 5206 5207/* Testing for overflow is unnecessary if we already know the result. */ 5208/* A - B > A */ 5209(for cmp (gt le) 5210 out (ne eq) 5211 (simplify 5212 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0) 5213 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) 5214 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) 5215 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) 5216/* A + B < A */ 5217(for cmp (lt ge) 5218 out (ne eq) 5219 (simplify 5220 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0) 5221 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) 5222 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) 5223 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) 5224 5225/* For unsigned operands, -1 / B < A checks whether A * B would overflow. 5226 Simplify it to __builtin_mul_overflow (A, B, <unused>). */ 5227(for cmp (lt ge) 5228 out (ne eq) 5229 (simplify 5230 (cmp:c (trunc_div:s integer_all_onesp @1) @0) 5231 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0))) 5232 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); } 5233 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); }))))) 5234 5235/* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type 5236 is at least twice as wide as type of A and B, simplify to 5237 __builtin_mul_overflow (A, B, <unused>). */ 5238(for cmp (eq ne) 5239 (simplify 5240 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2) 5241 integer_zerop) 5242 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 5243 && INTEGRAL_TYPE_P (TREE_TYPE (@3)) 5244 && TYPE_UNSIGNED (TREE_TYPE (@0)) 5245 && (TYPE_PRECISION (TREE_TYPE (@3)) 5246 >= 2 * TYPE_PRECISION (TREE_TYPE (@0))) 5247 && tree_fits_uhwi_p (@2) 5248 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0)) 5249 && types_match (@0, @1) 5250 && type_has_mode_precision_p (TREE_TYPE (@0)) 5251 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0))) 5252 != CODE_FOR_nothing)) 5253 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); } 5254 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); }))))) 5255 5256/* Simplification of math builtins. These rules must all be optimizations 5257 as well as IL simplifications. If there is a possibility that the new 5258 form could be a pessimization, the rule should go in the canonicalization 5259 section that follows this one. 5260 5261 Rules can generally go in this section if they satisfy one of 5262 the following: 5263 5264 - the rule describes an identity 5265 5266 - the rule replaces calls with something as simple as addition or 5267 multiplication 5268 5269 - the rule contains unary calls only and simplifies the surrounding 5270 arithmetic. (The idea here is to exclude non-unary calls in which 5271 one operand is constant and in which the call is known to be cheap 5272 when the operand has that value.) */ 5273 5274(if (flag_unsafe_math_optimizations) 5275 /* Simplify sqrt(x) * sqrt(x) -> x. */ 5276 (simplify 5277 (mult (SQRT_ALL@1 @0) @1) 5278 (if (!tree_expr_maybe_signaling_nan_p (@0)) 5279 @0)) 5280 5281 (for op (plus minus) 5282 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */ 5283 (simplify 5284 (op (rdiv @0 @1) 5285 (rdiv @2 @1)) 5286 (rdiv (op @0 @2) @1))) 5287 5288 (for cmp (lt le gt ge) 5289 neg_cmp (gt ge lt le) 5290 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */ 5291 (simplify 5292 (cmp (mult @0 REAL_CST@1) REAL_CST@2) 5293 (with 5294 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); } 5295 (if (tem 5296 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem)) 5297 || (real_zerop (tem) && !real_zerop (@1)))) 5298 (switch 5299 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1))) 5300 (cmp @0 { tem; })) 5301 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0)) 5302 (neg_cmp @0 { tem; }))))))) 5303 5304 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */ 5305 (for root (SQRT CBRT) 5306 (simplify 5307 (mult (root:s @0) (root:s @1)) 5308 (root (mult @0 @1)))) 5309 5310 /* Simplify expN(x) * expN(y) -> expN(x+y). */ 5311 (for exps (EXP EXP2 EXP10 POW10) 5312 (simplify 5313 (mult (exps:s @0) (exps:s @1)) 5314 (exps (plus @0 @1)))) 5315 5316 /* Simplify a/root(b/c) into a*root(c/b). */ 5317 (for root (SQRT CBRT) 5318 (simplify 5319 (rdiv @0 (root:s (rdiv:s @1 @2))) 5320 (mult @0 (root (rdiv @2 @1))))) 5321 5322 /* Simplify x/expN(y) into x*expN(-y). */ 5323 (for exps (EXP EXP2 EXP10 POW10) 5324 (simplify 5325 (rdiv @0 (exps:s @1)) 5326 (mult @0 (exps (negate @1))))) 5327 5328 (for logs (LOG LOG2 LOG10 LOG10) 5329 exps (EXP EXP2 EXP10 POW10) 5330 /* logN(expN(x)) -> x. */ 5331 (simplify 5332 (logs (exps @0)) 5333 @0) 5334 /* expN(logN(x)) -> x. */ 5335 (simplify 5336 (exps (logs @0)) 5337 @0)) 5338 5339 /* Optimize logN(func()) for various exponential functions. We 5340 want to determine the value "x" and the power "exponent" in 5341 order to transform logN(x**exponent) into exponent*logN(x). */ 5342 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10) 5343 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2) 5344 (simplify 5345 (logs (exps @0)) 5346 (if (SCALAR_FLOAT_TYPE_P (type)) 5347 (with { 5348 tree x; 5349 switch (exps) 5350 { 5351 CASE_CFN_EXP: 5352 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */ 5353 x = build_real_truncate (type, dconst_e ()); 5354 break; 5355 CASE_CFN_EXP2: 5356 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */ 5357 x = build_real (type, dconst2); 5358 break; 5359 CASE_CFN_EXP10: 5360 CASE_CFN_POW10: 5361 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */ 5362 { 5363 REAL_VALUE_TYPE dconst10; 5364 real_from_integer (&dconst10, VOIDmode, 10, SIGNED); 5365 x = build_real (type, dconst10); 5366 } 5367 break; 5368 default: 5369 gcc_unreachable (); 5370 } 5371 } 5372 (mult (logs { x; }) @0))))) 5373 5374 (for logs (LOG LOG 5375 LOG2 LOG2 5376 LOG10 LOG10) 5377 exps (SQRT CBRT) 5378 (simplify 5379 (logs (exps @0)) 5380 (if (SCALAR_FLOAT_TYPE_P (type)) 5381 (with { 5382 tree x; 5383 switch (exps) 5384 { 5385 CASE_CFN_SQRT: 5386 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */ 5387 x = build_real (type, dconsthalf); 5388 break; 5389 CASE_CFN_CBRT: 5390 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */ 5391 x = build_real_truncate (type, dconst_third ()); 5392 break; 5393 default: 5394 gcc_unreachable (); 5395 } 5396 } 5397 (mult { x; } (logs @0)))))) 5398 5399 /* logN(pow(x,exponent)) -> exponent*logN(x). */ 5400 (for logs (LOG LOG2 LOG10) 5401 pows (POW) 5402 (simplify 5403 (logs (pows @0 @1)) 5404 (mult @1 (logs @0)))) 5405 5406 /* pow(C,x) -> exp(log(C)*x) if C > 0, 5407 or if C is a positive power of 2, 5408 pow(C,x) -> exp2(log2(C)*x). */ 5409#if GIMPLE 5410 (for pows (POW) 5411 exps (EXP) 5412 logs (LOG) 5413 exp2s (EXP2) 5414 log2s (LOG2) 5415 (simplify 5416 (pows REAL_CST@0 @1) 5417 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0) 5418 && real_isfinite (TREE_REAL_CST_PTR (@0)) 5419 /* As libmvec doesn't have a vectorized exp2, defer optimizing 5420 the use_exp2 case until after vectorization. It seems actually 5421 beneficial for all constants to postpone this until later, 5422 because exp(log(C)*x), while faster, will have worse precision 5423 and if x folds into a constant too, that is unnecessary 5424 pessimization. */ 5425 && canonicalize_math_after_vectorization_p ()) 5426 (with { 5427 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0); 5428 bool use_exp2 = false; 5429 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0)) 5430 && value->cl == rvc_normal) 5431 { 5432 REAL_VALUE_TYPE frac_rvt = *value; 5433 SET_REAL_EXP (&frac_rvt, 1); 5434 if (real_equal (&frac_rvt, &dconst1)) 5435 use_exp2 = true; 5436 } 5437 } 5438 (if (!use_exp2) 5439 (if (optimize_pow_to_exp (@0, @1)) 5440 (exps (mult (logs @0) @1))) 5441 (exp2s (mult (log2s @0) @1))))))) 5442#endif 5443 5444 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */ 5445 (for pows (POW) 5446 exps (EXP EXP2 EXP10 POW10) 5447 logs (LOG LOG2 LOG10 LOG10) 5448 (simplify 5449 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2)) 5450 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0) 5451 && real_isfinite (TREE_REAL_CST_PTR (@0))) 5452 (exps (plus (mult (logs @0) @1) @2))))) 5453 5454 (for sqrts (SQRT) 5455 cbrts (CBRT) 5456 pows (POW) 5457 exps (EXP EXP2 EXP10 POW10) 5458 /* sqrt(expN(x)) -> expN(x*0.5). */ 5459 (simplify 5460 (sqrts (exps @0)) 5461 (exps (mult @0 { build_real (type, dconsthalf); }))) 5462 /* cbrt(expN(x)) -> expN(x/3). */ 5463 (simplify 5464 (cbrts (exps @0)) 5465 (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))) 5466 /* pow(expN(x), y) -> expN(x*y). */ 5467 (simplify 5468 (pows (exps @0) @1) 5469 (exps (mult @0 @1)))) 5470 5471 /* tan(atan(x)) -> x. */ 5472 (for tans (TAN) 5473 atans (ATAN) 5474 (simplify 5475 (tans (atans @0)) 5476 @0))) 5477 5478 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */ 5479 (for sins (SIN) 5480 atans (ATAN) 5481 sqrts (SQRT) 5482 copysigns (COPYSIGN) 5483 (simplify 5484 (sins (atans:s @0)) 5485 (with 5486 { 5487 REAL_VALUE_TYPE r_cst; 5488 build_sinatan_real (&r_cst, type); 5489 tree t_cst = build_real (type, r_cst); 5490 tree t_one = build_one_cst (type); 5491 } 5492 (if (SCALAR_FLOAT_TYPE_P (type)) 5493 (cond (lt (abs @0) { t_cst; }) 5494 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; }))) 5495 (copysigns { t_one; } @0)))))) 5496 5497/* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */ 5498 (for coss (COS) 5499 atans (ATAN) 5500 sqrts (SQRT) 5501 copysigns (COPYSIGN) 5502 (simplify 5503 (coss (atans:s @0)) 5504 (with 5505 { 5506 REAL_VALUE_TYPE r_cst; 5507 build_sinatan_real (&r_cst, type); 5508 tree t_cst = build_real (type, r_cst); 5509 tree t_one = build_one_cst (type); 5510 tree t_zero = build_zero_cst (type); 5511 } 5512 (if (SCALAR_FLOAT_TYPE_P (type)) 5513 (cond (lt (abs @0) { t_cst; }) 5514 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; }))) 5515 (copysigns { t_zero; } @0)))))) 5516 5517 (if (!flag_errno_math) 5518 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */ 5519 (for sinhs (SINH) 5520 atanhs (ATANH) 5521 sqrts (SQRT) 5522 (simplify 5523 (sinhs (atanhs:s @0)) 5524 (with { tree t_one = build_one_cst (type); } 5525 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))) 5526 5527 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */ 5528 (for coshs (COSH) 5529 atanhs (ATANH) 5530 sqrts (SQRT) 5531 (simplify 5532 (coshs (atanhs:s @0)) 5533 (with { tree t_one = build_one_cst (type); } 5534 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))) 5535 5536/* cabs(x+0i) or cabs(0+xi) -> abs(x). */ 5537(simplify 5538 (CABS (complex:C @0 real_zerop@1)) 5539 (abs @0)) 5540 5541/* trunc(trunc(x)) -> trunc(x), etc. */ 5542(for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL) 5543 (simplify 5544 (fns (fns @0)) 5545 (fns @0))) 5546/* f(x) -> x if x is integer valued and f does nothing for such values. */ 5547(for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL) 5548 (simplify 5549 (fns integer_valued_real_p@0) 5550 @0)) 5551 5552/* hypot(x,0) and hypot(0,x) -> abs(x). */ 5553(simplify 5554 (HYPOT:c @0 real_zerop@1) 5555 (abs @0)) 5556 5557/* pow(1,x) -> 1. */ 5558(simplify 5559 (POW real_onep@0 @1) 5560 @0) 5561 5562(simplify 5563 /* copysign(x,x) -> x. */ 5564 (COPYSIGN_ALL @0 @0) 5565 @0) 5566 5567(simplify 5568 /* copysign(x,-x) -> -x. */ 5569 (COPYSIGN_ALL @0 (negate@1 @0)) 5570 @1) 5571 5572(simplify 5573 /* copysign(x,y) -> fabs(x) if y is nonnegative. */ 5574 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1) 5575 (abs @0)) 5576 5577(for scale (LDEXP SCALBN SCALBLN) 5578 /* ldexp(0, x) -> 0. */ 5579 (simplify 5580 (scale real_zerop@0 @1) 5581 @0) 5582 /* ldexp(x, 0) -> x. */ 5583 (simplify 5584 (scale @0 integer_zerop@1) 5585 @0) 5586 /* ldexp(x, y) -> x if x is +-Inf or NaN. */ 5587 (simplify 5588 (scale REAL_CST@0 @1) 5589 (if (!real_isfinite (TREE_REAL_CST_PTR (@0))) 5590 @0))) 5591 5592/* Canonicalization of sequences of math builtins. These rules represent 5593 IL simplifications but are not necessarily optimizations. 5594 5595 The sincos pass is responsible for picking "optimal" implementations 5596 of math builtins, which may be more complicated and can sometimes go 5597 the other way, e.g. converting pow into a sequence of sqrts. 5598 We only want to do these canonicalizations before the pass has run. */ 5599 5600(if (flag_unsafe_math_optimizations && canonicalize_math_p ()) 5601 /* Simplify tan(x) * cos(x) -> sin(x). */ 5602 (simplify 5603 (mult:c (TAN:s @0) (COS:s @0)) 5604 (SIN @0)) 5605 5606 /* Simplify x * pow(x,c) -> pow(x,c+1). */ 5607 (simplify 5608 (mult:c @0 (POW:s @0 REAL_CST@1)) 5609 (if (!TREE_OVERFLOW (@1)) 5610 (POW @0 (plus @1 { build_one_cst (type); })))) 5611 5612 /* Simplify sin(x) / cos(x) -> tan(x). */ 5613 (simplify 5614 (rdiv (SIN:s @0) (COS:s @0)) 5615 (TAN @0)) 5616 5617 /* Simplify sinh(x) / cosh(x) -> tanh(x). */ 5618 (simplify 5619 (rdiv (SINH:s @0) (COSH:s @0)) 5620 (TANH @0)) 5621 5622 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */ 5623 (simplify 5624 (rdiv (TANH:s @0) (SINH:s @0)) 5625 (rdiv {build_one_cst (type);} (COSH @0))) 5626 5627 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */ 5628 (simplify 5629 (rdiv (COS:s @0) (SIN:s @0)) 5630 (rdiv { build_one_cst (type); } (TAN @0))) 5631 5632 /* Simplify sin(x) / tan(x) -> cos(x). */ 5633 (simplify 5634 (rdiv (SIN:s @0) (TAN:s @0)) 5635 (if (! HONOR_NANS (@0) 5636 && ! HONOR_INFINITIES (@0)) 5637 (COS @0))) 5638 5639 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */ 5640 (simplify 5641 (rdiv (TAN:s @0) (SIN:s @0)) 5642 (if (! HONOR_NANS (@0) 5643 && ! HONOR_INFINITIES (@0)) 5644 (rdiv { build_one_cst (type); } (COS @0)))) 5645 5646 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */ 5647 (simplify 5648 (mult (POW:s @0 @1) (POW:s @0 @2)) 5649 (POW @0 (plus @1 @2))) 5650 5651 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */ 5652 (simplify 5653 (mult (POW:s @0 @1) (POW:s @2 @1)) 5654 (POW (mult @0 @2) @1)) 5655 5656 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */ 5657 (simplify 5658 (mult (POWI:s @0 @1) (POWI:s @2 @1)) 5659 (POWI (mult @0 @2) @1)) 5660 5661 /* Simplify pow(x,c) / x -> pow(x,c-1). */ 5662 (simplify 5663 (rdiv (POW:s @0 REAL_CST@1) @0) 5664 (if (!TREE_OVERFLOW (@1)) 5665 (POW @0 (minus @1 { build_one_cst (type); })))) 5666 5667 /* Simplify x / pow (y,z) -> x * pow(y,-z). */ 5668 (simplify 5669 (rdiv @0 (POW:s @1 @2)) 5670 (mult @0 (POW @1 (negate @2)))) 5671 5672 (for sqrts (SQRT) 5673 cbrts (CBRT) 5674 pows (POW) 5675 /* sqrt(sqrt(x)) -> pow(x,1/4). */ 5676 (simplify 5677 (sqrts (sqrts @0)) 5678 (pows @0 { build_real (type, dconst_quarter ()); })) 5679 /* sqrt(cbrt(x)) -> pow(x,1/6). */ 5680 (simplify 5681 (sqrts (cbrts @0)) 5682 (pows @0 { build_real_truncate (type, dconst_sixth ()); })) 5683 /* cbrt(sqrt(x)) -> pow(x,1/6). */ 5684 (simplify 5685 (cbrts (sqrts @0)) 5686 (pows @0 { build_real_truncate (type, dconst_sixth ()); })) 5687 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */ 5688 (simplify 5689 (cbrts (cbrts tree_expr_nonnegative_p@0)) 5690 (pows @0 { build_real_truncate (type, dconst_ninth ()); })) 5691 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */ 5692 (simplify 5693 (sqrts (pows @0 @1)) 5694 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); }))) 5695 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */ 5696 (simplify 5697 (cbrts (pows tree_expr_nonnegative_p@0 @1)) 5698 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) 5699 /* pow(sqrt(x),y) -> pow(x,y*0.5). */ 5700 (simplify 5701 (pows (sqrts @0) @1) 5702 (pows @0 (mult @1 { build_real (type, dconsthalf); }))) 5703 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */ 5704 (simplify 5705 (pows (cbrts tree_expr_nonnegative_p@0) @1) 5706 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) 5707 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */ 5708 (simplify 5709 (pows (pows tree_expr_nonnegative_p@0 @1) @2) 5710 (pows @0 (mult @1 @2)))) 5711 5712 /* cabs(x+xi) -> fabs(x)*sqrt(2). */ 5713 (simplify 5714 (CABS (complex @0 @0)) 5715 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) 5716 5717 /* hypot(x,x) -> fabs(x)*sqrt(2). */ 5718 (simplify 5719 (HYPOT @0 @0) 5720 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) 5721 5722 /* cexp(x+yi) -> exp(x)*cexpi(y). */ 5723 (for cexps (CEXP) 5724 exps (EXP) 5725 cexpis (CEXPI) 5726 (simplify 5727 (cexps compositional_complex@0) 5728 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0))) 5729 (complex 5730 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0)))) 5731 (mult @1 (imagpart @2))))))) 5732 5733(if (canonicalize_math_p ()) 5734 /* floor(x) -> trunc(x) if x is nonnegative. */ 5735 (for floors (FLOOR_ALL) 5736 truncs (TRUNC_ALL) 5737 (simplify 5738 (floors tree_expr_nonnegative_p@0) 5739 (truncs @0)))) 5740 5741(match double_value_p 5742 @0 5743 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node))) 5744(for froms (BUILT_IN_TRUNCL 5745 BUILT_IN_FLOORL 5746 BUILT_IN_CEILL 5747 BUILT_IN_ROUNDL 5748 BUILT_IN_NEARBYINTL 5749 BUILT_IN_RINTL) 5750 tos (BUILT_IN_TRUNC 5751 BUILT_IN_FLOOR 5752 BUILT_IN_CEIL 5753 BUILT_IN_ROUND 5754 BUILT_IN_NEARBYINT 5755 BUILT_IN_RINT) 5756 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */ 5757 (if (optimize && canonicalize_math_p ()) 5758 (simplify 5759 (froms (convert double_value_p@0)) 5760 (convert (tos @0))))) 5761 5762(match float_value_p 5763 @0 5764 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node))) 5765(for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC 5766 BUILT_IN_FLOORL BUILT_IN_FLOOR 5767 BUILT_IN_CEILL BUILT_IN_CEIL 5768 BUILT_IN_ROUNDL BUILT_IN_ROUND 5769 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT 5770 BUILT_IN_RINTL BUILT_IN_RINT) 5771 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF 5772 BUILT_IN_FLOORF BUILT_IN_FLOORF 5773 BUILT_IN_CEILF BUILT_IN_CEILF 5774 BUILT_IN_ROUNDF BUILT_IN_ROUNDF 5775 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF 5776 BUILT_IN_RINTF BUILT_IN_RINTF) 5777 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc., 5778 if x is a float. */ 5779 (if (optimize && canonicalize_math_p () 5780 && targetm.libc_has_function (function_c99_misc, NULL_TREE)) 5781 (simplify 5782 (froms (convert float_value_p@0)) 5783 (convert (tos @0))))) 5784 5785(for froms (XFLOORL XCEILL XROUNDL XRINTL) 5786 tos (XFLOOR XCEIL XROUND XRINT) 5787 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */ 5788 (if (optimize && canonicalize_math_p ()) 5789 (simplify 5790 (froms (convert double_value_p@0)) 5791 (tos @0)))) 5792 5793(for froms (XFLOORL XCEILL XROUNDL XRINTL 5794 XFLOOR XCEIL XROUND XRINT) 5795 tos (XFLOORF XCEILF XROUNDF XRINTF) 5796 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc., 5797 if x is a float. */ 5798 (if (optimize && canonicalize_math_p ()) 5799 (simplify 5800 (froms (convert float_value_p@0)) 5801 (tos @0)))) 5802 5803(if (canonicalize_math_p ()) 5804 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */ 5805 (for floors (IFLOOR LFLOOR LLFLOOR) 5806 (simplify 5807 (floors tree_expr_nonnegative_p@0) 5808 (fix_trunc @0)))) 5809 5810(if (canonicalize_math_p ()) 5811 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */ 5812 (for fns (IFLOOR LFLOOR LLFLOOR 5813 ICEIL LCEIL LLCEIL 5814 IROUND LROUND LLROUND) 5815 (simplify 5816 (fns integer_valued_real_p@0) 5817 (fix_trunc @0))) 5818 (if (!flag_errno_math) 5819 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */ 5820 (for rints (IRINT LRINT LLRINT) 5821 (simplify 5822 (rints integer_valued_real_p@0) 5823 (fix_trunc @0))))) 5824 5825(if (canonicalize_math_p ()) 5826 (for ifn (IFLOOR ICEIL IROUND IRINT) 5827 lfn (LFLOOR LCEIL LROUND LRINT) 5828 llfn (LLFLOOR LLCEIL LLROUND LLRINT) 5829 /* Canonicalize iround (x) to lround (x) on ILP32 targets where 5830 sizeof (int) == sizeof (long). */ 5831 (if (TYPE_PRECISION (integer_type_node) 5832 == TYPE_PRECISION (long_integer_type_node)) 5833 (simplify 5834 (ifn @0) 5835 (lfn:long_integer_type_node @0))) 5836 /* Canonicalize llround (x) to lround (x) on LP64 targets where 5837 sizeof (long long) == sizeof (long). */ 5838 (if (TYPE_PRECISION (long_long_integer_type_node) 5839 == TYPE_PRECISION (long_integer_type_node)) 5840 (simplify 5841 (llfn @0) 5842 (lfn:long_integer_type_node @0))))) 5843 5844/* cproj(x) -> x if we're ignoring infinities. */ 5845(simplify 5846 (CPROJ @0) 5847 (if (!HONOR_INFINITIES (type)) 5848 @0)) 5849 5850/* If the real part is inf and the imag part is known to be 5851 nonnegative, return (inf + 0i). */ 5852(simplify 5853 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1)) 5854 (if (real_isinf (TREE_REAL_CST_PTR (@0))) 5855 { build_complex_inf (type, false); })) 5856 5857/* If the imag part is inf, return (inf+I*copysign(0,imag)). */ 5858(simplify 5859 (CPROJ (complex @0 REAL_CST@1)) 5860 (if (real_isinf (TREE_REAL_CST_PTR (@1))) 5861 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); })) 5862 5863(for pows (POW) 5864 sqrts (SQRT) 5865 cbrts (CBRT) 5866 (simplify 5867 (pows @0 REAL_CST@1) 5868 (with { 5869 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1); 5870 REAL_VALUE_TYPE tmp; 5871 } 5872 (switch 5873 /* pow(x,0) -> 1. */ 5874 (if (real_equal (value, &dconst0)) 5875 { build_real (type, dconst1); }) 5876 /* pow(x,1) -> x. */ 5877 (if (real_equal (value, &dconst1)) 5878 @0) 5879 /* pow(x,-1) -> 1/x. */ 5880 (if (real_equal (value, &dconstm1)) 5881 (rdiv { build_real (type, dconst1); } @0)) 5882 /* pow(x,0.5) -> sqrt(x). */ 5883 (if (flag_unsafe_math_optimizations 5884 && canonicalize_math_p () 5885 && real_equal (value, &dconsthalf)) 5886 (sqrts @0)) 5887 /* pow(x,1/3) -> cbrt(x). */ 5888 (if (flag_unsafe_math_optimizations 5889 && canonicalize_math_p () 5890 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()), 5891 real_equal (value, &tmp))) 5892 (cbrts @0)))))) 5893 5894/* powi(1,x) -> 1. */ 5895(simplify 5896 (POWI real_onep@0 @1) 5897 @0) 5898 5899(simplify 5900 (POWI @0 INTEGER_CST@1) 5901 (switch 5902 /* powi(x,0) -> 1. */ 5903 (if (wi::to_wide (@1) == 0) 5904 { build_real (type, dconst1); }) 5905 /* powi(x,1) -> x. */ 5906 (if (wi::to_wide (@1) == 1) 5907 @0) 5908 /* powi(x,-1) -> 1/x. */ 5909 (if (wi::to_wide (@1) == -1) 5910 (rdiv { build_real (type, dconst1); } @0)))) 5911 5912/* Narrowing of arithmetic and logical operations. 5913 5914 These are conceptually similar to the transformations performed for 5915 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long 5916 term we want to move all that code out of the front-ends into here. */ 5917 5918/* Convert (outertype)((innertype0)a+(innertype1)b) 5919 into ((newtype)a+(newtype)b) where newtype 5920 is the widest mode from all of these. */ 5921(for op (plus minus mult rdiv) 5922 (simplify 5923 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2))) 5924 /* If we have a narrowing conversion of an arithmetic operation where 5925 both operands are widening conversions from the same type as the outer 5926 narrowing conversion. Then convert the innermost operands to a 5927 suitable unsigned type (to avoid introducing undefined behavior), 5928 perform the operation and convert the result to the desired type. */ 5929 (if (INTEGRAL_TYPE_P (type) 5930 && op != MULT_EXPR 5931 && op != RDIV_EXPR 5932 /* We check for type compatibility between @0 and @1 below, 5933 so there's no need to check that @2/@4 are integral types. */ 5934 && INTEGRAL_TYPE_P (TREE_TYPE (@1)) 5935 && INTEGRAL_TYPE_P (TREE_TYPE (@3)) 5936 /* The precision of the type of each operand must match the 5937 precision of the mode of each operand, similarly for the 5938 result. */ 5939 && type_has_mode_precision_p (TREE_TYPE (@1)) 5940 && type_has_mode_precision_p (TREE_TYPE (@2)) 5941 && type_has_mode_precision_p (type) 5942 /* The inner conversion must be a widening conversion. */ 5943 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1)) 5944 && types_match (@1, type) 5945 && (types_match (@1, @2) 5946 /* Or the second operand is const integer or converted const 5947 integer from valueize. */ 5948 || TREE_CODE (@2) == INTEGER_CST)) 5949 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) 5950 (op @1 (convert @2)) 5951 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); } 5952 (convert (op (convert:utype @1) 5953 (convert:utype @2))))) 5954 (if (FLOAT_TYPE_P (type) 5955 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)) 5956 == DECIMAL_FLOAT_TYPE_P (type)) 5957 (with { tree arg0 = strip_float_extensions (@1); 5958 tree arg1 = strip_float_extensions (@2); 5959 tree itype = TREE_TYPE (@0); 5960 tree ty1 = TREE_TYPE (arg0); 5961 tree ty2 = TREE_TYPE (arg1); 5962 enum tree_code code = TREE_CODE (itype); } 5963 (if (FLOAT_TYPE_P (ty1) 5964 && FLOAT_TYPE_P (ty2)) 5965 (with { tree newtype = type; 5966 if (TYPE_MODE (ty1) == SDmode 5967 || TYPE_MODE (ty2) == SDmode 5968 || TYPE_MODE (type) == SDmode) 5969 newtype = dfloat32_type_node; 5970 if (TYPE_MODE (ty1) == DDmode 5971 || TYPE_MODE (ty2) == DDmode 5972 || TYPE_MODE (type) == DDmode) 5973 newtype = dfloat64_type_node; 5974 if (TYPE_MODE (ty1) == TDmode 5975 || TYPE_MODE (ty2) == TDmode 5976 || TYPE_MODE (type) == TDmode) 5977 newtype = dfloat128_type_node; } 5978 (if ((newtype == dfloat32_type_node 5979 || newtype == dfloat64_type_node 5980 || newtype == dfloat128_type_node) 5981 && newtype == type 5982 && types_match (newtype, type)) 5983 (op (convert:newtype @1) (convert:newtype @2)) 5984 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype)) 5985 newtype = ty1; 5986 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype)) 5987 newtype = ty2; } 5988 /* Sometimes this transformation is safe (cannot 5989 change results through affecting double rounding 5990 cases) and sometimes it is not. If NEWTYPE is 5991 wider than TYPE, e.g. (float)((long double)double 5992 + (long double)double) converted to 5993 (float)(double + double), the transformation is 5994 unsafe regardless of the details of the types 5995 involved; double rounding can arise if the result 5996 of NEWTYPE arithmetic is a NEWTYPE value half way 5997 between two representable TYPE values but the 5998 exact value is sufficiently different (in the 5999 right direction) for this difference to be 6000 visible in ITYPE arithmetic. If NEWTYPE is the 6001 same as TYPE, however, the transformation may be 6002 safe depending on the types involved: it is safe 6003 if the ITYPE has strictly more than twice as many 6004 mantissa bits as TYPE, can represent infinities 6005 and NaNs if the TYPE can, and has sufficient 6006 exponent range for the product or ratio of two 6007 values representable in the TYPE to be within the 6008 range of normal values of ITYPE. */ 6009 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype) 6010 && (flag_unsafe_math_optimizations 6011 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type) 6012 && real_can_shorten_arithmetic (TYPE_MODE (itype), 6013 TYPE_MODE (type)) 6014 && !excess_precision_type (newtype))) 6015 && !types_match (itype, newtype)) 6016 (convert:type (op (convert:newtype @1) 6017 (convert:newtype @2))) 6018 )))) ) 6019 )) 6020))) 6021 6022/* This is another case of narrowing, specifically when there's an outer 6023 BIT_AND_EXPR which masks off bits outside the type of the innermost 6024 operands. Like the previous case we have to convert the operands 6025 to unsigned types to avoid introducing undefined behavior for the 6026 arithmetic operation. */ 6027(for op (minus plus) 6028 (simplify 6029 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4) 6030 (if (INTEGRAL_TYPE_P (type) 6031 /* We check for type compatibility between @0 and @1 below, 6032 so there's no need to check that @1/@3 are integral types. */ 6033 && INTEGRAL_TYPE_P (TREE_TYPE (@0)) 6034 && INTEGRAL_TYPE_P (TREE_TYPE (@2)) 6035 /* The precision of the type of each operand must match the 6036 precision of the mode of each operand, similarly for the 6037 result. */ 6038 && type_has_mode_precision_p (TREE_TYPE (@0)) 6039 && type_has_mode_precision_p (TREE_TYPE (@1)) 6040 && type_has_mode_precision_p (type) 6041 /* The inner conversion must be a widening conversion. */ 6042 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) 6043 && types_match (@0, @1) 6044 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0))) 6045 <= TYPE_PRECISION (TREE_TYPE (@0))) 6046 && (wi::to_wide (@4) 6047 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)), 6048 true, TYPE_PRECISION (type))) == 0) 6049 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) 6050 (with { tree ntype = TREE_TYPE (@0); } 6051 (convert (bit_and (op @0 @1) (convert:ntype @4)))) 6052 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } 6053 (convert (bit_and (op (convert:utype @0) (convert:utype @1)) 6054 (convert:utype @4)))))))) 6055 6056/* Transform (@0 < @1 and @0 < @2) to use min, 6057 (@0 > @1 and @0 > @2) to use max */ 6058(for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior) 6059 op (lt le gt ge lt le gt ge ) 6060 ext (min min max max max max min min ) 6061 (simplify 6062 (logic (op:cs @0 @1) (op:cs @0 @2)) 6063 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 6064 && TREE_CODE (@0) != INTEGER_CST) 6065 (op @0 (ext @1 @2))))) 6066 6067(simplify 6068 /* signbit(x) -> 0 if x is nonnegative. */ 6069 (SIGNBIT tree_expr_nonnegative_p@0) 6070 { integer_zero_node; }) 6071 6072(simplify 6073 /* signbit(x) -> x<0 if x doesn't have signed zeros. */ 6074 (SIGNBIT @0) 6075 (if (!HONOR_SIGNED_ZEROS (@0)) 6076 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); })))) 6077 6078/* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */ 6079(for cmp (eq ne) 6080 (for op (plus minus) 6081 rop (minus plus) 6082 (simplify 6083 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) 6084 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) 6085 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) 6086 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0)) 6087 && !TYPE_SATURATING (TREE_TYPE (@0))) 6088 (with { tree res = int_const_binop (rop, @2, @1); } 6089 (if (TREE_OVERFLOW (res) 6090 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) 6091 { constant_boolean_node (cmp == NE_EXPR, type); } 6092 (if (single_use (@3)) 6093 (cmp @0 { TREE_OVERFLOW (res) 6094 ? drop_tree_overflow (res) : res; })))))))) 6095(for cmp (lt le gt ge) 6096 (for op (plus minus) 6097 rop (minus plus) 6098 (simplify 6099 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) 6100 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) 6101 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) 6102 (with { tree res = int_const_binop (rop, @2, @1); } 6103 (if (TREE_OVERFLOW (res)) 6104 { 6105 fold_overflow_warning (("assuming signed overflow does not occur " 6106 "when simplifying conditional to constant"), 6107 WARN_STRICT_OVERFLOW_CONDITIONAL); 6108 bool less = cmp == LE_EXPR || cmp == LT_EXPR; 6109 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */ 6110 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0, 6111 TYPE_SIGN (TREE_TYPE (@1))) 6112 != (op == MINUS_EXPR); 6113 constant_boolean_node (less == ovf_high, type); 6114 } 6115 (if (single_use (@3)) 6116 (with 6117 { 6118 fold_overflow_warning (("assuming signed overflow does not occur " 6119 "when changing X +- C1 cmp C2 to " 6120 "X cmp C2 -+ C1"), 6121 WARN_STRICT_OVERFLOW_COMPARISON); 6122 } 6123 (cmp @0 { res; }))))))))) 6124 6125/* Canonicalizations of BIT_FIELD_REFs. */ 6126 6127(simplify 6128 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4) 6129 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); })) 6130 6131(simplify 6132 (BIT_FIELD_REF (view_convert @0) @1 @2) 6133 (BIT_FIELD_REF @0 @1 @2)) 6134 6135(simplify 6136 (BIT_FIELD_REF @0 @1 integer_zerop) 6137 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0)))) 6138 (view_convert @0))) 6139 6140(simplify 6141 (BIT_FIELD_REF @0 @1 @2) 6142 (switch 6143 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE 6144 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) 6145 (switch 6146 (if (integer_zerop (@2)) 6147 (view_convert (realpart @0))) 6148 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) 6149 (view_convert (imagpart @0))))) 6150 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 6151 && INTEGRAL_TYPE_P (type) 6152 /* On GIMPLE this should only apply to register arguments. */ 6153 && (! GIMPLE || is_gimple_reg (@0)) 6154 /* A bit-field-ref that referenced the full argument can be stripped. */ 6155 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0 6156 && integer_zerop (@2)) 6157 /* Low-parts can be reduced to integral conversions. 6158 ??? The following doesn't work for PDP endian. */ 6159 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN 6160 /* But only do this after vectorization. */ 6161 && canonicalize_math_after_vectorization_p () 6162 /* Don't even think about BITS_BIG_ENDIAN. */ 6163 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0 6164 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0 6165 && compare_tree_int (@2, (BYTES_BIG_ENDIAN 6166 ? (TYPE_PRECISION (TREE_TYPE (@0)) 6167 - TYPE_PRECISION (type)) 6168 : 0)) == 0))) 6169 (convert @0)))) 6170 6171/* Simplify vector extracts. */ 6172 6173(simplify 6174 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2) 6175 (if (VECTOR_TYPE_P (TREE_TYPE (@0)) 6176 && (types_match (type, TREE_TYPE (TREE_TYPE (@0))) 6177 || (VECTOR_TYPE_P (type) 6178 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) 6179 (with 6180 { 6181 tree ctor = (TREE_CODE (@0) == SSA_NAME 6182 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); 6183 tree eltype = TREE_TYPE (TREE_TYPE (ctor)); 6184 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype)); 6185 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1); 6186 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2); 6187 } 6188 (if (n != 0 6189 && (idx % width) == 0 6190 && (n % width) == 0 6191 && known_le ((idx + n) / width, 6192 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))) 6193 (with 6194 { 6195 idx = idx / width; 6196 n = n / width; 6197 /* Constructor elements can be subvectors. */ 6198 poly_uint64 k = 1; 6199 if (CONSTRUCTOR_NELTS (ctor) != 0) 6200 { 6201 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value); 6202 if (TREE_CODE (cons_elem) == VECTOR_TYPE) 6203 k = TYPE_VECTOR_SUBPARTS (cons_elem); 6204 } 6205 unsigned HOST_WIDE_INT elt, count, const_k; 6206 } 6207 (switch 6208 /* We keep an exact subset of the constructor elements. */ 6209 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count)) 6210 (if (CONSTRUCTOR_NELTS (ctor) == 0) 6211 { build_constructor (type, NULL); } 6212 (if (count == 1) 6213 (if (elt < CONSTRUCTOR_NELTS (ctor)) 6214 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; }) 6215 { build_zero_cst (type); }) 6216 /* We don't want to emit new CTORs unless the old one goes away. 6217 ??? Eventually allow this if the CTOR ends up constant or 6218 uniform. */ 6219 (if (single_use (@0)) 6220 { 6221 vec<constructor_elt, va_gc> *vals; 6222 vec_alloc (vals, count); 6223 for (unsigned i = 0; 6224 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i) 6225 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, 6226 CONSTRUCTOR_ELT (ctor, elt + i)->value); 6227 build_constructor (type, vals); 6228 })))) 6229 /* The bitfield references a single constructor element. */ 6230 (if (k.is_constant (&const_k) 6231 && idx + n <= (idx / const_k + 1) * const_k) 6232 (switch 6233 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k) 6234 { build_zero_cst (type); }) 6235 (if (n == const_k) 6236 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; })) 6237 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; } 6238 @1 { bitsize_int ((idx % const_k) * width); }))))))))) 6239 6240/* Simplify a bit extraction from a bit insertion for the cases with 6241 the inserted element fully covering the extraction or the insertion 6242 not touching the extraction. */ 6243(simplify 6244 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos) 6245 (with 6246 { 6247 unsigned HOST_WIDE_INT isize; 6248 if (INTEGRAL_TYPE_P (TREE_TYPE (@1))) 6249 isize = TYPE_PRECISION (TREE_TYPE (@1)); 6250 else 6251 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1))); 6252 } 6253 (switch 6254 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos)) 6255 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize), 6256 wi::to_wide (@ipos) + isize)) 6257 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype, 6258 wi::to_wide (@rpos) 6259 - wi::to_wide (@ipos)); })) 6260 (if (wi::geu_p (wi::to_wide (@ipos), 6261 wi::to_wide (@rpos) + wi::to_wide (@rsize)) 6262 || wi::geu_p (wi::to_wide (@rpos), 6263 wi::to_wide (@ipos) + isize)) 6264 (BIT_FIELD_REF @0 @rsize @rpos))))) 6265 6266(if (canonicalize_math_after_vectorization_p ()) 6267 (for fmas (FMA) 6268 (simplify 6269 (fmas:c (negate @0) @1 @2) 6270 (IFN_FNMA @0 @1 @2)) 6271 (simplify 6272 (fmas @0 @1 (negate @2)) 6273 (IFN_FMS @0 @1 @2)) 6274 (simplify 6275 (fmas:c (negate @0) @1 (negate @2)) 6276 (IFN_FNMS @0 @1 @2)) 6277 (simplify 6278 (negate (fmas@3 @0 @1 @2)) 6279 (if (single_use (@3)) 6280 (IFN_FNMS @0 @1 @2)))) 6281 6282 (simplify 6283 (IFN_FMS:c (negate @0) @1 @2) 6284 (IFN_FNMS @0 @1 @2)) 6285 (simplify 6286 (IFN_FMS @0 @1 (negate @2)) 6287 (IFN_FMA @0 @1 @2)) 6288 (simplify 6289 (IFN_FMS:c (negate @0) @1 (negate @2)) 6290 (IFN_FNMA @0 @1 @2)) 6291 (simplify 6292 (negate (IFN_FMS@3 @0 @1 @2)) 6293 (if (single_use (@3)) 6294 (IFN_FNMA @0 @1 @2))) 6295 6296 (simplify 6297 (IFN_FNMA:c (negate @0) @1 @2) 6298 (IFN_FMA @0 @1 @2)) 6299 (simplify 6300 (IFN_FNMA @0 @1 (negate @2)) 6301 (IFN_FNMS @0 @1 @2)) 6302 (simplify 6303 (IFN_FNMA:c (negate @0) @1 (negate @2)) 6304 (IFN_FMS @0 @1 @2)) 6305 (simplify 6306 (negate (IFN_FNMA@3 @0 @1 @2)) 6307 (if (single_use (@3)) 6308 (IFN_FMS @0 @1 @2))) 6309 6310 (simplify 6311 (IFN_FNMS:c (negate @0) @1 @2) 6312 (IFN_FMS @0 @1 @2)) 6313 (simplify 6314 (IFN_FNMS @0 @1 (negate @2)) 6315 (IFN_FNMA @0 @1 @2)) 6316 (simplify 6317 (IFN_FNMS:c (negate @0) @1 (negate @2)) 6318 (IFN_FMA @0 @1 @2)) 6319 (simplify 6320 (negate (IFN_FNMS@3 @0 @1 @2)) 6321 (if (single_use (@3)) 6322 (IFN_FMA @0 @1 @2)))) 6323 6324/* CLZ simplifications. */ 6325(for clz (CLZ) 6326 (for op (eq ne) 6327 cmp (lt ge) 6328 (simplify 6329 (op (clz:s@2 @0) INTEGER_CST@1) 6330 (if (integer_zerop (@1) && single_use (@2)) 6331 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */ 6332 (with { tree stype = signed_type_for (TREE_TYPE (@0)); 6333 HOST_WIDE_INT val = 0; 6334#ifdef CLZ_DEFINED_VALUE_AT_ZERO 6335 /* Punt on hypothetical weird targets. */ 6336 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)), 6337 val) == 2 6338 && val == 0) 6339 stype = NULL_TREE; 6340#endif 6341 } 6342 (if (stype) 6343 (cmp (convert:stype @0) { build_zero_cst (stype); }))) 6344 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */ 6345 (with { bool ok = true; 6346#ifdef CLZ_DEFINED_VALUE_AT_ZERO 6347 /* Punt on hypothetical weird targets. */ 6348 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)), 6349 val) == 2 6350 && val == TYPE_PRECISION (TREE_TYPE (@0)) - 1) 6351 ok = false; 6352#endif 6353 } 6354 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (TREE_TYPE (@0)) - 1)) 6355 (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))) 6356 6357/* POPCOUNT simplifications. */ 6358/* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */ 6359(simplify 6360 (plus (POPCOUNT:s @0) (POPCOUNT:s @1)) 6361 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0) 6362 (POPCOUNT (bit_ior @0 @1)))) 6363 6364/* popcount(X) == 0 is X == 0, and related (in)equalities. */ 6365(for popcount (POPCOUNT) 6366 (for cmp (le eq ne gt) 6367 rep (eq eq ne ne) 6368 (simplify 6369 (cmp (popcount @0) integer_zerop) 6370 (rep @0 { build_zero_cst (TREE_TYPE (@0)); })))) 6371 6372/* Canonicalize POPCOUNT(x)&1 as PARITY(X). */ 6373(simplify 6374 (bit_and (POPCOUNT @0) integer_onep) 6375 (PARITY @0)) 6376 6377/* PARITY simplifications. */ 6378/* parity(~X) is parity(X). */ 6379(simplify 6380 (PARITY (bit_not @0)) 6381 (PARITY @0)) 6382 6383/* parity(X)^parity(Y) is parity(X^Y). */ 6384(simplify 6385 (bit_xor (PARITY:s @0) (PARITY:s @1)) 6386 (PARITY (bit_xor @0 @1))) 6387 6388/* Common POPCOUNT/PARITY simplifications. */ 6389/* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */ 6390(for pfun (POPCOUNT PARITY) 6391 (simplify 6392 (pfun @0) 6393 (with { wide_int nz = tree_nonzero_bits (@0); } 6394 (switch 6395 (if (nz == 1) 6396 (convert @0)) 6397 (if (wi::popcount (nz) == 1) 6398 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } 6399 (convert (rshift:utype (convert:utype @0) 6400 { build_int_cst (integer_type_node, 6401 wi::ctz (nz)); })))))))) 6402 6403#if GIMPLE 6404/* 64- and 32-bits branchless implementations of popcount are detected: 6405 6406 int popcount64c (uint64_t x) 6407 { 6408 x -= (x >> 1) & 0x5555555555555555ULL; 6409 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL); 6410 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL; 6411 return (x * 0x0101010101010101ULL) >> 56; 6412 } 6413 6414 int popcount32c (uint32_t x) 6415 { 6416 x -= (x >> 1) & 0x55555555; 6417 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); 6418 x = (x + (x >> 4)) & 0x0f0f0f0f; 6419 return (x * 0x01010101) >> 24; 6420 } */ 6421(simplify 6422 (rshift 6423 (mult 6424 (bit_and 6425 (plus:c 6426 (rshift @8 INTEGER_CST@5) 6427 (plus:c@8 6428 (bit_and @6 INTEGER_CST@7) 6429 (bit_and 6430 (rshift 6431 (minus@6 @0 6432 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11)) 6433 INTEGER_CST@10) 6434 INTEGER_CST@9))) 6435 INTEGER_CST@3) 6436 INTEGER_CST@2) 6437 INTEGER_CST@1) 6438 /* Check constants and optab. */ 6439 (with { unsigned prec = TYPE_PRECISION (type); 6440 int shift = (64 - prec) & 63; 6441 unsigned HOST_WIDE_INT c1 6442 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift; 6443 unsigned HOST_WIDE_INT c2 6444 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift; 6445 unsigned HOST_WIDE_INT c3 6446 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift; 6447 unsigned HOST_WIDE_INT c4 6448 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift; 6449 } 6450 (if (prec >= 16 6451 && prec <= 64 6452 && pow2p_hwi (prec) 6453 && TYPE_UNSIGNED (type) 6454 && integer_onep (@4) 6455 && wi::to_widest (@10) == 2 6456 && wi::to_widest (@5) == 4 6457 && wi::to_widest (@1) == prec - 8 6458 && tree_to_uhwi (@2) == c1 6459 && tree_to_uhwi (@3) == c2 6460 && tree_to_uhwi (@9) == c3 6461 && tree_to_uhwi (@7) == c3 6462 && tree_to_uhwi (@11) == c4 6463 && direct_internal_fn_supported_p (IFN_POPCOUNT, type, 6464 OPTIMIZE_FOR_BOTH)) 6465 (convert (IFN_POPCOUNT:type @0))))) 6466 6467/* __builtin_ffs needs to deal on many targets with the possible zero 6468 argument. If we know the argument is always non-zero, __builtin_ctz + 1 6469 should lead to better code. */ 6470(simplify 6471 (FFS tree_expr_nonzero_p@0) 6472 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) 6473 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0), 6474 OPTIMIZE_FOR_SPEED)) 6475 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } 6476 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); })))) 6477#endif 6478 6479(for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL 6480 BUILT_IN_FFSIMAX) 6481 /* __builtin_ffs (X) == 0 -> X == 0. 6482 __builtin_ffs (X) == 6 -> (X & 63) == 32. */ 6483 (for cmp (eq ne) 6484 (simplify 6485 (cmp (ffs@2 @0) INTEGER_CST@1) 6486 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); } 6487 (switch 6488 (if (integer_zerop (@1)) 6489 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); })) 6490 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec) 6491 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); }) 6492 (if (single_use (@2)) 6493 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), 6494 wi::mask (tree_to_uhwi (@1), 6495 false, prec)); }) 6496 { wide_int_to_tree (TREE_TYPE (@0), 6497 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1, 6498 false, prec)); })))))) 6499 6500 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */ 6501 (for cmp (gt le) 6502 cmp2 (ne eq) 6503 cmp3 (eq ne) 6504 bit_op (bit_and bit_ior) 6505 (simplify 6506 (cmp (ffs@2 @0) INTEGER_CST@1) 6507 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); } 6508 (switch 6509 (if (integer_zerop (@1)) 6510 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })) 6511 (if (tree_int_cst_sgn (@1) < 0) 6512 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); }) 6513 (if (wi::to_widest (@1) >= prec) 6514 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); }) 6515 (if (wi::to_widest (@1) == prec - 1) 6516 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0), 6517 wi::shifted_mask (prec - 1, 1, 6518 false, prec)); })) 6519 (if (single_use (@2)) 6520 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }) 6521 (cmp3 (bit_and @0 6522 { wide_int_to_tree (TREE_TYPE (@0), 6523 wi::mask (tree_to_uhwi (@1), 6524 false, prec)); }) 6525 { build_zero_cst (TREE_TYPE (@0)); })))))))) 6526 6527/* Simplify: 6528 6529 a = a1 op a2 6530 r = c ? a : b; 6531 6532 to: 6533 6534 r = c ? a1 op a2 : b; 6535 6536 if the target can do it in one go. This makes the operation conditional 6537 on c, so could drop potentially-trapping arithmetic, but that's a valid 6538 simplification if the result of the operation isn't needed. 6539 6540 Avoid speculatively generating a stand-alone vector comparison 6541 on targets that might not support them. Any target implementing 6542 conditional internal functions must support the same comparisons 6543 inside and outside a VEC_COND_EXPR. */ 6544 6545#if GIMPLE 6546(for uncond_op (UNCOND_BINARY) 6547 cond_op (COND_BINARY) 6548 (simplify 6549 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3) 6550 (with { tree op_type = TREE_TYPE (@4); } 6551 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) 6552 && element_precision (type) == element_precision (op_type)) 6553 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3)))))) 6554 (simplify 6555 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3))) 6556 (with { tree op_type = TREE_TYPE (@4); } 6557 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) 6558 && element_precision (type) == element_precision (op_type)) 6559 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1))))))) 6560 6561/* Same for ternary operations. */ 6562(for uncond_op (UNCOND_TERNARY) 6563 cond_op (COND_TERNARY) 6564 (simplify 6565 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4) 6566 (with { tree op_type = TREE_TYPE (@5); } 6567 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) 6568 && element_precision (type) == element_precision (op_type)) 6569 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4)))))) 6570 (simplify 6571 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4))) 6572 (with { tree op_type = TREE_TYPE (@5); } 6573 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) 6574 && element_precision (type) == element_precision (op_type)) 6575 (view_convert (cond_op (bit_not @0) @2 @3 @4 6576 (view_convert:op_type @1))))))) 6577#endif 6578 6579/* Detect cases in which a VEC_COND_EXPR effectively replaces the 6580 "else" value of an IFN_COND_*. */ 6581(for cond_op (COND_BINARY) 6582 (simplify 6583 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4) 6584 (with { tree op_type = TREE_TYPE (@3); } 6585 (if (element_precision (type) == element_precision (op_type)) 6586 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4)))))) 6587 (simplify 6588 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5))) 6589 (with { tree op_type = TREE_TYPE (@5); } 6590 (if (inverse_conditions_p (@0, @2) 6591 && element_precision (type) == element_precision (op_type)) 6592 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1))))))) 6593 6594/* Same for ternary operations. */ 6595(for cond_op (COND_TERNARY) 6596 (simplify 6597 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5) 6598 (with { tree op_type = TREE_TYPE (@4); } 6599 (if (element_precision (type) == element_precision (op_type)) 6600 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5)))))) 6601 (simplify 6602 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6))) 6603 (with { tree op_type = TREE_TYPE (@6); } 6604 (if (inverse_conditions_p (@0, @2) 6605 && element_precision (type) == element_precision (op_type)) 6606 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1))))))) 6607 6608/* For pointers @0 and @2 and nonnegative constant offset @1, look for 6609 expressions like: 6610 6611 A: (@0 + @1 < @2) | (@2 + @1 < @0) 6612 B: (@0 + @1 <= @2) | (@2 + @1 <= @0) 6613 6614 If pointers are known not to wrap, B checks whether @1 bytes starting 6615 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1 6616 bytes. A is more efficiently tested as: 6617 6618 A: (sizetype) (@0 + @1 - @2) > @1 * 2 6619 6620 The equivalent expression for B is given by replacing @1 with @1 - 1: 6621 6622 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2 6623 6624 @0 and @2 can be swapped in both expressions without changing the result. 6625 6626 The folds rely on sizetype's being unsigned (which is always true) 6627 and on its being the same width as the pointer (which we have to check). 6628 6629 The fold replaces two pointer_plus expressions, two comparisons and 6630 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in 6631 the best case it's a saving of two operations. The A fold retains one 6632 of the original pointer_pluses, so is a win even if both pointer_pluses 6633 are used elsewhere. The B fold is a wash if both pointer_pluses are 6634 used elsewhere, since all we end up doing is replacing a comparison with 6635 a pointer_plus. We do still apply the fold under those circumstances 6636 though, in case applying it to other conditions eventually makes one of the 6637 pointer_pluses dead. */ 6638(for ior (truth_orif truth_or bit_ior) 6639 (for cmp (le lt) 6640 (simplify 6641 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2) 6642 (cmp:cs (pointer_plus@4 @2 @1) @0)) 6643 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) 6644 && TYPE_OVERFLOW_WRAPS (sizetype) 6645 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype)) 6646 /* Calculate the rhs constant. */ 6647 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0); 6648 offset_int rhs = off * 2; } 6649 /* Always fails for negative values. */ 6650 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype)) 6651 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p 6652 pick a canonical order. This increases the chances of using the 6653 same pointer_plus in multiple checks. */ 6654 (with { bool swap_p = tree_swap_operands_p (@0, @2); 6655 tree rhs_tree = wide_int_to_tree (sizetype, rhs); } 6656 (if (cmp == LT_EXPR) 6657 (gt (convert:sizetype 6658 (pointer_diff:ssizetype { swap_p ? @4 : @3; } 6659 { swap_p ? @0 : @2; })) 6660 { rhs_tree; }) 6661 (gt (convert:sizetype 6662 (pointer_diff:ssizetype 6663 (pointer_plus { swap_p ? @2 : @0; } 6664 { wide_int_to_tree (sizetype, off); }) 6665 { swap_p ? @0 : @2; })) 6666 { rhs_tree; }))))))))) 6667 6668/* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero 6669 element of @1. */ 6670(for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR) 6671 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1))) 6672 (with { int i = single_nonzero_element (@1); } 6673 (if (i >= 0) 6674 (with { tree elt = vector_cst_elt (@1, i); 6675 tree elt_type = TREE_TYPE (elt); 6676 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type)); 6677 tree size = bitsize_int (elt_bits); 6678 tree pos = bitsize_int (elt_bits * i); } 6679 (view_convert 6680 (bit_and:elt_type 6681 (BIT_FIELD_REF:elt_type @0 { size; } { pos; }) 6682 { elt; }))))))) 6683 6684(simplify 6685 (vec_perm @0 @1 VECTOR_CST@2) 6686 (with 6687 { 6688 tree op0 = @0, op1 = @1, op2 = @2; 6689 6690 /* Build a vector of integers from the tree mask. */ 6691 vec_perm_builder builder; 6692 if (!tree_to_vec_perm_builder (&builder, op2)) 6693 return NULL_TREE; 6694 6695 /* Create a vec_perm_indices for the integer vector. */ 6696 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type); 6697 bool single_arg = (op0 == op1); 6698 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts); 6699 } 6700 (if (sel.series_p (0, 1, 0, 1)) 6701 { op0; } 6702 (if (sel.series_p (0, 1, nelts, 1)) 6703 { op1; } 6704 (with 6705 { 6706 if (!single_arg) 6707 { 6708 if (sel.all_from_input_p (0)) 6709 op1 = op0; 6710 else if (sel.all_from_input_p (1)) 6711 { 6712 op0 = op1; 6713 sel.rotate_inputs (1); 6714 } 6715 else if (known_ge (poly_uint64 (sel[0]), nelts)) 6716 { 6717 std::swap (op0, op1); 6718 sel.rotate_inputs (1); 6719 } 6720 } 6721 gassign *def; 6722 tree cop0 = op0, cop1 = op1; 6723 if (TREE_CODE (op0) == SSA_NAME 6724 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0))) 6725 && gimple_assign_rhs_code (def) == CONSTRUCTOR) 6726 cop0 = gimple_assign_rhs1 (def); 6727 if (TREE_CODE (op1) == SSA_NAME 6728 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1))) 6729 && gimple_assign_rhs_code (def) == CONSTRUCTOR) 6730 cop1 = gimple_assign_rhs1 (def); 6731 6732 tree t; 6733 } 6734 (if ((TREE_CODE (cop0) == VECTOR_CST 6735 || TREE_CODE (cop0) == CONSTRUCTOR) 6736 && (TREE_CODE (cop1) == VECTOR_CST 6737 || TREE_CODE (cop1) == CONSTRUCTOR) 6738 && (t = fold_vec_perm (type, cop0, cop1, sel))) 6739 { t; } 6740 (with 6741 { 6742 bool changed = (op0 == op1 && !single_arg); 6743 tree ins = NULL_TREE; 6744 unsigned at = 0; 6745 6746 /* See if the permutation is performing a single element 6747 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR 6748 in that case. But only if the vector mode is supported, 6749 otherwise this is invalid GIMPLE. */ 6750 if (TYPE_MODE (type) != BLKmode 6751 && (TREE_CODE (cop0) == VECTOR_CST 6752 || TREE_CODE (cop0) == CONSTRUCTOR 6753 || TREE_CODE (cop1) == VECTOR_CST 6754 || TREE_CODE (cop1) == CONSTRUCTOR)) 6755 { 6756 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1); 6757 if (insert_first_p) 6758 { 6759 /* After canonicalizing the first elt to come from the 6760 first vector we only can insert the first elt from 6761 the first vector. */ 6762 at = 0; 6763 if ((ins = fold_read_from_vector (cop0, sel[0]))) 6764 op0 = op1; 6765 } 6766 /* The above can fail for two-element vectors which always 6767 appear to insert the first element, so try inserting 6768 into the second lane as well. For more than two 6769 elements that's wasted time. */ 6770 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u))) 6771 { 6772 unsigned int encoded_nelts = sel.encoding ().encoded_nelts (); 6773 for (at = 0; at < encoded_nelts; ++at) 6774 if (maybe_ne (sel[at], at)) 6775 break; 6776 if (at < encoded_nelts 6777 && (known_eq (at + 1, nelts) 6778 || sel.series_p (at + 1, 1, at + 1, 1))) 6779 { 6780 if (known_lt (poly_uint64 (sel[at]), nelts)) 6781 ins = fold_read_from_vector (cop0, sel[at]); 6782 else 6783 ins = fold_read_from_vector (cop1, sel[at] - nelts); 6784 } 6785 } 6786 } 6787 6788 /* Generate a canonical form of the selector. */ 6789 if (!ins && sel.encoding () != builder) 6790 { 6791 /* Some targets are deficient and fail to expand a single 6792 argument permutation while still allowing an equivalent 6793 2-argument version. */ 6794 tree oldop2 = op2; 6795 if (sel.ninputs () == 2 6796 || can_vec_perm_const_p (TYPE_MODE (type), sel, false)) 6797 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel); 6798 else 6799 { 6800 vec_perm_indices sel2 (builder, 2, nelts); 6801 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false)) 6802 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2); 6803 else 6804 /* Not directly supported with either encoding, 6805 so use the preferred form. */ 6806 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel); 6807 } 6808 if (!operand_equal_p (op2, oldop2, 0)) 6809 changed = true; 6810 } 6811 } 6812 (if (ins) 6813 (bit_insert { op0; } { ins; } 6814 { bitsize_int (at * vector_element_bits (type)); }) 6815 (if (changed) 6816 (vec_perm { op0; } { op1; } { op2; })))))))))) 6817 6818/* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */ 6819 6820(match vec_same_elem_p 6821 @0 6822 (if (uniform_vector_p (@0)))) 6823 6824(match vec_same_elem_p 6825 (vec_duplicate @0)) 6826 6827(simplify 6828 (vec_perm vec_same_elem_p@0 @0 @1) 6829 @0) 6830 6831/* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop. 6832 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic 6833 constant which when multiplied by a power of 2 contains a unique value 6834 in the top 5 or 6 bits. This is then indexed into a table which maps it 6835 to the number of trailing zeroes. */ 6836(match (ctz_table_index @1 @2 @3) 6837 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3)) 6838