Moved index into BB to common graph class because its needed by ModuloSchedGraph.
[oota-llvm.git] / tools / bugpoint / ListReducer.h
1 //===- 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
13 template<typename ElTy>
14 struct 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   //
26   virtual TestResult doTest(std::vector<ElTy> &Prefix,
27                             std::vector<ElTy> &Kept) = 0;
28
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   //
33   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
51     unsigned MidTop = TheList.size();
52     while (MidTop > 1) {
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());
56
57       switch (doTest(Prefix, Suffix)) {
58       case KeepSuffix:
59         // The property still holds.  We can just drop the prefix elements, and
60         // shorten the list to the "kept" elements.
61         TheList.swap(Suffix);
62         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 necessary 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     }
100
101     return true; // there are some failure and we've narrowed them down
102   }
103 };
104
105 #endif