llvm.org GIT mirror llvm / 3a82e35
[Dominators] Simplify block and node printing Summary: This patch adds `BlockPrinter`-- a small wrapper for printing CFG nodes and DomTree nodes to `raw_ostream`. It is meant to be only used internally, for debugging and printing errors. Reviewers: dberlin, sanjoy, grosser, davide Reviewed By: grosser, davide Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D35286 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@308036 91177308-0d34-0410-b5e6-96231b3b80d8 Jakub Kuderski 3 years ago
1 changed file(s) with 39 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
100100
101101 static bool AlwaysDescend(NodePtr, NodePtr) { return true; }
102102
103 struct BlockNamePrinter {
104 NodePtr N;
105
106 BlockNamePrinter(NodePtr Block) : N(Block) {}
107 BlockNamePrinter(TreeNodePtr TN) : N(TN ? TN->getBlock() : nullptr) {}
108
109 friend raw_ostream &operator<<(raw_ostream &O, const BlockNamePrinter &BP) {
110 if (!BP.N)
111 O << "nullptr";
112 else
113 BP.N->printAsOperand(O, false);
114
115 return O;
116 }
117 };
118
103119 // Custom DFS implementation which can skip nodes based on a provided
104120 // predicate. It also collects ReverseChildren so that we don't have to spend
105121 // time getting predecessors in SemiNCA.
280296 // Loop over all of the discovered blocks in the function...
281297 for (size_t i = 1, e = NumToNode.size(); i != e; ++i) {
282298 NodePtr W = NumToNode[i];
283 DEBUG(dbgs() << "\tdiscovereed a new reachable node ");
284 DEBUG(PrintBlockOrNullptr(dbgs(), W));
285 DEBUG(dbgs() << "\n");
299 DEBUG(dbgs() << "\tdiscovered a new reachable node "
300 << BlockNamePrinter(W) << "\n");
286301
287302 // Don't replace this with 'count', the insertion side effect is important
288303 if (DT.DomTreeNodes[W]) continue; // Haven't calculated this node yet?
299314 }
300315 }
301316
302 static void PrintBlockOrNullptr(raw_ostream &O, NodePtr Obj) {
303 if (!Obj)
304 O << "nullptr";
305 else
306 Obj->printAsOperand(O, false);
307 }
308
309317 // Checks if the tree contains all reachable nodes in the input graph.
310318 bool verifyReachability(const DomTreeT &DT) {
311319 clear();
319327 if (DT.isVirtualRoot(TN)) continue;
320328
321329 if (NodeToInfo.count(BB) == 0) {
322 errs() << "DomTree node ";
323 PrintBlockOrNullptr(errs(), BB);
324 errs() << " not found by DFS walk!\n";
330 errs() << "DomTree node " << BlockNamePrinter(BB)
331 << " not found by DFS walk!\n";
325332 errs().flush();
326333
327334 return false;
330337
331338 for (const NodePtr N : NumToNode) {
332339 if (N && !DT.getNode(N)) {
333 errs() << "CFG node ";
334 PrintBlockOrNullptr(errs(), N);
335 errs() << " not found in the DomTree!\n";
340 errs() << "CFG node " << BlockNamePrinter(N)
341 << " not found in the DomTree!\n";
336342 errs().flush();
337343
338344 return false;
352358
353359 const TreeNodePtr IDom = TN->getIDom();
354360 if (!IDom && TN->getLevel() != 0) {
355 errs() << "Node without an IDom ";
356 PrintBlockOrNullptr(errs(), BB);
357 errs() << " has a nonzero level " << TN->getLevel() << "!\n";
361 errs() << "Node without an IDom " << BlockNamePrinter(BB)
362 << " has a nonzero level " << TN->getLevel() << "!\n";
358363 errs().flush();
359364
360365 return false;
361366 }
362367
363368 if (IDom && TN->getLevel() != IDom->getLevel() + 1) {
364 errs() << "Node ";
365 PrintBlockOrNullptr(errs(), BB);
366 errs() << " has level " << TN->getLevel() << " while it's IDom ";
367 PrintBlockOrNullptr(errs(), IDom->getBlock());
368 errs() << " has level " << IDom->getLevel() << "!\n";
369 errs() << "Node " << BlockNamePrinter(BB) << " has level "
370 << TN->getLevel() << " while its IDom "
371 << BlockNamePrinter(IDom->getBlock()) << " has level "
372 << IDom->getLevel() << "!\n";
369373 errs().flush();
370374
371375 return false;
395399 const TreeNodePtr NCDTN = DT.getNode(NCD);
396400 const TreeNodePtr ToIDom = ToTN->getIDom();
397401 if (NCDTN != ToTN && NCDTN != ToIDom) {
398 errs() << "NearestCommonDominator verification failed:\n\tNCD(From:";
399 PrintBlockOrNullptr(errs(), From);
400 errs() << ", To:";
401 PrintBlockOrNullptr(errs(), To);
402 errs() << ") = ";
403 PrintBlockOrNullptr(errs(), NCD);
404 errs() << ",\t (should be To or IDom[To]: ";
405 PrintBlockOrNullptr(errs(), ToIDom ? ToIDom->getBlock() : nullptr);
406 errs() << ")\n";
402 errs() << "NearestCommonDominator verification failed:\n\tNCD(From:"
403 << BlockNamePrinter(From) << ", To:" << BlockNamePrinter(To)
404 << ") = " << BlockNamePrinter(NCD)
405 << ",\t (should be To or IDom[To]: " << BlockNamePrinter(ToIDom)
406 << ")\n";
407407 errs().flush();
408408
409409 return false;
469469
470470 for (TreeNodePtr Child : TN->getChildren())
471471 if (NodeToInfo.count(Child->getBlock()) != 0) {
472 errs() << "Child ";
473 PrintBlockOrNullptr(errs(), Child->getBlock());
474 errs() << " reachable after its parent ";
475 PrintBlockOrNullptr(errs(), BB);
476 errs() << " is removed!\n";
472 errs() << "Child " << BlockNamePrinter(Child)
473 << " reachable after its parent " << BlockNamePrinter(BB)
474 << " is removed!\n";
477475 errs().flush();
478476
479477 return false;
506504 if (S == N) continue;
507505
508506 if (NodeToInfo.count(S->getBlock()) == 0) {
509 errs() << "Node ";
510 PrintBlockOrNullptr(errs(), S->getBlock());
511 errs() << " not reachable when its sibling ";
512 PrintBlockOrNullptr(errs(), N->getBlock());
513 errs() << " is removed!\n";
507 errs() << "Node " << BlockNamePrinter(S)
508 << " not reachable when its sibling " << BlockNamePrinter(N)
509 << " is removed!\n";
514510 errs().flush();
515511
516512 return false;