OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/compiler/node.h" | 5 #include "src/compiler/node.h" |
6 #include "src/compiler/node-properties.h" | 6 #include "src/compiler/node-properties.h" |
7 #include "src/compiler/node-properties-inl.h" | 7 #include "src/compiler/node-properties-inl.h" |
8 #include "src/compiler/schedule.h" | 8 #include "src/compiler/schedule.h" |
9 #include "src/ostreams.h" | 9 #include "src/ostreams.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 namespace compiler { | 13 namespace compiler { |
14 | 14 |
15 OStream& operator<<(OStream& os, const BasicBlockData::Control& c) { | 15 |
| 16 BasicBlock::BasicBlock(Zone* zone, Id id) |
| 17 : rpo_number_(-1), |
| 18 dominator_(NULL), |
| 19 loop_header_(NULL), |
| 20 loop_depth_(0), |
| 21 loop_end_(-1), |
| 22 code_start_(-1), |
| 23 code_end_(-1), |
| 24 deferred_(false), |
| 25 control_(kNone), |
| 26 control_input_(NULL), |
| 27 nodes_(zone), |
| 28 successors_(zone), |
| 29 predecessors_(zone), |
| 30 id_(id) {} |
| 31 |
| 32 |
| 33 bool BasicBlock::LoopContains(BasicBlock* block) const { |
| 34 // RPO numbers must be initialized. |
| 35 DCHECK(rpo_number_ >= 0); |
| 36 DCHECK(block->rpo_number_ >= 0); |
| 37 if (loop_end_ < 0) return false; // This is not a loop. |
| 38 return block->rpo_number_ >= rpo_number_ && block->rpo_number_ < loop_end_; |
| 39 } |
| 40 |
| 41 |
| 42 BasicBlock* BasicBlock::ContainingLoop() { |
| 43 if (IsLoopHeader()) return this; |
| 44 return loop_header(); |
| 45 } |
| 46 |
| 47 |
| 48 void BasicBlock::AddSuccessor(BasicBlock* successor) { |
| 49 successors_.push_back(successor); |
| 50 } |
| 51 |
| 52 |
| 53 void BasicBlock::AddPredecessor(BasicBlock* predecessor) { |
| 54 predecessors_.push_back(predecessor); |
| 55 } |
| 56 |
| 57 |
| 58 void BasicBlock::AddNode(Node* node) { nodes_.push_back(node); } |
| 59 |
| 60 |
| 61 void BasicBlock::set_control(Control control) { |
| 62 DCHECK(control_ == BasicBlock::kNone); |
| 63 control_ = control; |
| 64 } |
| 65 |
| 66 |
| 67 void BasicBlock::set_control_input(Node* control_input) { |
| 68 control_input_ = control_input; |
| 69 } |
| 70 |
| 71 |
| 72 void BasicBlock::set_dominator(BasicBlock* dominator) { |
| 73 dominator_ = dominator; |
| 74 } |
| 75 |
| 76 |
| 77 void BasicBlock::set_loop_depth(int32_t loop_depth) { |
| 78 loop_depth_ = loop_depth; |
| 79 } |
| 80 |
| 81 |
| 82 void BasicBlock::set_rpo_number(int32_t rpo_number) { |
| 83 rpo_number_ = rpo_number; |
| 84 } |
| 85 |
| 86 |
| 87 void BasicBlock::set_loop_end(int32_t loop_end) { loop_end_ = loop_end; } |
| 88 |
| 89 |
| 90 void BasicBlock::set_code_start(int32_t code_start) { |
| 91 code_start_ = code_start; |
| 92 } |
| 93 |
| 94 |
| 95 void BasicBlock::set_code_end(int32_t code_end) { code_end_ = code_end; } |
| 96 |
| 97 |
| 98 void BasicBlock::set_loop_header(BasicBlock* loop_header) { |
| 99 loop_header_ = loop_header; |
| 100 } |
| 101 |
| 102 |
| 103 size_t BasicBlock::PredecessorIndexOf(BasicBlock* predecessor) { |
| 104 size_t j = 0; |
| 105 for (BasicBlock::Predecessors::iterator i = predecessors_.begin(); |
| 106 i != predecessors_.end(); ++i, ++j) { |
| 107 if (*i == predecessor) break; |
| 108 } |
| 109 return j; |
| 110 } |
| 111 |
| 112 |
| 113 OStream& operator<<(OStream& os, const BasicBlock::Control& c) { |
16 switch (c) { | 114 switch (c) { |
17 case BasicBlockData::kNone: | 115 case BasicBlock::kNone: |
18 return os << "none"; | 116 return os << "none"; |
19 case BasicBlockData::kGoto: | 117 case BasicBlock::kGoto: |
20 return os << "goto"; | 118 return os << "goto"; |
21 case BasicBlockData::kBranch: | 119 case BasicBlock::kBranch: |
22 return os << "branch"; | 120 return os << "branch"; |
23 case BasicBlockData::kReturn: | 121 case BasicBlock::kReturn: |
24 return os << "return"; | 122 return os << "return"; |
25 case BasicBlockData::kThrow: | 123 case BasicBlock::kThrow: |
26 return os << "throw"; | 124 return os << "throw"; |
27 } | 125 } |
28 UNREACHABLE(); | 126 UNREACHABLE(); |
29 return os; | 127 return os; |
30 } | 128 } |
31 | 129 |
32 | 130 |
| 131 OStream& operator<<(OStream& os, const BasicBlock::Id& id) { |
| 132 return os << id.ToSize(); |
| 133 } |
| 134 |
| 135 |
| 136 Schedule::Schedule(Zone* zone, size_t node_count_hint) |
| 137 : zone_(zone), |
| 138 all_blocks_(zone), |
| 139 nodeid_to_block_(zone), |
| 140 rpo_order_(zone), |
| 141 start_(NewBasicBlock()), |
| 142 end_(NewBasicBlock()) { |
| 143 nodeid_to_block_.reserve(node_count_hint); |
| 144 } |
| 145 |
| 146 |
| 147 BasicBlock* Schedule::block(Node* node) const { |
| 148 if (node->id() < static_cast<NodeId>(nodeid_to_block_.size())) { |
| 149 return nodeid_to_block_[node->id()]; |
| 150 } |
| 151 return NULL; |
| 152 } |
| 153 |
| 154 |
| 155 bool Schedule::IsScheduled(Node* node) { |
| 156 int length = static_cast<int>(nodeid_to_block_.size()); |
| 157 if (node->id() >= length) return false; |
| 158 return nodeid_to_block_[node->id()] != NULL; |
| 159 } |
| 160 |
| 161 |
| 162 BasicBlock* Schedule::GetBlockById(BasicBlock::Id block_id) { |
| 163 DCHECK(block_id.ToSize() < all_blocks_.size()); |
| 164 return all_blocks_[block_id.ToSize()]; |
| 165 } |
| 166 |
| 167 |
| 168 bool Schedule::SameBasicBlock(Node* a, Node* b) const { |
| 169 BasicBlock* block = this->block(a); |
| 170 return block != NULL && block == this->block(b); |
| 171 } |
| 172 |
| 173 |
| 174 BasicBlock* Schedule::NewBasicBlock() { |
| 175 BasicBlock* block = new (zone_) |
| 176 BasicBlock(zone_, BasicBlock::Id::FromSize(all_blocks_.size())); |
| 177 all_blocks_.push_back(block); |
| 178 return block; |
| 179 } |
| 180 |
| 181 |
| 182 void Schedule::PlanNode(BasicBlock* block, Node* node) { |
| 183 if (FLAG_trace_turbo_scheduler) { |
| 184 OFStream os(stdout); |
| 185 os << "Planning #" << node->id() << ":" << node->op()->mnemonic() |
| 186 << " for future add to B" << block->id() << "\n"; |
| 187 } |
| 188 DCHECK(this->block(node) == NULL); |
| 189 SetBlockForNode(block, node); |
| 190 } |
| 191 |
| 192 |
| 193 void Schedule::AddNode(BasicBlock* block, Node* node) { |
| 194 if (FLAG_trace_turbo_scheduler) { |
| 195 OFStream os(stdout); |
| 196 os << "Adding #" << node->id() << ":" << node->op()->mnemonic() << " to B" |
| 197 << block->id() << "\n"; |
| 198 } |
| 199 DCHECK(this->block(node) == NULL || this->block(node) == block); |
| 200 block->AddNode(node); |
| 201 SetBlockForNode(block, node); |
| 202 } |
| 203 |
| 204 |
| 205 void Schedule::AddGoto(BasicBlock* block, BasicBlock* succ) { |
| 206 DCHECK(block->control() == BasicBlock::kNone); |
| 207 block->set_control(BasicBlock::kGoto); |
| 208 AddSuccessor(block, succ); |
| 209 } |
| 210 |
| 211 |
| 212 void Schedule::AddBranch(BasicBlock* block, Node* branch, BasicBlock* tblock, |
| 213 BasicBlock* fblock) { |
| 214 DCHECK(block->control() == BasicBlock::kNone); |
| 215 DCHECK(branch->opcode() == IrOpcode::kBranch); |
| 216 block->set_control(BasicBlock::kBranch); |
| 217 AddSuccessor(block, tblock); |
| 218 AddSuccessor(block, fblock); |
| 219 SetControlInput(block, branch); |
| 220 if (branch->opcode() == IrOpcode::kBranch) { |
| 221 // TODO(titzer): require a Branch node here. (sloppy tests). |
| 222 SetBlockForNode(block, branch); |
| 223 } |
| 224 } |
| 225 |
| 226 |
| 227 void Schedule::AddReturn(BasicBlock* block, Node* input) { |
| 228 DCHECK(block->control() == BasicBlock::kNone); |
| 229 block->set_control(BasicBlock::kReturn); |
| 230 SetControlInput(block, input); |
| 231 if (block != end()) { |
| 232 AddSuccessor(block, end()); |
| 233 } |
| 234 if (input->opcode() == IrOpcode::kReturn) { |
| 235 // TODO(titzer): require a Return node here. (sloppy tests). |
| 236 SetBlockForNode(block, input); |
| 237 } |
| 238 } |
| 239 |
| 240 |
| 241 void Schedule::AddThrow(BasicBlock* block, Node* input) { |
| 242 DCHECK(block->control() == BasicBlock::kNone); |
| 243 block->set_control(BasicBlock::kThrow); |
| 244 SetControlInput(block, input); |
| 245 if (block != end()) AddSuccessor(block, end()); |
| 246 } |
| 247 |
| 248 |
| 249 void Schedule::AddSuccessor(BasicBlock* block, BasicBlock* succ) { |
| 250 block->AddSuccessor(succ); |
| 251 succ->AddPredecessor(block); |
| 252 } |
| 253 |
| 254 |
| 255 void Schedule::SetControlInput(BasicBlock* block, Node* node) { |
| 256 block->set_control_input(node); |
| 257 SetBlockForNode(block, node); |
| 258 } |
| 259 |
| 260 |
| 261 void Schedule::SetBlockForNode(BasicBlock* block, Node* node) { |
| 262 int length = static_cast<int>(nodeid_to_block_.size()); |
| 263 if (node->id() >= length) { |
| 264 nodeid_to_block_.resize(node->id() + 1); |
| 265 } |
| 266 nodeid_to_block_[node->id()] = block; |
| 267 } |
| 268 |
| 269 |
33 OStream& operator<<(OStream& os, const Schedule& s) { | 270 OStream& operator<<(OStream& os, const Schedule& s) { |
34 // TODO(svenpanne) Const-correct the RPO stuff/iterators. | 271 // TODO(svenpanne) Const-correct the RPO stuff/iterators. |
35 BasicBlockVector* rpo = const_cast<Schedule*>(&s)->rpo_order(); | 272 BasicBlockVector* rpo = const_cast<Schedule*>(&s)->rpo_order(); |
36 for (BasicBlockVectorIter i = rpo->begin(); i != rpo->end(); ++i) { | 273 for (BasicBlockVectorIter i = rpo->begin(); i != rpo->end(); ++i) { |
37 BasicBlock* block = *i; | 274 BasicBlock* block = *i; |
38 os << "--- BLOCK B" << block->id(); | 275 os << "--- BLOCK B" << block->id(); |
39 if (block->PredecessorCount() != 0) os << " <- "; | 276 if (block->PredecessorCount() != 0) os << " <- "; |
40 BasicBlock::Predecessors predecessors = block->predecessors(); | |
41 bool comma = false; | 277 bool comma = false; |
42 for (BasicBlock::Predecessors::iterator j = predecessors.begin(); | 278 for (BasicBlock::Predecessors::iterator j = block->predecessors_begin(); |
43 j != predecessors.end(); ++j) { | 279 j != block->predecessors_end(); ++j) { |
44 if (comma) os << ", "; | 280 if (comma) os << ", "; |
45 comma = true; | 281 comma = true; |
46 os << "B" << (*j)->id(); | 282 os << "B" << (*j)->id(); |
47 } | 283 } |
48 os << " ---\n"; | 284 os << " ---\n"; |
49 for (BasicBlock::const_iterator j = block->begin(); j != block->end(); | 285 for (BasicBlock::const_iterator j = block->begin(); j != block->end(); |
50 ++j) { | 286 ++j) { |
51 Node* node = *j; | 287 Node* node = *j; |
52 os << " " << *node; | 288 os << " " << *node; |
53 if (!NodeProperties::IsControl(node)) { | 289 if (!NodeProperties::IsControl(node)) { |
54 Bounds bounds = NodeProperties::GetBounds(node); | 290 Bounds bounds = NodeProperties::GetBounds(node); |
55 os << " : "; | 291 os << " : "; |
56 bounds.lower->PrintTo(os); | 292 bounds.lower->PrintTo(os); |
57 if (!bounds.upper->Is(bounds.lower)) { | 293 if (!bounds.upper->Is(bounds.lower)) { |
58 os << ".."; | 294 os << ".."; |
59 bounds.upper->PrintTo(os); | 295 bounds.upper->PrintTo(os); |
60 } | 296 } |
61 } | 297 } |
62 os << "\n"; | 298 os << "\n"; |
63 } | 299 } |
64 BasicBlock::Control control = block->control_; | 300 BasicBlock::Control control = block->control(); |
65 if (control != BasicBlock::kNone) { | 301 if (control != BasicBlock::kNone) { |
66 os << " "; | 302 os << " "; |
67 if (block->control_input_ != NULL) { | 303 if (block->control_input() != NULL) { |
68 os << *block->control_input_; | 304 os << *block->control_input(); |
69 } else { | 305 } else { |
70 os << "Goto"; | 306 os << "Goto"; |
71 } | 307 } |
72 os << " -> "; | 308 os << " -> "; |
73 BasicBlock::Successors successors = block->successors(); | |
74 comma = false; | 309 comma = false; |
75 for (BasicBlock::Successors::iterator j = successors.begin(); | 310 for (BasicBlock::Successors::iterator j = block->successors_begin(); |
76 j != successors.end(); ++j) { | 311 j != block->successors_end(); ++j) { |
77 if (comma) os << ", "; | 312 if (comma) os << ", "; |
78 comma = true; | 313 comma = true; |
79 os << "B" << (*j)->id(); | 314 os << "B" << (*j)->id(); |
80 } | 315 } |
81 os << "\n"; | 316 os << "\n"; |
82 } | 317 } |
83 } | 318 } |
84 return os; | 319 return os; |
85 } | 320 } |
86 } // namespace compiler | 321 } // namespace compiler |
87 } // namespace internal | 322 } // namespace internal |
88 } // namespace v8 | 323 } // namespace v8 |
OLD | NEW |