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