1 /* Support routines for Value Range Propagation (VRP). 2 Copyright (C) 2005-2017 Free Software Foundation, Inc. 3 Contributed by Diego Novillo <dnovillo@redhat.com>. 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3, or (at your option) 10 any later version. 11 12 GCC is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 #include "config.h" 22 #include "system.h" 23 #include "coretypes.h" 24 #include "backend.h" 25 #include "insn-codes.h" 26 #include "rtl.h" 27 #include "tree.h" 28 #include "gimple.h" 29 #include "cfghooks.h" 30 #include "tree-pass.h" 31 #include "ssa.h" 32 #include "optabs-tree.h" 33 #include "gimple-pretty-print.h" 34 #include "diagnostic-core.h" 35 #include "flags.h" 36 #include "fold-const.h" 37 #include "stor-layout.h" 38 #include "calls.h" 39 #include "cfganal.h" 40 #include "gimple-fold.h" 41 #include "tree-eh.h" 42 #include "gimple-iterator.h" 43 #include "gimple-walk.h" 44 #include "tree-cfg.h" 45 #include "tree-ssa-loop-manip.h" 46 #include "tree-ssa-loop-niter.h" 47 #include "tree-ssa-loop.h" 48 #include "tree-into-ssa.h" 49 #include "tree-ssa.h" 50 #include "intl.h" 51 #include "cfgloop.h" 52 #include "tree-scalar-evolution.h" 53 #include "tree-ssa-propagate.h" 54 #include "tree-chrec.h" 55 #include "tree-ssa-threadupdate.h" 56 #include "tree-ssa-scopedtables.h" 57 #include "tree-ssa-threadedge.h" 58 #include "omp-general.h" 59 #include "target.h" 60 #include "case-cfn-macros.h" 61 #include "params.h" 62 #include "alloc-pool.h" 63 #include "domwalk.h" 64 #include "tree-cfgcleanup.h" 65 66 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL } 67 68 /* Allocation pools for tree-vrp allocations. */ 69 static object_allocator<value_range> vrp_value_range_pool ("Tree VRP value ranges"); 70 static bitmap_obstack vrp_equiv_obstack; 71 72 /* Set of SSA names found live during the RPO traversal of the function 73 for still active basic-blocks. */ 74 static sbitmap *live; 75 76 /* Return true if the SSA name NAME is live on the edge E. */ 77 78 static bool 79 live_on_edge (edge e, tree name) 80 { 81 return (live[e->dest->index] 82 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name))); 83 } 84 85 /* Local functions. */ 86 static int compare_values (tree val1, tree val2); 87 static int compare_values_warnv (tree val1, tree val2, bool *); 88 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code, 89 tree, tree, bool, bool *, 90 bool *); 91 92 /* Location information for ASSERT_EXPRs. Each instance of this 93 structure describes an ASSERT_EXPR for an SSA name. Since a single 94 SSA name may have more than one assertion associated with it, these 95 locations are kept in a linked list attached to the corresponding 96 SSA name. */ 97 struct assert_locus 98 { 99 /* Basic block where the assertion would be inserted. */ 100 basic_block bb; 101 102 /* Some assertions need to be inserted on an edge (e.g., assertions 103 generated by COND_EXPRs). In those cases, BB will be NULL. */ 104 edge e; 105 106 /* Pointer to the statement that generated this assertion. */ 107 gimple_stmt_iterator si; 108 109 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */ 110 enum tree_code comp_code; 111 112 /* Value being compared against. */ 113 tree val; 114 115 /* Expression to compare. */ 116 tree expr; 117 118 /* Next node in the linked list. */ 119 assert_locus *next; 120 }; 121 122 /* If bit I is present, it means that SSA name N_i has a list of 123 assertions that should be inserted in the IL. */ 124 static bitmap need_assert_for; 125 126 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I] 127 holds a list of ASSERT_LOCUS_T nodes that describe where 128 ASSERT_EXPRs for SSA name N_I should be inserted. */ 129 static assert_locus **asserts_for; 130 131 /* Value range array. After propagation, VR_VALUE[I] holds the range 132 of values that SSA name N_I may take. */ 133 static unsigned num_vr_values; 134 static value_range **vr_value; 135 static bool values_propagated; 136 137 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the 138 number of executable edges we saw the last time we visited the 139 node. */ 140 static int *vr_phi_edge_counts; 141 142 struct switch_update { 143 gswitch *stmt; 144 tree vec; 145 }; 146 147 static vec<edge> to_remove_edges; 148 static vec<switch_update> to_update_switch_stmts; 149 150 151 /* Return the maximum value for TYPE. */ 152 153 static inline tree 154 vrp_val_max (const_tree type) 155 { 156 if (!INTEGRAL_TYPE_P (type)) 157 return NULL_TREE; 158 159 return TYPE_MAX_VALUE (type); 160 } 161 162 /* Return the minimum value for TYPE. */ 163 164 static inline tree 165 vrp_val_min (const_tree type) 166 { 167 if (!INTEGRAL_TYPE_P (type)) 168 return NULL_TREE; 169 170 return TYPE_MIN_VALUE (type); 171 } 172 173 /* Return whether VAL is equal to the maximum value of its type. This 174 will be true for a positive overflow infinity. We can't do a 175 simple equality comparison with TYPE_MAX_VALUE because C typedefs 176 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not == 177 to the integer constant with the same value in the type. */ 178 179 static inline bool 180 vrp_val_is_max (const_tree val) 181 { 182 tree type_max = vrp_val_max (TREE_TYPE (val)); 183 return (val == type_max 184 || (type_max != NULL_TREE 185 && operand_equal_p (val, type_max, 0))); 186 } 187 188 /* Return whether VAL is equal to the minimum value of its type. This 189 will be true for a negative overflow infinity. */ 190 191 static inline bool 192 vrp_val_is_min (const_tree val) 193 { 194 tree type_min = vrp_val_min (TREE_TYPE (val)); 195 return (val == type_min 196 || (type_min != NULL_TREE 197 && operand_equal_p (val, type_min, 0))); 198 } 199 200 201 /* Return whether TYPE should use an overflow infinity distinct from 202 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to 203 represent a signed overflow during VRP computations. An infinity 204 is distinct from a half-range, which will go from some number to 205 TYPE_{MIN,MAX}_VALUE. */ 206 207 static inline bool 208 needs_overflow_infinity (const_tree type) 209 { 210 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type); 211 } 212 213 /* Return whether TYPE can support our overflow infinity 214 representation: we use the TREE_OVERFLOW flag, which only exists 215 for constants. If TYPE doesn't support this, we don't optimize 216 cases which would require signed overflow--we drop them to 217 VARYING. */ 218 219 static inline bool 220 supports_overflow_infinity (const_tree type) 221 { 222 tree min = vrp_val_min (type), max = vrp_val_max (type); 223 gcc_checking_assert (needs_overflow_infinity (type)); 224 return (min != NULL_TREE 225 && CONSTANT_CLASS_P (min) 226 && max != NULL_TREE 227 && CONSTANT_CLASS_P (max)); 228 } 229 230 /* VAL is the maximum or minimum value of a type. Return a 231 corresponding overflow infinity. */ 232 233 static inline tree 234 make_overflow_infinity (tree val) 235 { 236 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val)); 237 val = copy_node (val); 238 TREE_OVERFLOW (val) = 1; 239 return val; 240 } 241 242 /* Return a negative overflow infinity for TYPE. */ 243 244 static inline tree 245 negative_overflow_infinity (tree type) 246 { 247 gcc_checking_assert (supports_overflow_infinity (type)); 248 return make_overflow_infinity (vrp_val_min (type)); 249 } 250 251 /* Return a positive overflow infinity for TYPE. */ 252 253 static inline tree 254 positive_overflow_infinity (tree type) 255 { 256 gcc_checking_assert (supports_overflow_infinity (type)); 257 return make_overflow_infinity (vrp_val_max (type)); 258 } 259 260 /* Return whether VAL is a negative overflow infinity. */ 261 262 static inline bool 263 is_negative_overflow_infinity (const_tree val) 264 { 265 return (TREE_OVERFLOW_P (val) 266 && needs_overflow_infinity (TREE_TYPE (val)) 267 && vrp_val_is_min (val)); 268 } 269 270 /* Return whether VAL is a positive overflow infinity. */ 271 272 static inline bool 273 is_positive_overflow_infinity (const_tree val) 274 { 275 return (TREE_OVERFLOW_P (val) 276 && needs_overflow_infinity (TREE_TYPE (val)) 277 && vrp_val_is_max (val)); 278 } 279 280 /* Return whether VAL is a positive or negative overflow infinity. */ 281 282 static inline bool 283 is_overflow_infinity (const_tree val) 284 { 285 return (TREE_OVERFLOW_P (val) 286 && needs_overflow_infinity (TREE_TYPE (val)) 287 && (vrp_val_is_min (val) || vrp_val_is_max (val))); 288 } 289 290 /* Return whether STMT has a constant rhs that is_overflow_infinity. */ 291 292 static inline bool 293 stmt_overflow_infinity (gimple *stmt) 294 { 295 if (is_gimple_assign (stmt) 296 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) == 297 GIMPLE_SINGLE_RHS) 298 return is_overflow_infinity (gimple_assign_rhs1 (stmt)); 299 return false; 300 } 301 302 /* If VAL is now an overflow infinity, return VAL. Otherwise, return 303 the same value with TREE_OVERFLOW clear. This can be used to avoid 304 confusing a regular value with an overflow value. */ 305 306 static inline tree 307 avoid_overflow_infinity (tree val) 308 { 309 if (!is_overflow_infinity (val)) 310 return val; 311 312 if (vrp_val_is_max (val)) 313 return vrp_val_max (TREE_TYPE (val)); 314 else 315 { 316 gcc_checking_assert (vrp_val_is_min (val)); 317 return vrp_val_min (TREE_TYPE (val)); 318 } 319 } 320 321 322 /* Set value range VR to VR_UNDEFINED. */ 323 324 static inline void 325 set_value_range_to_undefined (value_range *vr) 326 { 327 vr->type = VR_UNDEFINED; 328 vr->min = vr->max = NULL_TREE; 329 if (vr->equiv) 330 bitmap_clear (vr->equiv); 331 } 332 333 334 /* Set value range VR to VR_VARYING. */ 335 336 static inline void 337 set_value_range_to_varying (value_range *vr) 338 { 339 vr->type = VR_VARYING; 340 vr->min = vr->max = NULL_TREE; 341 if (vr->equiv) 342 bitmap_clear (vr->equiv); 343 } 344 345 346 /* Set value range VR to {T, MIN, MAX, EQUIV}. */ 347 348 static void 349 set_value_range (value_range *vr, enum value_range_type t, tree min, 350 tree max, bitmap equiv) 351 { 352 /* Check the validity of the range. */ 353 if (flag_checking 354 && (t == VR_RANGE || t == VR_ANTI_RANGE)) 355 { 356 int cmp; 357 358 gcc_assert (min && max); 359 360 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min)) 361 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max))); 362 363 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE) 364 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max)); 365 366 cmp = compare_values (min, max); 367 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2); 368 } 369 370 if (flag_checking 371 && (t == VR_UNDEFINED || t == VR_VARYING)) 372 { 373 gcc_assert (min == NULL_TREE && max == NULL_TREE); 374 gcc_assert (equiv == NULL || bitmap_empty_p (equiv)); 375 } 376 377 vr->type = t; 378 vr->min = min; 379 vr->max = max; 380 381 /* Since updating the equivalence set involves deep copying the 382 bitmaps, only do it if absolutely necessary. */ 383 if (vr->equiv == NULL 384 && equiv != NULL) 385 vr->equiv = BITMAP_ALLOC (&vrp_equiv_obstack); 386 387 if (equiv != vr->equiv) 388 { 389 if (equiv && !bitmap_empty_p (equiv)) 390 bitmap_copy (vr->equiv, equiv); 391 else 392 bitmap_clear (vr->equiv); 393 } 394 } 395 396 397 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}. 398 This means adjusting T, MIN and MAX representing the case of a 399 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX] 400 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges. 401 In corner cases where MAX+1 or MIN-1 wraps this will fall back 402 to varying. 403 This routine exists to ease canonicalization in the case where we 404 extract ranges from var + CST op limit. */ 405 406 static void 407 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t, 408 tree min, tree max, bitmap equiv) 409 { 410 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */ 411 if (t == VR_UNDEFINED) 412 { 413 set_value_range_to_undefined (vr); 414 return; 415 } 416 else if (t == VR_VARYING) 417 { 418 set_value_range_to_varying (vr); 419 return; 420 } 421 422 /* Nothing to canonicalize for symbolic ranges. */ 423 if (TREE_CODE (min) != INTEGER_CST 424 || TREE_CODE (max) != INTEGER_CST) 425 { 426 set_value_range (vr, t, min, max, equiv); 427 return; 428 } 429 430 /* Wrong order for min and max, to swap them and the VR type we need 431 to adjust them. */ 432 if (tree_int_cst_lt (max, min)) 433 { 434 tree one, tmp; 435 436 /* For one bit precision if max < min, then the swapped 437 range covers all values, so for VR_RANGE it is varying and 438 for VR_ANTI_RANGE empty range, so drop to varying as well. */ 439 if (TYPE_PRECISION (TREE_TYPE (min)) == 1) 440 { 441 set_value_range_to_varying (vr); 442 return; 443 } 444 445 one = build_int_cst (TREE_TYPE (min), 1); 446 tmp = int_const_binop (PLUS_EXPR, max, one); 447 max = int_const_binop (MINUS_EXPR, min, one); 448 min = tmp; 449 450 /* There's one corner case, if we had [C+1, C] before we now have 451 that again. But this represents an empty value range, so drop 452 to varying in this case. */ 453 if (tree_int_cst_lt (max, min)) 454 { 455 set_value_range_to_varying (vr); 456 return; 457 } 458 459 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE; 460 } 461 462 /* Anti-ranges that can be represented as ranges should be so. */ 463 if (t == VR_ANTI_RANGE) 464 { 465 bool is_min = vrp_val_is_min (min); 466 bool is_max = vrp_val_is_max (max); 467 468 if (is_min && is_max) 469 { 470 /* We cannot deal with empty ranges, drop to varying. 471 ??? This could be VR_UNDEFINED instead. */ 472 set_value_range_to_varying (vr); 473 return; 474 } 475 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1 476 && (is_min || is_max)) 477 { 478 /* Non-empty boolean ranges can always be represented 479 as a singleton range. */ 480 if (is_min) 481 min = max = vrp_val_max (TREE_TYPE (min)); 482 else 483 min = max = vrp_val_min (TREE_TYPE (min)); 484 t = VR_RANGE; 485 } 486 else if (is_min 487 /* As a special exception preserve non-null ranges. */ 488 && !(TYPE_UNSIGNED (TREE_TYPE (min)) 489 && integer_zerop (max))) 490 { 491 tree one = build_int_cst (TREE_TYPE (max), 1); 492 min = int_const_binop (PLUS_EXPR, max, one); 493 max = vrp_val_max (TREE_TYPE (max)); 494 t = VR_RANGE; 495 } 496 else if (is_max) 497 { 498 tree one = build_int_cst (TREE_TYPE (min), 1); 499 max = int_const_binop (MINUS_EXPR, min, one); 500 min = vrp_val_min (TREE_TYPE (min)); 501 t = VR_RANGE; 502 } 503 } 504 505 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky 506 to make sure VRP iteration terminates, otherwise we can get into 507 oscillations. */ 508 509 set_value_range (vr, t, min, max, equiv); 510 } 511 512 /* Copy value range FROM into value range TO. */ 513 514 static inline void 515 copy_value_range (value_range *to, value_range *from) 516 { 517 set_value_range (to, from->type, from->min, from->max, from->equiv); 518 } 519 520 /* Set value range VR to a single value. This function is only called 521 with values we get from statements, and exists to clear the 522 TREE_OVERFLOW flag so that we don't think we have an overflow 523 infinity when we shouldn't. */ 524 525 static inline void 526 set_value_range_to_value (value_range *vr, tree val, bitmap equiv) 527 { 528 gcc_assert (is_gimple_min_invariant (val)); 529 if (TREE_OVERFLOW_P (val)) 530 val = drop_tree_overflow (val); 531 set_value_range (vr, VR_RANGE, val, val, equiv); 532 } 533 534 /* Set value range VR to a non-negative range of type TYPE. 535 OVERFLOW_INFINITY indicates whether to use an overflow infinity 536 rather than TYPE_MAX_VALUE; this should be true if we determine 537 that the range is nonnegative based on the assumption that signed 538 overflow does not occur. */ 539 540 static inline void 541 set_value_range_to_nonnegative (value_range *vr, tree type, 542 bool overflow_infinity) 543 { 544 tree zero; 545 546 if (overflow_infinity && !supports_overflow_infinity (type)) 547 { 548 set_value_range_to_varying (vr); 549 return; 550 } 551 552 zero = build_int_cst (type, 0); 553 set_value_range (vr, VR_RANGE, zero, 554 (overflow_infinity 555 ? positive_overflow_infinity (type) 556 : TYPE_MAX_VALUE (type)), 557 vr->equiv); 558 } 559 560 /* Set value range VR to a non-NULL range of type TYPE. */ 561 562 static inline void 563 set_value_range_to_nonnull (value_range *vr, tree type) 564 { 565 tree zero = build_int_cst (type, 0); 566 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv); 567 } 568 569 570 /* Set value range VR to a NULL range of type TYPE. */ 571 572 static inline void 573 set_value_range_to_null (value_range *vr, tree type) 574 { 575 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv); 576 } 577 578 579 /* Set value range VR to a range of a truthvalue of type TYPE. */ 580 581 static inline void 582 set_value_range_to_truthvalue (value_range *vr, tree type) 583 { 584 if (TYPE_PRECISION (type) == 1) 585 set_value_range_to_varying (vr); 586 else 587 set_value_range (vr, VR_RANGE, 588 build_int_cst (type, 0), build_int_cst (type, 1), 589 vr->equiv); 590 } 591 592 593 /* If abs (min) < abs (max), set VR to [-max, max], if 594 abs (min) >= abs (max), set VR to [-min, min]. */ 595 596 static void 597 abs_extent_range (value_range *vr, tree min, tree max) 598 { 599 int cmp; 600 601 gcc_assert (TREE_CODE (min) == INTEGER_CST); 602 gcc_assert (TREE_CODE (max) == INTEGER_CST); 603 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min))); 604 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min))); 605 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min); 606 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max); 607 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max)) 608 { 609 set_value_range_to_varying (vr); 610 return; 611 } 612 cmp = compare_values (min, max); 613 if (cmp == -1) 614 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max); 615 else if (cmp == 0 || cmp == 1) 616 { 617 max = min; 618 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min); 619 } 620 else 621 { 622 set_value_range_to_varying (vr); 623 return; 624 } 625 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL); 626 } 627 628 629 /* Return value range information for VAR. 630 631 If we have no values ranges recorded (ie, VRP is not running), then 632 return NULL. Otherwise create an empty range if none existed for VAR. */ 633 634 static value_range * 635 get_value_range (const_tree var) 636 { 637 static const value_range vr_const_varying 638 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL }; 639 value_range *vr; 640 tree sym; 641 unsigned ver = SSA_NAME_VERSION (var); 642 643 /* If we have no recorded ranges, then return NULL. */ 644 if (! vr_value) 645 return NULL; 646 647 /* If we query the range for a new SSA name return an unmodifiable VARYING. 648 We should get here at most from the substitute-and-fold stage which 649 will never try to change values. */ 650 if (ver >= num_vr_values) 651 return CONST_CAST (value_range *, &vr_const_varying); 652 653 vr = vr_value[ver]; 654 if (vr) 655 return vr; 656 657 /* After propagation finished do not allocate new value-ranges. */ 658 if (values_propagated) 659 return CONST_CAST (value_range *, &vr_const_varying); 660 661 /* Create a default value range. */ 662 vr_value[ver] = vr = vrp_value_range_pool.allocate (); 663 memset (vr, 0, sizeof (*vr)); 664 665 /* Defer allocating the equivalence set. */ 666 vr->equiv = NULL; 667 668 /* If VAR is a default definition of a parameter, the variable can 669 take any value in VAR's type. */ 670 if (SSA_NAME_IS_DEFAULT_DEF (var)) 671 { 672 sym = SSA_NAME_VAR (var); 673 if (TREE_CODE (sym) == PARM_DECL) 674 { 675 /* Try to use the "nonnull" attribute to create ~[0, 0] 676 anti-ranges for pointers. Note that this is only valid with 677 default definitions of PARM_DECLs. */ 678 if (POINTER_TYPE_P (TREE_TYPE (sym)) 679 && (nonnull_arg_p (sym) 680 || get_ptr_nonnull (var))) 681 set_value_range_to_nonnull (vr, TREE_TYPE (sym)); 682 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym))) 683 { 684 wide_int min, max; 685 value_range_type rtype = get_range_info (var, &min, &max); 686 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE) 687 set_value_range (vr, rtype, 688 wide_int_to_tree (TREE_TYPE (var), min), 689 wide_int_to_tree (TREE_TYPE (var), max), 690 NULL); 691 else 692 set_value_range_to_varying (vr); 693 } 694 else 695 set_value_range_to_varying (vr); 696 } 697 else if (TREE_CODE (sym) == RESULT_DECL 698 && DECL_BY_REFERENCE (sym)) 699 set_value_range_to_nonnull (vr, TREE_TYPE (sym)); 700 } 701 702 return vr; 703 } 704 705 /* Set value-ranges of all SSA names defined by STMT to varying. */ 706 707 static void 708 set_defs_to_varying (gimple *stmt) 709 { 710 ssa_op_iter i; 711 tree def; 712 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF) 713 { 714 value_range *vr = get_value_range (def); 715 /* Avoid writing to vr_const_varying get_value_range may return. */ 716 if (vr->type != VR_VARYING) 717 set_value_range_to_varying (vr); 718 } 719 } 720 721 722 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */ 723 724 static inline bool 725 vrp_operand_equal_p (const_tree val1, const_tree val2) 726 { 727 if (val1 == val2) 728 return true; 729 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0)) 730 return false; 731 return is_overflow_infinity (val1) == is_overflow_infinity (val2); 732 } 733 734 /* Return true, if the bitmaps B1 and B2 are equal. */ 735 736 static inline bool 737 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2) 738 { 739 return (b1 == b2 740 || ((!b1 || bitmap_empty_p (b1)) 741 && (!b2 || bitmap_empty_p (b2))) 742 || (b1 && b2 743 && bitmap_equal_p (b1, b2))); 744 } 745 746 /* Update the value range and equivalence set for variable VAR to 747 NEW_VR. Return true if NEW_VR is different from VAR's previous 748 value. 749 750 NOTE: This function assumes that NEW_VR is a temporary value range 751 object created for the sole purpose of updating VAR's range. The 752 storage used by the equivalence set from NEW_VR will be freed by 753 this function. Do not call update_value_range when NEW_VR 754 is the range object associated with another SSA name. */ 755 756 static inline bool 757 update_value_range (const_tree var, value_range *new_vr) 758 { 759 value_range *old_vr; 760 bool is_new; 761 762 /* If there is a value-range on the SSA name from earlier analysis 763 factor that in. */ 764 if (INTEGRAL_TYPE_P (TREE_TYPE (var))) 765 { 766 wide_int min, max; 767 value_range_type rtype = get_range_info (var, &min, &max); 768 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE) 769 { 770 tree nr_min, nr_max; 771 /* Range info on SSA names doesn't carry overflow information 772 so make sure to preserve the overflow bit on the lattice. */ 773 if (rtype == VR_RANGE 774 && needs_overflow_infinity (TREE_TYPE (var)) 775 && (new_vr->type == VR_VARYING 776 || (new_vr->type == VR_RANGE 777 && is_negative_overflow_infinity (new_vr->min))) 778 && wi::eq_p (vrp_val_min (TREE_TYPE (var)), min)) 779 nr_min = negative_overflow_infinity (TREE_TYPE (var)); 780 else 781 nr_min = wide_int_to_tree (TREE_TYPE (var), min); 782 if (rtype == VR_RANGE 783 && needs_overflow_infinity (TREE_TYPE (var)) 784 && (new_vr->type == VR_VARYING 785 || (new_vr->type == VR_RANGE 786 && is_positive_overflow_infinity (new_vr->max))) 787 && wi::eq_p (vrp_val_max (TREE_TYPE (var)), max)) 788 nr_max = positive_overflow_infinity (TREE_TYPE (var)); 789 else 790 nr_max = wide_int_to_tree (TREE_TYPE (var), max); 791 value_range nr = VR_INITIALIZER; 792 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL); 793 vrp_intersect_ranges (new_vr, &nr); 794 } 795 } 796 797 /* Update the value range, if necessary. */ 798 old_vr = get_value_range (var); 799 is_new = old_vr->type != new_vr->type 800 || !vrp_operand_equal_p (old_vr->min, new_vr->min) 801 || !vrp_operand_equal_p (old_vr->max, new_vr->max) 802 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv); 803 804 if (is_new) 805 { 806 /* Do not allow transitions up the lattice. The following 807 is slightly more awkward than just new_vr->type < old_vr->type 808 because VR_RANGE and VR_ANTI_RANGE need to be considered 809 the same. We may not have is_new when transitioning to 810 UNDEFINED. If old_vr->type is VARYING, we shouldn't be 811 called. */ 812 if (new_vr->type == VR_UNDEFINED) 813 { 814 BITMAP_FREE (new_vr->equiv); 815 set_value_range_to_varying (old_vr); 816 set_value_range_to_varying (new_vr); 817 return true; 818 } 819 else 820 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max, 821 new_vr->equiv); 822 } 823 824 BITMAP_FREE (new_vr->equiv); 825 826 return is_new; 827 } 828 829 830 /* Add VAR and VAR's equivalence set to EQUIV. This is the central 831 point where equivalence processing can be turned on/off. */ 832 833 static void 834 add_equivalence (bitmap *equiv, const_tree var) 835 { 836 unsigned ver = SSA_NAME_VERSION (var); 837 value_range *vr = get_value_range (var); 838 839 if (*equiv == NULL) 840 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack); 841 bitmap_set_bit (*equiv, ver); 842 if (vr && vr->equiv) 843 bitmap_ior_into (*equiv, vr->equiv); 844 } 845 846 847 /* Return true if VR is ~[0, 0]. */ 848 849 static inline bool 850 range_is_nonnull (value_range *vr) 851 { 852 return vr->type == VR_ANTI_RANGE 853 && integer_zerop (vr->min) 854 && integer_zerop (vr->max); 855 } 856 857 858 /* Return true if VR is [0, 0]. */ 859 860 static inline bool 861 range_is_null (value_range *vr) 862 { 863 return vr->type == VR_RANGE 864 && integer_zerop (vr->min) 865 && integer_zerop (vr->max); 866 } 867 868 /* Return true if max and min of VR are INTEGER_CST. It's not necessary 869 a singleton. */ 870 871 static inline bool 872 range_int_cst_p (value_range *vr) 873 { 874 return (vr->type == VR_RANGE 875 && TREE_CODE (vr->max) == INTEGER_CST 876 && TREE_CODE (vr->min) == INTEGER_CST); 877 } 878 879 /* Return true if VR is a INTEGER_CST singleton. */ 880 881 static inline bool 882 range_int_cst_singleton_p (value_range *vr) 883 { 884 return (range_int_cst_p (vr) 885 && !is_overflow_infinity (vr->min) 886 && !is_overflow_infinity (vr->max) 887 && tree_int_cst_equal (vr->min, vr->max)); 888 } 889 890 /* Return true if value range VR involves at least one symbol. */ 891 892 static inline bool 893 symbolic_range_p (value_range *vr) 894 { 895 return (!is_gimple_min_invariant (vr->min) 896 || !is_gimple_min_invariant (vr->max)); 897 } 898 899 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE 900 otherwise. We only handle additive operations and set NEG to true if the 901 symbol is negated and INV to the invariant part, if any. */ 902 903 static tree 904 get_single_symbol (tree t, bool *neg, tree *inv) 905 { 906 bool neg_; 907 tree inv_; 908 909 *inv = NULL_TREE; 910 *neg = false; 911 912 if (TREE_CODE (t) == PLUS_EXPR 913 || TREE_CODE (t) == POINTER_PLUS_EXPR 914 || TREE_CODE (t) == MINUS_EXPR) 915 { 916 if (is_gimple_min_invariant (TREE_OPERAND (t, 0))) 917 { 918 neg_ = (TREE_CODE (t) == MINUS_EXPR); 919 inv_ = TREE_OPERAND (t, 0); 920 t = TREE_OPERAND (t, 1); 921 } 922 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1))) 923 { 924 neg_ = false; 925 inv_ = TREE_OPERAND (t, 1); 926 t = TREE_OPERAND (t, 0); 927 } 928 else 929 return NULL_TREE; 930 } 931 else 932 { 933 neg_ = false; 934 inv_ = NULL_TREE; 935 } 936 937 if (TREE_CODE (t) == NEGATE_EXPR) 938 { 939 t = TREE_OPERAND (t, 0); 940 neg_ = !neg_; 941 } 942 943 if (TREE_CODE (t) != SSA_NAME) 944 return NULL_TREE; 945 946 *neg = neg_; 947 *inv = inv_; 948 return t; 949 } 950 951 /* The reverse operation: build a symbolic expression with TYPE 952 from symbol SYM, negated according to NEG, and invariant INV. */ 953 954 static tree 955 build_symbolic_expr (tree type, tree sym, bool neg, tree inv) 956 { 957 const bool pointer_p = POINTER_TYPE_P (type); 958 tree t = sym; 959 960 if (neg) 961 t = build1 (NEGATE_EXPR, type, t); 962 963 if (integer_zerop (inv)) 964 return t; 965 966 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv); 967 } 968 969 /* Return true if value range VR involves exactly one symbol SYM. */ 970 971 static bool 972 symbolic_range_based_on_p (value_range *vr, const_tree sym) 973 { 974 bool neg, min_has_symbol, max_has_symbol; 975 tree inv; 976 977 if (is_gimple_min_invariant (vr->min)) 978 min_has_symbol = false; 979 else if (get_single_symbol (vr->min, &neg, &inv) == sym) 980 min_has_symbol = true; 981 else 982 return false; 983 984 if (is_gimple_min_invariant (vr->max)) 985 max_has_symbol = false; 986 else if (get_single_symbol (vr->max, &neg, &inv) == sym) 987 max_has_symbol = true; 988 else 989 return false; 990 991 return (min_has_symbol || max_has_symbol); 992 } 993 994 /* Return true if value range VR uses an overflow infinity. */ 995 996 static inline bool 997 overflow_infinity_range_p (value_range *vr) 998 { 999 return (vr->type == VR_RANGE 1000 && (is_overflow_infinity (vr->min) 1001 || is_overflow_infinity (vr->max))); 1002 } 1003 1004 /* Return false if we can not make a valid comparison based on VR; 1005 this will be the case if it uses an overflow infinity and overflow 1006 is not undefined (i.e., -fno-strict-overflow is in effect). 1007 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR 1008 uses an overflow infinity. */ 1009 1010 static bool 1011 usable_range_p (value_range *vr, bool *strict_overflow_p) 1012 { 1013 gcc_assert (vr->type == VR_RANGE); 1014 if (is_overflow_infinity (vr->min)) 1015 { 1016 *strict_overflow_p = true; 1017 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min))) 1018 return false; 1019 } 1020 if (is_overflow_infinity (vr->max)) 1021 { 1022 *strict_overflow_p = true; 1023 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max))) 1024 return false; 1025 } 1026 return true; 1027 } 1028 1029 /* Return true if the result of assignment STMT is know to be non-zero. 1030 If the return value is based on the assumption that signed overflow is 1031 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change 1032 *STRICT_OVERFLOW_P.*/ 1033 1034 static bool 1035 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p) 1036 { 1037 enum tree_code code = gimple_assign_rhs_code (stmt); 1038 switch (get_gimple_rhs_class (code)) 1039 { 1040 case GIMPLE_UNARY_RHS: 1041 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt), 1042 gimple_expr_type (stmt), 1043 gimple_assign_rhs1 (stmt), 1044 strict_overflow_p); 1045 case GIMPLE_BINARY_RHS: 1046 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt), 1047 gimple_expr_type (stmt), 1048 gimple_assign_rhs1 (stmt), 1049 gimple_assign_rhs2 (stmt), 1050 strict_overflow_p); 1051 case GIMPLE_TERNARY_RHS: 1052 return false; 1053 case GIMPLE_SINGLE_RHS: 1054 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt), 1055 strict_overflow_p); 1056 case GIMPLE_INVALID_RHS: 1057 gcc_unreachable (); 1058 default: 1059 gcc_unreachable (); 1060 } 1061 } 1062 1063 /* Return true if STMT is known to compute a non-zero value. 1064 If the return value is based on the assumption that signed overflow is 1065 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change 1066 *STRICT_OVERFLOW_P.*/ 1067 1068 static bool 1069 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p) 1070 { 1071 switch (gimple_code (stmt)) 1072 { 1073 case GIMPLE_ASSIGN: 1074 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p); 1075 case GIMPLE_CALL: 1076 { 1077 tree fndecl = gimple_call_fndecl (stmt); 1078 if (!fndecl) return false; 1079 if (flag_delete_null_pointer_checks && !flag_check_new 1080 && DECL_IS_OPERATOR_NEW (fndecl) 1081 && !TREE_NOTHROW (fndecl)) 1082 return true; 1083 /* References are always non-NULL. */ 1084 if (flag_delete_null_pointer_checks 1085 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE) 1086 return true; 1087 if (flag_delete_null_pointer_checks && 1088 lookup_attribute ("returns_nonnull", 1089 TYPE_ATTRIBUTES (gimple_call_fntype (stmt)))) 1090 return true; 1091 1092 gcall *call_stmt = as_a<gcall *> (stmt); 1093 unsigned rf = gimple_call_return_flags (call_stmt); 1094 if (rf & ERF_RETURNS_ARG) 1095 { 1096 unsigned argnum = rf & ERF_RETURN_ARG_MASK; 1097 if (argnum < gimple_call_num_args (call_stmt)) 1098 { 1099 tree arg = gimple_call_arg (call_stmt, argnum); 1100 if (SSA_VAR_P (arg) 1101 && infer_nonnull_range_by_attribute (stmt, arg)) 1102 return true; 1103 } 1104 } 1105 return gimple_alloca_call_p (stmt); 1106 } 1107 default: 1108 gcc_unreachable (); 1109 } 1110 } 1111 1112 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges 1113 obtained so far. */ 1114 1115 static bool 1116 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p) 1117 { 1118 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p)) 1119 return true; 1120 1121 /* If we have an expression of the form &X->a, then the expression 1122 is nonnull if X is nonnull. */ 1123 if (is_gimple_assign (stmt) 1124 && gimple_assign_rhs_code (stmt) == ADDR_EXPR) 1125 { 1126 tree expr = gimple_assign_rhs1 (stmt); 1127 tree base = get_base_address (TREE_OPERAND (expr, 0)); 1128 1129 if (base != NULL_TREE 1130 && TREE_CODE (base) == MEM_REF 1131 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME) 1132 { 1133 value_range *vr = get_value_range (TREE_OPERAND (base, 0)); 1134 if (range_is_nonnull (vr)) 1135 return true; 1136 } 1137 } 1138 1139 return false; 1140 } 1141 1142 /* Returns true if EXPR is a valid value (as expected by compare_values) -- 1143 a gimple invariant, or SSA_NAME +- CST. */ 1144 1145 static bool 1146 valid_value_p (tree expr) 1147 { 1148 if (TREE_CODE (expr) == SSA_NAME) 1149 return true; 1150 1151 if (TREE_CODE (expr) == PLUS_EXPR 1152 || TREE_CODE (expr) == MINUS_EXPR) 1153 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME 1154 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST); 1155 1156 return is_gimple_min_invariant (expr); 1157 } 1158 1159 /* Return 1160 1 if VAL < VAL2 1161 0 if !(VAL < VAL2) 1162 -2 if those are incomparable. */ 1163 static inline int 1164 operand_less_p (tree val, tree val2) 1165 { 1166 /* LT is folded faster than GE and others. Inline the common case. */ 1167 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST) 1168 { 1169 if (! is_positive_overflow_infinity (val2)) 1170 return tree_int_cst_lt (val, val2); 1171 } 1172 else 1173 { 1174 tree tcmp; 1175 1176 fold_defer_overflow_warnings (); 1177 1178 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2); 1179 1180 fold_undefer_and_ignore_overflow_warnings (); 1181 1182 if (!tcmp 1183 || TREE_CODE (tcmp) != INTEGER_CST) 1184 return -2; 1185 1186 if (!integer_zerop (tcmp)) 1187 return 1; 1188 } 1189 1190 /* val >= val2, not considering overflow infinity. */ 1191 if (is_negative_overflow_infinity (val)) 1192 return is_negative_overflow_infinity (val2) ? 0 : 1; 1193 else if (is_positive_overflow_infinity (val2)) 1194 return is_positive_overflow_infinity (val) ? 0 : 1; 1195 1196 return 0; 1197 } 1198 1199 /* Compare two values VAL1 and VAL2. Return 1200 1201 -2 if VAL1 and VAL2 cannot be compared at compile-time, 1202 -1 if VAL1 < VAL2, 1203 0 if VAL1 == VAL2, 1204 +1 if VAL1 > VAL2, and 1205 +2 if VAL1 != VAL2 1206 1207 This is similar to tree_int_cst_compare but supports pointer values 1208 and values that cannot be compared at compile time. 1209 1210 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to 1211 true if the return value is only valid if we assume that signed 1212 overflow is undefined. */ 1213 1214 static int 1215 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p) 1216 { 1217 if (val1 == val2) 1218 return 0; 1219 1220 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or 1221 both integers. */ 1222 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1)) 1223 == POINTER_TYPE_P (TREE_TYPE (val2))); 1224 1225 /* Convert the two values into the same type. This is needed because 1226 sizetype causes sign extension even for unsigned types. */ 1227 val2 = fold_convert (TREE_TYPE (val1), val2); 1228 STRIP_USELESS_TYPE_CONVERSION (val2); 1229 1230 const bool overflow_undefined 1231 = INTEGRAL_TYPE_P (TREE_TYPE (val1)) 1232 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)); 1233 tree inv1, inv2; 1234 bool neg1, neg2; 1235 tree sym1 = get_single_symbol (val1, &neg1, &inv1); 1236 tree sym2 = get_single_symbol (val2, &neg2, &inv2); 1237 1238 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1 1239 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */ 1240 if (sym1 && sym2) 1241 { 1242 /* Both values must use the same name with the same sign. */ 1243 if (sym1 != sym2 || neg1 != neg2) 1244 return -2; 1245 1246 /* [-]NAME + CST == [-]NAME + CST. */ 1247 if (inv1 == inv2) 1248 return 0; 1249 1250 /* If overflow is defined we cannot simplify more. */ 1251 if (!overflow_undefined) 1252 return -2; 1253 1254 if (strict_overflow_p != NULL 1255 && (!inv1 || !TREE_NO_WARNING (val1)) 1256 && (!inv2 || !TREE_NO_WARNING (val2))) 1257 *strict_overflow_p = true; 1258 1259 if (!inv1) 1260 inv1 = build_int_cst (TREE_TYPE (val1), 0); 1261 if (!inv2) 1262 inv2 = build_int_cst (TREE_TYPE (val2), 0); 1263 1264 return compare_values_warnv (inv1, inv2, strict_overflow_p); 1265 } 1266 1267 const bool cst1 = is_gimple_min_invariant (val1); 1268 const bool cst2 = is_gimple_min_invariant (val2); 1269 1270 /* If one is of the form '[-]NAME + CST' and the other is constant, then 1271 it might be possible to say something depending on the constants. */ 1272 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1)) 1273 { 1274 if (!overflow_undefined) 1275 return -2; 1276 1277 if (strict_overflow_p != NULL 1278 && (!sym1 || !TREE_NO_WARNING (val1)) 1279 && (!sym2 || !TREE_NO_WARNING (val2))) 1280 *strict_overflow_p = true; 1281 1282 const signop sgn = TYPE_SIGN (TREE_TYPE (val1)); 1283 tree cst = cst1 ? val1 : val2; 1284 tree inv = cst1 ? inv2 : inv1; 1285 1286 /* Compute the difference between the constants. If it overflows or 1287 underflows, this means that we can trivially compare the NAME with 1288 it and, consequently, the two values with each other. */ 1289 wide_int diff = wi::sub (cst, inv); 1290 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn)) 1291 { 1292 const int res = wi::cmp (cst, inv, sgn); 1293 return cst1 ? res : -res; 1294 } 1295 1296 return -2; 1297 } 1298 1299 /* We cannot say anything more for non-constants. */ 1300 if (!cst1 || !cst2) 1301 return -2; 1302 1303 if (!POINTER_TYPE_P (TREE_TYPE (val1))) 1304 { 1305 /* We cannot compare overflowed values, except for overflow 1306 infinities. */ 1307 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2)) 1308 { 1309 if (strict_overflow_p != NULL) 1310 *strict_overflow_p = true; 1311 if (is_negative_overflow_infinity (val1)) 1312 return is_negative_overflow_infinity (val2) ? 0 : -1; 1313 else if (is_negative_overflow_infinity (val2)) 1314 return 1; 1315 else if (is_positive_overflow_infinity (val1)) 1316 return is_positive_overflow_infinity (val2) ? 0 : 1; 1317 else if (is_positive_overflow_infinity (val2)) 1318 return -1; 1319 return -2; 1320 } 1321 1322 return tree_int_cst_compare (val1, val2); 1323 } 1324 else 1325 { 1326 tree t; 1327 1328 /* First see if VAL1 and VAL2 are not the same. */ 1329 if (val1 == val2 || operand_equal_p (val1, val2, 0)) 1330 return 0; 1331 1332 /* If VAL1 is a lower address than VAL2, return -1. */ 1333 if (operand_less_p (val1, val2) == 1) 1334 return -1; 1335 1336 /* If VAL1 is a higher address than VAL2, return +1. */ 1337 if (operand_less_p (val2, val1) == 1) 1338 return 1; 1339 1340 /* If VAL1 is different than VAL2, return +2. 1341 For integer constants we either have already returned -1 or 1 1342 or they are equivalent. We still might succeed in proving 1343 something about non-trivial operands. */ 1344 if (TREE_CODE (val1) != INTEGER_CST 1345 || TREE_CODE (val2) != INTEGER_CST) 1346 { 1347 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2); 1348 if (t && integer_onep (t)) 1349 return 2; 1350 } 1351 1352 return -2; 1353 } 1354 } 1355 1356 /* Compare values like compare_values_warnv, but treat comparisons of 1357 nonconstants which rely on undefined overflow as incomparable. */ 1358 1359 static int 1360 compare_values (tree val1, tree val2) 1361 { 1362 bool sop; 1363 int ret; 1364 1365 sop = false; 1366 ret = compare_values_warnv (val1, val2, &sop); 1367 if (sop 1368 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))) 1369 ret = -2; 1370 return ret; 1371 } 1372 1373 1374 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX, 1375 0 if VAL is not inside [MIN, MAX], 1376 -2 if we cannot tell either way. 1377 1378 Benchmark compile/20001226-1.c compilation time after changing this 1379 function. */ 1380 1381 static inline int 1382 value_inside_range (tree val, tree min, tree max) 1383 { 1384 int cmp1, cmp2; 1385 1386 cmp1 = operand_less_p (val, min); 1387 if (cmp1 == -2) 1388 return -2; 1389 if (cmp1 == 1) 1390 return 0; 1391 1392 cmp2 = operand_less_p (max, val); 1393 if (cmp2 == -2) 1394 return -2; 1395 1396 return !cmp2; 1397 } 1398 1399 1400 /* Return true if value ranges VR0 and VR1 have a non-empty 1401 intersection. 1402 1403 Benchmark compile/20001226-1.c compilation time after changing this 1404 function. 1405 */ 1406 1407 static inline bool 1408 value_ranges_intersect_p (value_range *vr0, value_range *vr1) 1409 { 1410 /* The value ranges do not intersect if the maximum of the first range is 1411 less than the minimum of the second range or vice versa. 1412 When those relations are unknown, we can't do any better. */ 1413 if (operand_less_p (vr0->max, vr1->min) != 0) 1414 return false; 1415 if (operand_less_p (vr1->max, vr0->min) != 0) 1416 return false; 1417 return true; 1418 } 1419 1420 1421 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not 1422 include the value zero, -2 if we cannot tell. */ 1423 1424 static inline int 1425 range_includes_zero_p (tree min, tree max) 1426 { 1427 tree zero = build_int_cst (TREE_TYPE (min), 0); 1428 return value_inside_range (zero, min, max); 1429 } 1430 1431 /* Return true if *VR is know to only contain nonnegative values. */ 1432 1433 static inline bool 1434 value_range_nonnegative_p (value_range *vr) 1435 { 1436 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range 1437 which would return a useful value should be encoded as a 1438 VR_RANGE. */ 1439 if (vr->type == VR_RANGE) 1440 { 1441 int result = compare_values (vr->min, integer_zero_node); 1442 return (result == 0 || result == 1); 1443 } 1444 1445 return false; 1446 } 1447 1448 /* If *VR has a value rante that is a single constant value return that, 1449 otherwise return NULL_TREE. */ 1450 1451 static tree 1452 value_range_constant_singleton (value_range *vr) 1453 { 1454 if (vr->type == VR_RANGE 1455 && vrp_operand_equal_p (vr->min, vr->max) 1456 && is_gimple_min_invariant (vr->min)) 1457 return vr->min; 1458 1459 return NULL_TREE; 1460 } 1461 1462 /* If OP has a value range with a single constant value return that, 1463 otherwise return NULL_TREE. This returns OP itself if OP is a 1464 constant. */ 1465 1466 static tree 1467 op_with_constant_singleton_value_range (tree op) 1468 { 1469 if (is_gimple_min_invariant (op)) 1470 return op; 1471 1472 if (TREE_CODE (op) != SSA_NAME) 1473 return NULL_TREE; 1474 1475 return value_range_constant_singleton (get_value_range (op)); 1476 } 1477 1478 /* Return true if op is in a boolean [0, 1] value-range. */ 1479 1480 static bool 1481 op_with_boolean_value_range_p (tree op) 1482 { 1483 value_range *vr; 1484 1485 if (TYPE_PRECISION (TREE_TYPE (op)) == 1) 1486 return true; 1487 1488 if (integer_zerop (op) 1489 || integer_onep (op)) 1490 return true; 1491 1492 if (TREE_CODE (op) != SSA_NAME) 1493 return false; 1494 1495 vr = get_value_range (op); 1496 return (vr->type == VR_RANGE 1497 && integer_zerop (vr->min) 1498 && integer_onep (vr->max)); 1499 } 1500 1501 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is 1502 true and store it in *VR_P. */ 1503 1504 static void 1505 extract_range_for_var_from_comparison_expr (tree var, enum tree_code cond_code, 1506 tree op, tree limit, 1507 value_range *vr_p) 1508 { 1509 tree min, max, type; 1510 value_range *limit_vr; 1511 limit = avoid_overflow_infinity (limit); 1512 type = TREE_TYPE (var); 1513 gcc_assert (limit != var); 1514 1515 /* For pointer arithmetic, we only keep track of pointer equality 1516 and inequality. */ 1517 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR) 1518 { 1519 set_value_range_to_varying (vr_p); 1520 return; 1521 } 1522 1523 /* If LIMIT is another SSA name and LIMIT has a range of its own, 1524 try to use LIMIT's range to avoid creating symbolic ranges 1525 unnecessarily. */ 1526 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL; 1527 1528 /* LIMIT's range is only interesting if it has any useful information. */ 1529 if (! limit_vr 1530 || limit_vr->type == VR_UNDEFINED 1531 || limit_vr->type == VR_VARYING 1532 || (symbolic_range_p (limit_vr) 1533 && ! (limit_vr->type == VR_RANGE 1534 && (limit_vr->min == limit_vr->max 1535 || operand_equal_p (limit_vr->min, limit_vr->max, 0))))) 1536 limit_vr = NULL; 1537 1538 /* Initially, the new range has the same set of equivalences of 1539 VAR's range. This will be revised before returning the final 1540 value. Since assertions may be chained via mutually exclusive 1541 predicates, we will need to trim the set of equivalences before 1542 we are done. */ 1543 gcc_assert (vr_p->equiv == NULL); 1544 add_equivalence (&vr_p->equiv, var); 1545 1546 /* Extract a new range based on the asserted comparison for VAR and 1547 LIMIT's value range. Notice that if LIMIT has an anti-range, we 1548 will only use it for equality comparisons (EQ_EXPR). For any 1549 other kind of assertion, we cannot derive a range from LIMIT's 1550 anti-range that can be used to describe the new range. For 1551 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10], 1552 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is 1553 no single range for x_2 that could describe LE_EXPR, so we might 1554 as well build the range [b_4, +INF] for it. 1555 One special case we handle is extracting a range from a 1556 range test encoded as (unsigned)var + CST <= limit. */ 1557 if (TREE_CODE (op) == NOP_EXPR 1558 || TREE_CODE (op) == PLUS_EXPR) 1559 { 1560 if (TREE_CODE (op) == PLUS_EXPR) 1561 { 1562 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)), 1563 TREE_OPERAND (op, 1)); 1564 max = int_const_binop (PLUS_EXPR, limit, min); 1565 op = TREE_OPERAND (op, 0); 1566 } 1567 else 1568 { 1569 min = build_int_cst (TREE_TYPE (var), 0); 1570 max = limit; 1571 } 1572 1573 /* Make sure to not set TREE_OVERFLOW on the final type 1574 conversion. We are willingly interpreting large positive 1575 unsigned values as negative signed values here. */ 1576 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false); 1577 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false); 1578 1579 /* We can transform a max, min range to an anti-range or 1580 vice-versa. Use set_and_canonicalize_value_range which does 1581 this for us. */ 1582 if (cond_code == LE_EXPR) 1583 set_and_canonicalize_value_range (vr_p, VR_RANGE, 1584 min, max, vr_p->equiv); 1585 else if (cond_code == GT_EXPR) 1586 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE, 1587 min, max, vr_p->equiv); 1588 else 1589 gcc_unreachable (); 1590 } 1591 else if (cond_code == EQ_EXPR) 1592 { 1593 enum value_range_type range_type; 1594 1595 if (limit_vr) 1596 { 1597 range_type = limit_vr->type; 1598 min = limit_vr->min; 1599 max = limit_vr->max; 1600 } 1601 else 1602 { 1603 range_type = VR_RANGE; 1604 min = limit; 1605 max = limit; 1606 } 1607 1608 set_value_range (vr_p, range_type, min, max, vr_p->equiv); 1609 1610 /* When asserting the equality VAR == LIMIT and LIMIT is another 1611 SSA name, the new range will also inherit the equivalence set 1612 from LIMIT. */ 1613 if (TREE_CODE (limit) == SSA_NAME) 1614 add_equivalence (&vr_p->equiv, limit); 1615 } 1616 else if (cond_code == NE_EXPR) 1617 { 1618 /* As described above, when LIMIT's range is an anti-range and 1619 this assertion is an inequality (NE_EXPR), then we cannot 1620 derive anything from the anti-range. For instance, if 1621 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does 1622 not imply that VAR's range is [0, 0]. So, in the case of 1623 anti-ranges, we just assert the inequality using LIMIT and 1624 not its anti-range. 1625 1626 If LIMIT_VR is a range, we can only use it to build a new 1627 anti-range if LIMIT_VR is a single-valued range. For 1628 instance, if LIMIT_VR is [0, 1], the predicate 1629 VAR != [0, 1] does not mean that VAR's range is ~[0, 1]. 1630 Rather, it means that for value 0 VAR should be ~[0, 0] 1631 and for value 1, VAR should be ~[1, 1]. We cannot 1632 represent these ranges. 1633 1634 The only situation in which we can build a valid 1635 anti-range is when LIMIT_VR is a single-valued range 1636 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case, 1637 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */ 1638 if (limit_vr 1639 && limit_vr->type == VR_RANGE 1640 && compare_values (limit_vr->min, limit_vr->max) == 0) 1641 { 1642 min = limit_vr->min; 1643 max = limit_vr->max; 1644 } 1645 else 1646 { 1647 /* In any other case, we cannot use LIMIT's range to build a 1648 valid anti-range. */ 1649 min = max = limit; 1650 } 1651 1652 /* If MIN and MAX cover the whole range for their type, then 1653 just use the original LIMIT. */ 1654 if (INTEGRAL_TYPE_P (type) 1655 && vrp_val_is_min (min) 1656 && vrp_val_is_max (max)) 1657 min = max = limit; 1658 1659 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE, 1660 min, max, vr_p->equiv); 1661 } 1662 else if (cond_code == LE_EXPR || cond_code == LT_EXPR) 1663 { 1664 min = TYPE_MIN_VALUE (type); 1665 1666 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE) 1667 max = limit; 1668 else 1669 { 1670 /* If LIMIT_VR is of the form [N1, N2], we need to build the 1671 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for 1672 LT_EXPR. */ 1673 max = limit_vr->max; 1674 } 1675 1676 /* If the maximum value forces us to be out of bounds, simply punt. 1677 It would be pointless to try and do anything more since this 1678 all should be optimized away above us. */ 1679 if ((cond_code == LT_EXPR 1680 && compare_values (max, min) == 0) 1681 || is_overflow_infinity (max)) 1682 set_value_range_to_varying (vr_p); 1683 else 1684 { 1685 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */ 1686 if (cond_code == LT_EXPR) 1687 { 1688 if (TYPE_PRECISION (TREE_TYPE (max)) == 1 1689 && !TYPE_UNSIGNED (TREE_TYPE (max))) 1690 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max, 1691 build_int_cst (TREE_TYPE (max), -1)); 1692 else 1693 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, 1694 build_int_cst (TREE_TYPE (max), 1)); 1695 if (EXPR_P (max)) 1696 TREE_NO_WARNING (max) = 1; 1697 } 1698 1699 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv); 1700 } 1701 } 1702 else if (cond_code == GE_EXPR || cond_code == GT_EXPR) 1703 { 1704 max = TYPE_MAX_VALUE (type); 1705 1706 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE) 1707 min = limit; 1708 else 1709 { 1710 /* If LIMIT_VR is of the form [N1, N2], we need to build the 1711 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for 1712 GT_EXPR. */ 1713 min = limit_vr->min; 1714 } 1715 1716 /* If the minimum value forces us to be out of bounds, simply punt. 1717 It would be pointless to try and do anything more since this 1718 all should be optimized away above us. */ 1719 if ((cond_code == GT_EXPR 1720 && compare_values (min, max) == 0) 1721 || is_overflow_infinity (min)) 1722 set_value_range_to_varying (vr_p); 1723 else 1724 { 1725 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */ 1726 if (cond_code == GT_EXPR) 1727 { 1728 if (TYPE_PRECISION (TREE_TYPE (min)) == 1 1729 && !TYPE_UNSIGNED (TREE_TYPE (min))) 1730 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min, 1731 build_int_cst (TREE_TYPE (min), -1)); 1732 else 1733 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min, 1734 build_int_cst (TREE_TYPE (min), 1)); 1735 if (EXPR_P (min)) 1736 TREE_NO_WARNING (min) = 1; 1737 } 1738 1739 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv); 1740 } 1741 } 1742 else 1743 gcc_unreachable (); 1744 1745 /* Finally intersect the new range with what we already know about var. */ 1746 vrp_intersect_ranges (vr_p, get_value_range (var)); 1747 } 1748 1749 /* Extract value range information from an ASSERT_EXPR EXPR and store 1750 it in *VR_P. */ 1751 1752 static void 1753 extract_range_from_assert (value_range *vr_p, tree expr) 1754 { 1755 tree var = ASSERT_EXPR_VAR (expr); 1756 tree cond = ASSERT_EXPR_COND (expr); 1757 tree limit, op; 1758 enum tree_code cond_code; 1759 gcc_assert (COMPARISON_CLASS_P (cond)); 1760 1761 /* Find VAR in the ASSERT_EXPR conditional. */ 1762 if (var == TREE_OPERAND (cond, 0) 1763 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR 1764 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR) 1765 { 1766 /* If the predicate is of the form VAR COMP LIMIT, then we just 1767 take LIMIT from the RHS and use the same comparison code. */ 1768 cond_code = TREE_CODE (cond); 1769 limit = TREE_OPERAND (cond, 1); 1770 op = TREE_OPERAND (cond, 0); 1771 } 1772 else 1773 { 1774 /* If the predicate is of the form LIMIT COMP VAR, then we need 1775 to flip around the comparison code to create the proper range 1776 for VAR. */ 1777 cond_code = swap_tree_comparison (TREE_CODE (cond)); 1778 limit = TREE_OPERAND (cond, 0); 1779 op = TREE_OPERAND (cond, 1); 1780 } 1781 extract_range_for_var_from_comparison_expr (var, cond_code, op, 1782 limit, vr_p); 1783 } 1784 1785 /* Extract range information from SSA name VAR and store it in VR. If 1786 VAR has an interesting range, use it. Otherwise, create the 1787 range [VAR, VAR] and return it. This is useful in situations where 1788 we may have conditionals testing values of VARYING names. For 1789 instance, 1790 1791 x_3 = y_5; 1792 if (x_3 > y_5) 1793 ... 1794 1795 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is 1796 always false. */ 1797 1798 static void 1799 extract_range_from_ssa_name (value_range *vr, tree var) 1800 { 1801 value_range *var_vr = get_value_range (var); 1802 1803 if (var_vr->type != VR_VARYING) 1804 copy_value_range (vr, var_vr); 1805 else 1806 set_value_range (vr, VR_RANGE, var, var, NULL); 1807 1808 add_equivalence (&vr->equiv, var); 1809 } 1810 1811 1812 /* Wrapper around int_const_binop. If the operation overflows and we 1813 are not using wrapping arithmetic, then adjust the result to be 1814 -INF or +INF depending on CODE, VAL1 and VAL2. This can return 1815 NULL_TREE if we need to use an overflow infinity representation but 1816 the type does not support it. */ 1817 1818 static tree 1819 vrp_int_const_binop (enum tree_code code, tree val1, tree val2) 1820 { 1821 tree res; 1822 1823 res = int_const_binop (code, val1, val2); 1824 1825 /* If we are using unsigned arithmetic, operate symbolically 1826 on -INF and +INF as int_const_binop only handles signed overflow. */ 1827 if (TYPE_UNSIGNED (TREE_TYPE (val1))) 1828 { 1829 int checkz = compare_values (res, val1); 1830 bool overflow = false; 1831 1832 /* Ensure that res = val1 [+*] val2 >= val1 1833 or that res = val1 - val2 <= val1. */ 1834 if ((code == PLUS_EXPR 1835 && !(checkz == 1 || checkz == 0)) 1836 || (code == MINUS_EXPR 1837 && !(checkz == 0 || checkz == -1))) 1838 { 1839 overflow = true; 1840 } 1841 /* Checking for multiplication overflow is done by dividing the 1842 output of the multiplication by the first input of the 1843 multiplication. If the result of that division operation is 1844 not equal to the second input of the multiplication, then the 1845 multiplication overflowed. */ 1846 else if (code == MULT_EXPR && !integer_zerop (val1)) 1847 { 1848 tree tmp = int_const_binop (TRUNC_DIV_EXPR, 1849 res, 1850 val1); 1851 int check = compare_values (tmp, val2); 1852 1853 if (check != 0) 1854 overflow = true; 1855 } 1856 1857 if (overflow) 1858 { 1859 res = copy_node (res); 1860 TREE_OVERFLOW (res) = 1; 1861 } 1862 1863 } 1864 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1))) 1865 /* If the singed operation wraps then int_const_binop has done 1866 everything we want. */ 1867 ; 1868 /* Signed division of -1/0 overflows and by the time it gets here 1869 returns NULL_TREE. */ 1870 else if (!res) 1871 return NULL_TREE; 1872 else if ((TREE_OVERFLOW (res) 1873 && !TREE_OVERFLOW (val1) 1874 && !TREE_OVERFLOW (val2)) 1875 || is_overflow_infinity (val1) 1876 || is_overflow_infinity (val2)) 1877 { 1878 /* If the operation overflowed but neither VAL1 nor VAL2 are 1879 overflown, return -INF or +INF depending on the operation 1880 and the combination of signs of the operands. */ 1881 int sgn1 = tree_int_cst_sgn (val1); 1882 int sgn2 = tree_int_cst_sgn (val2); 1883 1884 if (needs_overflow_infinity (TREE_TYPE (res)) 1885 && !supports_overflow_infinity (TREE_TYPE (res))) 1886 return NULL_TREE; 1887 1888 /* We have to punt on adding infinities of different signs, 1889 since we can't tell what the sign of the result should be. 1890 Likewise for subtracting infinities of the same sign. */ 1891 if (((code == PLUS_EXPR && sgn1 != sgn2) 1892 || (code == MINUS_EXPR && sgn1 == sgn2)) 1893 && is_overflow_infinity (val1) 1894 && is_overflow_infinity (val2)) 1895 return NULL_TREE; 1896 1897 /* Don't try to handle division or shifting of infinities. */ 1898 if ((code == TRUNC_DIV_EXPR 1899 || code == FLOOR_DIV_EXPR 1900 || code == CEIL_DIV_EXPR 1901 || code == EXACT_DIV_EXPR 1902 || code == ROUND_DIV_EXPR 1903 || code == RSHIFT_EXPR) 1904 && (is_overflow_infinity (val1) 1905 || is_overflow_infinity (val2))) 1906 return NULL_TREE; 1907 1908 /* Notice that we only need to handle the restricted set of 1909 operations handled by extract_range_from_binary_expr. 1910 Among them, only multiplication, addition and subtraction 1911 can yield overflow without overflown operands because we 1912 are working with integral types only... except in the 1913 case VAL1 = -INF and VAL2 = -1 which overflows to +INF 1914 for division too. */ 1915 1916 /* For multiplication, the sign of the overflow is given 1917 by the comparison of the signs of the operands. */ 1918 if ((code == MULT_EXPR && sgn1 == sgn2) 1919 /* For addition, the operands must be of the same sign 1920 to yield an overflow. Its sign is therefore that 1921 of one of the operands, for example the first. For 1922 infinite operands X + -INF is negative, not positive. */ 1923 || (code == PLUS_EXPR 1924 && (sgn1 >= 0 1925 ? !is_negative_overflow_infinity (val2) 1926 : is_positive_overflow_infinity (val2))) 1927 /* For subtraction, non-infinite operands must be of 1928 different signs to yield an overflow. Its sign is 1929 therefore that of the first operand or the opposite of 1930 that of the second operand. A first operand of 0 counts 1931 as positive here, for the corner case 0 - (-INF), which 1932 overflows, but must yield +INF. For infinite operands 0 1933 - INF is negative, not positive. */ 1934 || (code == MINUS_EXPR 1935 && (sgn1 >= 0 1936 ? !is_positive_overflow_infinity (val2) 1937 : is_negative_overflow_infinity (val2))) 1938 /* We only get in here with positive shift count, so the 1939 overflow direction is the same as the sign of val1. 1940 Actually rshift does not overflow at all, but we only 1941 handle the case of shifting overflowed -INF and +INF. */ 1942 || (code == RSHIFT_EXPR 1943 && sgn1 >= 0) 1944 /* For division, the only case is -INF / -1 = +INF. */ 1945 || code == TRUNC_DIV_EXPR 1946 || code == FLOOR_DIV_EXPR 1947 || code == CEIL_DIV_EXPR 1948 || code == EXACT_DIV_EXPR 1949 || code == ROUND_DIV_EXPR) 1950 return (needs_overflow_infinity (TREE_TYPE (res)) 1951 ? positive_overflow_infinity (TREE_TYPE (res)) 1952 : TYPE_MAX_VALUE (TREE_TYPE (res))); 1953 else 1954 return (needs_overflow_infinity (TREE_TYPE (res)) 1955 ? negative_overflow_infinity (TREE_TYPE (res)) 1956 : TYPE_MIN_VALUE (TREE_TYPE (res))); 1957 } 1958 1959 return res; 1960 } 1961 1962 1963 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO 1964 bitmask if some bit is unset, it means for all numbers in the range 1965 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO 1966 bitmask if some bit is set, it means for all numbers in the range 1967 the bit is 1, otherwise it might be 0 or 1. */ 1968 1969 static bool 1970 zero_nonzero_bits_from_vr (const tree expr_type, 1971 value_range *vr, 1972 wide_int *may_be_nonzero, 1973 wide_int *must_be_nonzero) 1974 { 1975 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type)); 1976 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type)); 1977 if (!range_int_cst_p (vr) 1978 || is_overflow_infinity (vr->min) 1979 || is_overflow_infinity (vr->max)) 1980 return false; 1981 1982 if (range_int_cst_singleton_p (vr)) 1983 { 1984 *may_be_nonzero = vr->min; 1985 *must_be_nonzero = *may_be_nonzero; 1986 } 1987 else if (tree_int_cst_sgn (vr->min) >= 0 1988 || tree_int_cst_sgn (vr->max) < 0) 1989 { 1990 wide_int xor_mask = wi::bit_xor (vr->min, vr->max); 1991 *may_be_nonzero = wi::bit_or (vr->min, vr->max); 1992 *must_be_nonzero = wi::bit_and (vr->min, vr->max); 1993 if (xor_mask != 0) 1994 { 1995 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false, 1996 may_be_nonzero->get_precision ()); 1997 *may_be_nonzero = *may_be_nonzero | mask; 1998 *must_be_nonzero = must_be_nonzero->and_not (mask); 1999 } 2000 } 2001 2002 return true; 2003 } 2004 2005 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR 2006 so that *VR0 U *VR1 == *AR. Returns true if that is possible, 2007 false otherwise. If *AR can be represented with a single range 2008 *VR1 will be VR_UNDEFINED. */ 2009 2010 static bool 2011 ranges_from_anti_range (value_range *ar, 2012 value_range *vr0, value_range *vr1) 2013 { 2014 tree type = TREE_TYPE (ar->min); 2015 2016 vr0->type = VR_UNDEFINED; 2017 vr1->type = VR_UNDEFINED; 2018 2019 if (ar->type != VR_ANTI_RANGE 2020 || TREE_CODE (ar->min) != INTEGER_CST 2021 || TREE_CODE (ar->max) != INTEGER_CST 2022 || !vrp_val_min (type) 2023 || !vrp_val_max (type)) 2024 return false; 2025 2026 if (!vrp_val_is_min (ar->min)) 2027 { 2028 vr0->type = VR_RANGE; 2029 vr0->min = vrp_val_min (type); 2030 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1)); 2031 } 2032 if (!vrp_val_is_max (ar->max)) 2033 { 2034 vr1->type = VR_RANGE; 2035 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1)); 2036 vr1->max = vrp_val_max (type); 2037 } 2038 if (vr0->type == VR_UNDEFINED) 2039 { 2040 *vr0 = *vr1; 2041 vr1->type = VR_UNDEFINED; 2042 } 2043 2044 return vr0->type != VR_UNDEFINED; 2045 } 2046 2047 /* Helper to extract a value-range *VR for a multiplicative operation 2048 *VR0 CODE *VR1. */ 2049 2050 static void 2051 extract_range_from_multiplicative_op_1 (value_range *vr, 2052 enum tree_code code, 2053 value_range *vr0, value_range *vr1) 2054 { 2055 enum value_range_type type; 2056 tree val[4]; 2057 size_t i; 2058 tree min, max; 2059 bool sop; 2060 int cmp; 2061 2062 /* Multiplications, divisions and shifts are a bit tricky to handle, 2063 depending on the mix of signs we have in the two ranges, we 2064 need to operate on different values to get the minimum and 2065 maximum values for the new range. One approach is to figure 2066 out all the variations of range combinations and do the 2067 operations. 2068 2069 However, this involves several calls to compare_values and it 2070 is pretty convoluted. It's simpler to do the 4 operations 2071 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP 2072 MAX1) and then figure the smallest and largest values to form 2073 the new range. */ 2074 gcc_assert (code == MULT_EXPR 2075 || code == TRUNC_DIV_EXPR 2076 || code == FLOOR_DIV_EXPR 2077 || code == CEIL_DIV_EXPR 2078 || code == EXACT_DIV_EXPR 2079 || code == ROUND_DIV_EXPR 2080 || code == RSHIFT_EXPR 2081 || code == LSHIFT_EXPR); 2082 gcc_assert ((vr0->type == VR_RANGE 2083 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE)) 2084 && vr0->type == vr1->type); 2085 2086 type = vr0->type; 2087 2088 /* Compute the 4 cross operations. */ 2089 sop = false; 2090 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min); 2091 if (val[0] == NULL_TREE) 2092 sop = true; 2093 2094 if (vr1->max == vr1->min) 2095 val[1] = NULL_TREE; 2096 else 2097 { 2098 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max); 2099 if (val[1] == NULL_TREE) 2100 sop = true; 2101 } 2102 2103 if (vr0->max == vr0->min) 2104 val[2] = NULL_TREE; 2105 else 2106 { 2107 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min); 2108 if (val[2] == NULL_TREE) 2109 sop = true; 2110 } 2111 2112 if (vr0->min == vr0->max || vr1->min == vr1->max) 2113 val[3] = NULL_TREE; 2114 else 2115 { 2116 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max); 2117 if (val[3] == NULL_TREE) 2118 sop = true; 2119 } 2120 2121 if (sop) 2122 { 2123 set_value_range_to_varying (vr); 2124 return; 2125 } 2126 2127 /* Set MIN to the minimum of VAL[i] and MAX to the maximum 2128 of VAL[i]. */ 2129 min = val[0]; 2130 max = val[0]; 2131 for (i = 1; i < 4; i++) 2132 { 2133 if (!is_gimple_min_invariant (min) 2134 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min)) 2135 || !is_gimple_min_invariant (max) 2136 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max))) 2137 break; 2138 2139 if (val[i]) 2140 { 2141 if (!is_gimple_min_invariant (val[i]) 2142 || (TREE_OVERFLOW (val[i]) 2143 && !is_overflow_infinity (val[i]))) 2144 { 2145 /* If we found an overflowed value, set MIN and MAX 2146 to it so that we set the resulting range to 2147 VARYING. */ 2148 min = max = val[i]; 2149 break; 2150 } 2151 2152 if (compare_values (val[i], min) == -1) 2153 min = val[i]; 2154 2155 if (compare_values (val[i], max) == 1) 2156 max = val[i]; 2157 } 2158 } 2159 2160 /* If either MIN or MAX overflowed, then set the resulting range to 2161 VARYING. But we do accept an overflow infinity 2162 representation. */ 2163 if (min == NULL_TREE 2164 || !is_gimple_min_invariant (min) 2165 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min)) 2166 || max == NULL_TREE 2167 || !is_gimple_min_invariant (max) 2168 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max))) 2169 { 2170 set_value_range_to_varying (vr); 2171 return; 2172 } 2173 2174 /* We punt if: 2175 1) [-INF, +INF] 2176 2) [-INF, +-INF(OVF)] 2177 3) [+-INF(OVF), +INF] 2178 4) [+-INF(OVF), +-INF(OVF)] 2179 We learn nothing when we have INF and INF(OVF) on both sides. 2180 Note that we do accept [-INF, -INF] and [+INF, +INF] without 2181 overflow. */ 2182 if ((vrp_val_is_min (min) || is_overflow_infinity (min)) 2183 && (vrp_val_is_max (max) || is_overflow_infinity (max))) 2184 { 2185 set_value_range_to_varying (vr); 2186 return; 2187 } 2188 2189 cmp = compare_values (min, max); 2190 if (cmp == -2 || cmp == 1) 2191 { 2192 /* If the new range has its limits swapped around (MIN > MAX), 2193 then the operation caused one of them to wrap around, mark 2194 the new range VARYING. */ 2195 set_value_range_to_varying (vr); 2196 } 2197 else 2198 set_value_range (vr, type, min, max, NULL); 2199 } 2200 2201 /* Extract range information from a binary operation CODE based on 2202 the ranges of each of its operands *VR0 and *VR1 with resulting 2203 type EXPR_TYPE. The resulting range is stored in *VR. */ 2204 2205 static void 2206 extract_range_from_binary_expr_1 (value_range *vr, 2207 enum tree_code code, tree expr_type, 2208 value_range *vr0_, value_range *vr1_) 2209 { 2210 value_range vr0 = *vr0_, vr1 = *vr1_; 2211 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER; 2212 enum value_range_type type; 2213 tree min = NULL_TREE, max = NULL_TREE; 2214 int cmp; 2215 2216 if (!INTEGRAL_TYPE_P (expr_type) 2217 && !POINTER_TYPE_P (expr_type)) 2218 { 2219 set_value_range_to_varying (vr); 2220 return; 2221 } 2222 2223 /* Not all binary expressions can be applied to ranges in a 2224 meaningful way. Handle only arithmetic operations. */ 2225 if (code != PLUS_EXPR 2226 && code != MINUS_EXPR 2227 && code != POINTER_PLUS_EXPR 2228 && code != MULT_EXPR 2229 && code != TRUNC_DIV_EXPR 2230 && code != FLOOR_DIV_EXPR 2231 && code != CEIL_DIV_EXPR 2232 && code != EXACT_DIV_EXPR 2233 && code != ROUND_DIV_EXPR 2234 && code != TRUNC_MOD_EXPR 2235 && code != RSHIFT_EXPR 2236 && code != LSHIFT_EXPR 2237 && code != MIN_EXPR 2238 && code != MAX_EXPR 2239 && code != BIT_AND_EXPR 2240 && code != BIT_IOR_EXPR 2241 && code != BIT_XOR_EXPR) 2242 { 2243 set_value_range_to_varying (vr); 2244 return; 2245 } 2246 2247 /* If both ranges are UNDEFINED, so is the result. */ 2248 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED) 2249 { 2250 set_value_range_to_undefined (vr); 2251 return; 2252 } 2253 /* If one of the ranges is UNDEFINED drop it to VARYING for the following 2254 code. At some point we may want to special-case operations that 2255 have UNDEFINED result for all or some value-ranges of the not UNDEFINED 2256 operand. */ 2257 else if (vr0.type == VR_UNDEFINED) 2258 set_value_range_to_varying (&vr0); 2259 else if (vr1.type == VR_UNDEFINED) 2260 set_value_range_to_varying (&vr1); 2261 2262 /* We get imprecise results from ranges_from_anti_range when 2263 code is EXACT_DIV_EXPR. We could mask out bits in the resulting 2264 range, but then we also need to hack up vrp_meet. It's just 2265 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */ 2266 if (code == EXACT_DIV_EXPR 2267 && vr0.type == VR_ANTI_RANGE 2268 && vr0.min == vr0.max 2269 && integer_zerop (vr0.min)) 2270 { 2271 set_value_range_to_nonnull (vr, expr_type); 2272 return; 2273 } 2274 2275 /* Now canonicalize anti-ranges to ranges when they are not symbolic 2276 and express ~[] op X as ([]' op X) U ([]'' op X). */ 2277 if (vr0.type == VR_ANTI_RANGE 2278 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1)) 2279 { 2280 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_); 2281 if (vrtem1.type != VR_UNDEFINED) 2282 { 2283 value_range vrres = VR_INITIALIZER; 2284 extract_range_from_binary_expr_1 (&vrres, code, expr_type, 2285 &vrtem1, vr1_); 2286 vrp_meet (vr, &vrres); 2287 } 2288 return; 2289 } 2290 /* Likewise for X op ~[]. */ 2291 if (vr1.type == VR_ANTI_RANGE 2292 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1)) 2293 { 2294 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0); 2295 if (vrtem1.type != VR_UNDEFINED) 2296 { 2297 value_range vrres = VR_INITIALIZER; 2298 extract_range_from_binary_expr_1 (&vrres, code, expr_type, 2299 vr0_, &vrtem1); 2300 vrp_meet (vr, &vrres); 2301 } 2302 return; 2303 } 2304 2305 /* The type of the resulting value range defaults to VR0.TYPE. */ 2306 type = vr0.type; 2307 2308 /* Refuse to operate on VARYING ranges, ranges of different kinds 2309 and symbolic ranges. As an exception, we allow BIT_{AND,IOR} 2310 because we may be able to derive a useful range even if one of 2311 the operands is VR_VARYING or symbolic range. Similarly for 2312 divisions, MIN/MAX and PLUS/MINUS. 2313 2314 TODO, we may be able to derive anti-ranges in some cases. */ 2315 if (code != BIT_AND_EXPR 2316 && code != BIT_IOR_EXPR 2317 && code != TRUNC_DIV_EXPR 2318 && code != FLOOR_DIV_EXPR 2319 && code != CEIL_DIV_EXPR 2320 && code != EXACT_DIV_EXPR 2321 && code != ROUND_DIV_EXPR 2322 && code != TRUNC_MOD_EXPR 2323 && code != MIN_EXPR 2324 && code != MAX_EXPR 2325 && code != PLUS_EXPR 2326 && code != MINUS_EXPR 2327 && code != RSHIFT_EXPR 2328 && (vr0.type == VR_VARYING 2329 || vr1.type == VR_VARYING 2330 || vr0.type != vr1.type 2331 || symbolic_range_p (&vr0) 2332 || symbolic_range_p (&vr1))) 2333 { 2334 set_value_range_to_varying (vr); 2335 return; 2336 } 2337 2338 /* Now evaluate the expression to determine the new range. */ 2339 if (POINTER_TYPE_P (expr_type)) 2340 { 2341 if (code == MIN_EXPR || code == MAX_EXPR) 2342 { 2343 /* For MIN/MAX expressions with pointers, we only care about 2344 nullness, if both are non null, then the result is nonnull. 2345 If both are null, then the result is null. Otherwise they 2346 are varying. */ 2347 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1)) 2348 set_value_range_to_nonnull (vr, expr_type); 2349 else if (range_is_null (&vr0) && range_is_null (&vr1)) 2350 set_value_range_to_null (vr, expr_type); 2351 else 2352 set_value_range_to_varying (vr); 2353 } 2354 else if (code == POINTER_PLUS_EXPR) 2355 { 2356 /* For pointer types, we are really only interested in asserting 2357 whether the expression evaluates to non-NULL. */ 2358 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1)) 2359 set_value_range_to_nonnull (vr, expr_type); 2360 else if (range_is_null (&vr0) && range_is_null (&vr1)) 2361 set_value_range_to_null (vr, expr_type); 2362 else 2363 set_value_range_to_varying (vr); 2364 } 2365 else if (code == BIT_AND_EXPR) 2366 { 2367 /* For pointer types, we are really only interested in asserting 2368 whether the expression evaluates to non-NULL. */ 2369 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1)) 2370 set_value_range_to_nonnull (vr, expr_type); 2371 else if (range_is_null (&vr0) || range_is_null (&vr1)) 2372 set_value_range_to_null (vr, expr_type); 2373 else 2374 set_value_range_to_varying (vr); 2375 } 2376 else 2377 set_value_range_to_varying (vr); 2378 2379 return; 2380 } 2381 2382 /* For integer ranges, apply the operation to each end of the 2383 range and see what we end up with. */ 2384 if (code == PLUS_EXPR || code == MINUS_EXPR) 2385 { 2386 const bool minus_p = (code == MINUS_EXPR); 2387 tree min_op0 = vr0.min; 2388 tree min_op1 = minus_p ? vr1.max : vr1.min; 2389 tree max_op0 = vr0.max; 2390 tree max_op1 = minus_p ? vr1.min : vr1.max; 2391 tree sym_min_op0 = NULL_TREE; 2392 tree sym_min_op1 = NULL_TREE; 2393 tree sym_max_op0 = NULL_TREE; 2394 tree sym_max_op1 = NULL_TREE; 2395 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1; 2396 2397 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or 2398 single-symbolic ranges, try to compute the precise resulting range, 2399 but only if we know that this resulting range will also be constant 2400 or single-symbolic. */ 2401 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE 2402 && (TREE_CODE (min_op0) == INTEGER_CST 2403 || (sym_min_op0 2404 = get_single_symbol (min_op0, &neg_min_op0, &min_op0))) 2405 && (TREE_CODE (min_op1) == INTEGER_CST 2406 || (sym_min_op1 2407 = get_single_symbol (min_op1, &neg_min_op1, &min_op1))) 2408 && (!(sym_min_op0 && sym_min_op1) 2409 || (sym_min_op0 == sym_min_op1 2410 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1))) 2411 && (TREE_CODE (max_op0) == INTEGER_CST 2412 || (sym_max_op0 2413 = get_single_symbol (max_op0, &neg_max_op0, &max_op0))) 2414 && (TREE_CODE (max_op1) == INTEGER_CST 2415 || (sym_max_op1 2416 = get_single_symbol (max_op1, &neg_max_op1, &max_op1))) 2417 && (!(sym_max_op0 && sym_max_op1) 2418 || (sym_max_op0 == sym_max_op1 2419 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1)))) 2420 { 2421 const signop sgn = TYPE_SIGN (expr_type); 2422 const unsigned int prec = TYPE_PRECISION (expr_type); 2423 wide_int type_min, type_max, wmin, wmax; 2424 int min_ovf = 0; 2425 int max_ovf = 0; 2426 2427 /* Get the lower and upper bounds of the type. */ 2428 if (TYPE_OVERFLOW_WRAPS (expr_type)) 2429 { 2430 type_min = wi::min_value (prec, sgn); 2431 type_max = wi::max_value (prec, sgn); 2432 } 2433 else 2434 { 2435 type_min = vrp_val_min (expr_type); 2436 type_max = vrp_val_max (expr_type); 2437 } 2438 2439 /* Combine the lower bounds, if any. */ 2440 if (min_op0 && min_op1) 2441 { 2442 if (minus_p) 2443 { 2444 wmin = wi::sub (min_op0, min_op1); 2445 2446 /* Check for overflow. */ 2447 if (wi::cmp (0, min_op1, sgn) 2448 != wi::cmp (wmin, min_op0, sgn)) 2449 min_ovf = wi::cmp (min_op0, min_op1, sgn); 2450 } 2451 else 2452 { 2453 wmin = wi::add (min_op0, min_op1); 2454 2455 /* Check for overflow. */ 2456 if (wi::cmp (min_op1, 0, sgn) 2457 != wi::cmp (wmin, min_op0, sgn)) 2458 min_ovf = wi::cmp (min_op0, wmin, sgn); 2459 } 2460 } 2461 else if (min_op0) 2462 wmin = min_op0; 2463 else if (min_op1) 2464 { 2465 if (minus_p) 2466 { 2467 wmin = wi::neg (min_op1); 2468 2469 /* Check for overflow. */ 2470 if (sgn == SIGNED && wi::neg_p (min_op1) && wi::neg_p (wmin)) 2471 min_ovf = 1; 2472 else if (sgn == UNSIGNED && wi::ne_p (min_op1, 0)) 2473 min_ovf = -1; 2474 } 2475 else 2476 wmin = min_op1; 2477 } 2478 else 2479 wmin = wi::shwi (0, prec); 2480 2481 /* Combine the upper bounds, if any. */ 2482 if (max_op0 && max_op1) 2483 { 2484 if (minus_p) 2485 { 2486 wmax = wi::sub (max_op0, max_op1); 2487 2488 /* Check for overflow. */ 2489 if (wi::cmp (0, max_op1, sgn) 2490 != wi::cmp (wmax, max_op0, sgn)) 2491 max_ovf = wi::cmp (max_op0, max_op1, sgn); 2492 } 2493 else 2494 { 2495 wmax = wi::add (max_op0, max_op1); 2496 2497 if (wi::cmp (max_op1, 0, sgn) 2498 != wi::cmp (wmax, max_op0, sgn)) 2499 max_ovf = wi::cmp (max_op0, wmax, sgn); 2500 } 2501 } 2502 else if (max_op0) 2503 wmax = max_op0; 2504 else if (max_op1) 2505 { 2506 if (minus_p) 2507 { 2508 wmax = wi::neg (max_op1); 2509 2510 /* Check for overflow. */ 2511 if (sgn == SIGNED && wi::neg_p (max_op1) && wi::neg_p (wmax)) 2512 max_ovf = 1; 2513 else if (sgn == UNSIGNED && wi::ne_p (max_op1, 0)) 2514 max_ovf = -1; 2515 } 2516 else 2517 wmax = max_op1; 2518 } 2519 else 2520 wmax = wi::shwi (0, prec); 2521 2522 /* Check for type overflow. */ 2523 if (min_ovf == 0) 2524 { 2525 if (wi::cmp (wmin, type_min, sgn) == -1) 2526 min_ovf = -1; 2527 else if (wi::cmp (wmin, type_max, sgn) == 1) 2528 min_ovf = 1; 2529 } 2530 if (max_ovf == 0) 2531 { 2532 if (wi::cmp (wmax, type_min, sgn) == -1) 2533 max_ovf = -1; 2534 else if (wi::cmp (wmax, type_max, sgn) == 1) 2535 max_ovf = 1; 2536 } 2537 2538 /* If the resulting range will be symbolic, we need to eliminate any 2539 explicit or implicit overflow introduced in the above computation 2540 because compare_values could make an incorrect use of it. That's 2541 why we require one of the ranges to be a singleton. */ 2542 if ((sym_min_op0 != sym_min_op1 || sym_max_op0 != sym_max_op1) 2543 && (min_ovf || max_ovf 2544 || (min_op0 != max_op0 && min_op1 != max_op1))) 2545 { 2546 set_value_range_to_varying (vr); 2547 return; 2548 } 2549 2550 if (TYPE_OVERFLOW_WRAPS (expr_type)) 2551 { 2552 /* If overflow wraps, truncate the values and adjust the 2553 range kind and bounds appropriately. */ 2554 wide_int tmin = wide_int::from (wmin, prec, sgn); 2555 wide_int tmax = wide_int::from (wmax, prec, sgn); 2556 if (min_ovf == max_ovf) 2557 { 2558 /* No overflow or both overflow or underflow. The 2559 range kind stays VR_RANGE. */ 2560 min = wide_int_to_tree (expr_type, tmin); 2561 max = wide_int_to_tree (expr_type, tmax); 2562 } 2563 else if ((min_ovf == -1 && max_ovf == 0) 2564 || (max_ovf == 1 && min_ovf == 0)) 2565 { 2566 /* Min underflow or max overflow. The range kind 2567 changes to VR_ANTI_RANGE. */ 2568 bool covers = false; 2569 wide_int tem = tmin; 2570 type = VR_ANTI_RANGE; 2571 tmin = tmax + 1; 2572 if (wi::cmp (tmin, tmax, sgn) < 0) 2573 covers = true; 2574 tmax = tem - 1; 2575 if (wi::cmp (tmax, tem, sgn) > 0) 2576 covers = true; 2577 /* If the anti-range would cover nothing, drop to varying. 2578 Likewise if the anti-range bounds are outside of the 2579 types values. */ 2580 if (covers || wi::cmp (tmin, tmax, sgn) > 0) 2581 { 2582 set_value_range_to_varying (vr); 2583 return; 2584 } 2585 min = wide_int_to_tree (expr_type, tmin); 2586 max = wide_int_to_tree (expr_type, tmax); 2587 } 2588 else 2589 { 2590 /* Other underflow and/or overflow, drop to VR_VARYING. */ 2591 set_value_range_to_varying (vr); 2592 return; 2593 } 2594 } 2595 else 2596 { 2597 /* If overflow does not wrap, saturate to the types min/max 2598 value. */ 2599 if (min_ovf == -1) 2600 { 2601 if (needs_overflow_infinity (expr_type) 2602 && supports_overflow_infinity (expr_type)) 2603 min = negative_overflow_infinity (expr_type); 2604 else 2605 min = wide_int_to_tree (expr_type, type_min); 2606 } 2607 else if (min_ovf == 1) 2608 { 2609 if (needs_overflow_infinity (expr_type) 2610 && supports_overflow_infinity (expr_type)) 2611 min = positive_overflow_infinity (expr_type); 2612 else 2613 min = wide_int_to_tree (expr_type, type_max); 2614 } 2615 else 2616 min = wide_int_to_tree (expr_type, wmin); 2617 2618 if (max_ovf == -1) 2619 { 2620 if (needs_overflow_infinity (expr_type) 2621 && supports_overflow_infinity (expr_type)) 2622 max = negative_overflow_infinity (expr_type); 2623 else 2624 max = wide_int_to_tree (expr_type, type_min); 2625 } 2626 else if (max_ovf == 1) 2627 { 2628 if (needs_overflow_infinity (expr_type) 2629 && supports_overflow_infinity (expr_type)) 2630 max = positive_overflow_infinity (expr_type); 2631 else 2632 max = wide_int_to_tree (expr_type, type_max); 2633 } 2634 else 2635 max = wide_int_to_tree (expr_type, wmax); 2636 } 2637 2638 if (needs_overflow_infinity (expr_type) 2639 && supports_overflow_infinity (expr_type)) 2640 { 2641 if ((min_op0 && is_negative_overflow_infinity (min_op0)) 2642 || (min_op1 2643 && (minus_p 2644 ? is_positive_overflow_infinity (min_op1) 2645 : is_negative_overflow_infinity (min_op1)))) 2646 min = negative_overflow_infinity (expr_type); 2647 if ((max_op0 && is_positive_overflow_infinity (max_op0)) 2648 || (max_op1 2649 && (minus_p 2650 ? is_negative_overflow_infinity (max_op1) 2651 : is_positive_overflow_infinity (max_op1)))) 2652 max = positive_overflow_infinity (expr_type); 2653 } 2654 2655 /* If the result lower bound is constant, we're done; 2656 otherwise, build the symbolic lower bound. */ 2657 if (sym_min_op0 == sym_min_op1) 2658 ; 2659 else if (sym_min_op0) 2660 min = build_symbolic_expr (expr_type, sym_min_op0, 2661 neg_min_op0, min); 2662 else if (sym_min_op1) 2663 { 2664 /* We may not negate if that might introduce 2665 undefined overflow. */ 2666 if (! minus_p 2667 || neg_min_op1 2668 || TYPE_OVERFLOW_WRAPS (expr_type)) 2669 min = build_symbolic_expr (expr_type, sym_min_op1, 2670 neg_min_op1 ^ minus_p, min); 2671 else 2672 min = NULL_TREE; 2673 } 2674 2675 /* Likewise for the upper bound. */ 2676 if (sym_max_op0 == sym_max_op1) 2677 ; 2678 else if (sym_max_op0) 2679 max = build_symbolic_expr (expr_type, sym_max_op0, 2680 neg_max_op0, max); 2681 else if (sym_max_op1) 2682 { 2683 /* We may not negate if that might introduce 2684 undefined overflow. */ 2685 if (! minus_p 2686 || neg_max_op1 2687 || TYPE_OVERFLOW_WRAPS (expr_type)) 2688 max = build_symbolic_expr (expr_type, sym_max_op1, 2689 neg_max_op1 ^ minus_p, max); 2690 else 2691 max = NULL_TREE; 2692 } 2693 } 2694 else 2695 { 2696 /* For other cases, for example if we have a PLUS_EXPR with two 2697 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort 2698 to compute a precise range for such a case. 2699 ??? General even mixed range kind operations can be expressed 2700 by for example transforming ~[3, 5] + [1, 2] to range-only 2701 operations and a union primitive: 2702 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2] 2703 [-INF+1, 4] U [6, +INF(OVF)] 2704 though usually the union is not exactly representable with 2705 a single range or anti-range as the above is 2706 [-INF+1, +INF(OVF)] intersected with ~[5, 5] 2707 but one could use a scheme similar to equivalences for this. */ 2708 set_value_range_to_varying (vr); 2709 return; 2710 } 2711 } 2712 else if (code == MIN_EXPR 2713 || code == MAX_EXPR) 2714 { 2715 if (vr0.type == VR_RANGE 2716 && !symbolic_range_p (&vr0)) 2717 { 2718 type = VR_RANGE; 2719 if (vr1.type == VR_RANGE 2720 && !symbolic_range_p (&vr1)) 2721 { 2722 /* For operations that make the resulting range directly 2723 proportional to the original ranges, apply the operation to 2724 the same end of each range. */ 2725 min = vrp_int_const_binop (code, vr0.min, vr1.min); 2726 max = vrp_int_const_binop (code, vr0.max, vr1.max); 2727 } 2728 else if (code == MIN_EXPR) 2729 { 2730 min = vrp_val_min (expr_type); 2731 max = vr0.max; 2732 } 2733 else if (code == MAX_EXPR) 2734 { 2735 min = vr0.min; 2736 max = vrp_val_max (expr_type); 2737 } 2738 } 2739 else if (vr1.type == VR_RANGE 2740 && !symbolic_range_p (&vr1)) 2741 { 2742 type = VR_RANGE; 2743 if (code == MIN_EXPR) 2744 { 2745 min = vrp_val_min (expr_type); 2746 max = vr1.max; 2747 } 2748 else if (code == MAX_EXPR) 2749 { 2750 min = vr1.min; 2751 max = vrp_val_max (expr_type); 2752 } 2753 } 2754 else 2755 { 2756 set_value_range_to_varying (vr); 2757 return; 2758 } 2759 } 2760 else if (code == MULT_EXPR) 2761 { 2762 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not 2763 drop to varying. This test requires 2*prec bits if both 2764 operands are signed and 2*prec + 2 bits if either is not. */ 2765 2766 signop sign = TYPE_SIGN (expr_type); 2767 unsigned int prec = TYPE_PRECISION (expr_type); 2768 2769 if (range_int_cst_p (&vr0) 2770 && range_int_cst_p (&vr1) 2771 && TYPE_OVERFLOW_WRAPS (expr_type)) 2772 { 2773 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int; 2774 typedef generic_wide_int 2775 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst; 2776 vrp_int sizem1 = wi::mask <vrp_int> (prec, false); 2777 vrp_int size = sizem1 + 1; 2778 2779 /* Extend the values using the sign of the result to PREC2. 2780 From here on out, everthing is just signed math no matter 2781 what the input types were. */ 2782 vrp_int min0 = vrp_int_cst (vr0.min); 2783 vrp_int max0 = vrp_int_cst (vr0.max); 2784 vrp_int min1 = vrp_int_cst (vr1.min); 2785 vrp_int max1 = vrp_int_cst (vr1.max); 2786 /* Canonicalize the intervals. */ 2787 if (sign == UNSIGNED) 2788 { 2789 if (wi::ltu_p (size, min0 + max0)) 2790 { 2791 min0 -= size; 2792 max0 -= size; 2793 } 2794 2795 if (wi::ltu_p (size, min1 + max1)) 2796 { 2797 min1 -= size; 2798 max1 -= size; 2799 } 2800 } 2801 2802 vrp_int prod0 = min0 * min1; 2803 vrp_int prod1 = min0 * max1; 2804 vrp_int prod2 = max0 * min1; 2805 vrp_int prod3 = max0 * max1; 2806 2807 /* Sort the 4 products so that min is in prod0 and max is in 2808 prod3. */ 2809 /* min0min1 > max0max1 */ 2810 if (prod0 > prod3) 2811 std::swap (prod0, prod3); 2812 2813 /* min0max1 > max0min1 */ 2814 if (prod1 > prod2) 2815 std::swap (prod1, prod2); 2816 2817 if (prod0 > prod1) 2818 std::swap (prod0, prod1); 2819 2820 if (prod2 > prod3) 2821 std::swap (prod2, prod3); 2822 2823 /* diff = max - min. */ 2824 prod2 = prod3 - prod0; 2825 if (wi::geu_p (prod2, sizem1)) 2826 { 2827 /* the range covers all values. */ 2828 set_value_range_to_varying (vr); 2829 return; 2830 } 2831 2832 /* The following should handle the wrapping and selecting 2833 VR_ANTI_RANGE for us. */ 2834 min = wide_int_to_tree (expr_type, prod0); 2835 max = wide_int_to_tree (expr_type, prod3); 2836 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL); 2837 return; 2838 } 2839 2840 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs, 2841 drop to VR_VARYING. It would take more effort to compute a 2842 precise range for such a case. For example, if we have 2843 op0 == 65536 and op1 == 65536 with their ranges both being 2844 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so 2845 we cannot claim that the product is in ~[0,0]. Note that we 2846 are guaranteed to have vr0.type == vr1.type at this 2847 point. */ 2848 if (vr0.type == VR_ANTI_RANGE 2849 && !TYPE_OVERFLOW_UNDEFINED (expr_type)) 2850 { 2851 set_value_range_to_varying (vr); 2852 return; 2853 } 2854 2855 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1); 2856 return; 2857 } 2858 else if (code == RSHIFT_EXPR 2859 || code == LSHIFT_EXPR) 2860 { 2861 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1], 2862 then drop to VR_VARYING. Outside of this range we get undefined 2863 behavior from the shift operation. We cannot even trust 2864 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl 2865 shifts, and the operation at the tree level may be widened. */ 2866 if (range_int_cst_p (&vr1) 2867 && compare_tree_int (vr1.min, 0) >= 0 2868 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1) 2869 { 2870 if (code == RSHIFT_EXPR) 2871 { 2872 /* Even if vr0 is VARYING or otherwise not usable, we can derive 2873 useful ranges just from the shift count. E.g. 2874 x >> 63 for signed 64-bit x is always [-1, 0]. */ 2875 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0)) 2876 { 2877 vr0.type = type = VR_RANGE; 2878 vr0.min = vrp_val_min (expr_type); 2879 vr0.max = vrp_val_max (expr_type); 2880 } 2881 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1); 2882 return; 2883 } 2884 /* We can map lshifts by constants to MULT_EXPR handling. */ 2885 else if (code == LSHIFT_EXPR 2886 && range_int_cst_singleton_p (&vr1)) 2887 { 2888 bool saved_flag_wrapv; 2889 value_range vr1p = VR_INITIALIZER; 2890 vr1p.type = VR_RANGE; 2891 vr1p.min = (wide_int_to_tree 2892 (expr_type, 2893 wi::set_bit_in_zero (tree_to_shwi (vr1.min), 2894 TYPE_PRECISION (expr_type)))); 2895 vr1p.max = vr1p.min; 2896 /* We have to use a wrapping multiply though as signed overflow 2897 on lshifts is implementation defined in C89. */ 2898 saved_flag_wrapv = flag_wrapv; 2899 flag_wrapv = 1; 2900 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type, 2901 &vr0, &vr1p); 2902 flag_wrapv = saved_flag_wrapv; 2903 return; 2904 } 2905 else if (code == LSHIFT_EXPR 2906 && range_int_cst_p (&vr0)) 2907 { 2908 int prec = TYPE_PRECISION (expr_type); 2909 int overflow_pos = prec; 2910 int bound_shift; 2911 wide_int low_bound, high_bound; 2912 bool uns = TYPE_UNSIGNED (expr_type); 2913 bool in_bounds = false; 2914 2915 if (!uns) 2916 overflow_pos -= 1; 2917 2918 bound_shift = overflow_pos - tree_to_shwi (vr1.max); 2919 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can 2920 overflow. However, for that to happen, vr1.max needs to be 2921 zero, which means vr1 is a singleton range of zero, which 2922 means it should be handled by the previous LSHIFT_EXPR 2923 if-clause. */ 2924 wide_int bound = wi::set_bit_in_zero (bound_shift, prec); 2925 wide_int complement = ~(bound - 1); 2926 2927 if (uns) 2928 { 2929 low_bound = bound; 2930 high_bound = complement; 2931 if (wi::ltu_p (vr0.max, low_bound)) 2932 { 2933 /* [5, 6] << [1, 2] == [10, 24]. */ 2934 /* We're shifting out only zeroes, the value increases 2935 monotonically. */ 2936 in_bounds = true; 2937 } 2938 else if (wi::ltu_p (high_bound, vr0.min)) 2939 { 2940 /* [0xffffff00, 0xffffffff] << [1, 2] 2941 == [0xfffffc00, 0xfffffffe]. */ 2942 /* We're shifting out only ones, the value decreases 2943 monotonically. */ 2944 in_bounds = true; 2945 } 2946 } 2947 else 2948 { 2949 /* [-1, 1] << [1, 2] == [-4, 4]. */ 2950 low_bound = complement; 2951 high_bound = bound; 2952 if (wi::lts_p (vr0.max, high_bound) 2953 && wi::lts_p (low_bound, vr0.min)) 2954 { 2955 /* For non-negative numbers, we're shifting out only 2956 zeroes, the value increases monotonically. 2957 For negative numbers, we're shifting out only ones, the 2958 value decreases monotomically. */ 2959 in_bounds = true; 2960 } 2961 } 2962 2963 if (in_bounds) 2964 { 2965 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1); 2966 return; 2967 } 2968 } 2969 } 2970 set_value_range_to_varying (vr); 2971 return; 2972 } 2973 else if (code == TRUNC_DIV_EXPR 2974 || code == FLOOR_DIV_EXPR 2975 || code == CEIL_DIV_EXPR 2976 || code == EXACT_DIV_EXPR 2977 || code == ROUND_DIV_EXPR) 2978 { 2979 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0)) 2980 { 2981 /* For division, if op1 has VR_RANGE but op0 does not, something 2982 can be deduced just from that range. Say [min, max] / [4, max] 2983 gives [min / 4, max / 4] range. */ 2984 if (vr1.type == VR_RANGE 2985 && !symbolic_range_p (&vr1) 2986 && range_includes_zero_p (vr1.min, vr1.max) == 0) 2987 { 2988 vr0.type = type = VR_RANGE; 2989 vr0.min = vrp_val_min (expr_type); 2990 vr0.max = vrp_val_max (expr_type); 2991 } 2992 else 2993 { 2994 set_value_range_to_varying (vr); 2995 return; 2996 } 2997 } 2998 2999 /* For divisions, if flag_non_call_exceptions is true, we must 3000 not eliminate a division by zero. */ 3001 if (cfun->can_throw_non_call_exceptions 3002 && (vr1.type != VR_RANGE 3003 || range_includes_zero_p (vr1.min, vr1.max) != 0)) 3004 { 3005 set_value_range_to_varying (vr); 3006 return; 3007 } 3008 3009 /* For divisions, if op0 is VR_RANGE, we can deduce a range 3010 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can 3011 include 0. */ 3012 if (vr0.type == VR_RANGE 3013 && (vr1.type != VR_RANGE 3014 || range_includes_zero_p (vr1.min, vr1.max) != 0)) 3015 { 3016 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0); 3017 int cmp; 3018 3019 min = NULL_TREE; 3020 max = NULL_TREE; 3021 if (TYPE_UNSIGNED (expr_type) 3022 || value_range_nonnegative_p (&vr1)) 3023 { 3024 /* For unsigned division or when divisor is known 3025 to be non-negative, the range has to cover 3026 all numbers from 0 to max for positive max 3027 and all numbers from min to 0 for negative min. */ 3028 cmp = compare_values (vr0.max, zero); 3029 if (cmp == -1) 3030 { 3031 /* When vr0.max < 0, vr1.min != 0 and value 3032 ranges for dividend and divisor are available. */ 3033 if (vr1.type == VR_RANGE 3034 && !symbolic_range_p (&vr0) 3035 && !symbolic_range_p (&vr1) 3036 && compare_values (vr1.min, zero) != 0) 3037 max = int_const_binop (code, vr0.max, vr1.min); 3038 else 3039 max = zero; 3040 } 3041 else if (cmp == 0 || cmp == 1) 3042 max = vr0.max; 3043 else 3044 type = VR_VARYING; 3045 cmp = compare_values (vr0.min, zero); 3046 if (cmp == 1) 3047 { 3048 /* For unsigned division when value ranges for dividend 3049 and divisor are available. */ 3050 if (vr1.type == VR_RANGE 3051 && !symbolic_range_p (&vr0) 3052 && !symbolic_range_p (&vr1) 3053 && compare_values (vr1.max, zero) != 0) 3054 min = int_const_binop (code, vr0.min, vr1.max); 3055 else 3056 min = zero; 3057 } 3058 else if (cmp == 0 || cmp == -1) 3059 min = vr0.min; 3060 else 3061 type = VR_VARYING; 3062 } 3063 else 3064 { 3065 /* Otherwise the range is -max .. max or min .. -min 3066 depending on which bound is bigger in absolute value, 3067 as the division can change the sign. */ 3068 abs_extent_range (vr, vr0.min, vr0.max); 3069 return; 3070 } 3071 if (type == VR_VARYING) 3072 { 3073 set_value_range_to_varying (vr); 3074 return; 3075 } 3076 } 3077 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1)) 3078 { 3079 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1); 3080 return; 3081 } 3082 } 3083 else if (code == TRUNC_MOD_EXPR) 3084 { 3085 if (range_is_null (&vr1)) 3086 { 3087 set_value_range_to_undefined (vr); 3088 return; 3089 } 3090 /* ABS (A % B) < ABS (B) and either 3091 0 <= A % B <= A or A <= A % B <= 0. */ 3092 type = VR_RANGE; 3093 signop sgn = TYPE_SIGN (expr_type); 3094 unsigned int prec = TYPE_PRECISION (expr_type); 3095 wide_int wmin, wmax, tmp; 3096 wide_int zero = wi::zero (prec); 3097 wide_int one = wi::one (prec); 3098 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1)) 3099 { 3100 wmax = wi::sub (vr1.max, one); 3101 if (sgn == SIGNED) 3102 { 3103 tmp = wi::sub (wi::minus_one (prec), vr1.min); 3104 wmax = wi::smax (wmax, tmp); 3105 } 3106 } 3107 else 3108 { 3109 wmax = wi::max_value (prec, sgn); 3110 /* X % INT_MIN may be INT_MAX. */ 3111 if (sgn == UNSIGNED) 3112 wmax = wmax - one; 3113 } 3114 3115 if (sgn == UNSIGNED) 3116 wmin = zero; 3117 else 3118 { 3119 wmin = -wmax; 3120 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST) 3121 { 3122 tmp = vr0.min; 3123 if (wi::gts_p (tmp, zero)) 3124 tmp = zero; 3125 wmin = wi::smax (wmin, tmp); 3126 } 3127 } 3128 3129 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST) 3130 { 3131 tmp = vr0.max; 3132 if (sgn == SIGNED && wi::neg_p (tmp)) 3133 tmp = zero; 3134 wmax = wi::min (wmax, tmp, sgn); 3135 } 3136 3137 min = wide_int_to_tree (expr_type, wmin); 3138 max = wide_int_to_tree (expr_type, wmax); 3139 } 3140 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR) 3141 { 3142 bool int_cst_range0, int_cst_range1; 3143 wide_int may_be_nonzero0, may_be_nonzero1; 3144 wide_int must_be_nonzero0, must_be_nonzero1; 3145 3146 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0, 3147 &may_be_nonzero0, 3148 &must_be_nonzero0); 3149 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1, 3150 &may_be_nonzero1, 3151 &must_be_nonzero1); 3152 3153 type = VR_RANGE; 3154 if (code == BIT_AND_EXPR) 3155 { 3156 min = wide_int_to_tree (expr_type, 3157 must_be_nonzero0 & must_be_nonzero1); 3158 wide_int wmax = may_be_nonzero0 & may_be_nonzero1; 3159 /* If both input ranges contain only negative values we can 3160 truncate the result range maximum to the minimum of the 3161 input range maxima. */ 3162 if (int_cst_range0 && int_cst_range1 3163 && tree_int_cst_sgn (vr0.max) < 0 3164 && tree_int_cst_sgn (vr1.max) < 0) 3165 { 3166 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type)); 3167 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type)); 3168 } 3169 /* If either input range contains only non-negative values 3170 we can truncate the result range maximum to the respective 3171 maximum of the input range. */ 3172 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0) 3173 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type)); 3174 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0) 3175 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type)); 3176 max = wide_int_to_tree (expr_type, wmax); 3177 cmp = compare_values (min, max); 3178 /* PR68217: In case of signed & sign-bit-CST should 3179 result in [-INF, 0] instead of [-INF, INF]. */ 3180 if (cmp == -2 || cmp == 1) 3181 { 3182 wide_int sign_bit 3183 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1, 3184 TYPE_PRECISION (expr_type)); 3185 if (!TYPE_UNSIGNED (expr_type) 3186 && ((value_range_constant_singleton (&vr0) 3187 && !wi::cmps (vr0.min, sign_bit)) 3188 || (value_range_constant_singleton (&vr1) 3189 && !wi::cmps (vr1.min, sign_bit)))) 3190 { 3191 min = TYPE_MIN_VALUE (expr_type); 3192 max = build_int_cst (expr_type, 0); 3193 } 3194 } 3195 } 3196 else if (code == BIT_IOR_EXPR) 3197 { 3198 max = wide_int_to_tree (expr_type, 3199 may_be_nonzero0 | may_be_nonzero1); 3200 wide_int wmin = must_be_nonzero0 | must_be_nonzero1; 3201 /* If the input ranges contain only positive values we can 3202 truncate the minimum of the result range to the maximum 3203 of the input range minima. */ 3204 if (int_cst_range0 && int_cst_range1 3205 && tree_int_cst_sgn (vr0.min) >= 0 3206 && tree_int_cst_sgn (vr1.min) >= 0) 3207 { 3208 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type)); 3209 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type)); 3210 } 3211 /* If either input range contains only negative values 3212 we can truncate the minimum of the result range to the 3213 respective minimum range. */ 3214 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0) 3215 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type)); 3216 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0) 3217 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type)); 3218 min = wide_int_to_tree (expr_type, wmin); 3219 } 3220 else if (code == BIT_XOR_EXPR) 3221 { 3222 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1) 3223 | ~(may_be_nonzero0 | may_be_nonzero1)); 3224 wide_int result_one_bits 3225 = (must_be_nonzero0.and_not (may_be_nonzero1) 3226 | must_be_nonzero1.and_not (may_be_nonzero0)); 3227 max = wide_int_to_tree (expr_type, ~result_zero_bits); 3228 min = wide_int_to_tree (expr_type, result_one_bits); 3229 /* If the range has all positive or all negative values the 3230 result is better than VARYING. */ 3231 if (tree_int_cst_sgn (min) < 0 3232 || tree_int_cst_sgn (max) >= 0) 3233 ; 3234 else 3235 max = min = NULL_TREE; 3236 } 3237 } 3238 else 3239 gcc_unreachable (); 3240 3241 /* If either MIN or MAX overflowed, then set the resulting range to 3242 VARYING. But we do accept an overflow infinity representation. */ 3243 if (min == NULL_TREE 3244 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min)) 3245 || max == NULL_TREE 3246 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max))) 3247 { 3248 set_value_range_to_varying (vr); 3249 return; 3250 } 3251 3252 /* We punt if: 3253 1) [-INF, +INF] 3254 2) [-INF, +-INF(OVF)] 3255 3) [+-INF(OVF), +INF] 3256 4) [+-INF(OVF), +-INF(OVF)] 3257 We learn nothing when we have INF and INF(OVF) on both sides. 3258 Note that we do accept [-INF, -INF] and [+INF, +INF] without 3259 overflow. */ 3260 if ((vrp_val_is_min (min) || is_overflow_infinity (min)) 3261 && (vrp_val_is_max (max) || is_overflow_infinity (max))) 3262 { 3263 set_value_range_to_varying (vr); 3264 return; 3265 } 3266 3267 cmp = compare_values (min, max); 3268 if (cmp == -2 || cmp == 1) 3269 { 3270 /* If the new range has its limits swapped around (MIN > MAX), 3271 then the operation caused one of them to wrap around, mark 3272 the new range VARYING. */ 3273 set_value_range_to_varying (vr); 3274 } 3275 else 3276 set_value_range (vr, type, min, max, NULL); 3277 } 3278 3279 /* Extract range information from a binary expression OP0 CODE OP1 based on 3280 the ranges of each of its operands with resulting type EXPR_TYPE. 3281 The resulting range is stored in *VR. */ 3282 3283 static void 3284 extract_range_from_binary_expr (value_range *vr, 3285 enum tree_code code, 3286 tree expr_type, tree op0, tree op1) 3287 { 3288 value_range vr0 = VR_INITIALIZER; 3289 value_range vr1 = VR_INITIALIZER; 3290 3291 /* Get value ranges for each operand. For constant operands, create 3292 a new value range with the operand to simplify processing. */ 3293 if (TREE_CODE (op0) == SSA_NAME) 3294 vr0 = *(get_value_range (op0)); 3295 else if (is_gimple_min_invariant (op0)) 3296 set_value_range_to_value (&vr0, op0, NULL); 3297 else 3298 set_value_range_to_varying (&vr0); 3299 3300 if (TREE_CODE (op1) == SSA_NAME) 3301 vr1 = *(get_value_range (op1)); 3302 else if (is_gimple_min_invariant (op1)) 3303 set_value_range_to_value (&vr1, op1, NULL); 3304 else 3305 set_value_range_to_varying (&vr1); 3306 3307 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1); 3308 3309 /* Try harder for PLUS and MINUS if the range of one operand is symbolic 3310 and based on the other operand, for example if it was deduced from a 3311 symbolic comparison. When a bound of the range of the first operand 3312 is invariant, we set the corresponding bound of the new range to INF 3313 in order to avoid recursing on the range of the second operand. */ 3314 if (vr->type == VR_VARYING 3315 && (code == PLUS_EXPR || code == MINUS_EXPR) 3316 && TREE_CODE (op1) == SSA_NAME 3317 && vr0.type == VR_RANGE 3318 && symbolic_range_based_on_p (&vr0, op1)) 3319 { 3320 const bool minus_p = (code == MINUS_EXPR); 3321 value_range n_vr1 = VR_INITIALIZER; 3322 3323 /* Try with VR0 and [-INF, OP1]. */ 3324 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min)) 3325 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL); 3326 3327 /* Try with VR0 and [OP1, +INF]. */ 3328 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max)) 3329 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL); 3330 3331 /* Try with VR0 and [OP1, OP1]. */ 3332 else 3333 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL); 3334 3335 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1); 3336 } 3337 3338 if (vr->type == VR_VARYING 3339 && (code == PLUS_EXPR || code == MINUS_EXPR) 3340 && TREE_CODE (op0) == SSA_NAME 3341 && vr1.type == VR_RANGE 3342 && symbolic_range_based_on_p (&vr1, op0)) 3343 { 3344 const bool minus_p = (code == MINUS_EXPR); 3345 value_range n_vr0 = VR_INITIALIZER; 3346 3347 /* Try with [-INF, OP0] and VR1. */ 3348 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min)) 3349 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL); 3350 3351 /* Try with [OP0, +INF] and VR1. */ 3352 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max)) 3353 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL); 3354 3355 /* Try with [OP0, OP0] and VR1. */ 3356 else 3357 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL); 3358 3359 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1); 3360 } 3361 3362 /* If we didn't derive a range for MINUS_EXPR, and 3363 op1's range is ~[op0,op0] or vice-versa, then we 3364 can derive a non-null range. This happens often for 3365 pointer subtraction. */ 3366 if (vr->type == VR_VARYING 3367 && code == MINUS_EXPR 3368 && TREE_CODE (op0) == SSA_NAME 3369 && ((vr0.type == VR_ANTI_RANGE 3370 && vr0.min == op1 3371 && vr0.min == vr0.max) 3372 || (vr1.type == VR_ANTI_RANGE 3373 && vr1.min == op0 3374 && vr1.min == vr1.max))) 3375 set_value_range_to_nonnull (vr, TREE_TYPE (op0)); 3376 } 3377 3378 /* Extract range information from a unary operation CODE based on 3379 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE. 3380 The resulting range is stored in *VR. */ 3381 3382 void 3383 extract_range_from_unary_expr (value_range *vr, 3384 enum tree_code code, tree type, 3385 value_range *vr0_, tree op0_type) 3386 { 3387 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER; 3388 3389 /* VRP only operates on integral and pointer types. */ 3390 if (!(INTEGRAL_TYPE_P (op0_type) 3391 || POINTER_TYPE_P (op0_type)) 3392 || !(INTEGRAL_TYPE_P (type) 3393 || POINTER_TYPE_P (type))) 3394 { 3395 set_value_range_to_varying (vr); 3396 return; 3397 } 3398 3399 /* If VR0 is UNDEFINED, so is the result. */ 3400 if (vr0.type == VR_UNDEFINED) 3401 { 3402 set_value_range_to_undefined (vr); 3403 return; 3404 } 3405 3406 /* Handle operations that we express in terms of others. */ 3407 if (code == PAREN_EXPR || code == OBJ_TYPE_REF) 3408 { 3409 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */ 3410 copy_value_range (vr, &vr0); 3411 return; 3412 } 3413 else if (code == NEGATE_EXPR) 3414 { 3415 /* -X is simply 0 - X, so re-use existing code that also handles 3416 anti-ranges fine. */ 3417 value_range zero = VR_INITIALIZER; 3418 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL); 3419 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0); 3420 return; 3421 } 3422 else if (code == BIT_NOT_EXPR) 3423 { 3424 /* ~X is simply -1 - X, so re-use existing code that also handles 3425 anti-ranges fine. */ 3426 value_range minusone = VR_INITIALIZER; 3427 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL); 3428 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, 3429 type, &minusone, &vr0); 3430 return; 3431 } 3432 3433 /* Now canonicalize anti-ranges to ranges when they are not symbolic 3434 and express op ~[] as (op []') U (op []''). */ 3435 if (vr0.type == VR_ANTI_RANGE 3436 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1)) 3437 { 3438 extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type); 3439 if (vrtem1.type != VR_UNDEFINED) 3440 { 3441 value_range vrres = VR_INITIALIZER; 3442 extract_range_from_unary_expr (&vrres, code, type, 3443 &vrtem1, op0_type); 3444 vrp_meet (vr, &vrres); 3445 } 3446 return; 3447 } 3448 3449 if (CONVERT_EXPR_CODE_P (code)) 3450 { 3451 tree inner_type = op0_type; 3452 tree outer_type = type; 3453 3454 /* If the expression evaluates to a pointer, we are only interested in 3455 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */ 3456 if (POINTER_TYPE_P (type)) 3457 { 3458 if (range_is_nonnull (&vr0)) 3459 set_value_range_to_nonnull (vr, type); 3460 else if (range_is_null (&vr0)) 3461 set_value_range_to_null (vr, type); 3462 else 3463 set_value_range_to_varying (vr); 3464 return; 3465 } 3466 3467 /* If VR0 is varying and we increase the type precision, assume 3468 a full range for the following transformation. */ 3469 if (vr0.type == VR_VARYING 3470 && INTEGRAL_TYPE_P (inner_type) 3471 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)) 3472 { 3473 vr0.type = VR_RANGE; 3474 vr0.min = TYPE_MIN_VALUE (inner_type); 3475 vr0.max = TYPE_MAX_VALUE (inner_type); 3476 } 3477 3478 /* If VR0 is a constant range or anti-range and the conversion is 3479 not truncating we can convert the min and max values and 3480 canonicalize the resulting range. Otherwise we can do the 3481 conversion if the size of the range is less than what the 3482 precision of the target type can represent and the range is 3483 not an anti-range. */ 3484 if ((vr0.type == VR_RANGE 3485 || vr0.type == VR_ANTI_RANGE) 3486 && TREE_CODE (vr0.min) == INTEGER_CST 3487 && TREE_CODE (vr0.max) == INTEGER_CST 3488 && (!is_overflow_infinity (vr0.min) 3489 || (vr0.type == VR_RANGE 3490 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type) 3491 && needs_overflow_infinity (outer_type) 3492 && supports_overflow_infinity (outer_type))) 3493 && (!is_overflow_infinity (vr0.max) 3494 || (vr0.type == VR_RANGE 3495 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type) 3496 && needs_overflow_infinity (outer_type) 3497 && supports_overflow_infinity (outer_type))) 3498 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type) 3499 || (vr0.type == VR_RANGE 3500 && integer_zerop (int_const_binop (RSHIFT_EXPR, 3501 int_const_binop (MINUS_EXPR, vr0.max, vr0.min), 3502 size_int (TYPE_PRECISION (outer_type))))))) 3503 { 3504 tree new_min, new_max; 3505 if (is_overflow_infinity (vr0.min)) 3506 new_min = negative_overflow_infinity (outer_type); 3507 else 3508 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min), 3509 0, false); 3510 if (is_overflow_infinity (vr0.max)) 3511 new_max = positive_overflow_infinity (outer_type); 3512 else 3513 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max), 3514 0, false); 3515 set_and_canonicalize_value_range (vr, vr0.type, 3516 new_min, new_max, NULL); 3517 return; 3518 } 3519 3520 set_value_range_to_varying (vr); 3521 return; 3522 } 3523 else if (code == ABS_EXPR) 3524 { 3525 tree min, max; 3526 int cmp; 3527 3528 /* Pass through vr0 in the easy cases. */ 3529 if (TYPE_UNSIGNED (type) 3530 || value_range_nonnegative_p (&vr0)) 3531 { 3532 copy_value_range (vr, &vr0); 3533 return; 3534 } 3535 3536 /* For the remaining varying or symbolic ranges we can't do anything 3537 useful. */ 3538 if (vr0.type == VR_VARYING 3539 || symbolic_range_p (&vr0)) 3540 { 3541 set_value_range_to_varying (vr); 3542 return; 3543 } 3544 3545 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a 3546 useful range. */ 3547 if (!TYPE_OVERFLOW_UNDEFINED (type) 3548 && ((vr0.type == VR_RANGE 3549 && vrp_val_is_min (vr0.min)) 3550 || (vr0.type == VR_ANTI_RANGE 3551 && !vrp_val_is_min (vr0.min)))) 3552 { 3553 set_value_range_to_varying (vr); 3554 return; 3555 } 3556 3557 /* ABS_EXPR may flip the range around, if the original range 3558 included negative values. */ 3559 if (is_overflow_infinity (vr0.min)) 3560 min = positive_overflow_infinity (type); 3561 else if (!vrp_val_is_min (vr0.min)) 3562 min = fold_unary_to_constant (code, type, vr0.min); 3563 else if (!needs_overflow_infinity (type)) 3564 min = TYPE_MAX_VALUE (type); 3565 else if (supports_overflow_infinity (type)) 3566 min = positive_overflow_infinity (type); 3567 else 3568 { 3569 set_value_range_to_varying (vr); 3570 return; 3571 } 3572 3573 if (is_overflow_infinity (vr0.max)) 3574 max = positive_overflow_infinity (type); 3575 else if (!vrp_val_is_min (vr0.max)) 3576 max = fold_unary_to_constant (code, type, vr0.max); 3577 else if (!needs_overflow_infinity (type)) 3578 max = TYPE_MAX_VALUE (type); 3579 else if (supports_overflow_infinity (type) 3580 /* We shouldn't generate [+INF, +INF] as set_value_range 3581 doesn't like this and ICEs. */ 3582 && !is_positive_overflow_infinity (min)) 3583 max = positive_overflow_infinity (type); 3584 else 3585 { 3586 set_value_range_to_varying (vr); 3587 return; 3588 } 3589 3590 cmp = compare_values (min, max); 3591 3592 /* If a VR_ANTI_RANGEs contains zero, then we have 3593 ~[-INF, min(MIN, MAX)]. */ 3594 if (vr0.type == VR_ANTI_RANGE) 3595 { 3596 if (range_includes_zero_p (vr0.min, vr0.max) == 1) 3597 { 3598 /* Take the lower of the two values. */ 3599 if (cmp != 1) 3600 max = min; 3601 3602 /* Create ~[-INF, min (abs(MIN), abs(MAX))] 3603 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when 3604 flag_wrapv is set and the original anti-range doesn't include 3605 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */ 3606 if (TYPE_OVERFLOW_WRAPS (type)) 3607 { 3608 tree type_min_value = TYPE_MIN_VALUE (type); 3609 3610 min = (vr0.min != type_min_value 3611 ? int_const_binop (PLUS_EXPR, type_min_value, 3612 build_int_cst (TREE_TYPE (type_min_value), 1)) 3613 : type_min_value); 3614 } 3615 else 3616 { 3617 if (overflow_infinity_range_p (&vr0)) 3618 min = negative_overflow_infinity (type); 3619 else 3620 min = TYPE_MIN_VALUE (type); 3621 } 3622 } 3623 else 3624 { 3625 /* All else has failed, so create the range [0, INF], even for 3626 flag_wrapv since TYPE_MIN_VALUE is in the original 3627 anti-range. */ 3628 vr0.type = VR_RANGE; 3629 min = build_int_cst (type, 0); 3630 if (needs_overflow_infinity (type)) 3631 { 3632 if (supports_overflow_infinity (type)) 3633 max = positive_overflow_infinity (type); 3634 else 3635 { 3636 set_value_range_to_varying (vr); 3637 return; 3638 } 3639 } 3640 else 3641 max = TYPE_MAX_VALUE (type); 3642 } 3643 } 3644 3645 /* If the range contains zero then we know that the minimum value in the 3646 range will be zero. */ 3647 else if (range_includes_zero_p (vr0.min, vr0.max) == 1) 3648 { 3649 if (cmp == 1) 3650 max = min; 3651 min = build_int_cst (type, 0); 3652 } 3653 else 3654 { 3655 /* If the range was reversed, swap MIN and MAX. */ 3656 if (cmp == 1) 3657 std::swap (min, max); 3658 } 3659 3660 cmp = compare_values (min, max); 3661 if (cmp == -2 || cmp == 1) 3662 { 3663 /* If the new range has its limits swapped around (MIN > MAX), 3664 then the operation caused one of them to wrap around, mark 3665 the new range VARYING. */ 3666 set_value_range_to_varying (vr); 3667 } 3668 else 3669 set_value_range (vr, vr0.type, min, max, NULL); 3670 return; 3671 } 3672 3673 /* For unhandled operations fall back to varying. */ 3674 set_value_range_to_varying (vr); 3675 return; 3676 } 3677 3678 3679 /* Extract range information from a unary expression CODE OP0 based on 3680 the range of its operand with resulting type TYPE. 3681 The resulting range is stored in *VR. */ 3682 3683 static void 3684 extract_range_from_unary_expr (value_range *vr, enum tree_code code, 3685 tree type, tree op0) 3686 { 3687 value_range vr0 = VR_INITIALIZER; 3688 3689 /* Get value ranges for the operand. For constant operands, create 3690 a new value range with the operand to simplify processing. */ 3691 if (TREE_CODE (op0) == SSA_NAME) 3692 vr0 = *(get_value_range (op0)); 3693 else if (is_gimple_min_invariant (op0)) 3694 set_value_range_to_value (&vr0, op0, NULL); 3695 else 3696 set_value_range_to_varying (&vr0); 3697 3698 extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0)); 3699 } 3700 3701 3702 /* Extract range information from a conditional expression STMT based on 3703 the ranges of each of its operands and the expression code. */ 3704 3705 static void 3706 extract_range_from_cond_expr (value_range *vr, gassign *stmt) 3707 { 3708 tree op0, op1; 3709 value_range vr0 = VR_INITIALIZER; 3710 value_range vr1 = VR_INITIALIZER; 3711 3712 /* Get value ranges for each operand. For constant operands, create 3713 a new value range with the operand to simplify processing. */ 3714 op0 = gimple_assign_rhs2 (stmt); 3715 if (TREE_CODE (op0) == SSA_NAME) 3716 vr0 = *(get_value_range (op0)); 3717 else if (is_gimple_min_invariant (op0)) 3718 set_value_range_to_value (&vr0, op0, NULL); 3719 else 3720 set_value_range_to_varying (&vr0); 3721 3722 op1 = gimple_assign_rhs3 (stmt); 3723 if (TREE_CODE (op1) == SSA_NAME) 3724 vr1 = *(get_value_range (op1)); 3725 else if (is_gimple_min_invariant (op1)) 3726 set_value_range_to_value (&vr1, op1, NULL); 3727 else 3728 set_value_range_to_varying (&vr1); 3729 3730 /* The resulting value range is the union of the operand ranges */ 3731 copy_value_range (vr, &vr0); 3732 vrp_meet (vr, &vr1); 3733 } 3734 3735 3736 /* Extract range information from a comparison expression EXPR based 3737 on the range of its operand and the expression code. */ 3738 3739 static void 3740 extract_range_from_comparison (value_range *vr, enum tree_code code, 3741 tree type, tree op0, tree op1) 3742 { 3743 bool sop = false; 3744 tree val; 3745 3746 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop, 3747 NULL); 3748 3749 /* A disadvantage of using a special infinity as an overflow 3750 representation is that we lose the ability to record overflow 3751 when we don't have an infinity. So we have to ignore a result 3752 which relies on overflow. */ 3753 3754 if (val && !is_overflow_infinity (val) && !sop) 3755 { 3756 /* Since this expression was found on the RHS of an assignment, 3757 its type may be different from _Bool. Convert VAL to EXPR's 3758 type. */ 3759 val = fold_convert (type, val); 3760 if (is_gimple_min_invariant (val)) 3761 set_value_range_to_value (vr, val, vr->equiv); 3762 else 3763 set_value_range (vr, VR_RANGE, val, val, vr->equiv); 3764 } 3765 else 3766 /* The result of a comparison is always true or false. */ 3767 set_value_range_to_truthvalue (vr, type); 3768 } 3769 3770 /* Helper function for simplify_internal_call_using_ranges and 3771 extract_range_basic. Return true if OP0 SUBCODE OP1 for 3772 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or 3773 always overflow. Set *OVF to true if it is known to always 3774 overflow. */ 3775 3776 static bool 3777 check_for_binary_op_overflow (enum tree_code subcode, tree type, 3778 tree op0, tree op1, bool *ovf) 3779 { 3780 value_range vr0 = VR_INITIALIZER; 3781 value_range vr1 = VR_INITIALIZER; 3782 if (TREE_CODE (op0) == SSA_NAME) 3783 vr0 = *get_value_range (op0); 3784 else if (TREE_CODE (op0) == INTEGER_CST) 3785 set_value_range_to_value (&vr0, op0, NULL); 3786 else 3787 set_value_range_to_varying (&vr0); 3788 3789 if (TREE_CODE (op1) == SSA_NAME) 3790 vr1 = *get_value_range (op1); 3791 else if (TREE_CODE (op1) == INTEGER_CST) 3792 set_value_range_to_value (&vr1, op1, NULL); 3793 else 3794 set_value_range_to_varying (&vr1); 3795 3796 if (!range_int_cst_p (&vr0) 3797 || TREE_OVERFLOW (vr0.min) 3798 || TREE_OVERFLOW (vr0.max)) 3799 { 3800 vr0.min = vrp_val_min (TREE_TYPE (op0)); 3801 vr0.max = vrp_val_max (TREE_TYPE (op0)); 3802 } 3803 if (!range_int_cst_p (&vr1) 3804 || TREE_OVERFLOW (vr1.min) 3805 || TREE_OVERFLOW (vr1.max)) 3806 { 3807 vr1.min = vrp_val_min (TREE_TYPE (op1)); 3808 vr1.max = vrp_val_max (TREE_TYPE (op1)); 3809 } 3810 *ovf = arith_overflowed_p (subcode, type, vr0.min, 3811 subcode == MINUS_EXPR ? vr1.max : vr1.min); 3812 if (arith_overflowed_p (subcode, type, vr0.max, 3813 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf) 3814 return false; 3815 if (subcode == MULT_EXPR) 3816 { 3817 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf 3818 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf) 3819 return false; 3820 } 3821 if (*ovf) 3822 { 3823 /* So far we found that there is an overflow on the boundaries. 3824 That doesn't prove that there is an overflow even for all values 3825 in between the boundaries. For that compute widest_int range 3826 of the result and see if it doesn't overlap the range of 3827 type. */ 3828 widest_int wmin, wmax; 3829 widest_int w[4]; 3830 int i; 3831 w[0] = wi::to_widest (vr0.min); 3832 w[1] = wi::to_widest (vr0.max); 3833 w[2] = wi::to_widest (vr1.min); 3834 w[3] = wi::to_widest (vr1.max); 3835 for (i = 0; i < 4; i++) 3836 { 3837 widest_int wt; 3838 switch (subcode) 3839 { 3840 case PLUS_EXPR: 3841 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]); 3842 break; 3843 case MINUS_EXPR: 3844 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]); 3845 break; 3846 case MULT_EXPR: 3847 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]); 3848 break; 3849 default: 3850 gcc_unreachable (); 3851 } 3852 if (i == 0) 3853 { 3854 wmin = wt; 3855 wmax = wt; 3856 } 3857 else 3858 { 3859 wmin = wi::smin (wmin, wt); 3860 wmax = wi::smax (wmax, wt); 3861 } 3862 } 3863 /* The result of op0 CODE op1 is known to be in range 3864 [wmin, wmax]. */ 3865 widest_int wtmin = wi::to_widest (vrp_val_min (type)); 3866 widest_int wtmax = wi::to_widest (vrp_val_max (type)); 3867 /* If all values in [wmin, wmax] are smaller than 3868 [wtmin, wtmax] or all are larger than [wtmin, wtmax], 3869 the arithmetic operation will always overflow. */ 3870 if (wmax < wtmin || wmin > wtmax) 3871 return true; 3872 return false; 3873 } 3874 return true; 3875 } 3876 3877 /* Try to derive a nonnegative or nonzero range out of STMT relying 3878 primarily on generic routines in fold in conjunction with range data. 3879 Store the result in *VR */ 3880 3881 static void 3882 extract_range_basic (value_range *vr, gimple *stmt) 3883 { 3884 bool sop = false; 3885 tree type = gimple_expr_type (stmt); 3886 3887 if (is_gimple_call (stmt)) 3888 { 3889 tree arg; 3890 int mini, maxi, zerov = 0, prec; 3891 enum tree_code subcode = ERROR_MARK; 3892 combined_fn cfn = gimple_call_combined_fn (stmt); 3893 3894 switch (cfn) 3895 { 3896 case CFN_BUILT_IN_CONSTANT_P: 3897 /* If the call is __builtin_constant_p and the argument is a 3898 function parameter resolve it to false. This avoids bogus 3899 array bound warnings. 3900 ??? We could do this as early as inlining is finished. */ 3901 arg = gimple_call_arg (stmt, 0); 3902 if (TREE_CODE (arg) == SSA_NAME 3903 && SSA_NAME_IS_DEFAULT_DEF (arg) 3904 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL 3905 && cfun->after_inlining) 3906 { 3907 set_value_range_to_null (vr, type); 3908 return; 3909 } 3910 break; 3911 /* Both __builtin_ffs* and __builtin_popcount return 3912 [0, prec]. */ 3913 CASE_CFN_FFS: 3914 CASE_CFN_POPCOUNT: 3915 arg = gimple_call_arg (stmt, 0); 3916 prec = TYPE_PRECISION (TREE_TYPE (arg)); 3917 mini = 0; 3918 maxi = prec; 3919 if (TREE_CODE (arg) == SSA_NAME) 3920 { 3921 value_range *vr0 = get_value_range (arg); 3922 /* If arg is non-zero, then ffs or popcount 3923 are non-zero. */ 3924 if (((vr0->type == VR_RANGE 3925 && range_includes_zero_p (vr0->min, vr0->max) == 0) 3926 || (vr0->type == VR_ANTI_RANGE 3927 && range_includes_zero_p (vr0->min, vr0->max) == 1)) 3928 && !is_overflow_infinity (vr0->min) 3929 && !is_overflow_infinity (vr0->max)) 3930 mini = 1; 3931 /* If some high bits are known to be zero, 3932 we can decrease the maximum. */ 3933 if (vr0->type == VR_RANGE 3934 && TREE_CODE (vr0->max) == INTEGER_CST 3935 && !operand_less_p (vr0->min, 3936 build_zero_cst (TREE_TYPE (vr0->min))) 3937 && !is_overflow_infinity (vr0->max)) 3938 maxi = tree_floor_log2 (vr0->max) + 1; 3939 } 3940 goto bitop_builtin; 3941 /* __builtin_parity* returns [0, 1]. */ 3942 CASE_CFN_PARITY: 3943 mini = 0; 3944 maxi = 1; 3945 goto bitop_builtin; 3946 /* __builtin_c[lt]z* return [0, prec-1], except for 3947 when the argument is 0, but that is undefined behavior. 3948 On many targets where the CLZ RTL or optab value is defined 3949 for 0 the value is prec, so include that in the range 3950 by default. */ 3951 CASE_CFN_CLZ: 3952 arg = gimple_call_arg (stmt, 0); 3953 prec = TYPE_PRECISION (TREE_TYPE (arg)); 3954 mini = 0; 3955 maxi = prec; 3956 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg))) 3957 != CODE_FOR_nothing 3958 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)), 3959 zerov) 3960 /* Handle only the single common value. */ 3961 && zerov != prec) 3962 /* Magic value to give up, unless vr0 proves 3963 arg is non-zero. */ 3964 mini = -2; 3965 if (TREE_CODE (arg) == SSA_NAME) 3966 { 3967 value_range *vr0 = get_value_range (arg); 3968 /* From clz of VR_RANGE minimum we can compute 3969 result maximum. */ 3970 if (vr0->type == VR_RANGE 3971 && TREE_CODE (vr0->min) == INTEGER_CST 3972 && !is_overflow_infinity (vr0->min)) 3973 { 3974 maxi = prec - 1 - tree_floor_log2 (vr0->min); 3975 if (maxi != prec) 3976 mini = 0; 3977 } 3978 else if (vr0->type == VR_ANTI_RANGE 3979 && integer_zerop (vr0->min) 3980 && !is_overflow_infinity (vr0->min)) 3981 { 3982 maxi = prec - 1; 3983 mini = 0; 3984 } 3985 if (mini == -2) 3986 break; 3987 /* From clz of VR_RANGE maximum we can compute 3988 result minimum. */ 3989 if (vr0->type == VR_RANGE 3990 && TREE_CODE (vr0->max) == INTEGER_CST 3991 && !is_overflow_infinity (vr0->max)) 3992 { 3993 mini = prec - 1 - tree_floor_log2 (vr0->max); 3994 if (mini == prec) 3995 break; 3996 } 3997 } 3998 if (mini == -2) 3999 break; 4000 goto bitop_builtin; 4001 /* __builtin_ctz* return [0, prec-1], except for 4002 when the argument is 0, but that is undefined behavior. 4003 If there is a ctz optab for this mode and 4004 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range, 4005 otherwise just assume 0 won't be seen. */ 4006 CASE_CFN_CTZ: 4007 arg = gimple_call_arg (stmt, 0); 4008 prec = TYPE_PRECISION (TREE_TYPE (arg)); 4009 mini = 0; 4010 maxi = prec - 1; 4011 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg))) 4012 != CODE_FOR_nothing 4013 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)), 4014 zerov)) 4015 { 4016 /* Handle only the two common values. */ 4017 if (zerov == -1) 4018 mini = -1; 4019 else if (zerov == prec) 4020 maxi = prec; 4021 else 4022 /* Magic value to give up, unless vr0 proves 4023 arg is non-zero. */ 4024 mini = -2; 4025 } 4026 if (TREE_CODE (arg) == SSA_NAME) 4027 { 4028 value_range *vr0 = get_value_range (arg); 4029 /* If arg is non-zero, then use [0, prec - 1]. */ 4030 if (((vr0->type == VR_RANGE 4031 && integer_nonzerop (vr0->min)) 4032 || (vr0->type == VR_ANTI_RANGE 4033 && integer_zerop (vr0->min))) 4034 && !is_overflow_infinity (vr0->min)) 4035 { 4036 mini = 0; 4037 maxi = prec - 1; 4038 } 4039 /* If some high bits are known to be zero, 4040 we can decrease the result maximum. */ 4041 if (vr0->type == VR_RANGE 4042 && TREE_CODE (vr0->max) == INTEGER_CST 4043 && !is_overflow_infinity (vr0->max)) 4044 { 4045 maxi = tree_floor_log2 (vr0->max); 4046 /* For vr0 [0, 0] give up. */ 4047 if (maxi == -1) 4048 break; 4049 } 4050 } 4051 if (mini == -2) 4052 break; 4053 goto bitop_builtin; 4054 /* __builtin_clrsb* returns [0, prec-1]. */ 4055 CASE_CFN_CLRSB: 4056 arg = gimple_call_arg (stmt, 0); 4057 prec = TYPE_PRECISION (TREE_TYPE (arg)); 4058 mini = 0; 4059 maxi = prec - 1; 4060 goto bitop_builtin; 4061 bitop_builtin: 4062 set_value_range (vr, VR_RANGE, build_int_cst (type, mini), 4063 build_int_cst (type, maxi), NULL); 4064 return; 4065 case CFN_UBSAN_CHECK_ADD: 4066 subcode = PLUS_EXPR; 4067 break; 4068 case CFN_UBSAN_CHECK_SUB: 4069 subcode = MINUS_EXPR; 4070 break; 4071 case CFN_UBSAN_CHECK_MUL: 4072 subcode = MULT_EXPR; 4073 break; 4074 case CFN_GOACC_DIM_SIZE: 4075 case CFN_GOACC_DIM_POS: 4076 /* Optimizing these two internal functions helps the loop 4077 optimizer eliminate outer comparisons. Size is [1,N] 4078 and pos is [0,N-1]. */ 4079 { 4080 bool is_pos = cfn == CFN_GOACC_DIM_POS; 4081 int axis = oacc_get_ifn_dim_arg (stmt); 4082 int size = oacc_get_fn_dim_size (current_function_decl, axis); 4083 4084 if (!size) 4085 /* If it's dynamic, the backend might know a hardware 4086 limitation. */ 4087 size = targetm.goacc.dim_limit (axis); 4088 4089 tree type = TREE_TYPE (gimple_call_lhs (stmt)); 4090 set_value_range (vr, VR_RANGE, 4091 build_int_cst (type, is_pos ? 0 : 1), 4092 size ? build_int_cst (type, size - is_pos) 4093 : vrp_val_max (type), NULL); 4094 } 4095 return; 4096 case CFN_BUILT_IN_STRLEN: 4097 if (tree lhs = gimple_call_lhs (stmt)) 4098 if (ptrdiff_type_node 4099 && (TYPE_PRECISION (ptrdiff_type_node) 4100 == TYPE_PRECISION (TREE_TYPE (lhs)))) 4101 { 4102 tree type = TREE_TYPE (lhs); 4103 tree max = vrp_val_max (ptrdiff_type_node); 4104 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max))); 4105 tree range_min = build_zero_cst (type); 4106 tree range_max = wide_int_to_tree (type, wmax - 1); 4107 set_value_range (vr, VR_RANGE, range_min, range_max, NULL); 4108 return; 4109 } 4110 break; 4111 default: 4112 break; 4113 } 4114 if (subcode != ERROR_MARK) 4115 { 4116 bool saved_flag_wrapv = flag_wrapv; 4117 /* Pretend the arithmetics is wrapping. If there is 4118 any overflow, we'll complain, but will actually do 4119 wrapping operation. */ 4120 flag_wrapv = 1; 4121 extract_range_from_binary_expr (vr, subcode, type, 4122 gimple_call_arg (stmt, 0), 4123 gimple_call_arg (stmt, 1)); 4124 flag_wrapv = saved_flag_wrapv; 4125 4126 /* If for both arguments vrp_valueize returned non-NULL, 4127 this should have been already folded and if not, it 4128 wasn't folded because of overflow. Avoid removing the 4129 UBSAN_CHECK_* calls in that case. */ 4130 if (vr->type == VR_RANGE 4131 && (vr->min == vr->max 4132 || operand_equal_p (vr->min, vr->max, 0))) 4133 set_value_range_to_varying (vr); 4134 return; 4135 } 4136 } 4137 /* Handle extraction of the two results (result of arithmetics and 4138 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW 4139 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */ 4140 else if (is_gimple_assign (stmt) 4141 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR 4142 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR) 4143 && INTEGRAL_TYPE_P (type)) 4144 { 4145 enum tree_code code = gimple_assign_rhs_code (stmt); 4146 tree op = gimple_assign_rhs1 (stmt); 4147 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME) 4148 { 4149 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0)); 4150 if (is_gimple_call (g) && gimple_call_internal_p (g)) 4151 { 4152 enum tree_code subcode = ERROR_MARK; 4153 switch (gimple_call_internal_fn (g)) 4154 { 4155 case IFN_ADD_OVERFLOW: 4156 subcode = PLUS_EXPR; 4157 break; 4158 case IFN_SUB_OVERFLOW: 4159 subcode = MINUS_EXPR; 4160 break; 4161 case IFN_MUL_OVERFLOW: 4162 subcode = MULT_EXPR; 4163 break; 4164 case IFN_ATOMIC_COMPARE_EXCHANGE: 4165 if (code == IMAGPART_EXPR) 4166 { 4167 /* This is the boolean return value whether compare and 4168 exchange changed anything or not. */ 4169 set_value_range (vr, VR_RANGE, build_int_cst (type, 0), 4170 build_int_cst (type, 1), NULL); 4171 return; 4172 } 4173 break; 4174 default: 4175 break; 4176 } 4177 if (subcode != ERROR_MARK) 4178 { 4179 tree op0 = gimple_call_arg (g, 0); 4180 tree op1 = gimple_call_arg (g, 1); 4181 if (code == IMAGPART_EXPR) 4182 { 4183 bool ovf = false; 4184 if (check_for_binary_op_overflow (subcode, type, 4185 op0, op1, &ovf)) 4186 set_value_range_to_value (vr, 4187 build_int_cst (type, ovf), 4188 NULL); 4189 else if (TYPE_PRECISION (type) == 1 4190 && !TYPE_UNSIGNED (type)) 4191 set_value_range_to_varying (vr); 4192 else 4193 set_value_range (vr, VR_RANGE, build_int_cst (type, 0), 4194 build_int_cst (type, 1), NULL); 4195 } 4196 else if (types_compatible_p (type, TREE_TYPE (op0)) 4197 && types_compatible_p (type, TREE_TYPE (op1))) 4198 { 4199 bool saved_flag_wrapv = flag_wrapv; 4200 /* Pretend the arithmetics is wrapping. If there is 4201 any overflow, IMAGPART_EXPR will be set. */ 4202 flag_wrapv = 1; 4203 extract_range_from_binary_expr (vr, subcode, type, 4204 op0, op1); 4205 flag_wrapv = saved_flag_wrapv; 4206 } 4207 else 4208 { 4209 value_range vr0 = VR_INITIALIZER; 4210 value_range vr1 = VR_INITIALIZER; 4211 bool saved_flag_wrapv = flag_wrapv; 4212 /* Pretend the arithmetics is wrapping. If there is 4213 any overflow, IMAGPART_EXPR will be set. */ 4214 flag_wrapv = 1; 4215 extract_range_from_unary_expr (&vr0, NOP_EXPR, 4216 type, op0); 4217 extract_range_from_unary_expr (&vr1, NOP_EXPR, 4218 type, op1); 4219 extract_range_from_binary_expr_1 (vr, subcode, type, 4220 &vr0, &vr1); 4221 flag_wrapv = saved_flag_wrapv; 4222 } 4223 return; 4224 } 4225 } 4226 } 4227 } 4228 if (INTEGRAL_TYPE_P (type) 4229 && gimple_stmt_nonnegative_warnv_p (stmt, &sop)) 4230 set_value_range_to_nonnegative (vr, type, 4231 sop || stmt_overflow_infinity (stmt)); 4232 else if (vrp_stmt_computes_nonzero (stmt, &sop) 4233 && !sop) 4234 set_value_range_to_nonnull (vr, type); 4235 else 4236 set_value_range_to_varying (vr); 4237 } 4238 4239 4240 /* Try to compute a useful range out of assignment STMT and store it 4241 in *VR. */ 4242 4243 static void 4244 extract_range_from_assignment (value_range *vr, gassign *stmt) 4245 { 4246 enum tree_code code = gimple_assign_rhs_code (stmt); 4247 4248 if (code == ASSERT_EXPR) 4249 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt)); 4250 else if (code == SSA_NAME) 4251 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt)); 4252 else if (TREE_CODE_CLASS (code) == tcc_binary) 4253 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt), 4254 gimple_expr_type (stmt), 4255 gimple_assign_rhs1 (stmt), 4256 gimple_assign_rhs2 (stmt)); 4257 else if (TREE_CODE_CLASS (code) == tcc_unary) 4258 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt), 4259 gimple_expr_type (stmt), 4260 gimple_assign_rhs1 (stmt)); 4261 else if (code == COND_EXPR) 4262 extract_range_from_cond_expr (vr, stmt); 4263 else if (TREE_CODE_CLASS (code) == tcc_comparison) 4264 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt), 4265 gimple_expr_type (stmt), 4266 gimple_assign_rhs1 (stmt), 4267 gimple_assign_rhs2 (stmt)); 4268 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS 4269 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt))) 4270 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL); 4271 else 4272 set_value_range_to_varying (vr); 4273 4274 if (vr->type == VR_VARYING) 4275 extract_range_basic (vr, stmt); 4276 } 4277 4278 /* Given a range VR, a LOOP and a variable VAR, determine whether it 4279 would be profitable to adjust VR using scalar evolution information 4280 for VAR. If so, update VR with the new limits. */ 4281 4282 static void 4283 adjust_range_with_scev (value_range *vr, struct loop *loop, 4284 gimple *stmt, tree var) 4285 { 4286 tree init, step, chrec, tmin, tmax, min, max, type, tem; 4287 enum ev_direction dir; 4288 4289 /* TODO. Don't adjust anti-ranges. An anti-range may provide 4290 better opportunities than a regular range, but I'm not sure. */ 4291 if (vr->type == VR_ANTI_RANGE) 4292 return; 4293 4294 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var)); 4295 4296 /* Like in PR19590, scev can return a constant function. */ 4297 if (is_gimple_min_invariant (chrec)) 4298 { 4299 set_value_range_to_value (vr, chrec, vr->equiv); 4300 return; 4301 } 4302 4303 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC) 4304 return; 4305 4306 init = initial_condition_in_loop_num (chrec, loop->num); 4307 tem = op_with_constant_singleton_value_range (init); 4308 if (tem) 4309 init = tem; 4310 step = evolution_part_in_loop_num (chrec, loop->num); 4311 tem = op_with_constant_singleton_value_range (step); 4312 if (tem) 4313 step = tem; 4314 4315 /* If STEP is symbolic, we can't know whether INIT will be the 4316 minimum or maximum value in the range. Also, unless INIT is 4317 a simple expression, compare_values and possibly other functions 4318 in tree-vrp won't be able to handle it. */ 4319 if (step == NULL_TREE 4320 || !is_gimple_min_invariant (step) 4321 || !valid_value_p (init)) 4322 return; 4323 4324 dir = scev_direction (chrec); 4325 if (/* Do not adjust ranges if we do not know whether the iv increases 4326 or decreases, ... */ 4327 dir == EV_DIR_UNKNOWN 4328 /* ... or if it may wrap. */ 4329 || scev_probably_wraps_p (NULL_TREE, init, step, stmt, 4330 get_chrec_loop (chrec), true)) 4331 return; 4332 4333 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of 4334 negative_overflow_infinity and positive_overflow_infinity, 4335 because we have concluded that the loop probably does not 4336 wrap. */ 4337 4338 type = TREE_TYPE (var); 4339 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type)) 4340 tmin = lower_bound_in_type (type, type); 4341 else 4342 tmin = TYPE_MIN_VALUE (type); 4343 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type)) 4344 tmax = upper_bound_in_type (type, type); 4345 else 4346 tmax = TYPE_MAX_VALUE (type); 4347 4348 /* Try to use estimated number of iterations for the loop to constrain the 4349 final value in the evolution. */ 4350 if (TREE_CODE (step) == INTEGER_CST 4351 && is_gimple_val (init) 4352 && (TREE_CODE (init) != SSA_NAME 4353 || get_value_range (init)->type == VR_RANGE)) 4354 { 4355 widest_int nit; 4356 4357 /* We are only entering here for loop header PHI nodes, so using 4358 the number of latch executions is the correct thing to use. */ 4359 if (max_loop_iterations (loop, &nit)) 4360 { 4361 value_range maxvr = VR_INITIALIZER; 4362 signop sgn = TYPE_SIGN (TREE_TYPE (step)); 4363 bool overflow; 4364 4365 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn, 4366 &overflow); 4367 /* If the multiplication overflowed we can't do a meaningful 4368 adjustment. Likewise if the result doesn't fit in the type 4369 of the induction variable. For a signed type we have to 4370 check whether the result has the expected signedness which 4371 is that of the step as number of iterations is unsigned. */ 4372 if (!overflow 4373 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init)) 4374 && (sgn == UNSIGNED 4375 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0))) 4376 { 4377 tem = wide_int_to_tree (TREE_TYPE (init), wtmp); 4378 extract_range_from_binary_expr (&maxvr, PLUS_EXPR, 4379 TREE_TYPE (init), init, tem); 4380 /* Likewise if the addition did. */ 4381 if (maxvr.type == VR_RANGE) 4382 { 4383 value_range initvr = VR_INITIALIZER; 4384 4385 if (TREE_CODE (init) == SSA_NAME) 4386 initvr = *(get_value_range (init)); 4387 else if (is_gimple_min_invariant (init)) 4388 set_value_range_to_value (&initvr, init, NULL); 4389 else 4390 return; 4391 4392 /* Check if init + nit * step overflows. Though we checked 4393 scev {init, step}_loop doesn't wrap, it is not enough 4394 because the loop may exit immediately. Overflow could 4395 happen in the plus expression in this case. */ 4396 if ((dir == EV_DIR_DECREASES 4397 && (is_negative_overflow_infinity (maxvr.min) 4398 || compare_values (maxvr.min, initvr.min) != -1)) 4399 || (dir == EV_DIR_GROWS 4400 && (is_positive_overflow_infinity (maxvr.max) 4401 || compare_values (maxvr.max, initvr.max) != 1))) 4402 return; 4403 4404 tmin = maxvr.min; 4405 tmax = maxvr.max; 4406 } 4407 } 4408 } 4409 } 4410 4411 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED) 4412 { 4413 min = tmin; 4414 max = tmax; 4415 4416 /* For VARYING or UNDEFINED ranges, just about anything we get 4417 from scalar evolutions should be better. */ 4418 4419 if (dir == EV_DIR_DECREASES) 4420 max = init; 4421 else 4422 min = init; 4423 } 4424 else if (vr->type == VR_RANGE) 4425 { 4426 min = vr->min; 4427 max = vr->max; 4428 4429 if (dir == EV_DIR_DECREASES) 4430 { 4431 /* INIT is the maximum value. If INIT is lower than VR->MAX 4432 but no smaller than VR->MIN, set VR->MAX to INIT. */ 4433 if (compare_values (init, max) == -1) 4434 max = init; 4435 4436 /* According to the loop information, the variable does not 4437 overflow. If we think it does, probably because of an 4438 overflow due to arithmetic on a different INF value, 4439 reset now. */ 4440 if (is_negative_overflow_infinity (min) 4441 || compare_values (min, tmin) == -1) 4442 min = tmin; 4443 4444 } 4445 else 4446 { 4447 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */ 4448 if (compare_values (init, min) == 1) 4449 min = init; 4450 4451 if (is_positive_overflow_infinity (max) 4452 || compare_values (tmax, max) == -1) 4453 max = tmax; 4454 } 4455 } 4456 else 4457 return; 4458 4459 /* If we just created an invalid range with the minimum 4460 greater than the maximum, we fail conservatively. 4461 This should happen only in unreachable 4462 parts of code, or for invalid programs. */ 4463 if (compare_values (min, max) == 1 4464 || (is_negative_overflow_infinity (min) 4465 && is_positive_overflow_infinity (max))) 4466 return; 4467 4468 /* Even for valid range info, sometimes overflow flag will leak in. 4469 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we 4470 drop them except for +-overflow_infinity which still need special 4471 handling in vrp pass. */ 4472 if (TREE_OVERFLOW_P (min) 4473 && ! is_negative_overflow_infinity (min)) 4474 min = drop_tree_overflow (min); 4475 if (TREE_OVERFLOW_P (max) 4476 && ! is_positive_overflow_infinity (max)) 4477 max = drop_tree_overflow (max); 4478 4479 set_value_range (vr, VR_RANGE, min, max, vr->equiv); 4480 } 4481 4482 4483 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP: 4484 4485 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for 4486 all the values in the ranges. 4487 4488 - Return BOOLEAN_FALSE_NODE if the comparison always returns false. 4489 4490 - Return NULL_TREE if it is not always possible to determine the 4491 value of the comparison. 4492 4493 Also set *STRICT_OVERFLOW_P to indicate whether a range with an 4494 overflow infinity was used in the test. */ 4495 4496 4497 static tree 4498 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1, 4499 bool *strict_overflow_p) 4500 { 4501 /* VARYING or UNDEFINED ranges cannot be compared. */ 4502 if (vr0->type == VR_VARYING 4503 || vr0->type == VR_UNDEFINED 4504 || vr1->type == VR_VARYING 4505 || vr1->type == VR_UNDEFINED) 4506 return NULL_TREE; 4507 4508 /* Anti-ranges need to be handled separately. */ 4509 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE) 4510 { 4511 /* If both are anti-ranges, then we cannot compute any 4512 comparison. */ 4513 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE) 4514 return NULL_TREE; 4515 4516 /* These comparisons are never statically computable. */ 4517 if (comp == GT_EXPR 4518 || comp == GE_EXPR 4519 || comp == LT_EXPR 4520 || comp == LE_EXPR) 4521 return NULL_TREE; 4522 4523 /* Equality can be computed only between a range and an 4524 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */ 4525 if (vr0->type == VR_RANGE) 4526 { 4527 /* To simplify processing, make VR0 the anti-range. */ 4528 value_range *tmp = vr0; 4529 vr0 = vr1; 4530 vr1 = tmp; 4531 } 4532 4533 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR); 4534 4535 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0 4536 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0) 4537 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node; 4538 4539 return NULL_TREE; 4540 } 4541 4542 if (!usable_range_p (vr0, strict_overflow_p) 4543 || !usable_range_p (vr1, strict_overflow_p)) 4544 return NULL_TREE; 4545 4546 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the 4547 operands around and change the comparison code. */ 4548 if (comp == GT_EXPR || comp == GE_EXPR) 4549 { 4550 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR; 4551 std::swap (vr0, vr1); 4552 } 4553 4554 if (comp == EQ_EXPR) 4555 { 4556 /* Equality may only be computed if both ranges represent 4557 exactly one value. */ 4558 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0 4559 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0) 4560 { 4561 int cmp_min = compare_values_warnv (vr0->min, vr1->min, 4562 strict_overflow_p); 4563 int cmp_max = compare_values_warnv (vr0->max, vr1->max, 4564 strict_overflow_p); 4565 if (cmp_min == 0 && cmp_max == 0) 4566 return boolean_true_node; 4567 else if (cmp_min != -2 && cmp_max != -2) 4568 return boolean_false_node; 4569 } 4570 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */ 4571 else if (compare_values_warnv (vr0->min, vr1->max, 4572 strict_overflow_p) == 1 4573 || compare_values_warnv (vr1->min, vr0->max, 4574 strict_overflow_p) == 1) 4575 return boolean_false_node; 4576 4577 return NULL_TREE; 4578 } 4579 else if (comp == NE_EXPR) 4580 { 4581 int cmp1, cmp2; 4582 4583 /* If VR0 is completely to the left or completely to the right 4584 of VR1, they are always different. Notice that we need to 4585 make sure that both comparisons yield similar results to 4586 avoid comparing values that cannot be compared at 4587 compile-time. */ 4588 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p); 4589 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p); 4590 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1)) 4591 return boolean_true_node; 4592 4593 /* If VR0 and VR1 represent a single value and are identical, 4594 return false. */ 4595 else if (compare_values_warnv (vr0->min, vr0->max, 4596 strict_overflow_p) == 0 4597 && compare_values_warnv (vr1->min, vr1->max, 4598 strict_overflow_p) == 0 4599 && compare_values_warnv (vr0->min, vr1->min, 4600 strict_overflow_p) == 0 4601 && compare_values_warnv (vr0->max, vr1->max, 4602 strict_overflow_p) == 0) 4603 return boolean_false_node; 4604 4605 /* Otherwise, they may or may not be different. */ 4606 else 4607 return NULL_TREE; 4608 } 4609 else if (comp == LT_EXPR || comp == LE_EXPR) 4610 { 4611 int tst; 4612 4613 /* If VR0 is to the left of VR1, return true. */ 4614 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p); 4615 if ((comp == LT_EXPR && tst == -1) 4616 || (comp == LE_EXPR && (tst == -1 || tst == 0))) 4617 { 4618 if (overflow_infinity_range_p (vr0) 4619 || overflow_infinity_range_p (vr1)) 4620 *strict_overflow_p = true; 4621 return boolean_true_node; 4622 } 4623 4624 /* If VR0 is to the right of VR1, return false. */ 4625 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p); 4626 if ((comp == LT_EXPR && (tst == 0 || tst == 1)) 4627 || (comp == LE_EXPR && tst == 1)) 4628 { 4629 if (overflow_infinity_range_p (vr0) 4630 || overflow_infinity_range_p (vr1)) 4631 *strict_overflow_p = true; 4632 return boolean_false_node; 4633 } 4634 4635 /* Otherwise, we don't know. */ 4636 return NULL_TREE; 4637 } 4638 4639 gcc_unreachable (); 4640 } 4641 4642 4643 /* Given a value range VR, a value VAL and a comparison code COMP, return 4644 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the 4645 values in VR. Return BOOLEAN_FALSE_NODE if the comparison 4646 always returns false. Return NULL_TREE if it is not always 4647 possible to determine the value of the comparison. Also set 4648 *STRICT_OVERFLOW_P to indicate whether a range with an overflow 4649 infinity was used in the test. */ 4650 4651 static tree 4652 compare_range_with_value (enum tree_code comp, value_range *vr, tree val, 4653 bool *strict_overflow_p) 4654 { 4655 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED) 4656 return NULL_TREE; 4657 4658 /* Anti-ranges need to be handled separately. */ 4659 if (vr->type == VR_ANTI_RANGE) 4660 { 4661 /* For anti-ranges, the only predicates that we can compute at 4662 compile time are equality and inequality. */ 4663 if (comp == GT_EXPR 4664 || comp == GE_EXPR 4665 || comp == LT_EXPR 4666 || comp == LE_EXPR) 4667 return NULL_TREE; 4668 4669 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */ 4670 if (value_inside_range (val, vr->min, vr->max) == 1) 4671 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node; 4672 4673 return NULL_TREE; 4674 } 4675 4676 if (!usable_range_p (vr, strict_overflow_p)) 4677 return NULL_TREE; 4678 4679 if (comp == EQ_EXPR) 4680 { 4681 /* EQ_EXPR may only be computed if VR represents exactly 4682 one value. */ 4683 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0) 4684 { 4685 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p); 4686 if (cmp == 0) 4687 return boolean_true_node; 4688 else if (cmp == -1 || cmp == 1 || cmp == 2) 4689 return boolean_false_node; 4690 } 4691 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1 4692 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1) 4693 return boolean_false_node; 4694 4695 return NULL_TREE; 4696 } 4697 else if (comp == NE_EXPR) 4698 { 4699 /* If VAL is not inside VR, then they are always different. */ 4700 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1 4701 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1) 4702 return boolean_true_node; 4703 4704 /* If VR represents exactly one value equal to VAL, then return 4705 false. */ 4706 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0 4707 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0) 4708 return boolean_false_node; 4709 4710 /* Otherwise, they may or may not be different. */ 4711 return NULL_TREE; 4712 } 4713 else if (comp == LT_EXPR || comp == LE_EXPR) 4714 { 4715 int tst; 4716 4717 /* If VR is to the left of VAL, return true. */ 4718 tst = compare_values_warnv (vr->max, val, strict_overflow_p); 4719 if ((comp == LT_EXPR && tst == -1) 4720 || (comp == LE_EXPR && (tst == -1 || tst == 0))) 4721 { 4722 if (overflow_infinity_range_p (vr)) 4723 *strict_overflow_p = true; 4724 return boolean_true_node; 4725 } 4726 4727 /* If VR is to the right of VAL, return false. */ 4728 tst = compare_values_warnv (vr->min, val, strict_overflow_p); 4729 if ((comp == LT_EXPR && (tst == 0 || tst == 1)) 4730 || (comp == LE_EXPR && tst == 1)) 4731 { 4732 if (overflow_infinity_range_p (vr)) 4733 *strict_overflow_p = true; 4734 return boolean_false_node; 4735 } 4736 4737 /* Otherwise, we don't know. */ 4738 return NULL_TREE; 4739 } 4740 else if (comp == GT_EXPR || comp == GE_EXPR) 4741 { 4742 int tst; 4743 4744 /* If VR is to the right of VAL, return true. */ 4745 tst = compare_values_warnv (vr->min, val, strict_overflow_p); 4746 if ((comp == GT_EXPR && tst == 1) 4747 || (comp == GE_EXPR && (tst == 0 || tst == 1))) 4748 { 4749 if (overflow_infinity_range_p (vr)) 4750 *strict_overflow_p = true; 4751 return boolean_true_node; 4752 } 4753 4754 /* If VR is to the left of VAL, return false. */ 4755 tst = compare_values_warnv (vr->max, val, strict_overflow_p); 4756 if ((comp == GT_EXPR && (tst == -1 || tst == 0)) 4757 || (comp == GE_EXPR && tst == -1)) 4758 { 4759 if (overflow_infinity_range_p (vr)) 4760 *strict_overflow_p = true; 4761 return boolean_false_node; 4762 } 4763 4764 /* Otherwise, we don't know. */ 4765 return NULL_TREE; 4766 } 4767 4768 gcc_unreachable (); 4769 } 4770 4771 4772 /* Debugging dumps. */ 4773 4774 void dump_value_range (FILE *, const value_range *); 4775 void debug_value_range (value_range *); 4776 void dump_all_value_ranges (FILE *); 4777 void debug_all_value_ranges (void); 4778 void dump_vr_equiv (FILE *, bitmap); 4779 void debug_vr_equiv (bitmap); 4780 4781 4782 /* Dump value range VR to FILE. */ 4783 4784 void 4785 dump_value_range (FILE *file, const value_range *vr) 4786 { 4787 if (vr == NULL) 4788 fprintf (file, "[]"); 4789 else if (vr->type == VR_UNDEFINED) 4790 fprintf (file, "UNDEFINED"); 4791 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE) 4792 { 4793 tree type = TREE_TYPE (vr->min); 4794 4795 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : ""); 4796 4797 if (is_negative_overflow_infinity (vr->min)) 4798 fprintf (file, "-INF(OVF)"); 4799 else if (INTEGRAL_TYPE_P (type) 4800 && !TYPE_UNSIGNED (type) 4801 && vrp_val_is_min (vr->min)) 4802 fprintf (file, "-INF"); 4803 else 4804 print_generic_expr (file, vr->min, 0); 4805 4806 fprintf (file, ", "); 4807 4808 if (is_positive_overflow_infinity (vr->max)) 4809 fprintf (file, "+INF(OVF)"); 4810 else if (INTEGRAL_TYPE_P (type) 4811 && vrp_val_is_max (vr->max)) 4812 fprintf (file, "+INF"); 4813 else 4814 print_generic_expr (file, vr->max, 0); 4815 4816 fprintf (file, "]"); 4817 4818 if (vr->equiv) 4819 { 4820 bitmap_iterator bi; 4821 unsigned i, c = 0; 4822 4823 fprintf (file, " EQUIVALENCES: { "); 4824 4825 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi) 4826 { 4827 print_generic_expr (file, ssa_name (i), 0); 4828 fprintf (file, " "); 4829 c++; 4830 } 4831 4832 fprintf (file, "} (%u elements)", c); 4833 } 4834 } 4835 else if (vr->type == VR_VARYING) 4836 fprintf (file, "VARYING"); 4837 else 4838 fprintf (file, "INVALID RANGE"); 4839 } 4840 4841 4842 /* Dump value range VR to stderr. */ 4843 4844 DEBUG_FUNCTION void 4845 debug_value_range (value_range *vr) 4846 { 4847 dump_value_range (stderr, vr); 4848 fprintf (stderr, "\n"); 4849 } 4850 4851 4852 /* Dump value ranges of all SSA_NAMEs to FILE. */ 4853 4854 void 4855 dump_all_value_ranges (FILE *file) 4856 { 4857 size_t i; 4858 4859 for (i = 0; i < num_vr_values; i++) 4860 { 4861 if (vr_value[i]) 4862 { 4863 print_generic_expr (file, ssa_name (i), 0); 4864 fprintf (file, ": "); 4865 dump_value_range (file, vr_value[i]); 4866 fprintf (file, "\n"); 4867 } 4868 } 4869 4870 fprintf (file, "\n"); 4871 } 4872 4873 4874 /* Dump all value ranges to stderr. */ 4875 4876 DEBUG_FUNCTION void 4877 debug_all_value_ranges (void) 4878 { 4879 dump_all_value_ranges (stderr); 4880 } 4881 4882 4883 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V, 4884 create a new SSA name N and return the assertion assignment 4885 'N = ASSERT_EXPR <V, V OP W>'. */ 4886 4887 static gimple * 4888 build_assert_expr_for (tree cond, tree v) 4889 { 4890 tree a; 4891 gassign *assertion; 4892 4893 gcc_assert (TREE_CODE (v) == SSA_NAME 4894 && COMPARISON_CLASS_P (cond)); 4895 4896 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond); 4897 assertion = gimple_build_assign (NULL_TREE, a); 4898 4899 /* The new ASSERT_EXPR, creates a new SSA name that replaces the 4900 operand of the ASSERT_EXPR. Create it so the new name and the old one 4901 are registered in the replacement table so that we can fix the SSA web 4902 after adding all the ASSERT_EXPRs. */ 4903 tree new_def = create_new_def_for (v, assertion, NULL); 4904 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain 4905 given we have to be able to fully propagate those out to re-create 4906 valid SSA when removing the asserts. */ 4907 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v)) 4908 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def) = 1; 4909 4910 return assertion; 4911 } 4912 4913 4914 /* Return false if EXPR is a predicate expression involving floating 4915 point values. */ 4916 4917 static inline bool 4918 fp_predicate (gimple *stmt) 4919 { 4920 GIMPLE_CHECK (stmt, GIMPLE_COND); 4921 4922 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt))); 4923 } 4924 4925 /* If the range of values taken by OP can be inferred after STMT executes, 4926 return the comparison code (COMP_CODE_P) and value (VAL_P) that 4927 describes the inferred range. Return true if a range could be 4928 inferred. */ 4929 4930 static bool 4931 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p) 4932 { 4933 *val_p = NULL_TREE; 4934 *comp_code_p = ERROR_MARK; 4935 4936 /* Do not attempt to infer anything in names that flow through 4937 abnormal edges. */ 4938 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op)) 4939 return false; 4940 4941 /* If STMT is the last statement of a basic block with no normal 4942 successors, there is no point inferring anything about any of its 4943 operands. We would not be able to find a proper insertion point 4944 for the assertion, anyway. */ 4945 if (stmt_ends_bb_p (stmt)) 4946 { 4947 edge_iterator ei; 4948 edge e; 4949 4950 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs) 4951 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH))) 4952 break; 4953 if (e == NULL) 4954 return false; 4955 } 4956 4957 if (infer_nonnull_range (stmt, op)) 4958 { 4959 *val_p = build_int_cst (TREE_TYPE (op), 0); 4960 *comp_code_p = NE_EXPR; 4961 return true; 4962 } 4963 4964 return false; 4965 } 4966 4967 4968 void dump_asserts_for (FILE *, tree); 4969 void debug_asserts_for (tree); 4970 void dump_all_asserts (FILE *); 4971 void debug_all_asserts (void); 4972 4973 /* Dump all the registered assertions for NAME to FILE. */ 4974 4975 void 4976 dump_asserts_for (FILE *file, tree name) 4977 { 4978 assert_locus *loc; 4979 4980 fprintf (file, "Assertions to be inserted for "); 4981 print_generic_expr (file, name, 0); 4982 fprintf (file, "\n"); 4983 4984 loc = asserts_for[SSA_NAME_VERSION (name)]; 4985 while (loc) 4986 { 4987 fprintf (file, "\t"); 4988 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0); 4989 fprintf (file, "\n\tBB #%d", loc->bb->index); 4990 if (loc->e) 4991 { 4992 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index, 4993 loc->e->dest->index); 4994 dump_edge_info (file, loc->e, dump_flags, 0); 4995 } 4996 fprintf (file, "\n\tPREDICATE: "); 4997 print_generic_expr (file, loc->expr, 0); 4998 fprintf (file, " %s ", get_tree_code_name (loc->comp_code)); 4999 print_generic_expr (file, loc->val, 0); 5000 fprintf (file, "\n\n"); 5001 loc = loc->next; 5002 } 5003 5004 fprintf (file, "\n"); 5005 } 5006 5007 5008 /* Dump all the registered assertions for NAME to stderr. */ 5009 5010 DEBUG_FUNCTION void 5011 debug_asserts_for (tree name) 5012 { 5013 dump_asserts_for (stderr, name); 5014 } 5015 5016 5017 /* Dump all the registered assertions for all the names to FILE. */ 5018 5019 void 5020 dump_all_asserts (FILE *file) 5021 { 5022 unsigned i; 5023 bitmap_iterator bi; 5024 5025 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n"); 5026 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi) 5027 dump_asserts_for (file, ssa_name (i)); 5028 fprintf (file, "\n"); 5029 } 5030 5031 5032 /* Dump all the registered assertions for all the names to stderr. */ 5033 5034 DEBUG_FUNCTION void 5035 debug_all_asserts (void) 5036 { 5037 dump_all_asserts (stderr); 5038 } 5039 5040 5041 /* If NAME doesn't have an ASSERT_EXPR registered for asserting 5042 'EXPR COMP_CODE VAL' at a location that dominates block BB or 5043 E->DEST, then register this location as a possible insertion point 5044 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>. 5045 5046 BB, E and SI provide the exact insertion point for the new 5047 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted 5048 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on 5049 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E 5050 must not be NULL. */ 5051 5052 static void 5053 register_new_assert_for (tree name, tree expr, 5054 enum tree_code comp_code, 5055 tree val, 5056 basic_block bb, 5057 edge e, 5058 gimple_stmt_iterator si) 5059 { 5060 assert_locus *n, *loc, *last_loc; 5061 basic_block dest_bb; 5062 5063 gcc_checking_assert (bb == NULL || e == NULL); 5064 5065 if (e == NULL) 5066 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND 5067 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH); 5068 5069 /* Never build an assert comparing against an integer constant with 5070 TREE_OVERFLOW set. This confuses our undefined overflow warning 5071 machinery. */ 5072 if (TREE_OVERFLOW_P (val)) 5073 val = drop_tree_overflow (val); 5074 5075 /* The new assertion A will be inserted at BB or E. We need to 5076 determine if the new location is dominated by a previously 5077 registered location for A. If we are doing an edge insertion, 5078 assume that A will be inserted at E->DEST. Note that this is not 5079 necessarily true. 5080 5081 If E is a critical edge, it will be split. But even if E is 5082 split, the new block will dominate the same set of blocks that 5083 E->DEST dominates. 5084 5085 The reverse, however, is not true, blocks dominated by E->DEST 5086 will not be dominated by the new block created to split E. So, 5087 if the insertion location is on a critical edge, we will not use 5088 the new location to move another assertion previously registered 5089 at a block dominated by E->DEST. */ 5090 dest_bb = (bb) ? bb : e->dest; 5091 5092 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and 5093 VAL at a block dominating DEST_BB, then we don't need to insert a new 5094 one. Similarly, if the same assertion already exists at a block 5095 dominated by DEST_BB and the new location is not on a critical 5096 edge, then update the existing location for the assertion (i.e., 5097 move the assertion up in the dominance tree). 5098 5099 Note, this is implemented as a simple linked list because there 5100 should not be more than a handful of assertions registered per 5101 name. If this becomes a performance problem, a table hashed by 5102 COMP_CODE and VAL could be implemented. */ 5103 loc = asserts_for[SSA_NAME_VERSION (name)]; 5104 last_loc = loc; 5105 while (loc) 5106 { 5107 if (loc->comp_code == comp_code 5108 && (loc->val == val 5109 || operand_equal_p (loc->val, val, 0)) 5110 && (loc->expr == expr 5111 || operand_equal_p (loc->expr, expr, 0))) 5112 { 5113 /* If E is not a critical edge and DEST_BB 5114 dominates the existing location for the assertion, move 5115 the assertion up in the dominance tree by updating its 5116 location information. */ 5117 if ((e == NULL || !EDGE_CRITICAL_P (e)) 5118 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb)) 5119 { 5120 loc->bb = dest_bb; 5121 loc->e = e; 5122 loc->si = si; 5123 return; 5124 } 5125 } 5126 5127 /* Update the last node of the list and move to the next one. */ 5128 last_loc = loc; 5129 loc = loc->next; 5130 } 5131 5132 /* If we didn't find an assertion already registered for 5133 NAME COMP_CODE VAL, add a new one at the end of the list of 5134 assertions associated with NAME. */ 5135 n = XNEW (struct assert_locus); 5136 n->bb = dest_bb; 5137 n->e = e; 5138 n->si = si; 5139 n->comp_code = comp_code; 5140 n->val = val; 5141 n->expr = expr; 5142 n->next = NULL; 5143 5144 if (last_loc) 5145 last_loc->next = n; 5146 else 5147 asserts_for[SSA_NAME_VERSION (name)] = n; 5148 5149 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name)); 5150 } 5151 5152 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME. 5153 Extract a suitable test code and value and store them into *CODE_P and 5154 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P. 5155 5156 If no extraction was possible, return FALSE, otherwise return TRUE. 5157 5158 If INVERT is true, then we invert the result stored into *CODE_P. */ 5159 5160 static bool 5161 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code, 5162 tree cond_op0, tree cond_op1, 5163 bool invert, enum tree_code *code_p, 5164 tree *val_p) 5165 { 5166 enum tree_code comp_code; 5167 tree val; 5168 5169 /* Otherwise, we have a comparison of the form NAME COMP VAL 5170 or VAL COMP NAME. */ 5171 if (name == cond_op1) 5172 { 5173 /* If the predicate is of the form VAL COMP NAME, flip 5174 COMP around because we need to register NAME as the 5175 first operand in the predicate. */ 5176 comp_code = swap_tree_comparison (cond_code); 5177 val = cond_op0; 5178 } 5179 else if (name == cond_op0) 5180 { 5181 /* The comparison is of the form NAME COMP VAL, so the 5182 comparison code remains unchanged. */ 5183 comp_code = cond_code; 5184 val = cond_op1; 5185 } 5186 else 5187 gcc_unreachable (); 5188 5189 /* Invert the comparison code as necessary. */ 5190 if (invert) 5191 comp_code = invert_tree_comparison (comp_code, 0); 5192 5193 /* VRP only handles integral and pointer types. */ 5194 if (! INTEGRAL_TYPE_P (TREE_TYPE (val)) 5195 && ! POINTER_TYPE_P (TREE_TYPE (val))) 5196 return false; 5197 5198 /* Do not register always-false predicates. 5199 FIXME: this works around a limitation in fold() when dealing with 5200 enumerations. Given 'enum { N1, N2 } x;', fold will not 5201 fold 'if (x > N2)' to 'if (0)'. */ 5202 if ((comp_code == GT_EXPR || comp_code == LT_EXPR) 5203 && INTEGRAL_TYPE_P (TREE_TYPE (val))) 5204 { 5205 tree min = TYPE_MIN_VALUE (TREE_TYPE (val)); 5206 tree max = TYPE_MAX_VALUE (TREE_TYPE (val)); 5207 5208 if (comp_code == GT_EXPR 5209 && (!max 5210 || compare_values (val, max) == 0)) 5211 return false; 5212 5213 if (comp_code == LT_EXPR 5214 && (!min 5215 || compare_values (val, min) == 0)) 5216 return false; 5217 } 5218 *code_p = comp_code; 5219 *val_p = val; 5220 return true; 5221 } 5222 5223 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any 5224 (otherwise return VAL). VAL and MASK must be zero-extended for 5225 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT 5226 (to transform signed values into unsigned) and at the end xor 5227 SGNBIT back. */ 5228 5229 static wide_int 5230 masked_increment (const wide_int &val_in, const wide_int &mask, 5231 const wide_int &sgnbit, unsigned int prec) 5232 { 5233 wide_int bit = wi::one (prec), res; 5234 unsigned int i; 5235 5236 wide_int val = val_in ^ sgnbit; 5237 for (i = 0; i < prec; i++, bit += bit) 5238 { 5239 res = mask; 5240 if ((res & bit) == 0) 5241 continue; 5242 res = bit - 1; 5243 res = (val + bit).and_not (res); 5244 res &= mask; 5245 if (wi::gtu_p (res, val)) 5246 return res ^ sgnbit; 5247 } 5248 return val ^ sgnbit; 5249 } 5250 5251 /* Helper for overflow_comparison_p 5252 5253 OP0 CODE OP1 is a comparison. Examine the comparison and potentially 5254 OP1's defining statement to see if it ultimately has the form 5255 OP0 CODE (OP0 PLUS INTEGER_CST) 5256 5257 If so, return TRUE indicating this is an overflow test and store into 5258 *NEW_CST an updated constant that can be used in a narrowed range test. 5259 5260 REVERSED indicates if the comparison was originally: 5261 5262 OP1 CODE' OP0. 5263 5264 This affects how we build the updated constant. */ 5265 5266 static bool 5267 overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1, 5268 bool follow_assert_exprs, bool reversed, tree *new_cst) 5269 { 5270 /* See if this is a relational operation between two SSA_NAMES with 5271 unsigned, overflow wrapping values. If so, check it more deeply. */ 5272 if ((code == LT_EXPR || code == LE_EXPR 5273 || code == GE_EXPR || code == GT_EXPR) 5274 && TREE_CODE (op0) == SSA_NAME 5275 && TREE_CODE (op1) == SSA_NAME 5276 && INTEGRAL_TYPE_P (TREE_TYPE (op0)) 5277 && TYPE_UNSIGNED (TREE_TYPE (op0)) 5278 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))) 5279 { 5280 gimple *op1_def = SSA_NAME_DEF_STMT (op1); 5281 5282 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */ 5283 if (follow_assert_exprs) 5284 { 5285 while (gimple_assign_single_p (op1_def) 5286 && TREE_CODE (gimple_assign_rhs1 (op1_def)) == ASSERT_EXPR) 5287 { 5288 op1 = TREE_OPERAND (gimple_assign_rhs1 (op1_def), 0); 5289 if (TREE_CODE (op1) != SSA_NAME) 5290 break; 5291 op1_def = SSA_NAME_DEF_STMT (op1); 5292 } 5293 } 5294 5295 /* Now look at the defining statement of OP1 to see if it adds 5296 or subtracts a nonzero constant from another operand. */ 5297 if (op1_def 5298 && is_gimple_assign (op1_def) 5299 && gimple_assign_rhs_code (op1_def) == PLUS_EXPR 5300 && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST 5301 && !integer_zerop (gimple_assign_rhs2 (op1_def))) 5302 { 5303 tree target = gimple_assign_rhs1 (op1_def); 5304 5305 /* If requested, follow ASSERT_EXPRs backwards for op0 looking 5306 for one where TARGET appears on the RHS. */ 5307 if (follow_assert_exprs) 5308 { 5309 /* Now see if that "other operand" is op0, following the chain 5310 of ASSERT_EXPRs if necessary. */ 5311 gimple *op0_def = SSA_NAME_DEF_STMT (op0); 5312 while (op0 != target 5313 && gimple_assign_single_p (op0_def) 5314 && TREE_CODE (gimple_assign_rhs1 (op0_def)) == ASSERT_EXPR) 5315 { 5316 op0 = TREE_OPERAND (gimple_assign_rhs1 (op0_def), 0); 5317 if (TREE_CODE (op0) != SSA_NAME) 5318 break; 5319 op0_def = SSA_NAME_DEF_STMT (op0); 5320 } 5321 } 5322 5323 /* If we did not find our target SSA_NAME, then this is not 5324 an overflow test. */ 5325 if (op0 != target) 5326 return false; 5327 5328 tree type = TREE_TYPE (op0); 5329 wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED); 5330 tree inc = gimple_assign_rhs2 (op1_def); 5331 if (reversed) 5332 *new_cst = wide_int_to_tree (type, max + inc); 5333 else 5334 *new_cst = wide_int_to_tree (type, max - inc); 5335 return true; 5336 } 5337 } 5338 return false; 5339 } 5340 5341 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially 5342 OP1's defining statement to see if it ultimately has the form 5343 OP0 CODE (OP0 PLUS INTEGER_CST) 5344 5345 If so, return TRUE indicating this is an overflow test and store into 5346 *NEW_CST an updated constant that can be used in a narrowed range test. 5347 5348 These statements are left as-is in the IL to facilitate discovery of 5349 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But 5350 the alternate range representation is often useful within VRP. */ 5351 5352 static bool 5353 overflow_comparison_p (tree_code code, tree name, tree val, 5354 bool use_equiv_p, tree *new_cst) 5355 { 5356 if (overflow_comparison_p_1 (code, name, val, use_equiv_p, false, new_cst)) 5357 return true; 5358 return overflow_comparison_p_1 (swap_tree_comparison (code), val, name, 5359 use_equiv_p, true, new_cst); 5360 } 5361 5362 5363 /* Try to register an edge assertion for SSA name NAME on edge E for 5364 the condition COND contributing to the conditional jump pointed to by BSI. 5365 Invert the condition COND if INVERT is true. */ 5366 5367 static void 5368 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi, 5369 enum tree_code cond_code, 5370 tree cond_op0, tree cond_op1, bool invert) 5371 { 5372 tree val; 5373 enum tree_code comp_code; 5374 5375 if (!extract_code_and_val_from_cond_with_ops (name, cond_code, 5376 cond_op0, 5377 cond_op1, 5378 invert, &comp_code, &val)) 5379 return; 5380 5381 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph 5382 reachable from E. */ 5383 if (live_on_edge (e, name)) 5384 { 5385 tree x; 5386 if (overflow_comparison_p (comp_code, name, val, false, &x)) 5387 { 5388 enum tree_code new_code 5389 = ((comp_code == GT_EXPR || comp_code == GE_EXPR) 5390 ? GT_EXPR : LE_EXPR); 5391 register_new_assert_for (name, name, new_code, x, NULL, e, bsi); 5392 } 5393 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi); 5394 } 5395 5396 /* In the case of NAME <= CST and NAME being defined as 5397 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2 5398 and NAME2 <= CST - CST2. We can do the same for NAME > CST. 5399 This catches range and anti-range tests. */ 5400 if ((comp_code == LE_EXPR 5401 || comp_code == GT_EXPR) 5402 && TREE_CODE (val) == INTEGER_CST 5403 && TYPE_UNSIGNED (TREE_TYPE (val))) 5404 { 5405 gimple *def_stmt = SSA_NAME_DEF_STMT (name); 5406 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE; 5407 5408 /* Extract CST2 from the (optional) addition. */ 5409 if (is_gimple_assign (def_stmt) 5410 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR) 5411 { 5412 name2 = gimple_assign_rhs1 (def_stmt); 5413 cst2 = gimple_assign_rhs2 (def_stmt); 5414 if (TREE_CODE (name2) == SSA_NAME 5415 && TREE_CODE (cst2) == INTEGER_CST) 5416 def_stmt = SSA_NAME_DEF_STMT (name2); 5417 } 5418 5419 /* Extract NAME2 from the (optional) sign-changing cast. */ 5420 if (gimple_assign_cast_p (def_stmt)) 5421 { 5422 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)) 5423 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) 5424 && (TYPE_PRECISION (gimple_expr_type (def_stmt)) 5425 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))) 5426 name3 = gimple_assign_rhs1 (def_stmt); 5427 } 5428 5429 /* If name3 is used later, create an ASSERT_EXPR for it. */ 5430 if (name3 != NULL_TREE 5431 && TREE_CODE (name3) == SSA_NAME 5432 && (cst2 == NULL_TREE 5433 || TREE_CODE (cst2) == INTEGER_CST) 5434 && INTEGRAL_TYPE_P (TREE_TYPE (name3)) 5435 && live_on_edge (e, name3)) 5436 { 5437 tree tmp; 5438 5439 /* Build an expression for the range test. */ 5440 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3); 5441 if (cst2 != NULL_TREE) 5442 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2); 5443 5444 if (dump_file) 5445 { 5446 fprintf (dump_file, "Adding assert for "); 5447 print_generic_expr (dump_file, name3, 0); 5448 fprintf (dump_file, " from "); 5449 print_generic_expr (dump_file, tmp, 0); 5450 fprintf (dump_file, "\n"); 5451 } 5452 5453 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi); 5454 } 5455 5456 /* If name2 is used later, create an ASSERT_EXPR for it. */ 5457 if (name2 != NULL_TREE 5458 && TREE_CODE (name2) == SSA_NAME 5459 && TREE_CODE (cst2) == INTEGER_CST 5460 && INTEGRAL_TYPE_P (TREE_TYPE (name2)) 5461 && live_on_edge (e, name2)) 5462 { 5463 tree tmp; 5464 5465 /* Build an expression for the range test. */ 5466 tmp = name2; 5467 if (TREE_TYPE (name) != TREE_TYPE (name2)) 5468 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp); 5469 if (cst2 != NULL_TREE) 5470 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2); 5471 5472 if (dump_file) 5473 { 5474 fprintf (dump_file, "Adding assert for "); 5475 print_generic_expr (dump_file, name2, 0); 5476 fprintf (dump_file, " from "); 5477 print_generic_expr (dump_file, tmp, 0); 5478 fprintf (dump_file, "\n"); 5479 } 5480 5481 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi); 5482 } 5483 } 5484 5485 /* In the case of post-in/decrement tests like if (i++) ... and uses 5486 of the in/decremented value on the edge the extra name we want to 5487 assert for is not on the def chain of the name compared. Instead 5488 it is in the set of use stmts. 5489 Similar cases happen for conversions that were simplified through 5490 fold_{sign_changed,widened}_comparison. */ 5491 if ((comp_code == NE_EXPR 5492 || comp_code == EQ_EXPR) 5493 && TREE_CODE (val) == INTEGER_CST) 5494 { 5495 imm_use_iterator ui; 5496 gimple *use_stmt; 5497 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name) 5498 { 5499 if (!is_gimple_assign (use_stmt)) 5500 continue; 5501 5502 /* Cut off to use-stmts that are dominating the predecessor. */ 5503 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt))) 5504 continue; 5505 5506 tree name2 = gimple_assign_lhs (use_stmt); 5507 if (TREE_CODE (name2) != SSA_NAME 5508 || !live_on_edge (e, name2)) 5509 continue; 5510 5511 enum tree_code code = gimple_assign_rhs_code (use_stmt); 5512 tree cst; 5513 if (code == PLUS_EXPR 5514 || code == MINUS_EXPR) 5515 { 5516 cst = gimple_assign_rhs2 (use_stmt); 5517 if (TREE_CODE (cst) != INTEGER_CST) 5518 continue; 5519 cst = int_const_binop (code, val, cst); 5520 } 5521 else if (CONVERT_EXPR_CODE_P (code)) 5522 { 5523 /* For truncating conversions we cannot record 5524 an inequality. */ 5525 if (comp_code == NE_EXPR 5526 && (TYPE_PRECISION (TREE_TYPE (name2)) 5527 < TYPE_PRECISION (TREE_TYPE (name)))) 5528 continue; 5529 cst = fold_convert (TREE_TYPE (name2), val); 5530 } 5531 else 5532 continue; 5533 5534 if (TREE_OVERFLOW_P (cst)) 5535 cst = drop_tree_overflow (cst); 5536 register_new_assert_for (name2, name2, comp_code, cst, 5537 NULL, e, bsi); 5538 } 5539 } 5540 5541 if (TREE_CODE_CLASS (comp_code) == tcc_comparison 5542 && TREE_CODE (val) == INTEGER_CST) 5543 { 5544 gimple *def_stmt = SSA_NAME_DEF_STMT (name); 5545 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE; 5546 tree val2 = NULL_TREE; 5547 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val)); 5548 wide_int mask = wi::zero (prec); 5549 unsigned int nprec = prec; 5550 enum tree_code rhs_code = ERROR_MARK; 5551 5552 if (is_gimple_assign (def_stmt)) 5553 rhs_code = gimple_assign_rhs_code (def_stmt); 5554 5555 /* In the case of NAME != CST1 where NAME = A +- CST2 we can 5556 assert that A != CST1 -+ CST2. */ 5557 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR) 5558 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR)) 5559 { 5560 tree op0 = gimple_assign_rhs1 (def_stmt); 5561 tree op1 = gimple_assign_rhs2 (def_stmt); 5562 if (TREE_CODE (op0) == SSA_NAME 5563 && TREE_CODE (op1) == INTEGER_CST 5564 && live_on_edge (e, op0)) 5565 { 5566 enum tree_code reverse_op = (rhs_code == PLUS_EXPR 5567 ? MINUS_EXPR : PLUS_EXPR); 5568 op1 = int_const_binop (reverse_op, val, op1); 5569 if (TREE_OVERFLOW (op1)) 5570 op1 = drop_tree_overflow (op1); 5571 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi); 5572 } 5573 } 5574 5575 /* Add asserts for NAME cmp CST and NAME being defined 5576 as NAME = (int) NAME2. */ 5577 if (!TYPE_UNSIGNED (TREE_TYPE (val)) 5578 && (comp_code == LE_EXPR || comp_code == LT_EXPR 5579 || comp_code == GT_EXPR || comp_code == GE_EXPR) 5580 && gimple_assign_cast_p (def_stmt)) 5581 { 5582 name2 = gimple_assign_rhs1 (def_stmt); 5583 if (CONVERT_EXPR_CODE_P (rhs_code) 5584 && INTEGRAL_TYPE_P (TREE_TYPE (name2)) 5585 && TYPE_UNSIGNED (TREE_TYPE (name2)) 5586 && prec == TYPE_PRECISION (TREE_TYPE (name2)) 5587 && (comp_code == LE_EXPR || comp_code == GT_EXPR 5588 || !tree_int_cst_equal (val, 5589 TYPE_MIN_VALUE (TREE_TYPE (val)))) 5590 && live_on_edge (e, name2)) 5591 { 5592 tree tmp, cst; 5593 enum tree_code new_comp_code = comp_code; 5594 5595 cst = fold_convert (TREE_TYPE (name2), 5596 TYPE_MIN_VALUE (TREE_TYPE (val))); 5597 /* Build an expression for the range test. */ 5598 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst); 5599 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst, 5600 fold_convert (TREE_TYPE (name2), val)); 5601 if (comp_code == LT_EXPR || comp_code == GE_EXPR) 5602 { 5603 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR; 5604 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst, 5605 build_int_cst (TREE_TYPE (name2), 1)); 5606 } 5607 5608 if (dump_file) 5609 { 5610 fprintf (dump_file, "Adding assert for "); 5611 print_generic_expr (dump_file, name2, 0); 5612 fprintf (dump_file, " from "); 5613 print_generic_expr (dump_file, tmp, 0); 5614 fprintf (dump_file, "\n"); 5615 } 5616 5617 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL, 5618 e, bsi); 5619 } 5620 } 5621 5622 /* Add asserts for NAME cmp CST and NAME being defined as 5623 NAME = NAME2 >> CST2. 5624 5625 Extract CST2 from the right shift. */ 5626 if (rhs_code == RSHIFT_EXPR) 5627 { 5628 name2 = gimple_assign_rhs1 (def_stmt); 5629 cst2 = gimple_assign_rhs2 (def_stmt); 5630 if (TREE_CODE (name2) == SSA_NAME 5631 && tree_fits_uhwi_p (cst2) 5632 && INTEGRAL_TYPE_P (TREE_TYPE (name2)) 5633 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1) 5634 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val))) 5635 && live_on_edge (e, name2)) 5636 { 5637 mask = wi::mask (tree_to_uhwi (cst2), false, prec); 5638 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2); 5639 } 5640 } 5641 if (val2 != NULL_TREE 5642 && TREE_CODE (val2) == INTEGER_CST 5643 && simple_cst_equal (fold_build2 (RSHIFT_EXPR, 5644 TREE_TYPE (val), 5645 val2, cst2), val)) 5646 { 5647 enum tree_code new_comp_code = comp_code; 5648 tree tmp, new_val; 5649 5650 tmp = name2; 5651 if (comp_code == EQ_EXPR || comp_code == NE_EXPR) 5652 { 5653 if (!TYPE_UNSIGNED (TREE_TYPE (val))) 5654 { 5655 tree type = build_nonstandard_integer_type (prec, 1); 5656 tmp = build1 (NOP_EXPR, type, name2); 5657 val2 = fold_convert (type, val2); 5658 } 5659 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2); 5660 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask); 5661 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR; 5662 } 5663 else if (comp_code == LT_EXPR || comp_code == GE_EXPR) 5664 { 5665 wide_int minval 5666 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val))); 5667 new_val = val2; 5668 if (minval == new_val) 5669 new_val = NULL_TREE; 5670 } 5671 else 5672 { 5673 wide_int maxval 5674 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val))); 5675 mask |= val2; 5676 if (mask == maxval) 5677 new_val = NULL_TREE; 5678 else 5679 new_val = wide_int_to_tree (TREE_TYPE (val2), mask); 5680 } 5681 5682 if (new_val) 5683 { 5684 if (dump_file) 5685 { 5686 fprintf (dump_file, "Adding assert for "); 5687 print_generic_expr (dump_file, name2, 0); 5688 fprintf (dump_file, " from "); 5689 print_generic_expr (dump_file, tmp, 0); 5690 fprintf (dump_file, "\n"); 5691 } 5692 5693 register_new_assert_for (name2, tmp, new_comp_code, new_val, 5694 NULL, e, bsi); 5695 } 5696 } 5697 5698 /* Add asserts for NAME cmp CST and NAME being defined as 5699 NAME = NAME2 & CST2. 5700 5701 Extract CST2 from the and. 5702 5703 Also handle 5704 NAME = (unsigned) NAME2; 5705 casts where NAME's type is unsigned and has smaller precision 5706 than NAME2's type as if it was NAME = NAME2 & MASK. */ 5707 names[0] = NULL_TREE; 5708 names[1] = NULL_TREE; 5709 cst2 = NULL_TREE; 5710 if (rhs_code == BIT_AND_EXPR 5711 || (CONVERT_EXPR_CODE_P (rhs_code) 5712 && INTEGRAL_TYPE_P (TREE_TYPE (val)) 5713 && TYPE_UNSIGNED (TREE_TYPE (val)) 5714 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) 5715 > prec)) 5716 { 5717 name2 = gimple_assign_rhs1 (def_stmt); 5718 if (rhs_code == BIT_AND_EXPR) 5719 cst2 = gimple_assign_rhs2 (def_stmt); 5720 else 5721 { 5722 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val)); 5723 nprec = TYPE_PRECISION (TREE_TYPE (name2)); 5724 } 5725 if (TREE_CODE (name2) == SSA_NAME 5726 && INTEGRAL_TYPE_P (TREE_TYPE (name2)) 5727 && TREE_CODE (cst2) == INTEGER_CST 5728 && !integer_zerop (cst2) 5729 && (nprec > 1 5730 || TYPE_UNSIGNED (TREE_TYPE (val)))) 5731 { 5732 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2); 5733 if (gimple_assign_cast_p (def_stmt2)) 5734 { 5735 names[1] = gimple_assign_rhs1 (def_stmt2); 5736 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2)) 5737 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1])) 5738 || (TYPE_PRECISION (TREE_TYPE (name2)) 5739 != TYPE_PRECISION (TREE_TYPE (names[1]))) 5740 || !live_on_edge (e, names[1])) 5741 names[1] = NULL_TREE; 5742 } 5743 if (live_on_edge (e, name2)) 5744 names[0] = name2; 5745 } 5746 } 5747 if (names[0] || names[1]) 5748 { 5749 wide_int minv, maxv, valv, cst2v; 5750 wide_int tem, sgnbit; 5751 bool valid_p = false, valn, cst2n; 5752 enum tree_code ccode = comp_code; 5753 5754 valv = wide_int::from (val, nprec, UNSIGNED); 5755 cst2v = wide_int::from (cst2, nprec, UNSIGNED); 5756 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val))); 5757 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val))); 5758 /* If CST2 doesn't have most significant bit set, 5759 but VAL is negative, we have comparison like 5760 if ((x & 0x123) > -4) (always true). Just give up. */ 5761 if (!cst2n && valn) 5762 ccode = ERROR_MARK; 5763 if (cst2n) 5764 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec); 5765 else 5766 sgnbit = wi::zero (nprec); 5767 minv = valv & cst2v; 5768 switch (ccode) 5769 { 5770 case EQ_EXPR: 5771 /* Minimum unsigned value for equality is VAL & CST2 5772 (should be equal to VAL, otherwise we probably should 5773 have folded the comparison into false) and 5774 maximum unsigned value is VAL | ~CST2. */ 5775 maxv = valv | ~cst2v; 5776 valid_p = true; 5777 break; 5778 5779 case NE_EXPR: 5780 tem = valv | ~cst2v; 5781 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */ 5782 if (valv == 0) 5783 { 5784 cst2n = false; 5785 sgnbit = wi::zero (nprec); 5786 goto gt_expr; 5787 } 5788 /* If (VAL | ~CST2) is all ones, handle it as 5789 (X & CST2) < VAL. */ 5790 if (tem == -1) 5791 { 5792 cst2n = false; 5793 valn = false; 5794 sgnbit = wi::zero (nprec); 5795 goto lt_expr; 5796 } 5797 if (!cst2n && wi::neg_p (cst2v)) 5798 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec); 5799 if (sgnbit != 0) 5800 { 5801 if (valv == sgnbit) 5802 { 5803 cst2n = true; 5804 valn = true; 5805 goto gt_expr; 5806 } 5807 if (tem == wi::mask (nprec - 1, false, nprec)) 5808 { 5809 cst2n = true; 5810 goto lt_expr; 5811 } 5812 if (!cst2n) 5813 sgnbit = wi::zero (nprec); 5814 } 5815 break; 5816 5817 case GE_EXPR: 5818 /* Minimum unsigned value for >= if (VAL & CST2) == VAL 5819 is VAL and maximum unsigned value is ~0. For signed 5820 comparison, if CST2 doesn't have most significant bit 5821 set, handle it similarly. If CST2 has MSB set, 5822 the minimum is the same, and maximum is ~0U/2. */ 5823 if (minv != valv) 5824 { 5825 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to 5826 VAL. */ 5827 minv = masked_increment (valv, cst2v, sgnbit, nprec); 5828 if (minv == valv) 5829 break; 5830 } 5831 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec); 5832 valid_p = true; 5833 break; 5834 5835 case GT_EXPR: 5836 gt_expr: 5837 /* Find out smallest MINV where MINV > VAL 5838 && (MINV & CST2) == MINV, if any. If VAL is signed and 5839 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */ 5840 minv = masked_increment (valv, cst2v, sgnbit, nprec); 5841 if (minv == valv) 5842 break; 5843 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec); 5844 valid_p = true; 5845 break; 5846 5847 case LE_EXPR: 5848 /* Minimum unsigned value for <= is 0 and maximum 5849 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL. 5850 Otherwise, find smallest VAL2 where VAL2 > VAL 5851 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2 5852 as maximum. 5853 For signed comparison, if CST2 doesn't have most 5854 significant bit set, handle it similarly. If CST2 has 5855 MSB set, the maximum is the same and minimum is INT_MIN. */ 5856 if (minv == valv) 5857 maxv = valv; 5858 else 5859 { 5860 maxv = masked_increment (valv, cst2v, sgnbit, nprec); 5861 if (maxv == valv) 5862 break; 5863 maxv -= 1; 5864 } 5865 maxv |= ~cst2v; 5866 minv = sgnbit; 5867 valid_p = true; 5868 break; 5869 5870 case LT_EXPR: 5871 lt_expr: 5872 /* Minimum unsigned value for < is 0 and maximum 5873 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL. 5874 Otherwise, find smallest VAL2 where VAL2 > VAL 5875 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2 5876 as maximum. 5877 For signed comparison, if CST2 doesn't have most 5878 significant bit set, handle it similarly. If CST2 has 5879 MSB set, the maximum is the same and minimum is INT_MIN. */ 5880 if (minv == valv) 5881 { 5882 if (valv == sgnbit) 5883 break; 5884 maxv = valv; 5885 } 5886 else 5887 { 5888 maxv = masked_increment (valv, cst2v, sgnbit, nprec); 5889 if (maxv == valv) 5890 break; 5891 } 5892 maxv -= 1; 5893 maxv |= ~cst2v; 5894 minv = sgnbit; 5895 valid_p = true; 5896 break; 5897 5898 default: 5899 break; 5900 } 5901 if (valid_p 5902 && (maxv - minv) != -1) 5903 { 5904 tree tmp, new_val, type; 5905 int i; 5906 5907 for (i = 0; i < 2; i++) 5908 if (names[i]) 5909 { 5910 wide_int maxv2 = maxv; 5911 tmp = names[i]; 5912 type = TREE_TYPE (names[i]); 5913 if (!TYPE_UNSIGNED (type)) 5914 { 5915 type = build_nonstandard_integer_type (nprec, 1); 5916 tmp = build1 (NOP_EXPR, type, names[i]); 5917 } 5918 if (minv != 0) 5919 { 5920 tmp = build2 (PLUS_EXPR, type, tmp, 5921 wide_int_to_tree (type, -minv)); 5922 maxv2 = maxv - minv; 5923 } 5924 new_val = wide_int_to_tree (type, maxv2); 5925 5926 if (dump_file) 5927 { 5928 fprintf (dump_file, "Adding assert for "); 5929 print_generic_expr (dump_file, names[i], 0); 5930 fprintf (dump_file, " from "); 5931 print_generic_expr (dump_file, tmp, 0); 5932 fprintf (dump_file, "\n"); 5933 } 5934 5935 register_new_assert_for (names[i], tmp, LE_EXPR, 5936 new_val, NULL, e, bsi); 5937 } 5938 } 5939 } 5940 } 5941 } 5942 5943 /* OP is an operand of a truth value expression which is known to have 5944 a particular value. Register any asserts for OP and for any 5945 operands in OP's defining statement. 5946 5947 If CODE is EQ_EXPR, then we want to register OP is zero (false), 5948 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */ 5949 5950 static void 5951 register_edge_assert_for_1 (tree op, enum tree_code code, 5952 edge e, gimple_stmt_iterator bsi) 5953 { 5954 gimple *op_def; 5955 tree val; 5956 enum tree_code rhs_code; 5957 5958 /* We only care about SSA_NAMEs. */ 5959 if (TREE_CODE (op) != SSA_NAME) 5960 return; 5961 5962 /* We know that OP will have a zero or nonzero value. If OP is used 5963 more than once go ahead and register an assert for OP. */ 5964 if (live_on_edge (e, op)) 5965 { 5966 val = build_int_cst (TREE_TYPE (op), 0); 5967 register_new_assert_for (op, op, code, val, NULL, e, bsi); 5968 } 5969 5970 /* Now look at how OP is set. If it's set from a comparison, 5971 a truth operation or some bit operations, then we may be able 5972 to register information about the operands of that assignment. */ 5973 op_def = SSA_NAME_DEF_STMT (op); 5974 if (gimple_code (op_def) != GIMPLE_ASSIGN) 5975 return; 5976 5977 rhs_code = gimple_assign_rhs_code (op_def); 5978 5979 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison) 5980 { 5981 bool invert = (code == EQ_EXPR ? true : false); 5982 tree op0 = gimple_assign_rhs1 (op_def); 5983 tree op1 = gimple_assign_rhs2 (op_def); 5984 5985 if (TREE_CODE (op0) == SSA_NAME) 5986 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert); 5987 if (TREE_CODE (op1) == SSA_NAME) 5988 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert); 5989 } 5990 else if ((code == NE_EXPR 5991 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR) 5992 || (code == EQ_EXPR 5993 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR)) 5994 { 5995 /* Recurse on each operand. */ 5996 tree op0 = gimple_assign_rhs1 (op_def); 5997 tree op1 = gimple_assign_rhs2 (op_def); 5998 if (TREE_CODE (op0) == SSA_NAME 5999 && has_single_use (op0)) 6000 register_edge_assert_for_1 (op0, code, e, bsi); 6001 if (TREE_CODE (op1) == SSA_NAME 6002 && has_single_use (op1)) 6003 register_edge_assert_for_1 (op1, code, e, bsi); 6004 } 6005 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR 6006 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1) 6007 { 6008 /* Recurse, flipping CODE. */ 6009 code = invert_tree_comparison (code, false); 6010 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi); 6011 } 6012 else if (gimple_assign_rhs_code (op_def) == SSA_NAME) 6013 { 6014 /* Recurse through the copy. */ 6015 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi); 6016 } 6017 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def))) 6018 { 6019 /* Recurse through the type conversion, unless it is a narrowing 6020 conversion or conversion from non-integral type. */ 6021 tree rhs = gimple_assign_rhs1 (op_def); 6022 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs)) 6023 && (TYPE_PRECISION (TREE_TYPE (rhs)) 6024 <= TYPE_PRECISION (TREE_TYPE (op)))) 6025 register_edge_assert_for_1 (rhs, code, e, bsi); 6026 } 6027 } 6028 6029 /* Try to register an edge assertion for SSA name NAME on edge E for 6030 the condition COND contributing to the conditional jump pointed to by 6031 SI. */ 6032 6033 static void 6034 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si, 6035 enum tree_code cond_code, tree cond_op0, 6036 tree cond_op1) 6037 { 6038 tree val; 6039 enum tree_code comp_code; 6040 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0; 6041 6042 /* Do not attempt to infer anything in names that flow through 6043 abnormal edges. */ 6044 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)) 6045 return; 6046 6047 if (!extract_code_and_val_from_cond_with_ops (name, cond_code, 6048 cond_op0, cond_op1, 6049 is_else_edge, 6050 &comp_code, &val)) 6051 return; 6052 6053 /* Register ASSERT_EXPRs for name. */ 6054 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0, 6055 cond_op1, is_else_edge); 6056 6057 6058 /* If COND is effectively an equality test of an SSA_NAME against 6059 the value zero or one, then we may be able to assert values 6060 for SSA_NAMEs which flow into COND. */ 6061 6062 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining 6063 statement of NAME we can assert both operands of the BIT_AND_EXPR 6064 have nonzero value. */ 6065 if (((comp_code == EQ_EXPR && integer_onep (val)) 6066 || (comp_code == NE_EXPR && integer_zerop (val)))) 6067 { 6068 gimple *def_stmt = SSA_NAME_DEF_STMT (name); 6069 6070 if (is_gimple_assign (def_stmt) 6071 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR) 6072 { 6073 tree op0 = gimple_assign_rhs1 (def_stmt); 6074 tree op1 = gimple_assign_rhs2 (def_stmt); 6075 register_edge_assert_for_1 (op0, NE_EXPR, e, si); 6076 register_edge_assert_for_1 (op1, NE_EXPR, e, si); 6077 } 6078 } 6079 6080 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining 6081 statement of NAME we can assert both operands of the BIT_IOR_EXPR 6082 have zero value. */ 6083 if (((comp_code == EQ_EXPR && integer_zerop (val)) 6084 || (comp_code == NE_EXPR && integer_onep (val)))) 6085 { 6086 gimple *def_stmt = SSA_NAME_DEF_STMT (name); 6087 6088 /* For BIT_IOR_EXPR only if NAME == 0 both operands have 6089 necessarily zero value, or if type-precision is one. */ 6090 if (is_gimple_assign (def_stmt) 6091 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR 6092 && (TYPE_PRECISION (TREE_TYPE (name)) == 1 6093 || comp_code == EQ_EXPR))) 6094 { 6095 tree op0 = gimple_assign_rhs1 (def_stmt); 6096 tree op1 = gimple_assign_rhs2 (def_stmt); 6097 register_edge_assert_for_1 (op0, EQ_EXPR, e, si); 6098 register_edge_assert_for_1 (op1, EQ_EXPR, e, si); 6099 } 6100 } 6101 } 6102 6103 6104 /* Determine whether the outgoing edges of BB should receive an 6105 ASSERT_EXPR for each of the operands of BB's LAST statement. 6106 The last statement of BB must be a COND_EXPR. 6107 6108 If any of the sub-graphs rooted at BB have an interesting use of 6109 the predicate operands, an assert location node is added to the 6110 list of assertions for the corresponding operands. */ 6111 6112 static void 6113 find_conditional_asserts (basic_block bb, gcond *last) 6114 { 6115 gimple_stmt_iterator bsi; 6116 tree op; 6117 edge_iterator ei; 6118 edge e; 6119 ssa_op_iter iter; 6120 6121 bsi = gsi_for_stmt (last); 6122 6123 /* Look for uses of the operands in each of the sub-graphs 6124 rooted at BB. We need to check each of the outgoing edges 6125 separately, so that we know what kind of ASSERT_EXPR to 6126 insert. */ 6127 FOR_EACH_EDGE (e, ei, bb->succs) 6128 { 6129 if (e->dest == bb) 6130 continue; 6131 6132 /* Register the necessary assertions for each operand in the 6133 conditional predicate. */ 6134 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE) 6135 register_edge_assert_for (op, e, bsi, 6136 gimple_cond_code (last), 6137 gimple_cond_lhs (last), 6138 gimple_cond_rhs (last)); 6139 } 6140 } 6141 6142 struct case_info 6143 { 6144 tree expr; 6145 basic_block bb; 6146 }; 6147 6148 /* Compare two case labels sorting first by the destination bb index 6149 and then by the case value. */ 6150 6151 static int 6152 compare_case_labels (const void *p1, const void *p2) 6153 { 6154 const struct case_info *ci1 = (const struct case_info *) p1; 6155 const struct case_info *ci2 = (const struct case_info *) p2; 6156 int idx1 = ci1->bb->index; 6157 int idx2 = ci2->bb->index; 6158 6159 if (idx1 < idx2) 6160 return -1; 6161 else if (idx1 == idx2) 6162 { 6163 /* Make sure the default label is first in a group. */ 6164 if (!CASE_LOW (ci1->expr)) 6165 return -1; 6166 else if (!CASE_LOW (ci2->expr)) 6167 return 1; 6168 else 6169 return tree_int_cst_compare (CASE_LOW (ci1->expr), 6170 CASE_LOW (ci2->expr)); 6171 } 6172 else 6173 return 1; 6174 } 6175 6176 /* Determine whether the outgoing edges of BB should receive an 6177 ASSERT_EXPR for each of the operands of BB's LAST statement. 6178 The last statement of BB must be a SWITCH_EXPR. 6179 6180 If any of the sub-graphs rooted at BB have an interesting use of 6181 the predicate operands, an assert location node is added to the 6182 list of assertions for the corresponding operands. */ 6183 6184 static void 6185 find_switch_asserts (basic_block bb, gswitch *last) 6186 { 6187 gimple_stmt_iterator bsi; 6188 tree op; 6189 edge e; 6190 struct case_info *ci; 6191 size_t n = gimple_switch_num_labels (last); 6192 #if GCC_VERSION >= 4000 6193 unsigned int idx; 6194 #else 6195 /* Work around GCC 3.4 bug (PR 37086). */ 6196 volatile unsigned int idx; 6197 #endif 6198 6199 bsi = gsi_for_stmt (last); 6200 op = gimple_switch_index (last); 6201 if (TREE_CODE (op) != SSA_NAME) 6202 return; 6203 6204 /* Build a vector of case labels sorted by destination label. */ 6205 ci = XNEWVEC (struct case_info, n); 6206 for (idx = 0; idx < n; ++idx) 6207 { 6208 ci[idx].expr = gimple_switch_label (last, idx); 6209 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr)); 6210 } 6211 edge default_edge = find_edge (bb, ci[0].bb); 6212 qsort (ci, n, sizeof (struct case_info), compare_case_labels); 6213 6214 for (idx = 0; idx < n; ++idx) 6215 { 6216 tree min, max; 6217 tree cl = ci[idx].expr; 6218 basic_block cbb = ci[idx].bb; 6219 6220 min = CASE_LOW (cl); 6221 max = CASE_HIGH (cl); 6222 6223 /* If there are multiple case labels with the same destination 6224 we need to combine them to a single value range for the edge. */ 6225 if (idx + 1 < n && cbb == ci[idx + 1].bb) 6226 { 6227 /* Skip labels until the last of the group. */ 6228 do { 6229 ++idx; 6230 } while (idx < n && cbb == ci[idx].bb); 6231 --idx; 6232 6233 /* Pick up the maximum of the case label range. */ 6234 if (CASE_HIGH (ci[idx].expr)) 6235 max = CASE_HIGH (ci[idx].expr); 6236 else 6237 max = CASE_LOW (ci[idx].expr); 6238 } 6239 6240 /* Can't extract a useful assertion out of a range that includes the 6241 default label. */ 6242 if (min == NULL_TREE) 6243 continue; 6244 6245 /* Find the edge to register the assert expr on. */ 6246 e = find_edge (bb, cbb); 6247 6248 /* Register the necessary assertions for the operand in the 6249 SWITCH_EXPR. */ 6250 register_edge_assert_for (op, e, bsi, 6251 max ? GE_EXPR : EQ_EXPR, 6252 op, fold_convert (TREE_TYPE (op), min)); 6253 if (max) 6254 register_edge_assert_for (op, e, bsi, LE_EXPR, op, 6255 fold_convert (TREE_TYPE (op), max)); 6256 } 6257 6258 XDELETEVEC (ci); 6259 6260 if (!live_on_edge (default_edge, op)) 6261 return; 6262 6263 /* Now register along the default label assertions that correspond to the 6264 anti-range of each label. */ 6265 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS); 6266 if (insertion_limit == 0) 6267 return; 6268 6269 /* We can't do this if the default case shares a label with another case. */ 6270 tree default_cl = gimple_switch_default_label (last); 6271 for (idx = 1; idx < n; idx++) 6272 { 6273 tree min, max; 6274 tree cl = gimple_switch_label (last, idx); 6275 if (CASE_LABEL (cl) == CASE_LABEL (default_cl)) 6276 continue; 6277 6278 min = CASE_LOW (cl); 6279 max = CASE_HIGH (cl); 6280 6281 /* Combine contiguous case ranges to reduce the number of assertions 6282 to insert. */ 6283 for (idx = idx + 1; idx < n; idx++) 6284 { 6285 tree next_min, next_max; 6286 tree next_cl = gimple_switch_label (last, idx); 6287 if (CASE_LABEL (next_cl) == CASE_LABEL (default_cl)) 6288 break; 6289 6290 next_min = CASE_LOW (next_cl); 6291 next_max = CASE_HIGH (next_cl); 6292 6293 wide_int difference = wi::sub (next_min, max ? max : min); 6294 if (wi::eq_p (difference, 1)) 6295 max = next_max ? next_max : next_min; 6296 else 6297 break; 6298 } 6299 idx--; 6300 6301 if (max == NULL_TREE) 6302 { 6303 /* Register the assertion OP != MIN. */ 6304 min = fold_convert (TREE_TYPE (op), min); 6305 register_edge_assert_for (op, default_edge, bsi, NE_EXPR, op, min); 6306 } 6307 else 6308 { 6309 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN), 6310 which will give OP the anti-range ~[MIN,MAX]. */ 6311 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op); 6312 min = fold_convert (TREE_TYPE (uop), min); 6313 max = fold_convert (TREE_TYPE (uop), max); 6314 6315 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min); 6316 tree rhs = int_const_binop (MINUS_EXPR, max, min); 6317 register_new_assert_for (op, lhs, GT_EXPR, rhs, 6318 NULL, default_edge, bsi); 6319 } 6320 6321 if (--insertion_limit == 0) 6322 break; 6323 } 6324 } 6325 6326 6327 /* Traverse all the statements in block BB looking for statements that 6328 may generate useful assertions for the SSA names in their operand. 6329 If a statement produces a useful assertion A for name N_i, then the 6330 list of assertions already generated for N_i is scanned to 6331 determine if A is actually needed. 6332 6333 If N_i already had the assertion A at a location dominating the 6334 current location, then nothing needs to be done. Otherwise, the 6335 new location for A is recorded instead. 6336 6337 1- For every statement S in BB, all the variables used by S are 6338 added to bitmap FOUND_IN_SUBGRAPH. 6339 6340 2- If statement S uses an operand N in a way that exposes a known 6341 value range for N, then if N was not already generated by an 6342 ASSERT_EXPR, create a new assert location for N. For instance, 6343 if N is a pointer and the statement dereferences it, we can 6344 assume that N is not NULL. 6345 6346 3- COND_EXPRs are a special case of #2. We can derive range 6347 information from the predicate but need to insert different 6348 ASSERT_EXPRs for each of the sub-graphs rooted at the 6349 conditional block. If the last statement of BB is a conditional 6350 expression of the form 'X op Y', then 6351 6352 a) Remove X and Y from the set FOUND_IN_SUBGRAPH. 6353 6354 b) If the conditional is the only entry point to the sub-graph 6355 corresponding to the THEN_CLAUSE, recurse into it. On 6356 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then 6357 an ASSERT_EXPR is added for the corresponding variable. 6358 6359 c) Repeat step (b) on the ELSE_CLAUSE. 6360 6361 d) Mark X and Y in FOUND_IN_SUBGRAPH. 6362 6363 For instance, 6364 6365 if (a == 9) 6366 b = a; 6367 else 6368 b = c + 1; 6369 6370 In this case, an assertion on the THEN clause is useful to 6371 determine that 'a' is always 9 on that edge. However, an assertion 6372 on the ELSE clause would be unnecessary. 6373 6374 4- If BB does not end in a conditional expression, then we recurse 6375 into BB's dominator children. 6376 6377 At the end of the recursive traversal, every SSA name will have a 6378 list of locations where ASSERT_EXPRs should be added. When a new 6379 location for name N is found, it is registered by calling 6380 register_new_assert_for. That function keeps track of all the 6381 registered assertions to prevent adding unnecessary assertions. 6382 For instance, if a pointer P_4 is dereferenced more than once in a 6383 dominator tree, only the location dominating all the dereference of 6384 P_4 will receive an ASSERT_EXPR. */ 6385 6386 static void 6387 find_assert_locations_1 (basic_block bb, sbitmap live) 6388 { 6389 gimple *last; 6390 6391 last = last_stmt (bb); 6392 6393 /* If BB's last statement is a conditional statement involving integer 6394 operands, determine if we need to add ASSERT_EXPRs. */ 6395 if (last 6396 && gimple_code (last) == GIMPLE_COND 6397 && !fp_predicate (last) 6398 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE)) 6399 find_conditional_asserts (bb, as_a <gcond *> (last)); 6400 6401 /* If BB's last statement is a switch statement involving integer 6402 operands, determine if we need to add ASSERT_EXPRs. */ 6403 if (last 6404 && gimple_code (last) == GIMPLE_SWITCH 6405 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE)) 6406 find_switch_asserts (bb, as_a <gswitch *> (last)); 6407 6408 /* Traverse all the statements in BB marking used names and looking 6409 for statements that may infer assertions for their used operands. */ 6410 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si); 6411 gsi_prev (&si)) 6412 { 6413 gimple *stmt; 6414 tree op; 6415 ssa_op_iter i; 6416 6417 stmt = gsi_stmt (si); 6418 6419 if (is_gimple_debug (stmt)) 6420 continue; 6421 6422 /* See if we can derive an assertion for any of STMT's operands. */ 6423 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE) 6424 { 6425 tree value; 6426 enum tree_code comp_code; 6427 6428 /* If op is not live beyond this stmt, do not bother to insert 6429 asserts for it. */ 6430 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op))) 6431 continue; 6432 6433 /* If OP is used in such a way that we can infer a value 6434 range for it, and we don't find a previous assertion for 6435 it, create a new assertion location node for OP. */ 6436 if (infer_value_range (stmt, op, &comp_code, &value)) 6437 { 6438 /* If we are able to infer a nonzero value range for OP, 6439 then walk backwards through the use-def chain to see if OP 6440 was set via a typecast. 6441 6442 If so, then we can also infer a nonzero value range 6443 for the operand of the NOP_EXPR. */ 6444 if (comp_code == NE_EXPR && integer_zerop (value)) 6445 { 6446 tree t = op; 6447 gimple *def_stmt = SSA_NAME_DEF_STMT (t); 6448 6449 while (is_gimple_assign (def_stmt) 6450 && CONVERT_EXPR_CODE_P 6451 (gimple_assign_rhs_code (def_stmt)) 6452 && TREE_CODE 6453 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME 6454 && POINTER_TYPE_P 6455 (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))) 6456 { 6457 t = gimple_assign_rhs1 (def_stmt); 6458 def_stmt = SSA_NAME_DEF_STMT (t); 6459 6460 /* Note we want to register the assert for the 6461 operand of the NOP_EXPR after SI, not after the 6462 conversion. */ 6463 if (bitmap_bit_p (live, SSA_NAME_VERSION (t))) 6464 register_new_assert_for (t, t, comp_code, value, 6465 bb, NULL, si); 6466 } 6467 } 6468 6469 register_new_assert_for (op, op, comp_code, value, bb, NULL, si); 6470 } 6471 } 6472 6473 /* Update live. */ 6474 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE) 6475 bitmap_set_bit (live, SSA_NAME_VERSION (op)); 6476 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF) 6477 bitmap_clear_bit (live, SSA_NAME_VERSION (op)); 6478 } 6479 6480 /* Traverse all PHI nodes in BB, updating live. */ 6481 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si); 6482 gsi_next (&si)) 6483 { 6484 use_operand_p arg_p; 6485 ssa_op_iter i; 6486 gphi *phi = si.phi (); 6487 tree res = gimple_phi_result (phi); 6488 6489 if (virtual_operand_p (res)) 6490 continue; 6491 6492 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE) 6493 { 6494 tree arg = USE_FROM_PTR (arg_p); 6495 if (TREE_CODE (arg) == SSA_NAME) 6496 bitmap_set_bit (live, SSA_NAME_VERSION (arg)); 6497 } 6498 6499 bitmap_clear_bit (live, SSA_NAME_VERSION (res)); 6500 } 6501 } 6502 6503 /* Do an RPO walk over the function computing SSA name liveness 6504 on-the-fly and deciding on assert expressions to insert. */ 6505 6506 static void 6507 find_assert_locations (void) 6508 { 6509 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun)); 6510 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun)); 6511 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun)); 6512 int rpo_cnt, i; 6513 6514 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun)); 6515 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false); 6516 for (i = 0; i < rpo_cnt; ++i) 6517 bb_rpo[rpo[i]] = i; 6518 6519 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to 6520 the order we compute liveness and insert asserts we otherwise 6521 fail to insert asserts into the loop latch. */ 6522 loop_p loop; 6523 FOR_EACH_LOOP (loop, 0) 6524 { 6525 i = loop->latch->index; 6526 unsigned int j = single_succ_edge (loop->latch)->dest_idx; 6527 for (gphi_iterator gsi = gsi_start_phis (loop->header); 6528 !gsi_end_p (gsi); gsi_next (&gsi)) 6529 { 6530 gphi *phi = gsi.phi (); 6531 if (virtual_operand_p (gimple_phi_result (phi))) 6532 continue; 6533 tree arg = gimple_phi_arg_def (phi, j); 6534 if (TREE_CODE (arg) == SSA_NAME) 6535 { 6536 if (live[i] == NULL) 6537 { 6538 live[i] = sbitmap_alloc (num_ssa_names); 6539 bitmap_clear (live[i]); 6540 } 6541 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg)); 6542 } 6543 } 6544 } 6545 6546 for (i = rpo_cnt - 1; i >= 0; --i) 6547 { 6548 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]); 6549 edge e; 6550 edge_iterator ei; 6551 6552 if (!live[rpo[i]]) 6553 { 6554 live[rpo[i]] = sbitmap_alloc (num_ssa_names); 6555 bitmap_clear (live[rpo[i]]); 6556 } 6557 6558 /* Process BB and update the live information with uses in 6559 this block. */ 6560 find_assert_locations_1 (bb, live[rpo[i]]); 6561 6562 /* Merge liveness into the predecessor blocks and free it. */ 6563 if (!bitmap_empty_p (live[rpo[i]])) 6564 { 6565 int pred_rpo = i; 6566 FOR_EACH_EDGE (e, ei, bb->preds) 6567 { 6568 int pred = e->src->index; 6569 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK) 6570 continue; 6571 6572 if (!live[pred]) 6573 { 6574 live[pred] = sbitmap_alloc (num_ssa_names); 6575 bitmap_clear (live[pred]); 6576 } 6577 bitmap_ior (live[pred], live[pred], live[rpo[i]]); 6578 6579 if (bb_rpo[pred] < pred_rpo) 6580 pred_rpo = bb_rpo[pred]; 6581 } 6582 6583 /* Record the RPO number of the last visited block that needs 6584 live information from this block. */ 6585 last_rpo[rpo[i]] = pred_rpo; 6586 } 6587 else 6588 { 6589 sbitmap_free (live[rpo[i]]); 6590 live[rpo[i]] = NULL; 6591 } 6592 6593 /* We can free all successors live bitmaps if all their 6594 predecessors have been visited already. */ 6595 FOR_EACH_EDGE (e, ei, bb->succs) 6596 if (last_rpo[e->dest->index] == i 6597 && live[e->dest->index]) 6598 { 6599 sbitmap_free (live[e->dest->index]); 6600 live[e->dest->index] = NULL; 6601 } 6602 } 6603 6604 XDELETEVEC (rpo); 6605 XDELETEVEC (bb_rpo); 6606 XDELETEVEC (last_rpo); 6607 for (i = 0; i < last_basic_block_for_fn (cfun); ++i) 6608 if (live[i]) 6609 sbitmap_free (live[i]); 6610 XDELETEVEC (live); 6611 } 6612 6613 /* Create an ASSERT_EXPR for NAME and insert it in the location 6614 indicated by LOC. Return true if we made any edge insertions. */ 6615 6616 static bool 6617 process_assert_insertions_for (tree name, assert_locus *loc) 6618 { 6619 /* Build the comparison expression NAME_i COMP_CODE VAL. */ 6620 gimple *stmt; 6621 tree cond; 6622 gimple *assert_stmt; 6623 edge_iterator ei; 6624 edge e; 6625 6626 /* If we have X <=> X do not insert an assert expr for that. */ 6627 if (loc->expr == loc->val) 6628 return false; 6629 6630 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val); 6631 assert_stmt = build_assert_expr_for (cond, name); 6632 if (loc->e) 6633 { 6634 /* We have been asked to insert the assertion on an edge. This 6635 is used only by COND_EXPR and SWITCH_EXPR assertions. */ 6636 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND 6637 || (gimple_code (gsi_stmt (loc->si)) 6638 == GIMPLE_SWITCH)); 6639 6640 gsi_insert_on_edge (loc->e, assert_stmt); 6641 return true; 6642 } 6643 6644 /* If the stmt iterator points at the end then this is an insertion 6645 at the beginning of a block. */ 6646 if (gsi_end_p (loc->si)) 6647 { 6648 gimple_stmt_iterator si = gsi_after_labels (loc->bb); 6649 gsi_insert_before (&si, assert_stmt, GSI_SAME_STMT); 6650 return false; 6651 6652 } 6653 /* Otherwise, we can insert right after LOC->SI iff the 6654 statement must not be the last statement in the block. */ 6655 stmt = gsi_stmt (loc->si); 6656 if (!stmt_ends_bb_p (stmt)) 6657 { 6658 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT); 6659 return false; 6660 } 6661 6662 /* If STMT must be the last statement in BB, we can only insert new 6663 assertions on the non-abnormal edge out of BB. Note that since 6664 STMT is not control flow, there may only be one non-abnormal/eh edge 6665 out of BB. */ 6666 FOR_EACH_EDGE (e, ei, loc->bb->succs) 6667 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH))) 6668 { 6669 gsi_insert_on_edge (e, assert_stmt); 6670 return true; 6671 } 6672 6673 gcc_unreachable (); 6674 } 6675 6676 /* Qsort helper for sorting assert locations. */ 6677 6678 static int 6679 compare_assert_loc (const void *pa, const void *pb) 6680 { 6681 assert_locus * const a = *(assert_locus * const *)pa; 6682 assert_locus * const b = *(assert_locus * const *)pb; 6683 if (! a->e && b->e) 6684 return 1; 6685 else if (a->e && ! b->e) 6686 return -1; 6687 6688 /* Sort after destination index. */ 6689 if (! a->e && ! b->e) 6690 ; 6691 else if (a->e->dest->index > b->e->dest->index) 6692 return 1; 6693 else if (a->e->dest->index < b->e->dest->index) 6694 return -1; 6695 6696 /* Sort after comp_code. */ 6697 if (a->comp_code > b->comp_code) 6698 return 1; 6699 else if (a->comp_code < b->comp_code) 6700 return -1; 6701 6702 /* Break the tie using hashing and source/bb index. */ 6703 hashval_t ha = iterative_hash_expr (a->expr, iterative_hash_expr (a->val, 0)); 6704 hashval_t hb = iterative_hash_expr (b->expr, iterative_hash_expr (b->val, 0)); 6705 if (ha == hb) 6706 return (a->e && b->e 6707 ? a->e->src->index - b->e->src->index 6708 : a->bb->index - b->bb->index); 6709 return ha - hb; 6710 } 6711 6712 /* Process all the insertions registered for every name N_i registered 6713 in NEED_ASSERT_FOR. The list of assertions to be inserted are 6714 found in ASSERTS_FOR[i]. */ 6715 6716 static void 6717 process_assert_insertions (void) 6718 { 6719 unsigned i; 6720 bitmap_iterator bi; 6721 bool update_edges_p = false; 6722 int num_asserts = 0; 6723 6724 if (dump_file && (dump_flags & TDF_DETAILS)) 6725 dump_all_asserts (dump_file); 6726 6727 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi) 6728 { 6729 assert_locus *loc = asserts_for[i]; 6730 gcc_assert (loc); 6731 6732 auto_vec<assert_locus *, 16> asserts; 6733 for (; loc; loc = loc->next) 6734 asserts.safe_push (loc); 6735 asserts.qsort (compare_assert_loc); 6736 6737 /* Push down common asserts to successors and remove redundant ones. */ 6738 unsigned ecnt = 0; 6739 assert_locus *common = NULL; 6740 unsigned commonj = 0; 6741 for (unsigned j = 0; j < asserts.length (); ++j) 6742 { 6743 loc = asserts[j]; 6744 if (! loc->e) 6745 common = NULL; 6746 else if (! common 6747 || loc->e->dest != common->e->dest 6748 || loc->comp_code != common->comp_code 6749 || ! operand_equal_p (loc->val, common->val, 0) 6750 || ! operand_equal_p (loc->expr, common->expr, 0)) 6751 { 6752 commonj = j; 6753 common = loc; 6754 ecnt = 1; 6755 } 6756 else if (loc->e == asserts[j-1]->e) 6757 { 6758 /* Remove duplicate asserts. */ 6759 if (commonj == j - 1) 6760 { 6761 commonj = j; 6762 common = loc; 6763 } 6764 free (asserts[j-1]); 6765 asserts[j-1] = NULL; 6766 } 6767 else 6768 { 6769 ecnt++; 6770 if (EDGE_COUNT (common->e->dest->preds) == ecnt) 6771 { 6772 /* We have the same assertion on all incoming edges of a BB. 6773 Insert it at the beginning of that block. */ 6774 loc->bb = loc->e->dest; 6775 loc->e = NULL; 6776 loc->si = gsi_none (); 6777 common = NULL; 6778 /* Clear asserts commoned. */ 6779 for (; commonj != j; ++commonj) 6780 if (asserts[commonj]) 6781 { 6782 free (asserts[commonj]); 6783 asserts[commonj] = NULL; 6784 } 6785 } 6786 } 6787 } 6788 6789 for (unsigned j = 0; j < asserts.length (); ++j) 6790 { 6791 loc = asserts[j]; 6792 if (! loc) 6793 continue; 6794 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc); 6795 num_asserts++; 6796 free (loc); 6797 } 6798 } 6799 6800 if (update_edges_p) 6801 gsi_commit_edge_inserts (); 6802 6803 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted", 6804 num_asserts); 6805 } 6806 6807 6808 /* Traverse the flowgraph looking for conditional jumps to insert range 6809 expressions. These range expressions are meant to provide information 6810 to optimizations that need to reason in terms of value ranges. They 6811 will not be expanded into RTL. For instance, given: 6812 6813 x = ... 6814 y = ... 6815 if (x < y) 6816 y = x - 2; 6817 else 6818 x = y + 3; 6819 6820 this pass will transform the code into: 6821 6822 x = ... 6823 y = ... 6824 if (x < y) 6825 { 6826 x = ASSERT_EXPR <x, x < y> 6827 y = x - 2 6828 } 6829 else 6830 { 6831 y = ASSERT_EXPR <y, x >= y> 6832 x = y + 3 6833 } 6834 6835 The idea is that once copy and constant propagation have run, other 6836 optimizations will be able to determine what ranges of values can 'x' 6837 take in different paths of the code, simply by checking the reaching 6838 definition of 'x'. */ 6839 6840 static void 6841 insert_range_assertions (void) 6842 { 6843 need_assert_for = BITMAP_ALLOC (NULL); 6844 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names); 6845 6846 calculate_dominance_info (CDI_DOMINATORS); 6847 6848 find_assert_locations (); 6849 if (!bitmap_empty_p (need_assert_for)) 6850 { 6851 process_assert_insertions (); 6852 update_ssa (TODO_update_ssa_no_phi); 6853 } 6854 6855 if (dump_file && (dump_flags & TDF_DETAILS)) 6856 { 6857 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n"); 6858 dump_function_to_file (current_function_decl, dump_file, dump_flags); 6859 } 6860 6861 free (asserts_for); 6862 BITMAP_FREE (need_assert_for); 6863 } 6864 6865 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays 6866 and "struct" hacks. If VRP can determine that the 6867 array subscript is a constant, check if it is outside valid 6868 range. If the array subscript is a RANGE, warn if it is 6869 non-overlapping with valid range. 6870 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */ 6871 6872 static void 6873 check_array_ref (location_t location, tree ref, bool ignore_off_by_one) 6874 { 6875 value_range *vr = NULL; 6876 tree low_sub, up_sub; 6877 tree low_bound, up_bound, up_bound_p1; 6878 6879 if (TREE_NO_WARNING (ref)) 6880 return; 6881 6882 low_sub = up_sub = TREE_OPERAND (ref, 1); 6883 up_bound = array_ref_up_bound (ref); 6884 6885 /* Can not check flexible arrays. */ 6886 if (!up_bound 6887 || TREE_CODE (up_bound) != INTEGER_CST) 6888 return; 6889 6890 /* Accesses to trailing arrays via pointers may access storage 6891 beyond the types array bounds. */ 6892 if (warn_array_bounds < 2 6893 && array_at_struct_end_p (ref)) 6894 return; 6895 6896 low_bound = array_ref_low_bound (ref); 6897 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, 6898 build_int_cst (TREE_TYPE (up_bound), 1)); 6899 6900 /* Empty array. */ 6901 if (tree_int_cst_equal (low_bound, up_bound_p1)) 6902 { 6903 warning_at (location, OPT_Warray_bounds, 6904 "array subscript is above array bounds"); 6905 TREE_NO_WARNING (ref) = 1; 6906 } 6907 6908 if (TREE_CODE (low_sub) == SSA_NAME) 6909 { 6910 vr = get_value_range (low_sub); 6911 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE) 6912 { 6913 low_sub = vr->type == VR_RANGE ? vr->max : vr->min; 6914 up_sub = vr->type == VR_RANGE ? vr->min : vr->max; 6915 } 6916 } 6917 6918 if (vr && vr->type == VR_ANTI_RANGE) 6919 { 6920 if (TREE_CODE (up_sub) == INTEGER_CST 6921 && (ignore_off_by_one 6922 ? tree_int_cst_lt (up_bound, up_sub) 6923 : tree_int_cst_le (up_bound, up_sub)) 6924 && TREE_CODE (low_sub) == INTEGER_CST 6925 && tree_int_cst_le (low_sub, low_bound)) 6926 { 6927 warning_at (location, OPT_Warray_bounds, 6928 "array subscript is outside array bounds"); 6929 TREE_NO_WARNING (ref) = 1; 6930 } 6931 } 6932 else if (TREE_CODE (up_sub) == INTEGER_CST 6933 && (ignore_off_by_one 6934 ? !tree_int_cst_le (up_sub, up_bound_p1) 6935 : !tree_int_cst_le (up_sub, up_bound))) 6936 { 6937 if (dump_file && (dump_flags & TDF_DETAILS)) 6938 { 6939 fprintf (dump_file, "Array bound warning for "); 6940 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref); 6941 fprintf (dump_file, "\n"); 6942 } 6943 warning_at (location, OPT_Warray_bounds, 6944 "array subscript is above array bounds"); 6945 TREE_NO_WARNING (ref) = 1; 6946 } 6947 else if (TREE_CODE (low_sub) == INTEGER_CST 6948 && tree_int_cst_lt (low_sub, low_bound)) 6949 { 6950 if (dump_file && (dump_flags & TDF_DETAILS)) 6951 { 6952 fprintf (dump_file, "Array bound warning for "); 6953 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref); 6954 fprintf (dump_file, "\n"); 6955 } 6956 warning_at (location, OPT_Warray_bounds, 6957 "array subscript is below array bounds"); 6958 TREE_NO_WARNING (ref) = 1; 6959 } 6960 } 6961 6962 /* Searches if the expr T, located at LOCATION computes 6963 address of an ARRAY_REF, and call check_array_ref on it. */ 6964 6965 static void 6966 search_for_addr_array (tree t, location_t location) 6967 { 6968 /* Check each ARRAY_REFs in the reference chain. */ 6969 do 6970 { 6971 if (TREE_CODE (t) == ARRAY_REF) 6972 check_array_ref (location, t, true /*ignore_off_by_one*/); 6973 6974 t = TREE_OPERAND (t, 0); 6975 } 6976 while (handled_component_p (t)); 6977 6978 if (TREE_CODE (t) == MEM_REF 6979 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR 6980 && !TREE_NO_WARNING (t)) 6981 { 6982 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0); 6983 tree low_bound, up_bound, el_sz; 6984 offset_int idx; 6985 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE 6986 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE 6987 || !TYPE_DOMAIN (TREE_TYPE (tem))) 6988 return; 6989 6990 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem))); 6991 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem))); 6992 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem))); 6993 if (!low_bound 6994 || TREE_CODE (low_bound) != INTEGER_CST 6995 || !up_bound 6996 || TREE_CODE (up_bound) != INTEGER_CST 6997 || !el_sz 6998 || TREE_CODE (el_sz) != INTEGER_CST) 6999 return; 7000 7001 idx = mem_ref_offset (t); 7002 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz)); 7003 if (idx < 0) 7004 { 7005 if (dump_file && (dump_flags & TDF_DETAILS)) 7006 { 7007 fprintf (dump_file, "Array bound warning for "); 7008 dump_generic_expr (MSG_NOTE, TDF_SLIM, t); 7009 fprintf (dump_file, "\n"); 7010 } 7011 warning_at (location, OPT_Warray_bounds, 7012 "array subscript is below array bounds"); 7013 TREE_NO_WARNING (t) = 1; 7014 } 7015 else if (idx > (wi::to_offset (up_bound) 7016 - wi::to_offset (low_bound) + 1)) 7017 { 7018 if (dump_file && (dump_flags & TDF_DETAILS)) 7019 { 7020 fprintf (dump_file, "Array bound warning for "); 7021 dump_generic_expr (MSG_NOTE, TDF_SLIM, t); 7022 fprintf (dump_file, "\n"); 7023 } 7024 warning_at (location, OPT_Warray_bounds, 7025 "array subscript is above array bounds"); 7026 TREE_NO_WARNING (t) = 1; 7027 } 7028 } 7029 } 7030 7031 /* walk_tree() callback that checks if *TP is 7032 an ARRAY_REF inside an ADDR_EXPR (in which an array 7033 subscript one outside the valid range is allowed). Call 7034 check_array_ref for each ARRAY_REF found. The location is 7035 passed in DATA. */ 7036 7037 static tree 7038 check_array_bounds (tree *tp, int *walk_subtree, void *data) 7039 { 7040 tree t = *tp; 7041 struct walk_stmt_info *wi = (struct walk_stmt_info *) data; 7042 location_t location; 7043 7044 if (EXPR_HAS_LOCATION (t)) 7045 location = EXPR_LOCATION (t); 7046 else 7047 { 7048 location_t *locp = (location_t *) wi->info; 7049 location = *locp; 7050 } 7051 7052 *walk_subtree = TRUE; 7053 7054 if (TREE_CODE (t) == ARRAY_REF) 7055 check_array_ref (location, t, false /*ignore_off_by_one*/); 7056 7057 else if (TREE_CODE (t) == ADDR_EXPR) 7058 { 7059 search_for_addr_array (t, location); 7060 *walk_subtree = FALSE; 7061 } 7062 7063 return NULL_TREE; 7064 } 7065 7066 /* Walk over all statements of all reachable BBs and call check_array_bounds 7067 on them. */ 7068 7069 static void 7070 check_all_array_refs (void) 7071 { 7072 basic_block bb; 7073 gimple_stmt_iterator si; 7074 7075 FOR_EACH_BB_FN (bb, cfun) 7076 { 7077 edge_iterator ei; 7078 edge e; 7079 bool executable = false; 7080 7081 /* Skip blocks that were found to be unreachable. */ 7082 FOR_EACH_EDGE (e, ei, bb->preds) 7083 executable |= !!(e->flags & EDGE_EXECUTABLE); 7084 if (!executable) 7085 continue; 7086 7087 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) 7088 { 7089 gimple *stmt = gsi_stmt (si); 7090 struct walk_stmt_info wi; 7091 if (!gimple_has_location (stmt) 7092 || is_gimple_debug (stmt)) 7093 continue; 7094 7095 memset (&wi, 0, sizeof (wi)); 7096 7097 location_t loc = gimple_location (stmt); 7098 wi.info = &loc; 7099 7100 walk_gimple_op (gsi_stmt (si), 7101 check_array_bounds, 7102 &wi); 7103 } 7104 } 7105 } 7106 7107 /* Return true if all imm uses of VAR are either in STMT, or 7108 feed (optionally through a chain of single imm uses) GIMPLE_COND 7109 in basic block COND_BB. */ 7110 7111 static bool 7112 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb) 7113 { 7114 use_operand_p use_p, use2_p; 7115 imm_use_iterator iter; 7116 7117 FOR_EACH_IMM_USE_FAST (use_p, iter, var) 7118 if (USE_STMT (use_p) != stmt) 7119 { 7120 gimple *use_stmt = USE_STMT (use_p), *use_stmt2; 7121 if (is_gimple_debug (use_stmt)) 7122 continue; 7123 while (is_gimple_assign (use_stmt) 7124 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME 7125 && single_imm_use (gimple_assign_lhs (use_stmt), 7126 &use2_p, &use_stmt2)) 7127 use_stmt = use_stmt2; 7128 if (gimple_code (use_stmt) != GIMPLE_COND 7129 || gimple_bb (use_stmt) != cond_bb) 7130 return false; 7131 } 7132 return true; 7133 } 7134 7135 /* Handle 7136 _4 = x_3 & 31; 7137 if (_4 != 0) 7138 goto <bb 6>; 7139 else 7140 goto <bb 7>; 7141 <bb 6>: 7142 __builtin_unreachable (); 7143 <bb 7>: 7144 x_5 = ASSERT_EXPR <x_3, ...>; 7145 If x_3 has no other immediate uses (checked by caller), 7146 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits 7147 from the non-zero bitmask. */ 7148 7149 static void 7150 maybe_set_nonzero_bits (basic_block bb, tree var) 7151 { 7152 edge e = single_pred_edge (bb); 7153 basic_block cond_bb = e->src; 7154 gimple *stmt = last_stmt (cond_bb); 7155 tree cst; 7156 7157 if (stmt == NULL 7158 || gimple_code (stmt) != GIMPLE_COND 7159 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE) 7160 ? EQ_EXPR : NE_EXPR) 7161 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME 7162 || !integer_zerop (gimple_cond_rhs (stmt))) 7163 return; 7164 7165 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt)); 7166 if (!is_gimple_assign (stmt) 7167 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR 7168 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST) 7169 return; 7170 if (gimple_assign_rhs1 (stmt) != var) 7171 { 7172 gimple *stmt2; 7173 7174 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME) 7175 return; 7176 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); 7177 if (!gimple_assign_cast_p (stmt2) 7178 || gimple_assign_rhs1 (stmt2) != var 7179 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2)) 7180 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt))) 7181 != TYPE_PRECISION (TREE_TYPE (var)))) 7182 return; 7183 } 7184 cst = gimple_assign_rhs2 (stmt); 7185 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst)); 7186 } 7187 7188 /* Convert range assertion expressions into the implied copies and 7189 copy propagate away the copies. Doing the trivial copy propagation 7190 here avoids the need to run the full copy propagation pass after 7191 VRP. 7192 7193 FIXME, this will eventually lead to copy propagation removing the 7194 names that had useful range information attached to them. For 7195 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>, 7196 then N_i will have the range [3, +INF]. 7197 7198 However, by converting the assertion into the implied copy 7199 operation N_i = N_j, we will then copy-propagate N_j into the uses 7200 of N_i and lose the range information. We may want to hold on to 7201 ASSERT_EXPRs a little while longer as the ranges could be used in 7202 things like jump threading. 7203 7204 The problem with keeping ASSERT_EXPRs around is that passes after 7205 VRP need to handle them appropriately. 7206 7207 Another approach would be to make the range information a first 7208 class property of the SSA_NAME so that it can be queried from 7209 any pass. This is made somewhat more complex by the need for 7210 multiple ranges to be associated with one SSA_NAME. */ 7211 7212 static void 7213 remove_range_assertions (void) 7214 { 7215 basic_block bb; 7216 gimple_stmt_iterator si; 7217 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of 7218 a basic block preceeded by GIMPLE_COND branching to it and 7219 __builtin_trap, -1 if not yet checked, 0 otherwise. */ 7220 int is_unreachable; 7221 7222 /* Note that the BSI iterator bump happens at the bottom of the 7223 loop and no bump is necessary if we're removing the statement 7224 referenced by the current BSI. */ 7225 FOR_EACH_BB_FN (bb, cfun) 7226 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);) 7227 { 7228 gimple *stmt = gsi_stmt (si); 7229 7230 if (is_gimple_assign (stmt) 7231 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR) 7232 { 7233 tree lhs = gimple_assign_lhs (stmt); 7234 tree rhs = gimple_assign_rhs1 (stmt); 7235 tree var; 7236 7237 var = ASSERT_EXPR_VAR (rhs); 7238 7239 if (TREE_CODE (var) == SSA_NAME 7240 && !POINTER_TYPE_P (TREE_TYPE (lhs)) 7241 && SSA_NAME_RANGE_INFO (lhs)) 7242 { 7243 if (is_unreachable == -1) 7244 { 7245 is_unreachable = 0; 7246 if (single_pred_p (bb) 7247 && assert_unreachable_fallthru_edge_p 7248 (single_pred_edge (bb))) 7249 is_unreachable = 1; 7250 } 7251 /* Handle 7252 if (x_7 >= 10 && x_7 < 20) 7253 __builtin_unreachable (); 7254 x_8 = ASSERT_EXPR <x_7, ...>; 7255 if the only uses of x_7 are in the ASSERT_EXPR and 7256 in the condition. In that case, we can copy the 7257 range info from x_8 computed in this pass also 7258 for x_7. */ 7259 if (is_unreachable 7260 && all_imm_uses_in_stmt_or_feed_cond (var, stmt, 7261 single_pred (bb))) 7262 { 7263 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs), 7264 SSA_NAME_RANGE_INFO (lhs)->get_min (), 7265 SSA_NAME_RANGE_INFO (lhs)->get_max ()); 7266 maybe_set_nonzero_bits (bb, var); 7267 } 7268 } 7269 7270 /* Propagate the RHS into every use of the LHS. For SSA names 7271 also propagate abnormals as it merely restores the original 7272 IL in this case (an replace_uses_by would assert). */ 7273 if (TREE_CODE (var) == SSA_NAME) 7274 { 7275 imm_use_iterator iter; 7276 use_operand_p use_p; 7277 gimple *use_stmt; 7278 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs) 7279 FOR_EACH_IMM_USE_ON_STMT (use_p, iter) 7280 SET_USE (use_p, var); 7281 } 7282 else 7283 replace_uses_by (lhs, var); 7284 7285 /* And finally, remove the copy, it is not needed. */ 7286 gsi_remove (&si, true); 7287 release_defs (stmt); 7288 } 7289 else 7290 { 7291 if (!is_gimple_debug (gsi_stmt (si))) 7292 is_unreachable = 0; 7293 gsi_next (&si); 7294 } 7295 } 7296 } 7297 7298 7299 /* Return true if STMT is interesting for VRP. */ 7300 7301 static bool 7302 stmt_interesting_for_vrp (gimple *stmt) 7303 { 7304 if (gimple_code (stmt) == GIMPLE_PHI) 7305 { 7306 tree res = gimple_phi_result (stmt); 7307 return (!virtual_operand_p (res) 7308 && (INTEGRAL_TYPE_P (TREE_TYPE (res)) 7309 || POINTER_TYPE_P (TREE_TYPE (res)))); 7310 } 7311 else if (is_gimple_assign (stmt) || is_gimple_call (stmt)) 7312 { 7313 tree lhs = gimple_get_lhs (stmt); 7314 7315 /* In general, assignments with virtual operands are not useful 7316 for deriving ranges, with the obvious exception of calls to 7317 builtin functions. */ 7318 if (lhs && TREE_CODE (lhs) == SSA_NAME 7319 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs)) 7320 || POINTER_TYPE_P (TREE_TYPE (lhs))) 7321 && (is_gimple_call (stmt) 7322 || !gimple_vuse (stmt))) 7323 return true; 7324 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt)) 7325 switch (gimple_call_internal_fn (stmt)) 7326 { 7327 case IFN_ADD_OVERFLOW: 7328 case IFN_SUB_OVERFLOW: 7329 case IFN_MUL_OVERFLOW: 7330 case IFN_ATOMIC_COMPARE_EXCHANGE: 7331 /* These internal calls return _Complex integer type, 7332 but are interesting to VRP nevertheless. */ 7333 if (lhs && TREE_CODE (lhs) == SSA_NAME) 7334 return true; 7335 break; 7336 default: 7337 break; 7338 } 7339 } 7340 else if (gimple_code (stmt) == GIMPLE_COND 7341 || gimple_code (stmt) == GIMPLE_SWITCH) 7342 return true; 7343 7344 return false; 7345 } 7346 7347 /* Initialize VRP lattice. */ 7348 7349 static void 7350 vrp_initialize_lattice () 7351 { 7352 values_propagated = false; 7353 num_vr_values = num_ssa_names; 7354 vr_value = XCNEWVEC (value_range *, num_vr_values); 7355 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names); 7356 bitmap_obstack_initialize (&vrp_equiv_obstack); 7357 } 7358 7359 /* Initialization required by ssa_propagate engine. */ 7360 7361 static void 7362 vrp_initialize () 7363 { 7364 basic_block bb; 7365 7366 FOR_EACH_BB_FN (bb, cfun) 7367 { 7368 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si); 7369 gsi_next (&si)) 7370 { 7371 gphi *phi = si.phi (); 7372 if (!stmt_interesting_for_vrp (phi)) 7373 { 7374 tree lhs = PHI_RESULT (phi); 7375 set_value_range_to_varying (get_value_range (lhs)); 7376 prop_set_simulate_again (phi, false); 7377 } 7378 else 7379 prop_set_simulate_again (phi, true); 7380 } 7381 7382 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si); 7383 gsi_next (&si)) 7384 { 7385 gimple *stmt = gsi_stmt (si); 7386 7387 /* If the statement is a control insn, then we do not 7388 want to avoid simulating the statement once. Failure 7389 to do so means that those edges will never get added. */ 7390 if (stmt_ends_bb_p (stmt)) 7391 prop_set_simulate_again (stmt, true); 7392 else if (!stmt_interesting_for_vrp (stmt)) 7393 { 7394 set_defs_to_varying (stmt); 7395 prop_set_simulate_again (stmt, false); 7396 } 7397 else 7398 prop_set_simulate_again (stmt, true); 7399 } 7400 } 7401 } 7402 7403 /* Return the singleton value-range for NAME or NAME. */ 7404 7405 static inline tree 7406 vrp_valueize (tree name) 7407 { 7408 if (TREE_CODE (name) == SSA_NAME) 7409 { 7410 value_range *vr = get_value_range (name); 7411 if (vr->type == VR_RANGE 7412 && (TREE_CODE (vr->min) == SSA_NAME 7413 || is_gimple_min_invariant (vr->min)) 7414 && vrp_operand_equal_p (vr->min, vr->max)) 7415 return vr->min; 7416 } 7417 return name; 7418 } 7419 7420 /* Return the singleton value-range for NAME if that is a constant 7421 but signal to not follow SSA edges. */ 7422 7423 static inline tree 7424 vrp_valueize_1 (tree name) 7425 { 7426 if (TREE_CODE (name) == SSA_NAME) 7427 { 7428 /* If the definition may be simulated again we cannot follow 7429 this SSA edge as the SSA propagator does not necessarily 7430 re-visit the use. */ 7431 gimple *def_stmt = SSA_NAME_DEF_STMT (name); 7432 if (!gimple_nop_p (def_stmt) 7433 && prop_simulate_again_p (def_stmt)) 7434 return NULL_TREE; 7435 value_range *vr = get_value_range (name); 7436 if (range_int_cst_singleton_p (vr)) 7437 return vr->min; 7438 } 7439 return name; 7440 } 7441 7442 /* Visit assignment STMT. If it produces an interesting range, record 7443 the range in VR and set LHS to OUTPUT_P. */ 7444 7445 static void 7446 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p, value_range *vr) 7447 { 7448 tree lhs; 7449 enum gimple_code code = gimple_code (stmt); 7450 lhs = gimple_get_lhs (stmt); 7451 *output_p = NULL_TREE; 7452 7453 /* We only keep track of ranges in integral and pointer types. */ 7454 if (TREE_CODE (lhs) == SSA_NAME 7455 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs)) 7456 /* It is valid to have NULL MIN/MAX values on a type. See 7457 build_range_type. */ 7458 && TYPE_MIN_VALUE (TREE_TYPE (lhs)) 7459 && TYPE_MAX_VALUE (TREE_TYPE (lhs))) 7460 || POINTER_TYPE_P (TREE_TYPE (lhs)))) 7461 { 7462 *output_p = lhs; 7463 7464 /* Try folding the statement to a constant first. */ 7465 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize, 7466 vrp_valueize_1); 7467 if (tem) 7468 { 7469 if (TREE_CODE (tem) == SSA_NAME 7470 && (SSA_NAME_IS_DEFAULT_DEF (tem) 7471 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem)))) 7472 { 7473 extract_range_from_ssa_name (vr, tem); 7474 return; 7475 } 7476 else if (is_gimple_min_invariant (tem)) 7477 { 7478 set_value_range_to_value (vr, tem, NULL); 7479 return; 7480 } 7481 } 7482 /* Then dispatch to value-range extracting functions. */ 7483 if (code == GIMPLE_CALL) 7484 extract_range_basic (vr, stmt); 7485 else 7486 extract_range_from_assignment (vr, as_a <gassign *> (stmt)); 7487 } 7488 } 7489 7490 /* Helper that gets the value range of the SSA_NAME with version I 7491 or a symbolic range containing the SSA_NAME only if the value range 7492 is varying or undefined. */ 7493 7494 static inline value_range 7495 get_vr_for_comparison (int i) 7496 { 7497 value_range vr = *get_value_range (ssa_name (i)); 7498 7499 /* If name N_i does not have a valid range, use N_i as its own 7500 range. This allows us to compare against names that may 7501 have N_i in their ranges. */ 7502 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED) 7503 { 7504 vr.type = VR_RANGE; 7505 vr.min = ssa_name (i); 7506 vr.max = ssa_name (i); 7507 } 7508 7509 return vr; 7510 } 7511 7512 /* Compare all the value ranges for names equivalent to VAR with VAL 7513 using comparison code COMP. Return the same value returned by 7514 compare_range_with_value, including the setting of 7515 *STRICT_OVERFLOW_P. */ 7516 7517 static tree 7518 compare_name_with_value (enum tree_code comp, tree var, tree val, 7519 bool *strict_overflow_p, bool use_equiv_p) 7520 { 7521 bitmap_iterator bi; 7522 unsigned i; 7523 bitmap e; 7524 tree retval, t; 7525 int used_strict_overflow; 7526 bool sop; 7527 value_range equiv_vr; 7528 7529 /* Get the set of equivalences for VAR. */ 7530 e = get_value_range (var)->equiv; 7531 7532 /* Start at -1. Set it to 0 if we do a comparison without relying 7533 on overflow, or 1 if all comparisons rely on overflow. */ 7534 used_strict_overflow = -1; 7535 7536 /* Compare vars' value range with val. */ 7537 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var)); 7538 sop = false; 7539 retval = compare_range_with_value (comp, &equiv_vr, val, &sop); 7540 if (retval) 7541 used_strict_overflow = sop ? 1 : 0; 7542 7543 /* If the equiv set is empty we have done all work we need to do. */ 7544 if (e == NULL) 7545 { 7546 if (retval 7547 && used_strict_overflow > 0) 7548 *strict_overflow_p = true; 7549 return retval; 7550 } 7551 7552 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi) 7553 { 7554 tree name = ssa_name (i); 7555 if (! name) 7556 continue; 7557 7558 if (! use_equiv_p 7559 && ! SSA_NAME_IS_DEFAULT_DEF (name) 7560 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name))) 7561 continue; 7562 7563 equiv_vr = get_vr_for_comparison (i); 7564 sop = false; 7565 t = compare_range_with_value (comp, &equiv_vr, val, &sop); 7566 if (t) 7567 { 7568 /* If we get different answers from different members 7569 of the equivalence set this check must be in a dead 7570 code region. Folding it to a trap representation 7571 would be correct here. For now just return don't-know. */ 7572 if (retval != NULL 7573 && t != retval) 7574 { 7575 retval = NULL_TREE; 7576 break; 7577 } 7578 retval = t; 7579 7580 if (!sop) 7581 used_strict_overflow = 0; 7582 else if (used_strict_overflow < 0) 7583 used_strict_overflow = 1; 7584 } 7585 } 7586 7587 if (retval 7588 && used_strict_overflow > 0) 7589 *strict_overflow_p = true; 7590 7591 return retval; 7592 } 7593 7594 7595 /* Given a comparison code COMP and names N1 and N2, compare all the 7596 ranges equivalent to N1 against all the ranges equivalent to N2 7597 to determine the value of N1 COMP N2. Return the same value 7598 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate 7599 whether we relied on an overflow infinity in the comparison. */ 7600 7601 7602 static tree 7603 compare_names (enum tree_code comp, tree n1, tree n2, 7604 bool *strict_overflow_p) 7605 { 7606 tree t, retval; 7607 bitmap e1, e2; 7608 bitmap_iterator bi1, bi2; 7609 unsigned i1, i2; 7610 int used_strict_overflow; 7611 static bitmap_obstack *s_obstack = NULL; 7612 static bitmap s_e1 = NULL, s_e2 = NULL; 7613 7614 /* Compare the ranges of every name equivalent to N1 against the 7615 ranges of every name equivalent to N2. */ 7616 e1 = get_value_range (n1)->equiv; 7617 e2 = get_value_range (n2)->equiv; 7618 7619 /* Use the fake bitmaps if e1 or e2 are not available. */ 7620 if (s_obstack == NULL) 7621 { 7622 s_obstack = XNEW (bitmap_obstack); 7623 bitmap_obstack_initialize (s_obstack); 7624 s_e1 = BITMAP_ALLOC (s_obstack); 7625 s_e2 = BITMAP_ALLOC (s_obstack); 7626 } 7627 if (e1 == NULL) 7628 e1 = s_e1; 7629 if (e2 == NULL) 7630 e2 = s_e2; 7631 7632 /* Add N1 and N2 to their own set of equivalences to avoid 7633 duplicating the body of the loop just to check N1 and N2 7634 ranges. */ 7635 bitmap_set_bit (e1, SSA_NAME_VERSION (n1)); 7636 bitmap_set_bit (e2, SSA_NAME_VERSION (n2)); 7637 7638 /* If the equivalence sets have a common intersection, then the two 7639 names can be compared without checking their ranges. */ 7640 if (bitmap_intersect_p (e1, e2)) 7641 { 7642 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1)); 7643 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2)); 7644 7645 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR) 7646 ? boolean_true_node 7647 : boolean_false_node; 7648 } 7649 7650 /* Start at -1. Set it to 0 if we do a comparison without relying 7651 on overflow, or 1 if all comparisons rely on overflow. */ 7652 used_strict_overflow = -1; 7653 7654 /* Otherwise, compare all the equivalent ranges. First, add N1 and 7655 N2 to their own set of equivalences to avoid duplicating the body 7656 of the loop just to check N1 and N2 ranges. */ 7657 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1) 7658 { 7659 if (! ssa_name (i1)) 7660 continue; 7661 7662 value_range vr1 = get_vr_for_comparison (i1); 7663 7664 t = retval = NULL_TREE; 7665 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2) 7666 { 7667 if (! ssa_name (i2)) 7668 continue; 7669 7670 bool sop = false; 7671 7672 value_range vr2 = get_vr_for_comparison (i2); 7673 7674 t = compare_ranges (comp, &vr1, &vr2, &sop); 7675 if (t) 7676 { 7677 /* If we get different answers from different members 7678 of the equivalence set this check must be in a dead 7679 code region. Folding it to a trap representation 7680 would be correct here. For now just return don't-know. */ 7681 if (retval != NULL 7682 && t != retval) 7683 { 7684 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1)); 7685 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2)); 7686 return NULL_TREE; 7687 } 7688 retval = t; 7689 7690 if (!sop) 7691 used_strict_overflow = 0; 7692 else if (used_strict_overflow < 0) 7693 used_strict_overflow = 1; 7694 } 7695 } 7696 7697 if (retval) 7698 { 7699 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1)); 7700 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2)); 7701 if (used_strict_overflow > 0) 7702 *strict_overflow_p = true; 7703 return retval; 7704 } 7705 } 7706 7707 /* None of the equivalent ranges are useful in computing this 7708 comparison. */ 7709 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1)); 7710 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2)); 7711 return NULL_TREE; 7712 } 7713 7714 /* Helper function for vrp_evaluate_conditional_warnv & other 7715 optimizers. */ 7716 7717 static tree 7718 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code, 7719 tree op0, tree op1, 7720 bool * strict_overflow_p) 7721 { 7722 value_range *vr0, *vr1; 7723 7724 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL; 7725 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL; 7726 7727 tree res = NULL_TREE; 7728 if (vr0 && vr1) 7729 res = compare_ranges (code, vr0, vr1, strict_overflow_p); 7730 if (!res && vr0) 7731 res = compare_range_with_value (code, vr0, op1, strict_overflow_p); 7732 if (!res && vr1) 7733 res = (compare_range_with_value 7734 (swap_tree_comparison (code), vr1, op0, strict_overflow_p)); 7735 return res; 7736 } 7737 7738 /* Helper function for vrp_evaluate_conditional_warnv. */ 7739 7740 static tree 7741 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0, 7742 tree op1, bool use_equiv_p, 7743 bool *strict_overflow_p, bool *only_ranges) 7744 { 7745 tree ret; 7746 if (only_ranges) 7747 *only_ranges = true; 7748 7749 /* We only deal with integral and pointer types. */ 7750 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)) 7751 && !POINTER_TYPE_P (TREE_TYPE (op0))) 7752 return NULL_TREE; 7753 7754 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed 7755 as a simple equality test, then prefer that over its current form 7756 for evaluation. 7757 7758 An overflow test which collapses to an equality test can always be 7759 expressed as a comparison of one argument against zero. Overflow 7760 occurs when the chosen argument is zero and does not occur if the 7761 chosen argument is not zero. */ 7762 tree x; 7763 if (overflow_comparison_p (code, op0, op1, use_equiv_p, &x)) 7764 { 7765 wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED); 7766 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0) 7767 B = A - 1; if (A > B) -> B = A - 1; if (A != 0) 7768 B = A + 1; if (B < A) -> B = A + 1; if (B == 0) 7769 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */ 7770 if (integer_zerop (x)) 7771 { 7772 op1 = x; 7773 code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR; 7774 } 7775 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0) 7776 B = A + 1; if (A < B) -> B = A + 1; if (B != 0) 7777 B = A - 1; if (B > A) -> B = A - 1; if (A == 0) 7778 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */ 7779 else if (wi::eq_p (x, max - 1)) 7780 { 7781 op0 = op1; 7782 op1 = wide_int_to_tree (TREE_TYPE (op0), 0); 7783 code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR; 7784 } 7785 } 7786 7787 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges 7788 (code, op0, op1, strict_overflow_p))) 7789 return ret; 7790 if (only_ranges) 7791 *only_ranges = false; 7792 /* Do not use compare_names during propagation, it's quadratic. */ 7793 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME 7794 && use_equiv_p) 7795 return compare_names (code, op0, op1, strict_overflow_p); 7796 else if (TREE_CODE (op0) == SSA_NAME) 7797 return compare_name_with_value (code, op0, op1, 7798 strict_overflow_p, use_equiv_p); 7799 else if (TREE_CODE (op1) == SSA_NAME) 7800 return compare_name_with_value (swap_tree_comparison (code), op1, op0, 7801 strict_overflow_p, use_equiv_p); 7802 return NULL_TREE; 7803 } 7804 7805 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range 7806 information. Return NULL if the conditional can not be evaluated. 7807 The ranges of all the names equivalent with the operands in COND 7808 will be used when trying to compute the value. If the result is 7809 based on undefined signed overflow, issue a warning if 7810 appropriate. */ 7811 7812 static tree 7813 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt) 7814 { 7815 bool sop; 7816 tree ret; 7817 bool only_ranges; 7818 7819 /* Some passes and foldings leak constants with overflow flag set 7820 into the IL. Avoid doing wrong things with these and bail out. */ 7821 if ((TREE_CODE (op0) == INTEGER_CST 7822 && TREE_OVERFLOW (op0)) 7823 || (TREE_CODE (op1) == INTEGER_CST 7824 && TREE_OVERFLOW (op1))) 7825 return NULL_TREE; 7826 7827 sop = false; 7828 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop, 7829 &only_ranges); 7830 7831 if (ret && sop) 7832 { 7833 enum warn_strict_overflow_code wc; 7834 const char* warnmsg; 7835 7836 if (is_gimple_min_invariant (ret)) 7837 { 7838 wc = WARN_STRICT_OVERFLOW_CONDITIONAL; 7839 warnmsg = G_("assuming signed overflow does not occur when " 7840 "simplifying conditional to constant"); 7841 } 7842 else 7843 { 7844 wc = WARN_STRICT_OVERFLOW_COMPARISON; 7845 warnmsg = G_("assuming signed overflow does not occur when " 7846 "simplifying conditional"); 7847 } 7848 7849 if (issue_strict_overflow_warning (wc)) 7850 { 7851 location_t location; 7852 7853 if (!gimple_has_location (stmt)) 7854 location = input_location; 7855 else 7856 location = gimple_location (stmt); 7857 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg); 7858 } 7859 } 7860 7861 if (warn_type_limits 7862 && ret && only_ranges 7863 && TREE_CODE_CLASS (code) == tcc_comparison 7864 && TREE_CODE (op0) == SSA_NAME) 7865 { 7866 /* If the comparison is being folded and the operand on the LHS 7867 is being compared against a constant value that is outside of 7868 the natural range of OP0's type, then the predicate will 7869 always fold regardless of the value of OP0. If -Wtype-limits 7870 was specified, emit a warning. */ 7871 tree type = TREE_TYPE (op0); 7872 value_range *vr0 = get_value_range (op0); 7873 7874 if (vr0->type == VR_RANGE 7875 && INTEGRAL_TYPE_P (type) 7876 && vrp_val_is_min (vr0->min) 7877 && vrp_val_is_max (vr0->max) 7878 && is_gimple_min_invariant (op1)) 7879 { 7880 location_t location; 7881 7882 if (!gimple_has_location (stmt)) 7883 location = input_location; 7884 else 7885 location = gimple_location (stmt); 7886 7887 warning_at (location, OPT_Wtype_limits, 7888 integer_zerop (ret) 7889 ? G_("comparison always false " 7890 "due to limited range of data type") 7891 : G_("comparison always true " 7892 "due to limited range of data type")); 7893 } 7894 } 7895 7896 return ret; 7897 } 7898 7899 7900 /* Visit conditional statement STMT. If we can determine which edge 7901 will be taken out of STMT's basic block, record it in 7902 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */ 7903 7904 static void 7905 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p) 7906 { 7907 tree val; 7908 bool sop; 7909 7910 *taken_edge_p = NULL; 7911 7912 if (dump_file && (dump_flags & TDF_DETAILS)) 7913 { 7914 tree use; 7915 ssa_op_iter i; 7916 7917 fprintf (dump_file, "\nVisiting conditional with predicate: "); 7918 print_gimple_stmt (dump_file, stmt, 0, 0); 7919 fprintf (dump_file, "\nWith known ranges\n"); 7920 7921 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE) 7922 { 7923 fprintf (dump_file, "\t"); 7924 print_generic_expr (dump_file, use, 0); 7925 fprintf (dump_file, ": "); 7926 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]); 7927 } 7928 7929 fprintf (dump_file, "\n"); 7930 } 7931 7932 /* Compute the value of the predicate COND by checking the known 7933 ranges of each of its operands. 7934 7935 Note that we cannot evaluate all the equivalent ranges here 7936 because those ranges may not yet be final and with the current 7937 propagation strategy, we cannot determine when the value ranges 7938 of the names in the equivalence set have changed. 7939 7940 For instance, given the following code fragment 7941 7942 i_5 = PHI <8, i_13> 7943 ... 7944 i_14 = ASSERT_EXPR <i_5, i_5 != 0> 7945 if (i_14 == 1) 7946 ... 7947 7948 Assume that on the first visit to i_14, i_5 has the temporary 7949 range [8, 8] because the second argument to the PHI function is 7950 not yet executable. We derive the range ~[0, 0] for i_14 and the 7951 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for 7952 the first time, since i_14 is equivalent to the range [8, 8], we 7953 determine that the predicate is always false. 7954 7955 On the next round of propagation, i_13 is determined to be 7956 VARYING, which causes i_5 to drop down to VARYING. So, another 7957 visit to i_14 is scheduled. In this second visit, we compute the 7958 exact same range and equivalence set for i_14, namely ~[0, 0] and 7959 { i_5 }. But we did not have the previous range for i_5 7960 registered, so vrp_visit_assignment thinks that the range for 7961 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)' 7962 is not visited again, which stops propagation from visiting 7963 statements in the THEN clause of that if(). 7964 7965 To properly fix this we would need to keep the previous range 7966 value for the names in the equivalence set. This way we would've 7967 discovered that from one visit to the other i_5 changed from 7968 range [8, 8] to VR_VARYING. 7969 7970 However, fixing this apparent limitation may not be worth the 7971 additional checking. Testing on several code bases (GCC, DLV, 7972 MICO, TRAMP3D and SPEC2000) showed that doing this results in 7973 4 more predicates folded in SPEC. */ 7974 sop = false; 7975 7976 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt), 7977 gimple_cond_lhs (stmt), 7978 gimple_cond_rhs (stmt), 7979 false, &sop, NULL); 7980 if (val) 7981 { 7982 if (!sop) 7983 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val); 7984 else 7985 { 7986 if (dump_file && (dump_flags & TDF_DETAILS)) 7987 fprintf (dump_file, 7988 "\nIgnoring predicate evaluation because " 7989 "it assumes that signed overflow is undefined"); 7990 val = NULL_TREE; 7991 } 7992 } 7993 7994 if (dump_file && (dump_flags & TDF_DETAILS)) 7995 { 7996 fprintf (dump_file, "\nPredicate evaluates to: "); 7997 if (val == NULL_TREE) 7998 fprintf (dump_file, "DON'T KNOW\n"); 7999 else 8000 print_generic_stmt (dump_file, val, 0); 8001 } 8002 } 8003 8004 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL 8005 that includes the value VAL. The search is restricted to the range 8006 [START_IDX, n - 1] where n is the size of VEC. 8007 8008 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is 8009 returned. 8010 8011 If there is no CASE_LABEL for VAL and there is one that is larger than VAL, 8012 it is placed in IDX and false is returned. 8013 8014 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is 8015 returned. */ 8016 8017 static bool 8018 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx) 8019 { 8020 size_t n = gimple_switch_num_labels (stmt); 8021 size_t low, high; 8022 8023 /* Find case label for minimum of the value range or the next one. 8024 At each iteration we are searching in [low, high - 1]. */ 8025 8026 for (low = start_idx, high = n; high != low; ) 8027 { 8028 tree t; 8029 int cmp; 8030 /* Note that i != high, so we never ask for n. */ 8031 size_t i = (high + low) / 2; 8032 t = gimple_switch_label (stmt, i); 8033 8034 /* Cache the result of comparing CASE_LOW and val. */ 8035 cmp = tree_int_cst_compare (CASE_LOW (t), val); 8036 8037 if (cmp == 0) 8038 { 8039 /* Ranges cannot be empty. */ 8040 *idx = i; 8041 return true; 8042 } 8043 else if (cmp > 0) 8044 high = i; 8045 else 8046 { 8047 low = i + 1; 8048 if (CASE_HIGH (t) != NULL 8049 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0) 8050 { 8051 *idx = i; 8052 return true; 8053 } 8054 } 8055 } 8056 8057 *idx = high; 8058 return false; 8059 } 8060 8061 /* Searches the case label vector VEC for the range of CASE_LABELs that is used 8062 for values between MIN and MAX. The first index is placed in MIN_IDX. The 8063 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty 8064 then MAX_IDX < MIN_IDX. 8065 Returns true if the default label is not needed. */ 8066 8067 static bool 8068 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx, 8069 size_t *max_idx) 8070 { 8071 size_t i, j; 8072 bool min_take_default = !find_case_label_index (stmt, 1, min, &i); 8073 bool max_take_default = !find_case_label_index (stmt, i, max, &j); 8074 8075 if (i == j 8076 && min_take_default 8077 && max_take_default) 8078 { 8079 /* Only the default case label reached. 8080 Return an empty range. */ 8081 *min_idx = 1; 8082 *max_idx = 0; 8083 return false; 8084 } 8085 else 8086 { 8087 bool take_default = min_take_default || max_take_default; 8088 tree low, high; 8089 size_t k; 8090 8091 if (max_take_default) 8092 j--; 8093 8094 /* If the case label range is continuous, we do not need 8095 the default case label. Verify that. */ 8096 high = CASE_LOW (gimple_switch_label (stmt, i)); 8097 if (CASE_HIGH (gimple_switch_label (stmt, i))) 8098 high = CASE_HIGH (gimple_switch_label (stmt, i)); 8099 for (k = i + 1; k <= j; ++k) 8100 { 8101 low = CASE_LOW (gimple_switch_label (stmt, k)); 8102 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high))) 8103 { 8104 take_default = true; 8105 break; 8106 } 8107 high = low; 8108 if (CASE_HIGH (gimple_switch_label (stmt, k))) 8109 high = CASE_HIGH (gimple_switch_label (stmt, k)); 8110 } 8111 8112 *min_idx = i; 8113 *max_idx = j; 8114 return !take_default; 8115 } 8116 } 8117 8118 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are 8119 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and 8120 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1. 8121 Returns true if the default label is not needed. */ 8122 8123 static bool 8124 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1, 8125 size_t *max_idx1, size_t *min_idx2, 8126 size_t *max_idx2) 8127 { 8128 size_t i, j, k, l; 8129 unsigned int n = gimple_switch_num_labels (stmt); 8130 bool take_default; 8131 tree case_low, case_high; 8132 tree min = vr->min, max = vr->max; 8133 8134 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE); 8135 8136 take_default = !find_case_label_range (stmt, min, max, &i, &j); 8137 8138 /* Set second range to emtpy. */ 8139 *min_idx2 = 1; 8140 *max_idx2 = 0; 8141 8142 if (vr->type == VR_RANGE) 8143 { 8144 *min_idx1 = i; 8145 *max_idx1 = j; 8146 return !take_default; 8147 } 8148 8149 /* Set first range to all case labels. */ 8150 *min_idx1 = 1; 8151 *max_idx1 = n - 1; 8152 8153 if (i > j) 8154 return false; 8155 8156 /* Make sure all the values of case labels [i , j] are contained in 8157 range [MIN, MAX]. */ 8158 case_low = CASE_LOW (gimple_switch_label (stmt, i)); 8159 case_high = CASE_HIGH (gimple_switch_label (stmt, j)); 8160 if (tree_int_cst_compare (case_low, min) < 0) 8161 i += 1; 8162 if (case_high != NULL_TREE 8163 && tree_int_cst_compare (max, case_high) < 0) 8164 j -= 1; 8165 8166 if (i > j) 8167 return false; 8168 8169 /* If the range spans case labels [i, j], the corresponding anti-range spans 8170 the labels [1, i - 1] and [j + 1, n - 1]. */ 8171 k = j + 1; 8172 l = n - 1; 8173 if (k > l) 8174 { 8175 k = 1; 8176 l = 0; 8177 } 8178 8179 j = i - 1; 8180 i = 1; 8181 if (i > j) 8182 { 8183 i = k; 8184 j = l; 8185 k = 1; 8186 l = 0; 8187 } 8188 8189 *min_idx1 = i; 8190 *max_idx1 = j; 8191 *min_idx2 = k; 8192 *max_idx2 = l; 8193 return false; 8194 } 8195 8196 /* Visit switch statement STMT. If we can determine which edge 8197 will be taken out of STMT's basic block, record it in 8198 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */ 8199 8200 static void 8201 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p) 8202 { 8203 tree op, val; 8204 value_range *vr; 8205 size_t i = 0, j = 0, k, l; 8206 bool take_default; 8207 8208 *taken_edge_p = NULL; 8209 op = gimple_switch_index (stmt); 8210 if (TREE_CODE (op) != SSA_NAME) 8211 return; 8212 8213 vr = get_value_range (op); 8214 if (dump_file && (dump_flags & TDF_DETAILS)) 8215 { 8216 fprintf (dump_file, "\nVisiting switch expression with operand "); 8217 print_generic_expr (dump_file, op, 0); 8218 fprintf (dump_file, " with known range "); 8219 dump_value_range (dump_file, vr); 8220 fprintf (dump_file, "\n"); 8221 } 8222 8223 if ((vr->type != VR_RANGE 8224 && vr->type != VR_ANTI_RANGE) 8225 || symbolic_range_p (vr)) 8226 return; 8227 8228 /* Find the single edge that is taken from the switch expression. */ 8229 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l); 8230 8231 /* Check if the range spans no CASE_LABEL. If so, we only reach the default 8232 label */ 8233 if (j < i) 8234 { 8235 gcc_assert (take_default); 8236 val = gimple_switch_default_label (stmt); 8237 } 8238 else 8239 { 8240 /* Check if labels with index i to j and maybe the default label 8241 are all reaching the same label. */ 8242 8243 val = gimple_switch_label (stmt, i); 8244 if (take_default 8245 && CASE_LABEL (gimple_switch_default_label (stmt)) 8246 != CASE_LABEL (val)) 8247 { 8248 if (dump_file && (dump_flags & TDF_DETAILS)) 8249 fprintf (dump_file, " not a single destination for this " 8250 "range\n"); 8251 return; 8252 } 8253 for (++i; i <= j; ++i) 8254 { 8255 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val)) 8256 { 8257 if (dump_file && (dump_flags & TDF_DETAILS)) 8258 fprintf (dump_file, " not a single destination for this " 8259 "range\n"); 8260 return; 8261 } 8262 } 8263 for (; k <= l; ++k) 8264 { 8265 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val)) 8266 { 8267 if (dump_file && (dump_flags & TDF_DETAILS)) 8268 fprintf (dump_file, " not a single destination for this " 8269 "range\n"); 8270 return; 8271 } 8272 } 8273 } 8274 8275 *taken_edge_p = find_edge (gimple_bb (stmt), 8276 label_to_block (CASE_LABEL (val))); 8277 8278 if (dump_file && (dump_flags & TDF_DETAILS)) 8279 { 8280 fprintf (dump_file, " will take edge to "); 8281 print_generic_stmt (dump_file, CASE_LABEL (val), 0); 8282 } 8283 } 8284 8285 8286 /* Evaluate statement STMT. If the statement produces a useful range, 8287 set VR and corepsponding OUTPUT_P. 8288 8289 If STMT is a conditional branch and we can determine its truth 8290 value, the taken edge is recorded in *TAKEN_EDGE_P. */ 8291 8292 static void 8293 extract_range_from_stmt (gimple *stmt, edge *taken_edge_p, 8294 tree *output_p, value_range *vr) 8295 { 8296 8297 if (dump_file && (dump_flags & TDF_DETAILS)) 8298 { 8299 fprintf (dump_file, "\nVisiting statement:\n"); 8300 print_gimple_stmt (dump_file, stmt, 0, dump_flags); 8301 } 8302 8303 if (!stmt_interesting_for_vrp (stmt)) 8304 gcc_assert (stmt_ends_bb_p (stmt)); 8305 else if (is_gimple_assign (stmt) || is_gimple_call (stmt)) 8306 vrp_visit_assignment_or_call (stmt, output_p, vr); 8307 else if (gimple_code (stmt) == GIMPLE_COND) 8308 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p); 8309 else if (gimple_code (stmt) == GIMPLE_SWITCH) 8310 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p); 8311 } 8312 8313 /* Evaluate statement STMT. If the statement produces a useful range, 8314 return SSA_PROP_INTERESTING and record the SSA name with the 8315 interesting range into *OUTPUT_P. 8316 8317 If STMT is a conditional branch and we can determine its truth 8318 value, the taken edge is recorded in *TAKEN_EDGE_P. 8319 8320 If STMT produces a varying value, return SSA_PROP_VARYING. */ 8321 8322 static enum ssa_prop_result 8323 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p) 8324 { 8325 value_range vr = VR_INITIALIZER; 8326 tree lhs = gimple_get_lhs (stmt); 8327 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr); 8328 8329 if (*output_p) 8330 { 8331 if (update_value_range (*output_p, &vr)) 8332 { 8333 if (dump_file && (dump_flags & TDF_DETAILS)) 8334 { 8335 fprintf (dump_file, "Found new range for "); 8336 print_generic_expr (dump_file, *output_p, 0); 8337 fprintf (dump_file, ": "); 8338 dump_value_range (dump_file, &vr); 8339 fprintf (dump_file, "\n"); 8340 } 8341 8342 if (vr.type == VR_VARYING) 8343 return SSA_PROP_VARYING; 8344 8345 return SSA_PROP_INTERESTING; 8346 } 8347 return SSA_PROP_NOT_INTERESTING; 8348 } 8349 8350 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt)) 8351 switch (gimple_call_internal_fn (stmt)) 8352 { 8353 case IFN_ADD_OVERFLOW: 8354 case IFN_SUB_OVERFLOW: 8355 case IFN_MUL_OVERFLOW: 8356 case IFN_ATOMIC_COMPARE_EXCHANGE: 8357 /* These internal calls return _Complex integer type, 8358 which VRP does not track, but the immediate uses 8359 thereof might be interesting. */ 8360 if (lhs && TREE_CODE (lhs) == SSA_NAME) 8361 { 8362 imm_use_iterator iter; 8363 use_operand_p use_p; 8364 enum ssa_prop_result res = SSA_PROP_VARYING; 8365 8366 set_value_range_to_varying (get_value_range (lhs)); 8367 8368 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs) 8369 { 8370 gimple *use_stmt = USE_STMT (use_p); 8371 if (!is_gimple_assign (use_stmt)) 8372 continue; 8373 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt); 8374 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR) 8375 continue; 8376 tree rhs1 = gimple_assign_rhs1 (use_stmt); 8377 tree use_lhs = gimple_assign_lhs (use_stmt); 8378 if (TREE_CODE (rhs1) != rhs_code 8379 || TREE_OPERAND (rhs1, 0) != lhs 8380 || TREE_CODE (use_lhs) != SSA_NAME 8381 || !stmt_interesting_for_vrp (use_stmt) 8382 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs)) 8383 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs)) 8384 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs)))) 8385 continue; 8386 8387 /* If there is a change in the value range for any of the 8388 REALPART_EXPR/IMAGPART_EXPR immediate uses, return 8389 SSA_PROP_INTERESTING. If there are any REALPART_EXPR 8390 or IMAGPART_EXPR immediate uses, but none of them have 8391 a change in their value ranges, return 8392 SSA_PROP_NOT_INTERESTING. If there are no 8393 {REAL,IMAG}PART_EXPR uses at all, 8394 return SSA_PROP_VARYING. */ 8395 value_range new_vr = VR_INITIALIZER; 8396 extract_range_basic (&new_vr, use_stmt); 8397 value_range *old_vr = get_value_range (use_lhs); 8398 if (old_vr->type != new_vr.type 8399 || !vrp_operand_equal_p (old_vr->min, new_vr.min) 8400 || !vrp_operand_equal_p (old_vr->max, new_vr.max) 8401 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv)) 8402 res = SSA_PROP_INTERESTING; 8403 else 8404 res = SSA_PROP_NOT_INTERESTING; 8405 BITMAP_FREE (new_vr.equiv); 8406 if (res == SSA_PROP_INTERESTING) 8407 { 8408 *output_p = lhs; 8409 return res; 8410 } 8411 } 8412 8413 return res; 8414 } 8415 break; 8416 default: 8417 break; 8418 } 8419 8420 /* All other statements produce nothing of interest for VRP, so mark 8421 their outputs varying and prevent further simulation. */ 8422 set_defs_to_varying (stmt); 8423 8424 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING; 8425 } 8426 8427 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and 8428 { VR1TYPE, VR0MIN, VR0MAX } and store the result 8429 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest 8430 possible such range. The resulting range is not canonicalized. */ 8431 8432 static void 8433 union_ranges (enum value_range_type *vr0type, 8434 tree *vr0min, tree *vr0max, 8435 enum value_range_type vr1type, 8436 tree vr1min, tree vr1max) 8437 { 8438 bool mineq = vrp_operand_equal_p (*vr0min, vr1min); 8439 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max); 8440 8441 /* [] is vr0, () is vr1 in the following classification comments. */ 8442 if (mineq && maxeq) 8443 { 8444 /* [( )] */ 8445 if (*vr0type == vr1type) 8446 /* Nothing to do for equal ranges. */ 8447 ; 8448 else if ((*vr0type == VR_RANGE 8449 && vr1type == VR_ANTI_RANGE) 8450 || (*vr0type == VR_ANTI_RANGE 8451 && vr1type == VR_RANGE)) 8452 { 8453 /* For anti-range with range union the result is varying. */ 8454 goto give_up; 8455 } 8456 else 8457 gcc_unreachable (); 8458 } 8459 else if (operand_less_p (*vr0max, vr1min) == 1 8460 || operand_less_p (vr1max, *vr0min) == 1) 8461 { 8462 /* [ ] ( ) or ( ) [ ] 8463 If the ranges have an empty intersection, result of the union 8464 operation is the anti-range or if both are anti-ranges 8465 it covers all. */ 8466 if (*vr0type == VR_ANTI_RANGE 8467 && vr1type == VR_ANTI_RANGE) 8468 goto give_up; 8469 else if (*vr0type == VR_ANTI_RANGE 8470 && vr1type == VR_RANGE) 8471 ; 8472 else if (*vr0type == VR_RANGE 8473 && vr1type == VR_ANTI_RANGE) 8474 { 8475 *vr0type = vr1type; 8476 *vr0min = vr1min; 8477 *vr0max = vr1max; 8478 } 8479 else if (*vr0type == VR_RANGE 8480 && vr1type == VR_RANGE) 8481 { 8482 /* The result is the convex hull of both ranges. */ 8483 if (operand_less_p (*vr0max, vr1min) == 1) 8484 { 8485 /* If the result can be an anti-range, create one. */ 8486 if (TREE_CODE (*vr0max) == INTEGER_CST 8487 && TREE_CODE (vr1min) == INTEGER_CST 8488 && vrp_val_is_min (*vr0min) 8489 && vrp_val_is_max (vr1max)) 8490 { 8491 tree min = int_const_binop (PLUS_EXPR, 8492 *vr0max, 8493 build_int_cst (TREE_TYPE (*vr0max), 1)); 8494 tree max = int_const_binop (MINUS_EXPR, 8495 vr1min, 8496 build_int_cst (TREE_TYPE (vr1min), 1)); 8497 if (!operand_less_p (max, min)) 8498 { 8499 *vr0type = VR_ANTI_RANGE; 8500 *vr0min = min; 8501 *vr0max = max; 8502 } 8503 else 8504 *vr0max = vr1max; 8505 } 8506 else 8507 *vr0max = vr1max; 8508 } 8509 else 8510 { 8511 /* If the result can be an anti-range, create one. */ 8512 if (TREE_CODE (vr1max) == INTEGER_CST 8513 && TREE_CODE (*vr0min) == INTEGER_CST 8514 && vrp_val_is_min (vr1min) 8515 && vrp_val_is_max (*vr0max)) 8516 { 8517 tree min = int_const_binop (PLUS_EXPR, 8518 vr1max, 8519 build_int_cst (TREE_TYPE (vr1max), 1)); 8520 tree max = int_const_binop (MINUS_EXPR, 8521 *vr0min, 8522 build_int_cst (TREE_TYPE (*vr0min), 1)); 8523 if (!operand_less_p (max, min)) 8524 { 8525 *vr0type = VR_ANTI_RANGE; 8526 *vr0min = min; 8527 *vr0max = max; 8528 } 8529 else 8530 *vr0min = vr1min; 8531 } 8532 else 8533 *vr0min = vr1min; 8534 } 8535 } 8536 else 8537 gcc_unreachable (); 8538 } 8539 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1) 8540 && (mineq || operand_less_p (*vr0min, vr1min) == 1)) 8541 { 8542 /* [ ( ) ] or [( ) ] or [ ( )] */ 8543 if (*vr0type == VR_RANGE 8544 && vr1type == VR_RANGE) 8545 ; 8546 else if (*vr0type == VR_ANTI_RANGE 8547 && vr1type == VR_ANTI_RANGE) 8548 { 8549 *vr0type = vr1type; 8550 *vr0min = vr1min; 8551 *vr0max = vr1max; 8552 } 8553 else if (*vr0type == VR_ANTI_RANGE 8554 && vr1type == VR_RANGE) 8555 { 8556 /* Arbitrarily choose the right or left gap. */ 8557 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST) 8558 *vr0max = int_const_binop (MINUS_EXPR, vr1min, 8559 build_int_cst (TREE_TYPE (vr1min), 1)); 8560 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST) 8561 *vr0min = int_const_binop (PLUS_EXPR, vr1max, 8562 build_int_cst (TREE_TYPE (vr1max), 1)); 8563 else 8564 goto give_up; 8565 } 8566 else if (*vr0type == VR_RANGE 8567 && vr1type == VR_ANTI_RANGE) 8568 /* The result covers everything. */ 8569 goto give_up; 8570 else 8571 gcc_unreachable (); 8572 } 8573 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1) 8574 && (mineq || operand_less_p (vr1min, *vr0min) == 1)) 8575 { 8576 /* ( [ ] ) or ([ ] ) or ( [ ]) */ 8577 if (*vr0type == VR_RANGE 8578 && vr1type == VR_RANGE) 8579 { 8580 *vr0type = vr1type; 8581 *vr0min = vr1min; 8582 *vr0max = vr1max; 8583 } 8584 else if (*vr0type == VR_ANTI_RANGE 8585 && vr1type == VR_ANTI_RANGE) 8586 ; 8587 else if (*vr0type == VR_RANGE 8588 && vr1type == VR_ANTI_RANGE) 8589 { 8590 *vr0type = VR_ANTI_RANGE; 8591 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST) 8592 { 8593 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, 8594 build_int_cst (TREE_TYPE (*vr0min), 1)); 8595 *vr0min = vr1min; 8596 } 8597 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST) 8598 { 8599 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, 8600 build_int_cst (TREE_TYPE (*vr0max), 1)); 8601 *vr0max = vr1max; 8602 } 8603 else 8604 goto give_up; 8605 } 8606 else if (*vr0type == VR_ANTI_RANGE 8607 && vr1type == VR_RANGE) 8608 /* The result covers everything. */ 8609 goto give_up; 8610 else 8611 gcc_unreachable (); 8612 } 8613 else if ((operand_less_p (vr1min, *vr0max) == 1 8614 || operand_equal_p (vr1min, *vr0max, 0)) 8615 && operand_less_p (*vr0min, vr1min) == 1 8616 && operand_less_p (*vr0max, vr1max) == 1) 8617 { 8618 /* [ ( ] ) or [ ]( ) */ 8619 if (*vr0type == VR_RANGE 8620 && vr1type == VR_RANGE) 8621 *vr0max = vr1max; 8622 else if (*vr0type == VR_ANTI_RANGE 8623 && vr1type == VR_ANTI_RANGE) 8624 *vr0min = vr1min; 8625 else if (*vr0type == VR_ANTI_RANGE 8626 && vr1type == VR_RANGE) 8627 { 8628 if (TREE_CODE (vr1min) == INTEGER_CST) 8629 *vr0max = int_const_binop (MINUS_EXPR, vr1min, 8630 build_int_cst (TREE_TYPE (vr1min), 1)); 8631 else 8632 goto give_up; 8633 } 8634 else if (*vr0type == VR_RANGE 8635 && vr1type == VR_ANTI_RANGE) 8636 { 8637 if (TREE_CODE (*vr0max) == INTEGER_CST) 8638 { 8639 *vr0type = vr1type; 8640 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, 8641 build_int_cst (TREE_TYPE (*vr0max), 1)); 8642 *vr0max = vr1max; 8643 } 8644 else 8645 goto give_up; 8646 } 8647 else 8648 gcc_unreachable (); 8649 } 8650 else if ((operand_less_p (*vr0min, vr1max) == 1 8651 || operand_equal_p (*vr0min, vr1max, 0)) 8652 && operand_less_p (vr1min, *vr0min) == 1 8653 && operand_less_p (vr1max, *vr0max) == 1) 8654 { 8655 /* ( [ ) ] or ( )[ ] */ 8656 if (*vr0type == VR_RANGE 8657 && vr1type == VR_RANGE) 8658 *vr0min = vr1min; 8659 else if (*vr0type == VR_ANTI_RANGE 8660 && vr1type == VR_ANTI_RANGE) 8661 *vr0max = vr1max; 8662 else if (*vr0type == VR_ANTI_RANGE 8663 && vr1type == VR_RANGE) 8664 { 8665 if (TREE_CODE (vr1max) == INTEGER_CST) 8666 *vr0min = int_const_binop (PLUS_EXPR, vr1max, 8667 build_int_cst (TREE_TYPE (vr1max), 1)); 8668 else 8669 goto give_up; 8670 } 8671 else if (*vr0type == VR_RANGE 8672 && vr1type == VR_ANTI_RANGE) 8673 { 8674 if (TREE_CODE (*vr0min) == INTEGER_CST) 8675 { 8676 *vr0type = vr1type; 8677 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, 8678 build_int_cst (TREE_TYPE (*vr0min), 1)); 8679 *vr0min = vr1min; 8680 } 8681 else 8682 goto give_up; 8683 } 8684 else 8685 gcc_unreachable (); 8686 } 8687 else 8688 goto give_up; 8689 8690 return; 8691 8692 give_up: 8693 *vr0type = VR_VARYING; 8694 *vr0min = NULL_TREE; 8695 *vr0max = NULL_TREE; 8696 } 8697 8698 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and 8699 { VR1TYPE, VR0MIN, VR0MAX } and store the result 8700 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest 8701 possible such range. The resulting range is not canonicalized. */ 8702 8703 static void 8704 intersect_ranges (enum value_range_type *vr0type, 8705 tree *vr0min, tree *vr0max, 8706 enum value_range_type vr1type, 8707 tree vr1min, tree vr1max) 8708 { 8709 bool mineq = vrp_operand_equal_p (*vr0min, vr1min); 8710 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max); 8711 8712 /* [] is vr0, () is vr1 in the following classification comments. */ 8713 if (mineq && maxeq) 8714 { 8715 /* [( )] */ 8716 if (*vr0type == vr1type) 8717 /* Nothing to do for equal ranges. */ 8718 ; 8719 else if ((*vr0type == VR_RANGE 8720 && vr1type == VR_ANTI_RANGE) 8721 || (*vr0type == VR_ANTI_RANGE 8722 && vr1type == VR_RANGE)) 8723 { 8724 /* For anti-range with range intersection the result is empty. */ 8725 *vr0type = VR_UNDEFINED; 8726 *vr0min = NULL_TREE; 8727 *vr0max = NULL_TREE; 8728 } 8729 else 8730 gcc_unreachable (); 8731 } 8732 else if (operand_less_p (*vr0max, vr1min) == 1 8733 || operand_less_p (vr1max, *vr0min) == 1) 8734 { 8735 /* [ ] ( ) or ( ) [ ] 8736 If the ranges have an empty intersection, the result of the 8737 intersect operation is the range for intersecting an 8738 anti-range with a range or empty when intersecting two ranges. */ 8739 if (*vr0type == VR_RANGE 8740 && vr1type == VR_ANTI_RANGE) 8741 ; 8742 else if (*vr0type == VR_ANTI_RANGE 8743 && vr1type == VR_RANGE) 8744 { 8745 *vr0type = vr1type; 8746 *vr0min = vr1min; 8747 *vr0max = vr1max; 8748 } 8749 else if (*vr0type == VR_RANGE 8750 && vr1type == VR_RANGE) 8751 { 8752 *vr0type = VR_UNDEFINED; 8753 *vr0min = NULL_TREE; 8754 *vr0max = NULL_TREE; 8755 } 8756 else if (*vr0type == VR_ANTI_RANGE 8757 && vr1type == VR_ANTI_RANGE) 8758 { 8759 /* If the anti-ranges are adjacent to each other merge them. */ 8760 if (TREE_CODE (*vr0max) == INTEGER_CST 8761 && TREE_CODE (vr1min) == INTEGER_CST 8762 && operand_less_p (*vr0max, vr1min) == 1 8763 && integer_onep (int_const_binop (MINUS_EXPR, 8764 vr1min, *vr0max))) 8765 *vr0max = vr1max; 8766 else if (TREE_CODE (vr1max) == INTEGER_CST 8767 && TREE_CODE (*vr0min) == INTEGER_CST 8768 && operand_less_p (vr1max, *vr0min) == 1 8769 && integer_onep (int_const_binop (MINUS_EXPR, 8770 *vr0min, vr1max))) 8771 *vr0min = vr1min; 8772 /* Else arbitrarily take VR0. */ 8773 } 8774 } 8775 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1) 8776 && (mineq || operand_less_p (*vr0min, vr1min) == 1)) 8777 { 8778 /* [ ( ) ] or [( ) ] or [ ( )] */ 8779 if (*vr0type == VR_RANGE 8780 && vr1type == VR_RANGE) 8781 { 8782 /* If both are ranges the result is the inner one. */ 8783 *vr0type = vr1type; 8784 *vr0min = vr1min; 8785 *vr0max = vr1max; 8786 } 8787 else if (*vr0type == VR_RANGE 8788 && vr1type == VR_ANTI_RANGE) 8789 { 8790 /* Choose the right gap if the left one is empty. */ 8791 if (mineq) 8792 { 8793 if (TREE_CODE (vr1max) != INTEGER_CST) 8794 *vr0min = vr1max; 8795 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1 8796 && !TYPE_UNSIGNED (TREE_TYPE (vr1max))) 8797 *vr0min 8798 = int_const_binop (MINUS_EXPR, vr1max, 8799 build_int_cst (TREE_TYPE (vr1max), -1)); 8800 else 8801 *vr0min 8802 = int_const_binop (PLUS_EXPR, vr1max, 8803 build_int_cst (TREE_TYPE (vr1max), 1)); 8804 } 8805 /* Choose the left gap if the right one is empty. */ 8806 else if (maxeq) 8807 { 8808 if (TREE_CODE (vr1min) != INTEGER_CST) 8809 *vr0max = vr1min; 8810 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1 8811 && !TYPE_UNSIGNED (TREE_TYPE (vr1min))) 8812 *vr0max 8813 = int_const_binop (PLUS_EXPR, vr1min, 8814 build_int_cst (TREE_TYPE (vr1min), -1)); 8815 else 8816 *vr0max 8817 = int_const_binop (MINUS_EXPR, vr1min, 8818 build_int_cst (TREE_TYPE (vr1min), 1)); 8819 } 8820 /* Choose the anti-range if the range is effectively varying. */ 8821 else if (vrp_val_is_min (*vr0min) 8822 && vrp_val_is_max (*vr0max)) 8823 { 8824 *vr0type = vr1type; 8825 *vr0min = vr1min; 8826 *vr0max = vr1max; 8827 } 8828 /* Else choose the range. */ 8829 } 8830 else if (*vr0type == VR_ANTI_RANGE 8831 && vr1type == VR_ANTI_RANGE) 8832 /* If both are anti-ranges the result is the outer one. */ 8833 ; 8834 else if (*vr0type == VR_ANTI_RANGE 8835 && vr1type == VR_RANGE) 8836 { 8837 /* The intersection is empty. */ 8838 *vr0type = VR_UNDEFINED; 8839 *vr0min = NULL_TREE; 8840 *vr0max = NULL_TREE; 8841 } 8842 else 8843 gcc_unreachable (); 8844 } 8845 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1) 8846 && (mineq || operand_less_p (vr1min, *vr0min) == 1)) 8847 { 8848 /* ( [ ] ) or ([ ] ) or ( [ ]) */ 8849 if (*vr0type == VR_RANGE 8850 && vr1type == VR_RANGE) 8851 /* Choose the inner range. */ 8852 ; 8853 else if (*vr0type == VR_ANTI_RANGE 8854 && vr1type == VR_RANGE) 8855 { 8856 /* Choose the right gap if the left is empty. */ 8857 if (mineq) 8858 { 8859 *vr0type = VR_RANGE; 8860 if (TREE_CODE (*vr0max) != INTEGER_CST) 8861 *vr0min = *vr0max; 8862 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1 8863 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max))) 8864 *vr0min 8865 = int_const_binop (MINUS_EXPR, *vr0max, 8866 build_int_cst (TREE_TYPE (*vr0max), -1)); 8867 else 8868 *vr0min 8869 = int_const_binop (PLUS_EXPR, *vr0max, 8870 build_int_cst (TREE_TYPE (*vr0max), 1)); 8871 *vr0max = vr1max; 8872 } 8873 /* Choose the left gap if the right is empty. */ 8874 else if (maxeq) 8875 { 8876 *vr0type = VR_RANGE; 8877 if (TREE_CODE (*vr0min) != INTEGER_CST) 8878 *vr0max = *vr0min; 8879 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1 8880 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min))) 8881 *vr0max 8882 = int_const_binop (PLUS_EXPR, *vr0min, 8883 build_int_cst (TREE_TYPE (*vr0min), -1)); 8884 else 8885 *vr0max 8886 = int_const_binop (MINUS_EXPR, *vr0min, 8887 build_int_cst (TREE_TYPE (*vr0min), 1)); 8888 *vr0min = vr1min; 8889 } 8890 /* Choose the anti-range if the range is effectively varying. */ 8891 else if (vrp_val_is_min (vr1min) 8892 && vrp_val_is_max (vr1max)) 8893 ; 8894 /* Choose the anti-range if it is ~[0,0], that range is special 8895 enough to special case when vr1's range is relatively wide. */ 8896 else if (*vr0min == *vr0max 8897 && integer_zerop (*vr0min) 8898 && (TYPE_PRECISION (TREE_TYPE (*vr0min)) 8899 == TYPE_PRECISION (ptr_type_node)) 8900 && TREE_CODE (vr1max) == INTEGER_CST 8901 && TREE_CODE (vr1min) == INTEGER_CST 8902 && (wi::clz (wi::sub (vr1max, vr1min)) 8903 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2)) 8904 ; 8905 /* Else choose the range. */ 8906 else 8907 { 8908 *vr0type = vr1type; 8909 *vr0min = vr1min; 8910 *vr0max = vr1max; 8911 } 8912 } 8913 else if (*vr0type == VR_ANTI_RANGE 8914 && vr1type == VR_ANTI_RANGE) 8915 { 8916 /* If both are anti-ranges the result is the outer one. */ 8917 *vr0type = vr1type; 8918 *vr0min = vr1min; 8919 *vr0max = vr1max; 8920 } 8921 else if (vr1type == VR_ANTI_RANGE 8922 && *vr0type == VR_RANGE) 8923 { 8924 /* The intersection is empty. */ 8925 *vr0type = VR_UNDEFINED; 8926 *vr0min = NULL_TREE; 8927 *vr0max = NULL_TREE; 8928 } 8929 else 8930 gcc_unreachable (); 8931 } 8932 else if ((operand_less_p (vr1min, *vr0max) == 1 8933 || operand_equal_p (vr1min, *vr0max, 0)) 8934 && operand_less_p (*vr0min, vr1min) == 1) 8935 { 8936 /* [ ( ] ) or [ ]( ) */ 8937 if (*vr0type == VR_ANTI_RANGE 8938 && vr1type == VR_ANTI_RANGE) 8939 *vr0max = vr1max; 8940 else if (*vr0type == VR_RANGE 8941 && vr1type == VR_RANGE) 8942 *vr0min = vr1min; 8943 else if (*vr0type == VR_RANGE 8944 && vr1type == VR_ANTI_RANGE) 8945 { 8946 if (TREE_CODE (vr1min) == INTEGER_CST) 8947 *vr0max = int_const_binop (MINUS_EXPR, vr1min, 8948 build_int_cst (TREE_TYPE (vr1min), 1)); 8949 else 8950 *vr0max = vr1min; 8951 } 8952 else if (*vr0type == VR_ANTI_RANGE 8953 && vr1type == VR_RANGE) 8954 { 8955 *vr0type = VR_RANGE; 8956 if (TREE_CODE (*vr0max) == INTEGER_CST) 8957 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, 8958 build_int_cst (TREE_TYPE (*vr0max), 1)); 8959 else 8960 *vr0min = *vr0max; 8961 *vr0max = vr1max; 8962 } 8963 else 8964 gcc_unreachable (); 8965 } 8966 else if ((operand_less_p (*vr0min, vr1max) == 1 8967 || operand_equal_p (*vr0min, vr1max, 0)) 8968 && operand_less_p (vr1min, *vr0min) == 1) 8969 { 8970 /* ( [ ) ] or ( )[ ] */ 8971 if (*vr0type == VR_ANTI_RANGE 8972 && vr1type == VR_ANTI_RANGE) 8973 *vr0min = vr1min; 8974 else if (*vr0type == VR_RANGE 8975 && vr1type == VR_RANGE) 8976 *vr0max = vr1max; 8977 else if (*vr0type == VR_RANGE 8978 && vr1type == VR_ANTI_RANGE) 8979 { 8980 if (TREE_CODE (vr1max) == INTEGER_CST) 8981 *vr0min = int_const_binop (PLUS_EXPR, vr1max, 8982 build_int_cst (TREE_TYPE (vr1max), 1)); 8983 else 8984 *vr0min = vr1max; 8985 } 8986 else if (*vr0type == VR_ANTI_RANGE 8987 && vr1type == VR_RANGE) 8988 { 8989 *vr0type = VR_RANGE; 8990 if (TREE_CODE (*vr0min) == INTEGER_CST) 8991 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, 8992 build_int_cst (TREE_TYPE (*vr0min), 1)); 8993 else 8994 *vr0max = *vr0min; 8995 *vr0min = vr1min; 8996 } 8997 else 8998 gcc_unreachable (); 8999 } 9000 9001 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as 9002 result for the intersection. That's always a conservative 9003 correct estimate unless VR1 is a constant singleton range 9004 in which case we choose that. */ 9005 if (vr1type == VR_RANGE 9006 && is_gimple_min_invariant (vr1min) 9007 && vrp_operand_equal_p (vr1min, vr1max)) 9008 { 9009 *vr0type = vr1type; 9010 *vr0min = vr1min; 9011 *vr0max = vr1max; 9012 } 9013 9014 return; 9015 } 9016 9017 9018 /* Intersect the two value-ranges *VR0 and *VR1 and store the result 9019 in *VR0. This may not be the smallest possible such range. */ 9020 9021 static void 9022 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1) 9023 { 9024 value_range saved; 9025 9026 /* If either range is VR_VARYING the other one wins. */ 9027 if (vr1->type == VR_VARYING) 9028 return; 9029 if (vr0->type == VR_VARYING) 9030 { 9031 copy_value_range (vr0, vr1); 9032 return; 9033 } 9034 9035 /* When either range is VR_UNDEFINED the resulting range is 9036 VR_UNDEFINED, too. */ 9037 if (vr0->type == VR_UNDEFINED) 9038 return; 9039 if (vr1->type == VR_UNDEFINED) 9040 { 9041 set_value_range_to_undefined (vr0); 9042 return; 9043 } 9044 9045 /* Save the original vr0 so we can return it as conservative intersection 9046 result when our worker turns things to varying. */ 9047 saved = *vr0; 9048 intersect_ranges (&vr0->type, &vr0->min, &vr0->max, 9049 vr1->type, vr1->min, vr1->max); 9050 /* Make sure to canonicalize the result though as the inversion of a 9051 VR_RANGE can still be a VR_RANGE. */ 9052 set_and_canonicalize_value_range (vr0, vr0->type, 9053 vr0->min, vr0->max, vr0->equiv); 9054 /* If that failed, use the saved original VR0. */ 9055 if (vr0->type == VR_VARYING) 9056 { 9057 *vr0 = saved; 9058 return; 9059 } 9060 /* If the result is VR_UNDEFINED there is no need to mess with 9061 the equivalencies. */ 9062 if (vr0->type == VR_UNDEFINED) 9063 return; 9064 9065 /* The resulting set of equivalences for range intersection is the union of 9066 the two sets. */ 9067 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv) 9068 bitmap_ior_into (vr0->equiv, vr1->equiv); 9069 else if (vr1->equiv && !vr0->equiv) 9070 { 9071 vr0->equiv = BITMAP_ALLOC (&vrp_equiv_obstack); 9072 bitmap_copy (vr0->equiv, vr1->equiv); 9073 } 9074 } 9075 9076 void 9077 vrp_intersect_ranges (value_range *vr0, value_range *vr1) 9078 { 9079 if (dump_file && (dump_flags & TDF_DETAILS)) 9080 { 9081 fprintf (dump_file, "Intersecting\n "); 9082 dump_value_range (dump_file, vr0); 9083 fprintf (dump_file, "\nand\n "); 9084 dump_value_range (dump_file, vr1); 9085 fprintf (dump_file, "\n"); 9086 } 9087 vrp_intersect_ranges_1 (vr0, vr1); 9088 if (dump_file && (dump_flags & TDF_DETAILS)) 9089 { 9090 fprintf (dump_file, "to\n "); 9091 dump_value_range (dump_file, vr0); 9092 fprintf (dump_file, "\n"); 9093 } 9094 } 9095 9096 /* Meet operation for value ranges. Given two value ranges VR0 and 9097 VR1, store in VR0 a range that contains both VR0 and VR1. This 9098 may not be the smallest possible such range. */ 9099 9100 static void 9101 vrp_meet_1 (value_range *vr0, const value_range *vr1) 9102 { 9103 value_range saved; 9104 9105 if (vr0->type == VR_UNDEFINED) 9106 { 9107 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv); 9108 return; 9109 } 9110 9111 if (vr1->type == VR_UNDEFINED) 9112 { 9113 /* VR0 already has the resulting range. */ 9114 return; 9115 } 9116 9117 if (vr0->type == VR_VARYING) 9118 { 9119 /* Nothing to do. VR0 already has the resulting range. */ 9120 return; 9121 } 9122 9123 if (vr1->type == VR_VARYING) 9124 { 9125 set_value_range_to_varying (vr0); 9126 return; 9127 } 9128 9129 saved = *vr0; 9130 union_ranges (&vr0->type, &vr0->min, &vr0->max, 9131 vr1->type, vr1->min, vr1->max); 9132 if (vr0->type == VR_VARYING) 9133 { 9134 /* Failed to find an efficient meet. Before giving up and setting 9135 the result to VARYING, see if we can at least derive a useful 9136 anti-range. FIXME, all this nonsense about distinguishing 9137 anti-ranges from ranges is necessary because of the odd 9138 semantics of range_includes_zero_p and friends. */ 9139 if (((saved.type == VR_RANGE 9140 && range_includes_zero_p (saved.min, saved.max) == 0) 9141 || (saved.type == VR_ANTI_RANGE 9142 && range_includes_zero_p (saved.min, saved.max) == 1)) 9143 && ((vr1->type == VR_RANGE 9144 && range_includes_zero_p (vr1->min, vr1->max) == 0) 9145 || (vr1->type == VR_ANTI_RANGE 9146 && range_includes_zero_p (vr1->min, vr1->max) == 1))) 9147 { 9148 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min)); 9149 9150 /* Since this meet operation did not result from the meeting of 9151 two equivalent names, VR0 cannot have any equivalences. */ 9152 if (vr0->equiv) 9153 bitmap_clear (vr0->equiv); 9154 return; 9155 } 9156 9157 set_value_range_to_varying (vr0); 9158 return; 9159 } 9160 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max, 9161 vr0->equiv); 9162 if (vr0->type == VR_VARYING) 9163 return; 9164 9165 /* The resulting set of equivalences is always the intersection of 9166 the two sets. */ 9167 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv) 9168 bitmap_and_into (vr0->equiv, vr1->equiv); 9169 else if (vr0->equiv && !vr1->equiv) 9170 bitmap_clear (vr0->equiv); 9171 } 9172 9173 void 9174 vrp_meet (value_range *vr0, const value_range *vr1) 9175 { 9176 if (dump_file && (dump_flags & TDF_DETAILS)) 9177 { 9178 fprintf (dump_file, "Meeting\n "); 9179 dump_value_range (dump_file, vr0); 9180 fprintf (dump_file, "\nand\n "); 9181 dump_value_range (dump_file, vr1); 9182 fprintf (dump_file, "\n"); 9183 } 9184 vrp_meet_1 (vr0, vr1); 9185 if (dump_file && (dump_flags & TDF_DETAILS)) 9186 { 9187 fprintf (dump_file, "to\n "); 9188 dump_value_range (dump_file, vr0); 9189 fprintf (dump_file, "\n"); 9190 } 9191 } 9192 9193 9194 /* Visit all arguments for PHI node PHI that flow through executable 9195 edges. If a valid value range can be derived from all the incoming 9196 value ranges, set a new range in VR_RESULT. */ 9197 9198 static void 9199 extract_range_from_phi_node (gphi *phi, value_range *vr_result) 9200 { 9201 size_t i; 9202 tree lhs = PHI_RESULT (phi); 9203 value_range *lhs_vr = get_value_range (lhs); 9204 bool first = true; 9205 int edges, old_edges; 9206 struct loop *l; 9207 9208 if (dump_file && (dump_flags & TDF_DETAILS)) 9209 { 9210 fprintf (dump_file, "\nVisiting PHI node: "); 9211 print_gimple_stmt (dump_file, phi, 0, dump_flags); 9212 } 9213 9214 bool may_simulate_backedge_again = false; 9215 edges = 0; 9216 for (i = 0; i < gimple_phi_num_args (phi); i++) 9217 { 9218 edge e = gimple_phi_arg_edge (phi, i); 9219 9220 if (dump_file && (dump_flags & TDF_DETAILS)) 9221 { 9222 fprintf (dump_file, 9223 " Argument #%d (%d -> %d %sexecutable)\n", 9224 (int) i, e->src->index, e->dest->index, 9225 (e->flags & EDGE_EXECUTABLE) ? "" : "not "); 9226 } 9227 9228 if (e->flags & EDGE_EXECUTABLE) 9229 { 9230 tree arg = PHI_ARG_DEF (phi, i); 9231 value_range vr_arg; 9232 9233 ++edges; 9234 9235 if (TREE_CODE (arg) == SSA_NAME) 9236 { 9237 /* See if we are eventually going to change one of the args. */ 9238 gimple *def_stmt = SSA_NAME_DEF_STMT (arg); 9239 if (! gimple_nop_p (def_stmt) 9240 && prop_simulate_again_p (def_stmt) 9241 && e->flags & EDGE_DFS_BACK) 9242 may_simulate_backedge_again = true; 9243 9244 vr_arg = *(get_value_range (arg)); 9245 /* Do not allow equivalences or symbolic ranges to leak in from 9246 backedges. That creates invalid equivalencies. 9247 See PR53465 and PR54767. */ 9248 if (e->flags & EDGE_DFS_BACK) 9249 { 9250 if (vr_arg.type == VR_RANGE 9251 || vr_arg.type == VR_ANTI_RANGE) 9252 { 9253 vr_arg.equiv = NULL; 9254 if (symbolic_range_p (&vr_arg)) 9255 { 9256 vr_arg.type = VR_VARYING; 9257 vr_arg.min = NULL_TREE; 9258 vr_arg.max = NULL_TREE; 9259 } 9260 } 9261 } 9262 else 9263 { 9264 /* If the non-backedge arguments range is VR_VARYING then 9265 we can still try recording a simple equivalence. */ 9266 if (vr_arg.type == VR_VARYING) 9267 { 9268 vr_arg.type = VR_RANGE; 9269 vr_arg.min = arg; 9270 vr_arg.max = arg; 9271 vr_arg.equiv = NULL; 9272 } 9273 } 9274 } 9275 else 9276 { 9277 if (TREE_OVERFLOW_P (arg)) 9278 arg = drop_tree_overflow (arg); 9279 9280 vr_arg.type = VR_RANGE; 9281 vr_arg.min = arg; 9282 vr_arg.max = arg; 9283 vr_arg.equiv = NULL; 9284 } 9285 9286 if (dump_file && (dump_flags & TDF_DETAILS)) 9287 { 9288 fprintf (dump_file, "\t"); 9289 print_generic_expr (dump_file, arg, dump_flags); 9290 fprintf (dump_file, ": "); 9291 dump_value_range (dump_file, &vr_arg); 9292 fprintf (dump_file, "\n"); 9293 } 9294 9295 if (first) 9296 copy_value_range (vr_result, &vr_arg); 9297 else 9298 vrp_meet (vr_result, &vr_arg); 9299 first = false; 9300 9301 if (vr_result->type == VR_VARYING) 9302 break; 9303 } 9304 } 9305 9306 if (vr_result->type == VR_VARYING) 9307 goto varying; 9308 else if (vr_result->type == VR_UNDEFINED) 9309 goto update_range; 9310 9311 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)]; 9312 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges; 9313 9314 /* To prevent infinite iterations in the algorithm, derive ranges 9315 when the new value is slightly bigger or smaller than the 9316 previous one. We don't do this if we have seen a new executable 9317 edge; this helps us avoid an overflow infinity for conditionals 9318 which are not in a loop. If the old value-range was VR_UNDEFINED 9319 use the updated range and iterate one more time. If we will not 9320 simulate this PHI again via the backedge allow us to iterate. */ 9321 if (edges > 0 9322 && gimple_phi_num_args (phi) > 1 9323 && edges == old_edges 9324 && lhs_vr->type != VR_UNDEFINED 9325 && may_simulate_backedge_again) 9326 { 9327 /* Compare old and new ranges, fall back to varying if the 9328 values are not comparable. */ 9329 int cmp_min = compare_values (lhs_vr->min, vr_result->min); 9330 if (cmp_min == -2) 9331 goto varying; 9332 int cmp_max = compare_values (lhs_vr->max, vr_result->max); 9333 if (cmp_max == -2) 9334 goto varying; 9335 9336 /* For non VR_RANGE or for pointers fall back to varying if 9337 the range changed. */ 9338 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE 9339 || POINTER_TYPE_P (TREE_TYPE (lhs))) 9340 && (cmp_min != 0 || cmp_max != 0)) 9341 goto varying; 9342 9343 /* If the new minimum is larger than the previous one 9344 retain the old value. If the new minimum value is smaller 9345 than the previous one and not -INF go all the way to -INF + 1. 9346 In the first case, to avoid infinite bouncing between different 9347 minimums, and in the other case to avoid iterating millions of 9348 times to reach -INF. Going to -INF + 1 also lets the following 9349 iteration compute whether there will be any overflow, at the 9350 expense of one additional iteration. */ 9351 if (cmp_min < 0) 9352 vr_result->min = lhs_vr->min; 9353 else if (cmp_min > 0 9354 && !vrp_val_is_min (vr_result->min)) 9355 vr_result->min 9356 = int_const_binop (PLUS_EXPR, 9357 vrp_val_min (TREE_TYPE (vr_result->min)), 9358 build_int_cst (TREE_TYPE (vr_result->min), 1)); 9359 9360 /* Similarly for the maximum value. */ 9361 if (cmp_max > 0) 9362 vr_result->max = lhs_vr->max; 9363 else if (cmp_max < 0 9364 && !vrp_val_is_max (vr_result->max)) 9365 vr_result->max 9366 = int_const_binop (MINUS_EXPR, 9367 vrp_val_max (TREE_TYPE (vr_result->min)), 9368 build_int_cst (TREE_TYPE (vr_result->min), 1)); 9369 9370 /* If we dropped either bound to +-INF then if this is a loop 9371 PHI node SCEV may known more about its value-range. */ 9372 if (cmp_min > 0 || cmp_min < 0 9373 || cmp_max < 0 || cmp_max > 0) 9374 goto scev_check; 9375 9376 goto infinite_check; 9377 } 9378 9379 goto update_range; 9380 9381 varying: 9382 set_value_range_to_varying (vr_result); 9383 9384 scev_check: 9385 /* If this is a loop PHI node SCEV may known more about its value-range. 9386 scev_check can be reached from two paths, one is a fall through from above 9387 "varying" label, the other is direct goto from code block which tries to 9388 avoid infinite simulation. */ 9389 if ((l = loop_containing_stmt (phi)) 9390 && l->header == gimple_bb (phi)) 9391 adjust_range_with_scev (vr_result, l, phi, lhs); 9392 9393 infinite_check: 9394 /* If we will end up with a (-INF, +INF) range, set it to 9395 VARYING. Same if the previous max value was invalid for 9396 the type and we end up with vr_result.min > vr_result.max. */ 9397 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE) 9398 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min)) 9399 || compare_values (vr_result->min, vr_result->max) > 0)) 9400 ; 9401 else 9402 set_value_range_to_varying (vr_result); 9403 9404 /* If the new range is different than the previous value, keep 9405 iterating. */ 9406 update_range: 9407 return; 9408 } 9409 9410 /* Visit all arguments for PHI node PHI that flow through executable 9411 edges. If a valid value range can be derived from all the incoming 9412 value ranges, set a new range for the LHS of PHI. */ 9413 9414 static enum ssa_prop_result 9415 vrp_visit_phi_node (gphi *phi) 9416 { 9417 tree lhs = PHI_RESULT (phi); 9418 value_range vr_result = VR_INITIALIZER; 9419 extract_range_from_phi_node (phi, &vr_result); 9420 if (update_value_range (lhs, &vr_result)) 9421 { 9422 if (dump_file && (dump_flags & TDF_DETAILS)) 9423 { 9424 fprintf (dump_file, "Found new range for "); 9425 print_generic_expr (dump_file, lhs, 0); 9426 fprintf (dump_file, ": "); 9427 dump_value_range (dump_file, &vr_result); 9428 fprintf (dump_file, "\n"); 9429 } 9430 9431 if (vr_result.type == VR_VARYING) 9432 return SSA_PROP_VARYING; 9433 9434 return SSA_PROP_INTERESTING; 9435 } 9436 9437 /* Nothing changed, don't add outgoing edges. */ 9438 return SSA_PROP_NOT_INTERESTING; 9439 } 9440 9441 /* Simplify boolean operations if the source is known 9442 to be already a boolean. */ 9443 static bool 9444 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt) 9445 { 9446 enum tree_code rhs_code = gimple_assign_rhs_code (stmt); 9447 tree lhs, op0, op1; 9448 bool need_conversion; 9449 9450 /* We handle only !=/== case here. */ 9451 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR); 9452 9453 op0 = gimple_assign_rhs1 (stmt); 9454 if (!op_with_boolean_value_range_p (op0)) 9455 return false; 9456 9457 op1 = gimple_assign_rhs2 (stmt); 9458 if (!op_with_boolean_value_range_p (op1)) 9459 return false; 9460 9461 /* Reduce number of cases to handle to NE_EXPR. As there is no 9462 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */ 9463 if (rhs_code == EQ_EXPR) 9464 { 9465 if (TREE_CODE (op1) == INTEGER_CST) 9466 op1 = int_const_binop (BIT_XOR_EXPR, op1, 9467 build_int_cst (TREE_TYPE (op1), 1)); 9468 else 9469 return false; 9470 } 9471 9472 lhs = gimple_assign_lhs (stmt); 9473 need_conversion 9474 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0)); 9475 9476 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */ 9477 if (need_conversion 9478 && !TYPE_UNSIGNED (TREE_TYPE (op0)) 9479 && TYPE_PRECISION (TREE_TYPE (op0)) == 1 9480 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1) 9481 return false; 9482 9483 /* For A != 0 we can substitute A itself. */ 9484 if (integer_zerop (op1)) 9485 gimple_assign_set_rhs_with_ops (gsi, 9486 need_conversion 9487 ? NOP_EXPR : TREE_CODE (op0), op0); 9488 /* For A != B we substitute A ^ B. Either with conversion. */ 9489 else if (need_conversion) 9490 { 9491 tree tem = make_ssa_name (TREE_TYPE (op0)); 9492 gassign *newop 9493 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1); 9494 gsi_insert_before (gsi, newop, GSI_SAME_STMT); 9495 if (INTEGRAL_TYPE_P (TREE_TYPE (tem)) 9496 && TYPE_PRECISION (TREE_TYPE (tem)) > 1) 9497 set_range_info (tem, VR_RANGE, 9498 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))), 9499 wi::one (TYPE_PRECISION (TREE_TYPE (tem)))); 9500 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem); 9501 } 9502 /* Or without. */ 9503 else 9504 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1); 9505 update_stmt (gsi_stmt (*gsi)); 9506 fold_stmt (gsi, follow_single_use_edges); 9507 9508 return true; 9509 } 9510 9511 /* Simplify a division or modulo operator to a right shift or bitwise and 9512 if the first operand is unsigned or is greater than zero and the second 9513 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with 9514 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range, 9515 optimize it into just op0 if op0's range is known to be a subset of 9516 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned 9517 modulo. */ 9518 9519 static bool 9520 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt) 9521 { 9522 enum tree_code rhs_code = gimple_assign_rhs_code (stmt); 9523 tree val = NULL; 9524 tree op0 = gimple_assign_rhs1 (stmt); 9525 tree op1 = gimple_assign_rhs2 (stmt); 9526 tree op0min = NULL_TREE, op0max = NULL_TREE; 9527 tree op1min = op1; 9528 value_range *vr = NULL; 9529 9530 if (TREE_CODE (op0) == INTEGER_CST) 9531 { 9532 op0min = op0; 9533 op0max = op0; 9534 } 9535 else 9536 { 9537 vr = get_value_range (op0); 9538 if (range_int_cst_p (vr)) 9539 { 9540 op0min = vr->min; 9541 op0max = vr->max; 9542 } 9543 } 9544 9545 if (rhs_code == TRUNC_MOD_EXPR 9546 && TREE_CODE (op1) == SSA_NAME) 9547 { 9548 value_range *vr1 = get_value_range (op1); 9549 if (range_int_cst_p (vr1)) 9550 op1min = vr1->min; 9551 } 9552 if (rhs_code == TRUNC_MOD_EXPR 9553 && TREE_CODE (op1min) == INTEGER_CST 9554 && tree_int_cst_sgn (op1min) == 1 9555 && op0max 9556 && tree_int_cst_lt (op0max, op1min)) 9557 { 9558 if (TYPE_UNSIGNED (TREE_TYPE (op0)) 9559 || tree_int_cst_sgn (op0min) >= 0 9560 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min), 9561 op0min)) 9562 { 9563 /* If op0 already has the range op0 % op1 has, 9564 then TRUNC_MOD_EXPR won't change anything. */ 9565 gimple_assign_set_rhs_from_tree (gsi, op0); 9566 return true; 9567 } 9568 } 9569 9570 if (TREE_CODE (op0) != SSA_NAME) 9571 return false; 9572 9573 if (!integer_pow2p (op1)) 9574 { 9575 /* X % -Y can be only optimized into X % Y either if 9576 X is not INT_MIN, or Y is not -1. Fold it now, as after 9577 remove_range_assertions the range info might be not available 9578 anymore. */ 9579 if (rhs_code == TRUNC_MOD_EXPR 9580 && fold_stmt (gsi, follow_single_use_edges)) 9581 return true; 9582 return false; 9583 } 9584 9585 if (TYPE_UNSIGNED (TREE_TYPE (op0))) 9586 val = integer_one_node; 9587 else 9588 { 9589 bool sop = false; 9590 9591 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop); 9592 9593 if (val 9594 && sop 9595 && integer_onep (val) 9596 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC)) 9597 { 9598 location_t location; 9599 9600 if (!gimple_has_location (stmt)) 9601 location = input_location; 9602 else 9603 location = gimple_location (stmt); 9604 warning_at (location, OPT_Wstrict_overflow, 9605 "assuming signed overflow does not occur when " 9606 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>"); 9607 } 9608 } 9609 9610 if (val && integer_onep (val)) 9611 { 9612 tree t; 9613 9614 if (rhs_code == TRUNC_DIV_EXPR) 9615 { 9616 t = build_int_cst (integer_type_node, tree_log2 (op1)); 9617 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR); 9618 gimple_assign_set_rhs1 (stmt, op0); 9619 gimple_assign_set_rhs2 (stmt, t); 9620 } 9621 else 9622 { 9623 t = build_int_cst (TREE_TYPE (op1), 1); 9624 t = int_const_binop (MINUS_EXPR, op1, t); 9625 t = fold_convert (TREE_TYPE (op0), t); 9626 9627 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR); 9628 gimple_assign_set_rhs1 (stmt, op0); 9629 gimple_assign_set_rhs2 (stmt, t); 9630 } 9631 9632 update_stmt (stmt); 9633 fold_stmt (gsi, follow_single_use_edges); 9634 return true; 9635 } 9636 9637 return false; 9638 } 9639 9640 /* Simplify a min or max if the ranges of the two operands are 9641 disjoint. Return true if we do simplify. */ 9642 9643 static bool 9644 simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt) 9645 { 9646 tree op0 = gimple_assign_rhs1 (stmt); 9647 tree op1 = gimple_assign_rhs2 (stmt); 9648 bool sop = false; 9649 tree val; 9650 9651 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges 9652 (LE_EXPR, op0, op1, &sop)); 9653 if (!val) 9654 { 9655 sop = false; 9656 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges 9657 (LT_EXPR, op0, op1, &sop)); 9658 } 9659 9660 if (val) 9661 { 9662 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC)) 9663 { 9664 location_t location; 9665 9666 if (!gimple_has_location (stmt)) 9667 location = input_location; 9668 else 9669 location = gimple_location (stmt); 9670 warning_at (location, OPT_Wstrict_overflow, 9671 "assuming signed overflow does not occur when " 9672 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>"); 9673 } 9674 9675 /* VAL == TRUE -> OP0 < or <= op1 9676 VAL == FALSE -> OP0 > or >= op1. */ 9677 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR) 9678 == integer_zerop (val)) ? op0 : op1; 9679 gimple_assign_set_rhs_from_tree (gsi, res); 9680 return true; 9681 } 9682 9683 return false; 9684 } 9685 9686 /* If the operand to an ABS_EXPR is >= 0, then eliminate the 9687 ABS_EXPR. If the operand is <= 0, then simplify the 9688 ABS_EXPR into a NEGATE_EXPR. */ 9689 9690 static bool 9691 simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt) 9692 { 9693 tree op = gimple_assign_rhs1 (stmt); 9694 value_range *vr = get_value_range (op); 9695 9696 if (vr) 9697 { 9698 tree val = NULL; 9699 bool sop = false; 9700 9701 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop); 9702 if (!val) 9703 { 9704 /* The range is neither <= 0 nor > 0. Now see if it is 9705 either < 0 or >= 0. */ 9706 sop = false; 9707 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node, 9708 &sop); 9709 } 9710 9711 if (val) 9712 { 9713 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC)) 9714 { 9715 location_t location; 9716 9717 if (!gimple_has_location (stmt)) 9718 location = input_location; 9719 else 9720 location = gimple_location (stmt); 9721 warning_at (location, OPT_Wstrict_overflow, 9722 "assuming signed overflow does not occur when " 9723 "simplifying %<abs (X)%> to %<X%> or %<-X%>"); 9724 } 9725 9726 gimple_assign_set_rhs1 (stmt, op); 9727 if (integer_zerop (val)) 9728 gimple_assign_set_rhs_code (stmt, SSA_NAME); 9729 else 9730 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR); 9731 update_stmt (stmt); 9732 fold_stmt (gsi, follow_single_use_edges); 9733 return true; 9734 } 9735 } 9736 9737 return false; 9738 } 9739 9740 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR. 9741 If all the bits that are being cleared by & are already 9742 known to be zero from VR, or all the bits that are being 9743 set by | are already known to be one from VR, the bit 9744 operation is redundant. */ 9745 9746 static bool 9747 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt) 9748 { 9749 tree op0 = gimple_assign_rhs1 (stmt); 9750 tree op1 = gimple_assign_rhs2 (stmt); 9751 tree op = NULL_TREE; 9752 value_range vr0 = VR_INITIALIZER; 9753 value_range vr1 = VR_INITIALIZER; 9754 wide_int may_be_nonzero0, may_be_nonzero1; 9755 wide_int must_be_nonzero0, must_be_nonzero1; 9756 wide_int mask; 9757 9758 if (TREE_CODE (op0) == SSA_NAME) 9759 vr0 = *(get_value_range (op0)); 9760 else if (is_gimple_min_invariant (op0)) 9761 set_value_range_to_value (&vr0, op0, NULL); 9762 else 9763 return false; 9764 9765 if (TREE_CODE (op1) == SSA_NAME) 9766 vr1 = *(get_value_range (op1)); 9767 else if (is_gimple_min_invariant (op1)) 9768 set_value_range_to_value (&vr1, op1, NULL); 9769 else 9770 return false; 9771 9772 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0, 9773 &must_be_nonzero0)) 9774 return false; 9775 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1, 9776 &must_be_nonzero1)) 9777 return false; 9778 9779 switch (gimple_assign_rhs_code (stmt)) 9780 { 9781 case BIT_AND_EXPR: 9782 mask = may_be_nonzero0.and_not (must_be_nonzero1); 9783 if (mask == 0) 9784 { 9785 op = op0; 9786 break; 9787 } 9788 mask = may_be_nonzero1.and_not (must_be_nonzero0); 9789 if (mask == 0) 9790 { 9791 op = op1; 9792 break; 9793 } 9794 break; 9795 case BIT_IOR_EXPR: 9796 mask = may_be_nonzero0.and_not (must_be_nonzero1); 9797 if (mask == 0) 9798 { 9799 op = op1; 9800 break; 9801 } 9802 mask = may_be_nonzero1.and_not (must_be_nonzero0); 9803 if (mask == 0) 9804 { 9805 op = op0; 9806 break; 9807 } 9808 break; 9809 default: 9810 gcc_unreachable (); 9811 } 9812 9813 if (op == NULL_TREE) 9814 return false; 9815 9816 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op); 9817 update_stmt (gsi_stmt (*gsi)); 9818 return true; 9819 } 9820 9821 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has 9822 a known value range VR. 9823 9824 If there is one and only one value which will satisfy the 9825 conditional, then return that value. Else return NULL. 9826 9827 If signed overflow must be undefined for the value to satisfy 9828 the conditional, then set *STRICT_OVERFLOW_P to true. */ 9829 9830 static tree 9831 test_for_singularity (enum tree_code cond_code, tree op0, 9832 tree op1, value_range *vr, 9833 bool *strict_overflow_p) 9834 { 9835 tree min = NULL; 9836 tree max = NULL; 9837 9838 /* Extract minimum/maximum values which satisfy the conditional as it was 9839 written. */ 9840 if (cond_code == LE_EXPR || cond_code == LT_EXPR) 9841 { 9842 /* This should not be negative infinity; there is no overflow 9843 here. */ 9844 min = TYPE_MIN_VALUE (TREE_TYPE (op0)); 9845 9846 max = op1; 9847 if (cond_code == LT_EXPR && !is_overflow_infinity (max)) 9848 { 9849 tree one = build_int_cst (TREE_TYPE (op0), 1); 9850 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one); 9851 if (EXPR_P (max)) 9852 TREE_NO_WARNING (max) = 1; 9853 } 9854 } 9855 else if (cond_code == GE_EXPR || cond_code == GT_EXPR) 9856 { 9857 /* This should not be positive infinity; there is no overflow 9858 here. */ 9859 max = TYPE_MAX_VALUE (TREE_TYPE (op0)); 9860 9861 min = op1; 9862 if (cond_code == GT_EXPR && !is_overflow_infinity (min)) 9863 { 9864 tree one = build_int_cst (TREE_TYPE (op0), 1); 9865 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one); 9866 if (EXPR_P (min)) 9867 TREE_NO_WARNING (min) = 1; 9868 } 9869 } 9870 9871 /* Now refine the minimum and maximum values using any 9872 value range information we have for op0. */ 9873 if (min && max) 9874 { 9875 if (compare_values (vr->min, min) == 1) 9876 min = vr->min; 9877 if (compare_values (vr->max, max) == -1) 9878 max = vr->max; 9879 9880 /* If the new min/max values have converged to a single value, 9881 then there is only one value which can satisfy the condition, 9882 return that value. */ 9883 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min)) 9884 { 9885 if ((cond_code == LE_EXPR || cond_code == LT_EXPR) 9886 && is_overflow_infinity (vr->max)) 9887 *strict_overflow_p = true; 9888 if ((cond_code == GE_EXPR || cond_code == GT_EXPR) 9889 && is_overflow_infinity (vr->min)) 9890 *strict_overflow_p = true; 9891 9892 return min; 9893 } 9894 } 9895 return NULL; 9896 } 9897 9898 /* Return whether the value range *VR fits in an integer type specified 9899 by PRECISION and UNSIGNED_P. */ 9900 9901 static bool 9902 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn) 9903 { 9904 tree src_type; 9905 unsigned src_precision; 9906 widest_int tem; 9907 signop src_sgn; 9908 9909 /* We can only handle integral and pointer types. */ 9910 src_type = TREE_TYPE (vr->min); 9911 if (!INTEGRAL_TYPE_P (src_type) 9912 && !POINTER_TYPE_P (src_type)) 9913 return false; 9914 9915 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED, 9916 and so is an identity transform. */ 9917 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min)); 9918 src_sgn = TYPE_SIGN (src_type); 9919 if ((src_precision < dest_precision 9920 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED)) 9921 || (src_precision == dest_precision && src_sgn == dest_sgn)) 9922 return true; 9923 9924 /* Now we can only handle ranges with constant bounds. */ 9925 if (vr->type != VR_RANGE 9926 || TREE_CODE (vr->min) != INTEGER_CST 9927 || TREE_CODE (vr->max) != INTEGER_CST) 9928 return false; 9929 9930 /* For sign changes, the MSB of the wide_int has to be clear. 9931 An unsigned value with its MSB set cannot be represented by 9932 a signed wide_int, while a negative value cannot be represented 9933 by an unsigned wide_int. */ 9934 if (src_sgn != dest_sgn 9935 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0))) 9936 return false; 9937 9938 /* Then we can perform the conversion on both ends and compare 9939 the result for equality. */ 9940 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn); 9941 if (tem != wi::to_widest (vr->min)) 9942 return false; 9943 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn); 9944 if (tem != wi::to_widest (vr->max)) 9945 return false; 9946 9947 return true; 9948 } 9949 9950 /* Simplify a conditional using a relational operator to an equality 9951 test if the range information indicates only one value can satisfy 9952 the original conditional. */ 9953 9954 static bool 9955 simplify_cond_using_ranges (gcond *stmt) 9956 { 9957 tree op0 = gimple_cond_lhs (stmt); 9958 tree op1 = gimple_cond_rhs (stmt); 9959 enum tree_code cond_code = gimple_cond_code (stmt); 9960 9961 if (cond_code != NE_EXPR 9962 && cond_code != EQ_EXPR 9963 && TREE_CODE (op0) == SSA_NAME 9964 && INTEGRAL_TYPE_P (TREE_TYPE (op0)) 9965 && is_gimple_min_invariant (op1)) 9966 { 9967 value_range *vr = get_value_range (op0); 9968 9969 /* If we have range information for OP0, then we might be 9970 able to simplify this conditional. */ 9971 if (vr->type == VR_RANGE) 9972 { 9973 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON; 9974 bool sop = false; 9975 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop); 9976 9977 if (new_tree 9978 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))) 9979 { 9980 if (dump_file) 9981 { 9982 fprintf (dump_file, "Simplified relational "); 9983 print_gimple_stmt (dump_file, stmt, 0, 0); 9984 fprintf (dump_file, " into "); 9985 } 9986 9987 gimple_cond_set_code (stmt, EQ_EXPR); 9988 gimple_cond_set_lhs (stmt, op0); 9989 gimple_cond_set_rhs (stmt, new_tree); 9990 9991 update_stmt (stmt); 9992 9993 if (dump_file) 9994 { 9995 print_gimple_stmt (dump_file, stmt, 0, 0); 9996 fprintf (dump_file, "\n"); 9997 } 9998 9999 if (sop && issue_strict_overflow_warning (wc)) 10000 { 10001 location_t location = input_location; 10002 if (gimple_has_location (stmt)) 10003 location = gimple_location (stmt); 10004 10005 warning_at (location, OPT_Wstrict_overflow, 10006 "assuming signed overflow does not occur when " 10007 "simplifying conditional"); 10008 } 10009 10010 return true; 10011 } 10012 10013 /* Try again after inverting the condition. We only deal 10014 with integral types here, so no need to worry about 10015 issues with inverting FP comparisons. */ 10016 sop = false; 10017 new_tree = test_for_singularity 10018 (invert_tree_comparison (cond_code, false), 10019 op0, op1, vr, &sop); 10020 10021 if (new_tree 10022 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))) 10023 { 10024 if (dump_file) 10025 { 10026 fprintf (dump_file, "Simplified relational "); 10027 print_gimple_stmt (dump_file, stmt, 0, 0); 10028 fprintf (dump_file, " into "); 10029 } 10030 10031 gimple_cond_set_code (stmt, NE_EXPR); 10032 gimple_cond_set_lhs (stmt, op0); 10033 gimple_cond_set_rhs (stmt, new_tree); 10034 10035 update_stmt (stmt); 10036 10037 if (dump_file) 10038 { 10039 print_gimple_stmt (dump_file, stmt, 0, 0); 10040 fprintf (dump_file, "\n"); 10041 } 10042 10043 if (sop && issue_strict_overflow_warning (wc)) 10044 { 10045 location_t location = input_location; 10046 if (gimple_has_location (stmt)) 10047 location = gimple_location (stmt); 10048 10049 warning_at (location, OPT_Wstrict_overflow, 10050 "assuming signed overflow does not occur when " 10051 "simplifying conditional"); 10052 } 10053 10054 return true; 10055 } 10056 } 10057 } 10058 10059 /* If we have a comparison of an SSA_NAME (OP0) against a constant, 10060 see if OP0 was set by a type conversion where the source of 10061 the conversion is another SSA_NAME with a range that fits 10062 into the range of OP0's type. 10063 10064 If so, the conversion is redundant as the earlier SSA_NAME can be 10065 used for the comparison directly if we just massage the constant in the 10066 comparison. */ 10067 if (TREE_CODE (op0) == SSA_NAME 10068 && TREE_CODE (op1) == INTEGER_CST) 10069 { 10070 gimple *def_stmt = SSA_NAME_DEF_STMT (op0); 10071 tree innerop; 10072 10073 if (!is_gimple_assign (def_stmt) 10074 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) 10075 return false; 10076 10077 innerop = gimple_assign_rhs1 (def_stmt); 10078 10079 if (TREE_CODE (innerop) == SSA_NAME 10080 && !POINTER_TYPE_P (TREE_TYPE (innerop)) 10081 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop) 10082 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0))) 10083 { 10084 value_range *vr = get_value_range (innerop); 10085 10086 if (range_int_cst_p (vr) 10087 && range_fits_type_p (vr, 10088 TYPE_PRECISION (TREE_TYPE (op0)), 10089 TYPE_SIGN (TREE_TYPE (op0))) 10090 && int_fits_type_p (op1, TREE_TYPE (innerop)) 10091 /* The range must not have overflowed, or if it did overflow 10092 we must not be wrapping/trapping overflow and optimizing 10093 with strict overflow semantics. */ 10094 && ((!is_negative_overflow_infinity (vr->min) 10095 && !is_positive_overflow_infinity (vr->max)) 10096 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop)))) 10097 { 10098 /* If the range overflowed and the user has asked for warnings 10099 when strict overflow semantics were used to optimize code, 10100 issue an appropriate warning. */ 10101 if (cond_code != EQ_EXPR && cond_code != NE_EXPR 10102 && (is_negative_overflow_infinity (vr->min) 10103 || is_positive_overflow_infinity (vr->max)) 10104 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL)) 10105 { 10106 location_t location; 10107 10108 if (!gimple_has_location (stmt)) 10109 location = input_location; 10110 else 10111 location = gimple_location (stmt); 10112 warning_at (location, OPT_Wstrict_overflow, 10113 "assuming signed overflow does not occur when " 10114 "simplifying conditional"); 10115 } 10116 10117 tree newconst = fold_convert (TREE_TYPE (innerop), op1); 10118 gimple_cond_set_lhs (stmt, innerop); 10119 gimple_cond_set_rhs (stmt, newconst); 10120 return true; 10121 } 10122 } 10123 } 10124 10125 return false; 10126 } 10127 10128 /* Simplify a switch statement using the value range of the switch 10129 argument. */ 10130 10131 static bool 10132 simplify_switch_using_ranges (gswitch *stmt) 10133 { 10134 tree op = gimple_switch_index (stmt); 10135 value_range *vr = NULL; 10136 bool take_default; 10137 edge e; 10138 edge_iterator ei; 10139 size_t i = 0, j = 0, n, n2; 10140 tree vec2; 10141 switch_update su; 10142 size_t k = 1, l = 0; 10143 10144 if (TREE_CODE (op) == SSA_NAME) 10145 { 10146 vr = get_value_range (op); 10147 10148 /* We can only handle integer ranges. */ 10149 if ((vr->type != VR_RANGE 10150 && vr->type != VR_ANTI_RANGE) 10151 || symbolic_range_p (vr)) 10152 return false; 10153 10154 /* Find case label for min/max of the value range. */ 10155 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l); 10156 } 10157 else if (TREE_CODE (op) == INTEGER_CST) 10158 { 10159 take_default = !find_case_label_index (stmt, 1, op, &i); 10160 if (take_default) 10161 { 10162 i = 1; 10163 j = 0; 10164 } 10165 else 10166 { 10167 j = i; 10168 } 10169 } 10170 else 10171 return false; 10172 10173 n = gimple_switch_num_labels (stmt); 10174 10175 /* We can truncate the case label ranges that partially overlap with OP's 10176 value range. */ 10177 size_t min_idx = 1, max_idx = 0; 10178 if (vr != NULL) 10179 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx); 10180 if (min_idx <= max_idx) 10181 { 10182 tree min_label = gimple_switch_label (stmt, min_idx); 10183 tree max_label = gimple_switch_label (stmt, max_idx); 10184 10185 /* Avoid changing the type of the case labels when truncating. */ 10186 tree case_label_type = TREE_TYPE (CASE_LOW (min_label)); 10187 tree vr_min = fold_convert (case_label_type, vr->min); 10188 tree vr_max = fold_convert (case_label_type, vr->max); 10189 10190 if (vr->type == VR_RANGE) 10191 { 10192 /* If OP's value range is [2,8] and the low label range is 10193 0 ... 3, truncate the label's range to 2 .. 3. */ 10194 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0 10195 && CASE_HIGH (min_label) != NULL_TREE 10196 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0) 10197 CASE_LOW (min_label) = vr_min; 10198 10199 /* If OP's value range is [2,8] and the high label range is 10200 7 ... 10, truncate the label's range to 7 .. 8. */ 10201 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0 10202 && CASE_HIGH (max_label) != NULL_TREE 10203 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0) 10204 CASE_HIGH (max_label) = vr_max; 10205 } 10206 else if (vr->type == VR_ANTI_RANGE) 10207 { 10208 tree one_cst = build_one_cst (case_label_type); 10209 10210 if (min_label == max_label) 10211 { 10212 /* If OP's value range is ~[7,8] and the label's range is 10213 7 ... 10, truncate the label's range to 9 ... 10. */ 10214 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0 10215 && CASE_HIGH (min_label) != NULL_TREE 10216 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0) 10217 CASE_LOW (min_label) 10218 = int_const_binop (PLUS_EXPR, vr_max, one_cst); 10219 10220 /* If OP's value range is ~[7,8] and the label's range is 10221 5 ... 8, truncate the label's range to 5 ... 6. */ 10222 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0 10223 && CASE_HIGH (min_label) != NULL_TREE 10224 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0) 10225 CASE_HIGH (min_label) 10226 = int_const_binop (MINUS_EXPR, vr_min, one_cst); 10227 } 10228 else 10229 { 10230 /* If OP's value range is ~[2,8] and the low label range is 10231 0 ... 3, truncate the label's range to 0 ... 1. */ 10232 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0 10233 && CASE_HIGH (min_label) != NULL_TREE 10234 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0) 10235 CASE_HIGH (min_label) 10236 = int_const_binop (MINUS_EXPR, vr_min, one_cst); 10237 10238 /* If OP's value range is ~[2,8] and the high label range is 10239 7 ... 10, truncate the label's range to 9 ... 10. */ 10240 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0 10241 && CASE_HIGH (max_label) != NULL_TREE 10242 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0) 10243 CASE_LOW (max_label) 10244 = int_const_binop (PLUS_EXPR, vr_max, one_cst); 10245 } 10246 } 10247 10248 /* Canonicalize singleton case ranges. */ 10249 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label))) 10250 CASE_HIGH (min_label) = NULL_TREE; 10251 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label))) 10252 CASE_HIGH (max_label) = NULL_TREE; 10253 } 10254 10255 /* We can also eliminate case labels that lie completely outside OP's value 10256 range. */ 10257 10258 /* Bail out if this is just all edges taken. */ 10259 if (i == 1 10260 && j == n - 1 10261 && take_default) 10262 return false; 10263 10264 /* Build a new vector of taken case labels. */ 10265 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default); 10266 n2 = 0; 10267 10268 /* Add the default edge, if necessary. */ 10269 if (take_default) 10270 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt); 10271 10272 for (; i <= j; ++i, ++n2) 10273 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i); 10274 10275 for (; k <= l; ++k, ++n2) 10276 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k); 10277 10278 /* Mark needed edges. */ 10279 for (i = 0; i < n2; ++i) 10280 { 10281 e = find_edge (gimple_bb (stmt), 10282 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i)))); 10283 e->aux = (void *)-1; 10284 } 10285 10286 /* Queue not needed edges for later removal. */ 10287 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs) 10288 { 10289 if (e->aux == (void *)-1) 10290 { 10291 e->aux = NULL; 10292 continue; 10293 } 10294 10295 if (dump_file && (dump_flags & TDF_DETAILS)) 10296 { 10297 fprintf (dump_file, "removing unreachable case label\n"); 10298 } 10299 to_remove_edges.safe_push (e); 10300 e->flags &= ~EDGE_EXECUTABLE; 10301 } 10302 10303 /* And queue an update for the stmt. */ 10304 su.stmt = stmt; 10305 su.vec = vec2; 10306 to_update_switch_stmts.safe_push (su); 10307 return false; 10308 } 10309 10310 /* Simplify an integral conversion from an SSA name in STMT. */ 10311 10312 static bool 10313 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt) 10314 { 10315 tree innerop, middleop, finaltype; 10316 gimple *def_stmt; 10317 signop inner_sgn, middle_sgn, final_sgn; 10318 unsigned inner_prec, middle_prec, final_prec; 10319 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax; 10320 10321 finaltype = TREE_TYPE (gimple_assign_lhs (stmt)); 10322 if (!INTEGRAL_TYPE_P (finaltype)) 10323 return false; 10324 middleop = gimple_assign_rhs1 (stmt); 10325 def_stmt = SSA_NAME_DEF_STMT (middleop); 10326 if (!is_gimple_assign (def_stmt) 10327 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) 10328 return false; 10329 innerop = gimple_assign_rhs1 (def_stmt); 10330 if (TREE_CODE (innerop) != SSA_NAME 10331 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)) 10332 return false; 10333 10334 /* Get the value-range of the inner operand. Use get_range_info in 10335 case innerop was created during substitute-and-fold. */ 10336 wide_int imin, imax; 10337 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)) 10338 || get_range_info (innerop, &imin, &imax) != VR_RANGE) 10339 return false; 10340 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop))); 10341 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop))); 10342 10343 /* Simulate the conversion chain to check if the result is equal if 10344 the middle conversion is removed. */ 10345 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop)); 10346 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop)); 10347 final_prec = TYPE_PRECISION (finaltype); 10348 10349 /* If the first conversion is not injective, the second must not 10350 be widening. */ 10351 if (wi::gtu_p (innermax - innermin, 10352 wi::mask <widest_int> (middle_prec, false)) 10353 && middle_prec < final_prec) 10354 return false; 10355 /* We also want a medium value so that we can track the effect that 10356 narrowing conversions with sign change have. */ 10357 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop)); 10358 if (inner_sgn == UNSIGNED) 10359 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false); 10360 else 10361 innermed = 0; 10362 if (wi::cmp (innermin, innermed, inner_sgn) >= 0 10363 || wi::cmp (innermed, innermax, inner_sgn) >= 0) 10364 innermed = innermin; 10365 10366 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop)); 10367 middlemin = wi::ext (innermin, middle_prec, middle_sgn); 10368 middlemed = wi::ext (innermed, middle_prec, middle_sgn); 10369 middlemax = wi::ext (innermax, middle_prec, middle_sgn); 10370 10371 /* Require that the final conversion applied to both the original 10372 and the intermediate range produces the same result. */ 10373 final_sgn = TYPE_SIGN (finaltype); 10374 if (wi::ext (middlemin, final_prec, final_sgn) 10375 != wi::ext (innermin, final_prec, final_sgn) 10376 || wi::ext (middlemed, final_prec, final_sgn) 10377 != wi::ext (innermed, final_prec, final_sgn) 10378 || wi::ext (middlemax, final_prec, final_sgn) 10379 != wi::ext (innermax, final_prec, final_sgn)) 10380 return false; 10381 10382 gimple_assign_set_rhs1 (stmt, innerop); 10383 fold_stmt (gsi, follow_single_use_edges); 10384 return true; 10385 } 10386 10387 /* Simplify a conversion from integral SSA name to float in STMT. */ 10388 10389 static bool 10390 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, 10391 gimple *stmt) 10392 { 10393 tree rhs1 = gimple_assign_rhs1 (stmt); 10394 value_range *vr = get_value_range (rhs1); 10395 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt))); 10396 machine_mode mode; 10397 tree tem; 10398 gassign *conv; 10399 10400 /* We can only handle constant ranges. */ 10401 if (vr->type != VR_RANGE 10402 || TREE_CODE (vr->min) != INTEGER_CST 10403 || TREE_CODE (vr->max) != INTEGER_CST) 10404 return false; 10405 10406 /* First check if we can use a signed type in place of an unsigned. */ 10407 if (TYPE_UNSIGNED (TREE_TYPE (rhs1)) 10408 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0) 10409 != CODE_FOR_nothing) 10410 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED)) 10411 mode = TYPE_MODE (TREE_TYPE (rhs1)); 10412 /* If we can do the conversion in the current input mode do nothing. */ 10413 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 10414 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing) 10415 return false; 10416 /* Otherwise search for a mode we can use, starting from the narrowest 10417 integer mode available. */ 10418 else 10419 { 10420 mode = GET_CLASS_NARROWEST_MODE (MODE_INT); 10421 do 10422 { 10423 /* If we cannot do a signed conversion to float from mode 10424 or if the value-range does not fit in the signed type 10425 try with a wider mode. */ 10426 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing 10427 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED)) 10428 break; 10429 10430 mode = GET_MODE_WIDER_MODE (mode); 10431 /* But do not widen the input. Instead leave that to the 10432 optabs expansion code. */ 10433 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1))) 10434 return false; 10435 } 10436 while (mode != VOIDmode); 10437 if (mode == VOIDmode) 10438 return false; 10439 } 10440 10441 /* It works, insert a truncation or sign-change before the 10442 float conversion. */ 10443 tem = make_ssa_name (build_nonstandard_integer_type 10444 (GET_MODE_PRECISION (mode), 0)); 10445 conv = gimple_build_assign (tem, NOP_EXPR, rhs1); 10446 gsi_insert_before (gsi, conv, GSI_SAME_STMT); 10447 gimple_assign_set_rhs1 (stmt, tem); 10448 fold_stmt (gsi, follow_single_use_edges); 10449 10450 return true; 10451 } 10452 10453 /* Simplify an internal fn call using ranges if possible. */ 10454 10455 static bool 10456 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt) 10457 { 10458 enum tree_code subcode; 10459 bool is_ubsan = false; 10460 bool ovf = false; 10461 switch (gimple_call_internal_fn (stmt)) 10462 { 10463 case IFN_UBSAN_CHECK_ADD: 10464 subcode = PLUS_EXPR; 10465 is_ubsan = true; 10466 break; 10467 case IFN_UBSAN_CHECK_SUB: 10468 subcode = MINUS_EXPR; 10469 is_ubsan = true; 10470 break; 10471 case IFN_UBSAN_CHECK_MUL: 10472 subcode = MULT_EXPR; 10473 is_ubsan = true; 10474 break; 10475 case IFN_ADD_OVERFLOW: 10476 subcode = PLUS_EXPR; 10477 break; 10478 case IFN_SUB_OVERFLOW: 10479 subcode = MINUS_EXPR; 10480 break; 10481 case IFN_MUL_OVERFLOW: 10482 subcode = MULT_EXPR; 10483 break; 10484 default: 10485 return false; 10486 } 10487 10488 tree op0 = gimple_call_arg (stmt, 0); 10489 tree op1 = gimple_call_arg (stmt, 1); 10490 tree type; 10491 if (is_ubsan) 10492 { 10493 type = TREE_TYPE (op0); 10494 if (VECTOR_TYPE_P (type)) 10495 return false; 10496 } 10497 else if (gimple_call_lhs (stmt) == NULL_TREE) 10498 return false; 10499 else 10500 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt))); 10501 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf) 10502 || (is_ubsan && ovf)) 10503 return false; 10504 10505 gimple *g; 10506 location_t loc = gimple_location (stmt); 10507 if (is_ubsan) 10508 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1); 10509 else 10510 { 10511 int prec = TYPE_PRECISION (type); 10512 tree utype = type; 10513 if (ovf 10514 || !useless_type_conversion_p (type, TREE_TYPE (op0)) 10515 || !useless_type_conversion_p (type, TREE_TYPE (op1))) 10516 utype = build_nonstandard_integer_type (prec, 1); 10517 if (TREE_CODE (op0) == INTEGER_CST) 10518 op0 = fold_convert (utype, op0); 10519 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0))) 10520 { 10521 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0); 10522 gimple_set_location (g, loc); 10523 gsi_insert_before (gsi, g, GSI_SAME_STMT); 10524 op0 = gimple_assign_lhs (g); 10525 } 10526 if (TREE_CODE (op1) == INTEGER_CST) 10527 op1 = fold_convert (utype, op1); 10528 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1))) 10529 { 10530 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1); 10531 gimple_set_location (g, loc); 10532 gsi_insert_before (gsi, g, GSI_SAME_STMT); 10533 op1 = gimple_assign_lhs (g); 10534 } 10535 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1); 10536 gimple_set_location (g, loc); 10537 gsi_insert_before (gsi, g, GSI_SAME_STMT); 10538 if (utype != type) 10539 { 10540 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, 10541 gimple_assign_lhs (g)); 10542 gimple_set_location (g, loc); 10543 gsi_insert_before (gsi, g, GSI_SAME_STMT); 10544 } 10545 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR, 10546 gimple_assign_lhs (g), 10547 build_int_cst (type, ovf)); 10548 } 10549 gimple_set_location (g, loc); 10550 gsi_replace (gsi, g, false); 10551 return true; 10552 } 10553 10554 /* Return true if VAR is a two-valued variable. Set a and b with the 10555 two-values when it is true. Return false otherwise. */ 10556 10557 static bool 10558 two_valued_val_range_p (tree var, tree *a, tree *b) 10559 { 10560 value_range *vr = get_value_range (var); 10561 if ((vr->type != VR_RANGE 10562 && vr->type != VR_ANTI_RANGE) 10563 || TREE_CODE (vr->min) != INTEGER_CST 10564 || TREE_CODE (vr->max) != INTEGER_CST) 10565 return false; 10566 10567 if (vr->type == VR_RANGE 10568 && wi::sub (vr->max, vr->min) == 1) 10569 { 10570 *a = vr->min; 10571 *b = vr->max; 10572 return true; 10573 } 10574 10575 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */ 10576 if (vr->type == VR_ANTI_RANGE 10577 && wi::sub (vr->min, vrp_val_min (TREE_TYPE (var))) == 1 10578 && wi::sub (vrp_val_max (TREE_TYPE (var)), vr->max) == 1) 10579 { 10580 *a = vrp_val_min (TREE_TYPE (var)); 10581 *b = vrp_val_max (TREE_TYPE (var)); 10582 return true; 10583 } 10584 10585 return false; 10586 } 10587 10588 /* Simplify STMT using ranges if possible. */ 10589 10590 static bool 10591 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi) 10592 { 10593 gimple *stmt = gsi_stmt (*gsi); 10594 if (is_gimple_assign (stmt)) 10595 { 10596 enum tree_code rhs_code = gimple_assign_rhs_code (stmt); 10597 tree rhs1 = gimple_assign_rhs1 (stmt); 10598 tree rhs2 = gimple_assign_rhs2 (stmt); 10599 tree lhs = gimple_assign_lhs (stmt); 10600 tree val1 = NULL_TREE, val2 = NULL_TREE; 10601 use_operand_p use_p; 10602 gimple *use_stmt; 10603 10604 /* Convert: 10605 LHS = CST BINOP VAR 10606 Where VAR is two-valued and LHS is used in GIMPLE_COND only 10607 To: 10608 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2) 10609 10610 Also handles: 10611 LHS = VAR BINOP CST 10612 Where VAR is two-valued and LHS is used in GIMPLE_COND only 10613 To: 10614 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */ 10615 10616 if (TREE_CODE_CLASS (rhs_code) == tcc_binary 10617 && INTEGRAL_TYPE_P (TREE_TYPE (lhs)) 10618 && ((TREE_CODE (rhs1) == INTEGER_CST 10619 && TREE_CODE (rhs2) == SSA_NAME) 10620 || (TREE_CODE (rhs2) == INTEGER_CST 10621 && TREE_CODE (rhs1) == SSA_NAME)) 10622 && single_imm_use (lhs, &use_p, &use_stmt) 10623 && gimple_code (use_stmt) == GIMPLE_COND) 10624 10625 { 10626 tree new_rhs1 = NULL_TREE; 10627 tree new_rhs2 = NULL_TREE; 10628 tree cmp_var = NULL_TREE; 10629 10630 if (TREE_CODE (rhs2) == SSA_NAME 10631 && two_valued_val_range_p (rhs2, &val1, &val2)) 10632 { 10633 /* Optimize RHS1 OP [VAL1, VAL2]. */ 10634 new_rhs1 = int_const_binop (rhs_code, rhs1, val1); 10635 new_rhs2 = int_const_binop (rhs_code, rhs1, val2); 10636 cmp_var = rhs2; 10637 } 10638 else if (TREE_CODE (rhs1) == SSA_NAME 10639 && two_valued_val_range_p (rhs1, &val1, &val2)) 10640 { 10641 /* Optimize [VAL1, VAL2] OP RHS2. */ 10642 new_rhs1 = int_const_binop (rhs_code, val1, rhs2); 10643 new_rhs2 = int_const_binop (rhs_code, val2, rhs2); 10644 cmp_var = rhs1; 10645 } 10646 10647 /* If we could not find two-vals or the optimzation is invalid as 10648 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */ 10649 if (new_rhs1 && new_rhs2) 10650 { 10651 tree cond = build2 (EQ_EXPR, boolean_type_node, cmp_var, val1); 10652 gimple_assign_set_rhs_with_ops (gsi, 10653 COND_EXPR, cond, 10654 new_rhs1, 10655 new_rhs2); 10656 update_stmt (gsi_stmt (*gsi)); 10657 fold_stmt (gsi, follow_single_use_edges); 10658 return true; 10659 } 10660 } 10661 10662 switch (rhs_code) 10663 { 10664 case EQ_EXPR: 10665 case NE_EXPR: 10666 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity 10667 if the RHS is zero or one, and the LHS are known to be boolean 10668 values. */ 10669 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) 10670 return simplify_truth_ops_using_ranges (gsi, stmt); 10671 break; 10672 10673 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR 10674 and BIT_AND_EXPR respectively if the first operand is greater 10675 than zero and the second operand is an exact power of two. 10676 Also optimize TRUNC_MOD_EXPR away if the second operand is 10677 constant and the first operand already has the right value 10678 range. */ 10679 case TRUNC_DIV_EXPR: 10680 case TRUNC_MOD_EXPR: 10681 if ((TREE_CODE (rhs1) == SSA_NAME 10682 || TREE_CODE (rhs1) == INTEGER_CST) 10683 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) 10684 return simplify_div_or_mod_using_ranges (gsi, stmt); 10685 break; 10686 10687 /* Transform ABS (X) into X or -X as appropriate. */ 10688 case ABS_EXPR: 10689 if (TREE_CODE (rhs1) == SSA_NAME 10690 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) 10691 return simplify_abs_using_ranges (gsi, stmt); 10692 break; 10693 10694 case BIT_AND_EXPR: 10695 case BIT_IOR_EXPR: 10696 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR 10697 if all the bits being cleared are already cleared or 10698 all the bits being set are already set. */ 10699 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) 10700 return simplify_bit_ops_using_ranges (gsi, stmt); 10701 break; 10702 10703 CASE_CONVERT: 10704 if (TREE_CODE (rhs1) == SSA_NAME 10705 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) 10706 return simplify_conversion_using_ranges (gsi, stmt); 10707 break; 10708 10709 case FLOAT_EXPR: 10710 if (TREE_CODE (rhs1) == SSA_NAME 10711 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) 10712 return simplify_float_conversion_using_ranges (gsi, stmt); 10713 break; 10714 10715 case MIN_EXPR: 10716 case MAX_EXPR: 10717 return simplify_min_or_max_using_ranges (gsi, stmt); 10718 10719 default: 10720 break; 10721 } 10722 } 10723 else if (gimple_code (stmt) == GIMPLE_COND) 10724 return simplify_cond_using_ranges (as_a <gcond *> (stmt)); 10725 else if (gimple_code (stmt) == GIMPLE_SWITCH) 10726 return simplify_switch_using_ranges (as_a <gswitch *> (stmt)); 10727 else if (is_gimple_call (stmt) 10728 && gimple_call_internal_p (stmt)) 10729 return simplify_internal_call_using_ranges (gsi, stmt); 10730 10731 return false; 10732 } 10733 10734 /* If the statement pointed by SI has a predicate whose value can be 10735 computed using the value range information computed by VRP, compute 10736 its value and return true. Otherwise, return false. */ 10737 10738 static bool 10739 fold_predicate_in (gimple_stmt_iterator *si) 10740 { 10741 bool assignment_p = false; 10742 tree val; 10743 gimple *stmt = gsi_stmt (*si); 10744 10745 if (is_gimple_assign (stmt) 10746 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison) 10747 { 10748 assignment_p = true; 10749 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt), 10750 gimple_assign_rhs1 (stmt), 10751 gimple_assign_rhs2 (stmt), 10752 stmt); 10753 } 10754 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt)) 10755 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt), 10756 gimple_cond_lhs (cond_stmt), 10757 gimple_cond_rhs (cond_stmt), 10758 stmt); 10759 else 10760 return false; 10761 10762 if (val) 10763 { 10764 if (assignment_p) 10765 val = fold_convert (gimple_expr_type (stmt), val); 10766 10767 if (dump_file) 10768 { 10769 fprintf (dump_file, "Folding predicate "); 10770 print_gimple_expr (dump_file, stmt, 0, 0); 10771 fprintf (dump_file, " to "); 10772 print_generic_expr (dump_file, val, 0); 10773 fprintf (dump_file, "\n"); 10774 } 10775 10776 if (is_gimple_assign (stmt)) 10777 gimple_assign_set_rhs_from_tree (si, val); 10778 else 10779 { 10780 gcc_assert (gimple_code (stmt) == GIMPLE_COND); 10781 gcond *cond_stmt = as_a <gcond *> (stmt); 10782 if (integer_zerop (val)) 10783 gimple_cond_make_false (cond_stmt); 10784 else if (integer_onep (val)) 10785 gimple_cond_make_true (cond_stmt); 10786 else 10787 gcc_unreachable (); 10788 } 10789 10790 return true; 10791 } 10792 10793 return false; 10794 } 10795 10796 /* Callback for substitute_and_fold folding the stmt at *SI. */ 10797 10798 static bool 10799 vrp_fold_stmt (gimple_stmt_iterator *si) 10800 { 10801 if (fold_predicate_in (si)) 10802 return true; 10803 10804 return simplify_stmt_using_ranges (si); 10805 } 10806 10807 /* Return the LHS of any ASSERT_EXPR where OP appears as the first 10808 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates 10809 BB. If no such ASSERT_EXPR is found, return OP. */ 10810 10811 static tree 10812 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt) 10813 { 10814 imm_use_iterator imm_iter; 10815 gimple *use_stmt; 10816 use_operand_p use_p; 10817 10818 if (TREE_CODE (op) == SSA_NAME) 10819 { 10820 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op) 10821 { 10822 use_stmt = USE_STMT (use_p); 10823 if (use_stmt != stmt 10824 && gimple_assign_single_p (use_stmt) 10825 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR 10826 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op 10827 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt))) 10828 return gimple_assign_lhs (use_stmt); 10829 } 10830 } 10831 return op; 10832 } 10833 10834 /* A trivial wrapper so that we can present the generic jump threading 10835 code with a simple API for simplifying statements. STMT is the 10836 statement we want to simplify, WITHIN_STMT provides the location 10837 for any overflow warnings. */ 10838 10839 static tree 10840 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt, 10841 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED, 10842 basic_block bb) 10843 { 10844 /* First see if the conditional is in the hash table. */ 10845 tree cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, false, true); 10846 if (cached_lhs && is_gimple_min_invariant (cached_lhs)) 10847 return cached_lhs; 10848 10849 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt)) 10850 { 10851 tree op0 = gimple_cond_lhs (cond_stmt); 10852 op0 = lhs_of_dominating_assert (op0, bb, stmt); 10853 10854 tree op1 = gimple_cond_rhs (cond_stmt); 10855 op1 = lhs_of_dominating_assert (op1, bb, stmt); 10856 10857 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt), 10858 op0, op1, within_stmt); 10859 } 10860 10861 /* We simplify a switch statement by trying to determine which case label 10862 will be taken. If we are successful then we return the corresponding 10863 CASE_LABEL_EXPR. */ 10864 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt)) 10865 { 10866 tree op = gimple_switch_index (switch_stmt); 10867 if (TREE_CODE (op) != SSA_NAME) 10868 return NULL_TREE; 10869 10870 op = lhs_of_dominating_assert (op, bb, stmt); 10871 10872 value_range *vr = get_value_range (op); 10873 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE) 10874 || symbolic_range_p (vr)) 10875 return NULL_TREE; 10876 10877 if (vr->type == VR_RANGE) 10878 { 10879 size_t i, j; 10880 /* Get the range of labels that contain a part of the operand's 10881 value range. */ 10882 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j); 10883 10884 /* Is there only one such label? */ 10885 if (i == j) 10886 { 10887 tree label = gimple_switch_label (switch_stmt, i); 10888 10889 /* The i'th label will be taken only if the value range of the 10890 operand is entirely within the bounds of this label. */ 10891 if (CASE_HIGH (label) != NULL_TREE 10892 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0 10893 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0) 10894 : (tree_int_cst_equal (CASE_LOW (label), vr->min) 10895 && tree_int_cst_equal (vr->min, vr->max))) 10896 return label; 10897 } 10898 10899 /* If there are no such labels then the default label will be 10900 taken. */ 10901 if (i > j) 10902 return gimple_switch_label (switch_stmt, 0); 10903 } 10904 10905 if (vr->type == VR_ANTI_RANGE) 10906 { 10907 unsigned n = gimple_switch_num_labels (switch_stmt); 10908 tree min_label = gimple_switch_label (switch_stmt, 1); 10909 tree max_label = gimple_switch_label (switch_stmt, n - 1); 10910 10911 /* The default label will be taken only if the anti-range of the 10912 operand is entirely outside the bounds of all the (non-default) 10913 case labels. */ 10914 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0 10915 && (CASE_HIGH (max_label) != NULL_TREE 10916 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0 10917 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0)) 10918 return gimple_switch_label (switch_stmt, 0); 10919 } 10920 10921 return NULL_TREE; 10922 } 10923 10924 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt)) 10925 { 10926 value_range new_vr = VR_INITIALIZER; 10927 tree lhs = gimple_assign_lhs (assign_stmt); 10928 10929 if (TREE_CODE (lhs) == SSA_NAME 10930 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs)) 10931 || POINTER_TYPE_P (TREE_TYPE (lhs)))) 10932 { 10933 extract_range_from_assignment (&new_vr, assign_stmt); 10934 if (range_int_cst_singleton_p (&new_vr)) 10935 return new_vr.min; 10936 } 10937 } 10938 10939 return NULL_TREE; 10940 } 10941 10942 class vrp_dom_walker : public dom_walker 10943 { 10944 public: 10945 vrp_dom_walker (cdi_direction direction, 10946 class const_and_copies *const_and_copies, 10947 class avail_exprs_stack *avail_exprs_stack) 10948 : dom_walker (direction, true), 10949 m_const_and_copies (const_and_copies), 10950 m_avail_exprs_stack (avail_exprs_stack), 10951 m_dummy_cond (NULL) {} 10952 10953 virtual edge before_dom_children (basic_block); 10954 virtual void after_dom_children (basic_block); 10955 10956 private: 10957 class const_and_copies *m_const_and_copies; 10958 class avail_exprs_stack *m_avail_exprs_stack; 10959 10960 gcond *m_dummy_cond; 10961 }; 10962 10963 /* Called before processing dominator children of BB. We want to look 10964 at ASSERT_EXPRs and record information from them in the appropriate 10965 tables. 10966 10967 We could look at other statements here. It's not seen as likely 10968 to significantly increase the jump threads we discover. */ 10969 10970 edge 10971 vrp_dom_walker::before_dom_children (basic_block bb) 10972 { 10973 gimple_stmt_iterator gsi; 10974 10975 for (gsi = gsi_start_nondebug_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 10976 { 10977 gimple *stmt = gsi_stmt (gsi); 10978 if (gimple_assign_single_p (stmt) 10979 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR) 10980 { 10981 tree rhs1 = gimple_assign_rhs1 (stmt); 10982 tree cond = TREE_OPERAND (rhs1, 1); 10983 tree inverted = invert_truthvalue (cond); 10984 vec<cond_equivalence> p; 10985 p.create (3); 10986 record_conditions (&p, cond, inverted); 10987 for (unsigned int i = 0; i < p.length (); i++) 10988 m_avail_exprs_stack->record_cond (&p[i]); 10989 10990 tree lhs = gimple_assign_lhs (stmt); 10991 m_const_and_copies->record_const_or_copy (lhs, 10992 TREE_OPERAND (rhs1, 0)); 10993 p.release (); 10994 continue; 10995 } 10996 break; 10997 } 10998 return NULL; 10999 } 11000 11001 /* Called after processing dominator children of BB. This is where we 11002 actually call into the threader. */ 11003 void 11004 vrp_dom_walker::after_dom_children (basic_block bb) 11005 { 11006 if (!m_dummy_cond) 11007 m_dummy_cond = gimple_build_cond (NE_EXPR, 11008 integer_zero_node, integer_zero_node, 11009 NULL, NULL); 11010 11011 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies, 11012 m_avail_exprs_stack, 11013 simplify_stmt_for_jump_threading); 11014 11015 m_avail_exprs_stack->pop_to_marker (); 11016 m_const_and_copies->pop_to_marker (); 11017 } 11018 11019 /* Blocks which have more than one predecessor and more than 11020 one successor present jump threading opportunities, i.e., 11021 when the block is reached from a specific predecessor, we 11022 may be able to determine which of the outgoing edges will 11023 be traversed. When this optimization applies, we are able 11024 to avoid conditionals at runtime and we may expose secondary 11025 optimization opportunities. 11026 11027 This routine is effectively a driver for the generic jump 11028 threading code. It basically just presents the generic code 11029 with edges that may be suitable for jump threading. 11030 11031 Unlike DOM, we do not iterate VRP if jump threading was successful. 11032 While iterating may expose new opportunities for VRP, it is expected 11033 those opportunities would be very limited and the compile time cost 11034 to expose those opportunities would be significant. 11035 11036 As jump threading opportunities are discovered, they are registered 11037 for later realization. */ 11038 11039 static void 11040 identify_jump_threads (void) 11041 { 11042 int i; 11043 edge e; 11044 11045 /* Ugh. When substituting values earlier in this pass we can 11046 wipe the dominance information. So rebuild the dominator 11047 information as we need it within the jump threading code. */ 11048 calculate_dominance_info (CDI_DOMINATORS); 11049 11050 /* We do not allow VRP information to be used for jump threading 11051 across a back edge in the CFG. Otherwise it becomes too 11052 difficult to avoid eliminating loop exit tests. Of course 11053 EDGE_DFS_BACK is not accurate at this time so we have to 11054 recompute it. */ 11055 mark_dfs_back_edges (); 11056 11057 /* Do not thread across edges we are about to remove. Just marking 11058 them as EDGE_IGNORE will do. */ 11059 FOR_EACH_VEC_ELT (to_remove_edges, i, e) 11060 e->flags |= EDGE_IGNORE; 11061 11062 /* Allocate our unwinder stack to unwind any temporary equivalences 11063 that might be recorded. */ 11064 const_and_copies *equiv_stack = new const_and_copies (); 11065 11066 hash_table<expr_elt_hasher> *avail_exprs 11067 = new hash_table<expr_elt_hasher> (1024); 11068 avail_exprs_stack *avail_exprs_stack 11069 = new class avail_exprs_stack (avail_exprs); 11070 11071 vrp_dom_walker walker (CDI_DOMINATORS, equiv_stack, avail_exprs_stack); 11072 walker.walk (cfun->cfg->x_entry_block_ptr); 11073 11074 /* Clear EDGE_IGNORE. */ 11075 FOR_EACH_VEC_ELT (to_remove_edges, i, e) 11076 e->flags &= ~EDGE_IGNORE; 11077 11078 /* We do not actually update the CFG or SSA graphs at this point as 11079 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet 11080 handle ASSERT_EXPRs gracefully. */ 11081 delete equiv_stack; 11082 delete avail_exprs; 11083 delete avail_exprs_stack; 11084 } 11085 11086 /* Free VRP lattice. */ 11087 11088 static void 11089 vrp_free_lattice () 11090 { 11091 /* Free allocated memory. */ 11092 free (vr_value); 11093 free (vr_phi_edge_counts); 11094 bitmap_obstack_release (&vrp_equiv_obstack); 11095 vrp_value_range_pool.release (); 11096 11097 /* So that we can distinguish between VRP data being available 11098 and not available. */ 11099 vr_value = NULL; 11100 vr_phi_edge_counts = NULL; 11101 } 11102 11103 /* Traverse all the blocks folding conditionals with known ranges. */ 11104 11105 static void 11106 vrp_finalize (bool warn_array_bounds_p) 11107 { 11108 size_t i; 11109 11110 values_propagated = true; 11111 11112 if (dump_file) 11113 { 11114 fprintf (dump_file, "\nValue ranges after VRP:\n\n"); 11115 dump_all_value_ranges (dump_file); 11116 fprintf (dump_file, "\n"); 11117 } 11118 11119 /* Set value range to non pointer SSA_NAMEs. */ 11120 for (i = 0; i < num_vr_values; i++) 11121 if (vr_value[i]) 11122 { 11123 tree name = ssa_name (i); 11124 11125 if (!name 11126 || (vr_value[i]->type == VR_VARYING) 11127 || (vr_value[i]->type == VR_UNDEFINED) 11128 || (TREE_CODE (vr_value[i]->min) != INTEGER_CST) 11129 || (TREE_CODE (vr_value[i]->max) != INTEGER_CST)) 11130 continue; 11131 11132 if (POINTER_TYPE_P (TREE_TYPE (name)) 11133 && ((vr_value[i]->type == VR_RANGE 11134 && range_includes_zero_p (vr_value[i]->min, 11135 vr_value[i]->max) == 0) 11136 || (vr_value[i]->type == VR_ANTI_RANGE 11137 && range_includes_zero_p (vr_value[i]->min, 11138 vr_value[i]->max) == 1))) 11139 set_ptr_nonnull (name); 11140 else if (!POINTER_TYPE_P (TREE_TYPE (name))) 11141 set_range_info (name, vr_value[i]->type, vr_value[i]->min, 11142 vr_value[i]->max); 11143 } 11144 11145 substitute_and_fold (op_with_constant_singleton_value_range, vrp_fold_stmt); 11146 11147 if (warn_array_bounds && warn_array_bounds_p) 11148 check_all_array_refs (); 11149 } 11150 11151 /* evrp_dom_walker visits the basic blocks in the dominance order and set 11152 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to 11153 discover more VRs. */ 11154 11155 class evrp_dom_walker : public dom_walker 11156 { 11157 public: 11158 evrp_dom_walker () 11159 : dom_walker (CDI_DOMINATORS), stack (10) 11160 { 11161 need_eh_cleanup = BITMAP_ALLOC (NULL); 11162 } 11163 ~evrp_dom_walker () 11164 { 11165 BITMAP_FREE (need_eh_cleanup); 11166 } 11167 virtual edge before_dom_children (basic_block); 11168 virtual void after_dom_children (basic_block); 11169 void push_value_range (tree var, value_range *vr); 11170 value_range *pop_value_range (tree var); 11171 value_range *try_find_new_range (tree op, tree_code code, tree limit); 11172 11173 /* Cond_stack holds the old VR. */ 11174 auto_vec<std::pair <tree, value_range*> > stack; 11175 bitmap need_eh_cleanup; 11176 auto_vec<gimple *> stmts_to_fixup; 11177 auto_vec<gimple *> stmts_to_remove; 11178 }; 11179 11180 /* Find new range for OP such that (OP CODE LIMIT) is true. */ 11181 11182 value_range * 11183 evrp_dom_walker::try_find_new_range (tree op, tree_code code, tree limit) 11184 { 11185 value_range vr = VR_INITIALIZER; 11186 value_range *old_vr = get_value_range (op); 11187 11188 /* Discover VR when condition is true. */ 11189 extract_range_for_var_from_comparison_expr (op, code, op, 11190 limit, &vr); 11191 if (old_vr->type == VR_RANGE || old_vr->type == VR_ANTI_RANGE) 11192 vrp_intersect_ranges (&vr, old_vr); 11193 /* If we found any usable VR, set the VR to ssa_name and create a 11194 PUSH old value in the stack with the old VR. */ 11195 if (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE) 11196 { 11197 if (old_vr->type == vr.type 11198 && vrp_operand_equal_p (old_vr->min, vr.min) 11199 && vrp_operand_equal_p (old_vr->max, vr.max)) 11200 return NULL; 11201 value_range *new_vr = vrp_value_range_pool.allocate (); 11202 *new_vr = vr; 11203 return new_vr; 11204 } 11205 return NULL; 11206 } 11207 11208 /* See if there is any new scope is entered with new VR and set that VR to 11209 ssa_name before visiting the statements in the scope. */ 11210 11211 edge 11212 evrp_dom_walker::before_dom_children (basic_block bb) 11213 { 11214 tree op0 = NULL_TREE; 11215 edge_iterator ei; 11216 edge e; 11217 11218 if (dump_file && (dump_flags & TDF_DETAILS)) 11219 fprintf (dump_file, "Visiting BB%d\n", bb->index); 11220 11221 stack.safe_push (std::make_pair (NULL_TREE, (value_range *)NULL)); 11222 11223 edge pred_e = NULL; 11224 FOR_EACH_EDGE (e, ei, bb->preds) 11225 { 11226 /* Ignore simple backedges from this to allow recording conditions 11227 in loop headers. */ 11228 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest)) 11229 continue; 11230 if (! pred_e) 11231 pred_e = e; 11232 else 11233 { 11234 pred_e = NULL; 11235 break; 11236 } 11237 } 11238 if (pred_e) 11239 { 11240 gimple *stmt = last_stmt (pred_e->src); 11241 if (stmt 11242 && gimple_code (stmt) == GIMPLE_COND 11243 && (op0 = gimple_cond_lhs (stmt)) 11244 && TREE_CODE (op0) == SSA_NAME 11245 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt))) 11246 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt))))) 11247 { 11248 if (dump_file && (dump_flags & TDF_DETAILS)) 11249 { 11250 fprintf (dump_file, "Visiting controlling predicate "); 11251 print_gimple_stmt (dump_file, stmt, 0, 0); 11252 } 11253 /* Entering a new scope. Try to see if we can find a VR 11254 here. */ 11255 tree op1 = gimple_cond_rhs (stmt); 11256 tree_code code = gimple_cond_code (stmt); 11257 11258 if (TREE_OVERFLOW_P (op1)) 11259 op1 = drop_tree_overflow (op1); 11260 11261 /* If condition is false, invert the cond. */ 11262 if (pred_e->flags & EDGE_FALSE_VALUE) 11263 code = invert_tree_comparison (gimple_cond_code (stmt), 11264 HONOR_NANS (op0)); 11265 /* Add VR when (OP0 CODE OP1) condition is true. */ 11266 value_range *op0_range = try_find_new_range (op0, code, op1); 11267 11268 /* Register ranges for y in x < y where 11269 y might have ranges that are useful. */ 11270 tree limit; 11271 tree_code new_code; 11272 if (TREE_CODE (op1) == SSA_NAME 11273 && extract_code_and_val_from_cond_with_ops (op1, code, 11274 op0, op1, 11275 false, 11276 &new_code, &limit)) 11277 { 11278 /* Add VR when (OP1 NEW_CODE LIMIT) condition is true. */ 11279 value_range *op1_range = try_find_new_range (op1, new_code, limit); 11280 if (op1_range) 11281 push_value_range (op1, op1_range); 11282 } 11283 11284 if (op0_range) 11285 push_value_range (op0, op0_range); 11286 } 11287 } 11288 11289 /* Visit PHI stmts and discover any new VRs possible. */ 11290 bool has_unvisited_preds = false; 11291 FOR_EACH_EDGE (e, ei, bb->preds) 11292 if (e->flags & EDGE_EXECUTABLE 11293 && !(e->src->flags & BB_VISITED)) 11294 { 11295 has_unvisited_preds = true; 11296 break; 11297 } 11298 11299 for (gphi_iterator gpi = gsi_start_phis (bb); 11300 !gsi_end_p (gpi); gsi_next (&gpi)) 11301 { 11302 gphi *phi = gpi.phi (); 11303 tree lhs = PHI_RESULT (phi); 11304 if (virtual_operand_p (lhs)) 11305 continue; 11306 value_range vr_result = VR_INITIALIZER; 11307 bool interesting = stmt_interesting_for_vrp (phi); 11308 if (interesting && dump_file && (dump_flags & TDF_DETAILS)) 11309 { 11310 fprintf (dump_file, "Visiting PHI node "); 11311 print_gimple_stmt (dump_file, phi, 0, 0); 11312 } 11313 if (!has_unvisited_preds 11314 && interesting) 11315 extract_range_from_phi_node (phi, &vr_result); 11316 else 11317 { 11318 set_value_range_to_varying (&vr_result); 11319 /* When we have an unvisited executable predecessor we can't 11320 use PHI arg ranges which may be still UNDEFINED but have 11321 to use VARYING for them. But we can still resort to 11322 SCEV for loop header PHIs. */ 11323 struct loop *l; 11324 if (interesting 11325 && (l = loop_containing_stmt (phi)) 11326 && l->header == gimple_bb (phi)) 11327 adjust_range_with_scev (&vr_result, l, phi, lhs); 11328 } 11329 update_value_range (lhs, &vr_result); 11330 11331 /* Mark PHIs whose lhs we fully propagate for removal. */ 11332 tree val = op_with_constant_singleton_value_range (lhs); 11333 if (val && may_propagate_copy (lhs, val)) 11334 { 11335 stmts_to_remove.safe_push (phi); 11336 continue; 11337 } 11338 11339 /* Set the SSA with the value range. */ 11340 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))) 11341 { 11342 if ((vr_result.type == VR_RANGE 11343 || vr_result.type == VR_ANTI_RANGE) 11344 && (TREE_CODE (vr_result.min) == INTEGER_CST) 11345 && (TREE_CODE (vr_result.max) == INTEGER_CST)) 11346 set_range_info (lhs, 11347 vr_result.type, vr_result.min, vr_result.max); 11348 } 11349 else if (POINTER_TYPE_P (TREE_TYPE (lhs)) 11350 && ((vr_result.type == VR_RANGE 11351 && range_includes_zero_p (vr_result.min, 11352 vr_result.max) == 0) 11353 || (vr_result.type == VR_ANTI_RANGE 11354 && range_includes_zero_p (vr_result.min, 11355 vr_result.max) == 1))) 11356 set_ptr_nonnull (lhs); 11357 } 11358 11359 edge taken_edge = NULL; 11360 11361 /* Visit all other stmts and discover any new VRs possible. */ 11362 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); 11363 !gsi_end_p (gsi); gsi_next (&gsi)) 11364 { 11365 gimple *stmt = gsi_stmt (gsi); 11366 tree output = NULL_TREE; 11367 gimple *old_stmt = stmt; 11368 bool was_noreturn = (is_gimple_call (stmt) 11369 && gimple_call_noreturn_p (stmt)); 11370 11371 if (dump_file && (dump_flags & TDF_DETAILS)) 11372 { 11373 fprintf (dump_file, "Visiting stmt "); 11374 print_gimple_stmt (dump_file, stmt, 0, 0); 11375 } 11376 11377 if (gcond *cond = dyn_cast <gcond *> (stmt)) 11378 { 11379 vrp_visit_cond_stmt (cond, &taken_edge); 11380 if (taken_edge) 11381 { 11382 if (taken_edge->flags & EDGE_TRUE_VALUE) 11383 gimple_cond_make_true (cond); 11384 else if (taken_edge->flags & EDGE_FALSE_VALUE) 11385 gimple_cond_make_false (cond); 11386 else 11387 gcc_unreachable (); 11388 update_stmt (stmt); 11389 } 11390 } 11391 else if (stmt_interesting_for_vrp (stmt)) 11392 { 11393 edge taken_edge; 11394 value_range vr = VR_INITIALIZER; 11395 extract_range_from_stmt (stmt, &taken_edge, &output, &vr); 11396 if (output 11397 && (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE)) 11398 { 11399 update_value_range (output, &vr); 11400 vr = *get_value_range (output); 11401 11402 /* Mark stmts whose output we fully propagate for removal. */ 11403 tree val; 11404 if ((val = op_with_constant_singleton_value_range (output)) 11405 && may_propagate_copy (output, val) 11406 && !stmt_could_throw_p (stmt) 11407 && !gimple_has_side_effects (stmt)) 11408 { 11409 stmts_to_remove.safe_push (stmt); 11410 continue; 11411 } 11412 11413 /* Set the SSA with the value range. */ 11414 if (INTEGRAL_TYPE_P (TREE_TYPE (output))) 11415 { 11416 if ((vr.type == VR_RANGE 11417 || vr.type == VR_ANTI_RANGE) 11418 && (TREE_CODE (vr.min) == INTEGER_CST) 11419 && (TREE_CODE (vr.max) == INTEGER_CST)) 11420 set_range_info (output, vr.type, vr.min, vr.max); 11421 } 11422 else if (POINTER_TYPE_P (TREE_TYPE (output)) 11423 && ((vr.type == VR_RANGE 11424 && range_includes_zero_p (vr.min, 11425 vr.max) == 0) 11426 || (vr.type == VR_ANTI_RANGE 11427 && range_includes_zero_p (vr.min, 11428 vr.max) == 1))) 11429 set_ptr_nonnull (output); 11430 } 11431 else 11432 set_defs_to_varying (stmt); 11433 } 11434 else 11435 set_defs_to_varying (stmt); 11436 11437 /* See if we can derive a range for any of STMT's operands. */ 11438 tree op; 11439 ssa_op_iter i; 11440 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE) 11441 { 11442 tree value; 11443 enum tree_code comp_code; 11444 11445 /* If OP is used in such a way that we can infer a value 11446 range for it, and we don't find a previous assertion for 11447 it, create a new assertion location node for OP. */ 11448 if (infer_value_range (stmt, op, &comp_code, &value)) 11449 { 11450 /* If we are able to infer a nonzero value range for OP, 11451 then walk backwards through the use-def chain to see if OP 11452 was set via a typecast. 11453 If so, then we can also infer a nonzero value range 11454 for the operand of the NOP_EXPR. */ 11455 if (comp_code == NE_EXPR && integer_zerop (value)) 11456 { 11457 tree t = op; 11458 gimple *def_stmt = SSA_NAME_DEF_STMT (t); 11459 while (is_gimple_assign (def_stmt) 11460 && CONVERT_EXPR_CODE_P 11461 (gimple_assign_rhs_code (def_stmt)) 11462 && TREE_CODE 11463 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME 11464 && POINTER_TYPE_P 11465 (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))) 11466 { 11467 t = gimple_assign_rhs1 (def_stmt); 11468 def_stmt = SSA_NAME_DEF_STMT (t); 11469 11470 /* Add VR when (T COMP_CODE value) condition is 11471 true. */ 11472 value_range *op_range 11473 = try_find_new_range (t, comp_code, value); 11474 if (op_range) 11475 push_value_range (t, op_range); 11476 } 11477 } 11478 /* Add VR when (OP COMP_CODE value) condition is true. */ 11479 value_range *op_range = try_find_new_range (op, 11480 comp_code, value); 11481 if (op_range) 11482 push_value_range (op, op_range); 11483 } 11484 } 11485 11486 /* Try folding stmts with the VR discovered. */ 11487 bool did_replace 11488 = replace_uses_in (stmt, op_with_constant_singleton_value_range); 11489 if (fold_stmt (&gsi, follow_single_use_edges) 11490 || did_replace) 11491 { 11492 stmt = gsi_stmt (gsi); 11493 update_stmt (stmt); 11494 did_replace = true; 11495 } 11496 11497 if (did_replace) 11498 { 11499 /* If we cleaned up EH information from the statement, 11500 remove EH edges. */ 11501 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)) 11502 bitmap_set_bit (need_eh_cleanup, bb->index); 11503 11504 /* If we turned a not noreturn call into a noreturn one 11505 schedule it for fixup. */ 11506 if (!was_noreturn 11507 && is_gimple_call (stmt) 11508 && gimple_call_noreturn_p (stmt)) 11509 stmts_to_fixup.safe_push (stmt); 11510 11511 if (gimple_assign_single_p (stmt)) 11512 { 11513 tree rhs = gimple_assign_rhs1 (stmt); 11514 if (TREE_CODE (rhs) == ADDR_EXPR) 11515 recompute_tree_invariant_for_addr_expr (rhs); 11516 } 11517 } 11518 } 11519 11520 /* Visit BB successor PHI nodes and replace PHI args. */ 11521 FOR_EACH_EDGE (e, ei, bb->succs) 11522 { 11523 for (gphi_iterator gpi = gsi_start_phis (e->dest); 11524 !gsi_end_p (gpi); gsi_next (&gpi)) 11525 { 11526 gphi *phi = gpi.phi (); 11527 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e); 11528 tree arg = USE_FROM_PTR (use_p); 11529 if (TREE_CODE (arg) != SSA_NAME 11530 || virtual_operand_p (arg)) 11531 continue; 11532 tree val = op_with_constant_singleton_value_range (arg); 11533 if (val && may_propagate_copy (arg, val)) 11534 propagate_value (use_p, val); 11535 } 11536 } 11537 11538 bb->flags |= BB_VISITED; 11539 11540 return taken_edge; 11541 } 11542 11543 /* Restore/pop VRs valid only for BB when we leave BB. */ 11544 11545 void 11546 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED) 11547 { 11548 gcc_checking_assert (!stack.is_empty ()); 11549 while (stack.last ().first != NULL_TREE) 11550 pop_value_range (stack.last ().first); 11551 stack.pop (); 11552 } 11553 11554 /* Push the Value Range of VAR to the stack and update it with new VR. */ 11555 11556 void 11557 evrp_dom_walker::push_value_range (tree var, value_range *vr) 11558 { 11559 if (SSA_NAME_VERSION (var) >= num_vr_values) 11560 return; 11561 if (dump_file && (dump_flags & TDF_DETAILS)) 11562 { 11563 fprintf (dump_file, "pushing new range for "); 11564 print_generic_expr (dump_file, var, 0); 11565 fprintf (dump_file, ": "); 11566 dump_value_range (dump_file, vr); 11567 fprintf (dump_file, "\n"); 11568 } 11569 stack.safe_push (std::make_pair (var, get_value_range (var))); 11570 vr_value[SSA_NAME_VERSION (var)] = vr; 11571 } 11572 11573 /* Pop the Value Range from the vrp_stack and update VAR with it. */ 11574 11575 value_range * 11576 evrp_dom_walker::pop_value_range (tree var) 11577 { 11578 value_range *vr = stack.last ().second; 11579 gcc_checking_assert (var == stack.last ().first); 11580 if (dump_file && (dump_flags & TDF_DETAILS)) 11581 { 11582 fprintf (dump_file, "popping range for "); 11583 print_generic_expr (dump_file, var, 0); 11584 fprintf (dump_file, ", restoring "); 11585 dump_value_range (dump_file, vr); 11586 fprintf (dump_file, "\n"); 11587 } 11588 vr_value[SSA_NAME_VERSION (var)] = vr; 11589 stack.pop (); 11590 return vr; 11591 } 11592 11593 11594 /* Main entry point for the early vrp pass which is a simplified non-iterative 11595 version of vrp where basic blocks are visited in dominance order. Value 11596 ranges discovered in early vrp will also be used by ipa-vrp. */ 11597 11598 static unsigned int 11599 execute_early_vrp () 11600 { 11601 edge e; 11602 edge_iterator ei; 11603 basic_block bb; 11604 11605 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS); 11606 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); 11607 scev_initialize (); 11608 calculate_dominance_info (CDI_DOMINATORS); 11609 FOR_EACH_BB_FN (bb, cfun) 11610 { 11611 bb->flags &= ~BB_VISITED; 11612 FOR_EACH_EDGE (e, ei, bb->preds) 11613 e->flags |= EDGE_EXECUTABLE; 11614 } 11615 vrp_initialize_lattice (); 11616 11617 /* Walk stmts in dominance order and propagate VRP. */ 11618 evrp_dom_walker walker; 11619 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun)); 11620 11621 if (dump_file) 11622 { 11623 fprintf (dump_file, "\nValue ranges after Early VRP:\n\n"); 11624 dump_all_value_ranges (dump_file); 11625 fprintf (dump_file, "\n"); 11626 } 11627 11628 /* Remove stmts in reverse order to make debug stmt creation possible. */ 11629 while (! walker.stmts_to_remove.is_empty ()) 11630 { 11631 gimple *stmt = walker.stmts_to_remove.pop (); 11632 if (dump_file && dump_flags & TDF_DETAILS) 11633 { 11634 fprintf (dump_file, "Removing dead stmt "); 11635 print_gimple_stmt (dump_file, stmt, 0, 0); 11636 fprintf (dump_file, "\n"); 11637 } 11638 gimple_stmt_iterator gsi = gsi_for_stmt (stmt); 11639 if (gimple_code (stmt) == GIMPLE_PHI) 11640 remove_phi_node (&gsi, true); 11641 else 11642 { 11643 unlink_stmt_vdef (stmt); 11644 gsi_remove (&gsi, true); 11645 release_defs (stmt); 11646 } 11647 } 11648 11649 if (!bitmap_empty_p (walker.need_eh_cleanup)) 11650 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup); 11651 11652 /* Fixup stmts that became noreturn calls. This may require splitting 11653 blocks and thus isn't possible during the dominator walk. Do this 11654 in reverse order so we don't inadvertedly remove a stmt we want to 11655 fixup by visiting a dominating now noreturn call first. */ 11656 while (!walker.stmts_to_fixup.is_empty ()) 11657 { 11658 gimple *stmt = walker.stmts_to_fixup.pop (); 11659 fixup_noreturn_call (stmt); 11660 } 11661 11662 vrp_free_lattice (); 11663 scev_finalize (); 11664 loop_optimizer_finalize (); 11665 return 0; 11666 } 11667 11668 11669 /* Main entry point to VRP (Value Range Propagation). This pass is 11670 loosely based on J. R. C. Patterson, ``Accurate Static Branch 11671 Prediction by Value Range Propagation,'' in SIGPLAN Conference on 11672 Programming Language Design and Implementation, pp. 67-78, 1995. 11673 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html 11674 11675 This is essentially an SSA-CCP pass modified to deal with ranges 11676 instead of constants. 11677 11678 While propagating ranges, we may find that two or more SSA name 11679 have equivalent, though distinct ranges. For instance, 11680 11681 1 x_9 = p_3->a; 11682 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0> 11683 3 if (p_4 == q_2) 11684 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>; 11685 5 endif 11686 6 if (q_2) 11687 11688 In the code above, pointer p_5 has range [q_2, q_2], but from the 11689 code we can also determine that p_5 cannot be NULL and, if q_2 had 11690 a non-varying range, p_5's range should also be compatible with it. 11691 11692 These equivalences are created by two expressions: ASSERT_EXPR and 11693 copy operations. Since p_5 is an assertion on p_4, and p_4 was the 11694 result of another assertion, then we can use the fact that p_5 and 11695 p_4 are equivalent when evaluating p_5's range. 11696 11697 Together with value ranges, we also propagate these equivalences 11698 between names so that we can take advantage of information from 11699 multiple ranges when doing final replacement. Note that this 11700 equivalency relation is transitive but not symmetric. 11701 11702 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we 11703 cannot assert that q_2 is equivalent to p_5 because q_2 may be used 11704 in contexts where that assertion does not hold (e.g., in line 6). 11705 11706 TODO, the main difference between this pass and Patterson's is that 11707 we do not propagate edge probabilities. We only compute whether 11708 edges can be taken or not. That is, instead of having a spectrum 11709 of jump probabilities between 0 and 1, we only deal with 0, 1 and 11710 DON'T KNOW. In the future, it may be worthwhile to propagate 11711 probabilities to aid branch prediction. */ 11712 11713 static unsigned int 11714 execute_vrp (bool warn_array_bounds_p) 11715 { 11716 int i; 11717 edge e; 11718 switch_update *su; 11719 11720 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS); 11721 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); 11722 scev_initialize (); 11723 11724 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation. 11725 Inserting assertions may split edges which will invalidate 11726 EDGE_DFS_BACK. */ 11727 insert_range_assertions (); 11728 11729 to_remove_edges.create (10); 11730 to_update_switch_stmts.create (5); 11731 threadedge_initialize_values (); 11732 11733 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */ 11734 mark_dfs_back_edges (); 11735 11736 vrp_initialize_lattice (); 11737 vrp_initialize (); 11738 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node); 11739 vrp_finalize (warn_array_bounds_p); 11740 11741 /* We must identify jump threading opportunities before we release 11742 the datastructures built by VRP. */ 11743 identify_jump_threads (); 11744 11745 vrp_free_lattice (); 11746 11747 free_numbers_of_iterations_estimates (cfun); 11748 11749 /* ASSERT_EXPRs must be removed before finalizing jump threads 11750 as finalizing jump threads calls the CFG cleanup code which 11751 does not properly handle ASSERT_EXPRs. */ 11752 remove_range_assertions (); 11753 11754 /* If we exposed any new variables, go ahead and put them into 11755 SSA form now, before we handle jump threading. This simplifies 11756 interactions between rewriting of _DECL nodes into SSA form 11757 and rewriting SSA_NAME nodes into SSA form after block 11758 duplication and CFG manipulation. */ 11759 update_ssa (TODO_update_ssa); 11760 11761 /* We identified all the jump threading opportunities earlier, but could 11762 not transform the CFG at that time. This routine transforms the 11763 CFG and arranges for the dominator tree to be rebuilt if necessary. 11764 11765 Note the SSA graph update will occur during the normal TODO 11766 processing by the pass manager. */ 11767 thread_through_all_blocks (false); 11768 11769 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the 11770 CFG in a broken state and requires a cfg_cleanup run. */ 11771 FOR_EACH_VEC_ELT (to_remove_edges, i, e) 11772 remove_edge (e); 11773 /* Update SWITCH_EXPR case label vector. */ 11774 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su) 11775 { 11776 size_t j; 11777 size_t n = TREE_VEC_LENGTH (su->vec); 11778 tree label; 11779 gimple_switch_set_num_labels (su->stmt, n); 11780 for (j = 0; j < n; j++) 11781 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j)); 11782 /* As we may have replaced the default label with a regular one 11783 make sure to make it a real default label again. This ensures 11784 optimal expansion. */ 11785 label = gimple_switch_label (su->stmt, 0); 11786 CASE_LOW (label) = NULL_TREE; 11787 CASE_HIGH (label) = NULL_TREE; 11788 } 11789 11790 if (to_remove_edges.length () > 0) 11791 { 11792 free_dominance_info (CDI_DOMINATORS); 11793 loops_state_set (LOOPS_NEED_FIXUP); 11794 } 11795 11796 to_remove_edges.release (); 11797 to_update_switch_stmts.release (); 11798 threadedge_finalize_values (); 11799 11800 scev_finalize (); 11801 loop_optimizer_finalize (); 11802 return 0; 11803 } 11804 11805 namespace { 11806 11807 const pass_data pass_data_vrp = 11808 { 11809 GIMPLE_PASS, /* type */ 11810 "vrp", /* name */ 11811 OPTGROUP_NONE, /* optinfo_flags */ 11812 TV_TREE_VRP, /* tv_id */ 11813 PROP_ssa, /* properties_required */ 11814 0, /* properties_provided */ 11815 0, /* properties_destroyed */ 11816 0, /* todo_flags_start */ 11817 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */ 11818 }; 11819 11820 class pass_vrp : public gimple_opt_pass 11821 { 11822 public: 11823 pass_vrp (gcc::context *ctxt) 11824 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false) 11825 {} 11826 11827 /* opt_pass methods: */ 11828 opt_pass * clone () { return new pass_vrp (m_ctxt); } 11829 void set_pass_param (unsigned int n, bool param) 11830 { 11831 gcc_assert (n == 0); 11832 warn_array_bounds_p = param; 11833 } 11834 virtual bool gate (function *) { return flag_tree_vrp != 0; } 11835 virtual unsigned int execute (function *) 11836 { return execute_vrp (warn_array_bounds_p); } 11837 11838 private: 11839 bool warn_array_bounds_p; 11840 }; // class pass_vrp 11841 11842 } // anon namespace 11843 11844 gimple_opt_pass * 11845 make_pass_vrp (gcc::context *ctxt) 11846 { 11847 return new pass_vrp (ctxt); 11848 } 11849 11850 namespace { 11851 11852 const pass_data pass_data_early_vrp = 11853 { 11854 GIMPLE_PASS, /* type */ 11855 "evrp", /* name */ 11856 OPTGROUP_NONE, /* optinfo_flags */ 11857 TV_TREE_EARLY_VRP, /* tv_id */ 11858 PROP_ssa, /* properties_required */ 11859 0, /* properties_provided */ 11860 0, /* properties_destroyed */ 11861 0, /* todo_flags_start */ 11862 ( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ), 11863 }; 11864 11865 class pass_early_vrp : public gimple_opt_pass 11866 { 11867 public: 11868 pass_early_vrp (gcc::context *ctxt) 11869 : gimple_opt_pass (pass_data_early_vrp, ctxt) 11870 {} 11871 11872 /* opt_pass methods: */ 11873 opt_pass * clone () { return new pass_early_vrp (m_ctxt); } 11874 virtual bool gate (function *) 11875 { 11876 return flag_tree_vrp != 0; 11877 } 11878 virtual unsigned int execute (function *) 11879 { return execute_early_vrp (); } 11880 11881 }; // class pass_vrp 11882 } // anon namespace 11883 11884 gimple_opt_pass * 11885 make_pass_early_vrp (gcc::context *ctxt) 11886 { 11887 return new pass_early_vrp (ctxt); 11888 } 11889 11890