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" |
(...skipping 10 matching lines...) Expand all Loading... |
21 case IrOpcode::kJSLoadContext: | 21 case IrOpcode::kJSLoadContext: |
22 return ReduceJSLoadContext(node); | 22 return ReduceJSLoadContext(node); |
23 case IrOpcode::kJSStoreContext: | 23 case IrOpcode::kJSStoreContext: |
24 return ReduceJSStoreContext(node); | 24 return ReduceJSStoreContext(node); |
25 default: | 25 default: |
26 break; | 26 break; |
27 } | 27 } |
28 return NoChange(); | 28 return NoChange(); |
29 } | 29 } |
30 | 30 |
| 31 Reduction JSContextSpecialization::SimplifyJSLoadContext(Node* node, |
| 32 Node* new_context, |
| 33 size_t new_depth) { |
| 34 DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode()); |
| 35 const ContextAccess& access = ContextAccessOf(node->op()); |
| 36 DCHECK_LE(new_depth, access.depth()); |
31 | 37 |
32 MaybeHandle<Context> JSContextSpecialization::GetSpecializationContext( | 38 if (new_depth == access.depth() && |
33 Node* node) { | 39 new_context == NodeProperties::GetContextInput(node)) { |
34 DCHECK(node->opcode() == IrOpcode::kJSLoadContext || | 40 return NoChange(); |
35 node->opcode() == IrOpcode::kJSStoreContext); | 41 } |
36 Node* const object = NodeProperties::GetContextInput(node); | 42 |
37 return NodeProperties::GetSpecializationContext(object, context()); | 43 const Operator* op = jsgraph_->javascript()->LoadContext( |
| 44 new_depth, access.index(), access.immutable()); |
| 45 NodeProperties::ReplaceContextInput(node, new_context); |
| 46 NodeProperties::ChangeOp(node, op); |
| 47 return Changed(node); |
38 } | 48 } |
39 | 49 |
| 50 Reduction JSContextSpecialization::SimplifyJSStoreContext(Node* node, |
| 51 Node* new_context, |
| 52 size_t new_depth) { |
| 53 DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode()); |
| 54 const ContextAccess& access = ContextAccessOf(node->op()); |
| 55 DCHECK_LE(new_depth, access.depth()); |
| 56 |
| 57 if (new_depth == access.depth() && |
| 58 new_context == NodeProperties::GetContextInput(node)) { |
| 59 return NoChange(); |
| 60 } |
| 61 |
| 62 const Operator* op = |
| 63 jsgraph_->javascript()->StoreContext(new_depth, access.index()); |
| 64 NodeProperties::ReplaceContextInput(node, new_context); |
| 65 NodeProperties::ChangeOp(node, op); |
| 66 return Changed(node); |
| 67 } |
40 | 68 |
41 Reduction JSContextSpecialization::ReduceJSLoadContext(Node* node) { | 69 Reduction JSContextSpecialization::ReduceJSLoadContext(Node* node) { |
42 DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode()); | 70 DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode()); |
43 | 71 |
44 // Get the specialization context from the node. | 72 const ContextAccess& access = ContextAccessOf(node->op()); |
45 Handle<Context> context; | 73 size_t depth = access.depth(); |
46 if (!GetSpecializationContext(node).ToHandle(&context)) return NoChange(); | |
47 | 74 |
48 // Find the right parent context. | 75 // First walk up the context chain in the graph as far as possible. |
49 const ContextAccess& access = ContextAccessOf(node->op()); | 76 Node* outer = NodeProperties::GetOuterContext(node, &depth); |
50 for (size_t i = access.depth(); i > 0; --i) { | 77 |
51 context = handle(context->previous(), isolate()); | 78 Handle<Context> concrete; |
| 79 if (!NodeProperties::GetSpecializationContext(outer, context()) |
| 80 .ToHandle(&concrete)) { |
| 81 // We do not have a concrete context object, so we can only partially reduce |
| 82 // the load by folding-in the outer context node. |
| 83 return SimplifyJSLoadContext(node, outer, depth); |
52 } | 84 } |
53 | 85 |
54 // If the access itself is mutable, only fold-in the parent. | 86 // Now walk up the concrete context chain for the remaining depth. |
| 87 for (; depth > 0; --depth) { |
| 88 concrete = handle(concrete->previous(), isolate()); |
| 89 } |
| 90 |
55 if (!access.immutable()) { | 91 if (!access.immutable()) { |
56 // The access does not have to look up a parent, nothing to fold. | 92 // We found the requested context object but since the context slot is |
57 if (access.depth() == 0) { | 93 // mutable we can only partially reduce the load. |
58 return NoChange(); | 94 return SimplifyJSLoadContext(node, jsgraph()->Constant(concrete), depth); |
59 } | |
60 const Operator* op = jsgraph_->javascript()->LoadContext( | |
61 0, access.index(), access.immutable()); | |
62 NodeProperties::ReplaceContextInput(node, jsgraph_->Constant(context)); | |
63 NodeProperties::ChangeOp(node, op); | |
64 return Changed(node); | |
65 } | 95 } |
66 Handle<Object> value = | |
67 handle(context->get(static_cast<int>(access.index())), isolate()); | |
68 | 96 |
69 // Even though the context slot is immutable, the context might have escaped | 97 // Even though the context slot is immutable, the context might have escaped |
70 // before the function to which it belongs has initialized the slot. | 98 // before the function to which it belongs has initialized the slot. |
71 // We must be conservative and check if the value in the slot is currently the | 99 // We must be conservative and check if the value in the slot is currently |
72 // hole or undefined. If it is neither of these, then it must be initialized. | 100 // the hole or undefined. Only if it is neither of these, can we be sure that |
| 101 // it won't change anymore. |
| 102 Handle<Object> value(concrete->get(static_cast<int>(access.index())), |
| 103 isolate()); |
73 if (value->IsUndefined(isolate()) || value->IsTheHole(isolate())) { | 104 if (value->IsUndefined(isolate()) || value->IsTheHole(isolate())) { |
74 return NoChange(); | 105 return SimplifyJSLoadContext(node, jsgraph()->Constant(concrete), depth); |
75 } | 106 } |
76 | 107 |
77 // Success. The context load can be replaced with the constant. | 108 // Success. The context load can be replaced with the constant. |
78 // TODO(titzer): record the specialization for sharing code across multiple | 109 // TODO(titzer): record the specialization for sharing code across multiple |
79 // contexts that have the same value in the corresponding context slot. | 110 // contexts that have the same value in the corresponding context slot. |
80 Node* constant = jsgraph_->Constant(value); | 111 Node* constant = jsgraph_->Constant(value); |
81 ReplaceWithValue(node, constant); | 112 ReplaceWithValue(node, constant); |
82 return Replace(constant); | 113 return Replace(constant); |
83 } | 114 } |
84 | 115 |
85 | 116 |
86 Reduction JSContextSpecialization::ReduceJSStoreContext(Node* node) { | 117 Reduction JSContextSpecialization::ReduceJSStoreContext(Node* node) { |
87 DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode()); | 118 DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode()); |
88 | 119 |
89 // Get the specialization context from the node. | 120 const ContextAccess& access = ContextAccessOf(node->op()); |
90 Handle<Context> context; | 121 size_t depth = access.depth(); |
91 if (!GetSpecializationContext(node).ToHandle(&context)) return NoChange(); | |
92 | 122 |
93 // The access does not have to look up a parent, nothing to fold. | 123 // First walk up the context chain in the graph until we reduce the depth to 0 |
94 const ContextAccess& access = ContextAccessOf(node->op()); | 124 // or hit a node that does not have a CreateXYZContext operator. |
95 if (access.depth() == 0) { | 125 Node* outer = NodeProperties::GetOuterContext(node, &depth); |
96 return NoChange(); | 126 |
| 127 Handle<Context> concrete; |
| 128 if (!NodeProperties::GetSpecializationContext(outer, context()) |
| 129 .ToHandle(&concrete)) { |
| 130 // We do not have a concrete context object, so we can only partially reduce |
| 131 // the load by folding-in the outer context node. |
| 132 return SimplifyJSStoreContext(node, outer, depth); |
97 } | 133 } |
98 | 134 |
99 // Find the right parent context. | 135 // Now walk up the concrete context chain for the remaining depth. |
100 for (size_t i = access.depth(); i > 0; --i) { | 136 for (; depth > 0; --depth) { |
101 context = handle(context->previous(), isolate()); | 137 concrete = handle(concrete->previous(), isolate()); |
102 } | 138 } |
103 | 139 |
104 NodeProperties::ReplaceContextInput(node, jsgraph_->Constant(context)); | 140 return SimplifyJSStoreContext(node, jsgraph()->Constant(concrete), depth); |
105 NodeProperties::ChangeOp(node, javascript()->StoreContext(0, access.index())); | |
106 return Changed(node); | |
107 } | 141 } |
108 | 142 |
109 | 143 |
110 Isolate* JSContextSpecialization::isolate() const { | 144 Isolate* JSContextSpecialization::isolate() const { |
111 return jsgraph()->isolate(); | 145 return jsgraph()->isolate(); |
112 } | 146 } |
113 | 147 |
114 | 148 |
115 JSOperatorBuilder* JSContextSpecialization::javascript() const { | 149 JSOperatorBuilder* JSContextSpecialization::javascript() const { |
116 return jsgraph()->javascript(); | 150 return jsgraph()->javascript(); |
117 } | 151 } |
118 | 152 |
119 } // namespace compiler | 153 } // namespace compiler |
120 } // namespace internal | 154 } // namespace internal |
121 } // namespace v8 | 155 } // namespace v8 |
OLD | NEW |