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