pending-optimization-table.cc 4.83 KB
Newer Older
1 2 3 4 5 6
// Copyright 2019 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.

#include "src/codegen/pending-optimization-table.h"

7
#include "src/base/flags.h"
8 9 10 11 12 13 14 15
#include "src/execution/isolate-inl.h"
#include "src/heap/heap-inl.h"
#include "src/objects/hash-table.h"
#include "src/objects/js-objects.h"

namespace v8 {
namespace internal {

16 17 18 19 20 21 22
enum class FunctionStatus : int {
  kPrepareForOptimize = 1 << 0,
  kMarkForOptimize = 1 << 1,
  kAllowHeuristicOptimization = 1 << 2,
};

using FunctionStatusFlags = base::Flags<FunctionStatus>;
23 24

void PendingOptimizationTable::PreparedForOptimization(
25 26
    Isolate* isolate, Handle<JSFunction> function,
    bool allow_heuristic_optimization) {
27 28
  DCHECK(FLAG_testing_d8_test_runner);

29 30 31 32 33
  FunctionStatusFlags status = FunctionStatus::kPrepareForOptimize;
  if (allow_heuristic_optimization) {
    status |= FunctionStatus::kAllowHeuristicOptimization;
  }

34 35 36 37 38 39 40
  Handle<ObjectHashTable> table =
      isolate->heap()->pending_optimize_for_test_bytecode().IsUndefined()
          ? ObjectHashTable::New(isolate, 1)
          : handle(ObjectHashTable::cast(
                       isolate->heap()->pending_optimize_for_test_bytecode()),
                   isolate);
  Handle<Tuple2> tuple = isolate->factory()->NewTuple2(
41
      handle(function->shared().GetBytecodeArray(isolate), isolate),
42
      handle(Smi::FromInt(status), isolate), AllocationType::kYoung);
43 44 45 46 47
  table =
      ObjectHashTable::Put(table, handle(function->shared(), isolate), tuple);
  isolate->heap()->SetPendingOptimizeForTestBytecode(*table);
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
bool PendingOptimizationTable::IsHeuristicOptimizationAllowed(
    Isolate* isolate, JSFunction function) {
  DCHECK(FLAG_testing_d8_test_runner);

  Handle<Object> table =
      handle(isolate->heap()->pending_optimize_for_test_bytecode(), isolate);
  Handle<Object> entry =
      table->IsUndefined()
          ? handle(ReadOnlyRoots(isolate).the_hole_value(), isolate)
          : handle(Handle<ObjectHashTable>::cast(table)->Lookup(
                       handle(function.shared(), isolate)),
                   isolate);
  if (entry->IsTheHole()) {
    return true;
  }
  DCHECK(entry->IsTuple2());
  DCHECK(Handle<Tuple2>::cast(entry)->value2().IsSmi());
  FunctionStatusFlags status(Smi::ToInt(Handle<Tuple2>::cast(entry)->value2()));
  return status & FunctionStatus::kAllowHeuristicOptimization;
}

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
void PendingOptimizationTable::MarkedForOptimization(
    Isolate* isolate, Handle<JSFunction> function) {
  DCHECK(FLAG_testing_d8_test_runner);

  Handle<Object> table =
      handle(isolate->heap()->pending_optimize_for_test_bytecode(), isolate);
  Handle<Object> entry =
      table->IsUndefined()
          ? handle(ReadOnlyRoots(isolate).the_hole_value(), isolate)
          : handle(Handle<ObjectHashTable>::cast(table)->Lookup(
                       handle(function->shared(), isolate)),
                   isolate);
  if (entry->IsTheHole()) {
    PrintF("Error: Function ");
    function->ShortPrint();
    PrintF(
        " should be prepared for optimization with "
86
        "%%PrepareFunctionForOptimization before  "
87 88 89 90 91
        "%%OptimizeFunctionOnNextCall / %%OptimizeOSR ");
    UNREACHABLE();
  }

  DCHECK(entry->IsTuple2());
92 93 94 95 96
  DCHECK(Handle<Tuple2>::cast(entry)->value2().IsSmi());
  FunctionStatusFlags status(Smi::ToInt(Handle<Tuple2>::cast(entry)->value2()));
  status = status.without(FunctionStatus::kPrepareForOptimize) |
           FunctionStatus::kMarkForOptimize;
  Handle<Tuple2>::cast(entry)->set_value2(Smi::FromInt(status));
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  table = ObjectHashTable::Put(Handle<ObjectHashTable>::cast(table),
                               handle(function->shared(), isolate), entry);
  isolate->heap()->SetPendingOptimizeForTestBytecode(*table);
}

void PendingOptimizationTable::FunctionWasOptimized(
    Isolate* isolate, Handle<JSFunction> function) {
  DCHECK(FLAG_testing_d8_test_runner);

  if (isolate->heap()->pending_optimize_for_test_bytecode().IsUndefined()) {
    return;
  }

  Handle<ObjectHashTable> table =
      handle(ObjectHashTable::cast(
                 isolate->heap()->pending_optimize_for_test_bytecode()),
             isolate);
  Handle<Object> value(table->Lookup(handle(function->shared(), isolate)),
                       isolate);
  // Remove only if we have already seen %OptimizeFunctionOnNextCall. If it is
  // optimized for other reasons, still keep holding the bytecode since we may
  // optimize it later.
  if (!value->IsTheHole() &&
      Smi::cast(Handle<Tuple2>::cast(value)->value2()).value() ==
          static_cast<int>(FunctionStatus::kMarkForOptimize)) {
    bool was_present;
    table = table->Remove(isolate, table, handle(function->shared(), isolate),
                          &was_present);
    DCHECK(was_present);
    isolate->heap()->SetPendingOptimizeForTestBytecode(*table);
  }
}

}  // namespace internal
}  // namespace v8