type-info.cc 15.7 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/type-info.h"
6

7
#include "src/ast/ast.h"
8 9
#include "src/code-stubs.h"
#include "src/compiler.h"
10
#include "src/ic/ic.h"
11
#include "src/ic/stub-cache.h"
12
#include "src/objects-inl.h"
13

14 15 16 17
namespace v8 {
namespace internal {


18
TypeFeedbackOracle::TypeFeedbackOracle(
19 20 21
    Isolate* isolate, Zone* zone, Handle<Code> code,
    Handle<TypeFeedbackVector> feedback_vector, Handle<Context> native_context)
    : native_context_(native_context), isolate_(isolate), zone_(zone) {
22
  BuildDictionary(code);
23
  DCHECK(dictionary_->IsUnseededNumberDictionary());
24 25 26 27
  // We make a copy of the feedback vector because a GC could clear
  // the type feedback info contained therein.
  // TODO(mvstanton): revisit the decision to copy when we weakly
  // traverse the feedback vector at GC time.
28
  feedback_vector_ = TypeFeedbackVector::Copy(isolate, feedback_vector);
29 30 31
}


32 33 34 35 36 37 38
static uint32_t IdToKey(TypeFeedbackId ast_id) {
  return static_cast<uint32_t>(ast_id.ToInt());
}


Handle<Object> TypeFeedbackOracle::GetInfo(TypeFeedbackId ast_id) {
  int entry = dictionary_->FindEntry(IdToKey(ast_id));
39 40
  if (entry != UnseededNumberDictionary::kNotFound) {
    Object* value = dictionary_->ValueAt(entry);
41 42
    if (value->IsCell()) {
      Cell* cell = Cell::cast(value);
43
      return Handle<Object>(cell->value(), isolate());
44
    } else {
45
      return Handle<Object>(value, isolate());
46 47
    }
  }
48
  return Handle<Object>::cast(isolate()->factory()->undefined_value());
49 50 51
}


52 53
Handle<Object> TypeFeedbackOracle::GetInfo(FeedbackVectorSlot slot) {
  DCHECK(slot.ToInt() >= 0 && slot.ToInt() < feedback_vector_->length());
54 55
  Handle<Object> undefined =
      Handle<Object>::cast(isolate()->factory()->undefined_value());
56
  Object* obj = feedback_vector_->Get(slot);
57

58 59
  // Slots do not embed direct pointers to maps, functions. Instead
  // a WeakCell is always used.
60 61 62 63 64 65
  if (obj->IsWeakCell()) {
    WeakCell* cell = WeakCell::cast(obj);
    if (cell->cleared()) return undefined;
    obj = cell->value();
  }

66
  if (obj->IsJSFunction() || obj->IsAllocationSite() || obj->IsSymbol() ||
67
      obj->IsSimd128Value()) {
68 69
    return Handle<Object>(obj, isolate());
  }
70

71
  return undefined;
72 73 74
}


75
InlineCacheState TypeFeedbackOracle::LoadInlineCacheState(
76
    FeedbackVectorSlot slot) {
77
  if (!slot.IsInvalid()) {
78 79
    FeedbackVectorSlotKind kind = feedback_vector_->GetKind(slot);
    if (kind == FeedbackVectorSlotKind::LOAD_IC) {
80 81
      LoadICNexus nexus(feedback_vector_, slot);
      return nexus.StateFromFeedback();
82
    } else if (kind == FeedbackVectorSlotKind::KEYED_LOAD_IC) {
83 84 85
      KeyedLoadICNexus nexus(feedback_vector_, slot);
      return nexus.StateFromFeedback();
    }
86 87
  }

88 89 90
  // If we can't find an IC, assume we've seen *something*, but we don't know
  // what. PREMONOMORPHIC roughly encodes this meaning.
  return PREMONOMORPHIC;
91 92 93
}


94
bool TypeFeedbackOracle::StoreIsUninitialized(FeedbackVectorSlot slot) {
95
  if (!slot.IsInvalid()) {
96 97
    FeedbackVectorSlotKind kind = feedback_vector_->GetKind(slot);
    if (kind == FeedbackVectorSlotKind::STORE_IC) {
98
      StoreICNexus nexus(feedback_vector_, slot);
99
      return nexus.StateFromFeedback() == UNINITIALIZED;
100
    } else if (kind == FeedbackVectorSlotKind::KEYED_STORE_IC) {
101
      KeyedStoreICNexus nexus(feedback_vector_, slot);
102
      return nexus.StateFromFeedback() == UNINITIALIZED;
103 104 105 106 107 108
    }
  }
  return true;
}


109
bool TypeFeedbackOracle::CallIsUninitialized(FeedbackVectorSlot slot) {
110
  Handle<Object> value = GetInfo(slot);
111
  return value->IsUndefined(isolate()) ||
112 113 114 115 116
         value.is_identical_to(
             TypeFeedbackVector::UninitializedSentinel(isolate()));
}


117
bool TypeFeedbackOracle::CallIsMonomorphic(FeedbackVectorSlot slot) {
118
  Handle<Object> value = GetInfo(slot);
119
  return value->IsAllocationSite() || value->IsJSFunction();
120 121 122
}


123
bool TypeFeedbackOracle::CallNewIsMonomorphic(FeedbackVectorSlot slot) {
124
  Handle<Object> info = GetInfo(slot);
125
  return info->IsAllocationSite() || info->IsJSFunction();
126 127 128
}


129
byte TypeFeedbackOracle::ForInType(FeedbackVectorSlot feedback_vector_slot) {
130
  Handle<Object> value = GetInfo(feedback_vector_slot);
131
  return value.is_identical_to(
132 133 134
             TypeFeedbackVector::UninitializedSentinel(isolate()))
             ? ForInStatement::FAST_FOR_IN
             : ForInStatement::SLOW_FOR_IN;
135 136 137
}


138
void TypeFeedbackOracle::GetStoreModeAndKeyType(
139
    FeedbackVectorSlot slot, KeyedAccessStoreMode* store_mode,
140
    IcCheckType* key_type) {
141
  if (!slot.IsInvalid() &&
142 143
      feedback_vector_->GetKind(slot) ==
          FeedbackVectorSlotKind::KEYED_STORE_IC) {
144 145 146 147 148 149
    KeyedStoreICNexus nexus(feedback_vector_, slot);
    *store_mode = nexus.GetKeyedAccessStoreMode();
    *key_type = nexus.GetKeyType();
  } else {
    *store_mode = STANDARD_STORE;
    *key_type = ELEMENT;
150 151 152 153
  }
}


154
Handle<JSFunction> TypeFeedbackOracle::GetCallTarget(FeedbackVectorSlot slot) {
155
  Handle<Object> info = GetInfo(slot);
156 157
  if (info->IsAllocationSite()) {
    return Handle<JSFunction>(isolate()->native_context()->array_function());
158
  }
159

160
  return Handle<JSFunction>::cast(info);
161 162 163
}


164 165
Handle<JSFunction> TypeFeedbackOracle::GetCallNewTarget(
    FeedbackVectorSlot slot) {
166
  Handle<Object> info = GetInfo(slot);
167
  if (info->IsJSFunction()) {
168 169
    return Handle<JSFunction>::cast(info);
  }
170

171
  DCHECK(info->IsAllocationSite());
172
  return Handle<JSFunction>(isolate()->native_context()->array_function());
173 174 175
}


176
Handle<AllocationSite> TypeFeedbackOracle::GetCallAllocationSite(
177
    FeedbackVectorSlot slot) {
178 179 180 181 182 183 184 185
  Handle<Object> info = GetInfo(slot);
  if (info->IsAllocationSite()) {
    return Handle<AllocationSite>::cast(info);
  }
  return Handle<AllocationSite>::null();
}


186 187
Handle<AllocationSite> TypeFeedbackOracle::GetCallNewAllocationSite(
    FeedbackVectorSlot slot) {
188
  Handle<Object> info = GetInfo(slot);
189
  if (info->IsAllocationSite()) {
190 191 192
    return Handle<AllocationSite>::cast(info);
  }
  return Handle<AllocationSite>::null();
193 194
}

195

196
void TypeFeedbackOracle::CompareType(TypeFeedbackId id,
197 198 199
                                     Type** left_type,
                                     Type** right_type,
                                     Type** combined_type) {
200
  Handle<Object> info = GetInfo(id);
201 202
  if (!info->IsCode()) {
    // For some comparisons we don't have ICs, e.g. LiteralCompareTypeof.
203
    *left_type = *right_type = *combined_type = Type::None();
204 205
    return;
  }
206 207 208 209
  Handle<Code> code = Handle<Code>::cast(info);

  Handle<Map> map;
  Map* raw_map = code->FindFirstMap();
210
  if (raw_map != NULL) Map::TryUpdate(handle(raw_map)).ToHandle(&map);
211

212
  if (code->is_compare_ic_stub()) {
213
    CompareICStub stub(code->stub_key(), isolate());
214 215 216
    *left_type = CompareICState::StateToType(zone(), stub.left());
    *right_type = CompareICState::StateToType(zone(), stub.right());
    *combined_type = CompareICState::StateToType(zone(), stub.state(), map);
217 218 219 220
  }
}


221
void TypeFeedbackOracle::BinaryType(TypeFeedbackId id,
222 223 224
                                    Type** left,
                                    Type** right,
                                    Type** result,
225
                                    Maybe<int>* fixed_right_arg,
226
                                    Handle<AllocationSite>* allocation_site,
227
                                    Token::Value op) {
228
  Handle<Object> object = GetInfo(id);
229
  if (!object->IsCode()) {
230
    // For some binary ops we don't have ICs, e.g. Token::COMMA, but for the
231
    // operations covered by the BinaryOpIC we should always have them.
232 233
    DCHECK(op < BinaryOpICState::FIRST_TOKEN ||
           op > BinaryOpICState::LAST_TOKEN);
234
    *left = *right = *result = Type::None();
235
    *fixed_right_arg = Nothing<int>();
236
    *allocation_site = Handle<AllocationSite>::null();
237 238
    return;
  }
239
  Handle<Code> code = Handle<Code>::cast(object);
240
  DCHECK_EQ(Code::BINARY_OP_IC, code->kind());
241
  BinaryOpICState state(isolate(), code->extra_ic_state());
242
  DCHECK_EQ(op, state.op());
243

244 245 246
  *left = state.GetLeftType();
  *right = state.GetRightType();
  *result = state.GetResultType();
247
  *fixed_right_arg = state.fixed_right_arg();
248 249 250 251 252 253 254

  AllocationSite* first_allocation_site = code->FindFirstAllocationSite();
  if (first_allocation_site != NULL) {
    *allocation_site = handle(first_allocation_site);
  } else {
    *allocation_site = Handle<AllocationSite>::null();
  }
255 256
}

257

258
Type* TypeFeedbackOracle::CountType(TypeFeedbackId id) {
259
  Handle<Object> object = GetInfo(id);
260
  if (!object->IsCode()) return Type::None();
261
  Handle<Code> code = Handle<Code>::cast(object);
262
  DCHECK_EQ(Code::BINARY_OP_IC, code->kind());
263
  BinaryOpICState state(isolate(), code->extra_ic_state());
264
  return state.GetLeftType();
265 266 267
}


268 269 270 271 272 273 274 275 276
bool TypeFeedbackOracle::HasOnlyStringMaps(SmallMapList* receiver_types) {
  bool all_strings = receiver_types->length() > 0;
  for (int i = 0; i < receiver_types->length(); i++) {
    all_strings &= receiver_types->at(i)->IsStringMap();
  }
  return all_strings;
}


277
void TypeFeedbackOracle::PropertyReceiverTypes(FeedbackVectorSlot slot,
278
                                               Handle<Name> name,
279 280
                                               SmallMapList* receiver_types) {
  receiver_types->Clear();
281 282 283 284 285
  if (!slot.IsInvalid()) {
    LoadICNexus nexus(feedback_vector_, slot);
    Code::Flags flags = Code::ComputeHandlerFlags(Code::LOAD_IC);
    CollectReceiverTypes(&nexus, name, flags, receiver_types);
  }
286 287 288 289
}


void TypeFeedbackOracle::KeyedPropertyReceiverTypes(
290
    FeedbackVectorSlot slot, SmallMapList* receiver_types, bool* is_string,
291
    IcCheckType* key_type) {
292
  receiver_types->Clear();
293 294 295 296 297
  if (slot.IsInvalid()) {
    *is_string = false;
    *key_type = ELEMENT;
  } else {
    KeyedLoadICNexus nexus(feedback_vector_, slot);
verwaest's avatar
verwaest committed
298
    CollectReceiverTypes(&nexus, receiver_types);
299
    *is_string = HasOnlyStringMaps(receiver_types);
300
    *key_type = nexus.GetKeyType();
301
  }
302 303 304
}


305
void TypeFeedbackOracle::AssignmentReceiverTypes(FeedbackVectorSlot slot,
306 307 308 309 310 311 312 313 314
                                                 Handle<Name> name,
                                                 SmallMapList* receiver_types) {
  receiver_types->Clear();
  Code::Flags flags = Code::ComputeHandlerFlags(Code::STORE_IC);
  CollectReceiverTypes(slot, name, flags, receiver_types);
}


void TypeFeedbackOracle::KeyedAssignmentReceiverTypes(
315
    FeedbackVectorSlot slot, SmallMapList* receiver_types,
316 317 318 319 320 321 322
    KeyedAccessStoreMode* store_mode, IcCheckType* key_type) {
  receiver_types->Clear();
  CollectReceiverTypes(slot, receiver_types);
  GetStoreModeAndKeyType(slot, store_mode, key_type);
}


323
void TypeFeedbackOracle::CountReceiverTypes(FeedbackVectorSlot slot,
324 325 326 327 328 329
                                            SmallMapList* receiver_types) {
  receiver_types->Clear();
  if (!slot.IsInvalid()) CollectReceiverTypes(slot, receiver_types);
}


330
void TypeFeedbackOracle::CollectReceiverTypes(FeedbackVectorSlot slot,
331 332 333 334
                                              Handle<Name> name,
                                              Code::Flags flags,
                                              SmallMapList* types) {
  StoreICNexus nexus(feedback_vector_, slot);
verwaest's avatar
verwaest committed
335
  CollectReceiverTypes(&nexus, name, flags, types);
336 337
}

verwaest's avatar
verwaest committed
338 339
void TypeFeedbackOracle::CollectReceiverTypes(FeedbackNexus* nexus,
                                              Handle<Name> name,
340 341
                                              Code::Flags flags,
                                              SmallMapList* types) {
342
  if (FLAG_collect_megamorphic_maps_from_stub_cache &&
verwaest's avatar
verwaest committed
343
      nexus->ic_state() == MEGAMORPHIC) {
344
    types->Reserve(4, zone());
345
    isolate()->stub_cache()->CollectMatchingMaps(
346 347
        types, name, flags, native_context_, zone());
  } else {
verwaest's avatar
verwaest committed
348
    CollectReceiverTypes(nexus, types);
349 350 351 352
  }
}


353
void TypeFeedbackOracle::CollectReceiverTypes(FeedbackVectorSlot slot,
354
                                              SmallMapList* types) {
355 356
  FeedbackVectorSlotKind kind = feedback_vector_->GetKind(slot);
  if (kind == FeedbackVectorSlotKind::STORE_IC) {
357
    StoreICNexus nexus(feedback_vector_, slot);
verwaest's avatar
verwaest committed
358
    CollectReceiverTypes(&nexus, types);
359
  } else {
360
    DCHECK_EQ(FeedbackVectorSlotKind::KEYED_STORE_IC, kind);
361
    KeyedStoreICNexus nexus(feedback_vector_, slot);
verwaest's avatar
verwaest committed
362
    CollectReceiverTypes(&nexus, types);
363
  }
364 365
}

verwaest's avatar
verwaest committed
366 367
void TypeFeedbackOracle::CollectReceiverTypes(FeedbackNexus* nexus,
                                              SmallMapList* types) {
368
  MapHandleList maps;
verwaest's avatar
verwaest committed
369 370
  if (nexus->ic_state() == MONOMORPHIC) {
    Map* map = nexus->FindFirstMap();
371
    if (map != NULL) maps.Add(handle(map));
verwaest's avatar
verwaest committed
372 373
  } else if (nexus->ic_state() == POLYMORPHIC) {
    nexus->FindAllMaps(&maps);
374 375
  } else {
    return;
376
  }
377 378 379
  types->Reserve(maps.length(), zone());
  for (int i = 0; i < maps.length(); i++) {
    Handle<Map> map(maps.at(i));
380 381
    if (IsRelevantFeedback(*map, *native_context_)) {
      types->AddMapIfMissing(maps.at(i), zone());
382
    }
383 384 385 386
  }
}


387
uint16_t TypeFeedbackOracle::ToBooleanTypes(TypeFeedbackId id) {
388
  Handle<Object> object = GetInfo(id);
389 390 391 392
  return object->IsCode() ? Handle<Code>::cast(object)->to_boolean_state() : 0;
}


393 394 395 396 397
// Things are a bit tricky here: The iterator for the RelocInfos and the infos
// themselves are not GC-safe, so we first get all infos, then we create the
// dictionary (possibly triggering GC), and finally we relocate the collected
// infos before we process them.
void TypeFeedbackOracle::BuildDictionary(Handle<Code> code) {
398
  DisallowHeapAllocation no_allocation;
399
  ZoneList<RelocInfo> infos(16, zone());
400
  HandleScope scope(isolate());
401 402 403 404 405
  GetRelocInfos(code, &infos);
  CreateDictionary(code, &infos);
  ProcessRelocInfos(&infos);
  // Allocate handle in the parent scope.
  dictionary_ = scope.CloseAndEscape(dictionary_);
406 407 408
}


409 410 411 412
void TypeFeedbackOracle::GetRelocInfos(Handle<Code> code,
                                       ZoneList<RelocInfo>* infos) {
  int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID);
  for (RelocIterator it(*code, mask); !it.done(); it.next()) {
413
    infos->Add(*it.rinfo(), zone());
414 415
  }
}
416 417


418 419
void TypeFeedbackOracle::CreateDictionary(Handle<Code> code,
                                          ZoneList<RelocInfo>* infos) {
420
  AllowHeapAllocation allocation_allowed;
421
  Code* old_code = *code;
422
  dictionary_ = UnseededNumberDictionary::New(isolate(), infos->length());
423
  RelocateRelocInfos(infos, old_code, *code);
424
}
425

426 427

void TypeFeedbackOracle::RelocateRelocInfos(ZoneList<RelocInfo>* infos,
428 429
                                            Code* old_code,
                                            Code* new_code) {
430 431
  for (int i = 0; i < infos->length(); i++) {
    RelocInfo* info = &(*infos)[i];
432 433 434
    info->set_host(new_code);
    info->set_pc(new_code->instruction_start() +
                 (info->pc() - old_code->instruction_start()));
435 436 437 438
  }
}


439 440
void TypeFeedbackOracle::ProcessRelocInfos(ZoneList<RelocInfo>* infos) {
  for (int i = 0; i < infos->length(); i++) {
441 442
    RelocInfo reloc_entry = (*infos)[i];
    Address target_address = reloc_entry.target_address();
443 444
    TypeFeedbackId ast_id =
        TypeFeedbackId(static_cast<unsigned>((*infos)[i].data()));
445 446
    Code* target = Code::GetCodeFromTargetAddress(target_address);
    switch (target->kind()) {
447 448
      case Code::LOAD_IC:
      case Code::STORE_IC:
449 450 451 452 453
      case Code::KEYED_LOAD_IC:
      case Code::KEYED_STORE_IC:
      case Code::BINARY_OP_IC:
      case Code::COMPARE_IC:
      case Code::TO_BOOLEAN_IC:
454
        SetInfo(ast_id, target);
455 456 457 458 459
        break;

      default:
        break;
    }
460 461 462
  }
}

463

464
void TypeFeedbackOracle::SetInfo(TypeFeedbackId ast_id, Object* target) {
465
  DCHECK(dictionary_->FindEntry(IdToKey(ast_id)) ==
466
         UnseededNumberDictionary::kNotFound);
467
  // Dictionary has been allocated with sufficient size for all elements.
468 469
  DisallowHeapAllocation no_need_to_resize_dictionary;
  HandleScope scope(isolate());
470 471
  USE(UnseededNumberDictionary::AtNumberPut(
      dictionary_, IdToKey(ast_id), handle(target, isolate())));
472 473
}

474

475 476
}  // namespace internal
}  // namespace v8