1 // Copyright 2021 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
5 #include "src/compiler/node-observer.h"
6
7 #include "src/compiler/node-properties.h"
8
9 namespace v8 {
10 namespace internal {
11 namespace compiler {
12
ObservableNodeState(const Node * node,Zone * zone)13 ObservableNodeState::ObservableNodeState(const Node* node, Zone* zone)
14 : id_(node->id()),
15 op_(node->op()),
16 type_(NodeProperties::GetTypeOrAny(node)) {}
17
StartObserving(Node * node,NodeObserver * observer)18 void ObserveNodeManager::StartObserving(Node* node, NodeObserver* observer) {
19 DCHECK_NOT_NULL(node);
20 DCHECK_NOT_NULL(observer);
21 DCHECK(observations_.find(node->id()) == observations_.end());
22
23 observer->set_has_observed_changes();
24 NodeObserver::Observation observation = observer->OnNodeCreated(node);
25 if (observation == NodeObserver::Observation::kContinue) {
26 observations_[node->id()] =
27 zone_->New<NodeObservation>(observer, node, zone_);
28 } else {
29 DCHECK_EQ(observation, NodeObserver::Observation::kStop);
30 }
31 }
32
OnNodeChanged(const char * reducer_name,const Node * old_node,const Node * new_node)33 void ObserveNodeManager::OnNodeChanged(const char* reducer_name,
34 const Node* old_node,
35 const Node* new_node) {
36 const auto it = observations_.find(old_node->id());
37 if (it == observations_.end()) return;
38
39 ObservableNodeState new_state{new_node, zone_};
40 NodeObservation* observation = it->second;
41 if (observation->state == new_state) return;
42
43 ObservableNodeState old_state = observation->state;
44 observation->state = new_state;
45
46 NodeObserver::Observation result =
47 observation->observer->OnNodeChanged(reducer_name, new_node, old_state);
48 if (result == NodeObserver::Observation::kStop) {
49 observations_.erase(old_node->id());
50 } else {
51 DCHECK_EQ(result, NodeObserver::Observation::kContinue);
52 if (old_node != new_node) {
53 observations_.erase(old_node->id());
54 observations_[new_node->id()] = observation;
55 }
56 }
57 }
58
59 } // namespace compiler
60 } // namespace internal
61 } // namespace v8
62