blob: 84666d5f076747d7e1be76ced39f8d7c10dd4c79 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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#ifndef V8_COMPILER_NODE_MARKER_H_
6#define V8_COMPILER_NODE_MARKER_H_
7
8#include "src/compiler/node.h"
9
10namespace v8 {
11namespace internal {
12namespace compiler {
13
14// Forward declarations.
15class Graph;
16
17
18// Base class for templatized NodeMarkers.
19class NodeMarkerBase {
20 public:
21 NodeMarkerBase(Graph* graph, uint32_t num_states);
22
23 V8_INLINE Mark Get(Node* node) {
24 Mark mark = node->mark();
25 if (mark < mark_min_) {
26 mark = mark_min_;
27 node->set_mark(mark_min_);
28 }
29 DCHECK_LT(mark, mark_max_);
30 return mark - mark_min_;
31 }
32 V8_INLINE void Set(Node* node, Mark mark) {
33 DCHECK_LT(mark, mark_max_ - mark_min_);
34 DCHECK_LT(node->mark(), mark_max_);
35 node->set_mark(mark + mark_min_);
36 }
37
38 private:
39 Mark const mark_min_;
40 Mark const mark_max_;
41
42 DISALLOW_COPY_AND_ASSIGN(NodeMarkerBase);
43};
44
Ben Murdoch61f157c2016-09-16 13:49:30 +010045// A NodeMarker assigns a local "state" to every node of a graph in constant
46// memory. Only one NodeMarker per graph is valid at a given time, that is,
47// after you create a NodeMarker you should no longer use NodeMarkers that
48// were created earlier. Internally, the local state is stored in the Node
49// structure.
50//
51// When you initialize a NodeMarker, all the local states are conceptually
52// set to State(0) in constant time.
53//
54// In its current implementation, in debug mode NodeMarker will try to
55// (efficiently) detect invalid use of an older NodeMarker. Namely, if you get
56// or set a node with a NodeMarker, and then get or set that node
57// with an older NodeMarker you will get a crash.
58//
59// GraphReducer uses a NodeMarker, so individual Reducers cannot use a
60// NodeMarker.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061template <typename State>
62class NodeMarker : public NodeMarkerBase {
63 public:
64 V8_INLINE NodeMarker(Graph* graph, uint32_t num_states)
65 : NodeMarkerBase(graph, num_states) {}
66
67 V8_INLINE State Get(Node* node) {
68 return static_cast<State>(NodeMarkerBase::Get(node));
69 }
70
71 V8_INLINE void Set(Node* node, State state) {
72 NodeMarkerBase::Set(node, static_cast<Mark>(state));
73 }
74};
75
76} // namespace compiler
77} // namespace internal
78} // namespace v8
79
80#endif // V8_COMPILER_NODE_MARKER_H_