10b57cec5SDimitry Andric //===--- DAGDeltaAlgorithm.cpp - A DAG Minimization Algorithm --*- C++ -*--===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
70b57cec5SDimitry Andric //
80b57cec5SDimitry Andric // The algorithm we use attempts to exploit the dependency information by
90b57cec5SDimitry Andric // minimizing top-down. We start by constructing an initial root set R, and
100b57cec5SDimitry Andric // then iteratively:
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //   1. Minimize the set R using the test predicate:
130b57cec5SDimitry Andric //       P'(S) = P(S union pred*(S))
140b57cec5SDimitry Andric //
150b57cec5SDimitry Andric //   2. Extend R to R' = R union pred(R).
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric // until a fixed point is reached.
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric // The idea is that we want to quickly prune entire portions of the graph, so we
200b57cec5SDimitry Andric // try to find high-level nodes that can be eliminated with all of their
210b57cec5SDimitry Andric // dependents.
220b57cec5SDimitry Andric //
230b57cec5SDimitry Andric // FIXME: The current algorithm doesn't actually provide a strong guarantee
240b57cec5SDimitry Andric // about the minimality of the result. The problem is that after adding nodes to
250b57cec5SDimitry Andric // the required set, we no longer consider them for elimination. For strictly
260b57cec5SDimitry Andric // well formed predicates, this doesn't happen, but it commonly occurs in
270b57cec5SDimitry Andric // practice when there are unmodelled dependencies. I believe we can resolve
280b57cec5SDimitry Andric // this by allowing the required set to be minimized as well, but need more test
290b57cec5SDimitry Andric // cases first.
300b57cec5SDimitry Andric //
310b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric #include "llvm/ADT/DAGDeltaAlgorithm.h"
340b57cec5SDimitry Andric #include "llvm/ADT/DeltaAlgorithm.h"
350b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
360b57cec5SDimitry Andric #include "llvm/Support/Format.h"
370b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
380b57cec5SDimitry Andric #include <algorithm>
390b57cec5SDimitry Andric #include <cassert>
400b57cec5SDimitry Andric #include <iterator>
410b57cec5SDimitry Andric #include <map>
420b57cec5SDimitry Andric using namespace llvm;
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric #define DEBUG_TYPE "dag-delta"
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric namespace {
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric class DAGDeltaAlgorithmImpl {
490b57cec5SDimitry Andric   friend class DeltaActiveSetHelper;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric public:
520b57cec5SDimitry Andric   typedef DAGDeltaAlgorithm::change_ty change_ty;
530b57cec5SDimitry Andric   typedef DAGDeltaAlgorithm::changeset_ty changeset_ty;
540b57cec5SDimitry Andric   typedef DAGDeltaAlgorithm::changesetlist_ty changesetlist_ty;
550b57cec5SDimitry Andric   typedef DAGDeltaAlgorithm::edge_ty edge_ty;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric private:
580b57cec5SDimitry Andric   typedef std::vector<change_ty>::iterator pred_iterator_ty;
590b57cec5SDimitry Andric   typedef std::vector<change_ty>::iterator succ_iterator_ty;
600b57cec5SDimitry Andric   typedef std::set<change_ty>::iterator pred_closure_iterator_ty;
610b57cec5SDimitry Andric   typedef std::set<change_ty>::iterator succ_closure_iterator_ty;
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   DAGDeltaAlgorithm &DDA;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   std::vector<change_ty> Roots;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   /// Cache of failed test results. Successful test results are never cached
680b57cec5SDimitry Andric   /// since we always reduce following a success. We maintain an independent
690b57cec5SDimitry Andric   /// cache from that used by the individual delta passes because we may get
700b57cec5SDimitry Andric   /// hits across multiple individual delta invocations.
710b57cec5SDimitry Andric   mutable std::set<changeset_ty> FailedTestsCache;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   // FIXME: Gross.
740b57cec5SDimitry Andric   std::map<change_ty, std::vector<change_ty> > Predecessors;
750b57cec5SDimitry Andric   std::map<change_ty, std::vector<change_ty> > Successors;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   std::map<change_ty, std::set<change_ty> > PredClosure;
780b57cec5SDimitry Andric   std::map<change_ty, std::set<change_ty> > SuccClosure;
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric private:
pred_begin(change_ty Node)810b57cec5SDimitry Andric   pred_iterator_ty pred_begin(change_ty Node) {
820b57cec5SDimitry Andric     assert(Predecessors.count(Node) && "Invalid node!");
830b57cec5SDimitry Andric     return Predecessors[Node].begin();
840b57cec5SDimitry Andric   }
pred_end(change_ty Node)850b57cec5SDimitry Andric   pred_iterator_ty pred_end(change_ty Node) {
860b57cec5SDimitry Andric     assert(Predecessors.count(Node) && "Invalid node!");
870b57cec5SDimitry Andric     return Predecessors[Node].end();
880b57cec5SDimitry Andric   }
890b57cec5SDimitry Andric 
pred_closure_begin(change_ty Node)900b57cec5SDimitry Andric   pred_closure_iterator_ty pred_closure_begin(change_ty Node) {
910b57cec5SDimitry Andric     assert(PredClosure.count(Node) && "Invalid node!");
920b57cec5SDimitry Andric     return PredClosure[Node].begin();
930b57cec5SDimitry Andric   }
pred_closure_end(change_ty Node)940b57cec5SDimitry Andric   pred_closure_iterator_ty pred_closure_end(change_ty Node) {
950b57cec5SDimitry Andric     assert(PredClosure.count(Node) && "Invalid node!");
960b57cec5SDimitry Andric     return PredClosure[Node].end();
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric 
succ_begin(change_ty Node)990b57cec5SDimitry Andric   succ_iterator_ty succ_begin(change_ty Node) {
1000b57cec5SDimitry Andric     assert(Successors.count(Node) && "Invalid node!");
1010b57cec5SDimitry Andric     return Successors[Node].begin();
1020b57cec5SDimitry Andric   }
succ_end(change_ty Node)1030b57cec5SDimitry Andric   succ_iterator_ty succ_end(change_ty Node) {
1040b57cec5SDimitry Andric     assert(Successors.count(Node) && "Invalid node!");
1050b57cec5SDimitry Andric     return Successors[Node].end();
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric 
succ_closure_begin(change_ty Node)1080b57cec5SDimitry Andric   succ_closure_iterator_ty succ_closure_begin(change_ty Node) {
1090b57cec5SDimitry Andric     assert(SuccClosure.count(Node) && "Invalid node!");
1100b57cec5SDimitry Andric     return SuccClosure[Node].begin();
1110b57cec5SDimitry Andric   }
succ_closure_end(change_ty Node)1120b57cec5SDimitry Andric   succ_closure_iterator_ty succ_closure_end(change_ty Node) {
1130b57cec5SDimitry Andric     assert(SuccClosure.count(Node) && "Invalid node!");
1140b57cec5SDimitry Andric     return SuccClosure[Node].end();
1150b57cec5SDimitry Andric   }
1160b57cec5SDimitry Andric 
UpdatedSearchState(const changeset_ty & Changes,const changesetlist_ty & Sets,const changeset_ty & Required)1170b57cec5SDimitry Andric   void UpdatedSearchState(const changeset_ty &Changes,
1180b57cec5SDimitry Andric                           const changesetlist_ty &Sets,
1190b57cec5SDimitry Andric                           const changeset_ty &Required) {
1200b57cec5SDimitry Andric     DDA.UpdatedSearchState(Changes, Sets, Required);
1210b57cec5SDimitry Andric   }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   /// ExecuteOneTest - Execute a single test predicate on the change set \p S.
ExecuteOneTest(const changeset_ty & S)1240b57cec5SDimitry Andric   bool ExecuteOneTest(const changeset_ty &S) {
1250b57cec5SDimitry Andric     // Check dependencies invariant.
1260b57cec5SDimitry Andric     LLVM_DEBUG({
1270b57cec5SDimitry Andric       for (changeset_ty::const_iterator it = S.begin(), ie = S.end(); it != ie;
1280b57cec5SDimitry Andric            ++it)
1290b57cec5SDimitry Andric         for (succ_iterator_ty it2 = succ_begin(*it), ie2 = succ_end(*it);
1300b57cec5SDimitry Andric              it2 != ie2; ++it2)
1310b57cec5SDimitry Andric           assert(S.count(*it2) && "Attempt to run invalid changeset!");
1320b57cec5SDimitry Andric     });
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric     return DDA.ExecuteOneTest(S);
1350b57cec5SDimitry Andric   }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric public:
1380b57cec5SDimitry Andric   DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm &DDA, const changeset_ty &Changes,
1390b57cec5SDimitry Andric                         const std::vector<edge_ty> &Dependencies);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   changeset_ty Run();
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   /// GetTestResult - Get the test result for the active set \p Changes with
1440b57cec5SDimitry Andric   /// \p Required changes from the cache, executing the test if necessary.
1450b57cec5SDimitry Andric   ///
1460b57cec5SDimitry Andric   /// \param Changes - The set of active changes being minimized, which should
1470b57cec5SDimitry Andric   /// have their pred closure included in the test.
1480b57cec5SDimitry Andric   /// \param Required - The set of changes which have previously been
1490b57cec5SDimitry Andric   /// established to be required.
1500b57cec5SDimitry Andric   /// \return - The test result.
1510b57cec5SDimitry Andric   bool GetTestResult(const changeset_ty &Changes, const changeset_ty &Required);
1520b57cec5SDimitry Andric };
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric /// Helper object for minimizing an active set of changes.
1550b57cec5SDimitry Andric class DeltaActiveSetHelper : public DeltaAlgorithm {
1560b57cec5SDimitry Andric   DAGDeltaAlgorithmImpl &DDAI;
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   const changeset_ty &Required;
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric protected:
1610b57cec5SDimitry Andric   /// UpdatedSearchState - Callback used when the search state changes.
UpdatedSearchState(const changeset_ty & Changes,const changesetlist_ty & Sets)1620b57cec5SDimitry Andric   void UpdatedSearchState(const changeset_ty &Changes,
1630b57cec5SDimitry Andric                                   const changesetlist_ty &Sets) override {
1640b57cec5SDimitry Andric     DDAI.UpdatedSearchState(Changes, Sets, Required);
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric 
ExecuteOneTest(const changeset_ty & S)1670b57cec5SDimitry Andric   bool ExecuteOneTest(const changeset_ty &S) override {
1680b57cec5SDimitry Andric     return DDAI.GetTestResult(S, Required);
1690b57cec5SDimitry Andric   }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric public:
DeltaActiveSetHelper(DAGDeltaAlgorithmImpl & DDAI,const changeset_ty & Required)1720b57cec5SDimitry Andric   DeltaActiveSetHelper(DAGDeltaAlgorithmImpl &DDAI,
1730b57cec5SDimitry Andric                        const changeset_ty &Required)
1740b57cec5SDimitry Andric       : DDAI(DDAI), Required(Required) {}
1750b57cec5SDimitry Andric };
1760b57cec5SDimitry Andric 
177*5f7ddb14SDimitry Andric } // namespace
1780b57cec5SDimitry Andric 
DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm & DDA,const changeset_ty & Changes,const std::vector<edge_ty> & Dependencies)1790b57cec5SDimitry Andric DAGDeltaAlgorithmImpl::DAGDeltaAlgorithmImpl(
1800b57cec5SDimitry Andric     DAGDeltaAlgorithm &DDA, const changeset_ty &Changes,
1810b57cec5SDimitry Andric     const std::vector<edge_ty> &Dependencies)
1820b57cec5SDimitry Andric     : DDA(DDA) {
1830b57cec5SDimitry Andric   for (changeset_ty::const_iterator it = Changes.begin(),
1840b57cec5SDimitry Andric          ie = Changes.end(); it != ie; ++it) {
1850b57cec5SDimitry Andric     Predecessors.insert(std::make_pair(*it, std::vector<change_ty>()));
1860b57cec5SDimitry Andric     Successors.insert(std::make_pair(*it, std::vector<change_ty>()));
1870b57cec5SDimitry Andric   }
1880b57cec5SDimitry Andric   for (std::vector<edge_ty>::const_iterator it = Dependencies.begin(),
1890b57cec5SDimitry Andric          ie = Dependencies.end(); it != ie; ++it) {
1900b57cec5SDimitry Andric     Predecessors[it->second].push_back(it->first);
1910b57cec5SDimitry Andric     Successors[it->first].push_back(it->second);
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   // Compute the roots.
1950b57cec5SDimitry Andric   for (changeset_ty::const_iterator it = Changes.begin(),
1960b57cec5SDimitry Andric          ie = Changes.end(); it != ie; ++it)
1970b57cec5SDimitry Andric     if (succ_begin(*it) == succ_end(*it))
1980b57cec5SDimitry Andric       Roots.push_back(*it);
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   // Pre-compute the closure of the successor relation.
2010b57cec5SDimitry Andric   std::vector<change_ty> Worklist(Roots.begin(), Roots.end());
2020b57cec5SDimitry Andric   while (!Worklist.empty()) {
2030b57cec5SDimitry Andric     change_ty Change = Worklist.back();
2040b57cec5SDimitry Andric     Worklist.pop_back();
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric     std::set<change_ty> &ChangeSuccs = SuccClosure[Change];
2070b57cec5SDimitry Andric     for (pred_iterator_ty it = pred_begin(Change),
2080b57cec5SDimitry Andric            ie = pred_end(Change); it != ie; ++it) {
2090b57cec5SDimitry Andric       SuccClosure[*it].insert(Change);
2100b57cec5SDimitry Andric       SuccClosure[*it].insert(ChangeSuccs.begin(), ChangeSuccs.end());
2110b57cec5SDimitry Andric       Worklist.push_back(*it);
2120b57cec5SDimitry Andric     }
2130b57cec5SDimitry Andric   }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   // Invert to form the predecessor closure map.
2160b57cec5SDimitry Andric   for (changeset_ty::const_iterator it = Changes.begin(),
2170b57cec5SDimitry Andric          ie = Changes.end(); it != ie; ++it)
2180b57cec5SDimitry Andric     PredClosure.insert(std::make_pair(*it, std::set<change_ty>()));
2190b57cec5SDimitry Andric   for (changeset_ty::const_iterator it = Changes.begin(),
2200b57cec5SDimitry Andric          ie = Changes.end(); it != ie; ++it)
2210b57cec5SDimitry Andric     for (succ_closure_iterator_ty it2 = succ_closure_begin(*it),
2220b57cec5SDimitry Andric            ie2 = succ_closure_end(*it); it2 != ie2; ++it2)
2230b57cec5SDimitry Andric       PredClosure[*it2].insert(*it);
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric   // Dump useful debug info.
2260b57cec5SDimitry Andric   LLVM_DEBUG({
2270b57cec5SDimitry Andric     llvm::errs() << "-- DAGDeltaAlgorithmImpl --\n";
2280b57cec5SDimitry Andric     llvm::errs() << "Changes: [";
2290b57cec5SDimitry Andric     for (changeset_ty::const_iterator it = Changes.begin(), ie = Changes.end();
2300b57cec5SDimitry Andric          it != ie; ++it) {
2310b57cec5SDimitry Andric       if (it != Changes.begin())
2320b57cec5SDimitry Andric         llvm::errs() << ", ";
2330b57cec5SDimitry Andric       llvm::errs() << *it;
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric       if (succ_begin(*it) != succ_end(*it)) {
2360b57cec5SDimitry Andric         llvm::errs() << "(";
2370b57cec5SDimitry Andric         for (succ_iterator_ty it2 = succ_begin(*it), ie2 = succ_end(*it);
2380b57cec5SDimitry Andric              it2 != ie2; ++it2) {
2390b57cec5SDimitry Andric           if (it2 != succ_begin(*it))
2400b57cec5SDimitry Andric             llvm::errs() << ", ";
2410b57cec5SDimitry Andric           llvm::errs() << "->" << *it2;
2420b57cec5SDimitry Andric         }
2430b57cec5SDimitry Andric         llvm::errs() << ")";
2440b57cec5SDimitry Andric       }
2450b57cec5SDimitry Andric     }
2460b57cec5SDimitry Andric     llvm::errs() << "]\n";
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric     llvm::errs() << "Roots: [";
2490b57cec5SDimitry Andric     for (std::vector<change_ty>::const_iterator it = Roots.begin(),
2500b57cec5SDimitry Andric                                                 ie = Roots.end();
2510b57cec5SDimitry Andric          it != ie; ++it) {
2520b57cec5SDimitry Andric       if (it != Roots.begin())
2530b57cec5SDimitry Andric         llvm::errs() << ", ";
2540b57cec5SDimitry Andric       llvm::errs() << *it;
2550b57cec5SDimitry Andric     }
2560b57cec5SDimitry Andric     llvm::errs() << "]\n";
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric     llvm::errs() << "Predecessor Closure:\n";
2590b57cec5SDimitry Andric     for (changeset_ty::const_iterator it = Changes.begin(), ie = Changes.end();
2600b57cec5SDimitry Andric          it != ie; ++it) {
2610b57cec5SDimitry Andric       llvm::errs() << format("  %-4d: [", *it);
2620b57cec5SDimitry Andric       for (pred_closure_iterator_ty it2 = pred_closure_begin(*it),
2630b57cec5SDimitry Andric                                     ie2 = pred_closure_end(*it);
2640b57cec5SDimitry Andric            it2 != ie2; ++it2) {
2650b57cec5SDimitry Andric         if (it2 != pred_closure_begin(*it))
2660b57cec5SDimitry Andric           llvm::errs() << ", ";
2670b57cec5SDimitry Andric         llvm::errs() << *it2;
2680b57cec5SDimitry Andric       }
2690b57cec5SDimitry Andric       llvm::errs() << "]\n";
2700b57cec5SDimitry Andric     }
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric     llvm::errs() << "Successor Closure:\n";
2730b57cec5SDimitry Andric     for (changeset_ty::const_iterator it = Changes.begin(), ie = Changes.end();
2740b57cec5SDimitry Andric          it != ie; ++it) {
2750b57cec5SDimitry Andric       llvm::errs() << format("  %-4d: [", *it);
2760b57cec5SDimitry Andric       for (succ_closure_iterator_ty it2 = succ_closure_begin(*it),
2770b57cec5SDimitry Andric                                     ie2 = succ_closure_end(*it);
2780b57cec5SDimitry Andric            it2 != ie2; ++it2) {
2790b57cec5SDimitry Andric         if (it2 != succ_closure_begin(*it))
2800b57cec5SDimitry Andric           llvm::errs() << ", ";
2810b57cec5SDimitry Andric         llvm::errs() << *it2;
2820b57cec5SDimitry Andric       }
2830b57cec5SDimitry Andric       llvm::errs() << "]\n";
2840b57cec5SDimitry Andric     }
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric     llvm::errs() << "\n\n";
2870b57cec5SDimitry Andric   });
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric 
GetTestResult(const changeset_ty & Changes,const changeset_ty & Required)2900b57cec5SDimitry Andric bool DAGDeltaAlgorithmImpl::GetTestResult(const changeset_ty &Changes,
2910b57cec5SDimitry Andric                                           const changeset_ty &Required) {
2920b57cec5SDimitry Andric   changeset_ty Extended(Required);
2930b57cec5SDimitry Andric   Extended.insert(Changes.begin(), Changes.end());
2940b57cec5SDimitry Andric   for (changeset_ty::const_iterator it = Changes.begin(),
2950b57cec5SDimitry Andric          ie = Changes.end(); it != ie; ++it)
2960b57cec5SDimitry Andric     Extended.insert(pred_closure_begin(*it), pred_closure_end(*it));
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   if (FailedTestsCache.count(Extended))
2990b57cec5SDimitry Andric     return false;
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   bool Result = ExecuteOneTest(Extended);
3020b57cec5SDimitry Andric   if (!Result)
3030b57cec5SDimitry Andric     FailedTestsCache.insert(Extended);
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   return Result;
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric DAGDeltaAlgorithm::changeset_ty
Run()3090b57cec5SDimitry Andric DAGDeltaAlgorithmImpl::Run() {
3100b57cec5SDimitry Andric   // The current set of changes we are minimizing, starting at the roots.
3110b57cec5SDimitry Andric   changeset_ty CurrentSet(Roots.begin(), Roots.end());
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   // The set of required changes.
3140b57cec5SDimitry Andric   changeset_ty Required;
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   // Iterate until the active set of changes is empty. Convergence is guaranteed
3170b57cec5SDimitry Andric   // assuming input was a DAG.
3180b57cec5SDimitry Andric   //
3190b57cec5SDimitry Andric   // Invariant:  CurrentSet intersect Required == {}
3200b57cec5SDimitry Andric   // Invariant:  Required == (Required union succ*(Required))
3210b57cec5SDimitry Andric   while (!CurrentSet.empty()) {
3220b57cec5SDimitry Andric     LLVM_DEBUG({
3230b57cec5SDimitry Andric       llvm::errs() << "DAG_DD - " << CurrentSet.size() << " active changes, "
3240b57cec5SDimitry Andric                    << Required.size() << " required changes\n";
3250b57cec5SDimitry Andric     });
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric     // Minimize the current set of changes.
3280b57cec5SDimitry Andric     DeltaActiveSetHelper Helper(*this, Required);
3290b57cec5SDimitry Andric     changeset_ty CurrentMinSet = Helper.Run(CurrentSet);
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric     // Update the set of required changes. Since
3320b57cec5SDimitry Andric     //   CurrentMinSet subset CurrentSet
3330b57cec5SDimitry Andric     // and after the last iteration,
3340b57cec5SDimitry Andric     //   succ(CurrentSet) subset Required
3350b57cec5SDimitry Andric     // then
3360b57cec5SDimitry Andric     //   succ(CurrentMinSet) subset Required
3370b57cec5SDimitry Andric     // and our invariant on Required is maintained.
3380b57cec5SDimitry Andric     Required.insert(CurrentMinSet.begin(), CurrentMinSet.end());
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric     // Replace the current set with the predecssors of the minimized set of
3410b57cec5SDimitry Andric     // active changes.
3420b57cec5SDimitry Andric     CurrentSet.clear();
3430b57cec5SDimitry Andric     for (changeset_ty::const_iterator it = CurrentMinSet.begin(),
3440b57cec5SDimitry Andric            ie = CurrentMinSet.end(); it != ie; ++it)
3450b57cec5SDimitry Andric       CurrentSet.insert(pred_begin(*it), pred_end(*it));
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric     // FIXME: We could enforce CurrentSet intersect Required == {} here if we
3480b57cec5SDimitry Andric     // wanted to protect against cyclic graphs.
3490b57cec5SDimitry Andric   }
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric   return Required;
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric 
anchor()3540b57cec5SDimitry Andric void DAGDeltaAlgorithm::anchor() {
3550b57cec5SDimitry Andric }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric DAGDeltaAlgorithm::changeset_ty
Run(const changeset_ty & Changes,const std::vector<edge_ty> & Dependencies)3580b57cec5SDimitry Andric DAGDeltaAlgorithm::Run(const changeset_ty &Changes,
3590b57cec5SDimitry Andric                        const std::vector<edge_ty> &Dependencies) {
3600b57cec5SDimitry Andric   return DAGDeltaAlgorithmImpl(*this, Changes, Dependencies).Run();
3610b57cec5SDimitry Andric }
362