blob: cd629da3457df1476e1094b88bb6b25c2b3e3e22 [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
18#include <vector>
Chris Lattner043b9722003-11-29 20:04:13 +000019#include <iostream>
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!
61 std::cerr << "bugpoint ListReducer internal error: selected empty set.\n";
62 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) {
80 std::cerr << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
81 return true;
82 }
Chris Lattner137d0ec2006-10-10 21:42:25 +000083
84 // 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 &&
88 NumOfIterationsWithoutProgress > MaxIterations) {
89
90 std::vector<ElTy> ShuffledList(TheList);
91 std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
92 std::cerr << "\n\n*** Testing shuffled set...\n\n";
93 // Check that random shuffle doesn't loose the bug
94 if (doTest(ShuffledList, empty) == KeepPrefix) {
95 // If the bug is still here, use the shuffled list.
96 TheList.swap(ShuffledList);
97 MidTop = TheList.size();
98 // Must increase the shuffling treshold to avoid the small
99 // probability of inifinite looping without making progress.
100 MaxIterations += 2;
101 std::cerr << "\n\n*** Shuffling does not hide the bug...\n\n";
102 } else {
103 ShufflingEnabled = false; // Disable shuffling further on
104 std::cerr << "\n\n*** Shuffling hides the bug...\n\n";
105 }
106 NumOfIterationsWithoutProgress = 0;
107 }
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000108
Chris Lattner126840f2003-04-24 20:16:29 +0000109 unsigned Mid = MidTop / 2;
Chris Lattner0c139982003-04-25 03:16:33 +0000110 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
111 std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
Chris Lattner126840f2003-04-24 20:16:29 +0000112
Chris Lattner0c139982003-04-25 03:16:33 +0000113 switch (doTest(Prefix, Suffix)) {
Chris Lattner126840f2003-04-24 20:16:29 +0000114 case KeepSuffix:
115 // The property still holds. We can just drop the prefix elements, and
116 // shorten the list to the "kept" elements.
Chris Lattner0c139982003-04-25 03:16:33 +0000117 TheList.swap(Suffix);
Chris Lattner126840f2003-04-24 20:16:29 +0000118 MidTop = TheList.size();
Chris Lattner137d0ec2006-10-10 21:42:25 +0000119 // Reset progress treshold and progress counter
120 MaxIterations = MaxIterationsWithoutProgress;
121 NumOfIterationsWithoutProgress = 0;
Chris Lattner126840f2003-04-24 20:16:29 +0000122 break;
123 case KeepPrefix:
124 // The predicate still holds, shorten the list to the prefix elements.
125 TheList.swap(Prefix);
126 MidTop = TheList.size();
Chris Lattner137d0ec2006-10-10 21:42:25 +0000127 // Reset progress treshold and progress counter
128 MaxIterations = MaxIterationsWithoutProgress;
129 NumOfIterationsWithoutProgress = 0;
Chris Lattner126840f2003-04-24 20:16:29 +0000130 break;
131 case NoFailure:
132 // Otherwise the property doesn't hold. Some of the elements we removed
Misha Brukman5560c9d2003-08-18 14:43:39 +0000133 // must be necessary to maintain the property.
Chris Lattner126840f2003-04-24 20:16:29 +0000134 MidTop = Mid;
Chris Lattner137d0ec2006-10-10 21:42:25 +0000135 NumOfIterationsWithoutProgress++;
Chris Lattner126840f2003-04-24 20:16:29 +0000136 break;
137 }
138 }
139
Chris Lattner137d0ec2006-10-10 21:42:25 +0000140 // Probability of backjumping from the trimming loop back to the binary
141 // split reduction loop.
142 const int BackjumpProbability = 10;
143
Chris Lattner126840f2003-04-24 20:16:29 +0000144 // Okay, we trimmed as much off the top and the bottom of the list as we
Reid Spencerde83cee2006-01-08 22:40:10 +0000145 // could. If there is more than two elements in the list, try deleting
146 // interior elements and testing that.
Chris Lattner126840f2003-04-24 20:16:29 +0000147 //
Chris Lattnerd29c6362004-11-18 19:42:50 +0000148 if (TheList.size() > 2) {
Chris Lattner126840f2003-04-24 20:16:29 +0000149 bool Changed = true;
150 std::vector<ElTy> EmptyList;
Chris Lattner137d0ec2006-10-10 21:42:25 +0000151 while (Changed) { // Trimming loop.
Chris Lattner126840f2003-04-24 20:16:29 +0000152 Changed = false;
Chris Lattner137d0ec2006-10-10 21:42:25 +0000153
154 // If the binary split reduction loop made an unfortunate sequence of
155 // splits, the trimming loop might be left off with a huge number of
156 // remaining elements (large search space). Backjumping out of that
157 // search space and attempting a different split can significantly
158 // improve the convergence speed.
159 if (std::rand() % 100 < BackjumpProbability)
160 goto Backjump;
161
Chris Lattner126840f2003-04-24 20:16:29 +0000162 for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000163 if (BugpointIsInterrupted) {
164 std::cerr << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
165 return true;
166 }
167
Chris Lattner126840f2003-04-24 20:16:29 +0000168 std::vector<ElTy> TestList(TheList);
169 TestList.erase(TestList.begin()+i);
170
171 if (doTest(EmptyList, TestList) == KeepSuffix) {
172 // We can trim down the list!
173 TheList.swap(TestList);
174 --i; // Don't skip an element of the list
175 Changed = true;
176 }
177 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000178 // This can take a long time if left uncontrolled. For now, don't
Chris Lattnerd29c6362004-11-18 19:42:50 +0000179 // iterate.
180 break;
Chris Lattner126840f2003-04-24 20:16:29 +0000181 }
182 }
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000183
184 return true; // there are some failure and we've narrowed them down
Chris Lattner126840f2003-04-24 20:16:29 +0000185 }
186};
187
Brian Gaeked0fde302003-11-11 22:41:34 +0000188} // End llvm namespace
189
Chris Lattner126840f2003-04-24 20:16:29 +0000190#endif