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