LLVM API Documentation

DAGDeltaAlgorithm.cpp
Go to the documentation of this file.
00001 //===--- DAGDeltaAlgorithm.cpp - A DAG Minimization Algorithm --*- C++ -*--===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //===----------------------------------------------------------------------===//
00008 //
00009 // The algorithm we use attempts to exploit the dependency information by
00010 // minimizing top-down. We start by constructing an initial root set R, and
00011 // then iteratively:
00012 //
00013 //   1. Minimize the set R using the test predicate:
00014 //       P'(S) = P(S union pred*(S))
00015 //
00016 //   2. Extend R to R' = R union pred(R).
00017 //
00018 // until a fixed point is reached.
00019 //
00020 // The idea is that we want to quickly prune entire portions of the graph, so we
00021 // try to find high-level nodes that can be eliminated with all of their
00022 // dependents.
00023 //
00024 // FIXME: The current algorithm doesn't actually provide a strong guarantee
00025 // about the minimality of the result. The problem is that after adding nodes to
00026 // the required set, we no longer consider them for elimination. For strictly
00027 // well formed predicates, this doesn't happen, but it commonly occurs in
00028 // practice when there are unmodelled dependencies. I believe we can resolve
00029 // this by allowing the required set to be minimized as well, but need more test
00030 // cases first.
00031 //
00032 //===----------------------------------------------------------------------===//
00033 
00034 #include "llvm/ADT/DAGDeltaAlgorithm.h"
00035 #include "llvm/ADT/DeltaAlgorithm.h"
00036 #include "llvm/Support/Debug.h"
00037 #include "llvm/Support/Format.h"
00038 #include "llvm/Support/raw_ostream.h"
00039 #include <algorithm>
00040 #include <cassert>
00041 #include <iterator>
00042 #include <map>
00043 using namespace llvm;
00044 
00045 #define DEBUG_TYPE "dag-delta"
00046 
00047 namespace {
00048 
00049 class DAGDeltaAlgorithmImpl {
00050   friend class DeltaActiveSetHelper;
00051 
00052 public:
00053   typedef DAGDeltaAlgorithm::change_ty change_ty;
00054   typedef DAGDeltaAlgorithm::changeset_ty changeset_ty;
00055   typedef DAGDeltaAlgorithm::changesetlist_ty changesetlist_ty;
00056   typedef DAGDeltaAlgorithm::edge_ty edge_ty;
00057 
00058 private:
00059   typedef std::vector<change_ty>::iterator pred_iterator_ty;
00060   typedef std::vector<change_ty>::iterator succ_iterator_ty;
00061   typedef std::set<change_ty>::iterator pred_closure_iterator_ty;
00062   typedef std::set<change_ty>::iterator succ_closure_iterator_ty;
00063 
00064   DAGDeltaAlgorithm &DDA;
00065 
00066   const changeset_ty &Changes;
00067   const std::vector<edge_ty> &Dependencies;
00068 
00069   std::vector<change_ty> Roots;
00070 
00071   /// Cache of failed test results. Successful test results are never cached
00072   /// since we always reduce following a success. We maintain an independent
00073   /// cache from that used by the individual delta passes because we may get
00074   /// hits across multiple individual delta invocations.
00075   mutable std::set<changeset_ty> FailedTestsCache;
00076 
00077   // FIXME: Gross.
00078   std::map<change_ty, std::vector<change_ty> > Predecessors;
00079   std::map<change_ty, std::vector<change_ty> > Successors;
00080 
00081   std::map<change_ty, std::set<change_ty> > PredClosure;
00082   std::map<change_ty, std::set<change_ty> > SuccClosure;
00083 
00084 private:
00085   pred_iterator_ty pred_begin(change_ty Node) {
00086     assert(Predecessors.count(Node) && "Invalid node!");
00087     return Predecessors[Node].begin();
00088   }
00089   pred_iterator_ty pred_end(change_ty Node) {
00090     assert(Predecessors.count(Node) && "Invalid node!");
00091     return Predecessors[Node].end();
00092   }
00093 
00094   pred_closure_iterator_ty pred_closure_begin(change_ty Node) {
00095     assert(PredClosure.count(Node) && "Invalid node!");
00096     return PredClosure[Node].begin();
00097   }
00098   pred_closure_iterator_ty pred_closure_end(change_ty Node) {
00099     assert(PredClosure.count(Node) && "Invalid node!");
00100     return PredClosure[Node].end();
00101   }
00102   
00103   succ_iterator_ty succ_begin(change_ty Node) {
00104     assert(Successors.count(Node) && "Invalid node!");
00105     return Successors[Node].begin();
00106   }
00107   succ_iterator_ty succ_end(change_ty Node) {
00108     assert(Successors.count(Node) && "Invalid node!");
00109     return Successors[Node].end();
00110   }
00111 
00112   succ_closure_iterator_ty succ_closure_begin(change_ty Node) {
00113     assert(SuccClosure.count(Node) && "Invalid node!");
00114     return SuccClosure[Node].begin();
00115   }
00116   succ_closure_iterator_ty succ_closure_end(change_ty Node) {
00117     assert(SuccClosure.count(Node) && "Invalid node!");
00118     return SuccClosure[Node].end();
00119   }
00120 
00121   void UpdatedSearchState(const changeset_ty &Changes,
00122                           const changesetlist_ty &Sets,
00123                           const changeset_ty &Required) {
00124     DDA.UpdatedSearchState(Changes, Sets, Required);
00125   }
00126 
00127   /// ExecuteOneTest - Execute a single test predicate on the change set \p S.
00128   bool ExecuteOneTest(const changeset_ty &S) {
00129     // Check dependencies invariant.
00130     DEBUG({
00131         for (changeset_ty::const_iterator it = S.begin(),
00132                ie = S.end(); it != ie; ++it)
00133           for (succ_iterator_ty it2 = succ_begin(*it),
00134                  ie2 = succ_end(*it); it2 != ie2; ++it2)
00135             assert(S.count(*it2) && "Attempt to run invalid changeset!");
00136       });
00137 
00138     return DDA.ExecuteOneTest(S);
00139   }
00140 
00141 public:
00142   DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm &_DDA,
00143                         const changeset_ty &_Changes,
00144                         const std::vector<edge_ty> &_Dependencies);
00145 
00146   changeset_ty Run();
00147 
00148   /// GetTestResult - Get the test result for the active set \p Changes with
00149   /// \p Required changes from the cache, executing the test if necessary.
00150   ///
00151   /// \param Changes - The set of active changes being minimized, which should
00152   /// have their pred closure included in the test.
00153   /// \param Required - The set of changes which have previously been
00154   /// established to be required.
00155   /// \return - The test result.
00156   bool GetTestResult(const changeset_ty &Changes, const changeset_ty &Required);
00157 };
00158 
00159 /// Helper object for minimizing an active set of changes.
00160 class DeltaActiveSetHelper : public DeltaAlgorithm {
00161   DAGDeltaAlgorithmImpl &DDAI;
00162 
00163   const changeset_ty &Required;
00164 
00165 protected:
00166   /// UpdatedSearchState - Callback used when the search state changes.
00167   void UpdatedSearchState(const changeset_ty &Changes,
00168                                   const changesetlist_ty &Sets) override {
00169     DDAI.UpdatedSearchState(Changes, Sets, Required);
00170   }
00171 
00172   bool ExecuteOneTest(const changeset_ty &S) override {
00173     return DDAI.GetTestResult(S, Required);
00174   }
00175 
00176 public:
00177   DeltaActiveSetHelper(DAGDeltaAlgorithmImpl &_DDAI,
00178                        const changeset_ty &_Required)
00179     : DDAI(_DDAI), Required(_Required) {}
00180 };
00181 
00182 }
00183 
00184 DAGDeltaAlgorithmImpl::DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm &_DDA,
00185                                              const changeset_ty &_Changes,
00186                                              const std::vector<edge_ty>
00187                                                &_Dependencies)
00188   : DDA(_DDA),
00189     Changes(_Changes),
00190     Dependencies(_Dependencies)
00191 {
00192   for (changeset_ty::const_iterator it = Changes.begin(),
00193          ie = Changes.end(); it != ie; ++it) {
00194     Predecessors.insert(std::make_pair(*it, std::vector<change_ty>()));
00195     Successors.insert(std::make_pair(*it, std::vector<change_ty>()));
00196   }
00197   for (std::vector<edge_ty>::const_iterator it = Dependencies.begin(),
00198          ie = Dependencies.end(); it != ie; ++it) {
00199     Predecessors[it->second].push_back(it->first);
00200     Successors[it->first].push_back(it->second);
00201   }
00202 
00203   // Compute the roots.
00204   for (changeset_ty::const_iterator it = Changes.begin(),
00205          ie = Changes.end(); it != ie; ++it)
00206     if (succ_begin(*it) == succ_end(*it))
00207       Roots.push_back(*it);
00208 
00209   // Pre-compute the closure of the successor relation.
00210   std::vector<change_ty> Worklist(Roots.begin(), Roots.end());
00211   while (!Worklist.empty()) {
00212     change_ty Change = Worklist.back();
00213     Worklist.pop_back();
00214 
00215     std::set<change_ty> &ChangeSuccs = SuccClosure[Change];
00216     for (pred_iterator_ty it = pred_begin(Change), 
00217            ie = pred_end(Change); it != ie; ++it) {
00218       SuccClosure[*it].insert(Change);
00219       SuccClosure[*it].insert(ChangeSuccs.begin(), ChangeSuccs.end());
00220       Worklist.push_back(*it);
00221     }
00222   }
00223 
00224   // Invert to form the predecessor closure map.
00225   for (changeset_ty::const_iterator it = Changes.begin(),
00226          ie = Changes.end(); it != ie; ++it)
00227     PredClosure.insert(std::make_pair(*it, std::set<change_ty>()));
00228   for (changeset_ty::const_iterator it = Changes.begin(),
00229          ie = Changes.end(); it != ie; ++it)
00230     for (succ_closure_iterator_ty it2 = succ_closure_begin(*it),
00231            ie2 = succ_closure_end(*it); it2 != ie2; ++it2)
00232       PredClosure[*it2].insert(*it);
00233   
00234   // Dump useful debug info.
00235   DEBUG({
00236       llvm::errs() << "-- DAGDeltaAlgorithmImpl --\n";
00237       llvm::errs() << "Changes: [";
00238       for (changeset_ty::const_iterator it = Changes.begin(),
00239              ie = Changes.end(); it != ie; ++it) {
00240         if (it != Changes.begin()) llvm::errs() << ", ";
00241         llvm::errs() << *it;
00242 
00243         if (succ_begin(*it) != succ_end(*it)) {
00244           llvm::errs() << "(";
00245           for (succ_iterator_ty it2 = succ_begin(*it),
00246                  ie2 = succ_end(*it); it2 != ie2; ++it2) {
00247             if (it2 != succ_begin(*it)) llvm::errs() << ", ";
00248             llvm::errs() << "->" << *it2;
00249           }
00250           llvm::errs() << ")";
00251         }
00252       }
00253       llvm::errs() << "]\n";
00254 
00255       llvm::errs() << "Roots: [";
00256       for (std::vector<change_ty>::const_iterator it = Roots.begin(),
00257              ie = Roots.end(); it != ie; ++it) {
00258         if (it != Roots.begin()) llvm::errs() << ", ";
00259         llvm::errs() << *it;
00260       }
00261       llvm::errs() << "]\n";
00262 
00263       llvm::errs() << "Predecessor Closure:\n";
00264       for (changeset_ty::const_iterator it = Changes.begin(),
00265              ie = Changes.end(); it != ie; ++it) {
00266         llvm::errs() << format("  %-4d: [", *it);
00267         for (pred_closure_iterator_ty it2 = pred_closure_begin(*it),
00268                ie2 = pred_closure_end(*it); it2 != ie2; ++it2) {
00269           if (it2 != pred_closure_begin(*it)) llvm::errs() << ", ";
00270           llvm::errs() << *it2;
00271         }
00272         llvm::errs() << "]\n";
00273       }
00274       
00275       llvm::errs() << "Successor Closure:\n";
00276       for (changeset_ty::const_iterator it = Changes.begin(),
00277              ie = Changes.end(); it != ie; ++it) {
00278         llvm::errs() << format("  %-4d: [", *it);
00279         for (succ_closure_iterator_ty it2 = succ_closure_begin(*it),
00280                ie2 = succ_closure_end(*it); it2 != ie2; ++it2) {
00281           if (it2 != succ_closure_begin(*it)) llvm::errs() << ", ";
00282           llvm::errs() << *it2;
00283         }
00284         llvm::errs() << "]\n";
00285       }
00286 
00287       llvm::errs() << "\n\n";
00288     });
00289 }
00290 
00291 bool DAGDeltaAlgorithmImpl::GetTestResult(const changeset_ty &Changes,
00292                                           const changeset_ty &Required) {
00293   changeset_ty Extended(Required);
00294   Extended.insert(Changes.begin(), Changes.end());
00295   for (changeset_ty::const_iterator it = Changes.begin(),
00296          ie = Changes.end(); it != ie; ++it)
00297     Extended.insert(pred_closure_begin(*it), pred_closure_end(*it));
00298 
00299   if (FailedTestsCache.count(Extended))
00300     return false;
00301 
00302   bool Result = ExecuteOneTest(Extended);
00303   if (!Result)
00304     FailedTestsCache.insert(Extended);
00305 
00306   return Result;
00307 }
00308 
00309 DAGDeltaAlgorithm::changeset_ty
00310 DAGDeltaAlgorithmImpl::Run() {
00311   // The current set of changes we are minimizing, starting at the roots.
00312   changeset_ty CurrentSet(Roots.begin(), Roots.end());
00313 
00314   // The set of required changes.
00315   changeset_ty Required;
00316 
00317   // Iterate until the active set of changes is empty. Convergence is guaranteed
00318   // assuming input was a DAG.
00319   //
00320   // Invariant:  CurrentSet intersect Required == {}
00321   // Invariant:  Required == (Required union succ*(Required))
00322   while (!CurrentSet.empty()) {
00323     DEBUG({
00324         llvm::errs() << "DAG_DD - " << CurrentSet.size() << " active changes, "
00325                      << Required.size() << " required changes\n";
00326       });
00327 
00328     // Minimize the current set of changes.
00329     DeltaActiveSetHelper Helper(*this, Required);
00330     changeset_ty CurrentMinSet = Helper.Run(CurrentSet);
00331 
00332     // Update the set of required changes. Since
00333     //   CurrentMinSet subset CurrentSet
00334     // and after the last iteration,
00335     //   succ(CurrentSet) subset Required
00336     // then
00337     //   succ(CurrentMinSet) subset Required
00338     // and our invariant on Required is maintained.
00339     Required.insert(CurrentMinSet.begin(), CurrentMinSet.end());
00340 
00341     // Replace the current set with the predecssors of the minimized set of
00342     // active changes.
00343     CurrentSet.clear();
00344     for (changeset_ty::const_iterator it = CurrentMinSet.begin(),
00345            ie = CurrentMinSet.end(); it != ie; ++it)
00346       CurrentSet.insert(pred_begin(*it), pred_end(*it));
00347 
00348     // FIXME: We could enforce CurrentSet intersect Required == {} here if we
00349     // wanted to protect against cyclic graphs.
00350   }
00351 
00352   return Required;
00353 }
00354 
00355 void DAGDeltaAlgorithm::anchor() {
00356 }
00357 
00358 DAGDeltaAlgorithm::changeset_ty
00359 DAGDeltaAlgorithm::Run(const changeset_ty &Changes,
00360                        const std::vector<edge_ty> &Dependencies) {
00361   return DAGDeltaAlgorithmImpl(*this, Changes, Dependencies).Run();
00362 }