1 //===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
3 // This class is to be used as a base class for operations that want to zero in
4 // on a subset of the input which still causes the bug we are tracking.
6 //===----------------------------------------------------------------------===//
8 #ifndef BUGPOINT_LIST_REDUCER_H
9 #define BUGPOINT_LIST_REDUCER_H
13 template<typename ElTy>
16 NoFailure, // No failure of the predicate was detected
17 KeepSuffix, // The suffix alone satisfies the predicate
18 KeepPrefix, // The prefix alone satisfies the predicate
21 // doTest - This virtual function should be overriden by subclasses to
22 // implement the test desired. The testcase is only required to test to see
23 // if the Kept list still satisfies the property, but if it is going to check
24 // the prefix anyway, it can.
26 virtual TestResult doTest(std::vector<ElTy> &Prefix,
27 std::vector<ElTy> &Kept) = 0;
29 // reduceList - This function attempts to reduce the length of the specified
30 // list while still maintaining the "test" property. This is the core of the
31 // "work" that bugpoint does.
33 bool reduceList(std::vector<ElTy> &TheList) {
34 std::vector<ElTy> empty;
35 switch (doTest(TheList, empty)) {
37 if (TheList.size() == 1) // we are done, it's the base case and it fails
40 break; // there's definitely an error, but we need to narrow it down
44 std::cerr << "bugpoint ListReducer internal error: selected empty set.\n";
48 return false; // there is no failure with the full set of passes/funcs!
51 unsigned MidTop = TheList.size();
53 unsigned Mid = MidTop / 2;
54 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
55 std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
57 switch (doTest(Prefix, Suffix)) {
59 // The property still holds. We can just drop the prefix elements, and
60 // shorten the list to the "kept" elements.
62 MidTop = TheList.size();
65 // The predicate still holds, shorten the list to the prefix elements.
67 MidTop = TheList.size();
70 // Otherwise the property doesn't hold. Some of the elements we removed
71 // must be necessary to maintain the property.
77 // Okay, we trimmed as much off the top and the bottom of the list as we
78 // could. If there is more two elements in the list, try deleting interior
79 // elements and testing that.
81 if (TheList.size() > 2) {
83 std::vector<ElTy> EmptyList;
86 std::vector<ElTy> TrimmedList;
87 for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts
88 std::vector<ElTy> TestList(TheList);
89 TestList.erase(TestList.begin()+i);
91 if (doTest(EmptyList, TestList) == KeepSuffix) {
92 // We can trim down the list!
93 TheList.swap(TestList);
94 --i; // Don't skip an element of the list
101 return true; // there are some failure and we've narrowed them down