| 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 | #include "src/compiler/node.h" |
| 6 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | namespace v8 { |
| 8 | namespace internal { |
| 9 | namespace compiler { |
| 10 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11 | Node::OutOfLineInputs* Node::OutOfLineInputs::New(Zone* zone, int capacity) { |
| 12 | size_t size = |
| 13 | sizeof(OutOfLineInputs) + capacity * (sizeof(Node*) + sizeof(Use)); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 14 | intptr_t raw_buffer = |
| 15 | reinterpret_cast<intptr_t>(zone->Allocate<Node::OutOfLineInputs>(size)); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16 | Node::OutOfLineInputs* outline = |
| 17 | reinterpret_cast<OutOfLineInputs*>(raw_buffer + capacity * sizeof(Use)); |
| 18 | outline->capacity_ = capacity; |
| 19 | outline->count_ = 0; |
| 20 | return outline; |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 21 | } |
| 22 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 23 | void Node::OutOfLineInputs::ExtractFrom(Use* old_use_ptr, |
| 24 | ZoneNodePtr* old_input_ptr, int count) { |
| 25 | DCHECK_GE(count, 0); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 26 | // Extract the inputs from the old use and input pointers and copy them |
| 27 | // to this out-of-line-storage. |
| 28 | Use* new_use_ptr = reinterpret_cast<Use*>(this) - 1; |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 29 | ZoneNodePtr* new_input_ptr = inputs(); |
| 30 | CHECK_IMPLIES(count > 0, Use::InputIndexField::is_valid(count - 1)); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | for (int current = 0; current < count; current++) { |
| 32 | new_use_ptr->bit_field_ = |
| 33 | Use::InputIndexField::encode(current) | Use::InlineField::encode(false); |
| 34 | DCHECK_EQ(old_input_ptr, old_use_ptr->input_ptr()); |
| 35 | DCHECK_EQ(new_input_ptr, new_use_ptr->input_ptr()); |
| 36 | Node* old_to = *old_input_ptr; |
| 37 | if (old_to) { |
| 38 | *old_input_ptr = nullptr; |
| 39 | old_to->RemoveUse(old_use_ptr); |
| 40 | *new_input_ptr = old_to; |
| 41 | old_to->AppendUse(new_use_ptr); |
| 42 | } else { |
| 43 | *new_input_ptr = nullptr; |
| 44 | } |
| 45 | old_input_ptr++; |
| 46 | new_input_ptr++; |
| 47 | old_use_ptr--; |
| 48 | new_use_ptr--; |
| 49 | } |
| 50 | this->count_ = count; |
| 51 | } |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 52 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 53 | // These structs are just type tags for Zone::Allocate<T>(size_t) calls. |
| 54 | struct NodeWithOutOfLineInputs {}; |
| 55 | struct NodeWithInLineInputs {}; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 56 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 57 | template <typename NodePtrT> |
| 58 | Node* Node::NewImpl(Zone* zone, NodeId id, const Operator* op, int input_count, |
| 59 | NodePtrT const* inputs, bool has_extensible_inputs) { |
| 60 | // Node uses compressed pointers, so zone must support pointer compression. |
| 61 | DCHECK_IMPLIES(kCompressGraphZone, zone->supports_compression()); |
| 62 | DCHECK_GE(input_count, 0); |
| 63 | |
| 64 | ZoneNodePtr* input_ptr; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | Use* use_ptr; |
| 66 | Node* node; |
| 67 | bool is_inline; |
| 68 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | // Verify that none of the inputs are {nullptr}. |
| 70 | for (int i = 0; i < input_count; i++) { |
| 71 | if (inputs[i] == nullptr) { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 72 | FATAL("Node::New() Error: #%d:%s[%d] is nullptr", static_cast<int>(id), |
| 73 | op->mnemonic(), i); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | |
| 77 | if (input_count > kMaxInlineCapacity) { |
| 78 | // Allocate out-of-line inputs. |
| 79 | int capacity = |
| 80 | has_extensible_inputs ? input_count + kMaxInlineCapacity : input_count; |
| 81 | OutOfLineInputs* outline = OutOfLineInputs::New(zone, capacity); |
| 82 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 83 | // Allocate node, with space for OutOfLineInputs pointer. |
| 84 | void* node_buffer = zone->Allocate<NodeWithOutOfLineInputs>( |
| 85 | sizeof(Node) + sizeof(ZoneOutOfLineInputsPtr)); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 86 | node = new (node_buffer) Node(id, op, kOutlineMarker, 0); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 87 | node->set_outline_inputs(outline); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | |
| 89 | outline->node_ = node; |
| 90 | outline->count_ = input_count; |
| 91 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 92 | input_ptr = outline->inputs(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 93 | use_ptr = reinterpret_cast<Use*>(outline); |
| 94 | is_inline = false; |
| 95 | } else { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 96 | // Allocate node with inline inputs. Capacity must be at least 1 so that |
| 97 | // an OutOfLineInputs pointer can be stored when inputs are added later. |
| 98 | int capacity = std::max(1, input_count); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | if (has_extensible_inputs) { |
| 100 | const int max = kMaxInlineCapacity; |
| 101 | capacity = std::min(input_count + 3, max); |
| 102 | } |
| 103 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 104 | size_t size = sizeof(Node) + capacity * (sizeof(ZoneNodePtr) + sizeof(Use)); |
| 105 | intptr_t raw_buffer = |
| 106 | reinterpret_cast<intptr_t>(zone->Allocate<NodeWithInLineInputs>(size)); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 107 | void* node_buffer = |
| 108 | reinterpret_cast<void*>(raw_buffer + capacity * sizeof(Use)); |
| 109 | |
| 110 | node = new (node_buffer) Node(id, op, input_count, capacity); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 111 | input_ptr = node->inline_inputs(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 112 | use_ptr = reinterpret_cast<Use*>(node); |
| 113 | is_inline = true; |
| 114 | } |
| 115 | |
| 116 | // Initialize the input pointers and the uses. |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 117 | CHECK_IMPLIES(input_count > 0, |
| 118 | Use::InputIndexField::is_valid(input_count - 1)); |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 119 | for (int current = 0; current < input_count; ++current) { |
| 120 | Node* to = *inputs++; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 121 | input_ptr[current] = to; |
| 122 | Use* use = use_ptr - 1 - current; |
| 123 | use->bit_field_ = Use::InputIndexField::encode(current) | |
| 124 | Use::InlineField::encode(is_inline); |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 125 | to->AppendUse(use); |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 126 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 127 | node->Verify(); |
| 128 | return node; |
| 129 | } |
| 130 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 131 | Node* Node::New(Zone* zone, NodeId id, const Operator* op, int input_count, |
| 132 | Node* const* inputs, bool has_extensible_inputs) { |
| 133 | return NewImpl(zone, id, op, input_count, inputs, has_extensible_inputs); |
| 134 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | |
| 136 | Node* Node::Clone(Zone* zone, NodeId id, const Node* node) { |
| 137 | int const input_count = node->InputCount(); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 138 | ZoneNodePtr const* const inputs = node->has_inline_inputs() |
| 139 | ? node->inline_inputs() |
| 140 | : node->outline_inputs()->inputs(); |
| 141 | Node* const clone = NewImpl(zone, id, node->op(), input_count, inputs, false); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 142 | clone->set_type(node->type()); |
| 143 | return clone; |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 147 | void Node::Kill() { |
| 148 | DCHECK_NOT_NULL(op()); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 149 | NullAllInputs(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 150 | DCHECK(uses().empty()); |
| 151 | } |
| 152 | |
| 153 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 154 | void Node::AppendInput(Zone* zone, Node* new_to) { |
| 155 | DCHECK_NOT_NULL(zone); |
| 156 | DCHECK_NOT_NULL(new_to); |
| 157 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 158 | int const inline_count = InlineCountField::decode(bit_field_); |
| 159 | int const inline_capacity = InlineCapacityField::decode(bit_field_); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 160 | if (inline_count < inline_capacity) { |
| 161 | // Append inline input. |
| 162 | bit_field_ = InlineCountField::update(bit_field_, inline_count + 1); |
| 163 | *GetInputPtr(inline_count) = new_to; |
| 164 | Use* use = GetUsePtr(inline_count); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 165 | STATIC_ASSERT(InlineCapacityField::kMax <= Use::InputIndexField::kMax); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 166 | use->bit_field_ = Use::InputIndexField::encode(inline_count) | |
| 167 | Use::InlineField::encode(true); |
| 168 | new_to->AppendUse(use); |
| 169 | } else { |
| 170 | // Append out-of-line input. |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 171 | int const input_count = InputCount(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 172 | OutOfLineInputs* outline = nullptr; |
| 173 | if (inline_count != kOutlineMarker) { |
| 174 | // switch to out of line inputs. |
| 175 | outline = OutOfLineInputs::New(zone, input_count * 2 + 3); |
| 176 | outline->node_ = this; |
| 177 | outline->ExtractFrom(GetUsePtr(0), GetInputPtr(0), input_count); |
| 178 | bit_field_ = InlineCountField::update(bit_field_, kOutlineMarker); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 179 | set_outline_inputs(outline); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 180 | } else { |
| 181 | // use current out of line inputs. |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 182 | outline = outline_inputs(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 183 | if (input_count >= outline->capacity_) { |
| 184 | // out of space in out-of-line inputs. |
| 185 | outline = OutOfLineInputs::New(zone, input_count * 2 + 3); |
| 186 | outline->node_ = this; |
| 187 | outline->ExtractFrom(GetUsePtr(0), GetInputPtr(0), input_count); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 188 | set_outline_inputs(outline); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | outline->count_++; |
| 192 | *GetInputPtr(input_count) = new_to; |
| 193 | Use* use = GetUsePtr(input_count); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 194 | CHECK(Use::InputIndexField::is_valid(input_count)); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 195 | use->bit_field_ = Use::InputIndexField::encode(input_count) | |
| 196 | Use::InlineField::encode(false); |
| 197 | new_to->AppendUse(use); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 198 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 199 | Verify(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 203 | void Node::InsertInput(Zone* zone, int index, Node* new_to) { |
| 204 | DCHECK_NOT_NULL(zone); |
| 205 | DCHECK_LE(0, index); |
| 206 | DCHECK_LT(index, InputCount()); |
| 207 | AppendInput(zone, InputAt(InputCount() - 1)); |
| 208 | for (int i = InputCount() - 1; i > index; --i) { |
| 209 | ReplaceInput(i, InputAt(i - 1)); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 211 | ReplaceInput(index, new_to); |
| 212 | Verify(); |
| 213 | } |
| 214 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 215 | void Node::InsertInputs(Zone* zone, int index, int count) { |
| 216 | DCHECK_NOT_NULL(zone); |
| 217 | DCHECK_LE(0, index); |
| 218 | DCHECK_LT(0, count); |
| 219 | DCHECK_LT(index, InputCount()); |
| 220 | for (int i = 0; i < count; i++) { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 221 | AppendInput(zone, InputAt(std::max(InputCount() - count, 0))); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 222 | } |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 223 | for (int i = InputCount() - count - 1; i >= std::max(index, count); --i) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 224 | ReplaceInput(i, InputAt(i - count)); |
| 225 | } |
| 226 | for (int i = 0; i < count; i++) { |
| 227 | ReplaceInput(index + i, nullptr); |
| 228 | } |
| 229 | Verify(); |
| 230 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 231 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 232 | Node* Node::RemoveInput(int index) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 233 | DCHECK_LE(0, index); |
| 234 | DCHECK_LT(index, InputCount()); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 235 | Node* result = InputAt(index); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 236 | for (; index < InputCount() - 1; ++index) { |
| 237 | ReplaceInput(index, InputAt(index + 1)); |
| 238 | } |
| 239 | TrimInputCount(InputCount() - 1); |
| 240 | Verify(); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 241 | return result; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 244 | void Node::ClearInputs(int start, int count) { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 245 | ZoneNodePtr* input_ptr = GetInputPtr(start); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 246 | Use* use_ptr = GetUsePtr(start); |
| 247 | while (count-- > 0) { |
| 248 | DCHECK_EQ(input_ptr, use_ptr->input_ptr()); |
| 249 | Node* input = *input_ptr; |
| 250 | *input_ptr = nullptr; |
| 251 | if (input) input->RemoveUse(use_ptr); |
| 252 | input_ptr++; |
| 253 | use_ptr--; |
| 254 | } |
| 255 | Verify(); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | void Node::NullAllInputs() { ClearInputs(0, InputCount()); } |
| 260 | |
| 261 | |
| 262 | void Node::TrimInputCount(int new_input_count) { |
| 263 | int current_count = InputCount(); |
| 264 | DCHECK_LE(new_input_count, current_count); |
| 265 | if (new_input_count == current_count) return; // Nothing to do. |
| 266 | ClearInputs(new_input_count, current_count - new_input_count); |
| 267 | if (has_inline_inputs()) { |
| 268 | bit_field_ = InlineCountField::update(bit_field_, new_input_count); |
| 269 | } else { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 270 | outline_inputs()->count_ = new_input_count; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 271 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 274 | void Node::EnsureInputCount(Zone* zone, int new_input_count) { |
| 275 | int current_count = InputCount(); |
| 276 | DCHECK_NE(current_count, 0); |
| 277 | if (current_count > new_input_count) { |
| 278 | TrimInputCount(new_input_count); |
| 279 | } else if (current_count < new_input_count) { |
| 280 | Node* dummy = InputAt(current_count - 1); |
| 281 | do { |
| 282 | AppendInput(zone, dummy); |
| 283 | current_count++; |
| 284 | } while (current_count < new_input_count); |
| 285 | } |
| 286 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 287 | |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 288 | int Node::UseCount() const { |
| 289 | int use_count = 0; |
| 290 | for (const Use* use = first_use_; use; use = use->next) { |
| 291 | ++use_count; |
| 292 | } |
| 293 | return use_count; |
| 294 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 295 | |
| 296 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 297 | void Node::ReplaceUses(Node* that) { |
| 298 | DCHECK(this->first_use_ == nullptr || this->first_use_->prev == nullptr); |
| 299 | DCHECK(that->first_use_ == nullptr || that->first_use_->prev == nullptr); |
| 300 | |
| 301 | // Update the pointers to {this} to point to {that}. |
| 302 | Use* last_use = nullptr; |
| 303 | for (Use* use = this->first_use_; use; use = use->next) { |
| 304 | *use->input_ptr() = that; |
| 305 | last_use = use; |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 306 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 307 | if (last_use) { |
| 308 | // Concat the use list of {this} and {that}. |
| 309 | last_use->next = that->first_use_; |
| 310 | if (that->first_use_) that->first_use_->prev = last_use; |
| 311 | that->first_use_ = this->first_use_; |
| 312 | } |
| 313 | first_use_ = nullptr; |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 316 | bool Node::OwnedBy(Node const* owner) const { |
| 317 | unsigned mask = 0; |
| 318 | for (Use* use = first_use_; use; use = use->next) { |
| 319 | if (use->from() == owner) { |
| 320 | mask |= 1; |
| 321 | } else { |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | return mask == 1; |
| 326 | } |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 327 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 328 | bool Node::OwnedBy(Node const* owner1, Node const* owner2) const { |
| 329 | unsigned mask = 0; |
| 330 | for (Use* use = first_use_; use; use = use->next) { |
| 331 | Node* from = use->from(); |
| 332 | if (from == owner1) { |
| 333 | mask |= 1; |
| 334 | } else if (from == owner2) { |
| 335 | mask |= 2; |
| 336 | } else { |
| 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | return mask == 3; |
| 341 | } |
| 342 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 343 | void Node::Print(int depth) const { |
| 344 | StdoutStream os; |
| 345 | Print(os, depth); |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame] | 346 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 347 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 348 | namespace { |
| 349 | void PrintNode(const Node* node, std::ostream& os, int depth, |
| 350 | int indentation = 0) { |
| 351 | for (int i = 0; i < indentation; ++i) { |
| 352 | os << " "; |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame] | 353 | } |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 354 | if (node) { |
| 355 | os << *node; |
| 356 | } else { |
| 357 | os << "(NULL)"; |
| 358 | } |
| 359 | os << std::endl; |
| 360 | if (depth <= 0) return; |
| 361 | for (Node* input : node->inputs()) { |
| 362 | PrintNode(input, os, depth - 1, indentation + 1); |
| 363 | } |
| 364 | } |
| 365 | } // namespace |
| 366 | |
| 367 | void Node::Print(std::ostream& os, int depth) const { |
| 368 | PrintNode(this, os, depth); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame] | 371 | std::ostream& operator<<(std::ostream& os, const Node& n) { |
| 372 | os << n.id() << ": " << *n.op(); |
| 373 | if (n.InputCount() > 0) { |
| 374 | os << "("; |
| 375 | for (int i = 0; i < n.InputCount(); ++i) { |
| 376 | if (i != 0) os << ", "; |
| 377 | if (n.InputAt(i)) { |
| 378 | os << n.InputAt(i)->id(); |
| 379 | } else { |
| 380 | os << "null"; |
| 381 | } |
| 382 | } |
| 383 | os << ")"; |
| 384 | } |
| 385 | return os; |
| 386 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 387 | |
| 388 | Node::Node(NodeId id, const Operator* op, int inline_count, int inline_capacity) |
| 389 | : op_(op), |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 390 | mark_(0), |
| 391 | bit_field_(IdField::encode(id) | InlineCountField::encode(inline_count) | |
| 392 | InlineCapacityField::encode(inline_capacity)), |
| 393 | first_use_(nullptr) { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 394 | // Check that the id didn't overflow. |
| 395 | STATIC_ASSERT(IdField::kMax < std::numeric_limits<NodeId>::max()); |
| 396 | CHECK(IdField::is_valid(id)); |
| 397 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 398 | // Inputs must either be out of line or within the inline capacity. |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 399 | DCHECK(inline_count == kOutlineMarker || inline_count <= inline_capacity); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 400 | DCHECK_LE(inline_capacity, kMaxInlineCapacity); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
| 404 | void Node::AppendUse(Use* use) { |
| 405 | DCHECK(first_use_ == nullptr || first_use_->prev == nullptr); |
| 406 | DCHECK_EQ(this, *use->input_ptr()); |
| 407 | use->next = first_use_; |
| 408 | use->prev = nullptr; |
| 409 | if (first_use_) first_use_->prev = use; |
| 410 | first_use_ = use; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | void Node::RemoveUse(Use* use) { |
| 415 | DCHECK(first_use_ == nullptr || first_use_->prev == nullptr); |
| 416 | if (use->prev) { |
| 417 | DCHECK_NE(first_use_, use); |
| 418 | use->prev->next = use->next; |
| 419 | } else { |
| 420 | DCHECK_EQ(first_use_, use); |
| 421 | first_use_ = use->next; |
| 422 | } |
| 423 | if (use->next) { |
| 424 | use->next->prev = use->prev; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | |
| 429 | #if DEBUG |
| 430 | void Node::Verify() { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 431 | // Check basic validity of input data structures. |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 432 | fflush(stdout); |
| 433 | int count = this->InputCount(); |
| 434 | // Avoid quadratic explosion for mega nodes; only verify if the input |
| 435 | // count is less than 200 or is a round number of 100s. |
| 436 | if (count > 200 && count % 100) return; |
| 437 | |
| 438 | for (int i = 0; i < count; i++) { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 439 | DCHECK_EQ(i, this->GetUsePtr(i)->input_index()); |
| 440 | DCHECK_EQ(this->GetInputPtr(i), this->GetUsePtr(i)->input_ptr()); |
| 441 | DCHECK_EQ(count, this->InputCount()); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 442 | } |
| 443 | { // Direct input iteration. |
| 444 | int index = 0; |
| 445 | for (Node* input : this->inputs()) { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 446 | DCHECK_EQ(this->InputAt(index), input); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 447 | index++; |
| 448 | } |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 449 | DCHECK_EQ(count, index); |
| 450 | DCHECK_EQ(this->InputCount(), index); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 451 | } |
| 452 | { // Input edge iteration. |
| 453 | int index = 0; |
| 454 | for (Edge edge : this->input_edges()) { |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 455 | DCHECK_EQ(edge.from(), this); |
| 456 | DCHECK_EQ(index, edge.index()); |
| 457 | DCHECK_EQ(this->InputAt(index), edge.to()); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 458 | index++; |
| 459 | } |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 460 | DCHECK_EQ(count, index); |
| 461 | DCHECK_EQ(this->InputCount(), index); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | #endif |
| 465 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 466 | Node::InputEdges::iterator Node::InputEdges::iterator::operator++(int n) { |
| 467 | iterator result(*this); |
| 468 | ++(*this); |
| 469 | return result; |
| 470 | } |
| 471 | |
| 472 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 473 | Node::Inputs::const_iterator Node::Inputs::const_iterator::operator++(int n) { |
| 474 | const_iterator result(*this); |
| 475 | ++(*this); |
| 476 | return result; |
| 477 | } |
| 478 | |
| 479 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 480 | Node::UseEdges::iterator Node::UseEdges::iterator::operator++(int n) { |
| 481 | iterator result(*this); |
| 482 | ++(*this); |
| 483 | return result; |
| 484 | } |
| 485 | |
| 486 | |
| 487 | bool Node::UseEdges::empty() const { return begin() == end(); } |
| 488 | |
| 489 | |
| 490 | Node::Uses::const_iterator Node::Uses::const_iterator::operator++(int n) { |
| 491 | const_iterator result(*this); |
| 492 | ++(*this); |
| 493 | return result; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | bool Node::Uses::empty() const { return begin() == end(); } |
| 498 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 499 | } // namespace compiler |
| 500 | } // namespace internal |
| 501 | } // namespace v8 |