• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
FindInWorkList(const std::vector<block_info> & work_list,uint32_t id)157 bool CFA<BB>::FindInWorkList(const std::vector<block_info>& work_list,
158                              uint32_t id) {
159   for (const auto& b : work_list) {
160     if (b.block->id() == id) return true;
161   }
162   return false;
163 }
164 
165 template <class BB>
DepthFirstTraversal(const BB * entry,get_blocks_func successor_func,std::function<void (cbb_ptr)> preorder,std::function<void (cbb_ptr)> postorder,std::function<bool (cbb_ptr)> terminal)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                                   std::function<bool(cbb_ptr)> terminal) {
171   DepthFirstTraversal(entry, successor_func, preorder, postorder,
172                       /* backedge = */ {}, terminal);
173 }
174 
175 template <class BB>
DepthFirstTraversal(const BB * entry,get_blocks_func successor_func,std::function<void (cbb_ptr)> preorder,std::function<void (cbb_ptr)> postorder,std::function<void (cbb_ptr,cbb_ptr)> backedge,std::function<bool (cbb_ptr)> terminal)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     std::function<bool(cbb_ptr)> terminal) {
182   assert(successor_func && "The successor function cannot be empty.");
183   assert(preorder && "The preorder function cannot be empty.");
184   assert(postorder && "The postorder function cannot be empty.");
185   assert(terminal && "The terminal function cannot be empty.");
186 
187   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   std::vector<block_info> work_list;
192   work_list.reserve(10);
193 
194   work_list.push_back({entry, std::begin(*successor_func(entry))});
195   preorder(entry);
196   processed.insert(entry->id());
197 
198   while (!work_list.empty()) {
199     block_info& top = work_list.back();
200     if (terminal(top.block) || top.iter == end(*successor_func(top.block))) {
201       postorder(top.block);
202       work_list.pop_back();
203     } else {
204       BB* child = *top.iter;
205       top.iter++;
206       if (backedge && FindInWorkList(work_list, child->id())) {
207         backedge(top.block, child);
208       }
209       if (processed.count(child->id()) == 0) {
210         preorder(child);
211         work_list.emplace_back(
212             block_info{child, std::begin(*successor_func(child))});
213         processed.insert(child->id());
214       }
215     }
216   }
217 }
218 
219 template <class BB>
CalculateDominators(const std::vector<cbb_ptr> & postorder,get_blocks_func predecessor_func)220 std::vector<std::pair<BB*, BB*>> CFA<BB>::CalculateDominators(
221     const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) {
222   struct block_detail {
223     size_t dominator;  ///< The index of blocks's dominator in post order array
224     size_t postorder_index;  ///< The index of the block in the post order array
225   };
226   const size_t undefined_dom = postorder.size();
227 
228   std::unordered_map<cbb_ptr, block_detail> idoms;
229   for (size_t i = 0; i < postorder.size(); i++) {
230     idoms[postorder[i]] = {undefined_dom, i};
231   }
232   idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index;
233 
234   bool changed = true;
235   while (changed) {
236     changed = false;
237     for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) {
238       const std::vector<BB*>& predecessors = *predecessor_func(*b);
239       // Find the first processed/reachable predecessor that is reachable
240       // in the forward traversal.
241       auto res = std::find_if(std::begin(predecessors), std::end(predecessors),
242                               [&idoms, undefined_dom](BB* pred) {
243                                 return idoms.count(pred) &&
244                                        idoms[pred].dominator != undefined_dom;
245                               });
246       if (res == end(predecessors)) continue;
247       const BB* idom = *res;
248       size_t idom_idx = idoms[idom].postorder_index;
249 
250       // all other predecessors
251       for (const auto* p : predecessors) {
252         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         if (!idoms.count(p)) continue;
257         if (idoms[p].dominator != undefined_dom) {
258           size_t finger1 = idoms[p].postorder_index;
259           size_t finger2 = idom_idx;
260           while (finger1 != finger2) {
261             while (finger1 < finger2) {
262               finger1 = idoms[postorder[finger1]].dominator;
263             }
264             while (finger2 < finger1) {
265               finger2 = idoms[postorder[finger2]].dominator;
266             }
267           }
268           idom_idx = finger1;
269         }
270       }
271       if (idoms[*b].dominator != idom_idx) {
272         idoms[*b].dominator = idom_idx;
273         changed = true;
274       }
275     }
276   }
277 
278   std::vector<std::pair<bb_ptr, bb_ptr>> out;
279   for (auto idom : idoms) {
280     // At this point if there is no dominator for the node, just make it
281     // reflexive.
282     auto dominator = std::get<1>(idom).dominator;
283     if (dominator == undefined_dom) {
284       dominator = std::get<1>(idom).postorder_index;
285     }
286     // NOTE: performing a const cast for convenient usage with
287     // UpdateImmediateDominators
288     out.push_back({const_cast<BB*>(std::get<0>(idom)),
289                    const_cast<BB*>(postorder[dominator])});
290   }
291 
292   // Sort by postorder index to generate a deterministic ordering of edges.
293   std::sort(
294       out.begin(), out.end(),
295       [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs,
296                const std::pair<bb_ptr, bb_ptr>& rhs) {
297         assert(lhs.first);
298         assert(lhs.second);
299         assert(rhs.first);
300         assert(rhs.second);
301         auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index,
302                                           idoms[lhs.second].postorder_index);
303         auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index,
304                                           idoms[rhs.second].postorder_index);
305         return lhs_indices < rhs_indices;
306       });
307   return out;
308 }
309 
310 template <class BB>
TraversalRoots(const std::vector<BB * > & blocks,get_blocks_func succ_func,get_blocks_func pred_func)311 std::vector<BB*> CFA<BB>::TraversalRoots(const std::vector<BB*>& blocks,
312                                          get_blocks_func succ_func,
313                                          get_blocks_func pred_func) {
314   // The set of nodes which have been visited from any of the roots so far.
315   std::unordered_set<const BB*> visited;
316 
317   auto mark_visited = [&visited](const BB* b) { visited.insert(b); };
318   auto ignore_block = [](const BB*) {};
319   auto no_terminal_blocks = [](const BB*) { return false; };
320 
321   auto traverse_from_root = [&mark_visited, &succ_func, &ignore_block,
322                              &no_terminal_blocks](const BB* entry) {
323     DepthFirstTraversal(entry, succ_func, mark_visited, ignore_block,
324                         no_terminal_blocks);
325   };
326 
327   std::vector<BB*> result;
328 
329   // First collect nodes without predecessors.
330   for (auto block : blocks) {
331     if (pred_func(block)->empty()) {
332       assert(visited.count(block) == 0 && "Malformed graph!");
333       result.push_back(block);
334       traverse_from_root(block);
335     }
336   }
337 
338   // Now collect other stranded nodes.  These must be in unreachable cycles.
339   for (auto block : blocks) {
340     if (visited.count(block) == 0) {
341       result.push_back(block);
342       traverse_from_root(block);
343     }
344   }
345 
346   return result;
347 }
348 
349 template <class BB>
ComputeAugmentedCFG(std::vector<BB * > & ordered_blocks,BB * pseudo_entry_block,BB * pseudo_exit_block,std::unordered_map<const BB *,std::vector<BB * >> * augmented_successors_map,std::unordered_map<const BB *,std::vector<BB * >> * augmented_predecessors_map,get_blocks_func succ_func,get_blocks_func pred_func)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     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   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   std::vector<BB*> reversed_blocks(ordered_blocks.rbegin(),
372                                    ordered_blocks.rend());
373   auto sinks = TraversalRoots(reversed_blocks, pred_func, succ_func);
374 
375   // Wire up the pseudo entry block.
376   (*augmented_successors_map)[pseudo_entry_block] = sources;
377   for (auto block : sources) {
378     auto& augmented_preds = (*augmented_predecessors_map)[block];
379     const auto preds = pred_func(block);
380     augmented_preds.reserve(1 + preds->size());
381     augmented_preds.push_back(pseudo_entry_block);
382     augmented_preds.insert(augmented_preds.end(), preds->begin(), preds->end());
383   }
384 
385   // Wire up the pseudo exit block.
386   (*augmented_predecessors_map)[pseudo_exit_block] = sinks;
387   for (auto block : sinks) {
388     auto& augmented_succ = (*augmented_successors_map)[block];
389     const auto succ = succ_func(block);
390     augmented_succ.reserve(1 + succ->size());
391     augmented_succ.push_back(pseudo_exit_block);
392     augmented_succ.insert(augmented_succ.end(), succ->begin(), succ->end());
393   }
394 }
395 
396 }  // namespace spvtools
397 
398 #endif  // SOURCE_CFA_H_
399