blob: 83dcf980a7860640d389f63d57249c09d586dbea [file] [log] [blame]
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +00001//===- 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.
14//
15//===----------------------------------------------------------------------===//
16
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>
28
29using namespace llvm;
30
31static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
32static cl::opt<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden);
33
34static cl::opt<std::string> InputFilename(cl::Positional, cl::Required,
35 cl::desc("<input llvm ll/bc file>"));
36
37static cl::opt<std::string>
38 TestFilename("test", cl::Required,
39 cl::desc("Name of the interesting-ness test to be run"));
40
41static cl::list<std::string>
42 TestArguments("test-arg", cl::ZeroOrMore,
43 cl::desc("Arguments passed onto the interesting-ness test"));
44
45static cl::opt<std::string>
46 OutputFilename("output",
47 cl::desc("Specify the output file. default: reduced.ll"));
48static cl::alias OutputFileAlias("o", cl::desc("Alias for -output"),
49 cl::aliasopt(OutputFilename));
50
51static cl::opt<bool>
52 ReplaceInput("in-place",
53 cl::desc("WARNING: This option will replace your input file"
54 "with the reduced version!"));
55
56// Parses IR into a Module and verifies it
57static 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;
64 }
65
66 if (verifyModule(*Result, &errs())) {
67 errs() << "Error: " << Filename << " - input module is broken!\n";
68 return std::unique_ptr<Module>();
69 }
70
71 return Result;
72}
73
74int main(int argc, char **argv) {
75 InitLLVM X(argc, argv);
76
77 cl::ParseCommandLineOptions(argc, argv, "LLVM automatic testcase reducer.\n");
78
79 LLVMContext Context;
80 std::unique_ptr<Module> OriginalProgram =
81 parseInputFile(InputFilename, Context);
82
83 // Initialize test environment
David Blaikieaaef97a2019-09-12 00:31:57 +000084 TestRunner Tester(TestFilename, TestArguments);
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000085 Tester.setProgram(std::move(OriginalProgram));
86
87 // Try to reduce code
88 runDeltaPasses(Tester);
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000089
David Blaikieaaef97a2019-09-12 00:31:57 +000090 if (!Tester.getProgram()) {
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +000091 errs() << "\nCouldnt reduce input :/\n";
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000092 } else {
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +000093 // 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";
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000101
David Blaikieaaef97a2019-09-12 00:31:57 +0000102 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);
107 }
108 Tester.getProgram()->print(Out, /*AnnotationWriter=*/nullptr);
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +0000109 errs() << "\nDone reducing! Reduced testcase: " << OutputFilename << "\n";
110 }
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000111 }
112
113 return 0;
114}