blob: 94e63e3420053db110a372203230cad93cdd5db1 [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
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattner8adfe922006-10-10 21:42:25 +000019#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000020#include <cstdlib>
21#include <vector>
Chris Lattner9f5a1972003-04-24 20:16:29 +000022
Brian Gaeke960707c2003-11-11 22:41:34 +000023namespace llvm {
24
Justin Bogner06d466a2016-09-01 21:04:36 +000025extern bool BugpointIsInterrupted;
26
27template <typename ElTy> struct ListReducer {
Chris Lattner9f5a1972003-04-24 20:16:29 +000028 enum TestResult {
Justin Bogner06d466a2016-09-01 21:04:36 +000029 NoFailure, // No failure of the predicate was detected
30 KeepSuffix, // The suffix alone satisfies the predicate
31 KeepPrefix, // The prefix alone satisfies the predicate
32 InternalError // Encountered an error trying to run the predicate
Chris Lattner9f5a1972003-04-24 20:16:29 +000033 };
34
Chris Lattnerd7d574d2004-05-11 20:41:07 +000035 virtual ~ListReducer() {}
36
Justin Bogner06d466a2016-09-01 21:04:36 +000037 /// This virtual function should be overriden by subclasses to implement the
38 /// test desired. The testcase is only required to test to see if the Kept
39 /// list still satisfies the property, but if it is going to check the prefix
40 /// anyway, it can.
Justin Bogner46b1a9a2016-09-06 04:04:13 +000041 virtual TestResult doTest(std::vector<ElTy> &Prefix,
42 std::vector<ElTy> &Kept) = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +000043
Justin Bogner06d466a2016-09-01 21:04:36 +000044 /// This function attempts to reduce the length of the specified list while
45 /// still maintaining the "test" property. This is the core of the "work"
46 /// that bugpoint does.
Justin Bogner46b1a9a2016-09-06 04:04:13 +000047 bool reduceList(std::vector<ElTy> &TheList) {
Misha Brukman87154fd2003-07-30 20:15:56 +000048 std::vector<ElTy> empty;
Chris Lattner8adfe922006-10-10 21:42:25 +000049 std::srand(0x6e5ea738); // Seed the random number generator
Justin Bogner46b1a9a2016-09-06 04:04:13 +000050 switch (doTest(TheList, empty)) {
Misha Brukman87154fd2003-07-30 20:15:56 +000051 case KeepPrefix:
52 if (TheList.size() == 1) // we are done, it's the base case and it fails
53 return true;
Misha Brukman650ba8e2005-04-22 00:00:37 +000054 else
Misha Brukman87154fd2003-07-30 20:15:56 +000055 break; // there's definitely an error, but we need to narrow it down
56
57 case KeepSuffix:
58 // cannot be reached!
Nick Lewycky6ba630b2010-04-12 05:08:25 +000059 llvm_unreachable("bugpoint ListReducer internal error: "
60 "selected empty set.");
Misha Brukman87154fd2003-07-30 20:15:56 +000061
62 case NoFailure:
63 return false; // there is no failure with the full set of passes/funcs!
Nick Lewycky6ba630b2010-04-12 05:08:25 +000064
65 case InternalError:
Nick Lewycky6ba630b2010-04-12 05:08:25 +000066 return true;
Misha Brukman87154fd2003-07-30 20:15:56 +000067 }
68
Chris Lattner8adfe922006-10-10 21:42:25 +000069 // Maximal number of allowed splitting iterations,
70 // before the elements are randomly shuffled.
71 const unsigned MaxIterationsWithoutProgress = 3;
Tobias Grosser56eab362015-07-26 15:18:45 +000072
73 // Maximal number of allowed single-element trim iterations. We add a
74 // threshhold here as single-element reductions may otherwise take a
75 // very long time to complete.
76 const unsigned MaxTrimIterationsWithoutBackJump = 3;
Chris Lattner8adfe922006-10-10 21:42:25 +000077 bool ShufflingEnabled = true;
78
Justin Bogner06d466a2016-09-01 21:04:36 +000079 Backjump:
Chris Lattner9f5a1972003-04-24 20:16:29 +000080 unsigned MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +000081 unsigned MaxIterations = MaxIterationsWithoutProgress;
82 unsigned NumOfIterationsWithoutProgress = 0;
83 while (MidTop > 1) { // Binary split reduction loop
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000084 // Halt if the user presses ctrl-c.
85 if (BugpointIsInterrupted) {
Dan Gohmand8db3762009-07-15 16:35:29 +000086 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000087 return true;
88 }
Bill Wendling8ff43202008-02-26 10:46:10 +000089
Chris Lattner8adfe922006-10-10 21:42:25 +000090 // If the loop doesn't make satisfying progress, try shuffling.
91 // The purpose of shuffling is to avoid the heavy tails of the
92 // distribution (improving the speed of convergence).
Justin Bogner06d466a2016-09-01 21:04:36 +000093 if (ShufflingEnabled && NumOfIterationsWithoutProgress > MaxIterations) {
Bill Wendling8ff43202008-02-26 10:46:10 +000094 std::vector<ElTy> ShuffledList(TheList);
95 std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
Dan Gohmand8db3762009-07-15 16:35:29 +000096 errs() << "\n\n*** Testing shuffled set...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +000097 // Check that random shuffle doesn't loose the bug
Justin Bogner46b1a9a2016-09-06 04:04:13 +000098 if (doTest(ShuffledList, empty) == KeepPrefix) {
Chris Lattner8adfe922006-10-10 21:42:25 +000099 // If the bug is still here, use the shuffled list.
100 TheList.swap(ShuffledList);
101 MidTop = TheList.size();
Justin Bogner06d466a2016-09-01 21:04:36 +0000102 // Must increase the shuffling treshold to avoid the small
Chris Lattner8adfe922006-10-10 21:42:25 +0000103 // probability of inifinite looping without making progress.
104 MaxIterations += 2;
Dan Gohmand8db3762009-07-15 16:35:29 +0000105 errs() << "\n\n*** Shuffling does not hide the bug...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +0000106 } else {
Chris Lattner8adfe922006-10-10 21:42:25 +0000107 ShufflingEnabled = false; // Disable shuffling further on
Dan Gohmand8db3762009-07-15 16:35:29 +0000108 errs() << "\n\n*** Shuffling hides the bug...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +0000109 }
110 NumOfIterationsWithoutProgress = 0;
Chris Lattner8adfe922006-10-10 21:42:25 +0000111 }
Justin Bogner06d466a2016-09-01 21:04:36 +0000112
Chris Lattner9f5a1972003-04-24 20:16:29 +0000113 unsigned Mid = MidTop / 2;
Justin Bogner06d466a2016-09-01 21:04:36 +0000114 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin() + Mid);
115 std::vector<ElTy> Suffix(TheList.begin() + Mid, TheList.end());
Chris Lattner9f5a1972003-04-24 20:16:29 +0000116
Justin Bogner46b1a9a2016-09-06 04:04:13 +0000117 switch (doTest(Prefix, Suffix)) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000118 case KeepSuffix:
119 // The property still holds. We can just drop the prefix elements, and
120 // shorten the list to the "kept" elements.
Chris Lattner0347cda2003-04-25 03:16:33 +0000121 TheList.swap(Suffix);
Chris Lattner9f5a1972003-04-24 20:16:29 +0000122 MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +0000123 // Reset progress treshold and progress counter
124 MaxIterations = MaxIterationsWithoutProgress;
125 NumOfIterationsWithoutProgress = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000126 break;
127 case KeepPrefix:
128 // The predicate still holds, shorten the list to the prefix elements.
129 TheList.swap(Prefix);
130 MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +0000131 // Reset progress treshold and progress counter
132 MaxIterations = MaxIterationsWithoutProgress;
133 NumOfIterationsWithoutProgress = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000134 break;
135 case NoFailure:
136 // Otherwise the property doesn't hold. Some of the elements we removed
Misha Brukman7eb05a12003-08-18 14:43:39 +0000137 // must be necessary to maintain the property.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000138 MidTop = Mid;
Chris Lattner8adfe922006-10-10 21:42:25 +0000139 NumOfIterationsWithoutProgress++;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000140 break;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000141 case InternalError:
Justin Bogner46b1a9a2016-09-06 04:04:13 +0000142 return true;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000143 }
144 }
145
Chris Lattner8adfe922006-10-10 21:42:25 +0000146 // Probability of backjumping from the trimming loop back to the binary
147 // split reduction loop.
148 const int BackjumpProbability = 10;
149
Chris Lattner9f5a1972003-04-24 20:16:29 +0000150 // Okay, we trimmed as much off the top and the bottom of the list as we
Justin Bogner06d466a2016-09-01 21:04:36 +0000151 // could. If there is more than two elements in the list, try deleting
Reid Spencer9d59a822006-01-08 22:40:10 +0000152 // interior elements and testing that.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000153 //
Chris Lattner7fd27e22004-11-18 19:42:50 +0000154 if (TheList.size() > 2) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000155 bool Changed = true;
156 std::vector<ElTy> EmptyList;
Tobias Grosser56eab362015-07-26 15:18:45 +0000157 unsigned TrimIterations = 0;
Justin Bogner06d466a2016-09-01 21:04:36 +0000158 while (Changed) { // Trimming loop.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000159 Changed = false;
Justin Bogner06d466a2016-09-01 21:04:36 +0000160
Chris Lattner8adfe922006-10-10 21:42:25 +0000161 // If the binary split reduction loop made an unfortunate sequence of
162 // splits, the trimming loop might be left off with a huge number of
163 // remaining elements (large search space). Backjumping out of that
Justin Bogner06d466a2016-09-01 21:04:36 +0000164 // search space and attempting a different split can significantly
Chris Lattner8adfe922006-10-10 21:42:25 +0000165 // improve the convergence speed.
166 if (std::rand() % 100 < BackjumpProbability)
167 goto Backjump;
Justin Bogner06d466a2016-09-01 21:04:36 +0000168
169 for (unsigned i = 1; i < TheList.size() - 1; ++i) {
170 // Check interior elts
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000171 if (BugpointIsInterrupted) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000172 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000173 return true;
174 }
Justin Bogner06d466a2016-09-01 21:04:36 +0000175
Chris Lattner9f5a1972003-04-24 20:16:29 +0000176 std::vector<ElTy> TestList(TheList);
Justin Bogner06d466a2016-09-01 21:04:36 +0000177 TestList.erase(TestList.begin() + i);
Chris Lattner9f5a1972003-04-24 20:16:29 +0000178
Justin Bogner46b1a9a2016-09-06 04:04:13 +0000179 switch (doTest(EmptyList, TestList)) {
180 case KeepSuffix:
Chris Lattner9f5a1972003-04-24 20:16:29 +0000181 // We can trim down the list!
182 TheList.swap(TestList);
Justin Bogner06d466a2016-09-01 21:04:36 +0000183 --i; // Don't skip an element of the list
Chris Lattner9f5a1972003-04-24 20:16:29 +0000184 Changed = true;
Justin Bogner46b1a9a2016-09-06 04:04:13 +0000185 break;
186 case InternalError:
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000187 return true;
Justin Bogner46b1a9a2016-09-06 04:04:13 +0000188 default:
189 break;
190 }
Chris Lattner9f5a1972003-04-24 20:16:29 +0000191 }
Tobias Grosser56eab362015-07-26 15:18:45 +0000192 if (TrimIterations >= MaxTrimIterationsWithoutBackJump)
193 break;
194 TrimIterations++;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000195 }
196 }
Misha Brukman87154fd2003-07-30 20:15:56 +0000197
198 return true; // there are some failure and we've narrowed them down
Chris Lattner9f5a1972003-04-24 20:16:29 +0000199 }
200};
201
Brian Gaeke960707c2003-11-11 22:41:34 +0000202} // End llvm namespace
203
Chris Lattner9f5a1972003-04-24 20:16:29 +0000204#endif