2003-04-24 20:16:29 +00:00
|
|
|
//===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-04-24 20:16:29 +00:00
|
|
|
//
|
|
|
|
// This class is to be used as a base class for operations that want to zero in
|
|
|
|
// on a subset of the input which still causes the bug we are tracking.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef BUGPOINT_LIST_REDUCER_H
|
|
|
|
#define BUGPOINT_LIST_REDUCER_H
|
|
|
|
|
2009-07-15 16:35:29 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-04-12 05:08:25 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2003-04-24 20:16:29 +00:00
|
|
|
#include <vector>
|
2006-10-10 21:42:25 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <algorithm>
|
2003-04-24 20:16:29 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
2005-08-02 02:16:17 +00:00
|
|
|
|
|
|
|
extern bool BugpointIsInterrupted;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-04-24 20:16:29 +00:00
|
|
|
template<typename ElTy>
|
|
|
|
struct ListReducer {
|
|
|
|
enum TestResult {
|
|
|
|
NoFailure, // No failure of the predicate was detected
|
|
|
|
KeepSuffix, // The suffix alone satisfies the predicate
|
2010-04-12 05:08:25 +00:00
|
|
|
KeepPrefix, // The prefix alone satisfies the predicate
|
|
|
|
InternalError // Encountered an error trying to run the predicate
|
2003-04-24 20:16:29 +00:00
|
|
|
};
|
|
|
|
|
2004-05-11 20:41:07 +00:00
|
|
|
virtual ~ListReducer() {}
|
|
|
|
|
2003-04-24 20:16:29 +00:00
|
|
|
// doTest - This virtual function should be overriden by subclasses to
|
|
|
|
// implement the test desired. The testcase is only required to test to see
|
|
|
|
// if the Kept list still satisfies the property, but if it is going to check
|
|
|
|
// the prefix anyway, it can.
|
|
|
|
//
|
2003-04-24 22:23:56 +00:00
|
|
|
virtual TestResult doTest(std::vector<ElTy> &Prefix,
|
2010-04-12 05:08:25 +00:00
|
|
|
std::vector<ElTy> &Kept,
|
|
|
|
std::string &Error) = 0;
|
2003-04-24 20:16:29 +00:00
|
|
|
|
|
|
|
// reduceList - This function attempts to reduce the length of the specified
|
|
|
|
// list while still maintaining the "test" property. This is the core of the
|
|
|
|
// "work" that bugpoint does.
|
|
|
|
//
|
2010-04-12 05:08:25 +00:00
|
|
|
bool reduceList(std::vector<ElTy> &TheList, std::string &Error) {
|
2003-07-30 20:15:56 +00:00
|
|
|
std::vector<ElTy> empty;
|
2006-10-10 21:42:25 +00:00
|
|
|
std::srand(0x6e5ea738); // Seed the random number generator
|
2010-04-12 05:08:25 +00:00
|
|
|
switch (doTest(TheList, empty, Error)) {
|
2003-07-30 20:15:56 +00:00
|
|
|
case KeepPrefix:
|
|
|
|
if (TheList.size() == 1) // we are done, it's the base case and it fails
|
|
|
|
return true;
|
2005-04-22 00:00:37 +00:00
|
|
|
else
|
2003-07-30 20:15:56 +00:00
|
|
|
break; // there's definitely an error, but we need to narrow it down
|
|
|
|
|
|
|
|
case KeepSuffix:
|
|
|
|
// cannot be reached!
|
2010-04-12 05:08:25 +00:00
|
|
|
llvm_unreachable("bugpoint ListReducer internal error: "
|
|
|
|
"selected empty set.");
|
2003-07-30 20:15:56 +00:00
|
|
|
|
|
|
|
case NoFailure:
|
|
|
|
return false; // there is no failure with the full set of passes/funcs!
|
2010-04-12 05:08:25 +00:00
|
|
|
|
|
|
|
case InternalError:
|
|
|
|
assert(!Error.empty());
|
|
|
|
return true;
|
2003-07-30 20:15:56 +00:00
|
|
|
}
|
|
|
|
|
2006-10-10 21:42:25 +00:00
|
|
|
// Maximal number of allowed splitting iterations,
|
|
|
|
// before the elements are randomly shuffled.
|
|
|
|
const unsigned MaxIterationsWithoutProgress = 3;
|
|
|
|
bool ShufflingEnabled = true;
|
|
|
|
|
|
|
|
Backjump:
|
2003-04-24 20:16:29 +00:00
|
|
|
unsigned MidTop = TheList.size();
|
2006-10-10 21:42:25 +00:00
|
|
|
unsigned MaxIterations = MaxIterationsWithoutProgress;
|
|
|
|
unsigned NumOfIterationsWithoutProgress = 0;
|
|
|
|
while (MidTop > 1) { // Binary split reduction loop
|
2005-08-02 02:16:17 +00:00
|
|
|
// Halt if the user presses ctrl-c.
|
|
|
|
if (BugpointIsInterrupted) {
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
|
2005-08-02 02:16:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-02-26 10:46:10 +00:00
|
|
|
|
2006-10-10 21:42:25 +00:00
|
|
|
// If the loop doesn't make satisfying progress, try shuffling.
|
|
|
|
// The purpose of shuffling is to avoid the heavy tails of the
|
|
|
|
// distribution (improving the speed of convergence).
|
|
|
|
if (ShufflingEnabled &&
|
2008-02-26 10:46:10 +00:00
|
|
|
NumOfIterationsWithoutProgress > MaxIterations) {
|
|
|
|
std::vector<ElTy> ShuffledList(TheList);
|
|
|
|
std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << "\n\n*** Testing shuffled set...\n\n";
|
2008-02-26 10:46:10 +00:00
|
|
|
// Check that random shuffle doesn't loose the bug
|
2010-04-12 05:08:25 +00:00
|
|
|
if (doTest(ShuffledList, empty, Error) == KeepPrefix) {
|
2006-10-10 21:42:25 +00:00
|
|
|
// If the bug is still here, use the shuffled list.
|
|
|
|
TheList.swap(ShuffledList);
|
|
|
|
MidTop = TheList.size();
|
|
|
|
// Must increase the shuffling treshold to avoid the small
|
|
|
|
// probability of inifinite looping without making progress.
|
|
|
|
MaxIterations += 2;
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << "\n\n*** Shuffling does not hide the bug...\n\n";
|
2008-02-26 10:46:10 +00:00
|
|
|
} else {
|
2006-10-10 21:42:25 +00:00
|
|
|
ShufflingEnabled = false; // Disable shuffling further on
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << "\n\n*** Shuffling hides the bug...\n\n";
|
2008-02-26 10:46:10 +00:00
|
|
|
}
|
|
|
|
NumOfIterationsWithoutProgress = 0;
|
2006-10-10 21:42:25 +00:00
|
|
|
}
|
2005-08-02 02:16:17 +00:00
|
|
|
|
2003-04-24 20:16:29 +00:00
|
|
|
unsigned Mid = MidTop / 2;
|
2003-04-25 03:16:33 +00:00
|
|
|
std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
|
|
|
|
std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
|
2003-04-24 20:16:29 +00:00
|
|
|
|
2010-04-12 05:08:25 +00:00
|
|
|
switch (doTest(Prefix, Suffix, Error)) {
|
2003-04-24 20:16:29 +00:00
|
|
|
case KeepSuffix:
|
|
|
|
// The property still holds. We can just drop the prefix elements, and
|
|
|
|
// shorten the list to the "kept" elements.
|
2003-04-25 03:16:33 +00:00
|
|
|
TheList.swap(Suffix);
|
2003-04-24 20:16:29 +00:00
|
|
|
MidTop = TheList.size();
|
2006-10-10 21:42:25 +00:00
|
|
|
// Reset progress treshold and progress counter
|
|
|
|
MaxIterations = MaxIterationsWithoutProgress;
|
|
|
|
NumOfIterationsWithoutProgress = 0;
|
2003-04-24 20:16:29 +00:00
|
|
|
break;
|
|
|
|
case KeepPrefix:
|
|
|
|
// The predicate still holds, shorten the list to the prefix elements.
|
|
|
|
TheList.swap(Prefix);
|
|
|
|
MidTop = TheList.size();
|
2006-10-10 21:42:25 +00:00
|
|
|
// Reset progress treshold and progress counter
|
|
|
|
MaxIterations = MaxIterationsWithoutProgress;
|
|
|
|
NumOfIterationsWithoutProgress = 0;
|
2003-04-24 20:16:29 +00:00
|
|
|
break;
|
|
|
|
case NoFailure:
|
|
|
|
// Otherwise the property doesn't hold. Some of the elements we removed
|
2003-08-18 14:43:39 +00:00
|
|
|
// must be necessary to maintain the property.
|
2003-04-24 20:16:29 +00:00
|
|
|
MidTop = Mid;
|
2006-10-10 21:42:25 +00:00
|
|
|
NumOfIterationsWithoutProgress++;
|
2003-04-24 20:16:29 +00:00
|
|
|
break;
|
2010-04-12 05:08:25 +00:00
|
|
|
case InternalError:
|
|
|
|
return true; // Error was set by doTest.
|
2003-04-24 20:16:29 +00:00
|
|
|
}
|
2010-04-12 05:08:25 +00:00
|
|
|
assert(Error.empty() && "doTest did not return InternalError for error");
|
2003-04-24 20:16:29 +00:00
|
|
|
}
|
|
|
|
|
2006-10-10 21:42:25 +00:00
|
|
|
// Probability of backjumping from the trimming loop back to the binary
|
|
|
|
// split reduction loop.
|
|
|
|
const int BackjumpProbability = 10;
|
|
|
|
|
2003-04-24 20:16:29 +00:00
|
|
|
// Okay, we trimmed as much off the top and the bottom of the list as we
|
2006-01-08 22:40:10 +00:00
|
|
|
// could. If there is more than two elements in the list, try deleting
|
|
|
|
// interior elements and testing that.
|
2003-04-24 20:16:29 +00:00
|
|
|
//
|
2004-11-18 19:42:50 +00:00
|
|
|
if (TheList.size() > 2) {
|
2003-04-24 20:16:29 +00:00
|
|
|
bool Changed = true;
|
|
|
|
std::vector<ElTy> EmptyList;
|
2006-10-10 21:42:25 +00:00
|
|
|
while (Changed) { // Trimming loop.
|
2003-04-24 20:16:29 +00:00
|
|
|
Changed = false;
|
2006-10-10 21:42:25 +00:00
|
|
|
|
|
|
|
// If the binary split reduction loop made an unfortunate sequence of
|
|
|
|
// splits, the trimming loop might be left off with a huge number of
|
|
|
|
// remaining elements (large search space). Backjumping out of that
|
|
|
|
// search space and attempting a different split can significantly
|
|
|
|
// improve the convergence speed.
|
|
|
|
if (std::rand() % 100 < BackjumpProbability)
|
|
|
|
goto Backjump;
|
|
|
|
|
2003-04-24 20:16:29 +00:00
|
|
|
for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts
|
2005-08-02 02:16:17 +00:00
|
|
|
if (BugpointIsInterrupted) {
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
|
2005-08-02 02:16:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-04-24 20:16:29 +00:00
|
|
|
std::vector<ElTy> TestList(TheList);
|
|
|
|
TestList.erase(TestList.begin()+i);
|
|
|
|
|
2010-04-12 05:08:25 +00:00
|
|
|
if (doTest(EmptyList, TestList, Error) == KeepSuffix) {
|
2003-04-24 20:16:29 +00:00
|
|
|
// We can trim down the list!
|
|
|
|
TheList.swap(TestList);
|
|
|
|
--i; // Don't skip an element of the list
|
|
|
|
Changed = true;
|
|
|
|
}
|
2010-07-12 08:16:59 +00:00
|
|
|
if (!Error.empty())
|
|
|
|
return true;
|
2003-04-24 20:16:29 +00:00
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
// This can take a long time if left uncontrolled. For now, don't
|
2004-11-18 19:42:50 +00:00
|
|
|
// iterate.
|
|
|
|
break;
|
2003-04-24 20:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
2003-07-30 20:15:56 +00:00
|
|
|
|
|
|
|
return true; // there are some failure and we've narrowed them down
|
2003-04-24 20:16:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2003-04-24 20:16:29 +00:00
|
|
|
#endif
|