Home | History | Annotate | Download | only in ic
      1 // Copyright 2012 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.
      4 
      5 #include "src/ic/stub-cache.h"
      6 
      7 #include "src/base/bits.h"
      8 #include "src/type-info.h"
      9 
     10 namespace v8 {
     11 namespace internal {
     12 
     13 
     14 StubCache::StubCache(Isolate* isolate) : isolate_(isolate) {}
     15 
     16 
     17 void StubCache::Initialize() {
     18   DCHECK(base::bits::IsPowerOfTwo32(kPrimaryTableSize));
     19   DCHECK(base::bits::IsPowerOfTwo32(kSecondaryTableSize));
     20   Clear();
     21 }
     22 
     23 
     24 static Code::Flags CommonStubCacheChecks(Name* name, Map* map,
     25                                          Code::Flags flags) {
     26   flags = Code::RemoveTypeAndHolderFromFlags(flags);
     27 
     28   // Validate that the name does not move on scavenge, and that we
     29   // can use identity checks instead of structural equality checks.
     30   DCHECK(!name->GetHeap()->InNewSpace(name));
     31   DCHECK(name->IsUniqueName());
     32 
     33   // The state bits are not important to the hash function because the stub
     34   // cache only contains handlers. Make sure that the bits are the least
     35   // significant so they will be the ones masked out.
     36   DCHECK_EQ(Code::HANDLER, Code::ExtractKindFromFlags(flags));
     37   STATIC_ASSERT((Code::ICStateField::kMask & 1) == 1);
     38 
     39   // Make sure that the code type and cache holder are not included in the hash.
     40   DCHECK(Code::ExtractTypeFromFlags(flags) == 0);
     41   DCHECK(Code::ExtractCacheHolderFromFlags(flags) == 0);
     42 
     43   return flags;
     44 }
     45 
     46 
     47 Code* StubCache::Set(Name* name, Map* map, Code* code) {
     48   Code::Flags flags = CommonStubCacheChecks(name, map, code->flags());
     49 
     50   // Compute the primary entry.
     51   int primary_offset = PrimaryOffset(name, flags, map);
     52   Entry* primary = entry(primary_, primary_offset);
     53   Code* old_code = primary->value;
     54 
     55   // If the primary entry has useful data in it, we retire it to the
     56   // secondary cache before overwriting it.
     57   if (old_code != isolate_->builtins()->builtin(Builtins::kIllegal)) {
     58     Map* old_map = primary->map;
     59     Code::Flags old_flags =
     60         Code::RemoveTypeAndHolderFromFlags(old_code->flags());
     61     int seed = PrimaryOffset(primary->key, old_flags, old_map);
     62     int secondary_offset = SecondaryOffset(primary->key, old_flags, seed);
     63     Entry* secondary = entry(secondary_, secondary_offset);
     64     *secondary = *primary;
     65   }
     66 
     67   // Update primary cache.
     68   primary->key = name;
     69   primary->value = code;
     70   primary->map = map;
     71   isolate()->counters()->megamorphic_stub_cache_updates()->Increment();
     72   return code;
     73 }
     74 
     75 
     76 Code* StubCache::Get(Name* name, Map* map, Code::Flags flags) {
     77   flags = CommonStubCacheChecks(name, map, flags);
     78   int primary_offset = PrimaryOffset(name, flags, map);
     79   Entry* primary = entry(primary_, primary_offset);
     80   if (primary->key == name && primary->map == map) {
     81     return primary->value;
     82   }
     83   int secondary_offset = SecondaryOffset(name, flags, primary_offset);
     84   Entry* secondary = entry(secondary_, secondary_offset);
     85   if (secondary->key == name && secondary->map == map) {
     86     return secondary->value;
     87   }
     88   return NULL;
     89 }
     90 
     91 
     92 void StubCache::Clear() {
     93   Code* empty = isolate_->builtins()->builtin(Builtins::kIllegal);
     94   for (int i = 0; i < kPrimaryTableSize; i++) {
     95     primary_[i].key = isolate()->heap()->empty_string();
     96     primary_[i].map = NULL;
     97     primary_[i].value = empty;
     98   }
     99   for (int j = 0; j < kSecondaryTableSize; j++) {
    100     secondary_[j].key = isolate()->heap()->empty_string();
    101     secondary_[j].map = NULL;
    102     secondary_[j].value = empty;
    103   }
    104 }
    105 
    106 
    107 void StubCache::CollectMatchingMaps(SmallMapList* types, Handle<Name> name,
    108                                     Code::Flags flags,
    109                                     Handle<Context> native_context,
    110                                     Zone* zone) {
    111   for (int i = 0; i < kPrimaryTableSize; i++) {
    112     if (primary_[i].key == *name) {
    113       Map* map = primary_[i].map;
    114       // Map can be NULL, if the stub is constant function call
    115       // with a primitive receiver.
    116       if (map == NULL) continue;
    117 
    118       int offset = PrimaryOffset(*name, flags, map);
    119       if (entry(primary_, offset) == &primary_[i] &&
    120           TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) {
    121         types->AddMapIfMissing(Handle<Map>(map), zone);
    122       }
    123     }
    124   }
    125 
    126   for (int i = 0; i < kSecondaryTableSize; i++) {
    127     if (secondary_[i].key == *name) {
    128       Map* map = secondary_[i].map;
    129       // Map can be NULL, if the stub is constant function call
    130       // with a primitive receiver.
    131       if (map == NULL) continue;
    132 
    133       // Lookup in primary table and skip duplicates.
    134       int primary_offset = PrimaryOffset(*name, flags, map);
    135 
    136       // Lookup in secondary table and add matches.
    137       int offset = SecondaryOffset(*name, flags, primary_offset);
    138       if (entry(secondary_, offset) == &secondary_[i] &&
    139           TypeFeedbackOracle::IsRelevantFeedback(map, *native_context)) {
    140         types->AddMapIfMissing(Handle<Map>(map), zone);
    141       }
    142     }
    143   }
    144 }
    145 }  // namespace internal
    146 }  // namespace v8
    147