| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2013 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_GRAPH_H_ |
| 6 | #define V8_COMPILER_GRAPH_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | #include "src/compiler/node.h" |
| 12 | #include "src/compiler/node-aux-data.h" |
| 13 | #include "src/compiler/source-position.h" |
| 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | namespace compiler { |
| 18 | |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 19 | // Forward declarations. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | class GraphDecorator; |
| 21 | |
| 22 | |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 23 | class Graph : public ZoneObject { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | public: |
| 25 | explicit Graph(Zone* zone); |
| 26 | |
| 27 | // Base implementation used by all factory methods. |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 28 | Node* NewNode(const Operator* op, int input_count, Node** inputs, |
| 29 | bool incomplete = false); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | |
| 31 | // Factories for nodes with static input counts. |
| 32 | Node* NewNode(const Operator* op) { |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 33 | return NewNode(op, 0, static_cast<Node**>(nullptr)); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | } |
| 35 | Node* NewNode(const Operator* op, Node* n1) { return NewNode(op, 1, &n1); } |
| 36 | Node* NewNode(const Operator* op, Node* n1, Node* n2) { |
| 37 | Node* nodes[] = {n1, n2}; |
| 38 | return NewNode(op, arraysize(nodes), nodes); |
| 39 | } |
| 40 | Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3) { |
| 41 | Node* nodes[] = {n1, n2, n3}; |
| 42 | return NewNode(op, arraysize(nodes), nodes); |
| 43 | } |
| 44 | Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4) { |
| 45 | Node* nodes[] = {n1, n2, n3, n4}; |
| 46 | return NewNode(op, arraysize(nodes), nodes); |
| 47 | } |
| 48 | Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4, |
| 49 | Node* n5) { |
| 50 | Node* nodes[] = {n1, n2, n3, n4, n5}; |
| 51 | return NewNode(op, arraysize(nodes), nodes); |
| 52 | } |
| 53 | Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4, |
| 54 | Node* n5, Node* n6) { |
| 55 | Node* nodes[] = {n1, n2, n3, n4, n5, n6}; |
| 56 | return NewNode(op, arraysize(nodes), nodes); |
| 57 | } |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 58 | Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4, |
| 59 | Node* n5, Node* n6, Node* n7) { |
| 60 | Node* nodes[] = {n1, n2, n3, n4, n5, n6, n7}; |
| 61 | return NewNode(op, arraysize(nodes), nodes); |
| 62 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 63 | |
| 64 | template <class Visitor> |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 65 | inline void VisitNodeInputsFromEnd(Visitor* visitor); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 66 | |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 67 | Zone* zone() const { return zone_; } |
| 68 | Node* start() const { return start_; } |
| 69 | Node* end() const { return end_; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 70 | |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 71 | void SetStart(Node* start) { start_ = start; } |
| 72 | void SetEnd(Node* end) { end_ = end; } |
| 73 | |
| 74 | NodeId NextNodeID() { return next_node_id_++; } |
| 75 | NodeId NodeCount() const { return next_node_id_; } |
| 76 | |
| 77 | void Decorate(Node* node); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 78 | |
| 79 | void AddDecorator(GraphDecorator* decorator) { |
| 80 | decorators_.push_back(decorator); |
| 81 | } |
| 82 | |
| 83 | void RemoveDecorator(GraphDecorator* decorator) { |
| 84 | ZoneVector<GraphDecorator*>::iterator it = |
| 85 | std::find(decorators_.begin(), decorators_.end(), decorator); |
| 86 | DCHECK(it != decorators_.end()); |
| 87 | decorators_.erase(it, it + 1); |
| 88 | } |
| 89 | |
| 90 | private: |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 91 | template <typename State> |
| 92 | friend class NodeMarker; |
| 93 | |
| 94 | Zone* zone_; |
| 95 | Node* start_; |
| 96 | Node* end_; |
| 97 | Mark mark_max_; |
| 98 | NodeId next_node_id_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 99 | ZoneVector<GraphDecorator*> decorators_; |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(Graph); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 105 | // A NodeMarker uses monotonically increasing marks to assign local "states" |
| 106 | // to nodes. Only one NodeMarker per graph is valid at a given time. |
| 107 | template <typename State> |
| 108 | class NodeMarker BASE_EMBEDDED { |
| 109 | public: |
| 110 | NodeMarker(Graph* graph, uint32_t num_states) |
| 111 | : mark_min_(graph->mark_max_), mark_max_(graph->mark_max_ += num_states) { |
| 112 | DCHECK(num_states > 0); // user error! |
| 113 | DCHECK(mark_max_ > mark_min_); // check for wraparound. |
| 114 | } |
| 115 | |
| 116 | State Get(Node* node) { |
| 117 | Mark mark = node->mark(); |
| 118 | if (mark < mark_min_) { |
| 119 | mark = mark_min_; |
| 120 | node->set_mark(mark_min_); |
| 121 | } |
| 122 | DCHECK_LT(mark, mark_max_); |
| 123 | return static_cast<State>(mark - mark_min_); |
| 124 | } |
| 125 | |
| 126 | void Set(Node* node, State state) { |
| 127 | Mark local = static_cast<Mark>(state); |
| 128 | DCHECK(local < (mark_max_ - mark_min_)); |
| 129 | DCHECK_LT(node->mark(), mark_max_); |
| 130 | node->set_mark(local + mark_min_); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | Mark mark_min_; |
| 135 | Mark mark_max_; |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | // A graph decorator can be used to add behavior to the creation of nodes |
| 140 | // in a graph. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 141 | class GraphDecorator : public ZoneObject { |
| 142 | public: |
| 143 | virtual ~GraphDecorator() {} |
| 144 | virtual void Decorate(Node* node) = 0; |
| 145 | }; |
| 146 | |
| 147 | } // namespace compiler |
| 148 | } // namespace internal |
| 149 | } // namespace v8 |
| 150 | |
| 151 | #endif // V8_COMPILER_GRAPH_H_ |