1 //===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef BUGPOINT_LIST_REDUCER_H
16 #define BUGPOINT_LIST_REDUCER_H
23 extern bool BugpointIsInterrupted;
25 template<typename ElTy>
28 NoFailure, // No failure of the predicate was detected
29 KeepSuffix, // The suffix alone satisfies the predicate
30 KeepPrefix // The prefix alone satisfies the predicate
33 virtual ~ListReducer() {}
35 // doTest - This virtual function should be overriden by subclasses to
36 // implement the test desired. The testcase is only required to test to see
37 // if the Kept list still satisfies the property, but if it is going to check
38 // the prefix anyway, it can.
40 virtual TestResult doTest(std::vector<ElTy> &Prefix,
41 std::vector<ElTy> &Kept) = 0;
43 // reduceList - This function attempts to reduce the length of the specified
44 // list while still maintaining the "test" property. This is the core of the
45 // "work" that bugpoint does.
47 bool reduceList(std::vector<ElTy> &TheList) {
48 std::vector<ElTy> empty;
49 switch (doTest(TheList, empty)) {
51 if (TheList.size() == 1) // we are done, it's the base case and it fails
54 break; // there's definitely an error, but we need to narrow it down
58 std::cerr << "bugpoint ListReducer internal error: selected empty set.\n";
62 return false; // there is no failure with the full set of passes/funcs!
65 unsigned MidTop = TheList.size();
67 // Halt if the user presses ctrl-c.
68 if (BugpointIsInterrupted) {
69 std::cerr << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
73 unsigned Mid = MidTop / 2;
74 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
75 std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
77 switch (doTest(Prefix, Suffix)) {
79 // The property still holds. We can just drop the prefix elements, and
80 // shorten the list to the "kept" elements.
82 MidTop = TheList.size();
85 // The predicate still holds, shorten the list to the prefix elements.
87 MidTop = TheList.size();
90 // Otherwise the property doesn't hold. Some of the elements we removed
91 // must be necessary to maintain the property.
97 // Okay, we trimmed as much off the top and the bottom of the list as we
98 // could. If there is more than two elements in the list, try deleting
99 // interior elements and testing that.
101 if (TheList.size() > 2) {
103 std::vector<ElTy> EmptyList;
106 for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts
107 if (BugpointIsInterrupted) {
108 std::cerr << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
112 std::vector<ElTy> TestList(TheList);
113 TestList.erase(TestList.begin()+i);
115 if (doTest(EmptyList, TestList) == KeepSuffix) {
116 // We can trim down the list!
117 TheList.swap(TestList);
118 --i; // Don't skip an element of the list
122 // This can take a long time if left uncontrolled. For now, don't
128 return true; // there are some failure and we've narrowed them down
132 } // End llvm namespace