Skip to content

Commit e1c4655

Browse files
committedJul 14, 2017
[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 llvm-svn: 308036
1 parent 92b5c3f commit e1c4655

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed
 

‎llvm/include/llvm/Support/GenericDomTreeConstruction.h

+39-43
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ struct SemiNCAInfo {
101101

102102
static bool AlwaysDescend(NodePtr, NodePtr) { return true; }
103103

104+
struct BlockNamePrinter {
105+
NodePtr N;
106+
107+
BlockNamePrinter(NodePtr Block) : N(Block) {}
108+
BlockNamePrinter(TreeNodePtr TN) : N(TN ? TN->getBlock() : nullptr) {}
109+
110+
friend raw_ostream &operator<<(raw_ostream &O, const BlockNamePrinter &BP) {
111+
if (!BP.N)
112+
O << "nullptr";
113+
else
114+
BP.N->printAsOperand(O, false);
115+
116+
return O;
117+
}
118+
};
119+
104120
// Custom DFS implementation which can skip nodes based on a provided
105121
// predicate. It also collects ReverseChildren so that we don't have to spend
106122
// time getting predecessors in SemiNCA.
@@ -281,9 +297,8 @@ struct SemiNCAInfo {
281297
// Loop over all of the discovered blocks in the function...
282298
for (size_t i = 1, e = NumToNode.size(); i != e; ++i) {
283299
NodePtr W = NumToNode[i];
284-
DEBUG(dbgs() << "\tdiscovereed a new reachable node ");
285-
DEBUG(PrintBlockOrNullptr(dbgs(), W));
286-
DEBUG(dbgs() << "\n");
300+
DEBUG(dbgs() << "\tdiscovered a new reachable node "
301+
<< BlockNamePrinter(W) << "\n");
287302

288303
// Don't replace this with 'count', the insertion side effect is important
289304
if (DT.DomTreeNodes[W]) continue; // Haven't calculated this node yet?
@@ -300,13 +315,6 @@ struct SemiNCAInfo {
300315
}
301316
}
302317

303-
static void PrintBlockOrNullptr(raw_ostream &O, NodePtr Obj) {
304-
if (!Obj)
305-
O << "nullptr";
306-
else
307-
Obj->printAsOperand(O, false);
308-
}
309-
310318
// Checks if the tree contains all reachable nodes in the input graph.
311319
bool verifyReachability(const DomTreeT &DT) {
312320
clear();
@@ -320,9 +328,8 @@ struct SemiNCAInfo {
320328
if (DT.isVirtualRoot(TN)) continue;
321329

322330
if (NodeToInfo.count(BB) == 0) {
323-
errs() << "DomTree node ";
324-
PrintBlockOrNullptr(errs(), BB);
325-
errs() << " not found by DFS walk!\n";
331+
errs() << "DomTree node " << BlockNamePrinter(BB)
332+
<< " not found by DFS walk!\n";
326333
errs().flush();
327334

328335
return false;
@@ -331,9 +338,8 @@ struct SemiNCAInfo {
331338

332339
for (const NodePtr N : NumToNode) {
333340
if (N && !DT.getNode(N)) {
334-
errs() << "CFG node ";
335-
PrintBlockOrNullptr(errs(), N);
336-
errs() << " not found in the DomTree!\n";
341+
errs() << "CFG node " << BlockNamePrinter(N)
342+
<< " not found in the DomTree!\n";
337343
errs().flush();
338344

339345
return false;
@@ -353,20 +359,18 @@ struct SemiNCAInfo {
353359

354360
const TreeNodePtr IDom = TN->getIDom();
355361
if (!IDom && TN->getLevel() != 0) {
356-
errs() << "Node without an IDom ";
357-
PrintBlockOrNullptr(errs(), BB);
358-
errs() << " has a nonzero level " << TN->getLevel() << "!\n";
362+
errs() << "Node without an IDom " << BlockNamePrinter(BB)
363+
<< " has a nonzero level " << TN->getLevel() << "!\n";
359364
errs().flush();
360365

361366
return false;
362367
}
363368

364369
if (IDom && TN->getLevel() != IDom->getLevel() + 1) {
365-
errs() << "Node ";
366-
PrintBlockOrNullptr(errs(), BB);
367-
errs() << " has level " << TN->getLevel() << " while it's IDom ";
368-
PrintBlockOrNullptr(errs(), IDom->getBlock());
369-
errs() << " has level " << IDom->getLevel() << "!\n";
370+
errs() << "Node " << BlockNamePrinter(BB) << " has level "
371+
<< TN->getLevel() << " while its IDom "
372+
<< BlockNamePrinter(IDom->getBlock()) << " has level "
373+
<< IDom->getLevel() << "!\n";
370374
errs().flush();
371375

372376
return false;
@@ -396,15 +400,11 @@ struct SemiNCAInfo {
396400
const TreeNodePtr NCDTN = DT.getNode(NCD);
397401
const TreeNodePtr ToIDom = ToTN->getIDom();
398402
if (NCDTN != ToTN && NCDTN != ToIDom) {
399-
errs() << "NearestCommonDominator verification failed:\n\tNCD(From:";
400-
PrintBlockOrNullptr(errs(), From);
401-
errs() << ", To:";
402-
PrintBlockOrNullptr(errs(), To);
403-
errs() << ") = ";
404-
PrintBlockOrNullptr(errs(), NCD);
405-
errs() << ",\t (should be To or IDom[To]: ";
406-
PrintBlockOrNullptr(errs(), ToIDom ? ToIDom->getBlock() : nullptr);
407-
errs() << ")\n";
403+
errs() << "NearestCommonDominator verification failed:\n\tNCD(From:"
404+
<< BlockNamePrinter(From) << ", To:" << BlockNamePrinter(To)
405+
<< ") = " << BlockNamePrinter(NCD)
406+
<< ",\t (should be To or IDom[To]: " << BlockNamePrinter(ToIDom)
407+
<< ")\n";
408408
errs().flush();
409409

410410
return false;
@@ -470,11 +470,9 @@ struct SemiNCAInfo {
470470

471471
for (TreeNodePtr Child : TN->getChildren())
472472
if (NodeToInfo.count(Child->getBlock()) != 0) {
473-
errs() << "Child ";
474-
PrintBlockOrNullptr(errs(), Child->getBlock());
475-
errs() << " reachable after its parent ";
476-
PrintBlockOrNullptr(errs(), BB);
477-
errs() << " is removed!\n";
473+
errs() << "Child " << BlockNamePrinter(Child)
474+
<< " reachable after its parent " << BlockNamePrinter(BB)
475+
<< " is removed!\n";
478476
errs().flush();
479477

480478
return false;
@@ -507,11 +505,9 @@ struct SemiNCAInfo {
507505
if (S == N) continue;
508506

509507
if (NodeToInfo.count(S->getBlock()) == 0) {
510-
errs() << "Node ";
511-
PrintBlockOrNullptr(errs(), S->getBlock());
512-
errs() << " not reachable when its sibling ";
513-
PrintBlockOrNullptr(errs(), N->getBlock());
514-
errs() << " is removed!\n";
508+
errs() << "Node " << BlockNamePrinter(S)
509+
<< " not reachable when its sibling " << BlockNamePrinter(N)
510+
<< " is removed!\n";
515511
errs().flush();
516512

517513
return false;

0 commit comments

Comments
 (0)
Please sign in to comment.