Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 1 | //===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 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 | |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 20 | #include <vector> |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 21 | #include <cstdlib> |
| 22 | #include <algorithm> |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 23 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | namespace llvm { |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 25 | |
| 26 | extern bool BugpointIsInterrupted; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 28 | template<typename ElTy> |
| 29 | struct ListReducer { |
| 30 | enum TestResult { |
| 31 | NoFailure, // No failure of the predicate was detected |
| 32 | KeepSuffix, // The suffix alone satisfies the predicate |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 33 | KeepPrefix, // The prefix alone satisfies the predicate |
| 34 | InternalError // Encountered an error trying to run the predicate |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Chris Lattner | 1c2f686 | 2004-05-11 20:41:07 +0000 | [diff] [blame] | 37 | virtual ~ListReducer() {} |
| 38 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 39 | // doTest - This virtual function should be overriden by subclasses to |
| 40 | // implement the test desired. The testcase is only required to test to see |
| 41 | // if the Kept list still satisfies the property, but if it is going to check |
| 42 | // the prefix anyway, it can. |
| 43 | // |
Chris Lattner | b4ebe13 | 2003-04-24 22:23:56 +0000 | [diff] [blame] | 44 | virtual TestResult doTest(std::vector<ElTy> &Prefix, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 45 | std::vector<ElTy> &Kept, |
| 46 | std::string &Error) = 0; |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 47 | |
| 48 | // reduceList - This function attempts to reduce the length of the specified |
| 49 | // list while still maintaining the "test" property. This is the core of the |
| 50 | // "work" that bugpoint does. |
| 51 | // |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 52 | bool reduceList(std::vector<ElTy> &TheList, std::string &Error) { |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 53 | std::vector<ElTy> empty; |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 54 | std::srand(0x6e5ea738); // Seed the random number generator |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 55 | switch (doTest(TheList, empty, Error)) { |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 56 | case KeepPrefix: |
| 57 | if (TheList.size() == 1) // we are done, it's the base case and it fails |
| 58 | return true; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 59 | else |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 60 | break; // there's definitely an error, but we need to narrow it down |
| 61 | |
| 62 | case KeepSuffix: |
| 63 | // cannot be reached! |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 64 | llvm_unreachable("bugpoint ListReducer internal error: " |
| 65 | "selected empty set."); |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 66 | |
| 67 | case NoFailure: |
| 68 | return false; // there is no failure with the full set of passes/funcs! |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 69 | |
| 70 | case InternalError: |
| 71 | assert(!Error.empty()); |
| 72 | return true; |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 75 | // Maximal number of allowed splitting iterations, |
| 76 | // before the elements are randomly shuffled. |
| 77 | const unsigned MaxIterationsWithoutProgress = 3; |
| 78 | bool ShufflingEnabled = true; |
| 79 | |
| 80 | Backjump: |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 81 | unsigned MidTop = TheList.size(); |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 82 | unsigned MaxIterations = MaxIterationsWithoutProgress; |
| 83 | unsigned NumOfIterationsWithoutProgress = 0; |
| 84 | while (MidTop > 1) { // Binary split reduction loop |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 85 | // Halt if the user presses ctrl-c. |
| 86 | if (BugpointIsInterrupted) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 87 | errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n"; |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 88 | return true; |
| 89 | } |
Bill Wendling | 584c3bf | 2008-02-26 10:46:10 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 91 | // If the loop doesn't make satisfying progress, try shuffling. |
| 92 | // The purpose of shuffling is to avoid the heavy tails of the |
| 93 | // distribution (improving the speed of convergence). |
| 94 | if (ShufflingEnabled && |
Bill Wendling | 584c3bf | 2008-02-26 10:46:10 +0000 | [diff] [blame] | 95 | NumOfIterationsWithoutProgress > MaxIterations) { |
| 96 | std::vector<ElTy> ShuffledList(TheList); |
| 97 | std::random_shuffle(ShuffledList.begin(), ShuffledList.end()); |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 98 | errs() << "\n\n*** Testing shuffled set...\n\n"; |
Bill Wendling | 584c3bf | 2008-02-26 10:46:10 +0000 | [diff] [blame] | 99 | // Check that random shuffle doesn't loose the bug |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 100 | if (doTest(ShuffledList, empty, Error) == KeepPrefix) { |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 101 | // If the bug is still here, use the shuffled list. |
| 102 | TheList.swap(ShuffledList); |
| 103 | MidTop = TheList.size(); |
| 104 | // Must increase the shuffling treshold to avoid the small |
| 105 | // probability of inifinite looping without making progress. |
| 106 | MaxIterations += 2; |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 107 | errs() << "\n\n*** Shuffling does not hide the bug...\n\n"; |
Bill Wendling | 584c3bf | 2008-02-26 10:46:10 +0000 | [diff] [blame] | 108 | } else { |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 109 | ShufflingEnabled = false; // Disable shuffling further on |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 110 | errs() << "\n\n*** Shuffling hides the bug...\n\n"; |
Bill Wendling | 584c3bf | 2008-02-26 10:46:10 +0000 | [diff] [blame] | 111 | } |
| 112 | NumOfIterationsWithoutProgress = 0; |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 113 | } |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 115 | unsigned Mid = MidTop / 2; |
Chris Lattner | 0c13998 | 2003-04-25 03:16:33 +0000 | [diff] [blame] | 116 | std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid); |
| 117 | std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end()); |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 118 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 119 | switch (doTest(Prefix, Suffix, Error)) { |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 120 | case KeepSuffix: |
| 121 | // The property still holds. We can just drop the prefix elements, and |
| 122 | // shorten the list to the "kept" elements. |
Chris Lattner | 0c13998 | 2003-04-25 03:16:33 +0000 | [diff] [blame] | 123 | TheList.swap(Suffix); |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 124 | MidTop = TheList.size(); |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 125 | // Reset progress treshold and progress counter |
| 126 | MaxIterations = MaxIterationsWithoutProgress; |
| 127 | NumOfIterationsWithoutProgress = 0; |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 128 | break; |
| 129 | case KeepPrefix: |
| 130 | // The predicate still holds, shorten the list to the prefix elements. |
| 131 | TheList.swap(Prefix); |
| 132 | MidTop = TheList.size(); |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 133 | // Reset progress treshold and progress counter |
| 134 | MaxIterations = MaxIterationsWithoutProgress; |
| 135 | NumOfIterationsWithoutProgress = 0; |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 136 | break; |
| 137 | case NoFailure: |
| 138 | // Otherwise the property doesn't hold. Some of the elements we removed |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 139 | // must be necessary to maintain the property. |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 140 | MidTop = Mid; |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 141 | NumOfIterationsWithoutProgress++; |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 142 | break; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 143 | case InternalError: |
| 144 | return true; // Error was set by doTest. |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 145 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 146 | assert(Error.empty() && "doTest did not return InternalError for error"); |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 149 | // Probability of backjumping from the trimming loop back to the binary |
| 150 | // split reduction loop. |
| 151 | const int BackjumpProbability = 10; |
| 152 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 153 | // Okay, we trimmed as much off the top and the bottom of the list as we |
Reid Spencer | de83cee | 2006-01-08 22:40:10 +0000 | [diff] [blame] | 154 | // could. If there is more than two elements in the list, try deleting |
| 155 | // interior elements and testing that. |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 156 | // |
Chris Lattner | d29c636 | 2004-11-18 19:42:50 +0000 | [diff] [blame] | 157 | if (TheList.size() > 2) { |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 158 | bool Changed = true; |
| 159 | std::vector<ElTy> EmptyList; |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 160 | while (Changed) { // Trimming loop. |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 161 | Changed = false; |
Chris Lattner | 137d0ec | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 162 | |
| 163 | // If the binary split reduction loop made an unfortunate sequence of |
| 164 | // splits, the trimming loop might be left off with a huge number of |
| 165 | // remaining elements (large search space). Backjumping out of that |
| 166 | // search space and attempting a different split can significantly |
| 167 | // improve the convergence speed. |
| 168 | if (std::rand() % 100 < BackjumpProbability) |
| 169 | goto Backjump; |
| 170 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 171 | for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 172 | if (BugpointIsInterrupted) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 173 | errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n"; |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 177 | std::vector<ElTy> TestList(TheList); |
| 178 | TestList.erase(TestList.begin()+i); |
| 179 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 180 | if (doTest(EmptyList, TestList, Error) == KeepSuffix) { |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 181 | // We can trim down the list! |
| 182 | TheList.swap(TestList); |
| 183 | --i; // Don't skip an element of the list |
| 184 | Changed = true; |
| 185 | } |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame^] | 186 | if (!Error.empty()) |
| 187 | return true; |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 188 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 189 | // This can take a long time if left uncontrolled. For now, don't |
Chris Lattner | d29c636 | 2004-11-18 19:42:50 +0000 | [diff] [blame] | 190 | // iterate. |
| 191 | break; |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 194 | |
| 195 | return true; // there are some failure and we've narrowed them down |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 196 | } |
| 197 | }; |
| 198 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 199 | } // End llvm namespace |
| 200 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 201 | #endif |