Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: src/compiler/js-context-specialization.cc

Issue 771713002: [turbofan] Make context specialization into a reducer. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/compiler/js-context-specialization.h ('k') | src/compiler/pipeline.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/compiler/common-operator.h" 5 #include "src/compiler/common-operator.h"
6 #include "src/compiler/graph-inl.h" 6 #include "src/compiler/graph-inl.h"
7 #include "src/compiler/js-context-specialization.h" 7 #include "src/compiler/js-context-specialization.h"
8 #include "src/compiler/js-operator.h" 8 #include "src/compiler/js-operator.h"
9 #include "src/compiler/node-aux-data-inl.h"
10 #include "src/compiler/node-matchers.h" 9 #include "src/compiler/node-matchers.h"
11 #include "src/compiler/node-properties-inl.h" 10 #include "src/compiler/node-properties-inl.h"
12 11
13 namespace v8 { 12 namespace v8 {
14 namespace internal { 13 namespace internal {
15 namespace compiler { 14 namespace compiler {
16 15
17 class ContextSpecializationVisitor : public NullNodeVisitor {
18 public:
19 explicit ContextSpecializationVisitor(JSContextSpecializer* spec)
20 : spec_(spec) {}
21 16
22 void Post(Node* node) { 17 Reduction JSContextSpecializer::Reduce(Node* node) {
23 switch (node->opcode()) { 18 if (node == context_) {
24 case IrOpcode::kJSLoadContext: { 19 Node* constant = jsgraph_->Constant(info_->context());
25 Reduction r = spec_->ReduceJSLoadContext(node); 20 NodeProperties::ReplaceWithValue(node, constant);
26 if (r.Changed() && r.replacement() != node) { 21 return Replace(constant);
27 NodeProperties::ReplaceWithValue(node, r.replacement());
28 node->RemoveAllInputs();
29 }
30 break;
31 }
32 case IrOpcode::kJSStoreContext: {
33 Reduction r = spec_->ReduceJSStoreContext(node);
34 if (r.Changed() && r.replacement() != node) {
35 NodeProperties::ReplaceWithValue(node, r.replacement());
36 node->RemoveAllInputs();
37 }
38 break;
39 }
40 default:
41 break;
42 }
43 } 22 }
44 23 if (node->opcode() == IrOpcode::kJSLoadContext) {
45 private: 24 return ReduceJSLoadContext(node);
46 JSContextSpecializer* spec_; 25 }
47 }; 26 if (node->opcode() == IrOpcode::kJSStoreContext) {
48 27 return ReduceJSStoreContext(node);
49 28 }
50 void JSContextSpecializer::SpecializeToContext() { 29 return NoChange();
51 NodeProperties::ReplaceWithValue(context_,
52 jsgraph_->Constant(info_->context()));
53
54 ContextSpecializationVisitor visitor(this);
55 jsgraph_->graph()->VisitNodeInputsFromEnd(&visitor);
56 } 30 }
57 31
58 32
59 Reduction JSContextSpecializer::ReduceJSLoadContext(Node* node) { 33 Reduction JSContextSpecializer::ReduceJSLoadContext(Node* node) {
60 DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode()); 34 DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode());
61 35
62 HeapObjectMatcher<Context> m(NodeProperties::GetValueInput(node, 0)); 36 HeapObjectMatcher<Context> m(NodeProperties::GetValueInput(node, 0));
63 // If the context is not constant, no reduction can occur. 37 // If the context is not constant, no reduction can occur.
64 if (!m.HasValue()) { 38 if (!m.HasValue()) {
65 return Reducer::NoChange(); 39 return NoChange();
66 } 40 }
67 41
68 const ContextAccess& access = ContextAccessOf(node->op()); 42 const ContextAccess& access = ContextAccessOf(node->op());
69 43
70 // Find the right parent context. 44 // Find the right parent context.
71 Context* context = *m.Value().handle(); 45 Context* context = *m.Value().handle();
72 for (size_t i = access.depth(); i > 0; --i) { 46 for (size_t i = access.depth(); i > 0; --i) {
73 context = context->previous(); 47 context = context->previous();
74 } 48 }
75 49
76 // If the access itself is mutable, only fold-in the parent. 50 // If the access itself is mutable, only fold-in the parent.
77 if (!access.immutable()) { 51 if (!access.immutable()) {
78 // The access does not have to look up a parent, nothing to fold. 52 // The access does not have to look up a parent, nothing to fold.
79 if (access.depth() == 0) { 53 if (access.depth() == 0) {
80 return Reducer::NoChange(); 54 return NoChange();
81 } 55 }
82 const Operator* op = jsgraph_->javascript()->LoadContext( 56 const Operator* op = jsgraph_->javascript()->LoadContext(
83 0, access.index(), access.immutable()); 57 0, access.index(), access.immutable());
84 node->set_op(op); 58 node->set_op(op);
85 Handle<Object> context_handle = Handle<Object>(context, info_->isolate()); 59 Handle<Object> context_handle = Handle<Object>(context, info_->isolate());
86 node->ReplaceInput(0, jsgraph_->Constant(context_handle)); 60 node->ReplaceInput(0, jsgraph_->Constant(context_handle));
87 return Reducer::Changed(node); 61 return Changed(node);
88 } 62 }
89 Handle<Object> value = Handle<Object>( 63 Handle<Object> value = Handle<Object>(
90 context->get(static_cast<int>(access.index())), info_->isolate()); 64 context->get(static_cast<int>(access.index())), info_->isolate());
91 65
92 // Even though the context slot is immutable, the context might have escaped 66 // Even though the context slot is immutable, the context might have escaped
93 // before the function to which it belongs has initialized the slot. 67 // before the function to which it belongs has initialized the slot.
94 // We must be conservative and check if the value in the slot is currently the 68 // We must be conservative and check if the value in the slot is currently the
95 // hole or undefined. If it is neither of these, then it must be initialized. 69 // hole or undefined. If it is neither of these, then it must be initialized.
96 if (value->IsUndefined() || value->IsTheHole()) { 70 if (value->IsUndefined() || value->IsTheHole()) {
97 return Reducer::NoChange(); 71 return NoChange();
98 } 72 }
99 73
100 // Success. The context load can be replaced with the constant. 74 // Success. The context load can be replaced with the constant.
101 // TODO(titzer): record the specialization for sharing code across multiple 75 // TODO(titzer): record the specialization for sharing code across multiple
102 // contexts that have the same value in the corresponding context slot. 76 // contexts that have the same value in the corresponding context slot.
103 return Reducer::Replace(jsgraph_->Constant(value)); 77 Node* constant = jsgraph_->Constant(value);
78 NodeProperties::ReplaceWithValue(node, constant);
79 return Replace(constant);
104 } 80 }
105 81
106 82
107 Reduction JSContextSpecializer::ReduceJSStoreContext(Node* node) { 83 Reduction JSContextSpecializer::ReduceJSStoreContext(Node* node) {
108 DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode()); 84 DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode());
109 85
110 HeapObjectMatcher<Context> m(NodeProperties::GetValueInput(node, 0)); 86 HeapObjectMatcher<Context> m(NodeProperties::GetValueInput(node, 0));
111 // If the context is not constant, no reduction can occur. 87 // If the context is not constant, no reduction can occur.
112 if (!m.HasValue()) { 88 if (!m.HasValue()) {
113 return Reducer::NoChange(); 89 return NoChange();
114 } 90 }
115 91
116 const ContextAccess& access = ContextAccessOf(node->op()); 92 const ContextAccess& access = ContextAccessOf(node->op());
117 93
118 // The access does not have to look up a parent, nothing to fold. 94 // The access does not have to look up a parent, nothing to fold.
119 if (access.depth() == 0) { 95 if (access.depth() == 0) {
120 return Reducer::NoChange(); 96 return NoChange();
121 } 97 }
122 98
123 // Find the right parent context. 99 // Find the right parent context.
124 Context* context = *m.Value().handle(); 100 Context* context = *m.Value().handle();
125 for (size_t i = access.depth(); i > 0; --i) { 101 for (size_t i = access.depth(); i > 0; --i) {
126 context = context->previous(); 102 context = context->previous();
127 } 103 }
128 104
129 const Operator* op = jsgraph_->javascript()->StoreContext(0, access.index()); 105 const Operator* op = jsgraph_->javascript()->StoreContext(0, access.index());
130 node->set_op(op); 106 node->set_op(op);
131 Handle<Object> new_context_handle = Handle<Object>(context, info_->isolate()); 107 Handle<Object> new_context_handle = Handle<Object>(context, info_->isolate());
132 node->ReplaceInput(0, jsgraph_->Constant(new_context_handle)); 108 node->ReplaceInput(0, jsgraph_->Constant(new_context_handle));
133 109
134 return Reducer::Changed(node); 110 return Changed(node);
135 } 111 }
136 112
137 } // namespace compiler 113 } // namespace compiler
138 } // namespace internal 114 } // namespace internal
139 } // namespace v8 115 } // namespace v8
OLDNEW
« no previous file with comments | « src/compiler/js-context-specialization.h ('k') | src/compiler/pipeline.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698