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