blob: 8036d1f5449947ea1ff85d2a2ba8378c44697514 [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//
Chris Lattner21c62da2007-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 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
Dan Gohman65f57c22009-07-15 16:35:29 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattner126840f2003-04-24 20:16:29 +000019#include <vector>
Chris Lattner137d0ec2006-10-10 21:42:25 +000020#include <cstdlib>
21#include <algorithm>
Chris Lattner126840f2003-04-24 20:16:29 +000022
Brian Gaeked0fde302003-11-11 22:41:34 +000023namespace llvm {
Chris Lattnerf9aaae02005-08-02 02:16:17 +000024
25 extern bool BugpointIsInterrupted;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattner126840f2003-04-24 20:16:29 +000027template<typename ElTy>
28struct ListReducer {
29 enum TestResult {
30 NoFailure, // No failure of the predicate was detected
31 KeepSuffix, // The suffix alone satisfies the predicate
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000032 KeepPrefix // The prefix alone satisfies the predicate
Chris Lattner126840f2003-04-24 20:16:29 +000033 };
34
Chris Lattner1c2f6862004-05-11 20:41:07 +000035 virtual ~ListReducer() {}
36
Chris Lattner126840f2003-04-24 20:16:29 +000037 // doTest - This virtual function should be overriden by subclasses to
38 // implement the test desired. The testcase is only required to test to see
39 // if the Kept list still satisfies the property, but if it is going to check
40 // the prefix anyway, it can.
41 //
Chris Lattnerb4ebe132003-04-24 22:23:56 +000042 virtual TestResult doTest(std::vector<ElTy> &Prefix,
43 std::vector<ElTy> &Kept) = 0;
Chris Lattner126840f2003-04-24 20:16:29 +000044
45 // reduceList - This function attempts to reduce the length of the specified
46 // list while still maintaining the "test" property. This is the core of the
47 // "work" that bugpoint does.
48 //
Misha Brukmanbe6bf562003-07-30 20:15:56 +000049 bool reduceList(std::vector<ElTy> &TheList) {
50 std::vector<ElTy> empty;
Chris Lattner137d0ec2006-10-10 21:42:25 +000051 std::srand(0x6e5ea738); // Seed the random number generator
Misha Brukmanbe6bf562003-07-30 20:15:56 +000052 switch (doTest(TheList, empty)) {
53 case KeepPrefix:
54 if (TheList.size() == 1) // we are done, it's the base case and it fails
55 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +000056 else
Misha Brukmanbe6bf562003-07-30 20:15:56 +000057 break; // there's definitely an error, but we need to narrow it down
58
59 case KeepSuffix:
60 // cannot be reached!
Dan Gohman65f57c22009-07-15 16:35:29 +000061 errs() << "bugpoint ListReducer internal error: selected empty set.\n";
Misha Brukmanbe6bf562003-07-30 20:15:56 +000062 abort();
63
64 case NoFailure:
65 return false; // there is no failure with the full set of passes/funcs!
66 }
67
Chris Lattner137d0ec2006-10-10 21:42:25 +000068 // Maximal number of allowed splitting iterations,
69 // before the elements are randomly shuffled.
70 const unsigned MaxIterationsWithoutProgress = 3;
71 bool ShufflingEnabled = true;
72
73Backjump:
Chris Lattner126840f2003-04-24 20:16:29 +000074 unsigned MidTop = TheList.size();
Chris Lattner137d0ec2006-10-10 21:42:25 +000075 unsigned MaxIterations = MaxIterationsWithoutProgress;
76 unsigned NumOfIterationsWithoutProgress = 0;
77 while (MidTop > 1) { // Binary split reduction loop
Chris Lattnerf9aaae02005-08-02 02:16:17 +000078 // Halt if the user presses ctrl-c.
79 if (BugpointIsInterrupted) {
Dan Gohman65f57c22009-07-15 16:35:29 +000080 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerf9aaae02005-08-02 02:16:17 +000081 return true;
82 }
Bill Wendling584c3bf2008-02-26 10:46:10 +000083
Chris Lattner137d0ec2006-10-10 21:42:25 +000084 // If the loop doesn't make satisfying progress, try shuffling.
85 // The purpose of shuffling is to avoid the heavy tails of the
86 // distribution (improving the speed of convergence).
87 if (ShufflingEnabled &&
Bill Wendling584c3bf2008-02-26 10:46:10 +000088 NumOfIterationsWithoutProgress > MaxIterations) {
89 std::vector<ElTy> ShuffledList(TheList);
90 std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
Dan Gohman65f57c22009-07-15 16:35:29 +000091 errs() << "\n\n*** Testing shuffled set...\n\n";
Bill Wendling584c3bf2008-02-26 10:46:10 +000092 // Check that random shuffle doesn't loose the bug
93 if (doTest(ShuffledList, empty) == KeepPrefix) {
Chris Lattner137d0ec2006-10-10 21:42:25 +000094 // If the bug is still here, use the shuffled list.
95 TheList.swap(ShuffledList);
96 MidTop = TheList.size();
97 // Must increase the shuffling treshold to avoid the small
98 // probability of inifinite looping without making progress.
99 MaxIterations += 2;
Dan Gohman65f57c22009-07-15 16:35:29 +0000100 errs() << "\n\n*** Shuffling does not hide the bug...\n\n";
Bill Wendling584c3bf2008-02-26 10:46:10 +0000101 } else {
Chris Lattner137d0ec2006-10-10 21:42:25 +0000102 ShufflingEnabled = false; // Disable shuffling further on
Dan Gohman65f57c22009-07-15 16:35:29 +0000103 errs() << "\n\n*** Shuffling hides the bug...\n\n";
Bill Wendling584c3bf2008-02-26 10:46:10 +0000104 }
105 NumOfIterationsWithoutProgress = 0;
Chris Lattner137d0ec2006-10-10 21:42:25 +0000106 }
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000107
Chris Lattner126840f2003-04-24 20:16:29 +0000108 unsigned Mid = MidTop / 2;
Chris Lattner0c139982003-04-25 03:16:33 +0000109 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
110 std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
Chris Lattner126840f2003-04-24 20:16:29 +0000111
Chris Lattner0c139982003-04-25 03:16:33 +0000112 switch (doTest(Prefix, Suffix)) {
Chris Lattner126840f2003-04-24 20:16:29 +0000113 case KeepSuffix:
114 // The property still holds. We can just drop the prefix elements, and
115 // shorten the list to the "kept" elements.
Chris Lattner0c139982003-04-25 03:16:33 +0000116 TheList.swap(Suffix);
Chris Lattner126840f2003-04-24 20:16:29 +0000117 MidTop = TheList.size();
Chris Lattner137d0ec2006-10-10 21:42:25 +0000118 // Reset progress treshold and progress counter
119 MaxIterations = MaxIterationsWithoutProgress;
120 NumOfIterationsWithoutProgress = 0;
Chris Lattner126840f2003-04-24 20:16:29 +0000121 break;
122 case KeepPrefix:
123 // The predicate still holds, shorten the list to the prefix elements.
124 TheList.swap(Prefix);
125 MidTop = TheList.size();
Chris Lattner137d0ec2006-10-10 21:42:25 +0000126 // Reset progress treshold and progress counter
127 MaxIterations = MaxIterationsWithoutProgress;
128 NumOfIterationsWithoutProgress = 0;
Chris Lattner126840f2003-04-24 20:16:29 +0000129 break;
130 case NoFailure:
131 // Otherwise the property doesn't hold. Some of the elements we removed
Misha Brukman5560c9d2003-08-18 14:43:39 +0000132 // must be necessary to maintain the property.
Chris Lattner126840f2003-04-24 20:16:29 +0000133 MidTop = Mid;
Chris Lattner137d0ec2006-10-10 21:42:25 +0000134 NumOfIterationsWithoutProgress++;
Chris Lattner126840f2003-04-24 20:16:29 +0000135 break;
136 }
137 }
138
Chris Lattner137d0ec2006-10-10 21:42:25 +0000139 // Probability of backjumping from the trimming loop back to the binary
140 // split reduction loop.
141 const int BackjumpProbability = 10;
142
Chris Lattner126840f2003-04-24 20:16:29 +0000143 // Okay, we trimmed as much off the top and the bottom of the list as we
Reid Spencerde83cee2006-01-08 22:40:10 +0000144 // could. If there is more than two elements in the list, try deleting
145 // interior elements and testing that.
Chris Lattner126840f2003-04-24 20:16:29 +0000146 //
Chris Lattnerd29c6362004-11-18 19:42:50 +0000147 if (TheList.size() > 2) {
Chris Lattner126840f2003-04-24 20:16:29 +0000148 bool Changed = true;
149 std::vector<ElTy> EmptyList;
Chris Lattner137d0ec2006-10-10 21:42:25 +0000150 while (Changed) { // Trimming loop.
Chris Lattner126840f2003-04-24 20:16:29 +0000151 Changed = false;
Chris Lattner137d0ec2006-10-10 21:42:25 +0000152
153 // If the binary split reduction loop made an unfortunate sequence of
154 // splits, the trimming loop might be left off with a huge number of
155 // remaining elements (large search space). Backjumping out of that
156 // search space and attempting a different split can significantly
157 // improve the convergence speed.
158 if (std::rand() % 100 < BackjumpProbability)
159 goto Backjump;
160
Chris Lattner126840f2003-04-24 20:16:29 +0000161 for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000162 if (BugpointIsInterrupted) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000163 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000164 return true;
165 }
166
Chris Lattner126840f2003-04-24 20:16:29 +0000167 std::vector<ElTy> TestList(TheList);
168 TestList.erase(TestList.begin()+i);
169
170 if (doTest(EmptyList, TestList) == KeepSuffix) {
171 // We can trim down the list!
172 TheList.swap(TestList);
173 --i; // Don't skip an element of the list
174 Changed = true;
175 }
176 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000177 // This can take a long time if left uncontrolled. For now, don't
Chris Lattnerd29c6362004-11-18 19:42:50 +0000178 // iterate.
179 break;
Chris Lattner126840f2003-04-24 20:16:29 +0000180 }
181 }
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000182
183 return true; // there are some failure and we've narrowed them down
Chris Lattner126840f2003-04-24 20:16:29 +0000184 }
185};
186
Brian Gaeked0fde302003-11-11 22:41:34 +0000187} // End llvm namespace
188
Chris Lattner126840f2003-04-24 20:16:29 +0000189#endif