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 | // |
| 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. |
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 | |
| 18 | #include <vector> |
Chris Lattner | 043b972 | 2003-11-29 20:04:13 +0000 | [diff] [blame] | 19 | #include <iostream> |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 20 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | namespace llvm { |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 22 | |
| 23 | extern bool BugpointIsInterrupted; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 25 | template<typename ElTy> |
| 26 | struct ListReducer { |
| 27 | enum TestResult { |
| 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 |
| 31 | }; |
| 32 | |
Chris Lattner | 1c2f686 | 2004-05-11 20:41:07 +0000 | [diff] [blame] | 33 | virtual ~ListReducer() {} |
| 34 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 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. |
| 39 | // |
Chris Lattner | b4ebe13 | 2003-04-24 22:23:56 +0000 | [diff] [blame] | 40 | virtual TestResult doTest(std::vector<ElTy> &Prefix, |
| 41 | std::vector<ElTy> &Kept) = 0; |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 42 | |
| 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. |
| 46 | // |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 47 | bool reduceList(std::vector<ElTy> &TheList) { |
| 48 | std::vector<ElTy> empty; |
| 49 | switch (doTest(TheList, empty)) { |
| 50 | case KeepPrefix: |
| 51 | if (TheList.size() == 1) // we are done, it's the base case and it fails |
| 52 | return true; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 53 | else |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 54 | break; // there's definitely an error, but we need to narrow it down |
| 55 | |
| 56 | case KeepSuffix: |
| 57 | // cannot be reached! |
| 58 | std::cerr << "bugpoint ListReducer internal error: selected empty set.\n"; |
| 59 | abort(); |
| 60 | |
| 61 | case NoFailure: |
| 62 | return false; // there is no failure with the full set of passes/funcs! |
| 63 | } |
| 64 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 65 | unsigned MidTop = TheList.size(); |
| 66 | while (MidTop > 1) { |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 67 | // Halt if the user presses ctrl-c. |
| 68 | if (BugpointIsInterrupted) { |
| 69 | std::cerr << "\n\n*** Reduction Interrupted, cleaning up...\n\n"; |
| 70 | return true; |
| 71 | } |
| 72 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 73 | unsigned Mid = MidTop / 2; |
Chris Lattner | 0c13998 | 2003-04-25 03:16:33 +0000 | [diff] [blame] | 74 | std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid); |
| 75 | std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end()); |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 0c13998 | 2003-04-25 03:16:33 +0000 | [diff] [blame] | 77 | switch (doTest(Prefix, Suffix)) { |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 78 | case KeepSuffix: |
| 79 | // The property still holds. We can just drop the prefix elements, and |
| 80 | // shorten the list to the "kept" elements. |
Chris Lattner | 0c13998 | 2003-04-25 03:16:33 +0000 | [diff] [blame] | 81 | TheList.swap(Suffix); |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 82 | MidTop = TheList.size(); |
| 83 | break; |
| 84 | case KeepPrefix: |
| 85 | // The predicate still holds, shorten the list to the prefix elements. |
| 86 | TheList.swap(Prefix); |
| 87 | MidTop = TheList.size(); |
| 88 | break; |
| 89 | case NoFailure: |
| 90 | // Otherwise the property doesn't hold. Some of the elements we removed |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 91 | // must be necessary to maintain the property. |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 92 | MidTop = Mid; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // 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^] | 98 | // could. If there is more than two elements in the list, try deleting |
| 99 | // interior elements and testing that. |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 100 | // |
Chris Lattner | d29c636 | 2004-11-18 19:42:50 +0000 | [diff] [blame] | 101 | if (TheList.size() > 2) { |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 102 | bool Changed = true; |
| 103 | std::vector<ElTy> EmptyList; |
| 104 | while (Changed) { |
| 105 | Changed = false; |
| 106 | std::vector<ElTy> TrimmedList; |
| 107 | for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 108 | if (BugpointIsInterrupted) { |
| 109 | std::cerr << "\n\n*** Reduction Interrupted, cleaning up...\n\n"; |
| 110 | return true; |
| 111 | } |
| 112 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 113 | std::vector<ElTy> TestList(TheList); |
| 114 | TestList.erase(TestList.begin()+i); |
| 115 | |
| 116 | if (doTest(EmptyList, TestList) == KeepSuffix) { |
| 117 | // We can trim down the list! |
| 118 | TheList.swap(TestList); |
| 119 | --i; // Don't skip an element of the list |
| 120 | Changed = true; |
| 121 | } |
| 122 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 123 | // 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] | 124 | // iterate. |
| 125 | break; |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 128 | |
| 129 | return true; // there are some failure and we've narrowed them down |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 130 | } |
| 131 | }; |
| 132 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 133 | } // End llvm namespace |
| 134 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 135 | #endif |