Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 1 | //===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
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 { |
| 22 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 23 | template<typename ElTy> |
| 24 | struct ListReducer { |
| 25 | enum TestResult { |
| 26 | NoFailure, // No failure of the predicate was detected |
| 27 | KeepSuffix, // The suffix alone satisfies the predicate |
| 28 | KeepPrefix, // The prefix alone satisfies the predicate |
| 29 | }; |
| 30 | |
| 31 | // doTest - This virtual function should be overriden by subclasses to |
| 32 | // implement the test desired. The testcase is only required to test to see |
| 33 | // if the Kept list still satisfies the property, but if it is going to check |
| 34 | // the prefix anyway, it can. |
| 35 | // |
Chris Lattner | b4ebe13 | 2003-04-24 22:23:56 +0000 | [diff] [blame] | 36 | virtual TestResult doTest(std::vector<ElTy> &Prefix, |
| 37 | std::vector<ElTy> &Kept) = 0; |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 38 | |
| 39 | // reduceList - This function attempts to reduce the length of the specified |
| 40 | // list while still maintaining the "test" property. This is the core of the |
| 41 | // "work" that bugpoint does. |
| 42 | // |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 43 | bool reduceList(std::vector<ElTy> &TheList) { |
| 44 | std::vector<ElTy> empty; |
| 45 | switch (doTest(TheList, empty)) { |
| 46 | case KeepPrefix: |
| 47 | if (TheList.size() == 1) // we are done, it's the base case and it fails |
| 48 | return true; |
| 49 | else |
| 50 | break; // there's definitely an error, but we need to narrow it down |
| 51 | |
| 52 | case KeepSuffix: |
| 53 | // cannot be reached! |
| 54 | std::cerr << "bugpoint ListReducer internal error: selected empty set.\n"; |
| 55 | abort(); |
| 56 | |
| 57 | case NoFailure: |
| 58 | return false; // there is no failure with the full set of passes/funcs! |
| 59 | } |
| 60 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 61 | unsigned MidTop = TheList.size(); |
| 62 | while (MidTop > 1) { |
| 63 | unsigned Mid = MidTop / 2; |
Chris Lattner | 0c13998 | 2003-04-25 03:16:33 +0000 | [diff] [blame] | 64 | std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid); |
| 65 | std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end()); |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 0c13998 | 2003-04-25 03:16:33 +0000 | [diff] [blame] | 67 | switch (doTest(Prefix, Suffix)) { |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 68 | case KeepSuffix: |
| 69 | // The property still holds. We can just drop the prefix elements, and |
| 70 | // shorten the list to the "kept" elements. |
Chris Lattner | 0c13998 | 2003-04-25 03:16:33 +0000 | [diff] [blame] | 71 | TheList.swap(Suffix); |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 72 | MidTop = TheList.size(); |
| 73 | break; |
| 74 | case KeepPrefix: |
| 75 | // The predicate still holds, shorten the list to the prefix elements. |
| 76 | TheList.swap(Prefix); |
| 77 | MidTop = TheList.size(); |
| 78 | break; |
| 79 | case NoFailure: |
| 80 | // Otherwise the property doesn't hold. Some of the elements we removed |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 81 | // must be necessary to maintain the property. |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 82 | MidTop = Mid; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Okay, we trimmed as much off the top and the bottom of the list as we |
| 88 | // could. If there is more two elements in the list, try deleting interior |
| 89 | // elements and testing that. |
| 90 | // |
| 91 | if (TheList.size() > 2) { |
| 92 | bool Changed = true; |
| 93 | std::vector<ElTy> EmptyList; |
| 94 | while (Changed) { |
| 95 | Changed = false; |
| 96 | std::vector<ElTy> TrimmedList; |
| 97 | for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts |
| 98 | std::vector<ElTy> TestList(TheList); |
| 99 | TestList.erase(TestList.begin()+i); |
| 100 | |
| 101 | if (doTest(EmptyList, TestList) == KeepSuffix) { |
| 102 | // We can trim down the list! |
| 103 | TheList.swap(TestList); |
| 104 | --i; // Don't skip an element of the list |
| 105 | Changed = true; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 110 | |
| 111 | return true; // there are some failure and we've narrowed them down |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 112 | } |
| 113 | }; |
| 114 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 115 | } // End llvm namespace |
| 116 | |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 117 | #endif |