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