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
, InputFilename
);
85 Tester
.setProgram(std::move(OriginalProgram
));
88 runDeltaPasses(Tester
);
89 StringRef ReducedFilename
= sys::path::filename(Tester
.getReducedFilepath());
91 if (ReducedFilename
== sys::path::filename(InputFilename
)) {
92 errs() << "\nCouldnt reduce input :/\n";
94 // Print reduced file to STDOUT
95 if (OutputFilename
== "-")
96 Tester
.getProgram()->print(outs(), nullptr);
98 if (ReplaceInput
) // In-place
99 OutputFilename
= InputFilename
.c_str();
100 else if (OutputFilename
.empty())
101 OutputFilename
= "reduced.ll";
103 OutputFilename
+= ".ll";
105 sys::fs::copy_file(Tester
.getReducedFilepath(), OutputFilename
);
106 errs() << "\nDone reducing! Reduced testcase: " << OutputFilename
<< "\n";