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