Commit 77c6ee0c authored by titzer's avatar titzer Committed by Commit bot

[turbofan] Make context specialization into a reducer.

R=bmeurer@chromium.org
BUG=

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

Cr-Commit-Position: refs/heads/master@{#25600}
parent 81ec9916
......@@ -6,7 +6,6 @@
#include "src/compiler/graph-inl.h"
#include "src/compiler/js-context-specialization.h"
#include "src/compiler/js-operator.h"
#include "src/compiler/node-aux-data-inl.h"
#include "src/compiler/node-matchers.h"
#include "src/compiler/node-properties-inl.h"
......@@ -14,45 +13,20 @@ namespace v8 {
namespace internal {
namespace compiler {
class ContextSpecializationVisitor : public NullNodeVisitor {
public:
explicit ContextSpecializationVisitor(JSContextSpecializer* spec)
: spec_(spec) {}
void Post(Node* node) {
switch (node->opcode()) {
case IrOpcode::kJSLoadContext: {
Reduction r = spec_->ReduceJSLoadContext(node);
if (r.Changed() && r.replacement() != node) {
NodeProperties::ReplaceWithValue(node, r.replacement());
node->RemoveAllInputs();
}
break;
}
case IrOpcode::kJSStoreContext: {
Reduction r = spec_->ReduceJSStoreContext(node);
if (r.Changed() && r.replacement() != node) {
NodeProperties::ReplaceWithValue(node, r.replacement());
node->RemoveAllInputs();
}
break;
}
default:
break;
}
}
private:
JSContextSpecializer* spec_;
};
void JSContextSpecializer::SpecializeToContext() {
NodeProperties::ReplaceWithValue(context_,
jsgraph_->Constant(info_->context()));
ContextSpecializationVisitor visitor(this);
jsgraph_->graph()->VisitNodeInputsFromEnd(&visitor);
Reduction JSContextSpecializer::Reduce(Node* node) {
if (node == context_) {
Node* constant = jsgraph_->Constant(info_->context());
NodeProperties::ReplaceWithValue(node, constant);
return Replace(constant);
}
if (node->opcode() == IrOpcode::kJSLoadContext) {
return ReduceJSLoadContext(node);
}
if (node->opcode() == IrOpcode::kJSStoreContext) {
return ReduceJSStoreContext(node);
}
return NoChange();
}
......@@ -62,7 +36,7 @@ Reduction JSContextSpecializer::ReduceJSLoadContext(Node* node) {
HeapObjectMatcher<Context> m(NodeProperties::GetValueInput(node, 0));
// If the context is not constant, no reduction can occur.
if (!m.HasValue()) {
return Reducer::NoChange();
return NoChange();
}
const ContextAccess& access = ContextAccessOf(node->op());
......@@ -77,14 +51,14 @@ Reduction JSContextSpecializer::ReduceJSLoadContext(Node* node) {
if (!access.immutable()) {
// The access does not have to look up a parent, nothing to fold.
if (access.depth() == 0) {
return Reducer::NoChange();
return NoChange();
}
const Operator* op = jsgraph_->javascript()->LoadContext(
0, access.index(), access.immutable());
node->set_op(op);
Handle<Object> context_handle = Handle<Object>(context, info_->isolate());
node->ReplaceInput(0, jsgraph_->Constant(context_handle));
return Reducer::Changed(node);
return Changed(node);
}
Handle<Object> value = Handle<Object>(
context->get(static_cast<int>(access.index())), info_->isolate());
......@@ -94,13 +68,15 @@ Reduction JSContextSpecializer::ReduceJSLoadContext(Node* node) {
// We must be conservative and check if the value in the slot is currently the
// hole or undefined. If it is neither of these, then it must be initialized.
if (value->IsUndefined() || value->IsTheHole()) {
return Reducer::NoChange();
return NoChange();
}
// Success. The context load can be replaced with the constant.
// TODO(titzer): record the specialization for sharing code across multiple
// contexts that have the same value in the corresponding context slot.
return Reducer::Replace(jsgraph_->Constant(value));
Node* constant = jsgraph_->Constant(value);
NodeProperties::ReplaceWithValue(node, constant);
return Replace(constant);
}
......@@ -110,14 +86,14 @@ Reduction JSContextSpecializer::ReduceJSStoreContext(Node* node) {
HeapObjectMatcher<Context> m(NodeProperties::GetValueInput(node, 0));
// If the context is not constant, no reduction can occur.
if (!m.HasValue()) {
return Reducer::NoChange();
return NoChange();
}
const ContextAccess& access = ContextAccessOf(node->op());
// The access does not have to look up a parent, nothing to fold.
if (access.depth() == 0) {
return Reducer::NoChange();
return NoChange();
}
// Find the right parent context.
......@@ -131,7 +107,7 @@ Reduction JSContextSpecializer::ReduceJSStoreContext(Node* node) {
Handle<Object> new_context_handle = Handle<Object>(context, info_->isolate());
node->ReplaceInput(0, jsgraph_->Constant(new_context_handle));
return Reducer::Changed(node);
return Changed(node);
}
} // namespace compiler
......
......@@ -16,12 +16,14 @@ namespace compiler {
// Specializes a given JSGraph to a given context, potentially constant folding
// some {LoadContext} nodes or strength reducing some {StoreContext} nodes.
class JSContextSpecializer {
class JSContextSpecializer : public Reducer {
public:
JSContextSpecializer(CompilationInfo* info, JSGraph* jsgraph, Node* context)
: info_(info), jsgraph_(jsgraph), context_(context) {}
void SpecializeToContext();
virtual Reduction Reduce(Node* node) OVERRIDE;
// Visible for unit testing.
Reduction ReduceJSLoadContext(Node* node);
Reduction ReduceJSStoreContext(Node* node);
......
......@@ -352,14 +352,16 @@ struct GraphBuilderPhase {
struct ContextSpecializerPhase {
static const char* phase_name() { return nullptr; }
static const char* phase_name() { return "context specializing"; }
void Run(PipelineData* data, Zone* temp_zone) {
SourcePositionTable::Scope pos(data->source_positions(),
SourcePosition::Unknown());
JSContextSpecializer spec(data->info(), data->jsgraph(),
data->context_node());
spec.SpecializeToContext();
GraphReducer graph_reducer(data->graph(), temp_zone);
graph_reducer.AddReducer(&spec);
graph_reducer.ReduceGraph();
}
};
......
......@@ -203,7 +203,7 @@ TEST(SpecializeToContext) {
JSContextSpecializer spec(t.info(), t.jsgraph(), const_context);
{
// Check that SpecializeToContext() replaces values and forwards effects
// Check that specialization replaces values and forwards effects
// correctly, and folds values from constant and non-constant contexts
Node* effect_in = start;
Node* load = t.NewNode(t.javascript()->LoadContext(0, slot, true),
......@@ -229,8 +229,10 @@ TEST(SpecializeToContext) {
CheckEffectInput(effect_in, load);
CheckEffectInput(load, effect_use);
// Perform the substitution on the entire graph.
spec.SpecializeToContext();
// Perform the reduction on the entire graph.
GraphReducer graph_reducer(t.graph(), t.main_zone());
graph_reducer.AddReducer(&spec);
graph_reducer.ReduceGraph();
// Effects should have been forwarded (not replaced with a value).
CheckEffectInput(effect_in, effect_use);
......
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