xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ADT/DAGDeltaAlgorithm.h (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
10b57cec5SDimitry Andric //===- DAGDeltaAlgorithm.h - 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 #ifndef LLVM_ADT_DAGDELTAALGORITHM_H
90b57cec5SDimitry Andric #define LLVM_ADT_DAGDELTAALGORITHM_H
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include <set>
120b57cec5SDimitry Andric #include <utility>
130b57cec5SDimitry Andric #include <vector>
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric namespace llvm {
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric /// DAGDeltaAlgorithm - Implements a "delta debugging" algorithm for minimizing
180b57cec5SDimitry Andric /// directed acyclic graphs using a predicate function.
190b57cec5SDimitry Andric ///
200b57cec5SDimitry Andric /// The result of the algorithm is a subset of the input change set which is
210b57cec5SDimitry Andric /// guaranteed to satisfy the predicate, assuming that the input set did. For
220b57cec5SDimitry Andric /// well formed predicates, the result set is guaranteed to be such that
230b57cec5SDimitry Andric /// removing any single element not required by the dependencies on the other
240b57cec5SDimitry Andric /// elements would falsify the predicate.
250b57cec5SDimitry Andric ///
260b57cec5SDimitry Andric /// The DAG should be used to represent dependencies in the changes which are
270b57cec5SDimitry Andric /// likely to hold across the predicate function. That is, for a particular
280b57cec5SDimitry Andric /// changeset S and predicate P:
290b57cec5SDimitry Andric ///
300b57cec5SDimitry Andric ///   P(S) => P(S union pred(S))
310b57cec5SDimitry Andric ///
32*5ffd83dbSDimitry Andric /// The minimization algorithm uses this dependency information to attempt to
330b57cec5SDimitry Andric /// eagerly prune large subsets of changes. As with \see DeltaAlgorithm, the DAG
340b57cec5SDimitry Andric /// is not required to satisfy this property, but the algorithm will run
350b57cec5SDimitry Andric /// substantially fewer tests with appropriate dependencies. \see DeltaAlgorithm
360b57cec5SDimitry Andric /// for more information on the properties which the predicate function itself
370b57cec5SDimitry Andric /// should satisfy.
380b57cec5SDimitry Andric class DAGDeltaAlgorithm {
390b57cec5SDimitry Andric   virtual void anchor();
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric public:
420b57cec5SDimitry Andric   using change_ty = unsigned;
430b57cec5SDimitry Andric   using edge_ty = std::pair<change_ty, change_ty>;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   // FIXME: Use a decent data structure.
460b57cec5SDimitry Andric   using changeset_ty = std::set<change_ty>;
470b57cec5SDimitry Andric   using changesetlist_ty = std::vector<changeset_ty>;
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric public:
500b57cec5SDimitry Andric   virtual ~DAGDeltaAlgorithm() = default;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   /// Run - Minimize the DAG formed by the \p Changes vertices and the
530b57cec5SDimitry Andric   /// \p Dependencies edges by executing \see ExecuteOneTest() on subsets of
540b57cec5SDimitry Andric   /// changes and returning the smallest set which still satisfies the test
550b57cec5SDimitry Andric   /// predicate and the input \p Dependencies.
560b57cec5SDimitry Andric   ///
570b57cec5SDimitry Andric   /// \param Changes The list of changes.
580b57cec5SDimitry Andric   ///
590b57cec5SDimitry Andric   /// \param Dependencies The list of dependencies amongst changes. For each
600b57cec5SDimitry Andric   /// (x,y) in \p Dependencies, both x and y must be in \p Changes. The
610b57cec5SDimitry Andric   /// minimization algorithm guarantees that for each tested changed set S,
620b57cec5SDimitry Andric   /// \f$ x \in S \f$ implies \f$ y \in S \f$. It is an error to have cyclic
630b57cec5SDimitry Andric   /// dependencies.
640b57cec5SDimitry Andric   changeset_ty Run(const changeset_ty &Changes,
650b57cec5SDimitry Andric                    const std::vector<edge_ty> &Dependencies);
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   /// UpdatedSearchState - Callback used when the search state changes.
UpdatedSearchState(const changeset_ty & Changes,const changesetlist_ty & Sets,const changeset_ty & Required)680b57cec5SDimitry Andric   virtual void UpdatedSearchState(const changeset_ty &Changes,
690b57cec5SDimitry Andric                                   const changesetlist_ty &Sets,
700b57cec5SDimitry Andric                                   const changeset_ty &Required) {}
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   /// ExecuteOneTest - Execute a single test predicate on the change set \p S.
730b57cec5SDimitry Andric   virtual bool ExecuteOneTest(const changeset_ty &S) = 0;
740b57cec5SDimitry Andric };
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric } // end namespace llvm
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric #endif // LLVM_ADT_DAGDELTAALGORITHM_H
79