node-observer.cc 1.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright 2021 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/node-observer.h"

#include "src/compiler/node-properties.h"

namespace v8 {
namespace internal {
namespace compiler {

ObservableNodeState::ObservableNodeState(const Node* node, Zone* zone)
    : id_(node->id()),
      op_(node->op()),
      type_(NodeProperties::GetTypeOrAny(node)) {}

void ObserveNodeManager::StartObserving(Node* node, NodeObserver* observer) {
  DCHECK_NOT_NULL(node);
  DCHECK_NOT_NULL(observer);
  DCHECK(observations_.find(node->id()) == observations_.end());

23
  observer->set_has_observed_changes();
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  NodeObserver::Observation observation = observer->OnNodeCreated(node);
  if (observation == NodeObserver::Observation::kContinue) {
    observations_[node->id()] =
        zone_->New<NodeObservation>(observer, node, zone_);
  } else {
    DCHECK_EQ(observation, NodeObserver::Observation::kStop);
  }
}

void ObserveNodeManager::OnNodeChanged(const char* reducer_name,
                                       const Node* old_node,
                                       const Node* new_node) {
  const auto it = observations_.find(old_node->id());
  if (it == observations_.end()) return;

  ObservableNodeState new_state{new_node, zone_};
  NodeObservation* observation = it->second;
  if (observation->state == new_state) return;

  ObservableNodeState old_state = observation->state;
  observation->state = new_state;

  NodeObserver::Observation result =
      observation->observer->OnNodeChanged(reducer_name, new_node, old_state);
  if (result == NodeObserver::Observation::kStop) {
    observations_.erase(old_node->id());
  } else {
    DCHECK_EQ(result, NodeObserver::Observation::kContinue);
    if (old_node != new_node) {
      observations_.erase(old_node->id());
      observations_[new_node->id()] = observation;
    }
  }
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8