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
3 // found in the LICENSE file.
4
5 #include "src/compiler/type-hint-analyzer.h"
6
7 #include "src/assembler.h"
8 #include "src/code-stubs.h"
9 #include "src/compiler/type-hints.h"
10 #include "src/ic/ic-state.h"
11
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15
16 namespace {
17
18 // TODO(bmeurer): This detour via types is ugly.
ToHint(Type * type)19 BinaryOperationHints::Hint ToHint(Type* type) {
20 if (type->Is(Type::None())) return BinaryOperationHints::kNone;
21 if (type->Is(Type::SignedSmall())) return BinaryOperationHints::kSignedSmall;
22 if (type->Is(Type::Signed32())) return BinaryOperationHints::kSigned32;
23 if (type->Is(Type::Number())) return BinaryOperationHints::kNumber;
24 if (type->Is(Type::String())) return BinaryOperationHints::kString;
25 return BinaryOperationHints::kAny;
26 }
27
28 } // namespace
29
30
GetBinaryOperationHints(TypeFeedbackId id,BinaryOperationHints * hints) const31 bool TypeHintAnalysis::GetBinaryOperationHints(
32 TypeFeedbackId id, BinaryOperationHints* hints) const {
33 auto i = infos_.find(id);
34 if (i == infos_.end()) return false;
35 Handle<Code> code = i->second;
36 DCHECK_EQ(Code::BINARY_OP_IC, code->kind());
37 BinaryOpICState state(code->GetIsolate(), code->extra_ic_state());
38 *hints = BinaryOperationHints(ToHint(state.GetLeftType()),
39 ToHint(state.GetRightType()),
40 ToHint(state.GetResultType()));
41 return true;
42 }
43
44
GetToBooleanHints(TypeFeedbackId id,ToBooleanHints * hints) const45 bool TypeHintAnalysis::GetToBooleanHints(TypeFeedbackId id,
46 ToBooleanHints* hints) const {
47 auto i = infos_.find(id);
48 if (i == infos_.end()) return false;
49 Handle<Code> code = i->second;
50 DCHECK_EQ(Code::TO_BOOLEAN_IC, code->kind());
51 ToBooleanStub stub(code->GetIsolate(), code->extra_ic_state());
52 // TODO(bmeurer): Replace ToBooleanStub::Types with ToBooleanHints.
53 #define ASSERT_COMPATIBLE(NAME, Name) \
54 STATIC_ASSERT(1 << ToBooleanStub::NAME == \
55 static_cast<int>(ToBooleanHint::k##Name))
56 ASSERT_COMPATIBLE(UNDEFINED, Undefined);
57 ASSERT_COMPATIBLE(BOOLEAN, Boolean);
58 ASSERT_COMPATIBLE(NULL_TYPE, Null);
59 ASSERT_COMPATIBLE(SMI, SmallInteger);
60 ASSERT_COMPATIBLE(SPEC_OBJECT, Receiver);
61 ASSERT_COMPATIBLE(STRING, String);
62 ASSERT_COMPATIBLE(SYMBOL, Symbol);
63 ASSERT_COMPATIBLE(HEAP_NUMBER, HeapNumber);
64 ASSERT_COMPATIBLE(SIMD_VALUE, SimdValue);
65 #undef ASSERT_COMPATIBLE
66 *hints = ToBooleanHints(stub.types().ToIntegral());
67 return true;
68 }
69
70
Analyze(Handle<Code> code)71 TypeHintAnalysis* TypeHintAnalyzer::Analyze(Handle<Code> code) {
72 DisallowHeapAllocation no_gc;
73 TypeHintAnalysis::Infos infos(zone());
74 Isolate* const isolate = code->GetIsolate();
75 int const mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID);
76 for (RelocIterator it(*code, mask); !it.done(); it.next()) {
77 RelocInfo* rinfo = it.rinfo();
78 Address target_address = rinfo->target_address();
79 Code* target = Code::GetCodeFromTargetAddress(target_address);
80 switch (target->kind()) {
81 case Code::BINARY_OP_IC:
82 case Code::TO_BOOLEAN_IC: {
83 // Add this feedback to the {infos}.
84 TypeFeedbackId id(static_cast<unsigned>(rinfo->data()));
85 infos.insert(std::make_pair(id, handle(target, isolate)));
86 break;
87 }
88 default:
89 // Ignore the remaining code objects.
90 break;
91 }
92 }
93 return new (zone()) TypeHintAnalysis(infos);
94 }
95
96 } // namespace compiler
97 } // namespace internal
98 } // namespace v8
99