| OLD | NEW |
| 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/js-context-specialization.h" | 5 #include "src/compiler/js-context-specialization.h" |
| 6 | 6 |
| 7 #include "src/compiler/common-operator.h" | 7 #include "src/compiler/common-operator.h" |
| 8 #include "src/compiler/js-graph.h" | 8 #include "src/compiler/js-graph.h" |
| 9 #include "src/compiler/js-operator.h" | 9 #include "src/compiler/js-operator.h" |
| 10 #include "src/compiler/node-matchers.h" | 10 #include "src/compiler/node-matchers.h" |
| 11 #include "src/compiler/node-properties.h" | 11 #include "src/compiler/node-properties.h" |
| 12 #include "src/contexts.h" | 12 #include "src/contexts.h" |
| 13 | 13 |
| 14 namespace v8 { | 14 namespace v8 { |
| 15 namespace internal { | 15 namespace internal { |
| 16 namespace compiler { | 16 namespace compiler { |
| 17 | 17 |
| 18 Reduction JSContextSpecialization::Reduce(Node* node) { | 18 Reduction JSContextSpecialization::Reduce(Node* node) { |
| 19 switch (node->opcode()) { | 19 switch (node->opcode()) { |
| 20 case IrOpcode::kParameter: | |
| 21 return ReduceParameter(node); | |
| 22 case IrOpcode::kJSLoadContext: | 20 case IrOpcode::kJSLoadContext: |
| 23 return ReduceJSLoadContext(node); | 21 return ReduceJSLoadContext(node); |
| 24 case IrOpcode::kJSStoreContext: | 22 case IrOpcode::kJSStoreContext: |
| 25 return ReduceJSStoreContext(node); | 23 return ReduceJSStoreContext(node); |
| 26 default: | 24 default: |
| 27 break; | 25 break; |
| 28 } | 26 } |
| 29 return NoChange(); | 27 return NoChange(); |
| 30 } | 28 } |
| 31 | 29 |
| 32 | 30 |
| 33 Reduction JSContextSpecialization::ReduceParameter(Node* node) { | 31 MaybeHandle<Context> JSContextSpecialization::GetSpecializationContext( |
| 34 DCHECK_EQ(IrOpcode::kParameter, node->opcode()); | 32 Node* node) { |
| 35 Node* const start = NodeProperties::GetValueInput(node, 0); | 33 DCHECK(node->opcode() == IrOpcode::kJSLoadContext || |
| 36 DCHECK_EQ(IrOpcode::kStart, start->opcode()); | 34 node->opcode() == IrOpcode::kJSStoreContext); |
| 37 int const index = ParameterIndexOf(node->op()); | 35 Node* const object = NodeProperties::GetValueInput(node, 0); |
| 38 // The context is always the last parameter to a JavaScript function, and | 36 switch (object->opcode()) { |
| 39 // {Parameter} indices start at -1, so value outputs of {Start} look like | 37 case IrOpcode::kHeapConstant: |
| 40 // this: closure, receiver, param0, ..., paramN, context. | 38 return Handle<Context>::cast( |
| 41 if (index == start->op()->ValueOutputCount() - 2) { | 39 OpParameter<Unique<HeapObject>>(object).handle()); |
| 42 Handle<Context> context_constant; | 40 case IrOpcode::kParameter: { |
| 43 if (context().ToHandle(&context_constant)) { | 41 Node* const start = NodeProperties::GetValueInput(object, 0); |
| 44 return Replace(jsgraph()->Constant(context_constant)); | 42 DCHECK_EQ(IrOpcode::kStart, start->opcode()); |
| 43 int const index = ParameterIndexOf(object->op()); |
| 44 // The context is always the last parameter to a JavaScript function, and |
| 45 // {Parameter} indices start at -1, so value outputs of {Start} look like |
| 46 // this: closure, receiver, param0, ..., paramN, context. |
| 47 if (index == start->op()->ValueOutputCount() - 2) { |
| 48 return context(); |
| 49 } |
| 50 break; |
| 45 } | 51 } |
| 52 default: |
| 53 break; |
| 46 } | 54 } |
| 47 return NoChange(); | 55 return MaybeHandle<Context>(); |
| 48 } | 56 } |
| 49 | 57 |
| 50 | 58 |
| 51 Reduction JSContextSpecialization::ReduceJSLoadContext(Node* node) { | 59 Reduction JSContextSpecialization::ReduceJSLoadContext(Node* node) { |
| 52 DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode()); | 60 DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode()); |
| 53 | 61 |
| 54 HeapObjectMatcher m(NodeProperties::GetValueInput(node, 0)); | 62 // Get the specialization context from the node. |
| 55 // If the context is not constant, no reduction can occur. | 63 Handle<Context> context; |
| 56 if (!m.HasValue()) { | 64 if (!GetSpecializationContext(node).ToHandle(&context)) return NoChange(); |
| 57 return NoChange(); | |
| 58 } | |
| 59 | |
| 60 const ContextAccess& access = ContextAccessOf(node->op()); | |
| 61 | 65 |
| 62 // Find the right parent context. | 66 // Find the right parent context. |
| 63 Handle<Context> context = Handle<Context>::cast(m.Value().handle()); | 67 const ContextAccess& access = ContextAccessOf(node->op()); |
| 64 for (size_t i = access.depth(); i > 0; --i) { | 68 for (size_t i = access.depth(); i > 0; --i) { |
| 65 context = handle(context->previous(), isolate()); | 69 context = handle(context->previous(), isolate()); |
| 66 } | 70 } |
| 67 | 71 |
| 68 // If the access itself is mutable, only fold-in the parent. | 72 // If the access itself is mutable, only fold-in the parent. |
| 69 if (!access.immutable()) { | 73 if (!access.immutable()) { |
| 70 // The access does not have to look up a parent, nothing to fold. | 74 // The access does not have to look up a parent, nothing to fold. |
| 71 if (access.depth() == 0) { | 75 if (access.depth() == 0) { |
| 72 return NoChange(); | 76 return NoChange(); |
| 73 } | 77 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 93 // contexts that have the same value in the corresponding context slot. | 97 // contexts that have the same value in the corresponding context slot. |
| 94 Node* constant = jsgraph_->Constant(value); | 98 Node* constant = jsgraph_->Constant(value); |
| 95 ReplaceWithValue(node, constant); | 99 ReplaceWithValue(node, constant); |
| 96 return Replace(constant); | 100 return Replace(constant); |
| 97 } | 101 } |
| 98 | 102 |
| 99 | 103 |
| 100 Reduction JSContextSpecialization::ReduceJSStoreContext(Node* node) { | 104 Reduction JSContextSpecialization::ReduceJSStoreContext(Node* node) { |
| 101 DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode()); | 105 DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode()); |
| 102 | 106 |
| 103 HeapObjectMatcher m(NodeProperties::GetValueInput(node, 0)); | 107 // Get the specialization context from the node. |
| 104 // If the context is not constant, no reduction can occur. | 108 Handle<Context> context; |
| 105 if (!m.HasValue()) { | 109 if (!GetSpecializationContext(node).ToHandle(&context)) return NoChange(); |
| 106 return NoChange(); | |
| 107 } | |
| 108 | |
| 109 const ContextAccess& access = ContextAccessOf(node->op()); | |
| 110 | 110 |
| 111 // The access does not have to look up a parent, nothing to fold. | 111 // The access does not have to look up a parent, nothing to fold. |
| 112 const ContextAccess& access = ContextAccessOf(node->op()); |
| 112 if (access.depth() == 0) { | 113 if (access.depth() == 0) { |
| 113 return NoChange(); | 114 return NoChange(); |
| 114 } | 115 } |
| 115 | 116 |
| 116 // Find the right parent context. | 117 // Find the right parent context. |
| 117 Handle<Context> context = Handle<Context>::cast(m.Value().handle()); | |
| 118 for (size_t i = access.depth(); i > 0; --i) { | 118 for (size_t i = access.depth(); i > 0; --i) { |
| 119 context = handle(context->previous(), isolate()); | 119 context = handle(context->previous(), isolate()); |
| 120 } | 120 } |
| 121 | 121 |
| 122 node->set_op(javascript()->StoreContext(0, access.index())); | 122 node->set_op(javascript()->StoreContext(0, access.index())); |
| 123 node->ReplaceInput(0, jsgraph_->Constant(context)); | 123 node->ReplaceInput(0, jsgraph_->Constant(context)); |
| 124 return Changed(node); | 124 return Changed(node); |
| 125 } | 125 } |
| 126 | 126 |
| 127 | 127 |
| 128 Isolate* JSContextSpecialization::isolate() const { | 128 Isolate* JSContextSpecialization::isolate() const { |
| 129 return jsgraph()->isolate(); | 129 return jsgraph()->isolate(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 | 132 |
| 133 JSOperatorBuilder* JSContextSpecialization::javascript() const { | 133 JSOperatorBuilder* JSContextSpecialization::javascript() const { |
| 134 return jsgraph()->javascript(); | 134 return jsgraph()->javascript(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 } // namespace compiler | 137 } // namespace compiler |
| 138 } // namespace internal | 138 } // namespace internal |
| 139 } // namespace v8 | 139 } // namespace v8 |
| OLD | NEW |