Cast MO.getImm() to unsigned before comparing with an unsigned limit.
[llvm/avr.git] / tools / bugpoint / ListReducer.h
blob8036d1f5449947ea1ff85d2a2ba8378c44697514
1 //===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef BUGPOINT_LIST_REDUCER_H
16 #define BUGPOINT_LIST_REDUCER_H
18 #include "llvm/Support/raw_ostream.h"
19 #include <vector>
20 #include <cstdlib>
21 #include <algorithm>
23 namespace llvm {
25 extern bool BugpointIsInterrupted;
27 template<typename ElTy>
28 struct ListReducer {
29 enum TestResult {
30 NoFailure, // No failure of the predicate was detected
31 KeepSuffix, // The suffix alone satisfies the predicate
32 KeepPrefix // The prefix alone satisfies the predicate
35 virtual ~ListReducer() {}
37 // 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.
42 virtual TestResult doTest(std::vector<ElTy> &Prefix,
43 std::vector<ElTy> &Kept) = 0;
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.
49 bool reduceList(std::vector<ElTy> &TheList) {
50 std::vector<ElTy> empty;
51 std::srand(0x6e5ea738); // Seed the random number generator
52 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;
56 else
57 break; // there's definitely an error, but we need to narrow it down
59 case KeepSuffix:
60 // cannot be reached!
61 errs() << "bugpoint ListReducer internal error: selected empty set.\n";
62 abort();
64 case NoFailure:
65 return false; // there is no failure with the full set of passes/funcs!
68 // Maximal number of allowed splitting iterations,
69 // before the elements are randomly shuffled.
70 const unsigned MaxIterationsWithoutProgress = 3;
71 bool ShufflingEnabled = true;
73 Backjump:
74 unsigned MidTop = TheList.size();
75 unsigned MaxIterations = MaxIterationsWithoutProgress;
76 unsigned NumOfIterationsWithoutProgress = 0;
77 while (MidTop > 1) { // Binary split reduction loop
78 // Halt if the user presses ctrl-c.
79 if (BugpointIsInterrupted) {
80 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
81 return true;
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 std::vector<ElTy> ShuffledList(TheList);
90 std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
91 errs() << "\n\n*** Testing shuffled set...\n\n";
92 // Check that random shuffle doesn't loose the bug
93 if (doTest(ShuffledList, empty) == KeepPrefix) {
94 // 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;
100 errs() << "\n\n*** Shuffling does not hide the bug...\n\n";
101 } else {
102 ShufflingEnabled = false; // Disable shuffling further on
103 errs() << "\n\n*** Shuffling hides the bug...\n\n";
105 NumOfIterationsWithoutProgress = 0;
108 unsigned Mid = MidTop / 2;
109 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
110 std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
112 switch (doTest(Prefix, Suffix)) {
113 case KeepSuffix:
114 // The property still holds. We can just drop the prefix elements, and
115 // shorten the list to the "kept" elements.
116 TheList.swap(Suffix);
117 MidTop = TheList.size();
118 // Reset progress treshold and progress counter
119 MaxIterations = MaxIterationsWithoutProgress;
120 NumOfIterationsWithoutProgress = 0;
121 break;
122 case KeepPrefix:
123 // The predicate still holds, shorten the list to the prefix elements.
124 TheList.swap(Prefix);
125 MidTop = TheList.size();
126 // Reset progress treshold and progress counter
127 MaxIterations = MaxIterationsWithoutProgress;
128 NumOfIterationsWithoutProgress = 0;
129 break;
130 case NoFailure:
131 // Otherwise the property doesn't hold. Some of the elements we removed
132 // must be necessary to maintain the property.
133 MidTop = Mid;
134 NumOfIterationsWithoutProgress++;
135 break;
139 // Probability of backjumping from the trimming loop back to the binary
140 // split reduction loop.
141 const int BackjumpProbability = 10;
143 // Okay, we trimmed as much off the top and the bottom of the list as we
144 // could. If there is more than two elements in the list, try deleting
145 // interior elements and testing that.
147 if (TheList.size() > 2) {
148 bool Changed = true;
149 std::vector<ElTy> EmptyList;
150 while (Changed) { // Trimming loop.
151 Changed = false;
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;
161 for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts
162 if (BugpointIsInterrupted) {
163 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
164 return true;
167 std::vector<ElTy> TestList(TheList);
168 TestList.erase(TestList.begin()+i);
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;
177 // This can take a long time if left uncontrolled. For now, don't
178 // iterate.
179 break;
183 return true; // there are some failure and we've narrowed them down
187 } // End llvm namespace
189 #endif