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