[Alignment][NFC] Convert StoreInst to MaybeAlign
[llvm-complete.git] / lib / Support / DAGDeltaAlgorithm.cpp
blob4675fe3a9401037836d33965c51dd3a3530d8a97
1 //===--- DAGDeltaAlgorithm.cpp - A DAG Minimization Algorithm --*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //===----------------------------------------------------------------------===//
7 //
8 // The algorithm we use attempts to exploit the dependency information by
9 // minimizing top-down. We start by constructing an initial root set R, and
10 // then iteratively:
12 // 1. Minimize the set R using the test predicate:
13 // P'(S) = P(S union pred*(S))
15 // 2. Extend R to R' = R union pred(R).
17 // until a fixed point is reached.
19 // The idea is that we want to quickly prune entire portions of the graph, so we
20 // try to find high-level nodes that can be eliminated with all of their
21 // dependents.
23 // FIXME: The current algorithm doesn't actually provide a strong guarantee
24 // about the minimality of the result. The problem is that after adding nodes to
25 // the required set, we no longer consider them for elimination. For strictly
26 // well formed predicates, this doesn't happen, but it commonly occurs in
27 // practice when there are unmodelled dependencies. I believe we can resolve
28 // this by allowing the required set to be minimized as well, but need more test
29 // cases first.
31 //===----------------------------------------------------------------------===//
33 #include "llvm/ADT/DAGDeltaAlgorithm.h"
34 #include "llvm/ADT/DeltaAlgorithm.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/Format.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include <algorithm>
39 #include <cassert>
40 #include <iterator>
41 #include <map>
42 using namespace llvm;
44 #define DEBUG_TYPE "dag-delta"
46 namespace {
48 class DAGDeltaAlgorithmImpl {
49 friend class DeltaActiveSetHelper;
51 public:
52 typedef DAGDeltaAlgorithm::change_ty change_ty;
53 typedef DAGDeltaAlgorithm::changeset_ty changeset_ty;
54 typedef DAGDeltaAlgorithm::changesetlist_ty changesetlist_ty;
55 typedef DAGDeltaAlgorithm::edge_ty edge_ty;
57 private:
58 typedef std::vector<change_ty>::iterator pred_iterator_ty;
59 typedef std::vector<change_ty>::iterator succ_iterator_ty;
60 typedef std::set<change_ty>::iterator pred_closure_iterator_ty;
61 typedef std::set<change_ty>::iterator succ_closure_iterator_ty;
63 DAGDeltaAlgorithm &DDA;
65 std::vector<change_ty> Roots;
67 /// Cache of failed test results. Successful test results are never cached
68 /// since we always reduce following a success. We maintain an independent
69 /// cache from that used by the individual delta passes because we may get
70 /// hits across multiple individual delta invocations.
71 mutable std::set<changeset_ty> FailedTestsCache;
73 // FIXME: Gross.
74 std::map<change_ty, std::vector<change_ty> > Predecessors;
75 std::map<change_ty, std::vector<change_ty> > Successors;
77 std::map<change_ty, std::set<change_ty> > PredClosure;
78 std::map<change_ty, std::set<change_ty> > SuccClosure;
80 private:
81 pred_iterator_ty pred_begin(change_ty Node) {
82 assert(Predecessors.count(Node) && "Invalid node!");
83 return Predecessors[Node].begin();
85 pred_iterator_ty pred_end(change_ty Node) {
86 assert(Predecessors.count(Node) && "Invalid node!");
87 return Predecessors[Node].end();
90 pred_closure_iterator_ty pred_closure_begin(change_ty Node) {
91 assert(PredClosure.count(Node) && "Invalid node!");
92 return PredClosure[Node].begin();
94 pred_closure_iterator_ty pred_closure_end(change_ty Node) {
95 assert(PredClosure.count(Node) && "Invalid node!");
96 return PredClosure[Node].end();
99 succ_iterator_ty succ_begin(change_ty Node) {
100 assert(Successors.count(Node) && "Invalid node!");
101 return Successors[Node].begin();
103 succ_iterator_ty succ_end(change_ty Node) {
104 assert(Successors.count(Node) && "Invalid node!");
105 return Successors[Node].end();
108 succ_closure_iterator_ty succ_closure_begin(change_ty Node) {
109 assert(SuccClosure.count(Node) && "Invalid node!");
110 return SuccClosure[Node].begin();
112 succ_closure_iterator_ty succ_closure_end(change_ty Node) {
113 assert(SuccClosure.count(Node) && "Invalid node!");
114 return SuccClosure[Node].end();
117 void UpdatedSearchState(const changeset_ty &Changes,
118 const changesetlist_ty &Sets,
119 const changeset_ty &Required) {
120 DDA.UpdatedSearchState(Changes, Sets, Required);
123 /// ExecuteOneTest - Execute a single test predicate on the change set \p S.
124 bool ExecuteOneTest(const changeset_ty &S) {
125 // Check dependencies invariant.
126 LLVM_DEBUG({
127 for (changeset_ty::const_iterator it = S.begin(), ie = S.end(); it != ie;
128 ++it)
129 for (succ_iterator_ty it2 = succ_begin(*it), ie2 = succ_end(*it);
130 it2 != ie2; ++it2)
131 assert(S.count(*it2) && "Attempt to run invalid changeset!");
134 return DDA.ExecuteOneTest(S);
137 public:
138 DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm &DDA, const changeset_ty &Changes,
139 const std::vector<edge_ty> &Dependencies);
141 changeset_ty Run();
143 /// GetTestResult - Get the test result for the active set \p Changes with
144 /// \p Required changes from the cache, executing the test if necessary.
146 /// \param Changes - The set of active changes being minimized, which should
147 /// have their pred closure included in the test.
148 /// \param Required - The set of changes which have previously been
149 /// established to be required.
150 /// \return - The test result.
151 bool GetTestResult(const changeset_ty &Changes, const changeset_ty &Required);
154 /// Helper object for minimizing an active set of changes.
155 class DeltaActiveSetHelper : public DeltaAlgorithm {
156 DAGDeltaAlgorithmImpl &DDAI;
158 const changeset_ty &Required;
160 protected:
161 /// UpdatedSearchState - Callback used when the search state changes.
162 void UpdatedSearchState(const changeset_ty &Changes,
163 const changesetlist_ty &Sets) override {
164 DDAI.UpdatedSearchState(Changes, Sets, Required);
167 bool ExecuteOneTest(const changeset_ty &S) override {
168 return DDAI.GetTestResult(S, Required);
171 public:
172 DeltaActiveSetHelper(DAGDeltaAlgorithmImpl &DDAI,
173 const changeset_ty &Required)
174 : DDAI(DDAI), Required(Required) {}
179 DAGDeltaAlgorithmImpl::DAGDeltaAlgorithmImpl(
180 DAGDeltaAlgorithm &DDA, const changeset_ty &Changes,
181 const std::vector<edge_ty> &Dependencies)
182 : DDA(DDA) {
183 for (changeset_ty::const_iterator it = Changes.begin(),
184 ie = Changes.end(); it != ie; ++it) {
185 Predecessors.insert(std::make_pair(*it, std::vector<change_ty>()));
186 Successors.insert(std::make_pair(*it, std::vector<change_ty>()));
188 for (std::vector<edge_ty>::const_iterator it = Dependencies.begin(),
189 ie = Dependencies.end(); it != ie; ++it) {
190 Predecessors[it->second].push_back(it->first);
191 Successors[it->first].push_back(it->second);
194 // Compute the roots.
195 for (changeset_ty::const_iterator it = Changes.begin(),
196 ie = Changes.end(); it != ie; ++it)
197 if (succ_begin(*it) == succ_end(*it))
198 Roots.push_back(*it);
200 // Pre-compute the closure of the successor relation.
201 std::vector<change_ty> Worklist(Roots.begin(), Roots.end());
202 while (!Worklist.empty()) {
203 change_ty Change = Worklist.back();
204 Worklist.pop_back();
206 std::set<change_ty> &ChangeSuccs = SuccClosure[Change];
207 for (pred_iterator_ty it = pred_begin(Change),
208 ie = pred_end(Change); it != ie; ++it) {
209 SuccClosure[*it].insert(Change);
210 SuccClosure[*it].insert(ChangeSuccs.begin(), ChangeSuccs.end());
211 Worklist.push_back(*it);
215 // Invert to form the predecessor closure map.
216 for (changeset_ty::const_iterator it = Changes.begin(),
217 ie = Changes.end(); it != ie; ++it)
218 PredClosure.insert(std::make_pair(*it, std::set<change_ty>()));
219 for (changeset_ty::const_iterator it = Changes.begin(),
220 ie = Changes.end(); it != ie; ++it)
221 for (succ_closure_iterator_ty it2 = succ_closure_begin(*it),
222 ie2 = succ_closure_end(*it); it2 != ie2; ++it2)
223 PredClosure[*it2].insert(*it);
225 // Dump useful debug info.
226 LLVM_DEBUG({
227 llvm::errs() << "-- DAGDeltaAlgorithmImpl --\n";
228 llvm::errs() << "Changes: [";
229 for (changeset_ty::const_iterator it = Changes.begin(), ie = Changes.end();
230 it != ie; ++it) {
231 if (it != Changes.begin())
232 llvm::errs() << ", ";
233 llvm::errs() << *it;
235 if (succ_begin(*it) != succ_end(*it)) {
236 llvm::errs() << "(";
237 for (succ_iterator_ty it2 = succ_begin(*it), ie2 = succ_end(*it);
238 it2 != ie2; ++it2) {
239 if (it2 != succ_begin(*it))
240 llvm::errs() << ", ";
241 llvm::errs() << "->" << *it2;
243 llvm::errs() << ")";
246 llvm::errs() << "]\n";
248 llvm::errs() << "Roots: [";
249 for (std::vector<change_ty>::const_iterator it = Roots.begin(),
250 ie = Roots.end();
251 it != ie; ++it) {
252 if (it != Roots.begin())
253 llvm::errs() << ", ";
254 llvm::errs() << *it;
256 llvm::errs() << "]\n";
258 llvm::errs() << "Predecessor Closure:\n";
259 for (changeset_ty::const_iterator it = Changes.begin(), ie = Changes.end();
260 it != ie; ++it) {
261 llvm::errs() << format(" %-4d: [", *it);
262 for (pred_closure_iterator_ty it2 = pred_closure_begin(*it),
263 ie2 = pred_closure_end(*it);
264 it2 != ie2; ++it2) {
265 if (it2 != pred_closure_begin(*it))
266 llvm::errs() << ", ";
267 llvm::errs() << *it2;
269 llvm::errs() << "]\n";
272 llvm::errs() << "Successor Closure:\n";
273 for (changeset_ty::const_iterator it = Changes.begin(), ie = Changes.end();
274 it != ie; ++it) {
275 llvm::errs() << format(" %-4d: [", *it);
276 for (succ_closure_iterator_ty it2 = succ_closure_begin(*it),
277 ie2 = succ_closure_end(*it);
278 it2 != ie2; ++it2) {
279 if (it2 != succ_closure_begin(*it))
280 llvm::errs() << ", ";
281 llvm::errs() << *it2;
283 llvm::errs() << "]\n";
286 llvm::errs() << "\n\n";
290 bool DAGDeltaAlgorithmImpl::GetTestResult(const changeset_ty &Changes,
291 const changeset_ty &Required) {
292 changeset_ty Extended(Required);
293 Extended.insert(Changes.begin(), Changes.end());
294 for (changeset_ty::const_iterator it = Changes.begin(),
295 ie = Changes.end(); it != ie; ++it)
296 Extended.insert(pred_closure_begin(*it), pred_closure_end(*it));
298 if (FailedTestsCache.count(Extended))
299 return false;
301 bool Result = ExecuteOneTest(Extended);
302 if (!Result)
303 FailedTestsCache.insert(Extended);
305 return Result;
308 DAGDeltaAlgorithm::changeset_ty
309 DAGDeltaAlgorithmImpl::Run() {
310 // The current set of changes we are minimizing, starting at the roots.
311 changeset_ty CurrentSet(Roots.begin(), Roots.end());
313 // The set of required changes.
314 changeset_ty Required;
316 // Iterate until the active set of changes is empty. Convergence is guaranteed
317 // assuming input was a DAG.
319 // Invariant: CurrentSet intersect Required == {}
320 // Invariant: Required == (Required union succ*(Required))
321 while (!CurrentSet.empty()) {
322 LLVM_DEBUG({
323 llvm::errs() << "DAG_DD - " << CurrentSet.size() << " active changes, "
324 << Required.size() << " required changes\n";
327 // Minimize the current set of changes.
328 DeltaActiveSetHelper Helper(*this, Required);
329 changeset_ty CurrentMinSet = Helper.Run(CurrentSet);
331 // Update the set of required changes. Since
332 // CurrentMinSet subset CurrentSet
333 // and after the last iteration,
334 // succ(CurrentSet) subset Required
335 // then
336 // succ(CurrentMinSet) subset Required
337 // and our invariant on Required is maintained.
338 Required.insert(CurrentMinSet.begin(), CurrentMinSet.end());
340 // Replace the current set with the predecssors of the minimized set of
341 // active changes.
342 CurrentSet.clear();
343 for (changeset_ty::const_iterator it = CurrentMinSet.begin(),
344 ie = CurrentMinSet.end(); it != ie; ++it)
345 CurrentSet.insert(pred_begin(*it), pred_end(*it));
347 // FIXME: We could enforce CurrentSet intersect Required == {} here if we
348 // wanted to protect against cyclic graphs.
351 return Required;
354 void DAGDeltaAlgorithm::anchor() {
357 DAGDeltaAlgorithm::changeset_ty
358 DAGDeltaAlgorithm::Run(const changeset_ty &Changes,
359 const std::vector<edge_ty> &Dependencies) {
360 return DAGDeltaAlgorithmImpl(*this, Changes, Dependencies).Run();