allocation-site-inl.h 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2018 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_OBJECTS_ALLOCATION_SITE_INL_H_
#define V8_OBJECTS_ALLOCATION_SITE_INL_H_

#include "src/objects/allocation-site.h"

10
#include "src/heap/heap-write-barrier-inl.h"
11
#include "src/objects/js-objects-inl.h"
12 13 14 15 16 17 18

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

19 20
#include "torque-generated/src/objects/allocation-site-tq-inl.inc"

21
TQ_OBJECT_CONSTRUCTORS_IMPL(AllocationMemento)
22
OBJECT_CONSTRUCTORS_IMPL(AllocationSite, Struct)
23 24 25

NEVER_READ_ONLY_SPACE_IMPL(AllocationSite)

26
CAST_ACCESSOR(AllocationSite)
27 28 29

ACCESSORS(AllocationSite, transition_info_or_boilerplate, Object,
          kTransitionInfoOrBoilerplateOffset)
30 31
RELEASE_ACQUIRE_ACCESSORS(AllocationSite, transition_info_or_boilerplate,
                          Object, kTransitionInfoOrBoilerplateOffset)
32 33 34 35
ACCESSORS(AllocationSite, nested_site, Object, kNestedSiteOffset)
INT32_ACCESSORS(AllocationSite, pretenure_data, kPretenureDataOffset)
INT32_ACCESSORS(AllocationSite, pretenure_create_count,
                kPretenureCreateCountOffset)
36
ACCESSORS(AllocationSite, dependent_code, DependentCode, kDependentCodeOffset)
37 38 39 40
ACCESSORS_CHECKED(AllocationSite, weak_next, Object, kWeakNextOffset,
                  HasWeakNext())
ACCESSORS(AllocationMemento, allocation_site, Object, kAllocationSiteOffset)

41
JSObject AllocationSite::boilerplate() const {
42 43 44 45
  DCHECK(PointsToLiteral());
  return JSObject::cast(transition_info_or_boilerplate());
}

46 47 48 49 50 51 52 53
JSObject AllocationSite::boilerplate(AcquireLoadTag tag) const {
  DCHECK(PointsToLiteral());
  return JSObject::cast(transition_info_or_boilerplate(tag));
}

void AllocationSite::set_boilerplate(JSObject value, ReleaseStoreTag tag,
                                     WriteBarrierMode mode) {
  set_transition_info_or_boilerplate(value, tag, mode);
54 55 56 57
}

int AllocationSite::transition_info() const {
  DCHECK(!PointsToLiteral());
58
  return Smi::cast(transition_info_or_boilerplate()).value();
59 60 61 62 63 64 65 66 67 68 69 70
}

void AllocationSite::set_transition_info(int value) {
  DCHECK(!PointsToLiteral());
  set_transition_info_or_boilerplate(Smi::FromInt(value), SKIP_WRITE_BARRIER);
}

bool AllocationSite::HasWeakNext() const {
  return map() == GetReadOnlyRoots().allocation_site_map();
}

void AllocationSite::Initialize() {
71
  set_transition_info_or_boilerplate(Smi::zero());
72
  SetElementsKind(GetInitialFastElementsKind());
73
  set_nested_site(Smi::zero());
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  set_pretenure_data(0);
  set_pretenure_create_count(0);
  set_dependent_code(
      DependentCode::cast(GetReadOnlyRoots().empty_weak_fixed_array()),
      SKIP_WRITE_BARRIER);
}

bool AllocationSite::IsZombie() const {
  return pretenure_decision() == kZombie;
}

bool AllocationSite::IsMaybeTenure() const {
  return pretenure_decision() == kMaybeTenure;
}

bool AllocationSite::PretenuringDecisionMade() const {
  return pretenure_decision() != kUndecided;
}

void AllocationSite::MarkZombie() {
  DCHECK(!IsZombie());
  Initialize();
  set_pretenure_decision(kZombie);
}

ElementsKind AllocationSite::GetElementsKind() const {
  return ElementsKindBits::decode(transition_info());
}

void AllocationSite::SetElementsKind(ElementsKind kind) {
  set_transition_info(ElementsKindBits::update(transition_info(), kind));
}

bool AllocationSite::CanInlineCall() const {
  return DoNotInlineBit::decode(transition_info()) == 0;
}

void AllocationSite::SetDoNotInlineCall() {
  set_transition_info(DoNotInlineBit::update(transition_info(), true));
}

bool AllocationSite::PointsToLiteral() const {
116
  Object raw_value = transition_info_or_boilerplate();
117 118 119
  DCHECK_EQ(!raw_value.IsSmi(),
            raw_value.IsJSArray() || raw_value.IsJSObject());
  return !raw_value.IsSmi();
120 121 122 123 124
}

// Heuristic: We only need to create allocation site info if the boilerplate
// elements kind is the initial elements kind.
bool AllocationSite::ShouldTrack(ElementsKind boilerplate_elements_kind) {
125
  if (!V8_ALLOCATION_SITE_TRACKING_BOOL) return false;
126 127 128 129
  return IsSmiElementsKind(boilerplate_elements_kind);
}

inline bool AllocationSite::CanTrack(InstanceType type) {
130
  if (!V8_ALLOCATION_SITE_TRACKING_BOOL) return false;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  if (FLAG_allocation_site_pretenuring) {
    // TurboFan doesn't care at all about String pretenuring feedback,
    // so don't bother even trying to track that.
    return type == JS_ARRAY_TYPE || type == JS_OBJECT_TYPE;
  }
  return type == JS_ARRAY_TYPE;
}

AllocationSite::PretenureDecision AllocationSite::pretenure_decision() const {
  return PretenureDecisionBits::decode(pretenure_data());
}

void AllocationSite::set_pretenure_decision(PretenureDecision decision) {
  int32_t value = pretenure_data();
  set_pretenure_data(PretenureDecisionBits::update(value, decision));
}

bool AllocationSite::deopt_dependent_code() const {
  return DeoptDependentCodeBit::decode(pretenure_data());
}

void AllocationSite::set_deopt_dependent_code(bool deopt) {
  int32_t value = pretenure_data();
  set_pretenure_data(DeoptDependentCodeBit::update(value, deopt));
}

int AllocationSite::memento_found_count() const {
  return MementoFoundCountBits::decode(pretenure_data());
}

inline void AllocationSite::set_memento_found_count(int count) {
  int32_t value = pretenure_data();
  // Verify that we can count more mementos than we can possibly find in one
  // new space collection.
  DCHECK((GetHeap()->MaxSemiSpaceSize() /
166
          (Heap::kMinObjectSizeInTaggedWords * kTaggedSize +
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
           AllocationMemento::kSize)) < MementoFoundCountBits::kMax);
  DCHECK_LT(count, MementoFoundCountBits::kMax);
  set_pretenure_data(MementoFoundCountBits::update(value, count));
}

int AllocationSite::memento_create_count() const {
  return pretenure_create_count();
}

void AllocationSite::set_memento_create_count(int count) {
  set_pretenure_create_count(count);
}

bool AllocationSite::IncrementMementoFoundCount(int increment) {
  if (IsZombie()) return false;

  int value = memento_found_count();
  set_memento_found_count(value + increment);
  return memento_found_count() >= kPretenureMinimumCreated;
}

inline void AllocationSite::IncrementMementoCreateCount() {
  DCHECK(FLAG_allocation_site_pretenuring);
  int value = memento_create_count();
  set_memento_create_count(value + 1);
}

bool AllocationMemento::IsValid() const {
195 196
  return allocation_site().IsAllocationSite() &&
         !AllocationSite::cast(allocation_site()).IsZombie();
197 198
}

199
AllocationSite AllocationMemento::GetAllocationSite() const {
200 201 202 203 204
  DCHECK(IsValid());
  return AllocationSite::cast(allocation_site());
}

Address AllocationMemento::GetAllocationSiteUnchecked() const {
205
  return allocation_site().ptr();
206 207
}

208 209 210 211 212 213
template <AllocationSiteUpdateMode update_or_check>
bool AllocationSite::DigestTransitionFeedback(Handle<AllocationSite> site,
                                              ElementsKind to_kind) {
  Isolate* isolate = site->GetIsolate();
  bool result = false;

214
  if (site->PointsToLiteral() && site->boilerplate().IsJSArray()) {
215 216 217 218 219 220 221 222 223 224
    Handle<JSArray> boilerplate(JSArray::cast(site->boilerplate()), isolate);
    ElementsKind kind = boilerplate->GetElementsKind();
    // if kind is holey ensure that to_kind is as well.
    if (IsHoleyElementsKind(kind)) {
      to_kind = GetHoleyElementsKind(to_kind);
    }
    if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
      // If the array is huge, it's not likely to be defined in a local
      // function, so we shouldn't make new instances of it very often.
      uint32_t length = 0;
225
      CHECK(boilerplate->length().ToArrayLength(&length));
226 227 228 229 230 231 232 233 234 235 236 237
      if (length <= kMaximumArrayBytesToPretransition) {
        if (update_or_check == AllocationSiteUpdateMode::kCheckOnly) {
          return true;
        }
        if (FLAG_trace_track_allocation_sites) {
          bool is_nested = site->IsNested();
          PrintF("AllocationSite: JSArray %p boilerplate %supdated %s->%s\n",
                 reinterpret_cast<void*>(site->ptr()),
                 is_nested ? "(nested)" : " ", ElementsKindToString(kind),
                 ElementsKindToString(to_kind));
        }
        JSObject::TransitionElementsKind(boilerplate, to_kind);
238
        site->dependent_code().DeoptimizeDependentCodeGroup(
239
            DependentCode::kAllocationSiteTransitionChangedGroup);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        result = true;
      }
    }
  } else {
    // The AllocationSite is for a constructed Array.
    ElementsKind kind = site->GetElementsKind();
    // if kind is holey ensure that to_kind is as well.
    if (IsHoleyElementsKind(kind)) {
      to_kind = GetHoleyElementsKind(to_kind);
    }
    if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
      if (update_or_check == AllocationSiteUpdateMode::kCheckOnly) return true;
      if (FLAG_trace_track_allocation_sites) {
        PrintF("AllocationSite: JSArray %p site updated %s->%s\n",
               reinterpret_cast<void*>(site->ptr()), ElementsKindToString(kind),
               ElementsKindToString(to_kind));
      }
      site->SetElementsKind(to_kind);
258
      site->dependent_code().DeoptimizeDependentCodeGroup(
259
          DependentCode::kAllocationSiteTransitionChangedGroup);
260 261 262 263 264 265
      result = true;
    }
  }
  return result;
}

266 267 268 269 270 271
}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_ALLOCATION_SITE_INL_H_