2014-07-30 13:54:45 +00:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
2015-10-28 13:09:46 +00:00
|
|
|
// TODO(jochen): Remove this after the setting is turned on globally.
|
|
|
|
#define V8_IMMINENT_DEPRECATION_WARNINGS
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "src/compiler/js-context-specialization.h"
|
2015-06-19 12:48:58 +00:00
|
|
|
#include "src/compiler/js-graph.h"
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "src/compiler/js-operator.h"
|
|
|
|
#include "src/compiler/node-matchers.h"
|
2015-01-29 09:17:45 +00:00
|
|
|
#include "src/compiler/node-properties.h"
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "src/compiler/source-position.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
#include "test/cctest/compiler/function-tester.h"
|
|
|
|
#include "test/cctest/compiler/graph-builder-tester.h"
|
|
|
|
|
2015-10-30 09:16:26 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
2014-07-30 13:54:45 +00:00
|
|
|
|
2015-04-30 12:06:20 +00:00
|
|
|
class ContextSpecializationTester : public HandleAndZoneScope {
|
2014-07-30 13:54:45 +00:00
|
|
|
public:
|
|
|
|
ContextSpecializationTester()
|
2015-04-30 12:06:20 +00:00
|
|
|
: graph_(new (main_zone()) Graph(main_zone())),
|
2014-07-30 13:54:45 +00:00
|
|
|
common_(main_zone()),
|
|
|
|
javascript_(main_zone()),
|
2014-11-10 14:28:09 +00:00
|
|
|
machine_(main_zone()),
|
2014-07-30 13:54:45 +00:00
|
|
|
simplified_(main_zone()),
|
2015-10-16 12:38:46 +00:00
|
|
|
jsgraph_(main_isolate(), graph(), common(), &javascript_, &simplified_,
|
|
|
|
&machine_),
|
2015-06-08 12:10:06 +00:00
|
|
|
reducer_(main_zone(), graph()),
|
2015-07-06 12:56:05 +00:00
|
|
|
spec_(&reducer_, jsgraph(), MaybeHandle<Context>()) {}
|
2014-07-30 13:54:45 +00:00
|
|
|
|
2015-07-06 12:56:05 +00:00
|
|
|
JSContextSpecialization* spec() { return &spec_; }
|
2014-07-30 13:54:45 +00:00
|
|
|
Factory* factory() { return main_isolate()->factory(); }
|
|
|
|
CommonOperatorBuilder* common() { return &common_; }
|
|
|
|
JSOperatorBuilder* javascript() { return &javascript_; }
|
|
|
|
SimplifiedOperatorBuilder* simplified() { return &simplified_; }
|
|
|
|
JSGraph* jsgraph() { return &jsgraph_; }
|
2015-04-30 12:06:20 +00:00
|
|
|
Graph* graph() { return graph_; }
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
private:
|
2015-04-30 12:06:20 +00:00
|
|
|
Graph* graph_;
|
2014-07-30 13:54:45 +00:00
|
|
|
CommonOperatorBuilder common_;
|
|
|
|
JSOperatorBuilder javascript_;
|
2014-09-12 11:06:37 +00:00
|
|
|
MachineOperatorBuilder machine_;
|
2014-07-30 13:54:45 +00:00
|
|
|
SimplifiedOperatorBuilder simplified_;
|
|
|
|
JSGraph jsgraph_;
|
2015-06-08 12:10:06 +00:00
|
|
|
GraphReducer reducer_;
|
2015-07-06 12:56:05 +00:00
|
|
|
JSContextSpecialization spec_;
|
2014-07-30 13:54:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ReduceJSLoadContext) {
|
|
|
|
ContextSpecializationTester t;
|
|
|
|
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* start = t.graph()->NewNode(t.common()->Start(0));
|
2014-07-30 13:54:45 +00:00
|
|
|
t.graph()->SetStart(start);
|
|
|
|
|
|
|
|
// Make a context and initialize it a bit for this test.
|
|
|
|
Handle<Context> native = t.factory()->NewNativeContext();
|
2014-08-08 11:05:31 +00:00
|
|
|
Handle<Context> subcontext1 = t.factory()->NewNativeContext();
|
|
|
|
Handle<Context> subcontext2 = t.factory()->NewNativeContext();
|
|
|
|
subcontext2->set_previous(*subcontext1);
|
|
|
|
subcontext1->set_previous(*native);
|
2014-07-30 13:54:45 +00:00
|
|
|
Handle<Object> expected = t.factory()->InternalizeUtf8String("gboy!");
|
2015-11-27 16:59:28 +00:00
|
|
|
const int slot = Context::NATIVE_CONTEXT_INDEX;
|
2014-07-30 13:54:45 +00:00
|
|
|
native->set(slot, *expected);
|
|
|
|
|
|
|
|
Node* const_context = t.jsgraph()->Constant(native);
|
2014-08-08 11:05:31 +00:00
|
|
|
Node* deep_const_context = t.jsgraph()->Constant(subcontext2);
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* param_context = t.graph()->NewNode(t.common()->Parameter(0), start);
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Mutable slot, constant context, depth = 0 => do nothing.
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* load = t.graph()->NewNode(t.javascript()->LoadContext(0, 0, false),
|
|
|
|
const_context, const_context, start);
|
2015-07-06 12:56:05 +00:00
|
|
|
Reduction r = t.spec()->Reduce(load);
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK(!r.Changed());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Mutable slot, non-constant context, depth = 0 => do nothing.
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* load = t.graph()->NewNode(t.javascript()->LoadContext(0, 0, false),
|
|
|
|
param_context, param_context, start);
|
2015-07-06 12:56:05 +00:00
|
|
|
Reduction r = t.spec()->Reduce(load);
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK(!r.Changed());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-08-08 11:05:31 +00:00
|
|
|
// Mutable slot, constant context, depth > 0 => fold-in parent context.
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* load = t.graph()->NewNode(
|
2014-07-30 13:54:45 +00:00
|
|
|
t.javascript()->LoadContext(2, Context::GLOBAL_EVAL_FUN_INDEX, false),
|
2014-08-08 11:05:31 +00:00
|
|
|
deep_const_context, deep_const_context, start);
|
2015-07-06 12:56:05 +00:00
|
|
|
Reduction r = t.spec()->Reduce(load);
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK(r.Changed());
|
2014-08-08 11:05:31 +00:00
|
|
|
Node* new_context_input = NodeProperties::GetValueInput(r.replacement(), 0);
|
|
|
|
CHECK_EQ(IrOpcode::kHeapConstant, new_context_input->opcode());
|
2015-06-19 12:48:58 +00:00
|
|
|
HeapObjectMatcher match(new_context_input);
|
2015-08-31 08:24:52 +00:00
|
|
|
CHECK_EQ(*native, *match.Value());
|
2014-09-04 09:37:25 +00:00
|
|
|
ContextAccess access = OpParameter<ContextAccess>(r.replacement());
|
2014-09-30 10:42:44 +00:00
|
|
|
CHECK_EQ(Context::GLOBAL_EVAL_FUN_INDEX, static_cast<int>(access.index()));
|
|
|
|
CHECK_EQ(0, static_cast<int>(access.depth()));
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK_EQ(false, access.immutable());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-08-08 11:05:31 +00:00
|
|
|
// Immutable slot, constant context, depth = 0 => specialize.
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* load = t.graph()->NewNode(t.javascript()->LoadContext(0, slot, true),
|
|
|
|
const_context, const_context, start);
|
2015-07-06 12:56:05 +00:00
|
|
|
Reduction r = t.spec()->Reduce(load);
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK(r.Changed());
|
|
|
|
CHECK(r.replacement() != load);
|
|
|
|
|
2015-06-19 12:48:58 +00:00
|
|
|
HeapObjectMatcher match(r.replacement());
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK(match.HasValue());
|
2015-08-31 08:24:52 +00:00
|
|
|
CHECK_EQ(*expected, *match.Value());
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 11:05:31 +00:00
|
|
|
// TODO(titzer): test with other kinds of contexts, e.g. a function context.
|
|
|
|
// TODO(sigurds): test that loads below create context are not optimized
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ReduceJSStoreContext) {
|
|
|
|
ContextSpecializationTester t;
|
|
|
|
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* start = t.graph()->NewNode(t.common()->Start(0));
|
2014-08-08 11:05:31 +00:00
|
|
|
t.graph()->SetStart(start);
|
|
|
|
|
|
|
|
// Make a context and initialize it a bit for this test.
|
|
|
|
Handle<Context> native = t.factory()->NewNativeContext();
|
|
|
|
Handle<Context> subcontext1 = t.factory()->NewNativeContext();
|
|
|
|
Handle<Context> subcontext2 = t.factory()->NewNativeContext();
|
|
|
|
subcontext2->set_previous(*subcontext1);
|
|
|
|
subcontext1->set_previous(*native);
|
|
|
|
Handle<Object> expected = t.factory()->InternalizeUtf8String("gboy!");
|
2015-11-27 16:59:28 +00:00
|
|
|
const int slot = Context::NATIVE_CONTEXT_INDEX;
|
2014-08-08 11:05:31 +00:00
|
|
|
native->set(slot, *expected);
|
|
|
|
|
|
|
|
Node* const_context = t.jsgraph()->Constant(native);
|
|
|
|
Node* deep_const_context = t.jsgraph()->Constant(subcontext2);
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* param_context = t.graph()->NewNode(t.common()->Parameter(0), start);
|
2014-08-08 11:05:31 +00:00
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
{
|
2014-08-08 11:05:31 +00:00
|
|
|
// Mutable slot, constant context, depth = 0 => do nothing.
|
2015-09-23 09:08:15 +00:00
|
|
|
Node* load =
|
|
|
|
t.graph()->NewNode(t.javascript()->StoreContext(0, 0), const_context,
|
|
|
|
const_context, const_context, start, start);
|
2015-07-06 12:56:05 +00:00
|
|
|
Reduction r = t.spec()->Reduce(load);
|
2014-08-08 11:05:31 +00:00
|
|
|
CHECK(!r.Changed());
|
|
|
|
}
|
2014-07-30 13:54:45 +00:00
|
|
|
|
2014-08-08 11:05:31 +00:00
|
|
|
{
|
|
|
|
// Mutable slot, non-constant context, depth = 0 => do nothing.
|
2015-09-23 09:08:15 +00:00
|
|
|
Node* load =
|
|
|
|
t.graph()->NewNode(t.javascript()->StoreContext(0, 0), param_context,
|
|
|
|
param_context, const_context, start, start);
|
2015-07-06 12:56:05 +00:00
|
|
|
Reduction r = t.spec()->Reduce(load);
|
2014-08-08 11:05:31 +00:00
|
|
|
CHECK(!r.Changed());
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 11:05:31 +00:00
|
|
|
{
|
|
|
|
// Immutable slot, constant context, depth = 0 => do nothing.
|
2015-09-23 09:08:15 +00:00
|
|
|
Node* load =
|
|
|
|
t.graph()->NewNode(t.javascript()->StoreContext(0, slot), const_context,
|
|
|
|
const_context, const_context, start, start);
|
2015-07-06 12:56:05 +00:00
|
|
|
Reduction r = t.spec()->Reduce(load);
|
2014-08-08 11:05:31 +00:00
|
|
|
CHECK(!r.Changed());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Mutable slot, constant context, depth > 0 => fold-in parent context.
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* load = t.graph()->NewNode(
|
2014-08-08 11:05:31 +00:00
|
|
|
t.javascript()->StoreContext(2, Context::GLOBAL_EVAL_FUN_INDEX),
|
2015-09-23 09:08:15 +00:00
|
|
|
deep_const_context, deep_const_context, const_context, start, start);
|
2015-07-06 12:56:05 +00:00
|
|
|
Reduction r = t.spec()->Reduce(load);
|
2014-08-08 11:05:31 +00:00
|
|
|
CHECK(r.Changed());
|
|
|
|
Node* new_context_input = NodeProperties::GetValueInput(r.replacement(), 0);
|
|
|
|
CHECK_EQ(IrOpcode::kHeapConstant, new_context_input->opcode());
|
2015-06-19 12:48:58 +00:00
|
|
|
HeapObjectMatcher match(new_context_input);
|
2015-08-31 08:24:52 +00:00
|
|
|
CHECK_EQ(*native, *match.Value());
|
2014-09-04 09:37:25 +00:00
|
|
|
ContextAccess access = OpParameter<ContextAccess>(r.replacement());
|
2014-09-30 10:42:44 +00:00
|
|
|
CHECK_EQ(Context::GLOBAL_EVAL_FUN_INDEX, static_cast<int>(access.index()));
|
|
|
|
CHECK_EQ(0, static_cast<int>(access.depth()));
|
2014-08-08 11:05:31 +00:00
|
|
|
CHECK_EQ(false, access.immutable());
|
|
|
|
}
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(titzer): factor out common code with effects checking in typed lowering.
|
|
|
|
static void CheckEffectInput(Node* effect, Node* use) {
|
|
|
|
CHECK_EQ(effect, NodeProperties::GetEffectInput(use));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SpecializeToContext) {
|
|
|
|
ContextSpecializationTester t;
|
|
|
|
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* start = t.graph()->NewNode(t.common()->Start(0));
|
2014-07-30 13:54:45 +00:00
|
|
|
t.graph()->SetStart(start);
|
|
|
|
|
|
|
|
// Make a context and initialize it a bit for this test.
|
|
|
|
Handle<Context> native = t.factory()->NewNativeContext();
|
|
|
|
Handle<Object> expected = t.factory()->InternalizeUtf8String("gboy!");
|
2015-11-27 16:59:28 +00:00
|
|
|
const int slot = Context::NATIVE_CONTEXT_INDEX;
|
2014-07-30 13:54:45 +00:00
|
|
|
native->set(slot, *expected);
|
|
|
|
|
|
|
|
Node* const_context = t.jsgraph()->Constant(native);
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* param_context = t.graph()->NewNode(t.common()->Parameter(0), start);
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
{
|
2014-12-02 10:02:38 +00:00
|
|
|
// Check that specialization replaces values and forwards effects
|
2014-07-30 13:54:45 +00:00
|
|
|
// correctly, and folds values from constant and non-constant contexts
|
2014-08-08 11:05:31 +00:00
|
|
|
Node* effect_in = start;
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* load = t.graph()->NewNode(t.javascript()->LoadContext(0, slot, true),
|
|
|
|
const_context, const_context, effect_in);
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* value_use =
|
|
|
|
t.graph()->NewNode(t.simplified()->ChangeTaggedToInt32(), load);
|
|
|
|
Node* other_load =
|
|
|
|
t.graph()->NewNode(t.javascript()->LoadContext(0, slot, true),
|
|
|
|
param_context, param_context, load);
|
2014-07-30 13:54:45 +00:00
|
|
|
Node* effect_use = other_load;
|
2014-09-04 10:23:51 +00:00
|
|
|
Node* other_use =
|
2015-04-30 12:06:20 +00:00
|
|
|
t.graph()->NewNode(t.simplified()->ChangeTaggedToInt32(), other_load);
|
2014-07-30 13:54:45 +00:00
|
|
|
|
2015-09-23 09:08:15 +00:00
|
|
|
Node* add = t.graph()->NewNode(
|
2015-12-01 09:03:10 +00:00
|
|
|
t.javascript()->Add(LanguageMode::SLOPPY, BinaryOperationHints::Any()),
|
|
|
|
value_use, other_use, param_context, t.jsgraph()->EmptyFrameState(),
|
2015-09-23 09:08:15 +00:00
|
|
|
t.jsgraph()->EmptyFrameState(), other_load, start);
|
2014-08-08 11:05:31 +00:00
|
|
|
|
2015-04-30 12:06:20 +00:00
|
|
|
Node* ret =
|
|
|
|
t.graph()->NewNode(t.common()->Return(), add, effect_use, start);
|
2015-05-26 10:31:55 +00:00
|
|
|
Node* end = t.graph()->NewNode(t.common()->End(1), ret);
|
2014-08-08 11:05:31 +00:00
|
|
|
USE(end);
|
|
|
|
t.graph()->SetEnd(end);
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
// Double check the above graph is what we expect, or the test is broken.
|
|
|
|
CheckEffectInput(effect_in, load);
|
|
|
|
CheckEffectInput(load, effect_use);
|
|
|
|
|
2014-12-02 10:02:38 +00:00
|
|
|
// Perform the reduction on the entire graph.
|
2015-06-05 12:01:55 +00:00
|
|
|
GraphReducer graph_reducer(t.main_zone(), t.graph());
|
2015-07-06 12:56:05 +00:00
|
|
|
JSContextSpecialization spec(&graph_reducer, t.jsgraph(),
|
|
|
|
MaybeHandle<Context>());
|
2014-12-02 10:02:38 +00:00
|
|
|
graph_reducer.AddReducer(&spec);
|
|
|
|
graph_reducer.ReduceGraph();
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
// Effects should have been forwarded (not replaced with a value).
|
|
|
|
CheckEffectInput(effect_in, effect_use);
|
|
|
|
|
|
|
|
// Use of {other_load} should not have been replaced.
|
|
|
|
CHECK_EQ(other_load, other_use->InputAt(0));
|
|
|
|
|
|
|
|
Node* replacement = value_use->InputAt(0);
|
2015-06-19 12:48:58 +00:00
|
|
|
HeapObjectMatcher match(replacement);
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK(match.HasValue());
|
2015-08-31 08:24:52 +00:00
|
|
|
CHECK_EQ(*expected, *match.Value());
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
// TODO(titzer): clean up above test and test more complicated effects.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SpecializeJSFunction_ToConstant1) {
|
|
|
|
FunctionTester T(
|
|
|
|
"(function() { var x = 1; function inc(a)"
|
|
|
|
" { return a + x; } return inc; })()");
|
|
|
|
|
|
|
|
T.CheckCall(1.0, 0.0, 0.0);
|
|
|
|
T.CheckCall(2.0, 1.0, 0.0);
|
|
|
|
T.CheckCall(2.1, 1.1, 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SpecializeJSFunction_ToConstant2) {
|
|
|
|
FunctionTester T(
|
|
|
|
"(function() { var x = 1.5; var y = 2.25; var z = 3.75;"
|
|
|
|
" function f(a) { return a - x + y - z; } return f; })()");
|
|
|
|
|
|
|
|
T.CheckCall(-3.0, 0.0, 0.0);
|
|
|
|
T.CheckCall(-2.0, 1.0, 0.0);
|
|
|
|
T.CheckCall(-1.9, 1.1, 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SpecializeJSFunction_ToConstant3) {
|
|
|
|
FunctionTester T(
|
|
|
|
"(function() { var x = -11.5; function inc()"
|
|
|
|
" { return (function(a) { return a + x; }); }"
|
|
|
|
" return inc(); })()");
|
|
|
|
|
|
|
|
T.CheckCall(-11.5, 0.0, 0.0);
|
|
|
|
T.CheckCall(-10.5, 1.0, 0.0);
|
|
|
|
T.CheckCall(-10.4, 1.1, 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SpecializeJSFunction_ToConstant_uninit) {
|
|
|
|
{
|
|
|
|
FunctionTester T(
|
|
|
|
"(function() { if (false) { var x = 1; } function inc(a)"
|
|
|
|
" { return x; } return inc; })()"); // x is undefined!
|
|
|
|
|
|
|
|
CHECK(T.Call(T.Val(0.0), T.Val(0.0)).ToHandleChecked()->IsUndefined());
|
|
|
|
CHECK(T.Call(T.Val(2.0), T.Val(0.0)).ToHandleChecked()->IsUndefined());
|
|
|
|
CHECK(T.Call(T.Val(-2.1), T.Val(0.0)).ToHandleChecked()->IsUndefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
FunctionTester T(
|
|
|
|
"(function() { if (false) { var x = 1; } function inc(a)"
|
|
|
|
" { return a + x; } return inc; })()"); // x is undefined!
|
|
|
|
|
|
|
|
CHECK(T.Call(T.Val(0.0), T.Val(0.0)).ToHandleChecked()->IsNaN());
|
|
|
|
CHECK(T.Call(T.Val(2.0), T.Val(0.0)).ToHandleChecked()->IsNaN());
|
|
|
|
CHECK(T.Call(T.Val(-2.1), T.Val(0.0)).ToHandleChecked()->IsNaN());
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 09:16:26 +00:00
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|