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-operator.h" | 9 #include "src/compiler/js-operator.h" |
9 #include "src/compiler/node-matchers.h" | 10 #include "src/compiler/node-matchers.h" |
10 #include "src/compiler/node-properties.h" | 11 #include "src/compiler/node-properties.h" |
| 12 #include "src/contexts.h" |
11 | 13 |
12 namespace v8 { | 14 namespace v8 { |
13 namespace internal { | 15 namespace internal { |
14 namespace compiler { | 16 namespace compiler { |
15 | 17 |
16 Reduction JSContextSpecializer::Reduce(Node* node) { | 18 Reduction JSContextSpecializer::Reduce(Node* node) { |
17 if (node->opcode() == IrOpcode::kJSLoadContext) { | 19 if (node->opcode() == IrOpcode::kJSLoadContext) { |
18 return ReduceJSLoadContext(node); | 20 return ReduceJSLoadContext(node); |
19 } | 21 } |
20 if (node->opcode() == IrOpcode::kJSStoreContext) { | 22 if (node->opcode() == IrOpcode::kJSStoreContext) { |
21 return ReduceJSStoreContext(node); | 23 return ReduceJSStoreContext(node); |
22 } | 24 } |
23 return NoChange(); | 25 return NoChange(); |
24 } | 26 } |
25 | 27 |
26 | 28 |
27 Reduction JSContextSpecializer::ReduceJSLoadContext(Node* node) { | 29 Reduction JSContextSpecializer::ReduceJSLoadContext(Node* node) { |
28 DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode()); | 30 DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode()); |
29 | 31 |
30 HeapObjectMatcher<Context> m(NodeProperties::GetValueInput(node, 0)); | 32 HeapObjectMatcher m(NodeProperties::GetValueInput(node, 0)); |
31 // If the context is not constant, no reduction can occur. | 33 // If the context is not constant, no reduction can occur. |
32 if (!m.HasValue()) { | 34 if (!m.HasValue()) { |
33 return NoChange(); | 35 return NoChange(); |
34 } | 36 } |
35 | 37 |
36 const ContextAccess& access = ContextAccessOf(node->op()); | 38 const ContextAccess& access = ContextAccessOf(node->op()); |
37 | 39 |
38 // Find the right parent context. | 40 // Find the right parent context. |
39 Context* context = *m.Value().handle(); | 41 Handle<Context> context = Handle<Context>::cast(m.Value().handle()); |
40 for (size_t i = access.depth(); i > 0; --i) { | 42 for (size_t i = access.depth(); i > 0; --i) { |
41 context = context->previous(); | 43 context = handle(context->previous(), isolate()); |
42 } | 44 } |
43 | 45 |
44 // If the access itself is mutable, only fold-in the parent. | 46 // If the access itself is mutable, only fold-in the parent. |
45 if (!access.immutable()) { | 47 if (!access.immutable()) { |
46 // The access does not have to look up a parent, nothing to fold. | 48 // The access does not have to look up a parent, nothing to fold. |
47 if (access.depth() == 0) { | 49 if (access.depth() == 0) { |
48 return NoChange(); | 50 return NoChange(); |
49 } | 51 } |
50 const Operator* op = jsgraph_->javascript()->LoadContext( | 52 const Operator* op = jsgraph_->javascript()->LoadContext( |
51 0, access.index(), access.immutable()); | 53 0, access.index(), access.immutable()); |
52 node->set_op(op); | 54 node->set_op(op); |
53 Handle<Object> context_handle = | 55 node->ReplaceInput(0, jsgraph_->Constant(context)); |
54 Handle<Object>(context, jsgraph_->isolate()); | |
55 node->ReplaceInput(0, jsgraph_->Constant(context_handle)); | |
56 return Changed(node); | 56 return Changed(node); |
57 } | 57 } |
58 Handle<Object> value = Handle<Object>( | 58 Handle<Object> value = |
59 context->get(static_cast<int>(access.index())), jsgraph_->isolate()); | 59 handle(context->get(static_cast<int>(access.index())), isolate()); |
60 | 60 |
61 // Even though the context slot is immutable, the context might have escaped | 61 // Even though the context slot is immutable, the context might have escaped |
62 // before the function to which it belongs has initialized the slot. | 62 // before the function to which it belongs has initialized the slot. |
63 // We must be conservative and check if the value in the slot is currently the | 63 // We must be conservative and check if the value in the slot is currently the |
64 // hole or undefined. If it is neither of these, then it must be initialized. | 64 // hole or undefined. If it is neither of these, then it must be initialized. |
65 if (value->IsUndefined() || value->IsTheHole()) { | 65 if (value->IsUndefined() || value->IsTheHole()) { |
66 return NoChange(); | 66 return NoChange(); |
67 } | 67 } |
68 | 68 |
69 // Success. The context load can be replaced with the constant. | 69 // Success. The context load can be replaced with the constant. |
70 // TODO(titzer): record the specialization for sharing code across multiple | 70 // TODO(titzer): record the specialization for sharing code across multiple |
71 // contexts that have the same value in the corresponding context slot. | 71 // contexts that have the same value in the corresponding context slot. |
72 Node* constant = jsgraph_->Constant(value); | 72 Node* constant = jsgraph_->Constant(value); |
73 ReplaceWithValue(node, constant); | 73 ReplaceWithValue(node, constant); |
74 return Replace(constant); | 74 return Replace(constant); |
75 } | 75 } |
76 | 76 |
77 | 77 |
78 Reduction JSContextSpecializer::ReduceJSStoreContext(Node* node) { | 78 Reduction JSContextSpecializer::ReduceJSStoreContext(Node* node) { |
79 DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode()); | 79 DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode()); |
80 | 80 |
81 HeapObjectMatcher<Context> m(NodeProperties::GetValueInput(node, 0)); | 81 HeapObjectMatcher m(NodeProperties::GetValueInput(node, 0)); |
82 // If the context is not constant, no reduction can occur. | 82 // If the context is not constant, no reduction can occur. |
83 if (!m.HasValue()) { | 83 if (!m.HasValue()) { |
84 return NoChange(); | 84 return NoChange(); |
85 } | 85 } |
86 | 86 |
87 const ContextAccess& access = ContextAccessOf(node->op()); | 87 const ContextAccess& access = ContextAccessOf(node->op()); |
88 | 88 |
89 // The access does not have to look up a parent, nothing to fold. | 89 // The access does not have to look up a parent, nothing to fold. |
90 if (access.depth() == 0) { | 90 if (access.depth() == 0) { |
91 return NoChange(); | 91 return NoChange(); |
92 } | 92 } |
93 | 93 |
94 // Find the right parent context. | 94 // Find the right parent context. |
95 Context* context = *m.Value().handle(); | 95 Handle<Context> context = Handle<Context>::cast(m.Value().handle()); |
96 for (size_t i = access.depth(); i > 0; --i) { | 96 for (size_t i = access.depth(); i > 0; --i) { |
97 context = context->previous(); | 97 context = handle(context->previous(), isolate()); |
98 } | 98 } |
99 | 99 |
100 const Operator* op = jsgraph_->javascript()->StoreContext(0, access.index()); | 100 node->set_op(javascript()->StoreContext(0, access.index())); |
101 node->set_op(op); | 101 node->ReplaceInput(0, jsgraph_->Constant(context)); |
102 Handle<Object> new_context_handle = | 102 return Changed(node); |
103 Handle<Object>(context, jsgraph_->isolate()); | 103 } |
104 node->ReplaceInput(0, jsgraph_->Constant(new_context_handle)); | |
105 | 104 |
106 return Changed(node); | 105 |
| 106 Isolate* JSContextSpecializer::isolate() const { return jsgraph()->isolate(); } |
| 107 |
| 108 |
| 109 JSOperatorBuilder* JSContextSpecializer::javascript() const { |
| 110 return jsgraph()->javascript(); |
107 } | 111 } |
108 | 112 |
109 } // namespace compiler | 113 } // namespace compiler |
110 } // namespace internal | 114 } // namespace internal |
111 } // namespace v8 | 115 } // namespace v8 |
OLD | NEW |