-
Notifications
You must be signed in to change notification settings - Fork 10
[CGQuery] Refactor & add (Post) Dominator analysis #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devel
Are you sure you want to change the base?
Changes from all commits
6d242ae
68a57b8
b748d09
3be92e6
15803b7
c16deaa
ff31b16
dc48dbb
24e1abb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
|
|
||
| template <typename Container> | ||
| Container unordered_intersection(const Container& a, const Container& b) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) << | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why snake case |
||
| // 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here |
||
| 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>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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.