blob: cee68926c9c3dd279c50a422598cf9a6b1de1cd2 [file] [log] [blame]
Chris Lattner126840f2003-04-24 20:16:29 +00001//===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
2//
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.
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef BUGPOINT_LIST_REDUCER_H
9#define BUGPOINT_LIST_REDUCER_H
10
11#include <vector>
12
13template<typename ElTy>
14struct ListReducer {
15 enum TestResult {
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
19 };
20
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.
25 //
Chris Lattnerb4ebe132003-04-24 22:23:56 +000026 virtual TestResult doTest(std::vector<ElTy> &Prefix,
27 std::vector<ElTy> &Kept) = 0;
Chris Lattner126840f2003-04-24 20:16:29 +000028
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.
32 //
Misha Brukmanbe6bf562003-07-30 20:15:56 +000033 bool reduceList(std::vector<ElTy> &TheList) {
34 std::vector<ElTy> empty;
35 switch (doTest(TheList, empty)) {
36 case KeepPrefix:
37 if (TheList.size() == 1) // we are done, it's the base case and it fails
38 return true;
39 else
40 break; // there's definitely an error, but we need to narrow it down
41
42 case KeepSuffix:
43 // cannot be reached!
44 std::cerr << "bugpoint ListReducer internal error: selected empty set.\n";
45 abort();
46
47 case NoFailure:
48 return false; // there is no failure with the full set of passes/funcs!
49 }
50
Chris Lattner126840f2003-04-24 20:16:29 +000051 unsigned MidTop = TheList.size();
52 while (MidTop > 1) {
53 unsigned Mid = MidTop / 2;
Chris Lattner0c139982003-04-25 03:16:33 +000054 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
55 std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
Chris Lattner126840f2003-04-24 20:16:29 +000056
Chris Lattner0c139982003-04-25 03:16:33 +000057 switch (doTest(Prefix, Suffix)) {
Chris Lattner126840f2003-04-24 20:16:29 +000058 case KeepSuffix:
59 // The property still holds. We can just drop the prefix elements, and
60 // shorten the list to the "kept" elements.
Chris Lattner0c139982003-04-25 03:16:33 +000061 TheList.swap(Suffix);
Chris Lattner126840f2003-04-24 20:16:29 +000062 MidTop = TheList.size();
63 break;
64 case KeepPrefix:
65 // The predicate still holds, shorten the list to the prefix elements.
66 TheList.swap(Prefix);
67 MidTop = TheList.size();
68 break;
69 case NoFailure:
70 // Otherwise the property doesn't hold. Some of the elements we removed
71 // must be neccesary to maintain the property.
72 MidTop = Mid;
73 break;
74 }
75 }
76
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.
80 //
81 if (TheList.size() > 2) {
82 bool Changed = true;
83 std::vector<ElTy> EmptyList;
84 while (Changed) {
85 Changed = false;
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);
90
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
95 Changed = true;
96 }
97 }
98 }
99 }
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000100
101 return true; // there are some failure and we've narrowed them down
Chris Lattner126840f2003-04-24 20:16:29 +0000102 }
103};
104
105#endif