Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion llvm/include/llvm/CodeGen/SelectionDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,10 @@ class SelectionDAG {
LLVM_ABI SDValue makeEquivalentMemoryOrdering(LoadSDNode *OldLoad,
SDValue NewMemOp);

/// Get all the nodes in their topological order without modifying any states.
LLVM_ABI void getTopologicallyOrderedNodes(
SmallVectorImpl<const SDNode *> &SortedNodes) const;

/// Topological-sort the AllNodes list and a
/// assign a unique node id for each node in the DAG based on their
/// topological order. Returns the number of nodes.
Expand Down Expand Up @@ -2009,7 +2013,9 @@ class SelectionDAG {
/// function mirrors \c llvm::salvageDebugInfo.
LLVM_ABI void salvageDebugInfo(SDNode &N);

LLVM_ABI void dump() const;
/// Dump the textual format of this DAG. Print nodes in sorted orders if \p
/// Sorted is true.
LLVM_ABI void dump(bool Sorted = false) const;

/// In most cases this function returns the ABI alignment for a given type,
/// except for illegal vector types where the alignment exceeds that of the
Expand Down
39 changes: 39 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12693,6 +12693,45 @@ unsigned SelectionDAG::AssignTopologicalOrder() {
return DAGSize;
}

void SelectionDAG::getTopologicallyOrderedNodes(
SmallVectorImpl<const SDNode *> &SortedNodes) const {
SortedNodes.clear();
// Node -> remaining number of outstanding operands.
DenseMap<const SDNode *, unsigned> RemainingOperands;

// Put nodes without any operands into SortedNodes first.
for (const SDNode &N : allnodes()) {
checkForCycles(&N, this);
unsigned NumOperands = N.getNumOperands();
if (NumOperands == 0)
SortedNodes.push_back(&N);
else
// Record their total number of outstanding operands.
RemainingOperands[&N] = NumOperands;
}

// A node is pushed into SortedNodes when all of its operands (predecessors in
// the graph) are also in SortedNodes.
for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
const SDNode *N = SortedNodes[i];
for (const SDNode *U : N->users()) {
unsigned &NumRemOperands = RemainingOperands[U];
assert(NumRemOperands && "Invalid number of remaining operands");
--NumRemOperands;
if (!NumRemOperands)
SortedNodes.push_back(U);
}
}

assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
"First node in topological sort is not the entry token");
assert(SortedNodes.front()->getNumOperands() == 0 &&
"First node in topological sort has operands");
assert(SortedNodes.back()->use_empty() &&
"Last node in topologic sort has users");
}

/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
/// value is produced by SD.
void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
Expand Down
15 changes: 13 additions & 2 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,13 +1061,24 @@ static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
N->dump(G);
}

LLVM_DUMP_METHOD void SelectionDAG::dump() const {
LLVM_DUMP_METHOD void SelectionDAG::dump(bool Sorted) const {
dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:\n";

for (const SDNode &N : allnodes()) {
auto dumpEachNode = [this](const SDNode &N) {
if (!N.hasOneUse() && &N != getRoot().getNode() &&
(!shouldPrintInline(N, this) || N.use_empty()))
DumpNodes(&N, 2, this);
};

if (Sorted) {
SmallVector<const SDNode *> SortedNodes;
SortedNodes.reserve(AllNodes.size());
getTopologicallyOrderedNodes(SortedNodes);
for (const SDNode *N : SortedNodes)
dumpEachNode(*N);
} else {
for (const SDNode &N : allnodes())
dumpEachNode(N);
}

if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Expand Down
25 changes: 15 additions & 10 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ UseMBPI("use-mbpi",
cl::init(true), cl::Hidden);

#ifndef NDEBUG
static cl::opt<bool>
DumpSortedDAG("dump-sorted-dags", cl::Hidden,
cl::desc("Print DAGs with sorted nodes in debug dump"),
cl::init(false));

static cl::opt<std::string>
FilterDAGBasicBlockName("filter-view-dags", cl::Hidden,
cl::desc("Only display the basic block whose name "
Expand Down Expand Up @@ -932,7 +937,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nInitial selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
if (TTI->hasBranchDivergence())
Expand All @@ -952,7 +957,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nOptimized lowered selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
if (TTI->hasBranchDivergence())
Expand All @@ -974,7 +979,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nType-legalized selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
if (TTI->hasBranchDivergence())
Expand All @@ -998,7 +1003,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nOptimized type-legalized selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
if (TTI->hasBranchDivergence())
Expand All @@ -1016,7 +1021,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nVector-legalized selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
if (TTI->hasBranchDivergence())
Expand All @@ -1032,7 +1037,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nVector/type-legalized selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
if (TTI->hasBranchDivergence())
Expand All @@ -1052,7 +1057,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nOptimized vector-legalized selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
if (TTI->hasBranchDivergence())
Expand All @@ -1072,7 +1077,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nLegalized selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
if (TTI->hasBranchDivergence())
Expand All @@ -1092,7 +1097,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nOptimized legalized selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
if (TTI->hasBranchDivergence())
Expand All @@ -1116,7 +1121,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
ISEL_DUMP(dbgs() << "\nSelected selection DAG: "
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
<< "'\n";
CurDAG->dump());
CurDAG->dump(DumpSortedDAG));

if (ViewSchedDAGs && MatchFilterBB)
CurDAG->viewGraph("scheduler input for " + BlockName);
Expand Down