js-context-specialization.cc 5.06 KB
Newer Older
1 2 3 4 5 6
// 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.

#include "src/compiler/common-operator.h"
#include "src/compiler/generic-node-inl.h"
7
#include "src/compiler/graph-inl.h"
8 9 10 11 12 13 14 15 16 17
#include "src/compiler/js-context-specialization.h"
#include "src/compiler/js-operator.h"
#include "src/compiler/node-aux-data-inl.h"
#include "src/compiler/node-matchers.h"
#include "src/compiler/node-properties-inl.h"

namespace v8 {
namespace internal {
namespace compiler {

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// TODO(titzer): factor this out to a common routine with js-typed-lowering.
static void ReplaceEffectfulWithValue(Node* node, Node* value) {
  Node* effect = NULL;
  if (OperatorProperties::HasEffectInput(node->op())) {
    effect = NodeProperties::GetEffectInput(node);
  }

  // Requires distinguishing between value and effect edges.
  UseIter iter = node->uses().begin();
  while (iter != node->uses().end()) {
    if (NodeProperties::IsEffectEdge(iter.edge())) {
      DCHECK_NE(NULL, effect);
      iter = iter.UpdateToAndIncrement(effect);
    } else {
      iter = iter.UpdateToAndIncrement(value);
    }
  }
}


38 39 40 41 42 43 44 45 46 47
class ContextSpecializationVisitor : public NullNodeVisitor {
 public:
  explicit ContextSpecializationVisitor(JSContextSpecializer* spec)
      : spec_(spec) {}

  GenericGraphVisit::Control Post(Node* node) {
    switch (node->opcode()) {
      case IrOpcode::kJSLoadContext: {
        Reduction r = spec_->ReduceJSLoadContext(node);
        if (r.Changed() && r.replacement() != node) {
48
          ReplaceEffectfulWithValue(node, r.replacement());
49 50 51 52 53 54
        }
        break;
      }
      case IrOpcode::kJSStoreContext: {
        Reduction r = spec_->ReduceJSStoreContext(node);
        if (r.Changed() && r.replacement() != node) {
55
          ReplaceEffectfulWithValue(node, r.replacement());
56 57
        }
        break;
58
      }
59 60
      default:
        break;
61
    }
62
    return GenericGraphVisit::CONTINUE;
63
  }
64 65 66 67 68 69 70

 private:
  JSContextSpecializer* spec_;
};


void JSContextSpecializer::SpecializeToContext() {
71
  ReplaceEffectfulWithValue(context_, jsgraph_->Constant(info_->context()));
72 73 74

  ContextSpecializationVisitor visitor(this);
  jsgraph_->graph()->VisitNodeInputsFromEnd(&visitor);
75 76 77 78
}


Reduction JSContextSpecializer::ReduceJSLoadContext(Node* node) {
79
  DCHECK_EQ(IrOpcode::kJSLoadContext, node->opcode());
80

81 82 83 84 85 86 87
  ValueMatcher<Handle<Context> > match(NodeProperties::GetValueInput(node, 0));
  // If the context is not constant, no reduction can occur.
  if (!match.HasValue()) {
    return Reducer::NoChange();
  }

  ContextAccess access = OpParameter<ContextAccess>(node);
88 89

  // Find the right parent context.
90
  Context* context = *match.Value();
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  for (int i = access.depth(); i > 0; --i) {
    context = context->previous();
  }

  // If the access itself is mutable, only fold-in the parent.
  if (!access.immutable()) {
    // The access does not have to look up a parent, nothing to fold.
    if (access.depth() == 0) {
      return Reducer::NoChange();
    }
    Operator* op = jsgraph_->javascript()->LoadContext(0, access.index(),
                                                       access.immutable());
    node->set_op(op);
    Handle<Object> context_handle = Handle<Object>(context, info_->isolate());
    node->ReplaceInput(0, jsgraph_->Constant(context_handle));
    return Reducer::Changed(node);
  }
  Handle<Object> value =
      Handle<Object>(context->get(access.index()), info_->isolate());

  // Even though the context slot is immutable, the context might have escaped
  // before the function to which it belongs has initialized the slot.
  // We must be conservative and check if the value in the slot is currently the
  // hole or undefined. If it is neither of these, then it must be initialized.
115 116 117
  if (value->IsUndefined() || value->IsTheHole()) {
    return Reducer::NoChange();
  }
118 119 120 121 122 123

  // 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.
  return Reducer::Replace(jsgraph_->Constant(value));
}
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154


Reduction JSContextSpecializer::ReduceJSStoreContext(Node* node) {
  DCHECK_EQ(IrOpcode::kJSStoreContext, node->opcode());

  ValueMatcher<Handle<Context> > match(NodeProperties::GetValueInput(node, 0));
  // If the context is not constant, no reduction can occur.
  if (!match.HasValue()) {
    return Reducer::NoChange();
  }

  ContextAccess access = OpParameter<ContextAccess>(node);

  // The access does not have to look up a parent, nothing to fold.
  if (access.depth() == 0) {
    return Reducer::NoChange();
  }

  // Find the right parent context.
  Context* context = *match.Value();
  for (int i = access.depth(); i > 0; --i) {
    context = context->previous();
  }

  Operator* op = jsgraph_->javascript()->StoreContext(0, access.index());
  node->set_op(op);
  Handle<Object> new_context_handle = Handle<Object>(context, info_->isolate());
  node->ReplaceInput(0, jsgraph_->Constant(new_context_handle));

  return Reducer::Changed(node);
}
155 156 157
}
}
}  // namespace v8::internal::compiler