/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 | 25.9k | uint32_t id) { |
159 | 134k | for (const auto& b : work_list) { |
160 | 134k | if (b.block->id() == id) return true; |
161 | 134k | } |
162 | 24.9k | return false; |
163 | 25.9k | } 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 | 25.9k | uint32_t id) { | 159 | 134k | for (const auto& b : work_list) { | 160 | 134k | if (b.block->id() == id) return true; | 161 | 134k | } | 162 | 24.9k | return false; | 163 | 25.9k | } |
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 | 28.8k | std::function<bool(cbb_ptr)> terminal) { |
171 | 28.8k | DepthFirstTraversal(entry, successor_func, preorder, postorder, |
172 | 28.8k | /* backedge = */ {}, terminal); |
173 | 28.8k | } 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 | 9.76k | std::function<bool(cbb_ptr)> terminal) { | 171 | 9.76k | DepthFirstTraversal(entry, successor_func, preorder, postorder, | 172 | 9.76k | /* backedge = */ {}, terminal); | 173 | 9.76k | } |
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 | 16.0k | std::function<bool(cbb_ptr)> terminal) { | 171 | 16.0k | DepthFirstTraversal(entry, successor_func, preorder, postorder, | 172 | 16.0k | /* backedge = */ {}, terminal); | 173 | 16.0k | } |
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 | 3.03k | std::function<bool(cbb_ptr)> terminal) { | 171 | 3.03k | DepthFirstTraversal(entry, successor_func, preorder, postorder, | 172 | 3.03k | /* backedge = */ {}, terminal); | 173 | 3.03k | } |
|
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 | 30.7k | std::function<bool(cbb_ptr)> terminal) { |
182 | 30.7k | assert(successor_func && "The successor function cannot be empty."); |
183 | 30.7k | assert(preorder && "The preorder function cannot be empty."); |
184 | 30.7k | assert(postorder && "The postorder function cannot be empty."); |
185 | 30.7k | assert(terminal && "The terminal function cannot be empty."); |
186 | | |
187 | 30.7k | 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 | 30.7k | std::vector<block_info> work_list; |
192 | 30.7k | work_list.reserve(10); |
193 | | |
194 | 30.7k | work_list.push_back({entry, std::begin(*successor_func(entry))}); |
195 | 30.7k | preorder(entry); |
196 | 30.7k | processed.insert(entry->id()); |
197 | | |
198 | 1.44M | while (!work_list.empty()) { |
199 | 1.41M | block_info& top = work_list.back(); |
200 | 1.41M | if (terminal(top.block) || top.iter == end(*successor_func(top.block))) { |
201 | 565k | postorder(top.block); |
202 | 565k | work_list.pop_back(); |
203 | 845k | } else { |
204 | 845k | BB* child = *top.iter; |
205 | 845k | top.iter++; |
206 | 845k | if (backedge && FindInWorkList(work_list, child->id())) { |
207 | 1.08k | backedge(top.block, child); |
208 | 1.08k | } |
209 | 845k | if (processed.count(child->id()) == 0) { |
210 | 534k | preorder(child); |
211 | 534k | work_list.emplace_back( |
212 | 534k | block_info{child, std::begin(*successor_func(child))}); |
213 | 534k | processed.insert(child->id()); |
214 | 534k | } |
215 | 845k | } |
216 | 1.41M | } |
217 | 30.7k | } 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 | 11.6k | std::function<bool(cbb_ptr)> terminal) { | 182 | 11.6k | assert(successor_func && "The successor function cannot be empty."); | 183 | 11.6k | assert(preorder && "The preorder function cannot be empty."); | 184 | 11.6k | assert(postorder && "The postorder function cannot be empty."); | 185 | 11.6k | assert(terminal && "The terminal function cannot be empty."); | 186 | | | 187 | 11.6k | 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 | 11.6k | std::vector<block_info> work_list; | 192 | 11.6k | work_list.reserve(10); | 193 | | | 194 | 11.6k | work_list.push_back({entry, std::begin(*successor_func(entry))}); | 195 | 11.6k | preorder(entry); | 196 | 11.6k | processed.insert(entry->id()); | 197 | | | 198 | 246k | while (!work_list.empty()) { | 199 | 235k | block_info& top = work_list.back(); | 200 | 235k | if (terminal(top.block) || top.iter == end(*successor_func(top.block))) { | 201 | 95.9k | postorder(top.block); | 202 | 95.9k | work_list.pop_back(); | 203 | 139k | } else { | 204 | 139k | BB* child = *top.iter; | 205 | 139k | top.iter++; | 206 | 139k | if (backedge && FindInWorkList(work_list, child->id())) { | 207 | 1.08k | backedge(top.block, child); | 208 | 1.08k | } | 209 | 139k | if (processed.count(child->id()) == 0) { | 210 | 84.2k | preorder(child); | 211 | 84.2k | work_list.emplace_back( | 212 | 84.2k | block_info{child, std::begin(*successor_func(child))}); | 213 | 84.2k | processed.insert(child->id()); | 214 | 84.2k | } | 215 | 139k | } | 216 | 235k | } | 217 | 11.6k | } |
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 | 16.0k | std::function<bool(cbb_ptr)> terminal) { | 182 | 16.0k | assert(successor_func && "The successor function cannot be empty."); | 183 | 16.0k | assert(preorder && "The preorder function cannot be empty."); | 184 | 16.0k | assert(postorder && "The postorder function cannot be empty."); | 185 | 16.0k | assert(terminal && "The terminal function cannot be empty."); | 186 | | | 187 | 16.0k | 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 | 16.0k | std::vector<block_info> work_list; | 192 | 16.0k | work_list.reserve(10); | 193 | | | 194 | 16.0k | work_list.push_back({entry, std::begin(*successor_func(entry))}); | 195 | 16.0k | preorder(entry); | 196 | 16.0k | processed.insert(entry->id()); | 197 | | | 198 | 1.02M | while (!work_list.empty()) { | 199 | 1.00M | block_info& top = work_list.back(); | 200 | 1.00M | if (terminal(top.block) || top.iter == end(*successor_func(top.block))) { | 201 | 382k | postorder(top.block); | 202 | 382k | work_list.pop_back(); | 203 | 622k | } else { | 204 | 622k | BB* child = *top.iter; | 205 | 622k | top.iter++; | 206 | 622k | if (backedge && FindInWorkList(work_list, child->id())) { | 207 | 0 | backedge(top.block, child); | 208 | 0 | } | 209 | 622k | if (processed.count(child->id()) == 0) { | 210 | 366k | preorder(child); | 211 | 366k | work_list.emplace_back( | 212 | 366k | block_info{child, std::begin(*successor_func(child))}); | 213 | 366k | processed.insert(child->id()); | 214 | 366k | } | 215 | 622k | } | 216 | 1.00M | } | 217 | 16.0k | } |
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 | 3.03k | std::function<bool(cbb_ptr)> terminal) { | 182 | 3.03k | assert(successor_func && "The successor function cannot be empty."); | 183 | 3.03k | assert(preorder && "The preorder function cannot be empty."); | 184 | 3.03k | assert(postorder && "The postorder function cannot be empty."); | 185 | 3.03k | assert(terminal && "The terminal function cannot be empty."); | 186 | | | 187 | 3.03k | 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.03k | std::vector<block_info> work_list; | 192 | 3.03k | work_list.reserve(10); | 193 | | | 194 | 3.03k | work_list.push_back({entry, std::begin(*successor_func(entry))}); | 195 | 3.03k | preorder(entry); | 196 | 3.03k | processed.insert(entry->id()); | 197 | | | 198 | 173k | while (!work_list.empty()) { | 199 | 170k | block_info& top = work_list.back(); | 200 | 170k | if (terminal(top.block) || top.iter == end(*successor_func(top.block))) { | 201 | 86.7k | postorder(top.block); | 202 | 86.7k | work_list.pop_back(); | 203 | 86.7k | } else { | 204 | 83.7k | BB* child = *top.iter; | 205 | 83.7k | top.iter++; | 206 | 83.7k | if (backedge && FindInWorkList(work_list, child->id())) { | 207 | 0 | backedge(top.block, child); | 208 | 0 | } | 209 | 83.7k | if (processed.count(child->id()) == 0) { | 210 | 83.7k | preorder(child); | 211 | 83.7k | work_list.emplace_back( | 212 | 83.7k | block_info{child, std::begin(*successor_func(child))}); | 213 | 83.7k | processed.insert(child->id()); | 214 | 83.7k | } | 215 | 83.7k | } | 216 | 170k | } | 217 | 3.03k | } |
|
218 | | |
219 | | template <class BB> |
220 | | std::vector<std::pair<BB*, BB*>> CFA<BB>::CalculateDominators( |
221 | 8.64k | const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) { |
222 | 8.64k | struct block_detail { |
223 | 8.64k | size_t dominator; ///< The index of blocks's dominator in post order array |
224 | 8.64k | size_t postorder_index; ///< The index of the block in the post order array |
225 | 8.64k | }; |
226 | 8.64k | const size_t undefined_dom = postorder.size(); |
227 | | |
228 | 8.64k | std::unordered_map<cbb_ptr, block_detail> idoms; |
229 | 145k | for (size_t i = 0; i < postorder.size(); i++) { |
230 | 136k | idoms[postorder[i]] = {undefined_dom, i}; |
231 | 136k | } |
232 | 8.64k | idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index; |
233 | | |
234 | 8.64k | bool changed = true; |
235 | 25.9k | while (changed) { |
236 | 17.2k | changed = false; |
237 | 272k | for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) { |
238 | 255k | const std::vector<BB*>& predecessors = *predecessor_func(*b); |
239 | | // Find the first processed/reachable predecessor that is reachable |
240 | | // in the forward traversal. |
241 | 255k | auto res = std::find_if(std::begin(predecessors), std::end(predecessors), |
242 | 255k | [&idoms, undefined_dom](BB* pred) { |
243 | 255k | return idoms.count(pred) && |
244 | 255k | idoms[pred].dominator != undefined_dom; |
245 | 255k | }); 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 | 88.0k | [&idoms, undefined_dom](BB* pred) { | 243 | 88.0k | return idoms.count(pred) && | 244 | 88.0k | idoms[pred].dominator != undefined_dom; | 245 | 88.0k | }); |
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 | 167k | [&idoms, undefined_dom](BB* pred) { | 243 | 167k | return idoms.count(pred) && | 244 | 167k | idoms[pred].dominator != undefined_dom; | 245 | 167k | }); |
|
246 | 255k | if (res == end(predecessors)) continue; |
247 | 255k | const BB* idom = *res; |
248 | 255k | size_t idom_idx = idoms[idom].postorder_index; |
249 | | |
250 | | // all other predecessors |
251 | 368k | for (const auto* p : predecessors) { |
252 | 368k | 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 | 101k | if (!idoms.count(p)) continue; |
257 | 101k | if (idoms[p].dominator != undefined_dom) { |
258 | 89.5k | size_t finger1 = idoms[p].postorder_index; |
259 | 89.5k | size_t finger2 = idom_idx; |
260 | 182k | while (finger1 != finger2) { |
261 | 270k | while (finger1 < finger2) { |
262 | 177k | finger1 = idoms[postorder[finger1]].dominator; |
263 | 177k | } |
264 | 109k | while (finger2 < finger1) { |
265 | 16.3k | finger2 = idoms[postorder[finger2]].dominator; |
266 | 16.3k | } |
267 | 92.7k | } |
268 | 89.5k | idom_idx = finger1; |
269 | 89.5k | } |
270 | 101k | } |
271 | 255k | if (idoms[*b].dominator != idom_idx) { |
272 | 127k | idoms[*b].dominator = idom_idx; |
273 | 127k | changed = true; |
274 | 127k | } |
275 | 255k | } |
276 | 17.2k | } |
277 | | |
278 | 8.64k | std::vector<std::pair<bb_ptr, bb_ptr>> out; |
279 | 136k | for (auto idom : idoms) { |
280 | | // At this point if there is no dominator for the node, just make it |
281 | | // reflexive. |
282 | 136k | auto dominator = std::get<1>(idom).dominator; |
283 | 136k | 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 | 136k | out.push_back({const_cast<BB*>(std::get<0>(idom)), |
289 | 136k | const_cast<BB*>(postorder[dominator])}); |
290 | 136k | } |
291 | | |
292 | | // Sort by postorder index to generate a deterministic ordering of edges. |
293 | 8.64k | std::sort( |
294 | 8.64k | out.begin(), out.end(), |
295 | 8.64k | [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs, |
296 | 761k | const std::pair<bb_ptr, bb_ptr>& rhs) { |
297 | 761k | assert(lhs.first); |
298 | 761k | assert(lhs.second); |
299 | 761k | assert(rhs.first); |
300 | 761k | assert(rhs.second); |
301 | 761k | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, |
302 | 761k | idoms[lhs.second].postorder_index); |
303 | 761k | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, |
304 | 761k | idoms[rhs.second].postorder_index); |
305 | 761k | return lhs_indices < rhs_indices; |
306 | 761k | }); 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 | 230k | const std::pair<bb_ptr, bb_ptr>& rhs) { | 297 | 230k | assert(lhs.first); | 298 | 230k | assert(lhs.second); | 299 | 230k | assert(rhs.first); | 300 | | assert(rhs.second); | 301 | 230k | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, | 302 | 230k | idoms[lhs.second].postorder_index); | 303 | 230k | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, | 304 | 230k | idoms[rhs.second].postorder_index); | 305 | 230k | return lhs_indices < rhs_indices; | 306 | 230k | }); |
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 | 530k | const std::pair<bb_ptr, bb_ptr>& rhs) { | 297 | 530k | assert(lhs.first); | 298 | 530k | assert(lhs.second); | 299 | 530k | assert(rhs.first); | 300 | | assert(rhs.second); | 301 | 530k | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, | 302 | 530k | idoms[lhs.second].postorder_index); | 303 | 530k | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, | 304 | 530k | idoms[rhs.second].postorder_index); | 305 | 530k | return lhs_indices < rhs_indices; | 306 | 530k | }); |
|
307 | 8.64k | return out; |
308 | 8.64k | } 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 | 5.60k | const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) { | 222 | 5.60k | struct block_detail { | 223 | 5.60k | size_t dominator; ///< The index of blocks's dominator in post order array | 224 | 5.60k | size_t postorder_index; ///< The index of the block in the post order array | 225 | 5.60k | }; | 226 | 5.60k | const size_t undefined_dom = postorder.size(); | 227 | | | 228 | 5.60k | std::unordered_map<cbb_ptr, block_detail> idoms; | 229 | 55.2k | for (size_t i = 0; i < postorder.size(); i++) { | 230 | 49.6k | idoms[postorder[i]] = {undefined_dom, i}; | 231 | 49.6k | } | 232 | 5.60k | idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index; | 233 | | | 234 | 5.60k | bool changed = true; | 235 | 16.8k | while (changed) { | 236 | 11.2k | changed = false; | 237 | 99.2k | for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) { | 238 | 88.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 | 88.0k | auto res = std::find_if(std::begin(predecessors), std::end(predecessors), | 242 | 88.0k | [&idoms, undefined_dom](BB* pred) { | 243 | 88.0k | return idoms.count(pred) && | 244 | 88.0k | idoms[pred].dominator != undefined_dom; | 245 | 88.0k | }); | 246 | 88.0k | if (res == end(predecessors)) continue; | 247 | 88.0k | const BB* idom = *res; | 248 | 88.0k | size_t idom_idx = idoms[idom].postorder_index; | 249 | | | 250 | | // all other predecessors | 251 | 138k | for (const auto* p : predecessors) { | 252 | 138k | 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 | 38.4k | if (!idoms.count(p)) continue; | 257 | 38.4k | if (idoms[p].dominator != undefined_dom) { | 258 | 34.1k | size_t finger1 = idoms[p].postorder_index; | 259 | 34.1k | size_t finger2 = idom_idx; | 260 | 69.0k | while (finger1 != finger2) { | 261 | 85.3k | while (finger1 < finger2) { | 262 | 50.4k | finger1 = idoms[postorder[finger1]].dominator; | 263 | 50.4k | } | 264 | 46.3k | while (finger2 < finger1) { | 265 | 11.5k | finger2 = idoms[postorder[finger2]].dominator; | 266 | 11.5k | } | 267 | 34.8k | } | 268 | 34.1k | idom_idx = finger1; | 269 | 34.1k | } | 270 | 38.4k | } | 271 | 88.0k | if (idoms[*b].dominator != idom_idx) { | 272 | 44.0k | idoms[*b].dominator = idom_idx; | 273 | 44.0k | changed = true; | 274 | 44.0k | } | 275 | 88.0k | } | 276 | 11.2k | } | 277 | | | 278 | 5.60k | std::vector<std::pair<bb_ptr, bb_ptr>> out; | 279 | 49.6k | for (auto idom : idoms) { | 280 | | // At this point if there is no dominator for the node, just make it | 281 | | // reflexive. | 282 | 49.6k | auto dominator = std::get<1>(idom).dominator; | 283 | 49.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 | 49.6k | out.push_back({const_cast<BB*>(std::get<0>(idom)), | 289 | 49.6k | const_cast<BB*>(postorder[dominator])}); | 290 | 49.6k | } | 291 | | | 292 | | // Sort by postorder index to generate a deterministic ordering of edges. | 293 | 5.60k | std::sort( | 294 | 5.60k | out.begin(), out.end(), | 295 | 5.60k | [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs, | 296 | 5.60k | const std::pair<bb_ptr, bb_ptr>& rhs) { | 297 | 5.60k | assert(lhs.first); | 298 | 5.60k | assert(lhs.second); | 299 | 5.60k | assert(rhs.first); | 300 | 5.60k | assert(rhs.second); | 301 | 5.60k | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, | 302 | 5.60k | idoms[lhs.second].postorder_index); | 303 | 5.60k | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, | 304 | 5.60k | idoms[rhs.second].postorder_index); | 305 | 5.60k | return lhs_indices < rhs_indices; | 306 | 5.60k | }); | 307 | 5.60k | return out; | 308 | 5.60k | } |
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 | 3.03k | const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) { | 222 | 3.03k | struct block_detail { | 223 | 3.03k | size_t dominator; ///< The index of blocks's dominator in post order array | 224 | 3.03k | size_t postorder_index; ///< The index of the block in the post order array | 225 | 3.03k | }; | 226 | 3.03k | const size_t undefined_dom = postorder.size(); | 227 | | | 228 | 3.03k | std::unordered_map<cbb_ptr, block_detail> idoms; | 229 | 89.8k | for (size_t i = 0; i < postorder.size(); i++) { | 230 | 86.7k | idoms[postorder[i]] = {undefined_dom, i}; | 231 | 86.7k | } | 232 | 3.03k | idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index; | 233 | | | 234 | 3.03k | bool changed = true; | 235 | 9.11k | while (changed) { | 236 | 6.07k | changed = false; | 237 | 173k | for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) { | 238 | 167k | const std::vector<BB*>& predecessors = *predecessor_func(*b); | 239 | | // Find the first processed/reachable predecessor that is reachable | 240 | | // in the forward traversal. | 241 | 167k | auto res = std::find_if(std::begin(predecessors), std::end(predecessors), | 242 | 167k | [&idoms, undefined_dom](BB* pred) { | 243 | 167k | return idoms.count(pred) && | 244 | 167k | idoms[pred].dominator != undefined_dom; | 245 | 167k | }); | 246 | 167k | if (res == end(predecessors)) continue; | 247 | 167k | const BB* idom = *res; | 248 | 167k | size_t idom_idx = idoms[idom].postorder_index; | 249 | | | 250 | | // all other predecessors | 251 | 230k | for (const auto* p : predecessors) { | 252 | 230k | 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 | 62.7k | if (!idoms.count(p)) continue; | 257 | 62.7k | if (idoms[p].dominator != undefined_dom) { | 258 | 55.4k | size_t finger1 = idoms[p].postorder_index; | 259 | 55.4k | size_t finger2 = idom_idx; | 260 | 113k | while (finger1 != finger2) { | 261 | 185k | while (finger1 < finger2) { | 262 | 127k | finger1 = idoms[postorder[finger1]].dominator; | 263 | 127k | } | 264 | 62.6k | while (finger2 < finger1) { | 265 | 4.80k | finger2 = idoms[postorder[finger2]].dominator; | 266 | 4.80k | } | 267 | 57.8k | } | 268 | 55.4k | idom_idx = finger1; | 269 | 55.4k | } | 270 | 62.7k | } | 271 | 167k | if (idoms[*b].dominator != idom_idx) { | 272 | 83.7k | idoms[*b].dominator = idom_idx; | 273 | 83.7k | changed = true; | 274 | 83.7k | } | 275 | 167k | } | 276 | 6.07k | } | 277 | | | 278 | 3.03k | std::vector<std::pair<bb_ptr, bb_ptr>> out; | 279 | 86.7k | for (auto idom : idoms) { | 280 | | // At this point if there is no dominator for the node, just make it | 281 | | // reflexive. | 282 | 86.7k | auto dominator = std::get<1>(idom).dominator; | 283 | 86.7k | 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 | 86.7k | out.push_back({const_cast<BB*>(std::get<0>(idom)), | 289 | 86.7k | const_cast<BB*>(postorder[dominator])}); | 290 | 86.7k | } | 291 | | | 292 | | // Sort by postorder index to generate a deterministic ordering of edges. | 293 | 3.03k | std::sort( | 294 | 3.03k | out.begin(), out.end(), | 295 | 3.03k | [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs, | 296 | 3.03k | const std::pair<bb_ptr, bb_ptr>& rhs) { | 297 | 3.03k | assert(lhs.first); | 298 | 3.03k | assert(lhs.second); | 299 | 3.03k | assert(rhs.first); | 300 | 3.03k | assert(rhs.second); | 301 | 3.03k | auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index, | 302 | 3.03k | idoms[lhs.second].postorder_index); | 303 | 3.03k | auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index, | 304 | 3.03k | idoms[rhs.second].postorder_index); | 305 | 3.03k | return lhs_indices < rhs_indices; | 306 | 3.03k | }); | 307 | 3.03k | return out; | 308 | 3.03k | } |
|
309 | | |
310 | | template <class BB> |
311 | | std::vector<BB*> CFA<BB>::TraversalRoots(const std::vector<BB*>& blocks, |
312 | | get_blocks_func succ_func, |
313 | 3.98k | get_blocks_func pred_func) { |
314 | | // The set of nodes which have been visited from any of the roots so far. |
315 | 3.98k | std::unordered_set<const BB*> visited; |
316 | | |
317 | 28.5k | auto mark_visited = [&visited](const BB* b) { visited.insert(b); }; |
318 | 28.5k | auto ignore_block = [](const BB*) {}; |
319 | 72.6k | auto no_terminal_blocks = [](const BB*) { return false; }; |
320 | | |
321 | 3.98k | auto traverse_from_root = [&mark_visited, &succ_func, &ignore_block, |
322 | 4.16k | &no_terminal_blocks](const BB* entry) { |
323 | 4.16k | DepthFirstTraversal(entry, succ_func, mark_visited, ignore_block, |
324 | 4.16k | no_terminal_blocks); |
325 | 4.16k | }; |
326 | | |
327 | 3.98k | std::vector<BB*> result; |
328 | | |
329 | | // First collect nodes without predecessors. |
330 | 28.3k | for (auto block : blocks) { |
331 | 28.3k | if (pred_func(block)->empty()) { |
332 | 4.16k | assert(visited.count(block) == 0 && "Malformed graph!"); |
333 | 4.16k | result.push_back(block); |
334 | 4.16k | traverse_from_root(block); |
335 | 4.16k | } |
336 | 28.3k | } |
337 | | |
338 | | // Now collect other stranded nodes. These must be in unreachable cycles. |
339 | 28.3k | for (auto block : blocks) { |
340 | 28.3k | if (visited.count(block) == 0) { |
341 | 0 | result.push_back(block); |
342 | 0 | traverse_from_root(block); |
343 | 0 | } |
344 | 28.3k | } |
345 | | |
346 | 3.98k | return result; |
347 | 3.98k | } |
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 | 1.99k | 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 | 1.99k | 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 | 1.99k | std::vector<BB*> reversed_blocks(ordered_blocks.rbegin(), |
372 | 1.99k | ordered_blocks.rend()); |
373 | 1.99k | auto sinks = TraversalRoots(reversed_blocks, pred_func, succ_func); |
374 | | |
375 | | // Wire up the pseudo entry block. |
376 | 1.99k | (*augmented_successors_map)[pseudo_entry_block] = sources; |
377 | 1.99k | for (auto block : sources) { |
378 | 1.99k | auto& augmented_preds = (*augmented_predecessors_map)[block]; |
379 | 1.99k | const auto preds = pred_func(block); |
380 | 1.99k | augmented_preds.reserve(1 + preds->size()); |
381 | 1.99k | augmented_preds.push_back(pseudo_entry_block); |
382 | 1.99k | augmented_preds.insert(augmented_preds.end(), preds->begin(), preds->end()); |
383 | 1.99k | } |
384 | | |
385 | | // Wire up the pseudo exit block. |
386 | 1.99k | (*augmented_predecessors_map)[pseudo_exit_block] = sinks; |
387 | 2.16k | for (auto block : sinks) { |
388 | 2.16k | auto& augmented_succ = (*augmented_successors_map)[block]; |
389 | 2.16k | const auto succ = succ_func(block); |
390 | 2.16k | augmented_succ.reserve(1 + succ->size()); |
391 | 2.16k | augmented_succ.push_back(pseudo_exit_block); |
392 | 2.16k | augmented_succ.insert(augmented_succ.end(), succ->begin(), succ->end()); |
393 | 2.16k | } |
394 | 1.99k | } |
395 | | |
396 | | } // namespace spvtools |
397 | | |
398 | | #endif // SOURCE_CFA_H_ |