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
3 // found in the LICENSE file.
5 #include "src/ic/ic-state.h"
13 void ICUtility::Clear(Isolate* isolate, Address address,
14 Address constant_pool) {
15 IC::Clear(isolate, address, constant_pool);
19 CallICState::CallICState(ExtraICState extra_ic_state)
20 : argc_(ArgcBits::decode(extra_ic_state)),
21 call_type_(CallTypeBits::decode(extra_ic_state)) {}
24 ExtraICState CallICState::GetExtraICState() const {
25 ExtraICState extra_ic_state =
26 ArgcBits::encode(argc_) | CallTypeBits::encode(call_type_);
27 return extra_ic_state;
31 std::ostream& operator<<(std::ostream& os, const CallICState& s) {
32 return os << "(args(" << s.arg_count() << "), "
33 << (s.call_type() == CallICState::METHOD ? "METHOD" : "FUNCTION")
39 STATIC_CONST_MEMBER_DEFINITION const int BinaryOpICState::FIRST_TOKEN;
43 STATIC_CONST_MEMBER_DEFINITION const int BinaryOpICState::LAST_TOKEN;
46 BinaryOpICState::BinaryOpICState(Isolate* isolate, ExtraICState extra_ic_state)
48 HasFixedRightArgField::decode(extra_ic_state)
49 ? Just(1 << FixedRightArgValueField::decode(extra_ic_state))
53 static_cast<Token::Value>(FIRST_TOKEN + OpField::decode(extra_ic_state));
54 strong_ = StrengthField::decode(extra_ic_state);
55 left_kind_ = LeftKindField::decode(extra_ic_state);
56 right_kind_ = fixed_right_arg_.IsJust()
57 ? (Smi::IsValid(fixed_right_arg_.FromJust()) ? SMI : INT32)
58 : RightKindField::decode(extra_ic_state);
59 result_kind_ = ResultKindField::decode(extra_ic_state);
60 DCHECK_LE(FIRST_TOKEN, op_);
61 DCHECK_LE(op_, LAST_TOKEN);
65 ExtraICState BinaryOpICState::GetExtraICState() const {
66 ExtraICState extra_ic_state =
67 OpField::encode(op_ - FIRST_TOKEN) | LeftKindField::encode(left_kind_) |
68 ResultKindField::encode(result_kind_) | StrengthField::encode(strong_) |
69 HasFixedRightArgField::encode(fixed_right_arg_.IsJust());
70 if (fixed_right_arg_.IsJust()) {
71 extra_ic_state = FixedRightArgValueField::update(
72 extra_ic_state, WhichPowerOf2(fixed_right_arg_.FromJust()));
74 extra_ic_state = RightKindField::update(extra_ic_state, right_kind_);
76 return extra_ic_state;
81 void BinaryOpICState::GenerateAheadOfTime(
82 Isolate* isolate, void (*Generate)(Isolate*, const BinaryOpICState&)) {
83 // TODO(olivf) We should investigate why adding stubs to the snapshot is so
84 // expensive at runtime. When solved we should be able to add most binops to
85 // the snapshot instead of hand-picking them.
86 // Generated list of commonly used stubs
87 #define GENERATE(op, left_kind, right_kind, result_kind) \
89 BinaryOpICState state(isolate, op, Strength::WEAK); \
90 state.left_kind_ = left_kind; \
91 state.fixed_right_arg_ = Nothing<int>(); \
92 state.right_kind_ = right_kind; \
93 state.result_kind_ = result_kind; \
94 Generate(isolate, state); \
96 GENERATE(Token::ADD, INT32, INT32, INT32);
97 GENERATE(Token::ADD, INT32, INT32, NUMBER);
98 GENERATE(Token::ADD, INT32, NUMBER, NUMBER);
99 GENERATE(Token::ADD, INT32, SMI, INT32);
100 GENERATE(Token::ADD, NUMBER, INT32, NUMBER);
101 GENERATE(Token::ADD, NUMBER, NUMBER, NUMBER);
102 GENERATE(Token::ADD, NUMBER, SMI, NUMBER);
103 GENERATE(Token::ADD, SMI, INT32, INT32);
104 GENERATE(Token::ADD, SMI, INT32, NUMBER);
105 GENERATE(Token::ADD, SMI, NUMBER, NUMBER);
106 GENERATE(Token::ADD, SMI, SMI, INT32);
107 GENERATE(Token::ADD, SMI, SMI, SMI);
108 GENERATE(Token::BIT_AND, INT32, INT32, INT32);
109 GENERATE(Token::BIT_AND, INT32, INT32, SMI);
110 GENERATE(Token::BIT_AND, INT32, SMI, INT32);
111 GENERATE(Token::BIT_AND, INT32, SMI, SMI);
112 GENERATE(Token::BIT_AND, NUMBER, INT32, INT32);
113 GENERATE(Token::BIT_AND, NUMBER, SMI, SMI);
114 GENERATE(Token::BIT_AND, SMI, INT32, INT32);
115 GENERATE(Token::BIT_AND, SMI, INT32, SMI);
116 GENERATE(Token::BIT_AND, SMI, NUMBER, SMI);
117 GENERATE(Token::BIT_AND, SMI, SMI, SMI);
118 GENERATE(Token::BIT_OR, INT32, INT32, INT32);
119 GENERATE(Token::BIT_OR, INT32, INT32, SMI);
120 GENERATE(Token::BIT_OR, INT32, SMI, INT32);
121 GENERATE(Token::BIT_OR, INT32, SMI, SMI);
122 GENERATE(Token::BIT_OR, NUMBER, SMI, INT32);
123 GENERATE(Token::BIT_OR, NUMBER, SMI, SMI);
124 GENERATE(Token::BIT_OR, SMI, INT32, INT32);
125 GENERATE(Token::BIT_OR, SMI, INT32, SMI);
126 GENERATE(Token::BIT_OR, SMI, SMI, SMI);
127 GENERATE(Token::BIT_XOR, INT32, INT32, INT32);
128 GENERATE(Token::BIT_XOR, INT32, INT32, SMI);
129 GENERATE(Token::BIT_XOR, INT32, NUMBER, SMI);
130 GENERATE(Token::BIT_XOR, INT32, SMI, INT32);
131 GENERATE(Token::BIT_XOR, NUMBER, INT32, INT32);
132 GENERATE(Token::BIT_XOR, NUMBER, SMI, INT32);
133 GENERATE(Token::BIT_XOR, NUMBER, SMI, SMI);
134 GENERATE(Token::BIT_XOR, SMI, INT32, INT32);
135 GENERATE(Token::BIT_XOR, SMI, INT32, SMI);
136 GENERATE(Token::BIT_XOR, SMI, SMI, SMI);
137 GENERATE(Token::DIV, INT32, INT32, INT32);
138 GENERATE(Token::DIV, INT32, INT32, NUMBER);
139 GENERATE(Token::DIV, INT32, NUMBER, NUMBER);
140 GENERATE(Token::DIV, INT32, SMI, INT32);
141 GENERATE(Token::DIV, INT32, SMI, NUMBER);
142 GENERATE(Token::DIV, NUMBER, INT32, NUMBER);
143 GENERATE(Token::DIV, NUMBER, NUMBER, NUMBER);
144 GENERATE(Token::DIV, NUMBER, SMI, NUMBER);
145 GENERATE(Token::DIV, SMI, INT32, INT32);
146 GENERATE(Token::DIV, SMI, INT32, NUMBER);
147 GENERATE(Token::DIV, SMI, NUMBER, NUMBER);
148 GENERATE(Token::DIV, SMI, SMI, NUMBER);
149 GENERATE(Token::DIV, SMI, SMI, SMI);
150 GENERATE(Token::MOD, NUMBER, SMI, NUMBER);
151 GENERATE(Token::MOD, SMI, SMI, SMI);
152 GENERATE(Token::MUL, INT32, INT32, INT32);
153 GENERATE(Token::MUL, INT32, INT32, NUMBER);
154 GENERATE(Token::MUL, INT32, NUMBER, NUMBER);
155 GENERATE(Token::MUL, INT32, SMI, INT32);
156 GENERATE(Token::MUL, INT32, SMI, NUMBER);
157 GENERATE(Token::MUL, NUMBER, INT32, NUMBER);
158 GENERATE(Token::MUL, NUMBER, NUMBER, NUMBER);
159 GENERATE(Token::MUL, NUMBER, SMI, NUMBER);
160 GENERATE(Token::MUL, SMI, INT32, INT32);
161 GENERATE(Token::MUL, SMI, INT32, NUMBER);
162 GENERATE(Token::MUL, SMI, NUMBER, NUMBER);
163 GENERATE(Token::MUL, SMI, SMI, INT32);
164 GENERATE(Token::MUL, SMI, SMI, NUMBER);
165 GENERATE(Token::MUL, SMI, SMI, SMI);
166 GENERATE(Token::SAR, INT32, SMI, INT32);
167 GENERATE(Token::SAR, INT32, SMI, SMI);
168 GENERATE(Token::SAR, NUMBER, SMI, SMI);
169 GENERATE(Token::SAR, SMI, SMI, SMI);
170 GENERATE(Token::SHL, INT32, SMI, INT32);
171 GENERATE(Token::SHL, INT32, SMI, SMI);
172 GENERATE(Token::SHL, NUMBER, SMI, SMI);
173 GENERATE(Token::SHL, SMI, SMI, INT32);
174 GENERATE(Token::SHL, SMI, SMI, SMI);
175 GENERATE(Token::SHR, INT32, SMI, SMI);
176 GENERATE(Token::SHR, NUMBER, SMI, INT32);
177 GENERATE(Token::SHR, NUMBER, SMI, SMI);
178 GENERATE(Token::SHR, SMI, SMI, SMI);
179 GENERATE(Token::SUB, INT32, INT32, INT32);
180 GENERATE(Token::SUB, INT32, NUMBER, NUMBER);
181 GENERATE(Token::SUB, INT32, SMI, INT32);
182 GENERATE(Token::SUB, NUMBER, INT32, NUMBER);
183 GENERATE(Token::SUB, NUMBER, NUMBER, NUMBER);
184 GENERATE(Token::SUB, NUMBER, SMI, NUMBER);
185 GENERATE(Token::SUB, SMI, INT32, INT32);
186 GENERATE(Token::SUB, SMI, NUMBER, NUMBER);
187 GENERATE(Token::SUB, SMI, SMI, SMI);
189 #define GENERATE(op, left_kind, fixed_right_arg_value, result_kind) \
191 BinaryOpICState state(isolate, op, Strength::WEAK); \
192 state.left_kind_ = left_kind; \
193 state.fixed_right_arg_ = Just(fixed_right_arg_value); \
194 state.right_kind_ = SMI; \
195 state.result_kind_ = result_kind; \
196 Generate(isolate, state); \
198 GENERATE(Token::MOD, SMI, 2, SMI);
199 GENERATE(Token::MOD, SMI, 4, SMI);
200 GENERATE(Token::MOD, SMI, 8, SMI);
201 GENERATE(Token::MOD, SMI, 16, SMI);
202 GENERATE(Token::MOD, SMI, 32, SMI);
203 GENERATE(Token::MOD, SMI, 2048, SMI);
208 Type* BinaryOpICState::GetResultType(Zone* zone) const {
209 Kind result_kind = result_kind_;
210 if (HasSideEffects()) {
212 } else if (result_kind == GENERIC && op_ == Token::ADD) {
213 return Type::Union(Type::Number(zone), Type::String(zone), zone);
214 } else if (result_kind == NUMBER && op_ == Token::SHR) {
215 return Type::Unsigned32(zone);
217 DCHECK_NE(GENERIC, result_kind);
218 return KindToType(result_kind, zone);
222 std::ostream& operator<<(std::ostream& os, const BinaryOpICState& s) {
223 os << "(" << Token::Name(s.op_);
224 if (s.CouldCreateAllocationMementos()) os << "_CreateAllocationMementos";
225 if (is_strong(s.strength())) os << "_Strong";
226 os << ":" << BinaryOpICState::KindToString(s.left_kind_) << "*";
227 if (s.fixed_right_arg_.IsJust()) {
228 os << s.fixed_right_arg_.FromJust();
230 os << BinaryOpICState::KindToString(s.right_kind_);
232 return os << "->" << BinaryOpICState::KindToString(s.result_kind_) << ")";
236 void BinaryOpICState::Update(Handle<Object> left, Handle<Object> right,
237 Handle<Object> result) {
238 ExtraICState old_extra_ic_state = GetExtraICState();
240 left_kind_ = UpdateKind(left, left_kind_);
241 right_kind_ = UpdateKind(right, right_kind_);
243 int32_t fixed_right_arg_value = 0;
244 bool has_fixed_right_arg =
245 op_ == Token::MOD && right->ToInt32(&fixed_right_arg_value) &&
246 fixed_right_arg_value > 0 &&
247 base::bits::IsPowerOfTwo32(fixed_right_arg_value) &&
248 FixedRightArgValueField::is_valid(WhichPowerOf2(fixed_right_arg_value)) &&
249 (left_kind_ == SMI || left_kind_ == INT32) &&
250 (result_kind_ == NONE || !fixed_right_arg_.IsJust());
252 has_fixed_right_arg ? Just(fixed_right_arg_value) : Nothing<int32_t>();
253 result_kind_ = UpdateKind(result, result_kind_);
255 if (!Token::IsTruncatingBinaryOp(op_)) {
256 Kind input_kind = Max(left_kind_, right_kind_);
257 if (result_kind_ < input_kind && input_kind <= NUMBER) {
258 result_kind_ = input_kind;
262 // We don't want to distinguish INT32 and NUMBER for string add (because
263 // NumberToString can't make use of this anyway).
264 if (left_kind_ == STRING && right_kind_ == INT32) {
265 DCHECK_EQ(STRING, result_kind_);
266 DCHECK_EQ(Token::ADD, op_);
267 right_kind_ = NUMBER;
268 } else if (right_kind_ == STRING && left_kind_ == INT32) {
269 DCHECK_EQ(STRING, result_kind_);
270 DCHECK_EQ(Token::ADD, op_);
274 if (old_extra_ic_state == GetExtraICState()) {
275 // Tagged operations can lead to non-truncating HChanges
276 if (left->IsUndefined() || left->IsBoolean()) {
277 left_kind_ = GENERIC;
279 DCHECK(right->IsUndefined() || right->IsBoolean());
280 right_kind_ = GENERIC;
286 BinaryOpICState::Kind BinaryOpICState::UpdateKind(Handle<Object> object,
288 Kind new_kind = GENERIC;
289 bool is_truncating = Token::IsTruncatingBinaryOp(op());
290 if (object->IsBoolean() && is_truncating) {
291 // Booleans will be automatically truncated by HChange.
293 } else if (object->IsUndefined()) {
294 // Undefined will be automatically truncated by HChange.
295 new_kind = is_truncating ? INT32 : NUMBER;
296 } else if (object->IsSmi()) {
298 } else if (object->IsHeapNumber()) {
299 double value = Handle<HeapNumber>::cast(object)->value();
300 new_kind = IsInt32Double(value) ? INT32 : NUMBER;
301 } else if (object->IsString() && op() == Token::ADD) {
304 if (new_kind == INT32 && SmiValuesAre32Bits()) {
307 if (kind != NONE && ((new_kind <= NUMBER && kind > NUMBER) ||
308 (new_kind > NUMBER && kind <= NUMBER))) {
311 return Max(kind, new_kind);
316 const char* BinaryOpICState::KindToString(Kind kind) {
337 Type* BinaryOpICState::KindToType(Kind kind, Zone* zone) {
340 return Type::None(zone);
342 return Type::SignedSmall(zone);
344 return Type::Signed32(zone);
346 return Type::Number(zone);
348 return Type::String(zone);
350 return Type::Any(zone);
357 const char* CompareICState::GetStateName(State state) {
360 return "UNINITIALIZED";
365 case INTERNALIZED_STRING:
366 return "INTERNALIZED_STRING";
370 return "UNIQUE_NAME";
374 return "KNOWN_OBJECT";
383 Type* CompareICState::StateToType(Zone* zone, State state, Handle<Map> map) {
386 return Type::None(zone);
388 return Type::SignedSmall(zone);
390 return Type::Number(zone);
392 return Type::String(zone);
393 case INTERNALIZED_STRING:
394 return Type::InternalizedString(zone);
396 return Type::UniqueName(zone);
398 return Type::Receiver(zone);
400 return map.is_null() ? Type::Receiver(zone) : Type::Class(map, zone);
402 return Type::Any(zone);
409 CompareICState::State CompareICState::NewInputState(State old_state,
410 Handle<Object> value) {
413 if (value->IsSmi()) return SMI;
414 if (value->IsHeapNumber()) return NUMBER;
415 if (value->IsInternalizedString()) return INTERNALIZED_STRING;
416 if (value->IsString()) return STRING;
417 if (value->IsSymbol()) return UNIQUE_NAME;
418 if (value->IsJSObject()) return OBJECT;
421 if (value->IsSmi()) return SMI;
422 if (value->IsHeapNumber()) return NUMBER;
425 if (value->IsNumber()) return NUMBER;
427 case INTERNALIZED_STRING:
428 if (value->IsInternalizedString()) return INTERNALIZED_STRING;
429 if (value->IsString()) return STRING;
430 if (value->IsSymbol()) return UNIQUE_NAME;
433 if (value->IsString()) return STRING;
436 if (value->IsUniqueName()) return UNIQUE_NAME;
439 if (value->IsJSObject()) return OBJECT;
452 CompareICState::State CompareICState::TargetState(
453 State old_state, State old_left, State old_right, Token::Value op,
454 bool has_inlined_smi_code, Handle<Object> x, Handle<Object> y) {
457 if (x->IsSmi() && y->IsSmi()) return SMI;
458 if (x->IsNumber() && y->IsNumber()) return NUMBER;
459 if (Token::IsOrderedRelationalCompareOp(op)) {
460 // Ordered comparisons treat undefined as NaN, so the
461 // NUMBER stub will do the right thing.
462 if ((x->IsNumber() && y->IsUndefined()) ||
463 (y->IsNumber() && x->IsUndefined())) {
467 if (x->IsInternalizedString() && y->IsInternalizedString()) {
468 // We compare internalized strings as plain ones if we need to determine
469 // the order in a non-equality compare.
470 return Token::IsEqualityOp(op) ? INTERNALIZED_STRING : STRING;
472 if (x->IsString() && y->IsString()) return STRING;
473 if (!Token::IsEqualityOp(op)) return GENERIC;
474 if (x->IsUniqueName() && y->IsUniqueName()) return UNIQUE_NAME;
475 if (x->IsJSObject() && y->IsJSObject()) {
476 if (Handle<JSObject>::cast(x)->map() ==
477 Handle<JSObject>::cast(y)->map()) {
485 return x->IsNumber() && y->IsNumber() ? NUMBER : GENERIC;
486 case INTERNALIZED_STRING:
487 DCHECK(Token::IsEqualityOp(op));
488 if (x->IsString() && y->IsString()) return STRING;
489 if (x->IsUniqueName() && y->IsUniqueName()) return UNIQUE_NAME;
492 // If the failure was due to one side changing from smi to heap number,
493 // then keep the state (if other changed at the same time, we will get
494 // a second miss and then go to generic).
495 if (old_left == SMI && x->IsHeapNumber()) return NUMBER;
496 if (old_right == SMI && y->IsHeapNumber()) return NUMBER;
499 DCHECK(Token::IsEqualityOp(op));
500 if (x->IsJSObject() && y->IsJSObject()) {
511 return GENERIC; // Make the compiler happy.
513 } // namespace internal