blob: 484b3b57934e8d19289950859a2a1a146e7cd8d6 [file] [log] [blame]
Chris Lattner126840f2003-04-24 20:16:29 +00001//===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner126840f2003-04-24 20:16:29 +00009//
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 Lattner043b9722003-11-29 20:04:13 +000019#include <iostream>
Chris Lattner126840f2003-04-24 20:16:29 +000020
Brian Gaeked0fde302003-11-11 22:41:34 +000021namespace llvm {
Chris Lattnerf9aaae02005-08-02 02:16:17 +000022
23 extern bool BugpointIsInterrupted;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner126840f2003-04-24 20:16:29 +000025template<typename ElTy>
26struct 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 Lattner1c2f6862004-05-11 20:41:07 +000033 virtual ~ListReducer() {}
34
Chris Lattner126840f2003-04-24 20:16:29 +000035 // 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 Lattnerb4ebe132003-04-24 22:23:56 +000040 virtual TestResult doTest(std::vector<ElTy> &Prefix,
41 std::vector<ElTy> &Kept) = 0;
Chris Lattner126840f2003-04-24 20:16:29 +000042
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 Brukmanbe6bf562003-07-30 20:15:56 +000047 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 Brukman3da94ae2005-04-22 00:00:37 +000053 else
Misha Brukmanbe6bf562003-07-30 20:15:56 +000054 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 Lattner126840f2003-04-24 20:16:29 +000065 unsigned MidTop = TheList.size();
66 while (MidTop > 1) {
Chris Lattnerf9aaae02005-08-02 02:16:17 +000067 // 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 Lattner126840f2003-04-24 20:16:29 +000073 unsigned Mid = MidTop / 2;
Chris Lattner0c139982003-04-25 03:16:33 +000074 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
75 std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
Chris Lattner126840f2003-04-24 20:16:29 +000076
Chris Lattner0c139982003-04-25 03:16:33 +000077 switch (doTest(Prefix, Suffix)) {
Chris Lattner126840f2003-04-24 20:16:29 +000078 case KeepSuffix:
79 // The property still holds. We can just drop the prefix elements, and
80 // shorten the list to the "kept" elements.
Chris Lattner0c139982003-04-25 03:16:33 +000081 TheList.swap(Suffix);
Chris Lattner126840f2003-04-24 20:16:29 +000082 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 Brukman5560c9d2003-08-18 14:43:39 +000091 // must be necessary to maintain the property.
Chris Lattner126840f2003-04-24 20:16:29 +000092 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 Spencerde83cee2006-01-08 22:40:10 +000098 // could. If there is more than two elements in the list, try deleting
99 // interior elements and testing that.
Chris Lattner126840f2003-04-24 20:16:29 +0000100 //
Chris Lattnerd29c6362004-11-18 19:42:50 +0000101 if (TheList.size() > 2) {
Chris Lattner126840f2003-04-24 20:16:29 +0000102 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 Lattnerf9aaae02005-08-02 02:16:17 +0000108 if (BugpointIsInterrupted) {
109 std::cerr << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
110 return true;
111 }
112
Chris Lattner126840f2003-04-24 20:16:29 +0000113 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 Brukman3da94ae2005-04-22 00:00:37 +0000123 // This can take a long time if left uncontrolled. For now, don't
Chris Lattnerd29c6362004-11-18 19:42:50 +0000124 // iterate.
125 break;
Chris Lattner126840f2003-04-24 20:16:29 +0000126 }
127 }
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000128
129 return true; // there are some failure and we've narrowed them down
Chris Lattner126840f2003-04-24 20:16:29 +0000130 }
131};
132
Brian Gaeked0fde302003-11-11 22:41:34 +0000133} // End llvm namespace
134
Chris Lattner126840f2003-04-24 20:16:29 +0000135#endif