[Driver][FreeBSD] Remove FreeBSD/loongarch32 support (#122515)
[llvm-project.git] / llvm / tools / reduce-chunk-list / reduce-chunk-list.cpp
bloba819af6e5f1a056c1dc04844a4ab8e9795f7c5e8
1 //===-- reduce-chunk-list.cpp - Reduce a chunks list to its minimal size --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // See the llvm-project/llvm/docs/ProgrammersManual.rst to see how to use this
10 // tool
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/DebugCounter.h"
17 #include "llvm/Support/Program.h"
19 using namespace llvm;
21 cl::opt<std::string> ReproductionCmd(cl::Positional, cl::Required);
23 cl::opt<std::string> StartChunks(cl::Positional, cl::Required);
25 cl::opt<bool> Pessimist("pessimist", cl::init(false));
27 using Chunk = DebugCounter::Chunk;
29 namespace {
31 SmallVector<Chunk> simplifyChunksList(ArrayRef<Chunk> Chunks) {
32 SmallVector<Chunk> Res;
33 Res.push_back(Chunks.front());
34 for (unsigned Idx = 1; Idx < Chunks.size(); Idx++) {
35 if (Chunks[Idx].Begin == Res.back().End + 1)
36 Res.back().End = Chunks[Idx].End;
37 else
38 Res.push_back(Chunks[Idx]);
40 return Res;
43 bool isStillInteresting(ArrayRef<Chunk> Chunks) {
44 SmallVector<Chunk> SimpleChunks = simplifyChunksList(Chunks);
46 std::string ChunkStr;
48 raw_string_ostream OS(ChunkStr);
49 DebugCounter::printChunks(OS, SimpleChunks);
52 errs() << "Checking with: " << ChunkStr << "\n";
54 std::vector<StringRef> Argv;
55 Argv.push_back(ReproductionCmd);
56 Argv.push_back(ChunkStr);
58 std::string ErrMsg;
59 bool ExecutionFailed;
60 int Result = sys::ExecuteAndWait(Argv[0], Argv, std::nullopt, {}, 0, 0,
61 &ErrMsg, &ExecutionFailed);
62 if (ExecutionFailed) {
63 errs() << "failed to execute : " << Argv[0] << " : " << ErrMsg << "\n";
64 exit(1);
67 bool Res = Result != 0;
68 if (Res) {
69 errs() << "SUCCESS : Still Interesting\n";
70 } else {
71 errs() << "FAILURE : Not Interesting\n";
73 return Res;
76 bool increaseGranularity(SmallVector<Chunk> &Chunks) {
77 errs() << "Increasing granularity\n";
78 SmallVector<Chunk> NewChunks;
79 bool SplitOne = false;
81 for (auto &C : Chunks) {
82 if (C.Begin == C.End) {
83 NewChunks.push_back(C);
84 } else {
85 int Half = (C.Begin + C.End) / 2;
86 NewChunks.push_back({C.Begin, Half});
87 NewChunks.push_back({Half + 1, C.End});
88 SplitOne = true;
91 if (SplitOne) {
92 Chunks = std::move(NewChunks);
94 return SplitOne;
97 } // namespace
99 int main(int argc, char **argv) {
100 cl::ParseCommandLineOptions(argc, argv);
102 SmallVector<Chunk> CurrChunks;
103 if (DebugCounter::parseChunks(StartChunks, CurrChunks)) {
104 return 1;
107 auto Program = sys::findProgramByName(ReproductionCmd);
108 if (!Program) {
109 errs() << "failed to find command : " << ReproductionCmd << "\n";
110 return 1;
112 ReproductionCmd.setValue(Program.get());
114 errs() << "Input Checking:\n";
115 if (!isStillInteresting(CurrChunks)) {
116 errs() << "starting chunks are not interesting\n";
117 return 1;
119 if (CurrChunks.size() == 1)
120 increaseGranularity(CurrChunks);
121 if (Pessimist)
122 while (increaseGranularity(CurrChunks))
123 /* empty body */;
124 while (1) {
125 for (int Idx = (CurrChunks.size() - 1); Idx >= 0; Idx--) {
126 if (CurrChunks.size() == 1)
127 break;
129 Chunk Testing = CurrChunks[Idx];
130 errs() << "Trying to remove : ";
131 Testing.print(errs());
132 errs() << "\n";
134 CurrChunks.erase(CurrChunks.begin() + Idx);
136 if (!isStillInteresting(CurrChunks))
137 CurrChunks.insert(CurrChunks.begin() + Idx, Testing);
139 bool HasSplit = increaseGranularity(CurrChunks);
140 if (!HasSplit)
141 break;
144 errs() << "Minimal Chunks = ";
145 DebugCounter::printChunks(llvm::errs(), simplifyChunksList(CurrChunks));
146 errs() << "\n";