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
25 extern bool BugpointIsInterrupted;
27 template<typename ElTy>
30 NoFailure, // No failure of the predicate was detected
31 KeepSuffix, // The suffix alone satisfies the predicate
32 KeepPrefix // The prefix alone satisfies the predicate
35 virtual ~ListReducer() {}
37 // doTest - This virtual function should be overriden by subclasses to
38 // implement the test desired. The testcase is only required to test to see
39 // if the Kept list still satisfies the property, but if it is going to check
40 // the prefix anyway, it can.
42 virtual TestResult doTest(std::vector<ElTy> &Prefix,
43 std::vector<ElTy> &Kept) = 0;
45 // reduceList - This function attempts to reduce the length of the specified
46 // list while still maintaining the "test" property. This is the core of the
47 // "work" that bugpoint does.
49 bool reduceList(std::vector<ElTy> &TheList) {
50 std::vector<ElTy> empty;
51 std::srand(0x6e5ea738); // Seed the random number generator
52 switch (doTest(TheList, empty)) {
54 if (TheList.size() == 1) // we are done, it's the base case and it fails
57 break; // there's definitely an error, but we need to narrow it down
61 std::cerr << "bugpoint ListReducer internal error: selected empty set.\n";
65 return false; // there is no failure with the full set of passes/funcs!
68 // Maximal number of allowed splitting iterations,
69 // before the elements are randomly shuffled.
70 const unsigned MaxIterationsWithoutProgress = 3;
71 bool ShufflingEnabled = true;
74 unsigned MidTop = TheList.size();
75 unsigned MaxIterations = MaxIterationsWithoutProgress;
76 unsigned NumOfIterationsWithoutProgress = 0;
77 while (MidTop > 1) { // Binary split reduction loop
78 // Halt if the user presses ctrl-c.
79 if (BugpointIsInterrupted) {
80 std::cerr << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
84 // If the loop doesn't make satisfying progress, try shuffling.
85 // The purpose of shuffling is to avoid the heavy tails of the
86 // distribution (improving the speed of convergence).
87 if (ShufflingEnabled &&
88 NumOfIterationsWithoutProgress > MaxIterations) {
90 std::vector<ElTy> ShuffledList(TheList);
91 std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
92 std::cerr << "\n\n*** Testing shuffled set...\n\n";
93 // Check that random shuffle doesn't loose the bug
94 if (doTest(ShuffledList, empty) == KeepPrefix) {
95 // If the bug is still here, use the shuffled list.
96 TheList.swap(ShuffledList);
97 MidTop = TheList.size();
98 // Must increase the shuffling treshold to avoid the small
99 // probability of inifinite looping without making progress.
101 std::cerr << "\n\n*** Shuffling does not hide the bug...\n\n";
103 ShufflingEnabled = false; // Disable shuffling further on
104 std::cerr << "\n\n*** Shuffling hides the bug...\n\n";
106 NumOfIterationsWithoutProgress = 0;
109 unsigned Mid = MidTop / 2;
110 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
111 std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
113 switch (doTest(Prefix, Suffix)) {
115 // The property still holds. We can just drop the prefix elements, and
116 // shorten the list to the "kept" elements.
117 TheList.swap(Suffix);
118 MidTop = TheList.size();
119 // Reset progress treshold and progress counter
120 MaxIterations = MaxIterationsWithoutProgress;
121 NumOfIterationsWithoutProgress = 0;
124 // The predicate still holds, shorten the list to the prefix elements.
125 TheList.swap(Prefix);
126 MidTop = TheList.size();
127 // Reset progress treshold and progress counter
128 MaxIterations = MaxIterationsWithoutProgress;
129 NumOfIterationsWithoutProgress = 0;
132 // Otherwise the property doesn't hold. Some of the elements we removed
133 // must be necessary to maintain the property.
135 NumOfIterationsWithoutProgress++;
140 // Probability of backjumping from the trimming loop back to the binary
141 // split reduction loop.
142 const int BackjumpProbability = 10;
144 // Okay, we trimmed as much off the top and the bottom of the list as we
145 // could. If there is more than two elements in the list, try deleting
146 // interior elements and testing that.
148 if (TheList.size() > 2) {
150 std::vector<ElTy> EmptyList;
151 while (Changed) { // Trimming loop.
154 // If the binary split reduction loop made an unfortunate sequence of
155 // splits, the trimming loop might be left off with a huge number of
156 // remaining elements (large search space). Backjumping out of that
157 // search space and attempting a different split can significantly
158 // improve the convergence speed.
159 if (std::rand() % 100 < BackjumpProbability)
162 for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts
163 if (BugpointIsInterrupted) {
164 std::cerr << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
168 std::vector<ElTy> TestList(TheList);
169 TestList.erase(TestList.begin()+i);
171 if (doTest(EmptyList, TestList) == KeepSuffix) {
172 // We can trim down the list!
173 TheList.swap(TestList);
174 --i; // Don't skip an element of the list
178 // This can take a long time if left uncontrolled. For now, don't
184 return true; // there are some failure and we've narrowed them down
188 } // End llvm namespace