1c5b2ac4dSDaniel Dunbar //===--- DeltaAlgorithm.cpp - A Set Minimization Algorithm -----*- C++ -*--===//
2ff53d469SDaniel Dunbar //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ff53d469SDaniel Dunbar //===----------------------------------------------------------------------===//
7ff53d469SDaniel Dunbar 
8ff53d469SDaniel Dunbar #include "llvm/ADT/DeltaAlgorithm.h"
9ff53d469SDaniel Dunbar #include <algorithm>
10c5b2ac4dSDaniel Dunbar #include <iterator>
112ec8b150SVassil Vassilev #include <set>
12ff53d469SDaniel Dunbar using namespace llvm;
13ff53d469SDaniel Dunbar 
14*3a3cb929SKazu Hirata DeltaAlgorithm::~DeltaAlgorithm() = default;
15d3331394SDaniel Dunbar 
GetTestResult(const changeset_ty & Changes)16ff53d469SDaniel Dunbar bool DeltaAlgorithm::GetTestResult(const changeset_ty &Changes) {
17ff53d469SDaniel Dunbar   if (FailedTestsCache.count(Changes))
18ff53d469SDaniel Dunbar     return false;
19ff53d469SDaniel Dunbar 
20ff53d469SDaniel Dunbar   bool Result = ExecuteOneTest(Changes);
21ff53d469SDaniel Dunbar   if (!Result)
22ff53d469SDaniel Dunbar     FailedTestsCache.insert(Changes);
23ff53d469SDaniel Dunbar 
24ff53d469SDaniel Dunbar   return Result;
25ff53d469SDaniel Dunbar }
26ff53d469SDaniel Dunbar 
Split(const changeset_ty & S,changesetlist_ty & Res)27ff53d469SDaniel Dunbar void DeltaAlgorithm::Split(const changeset_ty &S, changesetlist_ty &Res) {
28ff53d469SDaniel Dunbar   // FIXME: Allow clients to provide heuristics for improved splitting.
29ff53d469SDaniel Dunbar 
306c704ebbSTimur Iskhodzhanov   // FIXME: This is really slow.
316c704ebbSTimur Iskhodzhanov   changeset_ty LHS, RHS;
326c704ebbSTimur Iskhodzhanov   unsigned idx = 0, N = S.size() / 2;
336c704ebbSTimur Iskhodzhanov   for (changeset_ty::const_iterator it = S.begin(),
346c704ebbSTimur Iskhodzhanov          ie = S.end(); it != ie; ++it, ++idx)
356c704ebbSTimur Iskhodzhanov     ((idx < N) ? LHS : RHS).insert(*it);
36ff53d469SDaniel Dunbar   if (!LHS.empty())
37ff53d469SDaniel Dunbar     Res.push_back(LHS);
38ff53d469SDaniel Dunbar   if (!RHS.empty())
39ff53d469SDaniel Dunbar     Res.push_back(RHS);
40ff53d469SDaniel Dunbar }
41ff53d469SDaniel Dunbar 
42ff53d469SDaniel Dunbar DeltaAlgorithm::changeset_ty
Delta(const changeset_ty & Changes,const changesetlist_ty & Sets)43ff53d469SDaniel Dunbar DeltaAlgorithm::Delta(const changeset_ty &Changes,
44ff53d469SDaniel Dunbar                       const changesetlist_ty &Sets) {
45ff53d469SDaniel Dunbar   // Invariant: union(Res) == Changes
46ff53d469SDaniel Dunbar   UpdatedSearchState(Changes, Sets);
47ff53d469SDaniel Dunbar 
48ff53d469SDaniel Dunbar   // If there is nothing left we can remove, we are done.
49ff53d469SDaniel Dunbar   if (Sets.size() <= 1)
50ff53d469SDaniel Dunbar     return Changes;
51ff53d469SDaniel Dunbar 
52ff53d469SDaniel Dunbar   // Look for a passing subset.
53ff53d469SDaniel Dunbar   changeset_ty Res;
54ff53d469SDaniel Dunbar   if (Search(Changes, Sets, Res))
55ff53d469SDaniel Dunbar     return Res;
56ff53d469SDaniel Dunbar 
57ff53d469SDaniel Dunbar   // Otherwise, partition the sets if possible; if not we are done.
58ff53d469SDaniel Dunbar   changesetlist_ty SplitSets;
593dbcccabSKazu Hirata   for (const changeset_ty &Set : Sets)
603dbcccabSKazu Hirata     Split(Set, SplitSets);
61ff53d469SDaniel Dunbar   if (SplitSets.size() == Sets.size())
62ff53d469SDaniel Dunbar     return Changes;
63ff53d469SDaniel Dunbar 
64ff53d469SDaniel Dunbar   return Delta(Changes, SplitSets);
65ff53d469SDaniel Dunbar }
66ff53d469SDaniel Dunbar 
Search(const changeset_ty & Changes,const changesetlist_ty & Sets,changeset_ty & Res)67ff53d469SDaniel Dunbar bool DeltaAlgorithm::Search(const changeset_ty &Changes,
68ff53d469SDaniel Dunbar                             const changesetlist_ty &Sets,
69ff53d469SDaniel Dunbar                             changeset_ty &Res) {
70ff53d469SDaniel Dunbar   // FIXME: Parallelize.
71ff53d469SDaniel Dunbar   for (changesetlist_ty::const_iterator it = Sets.begin(),
72ff53d469SDaniel Dunbar          ie = Sets.end(); it != ie; ++it) {
73ff53d469SDaniel Dunbar     // If the test passes on this subset alone, recurse.
74ff53d469SDaniel Dunbar     if (GetTestResult(*it)) {
75ff53d469SDaniel Dunbar       changesetlist_ty Sets;
76ff53d469SDaniel Dunbar       Split(*it, Sets);
77ff53d469SDaniel Dunbar       Res = Delta(*it, Sets);
78ff53d469SDaniel Dunbar       return true;
79ff53d469SDaniel Dunbar     }
80ff53d469SDaniel Dunbar 
81ff53d469SDaniel Dunbar     // Otherwise, if we have more than two sets, see if test passes on the
82ff53d469SDaniel Dunbar     // complement.
83ff53d469SDaniel Dunbar     if (Sets.size() > 2) {
84ff53d469SDaniel Dunbar       // FIXME: This is really slow.
85ff53d469SDaniel Dunbar       changeset_ty Complement;
86ff53d469SDaniel Dunbar       std::set_difference(
87ff53d469SDaniel Dunbar         Changes.begin(), Changes.end(), it->begin(), it->end(),
88ff53d469SDaniel Dunbar         std::insert_iterator<changeset_ty>(Complement, Complement.begin()));
89ff53d469SDaniel Dunbar       if (GetTestResult(Complement)) {
90ff53d469SDaniel Dunbar         changesetlist_ty ComplementSets;
91ff53d469SDaniel Dunbar         ComplementSets.insert(ComplementSets.end(), Sets.begin(), it);
92ff53d469SDaniel Dunbar         ComplementSets.insert(ComplementSets.end(), it + 1, Sets.end());
93ff53d469SDaniel Dunbar         Res = Delta(Complement, ComplementSets);
94ff53d469SDaniel Dunbar         return true;
95ff53d469SDaniel Dunbar       }
96ff53d469SDaniel Dunbar     }
97ff53d469SDaniel Dunbar   }
98ff53d469SDaniel Dunbar 
99ff53d469SDaniel Dunbar   return false;
100ff53d469SDaniel Dunbar }
101ff53d469SDaniel Dunbar 
Run(const changeset_ty & Changes)102ff53d469SDaniel Dunbar DeltaAlgorithm::changeset_ty DeltaAlgorithm::Run(const changeset_ty &Changes) {
103ff53d469SDaniel Dunbar   // Check empty set first to quickly find poor test functions.
104ff53d469SDaniel Dunbar   if (GetTestResult(changeset_ty()))
105ff53d469SDaniel Dunbar     return changeset_ty();
106ff53d469SDaniel Dunbar 
107ff53d469SDaniel Dunbar   // Otherwise run the real delta algorithm.
108ff53d469SDaniel Dunbar   changesetlist_ty Sets;
109ff53d469SDaniel Dunbar   Split(Changes, Sets);
110ff53d469SDaniel Dunbar 
111ff53d469SDaniel Dunbar   return Delta(Changes, Sets);
112ff53d469SDaniel Dunbar }
113