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