Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Side by Side Diff: runtime/vm/intermediate_language.cc

Issue 328503003: Extend Range analysis to 64-bit range and mint operations (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/intermediate_language.h" 5 #include "vm/intermediate_language.h"
6 6
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/bit_vector.h" 8 #include "vm/bit_vector.h"
9 #include "vm/cpu.h" 9 #include "vm/cpu.h"
10 #include "vm/dart_entry.h" 10 #include "vm/dart_entry.h"
(...skipping 2431 matching lines...) Expand 10 before | Expand all | Expand 10 after
2442 intptr_t use_index = instr->env()->Length(); // Start index after inner. 2442 intptr_t use_index = instr->env()->Length(); // Start index after inner.
2443 for (Environment::DeepIterator it(copy); !it.Done(); it.Advance()) { 2443 for (Environment::DeepIterator it(copy); !it.Done(); it.Advance()) {
2444 Value* value = it.CurrentValue(); 2444 Value* value = it.CurrentValue();
2445 value->set_instruction(instr); 2445 value->set_instruction(instr);
2446 value->set_use_index(use_index++); 2446 value->set_use_index(use_index++);
2447 value->definition()->AddEnvUse(value); 2447 value->definition()->AddEnvUse(value);
2448 } 2448 }
2449 } 2449 }
2450 2450
2451 2451
2452 RangeBoundary RangeBoundary::FromDefinition(Definition* defn, intptr_t offs) { 2452 RangeBoundary RangeBoundary::FromDefinition(Definition* defn, int64_t offs) {
2453 if (defn->IsConstant() && defn->AsConstant()->value().IsSmi()) { 2453 if (defn->IsConstant() && defn->AsConstant()->value().IsSmi()) {
2454 return FromConstant(Smi::Cast(defn->AsConstant()->value()).Value() + offs); 2454 return FromConstant(Smi::Cast(defn->AsConstant()->value()).Value() + offs);
2455 } 2455 }
2456 return RangeBoundary(kSymbol, reinterpret_cast<intptr_t>(defn), offs); 2456 return RangeBoundary(kSymbol, reinterpret_cast<intptr_t>(defn), offs);
2457 } 2457 }
2458 2458
2459 2459
2460 RangeBoundary RangeBoundary::LowerBound() const { 2460 RangeBoundary RangeBoundary::LowerBound() const {
2461 if (IsNegativeInfinity()) return *this; 2461 if (IsInfinity()) {
2462 return NegativeInfinity();
2463 }
2462 if (IsConstant()) return *this; 2464 if (IsConstant()) return *this;
2463 return Add(Range::ConstantMin(symbol()->range()), 2465 return Add(Range::ConstantMin(symbol()->range()),
2464 RangeBoundary::FromConstant(offset_), 2466 RangeBoundary::FromConstant(offset_),
2465 NegativeInfinity()); 2467 NegativeInfinity());
2466 } 2468 }
2467 2469
2468 2470
2469 RangeBoundary RangeBoundary::UpperBound() const { 2471 RangeBoundary RangeBoundary::UpperBound() const {
2470 if (IsPositiveInfinity()) return *this; 2472 if (IsInfinity()) {
2473 return PositiveInfinity();
2474 }
2471 if (IsConstant()) return *this; 2475 if (IsConstant()) return *this;
2472 return Add(Range::ConstantMax(symbol()->range()), 2476 return Add(Range::ConstantMax(symbol()->range()),
2473 RangeBoundary::FromConstant(offset_), 2477 RangeBoundary::FromConstant(offset_),
2474 PositiveInfinity()); 2478 PositiveInfinity());
2475 } 2479 }
2476 2480
2477 2481
2482 RangeBoundary RangeBoundary::Add(const RangeBoundary& a,
2483 const RangeBoundary& b,
2484 const RangeBoundary& overflow) {
2485 if (a.IsInfinity() || b.IsInfinity()) {
2486 // In that case that a or b is +/- inf, return the overflow boundary.
2487 return overflow;
2488 }
2489 ASSERT(a.IsConstant() && b.IsConstant());
2490
2491 int64_t result = a.ConstantValue() + b.ConstantValue();
2492
2493 if (Utils::DidAddOverflow(a.ConstantValue(), b.ConstantValue(), result)) {
2494 return overflow;
2495 }
2496
2497 return RangeBoundary::FromConstant(result);
2498 }
2499
2500
2501 RangeBoundary RangeBoundary::Sub(const RangeBoundary& a,
2502 const RangeBoundary& b,
2503 const RangeBoundary& overflow) {
2504 if (a.IsInfinity() || b.IsInfinity()) {
2505 // In that case that a or b is +/- inf, return the overflow boundary.
2506 return overflow;
2507 }
2508 ASSERT(a.IsConstant() && b.IsConstant());
2509
2510 int64_t result = a.ConstantValue() - b.ConstantValue();
2511
2512 if (Utils::DidSubOverflow(a.ConstantValue(), b.ConstantValue(), result)) {
2513 return overflow;
2514 }
2515 return RangeBoundary::FromConstant(result);
2516 }
2517
2518
2519 bool RangeBoundary::SymbolicAdd(const RangeBoundary& a,
2520 const RangeBoundary& b,
2521 RangeBoundary* result) {
2522 if (a.IsSymbol() && b.IsConstant()) {
2523 const int64_t offset = a.offset() + b.ConstantValue();
2524 if (Utils::DidAddOverflow(a.offset(), b.ConstantValue(), offset)) {
2525 return false;
2526 }
2527
2528 *result = RangeBoundary::FromDefinition(a.symbol(), offset);
2529 return true;
2530 } else if (b.IsSymbol() && a.IsConstant()) {
2531 return SymbolicAdd(b, a, result);
2532 }
2533 return false;
2534 }
2535
2536
2537 bool RangeBoundary::SymbolicSub(const RangeBoundary& a,
2538 const RangeBoundary& b,
2539 RangeBoundary* result) {
2540 if (a.IsSymbol() && b.IsConstant()) {
2541 const int64_t offset = a.offset() - b.ConstantValue();
2542 if (Utils::DidSubOverflow(a.offset(), b.ConstantValue(), offset)) {
2543 return false;
2544 }
2545
2546 *result = RangeBoundary::FromDefinition(a.symbol(), offset);
2547 return true;
2548 }
2549 return false;
2550 }
2551
2552
2478 static Definition* UnwrapConstraint(Definition* defn) { 2553 static Definition* UnwrapConstraint(Definition* defn) {
2479 while (defn->IsConstraint()) { 2554 while (defn->IsConstraint()) {
2480 defn = defn->AsConstraint()->value()->definition(); 2555 defn = defn->AsConstraint()->value()->definition();
2481 } 2556 }
2482 return defn; 2557 return defn;
2483 } 2558 }
2484 2559
2485 2560
2486 static bool AreEqualDefinitions(Definition* a, Definition* b) { 2561 static bool AreEqualDefinitions(Definition* a, Definition* b) {
2487 a = UnwrapConstraint(a); 2562 a = UnwrapConstraint(a);
2488 b = UnwrapConstraint(b); 2563 b = UnwrapConstraint(b);
2489 return (a == b) || 2564 return (a == b) ||
2490 (a->AllowsCSE() && 2565 (a->AllowsCSE() &&
2491 a->Dependencies().IsNone() && 2566 a->Dependencies().IsNone() &&
2492 b->AllowsCSE() && 2567 b->AllowsCSE() &&
2493 b->Dependencies().IsNone() && 2568 b->Dependencies().IsNone() &&
2494 a->Equals(b)); 2569 a->Equals(b));
2495 } 2570 }
2496 2571
2497 2572
2498 // Returns true if two range boundaries refer to the same symbol. 2573 // Returns true if two range boundaries refer to the same symbol.
2499 static bool DependOnSameSymbol(const RangeBoundary& a, const RangeBoundary& b) { 2574 static bool DependOnSameSymbol(const RangeBoundary& a, const RangeBoundary& b) {
2500 return a.IsSymbol() && b.IsSymbol() && 2575 return a.IsSymbol() && b.IsSymbol() &&
2501 AreEqualDefinitions(a.symbol(), b.symbol()); 2576 AreEqualDefinitions(a.symbol(), b.symbol());
2502 } 2577 }
2503 2578
2504 2579
2505 // Returns true if range has a least specific minimum value. 2580 bool RangeBoundary::Equals(const RangeBoundary& other) const {
2506 static bool IsMinSmi(Range* range) { 2581 if (IsConstant() && other.IsConstant()) {
2507 return (range == NULL) || 2582 return ConstantValue() == other.ConstantValue();
2508 (range->min().IsConstant() && 2583 } else if (IsInfinity() && other.IsInfinity()) {
2509 (range->min().value() <= Smi::kMinValue)); 2584 return kind() == other.kind();
2585 } else if (IsSymbol() && other.IsSymbol()) {
2586 return (offset() == other.offset()) && DependOnSameSymbol(*this, other);
2587 }
2588 return false;
2510 } 2589 }
2511 2590
2512 2591
2513 // Returns true if range has a least specific maximium value. 2592 RangeBoundary RangeBoundary::Shl(const RangeBoundary& value_boundary,
2514 static bool IsMaxSmi(Range* range) { 2593 int64_t shift_count,
2515 return (range == NULL) || 2594 const RangeBoundary& overflow) {
2516 (range->max().IsConstant() && 2595 ASSERT(value_boundary.IsConstant());
2517 (range->max().value() >= Smi::kMaxValue)); 2596 ASSERT(shift_count >= 0);
2518 } 2597 int64_t limit = 64 - shift_count;
2598 int64_t value = static_cast<int64_t>(value_boundary.ConstantValue());
2519 2599
2600 if ((value == 0) ||
2601 (shift_count == 0) ||
2602 ((limit > 0) && (Utils::IsInt(limit, value)))) {
2603 // Result stays in 64 bit range.
2604 int64_t result = value << shift_count;
2605 return Smi::IsValid64(result) ? RangeBoundary(result) : overflow;
2606 }
2520 2607
2521 // Returns true if two range boundaries can be proven to be equal. 2608 return overflow;
2522 static bool IsEqual(const RangeBoundary& a, const RangeBoundary& b) {
2523 if (a.IsConstant() && b.IsConstant()) {
2524 return a.value() == b.value();
2525 } else if (a.IsSymbol() && b.IsSymbol()) {
2526 return (a.offset() == b.offset()) && DependOnSameSymbol(a, b);
2527 } else {
2528 return false;
2529 }
2530 } 2609 }
2531 2610
2532 2611
2533 static RangeBoundary CanonicalizeBoundary(const RangeBoundary& a, 2612 static RangeBoundary CanonicalizeBoundary(const RangeBoundary& a,
2534 const RangeBoundary& overflow) { 2613 const RangeBoundary& overflow) {
2535 if (a.IsConstant() || a.IsNegativeInfinity() || a.IsPositiveInfinity()) { 2614 if (a.IsConstant() || a.IsInfinity()) {
2536 return a; 2615 return a;
2537 } 2616 }
2538 2617
2539 intptr_t offset = a.offset(); 2618 int64_t offset = a.offset();
2540 Definition* symbol = a.symbol(); 2619 Definition* symbol = a.symbol();
2541 2620
2542 bool changed; 2621 bool changed;
2543 do { 2622 do {
2544 changed = false; 2623 changed = false;
2545 if (symbol->IsConstraint()) { 2624 if (symbol->IsConstraint()) {
2546 symbol = symbol->AsConstraint()->value()->definition(); 2625 symbol = symbol->AsConstraint()->value()->definition();
2547 changed = true; 2626 changed = true;
2548 } else if (symbol->IsBinarySmiOp()) { 2627 } else if (symbol->IsBinarySmiOp()) {
2549 BinarySmiOpInstr* op = symbol->AsBinarySmiOp(); 2628 BinarySmiOpInstr* op = symbol->AsBinarySmiOp();
2550 Definition* left = op->left()->definition(); 2629 Definition* left = op->left()->definition();
2551 Definition* right = op->right()->definition(); 2630 Definition* right = op->right()->definition();
2552 switch (op->op_kind()) { 2631 switch (op->op_kind()) {
2553 case Token::kADD: 2632 case Token::kADD:
2554 if (right->IsConstant()) { 2633 if (right->IsConstant()) {
2555 offset += Smi::Cast(right->AsConstant()->value()).Value(); 2634 int64_t rhs = Smi::Cast(right->AsConstant()->value()).Value();
2635 int64_t old_offset = offset;
2636 offset += rhs;
2637 if (Utils::DidAddOverflow(old_offset, rhs, offset)) {
2638 return overflow;
2639 }
2556 symbol = left; 2640 symbol = left;
2557 changed = true; 2641 changed = true;
2558 } else if (left->IsConstant()) { 2642 } else if (left->IsConstant()) {
2559 offset += Smi::Cast(left->AsConstant()->value()).Value(); 2643 int64_t rhs = Smi::Cast(left->AsConstant()->value()).Value();
2644 int64_t old_offset = offset;
2645 offset += rhs;
2646 if (Utils::DidAddOverflow(old_offset, rhs, offset)) {
2647 return overflow;
2648 }
2560 symbol = right; 2649 symbol = right;
2561 changed = true; 2650 changed = true;
2562 } 2651 }
2563 break; 2652 break;
2564 2653
2565 case Token::kSUB: 2654 case Token::kSUB:
2566 if (right->IsConstant()) { 2655 if (right->IsConstant()) {
2567 offset -= Smi::Cast(right->AsConstant()->value()).Value(); 2656 int64_t rhs = Smi::Cast(right->AsConstant()->value()).Value();
2657 int64_t old_offset = offset;
2658 offset -= rhs;
2659 if (Utils::DidSubOverflow(old_offset, rhs, offset)) {
2660 return overflow;
2661 }
2568 symbol = left; 2662 symbol = left;
2569 changed = true; 2663 changed = true;
2570 } 2664 }
2571 break; 2665 break;
2572 2666
2573 default: 2667 default:
2574 break; 2668 break;
2575 } 2669 }
2576 } 2670 }
2577
2578 if (!Smi::IsValid(offset)) return overflow;
2579 } while (changed); 2671 } while (changed);
2580 2672
2581 return RangeBoundary::FromDefinition(symbol, offset); 2673 return RangeBoundary::FromDefinition(symbol, offset);
2582 } 2674 }
2583 2675
2584 2676
2585 static bool CanonicalizeMaxBoundary(RangeBoundary* a) { 2677 static bool CanonicalizeMaxBoundary(RangeBoundary* a) {
2586 if (!a->IsSymbol()) return false; 2678 if (!a->IsSymbol()) return false;
2587 2679
2588 Range* range = a->symbol()->range(); 2680 Range* range = a->symbol()->range();
2589 if ((range == NULL) || !range->max().IsSymbol()) return false; 2681 if ((range == NULL) || !range->max().IsSymbol()) return false;
2590 2682
2591 const intptr_t offset = range->max().offset() + a->offset(); 2683 const int64_t offset = range->max().offset() + a->offset();
2592 2684
2593 if (!Smi::IsValid(offset)) { 2685 if (Utils::DidAddOverflow(range->max().offset(), a->offset(), offset)) {
2594 *a = RangeBoundary::PositiveInfinity(); 2686 *a = RangeBoundary::PositiveInfinity();
2595 return true; 2687 return true;
2596 } 2688 }
2597 2689
2598 *a = CanonicalizeBoundary( 2690 *a = CanonicalizeBoundary(
2599 RangeBoundary::FromDefinition(range->max().symbol(), offset), 2691 RangeBoundary::FromDefinition(range->max().symbol(), offset),
2600 RangeBoundary::PositiveInfinity()); 2692 RangeBoundary::PositiveInfinity());
2601 2693
2602 return true; 2694 return true;
2603 } 2695 }
2604 2696
2605 2697
2606 static bool CanonicalizeMinBoundary(RangeBoundary* a) { 2698 static bool CanonicalizeMinBoundary(RangeBoundary* a) {
2607 if (!a->IsSymbol()) return false; 2699 if (!a->IsSymbol()) return false;
2608 2700
2609 Range* range = a->symbol()->range(); 2701 Range* range = a->symbol()->range();
2610 if ((range == NULL) || !range->min().IsSymbol()) return false; 2702 if ((range == NULL) || !range->min().IsSymbol()) return false;
2611 2703
2612 const intptr_t offset = range->min().offset() + a->offset(); 2704 const int64_t offset = range->min().offset() + a->offset();
2613 if (!Smi::IsValid(offset)) { 2705
2706 if (Utils::DidAddOverflow(range->min().offset(), a->offset(), offset)) {
2614 *a = RangeBoundary::NegativeInfinity(); 2707 *a = RangeBoundary::NegativeInfinity();
2615 return true; 2708 return true;
2616 } 2709 }
2617 2710
2618 *a = CanonicalizeBoundary( 2711 *a = CanonicalizeBoundary(
2619 RangeBoundary::FromDefinition(range->min().symbol(), offset), 2712 RangeBoundary::FromDefinition(range->min().symbol(), offset),
2620 RangeBoundary::NegativeInfinity()); 2713 RangeBoundary::NegativeInfinity());
2621 2714
2622 return true; 2715 return true;
2623 } 2716 }
2624 2717
2625 2718
2626 RangeBoundary RangeBoundary::Min(RangeBoundary a, RangeBoundary b) { 2719 RangeBoundary RangeBoundary::Min(RangeBoundary a, RangeBoundary b,
2720 RangeSize size) {
2721 ASSERT(!(a.IsNegativeInfinity() || b.IsNegativeInfinity()));
2722 ASSERT(!a.IsUnknown() || !b.IsUnknown());
2723 if (a.IsUnknown() && !b.IsUnknown()) {
2724 return b;
2725 }
2726 if (!a.IsUnknown() && b.IsUnknown()) {
2727 return a;
2728 }
2729 if (size == kRangeBoundarySmi) {
2730 if (a.IsSmiMaximumOrAbove() && !b.IsSmiMaximumOrAbove()) {
2731 return b;
2732 }
2733 if (!a.IsSmiMaximumOrAbove() && b.IsSmiMaximumOrAbove()) {
2734 return a;
2735 }
2736 } else {
2737 ASSERT(size == kRangeBoundaryInt64);
2738 if (a.IsMaximumOrAbove() && !b.IsMaximumOrAbove()) {
2739 return b;
2740 }
2741 if (!a.IsMaximumOrAbove() && b.IsMaximumOrAbove()) {
2742 return a;
2743 }
2744 }
2745
2746 if (a.Equals(b)) {
2747 return b;
2748 }
2749
2750 {
2751 RangeBoundary canonical_a =
2752 CanonicalizeBoundary(a, RangeBoundary::PositiveInfinity());
2753 RangeBoundary canonical_b =
2754 CanonicalizeBoundary(b, RangeBoundary::PositiveInfinity());
2755 do {
2756 if (DependOnSameSymbol(canonical_a, canonical_b)) {
2757 a = canonical_a;
2758 b = canonical_b;
2759 break;
2760 }
2761 } while (CanonicalizeMaxBoundary(&canonical_a) ||
2762 CanonicalizeMaxBoundary(&canonical_b));
2763 }
2764
2627 if (DependOnSameSymbol(a, b)) { 2765 if (DependOnSameSymbol(a, b)) {
2628 return (a.offset() <= b.offset()) ? a : b; 2766 return (a.offset() <= b.offset()) ? a : b;
2629 } 2767 }
2630 2768
2631 const intptr_t min_a = a.LowerBound().Clamp().value(); 2769 const int64_t min_a = a.UpperBound().Clamp(size).ConstantValue();
2632 const intptr_t min_b = b.LowerBound().Clamp().value(); 2770 const int64_t min_b = b.UpperBound().Clamp(size).ConstantValue();
2633 2771
2634 return RangeBoundary::FromConstant(Utils::Minimum(min_a, min_b)); 2772 return RangeBoundary::FromConstant(Utils::Minimum(min_a, min_b));
2635 } 2773 }
2636 2774
2637 2775
2638 RangeBoundary RangeBoundary::Max(RangeBoundary a, RangeBoundary b) { 2776 RangeBoundary RangeBoundary::Max(RangeBoundary a, RangeBoundary b,
2639 if (DependOnSameSymbol(a, b)) { 2777 RangeSize size) {
2640 return (a.offset() >= b.offset()) ? a : b; 2778 ASSERT(!(a.IsPositiveInfinity() || b.IsPositiveInfinity()));
2779 ASSERT(!a.IsUnknown() || !b.IsUnknown());
2780 if (a.IsUnknown() && !b.IsUnknown()) {
2781 return b;
2782 }
2783 if (!a.IsUnknown() && b.IsUnknown()) {
2784 return a;
2785 }
2786 if (size == kRangeBoundarySmi) {
2787 if (a.IsSmiMinimumOrBelow() && !b.IsSmiMinimumOrBelow()) {
2788 return b;
2789 }
2790 if (!a.IsSmiMinimumOrBelow() && b.IsSmiMinimumOrBelow()) {
2791 return a;
2792 }
2793 } else {
2794 ASSERT(size == kRangeBoundaryInt64);
2795 if (a.IsMinimumOrBelow() && !b.IsMinimumOrBelow()) {
2796 return b;
2797 }
2798 if (!a.IsMinimumOrBelow() && b.IsMinimumOrBelow()) {
2799 return a;
2800 }
2801 }
2802 if (a.Equals(b)) {
2803 return b;
2641 } 2804 }
2642 2805
2643 const intptr_t max_a = a.UpperBound().Clamp().value(); 2806 {
2644 const intptr_t max_b = b.UpperBound().Clamp().value(); 2807 RangeBoundary canonical_a =
2808 CanonicalizeBoundary(a, RangeBoundary::NegativeInfinity());
2809 RangeBoundary canonical_b =
2810 CanonicalizeBoundary(b, RangeBoundary::NegativeInfinity());
2811
2812 do {
2813 if (DependOnSameSymbol(canonical_a, canonical_b)) {
2814 a = canonical_a;
2815 b = canonical_b;
2816 break;
2817 }
2818 } while (CanonicalizeMinBoundary(&canonical_a) ||
2819 CanonicalizeMinBoundary(&canonical_b));
2820 }
2821
2822 if (DependOnSameSymbol(a, b)) {
2823 return (a.offset() <= b.offset()) ? b : a;
2824 }
2825
2826 const int64_t max_a = a.LowerBound().Clamp(size).ConstantValue();
2827 const int64_t max_b = b.LowerBound().Clamp(size).ConstantValue();
2645 2828
2646 return RangeBoundary::FromConstant(Utils::Maximum(max_a, max_b)); 2829 return RangeBoundary::FromConstant(Utils::Maximum(max_a, max_b));
2647 } 2830 }
2648 2831
2649 2832
2833 int64_t RangeBoundary::ConstantValue() const {
2834 ASSERT(IsConstant());
2835 return value_;
2836 }
2837
2838
2650 void Definition::InferRange() { 2839 void Definition::InferRange() {
2651 ASSERT(Type()->ToCid() == kSmiCid); // Has meaning only for smis. 2840 if (Type()->ToCid() == kSmiCid) {
2652 if (range_ == NULL) { 2841 if (range_ == NULL) {
2653 range_ = Range::Unknown(); 2842 range_ = Range::UnknownSmi();
2843 }
2844 } else if (IsMintDefinition()) {
2845 if (range_ == NULL) {
2846 range_ = Range::Unknown();
2847 }
2848 } else {
2849 // Only Smi and Mint supported.
2850 UNREACHABLE();
2654 } 2851 }
2655 } 2852 }
2656 2853
2657 2854
2658 void ConstantInstr::InferRange() { 2855 void ConstantInstr::InferRange() {
2659 ASSERT(value_.IsSmi()); 2856 if (value_.IsSmi()) {
2660 if (range_ == NULL) { 2857 if (range_ == NULL) {
2661 intptr_t value = Smi::Cast(value_).Value(); 2858 int64_t value = Smi::Cast(value_).Value();
2662 range_ = new Range(RangeBoundary::FromConstant(value), 2859 range_ = new Range(RangeBoundary::FromConstant(value),
2663 RangeBoundary::FromConstant(value)); 2860 RangeBoundary::FromConstant(value));
2861 }
2862 } else if (value_.IsMint()) {
2863 if (range_ == NULL) {
2864 int64_t value = Mint::Cast(value_).value();
2865 range_ = new Range(RangeBoundary::FromConstant(value),
2866 RangeBoundary::FromConstant(value));
2867 }
2868 } else {
2869 // Only Smi and Mint supported.
2870 UNREACHABLE();
2664 } 2871 }
2665 } 2872 }
2666 2873
2874
2875 void UnboxIntegerInstr::InferRange() {
2876 if (range_ == NULL) {
2877 Definition* unboxed = value()->definition();
2878 if (unboxed == NULL) {
2879 range_ = Range::Unknown();
2880 return;
2881 }
2882 Range* range = unboxed->range();
2883 if (range == NULL) {
2884 range_ = Range::Unknown();
2885 return;
2886 }
2887 range_ = new Range(range->min(), range->max());
2888 }
2889 }
2890
2667 2891
2668 void ConstraintInstr::InferRange() { 2892 void ConstraintInstr::InferRange() {
2669 Range* value_range = value()->definition()->range(); 2893 Range* value_range = value()->definition()->range();
2670 2894
2671 RangeBoundary min; 2895 RangeBoundary min;
2672 RangeBoundary max; 2896 RangeBoundary max;
2673 2897
2674 if (IsMinSmi(value_range) && !IsMinSmi(constraint())) { 2898 {
2675 min = constraint()->min(); 2899 RangeBoundary value_min = (value_range == NULL) ?
2676 } else if (IsMinSmi(constraint()) && !IsMinSmi(value_range)) { 2900 RangeBoundary() : value_range->min();
2677 min = value_range->min(); 2901 RangeBoundary constraint_min = constraint()->min();
2678 } else if ((value_range != NULL) && 2902 min = RangeBoundary::Max(value_min, constraint_min,
2679 IsEqual(constraint()->min(), value_range->min())) { 2903 RangeBoundary::kRangeBoundarySmi);
2680 min = constraint()->min();
2681 } else {
2682 if (value_range != NULL) {
2683 RangeBoundary canonical_a =
2684 CanonicalizeBoundary(constraint()->min(),
2685 RangeBoundary::NegativeInfinity());
2686 RangeBoundary canonical_b =
2687 CanonicalizeBoundary(value_range->min(),
2688 RangeBoundary::NegativeInfinity());
2689
2690 do {
2691 if (DependOnSameSymbol(canonical_a, canonical_b)) {
2692 min = (canonical_a.offset() <= canonical_b.offset()) ? canonical_b
2693 : canonical_a;
2694 }
2695 } while (CanonicalizeMinBoundary(&canonical_a) ||
2696 CanonicalizeMinBoundary(&canonical_b));
2697 }
2698
2699 if (min.IsUnknown()) {
2700 min = RangeBoundary::Max(Range::ConstantMin(value_range),
2701 Range::ConstantMin(constraint()));
2702 }
2703 } 2904 }
2704 2905
2705 if (IsMaxSmi(value_range) && !IsMaxSmi(constraint())) { 2906 ASSERT(!min.IsUnknown());
2706 max = constraint()->max();
2707 } else if (IsMaxSmi(constraint()) && !IsMaxSmi(value_range)) {
2708 max = value_range->max();
2709 } else if ((value_range != NULL) &&
2710 IsEqual(constraint()->max(), value_range->max())) {
2711 max = constraint()->max();
2712 } else {
2713 if (value_range != NULL) {
2714 RangeBoundary canonical_b =
2715 CanonicalizeBoundary(value_range->max(),
2716 RangeBoundary::PositiveInfinity());
2717 RangeBoundary canonical_a =
2718 CanonicalizeBoundary(constraint()->max(),
2719 RangeBoundary::PositiveInfinity());
2720 2907
2721 do { 2908 {
2722 if (DependOnSameSymbol(canonical_a, canonical_b)) { 2909 RangeBoundary value_max = (value_range == NULL) ?
2723 max = (canonical_a.offset() <= canonical_b.offset()) ? canonical_a 2910 RangeBoundary() : value_range->max();
2724 : canonical_b; 2911 RangeBoundary constraint_max = constraint()->max();
2725 break; 2912 max = RangeBoundary::Min(value_max, constraint_max,
2726 } 2913 RangeBoundary::kRangeBoundarySmi);
2727 } while (CanonicalizeMaxBoundary(&canonical_a) || 2914 }
2728 CanonicalizeMaxBoundary(&canonical_b));
2729 }
2730 2915
2731 if (max.IsUnknown()) { 2916 ASSERT(!max.IsUnknown());
2732 max = RangeBoundary::Min(Range::ConstantMax(value_range),
2733 Range::ConstantMax(constraint()));
2734 }
2735 }
2736 2917
2737 range_ = new Range(min, max); 2918 range_ = new Range(min, max);
2738 2919
2739 // Mark branches that generate unsatisfiable constraints as constant. 2920 // Mark branches that generate unsatisfiable constraints as constant.
2740 if (target() != NULL && range_->IsUnsatisfiable()) { 2921 if (target() != NULL && range_->IsUnsatisfiable()) {
2741 BranchInstr* branch = 2922 BranchInstr* branch =
2742 target()->PredecessorAt(0)->last_instruction()->AsBranch(); 2923 target()->PredecessorAt(0)->last_instruction()->AsBranch();
2743 if (target() == branch->true_successor()) { 2924 if (target() == branch->true_successor()) {
2744 // True unreachable. 2925 // True unreachable.
2745 if (FLAG_trace_constant_propagation) { 2926 if (FLAG_trace_constant_propagation) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
2798 RangeBoundary::FromConstant(255)); 2979 RangeBoundary::FromConstant(255));
2799 break; 2980 break;
2800 case kTypedDataInt16ArrayCid: 2981 case kTypedDataInt16ArrayCid:
2801 range_ = new Range(RangeBoundary::FromConstant(-32768), 2982 range_ = new Range(RangeBoundary::FromConstant(-32768),
2802 RangeBoundary::FromConstant(32767)); 2983 RangeBoundary::FromConstant(32767));
2803 break; 2984 break;
2804 case kTypedDataUint16ArrayCid: 2985 case kTypedDataUint16ArrayCid:
2805 range_ = new Range(RangeBoundary::FromConstant(0), 2986 range_ = new Range(RangeBoundary::FromConstant(0),
2806 RangeBoundary::FromConstant(65535)); 2987 RangeBoundary::FromConstant(65535));
2807 break; 2988 break;
2989 case kTypedDataInt32ArrayCid:
2990 range_ = new Range(RangeBoundary::FromConstant(kMinInt32),
2991 RangeBoundary::FromConstant(kMaxInt32));
2992 break;
2993 case kTypedDataUint32ArrayCid:
2994 range_ = new Range(RangeBoundary::FromConstant(0),
2995 RangeBoundary::FromConstant(kMaxUint32));
2996 break;
2808 case kOneByteStringCid: 2997 case kOneByteStringCid:
2809 range_ = new Range(RangeBoundary::FromConstant(0), 2998 range_ = new Range(RangeBoundary::FromConstant(0),
2810 RangeBoundary::FromConstant(0xFF)); 2999 RangeBoundary::FromConstant(0xFF));
2811 break; 3000 break;
2812 case kTwoByteStringCid: 3001 case kTwoByteStringCid:
2813 range_ = new Range(RangeBoundary::FromConstant(0), 3002 range_ = new Range(RangeBoundary::FromConstant(0),
2814 RangeBoundary::FromConstant(0xFFFF)); 3003 RangeBoundary::FromConstant(0xFFFF));
2815 break; 3004 break;
2816 default: 3005 default:
2817 Definition::InferRange(); 3006 Definition::InferRange();
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
2919 } 3108 }
2920 3109
2921 3110
2922 void PhiInstr::InferRange() { 3111 void PhiInstr::InferRange() {
2923 RangeBoundary new_min; 3112 RangeBoundary new_min;
2924 RangeBoundary new_max; 3113 RangeBoundary new_max;
2925 3114
2926 for (intptr_t i = 0; i < InputCount(); i++) { 3115 for (intptr_t i = 0; i < InputCount(); i++) {
2927 Range* input_range = InputAt(i)->definition()->range(); 3116 Range* input_range = InputAt(i)->definition()->range();
2928 if (input_range == NULL) { 3117 if (input_range == NULL) {
2929 range_ = Range::Unknown(); 3118 range_ = Range::UnknownSmi();
2930 return; 3119 return;
2931 } 3120 }
2932 3121
2933 if (new_min.IsUnknown()) { 3122 if (new_min.IsUnknown()) {
2934 new_min = Range::ConstantMin(input_range); 3123 new_min = Range::ConstantMin(input_range);
2935 } else { 3124 } else {
2936 new_min = RangeBoundary::Min(new_min, Range::ConstantMin(input_range)); 3125 new_min = RangeBoundary::Min(new_min,
3126 Range::ConstantMinSmi(input_range),
3127 RangeBoundary::kRangeBoundarySmi);
2937 } 3128 }
2938 3129
2939 if (new_max.IsUnknown()) { 3130 if (new_max.IsUnknown()) {
2940 new_max = Range::ConstantMax(input_range); 3131 new_max = Range::ConstantMax(input_range);
2941 } else { 3132 } else {
2942 new_max = RangeBoundary::Max(new_max, Range::ConstantMax(input_range)); 3133 new_max = RangeBoundary::Max(new_max,
3134 Range::ConstantMaxSmi(input_range),
3135 RangeBoundary::kRangeBoundarySmi);
2943 } 3136 }
2944 } 3137 }
2945 3138
2946 ASSERT(new_min.IsUnknown() == new_max.IsUnknown()); 3139 ASSERT(new_min.IsUnknown() == new_max.IsUnknown());
2947 if (new_min.IsUnknown()) { 3140 if (new_min.IsUnknown()) {
2948 range_ = Range::Unknown(); 3141 range_ = Range::UnknownSmi();
2949 return; 3142 return;
2950 } 3143 }
2951 3144
2952 range_ = new Range(new_min, new_max); 3145 range_ = new Range(new_min, new_max);
2953 } 3146 }
2954 3147
2955 3148
2956 bool PhiInstr::IsRedundant() const { 3149 bool PhiInstr::IsRedundant() const {
2957 ASSERT(InputCount() > 1); 3150 ASSERT(InputCount() > 1);
2958 Definition* first = InputAt(0)->definition(); 3151 Definition* first = InputAt(0)->definition();
2959 for (intptr_t i = 1; i < InputCount(); ++i) { 3152 for (intptr_t i = 1; i < InputCount(); ++i) {
2960 Definition* def = InputAt(i)->definition(); 3153 Definition* def = InputAt(i)->definition();
2961 if (def != first) return false; 3154 if (def != first) return false;
2962 } 3155 }
2963 return true; 3156 return true;
2964 } 3157 }
2965 3158
2966 3159
2967 static bool SymbolicSub(const RangeBoundary& a,
2968 const RangeBoundary& b,
2969 RangeBoundary* result) {
2970 if (a.IsSymbol() && b.IsConstant() && !b.Overflowed()) {
2971 const intptr_t offset = a.offset() - b.value();
2972 if (!Smi::IsValid(offset)) return false;
2973
2974 *result = RangeBoundary::FromDefinition(a.symbol(), offset);
2975 return true;
2976 }
2977 return false;
2978 }
2979
2980
2981 static bool SymbolicAdd(const RangeBoundary& a,
2982 const RangeBoundary& b,
2983 RangeBoundary* result) {
2984 if (a.IsSymbol() && b.IsConstant() && !b.Overflowed()) {
2985 const intptr_t offset = a.offset() + b.value();
2986 if (!Smi::IsValid(offset)) return false;
2987
2988 *result = RangeBoundary::FromDefinition(a.symbol(), offset);
2989 return true;
2990 } else if (b.IsSymbol() && a.IsConstant() && !a.Overflowed()) {
2991 const intptr_t offset = b.offset() + a.value();
2992 if (!Smi::IsValid(offset)) return false;
2993
2994 *result = RangeBoundary::FromDefinition(b.symbol(), offset);
2995 return true;
2996 }
2997 return false;
2998 }
2999
3000
3001 static bool IsArrayLength(Definition* defn) { 3160 static bool IsArrayLength(Definition* defn) {
3161 if (defn == NULL) {
3162 return false;
3163 }
3002 LoadFieldInstr* load = defn->AsLoadField(); 3164 LoadFieldInstr* load = defn->AsLoadField();
3003 return (load != NULL) && load->IsImmutableLengthLoad(); 3165 return (load != NULL) && load->IsImmutableLengthLoad();
3004 } 3166 }
3005 3167
3006 3168
3007 static int64_t ConstantAbsMax(const Range* range) {
3008 if (range == NULL) return Smi::kMaxValue;
3009 const int64_t abs_min = Utils::Abs(Range::ConstantMin(range).value());
3010 const int64_t abs_max = Utils::Abs(Range::ConstantMax(range).value());
3011 return abs_min > abs_max ? abs_min : abs_max;
3012 }
3013
3014
3015 static bool OnlyPositiveOrZero(const Range* a, const Range* b) {
3016 if ((a == NULL) || (b == NULL)) return false;
3017 if (Range::ConstantMin(a).value() < 0) return false;
3018 if (Range::ConstantMin(b).value() < 0) return false;
3019 return true;
3020 }
3021
3022
3023 static bool OnlyNegativeOrZero(const Range* a, const Range* b) {
3024 if ((a == NULL) || (b == NULL)) return false;
3025 if (Range::ConstantMax(a).value() > 0) return false;
3026 if (Range::ConstantMax(b).value() > 0) return false;
3027 return true;
3028 }
3029
3030
3031 void BinarySmiOpInstr::InferRange() { 3169 void BinarySmiOpInstr::InferRange() {
3032 // TODO(vegorov): canonicalize BinarySmiOp to always have constant on the 3170 // TODO(vegorov): canonicalize BinarySmiOp to always have constant on the
3033 // right and a non-constant on the left. 3171 // right and a non-constant on the left.
3034 Definition* left_defn = left()->definition(); 3172 Definition* left_defn = left()->definition();
3035 3173
3036 Range* left_range = left_defn->range(); 3174 Range* left_range = left_defn->range();
3037 Range* right_range = right()->definition()->range(); 3175 Range* right_range = right()->definition()->range();
3038 3176
3039 if ((left_range == NULL) || (right_range == NULL)) { 3177 if ((left_range == NULL) || (right_range == NULL)) {
3040 range_ = new Range(RangeBoundary::MinSmi(), RangeBoundary::MaxSmi()); 3178 range_ = Range::UnknownSmi();
3041 return; 3179 return;
3042 } 3180 }
3043 3181
3044 RangeBoundary left_min = 3182 Range* possible_range = Range::BinaryOp(op_kind(),
3045 IsArrayLength(left_defn) ? 3183 left_range,
3046 RangeBoundary::FromDefinition(left_defn) : left_range->min(); 3184 right_range,
3185 left_defn);
3047 3186
3048 RangeBoundary left_max = 3187 if ((range_ == NULL) && (possible_range == NULL)) {
3049 IsArrayLength(left_defn) ? 3188 // Initialize.
3050 RangeBoundary::FromDefinition(left_defn) : left_range->max(); 3189 range_ = Range::UnknownSmi();
3051 3190 return;
3052 RangeBoundary min;
3053 RangeBoundary max;
3054 switch (op_kind()) {
3055 case Token::kADD:
3056 if (!SymbolicAdd(left_min, right_range->min(), &min)) {
3057 min =
3058 RangeBoundary::Add(Range::ConstantMin(left_range),
3059 Range::ConstantMin(right_range),
3060 RangeBoundary::NegativeInfinity());
3061 }
3062
3063 if (!SymbolicAdd(left_max, right_range->max(), &max)) {
3064 max =
3065 RangeBoundary::Add(Range::ConstantMax(right_range),
3066 Range::ConstantMax(left_range),
3067 RangeBoundary::PositiveInfinity());
3068 }
3069 break;
3070
3071 case Token::kSUB:
3072 if (!SymbolicSub(left_min, right_range->max(), &min)) {
3073 min =
3074 RangeBoundary::Sub(Range::ConstantMin(left_range),
3075 Range::ConstantMax(right_range),
3076 RangeBoundary::NegativeInfinity());
3077 }
3078
3079 if (!SymbolicSub(left_max, right_range->min(), &max)) {
3080 max =
3081 RangeBoundary::Sub(Range::ConstantMax(left_range),
3082 Range::ConstantMin(right_range),
3083 RangeBoundary::PositiveInfinity());
3084 }
3085 break;
3086
3087 case Token::kMUL: {
3088 const int64_t left_max = ConstantAbsMax(left_range);
3089 const int64_t right_max = ConstantAbsMax(right_range);
3090 ASSERT(left_max <= -kSmiMin);
3091 ASSERT(right_max <= -kSmiMin);
3092 if ((left_max == 0) || (right_max <= kMaxInt64 / left_max)) {
3093 // Product of left and right max values stays in 64 bit range.
3094 const int64_t result_max = left_max * right_max;
3095 if (Smi::IsValid64(result_max) && Smi::IsValid64(-result_max)) {
3096 const intptr_t r_min =
3097 OnlyPositiveOrZero(left_range, right_range) ? 0 : -result_max;
3098 min = RangeBoundary::FromConstant(r_min);
3099 const intptr_t r_max =
3100 OnlyNegativeOrZero(left_range, right_range) ? 0 : result_max;
3101 max = RangeBoundary::FromConstant(r_max);
3102 break;
3103 }
3104 }
3105 if (range_ == NULL) {
3106 range_ = Range::Unknown();
3107 }
3108 return;
3109 }
3110 case Token::kSHL: {
3111 Range::Shl(left_range, right_range, &min, &max);
3112 break;
3113 }
3114 case Token::kBIT_AND:
3115 if (Range::ConstantMin(right_range).value() >= 0) {
3116 min = RangeBoundary::FromConstant(0);
3117 max = Range::ConstantMax(right_range);
3118 break;
3119 }
3120 if (Range::ConstantMin(left_range).value() >= 0) {
3121 min = RangeBoundary::FromConstant(0);
3122 max = Range::ConstantMax(left_range);
3123 break;
3124 }
3125
3126 if (range_ == NULL) {
3127 range_ = Range::Unknown();
3128 }
3129 return;
3130
3131 default:
3132 if (range_ == NULL) {
3133 range_ = Range::Unknown();
3134 }
3135 return;
3136 } 3191 }
3137 3192
3138 ASSERT(!min.IsUnknown() && !max.IsUnknown()); 3193 if (possible_range == NULL) {
3139 set_overflow(min.LowerBound().Overflowed() || max.UpperBound().Overflowed()); 3194 // Nothing new.
3195 return;
3196 }
3140 3197
3141 if (min.IsConstant()) min.Clamp(); 3198 // TODO(johnmccutchan): Clamp this to smi range?
3142 if (max.IsConstant()) max.Clamp(); 3199 range_ = possible_range;
3143 3200
3144 range_ = new Range(min, max); 3201 ASSERT(!range_->min().IsUnknown() && !range_->max().IsUnknown());
3202 const bool overflowed = range_->min().LowerBound().OverflowedSmi() ||
3203 range_->max().UpperBound().OverflowedSmi();
3204 set_overflow(overflowed);
3145 } 3205 }
3146 3206
3147 3207
3208 void BinaryMintOpInstr::InferRange() {
3209 // TODO(vegorov): canonicalize BinaryMintOpInstr to always have constant on
3210 // the right and a non-constant on the left.
3211 Definition* left_defn = left()->definition();
3212
3213 Range* left_range = left_defn->range();
3214 Range* right_range = right()->definition()->range();
3215
3216 if ((left_range == NULL) || (right_range == NULL)) {
3217 range_ = Range::Unknown();
3218 return;
3219 }
3220
3221 Range* possible_range = Range::BinaryOp(op_kind(),
3222 left_range,
3223 right_range,
3224 left_defn);
3225
3226 if ((range_ == NULL) && (possible_range == NULL)) {
3227 // Initialize.
3228 range_ = Range::Unknown();
3229 return;
3230 }
3231
3232 if (possible_range == NULL) {
3233 // Nothing new.
3234 return;
3235 }
3236
3237 // TODO(johnmccutchan): Clamp this to mint range?
3238 range_ = possible_range;
3239
3240 ASSERT(!range_->min().IsUnknown() && !range_->max().IsUnknown());
3241 }
3242
3243
3148 bool Range::IsPositive() const { 3244 bool Range::IsPositive() const {
3149 if (min().IsNegativeInfinity()) { 3245 if (min().IsNegativeInfinity()) {
3150 return false; 3246 return false;
3151 } 3247 }
3152 if (min().LowerBound().value() < 0) { 3248 if (min().LowerBound().ConstantValue() < 0) {
3153 return false; 3249 return false;
3154 } 3250 }
3155 if (max().IsPositiveInfinity()) { 3251 if (max().IsPositiveInfinity()) {
3156 return true; 3252 return true;
3157 } 3253 }
3158 return max().UpperBound().value() >= 0; 3254 return max().UpperBound().ConstantValue() >= 0;
3159 } 3255 }
3160 3256
3161 3257
3162 bool Range::IsNegative() const { 3258 bool Range::OnlyLessThanOrEqualTo(int64_t val) const {
3163 if (max().IsPositiveInfinity()) {
3164 return false;
3165 }
3166 if (max().UpperBound().value() >= 0) {
3167 return false;
3168 }
3169 if (min().IsNegativeInfinity()) {
3170 return true;
3171 }
3172 return min().LowerBound().value() < 0;
3173 }
3174
3175
3176 bool Range::OnlyLessThanOrEqualTo(intptr_t val) const {
3177 if (max().IsPositiveInfinity()) { 3259 if (max().IsPositiveInfinity()) {
3178 // Cannot be true. 3260 // Cannot be true.
3179 return false; 3261 return false;
3180 } 3262 }
3181 if (max().UpperBound().value() > val) { 3263 if (max().UpperBound().ConstantValue() > val) {
3182 // Not true. 3264 // Not true.
3183 return false; 3265 return false;
3184 } 3266 }
3185 if (!min().IsNegativeInfinity()) { 3267 return true;
3186 if (min().LowerBound().value() > val) { 3268 }
3187 // Lower bound is > value. 3269
3188 return false; 3270
3189 } 3271 bool Range::OnlyGreaterThanOrEqualTo(int64_t val) const {
3272 if (min().IsNegativeInfinity()) {
3273 return false;
3274 }
3275 if (min().LowerBound().ConstantValue() < val) {
3276 return false;
3190 } 3277 }
3191 return true; 3278 return true;
3192 } 3279 }
3193 3280
3194 3281
3195 // Inclusive. 3282 // Inclusive.
3196 bool Range::IsWithin(intptr_t min_int, intptr_t max_int) const { 3283 bool Range::IsWithin(int64_t min_int, int64_t max_int) const {
3197 RangeBoundary lower_min = min().LowerBound(); 3284 RangeBoundary lower_min = min().LowerBound();
3198 if (lower_min.IsNegativeInfinity() || (lower_min.value() < min_int)) { 3285 if (lower_min.IsNegativeInfinity() || (lower_min.ConstantValue() < min_int)) {
3199 return false; 3286 return false;
3200 } 3287 }
3201 RangeBoundary upper_max = max().UpperBound(); 3288 RangeBoundary upper_max = max().UpperBound();
3202 if (upper_max.IsPositiveInfinity() || (upper_max.value() > max_int)) { 3289 if (upper_max.IsPositiveInfinity() || (upper_max.ConstantValue() > max_int)) {
3203 return false; 3290 return false;
3204 } 3291 }
3205 return true; 3292 return true;
3206 } 3293 }
3207 3294
3208 3295
3209 bool Range::Overlaps(intptr_t min_int, intptr_t max_int) const { 3296 bool Range::Overlaps(int64_t min_int, int64_t max_int) const {
3210 const intptr_t this_min = min().IsNegativeInfinity() ? 3297 RangeBoundary lower = min().LowerBound();
3211 kIntptrMin : min().LowerBound().value(); 3298 RangeBoundary upper = max().UpperBound();
3212 const intptr_t this_max = max().IsPositiveInfinity() ? 3299 const int64_t this_min = lower.IsNegativeInfinity() ?
3213 kIntptrMax : max().UpperBound().value(); 3300 RangeBoundary::kMin : lower.ConstantValue();
3301 const int64_t this_max = upper.IsPositiveInfinity() ?
3302 RangeBoundary::kMax : upper.ConstantValue();
3214 if ((this_min <= min_int) && (min_int <= this_max)) return true; 3303 if ((this_min <= min_int) && (min_int <= this_max)) return true;
3215 if ((this_min <= max_int) && (max_int <= this_max)) return true; 3304 if ((this_min <= max_int) && (max_int <= this_max)) return true;
3216 if ((min_int < this_min) && (max_int > this_max)) return true; 3305 if ((min_int < this_min) && (max_int > this_max)) return true;
3217 return false; 3306 return false;
3218 } 3307 }
3219 3308
3220 3309
3221 bool Range::IsUnsatisfiable() const { 3310 bool Range::IsUnsatisfiable() const {
3222 // Infinity case: [+inf, ...] || [..., -inf] 3311 // Infinity case: [+inf, ...] || [..., -inf]
3223 if (min().IsPositiveInfinity() || max().IsNegativeInfinity()) { 3312 if (min().IsPositiveInfinity() || max().IsNegativeInfinity()) {
3224 return true; 3313 return true;
3225 } 3314 }
3226 // Constant case: For example [0, -1]. 3315 // Constant case: For example [0, -1].
3227 if (Range::ConstantMin(this).value() > Range::ConstantMax(this).value()) { 3316 if (Range::ConstantMin(this).ConstantValue() >
3317 Range::ConstantMax(this).ConstantValue()) {
3228 return true; 3318 return true;
3229 } 3319 }
3230 // Symbol case: For example [v+1, v]. 3320 // Symbol case: For example [v+1, v].
3231 if (DependOnSameSymbol(min(), max()) && min().offset() > max().offset()) { 3321 if (DependOnSameSymbol(min(), max()) && min().offset() > max().offset()) {
3232 return true; 3322 return true;
3233 } 3323 }
3234 return false; 3324 return false;
3235 } 3325 }
3236 3326
3237 3327
3238 void Range::Shl(Range* left, 3328 void Range::Shl(const Range* left,
3239 Range* right, 3329 const Range* right,
3240 RangeBoundary* result_min, 3330 RangeBoundary* result_min,
3241 RangeBoundary* result_max) { 3331 RangeBoundary* result_max) {
3332 ASSERT(left != NULL);
3333 ASSERT(right != NULL);
3334 ASSERT(result_min != NULL);
3335 ASSERT(result_max != NULL);
3242 RangeBoundary left_max = Range::ConstantMax(left); 3336 RangeBoundary left_max = Range::ConstantMax(left);
3243 RangeBoundary left_min = Range::ConstantMin(left); 3337 RangeBoundary left_min = Range::ConstantMin(left);
3244 // A negative shift count always deoptimizes (and throws), so the minimum 3338 // A negative shift count always deoptimizes (and throws), so the minimum
3245 // shift count is zero. 3339 // shift count is zero.
3246 intptr_t right_max = Utils::Maximum(Range::ConstantMax(right).value(), 3340 int64_t right_max = Utils::Maximum(Range::ConstantMax(right).ConstantValue(),
3247 static_cast<intptr_t>(0)); 3341 static_cast<int64_t>(0));
3248 intptr_t right_min = Utils::Maximum(Range::ConstantMin(right).value(), 3342 int64_t right_min = Utils::Maximum(Range::ConstantMin(right).ConstantValue(),
3249 static_cast<intptr_t>(0)); 3343 static_cast<int64_t>(0));
3250 3344
3251 *result_min = RangeBoundary::Shl( 3345 *result_min = RangeBoundary::Shl(
3252 left_min, 3346 left_min,
3253 left_min.value() > 0 ? right_min : right_max, 3347 left_min.ConstantValue() > 0 ? right_min : right_max,
3254 left_min.value() > 0 3348 left_min.ConstantValue() > 0
3255 ? RangeBoundary::PositiveInfinity() 3349 ? RangeBoundary::PositiveInfinity()
3256 : RangeBoundary::NegativeInfinity()); 3350 : RangeBoundary::NegativeInfinity());
3257 3351
3258 *result_max = RangeBoundary::Shl( 3352 *result_max = RangeBoundary::Shl(
3259 left_max, 3353 left_max,
3260 left_max.value() > 0 ? right_max : right_min, 3354 left_max.ConstantValue() > 0 ? right_max : right_min,
3261 left_max.value() > 0 3355 left_max.ConstantValue() > 0
3262 ? RangeBoundary::PositiveInfinity() 3356 ? RangeBoundary::PositiveInfinity()
3263 : RangeBoundary::NegativeInfinity()); 3357 : RangeBoundary::NegativeInfinity());
3264 } 3358 }
3265 3359
3266 3360
3361 bool Range::And(const Range* left_range,
3362 const Range* right_range,
3363 RangeBoundary* min,
3364 RangeBoundary* max) {
3365 ASSERT(left_range != NULL);
3366 ASSERT(right_range != NULL);
3367 ASSERT(min != NULL);
3368 ASSERT(max != NULL);
3369
3370 if (Range::ConstantMin(right_range).ConstantValue() >= 0) {
3371 *min = RangeBoundary::FromConstant(0);
3372 *max = Range::ConstantMax(right_range);
3373 return true;
3374 }
3375
3376 if (Range::ConstantMin(left_range).ConstantValue() >= 0) {
3377 *min = RangeBoundary::FromConstant(0);
3378 *max = Range::ConstantMax(left_range);
3379 return true;
3380 }
3381
3382 return false;
3383 }
3384
3385
3386 void Range::Add(const Range* left_range,
3387 const Range* right_range,
3388 RangeBoundary* min,
3389 RangeBoundary* max,
3390 Definition* left_defn) {
3391 ASSERT(left_range != NULL);
3392 ASSERT(right_range != NULL);
3393 ASSERT(min != NULL);
3394 ASSERT(max != NULL);
3395
3396 RangeBoundary left_min =
3397 IsArrayLength(left_defn) ?
3398 RangeBoundary::FromDefinition(left_defn) : left_range->min();
3399
3400 RangeBoundary left_max =
3401 IsArrayLength(left_defn) ?
3402 RangeBoundary::FromDefinition(left_defn) : left_range->max();
3403
3404 if (!RangeBoundary::SymbolicAdd(left_min, right_range->min(), min)) {
3405 *min = RangeBoundary::Add(left_range->min().LowerBound(),
3406 right_range->min().LowerBound(),
3407 RangeBoundary::NegativeInfinity());
3408 }
3409 if (!RangeBoundary::SymbolicAdd(left_max, right_range->max(), max)) {
3410 *max = RangeBoundary::Add(right_range->max().UpperBound(),
3411 left_range->max().UpperBound(),
3412 RangeBoundary::PositiveInfinity());
3413 }
3414 }
3415
3416
3417 void Range::Sub(const Range* left_range,
3418 const Range* right_range,
3419 RangeBoundary* min,
3420 RangeBoundary* max,
3421 Definition* left_defn) {
3422 ASSERT(left_range != NULL);
3423 ASSERT(right_range != NULL);
3424 ASSERT(min != NULL);
3425 ASSERT(max != NULL);
3426
3427 RangeBoundary left_min =
3428 IsArrayLength(left_defn) ?
3429 RangeBoundary::FromDefinition(left_defn) : left_range->min();
3430
3431 RangeBoundary left_max =
3432 IsArrayLength(left_defn) ?
3433 RangeBoundary::FromDefinition(left_defn) : left_range->max();
3434
3435 if (!RangeBoundary::SymbolicSub(left_min, right_range->max(), min)) {
3436 *min = RangeBoundary::Sub(left_range->min().LowerBound(),
3437 right_range->max().UpperBound(),
3438 RangeBoundary::NegativeInfinity());
3439 }
3440 if (!RangeBoundary::SymbolicSub(left_max, right_range->min(), max)) {
3441 *max = RangeBoundary::Sub(left_range->max().UpperBound(),
3442 right_range->min().LowerBound(),
3443 RangeBoundary::PositiveInfinity());
3444 }
3445 }
3446
3447
3448 bool Range::Mul(const Range* left_range,
3449 const Range* right_range,
3450 RangeBoundary* min,
3451 RangeBoundary* max) {
3452 ASSERT(left_range != NULL);
3453 ASSERT(right_range != NULL);
3454 ASSERT(min != NULL);
3455 ASSERT(max != NULL);
3456
3457 const int64_t left_max = ConstantAbsMax(left_range);
3458 const int64_t right_max = ConstantAbsMax(right_range);
3459 if ((left_max <= -kSmiMin) && (right_max <= -kSmiMin) &&
3460 ((left_max == 0) || (right_max <= kMaxInt64 / left_max))) {
3461 // Product of left and right max values stays in 64 bit range.
3462 const int64_t result_max = left_max * right_max;
3463 if (Smi::IsValid64(result_max) && Smi::IsValid64(-result_max)) {
3464 const intptr_t r_min =
3465 OnlyPositiveOrZero(*left_range, *right_range) ? 0 : -result_max;
3466 *min = RangeBoundary::FromConstant(r_min);
3467 const intptr_t r_max =
3468 OnlyNegativeOrZero(*left_range, *right_range) ? 0 : result_max;
3469 *max = RangeBoundary::FromConstant(r_max);
3470 return true;
3471 }
3472 }
3473 return false;
3474 }
3475
3476
3477 // Both the a and b ranges are >= 0.
3478 bool Range::OnlyPositiveOrZero(const Range& a, const Range& b) {
3479 return a.OnlyGreaterThanOrEqualTo(0) && b.OnlyGreaterThanOrEqualTo(0);
3480 }
3481
3482
3483 // Both the a and b ranges are <= 0.
3484 bool Range::OnlyNegativeOrZero(const Range& a, const Range& b) {
3485 return a.OnlyLessThanOrEqualTo(0) && b.OnlyLessThanOrEqualTo(0);
3486 }
3487
3488
3489 // Return the maximum absolute value included in range.
3490 int64_t Range::ConstantAbsMax(const Range* range) {
3491 if (range == NULL) {
3492 return RangeBoundary::kMax;
3493 }
3494 const int64_t abs_min = Utils::Abs(Range::ConstantMin(range).ConstantValue());
3495 const int64_t abs_max = Utils::Abs(Range::ConstantMax(range).ConstantValue());
3496 return Utils::Maximum(abs_min, abs_max);
3497 }
3498
3499
3500 Range* Range::BinaryOp(const Token::Kind op,
3501 const Range* left_range,
3502 const Range* right_range,
3503 Definition* left_defn) {
3504 ASSERT(left_range != NULL);
3505 ASSERT(right_range != NULL);
3506
3507 RangeBoundary min;
3508 RangeBoundary max;
3509 ASSERT(min.IsUnknown() && max.IsUnknown());
3510
3511 switch (op) {
3512 case Token::kADD:
3513 Range::Add(left_range, right_range, &min, &max, left_defn);
3514 break;
3515 case Token::kSUB:
3516 Range::Sub(left_range, right_range, &min, &max, left_defn);
3517 break;
3518 case Token::kMUL: {
3519 if (!Range::Mul(left_range, right_range, &min, &max)) {
3520 return NULL;
3521 }
3522 break;
3523 }
3524 case Token::kSHL: {
3525 Range::Shl(left_range, right_range, &min, &max);
3526 break;
3527 }
3528 case Token::kBIT_AND:
3529 if (!Range::And(left_range, right_range, &min, &max)) {
3530 return NULL;
3531 }
3532 break;
3533 default:
3534 return NULL;
3535 break;
3536 }
3537
3538 ASSERT(!min.IsUnknown() && !max.IsUnknown());
3539
3540 return new Range(min, max);
3541 }
3542
3543
3267 bool CheckArrayBoundInstr::IsFixedLengthArrayType(intptr_t cid) { 3544 bool CheckArrayBoundInstr::IsFixedLengthArrayType(intptr_t cid) {
3268 return LoadFieldInstr::IsFixedLengthArrayCid(cid); 3545 return LoadFieldInstr::IsFixedLengthArrayCid(cid);
3269 } 3546 }
3270 3547
3271 3548
3272 bool CheckArrayBoundInstr::IsRedundant(RangeBoundary length) { 3549 bool CheckArrayBoundInstr::IsRedundant(RangeBoundary length) {
3273 Range* index_range = index()->definition()->range(); 3550 Range* index_range = index()->definition()->range();
3274 3551
3275 // Range of the index is unknown can't decide if the check is redundant. 3552 // Range of the index is unknown can't decide if the check is redundant.
3276 if (index_range == NULL) { 3553 if (index_range == NULL) {
3277 return false; 3554 return false;
3278 } 3555 }
3279 3556
3280 // Range of the index is not positive. Check can't be redundant. 3557 // Range of the index is not positive. Check can't be redundant.
3281 if (Range::ConstantMin(index_range).value() < 0) { 3558 if (Range::ConstantMinSmi(index_range).ConstantValue() < 0) {
3282 return false; 3559 return false;
3283 } 3560 }
3284 3561
3285 RangeBoundary max = CanonicalizeBoundary(index_range->max(), 3562 RangeBoundary max = CanonicalizeBoundary(index_range->max(),
3286 RangeBoundary::PositiveInfinity()); 3563 RangeBoundary::PositiveInfinity());
3287 3564
3288 if (max.Overflowed()) { 3565 if (max.OverflowedSmi()) {
3289 return false; 3566 return false;
3290 } 3567 }
3291 3568
3292 3569
3293 RangeBoundary max_upper = max.UpperBound(); 3570 RangeBoundary max_upper = max.UpperBound();
3294 RangeBoundary length_lower = length.LowerBound(); 3571 RangeBoundary length_lower = length.LowerBound();
3295 3572
3296 if (max_upper.Overflowed() || length_lower.Overflowed()) { 3573 if (max_upper.OverflowedSmi() || length_lower.OverflowedSmi()) {
3297 return false; 3574 return false;
3298 } 3575 }
3299 3576
3300 // Try to compare constant boundaries. 3577 // Try to compare constant boundaries.
3301 if (max_upper.value() < length_lower.value()) { 3578 if (max_upper.ConstantValue() < length_lower.ConstantValue()) {
3302 return true; 3579 return true;
3303 } 3580 }
3304 3581
3305 length = CanonicalizeBoundary(length, RangeBoundary::PositiveInfinity()); 3582 length = CanonicalizeBoundary(length, RangeBoundary::PositiveInfinity());
3306 if (length.Overflowed()) { 3583 if (length.OverflowedSmi()) {
3307 return false; 3584 return false;
3308 } 3585 }
3309 3586
3310 // Try symbolic comparison. 3587 // Try symbolic comparison.
3311 do { 3588 do {
3312 if (DependOnSameSymbol(max, length)) return max.offset() < length.offset(); 3589 if (DependOnSameSymbol(max, length)) return max.offset() < length.offset();
3313 } while (CanonicalizeMaxBoundary(&max) || CanonicalizeMinBoundary(&length)); 3590 } while (CanonicalizeMaxBoundary(&max) || CanonicalizeMinBoundary(&length));
3314 3591
3315 // Failed to prove that maximum is bounded with array length. 3592 // Failed to prove that maximum is bounded with array length.
3316 return false; 3593 return false;
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
3589 case Token::kTRUNCDIV: return 0; 3866 case Token::kTRUNCDIV: return 0;
3590 case Token::kMOD: return 1; 3867 case Token::kMOD: return 1;
3591 default: UNIMPLEMENTED(); return -1; 3868 default: UNIMPLEMENTED(); return -1;
3592 } 3869 }
3593 } 3870 }
3594 3871
3595 3872
3596 #undef __ 3873 #undef __
3597 3874
3598 } // namespace dart 3875 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698