js-context-specialization.cc 7.32 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#include "src/compiler/js-context-specialization.h"

7
#include "src/compiler/common-operator.h"
8
#include "src/compiler/js-graph.h"
9
#include "src/compiler/js-operator.h"
10
#include "src/compiler/linkage.h"
11
#include "src/compiler/node-matchers.h"
12
#include "src/compiler/node-properties.h"
13
#include "src/contexts-inl.h"
14 15 16 17 18

namespace v8 {
namespace internal {
namespace compiler {

19 20
Reduction JSContextSpecialization::Reduce(Node* node) {
  switch (node->opcode()) {
21 22
    case IrOpcode::kParameter:
      return ReduceParameter(node);
23 24 25 26 27 28
    case IrOpcode::kJSLoadContext:
      return ReduceJSLoadContext(node);
    case IrOpcode::kJSStoreContext:
      return ReduceJSStoreContext(node);
    default:
      break;
29
  }
30 31 32
  return NoChange();
}

33 34 35 36 37 38 39 40 41 42 43 44 45 46
Reduction JSContextSpecialization::ReduceParameter(Node* node) {
  DCHECK_EQ(IrOpcode::kParameter, node->opcode());
  int const index = ParameterIndexOf(node->op());
  if (index == Linkage::kJSCallClosureParamIndex) {
    // Constant-fold the function parameter {node}.
    Handle<JSFunction> function;
    if (closure().ToHandle(&function)) {
      Node* value = jsgraph()->HeapConstant(function);
      return Replace(value);
    }
  }
  return NoChange();
}

47 48 49 50 51 52 53 54 55 56 57
Reduction JSContextSpecialization::SimplifyJSLoadContext(Node* node,
                                                         Node* new_context,
                                                         size_t new_depth) {
  DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode());
  const ContextAccess& access = ContextAccessOf(node->op());
  DCHECK_LE(new_depth, access.depth());

  if (new_depth == access.depth() &&
      new_context == NodeProperties::GetContextInput(node)) {
    return NoChange();
  }
58

59 60 61 62 63
  const Operator* op = jsgraph_->javascript()->LoadContext(
      new_depth, access.index(), access.immutable());
  NodeProperties::ReplaceContextInput(node, new_context);
  NodeProperties::ChangeOp(node, op);
  return Changed(node);
64 65
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
Reduction JSContextSpecialization::SimplifyJSStoreContext(Node* node,
                                                          Node* new_context,
                                                          size_t new_depth) {
  DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode());
  const ContextAccess& access = ContextAccessOf(node->op());
  DCHECK_LE(new_depth, access.depth());

  if (new_depth == access.depth() &&
      new_context == NodeProperties::GetContextInput(node)) {
    return NoChange();
  }

  const Operator* op =
      jsgraph_->javascript()->StoreContext(new_depth, access.index());
  NodeProperties::ReplaceContextInput(node, new_context);
  NodeProperties::ChangeOp(node, op);
  return Changed(node);
}
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
namespace {

bool IsContextParameter(Node* node) {
  DCHECK_EQ(IrOpcode::kParameter, node->opcode());
  Node* const start = NodeProperties::GetValueInput(node, 0);
  DCHECK_EQ(IrOpcode::kStart, start->opcode());
  int const index = ParameterIndexOf(node->op());
  // The context is always the last parameter to a JavaScript function, and
  // {Parameter} indices start at -1, so value outputs of {Start} look like
  // this: closure, receiver, param0, ..., paramN, context.
  return index == start->op()->ValueOutputCount() - 2;
}

// Given a context {node} and the {distance} from that context to the target
// context (which we want to read from or store to), try to return a
// specialization context.  If successful, update {distance} to whatever
// distance remains from the specialization context.
MaybeHandle<Context> GetSpecializationContext(Node* node, size_t* distance,
                                              Maybe<OuterContext> maybe_outer) {
  switch (node->opcode()) {
105 106 107 108 109
    case IrOpcode::kHeapConstant: {
      Handle<Object> object = OpParameter<Handle<HeapObject>>(node);
      if (object->IsContext()) return Handle<Context>::cast(object);
      break;
    }
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    case IrOpcode::kParameter: {
      OuterContext outer;
      if (maybe_outer.To(&outer) && IsContextParameter(node) &&
          *distance >= outer.distance) {
        *distance -= outer.distance;
        return outer.context;
      }
      break;
    }
    default:
      break;
  }
  return MaybeHandle<Context>();
}

}  // anonymous namespace

127
Reduction JSContextSpecialization::ReduceJSLoadContext(Node* node) {
128
  DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode());
129

130
  const ContextAccess& access = ContextAccessOf(node->op());
131 132 133
  size_t depth = access.depth();

  // First walk up the context chain in the graph as far as possible.
134
  Node* context = NodeProperties::GetOuterContext(node, &depth);
135 136

  Handle<Context> concrete;
137
  if (!GetSpecializationContext(context, &depth, outer()).ToHandle(&concrete)) {
138 139
    // We do not have a concrete context object, so we can only partially reduce
    // the load by folding-in the outer context node.
140
    return SimplifyJSLoadContext(node, context, depth);
141 142 143 144 145
  }

  // Now walk up the concrete context chain for the remaining depth.
  for (; depth > 0; --depth) {
    concrete = handle(concrete->previous(), isolate());
146 147 148
  }

  if (!access.immutable()) {
149 150 151
    // We found the requested context object but since the context slot is
    // mutable we can only partially reduce the load.
    return SimplifyJSLoadContext(node, jsgraph()->Constant(concrete), depth);
152 153 154 155
  }

  // Even though the context slot is immutable, the context might have escaped
  // before the function to which it belongs has initialized the slot.
156 157 158 159 160
  // We must be conservative and check if the value in the slot is currently
  // the hole or undefined. Only if it is neither of these, can we be sure that
  // it won't change anymore.
  Handle<Object> value(concrete->get(static_cast<int>(access.index())),
                       isolate());
161
  if (value->IsUndefined(isolate()) || value->IsTheHole(isolate())) {
162
    return SimplifyJSLoadContext(node, jsgraph()->Constant(concrete), depth);
163
  }
164 165 166 167

  // Success. The context load can be replaced with the constant.
  // TODO(titzer): record the specialization for sharing code across multiple
  // contexts that have the same value in the corresponding context slot.
168
  Node* constant = jsgraph_->Constant(value);
169
  ReplaceWithValue(node, constant);
170
  return Replace(constant);
171
}
172 173


174
Reduction JSContextSpecialization::ReduceJSStoreContext(Node* node) {
175 176
  DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode());

177
  const ContextAccess& access = ContextAccessOf(node->op());
178 179 180 181
  size_t depth = access.depth();

  // First walk up the context chain in the graph until we reduce the depth to 0
  // or hit a node that does not have a CreateXYZContext operator.
182
  Node* context = NodeProperties::GetOuterContext(node, &depth);
183 184

  Handle<Context> concrete;
185
  if (!GetSpecializationContext(context, &depth, outer()).ToHandle(&concrete)) {
186 187
    // We do not have a concrete context object, so we can only partially reduce
    // the load by folding-in the outer context node.
188
    return SimplifyJSStoreContext(node, context, depth);
189 190
  }

191 192 193
  // Now walk up the concrete context chain for the remaining depth.
  for (; depth > 0; --depth) {
    concrete = handle(concrete->previous(), isolate());
194 195
  }

196
  return SimplifyJSStoreContext(node, jsgraph()->Constant(concrete), depth);
197
}
198

199

200 201 202
Isolate* JSContextSpecialization::isolate() const {
  return jsgraph()->isolate();
}
203

204 205 206
}  // namespace compiler
}  // namespace internal
}  // namespace v8