/src/shaderc/third_party/spirv-tools/source/cfa.h
Line | Count | Source |
1 | | // Copyright (c) 2015-2016 The Khronos Group Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef SOURCE_CFA_H_ |
16 | | #define SOURCE_CFA_H_ |
17 | | |
18 | | #include <stddef.h> |
19 | | |
20 | | #include <algorithm> |
21 | | #include <cassert> |
22 | | #include <cstdint> |
23 | | #include <functional> |
24 | | #include <map> |
25 | | #include <unordered_map> |
26 | | #include <unordered_set> |
27 | | #include <utility> |
28 | | #include <vector> |
29 | | |
30 | | namespace spvtools { |
31 | | |
32 | | // Control Flow Analysis of control flow graphs of basic block nodes |BB|. |
33 | | template <class BB> |
34 | | class CFA { |
35 | | using bb_ptr = BB*; |
36 | | using cbb_ptr = const BB*; |
37 | | using bb_iter = typename std::vector<BB*>::const_iterator; |
38 | | using get_blocks_func = std::function<const std::vector<BB*>*(const BB*)>; |
39 | | |
40 | | struct block_info { |
41 | | cbb_ptr block; ///< pointer to the block |
42 | | bb_iter iter; ///< Iterator to the current child node being processed |
43 | | }; |
44 | | |
45 | | /// Returns true if a block with @p id is found in the @p work_list vector |
46 | | /// |
47 | | /// @param[in] work_list Set of blocks visited in the depth first |
48 | | /// traversal |
49 | | /// of the CFG |
50 | | /// @param[in] id The ID of the block being checked |
51 | | /// |
52 | | /// @return true if the edge work_list.back().block->id() => id is a back-edge |
53 | | static bool FindInWorkList(const std::vector<block_info>& work_list, |
54 | | uint32_t id); |
55 | | |
56 | | public: |
57 | | /// @brief Depth first traversal starting from the \p entry BasicBlock |
58 | | /// |
59 | | /// This function performs a depth first traversal from the \p entry |
60 | | /// BasicBlock and calls the pre/postorder functions when it needs to process |
61 | | /// the node in pre order, post order. |
62 | | /// |
63 | | /// @param[in] entry The root BasicBlock of a CFG |
64 | | /// @param[in] successor_func A function which will return a pointer to the |
65 | | /// successor nodes |
66 | | /// @param[in] preorder A function that will be called for every block in a |
67 | | /// CFG following preorder traversal semantics |
68 | | /// @param[in] postorder A function that will be called for every block in a |
69 | | /// CFG following postorder traversal semantics |
70 | | /// @param[in] terminal A function that will be called to determine if the |
71 | | /// search should stop at the given node. |
72 | | /// NOTE: The @p successor_func and predecessor_func each return a pointer to |
73 | | /// a collection such that iterators to that collection remain valid for the |
74 | | /// lifetime of the algorithm. |
75 | | static void DepthFirstTraversal(const BB* entry, |
76 | | get_blocks_func successor_func, |
77 | | std::function<void(cbb_ptr)> preorder, |
78 | | std::function<void(cbb_ptr)> postorder, |
79 | | std::function<bool(cbb_ptr)> terminal); |
80 | | |
81 | | /// @brief Depth first traversal starting from the \p entry BasicBlock |
82 | | /// |
83 | | /// This function performs a depth first traversal from the \p entry |
84 | | /// BasicBlock and calls the pre/postorder functions when it needs to process |
85 | | /// the node in pre order, post order. It also calls the backedge function |
86 | | /// when a back edge is encountered. The backedge function can be empty. The |
87 | | /// runtime of the algorithm is improved if backedge is empty. |
88 | | /// |
89 | | /// @param[in] entry The root BasicBlock of a CFG |
90 | | /// @param[in] successor_func A function which will return a pointer to the |
91 | | /// successor nodes |
92 | | /// @param[in] preorder A function that will be called for every block in a |
93 | | /// CFG following preorder traversal semantics |
94 | | /// @param[in] postorder A function that will be called for every block in a |
95 | | /// CFG following postorder traversal semantics |
96 | | /// @param[in] backedge A function that will be called when a backedge is |
97 | | /// encountered during a traversal. |
98 | | /// @param[in] terminal A function that will be called to determine if the |
99 | | /// search should stop at the given node. |
100 | | /// NOTE: The @p successor_func and predecessor_func each return a pointer to |
101 | | /// a collection such that iterators to that collection remain valid for the |
102 | | /// lifetime of the algorithm. |
103 | | static void DepthFirstTraversal( |
104 | | const BB* entry, get_blocks_func successor_func, |
105 | | std::function<void(cbb_ptr)> preorder, |
106 | | std::function<void(cbb_ptr)> postorder, |
107 | | std::function<void(cbb_ptr, cbb_ptr)> backedge, |
108 | | std::function<bool(cbb_ptr)> terminal); |
109 | | |
110 | | /// @brief Calculates dominator edges for a set of blocks |
111 | | /// |
112 | | /// Computes dominators using the algorithm of Cooper, Harvey, and Kennedy |
113 | | /// "A Simple, Fast Dominance Algorithm", 2001. |
114 | | /// |
115 | | /// The algorithm assumes there is a unique root node (a node without |
116 | | /// predecessors), and it is therefore at the end of the postorder vector. |
117 | | /// |
118 | | /// This function calculates the dominator edges for a set of blocks in the |
119 | | /// CFG. |
120 | | /// Uses the dominator algorithm by Cooper et al. |
121 | | /// |
122 | | /// @param[in] postorder A vector of blocks in post order traversal |
123 | | /// order |
124 | | /// in a CFG |
125 | | /// @param[in] predecessor_func Function used to get the predecessor nodes of |
126 | | /// a |
127 | | /// block |
128 | | /// |
129 | | /// @return the dominator tree of the graph, as a vector of pairs of nodes. |
130 | | /// The first node in the pair is a node in the graph. The second node in the |
131 | | /// pair is its immediate dominator in the sense of Cooper et.al., where a |
132 | | /// block |
133 | | /// without predecessors (such as the root node) is its own immediate |
134 | | /// dominator. |
135 | | static std::vector<std::pair<BB*, BB*>> CalculateDominators( |
136 | | const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func); |
137 | | |
138 | | // Computes a minimal set of root nodes required to traverse, in the forward |
139 | | // direction, the CFG represented by the given vector of blocks, and successor |
140 | | // and predecessor functions. When considering adding two nodes, each having |
141 | | // predecessors, favour using the one that appears earlier on the input blocks |
142 | | // list. |
143 | | static std::vector<BB*> TraversalRoots(const std::vector<BB*>& blocks, |
144 | | get_blocks_func succ_func, |
145 | | get_blocks_func pred_func); |
146 | | |
147 | | static void ComputeAugmentedCFG( |
148 | | std::vector<BB*>& ordered_blocks, BB* pseudo_entry_block, |
149 | | BB* pseudo_exit_block, |
150 | | std::unordered_map<const BB*, std::vector<BB*>>* augmented_successors_map, |
151 | | std::unordered_map<const BB*, std::vector<BB*>>* |
152 | | augmented_predecessors_map, |
153 | | get_blocks_func succ_func, get_blocks_func pred_func); |
154 | | }; |
155 | | |
156 | | template <class BB> |
157 | | bool CFA<BB>::FindInWorkList(const std::vector<block_info>& work_list, |
158 | 4.50k | uint32_t id) { |
159 | 29.0k | for (const auto& b : work_list) { |
160 | 29.0k | if (b.block->id() == id) return true; |
161 | 29.0k | } |
162 | 4.41k | return false; |
163 | 4.50k | } spvtools::CFA<spvtools::val::BasicBlock>::FindInWorkList(std::__1::vector<spvtools::CFA<spvtools::val::BasicBlock>::block_info, std::__1::allocator<spvtools::CFA<spvtools::val::BasicBlock>::block_info> > const&, unsigned int) Line | Count | Source | 158 | 4.50k | uint32_t id) { | 159 | 29.0k | for (const auto& b : work_list) { | 160 | 29.0k | if (b.block->id() == id) return true; | 161 | 29.0k | } | 162 | 4.41k | return false; | 163 | 4.50k | } |
Unexecuted instantiation: spvtools::CFA<spvtools::opt::BasicBlock>::FindInWorkList(std::__1::vector<spvtools::CFA<spvtools::opt::BasicBlock>::block_info, std::__1::allocator<spvtools::CFA<spvtools::opt::BasicBlock>::block_info> > const&, unsigned int) Unexecuted instantiation: spvtools::CFA<spvtools::opt::DominatorTreeNode>::FindInWorkList(std::__1::vector<spvtools::CFA<spvtools::opt::DominatorTreeNode>::block_info, std::__1::allocator<spvtools::CFA<spvtools::opt::DominatorTreeNode>::block_info> > const&, unsigned int) |
164 | | |
165 | | template <class BB> |
166 | | void CFA<BB>::DepthFirstTraversal(const BB* entry, |
167 | | get_blocks_func successor_func, |
168 | | std::function<void(cbb_ptr)> preorder, |
169 | | std::function<void(cbb_ptr)> postorder, |
170 | 3.64k | std::function<bool(cbb_ptr)> terminal) { |
171 | 3.64k | DepthFirstTraversal(entry, successor_func, preorder, postorder, |
172 | 3.64k | /* backedge = */ {}, terminal); |
173 | 3.64k | } spvtools::CFA<spvtools::val::BasicBlock>::DepthFirstTraversal(spvtools::val::BasicBlock const*, std::__1::function<std::__1::vector<spvtools::val::BasicBlock*, std::__1::allocator<spvtools::val::BasicBlock*> > const* (spvtools::val::BasicBlock const*)>, std::__1::function<void (spvtools::val::BasicBlock const*)>, std::__1::function<void (spvtools::val::BasicBlock const*)>, std::__1::function<bool (spvtools::val::BasicBlock const*)>) Line | Count | Source | 170 | 1.36k | std::function<bool(cbb_ptr)> terminal) { | 171 | 1.36k | DepthFirstTraversal(entry, successor_func, preorder, postorder, | 172 | 1.36k | /* backedge = */ {}, terminal); | 173 | 1.36k | } |
spvtools::CFA<spvtools::opt::BasicBlock>::DepthFirstTraversal(spvtools::opt::BasicBlock const*, std::__1::function<std::__1::vector<spvtools::opt::BasicBlock*, std::__1::allocator<spvtools::opt::BasicBlock*> > const* (spvtools::opt::BasicBlock const*)>, std::__1::function<void (spvtools::opt::BasicBlock const*)>, std::__1::function<void (spvtools::opt::BasicBlock const*)>, std::__1::function<bool (spvtools::opt::BasicBlock const*)>) Line | Count | Source | 170 | 1.91k | std::function<bool(cbb_ptr)> terminal) { | 171 | 1.91k | DepthFirstTraversal(entry, successor_func, preorder, postorder, | 172 | 1.91k | /* backedge = */ {}, terminal); | 173 | 1.91k | } |
spvtools::CFA<spvtools::opt::DominatorTreeNode>::DepthFirstTraversal(spvtools::opt::DominatorTreeNode const*, std::__1::function<std::__1::vector<spvtools::opt::DominatorTreeNode*, std::__1::allocator<spvtools::opt::DominatorTreeNode*> > const* (spvtools::opt::DominatorTreeNode const*)>, std::__1::function<void (spvtools::opt::DominatorTreeNode const*)>, std::__1::function<void (spvtools::opt::DominatorTreeNode const*)>, std::__1::function<bool (spvtools::opt::DominatorTreeNode const*)>) Line | Count | Source | 170 | 366 | std::function<bool(cbb_ptr)> terminal) { | 171 | 366 | DepthFirstTraversal(entry, successor_func, preorder, postorder, | 172 | 366 | /* backedge = */ {}, terminal); | 173 | 366 | } |
|
174 | | |
175 | | template <class BB> |
176 | | void CFA<BB>::DepthFirstTraversal( |
177 | | const BB* entry, get_blocks_func successor_func, |
178 | | std::function<void(cbb_ptr)> preorder, |
179 | | std::function<void(cbb_ptr)> postorder, |
180 | | std::function<void(cbb_ptr, cbb_ptr)> backedge, |
181 | 3.89k | std::function<bool(cbb_ptr)> terminal) { |
182 | 3.89k | assert(successor_func && "The successor function cannot be empty."); |
183 | 3.89k | assert(preorder && "The preorder function cannot be empty."); |
184 | 3.89k | assert(postorder && "The postorder function cannot be empty."); |
185 | 3.89k | assert(terminal && "The terminal function cannot be empty."); |
186 | | |
187 | 3.89k | std::unordered_set<uint32_t> processed; |
188 | | |
189 | | /// NOTE: work_list is the sequence of nodes from the root node to the node |
190 | | /// being processed in the traversal |
191 | 3.89k | std::vector<block_info> work_list; |
192 | 3.89k | work_list.reserve(10); |
193 | | |
194 | 3.89k | work_list.push_back({entry, std::begin(*successor_func(entry))}); |
195 | 3.89k | preorder(entry); |
196 | 3.89k | processed.insert(entry->id()); |
197 | | |
198 | 216k | while (!work_list.empty()) { |
199 | 212k | block_info& top = work_list.back(); |
200 | 212k | if (terminal(top.block) || top.iter == end(*successor_func(top.block))) { |
201 | 82.9k | postorder(top.block); |
202 | 82.9k | work_list.pop_back(); |
203 | 129k | } else { |
204 | 129k | BB* child = *top.iter; |
205 | 129k | top.iter++; |
206 | 129k | if (backedge && FindInWorkList(work_list, child->id())) { |
207 | 88 | backedge(top.block, child); |
208 | 88 | } |
209 | 129k | if (processed.count(child->id()) == 0) { |
210 | 79.1k | preorder(child); |
211 | 79.1k | work_list.emplace_back( |
212 | 79.1k | block_info{child, std::begin(*successor_func(child))}); |
213 | 79.1k | processed.insert(child->id()); |
214 | 79.1k | } |
215 | 129k | } |
216 | 212k | } |
217 | 3.89k | } spvtools::CFA<spvtools::val::BasicBlock>::DepthFirstTraversal(spvtools::val::BasicBlock const*, std::__1::function<std::__1::vector<spvtools::val::BasicBlock*, std::__1::allocator<spvtools::val::BasicBlock*> > const* (spvtools::val::BasicBlock const*)>, std::__1::function<void (spvtools::val::BasicBlock const*)>, std::__1::function<void (spvtools::val::BasicBlock const*)>, std::__1::function<void (spvtools::val::BasicBlock const*, spvtools::val::BasicBlock const*)>, std::__1::function<bool (spvtools::val::BasicBlock const*)>) Line | Count | Source | 181 | 1.61k | std::function<bool(cbb_ptr)> terminal) { | 182 | 1.61k | assert(successor_func && "The successor function cannot be empty."); | 183 | 1.61k | assert(preorder && "The preorder function cannot be empty."); | 184 | 1.61k | assert(postorder && "The postorder function cannot be empty."); | 185 | 1.61k | assert(terminal && "The terminal function cannot be empty."); | 186 | | | 187 | 1.61k | std::unordered_set<uint32_t> processed; | 188 | | | 189 | | /// NOTE: work_list is the sequence of nodes from the root node to the node | 190 | | /// being processed in the traversal | 191 | 1.61k | std::vector<block_info> work_list; | 192 | 1.61k | work_list.reserve(10); | 193 | | | 194 | 1.61k | work_list.push_back({entry, std::begin(*successor_func(entry))}); | 195 | 1.61k | preorder(entry); | 196 | 1.61k | processed.insert(entry->id()); | 197 | | | 198 | 41.8k | while (!work_list.empty()) { | 199 | 40.2k | block_info& top = work_list.back(); | 200 | 40.2k | if (terminal(top.block) || top.iter == end(*successor_func(top.block))) { | 201 | 15.6k | postorder(top.block); | 202 | 15.6k | work_list.pop_back(); | 203 | 24.5k | } else { | 204 | 24.5k | BB* child = *top.iter; | 205 | 24.5k | top.iter++; | 206 | 24.5k | if (backedge && FindInWorkList(work_list, child->id())) { | 207 | 88 | backedge(top.block, child); | 208 | 88 | } | 209 | 24.5k | if (processed.count(child->id()) == 0) { | 210 | 14.0k | preorder(child); | 211 | 14.0k | work_list.emplace_back( | 212 | 14.0k | block_info{child, std::begin(*successor_func(child))}); | 213 | 14.0k | processed.insert(child->id()); | 214 | 14.0k | } | 215 | 24.5k | } | 216 | 40.2k | } | 217 | 1.61k | } |
spvtools::CFA<spvtools::opt::BasicBlock>::DepthFirstTraversal(spvtools::opt::BasicBlock const*, std::__1::function<std::__1::vector<spvtools::opt::BasicBlock*, std::__1::allocator<spvtools::opt::BasicBlock*> > const* (spvtools::opt::BasicBlock const*)>, std::__1::function<void (spvtools::opt::BasicBlock const*)>, std::__1::function<void (spvtools::opt::BasicBlock const*)>, std::__1::function<void (spvtools::opt::BasicBlock const*, spvtools::opt::BasicBlock const*)>, std::__1::function<bool (spvtools::opt::BasicBlock const*)>) Line | Count | Source | 181 | 1.91k | std::function<bool(cbb_ptr)> terminal) { | 182 | 1.91k | assert(successor_func && "The successor function cannot be empty."); | 183 | 1.91k | assert(preorder && "The preorder function cannot be empty."); | 184 | 1.91k | assert(postorder && "The postorder function cannot be empty."); | 185 | 1.91k | assert(terminal && "The terminal function cannot be empty."); | 186 | | | 187 | 1.91k | std::unordered_set<uint32_t> processed; | 188 | | | 189 | | /// NOTE: work_list is the sequence of nodes from the root node to the node | 190 | | /// being processed in the traversal | 191 | 1.91k | std::vector<block_info> work_list; | 192 | 1.91k | work_list.reserve(10); | 193 | | | 194 | 1.91k | work_list.push_back({entry, std::begin(*successor_func(entry))}); | 195 | 1.91k | preorder(entry); | 196 | 1.91k | processed.insert(entry->id()); | 197 | | | 198 | 149k | while (!work_list.empty()) { | 199 | 147k | block_info& top = work_list.back(); | 200 | 147k | if (terminal(top.block) || top.iter == end(*successor_func(top.block))) { | 201 | 54.7k | postorder(top.block); | 202 | 54.7k | work_list.pop_back(); | 203 | 92.3k | } else { | 204 | 92.3k | BB* child = *top.iter; | 205 | 92.3k | top.iter++; | 206 | 92.3k | if (backedge && FindInWorkList(work_list, child->id())) { | 207 | 0 | backedge(top.block, child); | 208 | 0 | } | 209 | 92.3k | if (processed.count(child->id()) == 0) { | 210 | 52.8k | preorder(child); | 211 | 52.8k | work_list.emplace_back( | 212 | 52.8k | block_info{child, std::begin(*successor_func(child))}); | 213 | 52.8k | processed.insert(child->id()); | 214 | 52.8k | } | 215 | 92.3k | } | 216 | 147k | } | 217 | 1.91k | } |
spvtools::CFA<spvtools::opt::DominatorTreeNode>::DepthFirstTraversal(spvtools::opt::DominatorTreeNode const*, std::__1::function<std::__1::vector<spvtools::opt::DominatorTreeNode*, std::__1::allocator<spvtools::opt::DominatorTreeNode*> > const* (spvtools::opt::DominatorTreeNode const*)>, std::__1::function<void (spvtools::opt::DominatorTreeNode const*)>, std::__1::function<void (spvtools::opt::DominatorTreeNode const*)>, std::__1::function<void (spvtools::opt::DominatorTreeNode const*, spvtools::opt::DominatorTreeNode const*)>, std::__1::function<bool (spvtools::opt::DominatorTreeNode const*)>) Line | Count | Source | 181 | 366 | std::function<bool(cbb_ptr)> terminal) { | 182 | 366 | assert(successor_func && "The successor function cannot be empty."); | 183 | 366 | assert(preorder && "The preorder function cannot be empty."); | 184 | 366 | assert(postorder && "The postorder function cannot be empty."); | 185 | 366 | assert(terminal && "The terminal function cannot be empty."); | 186 | | | 187 | 366 | std::unordered_set<uint32_t> processed; | 188 | | | 189 | | /// NOTE: work_list is the sequence of nodes from the root node to the node | 190 | | /// being processed in the traversal | 191 | 366 | std::vector<block_info> work_list; | 192 | 366 | work_list.reserve(10); | 193 | | | 194 | 366 | work_list.push_back({entry, std::begin(*successor_func(entry))}); | 195 | 366 | preorder(entry); | 196 | 366 | processed.insert(entry->id()); | 197 | | | 198 | 25.1k | while (!work_list.empty()) { | 199 | 24.8k | block_info& top = work_list.back(); | 200 | 24.8k | if (terminal(top.block) || top.iter == end(*successor_func(top.block))) { | 201 | 12.5k | postorder(top.block); | 202 | 12.5k | work_list.pop_back(); | 203 | 12.5k | } else { | 204 | 12.2k | BB* child = *top.iter; | 205 | 12.2k | top.iter++; | 206 | 12.2k | if (backedge && FindInWorkList(work_list, child->id())) { | 207 | 0 | backedge(top.block, child); | 208 | 0 | } | 209 | 12.2k | if (processed.count(child->id()) == 0) { | 210 | 12.2k | preorder(child); | 211 | 12.2k | work_list.emplace_back( | 212 | 12.2k | block_info{child, std::begin(*successor_func(child))}); | 213 | 12.2k | processed.insert(child->id()); | 214 | 12.2k | } | 215 | 12.2k | } | 216 | 24.8k | } | 217 | 366 | } |
|
218 | | |
219 | | template <class BB> |
220 | | std::vector<std::pair<BB*, BB*>> CFA<BB>::CalculateDominators( |
221 | 1.12k | const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) { |
222 | 1.12k | struct block_detail { |
223 | 1.12k | size_t dominator; ///< The index of blocks's dominator in post order array |
224 | 1.12k | size_t postorder_index; ///< The index of the block in the post order array |
225 | 1.12k | }; |
226 | 1.12k | const size_t undefined_dom = postorder.size(); |
227 | | |
228 | 1.12k | std::unordered_map<cbb_ptr, block_detail> idoms; |
229 | 21.7k | for (size_t i = 0; i < postorder.size(); i++) { |
230 | 20.6k | idoms[postorder[i]] = {undefined_dom, i}; |
231 | 20.6k | } |
232 | 1.12k | idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index; |
233 | | |
234 | 1.12k | bool changed = true; |
235 | 3.36k | while (changed) { |
236 | 2.24k | changed = false; |
237 | 41.2k | for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) { |
238 | 39.0k | const std::vector<BB*>& predecessors = *predecessor_func(*b); |
239 | | // Find the first processed/reachable predecessor that is reachable |
240 | | // in the forward traversal. |
241 | 39.0k | auto res = std::find_if(std::begin(predecessors), std::end(predecessors), |
242 | 39.0k | [&idoms, undefined_dom](BB* pred) { |
243 | 39.0k | return idoms.count(pred) && |
244 | 39.0k | idoms[pred].dominator != undefined_dom; |
245 | 39.0k | }); spvtools::CFA<spvtools::val::BasicBlock>::CalculateDominators(std::__1::vector<spvtools::val::BasicBlock const*, std::__1::allocator<spvtools::val::BasicBlock const*> > const&, std::__1::function<std::__1::vector<spvtools::val::BasicBlock*, std::__1::allocator<spvtools::val::BasicBlock*> > const* (spvtools::val::BasicBlock const*)>)::{lambda(spvtools::val::BasicBlock*)#1}::operator()(spvtools::val::BasicBlock*) constLine | Count | Source | 242 | 14.5k | [&idoms, undefined_dom](BB* pred) { | 243 | 14.5k | return idoms.count(pred) && | 244 | 14.5k | idoms[pred].dominator != undefined_dom; | 245 | 14.5k | }); |
spvtools::CFA<spvtools::opt::BasicBlock>::CalculateDominators(std::__1::vector<spvtools::opt::BasicBlock const*, std::__1::allocator<spvtools::opt::BasicBlock const*> > const&, std::__1::function<std::__1::vector<spvtools::opt::BasicBlock*, std::__1::allocator<spvtools::opt::BasicBlock*> > const* (spvtools::opt::BasicBlock const*)>)::{lambda(spvtools::opt::BasicBlock*)#1}::operator()(spvtools::opt::BasicBlock*) constLine | Count | Source | 242 | 24.4k | [&idoms, undefined_dom](BB* pred) { | 243 | 24.4k | return idoms.count(pred) && | 244 | 24.4k | idoms[pred].dominator != undefined_dom; | 245 | 24.4k | }); |
|
246 | 39.0k | if (res == end(predecessors)) continue; |
247 | 39.0k | const BB* idom = *res; |
248 | 39.0k | size_t idom_idx = idoms[idom].postorder_index; |
249 | | |
250 | | // all other predecessors |
251 | 58.6k | for (const auto* p : predecessors) { |
252 | 58.6k | if (idom == p) continue; |
253 | | // Only consider nodes reachable in the forward traversal. |
254 | | // Otherwise the intersection doesn't make sense and will never |
255 | | // terminate. |
256 | 16.5k | if (!idoms.count(p)) continue; |
257 | 16.5k | if (idoms[p].dominator != undefined_dom) { |
258 | 15.4k | size_t finger1 = idoms[p].postorder_index; |
259 | 15.4k | size_t finger2 = idom_idx; |
260 | 31.1k | while (finger1 != finger2) { |
261 | 44.7k | while (finger1 < finger2) { |
262 | 29.0k | finger1 = idoms[postorder[finger1]].dominator; |
263 | 29.0k | } |
264 | 17.2k | while (finger2 < finger1) { |
265 | 1.49k | finger2 = idoms[postorder[finger2]].dominator; |
266 | 1.49k | } |
267 | 15.7k | } |
268 | 15.4k | idom_idx = finger1; |
269 | 15.4k | } |
270 | 16.5k | } |
271 | 39.0k | if (idoms[*b].dominator != idom_idx) { |
272 | 19.5k | idoms[*b].dominator = idom_idx; |
273 | 19.5k | changed = true; |
274 | 19.5k | } |
275 | 39.0k | } |
276 | 2.24k | } |
277 | | |
278 | 1.12k | std::vector<std::pair<bb_ptr, bb_ptr>> out; |
279 | 20.6k | for (auto idom : idoms) { |
280 | | // At this point if there is no dominator for the node, just make it |
281 | | // reflexive. |
282 | 20.6k | auto dominator = std::get<1>(idom).dominator; |
283 | 20.6k | if (dominator == undefined_dom) { |
284 | 0 | dominator = std::get<1>(idom).postorder_index; |
285 | 0 | } |
286 | | // NOTE: performing a const cast for convenient usage with |
287 | | // UpdateImmediateDominators |
288 | 20.6k | out.push_back({const_cast<BB*>(std::get<0>(idom)), |
289 | 20.6k | const_cast<BB*>(postorder[dominator])}); |
290 | 20.6k | } |
291 | | |
292 | | // Sort by postorder index to generate a deterministic ordering of edges. |
293 | 1.12k | std::sort( |
294 | 1.12k | out.begin(), out.end(), |
295 | 1.12k | [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs, |
296 | 119k | const std::pair<bb_ptr, bb_ptr>& rhs) { |
297 | 119k | assert(lhs.first); |
298 | 119k | assert(lhs.second); |
299 | 119k | assert(rhs.first); |
300 | 119k | assert(rhs.second); |
301 | 119k | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, |
302 | 119k | idoms[lhs.second].postorder_index); |
303 | 119k | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, |
304 | 119k | idoms[rhs.second].postorder_index); |
305 | 119k | return lhs_indices < rhs_indices; |
306 | 119k | }); spvtools::CFA<spvtools::val::BasicBlock>::CalculateDominators(std::__1::vector<spvtools::val::BasicBlock const*, std::__1::allocator<spvtools::val::BasicBlock const*> > const&, std::__1::function<std::__1::vector<spvtools::val::BasicBlock*, std::__1::allocator<spvtools::val::BasicBlock*> > const* (spvtools::val::BasicBlock const*)>)::{lambda(std::__1::pair<spvtools::val::BasicBlock*, spvtools::val::BasicBlock*> const&, std::__1::pair<spvtools::val::BasicBlock*, spvtools::val::BasicBlock*> const&)#1}::operator()(std::__1::pair<spvtools::val::BasicBlock*, spvtools::val::BasicBlock*> const&, std::__1::pair<spvtools::val::BasicBlock*, spvtools::val::BasicBlock*> const&) constLine | Count | Source | 296 | 40.8k | const std::pair<bb_ptr, bb_ptr>& rhs) { | 297 | 40.8k | assert(lhs.first); | 298 | 40.8k | assert(lhs.second); | 299 | 40.8k | assert(rhs.first); | 300 | | assert(rhs.second); | 301 | 40.8k | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, | 302 | 40.8k | idoms[lhs.second].postorder_index); | 303 | 40.8k | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, | 304 | 40.8k | idoms[rhs.second].postorder_index); | 305 | 40.8k | return lhs_indices < rhs_indices; | 306 | 40.8k | }); |
spvtools::CFA<spvtools::opt::BasicBlock>::CalculateDominators(std::__1::vector<spvtools::opt::BasicBlock const*, std::__1::allocator<spvtools::opt::BasicBlock const*> > const&, std::__1::function<std::__1::vector<spvtools::opt::BasicBlock*, std::__1::allocator<spvtools::opt::BasicBlock*> > const* (spvtools::opt::BasicBlock const*)>)::{lambda(std::__1::pair<spvtools::opt::BasicBlock*, spvtools::opt::BasicBlock*> const&, std::__1::pair<spvtools::opt::BasicBlock*, spvtools::opt::BasicBlock*> const&)#1}::operator()(std::__1::pair<spvtools::opt::BasicBlock*, spvtools::opt::BasicBlock*> const&, std::__1::pair<spvtools::opt::BasicBlock*, spvtools::opt::BasicBlock*> const&) constLine | Count | Source | 296 | 78.3k | const std::pair<bb_ptr, bb_ptr>& rhs) { | 297 | 78.3k | assert(lhs.first); | 298 | 78.3k | assert(lhs.second); | 299 | 78.3k | assert(rhs.first); | 300 | | assert(rhs.second); | 301 | 78.3k | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, | 302 | 78.3k | idoms[lhs.second].postorder_index); | 303 | 78.3k | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, | 304 | 78.3k | idoms[rhs.second].postorder_index); | 305 | 78.3k | return lhs_indices < rhs_indices; | 306 | 78.3k | }); |
|
307 | 1.12k | return out; |
308 | 1.12k | } spvtools::CFA<spvtools::val::BasicBlock>::CalculateDominators(std::__1::vector<spvtools::val::BasicBlock const*, std::__1::allocator<spvtools::val::BasicBlock const*> > const&, std::__1::function<std::__1::vector<spvtools::val::BasicBlock*, std::__1::allocator<spvtools::val::BasicBlock*> > const* (spvtools::val::BasicBlock const*)>) Line | Count | Source | 221 | 756 | const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) { | 222 | 756 | struct block_detail { | 223 | 756 | size_t dominator; ///< The index of blocks's dominator in post order array | 224 | 756 | size_t postorder_index; ///< The index of the block in the post order array | 225 | 756 | }; | 226 | 756 | const size_t undefined_dom = postorder.size(); | 227 | | | 228 | 756 | std::unordered_map<cbb_ptr, block_detail> idoms; | 229 | 8.78k | for (size_t i = 0; i < postorder.size(); i++) { | 230 | 8.02k | idoms[postorder[i]] = {undefined_dom, i}; | 231 | 8.02k | } | 232 | 756 | idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index; | 233 | | | 234 | 756 | bool changed = true; | 235 | 2.26k | while (changed) { | 236 | 1.51k | changed = false; | 237 | 16.0k | for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) { | 238 | 14.5k | const std::vector<BB*>& predecessors = *predecessor_func(*b); | 239 | | // Find the first processed/reachable predecessor that is reachable | 240 | | // in the forward traversal. | 241 | 14.5k | auto res = std::find_if(std::begin(predecessors), std::end(predecessors), | 242 | 14.5k | [&idoms, undefined_dom](BB* pred) { | 243 | 14.5k | return idoms.count(pred) && | 244 | 14.5k | idoms[pred].dominator != undefined_dom; | 245 | 14.5k | }); | 246 | 14.5k | if (res == end(predecessors)) continue; | 247 | 14.5k | const BB* idom = *res; | 248 | 14.5k | size_t idom_idx = idoms[idom].postorder_index; | 249 | | | 250 | | // all other predecessors | 251 | 24.1k | for (const auto* p : predecessors) { | 252 | 24.1k | if (idom == p) continue; | 253 | | // Only consider nodes reachable in the forward traversal. | 254 | | // Otherwise the intersection doesn't make sense and will never | 255 | | // terminate. | 256 | 6.58k | if (!idoms.count(p)) continue; | 257 | 6.58k | if (idoms[p].dominator != undefined_dom) { | 258 | 6.23k | size_t finger1 = idoms[p].postorder_index; | 259 | 6.23k | size_t finger2 = idom_idx; | 260 | 12.5k | while (finger1 != finger2) { | 261 | 16.1k | while (finger1 < finger2) { | 262 | 9.89k | finger1 = idoms[postorder[finger1]].dominator; | 263 | 9.89k | } | 264 | 7.35k | while (finger2 < finger1) { | 265 | 1.05k | finger2 = idoms[postorder[finger2]].dominator; | 266 | 1.05k | } | 267 | 6.29k | } | 268 | 6.23k | idom_idx = finger1; | 269 | 6.23k | } | 270 | 6.58k | } | 271 | 14.5k | if (idoms[*b].dominator != idom_idx) { | 272 | 7.27k | idoms[*b].dominator = idom_idx; | 273 | 7.27k | changed = true; | 274 | 7.27k | } | 275 | 14.5k | } | 276 | 1.51k | } | 277 | | | 278 | 756 | std::vector<std::pair<bb_ptr, bb_ptr>> out; | 279 | 8.02k | for (auto idom : idoms) { | 280 | | // At this point if there is no dominator for the node, just make it | 281 | | // reflexive. | 282 | 8.02k | auto dominator = std::get<1>(idom).dominator; | 283 | 8.02k | if (dominator == undefined_dom) { | 284 | 0 | dominator = std::get<1>(idom).postorder_index; | 285 | 0 | } | 286 | | // NOTE: performing a const cast for convenient usage with | 287 | | // UpdateImmediateDominators | 288 | 8.02k | out.push_back({const_cast<BB*>(std::get<0>(idom)), | 289 | 8.02k | const_cast<BB*>(postorder[dominator])}); | 290 | 8.02k | } | 291 | | | 292 | | // Sort by postorder index to generate a deterministic ordering of edges. | 293 | 756 | std::sort( | 294 | 756 | out.begin(), out.end(), | 295 | 756 | [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs, | 296 | 756 | const std::pair<bb_ptr, bb_ptr>& rhs) { | 297 | 756 | assert(lhs.first); | 298 | 756 | assert(lhs.second); | 299 | 756 | assert(rhs.first); | 300 | 756 | assert(rhs.second); | 301 | 756 | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, | 302 | 756 | idoms[lhs.second].postorder_index); | 303 | 756 | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, | 304 | 756 | idoms[rhs.second].postorder_index); | 305 | 756 | return lhs_indices < rhs_indices; | 306 | 756 | }); | 307 | 756 | return out; | 308 | 756 | } |
spvtools::CFA<spvtools::opt::BasicBlock>::CalculateDominators(std::__1::vector<spvtools::opt::BasicBlock const*, std::__1::allocator<spvtools::opt::BasicBlock const*> > const&, std::__1::function<std::__1::vector<spvtools::opt::BasicBlock*, std::__1::allocator<spvtools::opt::BasicBlock*> > const* (spvtools::opt::BasicBlock const*)>) Line | Count | Source | 221 | 366 | const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) { | 222 | 366 | struct block_detail { | 223 | 366 | size_t dominator; ///< The index of blocks's dominator in post order array | 224 | 366 | size_t postorder_index; ///< The index of the block in the post order array | 225 | 366 | }; | 226 | 366 | const size_t undefined_dom = postorder.size(); | 227 | | | 228 | 366 | std::unordered_map<cbb_ptr, block_detail> idoms; | 229 | 12.9k | for (size_t i = 0; i < postorder.size(); i++) { | 230 | 12.5k | idoms[postorder[i]] = {undefined_dom, i}; | 231 | 12.5k | } | 232 | 366 | idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index; | 233 | | | 234 | 366 | bool changed = true; | 235 | 1.09k | while (changed) { | 236 | 732 | changed = false; | 237 | 25.1k | for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) { | 238 | 24.4k | const std::vector<BB*>& predecessors = *predecessor_func(*b); | 239 | | // Find the first processed/reachable predecessor that is reachable | 240 | | // in the forward traversal. | 241 | 24.4k | auto res = std::find_if(std::begin(predecessors), std::end(predecessors), | 242 | 24.4k | [&idoms, undefined_dom](BB* pred) { | 243 | 24.4k | return idoms.count(pred) && | 244 | 24.4k | idoms[pred].dominator != undefined_dom; | 245 | 24.4k | }); | 246 | 24.4k | if (res == end(predecessors)) continue; | 247 | 24.4k | const BB* idom = *res; | 248 | 24.4k | size_t idom_idx = idoms[idom].postorder_index; | 249 | | | 250 | | // all other predecessors | 251 | 34.4k | for (const auto* p : predecessors) { | 252 | 34.4k | if (idom == p) continue; | 253 | | // Only consider nodes reachable in the forward traversal. | 254 | | // Otherwise the intersection doesn't make sense and will never | 255 | | // terminate. | 256 | 9.98k | if (!idoms.count(p)) continue; | 257 | 9.98k | if (idoms[p].dominator != undefined_dom) { | 258 | 9.20k | size_t finger1 = idoms[p].postorder_index; | 259 | 9.20k | size_t finger2 = idom_idx; | 260 | 18.6k | while (finger1 != finger2) { | 261 | 28.5k | while (finger1 < finger2) { | 262 | 19.1k | finger1 = idoms[postorder[finger1]].dominator; | 263 | 19.1k | } | 264 | 9.86k | while (finger2 < finger1) { | 265 | 440 | finger2 = idoms[postorder[finger2]].dominator; | 266 | 440 | } | 267 | 9.42k | } | 268 | 9.20k | idom_idx = finger1; | 269 | 9.20k | } | 270 | 9.98k | } | 271 | 24.4k | if (idoms[*b].dominator != idom_idx) { | 272 | 12.2k | idoms[*b].dominator = idom_idx; | 273 | 12.2k | changed = true; | 274 | 12.2k | } | 275 | 24.4k | } | 276 | 732 | } | 277 | | | 278 | 366 | std::vector<std::pair<bb_ptr, bb_ptr>> out; | 279 | 12.5k | for (auto idom : idoms) { | 280 | | // At this point if there is no dominator for the node, just make it | 281 | | // reflexive. | 282 | 12.5k | auto dominator = std::get<1>(idom).dominator; | 283 | 12.5k | if (dominator == undefined_dom) { | 284 | 0 | dominator = std::get<1>(idom).postorder_index; | 285 | 0 | } | 286 | | // NOTE: performing a const cast for convenient usage with | 287 | | // UpdateImmediateDominators | 288 | 12.5k | out.push_back({const_cast<BB*>(std::get<0>(idom)), | 289 | 12.5k | const_cast<BB*>(postorder[dominator])}); | 290 | 12.5k | } | 291 | | | 292 | | // Sort by postorder index to generate a deterministic ordering of edges. | 293 | 366 | std::sort( | 294 | 366 | out.begin(), out.end(), | 295 | 366 | [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs, | 296 | 366 | const std::pair<bb_ptr, bb_ptr>& rhs) { | 297 | 366 | assert(lhs.first); | 298 | 366 | assert(lhs.second); | 299 | 366 | assert(rhs.first); | 300 | 366 | assert(rhs.second); | 301 | 366 | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, | 302 | 366 | idoms[lhs.second].postorder_index); | 303 | 366 | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, | 304 | 366 | idoms[rhs.second].postorder_index); | 305 | 366 | return lhs_indices < rhs_indices; | 306 | 366 | }); | 307 | 366 | return out; | 308 | 366 | } |
|
309 | | |
310 | | template <class BB> |
311 | | std::vector<BB*> CFA<BB>::TraversalRoots(const std::vector<BB*>& blocks, |
312 | | get_blocks_func succ_func, |
313 | 592 | get_blocks_func pred_func) { |
314 | | // The set of nodes which have been visited from any of the roots so far. |
315 | 592 | std::unordered_set<const BB*> visited; |
316 | | |
317 | 4.78k | auto mark_visited = [&visited](const BB* b) { visited.insert(b); }; |
318 | 4.78k | auto ignore_block = [](const BB*) {}; |
319 | 12.7k | auto no_terminal_blocks = [](const BB*) { return false; }; |
320 | | |
321 | 592 | auto traverse_from_root = [&mark_visited, &succ_func, &ignore_block, |
322 | 608 | &no_terminal_blocks](const BB* entry) { |
323 | 608 | DepthFirstTraversal(entry, succ_func, mark_visited, ignore_block, |
324 | 608 | no_terminal_blocks); |
325 | 608 | }; |
326 | | |
327 | 592 | std::vector<BB*> result; |
328 | | |
329 | | // First collect nodes without predecessors. |
330 | 4.76k | for (auto block : blocks) { |
331 | 4.76k | if (pred_func(block)->empty()) { |
332 | 608 | assert(visited.count(block) == 0 && "Malformed graph!"); |
333 | 608 | result.push_back(block); |
334 | 608 | traverse_from_root(block); |
335 | 608 | } |
336 | 4.76k | } |
337 | | |
338 | | // Now collect other stranded nodes. These must be in unreachable cycles. |
339 | 4.76k | for (auto block : blocks) { |
340 | 4.76k | if (visited.count(block) == 0) { |
341 | 0 | result.push_back(block); |
342 | 0 | traverse_from_root(block); |
343 | 0 | } |
344 | 4.76k | } |
345 | | |
346 | 592 | return result; |
347 | 592 | } |
348 | | |
349 | | template <class BB> |
350 | | void CFA<BB>::ComputeAugmentedCFG( |
351 | | std::vector<BB*>& ordered_blocks, BB* pseudo_entry_block, |
352 | | BB* pseudo_exit_block, |
353 | | std::unordered_map<const BB*, std::vector<BB*>>* augmented_successors_map, |
354 | | std::unordered_map<const BB*, std::vector<BB*>>* augmented_predecessors_map, |
355 | 296 | get_blocks_func succ_func, get_blocks_func pred_func) { |
356 | | // Compute the successors of the pseudo-entry block, and |
357 | | // the predecessors of the pseudo exit block. |
358 | 296 | auto sources = TraversalRoots(ordered_blocks, succ_func, pred_func); |
359 | | |
360 | | // For the predecessor traversals, reverse the order of blocks. This |
361 | | // will affect the post-dominance calculation as follows: |
362 | | // - Suppose you have blocks A and B, with A appearing before B in |
363 | | // the list of blocks. |
364 | | // - Also, A branches only to B, and B branches only to A. |
365 | | // - We want to compute A as dominating B, and B as post-dominating B. |
366 | | // By using reversed blocks for predecessor traversal roots discovery, |
367 | | // we'll add an edge from B to the pseudo-exit node, rather than from A. |
368 | | // All this is needed to correctly process the dominance/post-dominance |
369 | | // constraint when A is a loop header that points to itself as its |
370 | | // own continue target, and B is the latch block for the loop. |
371 | 296 | std::vector<BB*> reversed_blocks(ordered_blocks.rbegin(), |
372 | 296 | ordered_blocks.rend()); |
373 | 296 | auto sinks = TraversalRoots(reversed_blocks, pred_func, succ_func); |
374 | | |
375 | | // Wire up the pseudo entry block. |
376 | 296 | (*augmented_successors_map)[pseudo_entry_block] = sources; |
377 | 296 | for (auto block : sources) { |
378 | 296 | auto& augmented_preds = (*augmented_predecessors_map)[block]; |
379 | 296 | const auto preds = pred_func(block); |
380 | 296 | augmented_preds.reserve(1 + preds->size()); |
381 | 296 | augmented_preds.push_back(pseudo_entry_block); |
382 | 296 | augmented_preds.insert(augmented_preds.end(), preds->begin(), preds->end()); |
383 | 296 | } |
384 | | |
385 | | // Wire up the pseudo exit block. |
386 | 296 | (*augmented_predecessors_map)[pseudo_exit_block] = sinks; |
387 | 312 | for (auto block : sinks) { |
388 | 312 | auto& augmented_succ = (*augmented_successors_map)[block]; |
389 | 312 | const auto succ = succ_func(block); |
390 | 312 | augmented_succ.reserve(1 + succ->size()); |
391 | 312 | augmented_succ.push_back(pseudo_exit_block); |
392 | 312 | augmented_succ.insert(augmented_succ.end(), succ->begin(), succ->end()); |
393 | 312 | } |
394 | 296 | } |
395 | | |
396 | | } // namespace spvtools |
397 | | |
398 | | #endif // SOURCE_CFA_H_ |