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

Side by Side Diff: src/compiler/types.cc

Issue 2381523002: [Turbofan] Introduce OtherNumberConstant. (Closed)
Patch Set: Better DCHECK in HeapConstantType(). Created 4 years, 2 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
« no previous file with comments | « src/compiler/types.h ('k') | test/cctest/test-types.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <iomanip> 5 #include <iomanip>
6 6
7 #include "src/compiler/types.h" 7 #include "src/compiler/types.h"
8 8
9 #include "src/handles-inl.h" 9 #include "src/handles-inl.h"
10 #include "src/ostreams.h" 10 #include "src/ostreams.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 return !RangeType::Limits::Intersect(RangeType::Limits(lhs), 49 return !RangeType::Limits::Intersect(RangeType::Limits(lhs),
50 RangeType::Limits(rhs)) 50 RangeType::Limits(rhs))
51 .IsEmpty(); 51 .IsEmpty();
52 } 52 }
53 53
54 bool Type::Contains(RangeType* lhs, RangeType* rhs) { 54 bool Type::Contains(RangeType* lhs, RangeType* rhs) {
55 DisallowHeapAllocation no_allocation; 55 DisallowHeapAllocation no_allocation;
56 return lhs->Min() <= rhs->Min() && rhs->Max() <= lhs->Max(); 56 return lhs->Min() <= rhs->Min() && rhs->Max() <= lhs->Max();
57 } 57 }
58 58
59 bool Type::Contains(RangeType* lhs, ConstantType* rhs) {
60 DisallowHeapAllocation no_allocation;
61 return IsInteger(*rhs->Value()) && lhs->Min() <= rhs->Value()->Number() &&
62 rhs->Value()->Number() <= lhs->Max();
63 }
64
65 bool Type::Contains(RangeType* range, i::Object* val) { 59 bool Type::Contains(RangeType* range, i::Object* val) {
66 DisallowHeapAllocation no_allocation; 60 DisallowHeapAllocation no_allocation;
67 return IsInteger(val) && range->Min() <= val->Number() && 61 return IsInteger(val) && range->Min() <= val->Number() &&
68 val->Number() <= range->Max(); 62 val->Number() <= range->Max();
69 } 63 }
70 64
71 // ----------------------------------------------------------------------------- 65 // -----------------------------------------------------------------------------
72 // Min and Max computation. 66 // Min and Max computation.
73 67
74 double Type::Min() { 68 double Type::Min() {
75 DCHECK(this->Is(Number())); 69 DCHECK(this->Is(Number()));
76 if (this->IsBitset()) return BitsetType::Min(this->AsBitset()); 70 if (this->IsBitset()) return BitsetType::Min(this->AsBitset());
77 if (this->IsUnion()) { 71 if (this->IsUnion()) {
78 double min = +V8_INFINITY; 72 double min = +V8_INFINITY;
79 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { 73 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
80 min = std::min(min, this->AsUnion()->Get(i)->Min()); 74 min = std::min(min, this->AsUnion()->Get(i)->Min());
81 } 75 }
82 return min; 76 return min;
83 } 77 }
84 if (this->IsRange()) return this->AsRange()->Min(); 78 if (this->IsRange()) return this->AsRange()->Min();
85 if (this->IsConstant()) return this->AsConstant()->Value()->Number(); 79 if (this->IsOtherNumberConstant())
80 return this->AsOtherNumberConstant()->Value();
86 UNREACHABLE(); 81 UNREACHABLE();
87 return 0; 82 return 0;
88 } 83 }
89 84
90 double Type::Max() { 85 double Type::Max() {
91 DCHECK(this->Is(Number())); 86 DCHECK(this->Is(Number()));
92 if (this->IsBitset()) return BitsetType::Max(this->AsBitset()); 87 if (this->IsBitset()) return BitsetType::Max(this->AsBitset());
93 if (this->IsUnion()) { 88 if (this->IsUnion()) {
94 double max = -V8_INFINITY; 89 double max = -V8_INFINITY;
95 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { 90 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
96 max = std::max(max, this->AsUnion()->Get(i)->Max()); 91 max = std::max(max, this->AsUnion()->Get(i)->Max());
97 } 92 }
98 return max; 93 return max;
99 } 94 }
100 if (this->IsRange()) return this->AsRange()->Max(); 95 if (this->IsRange()) return this->AsRange()->Max();
101 if (this->IsConstant()) return this->AsConstant()->Value()->Number(); 96 if (this->IsOtherNumberConstant())
97 return this->AsOtherNumberConstant()->Value();
102 UNREACHABLE(); 98 UNREACHABLE();
103 return 0; 99 return 0;
104 } 100 }
105 101
106 // ----------------------------------------------------------------------------- 102 // -----------------------------------------------------------------------------
107 // Glb and lub computation. 103 // Glb and lub computation.
108 104
109 // The largest bitset subsumed by this type. 105 // The largest bitset subsumed by this type.
110 Type::bitset BitsetType::Glb(Type* type) { 106 Type::bitset BitsetType::Glb(Type* type) {
111 DisallowHeapAllocation no_allocation; 107 DisallowHeapAllocation no_allocation;
(...skipping 20 matching lines...) Expand all
132 if (type->IsUnion()) { 128 if (type->IsUnion()) {
133 // Take the representation from the first element, which is always 129 // Take the representation from the first element, which is always
134 // a bitset. 130 // a bitset.
135 int bitset = type->AsUnion()->Get(0)->BitsetLub(); 131 int bitset = type->AsUnion()->Get(0)->BitsetLub();
136 for (int i = 0, n = type->AsUnion()->Length(); i < n; ++i) { 132 for (int i = 0, n = type->AsUnion()->Length(); i < n; ++i) {
137 // Other elements only contribute their semantic part. 133 // Other elements only contribute their semantic part.
138 bitset |= type->AsUnion()->Get(i)->BitsetLub(); 134 bitset |= type->AsUnion()->Get(i)->BitsetLub();
139 } 135 }
140 return bitset; 136 return bitset;
141 } 137 }
142 if (type->IsConstant()) return type->AsConstant()->Lub(); 138 if (type->IsHeapConstant()) return type->AsHeapConstant()->Lub();
139 if (type->IsOtherNumberConstant())
140 return type->AsOtherNumberConstant()->Lub();
143 if (type->IsRange()) return type->AsRange()->Lub(); 141 if (type->IsRange()) return type->AsRange()->Lub();
144 if (type->IsTuple()) return kOtherInternal; 142 if (type->IsTuple()) return kOtherInternal;
145 UNREACHABLE(); 143 UNREACHABLE();
146 return kNone; 144 return kNone;
147 } 145 }
148 146
149 Type::bitset BitsetType::Lub(i::Map* map) { 147 Type::bitset BitsetType::Lub(i::Map* map) {
150 DisallowHeapAllocation no_allocation; 148 DisallowHeapAllocation no_allocation;
151 switch (map->instance_type()) { 149 switch (map->instance_type()) {
152 case STRING_TYPE: 150 case STRING_TYPE:
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 } 381 }
384 for (size_t i = BoundariesSize() - 1; i-- > 0;) { 382 for (size_t i = BoundariesSize() - 1; i-- > 0;) {
385 if (Is(mins[i].internal, bits)) { 383 if (Is(mins[i].internal, bits)) {
386 return mz ? std::max(0.0, mins[i + 1].min - 1) : mins[i + 1].min - 1; 384 return mz ? std::max(0.0, mins[i + 1].min - 1) : mins[i + 1].min - 1;
387 } 385 }
388 } 386 }
389 if (mz) return 0; 387 if (mz) return 0;
390 return std::numeric_limits<double>::quiet_NaN(); 388 return std::numeric_limits<double>::quiet_NaN();
391 } 389 }
392 390
391 // static
392 bool OtherNumberConstantType::IsOtherNumberConstant(double value) {
393 // Not an integer, not NaN, and not -0.
394 return !std::isnan(value) && !Type::IsInteger(value) &&
395 !i::IsMinusZero(value);
396 }
397
398 // static
399 bool OtherNumberConstantType::IsOtherNumberConstant(Object* value) {
400 return value->IsHeapNumber() &&
401 IsOtherNumberConstant(HeapNumber::cast(value)->value());
402 }
403
404 HeapConstantType::HeapConstantType(BitsetType::bitset bitset,
405 i::Handle<i::Object> object)
406 : TypeBase(kHeapConstant), bitset_(bitset), object_(object) {
407 // All number types should be expressed as Ranges, OtherNumberConstants,
408 // or bitsets (nan, negative-zero).
409 DCHECK(!object->IsSmi() && !object->IsHeapNumber());
410 }
411
393 // ----------------------------------------------------------------------------- 412 // -----------------------------------------------------------------------------
394 // Predicates. 413 // Predicates.
395 414
396 bool Type::SimplyEquals(Type* that) { 415 bool Type::SimplyEquals(Type* that) {
397 DisallowHeapAllocation no_allocation; 416 DisallowHeapAllocation no_allocation;
398 if (this->IsConstant()) { 417 if (this->IsHeapConstant()) {
399 return that->IsConstant() && 418 return that->IsHeapConstant() &&
400 *this->AsConstant()->Value() == *that->AsConstant()->Value(); 419 *this->AsHeapConstant()->Value() == *that->AsHeapConstant()->Value();
420 }
421 if (this->IsOtherNumberConstant()) {
422 return that->IsOtherNumberConstant() &&
423 this->AsOtherNumberConstant()->Value() ==
424 that->AsOtherNumberConstant()->Value();
425 }
426 if (this->IsRange()) {
427 if (that->IsHeapConstant() || that->IsOtherNumberConstant()) return false;
401 } 428 }
402 if (this->IsTuple()) { 429 if (this->IsTuple()) {
403 if (!that->IsTuple()) return false; 430 if (!that->IsTuple()) return false;
404 TupleType* this_tuple = this->AsTuple(); 431 TupleType* this_tuple = this->AsTuple();
405 TupleType* that_tuple = that->AsTuple(); 432 TupleType* that_tuple = that->AsTuple();
406 if (this_tuple->Arity() != that_tuple->Arity()) { 433 if (this_tuple->Arity() != that_tuple->Arity()) {
407 return false; 434 return false;
408 } 435 }
409 for (int i = 0, n = this_tuple->Arity(); i < n; ++i) { 436 for (int i = 0, n = this_tuple->Arity(); i < n; ++i) {
410 if (!this_tuple->Element(i)->Equals(that_tuple->Element(i))) return false; 437 if (!this_tuple->Element(i)->Equals(that_tuple->Element(i))) return false;
(...skipping 28 matching lines...) Expand all
439 // T <= (T1 \/ ... \/ Tn) if (T <= T1) \/ ... \/ (T <= Tn) 466 // T <= (T1 \/ ... \/ Tn) if (T <= T1) \/ ... \/ (T <= Tn)
440 if (that->IsUnion()) { 467 if (that->IsUnion()) {
441 for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) { 468 for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) {
442 if (this->Is(that->AsUnion()->Get(i))) return true; 469 if (this->Is(that->AsUnion()->Get(i))) return true;
443 if (i > 1 && this->IsRange()) return false; // Shortcut. 470 if (i > 1 && this->IsRange()) return false; // Shortcut.
444 } 471 }
445 return false; 472 return false;
446 } 473 }
447 474
448 if (that->IsRange()) { 475 if (that->IsRange()) {
449 return (this->IsRange() && Contains(that->AsRange(), this->AsRange())) || 476 return (this->IsRange() && Contains(that->AsRange(), this->AsRange()));
450 (this->IsConstant() &&
451 Contains(that->AsRange(), this->AsConstant()));
452 } 477 }
453 if (this->IsRange()) return false; 478 if (this->IsRange()) return false;
454 479
455 return this->SimplyEquals(that); 480 return this->SimplyEquals(that);
456 } 481 }
457 482
458 // Check if [this] and [that] overlap. 483 // Check if [this] and [that] overlap.
459 bool Type::Maybe(Type* that) { 484 bool Type::Maybe(Type* that) {
460 DisallowHeapAllocation no_allocation; 485 DisallowHeapAllocation no_allocation;
461 486
(...skipping 12 matching lines...) Expand all
474 if (that->IsUnion()) { 499 if (that->IsUnion()) {
475 for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) { 500 for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) {
476 if (this->Maybe(that->AsUnion()->Get(i))) return true; 501 if (this->Maybe(that->AsUnion()->Get(i))) return true;
477 } 502 }
478 return false; 503 return false;
479 } 504 }
480 505
481 if (this->IsBitset() && that->IsBitset()) return true; 506 if (this->IsBitset() && that->IsBitset()) return true;
482 507
483 if (this->IsRange()) { 508 if (this->IsRange()) {
484 if (that->IsConstant()) {
485 return Contains(this->AsRange(), that->AsConstant());
486 }
487 if (that->IsRange()) { 509 if (that->IsRange()) {
488 return Overlap(this->AsRange(), that->AsRange()); 510 return Overlap(this->AsRange(), that->AsRange());
489 } 511 }
490 if (that->IsBitset()) { 512 if (that->IsBitset()) {
491 bitset number_bits = BitsetType::NumberBits(that->AsBitset()); 513 bitset number_bits = BitsetType::NumberBits(that->AsBitset());
492 if (number_bits == BitsetType::kNone) { 514 if (number_bits == BitsetType::kNone) {
493 return false; 515 return false;
494 } 516 }
495 double min = std::max(BitsetType::Min(number_bits), this->Min()); 517 double min = std::max(BitsetType::Min(number_bits), this->Min());
496 double max = std::min(BitsetType::Max(number_bits), this->Max()); 518 double max = std::min(BitsetType::Max(number_bits), this->Max());
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 688
667 if (lhs->IsRange()) { 689 if (lhs->IsRange()) {
668 if (rhs->IsBitset()) { 690 if (rhs->IsBitset()) {
669 RangeType::Limits lim = IntersectRangeAndBitset(lhs, rhs, zone); 691 RangeType::Limits lim = IntersectRangeAndBitset(lhs, rhs, zone);
670 692
671 if (!lim.IsEmpty()) { 693 if (!lim.IsEmpty()) {
672 *lims = RangeType::Limits::Union(lim, *lims); 694 *lims = RangeType::Limits::Union(lim, *lims);
673 } 695 }
674 return size; 696 return size;
675 } 697 }
676 if (rhs->IsConstant() && Contains(lhs->AsRange(), rhs->AsConstant())) {
677 return AddToUnion(rhs, result, size, zone);
678 }
679 if (rhs->IsRange()) { 698 if (rhs->IsRange()) {
680 RangeType::Limits lim = RangeType::Limits::Intersect( 699 RangeType::Limits lim = RangeType::Limits::Intersect(
681 RangeType::Limits(lhs->AsRange()), RangeType::Limits(rhs->AsRange())); 700 RangeType::Limits(lhs->AsRange()), RangeType::Limits(rhs->AsRange()));
682 if (!lim.IsEmpty()) { 701 if (!lim.IsEmpty()) {
683 *lims = RangeType::Limits::Union(lim, *lims); 702 *lims = RangeType::Limits::Union(lim, *lims);
684 } 703 }
685 } 704 }
686 return size; 705 return size;
687 } 706 }
688 if (rhs->IsRange()) { 707 if (rhs->IsRange()) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 755
737 if (bitset_min < range_min) { 756 if (bitset_min < range_min) {
738 range_min = bitset_min; 757 range_min = bitset_min;
739 } 758 }
740 if (bitset_max > range_max) { 759 if (bitset_max > range_max) {
741 range_max = bitset_max; 760 range_max = bitset_max;
742 } 761 }
743 return RangeType::New(range_min, range_max, zone); 762 return RangeType::New(range_min, range_max, zone);
744 } 763 }
745 764
765 Type* Type::NewConstant(double value, Zone* zone) {
766 if (IsInteger(value)) {
767 return Range(value, value, zone);
768 } else if (i::IsMinusZero(value)) {
769 return Type::MinusZero();
770 } else if (std::isnan(value)) {
771 return Type::NaN();
772 }
773
774 DCHECK(OtherNumberConstantType::IsOtherNumberConstant(value));
775 return OtherNumberConstant(value, zone);
776 }
777
778 Type* Type::NewConstant(i::Handle<i::Object> value, Zone* zone) {
779 if (IsInteger(*value)) {
780 double v = value->Number();
781 return Range(v, v, zone);
782 } else if (value->IsHeapNumber()) {
783 return NewConstant(value->Number(), zone);
784 }
785 return HeapConstant(value, zone);
786 }
787
746 Type* Type::Union(Type* type1, Type* type2, Zone* zone) { 788 Type* Type::Union(Type* type1, Type* type2, Zone* zone) {
747 // Fast case: bit sets. 789 // Fast case: bit sets.
748 if (type1->IsBitset() && type2->IsBitset()) { 790 if (type1->IsBitset() && type2->IsBitset()) {
749 return BitsetType::New(type1->AsBitset() | type2->AsBitset()); 791 return BitsetType::New(type1->AsBitset() | type2->AsBitset());
750 } 792 }
751 793
752 // Fast case: top or bottom types. 794 // Fast case: top or bottom types.
753 if (type1->IsAny() || type2->IsNone()) return type1; 795 if (type1->IsAny() || type2->IsNone()) return type1;
754 if (type2->IsAny() || type1->IsNone()) return type2; 796 if (type2->IsAny() || type1->IsNone()) return type2;
755 797
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 if (unioned->Get(1)->IsRange()) { 868 if (unioned->Get(1)->IsRange()) {
827 return RangeType::New(unioned->Get(1)->AsRange()->Min(), 869 return RangeType::New(unioned->Get(1)->AsRange()->Min(),
828 unioned->Get(1)->AsRange()->Max(), zone); 870 unioned->Get(1)->AsRange()->Max(), zone);
829 } 871 }
830 } 872 }
831 unioned->Shrink(size); 873 unioned->Shrink(size);
832 SLOW_DCHECK(unioned->Wellformed()); 874 SLOW_DCHECK(unioned->Wellformed());
833 return union_type; 875 return union_type;
834 } 876 }
835 877
836 // -----------------------------------------------------------------------------
837 // Iteration.
838
839 int Type::NumConstants() { 878 int Type::NumConstants() {
840 DisallowHeapAllocation no_allocation; 879 DisallowHeapAllocation no_allocation;
841 if (this->IsConstant()) { 880 if (this->IsHeapConstant() || this->IsOtherNumberConstant()) {
842 return 1; 881 return 1;
843 } else if (this->IsUnion()) { 882 } else if (this->IsUnion()) {
844 int result = 0; 883 int result = 0;
845 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { 884 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
846 if (this->AsUnion()->Get(i)->IsConstant()) ++result; 885 if (this->AsUnion()->Get(i)->IsHeapConstant()) ++result;
847 } 886 }
848 return result; 887 return result;
849 } else { 888 } else {
850 return 0; 889 return 0;
851 } 890 }
852 } 891 }
853 892
854 // ----------------------------------------------------------------------------- 893 // -----------------------------------------------------------------------------
855 // Printing. 894 // Printing.
856 895
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 } 937 }
899 } 938 }
900 DCHECK(bits == 0); 939 DCHECK(bits == 0);
901 os << ")"; 940 os << ")";
902 } 941 }
903 942
904 void Type::PrintTo(std::ostream& os) { 943 void Type::PrintTo(std::ostream& os) {
905 DisallowHeapAllocation no_allocation; 944 DisallowHeapAllocation no_allocation;
906 if (this->IsBitset()) { 945 if (this->IsBitset()) {
907 BitsetType::Print(os, this->AsBitset()); 946 BitsetType::Print(os, this->AsBitset());
908 } else if (this->IsConstant()) { 947 } else if (this->IsHeapConstant()) {
909 os << "Constant(" << Brief(*this->AsConstant()->Value()) << ")"; 948 os << "HeapConstant(" << Brief(*this->AsHeapConstant()->Value()) << ")";
949 } else if (this->IsOtherNumberConstant()) {
950 os << "OtherNumberConstant(" << this->AsOtherNumberConstant()->Value()
951 << ")";
910 } else if (this->IsRange()) { 952 } else if (this->IsRange()) {
911 std::ostream::fmtflags saved_flags = os.setf(std::ios::fixed); 953 std::ostream::fmtflags saved_flags = os.setf(std::ios::fixed);
912 std::streamsize saved_precision = os.precision(0); 954 std::streamsize saved_precision = os.precision(0);
913 os << "Range(" << this->AsRange()->Min() << ", " << this->AsRange()->Max() 955 os << "Range(" << this->AsRange()->Min() << ", " << this->AsRange()->Max()
914 << ")"; 956 << ")";
915 os.flags(saved_flags); 957 os.flags(saved_flags);
916 os.precision(saved_precision); 958 os.precision(saved_precision);
917 } else if (this->IsUnion()) { 959 } else if (this->IsUnion()) {
918 os << "("; 960 os << "(";
919 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { 961 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
952 return i::SmiValuesAre31Bits() ? kSigned31 : kSigned32; 994 return i::SmiValuesAre31Bits() ? kSigned31 : kSigned32;
953 } 995 }
954 996
955 BitsetType::bitset BitsetType::UnsignedSmall() { 997 BitsetType::bitset BitsetType::UnsignedSmall() {
956 return i::SmiValuesAre31Bits() ? kUnsigned30 : kUnsigned31; 998 return i::SmiValuesAre31Bits() ? kUnsigned30 : kUnsigned31;
957 } 999 }
958 1000
959 } // namespace compiler 1001 } // namespace compiler
960 } // namespace internal 1002 } // namespace internal
961 } // namespace v8 1003 } // namespace v8
OLDNEW
« no previous file with comments | « src/compiler/types.h ('k') | test/cctest/test-types.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698