• 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 SPVTOOLS_CFA_H_
16 #define SPVTOOLS_CFA_H_
17 
18 #include <algorithm>
19 #include <cassert>
20 #include <functional>
21 #include <map>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <utility>
25 #include <vector>
26 
27 using std::find;
28 using std::function;
29 using std::get;
30 using std::pair;
31 using std::unordered_map;
32 using std::unordered_set;
33 using std::vector;
34 
35 namespace spvtools {
36 
37 // Control Flow Analysis of control flow graphs of basic block nodes |BB|.
38 template<class BB> class CFA {
39   using bb_ptr = BB*;
40   using cbb_ptr = const BB*;
41   using bb_iter = typename std::vector<BB*>::const_iterator;
42   using get_blocks_func =
43     std::function<const std::vector<BB*>*(const BB*)>;
44 
45   struct block_info {
46     cbb_ptr block;  ///< pointer to the block
47     bb_iter iter;   ///< Iterator to the current child node being processed
48   };
49 
50   /// Returns true if a block with @p id is found in the @p work_list vector
51   ///
52   /// @param[in] work_list  Set of blocks visited in the the depth first traversal
53   ///                       of the CFG
54   /// @param[in] id         The ID of the block being checked
55   ///
56   /// @return true if the edge work_list.back().block->id() => id is a back-edge
57   static bool FindInWorkList(
58       const std::vector<block_info>& work_list, uint32_t id);
59 
60 public:
61   /// @brief Depth first traversal starting from the \p entry BasicBlock
62   ///
63   /// This function performs a depth first traversal from the \p entry
64   /// BasicBlock and calls the pre/postorder functions when it needs to process
65   /// the node in pre order, post order. It also calls the backedge function
66   /// when a back edge is encountered.
67   ///
68   /// @param[in] entry      The root BasicBlock of a CFG
69   /// @param[in] successor_func  A function which will return a pointer to the
70   ///                            successor nodes
71   /// @param[in] preorder   A function that will be called for every block in a
72   ///                       CFG following preorder traversal semantics
73   /// @param[in] postorder  A function that will be called for every block in a
74   ///                       CFG following postorder traversal semantics
75   /// @param[in] backedge   A function that will be called when a backedge is
76   ///                       encountered during a traversal
77   /// NOTE: The @p successor_func and predecessor_func each return a pointer to a
78   /// collection such that iterators to that collection remain valid for the
79   /// lifetime of the algorithm.
80   static void DepthFirstTraversal(const BB* entry,
81     get_blocks_func successor_func,
82     std::function<void(cbb_ptr)> preorder,
83     std::function<void(cbb_ptr)> postorder,
84     std::function<void(cbb_ptr, cbb_ptr)> backedge);
85 
86   /// @brief Calculates dominator edges for a set of blocks
87   ///
88   /// Computes dominators using the algorithm of Cooper, Harvey, and Kennedy
89   /// "A Simple, Fast Dominance Algorithm", 2001.
90   ///
91   /// The algorithm assumes there is a unique root node (a node without
92   /// predecessors), and it is therefore at the end of the postorder vector.
93   ///
94   /// This function calculates the dominator edges for a set of blocks in the CFG.
95   /// Uses the dominator algorithm by Cooper et al.
96   ///
97   /// @param[in] postorder        A vector of blocks in post order traversal order
98   ///                             in a CFG
99   /// @param[in] predecessor_func Function used to get the predecessor nodes of a
100   ///                             block
101   ///
102   /// @return the dominator tree of the graph, as a vector of pairs of nodes.
103   /// The first node in the pair is a node in the graph. The second node in the
104   /// pair is its immediate dominator in the sense of Cooper et.al., where a block
105   /// without predecessors (such as the root node) is its own immediate dominator.
106   static vector<pair<BB*, BB*>> CalculateDominators(
107     const vector<cbb_ptr>& postorder, get_blocks_func predecessor_func);
108 
109   // Computes a minimal set of root nodes required to traverse, in the forward
110   // direction, the CFG represented by the given vector of blocks, and successor
111   // and predecessor functions.  When considering adding two nodes, each having
112   // predecessors, favour using the one that appears earlier on the input blocks
113   // list.
114   static std::vector<BB*> TraversalRoots(
115     const std::vector<BB*>& blocks,
116     get_blocks_func succ_func,
117     get_blocks_func pred_func);
118 
119   static void ComputeAugmentedCFG(
120     std::vector<BB*>& ordered_blocks,
121     BB* pseudo_entry_block,
122     BB* pseudo_exit_block,
123     std::unordered_map<const BB*, std::vector<BB*>>* augmented_successors_map,
124     std::unordered_map<const BB*, std::vector<BB*>>* augmented_predecessors_map,
125     get_blocks_func succ_func,
126     get_blocks_func pred_func);
127 };
128 
FindInWorkList(const vector<block_info> & work_list,uint32_t id)129 template<class BB> bool CFA<BB>::FindInWorkList(const vector<block_info>& work_list,
130                                                 uint32_t id) {
131   for (const auto b : work_list) {
132     if (b.block->id() == id) return true;
133   }
134   return false;
135 }
136 
DepthFirstTraversal(const BB * entry,get_blocks_func successor_func,function<void (cbb_ptr)> preorder,function<void (cbb_ptr)> postorder,function<void (cbb_ptr,cbb_ptr)> backedge)137 template<class BB> void CFA<BB>::DepthFirstTraversal(const BB* entry,
138   get_blocks_func successor_func,
139   function<void(cbb_ptr)> preorder,
140   function<void(cbb_ptr)> postorder,
141   function<void(cbb_ptr, cbb_ptr)> backedge) {
142   unordered_set<uint32_t> processed;
143 
144   /// NOTE: work_list is the sequence of nodes from the root node to the node
145   /// being processed in the traversal
146   vector<block_info> work_list;
147   work_list.reserve(10);
148 
149   work_list.push_back({ entry, begin(*successor_func(entry)) });
150   preorder(entry);
151   processed.insert(entry->id());
152 
153   while (!work_list.empty()) {
154     block_info& top = work_list.back();
155     if (top.iter == end(*successor_func(top.block))) {
156       postorder(top.block);
157       work_list.pop_back();
158     }
159     else {
160       BB* child = *top.iter;
161       top.iter++;
162       if (FindInWorkList(work_list, child->id())) {
163         backedge(top.block, child);
164       }
165       if (processed.count(child->id()) == 0) {
166         preorder(child);
167         work_list.emplace_back(
168           block_info{ child, begin(*successor_func(child)) });
169         processed.insert(child->id());
170       }
171     }
172   }
173 }
174 
175 template<class BB>
CalculateDominators(const vector<cbb_ptr> & postorder,get_blocks_func predecessor_func)176 vector<pair<BB*, BB*>> CFA<BB>::CalculateDominators(
177   const vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) {
178   struct block_detail {
179     size_t dominator;  ///< The index of blocks's dominator in post order array
180     size_t postorder_index;  ///< The index of the block in the post order array
181   };
182   const size_t undefined_dom = postorder.size();
183 
184   unordered_map<cbb_ptr, block_detail> idoms;
185   for (size_t i = 0; i < postorder.size(); i++) {
186     idoms[postorder[i]] = { undefined_dom, i };
187   }
188   idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index;
189 
190   bool changed = true;
191   while (changed) {
192     changed = false;
193     for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) {
194       const vector<BB*>& predecessors = *predecessor_func(*b);
195       // Find the first processed/reachable predecessor that is reachable
196       // in the forward traversal.
197       auto res = find_if(begin(predecessors), end(predecessors),
198         [&idoms, undefined_dom](BB* pred) {
199         return idoms.count(pred) &&
200           idoms[pred].dominator != undefined_dom;
201       });
202       if (res == end(predecessors)) continue;
203       const BB* idom = *res;
204       size_t idom_idx = idoms[idom].postorder_index;
205 
206       // all other predecessors
207       for (const auto* p : predecessors) {
208         if (idom == p) continue;
209         // Only consider nodes reachable in the forward traversal.
210         // Otherwise the intersection doesn't make sense and will never
211         // terminate.
212         if (!idoms.count(p)) continue;
213         if (idoms[p].dominator != undefined_dom) {
214           size_t finger1 = idoms[p].postorder_index;
215           size_t finger2 = idom_idx;
216           while (finger1 != finger2) {
217             while (finger1 < finger2) {
218               finger1 = idoms[postorder[finger1]].dominator;
219             }
220             while (finger2 < finger1) {
221               finger2 = idoms[postorder[finger2]].dominator;
222             }
223           }
224           idom_idx = finger1;
225         }
226       }
227       if (idoms[*b].dominator != idom_idx) {
228         idoms[*b].dominator = idom_idx;
229         changed = true;
230       }
231     }
232   }
233 
234   vector<pair<bb_ptr, bb_ptr>> out;
235   for (auto idom : idoms) {
236     // NOTE: performing a const cast for convenient usage with
237     // UpdateImmediateDominators
238     out.push_back({ const_cast<BB*>(get<0>(idom)),
239       const_cast<BB*>(postorder[get<1>(idom).dominator]) });
240   }
241   return out;
242 }
243 
244 template<class BB>
TraversalRoots(const std::vector<BB * > & blocks,get_blocks_func succ_func,get_blocks_func pred_func)245 std::vector<BB*> CFA<BB>::TraversalRoots(
246     const std::vector<BB*>& blocks,
247     get_blocks_func succ_func,
248     get_blocks_func pred_func) {
249   // The set of nodes which have been visited from any of the roots so far.
250   std::unordered_set<const BB*> visited;
251 
252   auto mark_visited = [&visited](const BB* b) { visited.insert(b); };
253   auto ignore_block = [](const BB*) {};
254   auto ignore_blocks = [](const BB*, const BB*) {};
255 
256 
257   auto traverse_from_root = [&mark_visited, &succ_func, &ignore_block,
258     &ignore_blocks](const BB* entry) {
259     DepthFirstTraversal(
260       entry, succ_func, mark_visited, ignore_block, ignore_blocks);
261   };
262 
263   std::vector<BB*> result;
264 
265   // First collect nodes without predecessors.
266   for (auto block : blocks) {
267     if (pred_func(block)->empty()) {
268       assert(visited.count(block) == 0 && "Malformed graph!");
269       result.push_back(block);
270       traverse_from_root(block);
271     }
272   }
273 
274   // Now collect other stranded nodes.  These must be in unreachable cycles.
275   for (auto block : blocks) {
276     if (visited.count(block) == 0) {
277       result.push_back(block);
278       traverse_from_root(block);
279     }
280   }
281 
282   return result;
283 }
284 
285 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)286 void CFA<BB>::ComputeAugmentedCFG(
287     std::vector<BB*>& ordered_blocks,
288     BB* pseudo_entry_block, BB* pseudo_exit_block,
289     std::unordered_map<const BB*, std::vector<BB*>>* augmented_successors_map,
290     std::unordered_map<const BB*, std::vector<BB*>>* augmented_predecessors_map,
291     get_blocks_func succ_func,
292     get_blocks_func pred_func) {
293 
294   // Compute the successors of the pseudo-entry block, and
295   // the predecessors of the pseudo exit block.
296   auto sources = TraversalRoots(ordered_blocks, succ_func, pred_func);
297 
298   // For the predecessor traversals, reverse the order of blocks.  This
299   // will affect the post-dominance calculation as follows:
300   //  - Suppose you have blocks A and B, with A appearing before B in
301   //    the list of blocks.
302   //  - Also, A branches only to B, and B branches only to A.
303   //  - We want to compute A as dominating B, and B as post-dominating B.
304   // By using reversed blocks for predecessor traversal roots discovery,
305   // we'll add an edge from B to the pseudo-exit node, rather than from A.
306   // All this is needed to correctly process the dominance/post-dominance
307   // constraint when A is a loop header that points to itself as its
308   // own continue target, and B is the latch block for the loop.
309   std::vector<BB*> reversed_blocks(ordered_blocks.rbegin(),
310     ordered_blocks.rend());
311   auto sinks = TraversalRoots(reversed_blocks, pred_func, succ_func);
312 
313   // Wire up the pseudo entry block.
314   (*augmented_successors_map)[pseudo_entry_block] = sources;
315   for (auto block : sources) {
316     auto& augmented_preds = (*augmented_predecessors_map)[block];
317     const auto preds = pred_func(block);
318     augmented_preds.reserve(1 + preds->size());
319     augmented_preds.push_back(pseudo_entry_block);
320     augmented_preds.insert(augmented_preds.end(), preds->begin(), preds->end());
321   }
322 
323   // Wire up the pseudo exit block.
324   (*augmented_predecessors_map)[pseudo_exit_block] = sinks;
325   for (auto block : sinks) {
326     auto& augmented_succ = (*augmented_successors_map)[block];
327     const auto succ = succ_func(block);
328     augmented_succ.reserve(1 + succ->size());
329     augmented_succ.push_back(pseudo_exit_block);
330     augmented_succ.insert(augmented_succ.end(), succ->begin(), succ->end());
331   }
332 };
333 
334 } // namespace spvtools
335 
336 #endif  // SPVTOOLS_CFA_H_
337