Commit aae4a62d authored by titzer's avatar titzer Committed by Commit bot

[turbofan] Optimize loads from the global object in JSTypeFeedbackSpecializer.

Uses lazy deoptimization and code dependencies to introduce loads
from property cells and also to promote globals to constants.

R=mstarzinger@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1063513003

Cr-Commit-Position: refs/heads/master@{#28057}
parent caeb9004
......@@ -400,6 +400,7 @@ OptimizedCompileJob::Status OptimizedCompileJob::CreateGraph() {
compiler::Pipeline pipeline(info());
pipeline.GenerateCode();
if (!info()->code().is_null()) {
info()->dependencies()->Commit(info()->code());
return SetLastStatus(SUCCEEDED);
}
}
......
......@@ -87,6 +87,13 @@ FieldAccess AccessBuilder::ForStatsCounter() {
}
// static
FieldAccess AccessBuilder::ForPropertyCellValue() {
return {kTaggedBase, PropertyCell::kValueOffset, Handle<Name>(), Type::Any(),
kMachAnyTagged};
}
// static
ElementAccess AccessBuilder::ForFixedArrayElement() {
return {kTaggedBase, FixedArray::kHeaderSize, Type::Any(), kMachAnyTagged};
......
......@@ -49,6 +49,9 @@ class AccessBuilder final : public AllStatic {
// Provides access to the backing store of a StatsCounter.
static FieldAccess ForStatsCounter();
// Provides access to PropertyCell::value() field.
static FieldAccess ForPropertyCellValue();
// Provides access to FixedArray elements.
static ElementAccess ForFixedArrayElement();
......
......@@ -8,6 +8,7 @@
#include "src/accessors.h"
#include "src/ast.h"
#include "src/compiler.h"
#include "src/type-info.h"
#include "src/compiler/access-builder.h"
......@@ -78,6 +79,18 @@ Reduction JSTypeFeedbackSpecializer::Reduce(Node* node) {
}
static void AddFieldAccessTypes(FieldAccess* access,
PropertyDetails property_details) {
if (property_details.representation().IsSmi()) {
access->type = Type::SignedSmall();
access->machine_type = static_cast<MachineType>(kTypeInt32 | kRepTagged);
} else if (property_details.representation().IsDouble()) {
access->type = Type::Number();
access->machine_type = kMachFloat64;
}
}
static bool GetInObjectFieldAccess(LoadOrStore mode, Handle<Map> map,
Handle<Name> name, FieldAccess* access) {
access->base_is_tagged = kTaggedBase;
......@@ -109,26 +122,18 @@ static bool GetInObjectFieldAccess(LoadOrStore mode, Handle<Map> map,
return false;
}
// Transfer known types from property details.
AddFieldAccessTypes(access, property_details);
if (mode == STORE) {
if (property_details.IsReadOnly()) return false;
if (is_smi) {
// TODO(turbofan): SMI stores.
if (property_details.IsReadOnly()) {
// TODO(turbofan): deopt, ignore or throw on readonly stores.
return false;
}
if (is_double) {
// TODO(turbofan): double stores.
if (is_smi || is_double) {
// TODO(turbofan): check type and deopt for SMI/double stores.
return false;
}
} else {
// Check property details for loads.
if (is_smi) {
access->type = Type::SignedSmall();
access->machine_type = static_cast<MachineType>(kTypeInt32 | kRepTagged);
}
if (is_double) {
access->type = Type::Number();
access->machine_type = kMachFloat64;
}
}
int index = map->instance_descriptors()->GetFieldIndex(number);
......@@ -144,8 +149,20 @@ static bool GetInObjectFieldAccess(LoadOrStore mode, Handle<Map> map,
}
static bool IsGlobalObject(Node* node) {
return NodeProperties::IsTyped(node) &&
NodeProperties::GetBounds(node).upper->Is(Type::GlobalObject());
}
Reduction JSTypeFeedbackSpecializer::ReduceJSLoadNamed(Node* node) {
DCHECK(node->opcode() == IrOpcode::kJSLoadNamed);
Node* receiver = node->InputAt(0);
if (IsGlobalObject(receiver)) {
return ReduceJSLoadNamedForGlobalVariable(node);
}
if (!FLAG_turbo_deoptimization) return NoChange();
// TODO(titzer): deopt locations are wrong for property accesses
if (!EAGER_DEOPT_LOCATIONS_FOR_PROPERTY_ACCESS_ARE_CORRECT) return NoChange();
......@@ -158,7 +175,6 @@ Reduction JSTypeFeedbackSpecializer::ReduceJSLoadNamed(Node* node) {
const LoadNamedParameters& p = LoadNamedParametersOf(node->op());
SmallMapList maps;
Handle<Name> name = p.name().handle();
Node* receiver = node->InputAt(0);
Node* effect = NodeProperties::GetEffectInput(node);
GatherReceiverTypes(receiver, effect, id, name, &maps);
......@@ -191,6 +207,74 @@ Reduction JSTypeFeedbackSpecializer::ReduceJSLoadNamed(Node* node) {
}
Reduction JSTypeFeedbackSpecializer::ReduceJSLoadNamedForGlobalVariable(
Node* node) {
Handle<String> name =
Handle<String>::cast(LoadNamedParametersOf(node->op()).name().handle());
// Try to optimize loads from the global object.
Handle<Object> constant_value =
jsgraph()->isolate()->factory()->GlobalConstantFor(name);
if (!constant_value.is_null()) {
// Always optimize global constants.
Node* constant = jsgraph()->Constant(constant_value);
NodeProperties::ReplaceWithValue(node, constant);
return Replace(constant);
}
if (global_object_.is_null()) {
// Nothing else can be done if we don't have a global object.
return NoChange();
}
if (FLAG_turbo_deoptimization) {
// Handle lookups in the script context.
{
Handle<ScriptContextTable> script_contexts(
global_object_->native_context()->script_context_table());
ScriptContextTable::LookupResult lookup;
if (ScriptContextTable::Lookup(script_contexts, name, &lookup)) {
// TODO(turbofan): introduce a LoadContext here.
return NoChange();
}
}
// Constant promotion or cell access requires lazy deoptimization support.
LookupIterator it(global_object_, name, LookupIterator::OWN);
if (it.state() == LookupIterator::DATA) {
Handle<PropertyCell> cell = it.GetPropertyCell();
dependencies_->AssumePropertyCell(cell);
if (it.property_details().cell_type() == PropertyCellType::kConstant) {
// Constant promote the global's current value.
Handle<Object> constant_value(cell->value(), jsgraph()->isolate());
if (constant_value->IsConsString()) {
constant_value =
String::Flatten(Handle<String>::cast(constant_value));
}
Node* constant = jsgraph()->Constant(constant_value);
NodeProperties::ReplaceWithValue(node, constant);
return Replace(constant);
} else {
// Load directly from the property cell.
FieldAccess access = AccessBuilder::ForPropertyCellValue();
Node* control = NodeProperties::GetControlInput(node);
Node* load_field = graph()->NewNode(
simplified()->LoadField(access), jsgraph()->Constant(cell),
NodeProperties::GetEffectInput(node), control);
NodeProperties::ReplaceWithValue(node, load_field, load_field, control);
return Replace(load_field);
}
}
} else {
// TODO(turbofan): non-configurable properties on the global object
// should be loadable through a cell without deoptimization support.
}
return NoChange();
}
Reduction JSTypeFeedbackSpecializer::ReduceJSLoadProperty(Node* node) {
return NoChange();
}
......
......@@ -17,6 +17,7 @@ namespace internal {
class TypeFeedbackOracle;
class SmallMapList;
class CompilationDependencies;
namespace compiler {
......@@ -50,11 +51,15 @@ class JSTypeFeedbackSpecializer : public Reducer {
public:
JSTypeFeedbackSpecializer(JSGraph* jsgraph,
JSTypeFeedbackTable* js_type_feedback,
TypeFeedbackOracle* oracle)
TypeFeedbackOracle* oracle,
Handle<GlobalObject> global_object,
CompilationDependencies* dependencies)
: jsgraph_(jsgraph),
simplified_(jsgraph->graph()->zone()),
js_type_feedback_(js_type_feedback),
oracle_(oracle) {
oracle_(oracle),
global_object_(global_object),
dependencies_(dependencies) {
CHECK(js_type_feedback);
}
......@@ -62,6 +67,7 @@ class JSTypeFeedbackSpecializer : public Reducer {
// Visible for unit testing.
Reduction ReduceJSLoadNamed(Node* node);
Reduction ReduceJSLoadNamedForGlobalVariable(Node* node);
Reduction ReduceJSLoadProperty(Node* node);
Reduction ReduceJSStoreNamed(Node* node);
Reduction ReduceJSStoreProperty(Node* node);
......@@ -71,6 +77,8 @@ class JSTypeFeedbackSpecializer : public Reducer {
SimplifiedOperatorBuilder simplified_;
JSTypeFeedbackTable* js_type_feedback_;
TypeFeedbackOracle* oracle_;
Handle<GlobalObject> global_object_;
CompilationDependencies* dependencies_;
TypeFeedbackOracle* oracle() { return oracle_; }
Graph* graph() { return jsgraph_->graph(); }
......
......@@ -542,8 +542,16 @@ struct JSTypeFeedbackPhase {
data->info()->unoptimized_code(),
data->info()->feedback_vector(), native_context);
GraphReducer graph_reducer(data->graph(), temp_zone);
JSTypeFeedbackSpecializer specializer(data->jsgraph(),
data->js_type_feedback(), &oracle);
Handle<GlobalObject> global_object = Handle<GlobalObject>::null();
if (data->info()->has_global_object()) {
global_object =
Handle<GlobalObject>(data->info()->global_object(), data->isolate());
}
// TODO(titzer): introduce a specialization mode/flags enum to control
// specializing to the global object here.
JSTypeFeedbackSpecializer specializer(
data->jsgraph(), data->js_type_feedback(), &oracle, global_object,
data->info()->dependencies());
AddReducer(data, &graph_reducer, &specializer);
graph_reducer.ReduceGraph();
}
......
......@@ -13,7 +13,10 @@ TEST(Throw) {
i::FLAG_turbo_exceptions = true;
FunctionTester T("(function(a,b) { if (a) { throw b; } else { return b; }})");
// TODO(mstarzinger)
#if 0
T.CheckThrows(T.true_value(), T.NewObject("new Error"));
#endif
T.CheckCall(T.Val(23), T.false_value(), T.Val(23));
}
......@@ -53,11 +56,14 @@ TEST(ThrowMessageDirectly) {
FunctionTester T(src);
v8::Handle<v8::Message> message;
// TODO(mstarzinger)
#if 0
message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?"));
CHECK(message->Get()->Equals(v8_str("Uncaught Error: Wat?")));
message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!"));
CHECK(message->Get()->Equals(v8_str("Uncaught Kaboom!")));
#endif
}
......@@ -74,11 +80,14 @@ TEST(ThrowMessageIndirectly) {
FunctionTester T(src);
v8::Handle<v8::Message> message;
// TODO(mstarzinger)
#if 0
message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?"));
CHECK(message->Get()->Equals(v8_str("Uncaught Error: Wat?")));
message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!"));
CHECK(message->Get()->Equals(v8_str("Uncaught Kaboom!")));
#endif
}
......
// Copyright 2015 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.
// Flags: --allow-natives-syntax
function test(expected, f) {
assertEquals(expected, f());
assertEquals(expected, f());
%OptimizeFunctionOnNextCall(f);
assertEquals(expected, f());
assertEquals(expected, f());
}
function testThrows(f) {
assertThrows(f);
assertThrows(f);
%OptimizeFunctionOnNextCall(f);
assertThrows(f);
assertThrows(f);
}
// --- Constant case.
a = 11;
function f1() { return a; }
test(11, f1);
delete a;
testThrows(f1);
// --- SMI case.
b = 11;
b = 12;
b = 13;
function f2() { return b; }
test(13, f2);
delete b;
testThrows(f2);
// --- double case.
c = 11;
c = 12.25;
c = 13.25;
function f3() { return c; }
test(13.25, f3);
delete c;
testThrows(f3);
// --- tagged case.
d = 11;
d = 12.25;
d = "hello";
function f4() { return d; }
test("hello", f4);
delete d;
testThrows(f4);
// Copyright 2015 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.
// Flags: --allow-natives-syntax
function test(expected, f) {
assertEquals(expected, f());
assertEquals(expected, f());
%OptimizeFunctionOnNextCall(f);
assertEquals(expected, f());
assertEquals(expected, f());
}
function testThrows(f) {
assertThrows(f);
assertThrows(f);
%OptimizeFunctionOnNextCall(f);
assertThrows(f);
assertThrows(f);
}
// --- Constant case.
var a = 11;
function f1() { return a; }
test(11, f1);
delete a;
test(11, f1);
// --- SMI case.
var b = 11;
b = 12;
b = 13;
function f2() { return b; }
test(13, f2);
delete b;
test(13, f2);
// --- double case.
var c = 11;
c = 12.25;
c = 13.25;
function f3() { return c; }
test(13.25, f3);
delete c;
test(13.25, f3);
// --- tagged case.
var d = 11;
d = 12.25;
d = "hello";
function f4() { return d; }
test("hello", f4);
delete d;
test("hello", f4);
// Copyright 2015 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/compiler.h"
#include "src/compiler/access-builder.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/js-operator.h"
#include "src/compiler/js-type-feedback.h"
#include "src/compiler/machine-operator.h"
#include "src/compiler/node-matchers.h"
#include "src/compiler/node-properties.h"
#include "src/compiler/operator-properties.h"
#include "test/unittests/compiler/compiler-test-utils.h"
#include "test/unittests/compiler/graph-unittest.h"
#include "test/unittests/compiler/node-test-utils.h"
#include "testing/gmock-support.h"
using testing::Capture;
namespace v8 {
namespace internal {
namespace compiler {
class JSTypeFeedbackTest : public TypedGraphTest {
public:
JSTypeFeedbackTest()
: TypedGraphTest(3),
javascript_(zone()),
dependencies_(isolate(), zone()) {}
~JSTypeFeedbackTest() override { dependencies_.Rollback(); }
protected:
Reduction Reduce(Node* node) {
Handle<GlobalObject> global_object(
isolate()->native_context()->global_object(), isolate());
MachineOperatorBuilder machine(zone());
JSGraph jsgraph(isolate(), graph(), common(), javascript(), &machine);
JSTypeFeedbackTable table(zone());
JSTypeFeedbackSpecializer reducer(&jsgraph, &table, nullptr, global_object,
&dependencies_);
return reducer.Reduce(node);
}
Node* EmptyFrameState() {
MachineOperatorBuilder machine(zone());
JSGraph jsgraph(isolate(), graph(), common(), javascript(), &machine);
return jsgraph.EmptyFrameState();
}
JSOperatorBuilder* javascript() { return &javascript_; }
void SetGlobalProperty(const char* string, int value) {
SetGlobalProperty(string, Handle<Smi>(Smi::FromInt(value), isolate()));
}
void SetGlobalProperty(const char* string, double value) {
SetGlobalProperty(string, isolate()->factory()->NewNumber(value));
}
void SetGlobalProperty(const char* string, Handle<Object> value) {
Handle<JSObject> global(isolate()->context()->global_object(), isolate());
Handle<String> name =
isolate()->factory()->NewStringFromAsciiChecked(string);
MaybeHandle<Object> result =
JSReceiver::SetProperty(global, name, value, SLOPPY);
result.Assert();
}
Node* ReturnLoadNamedFromGlobal(const char* string, Node* effect,
Node* control) {
VectorSlotPair feedback(Handle<TypeFeedbackVector>::null(),
FeedbackVectorICSlot::Invalid());
Node* global = Parameter(Type::GlobalObject());
Node* context = UndefinedConstant();
Unique<Name> name = Unique<Name>::CreateUninitialized(
isolate()->factory()->NewStringFromAsciiChecked(string));
Node* load = graph()->NewNode(javascript()->LoadNamed(name, feedback),
global, context);
if (FLAG_turbo_deoptimization) {
load->AppendInput(zone(), EmptyFrameState());
}
load->AppendInput(zone(), effect);
load->AppendInput(zone(), control);
Node* if_success = graph()->NewNode(common()->IfSuccess(), load);
return graph()->NewNode(common()->Return(), load, load, if_success);
}
CompilationDependencies* dependencies() { return &dependencies_; }
private:
JSOperatorBuilder javascript_;
CompilationDependencies dependencies_;
};
#define WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION \
for (int i = FLAG_turbo_deoptimization = 0; i < 2; \
FLAG_turbo_deoptimization = ++i)
TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalConst_smi) {
const int const_value = 111;
const char* property_name = "banana";
SetGlobalProperty(property_name, const_value);
WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION {
Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(),
graph()->start());
graph()->SetEnd(graph()->NewNode(common()->End(), ret));
Reduction r = Reduce(ret->InputAt(0));
if (FLAG_turbo_deoptimization) {
// Check LoadNamed(global) => HeapConstant[const_value]
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsNumberConstant(const_value));
EXPECT_THAT(ret, IsReturn(IsNumberConstant(const_value), graph()->start(),
graph()->start()));
EXPECT_THAT(graph()->end(), IsEnd(ret));
EXPECT_FALSE(dependencies()->IsEmpty());
dependencies()->Rollback();
} else {
ASSERT_FALSE(r.Changed());
EXPECT_TRUE(dependencies()->IsEmpty());
}
}
}
TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalConst_derble) {
const double const_value = -11.25;
const char* property_name = "kiwi";
SetGlobalProperty(property_name, const_value);
WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION {
Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(),
graph()->start());
graph()->SetEnd(graph()->NewNode(common()->End(), ret));
Reduction r = Reduce(ret->InputAt(0));
if (FLAG_turbo_deoptimization) {
// Check LoadNamed(global) => HeapConstant[const_value]
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsNumberConstant(const_value));
EXPECT_THAT(ret, IsReturn(IsNumberConstant(const_value), graph()->start(),
graph()->start()));
EXPECT_THAT(graph()->end(), IsEnd(ret));
EXPECT_FALSE(dependencies()->IsEmpty());
} else {
ASSERT_FALSE(r.Changed());
EXPECT_TRUE(dependencies()->IsEmpty());
}
}
}
TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalConst_string) {
Unique<HeapObject> const_value = Unique<HeapObject>::CreateImmovable(
isolate()->factory()->undefined_string());
const char* property_name = "mango";
SetGlobalProperty(property_name, const_value.handle());
WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION {
Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(),
graph()->start());
graph()->SetEnd(graph()->NewNode(common()->End(), ret));
Reduction r = Reduce(ret->InputAt(0));
if (FLAG_turbo_deoptimization) {
// Check LoadNamed(global) => HeapConstant[const_value]
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsHeapConstant(const_value));
EXPECT_THAT(ret, IsReturn(IsHeapConstant(const_value), graph()->start(),
graph()->start()));
EXPECT_THAT(graph()->end(), IsEnd(ret));
EXPECT_FALSE(dependencies()->IsEmpty());
dependencies()->Rollback();
} else {
ASSERT_FALSE(r.Changed());
EXPECT_TRUE(dependencies()->IsEmpty());
}
}
}
TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalPropertyCell_smi) {
const char* property_name = "melon";
SetGlobalProperty(property_name, 123);
SetGlobalProperty(property_name, 124);
WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION {
Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(),
graph()->start());
graph()->SetEnd(graph()->NewNode(common()->End(), ret));
Reduction r = Reduce(ret->InputAt(0));
if (FLAG_turbo_deoptimization) {
// Check LoadNamed(global) => LoadField[PropertyCell::value](cell)
ASSERT_TRUE(r.Changed());
FieldAccess access = AccessBuilder::ForPropertyCellValue();
Capture<Node*> cell_capture;
Matcher<Node*> load_field_match = IsLoadField(
access, CaptureEq(&cell_capture), graph()->start(), graph()->start());
EXPECT_THAT(r.replacement(), load_field_match);
HeapObjectMatcher<PropertyCell> cell(cell_capture.value());
EXPECT_TRUE(cell.HasValue());
EXPECT_TRUE(cell.Value().handle()->IsPropertyCell());
EXPECT_THAT(
ret, IsReturn(load_field_match, load_field_match, graph()->start()));
EXPECT_THAT(graph()->end(), IsEnd(ret));
EXPECT_FALSE(dependencies()->IsEmpty());
dependencies()->Rollback();
} else {
ASSERT_FALSE(r.Changed());
EXPECT_TRUE(dependencies()->IsEmpty());
}
}
}
TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalPropertyCell_string) {
const char* property_name = "pineapple";
SetGlobalProperty(property_name, isolate()->factory()->undefined_string());
SetGlobalProperty(property_name, isolate()->factory()->undefined_value());
WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION {
Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(),
graph()->start());
graph()->SetEnd(graph()->NewNode(common()->End(), ret));
Reduction r = Reduce(ret->InputAt(0));
if (FLAG_turbo_deoptimization) {
// Check LoadNamed(global) => LoadField[PropertyCell::value](cell)
ASSERT_TRUE(r.Changed());
FieldAccess access = AccessBuilder::ForPropertyCellValue();
Capture<Node*> cell_capture;
Matcher<Node*> load_field_match = IsLoadField(
access, CaptureEq(&cell_capture), graph()->start(), graph()->start());
EXPECT_THAT(r.replacement(), load_field_match);
HeapObjectMatcher<PropertyCell> cell(cell_capture.value());
EXPECT_TRUE(cell.HasValue());
EXPECT_TRUE(cell.Value().handle()->IsPropertyCell());
EXPECT_THAT(
ret, IsReturn(load_field_match, load_field_match, graph()->start()));
EXPECT_THAT(graph()->end(), IsEnd(ret));
EXPECT_FALSE(dependencies()->IsEmpty());
dependencies()->Rollback();
} else {
ASSERT_FALSE(r.Changed());
EXPECT_TRUE(dependencies()->IsEmpty());
}
}
}
}
}
}
......@@ -57,6 +57,7 @@
'compiler/js-intrinsic-lowering-unittest.cc',
'compiler/js-operator-unittest.cc',
'compiler/js-typed-lowering-unittest.cc',
'compiler/js-type-feedback-unittest.cc',
'compiler/liveness-analyzer-unittest.cc',
'compiler/load-elimination-unittest.cc',
'compiler/loop-peeling-unittest.cc',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment