type-feedback-vector.h 14.7 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 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_H_
#define V8_TYPE_FEEDBACK_VECTOR_H_

8 9
#include <vector>

10 11 12 13 14
#include "src/checks.h"
#include "src/elements-kind.h"
#include "src/heap/heap.h"
#include "src/isolate.h"
#include "src/objects.h"
15
#include "src/zone-containers.h"
16 17 18 19

namespace v8 {
namespace internal {

20 21
class FeedbackVectorSpec {
 public:
22 23 24 25 26
  FeedbackVectorSpec() : slots_(0), ic_slots_(0), ic_kinds_(NULL) {}
  explicit FeedbackVectorSpec(int slots)
      : slots_(slots), ic_slots_(0), ic_kinds_(NULL) {}
  FeedbackVectorSpec(int slots, int ic_slots, Code::Kind* ic_slot_kinds)
      : slots_(slots), ic_slots_(ic_slots), ic_kinds_(ic_slot_kinds) {}
27 28 29

  int slots() const { return slots_; }

30
  int ic_slots() const { return ic_slots_; }
31 32

  Code::Kind GetKind(int ic_slot) const {
33 34
    DCHECK(ic_slots_ > 0 && ic_slot < ic_slots_);
    return ic_kinds_[ic_slot];
35 36
  }

37 38
 private:
  int slots_;
39 40
  int ic_slots_;
  Code::Kind* ic_kinds_;
41 42 43 44 45 46 47 48 49
};


class ZoneFeedbackVectorSpec {
 public:
  explicit ZoneFeedbackVectorSpec(Zone* zone)
      : slots_(0), ic_slots_(0), ic_slot_kinds_(zone) {}

  ZoneFeedbackVectorSpec(Zone* zone, int slots, int ic_slots)
50
      : slots_(slots), ic_slots_(ic_slots), ic_slot_kinds_(ic_slots, zone) {}
51

52 53 54 55 56 57
  int slots() const { return slots_; }
  void increase_slots(int count) { slots_ += count; }

  int ic_slots() const { return ic_slots_; }
  void increase_ic_slots(int count) {
    ic_slots_ += count;
58
    ic_slot_kinds_.resize(ic_slots_);
59 60 61 62 63 64 65 66 67 68 69 70 71
  }

  void SetKind(int ic_slot, Code::Kind kind) {
    ic_slot_kinds_[ic_slot] = kind;
  }

  Code::Kind GetKind(int ic_slot) const {
    return static_cast<Code::Kind>(ic_slot_kinds_.at(ic_slot));
  }

 private:
  int slots_;
  int ic_slots_;
72
  ZoneVector<unsigned char> ic_slot_kinds_;
73 74 75
};


76 77 78 79
// The shape of the TypeFeedbackVector is an array with:
// 0: first_ic_slot_index (== length() if no ic slots are present)
// 1: ics_with_types
// 2: ics_with_generic_info
80 81 82
// 3: type information for ic slots, if any
// ...
// N: first feedback slot (N >= 3)
83 84 85 86
// ...
// [<first_ic_slot_index>: feedback slot]
// ...to length() - 1
//
87 88 89
class TypeFeedbackVector : public FixedArray {
 public:
  // Casting.
90
  static inline TypeFeedbackVector* cast(Object* obj);
91

92 93 94 95 96
  static const int kReservedIndexCount = 3;
  static const int kFirstICSlotIndex = 0;
  static const int kWithTypesIndex = 1;
  static const int kGenericCountIndex = 2;

97
  static int elements_per_ic_slot() { return 2; }
98

99 100 101 102 103
  inline int first_ic_slot_index() const;
  inline int ic_with_type_info_count();
  inline void change_ic_with_type_info_count(int delta);
  inline int ic_generic_count();
  inline void change_ic_generic_count(int delta);
104 105
  inline int ic_metadata_length() const;

106 107
  bool SpecDiffersFrom(const ZoneFeedbackVectorSpec* other_spec) const;

108 109
  inline int Slots() const;
  inline int ICSlots() const;
110 111 112

  // Conversion from a slot or ic slot to an integer index to the underlying
  // array.
113 114
  inline int GetIndex(FeedbackVectorSlot slot) const;
  inline int GetIndex(FeedbackVectorICSlot slot) const;
115 116 117

  // Conversion from an integer index to either a slot or an ic slot. The caller
  // should know what kind she expects.
118 119 120 121 122 123 124 125
  inline FeedbackVectorSlot ToSlot(int index) const;
  inline FeedbackVectorICSlot ToICSlot(int index) const;
  inline Object* Get(FeedbackVectorSlot slot) const;
  inline void Set(FeedbackVectorSlot slot, Object* value,
                  WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
  inline Object* Get(FeedbackVectorICSlot slot) const;
  inline void Set(FeedbackVectorICSlot slot, Object* value,
                  WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
126

127
  // IC slots need metadata to recognize the type of IC.
128
  Code::Kind GetKind(FeedbackVectorICSlot slot) const;
129

130
  template <typename Spec>
131
  static Handle<TypeFeedbackVector> Allocate(Isolate* isolate,
132
                                             const Spec* spec);
133

134 135 136
  static Handle<TypeFeedbackVector> Copy(Isolate* isolate,
                                         Handle<TypeFeedbackVector> vector);

137 138 139 140 141 142 143
#ifdef OBJECT_PRINT
  // For gdb debugging.
  void Print();
#endif  // OBJECT_PRINT

  DECLARE_PRINTER(TypeFeedbackVector)

144
  // Clears the vector slots and the vector ic slots.
145 146 147 148 149
  void ClearSlots(SharedFunctionInfo* shared) { ClearSlotsImpl(shared, true); }
  void ClearSlotsAtGCTime(SharedFunctionInfo* shared) {
    ClearSlotsImpl(shared, false);
  }

150 151 152 153 154 155
  void ClearICSlots(SharedFunctionInfo* shared) {
    ClearICSlotsImpl(shared, true);
  }
  void ClearICSlotsAtGCTime(SharedFunctionInfo* shared) {
    ClearICSlotsImpl(shared, false);
  }
156

157 158 159
  static void ClearAllKeyedStoreICs(Isolate* isolate);
  void ClearKeyedStoreICs(SharedFunctionInfo* shared);

160 161 162 163 164 165 166 167 168 169 170 171 172
  // The object that indicates an uninitialized cache.
  static inline Handle<Object> UninitializedSentinel(Isolate* isolate);

  // The object that indicates a megamorphic state.
  static inline Handle<Object> MegamorphicSentinel(Isolate* isolate);

  // The object that indicates a premonomorphic state.
  static inline Handle<Object> PremonomorphicSentinel(Isolate* isolate);

  // A raw version of the uninitialized sentinel that's safe to read during
  // garbage collection (e.g., for patching the cache).
  static inline Object* RawUninitializedSentinel(Heap* heap);

173 174 175 176 177 178 179 180 181 182 183
  static const int kDummyLoadICSlot = 0;
  static const int kDummyKeyedLoadICSlot = 1;
  static const int kDummyStoreICSlot = 2;
  static const int kDummyKeyedStoreICSlot = 3;

  static Handle<TypeFeedbackVector> DummyVector(Isolate* isolate);
  static FeedbackVectorICSlot DummySlot(int dummyIndex) {
    DCHECK(dummyIndex >= 0 && dummyIndex <= kDummyKeyedStoreICSlot);
    return FeedbackVectorICSlot(dummyIndex);
  }

184
 private:
185 186 187 188
  enum VectorICKind {
    KindUnused = 0x0,
    KindCallIC = 0x1,
    KindLoadIC = 0x2,
189 190 191
    KindKeyedLoadIC = 0x3,
    KindStoreIC = 0x4,
    KindKeyedStoreIC = 0x5,
192 193
  };

194
  static const int kVectorICKindBits = 3;
195 196
  static VectorICKind FromCodeKind(Code::Kind kind);
  static Code::Kind FromVectorICKind(VectorICKind kind);
197 198
  void SetKind(FeedbackVectorICSlot slot, Code::Kind kind);

199 200 201
  typedef BitSetComputer<VectorICKind, kVectorICKindBits, kSmiValueSize,
                         uint32_t> VectorICComputer;

202
  void ClearSlotsImpl(SharedFunctionInfo* shared, bool force_clear);
203 204
  void ClearICSlotsImpl(SharedFunctionInfo* shared, bool force_clear);

205 206
  DISALLOW_IMPLICIT_CONSTRUCTORS(TypeFeedbackVector);
};
207 208


209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
// The following asserts protect an optimization in type feedback vector
// code that looks into the contents of a slot assuming to find a String,
// a Symbol, an AllocationSite, a WeakCell, or a FixedArray.
STATIC_ASSERT(WeakCell::kSize >= 2 * kPointerSize);
STATIC_ASSERT(WeakCell::kValueOffset == AllocationSite::kTransitionInfoOffset);
STATIC_ASSERT(WeakCell::kValueOffset == FixedArray::kLengthOffset);
STATIC_ASSERT(WeakCell::kValueOffset == Name::kHashFieldSlot);
// Verify that an empty hash field looks like a tagged object, but can't
// possibly be confused with a pointer.
STATIC_ASSERT((Name::kEmptyHashField & kHeapObjectTag) == kHeapObjectTag);
STATIC_ASSERT(Name::kEmptyHashField == 0x3);
// Verify that a set hash field will not look like a tagged object.
STATIC_ASSERT(Name::kHashNotComputedMask == kHeapObjectTag);


224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
// A FeedbackNexus is the combination of a TypeFeedbackVector and a slot.
// Derived classes customize the update and retrieval of feedback.
class FeedbackNexus {
 public:
  FeedbackNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
      : vector_handle_(vector), vector_(NULL), slot_(slot) {}
  FeedbackNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
      : vector_(vector), slot_(slot) {}
  virtual ~FeedbackNexus() {}

  Handle<TypeFeedbackVector> vector_handle() const {
    DCHECK(vector_ == NULL);
    return vector_handle_;
  }
  TypeFeedbackVector* vector() const {
    return vector_handle_.is_null() ? vector_ : *vector_handle_;
  }
  FeedbackVectorICSlot slot() const { return slot_; }

  InlineCacheState ic_state() const { return StateFromFeedback(); }
  Map* FindFirstMap() const {
    MapHandleList maps;
    ExtractMaps(&maps);
    if (maps.length() > 0) return *maps.at(0);
    return NULL;
  }

251 252 253
  // TODO(mvstanton): remove FindAllMaps, it didn't survive a code review.
  void FindAllMaps(MapHandleList* maps) const { ExtractMaps(maps); }

254
  virtual InlineCacheState StateFromFeedback() const = 0;
255 256 257
  virtual int ExtractMaps(MapHandleList* maps) const;
  virtual MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const;
  virtual bool FindHandlers(CodeHandleList* code_list, int length = -1) const;
258 259
  virtual Name* FindFirstName() const { return NULL; }

260 261 262 263
  virtual void ConfigureUninitialized();
  virtual void ConfigurePremonomorphic();
  virtual void ConfigureMegamorphic();

264 265
  inline Object* GetFeedback() const;
  inline Object* GetFeedbackExtra() const;
266 267

 protected:
268
  inline Isolate* GetIsolate() const;
269

270 271 272 273
  inline void SetFeedback(Object* feedback,
                          WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
  inline void SetFeedbackExtra(Object* feedback_extra,
                               WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
274

275
  Handle<FixedArray> EnsureArrayOfSize(int length);
276 277
  Handle<FixedArray> EnsureExtraArrayOfSize(int length);
  void InstallHandlers(Handle<FixedArray> array, MapHandleList* maps,
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                       CodeHandleList* handlers);

 private:
  // The reason for having a vector handle and a raw pointer is that we can and
  // should use handles during IC miss, but not during GC when we clear ICs. If
  // you have a handle to the vector that is better because more operations can
  // be done, like allocation.
  Handle<TypeFeedbackVector> vector_handle_;
  TypeFeedbackVector* vector_;
  FeedbackVectorICSlot slot_;
};


class CallICNexus : public FeedbackNexus {
 public:
293 294 295 296
  // Monomorphic call ics store call counts. Platform code needs to increment
  // the count appropriately (ie, by 2).
  static const int kCallCountIncrement = 2;

297 298 299 300 301 302 303 304 305
  CallICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::CALL_IC);
  }
  CallICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::CALL_IC);
  }

306 307
  void Clear(Code* host);

308 309 310
  void ConfigureMonomorphicArray();
  void ConfigureMonomorphic(Handle<JSFunction> function);

311
  InlineCacheState StateFromFeedback() const override;
312

313
  int ExtractMaps(MapHandleList* maps) const override {
314 315 316
    // CallICs don't record map feedback.
    return 0;
  }
317
  MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const override {
318 319
    return MaybeHandle<Code>();
  }
320
  bool FindHandlers(CodeHandleList* code_list, int length = -1) const override {
321 322
    return length == 0;
  }
323 324

  int ExtractCallCount();
325
};
326 327 328 329 330 331 332 333


class LoadICNexus : public FeedbackNexus {
 public:
  LoadICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::LOAD_IC);
  }
334 335 336 337
  explicit LoadICNexus(Isolate* isolate)
      : FeedbackNexus(TypeFeedbackVector::DummyVector(isolate),
                      TypeFeedbackVector::DummySlot(
                          TypeFeedbackVector::kDummyLoadICSlot)) {}
338 339 340 341 342 343 344
  LoadICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::LOAD_IC);
  }

  void Clear(Code* host);

345
  void ConfigureMonomorphic(Handle<Map> receiver_map, Handle<Code> handler);
346

347
  void ConfigurePolymorphic(MapHandleList* maps, CodeHandleList* handlers);
348

349
  InlineCacheState StateFromFeedback() const override;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
};


class KeyedLoadICNexus : public FeedbackNexus {
 public:
  KeyedLoadICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::KEYED_LOAD_IC);
  }
  KeyedLoadICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::KEYED_LOAD_IC);
  }

  void Clear(Code* host);

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  // name can be a null handle for element loads.
  void ConfigureMonomorphic(Handle<Name> name, Handle<Map> receiver_map,
                            Handle<Code> handler);
  // name can be null.
  void ConfigurePolymorphic(Handle<Name> name, MapHandleList* maps,
                            CodeHandleList* handlers);

  InlineCacheState StateFromFeedback() const override;
  Name* FindFirstName() const override;
};


class StoreICNexus : public FeedbackNexus {
 public:
  StoreICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::STORE_IC);
  }
384 385 386 387
  explicit StoreICNexus(Isolate* isolate)
      : FeedbackNexus(TypeFeedbackVector::DummyVector(isolate),
                      TypeFeedbackVector::DummySlot(
                          TypeFeedbackVector::kDummyStoreICSlot)) {}
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  StoreICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::STORE_IC);
  }

  void Clear(Code* host);

  void ConfigureMonomorphic(Handle<Map> receiver_map, Handle<Code> handler);

  void ConfigurePolymorphic(MapHandleList* maps, CodeHandleList* handlers);

  InlineCacheState StateFromFeedback() const override;
};


class KeyedStoreICNexus : public FeedbackNexus {
 public:
  KeyedStoreICNexus(Handle<TypeFeedbackVector> vector,
                    FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::KEYED_STORE_IC);
  }
410 411 412 413
  explicit KeyedStoreICNexus(Isolate* isolate)
      : FeedbackNexus(TypeFeedbackVector::DummyVector(isolate),
                      TypeFeedbackVector::DummySlot(
                          TypeFeedbackVector::kDummyKeyedStoreICSlot)) {}
414 415 416 417 418 419 420
  KeyedStoreICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
      : FeedbackNexus(vector, slot) {
    DCHECK(vector->GetKind(slot) == Code::KEYED_STORE_IC);
  }

  void Clear(Code* host);

421
  // name can be a null handle for element loads.
422
  void ConfigureMonomorphic(Handle<Name> name, Handle<Map> receiver_map,
423 424
                            Handle<Code> handler);
  // name can be null.
425
  void ConfigurePolymorphic(Handle<Name> name, MapHandleList* maps,
426
                            CodeHandleList* handlers);
427 428 429 430 431 432
  void ConfigurePolymorphic(MapHandleList* maps,
                            MapHandleList* transitioned_maps,
                            CodeHandleList* handlers);

  KeyedAccessStoreMode GetKeyedAccessStoreMode() const;
  IcCheckType GetKeyType() const;
433

434 435
  InlineCacheState StateFromFeedback() const override;
  Name* FindFirstName() const override;
436
};
437 438 439 440
}
}  // namespace v8::internal

#endif  // V8_TRANSITIONS_H_