11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/compiler/node-observer.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/compiler/node-properties.h"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cinamespace v8 {
101cb0ef41Sopenharmony_cinamespace internal {
111cb0ef41Sopenharmony_cinamespace compiler {
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciObservableNodeState::ObservableNodeState(const Node* node, Zone* zone)
141cb0ef41Sopenharmony_ci    : id_(node->id()),
151cb0ef41Sopenharmony_ci      op_(node->op()),
161cb0ef41Sopenharmony_ci      type_(NodeProperties::GetTypeOrAny(node)) {}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_civoid ObserveNodeManager::StartObserving(Node* node, NodeObserver* observer) {
191cb0ef41Sopenharmony_ci  DCHECK_NOT_NULL(node);
201cb0ef41Sopenharmony_ci  DCHECK_NOT_NULL(observer);
211cb0ef41Sopenharmony_ci  DCHECK(observations_.find(node->id()) == observations_.end());
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  observer->set_has_observed_changes();
241cb0ef41Sopenharmony_ci  NodeObserver::Observation observation = observer->OnNodeCreated(node);
251cb0ef41Sopenharmony_ci  if (observation == NodeObserver::Observation::kContinue) {
261cb0ef41Sopenharmony_ci    observations_[node->id()] =
271cb0ef41Sopenharmony_ci        zone_->New<NodeObservation>(observer, node, zone_);
281cb0ef41Sopenharmony_ci  } else {
291cb0ef41Sopenharmony_ci    DCHECK_EQ(observation, NodeObserver::Observation::kStop);
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_civoid ObserveNodeManager::OnNodeChanged(const char* reducer_name,
341cb0ef41Sopenharmony_ci                                       const Node* old_node,
351cb0ef41Sopenharmony_ci                                       const Node* new_node) {
361cb0ef41Sopenharmony_ci  const auto it = observations_.find(old_node->id());
371cb0ef41Sopenharmony_ci  if (it == observations_.end()) return;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  ObservableNodeState new_state{new_node, zone_};
401cb0ef41Sopenharmony_ci  NodeObservation* observation = it->second;
411cb0ef41Sopenharmony_ci  if (observation->state == new_state) return;
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  ObservableNodeState old_state = observation->state;
441cb0ef41Sopenharmony_ci  observation->state = new_state;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  NodeObserver::Observation result =
471cb0ef41Sopenharmony_ci      observation->observer->OnNodeChanged(reducer_name, new_node, old_state);
481cb0ef41Sopenharmony_ci  if (result == NodeObserver::Observation::kStop) {
491cb0ef41Sopenharmony_ci    observations_.erase(old_node->id());
501cb0ef41Sopenharmony_ci  } else {
511cb0ef41Sopenharmony_ci    DCHECK_EQ(result, NodeObserver::Observation::kContinue);
521cb0ef41Sopenharmony_ci    if (old_node != new_node) {
531cb0ef41Sopenharmony_ci      observations_.erase(old_node->id());
541cb0ef41Sopenharmony_ci      observations_[new_node->id()] = observation;
551cb0ef41Sopenharmony_ci    }
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci}  // namespace compiler
601cb0ef41Sopenharmony_ci}  // namespace internal
611cb0ef41Sopenharmony_ci}  // namespace v8
62