Index: include/llvm/Support/GenericDomTreeConstruction.h =================================================================== --- include/llvm/Support/GenericDomTreeConstruction.h +++ include/llvm/Support/GenericDomTreeConstruction.h @@ -100,6 +100,22 @@ static bool AlwaysDescend(NodePtr, NodePtr) { return true; } + struct BlockPrinter { + NodePtr N; + + BlockPrinter(NodePtr Block) : N(Block) {} + BlockPrinter(TreeNodePtr TN) : N(TN ? TN->getBlock() : nullptr) {} + + friend raw_ostream &operator<<(raw_ostream &O, const BlockPrinter &BP) { + if (!BP.N) + O << "nullptr"; + else + BP.N->printAsOperand(O, false); + + return O; + } + }; + // Custom DFS implementation which can skip nodes based on a provided // predicate. It also collects ReverseChildren so that we don't have to spend // time getting predecessors in SemiNCA. @@ -279,9 +295,8 @@ // Loop over all of the discovered blocks in the function... for (size_t i = 1, e = NumToNode.size(); i != e; ++i) { NodePtr W = NumToNode[i]; - DTB_DEBUG(dbgs() << "\tdiscovereed a new reachable node "); - DTB_DEBUG(PrintBlockOrNullptr(dbgs(), W)); - DTB_DEBUG(dbgs() << "\n"); + DTB_DEBUG(dbgs() << "\tdiscovereed a new reachable node " + << BlockPrinter(W) << "\n"); // Don't replace this with 'count', the insertion side effect is important if (DT.DomTreeNodes[W]) continue; // Haven't calculated this node yet? @@ -298,13 +313,6 @@ } } - static void PrintBlockOrNullptr(raw_ostream &O, NodePtr Obj) { - if (!Obj) - O << "nullptr"; - else - Obj->printAsOperand(O, false); - } - // Checks if the tree contains all reachable nodes in the input graph. bool verifyReachability(const DomTreeT &DT) { clear(); @@ -318,9 +326,8 @@ if (DT.isVirtualRoot(TN)) continue; if (!BB || NodeToInfo.count(BB) == 0) { - errs() << "DomTree node "; - PrintBlockOrNullptr(errs(), BB); - errs() << " not found by DFS walk!\n"; + errs() << "DomTree node " << BlockPrinter(BB) + << " not found by DFS walk!\n"; errs().flush(); return false; @@ -329,9 +336,8 @@ for (const NodePtr N : NumToNode) { if (N && !DT.getNode(N)) { - errs() << "CFG node "; - PrintBlockOrNullptr(errs(), N); - errs() << " not found in the DomTree!\n"; + errs() << "CFG node " << BlockPrinter(N) + << " not found in the DomTree!\n"; errs().flush(); return false; @@ -351,20 +357,17 @@ const TreeNodePtr IDom = TN->getIDom(); if (!IDom && TN->getLevel() != 0) { - errs() << "Node without an IDom "; - PrintBlockOrNullptr(errs(), BB); - errs() << " has a nonzero level " << TN->getLevel() << "!\n"; + errs() << "Node without an IDom " << BlockPrinter(BB) + << " has a nonzero level " << TN->getLevel() << "!\n"; errs().flush(); return false; } if (IDom && TN->getLevel() != IDom->getLevel() + 1) { - errs() << "Node "; - PrintBlockOrNullptr(errs(), BB); - errs() << " has level " << TN->getLevel() << " while it's IDom "; - PrintBlockOrNullptr(errs(), IDom->getBlock()); - errs() << " has level " << IDom->getLevel() << "!\n"; + errs() << "Node " << BlockPrinter(BB) << " has level " << TN->getLevel() + << " while its IDom " << BlockPrinter(IDom->getBlock()) + << " has level " << IDom->getLevel() << "!\n"; errs().flush(); return false; @@ -394,15 +397,11 @@ const TreeNodePtr NCDTN = DT.getNode(NCD); const TreeNodePtr ToIDom = ToTN->getIDom(); if (NCDTN != ToTN && NCDTN != ToIDom) { - errs() << "NearestCommonDominator verification failed:\n\tNCD(From:"; - PrintBlockOrNullptr(errs(), From); - errs() << ", To:"; - PrintBlockOrNullptr(errs(), To); - errs() << ") = "; - PrintBlockOrNullptr(errs(), NCD); - errs() << ",\t (should be To or IDom[To]: "; - PrintBlockOrNullptr(errs(), ToIDom ? ToIDom->getBlock() : nullptr); - errs() << ")\n"; + errs() << "NearestCommonDominator verification failed:\n\tNCD(From:" + << BlockPrinter(From) << ", To:" << BlockPrinter(To) + << ") = " << BlockPrinter(NCD) + << ",\t (should be To or IDom[To]: " << BlockPrinter(ToIDom) + << ")\n"; errs().flush(); return false; @@ -468,11 +467,9 @@ for (TreeNodePtr Child : TN->getChildren()) if (NodeToInfo.count(Child->getBlock()) != 0) { - errs() << "Child "; - PrintBlockOrNullptr(errs(), Child->getBlock()); - errs() << " reachable after its parent "; - PrintBlockOrNullptr(errs(), BB); - errs() << " is removed!\n"; + errs() << "Child " << BlockPrinter(Child) + << " reachable after its parent " << BlockPrinter(BB) + << " is removed!\n"; errs().flush(); return false; @@ -505,11 +502,9 @@ if (S == N) continue; if (NodeToInfo.count(S->getBlock()) == 0) { - errs() << "Node "; - PrintBlockOrNullptr(errs(), S->getBlock()); - errs() << " not reachable when its sibling "; - PrintBlockOrNullptr(errs(), N->getBlock()); - errs() << " is removed!\n"; + errs() << "Node " << BlockPrinter(S) + << " not reachable when its sibling " << BlockPrinter(N) + << " is removed!\n"; errs().flush(); return false;