Coverage Report

Created: 2025-07-18 06:38

/src/shaderc/third_party/spirv-tools/source/cfa.h
Line
Count
Source (jump to first uncovered line)
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
18.0k
                             uint32_t id) {
159
81.1k
  for (const auto& b : work_list) {
160
81.1k
    if (b.block->id() == id) return true;
161
81.1k
  }
162
17.1k
  return false;
163
18.0k
}
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
18.0k
                             uint32_t id) {
159
81.1k
  for (const auto& b : work_list) {
160
81.1k
    if (b.block->id() == id) return true;
161
81.1k
  }
162
17.1k
  return false;
163
18.0k
}
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
33.5k
                                  std::function<bool(cbb_ptr)> terminal) {
171
33.5k
  DepthFirstTraversal(entry, successor_func, preorder, postorder,
172
33.5k
                      /* backedge = */ {}, terminal);
173
33.5k
}
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
10.0k
                                  std::function<bool(cbb_ptr)> terminal) {
171
10.0k
  DepthFirstTraversal(entry, successor_func, preorder, postorder,
172
10.0k
                      /* backedge = */ {}, terminal);
173
10.0k
}
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
20.1k
                                  std::function<bool(cbb_ptr)> terminal) {
171
20.1k
  DepthFirstTraversal(entry, successor_func, preorder, postorder,
172
20.1k
                      /* backedge = */ {}, terminal);
173
20.1k
}
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.42k
                                  std::function<bool(cbb_ptr)> terminal) {
171
3.42k
  DepthFirstTraversal(entry, successor_func, preorder, postorder,
172
3.42k
                      /* backedge = */ {}, terminal);
173
3.42k
}
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
35.4k
    std::function<bool(cbb_ptr)> terminal) {
182
35.4k
  assert(successor_func && "The successor function cannot be empty.");
183
35.4k
  assert(preorder && "The preorder function cannot be empty.");
184
35.4k
  assert(postorder && "The postorder function cannot be empty.");
185
35.4k
  assert(terminal && "The terminal function cannot be empty.");
186
187
35.4k
  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
35.4k
  std::vector<block_info> work_list;
192
35.4k
  work_list.reserve(10);
193
194
35.4k
  work_list.push_back({entry, std::begin(*successor_func(entry))});
195
35.4k
  preorder(entry);
196
35.4k
  processed.insert(entry->id());
197
198
1.03M
  while (!work_list.empty()) {
199
996k
    block_info& top = work_list.back();
200
996k
    if (terminal(top.block) || top.iter == end(*successor_func(top.block))) {
201
412k
      postorder(top.block);
202
412k
      work_list.pop_back();
203
584k
    } else {
204
584k
      BB* child = *top.iter;
205
584k
      top.iter++;
206
584k
      if (backedge && FindInWorkList(work_list, child->id())) {
207
866
        backedge(top.block, child);
208
866
      }
209
584k
      if (processed.count(child->id()) == 0) {
210
377k
        preorder(child);
211
377k
        work_list.emplace_back(
212
377k
            block_info{child, std::begin(*successor_func(child))});
213
377k
        processed.insert(child->id());
214
377k
      }
215
584k
    }
216
996k
  }
217
35.4k
}
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.9k
    std::function<bool(cbb_ptr)> terminal) {
182
11.9k
  assert(successor_func && "The successor function cannot be empty.");
183
11.9k
  assert(preorder && "The preorder function cannot be empty.");
184
11.9k
  assert(postorder && "The postorder function cannot be empty.");
185
11.9k
  assert(terminal && "The terminal function cannot be empty.");
186
187
11.9k
  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.9k
  std::vector<block_info> work_list;
192
11.9k
  work_list.reserve(10);
193
194
11.9k
  work_list.push_back({entry, std::begin(*successor_func(entry))});
195
11.9k
  preorder(entry);
196
11.9k
  processed.insert(entry->id());
197
198
176k
  while (!work_list.empty()) {
199
164k
    block_info& top = work_list.back();
200
164k
    if (terminal(top.block) || top.iter == end(*successor_func(top.block))) {
201
71.1k
      postorder(top.block);
202
71.1k
      work_list.pop_back();
203
93.2k
    } else {
204
93.2k
      BB* child = *top.iter;
205
93.2k
      top.iter++;
206
93.2k
      if (backedge && FindInWorkList(work_list, child->id())) {
207
866
        backedge(top.block, child);
208
866
      }
209
93.2k
      if (processed.count(child->id()) == 0) {
210
59.1k
        preorder(child);
211
59.1k
        work_list.emplace_back(
212
59.1k
            block_info{child, std::begin(*successor_func(child))});
213
59.1k
        processed.insert(child->id());
214
59.1k
      }
215
93.2k
    }
216
164k
  }
217
11.9k
}
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
20.1k
    std::function<bool(cbb_ptr)> terminal) {
182
20.1k
  assert(successor_func && "The successor function cannot be empty.");
183
20.1k
  assert(preorder && "The preorder function cannot be empty.");
184
20.1k
  assert(postorder && "The postorder function cannot be empty.");
185
20.1k
  assert(terminal && "The terminal function cannot be empty.");
186
187
20.1k
  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
20.1k
  std::vector<block_info> work_list;
192
20.1k
  work_list.reserve(10);
193
194
20.1k
  work_list.push_back({entry, std::begin(*successor_func(entry))});
195
20.1k
  preorder(entry);
196
20.1k
  processed.insert(entry->id());
197
198
728k
  while (!work_list.empty()) {
199
708k
    block_info& top = work_list.back();
200
708k
    if (terminal(top.block) || top.iter == end(*successor_func(top.block))) {
201
277k
      postorder(top.block);
202
277k
      work_list.pop_back();
203
430k
    } else {
204
430k
      BB* child = *top.iter;
205
430k
      top.iter++;
206
430k
      if (backedge && FindInWorkList(work_list, child->id())) {
207
0
        backedge(top.block, child);
208
0
      }
209
430k
      if (processed.count(child->id()) == 0) {
210
257k
        preorder(child);
211
257k
        work_list.emplace_back(
212
257k
            block_info{child, std::begin(*successor_func(child))});
213
257k
        processed.insert(child->id());
214
257k
      }
215
430k
    }
216
708k
  }
217
20.1k
}
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.42k
    std::function<bool(cbb_ptr)> terminal) {
182
3.42k
  assert(successor_func && "The successor function cannot be empty.");
183
3.42k
  assert(preorder && "The preorder function cannot be empty.");
184
3.42k
  assert(postorder && "The postorder function cannot be empty.");
185
3.42k
  assert(terminal && "The terminal function cannot be empty.");
186
187
3.42k
  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.42k
  std::vector<block_info> work_list;
192
3.42k
  work_list.reserve(10);
193
194
3.42k
  work_list.push_back({entry, std::begin(*successor_func(entry))});
195
3.42k
  preorder(entry);
196
3.42k
  processed.insert(entry->id());
197
198
127k
  while (!work_list.empty()) {
199
124k
    block_info& top = work_list.back();
200
124k
    if (terminal(top.block) || top.iter == end(*successor_func(top.block))) {
201
63.7k
      postorder(top.block);
202
63.7k
      work_list.pop_back();
203
63.7k
    } else {
204
60.3k
      BB* child = *top.iter;
205
60.3k
      top.iter++;
206
60.3k
      if (backedge && FindInWorkList(work_list, child->id())) {
207
0
        backedge(top.block, child);
208
0
      }
209
60.3k
      if (processed.count(child->id()) == 0) {
210
60.3k
        preorder(child);
211
60.3k
        work_list.emplace_back(
212
60.3k
            block_info{child, std::begin(*successor_func(child))});
213
60.3k
        processed.insert(child->id());
214
60.3k
      }
215
60.3k
    }
216
124k
  }
217
3.42k
}
218
219
template <class BB>
220
std::vector<std::pair<BB*, BB*>> CFA<BB>::CalculateDominators(
221
9.22k
    const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) {
222
9.22k
  struct block_detail {
223
9.22k
    size_t dominator;  ///< The index of blocks's dominator in post order array
224
9.22k
    size_t postorder_index;  ///< The index of the block in the post order array
225
9.22k
  };
226
9.22k
  const size_t undefined_dom = postorder.size();
227
228
9.22k
  std::unordered_map<cbb_ptr, block_detail> idoms;
229
110k
  for (size_t i = 0; i < postorder.size(); i++) {
230
101k
    idoms[postorder[i]] = {undefined_dom, i};
231
101k
  }
232
9.22k
  idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index;
233
234
9.22k
  bool changed = true;
235
27.6k
  while (changed) {
236
18.4k
    changed = false;
237
202k
    for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) {
238
183k
      const std::vector<BB*>& predecessors = *predecessor_func(*b);
239
      // Find the first processed/reachable predecessor that is reachable
240
      // in the forward traversal.
241
183k
      auto res = std::find_if(std::begin(predecessors), std::end(predecessors),
242
183k
                              [&idoms, undefined_dom](BB* pred) {
243
183k
                                return idoms.count(pred) &&
244
183k
                                       idoms[pred].dominator != undefined_dom;
245
183k
                              });
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*) const
Line
Count
Source
242
63.0k
                              [&idoms, undefined_dom](BB* pred) {
243
63.0k
                                return idoms.count(pred) &&
244
63.0k
                                       idoms[pred].dominator != undefined_dom;
245
63.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*) const
Line
Count
Source
242
120k
                              [&idoms, undefined_dom](BB* pred) {
243
120k
                                return idoms.count(pred) &&
244
120k
                                       idoms[pred].dominator != undefined_dom;
245
120k
                              });
246
183k
      if (res == end(predecessors)) continue;
247
183k
      const BB* idom = *res;
248
183k
      size_t idom_idx = idoms[idom].postorder_index;
249
250
      // all other predecessors
251
257k
      for (const auto* p : predecessors) {
252
257k
        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
67.3k
        if (!idoms.count(p)) continue;
257
67.3k
        if (idoms[p].dominator != undefined_dom) {
258
58.1k
          size_t finger1 = idoms[p].postorder_index;
259
58.1k
          size_t finger2 = idom_idx;
260
118k
          while (finger1 != finger2) {
261
180k
            while (finger1 < finger2) {
262
119k
              finger1 = idoms[postorder[finger1]].dominator;
263
119k
            }
264
73.9k
            while (finger2 < finger1) {
265
13.1k
              finger2 = idoms[postorder[finger2]].dominator;
266
13.1k
            }
267
60.7k
          }
268
58.1k
          idom_idx = finger1;
269
58.1k
        }
270
67.3k
      }
271
183k
      if (idoms[*b].dominator != idom_idx) {
272
91.8k
        idoms[*b].dominator = idom_idx;
273
91.8k
        changed = true;
274
91.8k
      }
275
183k
    }
276
18.4k
  }
277
278
9.22k
  std::vector<std::pair<bb_ptr, bb_ptr>> out;
279
101k
  for (auto idom : idoms) {
280
    // At this point if there is no dominator for the node, just make it
281
    // reflexive.
282
101k
    auto dominator = std::get<1>(idom).dominator;
283
101k
    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
101k
    out.push_back({const_cast<BB*>(std::get<0>(idom)),
289
101k
                   const_cast<BB*>(postorder[dominator])});
290
101k
  }
291
292
  // Sort by postorder index to generate a deterministic ordering of edges.
293
9.22k
  std::sort(
294
9.22k
      out.begin(), out.end(),
295
9.22k
      [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs,
296
527k
               const std::pair<bb_ptr, bb_ptr>& rhs) {
297
527k
        assert(lhs.first);
298
527k
        assert(lhs.second);
299
527k
        assert(rhs.first);
300
527k
        assert(rhs.second);
301
527k
        auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index,
302
527k
                                          idoms[lhs.second].postorder_index);
303
527k
        auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index,
304
527k
                                          idoms[rhs.second].postorder_index);
305
527k
        return lhs_indices < rhs_indices;
306
527k
      });
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&) const
Line
Count
Source
296
149k
               const std::pair<bb_ptr, bb_ptr>& rhs) {
297
149k
        assert(lhs.first);
298
149k
        assert(lhs.second);
299
149k
        assert(rhs.first);
300
149k
        assert(rhs.second);
301
149k
        auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index,
302
149k
                                          idoms[lhs.second].postorder_index);
303
149k
        auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index,
304
149k
                                          idoms[rhs.second].postorder_index);
305
149k
        return lhs_indices < rhs_indices;
306
149k
      });
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&) const
Line
Count
Source
296
378k
               const std::pair<bb_ptr, bb_ptr>& rhs) {
297
378k
        assert(lhs.first);
298
378k
        assert(lhs.second);
299
378k
        assert(rhs.first);
300
378k
        assert(rhs.second);
301
378k
        auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index,
302
378k
                                          idoms[lhs.second].postorder_index);
303
378k
        auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index,
304
378k
                                          idoms[rhs.second].postorder_index);
305
378k
        return lhs_indices < rhs_indices;
306
378k
      });
307
9.22k
  return out;
308
9.22k
}
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.79k
    const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) {
222
5.79k
  struct block_detail {
223
5.79k
    size_t dominator;  ///< The index of blocks's dominator in post order array
224
5.79k
    size_t postorder_index;  ///< The index of the block in the post order array
225
5.79k
  };
226
5.79k
  const size_t undefined_dom = postorder.size();
227
228
5.79k
  std::unordered_map<cbb_ptr, block_detail> idoms;
229
43.1k
  for (size_t i = 0; i < postorder.size(); i++) {
230
37.3k
    idoms[postorder[i]] = {undefined_dom, i};
231
37.3k
  }
232
5.79k
  idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index;
233
234
5.79k
  bool changed = true;
235
17.3k
  while (changed) {
236
11.5k
    changed = false;
237
74.6k
    for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) {
238
63.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
63.0k
      auto res = std::find_if(std::begin(predecessors), std::end(predecessors),
242
63.0k
                              [&idoms, undefined_dom](BB* pred) {
243
63.0k
                                return idoms.count(pred) &&
244
63.0k
                                       idoms[pred].dominator != undefined_dom;
245
63.0k
                              });
246
63.0k
      if (res == end(predecessors)) continue;
247
63.0k
      const BB* idom = *res;
248
63.0k
      size_t idom_idx = idoms[idom].postorder_index;
249
250
      // all other predecessors
251
94.1k
      for (const auto* p : predecessors) {
252
94.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
24.9k
        if (!idoms.count(p)) continue;
257
24.9k
        if (idoms[p].dominator != undefined_dom) {
258
21.4k
          size_t finger1 = idoms[p].postorder_index;
259
21.4k
          size_t finger2 = idom_idx;
260
43.5k
          while (finger1 != finger2) {
261
52.7k
            while (finger1 < finger2) {
262
30.6k
              finger1 = idoms[postorder[finger1]].dominator;
263
30.6k
            }
264
31.2k
            while (finger2 < finger1) {
265
9.14k
              finger2 = idoms[postorder[finger2]].dominator;
266
9.14k
            }
267
22.0k
          }
268
21.4k
          idom_idx = finger1;
269
21.4k
        }
270
24.9k
      }
271
63.0k
      if (idoms[*b].dominator != idom_idx) {
272
31.5k
        idoms[*b].dominator = idom_idx;
273
31.5k
        changed = true;
274
31.5k
      }
275
63.0k
    }
276
11.5k
  }
277
278
5.79k
  std::vector<std::pair<bb_ptr, bb_ptr>> out;
279
37.3k
  for (auto idom : idoms) {
280
    // At this point if there is no dominator for the node, just make it
281
    // reflexive.
282
37.3k
    auto dominator = std::get<1>(idom).dominator;
283
37.3k
    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
37.3k
    out.push_back({const_cast<BB*>(std::get<0>(idom)),
289
37.3k
                   const_cast<BB*>(postorder[dominator])});
290
37.3k
  }
291
292
  // Sort by postorder index to generate a deterministic ordering of edges.
293
5.79k
  std::sort(
294
5.79k
      out.begin(), out.end(),
295
5.79k
      [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs,
296
5.79k
               const std::pair<bb_ptr, bb_ptr>& rhs) {
297
5.79k
        assert(lhs.first);
298
5.79k
        assert(lhs.second);
299
5.79k
        assert(rhs.first);
300
5.79k
        assert(rhs.second);
301
5.79k
        auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index,
302
5.79k
                                          idoms[lhs.second].postorder_index);
303
5.79k
        auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index,
304
5.79k
                                          idoms[rhs.second].postorder_index);
305
5.79k
        return lhs_indices < rhs_indices;
306
5.79k
      });
307
5.79k
  return out;
308
5.79k
}
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.42k
    const std::vector<cbb_ptr>& postorder, get_blocks_func predecessor_func) {
222
3.42k
  struct block_detail {
223
3.42k
    size_t dominator;  ///< The index of blocks's dominator in post order array
224
3.42k
    size_t postorder_index;  ///< The index of the block in the post order array
225
3.42k
  };
226
3.42k
  const size_t undefined_dom = postorder.size();
227
228
3.42k
  std::unordered_map<cbb_ptr, block_detail> idoms;
229
67.1k
  for (size_t i = 0; i < postorder.size(); i++) {
230
63.7k
    idoms[postorder[i]] = {undefined_dom, i};
231
63.7k
  }
232
3.42k
  idoms[postorder.back()].dominator = idoms[postorder.back()].postorder_index;
233
234
3.42k
  bool changed = true;
235
10.2k
  while (changed) {
236
6.85k
    changed = false;
237
127k
    for (auto b = postorder.rbegin() + 1; b != postorder.rend(); ++b) {
238
120k
      const std::vector<BB*>& predecessors = *predecessor_func(*b);
239
      // Find the first processed/reachable predecessor that is reachable
240
      // in the forward traversal.
241
120k
      auto res = std::find_if(std::begin(predecessors), std::end(predecessors),
242
120k
                              [&idoms, undefined_dom](BB* pred) {
243
120k
                                return idoms.count(pred) &&
244
120k
                                       idoms[pred].dominator != undefined_dom;
245
120k
                              });
246
120k
      if (res == end(predecessors)) continue;
247
120k
      const BB* idom = *res;
248
120k
      size_t idom_idx = idoms[idom].postorder_index;
249
250
      // all other predecessors
251
162k
      for (const auto* p : predecessors) {
252
162k
        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
42.3k
        if (!idoms.count(p)) continue;
257
42.3k
        if (idoms[p].dominator != undefined_dom) {
258
36.7k
          size_t finger1 = idoms[p].postorder_index;
259
36.7k
          size_t finger2 = idom_idx;
260
75.4k
          while (finger1 != finger2) {
261
127k
            while (finger1 < finger2) {
262
89.2k
              finger1 = idoms[postorder[finger1]].dominator;
263
89.2k
            }
264
42.6k
            while (finger2 < finger1) {
265
3.98k
              finger2 = idoms[postorder[finger2]].dominator;
266
3.98k
            }
267
38.7k
          }
268
36.7k
          idom_idx = finger1;
269
36.7k
        }
270
42.3k
      }
271
120k
      if (idoms[*b].dominator != idom_idx) {
272
60.3k
        idoms[*b].dominator = idom_idx;
273
60.3k
        changed = true;
274
60.3k
      }
275
120k
    }
276
6.85k
  }
277
278
3.42k
  std::vector<std::pair<bb_ptr, bb_ptr>> out;
279
63.7k
  for (auto idom : idoms) {
280
    // At this point if there is no dominator for the node, just make it
281
    // reflexive.
282
63.7k
    auto dominator = std::get<1>(idom).dominator;
283
63.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
63.7k
    out.push_back({const_cast<BB*>(std::get<0>(idom)),
289
63.7k
                   const_cast<BB*>(postorder[dominator])});
290
63.7k
  }
291
292
  // Sort by postorder index to generate a deterministic ordering of edges.
293
3.42k
  std::sort(
294
3.42k
      out.begin(), out.end(),
295
3.42k
      [&idoms](const std::pair<bb_ptr, bb_ptr>& lhs,
296
3.42k
               const std::pair<bb_ptr, bb_ptr>& rhs) {
297
3.42k
        assert(lhs.first);
298
3.42k
        assert(lhs.second);
299
3.42k
        assert(rhs.first);
300
3.42k
        assert(rhs.second);
301
3.42k
        auto lhs_indices = std::make_pair(idoms[lhs.first].postorder_index,
302
3.42k
                                          idoms[lhs.second].postorder_index);
303
3.42k
        auto rhs_indices = std::make_pair(idoms[rhs.first].postorder_index,
304
3.42k
                                          idoms[rhs.second].postorder_index);
305
3.42k
        return lhs_indices < rhs_indices;
306
3.42k
      });
307
3.42k
  return out;
308
3.42k
}
309
310
template <class BB>
311
std::vector<BB*> CFA<BB>::TraversalRoots(const std::vector<BB*>& blocks,
312
                                         get_blocks_func succ_func,
313
4.06k
                                         get_blocks_func pred_func) {
314
  // The set of nodes which have been visited from any of the roots so far.
315
4.06k
  std::unordered_set<const BB*> visited;
316
317
20.0k
  auto mark_visited = [&visited](const BB* b) { visited.insert(b); };
318
20.0k
  auto ignore_block = [](const BB*) {};
319
48.1k
  auto no_terminal_blocks = [](const BB*) { return false; };
320
321
4.06k
  auto traverse_from_root = [&mark_visited, &succ_func, &ignore_block,
322
4.20k
                             &no_terminal_blocks](const BB* entry) {
323
4.20k
    DepthFirstTraversal(entry, succ_func, mark_visited, ignore_block,
324
4.20k
                        no_terminal_blocks);
325
4.20k
  };
326
327
4.06k
  std::vector<BB*> result;
328
329
  // First collect nodes without predecessors.
330
19.9k
  for (auto block : blocks) {
331
19.9k
    if (pred_func(block)->empty()) {
332
4.20k
      assert(visited.count(block) == 0 && "Malformed graph!");
333
4.20k
      result.push_back(block);
334
4.20k
      traverse_from_root(block);
335
4.20k
    }
336
19.9k
  }
337
338
  // Now collect other stranded nodes.  These must be in unreachable cycles.
339
19.9k
  for (auto block : blocks) {
340
19.9k
    if (visited.count(block) == 0) {
341
0
      result.push_back(block);
342
0
      traverse_from_root(block);
343
0
    }
344
19.9k
  }
345
346
4.06k
  return result;
347
4.06k
}
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
2.03k
    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
2.03k
  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
2.03k
  std::vector<BB*> reversed_blocks(ordered_blocks.rbegin(),
372
2.03k
                                   ordered_blocks.rend());
373
2.03k
  auto sinks = TraversalRoots(reversed_blocks, pred_func, succ_func);
374
375
  // Wire up the pseudo entry block.
376
2.03k
  (*augmented_successors_map)[pseudo_entry_block] = sources;
377
2.03k
  for (auto block : sources) {
378
2.03k
    auto& augmented_preds = (*augmented_predecessors_map)[block];
379
2.03k
    const auto preds = pred_func(block);
380
2.03k
    augmented_preds.reserve(1 + preds->size());
381
2.03k
    augmented_preds.push_back(pseudo_entry_block);
382
2.03k
    augmented_preds.insert(augmented_preds.end(), preds->begin(), preds->end());
383
2.03k
  }
384
385
  // Wire up the pseudo exit block.
386
2.03k
  (*augmented_predecessors_map)[pseudo_exit_block] = sinks;
387
2.17k
  for (auto block : sinks) {
388
2.17k
    auto& augmented_succ = (*augmented_successors_map)[block];
389
2.17k
    const auto succ = succ_func(block);
390
2.17k
    augmented_succ.reserve(1 + succ->size());
391
2.17k
    augmented_succ.push_back(pseudo_exit_block);
392
2.17k
    augmented_succ.insert(augmented_succ.end(), succ->begin(), succ->end());
393
2.17k
  }
394
2.03k
}
395
396
}  // namespace spvtools
397
398
#endif  // SOURCE_CFA_H_