Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/compiler/verifier.cc

Issue 491303002: Add ScheduleVerifier. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/compiler/verifier.h ('k') | src/data-flow.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 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/verifier.h" 5 #include "src/compiler/verifier.h"
6 6
7 #include <deque>
8 #include <queue>
9
7 #include "src/compiler/generic-algorithm.h" 10 #include "src/compiler/generic-algorithm.h"
8 #include "src/compiler/generic-node-inl.h" 11 #include "src/compiler/generic-node-inl.h"
9 #include "src/compiler/generic-node.h" 12 #include "src/compiler/generic-node.h"
10 #include "src/compiler/graph-inl.h" 13 #include "src/compiler/graph-inl.h"
11 #include "src/compiler/graph.h" 14 #include "src/compiler/graph.h"
12 #include "src/compiler/node.h" 15 #include "src/compiler/node.h"
13 #include "src/compiler/node-properties-inl.h" 16 #include "src/compiler/node-properties-inl.h"
14 #include "src/compiler/node-properties.h" 17 #include "src/compiler/node-properties.h"
15 #include "src/compiler/opcodes.h" 18 #include "src/compiler/opcodes.h"
16 #include "src/compiler/operator.h" 19 #include "src/compiler/operator.h"
20 #include "src/compiler/schedule.h"
21 #include "src/data-flow.h"
17 22
18 namespace v8 { 23 namespace v8 {
19 namespace internal { 24 namespace internal {
20 namespace compiler { 25 namespace compiler {
21 26
22 27
23 static bool IsDefUseChainLinkPresent(Node* def, Node* use) { 28 static bool IsDefUseChainLinkPresent(Node* def, Node* use) {
24 Node::Uses uses = def->uses(); 29 Node::Uses uses = def->uses();
25 for (Node::Uses::iterator it = uses.begin(); it != uses.end(); ++it) { 30 for (Node::Uses::iterator it = uses.begin(); it != uses.end(); ++it) {
26 if (*it == use) return true; 31 if (*it == use) return true;
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 visitor.from_start = false; 241 visitor.from_start = false;
237 graph->VisitNodeInputsFromEnd(&visitor); 242 graph->VisitNodeInputsFromEnd(&visitor);
238 243
239 // All control nodes reachable from end are reachable from start. 244 // All control nodes reachable from end are reachable from start.
240 for (NodeSet::iterator it = visitor.reached_from_end.begin(); 245 for (NodeSet::iterator it = visitor.reached_from_end.begin();
241 it != visitor.reached_from_end.end(); ++it) { 246 it != visitor.reached_from_end.end(); ++it) {
242 CHECK(!NodeProperties::IsControl(*it) || 247 CHECK(!NodeProperties::IsControl(*it) ||
243 visitor.reached_from_start.count(*it)); 248 visitor.reached_from_start.count(*it));
244 } 249 }
245 } 250 }
251
252
253 static bool HasDominatingDef(Schedule* schedule, Node* node,
254 BasicBlock* container, BasicBlock* use_block,
255 int use_pos) {
256 BasicBlock* block = use_block;
257 while (true) {
258 while (use_pos >= 0) {
259 if (block->nodes_[use_pos] == node) return true;
260 use_pos--;
261 }
262 block = schedule->dominator(block);
263 if (block == NULL) break;
264 use_pos = static_cast<int>(block->nodes_.size()) - 1;
265 if (node == block->control_input_) return true;
266 }
267 return false;
268 }
269
270
271 static void CheckInputsDominate(Schedule* schedule, BasicBlock* block,
272 Node* node, int use_pos) {
273 for (int j = OperatorProperties::GetValueInputCount(node->op()) - 1; j >= 0;
274 j--) {
275 BasicBlock* use_block = block;
276 if (node->opcode() == IrOpcode::kPhi) {
277 use_block = use_block->PredecessorAt(j);
278 use_pos = static_cast<int>(use_block->nodes_.size()) - 1;
279 }
280 Node* input = node->InputAt(j);
281 if (!HasDominatingDef(schedule, node->InputAt(j), block, use_block,
282 use_pos)) {
283 V8_Fatal(__FILE__, __LINE__,
284 "Node #%d:%s in B%d is not dominated by input@%d #%d:%s",
285 node->id(), node->op()->mnemonic(), block->id(), j, input->id(),
286 input->op()->mnemonic());
287 }
288 }
289 }
290
291
292 void ScheduleVerifier::Run(Schedule* schedule) {
293 const int count = schedule->BasicBlockCount();
294 Zone tmp_zone(schedule->zone()->isolate());
295 Zone* zone = &tmp_zone;
296 BasicBlock* start = schedule->start();
297 BasicBlockVector* rpo_order = schedule->rpo_order();
298
299 // Verify the RPO order contains only blocks from this schedule.
300 CHECK_GE(count, static_cast<int>(rpo_order->size()));
301 for (BasicBlockVector::iterator b = rpo_order->begin(); b != rpo_order->end();
302 ++b) {
303 CHECK_EQ((*b), schedule->GetBlockById((*b)->id()));
304 }
305
306 // Verify RPO numbers of blocks.
307 CHECK_EQ(start, rpo_order->at(0)); // Start should be first.
308 for (size_t b = 0; b < rpo_order->size(); b++) {
309 BasicBlock* block = rpo_order->at(b);
310 CHECK_EQ(b, block->rpo_number_);
311 BasicBlock* dom = schedule->dominator(block);
312 if (b == 0) {
313 // All blocks except start should have a dominator.
314 CHECK_EQ(NULL, dom);
315 } else {
316 // Check that the immediate dominator appears somewhere before the block.
317 CHECK_NE(NULL, dom);
318 CHECK_LT(dom->rpo_number_, block->rpo_number_);
319 }
320 }
321
322 // Verify that all blocks reachable from start are in the RPO.
323 BoolVector marked(count, false, BoolVector::allocator_type(zone));
324 {
325 std::queue<BasicBlock*> queue;
326 queue.push(start);
327 marked[start->id()] = true;
328 while (!queue.empty()) {
329 BasicBlock* block = queue.front();
330 queue.pop();
331 for (int s = 0; s < block->SuccessorCount(); s++) {
332 BasicBlock* succ = block->SuccessorAt(s);
333 if (!marked[succ->id()]) {
334 marked[succ->id()] = true;
335 queue.push(succ);
336 }
337 }
338 }
339 }
340 // Verify marked blocks are in the RPO.
341 for (int i = 0; i < count; i++) {
342 BasicBlock* block = schedule->GetBlockById(i);
343 if (marked[i]) {
344 CHECK_GE(block->rpo_number_, 0);
345 CHECK_EQ(block, rpo_order->at(block->rpo_number_));
346 }
347 }
348 // Verify RPO blocks are marked.
349 for (size_t b = 0; b < rpo_order->size(); b++) {
350 CHECK(marked[rpo_order->at(b)->id()]);
351 }
352
353 {
354 // Verify the dominance relation.
355 ZoneList<BitVector*> dominators(count, zone);
356 dominators.Initialize(count, zone);
357 dominators.AddBlock(NULL, count, zone);
358
359 // Compute a set of all the nodes that dominate a given node by using
360 // a forward fixpoint. O(n^2).
361 std::queue<BasicBlock*> queue;
362 queue.push(start);
363 dominators[start->id()] = new (zone) BitVector(count, zone);
364 while (!queue.empty()) {
365 BasicBlock* block = queue.front();
366 queue.pop();
367 BitVector* block_doms = dominators[block->id()];
368 BasicBlock* idom = schedule->dominator(block);
369 if (idom != NULL && !block_doms->Contains(idom->id())) {
370 V8_Fatal(__FILE__, __LINE__, "Block B%d is not dominated by B%d",
371 block->id(), idom->id());
372 }
373 for (int s = 0; s < block->SuccessorCount(); s++) {
374 BasicBlock* succ = block->SuccessorAt(s);
375 BitVector* succ_doms = dominators[succ->id()];
376
377 if (succ_doms == NULL) {
378 // First time visiting the node. S.vec = B U B.vec
379 succ_doms = new (zone) BitVector(count, zone);
380 succ_doms->CopyFrom(*block_doms);
381 succ_doms->Add(block->id());
382 dominators[succ->id()] = succ_doms;
383 queue.push(succ);
384 } else {
385 // Nth time visiting the successor. S.vec = S.vec ^ (B U B.vec)
386 bool had = succ_doms->Contains(block->id());
387 if (had) succ_doms->Remove(block->id());
388 if (succ_doms->IntersectIsChanged(*block_doms)) queue.push(succ);
389 if (had) succ_doms->Add(block->id());
390 }
391 }
392 }
393
394 // Verify the immediateness of dominators.
395 for (BasicBlockVector::iterator b = rpo_order->begin();
396 b != rpo_order->end(); ++b) {
397 BasicBlock* block = *b;
398 BasicBlock* idom = schedule->dominator(block);
399 if (idom == NULL) continue;
400 BitVector* block_doms = dominators[block->id()];
401
402 for (BitVector::Iterator it(block_doms); !it.Done(); it.Advance()) {
403 BasicBlock* dom = schedule->GetBlockById(it.Current());
404 if (dom != idom && !dominators[idom->id()]->Contains(dom->id())) {
405 V8_Fatal(__FILE__, __LINE__,
406 "Block B%d is not immediately dominated by B%d", block->id(),
407 idom->id());
408 }
409 }
410 }
411 }
412
413 // Verify phis are placed in the block of their control input.
414 for (BasicBlockVector::iterator b = rpo_order->begin(); b != rpo_order->end();
415 ++b) {
416 for (BasicBlock::const_iterator i = (*b)->begin(); i != (*b)->end(); ++i) {
417 Node* phi = *i;
418 if (phi->opcode() != IrOpcode::kPhi) continue;
419 // TODO(titzer): Nasty special case. Phis from RawMachineAssembler
420 // schedules don't have control inputs.
421 if (phi->InputCount() >
422 OperatorProperties::GetValueInputCount(phi->op())) {
423 Node* control = NodeProperties::GetControlInput(phi);
424 CHECK(control->opcode() == IrOpcode::kMerge ||
425 control->opcode() == IrOpcode::kLoop);
426 CHECK_EQ((*b), schedule->block(control));
427 }
428 }
429 }
430
431 // Verify that all uses are dominated by their definitions.
432 for (BasicBlockVector::iterator b = rpo_order->begin(); b != rpo_order->end();
433 ++b) {
434 BasicBlock* block = *b;
435
436 // Check inputs to control for this block.
437 Node* control = block->control_input_;
438 if (control != NULL) {
439 CHECK_EQ(block, schedule->block(control));
440 CheckInputsDominate(schedule, block, control,
441 static_cast<int>(block->nodes_.size()) - 1);
442 }
443 // Check inputs for all nodes in the block.
444 for (size_t i = 0; i < block->nodes_.size(); i++) {
445 Node* node = block->nodes_[i];
446 CheckInputsDominate(schedule, block, node, static_cast<int>(i) - 1);
447 }
448 }
449 }
246 } 450 }
247 } 451 }
248 } // namespace v8::internal::compiler 452 } // namespace v8::internal::compiler
OLDNEW
« no previous file with comments | « src/compiler/verifier.h ('k') | src/data-flow.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698