blob: 78af1aeb6a27b5105b623a858b0124ab0a142175 [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 {
25
Justin Bogner06d466a2016-09-01 21:04:36 +000026extern bool BugpointIsInterrupted;
27
28template <typename ElTy> struct ListReducer {
Chris Lattner9f5a1972003-04-24 20:16:29 +000029 enum TestResult {
Justin Bogner06d466a2016-09-01 21:04:36 +000030 NoFailure, // No failure of the predicate was detected
31 KeepSuffix, // The suffix alone satisfies the predicate
32 KeepPrefix, // The prefix alone satisfies the predicate
33 InternalError // Encountered an error trying to run the predicate
Chris Lattner9f5a1972003-04-24 20:16:29 +000034 };
35
Chris Lattnerd7d574d2004-05-11 20:41:07 +000036 virtual ~ListReducer() {}
37
Justin Bogner06d466a2016-09-01 21:04:36 +000038 /// This virtual function should be overriden by subclasses to implement the
39 /// test desired. The testcase is only required to test to see if the Kept
40 /// list still satisfies the property, but if it is going to check the prefix
41 /// anyway, it can.
42 virtual TestResult doTest(std::vector<ElTy> &Prefix, std::vector<ElTy> &Kept,
Nick Lewycky6ba630b2010-04-12 05:08:25 +000043 std::string &Error) = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +000044
Justin Bogner06d466a2016-09-01 21:04:36 +000045 /// This function attempts to reduce the length of the specified list while
46 /// still maintaining the "test" property. This is the core of the "work"
47 /// that bugpoint does.
Nick Lewycky6ba630b2010-04-12 05:08:25 +000048 bool reduceList(std::vector<ElTy> &TheList, std::string &Error) {
Misha Brukman87154fd2003-07-30 20:15:56 +000049 std::vector<ElTy> empty;
Chris Lattner8adfe922006-10-10 21:42:25 +000050 std::srand(0x6e5ea738); // Seed the random number generator
Nick Lewycky6ba630b2010-04-12 05:08:25 +000051 switch (doTest(TheList, empty, Error)) {
Misha Brukman87154fd2003-07-30 20:15:56 +000052 case KeepPrefix:
53 if (TheList.size() == 1) // we are done, it's the base case and it fails
54 return true;
Misha Brukman650ba8e2005-04-22 00:00:37 +000055 else
Misha Brukman87154fd2003-07-30 20:15:56 +000056 break; // there's definitely an error, but we need to narrow it down
57
58 case KeepSuffix:
59 // cannot be reached!
Nick Lewycky6ba630b2010-04-12 05:08:25 +000060 llvm_unreachable("bugpoint ListReducer internal error: "
61 "selected empty set.");
Misha Brukman87154fd2003-07-30 20:15:56 +000062
63 case NoFailure:
64 return false; // there is no failure with the full set of passes/funcs!
Nick Lewycky6ba630b2010-04-12 05:08:25 +000065
66 case InternalError:
67 assert(!Error.empty());
68 return true;
Misha Brukman87154fd2003-07-30 20:15:56 +000069 }
70
Chris Lattner8adfe922006-10-10 21:42:25 +000071 // Maximal number of allowed splitting iterations,
72 // before the elements are randomly shuffled.
73 const unsigned MaxIterationsWithoutProgress = 3;
Tobias Grosser56eab362015-07-26 15:18:45 +000074
75 // Maximal number of allowed single-element trim iterations. We add a
76 // threshhold here as single-element reductions may otherwise take a
77 // very long time to complete.
78 const unsigned MaxTrimIterationsWithoutBackJump = 3;
Chris Lattner8adfe922006-10-10 21:42:25 +000079 bool ShufflingEnabled = true;
80
Justin Bogner06d466a2016-09-01 21:04:36 +000081 Backjump:
Chris Lattner9f5a1972003-04-24 20:16:29 +000082 unsigned MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +000083 unsigned MaxIterations = MaxIterationsWithoutProgress;
84 unsigned NumOfIterationsWithoutProgress = 0;
85 while (MidTop > 1) { // Binary split reduction loop
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000086 // Halt if the user presses ctrl-c.
87 if (BugpointIsInterrupted) {
Dan Gohmand8db3762009-07-15 16:35:29 +000088 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000089 return true;
90 }
Bill Wendling8ff43202008-02-26 10:46:10 +000091
Chris Lattner8adfe922006-10-10 21:42:25 +000092 // If the loop doesn't make satisfying progress, try shuffling.
93 // The purpose of shuffling is to avoid the heavy tails of the
94 // distribution (improving the speed of convergence).
Justin Bogner06d466a2016-09-01 21:04:36 +000095 if (ShufflingEnabled && NumOfIterationsWithoutProgress > MaxIterations) {
Bill Wendling8ff43202008-02-26 10:46:10 +000096 std::vector<ElTy> ShuffledList(TheList);
97 std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
Dan Gohmand8db3762009-07-15 16:35:29 +000098 errs() << "\n\n*** Testing shuffled set...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +000099 // Check that random shuffle doesn't loose the bug
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000100 if (doTest(ShuffledList, empty, Error) == KeepPrefix) {
Chris Lattner8adfe922006-10-10 21:42:25 +0000101 // If the bug is still here, use the shuffled list.
102 TheList.swap(ShuffledList);
103 MidTop = TheList.size();
Justin Bogner06d466a2016-09-01 21:04:36 +0000104 // Must increase the shuffling treshold to avoid the small
Chris Lattner8adfe922006-10-10 21:42:25 +0000105 // probability of inifinite looping without making progress.
106 MaxIterations += 2;
Dan Gohmand8db3762009-07-15 16:35:29 +0000107 errs() << "\n\n*** Shuffling does not hide the bug...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +0000108 } else {
Chris Lattner8adfe922006-10-10 21:42:25 +0000109 ShufflingEnabled = false; // Disable shuffling further on
Dan Gohmand8db3762009-07-15 16:35:29 +0000110 errs() << "\n\n*** Shuffling hides the bug...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +0000111 }
112 NumOfIterationsWithoutProgress = 0;
Chris Lattner8adfe922006-10-10 21:42:25 +0000113 }
Justin Bogner06d466a2016-09-01 21:04:36 +0000114
Chris Lattner9f5a1972003-04-24 20:16:29 +0000115 unsigned Mid = MidTop / 2;
Justin Bogner06d466a2016-09-01 21:04:36 +0000116 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin() + Mid);
117 std::vector<ElTy> Suffix(TheList.begin() + Mid, TheList.end());
Chris Lattner9f5a1972003-04-24 20:16:29 +0000118
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000119 switch (doTest(Prefix, Suffix, Error)) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000120 case KeepSuffix:
121 // The property still holds. We can just drop the prefix elements, and
122 // shorten the list to the "kept" elements.
Chris Lattner0347cda2003-04-25 03:16:33 +0000123 TheList.swap(Suffix);
Chris Lattner9f5a1972003-04-24 20:16:29 +0000124 MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +0000125 // Reset progress treshold and progress counter
126 MaxIterations = MaxIterationsWithoutProgress;
127 NumOfIterationsWithoutProgress = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000128 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 Lattner8adfe922006-10-10 21:42:25 +0000133 // Reset progress treshold and progress counter
134 MaxIterations = MaxIterationsWithoutProgress;
135 NumOfIterationsWithoutProgress = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000136 break;
137 case NoFailure:
138 // Otherwise the property doesn't hold. Some of the elements we removed
Misha Brukman7eb05a12003-08-18 14:43:39 +0000139 // must be necessary to maintain the property.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000140 MidTop = Mid;
Chris Lattner8adfe922006-10-10 21:42:25 +0000141 NumOfIterationsWithoutProgress++;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000142 break;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000143 case InternalError:
Justin Bogner06d466a2016-09-01 21:04:36 +0000144 return true; // Error was set by doTest.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000145 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000146 assert(Error.empty() && "doTest did not return InternalError for error");
Chris Lattner9f5a1972003-04-24 20:16:29 +0000147 }
148
Chris Lattner8adfe922006-10-10 21:42:25 +0000149 // Probability of backjumping from the trimming loop back to the binary
150 // split reduction loop.
151 const int BackjumpProbability = 10;
152
Chris Lattner9f5a1972003-04-24 20:16:29 +0000153 // Okay, we trimmed as much off the top and the bottom of the list as we
Justin Bogner06d466a2016-09-01 21:04:36 +0000154 // could. If there is more than two elements in the list, try deleting
Reid Spencer9d59a822006-01-08 22:40:10 +0000155 // interior elements and testing that.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000156 //
Chris Lattner7fd27e22004-11-18 19:42:50 +0000157 if (TheList.size() > 2) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000158 bool Changed = true;
159 std::vector<ElTy> EmptyList;
Tobias Grosser56eab362015-07-26 15:18:45 +0000160 unsigned TrimIterations = 0;
Justin Bogner06d466a2016-09-01 21:04:36 +0000161 while (Changed) { // Trimming loop.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000162 Changed = false;
Justin Bogner06d466a2016-09-01 21:04:36 +0000163
Chris Lattner8adfe922006-10-10 21:42:25 +0000164 // If the binary split reduction loop made an unfortunate sequence of
165 // splits, the trimming loop might be left off with a huge number of
166 // remaining elements (large search space). Backjumping out of that
Justin Bogner06d466a2016-09-01 21:04:36 +0000167 // search space and attempting a different split can significantly
Chris Lattner8adfe922006-10-10 21:42:25 +0000168 // improve the convergence speed.
169 if (std::rand() % 100 < BackjumpProbability)
170 goto Backjump;
Justin Bogner06d466a2016-09-01 21:04:36 +0000171
172 for (unsigned i = 1; i < TheList.size() - 1; ++i) {
173 // Check interior elts
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000174 if (BugpointIsInterrupted) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000175 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000176 return true;
177 }
Justin Bogner06d466a2016-09-01 21:04:36 +0000178
Chris Lattner9f5a1972003-04-24 20:16:29 +0000179 std::vector<ElTy> TestList(TheList);
Justin Bogner06d466a2016-09-01 21:04:36 +0000180 TestList.erase(TestList.begin() + i);
Chris Lattner9f5a1972003-04-24 20:16:29 +0000181
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000182 if (doTest(EmptyList, TestList, Error) == KeepSuffix) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000183 // We can trim down the list!
184 TheList.swap(TestList);
Justin Bogner06d466a2016-09-01 21:04:36 +0000185 --i; // Don't skip an element of the list
Chris Lattner9f5a1972003-04-24 20:16:29 +0000186 Changed = true;
187 }
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000188 if (!Error.empty())
189 return true;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000190 }
Tobias Grosser56eab362015-07-26 15:18:45 +0000191 if (TrimIterations >= MaxTrimIterationsWithoutBackJump)
192 break;
193 TrimIterations++;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000194 }
195 }
Misha Brukman87154fd2003-07-30 20:15:56 +0000196
197 return true; // there are some failure and we've narrowed them down
Chris Lattner9f5a1972003-04-24 20:16:29 +0000198 }
199};
200
Brian Gaeke960707c2003-11-11 22:41:34 +0000201} // End llvm namespace
202
Chris Lattner9f5a1972003-04-24 20:16:29 +0000203#endif