Skip to content
Open
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
141 changes: 141 additions & 0 deletions graph/include/DominatorAnalysis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* File: DominatorAnalysis.h
* License: Part of the MetaCG project. Licensed under BSD 3 clause license. See LICENSE.txt file at
* https://github.com/tudasc/metacg/LICENSE.txt
*/
#ifndef METACG_DOMINATORANALYSIS_H
#define METACG_DOMINATORANALYSIS_H

#include <algorithm>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace metacg::analysis {

enum class TraverseDir { Forward, Backward };

template <typename NodeT>
struct DomData {
using NodeSet = std::unordered_set<const NodeT*>;
const NodeT* node{nullptr};
NodeSet Doms{};
bool initialized{false};
};

template <typename NodeT>
using DomAnalysisResult = std::unordered_map<const NodeT*, DomData<NodeT>>;

template <typename NodeT, typename GraphT>
DomAnalysisResult<NodeT> computeDoms(const GraphT& graph, const NodeT& exitNode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are people opinions on renaming computeDominators? I would think we can afford the additional 6 characters.


template <typename Container>
Container unordered_intersection(const Container& a, const Container& b) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why snack case here?

// std::set<typename Container::value_type> orderedA(a.begin(), a.end());
// std::set<typename Container::value_type> orderedB(b.begin(), b.end());
Comment on lines +35 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

Container result;
// std::set_intersection(orderedA.begin(), orderedA.end(),
// orderedB.begin(), orderedB.end(),
// std::inserter(result, result.begin()));
Comment on lines +38 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

for (auto& item : a) {
if (std::find(b.begin(), b.end(), item) != b.end()) {
result.insert(item);
}
}
return result;
}

// Adopted from https://stackoverflow.com/questions/25505868/the-intersection-of-multiple-sorted-arrays
template <typename NodeT>
typename DomData<NodeT>::NodeSet intersection(const std::vector<const NodeT*>& nodes,
const DomAnalysisResult<NodeT>& DomMap) {
if (nodes.empty())
return {};

auto last_intersection = DomMap.at(nodes[0]).Doms;

typename DomData<NodeT>::NodeSet curr_intersection;

for (std::size_t i = 1; i < nodes.size(); ++i) {
auto& currSet = DomMap.at(nodes[i]).Doms;
// LOG_STATUS("Intersecting " << dumpNodeSet("a", last_intersection) << " and " << dumpNodeSet("b", currSet) <<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either remove or leave as debug / trace

// "\n");

// Note: std::set_intersection does not work with unordered_set.
curr_intersection = unordered_intersection(last_intersection, currSet);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why snake case curr_intersection here?

// std::set_intersection(last_intersection.begin(), last_intersection.end(),
// currSet.begin(), currSet.end(),
// std::inserter(curr_intersection, curr_intersection.begin()));
// LOG_STATUS("Result of intersection" << dumpNodeSet("", curr_intersection) << "\n");
Comment on lines +67 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

std::swap(last_intersection, curr_intersection);
curr_intersection.clear();
}
return last_intersection;
}

template <typename NodeT, typename GraphT, TraverseDir dir>
DomAnalysisResult<NodeT> computeDoms(const GraphT& graph, const NodeT& exitNode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here computeDominators?

auto incoming = [&](const NodeT* n) {
if constexpr (dir == TraverseDir::Forward)
return graph.getCallers(*n);
else
return graph.getCallees(*n);
};

auto outgoing = [&](const NodeT* n) {
if constexpr (dir == TraverseDir::Backward)
return graph.getCallers(*n);
else
return graph.getCallees(*n);
};

using DomDataT = DomData<NodeT>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this move to beginning of scope?


DomAnalysisResult<NodeT> DomMap{{&exitNode, {&exitNode, {}, false}}};

std::deque<DomDataT*> workQueue{&DomMap[&exitNode]};

auto addToQueue = [&workQueue](DomDataT* const data) {
if (std::find(workQueue.begin(), workQueue.end(), data) == workQueue.end()) {
workQueue.push_back(data);
}
};

do {
auto& nodeData = *workQueue.front();
workQueue.pop_front();

std::vector<const NodeT*> initializedCallees{};
for (auto* calleePtr : incoming(nodeData.node)) {
if (DomMap[calleePtr].initialized) {
initializedCallees.push_back(calleePtr);
}
}

typename DomDataT::NodeSet DomNew = intersection(initializedCallees, DomMap);
DomNew.insert(nodeData.node);

// LOG_STATUS("New Doms " << dumpNodeSet(nodeData.node->getName(), DomNew) << "\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?


if (!nodeData.initialized || nodeData.Doms != DomNew) {
nodeData.Doms = std::move(DomNew);
nodeData.initialized = true;

auto outs = outgoing(nodeData.node);
for (auto* callerPtr : outs) {
auto& data = DomMap[callerPtr];
if (!data.node) {
data.node = callerPtr;
}
addToQueue(&data);
}
}
} while (!workQueue.empty());

return DomMap;
}

} // namespace metacg::analysis

#endif
1 change: 1 addition & 0 deletions graph/test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
add_executable(
libtests
CGNodeTests.cpp
DominatorAnalysisTest.cpp
DotIOTest.cpp
GlobalMDTest.cpp
LoggingTest.cpp
Expand Down
Loading