OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/compiler/common-operator.h" |
| 6 #include "src/compiler/generic-node-inl.h" |
| 7 #include "src/compiler/js-context-specialization.h" |
| 8 #include "src/compiler/js-operator.h" |
| 9 #include "src/compiler/node-aux-data-inl.h" |
| 10 #include "src/compiler/node-matchers.h" |
| 11 #include "src/compiler/node-properties-inl.h" |
| 12 |
| 13 namespace v8 { |
| 14 namespace internal { |
| 15 namespace compiler { |
| 16 |
| 17 // TODO(titzer): factor this out to a common routine with js-typed-lowering. |
| 18 static void ReplaceEffectfulWithValue(Node* node, Node* value) { |
| 19 Node* effect = NodeProperties::GetEffectInput(node); |
| 20 |
| 21 // Requires distinguishing between value and effect edges. |
| 22 UseIter iter = node->uses().begin(); |
| 23 while (iter != node->uses().end()) { |
| 24 if (NodeProperties::IsEffectEdge(iter.edge())) { |
| 25 iter = iter.UpdateToAndIncrement(effect); |
| 26 } else { |
| 27 iter = iter.UpdateToAndIncrement(value); |
| 28 } |
| 29 } |
| 30 } |
| 31 |
| 32 |
| 33 void JSContextSpecializer::SpecializeToContext() { |
| 34 ValueMatcher<Handle<Context> > match(context_); |
| 35 |
| 36 // Iterate over all uses of the context and try to replace {LoadContext} |
| 37 // nodes with their values from the constant context. |
| 38 UseIter iter = match.node()->uses().begin(); |
| 39 while (iter != match.node()->uses().end()) { |
| 40 Node* use = *iter; |
| 41 if (use->opcode() == IrOpcode::kJSLoadContext) { |
| 42 Reduction r = ReduceJSLoadContext(use); |
| 43 if (r.Changed() && r.replacement() != use) { |
| 44 ReplaceEffectfulWithValue(use, r.replacement()); |
| 45 } |
| 46 } |
| 47 ++iter; |
| 48 } |
| 49 } |
| 50 |
| 51 |
| 52 Reduction JSContextSpecializer::ReduceJSLoadContext(Node* node) { |
| 53 ASSERT_EQ(IrOpcode::kJSLoadContext, node->opcode()); |
| 54 |
| 55 ContextAccess access = |
| 56 static_cast<Operator1<ContextAccess>*>(node->op())->parameter(); |
| 57 |
| 58 // Find the right parent context. |
| 59 Context* context = *info_->context(); |
| 60 for (int i = access.depth(); i > 0; --i) { |
| 61 context = context->previous(); |
| 62 } |
| 63 |
| 64 // If the access itself is mutable, only fold-in the parent. |
| 65 if (!access.immutable()) { |
| 66 // The access does not have to look up a parent, nothing to fold. |
| 67 if (access.depth() == 0) { |
| 68 return Reducer::NoChange(); |
| 69 } |
| 70 Operator* op = jsgraph_->javascript()->LoadContext(0, access.index(), |
| 71 access.immutable()); |
| 72 node->set_op(op); |
| 73 Handle<Object> context_handle = Handle<Object>(context, info_->isolate()); |
| 74 node->ReplaceInput(0, jsgraph_->Constant(context_handle)); |
| 75 return Reducer::Changed(node); |
| 76 } |
| 77 Handle<Object> value = |
| 78 Handle<Object>(context->get(access.index()), info_->isolate()); |
| 79 |
| 80 // Even though the context slot is immutable, the context might have escaped |
| 81 // before the function to which it belongs has initialized the slot. |
| 82 // We must be conservative and check if the value in the slot is currently the |
| 83 // hole or undefined. If it is neither of these, then it must be initialized. |
| 84 if (value->IsUndefined() || value->IsTheHole()) return Reducer::NoChange(); |
| 85 |
| 86 // Success. The context load can be replaced with the constant. |
| 87 // TODO(titzer): record the specialization for sharing code across multiple |
| 88 // contexts that have the same value in the corresponding context slot. |
| 89 return Reducer::Replace(jsgraph_->Constant(value)); |
| 90 } |
| 91 } |
| 92 } |
| 93 } // namespace v8::internal::compiler |
OLD | NEW |