type-feedback-vector-inl.h 5.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_TYPE_FEEDBACK_VECTOR_INL_H_
#define V8_TYPE_FEEDBACK_VECTOR_INL_H_

#include "src/type-feedback-vector.h"

namespace v8 {
namespace internal {

13 14 15 16

template <typename Derived>
FeedbackVectorSlot FeedbackVectorSpecBase<Derived>::AddSlot(
    FeedbackVectorSlotKind kind) {
17
  int slot = This()->slots();
18
  int entries_per_slot = TypeFeedbackMetadata::GetSlotSize(kind);
19
  This()->append(kind);
20
  for (int i = 1; i < entries_per_slot; i++) {
21
    This()->append(FeedbackVectorSlotKind::INVALID);
22 23 24 25 26
  }
  return FeedbackVectorSlot(slot);
}


27 28 29 30 31 32
// static
TypeFeedbackMetadata* TypeFeedbackMetadata::cast(Object* obj) {
  DCHECK(obj->IsTypeFeedbackVector());
  return reinterpret_cast<TypeFeedbackMetadata*>(obj);
}

33 34 35 36
bool TypeFeedbackMetadata::is_empty() const {
  if (length() == 0) return true;
  return false;
}
37 38 39 40 41 42 43 44

int TypeFeedbackMetadata::slot_count() const {
  if (length() == 0) return 0;
  DCHECK(length() > kReservedIndexCount);
  return Smi::cast(get(kSlotsCountIndex))->value();
}


45 46 47 48 49 50 51
// static
TypeFeedbackVector* TypeFeedbackVector::cast(Object* obj) {
  DCHECK(obj->IsTypeFeedbackVector());
  return reinterpret_cast<TypeFeedbackVector*>(obj);
}


52
int TypeFeedbackMetadata::GetSlotSize(FeedbackVectorSlotKind kind) {
53 54 55 56 57
  DCHECK_NE(FeedbackVectorSlotKind::INVALID, kind);
  DCHECK_NE(FeedbackVectorSlotKind::KINDS_NUMBER, kind);
  return kind == FeedbackVectorSlotKind::GENERAL ? 1 : 2;
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
bool TypeFeedbackMetadata::SlotRequiresName(FeedbackVectorSlotKind kind) {
  switch (kind) {
    case FeedbackVectorSlotKind::LOAD_GLOBAL_IC:
      return true;

    case FeedbackVectorSlotKind::CALL_IC:
    case FeedbackVectorSlotKind::LOAD_IC:
    case FeedbackVectorSlotKind::KEYED_LOAD_IC:
    case FeedbackVectorSlotKind::STORE_IC:
    case FeedbackVectorSlotKind::KEYED_STORE_IC:
    case FeedbackVectorSlotKind::GENERAL:
    case FeedbackVectorSlotKind::INVALID:
      return false;

    case FeedbackVectorSlotKind::KINDS_NUMBER:
      break;
  }
  UNREACHABLE();
  return false;
}
78 79 80

bool TypeFeedbackVector::is_empty() const {
  if (length() == 0) return true;
81
  DCHECK(length() > kReservedIndexCount);
82 83 84 85
  return false;
}


86
int TypeFeedbackVector::slot_count() const {
87
  if (length() == 0) return 0;
88 89 90 91 92 93 94 95 96 97
  DCHECK(length() > kReservedIndexCount);
  return length() - kReservedIndexCount;
}


TypeFeedbackMetadata* TypeFeedbackVector::metadata() const {
  return is_empty() ? TypeFeedbackMetadata::cast(GetHeap()->empty_fixed_array())
                    : TypeFeedbackMetadata::cast(get(kMetadataIndex));
}

mvstanton's avatar
mvstanton committed
98
// Conversion from an integer index to either a slot or an ic slot.
99 100 101
// static
FeedbackVectorSlot TypeFeedbackVector::ToSlot(int index) {
  DCHECK(index >= kReservedIndexCount);
102
  return FeedbackVectorSlot(index - kReservedIndexCount);
103 104 105 106 107 108 109 110 111 112 113 114 115 116
}


Object* TypeFeedbackVector::Get(FeedbackVectorSlot slot) const {
  return get(GetIndex(slot));
}


void TypeFeedbackVector::Set(FeedbackVectorSlot slot, Object* value,
                             WriteBarrierMode mode) {
  set(GetIndex(slot), value, mode);
}


117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
void TypeFeedbackVector::ComputeCounts(int* with_type_info, int* generic) {
  Object* uninitialized_sentinel =
      TypeFeedbackVector::RawUninitializedSentinel(GetIsolate());
  Object* megamorphic_sentinel =
      *TypeFeedbackVector::MegamorphicSentinel(GetIsolate());
  int with = 0;
  int gen = 0;
  TypeFeedbackMetadataIterator iter(metadata());
  while (iter.HasNext()) {
    FeedbackVectorSlot slot = iter.Next();
    FeedbackVectorSlotKind kind = iter.kind();

    Object* obj = Get(slot);
    if (obj != uninitialized_sentinel &&
        kind != FeedbackVectorSlotKind::GENERAL) {
      if (obj->IsWeakCell() || obj->IsFixedArray() || obj->IsString()) {
        with++;
      } else if (obj == megamorphic_sentinel) {
        gen++;
      }
    }
  }

  *with_type_info = with;
  *generic = gen;
}

144
Handle<Symbol> TypeFeedbackVector::UninitializedSentinel(Isolate* isolate) {
145 146 147
  return isolate->factory()->uninitialized_symbol();
}

148
Handle<Symbol> TypeFeedbackVector::MegamorphicSentinel(Isolate* isolate) {
149 150 151
  return isolate->factory()->megamorphic_symbol();
}

152
Handle<Symbol> TypeFeedbackVector::PremonomorphicSentinel(Isolate* isolate) {
153
  return isolate->factory()->premonomorphic_symbol();
154 155
}

156
Symbol* TypeFeedbackVector::RawUninitializedSentinel(Isolate* isolate) {
157
  return isolate->heap()->uninitialized_symbol();
158
}
159

160
bool TypeFeedbackMetadataIterator::HasNext() const {
161
  return next_slot_.ToInt() < metadata()->slot_count();
162 163 164 165
}

FeedbackVectorSlot TypeFeedbackMetadataIterator::Next() {
  DCHECK(HasNext());
166 167 168 169
  cur_slot_ = next_slot_;
  slot_kind_ = metadata()->GetKind(cur_slot_);
  next_slot_ = FeedbackVectorSlot(next_slot_.ToInt() + entry_size());
  return cur_slot_;
170 171 172 173 174
}

int TypeFeedbackMetadataIterator::entry_size() const {
  return TypeFeedbackMetadata::GetSlotSize(kind());
}
175 176 177 178 179

Object* FeedbackNexus::GetFeedback() const { return vector()->Get(slot()); }


Object* FeedbackNexus::GetFeedbackExtra() const {
180 181
#ifdef DEBUG
  FeedbackVectorSlotKind kind = vector()->GetKind(slot());
182
  DCHECK_LT(1, TypeFeedbackMetadata::GetSlotSize(kind));
183
#endif
184 185 186 187 188 189 190 191 192 193 194 195
  int extra_index = vector()->GetIndex(slot()) + 1;
  return vector()->get(extra_index);
}


void FeedbackNexus::SetFeedback(Object* feedback, WriteBarrierMode mode) {
  vector()->Set(slot(), feedback, mode);
}


void FeedbackNexus::SetFeedbackExtra(Object* feedback_extra,
                                     WriteBarrierMode mode) {
196 197
#ifdef DEBUG
  FeedbackVectorSlotKind kind = vector()->GetKind(slot());
198
  DCHECK_LT(1, TypeFeedbackMetadata::GetSlotSize(kind));
199
#endif
200 201 202 203 204 205
  int index = vector()->GetIndex(slot()) + 1;
  vector()->set(index, feedback_extra, mode);
}


Isolate* FeedbackNexus::GetIsolate() const { return vector()->GetIsolate(); }
206 207
}  // namespace internal
}  // namespace v8
208 209

#endif  // V8_TYPE_FEEDBACK_VECTOR_INL_H_