diff --git a/llvm/test/LLVMReduce/inputs/remove-funcs-test.sh b/llvm/test/LLVMReduce/inputs/remove-funcs-test.sh new file mode 100755 --- /dev/null +++ b/llvm/test/LLVMReduce/inputs/remove-funcs-test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# TODO: switch to python +ret=$(lli $1) + +if [[ $ret -eq 10 ]]; then + exit 0 +else + exit 1 +fi diff --git a/llvm/test/LLVMReduce/remove-functions.ll b/llvm/test/LLVMReduce/remove-functions.ll new file mode 100644 --- /dev/null +++ b/llvm/test/LLVMReduce/remove-functions.ll @@ -0,0 +1,42 @@ +; Test that bp2 can remove uninteresting functions (and its respective calls) +; +; RUN: llvm-reduce --test=remove-funcs-test.sh remove-functions.ll | FileCheck %s +; RUN: bash remove-funcs-test.sh reduced.bc +; REQUIRES: plugins +@.str = private unnamed_addr constant [4 x i8] c"%i\0A\00", align 1 + +define i32 @uninteresting1() { +entry: + ret i32 25 +} + +; CHECK: interesting() +define i32 @interesting() { +entry: + ret i32 10 +} + +; CHECK-NOT: uninteresting2() +define i32 @uninteresting2() { +entry: + ret i32 2000 +} + +; CHECK: main() +define i32 @main() { +entry: + %retval = alloca i32, align 4 + %number = alloca i32, align 4 + store i32 0, i32* %retval, align 4 + store i32 0, i32* %number, align 4 + %call = call i32 @uninteresting1() + %call1 = call i32 @interesting() + store i32 %call1, i32* %number, align 4 + %0 = load i32, i32* %number, align 4 + %call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %0) + %call3 = call i32 @uninteresting1() + %call4 = call i32 @uninteresting2() + ret i32 0 +} + +declare i32 @printf(i8*, ...) diff --git a/llvm/tools/LLVMBuild.txt b/llvm/tools/LLVMBuild.txt --- a/llvm/tools/LLVMBuild.txt +++ b/llvm/tools/LLVMBuild.txt @@ -48,6 +48,7 @@ llvm-pdbutil llvm-profdata llvm-rc + llvm-reduce llvm-rtdyld llvm-size llvm-split diff --git a/llvm/tools/llvm-reduce/CMakeLists.txt b/llvm/tools/llvm-reduce/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/CMakeLists.txt @@ -0,0 +1,35 @@ +set(LLVM_LINK_COMPONENTS + AllTargetsAsmParsers + AllTargetsCodeGens + AllTargetsDescs + AllTargetsInfos + Analysis + BitWriter + CodeGen + Core + IPO + IRReader + AggressiveInstCombine + InstCombine + Instrumentation + Linker + ObjCARCOpts + ScalarOpts + Support + Target + TransformUtils + Vectorize + ) + +# Support plugins. +set(LLVM_NO_DEAD_STRIP 1) + +add_llvm_tool(llvm-reduce + llvm-reduce.cpp + TestRunner.cpp + deltas/RemoveFunctions.cpp + + DEPENDS + intrinsics_gen + ) +export_executable_symbols(llvm-reduce) diff --git a/llvm/tools/llvm-reduce/DeltaManager.h b/llvm/tools/llvm-reduce/DeltaManager.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/DeltaManager.h @@ -0,0 +1,34 @@ +//===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This class calls each specialized Delta pass by passing it as a template to +// the generic Delta Pass. +// +//===----------------------------------------------------------------------===// + +#include "TestRunner.h" +#include "deltas/Delta.h" +#include "deltas/RemoveFunctions.h" +#include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/IR/Attributes.h" +#include "llvm/IR/Module.h" +#include "llvm/Pass.h" + + +namespace llvm { + +class DeltaManager { +public: + static void runDeltaPasses(TestRunner &Tester) { + outs() << "Reducing functions...\n"; + Delta::run(Tester); + // TODO: Implement the rest of the Delta Passes + } +}; + +} // namespace llvm diff --git a/llvm/tools/LLVMBuild.txt b/llvm/tools/llvm-reduce/LLVMBuild.txt copy from llvm/tools/LLVMBuild.txt copy to llvm/tools/llvm-reduce/LLVMBuild.txt --- a/llvm/tools/LLVMBuild.txt +++ b/llvm/tools/llvm-reduce/LLVMBuild.txt @@ -1,4 +1,4 @@ -;===- ./tools/LLVMBuild.txt ------------------------------------*- Conf -*--===; +;===- ./tools/llvm-reduce/LLVMBuild.txt ------------------------*- Conf -*--===; ; ; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. ; See https://llvm.org/LICENSE.txt for license information. @@ -14,48 +14,19 @@ ; ;===------------------------------------------------------------------------===; -[common] -subdirectories = - bugpoint - dsymutil - llc - lli - llvm-ar - llvm-as - llvm-bcanalyzer - llvm-cat - llvm-cfi-verify - llvm-cov - llvm-cvtres - llvm-diff - llvm-dis - llvm-dwarfdump - llvm-dwp - llvm-elfabi - llvm-exegesis - llvm-extract - llvm-jitlistener - llvm-jitlink - llvm-link - llvm-lto - llvm-mc - llvm-mca - llvm-modextract - llvm-mt - llvm-nm - llvm-objcopy - llvm-objdump - llvm-pdbutil - llvm-profdata - llvm-rc - llvm-rtdyld - llvm-size - llvm-split - llvm-undname - opt - verify-uselistorder - [component_0] -type = Group -name = Tools -parent = $ROOT +type = Tool +name = llvm-reduce +parent = Tools +required_libraries = + AsmParser + BitReader + BitWriter + CodeGen + IRReader + IPO + Instrumentation + Linker + ObjCARC + Scalar + all-targets diff --git a/llvm/tools/llvm-reduce/TestRunner.h b/llvm/tools/llvm-reduce/TestRunner.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/TestRunner.h @@ -0,0 +1,56 @@ +//===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains all the info necessary for running the provided +// interesting-ness test, as well as the most reduced module and its +// respective filename. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_LLVMREDUCE_TESTRUNNER_H +#define LLVM_TOOLS_LLVMREDUCE_TESTRUNNER_H + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/Program.h" +#include + +namespace llvm { + +class TestRunner { +public: + TestRunner(std::string TestFilename, std::vector TArgs, + std::string InputFilename, SmallString<128> CWD); + + /// Runs the interestingness test for the given filename + /// @returns 1 if the IR is interesting, 0 if otherwise. + int run(SmallString<128> Filename); + + /// Filename to the most reduced testcase + SmallString<128> getReducedFilename() { return ReducedFilename; } + /// Directory where tmp files are created + SmallString<128> getTmpDir() { return TmpDirectory; } + /// Returns the most reduced version of the original testcase + Module* getProgram() { return Program.get(); } + + void setReducedFilename(SmallString<128> F) { ReducedFilename = F; } + void setProgram(std::unique_ptr &P) { Program = std::move(P); } + +private: + SmallString<128> TestName; + std::vector TestArgs; + SmallString<128> ReducedFilename; + SmallString<128> TmpDirectory; + std::unique_ptr Program; +}; + +} // namespace llvm + +#endif diff --git a/llvm/tools/llvm-reduce/TestRunner.cpp b/llvm/tools/llvm-reduce/TestRunner.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/TestRunner.cpp @@ -0,0 +1,49 @@ +#include "TestRunner.h" + +using namespace llvm; + +/// RunProgramWithTimeout - This function provides an alternate interface +/// to the sys::Program::ExecuteAndWait interface. +/// @see sys::Program::ExecuteAndWait +int runProgramWithTimeout(StringRef ProgramPath, ArrayRef Args, + unsigned NumSeconds = 0, unsigned MemoryLimit = 0, + std::string *ErrMsg = nullptr) { + StringRef SR = ""; + Optional Redirects[3] = {SR, SR, SR}; + return sys::ExecuteAndWait(ProgramPath, Args, None, Redirects, NumSeconds, + MemoryLimit, ErrMsg); +} + +SmallString<128> append(SmallString<128> Path, std::string Filename) { + SmallString<128> Filepath = Path; + Filepath += "/" + Filename; + return Filepath; +} + +TestRunner::TestRunner(std::string TestFilename, std::vector TArgs, + std::string InputFilename, SmallString<128> CWD) + : TestName(append(CWD, TestFilename)), TestArgs(TArgs), + ReducedFilename(append(CWD, InputFilename)), + TmpDirectory(append(CWD, "tmp")) {} + + +/// Runs the interesting-ness test with the given arguments +int TestRunner::run(SmallString<128> Filename) { + std::vector ProgramArgs; + ProgramArgs.push_back(TestName.c_str()); + ProgramArgs.push_back(Filename.c_str()); + + for (auto Arg : TestArgs) + ProgramArgs.push_back(Arg.c_str()); + + int Result = runProgramWithTimeout(TestName, ProgramArgs); + + if (Result < 0) { + Error E = make_error("Error running interesting-ness test\n", + inconvertibleErrorCode()); + outs() << toString(std::move(E)); + exit(1); + } + + return !Result; +} diff --git a/llvm/tools/llvm-reduce/deltas/Chunk.h b/llvm/tools/llvm-reduce/deltas/Chunk.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/Chunk.h @@ -0,0 +1,31 @@ +//===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This Data type is used in the Delta Reduction Algorithm, contains the +// start and end index for the given chunk of Targets (i.e. Functions, Basic +// Blocks, GVs, etc.) +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_LLVMREDUCE_LLVMREDUCE_CHUNK_H +#define LLVM_TOOLS_LLVMREDUCE_LLVMREDUCE_CHUNK_H + +struct Chunk { + int begin; + int end; + + friend bool operator!=(const Chunk &C1, const Chunk &C2) { + return C1.begin != C2.begin && C1.end != C2.end; + } + + friend bool operator<(const Chunk& C1, const Chunk &C2) { + return C1.end < C2.begin; + } +}; + +#endif diff --git a/llvm/tools/llvm-reduce/deltas/Delta.h b/llvm/tools/llvm-reduce/deltas/Delta.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/Delta.h @@ -0,0 +1,148 @@ +//===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains the implementation for the Delta Debugging Algorithm: +// it splits a given set of Targets (i.e. Functions, Instructions, BBs, etc.) +// into chunks, and tests if a certain chunk is un-interesting (until chunk +// size cannot be reduced). +// +//===----------------------------------------------------------------------===// + +#include "../TestRunner.h" +#include "Chunk.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/Program.h" +#include "llvm/Support/ScopedPrinter.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +#include + +namespace llvm { + +/// Prints the Chunk Indexes with the following format: [start, end]. +inline void printChunks(std::vector Chunks, bool Oneline = false) { + for (auto C : Chunks) { + if (!Oneline) + outs() << '\t'; + outs() << "[" << C.begin; + if (C.end - C.begin != 0) + outs() << "," << C.end; + outs() << "]"; + if (!Oneline) + outs() << '\n'; + } +} + +/// Counts the amount of lines for a given file +inline unsigned getLines(SmallString<128> Filepath) { + unsigned Lines = 0; + std::string CurrLine; + std::ifstream FileStream(to_string(Filepath)); + + while (std::getline(FileStream, CurrLine)) + ++Lines; + + return Lines; +} + +/// Splits Chunks in half, if unable to split returns false. +inline bool increaseGranularity(std::vector &Chunks) { + outs() << "Increasing granularity..."; + std::vector NewChunks; + bool SplitOne = false; + + for (auto &C : Chunks) { + int Half = (C.begin + C.end) / 2; + if (C.end - C.begin == 0) + NewChunks.push_back(C); + else { + NewChunks.push_back({C.begin, Half}); + NewChunks.push_back({Half + 1, C.end}); + SplitOne = true; + } + } + if (SplitOne) { + Chunks = NewChunks; + outs() << "Success! New Chunks:\n"; + printChunks(Chunks); + } + return SplitOne; +} + +/// Returns the Filename (with extension) or a given path +inline std::string getFilename(SmallString<128> Filepath) { + return Filepath.substr(Filepath.rfind('/') + 1); +} + +template class Delta { +public: + /// Runs the Delta Debugging algorithm, splits the code into chunks and + /// reduces the amount of chunks that are considered interesting by the + /// given test. + static void run(TestRunner &Test) { + // Get the amount of Fuctions, GVs, Basic Blocks, etc. + int TargetCount = D::getTargetCount(Test.getProgram()); + std::vector Chunks = {{1, TargetCount}}; + std::vector CurrentChunks; + std::set UninterestingChunks; + + if (Test.run(Test.getReducedFilename())) + increaseGranularity(Chunks); + else { + outs() << "Error: input file isnt interesting\n"; + exit(1); + } + + do { + UninterestingChunks = {}; + for (int I = Chunks.size() - 1; I >= 0; --I) { + CurrentChunks = {}; + + for (auto C : Chunks) + if (!UninterestingChunks.count(C) && C != Chunks[I]) + CurrentChunks.push_back(C); + + outs() << "Testing with: "; + printChunks(CurrentChunks, /*Oneline=*/ true); + + // Call Delta Pass to Make Module with only Chunk Targets + SmallString<128> CurrentFilepath; + std::unique_ptr CurrentProgram = + D::prepareIR(CurrentChunks, Test, CurrentFilepath); + outs() << " | " << getFilename(CurrentFilepath); + // Current Chunks aren't interesting + if (!Test.run(CurrentFilepath)) { + outs() << "\n"; + continue; + } + // We only care about interesting chunks if they reduce the testcase + if (getLines(CurrentFilepath) < getLines(Test.getReducedFilename())) { + UninterestingChunks.insert(Chunks[I]); + Test.setReducedFilename(CurrentFilepath); + Test.setProgram(CurrentProgram); + outs() << " **** SUCCESS | lines: " << getLines(CurrentFilepath); + } + outs() << "\n"; + } + // Delete proven Uninteresting chunks + auto It = Chunks.begin(), E = Chunks.end(); + while (It != E) { + if (UninterestingChunks.count(*It)) + It = Chunks.erase(It); + else + ++It; + } + } while (!UninterestingChunks.empty() || increaseGranularity(Chunks)); + outs() << "Couldn't increase anymore.\n"; + } +}; + +} // namespace llvm diff --git a/llvm/tools/llvm-reduce/deltas/RemoveFunctions.h b/llvm/tools/llvm-reduce/deltas/RemoveFunctions.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/RemoveFunctions.h @@ -0,0 +1,38 @@ +//===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file is a Specialized Delta Pass, which removes the functions that are +// not in the provided function-chunks. +// +//===----------------------------------------------------------------------===// + +#include "../TestRunner.h" +#include "Chunk.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/ADT/SmallString.h" +#include +#include + +namespace llvm { + +class RemoveFunctions { +public: + /// Outputs the number of Functions in the given Module + static int getTargetCount(Module* P); + /// Deletes non-chunk Functions from module and writes the modified module + /// to a tmp file. + static std::unique_ptr prepareIR(std::vector ChunksToKeep, + TestRunner &Test, + SmallString<128> &CurrentFilepath); +}; + +} // namespace llvm + diff --git a/llvm/tools/llvm-reduce/deltas/RemoveFunctions.cpp b/llvm/tools/llvm-reduce/deltas/RemoveFunctions.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/RemoveFunctions.cpp @@ -0,0 +1,119 @@ +//===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file is a Specialized Delta Pass, which removes the functions that are +// not in the provided function-chunks. +// +//===----------------------------------------------------------------------===// + +#include "RemoveFunctions.h" + +using namespace llvm; + +/// Writes Bitcode to a unique file inside the "tmp" folder +bool writeProgramToFile(SmallString<128> Filename, int FD, + const Module &M) { + ToolOutputFile Out(Filename, FD); + M.print(Out.os(), /*AnnotationWriter=*/ nullptr, + /*ShouldPreserveUseListOrder=*/ true); + Out.os().close(); + + if (!Out.os().has_error()) { + Out.keep(); + return false; + } + return true; +} + +/// Goes over the Function Chunks, and removes all the Defined Functions that +/// aren't inside any given Chunk. Once stripped, it is written to an +/// IR file (which is tested by the generic Delta pass). +std::unique_ptr RemoveFunctions::prepareIR( + std::vector ChunksToKeep, + TestRunner &Test, + SmallString<128> &CurrentFilepath) { + std::unique_ptr Clone = CloneModule(*Test.getProgram()); + + // Get all functions inside desired chunks + std::set FuncsToKeep; + int I = 0, FunctionCount = 1; + for(auto &F : *Clone) { + if (!F.isDeclaration()) { + if (FunctionCount >= ChunksToKeep[I].begin && + FunctionCount <= ChunksToKeep[I].end) { + FuncsToKeep.insert(&F); + } + if (FunctionCount >= ChunksToKeep[I].end) + ++I; + ++FunctionCount; + } + } + + // Delete out-of-chunk functions, and replace their calls with null + std::vector FuncsToRemove; + for (Function &F : *Clone) { + if (!F.isDeclaration() && !FuncsToKeep.count(&F)) { + F.replaceAllUsesWith(UndefValue::get(F.getType())); + FuncsToRemove.push_back(&F); + } + } + + for (auto *F : FuncsToRemove) { + F->eraseFromParent(); + } + + // Clean up null calls + std::vector InstToRemove; + for(auto &F : *Clone) + for(auto &BB : F) + for(auto &I : BB) + if (auto *Call = dyn_cast(&I)) + if (!Call->getCalledFunction()) { + I.replaceAllUsesWith(UndefValue::get(I.getType())); + InstToRemove.push_back(&I); + } + + for (auto *I : InstToRemove) { + I->eraseFromParent(); + } + + // Write modified module to file + SmallString<128> UniqueFilepath; + SmallString<128> TmpDirectory = Test.getTmpDir(); + int UniqueFD; + std::error_code EC = sys::fs::createUniqueFile(TmpDirectory + "/tmp-%%%.ll", + UniqueFD, UniqueFilepath); + if (EC) { + errs() << "Error making unique filename: " << EC.message() << "!\n"; + exit(1); + } + + if (writeProgramToFile(UniqueFilepath, UniqueFD, *Clone)) { + errs() << "Error emitting bitcode to file '" << UniqueFilepath << "'!\n"; + exit(1); + } + CurrentFilepath = UniqueFilepath; + + return Clone; +} + +/// Counts the amount of non-declaration functions and displays their +/// respective index & name +int RemoveFunctions::getTargetCount(Module* P) { + // TODO: Silence index with --quiet flag + outs() << "----------------------------\n"; + outs() << "Chunk Index Reference:\n"; + int FunctionCount = 0; + for (auto &F : *P) + if (!F.isDeclaration()) { + ++FunctionCount; + outs() << "\t" << FunctionCount << ": " << F.getName() << "\n"; + } + outs() << "----------------------------\n"; + return FunctionCount; +} diff --git a/llvm/tools/llvm-reduce/llvm-reduce.cpp b/llvm/tools/llvm-reduce/llvm-reduce.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/llvm-reduce.cpp @@ -0,0 +1,107 @@ +//===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This program tries to reduce an IR test case for a given interesting-ness +// test. It runs multiple delta debugging passes. +// +//===----------------------------------------------------------------------===// + +#include "DeltaManager.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Verifier.h" +#include "llvm/IRReader/IRReader.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/Program.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/raw_ostream.h" +#include +#include + +using namespace llvm; + +static cl::opt InputFilename(cl::Positional, cl::Required, + cl::desc("")); + +static cl::opt + TestFilename("test", cl::Required, + cl::desc("Name of the interesting-ness test to be run")); + +static cl::list + TestArguments("test-arg", cl::ZeroOrMore, + cl::desc("Arguments passed onto the interesting-ness test")); + +static cl::opt + OutputFilename("output", + cl::desc("Filename of reduced file, if unset it's outputted" + "to STDOUT")); + +// Parses IR into a Module and verifies it +std::unique_ptr parseInputFile(StringRef Filename, LLVMContext &Ctxt) { + SMDiagnostic Err; + std::unique_ptr Result = parseIRFile(Filename, Err, Ctxt); + if (!Result) { + Err.print("llvm-reduce", errs()); + return Result; + } + + if (verifyModule(*Result, &errs())) { + errs() << "Error: " << Filename << " - input module is broken!\n"; + return std::unique_ptr(); + } + + return Result; +} + +void initializeTmpDirectory(SmallString<128> &CWD) { + if(std::error_code EC = sys::fs::current_path(CWD)) { + errs() << "Error getting current directory: " << EC.message() << "!\n"; + exit(1); + } + + SmallString<128> TmpDirectory = CWD; + TmpDirectory.append("/tmp"); + if(std::error_code EC = sys::fs::create_directory(TmpDirectory)) { + errs() << "Error creating tmp directory: " << EC.message() << "!\n"; + } +} + +int main(int argc, char **argv) { + InitLLVM X(argc, argv); + + cl::ParseCommandLineOptions(argc, argv, + "LLVM automatic testcase reducer. See\nhttp://" + "llvm.org/cmds/bugpoint.html" + " for more information.\n"); + + LLVMContext Context; + std::unique_ptr OriginalProgram = + parseInputFile(InputFilename, Context); + + SmallString<128> CWD; + initializeTmpDirectory(CWD); + + std::vector TestArgs(std::move(TestArguments)); + TestRunner Tester(TestFilename, TestArgs, InputFilename, CWD); + Tester.setProgram(OriginalProgram); + + DeltaManager::runDeltaPasses(Tester); + + if(OutputFilename != "") { + sys::fs::rename(Tester.getReducedFilename(), + CWD + "/" + OutputFilename + ".ll"); + } + else { + // TODO: Output to STDOUT + } + + return 0; +}