[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / tools / llvm-reduce / llvm-reduce.cpp
blob83dcf980a7860640d389f63d57249c09d586dbea
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 "llvm/ADT/SmallString.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/IRReader/IRReader.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <system_error>
27 #include <vector>
29 using namespace llvm;
31 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
32 static cl::opt<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden);
34 static cl::opt<std::string> InputFilename(cl::Positional, cl::Required,
35 cl::desc("<input llvm ll/bc file>"));
37 static cl::opt<std::string>
38 TestFilename("test", cl::Required,
39 cl::desc("Name of the interesting-ness test to be run"));
41 static cl::list<std::string>
42 TestArguments("test-arg", cl::ZeroOrMore,
43 cl::desc("Arguments passed onto the interesting-ness test"));
45 static cl::opt<std::string>
46 OutputFilename("output",
47 cl::desc("Specify the output file. default: reduced.ll"));
48 static cl::alias OutputFileAlias("o", cl::desc("Alias for -output"),
49 cl::aliasopt(OutputFilename));
51 static cl::opt<bool>
52 ReplaceInput("in-place",
53 cl::desc("WARNING: This option will replace your input file"
54 "with the reduced version!"));
56 // Parses IR into a Module and verifies it
57 static std::unique_ptr<Module> parseInputFile(StringRef Filename,
58 LLVMContext &Ctxt) {
59 SMDiagnostic Err;
60 std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
61 if (!Result) {
62 Err.print("llvm-reduce", errs());
63 return Result;
66 if (verifyModule(*Result, &errs())) {
67 errs() << "Error: " << Filename << " - input module is broken!\n";
68 return std::unique_ptr<Module>();
71 return Result;
74 int main(int argc, char **argv) {
75 InitLLVM X(argc, argv);
77 cl::ParseCommandLineOptions(argc, argv, "LLVM automatic testcase reducer.\n");
79 LLVMContext Context;
80 std::unique_ptr<Module> OriginalProgram =
81 parseInputFile(InputFilename, Context);
83 // Initialize test environment
84 TestRunner Tester(TestFilename, TestArguments);
85 Tester.setProgram(std::move(OriginalProgram));
87 // Try to reduce code
88 runDeltaPasses(Tester);
90 if (!Tester.getProgram()) {
91 errs() << "\nCouldnt reduce input :/\n";
92 } else {
93 // Print reduced file to STDOUT
94 if (OutputFilename == "-")
95 Tester.getProgram()->print(outs(), nullptr);
96 else {
97 if (ReplaceInput) // In-place
98 OutputFilename = InputFilename.c_str();
99 else if (OutputFilename.empty())
100 OutputFilename = "reduced.ll";
102 std::error_code EC;
103 raw_fd_ostream Out(OutputFilename, EC);
104 if (EC) {
105 errs() << "Error opening output file: " << EC.message() << "!\n";
106 exit(1);
108 Tester.getProgram()->print(Out, /*AnnotationWriter=*/nullptr);
109 errs() << "\nDone reducing! Reduced testcase: " << OutputFilename << "\n";
113 return 0;