1 //===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===//
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
7 //===----------------------------------------------------------------------===//
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>
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
));
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
,
60 std::unique_ptr
<Module
> Result
= parseIRFile(Filename
, Err
, Ctxt
);
62 Err
.print("llvm-reduce", errs());
66 if (verifyModule(*Result
, &errs())) {
67 errs() << "Error: " << Filename
<< " - input module is broken!\n";
68 return std::unique_ptr
<Module
>();
74 int main(int argc
, char **argv
) {
75 InitLLVM
X(argc
, argv
);
77 cl::ParseCommandLineOptions(argc
, argv
, "LLVM automatic testcase reducer.\n");
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
));
88 runDeltaPasses(Tester
);
90 if (!Tester
.getProgram()) {
91 errs() << "\nCouldnt reduce input :/\n";
93 // Print reduced file to STDOUT
94 if (OutputFilename
== "-")
95 Tester
.getProgram()->print(outs(), nullptr);
97 if (ReplaceInput
) // In-place
98 OutputFilename
= InputFilename
.c_str();
99 else if (OutputFilename
.empty())
100 OutputFilename
= "reduced.ll";
103 raw_fd_ostream
Out(OutputFilename
, EC
);
105 errs() << "Error opening output file: " << EC
.message() << "!\n";
108 Tester
.getProgram()->print(Out
, /*AnnotationWriter=*/nullptr);
109 errs() << "\nDone reducing! Reduced testcase: " << OutputFilename
<< "\n";