Do not crash when dealing with invoke and unwind instructions!
[oota-llvm.git] / tools / bugpoint / ListReducer.h
1 //===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This class is to be used as a base class for operations that want to zero in
11 // on a subset of the input which still causes the bug we are tracking.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef BUGPOINT_LIST_REDUCER_H
16 #define BUGPOINT_LIST_REDUCER_H
17
18 #include <vector>
19
20 namespace llvm {
21
22 template<typename ElTy>
23 struct ListReducer {
24   enum TestResult {
25     NoFailure,         // No failure of the predicate was detected
26     KeepSuffix,        // The suffix alone satisfies the predicate
27     KeepPrefix,        // The prefix alone satisfies the predicate
28   };
29
30   // doTest - This virtual function should be overriden by subclasses to
31   // implement the test desired.  The testcase is only required to test to see
32   // if the Kept list still satisfies the property, but if it is going to check
33   // the prefix anyway, it can.
34   //
35   virtual TestResult doTest(std::vector<ElTy> &Prefix,
36                             std::vector<ElTy> &Kept) = 0;
37
38   // reduceList - This function attempts to reduce the length of the specified
39   // list while still maintaining the "test" property.  This is the core of the
40   // "work" that bugpoint does.
41   //
42   bool reduceList(std::vector<ElTy> &TheList) {
43     std::vector<ElTy> empty;
44     switch (doTest(TheList, empty)) {
45     case KeepPrefix:
46       if (TheList.size() == 1) // we are done, it's the base case and it fails
47         return true;
48       else 
49         break; // there's definitely an error, but we need to narrow it down
50
51     case KeepSuffix:
52       // cannot be reached!
53       std::cerr << "bugpoint ListReducer internal error: selected empty set.\n";
54       abort();
55
56     case NoFailure:
57       return false; // there is no failure with the full set of passes/funcs!
58     }
59
60     unsigned MidTop = TheList.size();
61     while (MidTop > 1) {
62       unsigned Mid = MidTop / 2;
63       std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
64       std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
65
66       switch (doTest(Prefix, Suffix)) {
67       case KeepSuffix:
68         // The property still holds.  We can just drop the prefix elements, and
69         // shorten the list to the "kept" elements.
70         TheList.swap(Suffix);
71         MidTop = TheList.size();
72         break;
73       case KeepPrefix:
74         // The predicate still holds, shorten the list to the prefix elements.
75         TheList.swap(Prefix);
76         MidTop = TheList.size();
77         break;
78       case NoFailure:
79         // Otherwise the property doesn't hold.  Some of the elements we removed
80         // must be necessary to maintain the property.
81         MidTop = Mid;
82         break;
83       }
84     }
85
86     // Okay, we trimmed as much off the top and the bottom of the list as we
87     // could.  If there is more two elements in the list, try deleting interior
88     // elements and testing that.
89     //
90     if (TheList.size() > 2) {
91       bool Changed = true;
92       std::vector<ElTy> EmptyList;
93       while (Changed) {
94         Changed = false;
95         std::vector<ElTy> TrimmedList;
96         for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts
97           std::vector<ElTy> TestList(TheList);
98           TestList.erase(TestList.begin()+i);
99
100           if (doTest(EmptyList, TestList) == KeepSuffix) {
101             // We can trim down the list!
102             TheList.swap(TestList);
103             --i;  // Don't skip an element of the list
104             Changed = true;
105           }
106         }
107       }
108     }
109
110     return true; // there are some failure and we've narrowed them down
111   }
112 };
113
114 } // End llvm namespace
115
116 #endif