blob: f08bc97a9988ae1c769fe23cb91c2bd655713a88 [file] [log] [blame]
Chris Lattner9f5a1972003-04-24 20:16:29 +00001//===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner9f5a1972003-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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_TOOLS_BUGPOINT_LISTREDUCER_H
16#define LLVM_TOOLS_BUGPOINT_LISTREDUCER_H
Chris Lattner9f5a1972003-04-24 20:16:29 +000017
Nick Lewycky6ba630b2010-04-12 05:08:25 +000018#include "llvm/Support/ErrorHandling.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner8adfe922006-10-10 21:42:25 +000020#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000021#include <cstdlib>
22#include <vector>
Chris Lattner9f5a1972003-04-24 20:16:29 +000023
Brian Gaeke960707c2003-11-11 22:41:34 +000024namespace llvm {
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000025
26 extern bool BugpointIsInterrupted;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner9f5a1972003-04-24 20:16:29 +000028template<typename ElTy>
29struct ListReducer {
30 enum TestResult {
31 NoFailure, // No failure of the predicate was detected
32 KeepSuffix, // The suffix alone satisfies the predicate
Nick Lewycky6ba630b2010-04-12 05:08:25 +000033 KeepPrefix, // The prefix alone satisfies the predicate
34 InternalError // Encountered an error trying to run the predicate
Chris Lattner9f5a1972003-04-24 20:16:29 +000035 };
36
Chris Lattnerd7d574d2004-05-11 20:41:07 +000037 virtual ~ListReducer() {}
38
Chris Lattner9f5a1972003-04-24 20:16:29 +000039 // 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 Lattner69f8e6f2003-04-24 22:23:56 +000044 virtual TestResult doTest(std::vector<ElTy> &Prefix,
Nick Lewycky6ba630b2010-04-12 05:08:25 +000045 std::vector<ElTy> &Kept,
46 std::string &Error) = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +000047
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 Lewycky6ba630b2010-04-12 05:08:25 +000052 bool reduceList(std::vector<ElTy> &TheList, std::string &Error) {
Misha Brukman87154fd2003-07-30 20:15:56 +000053 std::vector<ElTy> empty;
Chris Lattner8adfe922006-10-10 21:42:25 +000054 std::srand(0x6e5ea738); // Seed the random number generator
Nick Lewycky6ba630b2010-04-12 05:08:25 +000055 switch (doTest(TheList, empty, Error)) {
Misha Brukman87154fd2003-07-30 20:15:56 +000056 case KeepPrefix:
57 if (TheList.size() == 1) // we are done, it's the base case and it fails
58 return true;
Misha Brukman650ba8e2005-04-22 00:00:37 +000059 else
Misha Brukman87154fd2003-07-30 20:15:56 +000060 break; // there's definitely an error, but we need to narrow it down
61
62 case KeepSuffix:
63 // cannot be reached!
Nick Lewycky6ba630b2010-04-12 05:08:25 +000064 llvm_unreachable("bugpoint ListReducer internal error: "
65 "selected empty set.");
Misha Brukman87154fd2003-07-30 20:15:56 +000066
67 case NoFailure:
68 return false; // there is no failure with the full set of passes/funcs!
Nick Lewycky6ba630b2010-04-12 05:08:25 +000069
70 case InternalError:
71 assert(!Error.empty());
72 return true;
Misha Brukman87154fd2003-07-30 20:15:56 +000073 }
74
Chris Lattner8adfe922006-10-10 21:42:25 +000075 // Maximal number of allowed splitting iterations,
76 // before the elements are randomly shuffled.
77 const unsigned MaxIterationsWithoutProgress = 3;
Tobias Grosser56eab362015-07-26 15:18:45 +000078
79 // Maximal number of allowed single-element trim iterations. We add a
80 // threshhold here as single-element reductions may otherwise take a
81 // very long time to complete.
82 const unsigned MaxTrimIterationsWithoutBackJump = 3;
Chris Lattner8adfe922006-10-10 21:42:25 +000083 bool ShufflingEnabled = true;
84
85Backjump:
Chris Lattner9f5a1972003-04-24 20:16:29 +000086 unsigned MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +000087 unsigned MaxIterations = MaxIterationsWithoutProgress;
88 unsigned NumOfIterationsWithoutProgress = 0;
89 while (MidTop > 1) { // Binary split reduction loop
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000090 // Halt if the user presses ctrl-c.
91 if (BugpointIsInterrupted) {
Dan Gohmand8db3762009-07-15 16:35:29 +000092 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000093 return true;
94 }
Bill Wendling8ff43202008-02-26 10:46:10 +000095
Chris Lattner8adfe922006-10-10 21:42:25 +000096 // If the loop doesn't make satisfying progress, try shuffling.
97 // The purpose of shuffling is to avoid the heavy tails of the
98 // distribution (improving the speed of convergence).
99 if (ShufflingEnabled &&
Bill Wendling8ff43202008-02-26 10:46:10 +0000100 NumOfIterationsWithoutProgress > MaxIterations) {
101 std::vector<ElTy> ShuffledList(TheList);
102 std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
Dan Gohmand8db3762009-07-15 16:35:29 +0000103 errs() << "\n\n*** Testing shuffled set...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +0000104 // Check that random shuffle doesn't loose the bug
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000105 if (doTest(ShuffledList, empty, Error) == KeepPrefix) {
Chris Lattner8adfe922006-10-10 21:42:25 +0000106 // If the bug is still here, use the shuffled list.
107 TheList.swap(ShuffledList);
108 MidTop = TheList.size();
109 // Must increase the shuffling treshold to avoid the small
110 // probability of inifinite looping without making progress.
111 MaxIterations += 2;
Dan Gohmand8db3762009-07-15 16:35:29 +0000112 errs() << "\n\n*** Shuffling does not hide the bug...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +0000113 } else {
Chris Lattner8adfe922006-10-10 21:42:25 +0000114 ShufflingEnabled = false; // Disable shuffling further on
Dan Gohmand8db3762009-07-15 16:35:29 +0000115 errs() << "\n\n*** Shuffling hides the bug...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +0000116 }
117 NumOfIterationsWithoutProgress = 0;
Chris Lattner8adfe922006-10-10 21:42:25 +0000118 }
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000119
Chris Lattner9f5a1972003-04-24 20:16:29 +0000120 unsigned Mid = MidTop / 2;
Chris Lattner0347cda2003-04-25 03:16:33 +0000121 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
122 std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
Chris Lattner9f5a1972003-04-24 20:16:29 +0000123
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000124 switch (doTest(Prefix, Suffix, Error)) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000125 case KeepSuffix:
126 // The property still holds. We can just drop the prefix elements, and
127 // shorten the list to the "kept" elements.
Chris Lattner0347cda2003-04-25 03:16:33 +0000128 TheList.swap(Suffix);
Chris Lattner9f5a1972003-04-24 20:16:29 +0000129 MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +0000130 // Reset progress treshold and progress counter
131 MaxIterations = MaxIterationsWithoutProgress;
132 NumOfIterationsWithoutProgress = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000133 break;
134 case KeepPrefix:
135 // The predicate still holds, shorten the list to the prefix elements.
136 TheList.swap(Prefix);
137 MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +0000138 // Reset progress treshold and progress counter
139 MaxIterations = MaxIterationsWithoutProgress;
140 NumOfIterationsWithoutProgress = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000141 break;
142 case NoFailure:
143 // Otherwise the property doesn't hold. Some of the elements we removed
Misha Brukman7eb05a12003-08-18 14:43:39 +0000144 // must be necessary to maintain the property.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000145 MidTop = Mid;
Chris Lattner8adfe922006-10-10 21:42:25 +0000146 NumOfIterationsWithoutProgress++;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000147 break;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000148 case InternalError:
149 return true; // Error was set by doTest.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000150 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000151 assert(Error.empty() && "doTest did not return InternalError for error");
Chris Lattner9f5a1972003-04-24 20:16:29 +0000152 }
153
Chris Lattner8adfe922006-10-10 21:42:25 +0000154 // Probability of backjumping from the trimming loop back to the binary
155 // split reduction loop.
156 const int BackjumpProbability = 10;
157
Chris Lattner9f5a1972003-04-24 20:16:29 +0000158 // Okay, we trimmed as much off the top and the bottom of the list as we
Reid Spencer9d59a822006-01-08 22:40:10 +0000159 // could. If there is more than two elements in the list, try deleting
160 // interior elements and testing that.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000161 //
Chris Lattner7fd27e22004-11-18 19:42:50 +0000162 if (TheList.size() > 2) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000163 bool Changed = true;
164 std::vector<ElTy> EmptyList;
Tobias Grosser56eab362015-07-26 15:18:45 +0000165 unsigned TrimIterations = 0;
Chris Lattner8adfe922006-10-10 21:42:25 +0000166 while (Changed) { // Trimming loop.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000167 Changed = false;
Chris Lattner8adfe922006-10-10 21:42:25 +0000168
169 // If the binary split reduction loop made an unfortunate sequence of
170 // splits, the trimming loop might be left off with a huge number of
171 // remaining elements (large search space). Backjumping out of that
172 // search space and attempting a different split can significantly
173 // improve the convergence speed.
174 if (std::rand() % 100 < BackjumpProbability)
175 goto Backjump;
176
Chris Lattner9f5a1972003-04-24 20:16:29 +0000177 for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000178 if (BugpointIsInterrupted) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000179 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000180 return true;
181 }
182
Chris Lattner9f5a1972003-04-24 20:16:29 +0000183 std::vector<ElTy> TestList(TheList);
184 TestList.erase(TestList.begin()+i);
185
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000186 if (doTest(EmptyList, TestList, Error) == KeepSuffix) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000187 // We can trim down the list!
188 TheList.swap(TestList);
189 --i; // Don't skip an element of the list
190 Changed = true;
191 }
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000192 if (!Error.empty())
193 return true;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000194 }
Tobias Grosser56eab362015-07-26 15:18:45 +0000195 if (TrimIterations >= MaxTrimIterationsWithoutBackJump)
196 break;
197 TrimIterations++;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000198 }
199 }
Misha Brukman87154fd2003-07-30 20:15:56 +0000200
201 return true; // there are some failure and we've narrowed them down
Chris Lattner9f5a1972003-04-24 20:16:29 +0000202 }
203};
204
Brian Gaeke960707c2003-11-11 22:41:34 +0000205} // End llvm namespace
206
Chris Lattner9f5a1972003-04-24 20:16:29 +0000207#endif