[RISCV] Add missing SiFive P400 scheduling model test for divisions. NFC
[llvm-project.git] / llvm / tools / llvm-reduce / llvm-reduce.cpp
blob288a384c2ed49803819ace54b6fcf96a02e88e2c
1 //===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===//
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 // This program tries to reduce an IR test case for a given interesting-ness
10 // test. It runs multiple delta debugging passes in order to minimize the input
11 // file. It's worth noting that this is a part of the bugpoint redesign
12 // proposal, and thus a *temporary* tool that will eventually be integrated
13 // into the bugpoint tool itself.
15 //===----------------------------------------------------------------------===//
17 #include "DeltaManager.h"
18 #include "ReducerWorkItem.h"
19 #include "TestRunner.h"
20 #include "llvm/Bitcode/BitcodeReader.h"
21 #include "llvm/CodeGen/CommandFlags.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/MemoryBufferRef.h"
25 #include "llvm/Support/Process.h"
26 #include "llvm/Support/WithColor.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <system_error>
30 #ifdef _WIN32
31 #include <windows.h>
32 #endif
34 using namespace llvm;
36 cl::OptionCategory LLVMReduceOptions("llvm-reduce options");
38 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden,
39 cl::cat(LLVMReduceOptions));
40 static cl::opt<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden,
41 cl::cat(LLVMReduceOptions));
43 static cl::opt<bool> PreserveDebugEnvironment(
44 "preserve-debug-environment",
45 cl::desc("Don't disable features used for crash "
46 "debugging (crash reports, llvm-symbolizer and core dumps)"),
47 cl::cat(LLVMReduceOptions));
49 static cl::opt<bool>
50 PrintDeltaPasses("print-delta-passes",
51 cl::desc("Print list of delta passes, passable to "
52 "--delta-passes as a comma separated list"),
53 cl::cat(LLVMReduceOptions));
55 static cl::opt<std::string> InputFilename(cl::Positional,
56 cl::desc("<input llvm ll/bc file>"),
57 cl::cat(LLVMReduceOptions));
59 static cl::opt<std::string>
60 TestFilename("test",
61 cl::desc("Name of the interesting-ness test to be run"),
62 cl::cat(LLVMReduceOptions));
64 static cl::list<std::string>
65 TestArguments("test-arg",
66 cl::desc("Arguments passed onto the interesting-ness test"),
67 cl::cat(LLVMReduceOptions));
69 static cl::opt<std::string> OutputFilename(
70 "output",
71 cl::desc("Specify the output file. default: reduced.ll|.bc|.mir"));
72 static cl::alias OutputFileAlias("o", cl::desc("Alias for -output"),
73 cl::aliasopt(OutputFilename),
74 cl::cat(LLVMReduceOptions));
76 static cl::opt<bool>
77 ReplaceInput("in-place",
78 cl::desc("WARNING: This option will replace your input file "
79 "with the reduced version!"),
80 cl::cat(LLVMReduceOptions));
82 enum class InputLanguages { None, IR, MIR };
84 static cl::opt<InputLanguages>
85 InputLanguage("x", cl::ValueOptional,
86 cl::desc("Input language ('ir' or 'mir')"),
87 cl::init(InputLanguages::None),
88 cl::values(clEnumValN(InputLanguages::IR, "ir", ""),
89 clEnumValN(InputLanguages::MIR, "mir", "")),
90 cl::cat(LLVMReduceOptions));
92 static cl::opt<bool> ForceOutputBitcode(
93 "output-bitcode",
94 cl::desc("Emit final result as bitcode instead of text IR"), cl::Hidden,
95 cl::cat(LLVMReduceOptions));
97 static cl::opt<int>
98 MaxPassIterations("max-pass-iterations",
99 cl::desc("Maximum number of times to run the full set "
100 "of delta passes (default=5)"),
101 cl::init(5), cl::cat(LLVMReduceOptions));
103 extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
105 static codegen::RegisterCodeGenFlags CGF;
107 /// Turn off crash debugging features
109 /// Crash is expected, so disable crash reports and symbolization to reduce
110 /// output clutter and avoid potentially slow symbolization.
111 static void disableEnvironmentDebugFeatures() {
112 sys::Process::PreventCoreFiles();
114 // TODO: Copied from not. Should have a wrapper around setenv.
115 #ifdef _WIN32
116 SetEnvironmentVariableA("LLVM_DISABLE_CRASH_REPORT", "1");
117 SetEnvironmentVariableA("LLVM_DISABLE_SYMBOLIZATION", "1");
118 #else
119 setenv("LLVM_DISABLE_CRASH_REPORT", "1", /*overwrite=*/1);
120 setenv("LLVM_DISABLE_SYMBOLIZATION", "1", /*overwrite=*/1);
121 #endif
124 static std::pair<StringRef, bool> determineOutputType(bool IsMIR,
125 bool InputIsBitcode) {
126 bool OutputBitcode = ForceOutputBitcode || InputIsBitcode;
128 if (ReplaceInput) { // In-place
129 OutputFilename = InputFilename.c_str();
130 } else if (OutputFilename.empty()) {
131 // Default to producing bitcode if the input was bitcode, if not explicitly
132 // requested.
134 OutputFilename =
135 IsMIR ? "reduced.mir" : (OutputBitcode ? "reduced.bc" : "reduced.ll");
138 return {OutputFilename, OutputBitcode};
141 int main(int Argc, char **Argv) {
142 InitLLVM X(Argc, Argv);
143 const StringRef ToolName(Argv[0]);
144 PreserveInputDbgFormat = cl::boolOrDefault::BOU_TRUE;
146 cl::HideUnrelatedOptions({&LLVMReduceOptions, &getColorCategory()});
147 cl::ParseCommandLineOptions(Argc, Argv, "LLVM automatic testcase reducer.\n");
149 if (Argc == 1) {
150 cl::PrintHelpMessage();
151 return 0;
154 if (PrintDeltaPasses) {
155 printDeltaPasses(outs());
156 return 0;
159 bool ReduceModeMIR = false;
160 if (InputLanguage != InputLanguages::None) {
161 if (InputLanguage == InputLanguages::MIR)
162 ReduceModeMIR = true;
163 } else if (StringRef(InputFilename).ends_with(".mir")) {
164 ReduceModeMIR = true;
167 if (InputFilename.empty()) {
168 WithColor::error(errs(), ToolName)
169 << "reduction testcase positional argument must be specified\n";
170 return 1;
173 if (TestFilename.empty()) {
174 WithColor::error(errs(), ToolName) << "--test option must be specified\n";
175 return 1;
178 if (!PreserveDebugEnvironment)
179 disableEnvironmentDebugFeatures();
181 LLVMContext Context;
182 std::unique_ptr<TargetMachine> TM;
184 auto [OriginalProgram, InputIsBitcode] =
185 parseReducerWorkItem(ToolName, InputFilename, Context, TM, ReduceModeMIR);
186 if (!OriginalProgram) {
187 return 1;
190 StringRef OutputFilename;
191 bool OutputBitcode;
192 std::tie(OutputFilename, OutputBitcode) =
193 determineOutputType(ReduceModeMIR, InputIsBitcode);
195 // Initialize test environment
196 TestRunner Tester(TestFilename, TestArguments, std::move(OriginalProgram),
197 std::move(TM), ToolName, OutputFilename, InputIsBitcode,
198 OutputBitcode);
200 // This parses and writes out the testcase into a temporary file copy for the
201 // test, rather than evaluating the source IR directly. This is for the
202 // convenience of lit tests; the stripped out comments may have broken the
203 // interestingness checks.
204 if (!Tester.getProgram().isReduced(Tester)) {
205 errs() << "\nInput isn't interesting! Verify interesting-ness test\n";
206 return 1;
209 // Try to reduce code
210 runDeltaPasses(Tester, MaxPassIterations);
212 // Print reduced file to STDOUT
213 if (OutputFilename == "-")
214 Tester.getProgram().print(outs(), nullptr);
215 else
216 Tester.writeOutput("Done reducing! Reduced testcase: ");
218 return 0;