stub-cache.cc 5.34 KB
Newer Older
1 2 3 4
// 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.

5
#include "src/ic/stub-cache.h"
6

7
#include "src/ast/ast.h"
8
#include "src/base/bits.h"
9
#include "src/heap/heap-inl.h"  // For InYoungGeneration().
10
#include "src/ic/ic-inl.h"
11
#include "src/logging/counters.h"
12
#include "src/objects/tagged-value-inl.h"
13 14 15 16

namespace v8 {
namespace internal {

17
StubCache::StubCache(Isolate* isolate) : isolate_(isolate) {
18
  // Ensure the nullptr (aka Smi::zero()) which StubCache::Get() returns
19
  // when the entry is not found is not considered as a handler.
20
  DCHECK(!IC::IsHandler(MaybeObject()));
21
}
22 23

void StubCache::Initialize() {
24 25
  DCHECK(base::bits::IsPowerOfTwo(kPrimaryTableSize));
  DCHECK(base::bits::IsPowerOfTwo(kSecondaryTableSize));
26 27 28
  Clear();
}

29 30
// Hash algorithm for the primary table. This algorithm is replicated in
// the AccessorAssembler.  Returns an index into the table that
31
// is scaled by 1 << kCacheIndexShift.
32
int StubCache::PrimaryOffset(Name name, Map map) {
33
  // Compute the hash of the name (use entire hash field).
34
  DCHECK(name.HasHashCode());
35
  uint32_t field = name.raw_hash_field();
36 37 38
  // Using only the low bits in 64-bit mode is unlikely to increase the
  // risk of collision even if the heap is spread over an area larger than
  // 4Gb (and not at all if it isn't).
39 40
  uint32_t map_low32bits =
      static_cast<uint32_t>(map.ptr() ^ (map.ptr() >> kMapKeyShift));
41
  // Base the offset on a simple combination of name and map.
42
  uint32_t key = map_low32bits + field;
43 44 45 46
  return key & ((kPrimaryTableSize - 1) << kCacheIndexShift);
}

// Hash algorithm for the secondary table.  This algorithm is replicated in
47 48 49 50
// assembler. This hash should be sufficiently different from the primary one
// in order to avoid collisions for minified code with short names.
// Returns an index into the table that is scaled by 1 << kCacheIndexShift.
int StubCache::SecondaryOffset(Name name, Map old_map) {
51
  uint32_t name_low32bits = static_cast<uint32_t>(name.ptr());
52 53 54
  uint32_t map_low32bits = static_cast<uint32_t>(old_map.ptr());
  uint32_t key = (map_low32bits + name_low32bits);
  key = key + (key >> kSecondaryKeyShift);
55 56 57
  return key & ((kSecondaryTableSize - 1) << kCacheIndexShift);
}

58
int StubCache::PrimaryOffsetForTesting(Name name, Map map) {
59 60 61
  return PrimaryOffset(name, map);
}

62 63
int StubCache::SecondaryOffsetForTesting(Name name, Map map) {
  return SecondaryOffset(name, map);
64 65
}

66 67
#ifdef DEBUG
namespace {
68

69
bool CommonStubCacheChecks(StubCache* stub_cache, Name name, Map map,
70
                           MaybeObject handler) {
71
  // Validate that the name and handler do not move on scavenge, and that we
72
  // can use identity checks instead of structural equality checks.
73 74
  DCHECK(!Heap::InYoungGeneration(name));
  DCHECK(!Heap::InYoungGeneration(handler));
75
  DCHECK(name.IsUniqueName());
76
  if (handler->ptr() != kNullAddress) DCHECK(IC::IsHandler(handler));
77
  return true;
78 79
}

80 81
}  // namespace
#endif
82

83
void StubCache::Set(Name name, Map map, MaybeObject handler) {
84
  DCHECK(CommonStubCacheChecks(this, name, map, handler));
85 86

  // Compute the primary entry.
87
  int primary_offset = PrimaryOffset(name, map);
88
  Entry* primary = entry(primary_, primary_offset);
89 90
  MaybeObject old_handler(
      TaggedValue::ToMaybeObject(isolate(), primary->value));
91 92
  // If the primary entry has useful data in it, we retire it to the
  // secondary cache before overwriting it.
93
  if (old_handler != MaybeObject::FromObject(
94
                         isolate()->builtins()->code(Builtin::kIllegal)) &&
95 96 97
      !primary->map.IsSmi()) {
    Map old_map =
        Map::cast(StrongTaggedValue::ToObject(isolate(), primary->map));
98 99 100
    Name old_name =
        Name::cast(StrongTaggedValue::ToObject(isolate(), primary->key));
    int secondary_offset = SecondaryOffset(old_name, old_map);
101 102 103 104 105
    Entry* secondary = entry(secondary_, secondary_offset);
    *secondary = *primary;
  }

  // Update primary cache.
106 107 108
  primary->key = StrongTaggedValue(name);
  primary->value = TaggedValue(handler);
  primary->map = StrongTaggedValue(map);
109 110 111
  isolate()->counters()->megamorphic_stub_cache_updates()->Increment();
}

112
MaybeObject StubCache::Get(Name name, Map map) {
113
  DCHECK(CommonStubCacheChecks(this, name, map, MaybeObject()));
114
  int primary_offset = PrimaryOffset(name, map);
115
  Entry* primary = entry(primary_, primary_offset);
116 117
  if (primary->key == name && primary->map == map) {
    return TaggedValue::ToMaybeObject(isolate(), primary->value);
118
  }
119
  int secondary_offset = SecondaryOffset(name, map);
120
  Entry* secondary = entry(secondary_, secondary_offset);
121 122
  if (secondary->key == name && secondary->map == map) {
    return TaggedValue::ToMaybeObject(isolate(), secondary->value);
123
  }
124
  return MaybeObject();
125 126 127
}

void StubCache::Clear() {
128
  MaybeObject empty =
129
      MaybeObject::FromObject(isolate_->builtins()->code(Builtin::kIllegal));
130
  Name empty_string = ReadOnlyRoots(isolate()).empty_string();
131
  for (int i = 0; i < kPrimaryTableSize; i++) {
132 133 134
    primary_[i].key = StrongTaggedValue(empty_string);
    primary_[i].map = StrongTaggedValue(Smi::zero());
    primary_[i].value = TaggedValue(empty);
135 136
  }
  for (int j = 0; j < kSecondaryTableSize; j++) {
137 138 139
    secondary_[j].key = StrongTaggedValue(empty_string);
    secondary_[j].map = StrongTaggedValue(Smi::zero());
    secondary_[j].value = TaggedValue(empty);
140 141 142
  }
}

143 144
}  // namespace internal
}  // namespace v8