OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 "src/compiler/type-hint-analyzer.h" | 5 #include "src/compiler/type-hint-analyzer.h" |
6 | 6 |
7 #include "src/assembler.h" | 7 #include "src/assembler.h" |
8 #include "src/code-stubs.h" | 8 #include "src/code-stubs.h" |
9 #include "src/compiler/type-hints.h" | 9 #include "src/compiler/type-hints.h" |
10 #include "src/ic/ic-state.h" | 10 #include "src/ic/ic-state.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 namespace compiler { | 14 namespace compiler { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // TODO(bmeurer): This detour via types is ugly. | 18 // TODO(bmeurer): This detour via types is ugly. |
19 BinaryOperationHints::Hint ToHint(Type* type) { | 19 BinaryOperationHints::Hint ToBinaryOperationHint(Type* type) { |
20 if (type->Is(Type::None())) return BinaryOperationHints::kNone; | 20 if (type->Is(Type::None())) return BinaryOperationHints::kNone; |
21 if (type->Is(Type::SignedSmall())) return BinaryOperationHints::kSignedSmall; | 21 if (type->Is(Type::SignedSmall())) return BinaryOperationHints::kSignedSmall; |
22 if (type->Is(Type::Signed32())) return BinaryOperationHints::kSigned32; | 22 if (type->Is(Type::Signed32())) return BinaryOperationHints::kSigned32; |
23 if (type->Is(Type::Number())) return BinaryOperationHints::kNumberOrUndefined; | 23 if (type->Is(Type::Number())) return BinaryOperationHints::kNumberOrUndefined; |
24 if (type->Is(Type::String())) return BinaryOperationHints::kString; | 24 if (type->Is(Type::String())) return BinaryOperationHints::kString; |
25 return BinaryOperationHints::kAny; | 25 return BinaryOperationHints::kAny; |
26 } | 26 } |
27 | 27 |
| 28 CompareOperationHints::Hint ToCompareOperationHint( |
| 29 CompareICState::State state) { |
| 30 switch (state) { |
| 31 case CompareICState::UNINITIALIZED: |
| 32 return CompareOperationHints::kNone; |
| 33 case CompareICState::BOOLEAN: |
| 34 return CompareOperationHints::kBoolean; |
| 35 case CompareICState::SMI: |
| 36 return CompareOperationHints::kSignedSmall; |
| 37 case CompareICState::NUMBER: |
| 38 return CompareOperationHints::kNumber; |
| 39 case CompareICState::STRING: |
| 40 return CompareOperationHints::kString; |
| 41 case CompareICState::INTERNALIZED_STRING: |
| 42 return CompareOperationHints::kInternalizedString; |
| 43 case CompareICState::UNIQUE_NAME: |
| 44 return CompareOperationHints::kUniqueName; |
| 45 case CompareICState::RECEIVER: |
| 46 case CompareICState::KNOWN_RECEIVER: |
| 47 return CompareOperationHints::kReceiver; |
| 48 case CompareICState::GENERIC: |
| 49 return CompareOperationHints::kAny; |
| 50 } |
| 51 UNREACHABLE(); |
| 52 return CompareOperationHints::kAny; |
| 53 } |
| 54 |
28 } // namespace | 55 } // namespace |
29 | 56 |
30 | |
31 bool TypeHintAnalysis::GetBinaryOperationHints( | 57 bool TypeHintAnalysis::GetBinaryOperationHints( |
32 TypeFeedbackId id, BinaryOperationHints* hints) const { | 58 TypeFeedbackId id, BinaryOperationHints* hints) const { |
33 auto i = infos_.find(id); | 59 auto i = infos_.find(id); |
34 if (i == infos_.end()) return false; | 60 if (i == infos_.end()) return false; |
35 Handle<Code> code = i->second; | 61 Handle<Code> code = i->second; |
36 DCHECK_EQ(Code::BINARY_OP_IC, code->kind()); | 62 DCHECK_EQ(Code::BINARY_OP_IC, code->kind()); |
37 BinaryOpICState state(code->GetIsolate(), code->extra_ic_state()); | 63 BinaryOpICState state(code->GetIsolate(), code->extra_ic_state()); |
38 *hints = BinaryOperationHints(ToHint(state.GetLeftType()), | 64 *hints = BinaryOperationHints(ToBinaryOperationHint(state.GetLeftType()), |
39 ToHint(state.GetRightType()), | 65 ToBinaryOperationHint(state.GetRightType()), |
40 ToHint(state.GetResultType())); | 66 ToBinaryOperationHint(state.GetResultType())); |
41 return true; | 67 return true; |
42 } | 68 } |
43 | 69 |
| 70 bool TypeHintAnalysis::GetCompareOperationHints( |
| 71 TypeFeedbackId id, CompareOperationHints* hints) const { |
| 72 auto i = infos_.find(id); |
| 73 if (i == infos_.end()) return false; |
| 74 Handle<Code> code = i->second; |
| 75 DCHECK_EQ(Code::COMPARE_IC, code->kind()); |
| 76 |
| 77 Handle<Map> map; |
| 78 Map* raw_map = code->FindFirstMap(); |
| 79 if (raw_map != nullptr) Map::TryUpdate(handle(raw_map)).ToHandle(&map); |
| 80 |
| 81 CompareICStub stub(code->stub_key(), code->GetIsolate()); |
| 82 *hints = CompareOperationHints(ToCompareOperationHint(stub.left()), |
| 83 ToCompareOperationHint(stub.right()), |
| 84 ToCompareOperationHint(stub.state())); |
| 85 return true; |
| 86 } |
44 | 87 |
45 bool TypeHintAnalysis::GetToBooleanHints(TypeFeedbackId id, | 88 bool TypeHintAnalysis::GetToBooleanHints(TypeFeedbackId id, |
46 ToBooleanHints* hints) const { | 89 ToBooleanHints* hints) const { |
47 auto i = infos_.find(id); | 90 auto i = infos_.find(id); |
48 if (i == infos_.end()) return false; | 91 if (i == infos_.end()) return false; |
49 Handle<Code> code = i->second; | 92 Handle<Code> code = i->second; |
50 DCHECK_EQ(Code::TO_BOOLEAN_IC, code->kind()); | 93 DCHECK_EQ(Code::TO_BOOLEAN_IC, code->kind()); |
51 ToBooleanICStub stub(code->GetIsolate(), code->extra_ic_state()); | 94 ToBooleanICStub stub(code->GetIsolate(), code->extra_ic_state()); |
52 // TODO(bmeurer): Replace ToBooleanICStub::Types with ToBooleanHints. | 95 // TODO(bmeurer): Replace ToBooleanICStub::Types with ToBooleanHints. |
53 #define ASSERT_COMPATIBLE(NAME, Name) \ | 96 #define ASSERT_COMPATIBLE(NAME, Name) \ |
54 STATIC_ASSERT(1 << ToBooleanICStub::NAME == \ | 97 STATIC_ASSERT(1 << ToBooleanICStub::NAME == \ |
55 static_cast<int>(ToBooleanHint::k##Name)) | 98 static_cast<int>(ToBooleanHint::k##Name)) |
56 ASSERT_COMPATIBLE(UNDEFINED, Undefined); | 99 ASSERT_COMPATIBLE(UNDEFINED, Undefined); |
57 ASSERT_COMPATIBLE(BOOLEAN, Boolean); | 100 ASSERT_COMPATIBLE(BOOLEAN, Boolean); |
58 ASSERT_COMPATIBLE(NULL_TYPE, Null); | 101 ASSERT_COMPATIBLE(NULL_TYPE, Null); |
59 ASSERT_COMPATIBLE(SMI, SmallInteger); | 102 ASSERT_COMPATIBLE(SMI, SmallInteger); |
60 ASSERT_COMPATIBLE(SPEC_OBJECT, Receiver); | 103 ASSERT_COMPATIBLE(SPEC_OBJECT, Receiver); |
61 ASSERT_COMPATIBLE(STRING, String); | 104 ASSERT_COMPATIBLE(STRING, String); |
62 ASSERT_COMPATIBLE(SYMBOL, Symbol); | 105 ASSERT_COMPATIBLE(SYMBOL, Symbol); |
63 ASSERT_COMPATIBLE(HEAP_NUMBER, HeapNumber); | 106 ASSERT_COMPATIBLE(HEAP_NUMBER, HeapNumber); |
64 ASSERT_COMPATIBLE(SIMD_VALUE, SimdValue); | 107 ASSERT_COMPATIBLE(SIMD_VALUE, SimdValue); |
65 #undef ASSERT_COMPATIBLE | 108 #undef ASSERT_COMPATIBLE |
66 *hints = ToBooleanHints(stub.types().ToIntegral()); | 109 *hints = ToBooleanHints(stub.types().ToIntegral()); |
67 return true; | 110 return true; |
68 } | 111 } |
69 | 112 |
70 | |
71 TypeHintAnalysis* TypeHintAnalyzer::Analyze(Handle<Code> code) { | 113 TypeHintAnalysis* TypeHintAnalyzer::Analyze(Handle<Code> code) { |
72 DisallowHeapAllocation no_gc; | 114 DisallowHeapAllocation no_gc; |
73 TypeHintAnalysis::Infos infos(zone()); | 115 TypeHintAnalysis::Infos infos(zone()); |
74 Isolate* const isolate = code->GetIsolate(); | 116 Isolate* const isolate = code->GetIsolate(); |
75 int const mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID); | 117 int const mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID); |
76 for (RelocIterator it(*code, mask); !it.done(); it.next()) { | 118 for (RelocIterator it(*code, mask); !it.done(); it.next()) { |
77 RelocInfo* rinfo = it.rinfo(); | 119 RelocInfo* rinfo = it.rinfo(); |
78 Address target_address = rinfo->target_address(); | 120 Address target_address = rinfo->target_address(); |
79 Code* target = Code::GetCodeFromTargetAddress(target_address); | 121 Code* target = Code::GetCodeFromTargetAddress(target_address); |
80 switch (target->kind()) { | 122 switch (target->kind()) { |
81 case Code::BINARY_OP_IC: | 123 case Code::BINARY_OP_IC: |
| 124 case Code::COMPARE_IC: |
82 case Code::TO_BOOLEAN_IC: { | 125 case Code::TO_BOOLEAN_IC: { |
83 // Add this feedback to the {infos}. | 126 // Add this feedback to the {infos}. |
84 TypeFeedbackId id(static_cast<unsigned>(rinfo->data())); | 127 TypeFeedbackId id(static_cast<unsigned>(rinfo->data())); |
85 infos.insert(std::make_pair(id, handle(target, isolate))); | 128 infos.insert(std::make_pair(id, handle(target, isolate))); |
86 break; | 129 break; |
87 } | 130 } |
88 default: | 131 default: |
89 // Ignore the remaining code objects. | 132 // Ignore the remaining code objects. |
90 break; | 133 break; |
91 } | 134 } |
92 } | 135 } |
93 return new (zone()) TypeHintAnalysis(infos); | 136 return new (zone()) TypeHintAnalysis(infos, zone()); |
94 } | 137 } |
95 | 138 |
96 } // namespace compiler | 139 } // namespace compiler |
97 } // namespace internal | 140 } // namespace internal |
98 } // namespace v8 | 141 } // namespace v8 |
OLD | NEW |