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.
5 #ifndef V8_COMPILER_GENERIC_NODE_H_
6 #define V8_COMPILER_GENERIC_NODE_H_
12 #include "src/compiler/operator.h"
14 #include "src/zone-allocator.h"
21 class GenericGraphBase;
25 // A GenericNode<> is the basic primitive of graphs. GenericNode's are
26 // chained together by input/use chains but by default otherwise contain only an
27 // identifying number which specific applications of graphs and nodes can use
28 // to index auxiliary out-of-line data, especially transient data.
29 // Specializations of the templatized GenericNode<> class must provide a base
30 // class B that contains all of the members to be made available in each
31 // specialized Node instance. GenericNode uses a mixin template pattern to
32 // ensure that common accessors and methods expect the derived class S type
33 // rather than the GenericNode<B, S> type.
34 template <class B, class S>
35 class GenericNode : public B {
38 typedef S DerivedClass;
40 inline NodeId id() const { return id_; }
42 int InputCount() const { return input_count_; }
43 S* InputAt(int index) const {
44 return static_cast<S*>(GetInputRecordPtr(index)->to);
46 void ReplaceInput(int index, GenericNode* new_input);
47 void AppendInput(Zone* zone, GenericNode* new_input);
48 void InsertInput(Zone* zone, int index, GenericNode* new_input);
50 int UseCount() { return use_count_; }
52 DCHECK(index < use_count_);
53 Use* current = first_use_;
54 while (index-- != 0) {
55 current = current->next;
57 return static_cast<S*>(current->from);
59 inline void ReplaceUses(GenericNode* replace_to);
60 template <class UnaryPredicate>
61 inline void ReplaceUsesIf(UnaryPredicate pred, GenericNode* replace_to);
62 void RemoveAllInputs();
64 void TrimInputCount(int input_count);
72 explicit Inputs(GenericNode* node) : node_(node) {}
78 Inputs inputs() { return Inputs(this); }
85 bool empty() { return begin() == end(); }
87 explicit Uses(GenericNode* node) : node_(node) {}
93 Uses uses() { return Uses(this); }
97 bool OwnedBy(GenericNode* owner) const;
99 static S* New(GenericGraphBase* graph, int input_count, S** inputs);
102 friend class GenericGraphBase;
104 class Use : public ZoneObject {
117 void Update(GenericNode* new_to);
120 void EnsureAppendableInputs(Zone* zone);
122 Input* GetInputRecordPtr(int index) const {
123 if (has_appendable_inputs_) {
124 return &((*inputs_.appendable_)[index]);
126 return inputs_.static_ + index;
130 void AppendUse(Use* use);
131 void RemoveUse(Use* use);
133 void* operator new(size_t, void* location) { return location; }
135 GenericNode(GenericGraphBase* graph, int input_count);
138 void AssignUniqueID(GenericGraphBase* graph);
140 typedef zone_allocator<Input> ZoneInputAllocator;
141 typedef std::deque<Input, ZoneInputAllocator> InputDeque;
144 int input_count_ : 31;
145 bool has_appendable_inputs_ : 1;
147 // When a node is initially allocated, it uses a static buffer to hold its
148 // inputs under the assumption that the number of outputs will not increase.
149 // When the first input is appended, the static buffer is converted into a
150 // deque to allow for space-efficient growing.
152 InputDeque* appendable_;
158 DISALLOW_COPY_AND_ASSIGN(GenericNode);
161 // An encapsulation for information associated with a single use of node as a
162 // input from another node, allowing access to both the defining node and
163 // the ndoe having the input.
164 template <class B, class S>
165 class GenericNode<B, S>::Edge {
167 S* from() const { return static_cast<S*>(input_->use->from); }
168 S* to() const { return static_cast<S*>(input_->to); }
170 int index = input_->use->input_index;
171 DCHECK(index < input_->use->from->input_count_);
176 friend class GenericNode<B, S>::Uses::iterator;
177 friend class GenericNode<B, S>::Inputs::iterator;
179 explicit Edge(typename GenericNode<B, S>::Input* input) : input_(input) {}
181 typename GenericNode<B, S>::Input* input_;
184 // A forward iterator to visit the nodes which are depended upon by a node
185 // in the order of input.
186 template <class B, class S>
187 class GenericNode<B, S>::Inputs::iterator {
189 iterator(const typename GenericNode<B, S>::Inputs::iterator& other) // NOLINT
190 : node_(other.node_),
191 index_(other.index_) {}
193 S* operator*() { return static_cast<S*>(GetInput()->to); }
194 typename GenericNode<B, S>::Edge edge() {
195 return typename GenericNode::Edge(GetInput());
197 bool operator==(const iterator& other) const {
198 return other.index_ == index_ && other.node_ == node_;
200 bool operator!=(const iterator& other) const { return !(other == *this); }
201 iterator& operator++() {
202 DCHECK(node_ != NULL);
203 DCHECK(index_ < node_->input_count_);
207 int index() { return index_; }
210 friend class GenericNode;
212 explicit iterator(GenericNode* node, int index)
213 : node_(node), index_(index) {}
215 Input* GetInput() const { return node_->GetInputRecordPtr(index_); }
221 // A forward iterator to visit the uses of a node. The uses are returned in
222 // the order in which they were added as inputs.
223 template <class B, class S>
224 class GenericNode<B, S>::Uses::iterator {
226 iterator(const typename GenericNode<B, S>::Uses::iterator& other) // NOLINT
227 : current_(other.current_),
228 index_(other.index_) {}
230 S* operator*() { return static_cast<S*>(current_->from); }
231 typename GenericNode<B, S>::Edge edge() {
232 return typename GenericNode::Edge(CurrentInput());
235 bool operator==(const iterator& other) { return other.current_ == current_; }
236 bool operator!=(const iterator& other) { return other.current_ != current_; }
237 iterator& operator++() {
238 DCHECK(current_ != NULL);
240 current_ = current_->next;
243 iterator& UpdateToAndIncrement(GenericNode<B, S>* new_to) {
244 DCHECK(current_ != NULL);
246 typename GenericNode<B, S>::Input* input = CurrentInput();
247 current_ = current_->next;
248 input->Update(new_to);
251 int index() const { return index_; }
254 friend class GenericNode<B, S>::Uses;
256 iterator() : current_(NULL), index_(0) {}
257 explicit iterator(GenericNode<B, S>* node)
258 : current_(node->first_use_), index_(0) {}
260 Input* CurrentInput() const {
261 return current_->from->GetInputRecordPtr(current_->input_index);
264 typename GenericNode<B, S>::Use* current_;
269 } // namespace v8::internal::compiler
271 #endif // V8_COMPILER_GENERIC_NODE_H_